aboutsummaryrefslogtreecommitdiff
path: root/bootstrap.lua
blob: f7308fa1319bdb6358d0b42928a654e2a93a55c6 (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env lua

local macros = {
	["$"] = [[
mov rax, [r12]
sub r12, 8
mov [r12], rax
]],
	["%"] = [[
add r12, 8
]],
	["\\"] = [[
mov rax, [r12]
mov rbx, [r12+8]
mov [r12], rbx
mov [r12+8], rax
]],
	["@"] = [[
mov rax, [r12]
mov rbx, [r12+8]
mov rcx, [r12+16]
mov [r12], rcx
mov [r12+8], rax
mov [r12+16], rbx
]],
	["O"] = [[
mov rax, [r12]
lea rax, [r12+8*rax]
mov rax, [rax+8]
mov [r12], rax
]],
	["+"] = [[
mov eax, [r12]
add r12, 8
add [r12], eax
]],
	["-"] = [[
mov eax, [r12]
add r12, 8
sub [r12], eax
]],
	["*"] = [[
mov eax, [r12+8]
imul dword[r12]
add r12, 8
mov [r12], eax
]],
	["/"] = [[
xor edx, edx
mov eax, [r12+8]
idiv dword[r12]
add r12, 8
mov [r12], eax
]],
	["_"] = [[
neg dword[r12]
]],
	["&"] = [[
mov eax, [r12]
and eax, [r12+8]
add r12, 8
mov [r12], eax
]],
	["|"] = [[
mov eax, [r12]
or eax, [r12+8]
add r12, 8
mov [r12], eax
]],
	["~"] = [[
not dword[r12]
]],
	[">"] = [[
mov ebx, 0
mov ecx, -1
add r12, 8
mov eax, [r12]
cmp eax, [r12-8]
cmovg ebx, ecx
mov [r12], ebx
]],
	["="] = [[
mov ebx, 0
mov ecx, -1
add r12, 8
mov eax, [r12]
cmp eax, [r12-8]
cmove ebx, ecx
mov [r12], ebx
]],
	["!"] = [[
add r12, 8
call [r12-8]
]],
	["?"] = [[
call conditional
]],
	["#"] = [[
call loop
]],
	[":"] = [[
add r12, 16
mov rax, [r12-16]
mov rbx, [r12-8]
mov [rax], rbx
]],
	[";"] = [[
mov rax, [r12]
mov rax, [rax]
mov [r12], rax
]],
	[","] = [[
mov rax, 1
mov rdi, 1
mov rsi, r12
mov rdx, 1
syscall
add r12, 8
]],
	["^"] = [[
sub r12, 8
mov qword[r12], 0
mov rax, 0
mov rdi, 0
mov rsi, r12
mov rdx, 1
syscall
mov ebx, [r12]
mov ecx, -1
cmp rax, 0
cmove ebx, ecx
mov [r12], ebx
]],
	["."] = [[
call print_num
]],
	["B"] = "",
}

local fn_counter = 0
local str_counter = 0
local current_int

local line_number = 1
local line_position = 0

local c

local function read_char()
	local char = io.read(1)

	if char == "\n" then
		line_number = line_number + 1
		line_position = 0
	else
		line_position = line_position + 1
	end

	return char
end

local function syntax_error(msg)
	error("FALSE syntax error at "..line_number..":"..line_position..": " .. msg)
end

local function compile_fn()
	local fn_id = fn_counter
	fn_counter = fn_counter + 1

	print("fun_" .. fn_id .. ":")

	local strings = ""

	c = nil

	while true do
		if not c then
			c = read_char()
		end

		if c and c:match("%d") then
			current_int = current_int or 0
			current_int = current_int * 10 + tonumber(c)
			c = nil
		elseif current_int then
			print("sub r12, 8")
			print("mov qword[r12], " .. current_int)
			current_int = nil
		elseif c and c:match("[a-z]") then
			print("sub r12, 8")
			print("mov qword[r12], var_" .. c)
			c = nil
		elseif c == "'" then
			local x = read_char()
			if not x then
				syntax_error("unterminated char literal")
			end
			print("sub r12, 8")
			print("mov qword[r12], " .. x:byte(1))
			c = nil
		elseif c == "\"" then
			local str = {}
			while true do
				local x = read_char()
				if not x then
					syntax_error("unterminated string literal")
				end
				if x == "\"" then
					break
				end
				table.insert(str, x:byte(1))
			end
			print("mov rax, 1")
			print("mov rdi, 1")
			print("mov rsi, str_" .. str_counter)
			print("mov rdx, " .. #str)
			print("syscall")
			strings = "str_" .. str_counter .. ": db " .. table.concat(str, ",") .. "\n" .. strings
			str_counter = str_counter + 1
			c = nil
		elseif c == "`" then
			while true do
				local x = read_char()
				if not x then
					syntax_error("unterminated inline assembly")
				end
				if x == "`" then
					break
				end
				io.write(x)
			end
			c = nil
		elseif c == "[" then
			local lambda = fn_counter
			print("jmp end_" .. lambda)
			compile_fn()
			if c ~= "]" then
				syntax_error("unterminated lambda")
			end
			print("sub r12, 8")
			print("mov qword[r12], fun_" .. lambda)
			c = nil
		elseif c and c:match("%s") then
			c = nil
		elseif not c or c == "]" then
			break
		elseif c and c:byte(1) == 195 then
			c = read_char()

			if c and c:byte(1) == 184 then
				c = "O"
			elseif c and c:byte(1) == 159 then
				c = "B"
			else
				syntax_error("unknown UTF-8 character")
			end
		elseif c and c:byte(1) == 248 then
			c = "O"
		elseif c and c:byte(1) == 223 then
			c = "B"
		elseif c == "{" then
			while read_char() ~= "}" do end
			c = nil
		elseif c and macros[c] then
			io.write(macros[c])
			c = nil
		else
			syntax_error("unknown character: " .. c)
		end
	end

	print("ret")

	print("section .data")
	io.write(strings)
	print("section .text")

	print("end_" .. fn_id .. ":")
end

print("section .text")

compile_fn()

io.write([[
conditional:
add r12, 16
mov eax, [r12-8]
cmp eax, 0
je .return
call [r12-16]
.return:
ret
loop:
add r12, 16
sub rsp, 16
mov rax, [r12-8]
mov [rsp], rax
mov rax, [r12-16]
mov [rsp+8], rax
.loop:
call [rsp]
add r12, 8
mov eax, [r12-8]
cmp eax, 0
je .return
call [rsp+8]
jmp .loop
.return:
add rsp, 16
ret
print_num:
mov rcx, rsp
sub rsp, 16
mov eax, dword[r12]
add r12, 8
mov ebx, eax
neg ebx
cmp eax, 0
cmovl eax, ebx
mov edi, 10
.loop:
dec rcx
xor edx, edx
idiv edi
add dl, '0'
mov byte[rcx], dl
cmp eax, 0
jne .loop
cmp ebx, 0
jle .print
dec rcx
mov byte[rcx], '-'
.print:
mov rax, 1
mov rdi, 1
mov rsi, rcx
lea rdx, [rsp+16]
sub rdx, rcx
syscall
add rsp, 16
ret
]])

io.write([[
global _start
_start:
lea r12, [stack+8*1000000]
call fun_0
mov rax, 60
mov rdi, 0
syscall
section .bss
stack: resq 1000000
]])

print("section .data")
for x = ("a"):byte(1), ("z"):byte(1) do
	print("var_" .. string.char(x) .. ": dq 0")
end