forked from pfalcon/llvm-codegen-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
llvm2icode.py
executable file
·77 lines (63 loc) · 2.1 KB
/
llvm2icode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
#
# This tried to convert LLVM IR to SDCC iCode
#
import sys
from llvm.core import *
import llvm
CMP_MAP = {ICMP_EQ: "=="}
def oper(opers):
return [str(x) for x in opers]
with open(sys.argv[1]) as asm:
mod = Module.from_assembly(asm)
tmp_i = 1
def number_tmps(mod):
global tmp_i
for f in mod.functions:
print `f`
f_type = str(f.type)[:-1]
f_type = f_type.split(" ", 1)
f_type = f_type[0] + " function " + f_type[1]
print "proc _%s{%s}" % (f.name, f_type)
for b in f.basic_blocks:
# print "BB name:", b.name
for i in b.instructions:
# print i
if not i.name and i.type != Type.void():
i.name = "t%d" % tmp_i
tmp_i += 1
def arg(a):
if isinstance(a, Argument):
# print "arg name:", a.name
# return str(a)
return "%s{%s}" % (a.name, a.type)
if isinstance(a, GlobalVariable):
# print "arg name:", a.name
# return str(a)
return "%s{%s}" % (a.name, str(a.type)[:-1])
if isinstance(a, ConstantInt):
# print "arg val:", a.z_ext_value
# return str(a)
return "%s{%s}" % (a.z_ext_value, a.type)
1/0
number_tmps(mod)
lab = 1
for f in mod.functions:
# print `f`
for b in f.basic_blocks:
print " _%s($) :" % b.name
for i in b.instructions:
print "#", i
print "# name:", i.name, "type:", i.type, "op:", i.opcode_name, "operands:", i.operands
if i.name:
if i.opcode_name == "icmp":
print "%s{%s} = %s %s %s" % (i.name, i.type, arg(i.operands[0]), CMP_MAP[i.predicate], arg(i.operands[1]))
elif i.opcode_name == "load":
a = i.operands[0]
if isinstance(a, GlobalVariable):
print "%s<nospill>{%s} := %s<addr>" % (i.name, i.type, arg(a))
else:
1/0
# elif i.opcode_name == "add":
else:
print "??? %s{%s}" % (i.name, i.type)