-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompiler.rb
228 lines (192 loc) · 6.99 KB
/
compiler.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
require "rugged"
class String
def myshellsplit
pieces = [""]
stringmode = false
self.strip.gsub(/\s+/, " ").split("").each do |c|
if c == '"'
stringmode = (not stringmode)
elsif c == ' ' and not stringmode
pieces << ""
end
unless c == ' ' and not stringmode
pieces[-1] = pieces.last+c
end
end
return pieces
end
end
class LegitCompiler
def initialize path
@repo = Rugged::Repository.new(path)
@debug = false
@did_jump = false
end
def compile outfile
commits = []
queue = [@repo.branches["master"].target]
until queue.empty?
c = queue.shift
commits << c
c.parents.each do |parent|
queue << parent unless commits.include? parent
end
end
ir = <<HERE
declare void @exit(i32)
declare i32 @getchar()
declare void @putchar(i8)
@stack = global [10000 x i64] zeroinitializer
; The stack pointer is an index into the stack.
@sp = global i64 0
@tape = global [10000 x i64] zeroinitializer
; The tape pointer is an index into the tape.
@tp = global i64 5000
define void @push(i64 %val) {
%sp = load i64, i64* @sp
%addr = getelementptr [10000 x i64], [10000 x i64]* @stack, i64 0, i64 %sp
store i64 %val, i64* %addr
%newsp = add i64 %sp, 1
store i64 %newsp, i64* @sp
ret void
}
define i64 @pop() {
%sp = load i64, i64* @sp
%newsp = sub i64 %sp, 1
%cmp = icmp eq i64 %newsp, -1
br i1 %cmp, label %empty, label %continue
continue:
%addr = getelementptr [10000 x i64], [10000 x i64]* @stack, i64 0, i64 %newsp
%val = load i64, i64* %addr
store i64 %newsp, i64* @sp
ret i64 %val
empty:
%newsp2 = add i64 %newsp, 1
store i64 %newsp2, i64* @sp
ret i64 0
}
define void @right(i64 %offset) {
%tp = load i64, i64* @tp
%newtp = add i64 %tp, %offset
store i64 %newtp, i64* @tp
ret void
}
define void @left(i64 %offset) {
%tp = load i64, i64* @tp
%newtp = sub i64 %tp, %offset
store i64 %newtp, i64* @tp
ret void
}
define i64 @read() {
%tp = load i64, i64* @tp
%addr = getelementptr [10000 x i64], [10000 x i64]* @tape, i64 0, i64 %tp
%val = load i64, i64* %addr
ret i64 %val
}
define void @write(i64 %val) {
%tp = load i64, i64* @tp
%addr = getelementptr [10000 x i64], [10000 x i64]* @tape, i64 0, i64 %tp
store i64 %val, i64* %addr
ret void
}
define i32 @main() {
HERE
# It's not possible to jump to the first basic block of a function, so this
# is a workaround to avoid having a label on the first line.
ir << " br label %commit#{commits.first.oid[0..7]}\n"
commits.each do |c|
ir << "commit"+c.oid[0..7]+":\n"
u = c.oid[0..7]
i = 0
@did_jump = false
c.message.myshellsplit.each do |command|
uu = u+i.to_s
i += 1
case command
when "get"
ir << " %c#{uu} = call i32 @getchar()\n"
ir << " %c2#{uu} = sext i32 %c#{uu} to i64\n"
ir << " %c3#{uu} = icmp eq i64 %c2#{uu}, -1\n"
ir << " %c4#{uu} = select i1 %c3#{uu}, i64 0, i64 %c2#{uu}\n"
ir << " call void @push(i64 %c4#{uu})\n"
when "put"
ir << " %c#{uu} = call i64 @pop()\n"
ir << " %c2#{uu} = trunc i64 %c#{uu} to i8\n"
ir << " call void @putchar(i8 %c2#{uu})\n"
when "dup"
ir << " %c#{uu} = call i64 @pop()\n"
ir << " call void @push(i64 %c#{uu})\n"
ir << " call void @push(i64 %c#{uu})\n"
when "pop"
ir << " call i64 @pop()\n"
when "add"
ir << " %a#{uu} = call i64 @pop()\n"
ir << " %b#{uu} = call i64 @pop()\n"
ir << " %c#{uu} = add i64 %a#{uu}, %b#{uu}\n"
ir << " call void @push(i64 %c#{uu})\n"
when "sub"
ir << " %a#{uu} = call i64 @pop()\n"
ir << " %b#{uu} = call i64 @pop()\n"
ir << " %c#{uu} = sub i64 %b#{uu}, %a#{uu}\n"
ir << " call void @push(i64 %c#{uu})\n"
when "cmp"
ir << " %a#{uu} = call i64 @pop()\n"
ir << " %b#{uu} = call i64 @pop()\n"
ir << " %c#{uu} = icmp ugt i64 %b#{uu}, %a#{uu}\n"
ir << " %c2#{uu} = zext i1 %c#{uu} to i64\n"
ir << " call void @push(i64 %c2#{uu})\n"
when "read"
ir << " %a#{uu} = call i64 @read()\n"
ir << " call void @push(i64 %a#{uu})\n"
when "write"
ir << " %a#{uu} = call i64 @pop()\n"
ir << " call void @write(i64 %a#{uu})\n"
when "right"
ir << " %a#{uu} = call i64 @pop()\n"
ir << " call void @right(i64 %a#{uu})\n"
when "left"
ir << " %a#{uu} = call i64 @pop()\n"
ir << " call void @left(i64 %a#{uu})\n"
when "quit"
ir << " call void @exit(i32 0)\n"
when /^\d+$/
ir << " call void @push(i64 #{command.to_i})\n"
when /^[a-zA-Z]$/
ir << " call void @push(i64 #{command[0].ord})\n"
when /^".*"$/
command.undump.split("").each do |c|
ir << " call void @push(i64 #{c[0].ord})\n"
end
when /^\[.*\]$/
tag_name = command[1..-2]
tag = @repo.tags[tag_name]
raise "Could not jump to tag '"+tagname+"'" unless tag
ir << " br label %commit#{tag.target.oid[0..7]}\n"
@did_jump = true
else
raise "Unknown command '"+command+"'"
end
end
unless @did_jump
case c.parents.size
when 0
ir << " call void @exit(i32 0)\n"
ir << " unreachable\n"
when 1
ir << " br label %commit#{c.parents.last.oid[0..7]}\n"
else
ir << " %val#{u} = call i64 @pop()\n"
ir << " switch i64 %val#{u}, label %commit#{c.parents.last.oid[0..7]} ["
c.parents[0..-2].each_with_index do |p, i|
ir << "i64 #{i}, label %commit#{p.oid[0..7]} "
end
ir << "]\n"
end
end
end
ir << "}\n"
IO.write(outfile, ir)
end
end
i = LegitCompiler.new(ARGV.first)
i.compile(File.basename(ARGV.first)+".ll")