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

View File

@ -3,7 +3,7 @@ import parser
def TODO(msg): def TODO(msg):
print('TODO, not implemented yet at {}' print('TODO: not implemented yet at {}'
.format(msg)) .format(msg))
@ -17,6 +17,9 @@ def warn(msg):
.format(msg)) .format(msg))
GARBAGE = '<<Unitialized memory>>'
def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, memory, def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, memory,
tdecs, fdecs, call_res): tdecs, fdecs, call_res):
if len(insns) == 0: if len(insns) == 0:
@ -42,7 +45,18 @@ def step(insns, terminator, blocks, stack_frames, ssa_env, global_env, memory,
print('{} {}, {}' print('{} {}, {}'
.format(bop, left_v, right_v)) .format(bop, left_v, right_v))
elif isinstance(next_insn, ll.Alloca): 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): elif isinstance(next_insn, ll.Load):
TODO('Load') TODO('Load')
elif isinstance(next_insn, ll.Store): elif isinstance(next_insn, ll.Store):
@ -287,23 +301,36 @@ def eval_icmp(cnd, left, right):
return 0 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(): def gogo():
p = parser.LLVMParser() p = parser.LLVMParser()
p.build() p.build()
data = r''' data = r'''
define i64 @tigermain (i64 %U_mainSL_8, i64 %U_mainDummy_9) { define i64 @tigermain (i64 %U_mainSL_8, i64 %U_mainDummy_9) {
%a = add i64 3, 5 ; please be 8 %a = alloca i64
%c = icmp eq i64 %a, 8 %b = alloca i64
br i1 %c, label %L1, label %L1 ret i64 77
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
} }
''' '''