From a4885103c090d50c9ec272a04504a643dc5f786c Mon Sep 17 00:00:00 2001 From: cfreksen Date: Sun, 29 Oct 2017 18:42:38 +0100 Subject: [PATCH] Add unconditional branching. --- stepper.py | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/stepper.py b/stepper.py index 9339d51..4c95558 100644 --- a/stepper.py +++ b/stepper.py @@ -51,6 +51,11 @@ def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, memory, def terminate(terminator, blocks, stack_frames, ssa_env, global_env, memory): + def clear_block_from_ssa_env(insns, ssa_env): + for (id, insn) in insns: + if id is not None and id in ssa_env: + del ssa_env[id] + if isinstance(terminator, ll.Ret): oper = terminator.oper if oper is None: @@ -64,6 +69,22 @@ def terminate(terminator, blocks, stack_frames, ssa_env, global_env, memory): new_stack_frames = stack_frames[1:] return (new_insns, new_terminator, new_blocks, new_stack_frames, new_ssa_env, memory, oper_v) + elif isinstance(terminator, ll.Br): + label = terminator.label + next_block = blocks[label] + new_insns = next_block.insns + new_terminator = next_block.terminator + + # TODO: Might need to find a better solution as we will ignore + # multiple assignments, if they are spread over multiple + # blocks. + clear_block_from_ssa_env(new_insns, ssa_env) + + print('Jumping unconditionally to {}' + .format(label)) + + return (new_insns, new_terminator, blocks, stack_frames, + ssa_env, memory, None) else: err('Unknown LLVM terminator: {}' .format(terminator)) @@ -118,18 +139,19 @@ def gogo(): data = r''' define i64 @tigermain (i64 %U_mainSL_8, i64 %U_mainDummy_9) { %a = add i64 3, 5 ; please be 8 + br label %L1 +L1: %b = add i64 %a, %a %c = add i64 %a, %b - %d = add i64 100, %c - %e = mul i64 7, %b - %f = sdiv i64 %e, 2 - %g = sub i64 %d, %f - %h = or i64 1, %g - %i = shl i64 %h, 2 - ret i64 %g + ret i64 %c +L2: + %d = add i64 %a, 1 + %e = add i64 10, %b + ret i64 %e } - ''' +''' + print(data) ast = p.parse(data) tdecs = ast.tdecls