-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcg.rb
245 lines (214 loc) · 5.52 KB
/
cg.rb
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
require_relative "types.rb"
class Cg
def initialize(output, sym)
@output = output
@sym = sym
@freereg = Array.new(4).fill(0)
@reglist = ["%r8","%r9", "%r10", "%r11"].freeze
@breglist = ["%r8b","%r9b", "%r10b", "%r11b"].freeze
@dreglist = ["%r8d", "%r9d", "%r10d", "%r11d"].freeze
end
def allocate_register
r = nil
for i in 0..3
if @freereg[i] == 1
r = i
@freereg[i] = 0
break
end
end
if r then r else fatal("out of registers") end
end
def free_register(reg)
if @freereg[reg] != 0
puts "Error trying to free register #{reg}"
exit(1)
end
@freereg[reg] = 1
end
def freeall_registers
@freereg.fill(1)
end
def cgpreamble
freeall_registers
code = "\t.text\n"
@output.puts code
end
def cgfuncpreamble(name)
code = "\t.text\n"
code += "\t.globl\t#{name}\n"
code += "\t.type\t#{name}, @function\n"
code += "#{name}:\n"
code += "\tpushq\t%rbp\n"
code += "\tmovq\t%rsp, %rbp\n"
@output.puts code
end
def cgfuncpostamble(id)
cglabel(@sym.names[id]["endlabel"])
code = "\tpopq %rbp\n"
code += "\tret\n"
@output.puts code
end
def cgloadint(val, type)
r = allocate_register
code = "\tmovq\t$#{val}, #{@reglist[r]}\n"
@output.puts code
r
end
def cgadd(r1, r2)
code = "\taddq\t#{@reglist[r1]}, #{@reglist[r2]}\n"
@output.puts code
free_register(r1)
r2
end
def cgsub(r1, r2)
code = "\tsubq\t#{@reglist[r2]}, #{@reglist[r1]}\n"
@output.puts code
free_register(r2)
r1
end
def cgmul(r1, r2)
code = "\timulq\t#{@reglist[r1]}, #{@reglist[r2]}\n"
@output.puts code
free_register(r1)
r2
end
def cgdiv(r1, r2)
code = "\tmovq\t#{@reglist[r1]}, %rax\n"
code += "\tcqo\n"
code += "\tidivq\t#{@reglist[r2]}\n"
code += "\tmovq\t%rax, #{@reglist[r1]}\n"
@output.puts code
free_register(r2)
r1
end
def cgprintint(r)
code = "\tmovq\t#{@reglist[r]}, %rdi\n"
code += "\tcall\tprintint\n"
@output.puts code
free_register(r)
end
def cgloadglob(id)
identifier = @sym.names[id]["name"]
type = @sym.names[id]["type"]
r = allocate_register()
case type
when :P_CHAR
code = "\tmovzbq\t#{identifier}(%rip), #{@reglist[r]}\n"
when :P_INT
code = "\tmovzbl\t#{identifier}(%rip), #{@reglist[r]}\n"
when :P_LONG, :P_CHARPTR, :P_INTPTR, :P_LONGPTR
code = "\tmovq\t#{identifier}(%rip), #{@reglist[r]}\n"
else
fatal("bad type in cgloaglglob #{type}")
end
@output.puts code
r
end
def cgstoreglob(r, id)
identifier = @sym.names[id]["name"]
type = @sym.names[id]["type"]
case type
when :P_INT
code = "\tmovl\t#{@dreglist[r]}, #{identifier}(%rip)\n"
when :P_CHAR
code = "\tmovb\t#{@breglist[r]}, #{identifier}(%rip)\n"
when :P_LONG, :P_CHARPTR, :P_INTPTR, :P_LONGPTR
code = "\tmovq\t#{@reglist[r]}, #{identifier}(%rip)\n"
else
fatal("Bad type in cgstoreglob, #{type}")
end
@output.puts code
r
end
def cgglobsym(id)
typesize = Types::primsize(@sym.names[id]["type"])
name = @sym.names[id]["name"]
code = "\t.data\n\t.globl\t#{name}\n"
case typesize
when 1
code += "#{name}:\t.byte\t0\n"
when 4
code += "#{name}:\t.long\t0\n"
when 8
code += "#{name}:\t.quad\t0\n"
end
@output.puts code
end
def cgcompare_and_set(op, r1, r2)
if not [:EQ_EQ, :NE, :LT, :LE, :GT, :GE].include? op
fatal("bad comparision op")
end
cmplist = {:EQ_EQ => "sete", :NE => "setne", :LT => "setl", :GT => "setg", :LE => "setle", :GE => "setge"}
code = "\tcmpq\t#{@reglist[r2]}, #{@reglist[r1]}\n"
code += "\t#{cmplist[op]}\t#{@breglist[r2]}\n"
code += "\tmovzbq\t#{@breglist[r2]}, #{@reglist[r2]}"
@output.puts code
free_register(r1)
r2
end
def cglabel(l)
@output.puts "L#{l}:\n"
end
def cgjmp(l)
@output.puts "\tjmp\tL#{l}\n"
end
def cgcompare_and_jump(op, r1, r2, label)
if not [:EQ_EQ, :NE, :LT, :LE, :GT, :GE].include? op
fatal("bad comparision op")
end
invcmplist = {:EQ_EQ => "jne", :NE => "je", :LT => "jge", :GT => "jle", :LE => "jg", :GE => "jl"}
code = "\tcmpq\t#{@reglist[r2]}, #{@reglist[r1]}\n"
code += "\t#{invcmplist[op]}\tL#{label}\n"
@output.puts code
freeall_registers()
-1
end
def cgcall(reg, id)
outr = allocate_register
code = "\tmovq\t#{@reglist[reg]}, %rdi\n"
code += "\tcall\t#{@sym.names[id]["name"]}\n"
code += "\tmovq\t%rax, #{@reglist[outr]}\n"
@output.puts code
free_register reg
outr
end
def cgreturn(reg, id)
case @sym.names[id]["type"]
when :P_CHAR
code = "\tmovzbl\t#{@breglist[reg]}, %eax\n"
when :P_INT
code = "\tmovl\t#{@dreglist[reg]}, %eax\n"
when :P_LONG
code = "\tmovq\t#{@reglist[reg]}, %rax\n"
else
fatal("bad type in cgreturn")
end
@output.puts code
cgjmp(@sym.names[id]["endlabel"])
end
def cgaddr(id)
r = allocate_register
code = "\tleaq\t#{@sym.names[id]["name"]}(%rip), #{@reglist[r]}"
@output.puts code
r
end
def cgderef(r, type)
case type
when :P_CHARPTR
code = "\tmovzbq\t(#{@reglist[r]}), #{@reglist[r]}\n"
when :P_INTPTR, :P_LONGPTR
code = "\tmovq\t(#{@reglist[r]}), #{@reglist[r]}\n"
end
@output.puts code
r
end
def cgshlconst(r, val)
@output.puts "\tsalq\t$#{val}, #{@reglist[r]}\n"
r
end
def fatal(message)
puts "CG: #{message}"
exit(1)
end
end