forked from tsoding/porth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
porth.py
executable file
·2049 lines (1932 loc) · 89 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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import os
import sys
import subprocess
import shlex
from os import path
from typing import *
from enum import IntEnum, Enum, auto
from dataclasses import dataclass
from copy import copy
from time import sleep
import traceback
PORTH_EXT = '.porth'
DEFAULT_EXPANSION_LIMIT=1000
EXPANSION_DIAGNOSTIC_LIMIT=10
MEM_CAPACITY = 640_000 # should be enough for everyone
SIM_NULL_POINTER_PADDING = 1 # just a little bit of a padding at the beginning of the memory to make 0 an invalid address
SIM_STR_CAPACITY = 640_000
SIM_ARGV_CAPACITY = 640_000
debug=False
Loc=Tuple[str, int, int]
class Keyword(Enum):
IF=auto()
ELIF=auto()
ELSE=auto()
END=auto()
WHILE=auto()
DO=auto()
MACRO=auto()
INCLUDE=auto()
class Intrinsic(Enum):
PLUS=auto()
MINUS=auto()
MUL=auto()
DIVMOD=auto()
EQ=auto()
GT=auto()
LT=auto()
GE=auto()
LE=auto()
NE=auto()
SHR=auto()
SHL=auto()
OR=auto()
AND=auto()
NOT=auto()
PRINT=auto()
DUP=auto()
SWAP=auto()
DROP=auto()
OVER=auto()
ROT=auto()
MEM=auto()
LOAD=auto()
STORE=auto()
FORTH_LOAD=auto()
FORTH_STORE=auto()
LOAD64=auto()
STORE64=auto()
FORTH_LOAD64=auto()
FORTH_STORE64=auto()
CAST_PTR=auto()
ARGC=auto()
ARGV=auto()
HERE=auto()
SYSCALL0=auto()
SYSCALL1=auto()
SYSCALL2=auto()
SYSCALL3=auto()
SYSCALL4=auto()
SYSCALL5=auto()
SYSCALL6=auto()
class OpType(Enum):
PUSH_INT=auto()
PUSH_STR=auto()
PUSH_CSTR=auto()
INTRINSIC=auto()
IF=auto()
ELIF=auto()
ELSE=auto()
END=auto()
WHILE=auto()
DO=auto()
class TokenType(Enum):
WORD=auto()
INT=auto()
STR=auto()
CSTR=auto()
CHAR=auto()
KEYWORD=auto()
assert len(TokenType) == 6, "Exhaustive Token type definition. The `value` field of the Token dataclass may require an update"
@dataclass
class Token:
typ: TokenType
text: str
loc: Loc
value: Union[int, str, Keyword]
# https://www.python.org/dev/peps/pep-0484/#forward-references
expanded_from: Optional['Token'] = None
expanded_count: int = 0
OpAddr=int
@dataclass
class Op:
typ: OpType
token: Token
operand: Optional[Union[int, str, Intrinsic, OpAddr]] = None
Program=List[Op]
def get_cstr_from_mem(mem: bytearray, ptr: int) -> bytes:
end = ptr
while mem[end] != 0:
end += 1
return mem[ptr:end]
def simulate_little_endian_linux(program: Program, argv: List[str]):
AT_FDCWD=-100
O_RDONLY=0
ENOENT=2
CLOCK_MONOTONIC=1
stack: List[int] = []
mem = bytearray(SIM_NULL_POINTER_PADDING + SIM_STR_CAPACITY + SIM_ARGV_CAPACITY + MEM_CAPACITY)
str_buf_ptr = SIM_NULL_POINTER_PADDING
str_ptrs: Dict[int, int] = {}
str_size = 0
argv_buf_ptr = SIM_NULL_POINTER_PADDING + SIM_STR_CAPACITY
argc = 0
mem_buf_ptr = SIM_NULL_POINTER_PADDING + SIM_STR_CAPACITY + SIM_ARGV_CAPACITY
fds: List[BinaryIO] = [sys.stdin.buffer, sys.stdout.buffer, sys.stderr.buffer]
for arg in argv:
value = arg.encode('utf-8')
n = len(value)
arg_ptr = str_buf_ptr + str_size
mem[arg_ptr:arg_ptr+n] = value
mem[arg_ptr+n] = 0
str_size += n + 1
assert str_size <= SIM_STR_CAPACITY, "String buffer overflow"
argv_ptr = argv_buf_ptr+argc*8
mem[argv_ptr:argv_ptr+8] = arg_ptr.to_bytes(8, byteorder='little')
argc += 1
assert argc*8 <= SIM_ARGV_CAPACITY, "Argv buffer, overflow"
ip = 0
while ip < len(program):
assert len(OpType) == 10, "Exhaustive op handling in simulate_little_endian_linux"
op = program[ip]
try:
if op.typ == OpType.PUSH_INT:
assert isinstance(op.operand, int), "This could be a bug in the parsing step"
stack.append(op.operand)
ip += 1
elif op.typ == OpType.PUSH_STR:
assert isinstance(op.operand, str), "This could be a bug in the parsing step"
value = op.operand.encode('utf-8')
n = len(value)
stack.append(n)
if ip not in str_ptrs:
str_ptr = str_buf_ptr+str_size
str_ptrs[ip] = str_ptr
mem[str_ptr:str_ptr+n] = value
str_size += n
assert str_size <= SIM_STR_CAPACITY, "String buffer overflow"
stack.append(str_ptrs[ip])
ip += 1
elif op.typ == OpType.PUSH_CSTR:
assert isinstance(op.operand, str), "This could be a bug in the parsing step"
value = op.operand.encode('utf-8') + b'\0'
n = len(value)
if ip not in str_ptrs:
str_ptr = str_buf_ptr+str_size
str_ptrs[ip] = str_ptr
mem[str_ptr:str_ptr+n] = value
str_size += n
assert str_size <= SIM_STR_CAPACITY, "String buffer overflow"
stack.append(str_ptrs[ip])
ip += 1
elif op.typ == OpType.IF:
ip += 1
elif op.typ == OpType.WHILE:
ip += 1
elif op.typ == OpType.ELSE:
assert isinstance(op.operand, OpAddr), "This could be a bug in the parsing step"
ip = op.operand
elif op.typ == OpType.ELIF:
assert isinstance(op.operand, OpAddr), "This could be a bug in the parsing step"
ip = op.operand
elif op.typ == OpType.END:
assert isinstance(op.operand, OpAddr), "This could be a bug in the parsing step"
ip = op.operand
elif op.typ == OpType.DO:
a = stack.pop()
if a == 0:
assert isinstance(op.operand, OpAddr), "This could be a bug in the parsing step"
ip = op.operand
else:
ip += 1
elif op.typ == OpType.INTRINSIC:
assert len(Intrinsic) == 41, "Exhaustive handling of intrinsic in simulate_little_endian_linux()"
if op.operand == Intrinsic.PLUS:
a = stack.pop()
b = stack.pop()
stack.append(a + b)
ip += 1
elif op.operand == Intrinsic.MINUS:
a = stack.pop()
b = stack.pop()
stack.append(b - a)
ip += 1
elif op.operand == Intrinsic.MUL:
a = stack.pop()
b = stack.pop()
stack.append(b * a)
ip += 1
elif op.operand == Intrinsic.DIVMOD:
a = stack.pop()
b = stack.pop()
stack.append(b // a)
stack.append(b % a)
ip += 1
elif op.operand == Intrinsic.EQ:
a = stack.pop()
b = stack.pop()
stack.append(int(a == b))
ip += 1
elif op.operand == Intrinsic.GT:
a = stack.pop()
b = stack.pop()
stack.append(int(b > a))
ip += 1
elif op.operand == Intrinsic.LT:
a = stack.pop()
b = stack.pop()
stack.append(int(b < a))
ip += 1
elif op.operand == Intrinsic.GE:
a = stack.pop()
b = stack.pop()
stack.append(int(b >= a))
ip += 1
elif op.operand == Intrinsic.LE:
a = stack.pop()
b = stack.pop()
stack.append(int(b <= a))
ip += 1
elif op.operand == Intrinsic.NE:
a = stack.pop()
b = stack.pop()
stack.append(int(b != a))
ip += 1
elif op.operand == Intrinsic.SHR:
a = stack.pop()
b = stack.pop()
stack.append(int(b >> a))
ip += 1
elif op.operand == Intrinsic.SHL:
a = stack.pop()
b = stack.pop()
stack.append(int(b << a))
ip += 1
elif op.operand == Intrinsic.OR:
a = stack.pop()
b = stack.pop()
stack.append(int(a | b))
ip += 1
elif op.operand == Intrinsic.AND:
a = stack.pop()
b = stack.pop()
stack.append(int(a & b))
ip += 1
elif op.operand == Intrinsic.NOT:
a = stack.pop()
stack.append(int(~a))
ip += 1
elif op.operand == Intrinsic.PRINT:
a = stack.pop()
fds[1].write(b"%d\n" % a)
fds[1].flush()
ip += 1
elif op.operand == Intrinsic.DUP:
a = stack.pop()
stack.append(a)
stack.append(a)
ip += 1
elif op.operand == Intrinsic.SWAP:
a = stack.pop()
b = stack.pop()
stack.append(a)
stack.append(b)
ip += 1
elif op.operand == Intrinsic.DROP:
stack.pop()
ip += 1
elif op.operand == Intrinsic.OVER:
a = stack.pop()
b = stack.pop()
stack.append(b)
stack.append(a)
stack.append(b)
ip += 1
elif op.operand == Intrinsic.ROT:
a = stack.pop()
b = stack.pop()
c = stack.pop()
stack.append(b)
stack.append(a)
stack.append(c)
ip += 1
elif op.operand == Intrinsic.MEM:
stack.append(mem_buf_ptr)
ip += 1
elif op.operand == Intrinsic.LOAD:
addr = stack.pop()
byte = mem[addr]
stack.append(byte)
ip += 1
elif op.operand == Intrinsic.STORE:
store_value = stack.pop()
store_addr = stack.pop()
mem[store_addr] = store_value & 0xFF
ip += 1
elif op.operand == Intrinsic.FORTH_LOAD:
addr = stack.pop()
byte = mem[addr]
stack.append(byte)
ip += 1
elif op.operand == Intrinsic.FORTH_STORE:
store_addr = stack.pop()
store_value = stack.pop()
mem[store_addr] = store_value & 0xFF
ip += 1
elif op.operand == Intrinsic.LOAD64:
addr = stack.pop()
_bytes = bytearray(8)
for offset in range(0,8):
_bytes[offset] = mem[addr + offset]
stack.append(int.from_bytes(_bytes, byteorder="little"))
ip += 1
elif op.operand == Intrinsic.STORE64:
store_value = stack.pop()
store_value64 = store_value.to_bytes(length=8, byteorder="little", signed=(store_value < 0));
store_addr64 = stack.pop();
for byte in store_value64:
mem[store_addr64] = byte;
store_addr64 += 1;
ip += 1
elif op.operand == Intrinsic.FORTH_LOAD64:
addr = stack.pop()
_bytes = bytearray(8)
for offset in range(0,8):
_bytes[offset] = mem[addr + offset]
stack.append(int.from_bytes(_bytes, byteorder="little"))
ip += 1
elif op.operand == Intrinsic.FORTH_STORE64:
store_addr64 = stack.pop();
store_value = stack.pop()
store_value64 = store_value.to_bytes(length=8, byteorder="little", signed=(store_value < 0));
for byte in store_value64:
mem[store_addr64] = byte;
store_addr64 += 1;
ip += 1
elif op.operand == Intrinsic.ARGC:
stack.append(argc)
ip += 1
elif op.operand == Intrinsic.ARGV:
stack.append(argv_buf_ptr)
ip += 1
elif op.operand == Intrinsic.HERE:
value = ("%s:%d:%d" % op.token.loc).encode('utf-8')
n = len(value)
stack.append(n)
if ip not in str_ptrs:
str_ptr = str_buf_ptr+str_size
str_ptrs[ip] = str_ptr
mem[str_ptr:str_ptr+n] = value
str_size += n
assert str_size <= SIM_STR_CAPACITY, "String buffer overflow"
stack.append(str_ptrs[ip])
ip += 1
elif op.operand == Intrinsic.CAST_PTR:
# Ignore the type casting. It's only useful for type_check_program() phase
ip += 1
elif op.operand == Intrinsic.SYSCALL0:
syscall_number = stack.pop();
if syscall_number == 39: # SYS_getpid
stack.append(os.getpid());
else:
assert False, "unknown syscall number %d" % syscall_number
ip += 1
elif op.operand == Intrinsic.SYSCALL1:
syscall_number = stack.pop()
arg1 = stack.pop()
if syscall_number == 60: # SYS_exit
exit(arg1)
elif syscall_number == 3: # SYS_close
fds[arg1].close()
stack.append(0)
else:
assert False, "unknown syscall number %d" % syscall_number
ip += 1
elif op.operand == Intrinsic.SYSCALL2:
assert False, "not implemented"
elif op.operand == Intrinsic.SYSCALL3:
syscall_number = stack.pop()
arg1 = stack.pop()
arg2 = stack.pop()
arg3 = stack.pop()
if syscall_number == 0: # SYS_read
fd = arg1
buf = arg2
count = arg3
# NOTE: trying to behave like a POSIX tty in canonical mode by making the data available
# on each newline
# https://en.wikipedia.org/wiki/POSIX_terminal_interface#Canonical_mode_processing
# TODO: maybe this behavior should be customizable
data = fds[fd].readline(count)
mem[buf:buf+len(data)] = data
stack.append(len(data))
elif syscall_number == 1: # SYS_write
fd = arg1
buf = arg2
count = arg3
fds[fd].write(mem[buf:buf+count])
fds[fd].flush()
stack.append(count)
elif syscall_number == 257: # SYS_openat
dirfd = arg1
pathname_ptr = arg2
flags = arg3
if dirfd != AT_FDCWD:
assert False, "openat: unsupported dirfd"
if flags != O_RDONLY:
assert False, "openat: unsupported flags"
pathname = get_cstr_from_mem(mem, pathname_ptr).decode('utf-8')
fd = len(fds)
try:
fds.append(open(pathname, 'rb'))
stack.append(fd)
except FileNotFoundError:
stack.append(-ENOENT)
else:
assert False, "unknown syscall number %d" % syscall_number
ip += 1
elif op.operand == Intrinsic.SYSCALL4:
syscall_number = stack.pop()
arg1 = stack.pop()
arg2 = stack.pop()
arg3 = stack.pop()
arg4 = stack.pop()
if syscall_number == 230: # clock_nanosleep
clock_id = arg1
flags = arg2
request_ptr = arg3
remain_ptr = arg4
assert clock_id == CLOCK_MONOTONIC, "Only CLOCK_MONOTONIC is implemented for SYS_clock_nanosleep"
assert flags == 0, "Only relative time is supported for SYS_clock_nanosleep"
assert request_ptr != 0, "request cannot be NULL for SYS_clock_nanosleep. We should probably return -1 in that case..."
assert remain_ptr == 0, "remain is not supported for SYS_clock_nanosleep"
seconds = int.from_bytes(mem[request_ptr:request_ptr+8], byteorder='little')
nano_seconds = int.from_bytes(mem[request_ptr+8:request_ptr+8+8], byteorder='little')
sleep(float(seconds)+float(nano_seconds)*1e-09)
stack.append(0)
else:
assert False, "unknown syscall number %d" % syscall_number
ip += 1
elif op.operand == Intrinsic.SYSCALL5:
assert False, "not implemented"
elif op.operand == Intrinsic.SYSCALL6:
assert False, "not implemented"
else:
assert False, "unreachable"
else:
assert False, "unreachable"
except Exception as e:
compiler_error_with_expansion_stack(op.token, "Python Exception during simulation")
traceback.print_exception(type(e), e, e.__traceback__)
exit(1)
if debug:
print("[INFO] Memory dump")
print(mem[:20])
class DataType(IntEnum):
INT=auto()
BOOL=auto()
PTR=auto()
def compiler_diagnostic(loc: Loc, tag: str, message: str):
print("%s:%d:%d: %s: %s" % (loc + (tag, message)), file=sys.stderr)
def compiler_diagnostic_with_expansion_stack(token: Token, tag: str, message: str):
compiler_diagnostic(token.loc, tag, message)
stack = token.expanded_from
limit = 0
while stack is not None and limit <= EXPANSION_DIAGNOSTIC_LIMIT:
compiler_note(stack.loc, "expanded from `%s`" % stack.text)
stack = stack.expanded_from
limit += 1
if limit > EXPANSION_DIAGNOSTIC_LIMIT:
print('...', file=sys.stderr)
print('... too many expansions ...', file=sys.stderr)
print('...', file=sys.stderr)
def compiler_error(loc: Loc, message: str):
compiler_diagnostic(loc, 'ERROR', message)
def compiler_error_with_expansion_stack(token: Token, message: str):
compiler_diagnostic_with_expansion_stack(token, 'ERROR', message)
def compiler_note(loc: Loc, message: str):
compiler_diagnostic(loc, 'NOTE', message)
def not_enough_arguments(op: Op):
if op.typ == OpType.INTRINSIC:
assert isinstance(op.operand, Intrinsic)
compiler_error_with_expansion_stack(op.token, "not enough arguments for the `%s` intrinsic" % INTRINSIC_NAMES[op.operand])
# TODO: why don't we add while-do here too?
elif op.typ == OpType.IF:
compiler_error_with_expansion_stack(op.token, "not enough arguments for the if-block")
else:
assert False, "unsupported type of operation"
DataStack=List[Tuple[DataType, Token]]
# TODO: `if 1 10 < do 69 32 elif 2 10 < do 420 end` does not properly type check
def type_check_program(program: Program):
stack: DataStack = []
block_stack: List[Tuple[DataStack, OpType]] = []
for ip in range(len(program)):
op = program[ip]
assert len(OpType) == 10, "Exhaustive ops handling in type_check_program()"
if op.typ == OpType.PUSH_INT:
stack.append((DataType.INT, op.token))
elif op.typ == OpType.PUSH_STR:
stack.append((DataType.INT, op.token))
stack.append((DataType.PTR, op.token))
elif op.typ == OpType.PUSH_CSTR:
stack.append((DataType.PTR, op.token))
elif op.typ == OpType.INTRINSIC:
assert len(Intrinsic) == 41, "Exhaustive intrinsic handling in type_check_program()"
assert isinstance(op.operand, Intrinsic), "This could be a bug in compilation step"
if op.operand == Intrinsic.PLUS:
assert len(DataType) == 3, "Exhaustive type handling in PLUS intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if a_type == DataType.INT and b_type == DataType.INT:
stack.append((DataType.INT, op.token))
elif a_type == DataType.INT and b_type == DataType.PTR:
stack.append((DataType.PTR, op.token))
elif a_type == DataType.PTR and b_type == DataType.INT:
stack.append((DataType.PTR, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument types for PLUS intrinsic. Expected INT or PTR")
exit(1)
elif op.operand == Intrinsic.MINUS:
assert len(DataType) == 3, "Exhaustive type handling in MINUS intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if a_type == b_type and (a_type == DataType.INT or a_type == DataType.PTR):
stack.append((DataType.INT, op.token))
elif b_type == DataType.PTR and a_type == DataType.INT:
stack.append((DataType.PTR, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument types fo MINUS intrinsic: %s" % [b_type, a_type])
exit(1)
elif op.operand == Intrinsic.MUL:
assert len(DataType) == 3, "Exhaustive type handling in MUL intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if a_type == b_type and a_type == DataType.INT:
stack.append((DataType.INT, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument types fo MUL intrinsic. Expected INT.")
exit(1)
elif op.operand == Intrinsic.DIVMOD:
assert len(DataType) == 3, "Exhaustive type handling in DIVMOD intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if a_type == b_type and a_type == DataType.INT:
stack.append((DataType.INT, op.token))
stack.append((DataType.INT, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument types fo DIVMOD intrinsic. Expected INT.")
exit(1)
elif op.operand == Intrinsic.EQ:
assert len(DataType) == 3, "Exhaustive type handling in EQ intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if a_type == b_type and a_type == DataType.INT:
stack.append((DataType.BOOL, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument types fo EQ intrinsic. Expected INT.")
exit(1)
elif op.operand == Intrinsic.GT:
assert len(DataType) == 3, "Exhaustive type handling in GT intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if a_type == b_type and a_type == DataType.INT:
stack.append((DataType.BOOL, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for GT intrinsic")
exit(1)
elif op.operand == Intrinsic.LT:
assert len(DataType) == 3, "Exhaustive type handling in LT intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if a_type == b_type and a_type == DataType.INT:
stack.append((DataType.BOOL, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for LT intrinsic")
exit(1)
elif op.operand == Intrinsic.GE:
assert len(DataType) == 3, "Exhaustive type handling in GE intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if a_type == b_type and a_type == DataType.INT:
stack.append((DataType.BOOL, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for GE intrinsic")
exit(1)
elif op.operand == Intrinsic.LE:
assert len(DataType) == 3, "Exhaustive type handling in LE intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if a_type == b_type and a_type == DataType.INT:
stack.append((DataType.BOOL, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for LE intrinsic")
exit(1)
elif op.operand == Intrinsic.NE:
assert len(DataType) == 3, "Exhaustive type handling in NE intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if a_type == b_type and a_type == DataType.INT:
stack.append((DataType.BOOL, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for NE intrinsic")
exit(1)
elif op.operand == Intrinsic.SHR:
assert len(DataType) == 3, "Exhaustive type handling in SHR intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if a_type == b_type and a_type == DataType.INT:
stack.append((DataType.INT, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for SHR intrinsic")
exit(1)
elif op.operand == Intrinsic.SHL:
assert len(DataType) == 3, "Exhaustive type handling in SHL intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if a_type == b_type and a_type == DataType.INT:
stack.append((DataType.INT, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for SHL intrinsic")
exit(1)
elif op.operand == Intrinsic.OR:
assert len(DataType) == 3, "Exhaustive type handling in OR intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if a_type == b_type and a_type == DataType.INT:
stack.append((DataType.INT, op.token))
elif a_type == b_type and a_type == DataType.BOOL:
stack.append((DataType.BOOL, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for OR intrinsic")
exit(1)
elif op.operand == Intrinsic.AND:
assert len(DataType) == 3, "Exhaustive type handling in AND intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if a_type == b_type and a_type == DataType.INT:
stack.append((DataType.INT, op.token))
elif a_type == b_type and a_type == DataType.BOOL:
stack.append((DataType.BOOL, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for AND intrinsic")
exit(1)
elif op.operand == Intrinsic.NOT:
assert len(DataType) == 3, "Exhaustive type handling in NOT intrinsic"
if len(stack) < 1:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
if a_type == DataType.INT:
stack.append((DataType.INT, op.token))
elif a_type == DataType.BOOL:
stack.append((DataType.BOOL, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for NOT intrinsic")
exit(1)
elif op.operand == Intrinsic.PRINT:
if len(stack) < 1:
not_enough_arguments(op)
exit(1)
stack.pop()
elif op.operand == Intrinsic.DUP:
if len(stack) < 1:
not_enough_arguments(op)
exit(1)
a = stack.pop()
stack.append(a)
stack.append(a)
elif op.operand == Intrinsic.SWAP:
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a = stack.pop()
b = stack.pop()
stack.append(a)
stack.append(b)
elif op.operand == Intrinsic.DROP:
if len(stack) < 1:
not_enough_arguments(op)
exit(1)
stack.pop()
elif op.operand == Intrinsic.OVER:
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a = stack.pop()
b = stack.pop()
stack.append(b)
stack.append(a)
stack.append(b)
elif op.operand == Intrinsic.ROT:
if len(stack) < 3:
not_enough_arguments(op)
exit(1)
a = stack.pop()
b = stack.pop()
c = stack.pop()
stack.append(b)
stack.append(a)
stack.append(c)
elif op.operand == Intrinsic.MEM:
stack.append((DataType.PTR, op.token))
elif op.operand == Intrinsic.LOAD:
assert len(DataType) == 3, "Exhaustive type handling in LOAD intrinsic"
if len(stack) < 1:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
if a_type == DataType.PTR:
stack.append((DataType.INT, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for LOAD intrinsic: %s" % a_type)
exit(1)
elif op.operand == Intrinsic.STORE:
assert len(DataType) == 3, "Exhaustive type handling in STORE intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if a_type == DataType.INT and b_type == DataType.PTR:
pass
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for STORE intrinsic")
exit(1)
elif op.operand == Intrinsic.FORTH_LOAD:
assert len(DataType) == 3, "Exhaustive type handling in LOAD intrinsic"
if len(stack) < 1:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
if a_type == DataType.PTR:
stack.append((DataType.INT, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for LOAD intrinsic: %s" % a_type)
exit(1)
elif op.operand == Intrinsic.FORTH_STORE:
assert len(DataType) == 3, "Exhaustive type handling in STORE intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if a_type == DataType.PTR and b_type == DataType.INT:
pass
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for STORE intrinsic")
exit(1)
elif op.operand == Intrinsic.LOAD64:
assert len(DataType) == 3, "Exhaustive type handling in LOAD64 intrinsic"
if len(stack) < 1:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
if a_type == DataType.PTR:
stack.append((DataType.INT, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for LOAD64 intrinsic")
exit(1)
elif op.operand == Intrinsic.STORE64:
assert len(DataType) == 3, "Exhaustive type handling in STORE64 intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if (a_type == DataType.INT or a_type == DataType.PTR) and b_type == DataType.PTR:
pass
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for STORE64 intrinsic: %s" % [b_type, a_type])
exit(1)
elif op.operand == Intrinsic.FORTH_LOAD64:
assert len(DataType) == 3, "Exhaustive type handling in LOAD64 intrinsic"
if len(stack) < 1:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
if a_type == DataType.PTR:
stack.append((DataType.INT, op.token))
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for LOAD64 intrinsic")
exit(1)
elif op.operand == Intrinsic.FORTH_STORE64:
assert len(DataType) == 3, "Exhaustive type handling in STORE64 intrinsic"
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
a_type, a_loc = stack.pop()
b_type, b_loc = stack.pop()
if (b_type == DataType.INT or b_type == DataType.PTR) and a_type == DataType.PTR:
pass
else:
compiler_error_with_expansion_stack(op.token, "invalid argument type for STORE64 intrinsic: %s" % [b_type, a_type])
exit(1)
elif op.operand == Intrinsic.CAST_PTR:
if len(stack) < 1:
not_enough_arguments(op)
exit(1)
a_type, a_token = stack.pop()
stack.append((DataType.PTR, a_token))
elif op.operand == Intrinsic.ARGC:
stack.append((DataType.INT, op.token))
elif op.operand == Intrinsic.ARGV:
stack.append((DataType.PTR, op.token))
elif op.operand == Intrinsic.HERE:
stack.append((DataType.INT, op.token))
stack.append((DataType.PTR, op.token))
# TODO: figure out how to type check syscall arguments and return types
elif op.operand == Intrinsic.SYSCALL0:
if len(stack) < 1:
not_enough_arguments(op)
exit(1)
for i in range(1):
stack.pop()
stack.append((DataType.INT, op.token))
elif op.operand == Intrinsic.SYSCALL1:
if len(stack) < 2:
not_enough_arguments(op)
exit(1)
for i in range(2):
stack.pop()
stack.append((DataType.INT, op.token))
elif op.operand == Intrinsic.SYSCALL2:
if len(stack) < 3:
not_enough_arguments(op)
exit(1)
for i in range(3):
stack.pop()
stack.append((DataType.INT, op.token))
elif op.operand == Intrinsic.SYSCALL3:
if len(stack) < 4:
not_enough_arguments(op)
exit(1)
for i in range(4):
stack.pop()
stack.append((DataType.INT, op.token))
elif op.operand == Intrinsic.SYSCALL4:
if len(stack) < 5:
not_enough_arguments(op)
exit(1)
for i in range(5):
stack.pop()
stack.append((DataType.INT, op.token))
elif op.operand == Intrinsic.SYSCALL5:
if len(stack) < 6:
not_enough_arguments(op)
exit(1)
for i in range(6):
stack.pop()
stack.append((DataType.INT, op.token))
elif op.operand == Intrinsic.SYSCALL6:
if len(stack) < 7:
not_enough_arguments(op)
exit(1)
for i in range(7):
stack.pop()
stack.append((DataType.INT, op.token))
else:
assert False, "unreachable"
elif op.typ == OpType.IF:
block_stack.append((copy(stack), op.typ))
elif op.typ == OpType.WHILE:
block_stack.append((copy(stack), op.typ))
elif op.typ == OpType.END:
block_snapshot, block_type = block_stack.pop()
assert len(OpType) == 10, "Exhaustive handling of op types"