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 ('{} {} {}, {}'
.format(insn.bop, ty2s(insn.ty),
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:
# TODO
print('Unknown insn: {}'

View File

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