aboutsummaryrefslogtreecommitdiff
path: root/bootstrap.lua
blob: ac6cbc47cb963ee4f06ee5ddcd8e0dde4880f184 (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/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 rsi, r12
mov rcx, 1
call write
add r12, 8
]],
	["^"] = [[
call read
sub r12, 8
mov [r12], eax
]],
	["."] = [[
call print_num
]],
	["B"] = [[
call flush
]],
}

local fn_counter = 0
local str_counter = 0
local current_int

local line_number = 1
local line_position = 0

local stack_size = 8388608
local buffer_size = 8192

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
			if c == "S" then
				stack_size = current_int
				c = nil
			elseif c == "U" then
				buffer_size = current_int
				c = nil
			else
				print("sub r12, 8")
				print("mov qword[r12], " .. current_int)
			end
			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 rsi, str_" .. str_counter)
			print("mov rcx, " .. #str)
			print("call write")
			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()

print("%define STKSIZ " .. stack_size)
print("%define BUFSIZ " .. buffer_size)

io.write([[
section .data
readbuf_len: dq 0
readbuf_cursor: dq 0
writebuf_len: dq 0
section .bss
readbuf: resb BUFSIZ
writebuf: resb BUFSIZ
section .text
read:
mov rax, [readbuf_cursor]
mov rbx, [readbuf_len]
cmp rax, rbx
jb .has
cmp rbx, BUFSIZ
jb .fill
xor rbx, rbx
mov [readbuf_len], rbx
mov [readbuf_cursor], rbx
.fill:
mov rax, 0
mov rdi, 0
lea rsi, [rbx+readbuf]
mov rdx, BUFSIZ
sub rdx, rbx
syscall
add [readbuf_len], rax
cmp rax, 0
jne .has
mov eax, -1
ret
.has:
mov rax, [readbuf_cursor]
movzx eax, byte[readbuf+rax]
inc qword[readbuf_cursor]
ret
write:
mov rdi, [writebuf_len]
mov rax, BUFSIZ
sub rax, rdi
add rdi, writebuf
mov rdx, rcx
sub rdx, rax
jna .simple
mov rcx, rax
rep movsb
push rsi
push rdx
mov qword[writebuf_len], BUFSIZ
call flush
pop rdx
pop rsi
cmp rdx, BUFSIZ
ja .direct
mov rcx, rdx
mov rdi, writebuf
.simple:
add [writebuf_len], rcx
rep movsb
ret
.direct:
mov rax, 1
mov rdi, 1
syscall
ret
flush:
mov rdx, [writebuf_len]
cmp rdx, 0
je .return
mov rax, 1
mov rdi, 1
mov rsi, writebuf
syscall
mov qword[writebuf_len], 0
.return:
ret
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 rsi, rcx
lea rcx, [rsp+16]
sub rcx, rsi
call write
add rsp, 16
ret
global _start
_start:
lea r12, [data_stack+STKSIZ]
lea rsp, [call_stack+STKSIZ]
call fun_0
call flush
mov rax, 60
mov rdi, 0
syscall
section .bss
data_stack: resq STKSIZ
call_stack: resq STKSIZ
]])

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