diff --git a/stepper.py b/stepper.py index 5d11eaa..1592f05 100644 --- a/stepper.py +++ b/stepper.py @@ -23,10 +23,10 @@ class Garbage(Enum): GARBAGE = '<>' -def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, memory, +def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, heap, tdecs, fdecs, call_res): if len(insns) == 0: - return terminate(terminator, blocks, stack_frames, ssa_env, global_env, memory, + return terminate(terminator, blocks, stack_frames, ssa_env, global_env, heap, call_res) ssa_target, next_insn = insns[0] insns_rest = insns[1:] @@ -56,9 +56,9 @@ def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, memory, print('alloca {} --> allocating {} cells' .format(ll.ty2s(base_ty), size)) - ptr = len(memory) + ptr = len(heap) for i in range(max(size, 1)): - memory.append(Garbage.GARBAGE) + heap.append(Garbage.GARBAGE) res = ptr elif isinstance(next_insn, ll.Load): @@ -83,7 +83,7 @@ def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, memory, if not isinstance(callee, ll.Gid): err('Cannot call anything but global identifiers: {}' .format(ll.oper2s(callee))) - return insns_rest, terminator, blocks, stack_frames, ssa_env, memory, call_res + return insns_rest, terminator, blocks, stack_frames, ssa_env, heap, call_res arguments_v = [eval_oper(oper, ssa_env, global_env) for ty, oper in arguments] @@ -93,7 +93,7 @@ def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, memory, except KeyError: err('Could not find function {} in environment:\n{}' .format(callee.val, fdecs.keys())) - return insns_rest, terminator, blocks, stack_frames, ssa_env, memory, call_res + return insns_rest, terminator, blocks, stack_frames, ssa_env, heap, call_res parameters = function.parameters print('call @{} ({})' @@ -105,10 +105,10 @@ def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, memory, child_blocks = function.body.named_blocks child_stack_frames = [(insns_rest, terminator, blocks, ssa_env)] + stack_frames child_ssa_env = {par[1]: arg for par, arg in zip(parameters, arguments_v)} - child_memory = memory + child_heap = heap child_call_res = [ssa_target] + call_res return (child_insns, child_terminator, child_blocks, child_stack_frames, - child_ssa_env, child_memory, child_call_res) + child_ssa_env, child_heap, child_call_res) elif isinstance(next_insn, ll.Bitcast): oper = next_insn.oper @@ -161,10 +161,10 @@ def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, memory, .format(ssa_target, res)) ssa_env[ssa_target] = res - return insns_rest, terminator, blocks, stack_frames, ssa_env, memory, call_res + return insns_rest, terminator, blocks, stack_frames, ssa_env, heap, call_res -def terminate(terminator, blocks, stack_frames, ssa_env, global_env, memory, call_res): +def terminate(terminator, blocks, stack_frames, ssa_env, global_env, heap, call_res): def clear_block_from_ssa_env(insns, ssa_env): for (id, insn) in insns: if id is not None and id in ssa_env: @@ -197,7 +197,7 @@ def terminate(terminator, blocks, stack_frames, ssa_env, global_env, memory, cal new_stack_frames = stack_frames[1:] new_call_res = call_res[1:] return (new_insns, new_terminator, new_blocks, new_stack_frames, - new_ssa_env, memory, new_call_res) + new_ssa_env, heap, new_call_res) elif isinstance(terminator, ll.Br): label = terminator.label next_block = blocks[label] @@ -214,7 +214,7 @@ def terminate(terminator, blocks, stack_frames, ssa_env, global_env, memory, cal .format(label)) return (new_insns, new_terminator, blocks, stack_frames, - ssa_env, memory, call_res) + ssa_env, heap, call_res) elif isinstance(terminator, ll.Cbr): ty = terminator.ty if ty != ll.SimpleType.I1: @@ -238,7 +238,7 @@ def terminate(terminator, blocks, stack_frames, ssa_env, global_env, memory, cal .format(operand_v, label)) return (new_insns, new_terminator, blocks, stack_frames, - ssa_env, memory, call_res) + ssa_env, heap, call_res) else: err('Unknown LLVM terminator: {}' .format(terminator)) @@ -353,15 +353,15 @@ define i64 @tigermain (i64 %U_mainSL_8, i64 %U_mainDummy_9) { stack_frames = [] ssa_env = {} # TODO: memory structure has not been decided yet - memory = [None] + heap = [None] call_res = [] while True: (insns, terminator, blocks, - stack_frames, ssa_env, memory, call_res) = step(insns, terminator, blocks, - stack_frames, ssa_env, - global_env, memory, tdecs, - fdecs, call_res) + stack_frames, ssa_env, heap, call_res) = step(insns, terminator, blocks, + stack_frames, ssa_env, + global_env, heap, tdecs, + fdecs, call_res) if terminator is None: print('Stepping done! Final ssa_env:\n{}'