-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbootstrap4.s
4087 lines (3548 loc) · 66.9 KB
/
bootstrap4.s
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
# Stage 4 bootstrap
# =================
#
# Historical note: this was previously bootstrap3 and used the raw VM
# opcodes. We introduced a new bootstrap3 in 2024 and mechanically
# translated each line 1:1 with the newer syntax to make this stage
# easier to maintain.
#
# Implements an assembler that supports a much richer, more human-readable format
#
# Includes real, two-pass label support (up to 32 chars long), simple call/return semantics
# TODO:
# - During mechanical translation, labels were not expanded to 8+ chars,
# and we did not trim the extraneous trailing underscores that are no
# longer necessary.
# - Polish/performance:
# - Symbol table with local symbs should be "rolled back" at next global symbol for perf
# - Can we do local fixups per global?
# - Short immediate constants should use '=!x.' format
# - readtok_ subroutines should be real functions
# - macro for locals/args copy (ie: r0->r4, r1->r5, pushed/restored automatically)
# Register notes:
#
# Ra-Re = 0, 1, 2, 4, 8 values
# Rx = Temp var
# Ry = Stack pointer
# Rz = PC
:entry___
# Ra = Zero register
sub ra, ra
# Rb = One register
ldh rb, #0001
# Rc = Two register
ldh rc, #0002
# Rd = Four register
ldh rd, #0004
# Re = Eight register
ldh re, #0008
# Set stack to memsize
ldc r0, :SC_GTMEM
sys r0
mov ry, r0
sub ry, rd
jump :main____
=newline_ 000a
=hash____ 0023
=colon___ 003a
=tab_____ 0009
=space___ 0020
=equals__ 003d
=dollar__ 0024
=question 003f
=hat_____ 005e
=zero____ 0030
=amp_____ 0026
=comma___ 002c
=plus____ 002b
=minus___ 002d
=left[___ 005b
=right]__ 005d
=undersc_ 005f
=left{___ 007b
=left(___ 0028
=at______ 0040
=period__ 002e
=L_______ 004c
=S_______ 0053
=n_______ 006e
=r_______ 0072
=x_______ 0078
=y_______ 0079
=z_______ 007a
=question 003f
=exclaim_ 0021
=gt______ 003e
=lt______ 003c
=multiply 002a
=div_____ 002f
=and_____ 0026
=or______ 007c
=quote___ 0022
=squote__ 0027
=percent_ 0025
=bslash__ 005c
=fslash__ 002f
# Program counter
=R_pc____ 003d
# Stack pointer
=R_sp____ 003c
# Compiler temporary
=R_ctmp__ 003b
# Compiler temporary for %call
=R_ctmp2_ 003a
# EOF
=T_EOF___ 0000
# Immediate constant (data = value)
=T_IMM___ 0001
# Immediate indirect (data = value)
=T_IMI___ 0002
# Reference (constant or label, data = ptr to zero-terminated label)
=T_REF___ 0003
# Reference indirect (constant or label, data = ptr to zero-terminated label)
=T_RFI___ 0004
# Instruction (data = ins handler function)
=T_INS___ 0005
# Register (data = reg #)
=T_REG___ 0006
# Register indirect (data = reg #)
=T_RGI___ 0007
# EOL
=T_EOL___ 0008
# String
=T_STR___ 0009
# Define
=T_DEF___ 000a
# Include
=T_INC___ 000b
# String immediate
=T_SIM___ 000c
=INS_UNCO 0000
=INS_IF_T 0001
=INS_IF_F 0002
=OPEN_RO_ 0000
=OPEN_RW_ 0001
:tokens__
data EOF\00
data IMM\00
data IMI\00
data REF\00
data RFI\00
data INS\00
data REG\00
data RGI\00
data EOL\00
data STR\00
data DEF\00
data INC\00
data SIM\00
=SC_OPEN_ 0000
=O_RDONLY 0000
=O_WRONLY 0001
=O_RDWR__ 0002
=O_TRUNC_ 0200
=O_CREAT_ 0400
=SC_READ_ 0001
=SC_WRITE 0002
=SC_SEEK_ 0003
=SEEK_SET 0000
=SEEK_CUR 0001
=SEEK_END 0002
=SC_CLOSE 0004
=SC_GTARG 0005
=SC_GTMEM 0006
=SC_EXIT_ 0007
=SC_OPNAT 0008
=JUMPEND_ fefe
# Too large for a normal constant!
:AT_FDCWD
data \38\ff\ff\ff
:inclhand
dd 0
# Stack of input file handles, used for #include
:in_hands
dd 0
# Global: Output file handle
:out_hand
dd 0
#===========================================================================
# Special jump table proc
# Preserves all registers except x
# Args:
# Stack 0: Lookup
# Stack 1: Table
#===========================================================================
:jumptabl
# Stash the return value in .ret_____
pop r0
ldc rx, .ret_____
std [rx], r0
# Table address
pop r0
mov rx, r0
# Lookup value
pop r0
# Preserve R0-R3
push r1
push r2
push r3
# R3 holds the table
mov r3, rx
# R2 holds the magic constant
ldc r2, :JUMPEND_
.loop____
ldd r1, [r3]
eq r1, r2
jump? .done____
eq r1, r0
jump? .found___
add r3, re
jump .loop____
.found___
add r3, rd
# Put the address in Rx
ldd rx, [r3]
pop r3
pop r2
pop r1
# Jump (not call)
mov rz, rx
.done____
# Restore
pop r3
pop r2
pop r1
# Manual jump
ldc rx, .ret_____
ldd rx, [rx]
mov rz, rx
.ret_____
dd 0
#===========================================================================
#===========================================================================
# Args:
# R0: Error string
# Does not return
#===========================================================================
:error___
# Stash R0 in stack
push r0
call :strlen__
pop r3
ldc r1, :SC_WRITE
ldh r2, #0002
sys r1, r2, r3, r0
# Write a newline
ldc r1, :SC_WRITE
ldh r3, #000a
stb [r3], r3
sys r1, r2, r3, rb
# Exit with code 1
ldc r0, :SC_EXIT_
ldh r1, #0001
sys r0, r1
#===========================================================================
#===========================================================================
# Register-friendly stderr logging.
# Args:
# R0: String
# Returns:
# All registers: Unchanged
#===========================================================================
:log_____
eq r0, ra
ret?
push r1
push r2
push r0
call :strlen__
mov r2, r0
pop r0
ldc r1, :SC_WRITE
sys r1, rc, r0, r2
pop r2
pop r1
ret
.stash___
dd 0
#===========================================================================
#===========================================================================
# Register-friendly stderr logging.
# Args:
# R0: Number
# Returns:
# All registers: Unchanged
#===========================================================================
:lognum__
push r0
push r1
push r2
push r0
# Clear the buffer
ldc r0, .buffer__
mov r1, ra
ldh r2, #0010
call :memset__
pop r0
# Start at the end of the buffer
ldc r1, .buffer__
ldh rx, #000e
add r1, rx
.loop____
mov r2, r0
# Get the lowest digit
ldh rx, #000a
mod r2, rx
# Get the ASCII version
ldc rx, .digits__
add r2, rx
ldb r2, [r2]
# Write it to the buffer
stb [r1], r2
sub r1, rb
ldh rx, #000a
# If we still have digits to write, continue
div r0, rx
gt r0, ra
jump? .loop____
mov r0, r1
add r0, rb
call :log_____
pop r2
pop r1
pop r0
ret
.buffer__
dd 0
dd 0
dd 0
dd 0
.digits__
data 0123456789__
#===========================================================================
#===========================================================================
# Register-friendly stderr logging.
# Args:
# R0: Number
# Returns:
# All registers: Unchanged
#===========================================================================
:lognumh_
push r0
push r1
push r2
push r0
# Clear the buffer
ldc r0, .buffer__
mov r1, ra
ldh r2, #0010
call :memset__
pop r0
# Start at the end of the buffer
ldc r1, .buffer__
ldh rx, #000e
add r1, rx
.loop____
mov r2, r0
# Get the lowest digit
ldh rx, #0010
mod r2, rx
# Get the ASCII version
ldc rx, .digits__
add r2, rx
ldb r2, [r2]
# Write it to the buffer
stb [r1], r2
sub r1, rb
ldh rx, #0010
# If we still have digits to write, continue
div r0, rx
gt r0, ra
jump? .loop____
mov r0, r1
ldc rx, :x_______
stb [r0], rx
sub r0, rb
ldc rx, :zero____
stb [r0], rx
call :log_____
pop r2
pop r1
pop r0
ret
.buffer__
dd 0
dd 0
dd 0
dd 0
.digits__
data 0123456789abcdef
#===========================================================================
#===========================================================================
# Does not return
#===========================================================================
:exit____
ldc r0, :SC_EXIT_
sub r1, r1
sys r0, r1
#===========================================================================
#===========================================================================
# Args:
# R0: Which
# Returns:
# R0: Pointer to string (zero terminated)
#===========================================================================
:getargv_
ldc rx, :args____
mul r0, rd
add r0, rx
ldd r0, [r0]
ret
#===========================================================================
#===========================================================================
# Returns:
# R0: Argument count (includes argv[0] which is the binary)
#===========================================================================
:getargc_
ldc r1, :args____
sub r0, r0
.loop____
ldd rx, [r1]
eq rx, ra
ret?
add r0, rb
add r1, rd
jump .loop____
#===========================================================================
#===========================================================================
# Args:
# R0: String
# Returns:
# R0: Length
#===========================================================================
:strlen__
mov r1, r0
.loop____
ldb r2, [r0]
eq r2, ra
jump? .ret_____
add r0, rb
jump .loop____
.ret_____
sub r0, r1
ret
#===========================================================================
#===========================================================================
# Args:
# R0: Dest
# R1: Src
# Returns:
# R0: Dest
#===========================================================================
:strcpy__
push r0
.loop____
ldb r2, [r1]
stb [r0], r2
eq r2, ra
jump? .ret_____
add r0, rb
add r1, rb
jump .loop____
.ret_____
pop r0
ret
#===========================================================================
#===========================================================================
# Args:
# R0: String 1
# R1: String 2
# Returns:
# Equal? in flags
#===========================================================================
:strcmp__
ldb r2, [r0]
ldb r3, [r1]
ne r2, r3
jump? :retfalse
eq r2, ra
ret?
add r0, rb
add r1, rb
jump :strcmp__
#===========================================================================
#===========================================================================
# Args:
# R0: Address
# R1: Value
# R2: Length
#===========================================================================
:memset__
# If length == 0, return
eq r2, ra
ret?
stb [r0], r1
add r0, rb
sub r2, rb
jump :memset__
#===========================================================================
#===========================================================================
# Args:
# R0: Size
# Returns:
# R0: Address
#===========================================================================
:malloc__
ldc rx, :heap____
ldd rx, [rx]
mov r1, rx
add r1, r0
mov r0, rx
ldc rx, :heap____
std [rx], r1
gt r1, ry
ret^
ldc r0, .error
jump :error___
.error
data Stack collision with heap (memory exhausted)\00
#===========================================================================
#===========================================================================
# Allocates a string on the heap, returning a previous copy if it already
# exists to save memory.
# Args:
# R0: String
# Returns:
# R0: Address
#===========================================================================
:mallocst
push r1
push r2
push r3
# Search the string heap for the string if it already exists
# otherwise malloc a new block
ldc r2, .string_heap
ldd r2, [r2]
.loop
eq r2, ra
jump? .does_not_exist
# R1 = Address of string
# R2 = Address of next string
mov r1, rd
add r1, r2
ldd r2, [r2]
# Compare the strings (return in flags)
push r0
push r1
push r2
push r3
call :strcmp__
pop r3
pop r2
pop r1
pop r0
jump? .found
jump .loop
.does_not_exist
# Malloc + 4 bytes for the link + 1 byte for the NUL terminator
push r0
call :strlen__
add r0, rd
add r0, rb
call :malloc__
pop r1
# Link the new string to the heap
ldc r2, .string_heap
ldd r2, [r2]
std [r0], r2
ldc r2, .string_heap
std [r2], r0
add r0, rd
call :strcpy__
pop r3
pop r2
pop r1
ret
.found
mov r0, r1
pop r3
pop r2
pop r1
ret
.string_heap
dd 0
#===========================================================================
#===========================================================================
# Args:
# R0: Filename
# R1: Mode (0 = read, 1 = write)
# R2: Relative (0 = cwd, 1 = include)
# Returns:
# R0: Handle
#===========================================================================
:open____
push r2
push r0
push r1
call :is_vrbos
jump^ .nolog___
push r0
ldc r0, .opening_
call :log_____
pop r0
call :log_____
ldc r0, .as______
call :log_____
pop r1
push r1
mul r1, rd
ldc rx, .mode____
add r1, rx
ldd r0, [r1]
call :log_____
.nolog___
pop r1
pop r0
pop r2
eq r2, rb
jump? .relative
ldc r4, :AT_FDCWD
ldd r4, [r4]
jump .doopen__
.relative
ldc r4, :inclhand
ldd r4, [r4]
.doopen__
ldc r2, :SC_OPNAT
ldc r3, :O_RDONLY
ldc rx, :OPEN_RO_
eq r1, rx
jump? .readonly
ldc r3, :O_RDWR__
ldc rx, :O_TRUNC_
or r3, rx
ldc rx, :O_CREAT_
or r3, rx
.readonly
sys r2, r4, r0, r3
add r2, rb
ne r2, ra
jump? .success_
call :log_____
ldc r0, .errfail_
jump :error___
.success_
mov r0, r2
sub r0, rb
ret
.opening_
data Opening '\00
.as______
data ' as \00
.s_ro____
data read-only\0a\00
.s_rw____
data read/write\0a\00
.mode____
.s_ro____
.s_rw____
.errfail_
data \3a Failed to open file\00
#===========================================================================
#===========================================================================
# Steps back in the input file by one char
# No args/return
#===========================================================================
:rewind__
call :getinhnd
ldc r3, :SC_SEEK_
sub r1, r1
sub r1, rb
ldc r2, :SEEK_CUR
sys r3, r0, r1, r2
ret
#===========================================================================
#===========================================================================
# Args:
# R0: NUL-terminated definition name
# Returns:
# R0: Token type
# R1: Token data
#===========================================================================
:lookupdf
push r0
ldc r2, :deftab__
.loop____
# Get the definition record
ldd r2, [r2]
eq r2, ra
jump? .notfound
# Get the pointer to the string
add r2, re
ldd r1, [r2]
pop r0
push r0
push r2
call :strcmp__
pop r2
jump? .found___
add r2, rd
jump .loop____
# Found
.found___
sub r2, rd
ldd r0, [r2]
sub r2, rd
ldd r1, [r2]
pop r2
ret
.notfound
call :log_____
ldc r0, .errnotfo
jump :error___
.errnotfo
data \3a define not found\00
#===========================================================================
#===========================================================================
# Args:
# R0: Definition name
# R1: Token type
# R2: Token value
#===========================================================================
:createdf
push r0
push r1
push r2
# Allocate a record
ldh r0, #0010
call :malloc__
# Read the current record
ldc rx, :deftab__
ldd rx, [rx]
# Write everything to the struct
mov r1, r0
pop r2
std [r1], r2
add r1, rd
pop r2
std [r1], r2
add r1, rd
pop r2
std [r1], r2
add r1, rd
std [r1], rx
# Write the struct as the latest
ldc rx, :deftab__
std [rx], r0
# If we're in a global, just return
ldc rx, :inglobal
ldb rx, [rx]
eq rx, rb
ret?
ldc rx, :lastdef_
std [rx], r0
ret
#===========================================================================
#===========================================================================
# Args:
# R0: String A (or 0)
# R1: String B (or 0)
# Returns:
# Equals in flag
#===========================================================================
:comparsm
eq r0, ra
jump? .zero____
eq r1, ra
jump? .zero____
jump :strcmp__
.zero____
eq r0, r1
ret
#===========================================================================
#===========================================================================
# Args:
# R0: Global symbol name
# R1: Local symbol name
# Returns:
# R0: Address
#===========================================================================
:lookupsm
push r0
push r1
ldc r2, :symtab__
.loop____
# Get the symbol record
ldd r2, [r2]
eq r2, ra
jump? .notfound
# Get the pointer to the local name
add r2, rd
ldd r1, [r2]
pop r0
push r0
push r2
call :comparsm
pop r2
# Not a match, next record
add^ r2, re
jump^ .loop____
pop r1
pop r0
push r0
push r1
add r2, rd
ldd r1, [r2]
push r2
call :comparsm
pop r2
# Not a match, next record
add^ r2, rd
jump^ .loop____
sub r2, re
ldd r0, [r2]
pop r2
pop r2
ret
.notfound
call :log_____
ldc r0, .errnotfo
jump :error___
.errnotfo
data \3a symbol not found\00
#===========================================================================
#===========================================================================
# Args:
# R0: Global symbol
# R1: Local symbol (0 ok)
# R2: Address
#===========================================================================
:createsm
push r0
push r1
push r2
# Allocate a record
ldh r0, #0010
call :malloc__
# Read the current record
ldc rx, :symtab__
ldd rx, [rx]
# Write everything to the struct
mov r1, r0
pop r2
std [r1], r2
add r1, rd
pop r2
std [r1], r2
add r1, rd
pop r2
std [r1], r2
add r1, rd
std [r1], rx
# Write the struct as the latest
ldc rx, :symtab__
std [rx], r0
ret
#===========================================================================
#===========================================================================
# Args:
# R0: Global symbol
# R1: Local symbol (0 ok)
# R2: Address
#===========================================================================
:createfx
push r0
push r1
push r2
# Allocate a record
ldh r0, #0010
call :malloc__
# Read the current record
ldc rx, :fixuptab
ldd rx, [rx]
# Write everything to the struct
mov r1, r0
pop r2
std [r1], r2
add r1, rd
pop r2
std [r1], r2
add r1, rd
pop r2
std [r1], r2
add r1, rd
std [r1], rx
# Write the struct as the latest
ldc rx, :fixuptab
std [rx], r0
ret
#===========================================================================
#===========================================================================