2017-10-29 20:10:22 +00:00
|
|
|
from enum import Enum
|
|
|
|
|
2017-10-31 18:52:53 +00:00
|
|
|
from llvm_emulator import ll
|
2017-10-30 01:31:29 +00:00
|
|
|
|
2017-11-13 17:05:44 +00:00
|
|
|
import sys
|
2017-10-29 17:57:13 +00:00
|
|
|
|
2017-11-13 17:05:44 +00:00
|
|
|
############
|
|
|
|
# Messages #
|
|
|
|
|
|
|
|
PRINT_LEVEL_NONE = 0
|
|
|
|
PRINT_LEVEL_ERR = 1
|
|
|
|
PRINT_LEVEL_WARN = 2
|
|
|
|
PRINT_LEVEL_INFO = 3
|
|
|
|
|
|
|
|
PRINT_LEVEL = PRINT_LEVEL_NONE
|
|
|
|
|
|
|
|
###
|
2017-10-29 14:18:14 +00:00
|
|
|
|
2017-11-13 17:05:44 +00:00
|
|
|
def eprint(*args, **kwargs):
|
|
|
|
print(*args, file=sys.stderr, **kwargs)
|
2017-10-29 17:57:13 +00:00
|
|
|
|
2017-10-29 14:18:14 +00:00
|
|
|
def err(msg):
|
2017-11-13 17:05:44 +00:00
|
|
|
if PRINT_LEVEL >= PRINT_LEVEL_ERR:
|
|
|
|
eprint('ERROR: {}'
|
|
|
|
.format(msg))
|
2017-10-29 14:18:14 +00:00
|
|
|
|
2017-11-13 17:05:44 +00:00
|
|
|
def TODO(msg):
|
|
|
|
err('TODO: not implemented yet at {}'
|
|
|
|
.format(msg))
|
2017-10-29 17:57:13 +00:00
|
|
|
|
2017-10-29 18:28:19 +00:00
|
|
|
def warn(msg):
|
2017-11-13 17:05:44 +00:00
|
|
|
if PRINT_LEVEL >= PRINT_LEVEL_WARN:
|
|
|
|
eprint('WARNING: {}'
|
|
|
|
.format(msg))
|
|
|
|
|
|
|
|
def info(*args, **kwargs):
|
|
|
|
if PRINT_LEVEL >= PRINT_LEVEL_INFO:
|
|
|
|
print(*args, **kwargs)
|
2017-10-29 18:28:19 +00:00
|
|
|
|
2017-11-13 17:05:44 +00:00
|
|
|
##########
|
|
|
|
# Stuff? #
|
2017-10-29 18:28:19 +00:00
|
|
|
|
2017-10-29 20:10:22 +00:00
|
|
|
class Garbage(Enum):
|
|
|
|
GARBAGE = '<<Unitialized memory>>'
|
2017-10-29 20:08:14 +00:00
|
|
|
|
2017-10-29 23:00:35 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return '<<Garbage>>'
|
|
|
|
|
2017-10-29 20:08:14 +00:00
|
|
|
|
2017-11-09 14:32:29 +00:00
|
|
|
builtins = ['allocRecord', 'initArray', 'stringEqual', 'stringNotEq', 'stringLess',
|
|
|
|
'stringLessEq', 'stringGreater', 'stringGreaterEq', 'exponent', 'print',
|
|
|
|
'flush', 'getChar', 'ord', 'chr', 'size', 'substring', 'concat', 'not',
|
|
|
|
'exit_tig']
|
2017-11-09 13:28:02 +00:00
|
|
|
|
|
|
|
|
2017-10-29 20:21:25 +00:00
|
|
|
def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, heap,
|
2017-10-29 14:18:14 +00:00
|
|
|
tdecs, fdecs, call_res):
|
|
|
|
if len(insns) == 0:
|
2017-10-29 20:21:25 +00:00
|
|
|
return terminate(terminator, blocks, stack_frames, ssa_env, global_env, heap,
|
2017-10-29 19:39:58 +00:00
|
|
|
call_res)
|
2017-10-29 14:18:14 +00:00
|
|
|
ssa_target, next_insn = insns[0]
|
|
|
|
insns_rest = insns[1:]
|
|
|
|
|
2017-11-09 13:28:02 +00:00
|
|
|
def store_in_ssa(res, insns_rest, terminator, blocks, stack_frames, ssa_env, heap,
|
|
|
|
call_res):
|
|
|
|
if ssa_target is not None:
|
|
|
|
if ssa_target in ssa_env:
|
|
|
|
err('Cannot assign to variable twice: {}'
|
|
|
|
.format(ssa_target))
|
|
|
|
elif res is None:
|
|
|
|
err('Cannot assign empty value to %{}'
|
|
|
|
.format(ssa_target))
|
|
|
|
else:
|
|
|
|
# TODO
|
2017-11-13 17:05:44 +00:00
|
|
|
info('%{} <- {}'
|
2017-11-09 13:28:02 +00:00
|
|
|
.format(ssa_target, res))
|
|
|
|
ssa_env[ssa_target] = res
|
|
|
|
|
|
|
|
return insns_rest, terminator, blocks, stack_frames, ssa_env, heap, call_res
|
|
|
|
|
2017-10-29 14:18:14 +00:00
|
|
|
# TODO
|
2017-11-13 17:05:44 +00:00
|
|
|
info('Evaluating {}'
|
2017-10-29 14:18:14 +00:00
|
|
|
.format(ll.insn2s(next_insn)))
|
|
|
|
|
|
|
|
res = None
|
|
|
|
if isinstance(next_insn, ll.Binop):
|
|
|
|
bop = next_insn.bop
|
|
|
|
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
|
2017-11-13 17:05:44 +00:00
|
|
|
info('{} {}, {}'
|
2017-10-29 14:18:14 +00:00
|
|
|
.format(bop, left_v, right_v))
|
2017-10-29 19:44:17 +00:00
|
|
|
elif isinstance(next_insn, ll.Alloca):
|
2017-10-29 20:08:14 +00:00
|
|
|
ty = next_insn.ty
|
|
|
|
base_ty = ty2base_ty(ty, tdecs)
|
|
|
|
size = base_ty2size(base_ty)
|
|
|
|
|
|
|
|
# TODO
|
2017-11-13 17:05:44 +00:00
|
|
|
info('alloca {} --> allocating {} cells'
|
2017-10-29 20:08:14 +00:00
|
|
|
.format(ll.ty2s(base_ty), size))
|
|
|
|
|
2017-10-29 20:21:25 +00:00
|
|
|
ptr = len(heap)
|
2017-10-29 20:08:14 +00:00
|
|
|
for i in range(max(size, 1)):
|
2017-10-29 20:21:25 +00:00
|
|
|
heap.append(Garbage.GARBAGE)
|
2017-10-29 20:10:22 +00:00
|
|
|
|
2017-10-29 20:08:14 +00:00
|
|
|
res = ptr
|
2017-10-29 19:44:17 +00:00
|
|
|
elif isinstance(next_insn, ll.Load):
|
2017-10-29 20:40:26 +00:00
|
|
|
ty = next_insn.ty
|
|
|
|
base_ty = ty2base_ty(ty, tdecs)
|
|
|
|
size = base_ty2size(base_ty)
|
|
|
|
location = next_insn.location
|
|
|
|
location_v = eval_oper(location, ssa_env, global_env)
|
|
|
|
|
|
|
|
# TODO
|
2017-11-13 17:05:44 +00:00
|
|
|
info('load heap[{}]'
|
2017-10-29 20:40:26 +00:00
|
|
|
.format(location_v))
|
|
|
|
|
|
|
|
if size != 1:
|
|
|
|
err(('This emulator cannot load objects larger than 1 cell.'
|
|
|
|
' Current size is {}')
|
|
|
|
.format(size))
|
|
|
|
|
2017-10-29 21:03:31 +00:00
|
|
|
if location_v == 0:
|
|
|
|
err('You are not allowed to read from location 0')
|
|
|
|
res = 0
|
|
|
|
else:
|
|
|
|
res = heap[location_v]
|
2017-10-29 19:44:17 +00:00
|
|
|
elif isinstance(next_insn, ll.Store):
|
2017-10-29 20:23:57 +00:00
|
|
|
ty = next_insn.ty
|
2017-10-29 20:33:57 +00:00
|
|
|
base_ty = ty2base_ty(ty, tdecs)
|
|
|
|
size = base_ty2size(base_ty)
|
2017-10-29 20:23:57 +00:00
|
|
|
value = next_insn.value
|
|
|
|
location = next_insn.location
|
|
|
|
|
|
|
|
value_v = eval_oper(value, ssa_env, global_env)
|
|
|
|
location_v = eval_oper(location, ssa_env, global_env)
|
|
|
|
|
|
|
|
# TODO
|
2017-11-13 17:05:44 +00:00
|
|
|
info('heap[{}] <- {}'
|
2017-10-29 20:23:57 +00:00
|
|
|
.format(location_v, value_v))
|
2017-10-29 21:03:31 +00:00
|
|
|
if location_v == 0:
|
|
|
|
err('You are not allowed to store at location 0 (Null)')
|
|
|
|
elif size == 1:
|
2017-10-29 20:33:57 +00:00
|
|
|
heap[location_v] = value_v
|
|
|
|
else:
|
|
|
|
err(('This emulator cannot store objects larger than 1 cell.'
|
|
|
|
' Current size is {}')
|
|
|
|
.format(size))
|
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
|
2017-11-13 17:05:44 +00:00
|
|
|
info('icmp {} {}, {}'
|
2017-10-29 17:57:13 +00:00
|
|
|
.format(cnd, left_v, right_v))
|
2017-10-29 19:39:58 +00:00
|
|
|
elif isinstance(next_insn, ll.Call):
|
|
|
|
callee = next_insn.callee
|
|
|
|
arguments = next_insn.arguments
|
|
|
|
|
|
|
|
if not isinstance(callee, ll.Gid):
|
|
|
|
err('Cannot call anything but global identifiers: {}'
|
|
|
|
.format(ll.oper2s(callee)))
|
2017-10-29 20:21:25 +00:00
|
|
|
return insns_rest, terminator, blocks, stack_frames, ssa_env, heap, call_res
|
2017-10-29 19:39:58 +00:00
|
|
|
|
|
|
|
arguments_v = [eval_oper(oper, ssa_env, global_env)
|
|
|
|
for ty, oper in arguments]
|
|
|
|
|
2017-11-09 13:28:02 +00:00
|
|
|
if callee.val in fdecs:
|
2017-10-29 19:39:58 +00:00
|
|
|
function = fdecs[callee.val]
|
2017-11-09 13:28:02 +00:00
|
|
|
elif callee.val in builtins:
|
|
|
|
res, new_heap, should_exit = emulate_builtin(callee.val, arguments_v, ssa_env,
|
|
|
|
heap)
|
|
|
|
if should_exit:
|
|
|
|
new_insns = [(None, ll.CallResult(res))]
|
|
|
|
new_terminator = None
|
|
|
|
new_blocks = {}
|
|
|
|
new_ssa_env = ssa_env
|
|
|
|
new_stack_frames = []
|
|
|
|
new_call_res = []
|
|
|
|
return (new_insns, new_terminator, new_blocks, new_stack_frames,
|
|
|
|
new_ssa_env, heap, new_call_res)
|
|
|
|
else:
|
|
|
|
return store_in_ssa(res, insns_rest, terminator, blocks, stack_frames,
|
|
|
|
ssa_env, new_heap, call_res)
|
|
|
|
else:
|
2017-10-29 19:39:58 +00:00
|
|
|
err('Could not find function {} in environment:\n{}'
|
2017-11-09 13:28:02 +00:00
|
|
|
.format(callee.val, list(fdecs.keys()) + builtins))
|
2017-10-29 20:21:25 +00:00
|
|
|
return insns_rest, terminator, blocks, stack_frames, ssa_env, heap, call_res
|
2017-10-29 19:39:58 +00:00
|
|
|
|
|
|
|
parameters = function.parameters
|
2017-11-13 17:05:44 +00:00
|
|
|
info('call @{} ({})'
|
2017-10-29 19:39:58 +00:00
|
|
|
.format(callee.val,
|
|
|
|
', '.join('%{} <- {}'.format(par[1], arg)
|
|
|
|
for par, arg in zip(parameters, arguments_v))))
|
|
|
|
child_insns = function.body.first_block.insns
|
|
|
|
child_terminator = function.body.first_block.terminator
|
|
|
|
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)}
|
2017-10-29 20:21:25 +00:00
|
|
|
child_heap = heap
|
2017-10-29 19:39:58 +00:00
|
|
|
child_call_res = [ssa_target] + call_res
|
|
|
|
return (child_insns, child_terminator, child_blocks, child_stack_frames,
|
2017-10-29 20:21:25 +00:00
|
|
|
child_ssa_env, child_heap, child_call_res)
|
2017-10-29 19:39:58 +00:00
|
|
|
|
2017-10-29 18:41:40 +00:00
|
|
|
elif isinstance(next_insn, ll.Bitcast):
|
|
|
|
oper = next_insn.oper
|
|
|
|
from_ty = next_insn.from_ty
|
|
|
|
to_ty = next_insn.to_ty
|
|
|
|
oper_v = eval_oper(oper, ssa_env, global_env)
|
|
|
|
res = oper_v
|
|
|
|
|
|
|
|
# TODO
|
2017-11-13 17:05:44 +00:00
|
|
|
info('bitcast {} {} to {}'
|
2017-10-29 18:41:40 +00:00
|
|
|
.format(ll.ty2s(from_ty), oper_v, ll.ty2s(to_ty)))
|
2017-10-29 19:44:17 +00:00
|
|
|
elif isinstance(next_insn, ll.Gep):
|
2017-10-29 23:00:56 +00:00
|
|
|
res = 0
|
|
|
|
base_ty = next_insn.base_ty
|
|
|
|
oper_ty = next_insn.oper_ty
|
|
|
|
oper = next_insn.oper
|
|
|
|
steps = next_insn.steps
|
|
|
|
|
|
|
|
actual_base_ty = ty2base_ty(base_ty, tdecs)
|
|
|
|
actual_oper_ty = ty2base_ty(oper_ty, tdecs)
|
|
|
|
|
|
|
|
if not isinstance(actual_oper_ty, ll.PointerType):
|
|
|
|
err('Type of main operand to getelementptr must be a pointer type. It was {}'
|
|
|
|
.format(ll.ty2s(actual_oper_ty)))
|
|
|
|
else:
|
2017-11-09 17:23:21 +00:00
|
|
|
oper_inner_ty = ty2base_ty(actual_oper_ty.inner_ty, tdecs)
|
|
|
|
if actual_base_ty != oper_inner_ty:
|
|
|
|
warn(('Type of the main operand might not match the type getelementptr'
|
|
|
|
' navigates through.\n'
|
|
|
|
' Getelementptr type: {}\n'
|
|
|
|
' Operand type: {}')
|
|
|
|
.format(ll.ty2s(actual_base_ty), ll.ty2s(oper_inner_ty)))
|
|
|
|
|
2017-10-29 23:00:56 +00:00
|
|
|
oper_v = eval_oper(oper, ssa_env, global_env)
|
|
|
|
gep_res, formula = handle_gep(oper_v, actual_base_ty, steps, ssa_env,
|
|
|
|
global_env)
|
|
|
|
res = gep_res
|
|
|
|
|
|
|
|
# TODO
|
2017-11-13 17:05:44 +00:00
|
|
|
info('Getting adress of {}{}'.
|
2017-11-09 17:23:21 +00:00
|
|
|
format(ll.ty2s(actual_base_ty),
|
|
|
|
''.join('[{}]'
|
|
|
|
.format(eval_oper(step_oper, ssa_env, global_env))
|
|
|
|
for _, step_oper in steps)))
|
2017-11-13 17:05:44 +00:00
|
|
|
info('Gep formula: {}'
|
2017-10-29 23:00:56 +00:00
|
|
|
.format(formula))
|
2017-10-29 18:39:09 +00:00
|
|
|
elif isinstance(next_insn, ll.Zext):
|
|
|
|
oper = next_insn.oper
|
|
|
|
from_ty = next_insn.from_ty
|
|
|
|
to_ty = next_insn.to_ty
|
|
|
|
oper_v = eval_oper(oper, ssa_env, global_env)
|
|
|
|
res = oper_v
|
|
|
|
|
|
|
|
# TODO
|
2017-11-13 17:05:44 +00:00
|
|
|
info('zext {} {} to {}'
|
2017-10-29 18:39:09 +00:00
|
|
|
.format(ll.ty2s(from_ty), oper_v, ll.ty2s(to_ty)))
|
|
|
|
elif isinstance(next_insn, ll.Ptrtoint):
|
|
|
|
oper = next_insn.oper
|
|
|
|
pointer_ty = next_insn.pointer_ty
|
|
|
|
to_ty = next_insn.to_ty
|
|
|
|
oper_v = eval_oper(oper, ssa_env, global_env)
|
|
|
|
res = oper_v
|
|
|
|
|
|
|
|
# TODO
|
2017-11-13 17:05:44 +00:00
|
|
|
info('ptrtoint {}* {} to {}'
|
2017-10-29 18:39:09 +00:00
|
|
|
.format(ll.ty2s(pointer_ty), oper_v, ll.ty2s(to_ty)))
|
2017-10-29 19:39:58 +00:00
|
|
|
elif isinstance(next_insn, ll.CallResult):
|
|
|
|
res = next_insn.val
|
2017-10-29 14:18:14 +00:00
|
|
|
else:
|
|
|
|
err('Unknown LLVM instruction: {}'
|
|
|
|
.format(next_insn))
|
|
|
|
|
2017-11-09 13:28:02 +00:00
|
|
|
return store_in_ssa(res, insns_rest, terminator, blocks, stack_frames, ssa_env, heap,
|
|
|
|
call_res)
|
2017-10-29 14:18:14 +00:00
|
|
|
|
|
|
|
|
2017-10-29 20:21:25 +00:00
|
|
|
def terminate(terminator, blocks, stack_frames, ssa_env, global_env, heap, call_res):
|
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-11-13 17:05:44 +00:00
|
|
|
info('Evaluating {}'
|
2017-10-29 18:12:30 +00:00
|
|
|
.format(ll.terminator2s(terminator)))
|
|
|
|
|
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)
|
2017-10-29 18:12:30 +00:00
|
|
|
|
|
|
|
# TODO
|
2017-11-13 17:05:44 +00:00
|
|
|
info('Returning {}'
|
2017-10-29 18:12:30 +00:00
|
|
|
.format(oper_v))
|
2017-10-29 19:39:58 +00:00
|
|
|
|
2017-10-29 14:18:14 +00:00
|
|
|
if len(stack_frames) == 0:
|
2017-10-29 19:39:58 +00:00
|
|
|
new_insns = [(None, ll.CallResult(oper_v))]
|
2017-10-29 17:42:56 +00:00
|
|
|
new_terminator = None
|
|
|
|
new_blocks = {}
|
|
|
|
new_ssa_env = ssa_env
|
|
|
|
new_stack_frames = []
|
2017-10-29 19:39:58 +00:00
|
|
|
new_call_res = []
|
2017-10-29 14:18:14 +00:00
|
|
|
else:
|
|
|
|
new_insns, new_terminator, new_blocks, new_ssa_env = stack_frames[0]
|
2017-10-29 19:39:58 +00:00
|
|
|
new_insns = [(call_res[0], ll.CallResult(oper_v))] + new_insns
|
2017-10-29 14:18:14 +00:00
|
|
|
new_stack_frames = stack_frames[1:]
|
2017-10-29 19:39:58 +00:00
|
|
|
new_call_res = call_res[1:]
|
2017-10-29 17:42:56 +00:00
|
|
|
return (new_insns, new_terminator, new_blocks, new_stack_frames,
|
2017-10-29 20:21:25 +00:00
|
|
|
new_ssa_env, heap, new_call_res)
|
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)
|
|
|
|
|
2017-10-29 18:12:30 +00:00
|
|
|
# TODO
|
2017-11-13 17:05:44 +00:00
|
|
|
info('Jumping unconditionally to {}'
|
2017-10-29 17:42:38 +00:00
|
|
|
.format(label))
|
|
|
|
|
2017-10-29 18:28:19 +00:00
|
|
|
return (new_insns, new_terminator, blocks, stack_frames,
|
2017-10-29 20:21:25 +00:00
|
|
|
ssa_env, heap, call_res)
|
2017-10-29 18:28:19 +00:00
|
|
|
elif isinstance(terminator, ll.Cbr):
|
|
|
|
ty = terminator.ty
|
|
|
|
if ty != ll.SimpleType.I1:
|
|
|
|
warn('Branching based on value of type {}. You ought to branch on {}'
|
|
|
|
.format(ll.ty2s(ty), ll.ty2s(ll.SimpleType.I1)))
|
|
|
|
operand = terminator.oper
|
|
|
|
operand_v = eval_oper(operand, ssa_env, global_env)
|
|
|
|
|
|
|
|
if operand_v:
|
|
|
|
label = terminator.then_label
|
|
|
|
else:
|
|
|
|
label = terminator.else_label
|
|
|
|
|
|
|
|
next_block = blocks[label]
|
|
|
|
new_insns = next_block.insns
|
|
|
|
new_terminator = next_block.terminator
|
|
|
|
clear_block_from_ssa_env(new_insns, ssa_env)
|
|
|
|
|
|
|
|
# TODO
|
2017-11-13 17:05:44 +00:00
|
|
|
info('Operand was {}. Branching to {}'
|
2017-10-29 18:28:19 +00:00
|
|
|
.format(operand_v, label))
|
|
|
|
|
2017-10-29 17:42:38 +00:00
|
|
|
return (new_insns, new_terminator, blocks, stack_frames,
|
2017-10-29 20:21:25 +00:00
|
|
|
ssa_env, heap, call_res)
|
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):
|
2017-10-30 00:09:36 +00:00
|
|
|
gid = operand.val
|
|
|
|
try:
|
|
|
|
return global_env[gid]
|
|
|
|
except KeyError:
|
|
|
|
err('Unable to find @{} in environment:\n{}'
|
|
|
|
.format(global_env))
|
|
|
|
return 0
|
2017-10-29 14:18:14 +00:00
|
|
|
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))
|
2017-10-30 00:09:36 +00:00
|
|
|
return 0
|
2017-11-09 17:23:21 +00:00
|
|
|
else:
|
|
|
|
err('Unknown operand in eval_oper: {}'
|
|
|
|
.format(operand))
|
2017-10-29 14:18:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2017-10-29 18:00:10 +00:00
|
|
|
elif cnd == 'slt':
|
|
|
|
return left < right
|
|
|
|
elif cnd == 'sle':
|
|
|
|
return left <= right
|
|
|
|
elif cnd == 'sgt':
|
|
|
|
return left > right
|
|
|
|
elif cnd == 'sge':
|
|
|
|
return left >= right
|
2017-10-29 17:57:13 +00:00
|
|
|
else:
|
|
|
|
err('eval_icmp: Unknown cnd: {}'
|
|
|
|
.format(cnd))
|
|
|
|
return 0
|
2017-10-29 14:18:14 +00:00
|
|
|
|
2017-10-29 18:01:29 +00:00
|
|
|
|
2017-10-29 23:00:56 +00:00
|
|
|
def handle_gep(starting_location, starting_type, starting_steps, ssa_env, global_env):
|
|
|
|
def visit(current_location, current_type, current_steps, current_formula):
|
|
|
|
if len(current_steps) == 0:
|
|
|
|
return current_location, current_formula
|
|
|
|
else:
|
|
|
|
s_ty, s_oper = current_steps[0]
|
|
|
|
s_oper_v = eval_oper(s_oper, ssa_env, global_env)
|
|
|
|
next_steps = current_steps[1:]
|
|
|
|
if isinstance(current_type, ll.StructType):
|
|
|
|
if not isinstance(s_oper, ll.Const):
|
|
|
|
err('Index into struct must be a constant. It was: {}'
|
|
|
|
.format(ll.oper2s(s_oper)))
|
|
|
|
return current_location, current_formula + ' + ???'
|
|
|
|
jumps = [base_ty2size(current_type.fields[i])
|
|
|
|
for i in range(s_oper_v)]
|
|
|
|
next_location = current_location + sum(jumps)
|
|
|
|
next_type = current_type.fields[s_oper_v]
|
|
|
|
if len(jumps) > 0:
|
|
|
|
next_formula = ('{} + ({})'
|
|
|
|
.format(current_formula,
|
|
|
|
' + '.join(map(str, jumps))))
|
|
|
|
else:
|
|
|
|
next_formula = ('{} + 0'
|
|
|
|
.format(current_formula))
|
|
|
|
|
|
|
|
return visit(next_location, next_type, next_steps, next_formula)
|
|
|
|
elif isinstance(current_type, ll.ArrayType):
|
|
|
|
TODO('gep arrays')
|
|
|
|
return current_location, current_formula + ' + ???'
|
|
|
|
elif isinstance(current_type, ll.PointerType):
|
|
|
|
err(('Cannot use getelementptr to traverse pointers.'
|
|
|
|
' Use Load, and getelementptr on the result from that'
|
|
|
|
' to go through a pointer.'))
|
|
|
|
return current_location, current_formula + ' + ???'
|
|
|
|
else:
|
|
|
|
err('Unknown type to getelementptr on: {}'
|
|
|
|
.format(ll.ty2s(current_type)))
|
|
|
|
return current_location, current_formula + ' + ???'
|
|
|
|
|
|
|
|
if len(starting_steps) == 0:
|
|
|
|
err('There must be at least one stepping argument to a getelementptr instruction')
|
|
|
|
return 0, ''
|
|
|
|
s_ty, s_oper = starting_steps[0]
|
|
|
|
s_oper_v = eval_oper(s_oper, ssa_env, global_env)
|
|
|
|
size = base_ty2size(starting_type)
|
|
|
|
next_location = starting_location + s_oper_v * size
|
|
|
|
formula = ('{} + {} * {}'
|
|
|
|
.format(starting_location, s_oper_v, size))
|
|
|
|
return visit(next_location, starting_type, starting_steps[1:], formula)
|
|
|
|
|
|
|
|
|
2017-10-29 21:00:18 +00:00
|
|
|
def ty2base_ty(ty, tdecs, seen=[]):
|
2017-10-29 20:08:14 +00:00
|
|
|
if isinstance(ty, ll.SimpleType):
|
|
|
|
return ty
|
|
|
|
elif isinstance(ty, ll.PointerType):
|
2017-10-29 23:00:35 +00:00
|
|
|
# TODO: Consider if types behind pointers should not be
|
|
|
|
# expanded. They might be allowed for cyclic types
|
2017-11-09 17:23:21 +00:00
|
|
|
return ty
|
2017-10-29 21:00:18 +00:00
|
|
|
if isinstance(ty, ll.StructType):
|
|
|
|
return ll.StructType([ty2base_ty(t, tdecs, seen)
|
|
|
|
for t in ty.fields])
|
|
|
|
if isinstance(ty, ll.NamedType):
|
|
|
|
other_name = ty.other_name
|
|
|
|
if other_name in seen:
|
|
|
|
err('Cyclic type definition, offender: {}. Seen: {}'
|
|
|
|
.format(other_name, seen))
|
|
|
|
elif other_name in tdecs:
|
|
|
|
return ty2base_ty(tdecs[other_name].body, tdecs, [other_name] + seen)
|
|
|
|
else:
|
2017-10-30 16:09:43 +00:00
|
|
|
err('Could not find type {} in global type environment:\n{}'
|
|
|
|
.format(ll.ty2s(ty), list(tdecs.keys())))
|
2017-10-29 21:00:18 +00:00
|
|
|
return ll.SimpleType.Void
|
2017-10-29 20:08:14 +00:00
|
|
|
else:
|
|
|
|
# TODO
|
|
|
|
err('ty2base_ty: Unknown type: {}'
|
|
|
|
.format(ll.ty2s(ty)))
|
|
|
|
return ty
|
|
|
|
|
|
|
|
|
|
|
|
def base_ty2size(base_ty):
|
|
|
|
if isinstance(base_ty, ll.SimpleType):
|
|
|
|
return 1
|
2017-10-29 21:00:18 +00:00
|
|
|
elif isinstance(base_ty, ll.PointerType):
|
|
|
|
return 1
|
|
|
|
elif isinstance(base_ty, ll.StructType):
|
2017-11-09 13:37:40 +00:00
|
|
|
return max(1, sum(map(base_ty2size, base_ty.fields)))
|
2017-10-29 20:08:14 +00:00
|
|
|
else:
|
|
|
|
# TODO
|
|
|
|
err('base_ty2size: Unknown type or illegal type: {}'
|
|
|
|
.format(ll.ty2s(base_ty)))
|
|
|
|
return 1
|
|
|
|
|
2017-10-30 00:09:36 +00:00
|
|
|
|
2017-10-29 23:47:50 +00:00
|
|
|
def alloc_globals(gdecls, heap):
|
|
|
|
def alloc_global(ginit):
|
|
|
|
next_idx = len(heap)
|
|
|
|
if isinstance(ginit, ll.GNull):
|
|
|
|
TODO('alloc_global: GNull')
|
|
|
|
elif isinstance(ginit, ll.GGid):
|
|
|
|
TODO('alloc_global: GGid')
|
|
|
|
elif isinstance(ginit, ll.GInt):
|
|
|
|
heap.append(ginit.val)
|
|
|
|
elif isinstance(ginit, ll.GString):
|
|
|
|
heap.append(ginit.val)
|
|
|
|
elif isinstance(ginit, ll.GArray):
|
|
|
|
TODO('alloc_global: GArray')
|
|
|
|
elif isinstance(ginit, ll.GStruct):
|
|
|
|
for ty, field in ginit.fields:
|
|
|
|
alloc_global(field)
|
|
|
|
else:
|
|
|
|
err('alloc_global: Unknown global init value: {}'
|
|
|
|
.format(ll.ginit2s(ginit)))
|
|
|
|
return next_idx
|
|
|
|
|
|
|
|
global_env = {}
|
|
|
|
|
|
|
|
for gdecl in gdecls.values():
|
|
|
|
location = alloc_global(gdecl.body)
|
2017-11-13 17:05:44 +00:00
|
|
|
info('heap[{}] <- {}'
|
2017-10-29 23:47:50 +00:00
|
|
|
.format(location, ll.gdecl2s(gdecl)))
|
|
|
|
global_env[gdecl.name] = location
|
|
|
|
|
|
|
|
return global_env
|
|
|
|
|
2017-10-29 20:08:14 +00:00
|
|
|
|
2017-11-09 13:28:02 +00:00
|
|
|
def emulate_builtin(name, arguments_v, ssa_env, heap):
|
|
|
|
new_heap = heap
|
|
|
|
res = None
|
|
|
|
should_exit = False
|
|
|
|
if name == 'allocRecord':
|
|
|
|
if len(arguments_v) != 1:
|
|
|
|
err('Number of arguments to {} should be 1'
|
|
|
|
.format(name))
|
|
|
|
else:
|
|
|
|
size = max(arguments_v[0], 1)
|
2017-11-13 17:05:44 +00:00
|
|
|
info('Allocating {} cells'
|
2017-11-09 13:28:02 +00:00
|
|
|
.format(size))
|
|
|
|
res = len(heap)
|
|
|
|
for i in range(size):
|
|
|
|
heap.append(Garbage.GARBAGE)
|
|
|
|
elif name == 'initArray':
|
|
|
|
if len(arguments_v) != 3:
|
|
|
|
err('Number of arguments to {} should be 3'
|
|
|
|
.format(name))
|
|
|
|
else:
|
|
|
|
num_elements = arguments_v[0]
|
|
|
|
cells_per_element = arguments_v[1]
|
|
|
|
init_ptr = arguments_v[2]
|
2017-11-13 17:05:44 +00:00
|
|
|
info('Allocating {} + {} + {} (size + pointer + content) cells'
|
2017-11-09 13:28:02 +00:00
|
|
|
.format(1, 1, num_elements * cells_per_element))
|
|
|
|
struct_begin = len(heap)
|
|
|
|
heap.append(num_elements)
|
|
|
|
heap.append(Garbage.GARBAGE) # pointer to array contents
|
|
|
|
array_begin = len(heap)
|
|
|
|
heap[struct_begin + 1] = array_begin
|
|
|
|
init_val = [heap[init_ptr + j]
|
|
|
|
for j in range(cells_per_element)]
|
|
|
|
if len(init_val) == 1:
|
|
|
|
init_val_s = init_val[0]
|
|
|
|
else:
|
|
|
|
init_val_s = '{{}}'.format(', '.join(init_val))
|
|
|
|
|
|
|
|
for i in range(num_elements):
|
|
|
|
array_last = len(heap)
|
|
|
|
for j in init_val:
|
|
|
|
heap.append(j)
|
|
|
|
|
|
|
|
if num_elements <= 0:
|
|
|
|
array_init_s = 'No elements initialized for zero length array'
|
|
|
|
else:
|
|
|
|
array_init_s = ('heap[{}..{}] <- heap[{}] = {}'
|
|
|
|
.format(array_begin, array_last,
|
|
|
|
init_ptr, init_val_s))
|
|
|
|
# TODO
|
2017-11-13 17:05:44 +00:00
|
|
|
info('heap[{}] <- {}, heap[{}] <- {}, --- {}'
|
2017-11-09 13:28:02 +00:00
|
|
|
.format(struct_begin, num_elements,
|
|
|
|
struct_begin + 1, array_begin,
|
|
|
|
array_init_s))
|
|
|
|
res = struct_begin
|
2017-11-09 14:32:29 +00:00
|
|
|
elif name == 'print':
|
|
|
|
if len(arguments_v) != 2:
|
|
|
|
err('Number of arguments to {} should be 3'
|
|
|
|
.format(name))
|
|
|
|
else:
|
|
|
|
struct_begin = arguments_v[1]
|
2017-11-13 17:05:44 +00:00
|
|
|
info('Printing string, heap[{}]:'
|
2017-11-09 14:32:29 +00:00
|
|
|
.format(struct_begin))
|
|
|
|
printee = heap[struct_begin + 1]
|
2017-11-13 17:05:44 +00:00
|
|
|
info(printee)
|
2017-11-09 14:32:29 +00:00
|
|
|
if not isinstance(printee, str):
|
|
|
|
warn('What was printed, was not stored as a string in the heap')
|
2017-11-09 13:28:02 +00:00
|
|
|
elif name in builtins:
|
|
|
|
TODO('Have not implemented builtin function {}, yet.'
|
|
|
|
.format(name))
|
|
|
|
else:
|
|
|
|
err('Unknown builtin function {}.'
|
|
|
|
.format(name))
|
|
|
|
return None, heap
|
|
|
|
|
|
|
|
return res, new_heap, should_exit
|
|
|
|
|
|
|
|
|
2017-11-13 17:05:44 +00:00
|
|
|
def auto_step(ast, function_name='tigermain', function_args=[1234, 5678], step_behavior={}):
|
|
|
|
# Set info-level
|
|
|
|
if True:
|
|
|
|
global PRINT_LEVEL
|
|
|
|
PRINT_LEVEL = step_behavior['step_print_level']
|
|
|
|
|
|
|
|
#
|
2017-10-30 00:55:57 +00:00
|
|
|
tdecls = ast.tdecls
|
|
|
|
fdecls = ast.fdecls
|
|
|
|
gdecls = ast.gdecls
|
|
|
|
|
|
|
|
function = fdecls[function_name]
|
|
|
|
body = function.body
|
|
|
|
first_block = body.first_block
|
|
|
|
blocks = body.named_blocks
|
2017-10-29 14:18:14 +00:00
|
|
|
insns = first_block.insns
|
|
|
|
terminator = first_block.terminator
|
|
|
|
stack_frames = []
|
2017-10-30 00:55:57 +00:00
|
|
|
ssa_env = {par[1]: arg for par, arg in zip(function.parameters, function_args)}
|
2017-10-29 20:21:25 +00:00
|
|
|
heap = [None]
|
2017-10-29 19:39:58 +00:00
|
|
|
call_res = []
|
2017-10-29 14:18:14 +00:00
|
|
|
|
2017-10-30 00:55:57 +00:00
|
|
|
global_env = alloc_globals(gdecls, heap)
|
2017-11-13 17:05:44 +00:00
|
|
|
info('Heap after globals are allocated:')
|
|
|
|
info(heap)
|
|
|
|
info()
|
2017-10-29 23:47:50 +00:00
|
|
|
|
2017-10-30 00:55:57 +00:00
|
|
|
step_cnt = 0
|
2017-10-29 14:18:14 +00:00
|
|
|
while True:
|
|
|
|
(insns, terminator, blocks,
|
2017-10-29 20:21:25 +00:00
|
|
|
stack_frames, ssa_env, heap, call_res) = step(insns, terminator, blocks,
|
|
|
|
stack_frames, ssa_env,
|
2017-10-30 00:55:57 +00:00
|
|
|
global_env, heap, tdecls,
|
|
|
|
fdecls, call_res)
|
2017-11-13 17:05:44 +00:00
|
|
|
info()
|
2017-10-29 21:31:07 +00:00
|
|
|
step_cnt += 1
|
2017-10-29 14:18:14 +00:00
|
|
|
if terminator is None:
|
2017-11-13 17:05:44 +00:00
|
|
|
info('Stepping done!\nFinal ssa_env: {}'
|
2017-10-29 14:18:14 +00:00
|
|
|
.format(ssa_env))
|
2017-11-13 17:05:44 +00:00
|
|
|
info('Final heap: {}'
|
2017-10-29 23:00:35 +00:00
|
|
|
.format(heap))
|
2017-11-13 17:05:44 +00:00
|
|
|
info('Program resulted in {} after {} steps'.
|
2017-10-30 00:55:57 +00:00
|
|
|
format(insns[0][1].val, step_cnt))
|
2017-10-29 14:18:14 +00:00
|
|
|
break
|
|
|
|
|
2017-11-13 17:05:44 +00:00
|
|
|
if step_behavior['step_ask'] != 0 and step_cnt % step_behavior['step_ask'] == 0:
|
2017-10-29 21:31:07 +00:00
|
|
|
while True:
|
|
|
|
stop_q = input('We have now done {} steps. Continue? [Y/n]: '
|
|
|
|
.format(step_cnt)).lower()
|
|
|
|
if stop_q in ['y', 'yes', 'n', 'no', '']:
|
|
|
|
break
|
|
|
|
if stop_q in ['n', 'no']:
|
|
|
|
break
|