From 2008b639193cdb2d40324ba4cd2c0469df7993ce Mon Sep 17 00:00:00 2001 From: cfreksen Date: Mon, 30 Oct 2017 00:47:20 +0100 Subject: [PATCH] Improve stringification of llvm AST nodes. --- ll.py | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/ll.py b/ll.py index 45cbfa0..bf20b96 100644 --- a/ll.py +++ b/ll.py @@ -80,10 +80,15 @@ def ty2s(ty): elif isinstance(ty, StructType): return ('{{{}}}' .format(', '.join(map(ty2s, ty.fields)))) - if isinstance(ty, NamedType): + elif isinstance(ty, ArrayType): + return ('[{} x {}]' + .format(ty.length, ty2s(ty.inner_ty))) + elif isinstance(ty, FunctionType): + print('TODO: ty2s: FunctionType') + return '' + elif isinstance(ty, NamedType): return '%' + ty.other_name else: - # TODO print('ty2s: Unknown type: {}' .format(ty)) return str(ty) @@ -161,6 +166,7 @@ def insn2s(insn): .format(insn)) return '???' + def terminator2s(terminator): if isinstance(terminator, Ret): if terminator.oper is None: @@ -182,3 +188,33 @@ def terminator2s(terminator): else: print('terminator2s: Unknown terminator {}' .format(terminator)) + + +def ginit2s(ginit): + if isinstance(ginit, GNull): + return 'null' + elif isinstance(ginit, GGid): + return '@' + ginit.val + elif isinstance(ginit, GInt): + return str(ginit.val) + elif isinstance(ginit, GString): + return ('c"{}"' + .format(ll_encode(ginit.val))) + elif isinstance(ginit, GArray): + return ('[{}]' + .format(', '.join('{} {}'.format(ty2s(t), ginit2s(g)) + for t, g in ginit.entries))) + elif isinstance(ginit, GStruct): + return ('{{{}}}' + .format(', '.join('{} {}'.format(ty2s(t), ginit2s(g)) + for t, g in ginit.fields))) + + +def gdecl2s(gdecl): + return ('@{} = {} {}' + .format(gdecl.name, ty2s(gdecl.ty), ginit2s(gdecl.body))) + + +def ll_encode(s): + # TODO + return s