From d7598a003944e8f5847e8eecf16b4190c04dabed Mon Sep 17 00:00:00 2001 From: cfreksen Date: Sun, 29 Oct 2017 18:57:13 +0100 Subject: [PATCH] Add icmp for some comparisons. --- ll.py | 4 ++++ stepper.py | 27 +++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/ll.py b/ll.py index acc63c5..1394609 100644 --- a/ll.py +++ b/ll.py @@ -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: {}' diff --git a/stepper.py b/stepper.py index ee4f774..acced2e 100644 --- a/stepper.py +++ b/stepper.py @@ -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