Add icmp for some comparisons.

This commit is contained in:
cfreksen 2017-10-29 18:57:13 +01:00
parent 138eb3fc72
commit d7598a0039
No known key found for this signature in database
GPG Key ID: EAC13EE101008978
2 changed files with 29 additions and 2 deletions

4
ll.py
View File

@ -98,6 +98,10 @@ def insn2s(insn):
return ('{} {} {}, {}' return ('{} {} {}, {}'
.format(insn.bop, ty2s(insn.ty), .format(insn.bop, ty2s(insn.ty),
oper2s(insn.left), oper2s(insn.right))) oper2s(insn.left), oper2s(insn.right)))
elif isinstance(insn, Icmp):
return ('icmp {} {} {}, {}'
.format(insn.cnd, ty2s(insn.ty),
oper2s(insn.left), oper2s(insn.right)))
else: else:
# TODO # TODO
print('Unknown insn: {}' print('Unknown insn: {}'

View File

@ -1,14 +1,17 @@
import ll import ll
import parser import parser
def TODO(msg): def TODO(msg):
print('TODO, not implemented yet at {}' print('TODO, not implemented yet at {}'
.format(msg)) .format(msg))
def err(msg): def err(msg):
print('ERROR: {}' print('ERROR: {}'
.format(msg)) .format(msg))
def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, memory, def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, memory,
tdecs, fdecs, call_res): tdecs, fdecs, call_res):
if len(insns) == 0: if len(insns) == 0:
@ -33,6 +36,17 @@ def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, memory,
# TODO # TODO
print('{} {}, {}' print('{} {}, {}'
.format(bop, left_v, right_v)) .format(bop, left_v, right_v))
elif isinstance(next_insn, ll.Icmp):
cnd = next_insn.cnd
left = next_insn.left
right = next_insn.right
left_v = eval_oper(left, ssa_env, global_env)
right_v = eval_oper(right, ssa_env, global_env)
res = eval_icmp(cnd, left_v, right_v)
# TODO
print('icmp {} {}, {}'
.format(cnd, left_v, right_v))
else: else:
err('Unknown LLVM instruction: {}' err('Unknown LLVM instruction: {}'
.format(next_insn)) .format(next_insn))
@ -136,6 +150,15 @@ def eval_binop(bop, left, right):
.format(bop)) .format(bop))
def eval_icmp(cnd, left, right):
if cnd == 'eq':
return left == right
elif cnd == 'ne':
return left != right
else:
err('eval_icmp: Unknown cnd: {}'
.format(cnd))
return 0
def gogo(): def gogo():
p = parser.LLVMParser() p = parser.LLVMParser()
@ -146,8 +169,8 @@ define i64 @tigermain (i64 %U_mainSL_8, i64 %U_mainDummy_9) {
br label %L1 br label %L1
L1: L1:
%b = add i64 %a, %a %b = add i64 %a, %a
%c = add i64 %a, %b %c = icmp eq i64 %a, %b
ret i64 %c ret i1 %c
L2: L2:
%d = add i64 %a, 1 %d = add i64 %a, 1
%e = add i64 10, %b %e = add i64 10, %b