-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
150 lines (116 loc) · 3.47 KB
/
test.py
File metadata and controls
150 lines (116 loc) · 3.47 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from llvmlite import ir
from llvmlite import binding
'''
Este módulo contém uma função main e criação de um array unidimensional como variável global e outro como local.
Será gerado um código em LLVM como este em C:
int main(){
int a = 1;
int b = 2;
int c = 0;
if(a < b) {
c = 5;
}
else {
c = 6;
}
if(a < 1024){
c = 10;
}
else {
c = 20;
}
return 0;
}
'''
binding.initialize()
binding.initialize_native_target()
binding.initialize_native_asmprinter() # yes, even this one
# Define o target.
target = binding.Target.from_triple("x86_64-pc-linux-gnu")
target_machine = target.create_target_machine()
# Cria o módulo.
module = ir.Module(name='meu_modulo.bc')
# Cria um valor zero para colocar no retorno.
Zero = ir.Constant(ir.IntType(32), 0)
# Declara o tipo do retorno da função main.
mainFnReturnType = ir.IntType(32)
# Cria a função main.
t_func_main = ir.FunctionType(mainFnReturnType, ())
# Declara a função main.
main = ir.Function(module, t_func_main, name='main')
# Declara o bloco de entrada.
entryBlock = main.append_basic_block('entry')
exitBasicBlock = main.append_basic_block('exit')
# Adiciona o bloco de entrada.
builder = ir.IRBuilder(entryBlock)
# Cria o valor de retorno e inicializa com zero.
returnVal = builder.alloca(ir.IntType(32), name='retorno')
builder.store(Zero, returnVal)
# int a = 1;
a = builder.alloca(ir.IntType(32), name='a')
a.align = 4
# int b = 2;
b = builder.alloca(ir.IntType(32), name='b')
b.align = 4
# int c = 0;
c = builder.alloca(ir.IntType(32), name='c')
c.align = 4
# Inicializa as variáveis.
builder.store(ir.Constant(ir.IntType(32), 1), a)
builder.store(ir.Constant(ir.IntType(32), 2), b)
builder.store(ir.Constant(ir.IntType(32), 0), c)
# Declara os blocos básicos para o primeiro if.
# if(a < b) {
# c = 5;
# }
# else {
# c = 6;
# }
iftrue_1 = main.append_basic_block('iftrue_1')
iffalse_1 = main.append_basic_block('iffalse_1')
ifend_1 = main.append_basic_block('ifend_1')
# Carrega as variáveis a e b para comparação.
# IRBuilder.load(ptr, name='', align=None)
a_cmp = builder.load(a, 'a_cmp', align=4)
b_cmp = builder.load(a, 'b_cmp', align=4)
# IRBuilder.icmp_signed(cmpop, lhs, rhs, name='')
If_1 = builder.icmp_signed('<', a_cmp, b_cmp, name='if_test_1')
builder.cbranch(If_1, iftrue_1, iffalse_1)
builder.position_at_end(iftrue_1)
builder.store(ir.Constant(ir.IntType(32), 5), c)
builder.branch(ifend_1)
builder.position_at_end(iffalse_1)
builder.store(ir.Constant(ir.IntType(32), 6), c)
builder.branch(ifend_1)
builder.position_at_end(ifend_1)
# Declara os blocos básicos para o if.
# if(a < 1024){
# c = 10;
# }
# else {
# c = 20;
# }
# return 0;
iftrue_2 = main.append_basic_block('iftrue_2')
iffalse_2 = main.append_basic_block('iffalse_2')
ifend_2 = main.append_basic_block('ifend_2')
a_cmp_2 = builder.load(a, 'a_cmp_2', align=4)
If_2 = builder.icmp_signed('<', a_cmp, ir.Constant(ir.IntType(32), 1024), name='if_test_1')
builder.cbranch(If_2, iftrue_2, iffalse_2)
builder.position_at_end(iftrue_2)
builder.store(ir.Constant(ir.IntType(32), 10), c)
builder.branch(ifend_2)
builder.position_at_end(iffalse_2)
builder.store(ir.Constant(ir.IntType(32), 20), c)
builder.branch(ifend_2)
builder.position_at_end(ifend_2)
# Cria um salto para o bloco de saída.
builder.branch(exitBasicBlock);
# Adiciona o bloco de saída.
builder = ir.IRBuilder(exitBasicBlock)
# Cria o return.
builder.ret(builder.load(returnVal, ""))
arquivo = open('meu_modulo.ll', 'w')
arquivo.write(str(module))
arquivo.close()
print(module)