llvm--emulator/stepper.py

216 lines
6.0 KiB
Python
Raw Normal View History

2017-10-29 14:18:14 +00:00
import ll
import parser
2017-10-29 17:57:13 +00:00
2017-10-29 14:18:14 +00:00
def TODO(msg):
print('TODO, not implemented yet at {}'
.format(msg))
2017-10-29 17:57:13 +00:00
2017-10-29 14:18:14 +00:00
def err(msg):
print('ERROR: {}'
.format(msg))
2017-10-29 17:57:13 +00:00
2017-10-29 14:18:14 +00:00
def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, memory,
tdecs, fdecs, call_res):
if len(insns) == 0:
return terminate(terminator, blocks, stack_frames, ssa_env, global_env, memory)
ssa_target, next_insn = insns[0]
insns_rest = insns[1:]
# TODO
print('Evaluating {}'
.format(ll.insn2s(next_insn)))
res = None
if isinstance(next_insn, ll.Binop):
bop = next_insn.bop
ty = next_insn.ty
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_binop(bop, left_v, right_v)
# TODO
print('{} {}, {}'
.format(bop, left_v, right_v))
2017-10-29 17:57:13 +00:00
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))
2017-10-29 14:18:14 +00:00
else:
err('Unknown LLVM instruction: {}'
.format(next_insn))
if ssa_target is not None:
if ssa_target in ssa_env:
err('Cannot assign to variable twice: {}'
.format(ssa_target))
else:
# TODO
print('%{} <- {}'
.format(ssa_target, res))
ssa_env[ssa_target] = res
return insns_rest, terminator, blocks, stack_frames, ssa_env, memory, None
def terminate(terminator, blocks, stack_frames, ssa_env, global_env, memory):
2017-10-29 17:42:38 +00:00
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]
2017-10-29 14:18:14 +00:00
if isinstance(terminator, ll.Ret):
oper = terminator.oper
if oper is None:
oper_v = None
else:
oper_v = eval_oper(oper, ssa_env, global_env)
if len(stack_frames) == 0:
2017-10-29 17:42:56 +00:00
new_insns = []
new_terminator = None
new_blocks = {}
new_ssa_env = ssa_env
new_stack_frames = []
2017-10-29 14:18:14 +00:00
else:
new_insns, new_terminator, new_blocks, new_ssa_env = stack_frames[0]
new_stack_frames = stack_frames[1:]
2017-10-29 17:42:56 +00:00
return (new_insns, new_terminator, new_blocks, new_stack_frames,
new_ssa_env, memory, oper_v)
2017-10-29 17:42:38 +00:00
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)
2017-10-29 14:18:14 +00:00
else:
err('Unknown LLVM terminator: {}'
.format(terminator))
def eval_oper(operand, ssa_env, global_env):
if isinstance(operand, ll.Null):
return 0
elif isinstance(operand, ll.Const):
return operand.val
elif isinstance(operand, ll.Gid):
TODO('eval_oper Gid')
elif isinstance(operand, ll.Id):
id = operand.val
try:
return ssa_env[id]
except KeyError:
err('Unable to find %{} in environment:\n{}'
.format(id, ssa_env))
def eval_binop(bop, left, right):
if bop == 'add':
return left + right
2017-10-29 15:02:00 +00:00
elif bop == 'sub':
return left - right
elif bop == 'mul':
return left * right
elif bop == 'sdiv':
return left // right
elif bop == 'shl':
return left << right
elif bop == 'ashr':
return left >> right
elif bop == 'lshr':
return (left >> right) % 0x10000000000000000
elif bop == 'and':
return left & right
elif bop == 'or':
return left | right
elif bop == 'xor':
return left ^ right
2017-10-29 14:18:14 +00:00
else:
err('Unknown LLVM Binary operator: {}'
.format(bop))
2017-10-29 17:57:13 +00:00
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
2017-10-29 14:18:14 +00:00
def gogo():
p = parser.LLVMParser()
p.build()
data = r'''
define i64 @tigermain (i64 %U_mainSL_8, i64 %U_mainDummy_9) {
%a = add i64 3, 5 ; please be 8
2017-10-29 17:42:38 +00:00
br label %L1
L1:
2017-10-29 14:18:14 +00:00
%b = add i64 %a, %a
2017-10-29 17:57:13 +00:00
%c = icmp eq i64 %a, %b
ret i1 %c
2017-10-29 17:42:38 +00:00
L2:
%d = add i64 %a, 1
%e = add i64 10, %b
ret i64 %e
2017-10-29 14:18:14 +00:00
}
2017-10-29 17:42:38 +00:00
'''
2017-10-29 14:18:14 +00:00
2017-10-29 17:42:38 +00:00
print(data)
2017-10-29 14:18:14 +00:00
ast = p.parse(data)
tdecs = ast.tdecls
fdecs = ast.fdecls
global_env = ast.gdecls
tigermain = ast.fdecls['tigermain']
first_block = tigermain.body.first_block
blocks = tigermain.body.named_blocks
insns = first_block.insns
terminator = first_block.terminator
stack_frames = []
ssa_env = {}
# TODO: memory structure has not been decided yet
memory = [None]
call_res = None
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)
if terminator is None:
print('Stepping done! Final ssa_env:\n{}'
.format(ssa_env))
print('Program resulted in {}'.
format(call_res))
break
if __name__ == '__main__':
gogo()