Allocate globals.

This commit is contained in:
cfreksen 2017-10-30 00:47:50 +01:00
parent 2008b63919
commit 397c144bba
No known key found for this signature in database
GPG Key ID: EAC13EE101008978

View File

@ -464,6 +464,37 @@ def base_ty2size(base_ty):
.format(ll.ty2s(base_ty))) .format(ll.ty2s(base_ty)))
return 1 return 1
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)
print('heap[{}] <- {}'
.format(location, ll.gdecl2s(gdecl)))
global_env[gdecl.name] = location
return global_env
def gogo(): def gogo():
p = parser.LLVMParser() p = parser.LLVMParser()
@ -471,6 +502,9 @@ def gogo():
data = r''' data = r'''
%T_tigermain = type { i64, i64, {i64, i64}, i64 } %T_tigermain = type { i64, i64, {i64, i64}, i64 }
@G_str_111 = global { i64, [4 x i8] } {i64 4, [4 x i8] c"Hej\0A"}
@G_str_999 = global { i64, [5 x i8] } {i64 5, [5 x i8] c"QRST\0A"}
define i64 @tigermain (i64 %U_mainSL_8, i64 %U_mainDummy_9) { define i64 @tigermain (i64 %U_mainSL_8, i64 %U_mainDummy_9) {
%t = alloca %T_tigermain %t = alloca %T_tigermain
%a = getelementptr %T_tigermain, %T_tigermain* %t, i32 0, i32 2, i32 1 %a = getelementptr %T_tigermain, %T_tigermain* %t, i32 0, i32 2, i32 1
@ -485,7 +519,7 @@ define i64 @tigermain (i64 %U_mainSL_8, i64 %U_mainDummy_9) {
tdecs = ast.tdecls tdecs = ast.tdecls
fdecs = ast.fdecls fdecs = ast.fdecls
global_env = ast.gdecls gdecs = ast.gdecls
tigermain = ast.fdecls['tigermain'] tigermain = ast.fdecls['tigermain']
first_block = tigermain.body.first_block first_block = tigermain.body.first_block
@ -498,6 +532,9 @@ define i64 @tigermain (i64 %U_mainSL_8, i64 %U_mainDummy_9) {
call_res = [] call_res = []
step_cnt = 0 step_cnt = 0
global_env = alloc_globals(gdecs, heap)
while True: while True:
(insns, terminator, blocks, (insns, terminator, blocks,
stack_frames, ssa_env, heap, call_res) = step(insns, terminator, blocks, stack_frames, ssa_env, heap, call_res) = step(insns, terminator, blocks,