Add alloca (for simple types).

This commit is contained in:
cfreksen 2017-10-29 21:08:14 +01:00
parent 648c718843
commit 13017ef95b
No known key found for this signature in database
GPG Key ID: EAC13EE101008978
2 changed files with 45 additions and 14 deletions

4
ll.py
View File

@ -107,6 +107,9 @@ def insn2s(insn):
return ('{} {} {}, {}'
.format(insn.bop, ty2s(insn.ty),
oper2s(insn.left), oper2s(insn.right)))
if isinstance(insn, Alloca):
return ('alloca {}'
.format(ty2s(insn.ty)))
elif isinstance(insn, Icmp):
return ('icmp {} {} {}, {}'
.format(insn.cnd, ty2s(insn.ty),
@ -134,6 +137,7 @@ def insn2s(insn):
# TODO
print('insn2s: Unknown insn: {}'
.format(insn))
return '???'
def terminator2s(terminator):
if isinstance(terminator, Ret):

View File

@ -3,7 +3,7 @@ import parser
def TODO(msg):
print('TODO, not implemented yet at {}'
print('TODO: not implemented yet at {}'
.format(msg))
@ -17,6 +17,9 @@ def warn(msg):
.format(msg))
GARBAGE = '<<Unitialized memory>>'
def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, memory,
tdecs, fdecs, call_res):
if len(insns) == 0:
@ -42,7 +45,18 @@ def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, memory,
print('{} {}, {}'
.format(bop, left_v, right_v))
elif isinstance(next_insn, ll.Alloca):
TODO('Alloca')
ty = next_insn.ty
base_ty = ty2base_ty(ty, tdecs)
size = base_ty2size(base_ty)
# TODO
print('alloca {} --> allocating {} cells'
.format(ll.ty2s(base_ty), size))
ptr = len(memory)
for i in range(max(size, 1)):
memory.append(GARBAGE)
res = ptr
elif isinstance(next_insn, ll.Load):
TODO('Load')
elif isinstance(next_insn, ll.Store):
@ -287,23 +301,36 @@ def eval_icmp(cnd, left, right):
return 0
def ty2base_ty(ty, tdecs):
if isinstance(ty, ll.SimpleType):
return ty
elif isinstance(ty, ll.PointerType):
return ll.PointerType(ty2base_ty(ty.inner_ty))
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
else:
# TODO
err('base_ty2size: Unknown type or illegal type: {}'
.format(ll.ty2s(base_ty)))
return 1
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
%c = icmp eq i64 %a, 8
br i1 %c, label %L1, label %L1
L1:
%b = call i64 @f (i64 7, i64 %a)
ret i1 %c
}
define i64 @f (i64 %x, i64 %y) {
%a = mul i64 2, %y
%b = mul i64 %x, %a
ret i64 %b
%a = alloca i64
%b = alloca i64
ret i64 77
}
'''