From 7deddce45d8c049a63491fba3cc174bedde054aa Mon Sep 17 00:00:00 2001 From: cfreksen Date: Sun, 29 Oct 2017 22:00:18 +0100 Subject: [PATCH] Handle more types for Alloca. --- ll.py | 5 +++++ stepper.py | 27 +++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/ll.py b/ll.py index 4477e78..31d706a 100644 --- a/ll.py +++ b/ll.py @@ -77,6 +77,11 @@ def ty2s(ty): return repr(ty) elif isinstance(ty, PointerType): return ty2s(ty.inner_ty) + '*' + elif isinstance(ty, StructType): + return ('{{{}}}' + .format(', '.join(map(ty2s, ty.fields)))) + if isinstance(ty, NamedType): + return '%' + ty.other_name else: # TODO print('ty2s: Unknown type: {}' diff --git a/stepper.py b/stepper.py index 77c5a12..c1b2feb 100644 --- a/stepper.py +++ b/stepper.py @@ -338,11 +338,25 @@ def eval_icmp(cnd, left, right): return 0 -def ty2base_ty(ty, tdecs): +def ty2base_ty(ty, tdecs, seen=[]): if isinstance(ty, ll.SimpleType): return ty elif isinstance(ty, ll.PointerType): - return ll.PointerType(ty2base_ty(ty.inner_ty)) + return ll.PointerType(ty2base_ty(ty.inner_ty, tdecs, seen)) + 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: + err('Could not find type {} in gloval type environment:\n{}' + .format(ty2s(ty), tdecs.keys())) + return ll.SimpleType.Void else: # TODO err('ty2base_ty: Unknown type: {}' @@ -353,6 +367,10 @@ def ty2base_ty(ty, tdecs): def base_ty2size(base_ty): if isinstance(base_ty, ll.SimpleType): return 1 + elif isinstance(base_ty, ll.PointerType): + return 1 + elif isinstance(base_ty, ll.StructType): + return sum(map(base_ty2size, base_ty.fields)) else: # TODO err('base_ty2size: Unknown type or illegal type: {}' @@ -364,12 +382,13 @@ def gogo(): p = parser.LLVMParser() p.build() data = r''' +%T_tigermain = type { i64, i64 } + define i64 @tigermain (i64 %U_mainSL_8, i64 %U_mainDummy_9) { %a = alloca i64 - %a_alt = alloca i64 + %t = alloca %T_tigermain store i64 9, i64* %a %b = load i64, i64* %a - %b_alt = load i64, i64* %a_alt ret i64 %b } '''