From 397c144bba95761f10317648177c75dced7b862d Mon Sep 17 00:00:00 2001 From: cfreksen Date: Mon, 30 Oct 2017 00:47:50 +0100 Subject: [PATCH] Allocate globals. --- stepper.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/stepper.py b/stepper.py index 4493084..a85f3ef 100644 --- a/stepper.py +++ b/stepper.py @@ -464,6 +464,37 @@ def base_ty2size(base_ty): .format(ll.ty2s(base_ty))) 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(): p = parser.LLVMParser() @@ -471,6 +502,9 @@ def gogo(): data = r''' %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) { %t = alloca %T_tigermain %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 fdecs = ast.fdecls - global_env = ast.gdecls + gdecs = ast.gdecls tigermain = ast.fdecls['tigermain'] first_block = tigermain.body.first_block @@ -498,6 +532,9 @@ define i64 @tigermain (i64 %U_mainSL_8, i64 %U_mainDummy_9) { call_res = [] step_cnt = 0 + + global_env = alloc_globals(gdecs, heap) + while True: (insns, terminator, blocks, stack_frames, ssa_env, heap, call_res) = step(insns, terminator, blocks,