-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathporth.py
executable file
·370 lines (342 loc) · 12.6 KB
/
porth.py
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
#!/usr/bin/env python3
import sys
import subprocess
from shlex import quote
from os import path
iota_counter = 0
def iota(reset=False):
global iota_counter
if reset:
iota_counter = 0
result = iota_counter
iota_counter +=1
return result
OP_PUSH = iota(True)
OP_PLUS = iota()
OP_MINUS = iota()
OP_EQUAL = iota()
OP_DUMP = iota()
OP_IF = iota()
OP_END = iota()
OP_ELSE = iota()
OP_DUP = iota()
OP_GT = iota()
OP_WHILE = iota()
OP_DO = iota()
COUNT_OPS = iota()
def push(x):
return (OP_PUSH, x)
def plus():
return (OP_PLUS, )
def minus():
return (OP_MINUS, )
def equal():
return (OP_EQUAL, )
def dump():
return (OP_DUMP, )
def iff():
return (OP_IF, )
def end():
return (OP_END, )
def elze():
return (OP_ELSE, )
def dup():
return (OP_DUP, )
def gt():
return (OP_GT, )
def wile():
return (OP_WHILE, )
def doo():
return (OP_DO, )
def simulate_program(program):
stack = []
ip = 0
while ip < len(program):
assert COUNT_OPS == 12, "Exhaustive handling of operations in simulation"
op = program[ip]
if op[0] == OP_PUSH:
stack.append(op[1])
ip += 1
elif op[0] == OP_PLUS:
a = stack.pop()
b = stack.pop()
stack.append(a + b)
ip += 1
elif op[0] == OP_MINUS:
a = stack.pop()
b = stack.pop()
stack.append(b - a)
ip += 1
elif op[0] == OP_EQUAL:
a = stack.pop()
b = stack.pop()
stack.append(int(a == b))
ip += 1
elif op[0] == OP_IF:
a = stack.pop()
if a == 0:
assert len(op) >= 2, "'if' instruction does not have a reference to the end of its block. Please call cross_referenceblocks() on the program before trying to simulate it"
ip = op[1]
else:
ip += 1
elif op[0] == OP_ELSE:
assert len(op) >= 2, "'else' instruction does not have a reference to the end of its block. Please call cross_referenceblocks() on the program before trying to simulate it"
ip = op[1]
elif op[0] == OP_END:
assert len(op) >= 2, "'end' instruction does not have a reference to the next instruction to jump to. please call cross_referenceblocks() on the program before trying to simulate it"
ip = op[1]
elif op[0] == OP_DUP:
a = stack.pop()
stack.append(a)
stack.append(a)
ip += 1
elif op[0] == OP_DUMP:
a = stack.pop()
print(a)
ip += 1
elif op[0] == OP_GT:
a = stack.pop()
b = stack.pop()
stack.append(int(a < b))
ip += 1
elif op[0] == OP_WHILE:
ip += 1
elif op[0] == OP_DO:
a = stack.pop()
if a == 0:
assert len(op) >= 2, "'end' instruction does not have a reference to the next instruction to jump to. please call cross_referenceblocks() on the program before trying to simulate it"
ip = op[1]
else:
ip += 1
else:
assert False, "unreachable"
def compile_program(program, out_file_path):
with open(out_file_path, "w") as out:
out.write("BITS 64\n")
out.write("segment .text\n")
out.write("dump:\n")
out.write(" mov r9, -3689348814741910323\n")
out.write(" sub rsp, 40\n")
out.write(" mov BYTE [rsp+31], 10\n")
out.write(" lea rcx, [rsp+30]\n")
out.write(".L2:\n")
out.write(" mov rax, rdi\n")
out.write(" lea r8, [rsp+32]\n")
out.write(" mul r9\n")
out.write(" mov rax, rdi\n")
out.write(" sub r8, rcx\n")
out.write(" shr rdx, 3\n")
out.write(" lea rsi, [rdx+rdx*4]\n")
out.write(" add rsi, rsi\n")
out.write(" sub rax, rsi\n")
out.write(" add eax, 48\n")
out.write(" mov BYTE [rcx], al\n")
out.write(" mov rax, rdi\n")
out.write(" mov rdi, rdx\n")
out.write(" mov rdx, rcx\n")
out.write(" sub rcx, 1\n")
out.write(" cmp rax, 9\n")
out.write(" ja .L2\n")
out.write(" lea rax, [rsp+32]\n")
out.write(" mov edi, 1\n")
out.write(" sub rdx, rax\n")
out.write(" xor eax, eax\n")
out.write(" lea rsi, [rsp+32+rdx]\n")
out.write(" mov rdx, r8\n")
out.write(" mov rax, 1\n")
out.write(" syscall\n")
out.write(" add rsp, 40\n")
out.write(" ret\n")
out.write("global _start\n")
out.write("_start:\n")
for ip in range(len(program)):
op = program[ip]
assert COUNT_OPS == 10, "Exhaustive handling of operations in simulation"
if op[0] == OP_PUSH:
out.write(" ;; -- push %d --\n" % op[1])
out.write(" push %d\n" % op[1])
elif op[0] == OP_PLUS:
out.write(" ;; -- plus --\n")
out.write(" pop rax\n")
out.write(" pop rbx\n")
out.write(" add rax, rbx\n")
out.write(" push rax\n")
elif op[0] == OP_MINUS:
out.write(" ;; -- minus --\n")
out.write(" pop rax\n")
out.write(" pop rbx\n")
out.write(" sub rbx, rax\n")
out.write(" push rbx\n")
elif op[0] == OP_EQUAL:
out.write(" ;; -- equal --\n")
out.write(" mov rcx, 0\n")
out.write(" mov rdx, 1\n")
out.write(" pop rax\n")
out.write(" pop rbx\n")
out.write(" cmp rax, rbx\n")
out.write(" cmove rcx, rdx\n")
out.write(" push rcx\n")
elif op[0] == OP_DUMP:
out.write(" ;; -- dump --\n")
out.write(" pop rdi\n")
out.write(" call dump\n")
elif op[0] == OP_IF:
out.write(" ;; -- if --\n")
out.write(" pop rax\n")
out.write(" test rax, rax\n")
assert len(op) >= 2, "'if' instruction does not have a reference to the end of its block. Please call crossreference_blocks() on the program before trying to compile it."
out.write(" jz addr_%d\n" % op[1])
elif op[0] == OP_ELSE:
out.write(" ;; -- else --\n")
assert len(op) >= 2, "'else' instruction does not have a reference to the end of its block. Please call crossreference_blocks() on the program before trying to compile it."
out.write(" jmp addr_%d\n" % op[1])
out.write("addr_%d:\n" % (ip + 1))
elif op[0] == OP_END:
out.write("addr_%d:\n" % ip)
elif op[0] == OP_DUP:
out.write(" ;; -- dup --\n")
out.write(" pop rax\n")
out.write(" push rax\n")
out.write(" push rax\n")
elif op[0] == OP_GT:
out.write(" ;; -- gt --\n")
out.write(" mov rcx, 0\n")
out.write(" mov rdx, 1\n")
out.write(" pop rbx\n")
out.write(" pop rax\n")
out.write(" cmp rax, rbx\n")
out.write(" cmovg rcx, rdx\n")
out.write(" push rcx\n")
else:
assert False, "unreachable";
out.write(" mov rax, 60\n")
out.write(" mov rdi, 0\n")
out.write(" syscall\n")
def parse_token_as_op(token):
(file_path, row, col, word) = token
assert COUNT_OPS == 12, "Exhaustive op handling in parse_token_as_op"
if word == '+':
return plus()
elif word == '-':
return minus()
elif word == '.':
return dump()
elif word == '=':
return equal()
elif word == "if":
return iff()
elif word == "end":
return end()
elif word == "else":
return elze()
elif word == "dup":
return dup()
elif word == ">":
return gt()
elif word == "while":
return wile()
elif word == "do":
return doo()
else:
try:
return push(int(word))
except ValueError as err:
print("%s:%d:%d: %s" % (file_path, row, col, err))
exit(1)
def crossreference_blocks(program):
stack = []
for ip in range (len(program)):
op = program[ip]
assert COUNT_OPS == 12, "Exhaustive handling of ops in crossreference_program. Keep in mind that not all of the ops need to be handled in here. Only those that form blocks."
if op[0] == OP_IF:
stack.append(ip)
elif op[0] == OP_ELSE:
if_ip = stack.pop()
assert program[if_ip][0] == OP_IF, "`else` can only be used in `if`-blocks"
program[if_ip] = (OP_IF, ip + 1)
stack.append(ip)
elif op[0] == OP_END:
block_ip = stack.pop()
if program[block_ip][0] == OP_IF or program[block_ip][0] == OP_ELSE:
program[block_ip] = (program[block_ip][0], ip)
program[ip] = (OP_END, ip + 1)
elif program[block_ip][0] == OP_DO:
assert len(program[block_ip]) >= 2
program[ip] = (OP_END, program[block_ip][1])
program[block_ip] = (OP_DO, ip + 1)
else:
assert False, "`end` can only close `if`, `else` or `do` blocks for now."
elif op[0] == OP_WHILE:
stack.append(ip)
elif op[0] == OP_DO:
wile_ip = stack.pop()
program[ip] = (OP_DO, wile_ip)
stack.append(ip)
return program
def find_col(line, start, predicate):
while start < len(line) and not predicate(line[start]):
start += 1
return start
def lex_line(line):
col = find_col(line, 0, lambda x: not x.isspace())
while col < len(line):
col_end = find_col(line, col, lambda x: x.isspace())
yield(col, line[col:col_end])
col = find_col(line, col_end, lambda x: not x.isspace())
# TODO: Lexer does not support any style of comments
def lex_file(file_path):
with open(file_path, "r") as f:
for (row, line) in enumerate(f.readlines()):
for (col, token) in lex_line(line):
yield(file_path, row, col, token)
def load_program_from_file(file_path):
return crossreference_blocks([parse_token_as_op(token) for token in lex_file(file_path)])
def cmd_echoed(cmd):
print("[CMD] %s" % " ".join(map(quote, cmd)))
subprocess.call(cmd)
def usage():
print("Usage: %s <SUBCOMMAND> [ARGS]" % program_name)
print("SUBCOMMANDS:")
print(" sim <file> Simulate the program")
print(" com <file> Compile the program")
print(" help Print this help to stdout and exit with 0 code")
if __name__ == '__main__':
argv = sys.argv
assert len(argv) >= 1
compiler_name, *argv = argv
if len(argv) < 1:
usage(compiler_name)
print("ERROR: no subcommand is provided")
exit(1)
subcommand, *argv = argv
if subcommand == "sim":
if len(argv) < 1:
usage(compiler_name)
print("ERROR: no input file is provided for the simulation")
exit(1)
program_path, *argv = argv
program = load_program_from_file(program_path);
simulate_program(program)
elif subcommand == "com":
# TOTO: -r flag for com that runs application upon successful compilation
if len(argv) < 1:
usage(compiler_name)
print("ERROR: no input file is provided for the compilation")
exit(1)
program_path, *argv = argv
program = load_program_from_file(program_path);
porth_ext = '.porth'
basename = path.basename(program_path)
if basename.endswith(porth_ext):
basename = basename[:-len(porth_ext)]
print("[INFO] Generating %s" % (basename + ".asm"))
compile_program(program, basename + ".asm")
cmd_echoed(["nasm", "-felf64", basename + ".asm"])
cmd_echoed(["ld", "-o", basename, basename + ".o"])
elif subcommand == "help":
usage(compiler_name)
exit(1)
else:
usage()
print("ERROR: unknown subcommand %s" % (subcommand))
exit(1)