-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathas.oo
More file actions
1429 lines (1149 loc) · 51.4 KB
/
Copy pathas.oo
File metadata and controls
1429 lines (1149 loc) · 51.4 KB
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
; The MIT License (MIT)
;
; Copyright (c) 2023-2026 Fraser Heavy Software
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in all
; copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
; SOFTWARE.
; This is the implementation of the basic first stage assembler. It is written
; in Onramp object code.
;
; Its main purpose is simply to replace keywords with bytes. Keywords are the
; names of opcodes and registers. When it encounters a keyword, it finds it in
; the replacement map and emits the corresponding byte as hex.
;
; In addition:
;
; - linker directives (symbol and label declarations and invocations) are
; passed through to the output verbatim;
; - strings are converted to hexadecimal; and
; - quoted hexadecimal bytes are output without the quote.
;
; This constitutes the total functionality of the first-stage assembler.
;
; It is linked against our first stage libc so we have a lot of standard
; functionality available. Our entry point is main().
;
; (This stage does not verify that keywords make sense in the context in which
; they are used. Keywords must still be used correctly, otherwise the program
; will not be forward-compatible with the later stage assemblers.)
; ==========================================================
; char* keyword_map;
; ==========================================================
; This is a mapping of keywords to single hex bytes. Each entry is a
; null-terminated string followed by the replacement byte, and the list itself
; is null-terminated.
;
; When the assembler encounters a keyword, it searches this map linearly,
; checking if each name matches. When it finds a match it emits the
; corresponding byte in hexadecimal. If no match is found, an error is printed
; and the assembler aborts.
; ==========================================================
=keyword_map
; registers
72 30 00 80 ; r0
72 31 00 81 ; r1
72 32 00 82 ; r2
72 33 00 83 ; r3
72 34 00 84 ; r4
72 35 00 85 ; r5
72 36 00 86 ; r6
72 37 00 87 ; r7
72 38 00 88 ; r8
72 39 00 89 ; r9
72 61 00 8a ; ra
72 62 00 8b ; rb
72 73 70 00 8c ; rsp
72 66 70 00 8d ; rfp
72 70 70 00 8e ; rpp
72 69 70 00 8f ; rip
; opcodes
61 64 64 00 70 ; add
73 75 62 00 71 ; sub
6d 75 6c 00 72 ; mul
64 69 76 75 00 73 ; divu
61 6e 64 00 74 ; and
6f 72 00 75 ; or
73 68 6c 00 76 ; shl
73 68 72 75 00 77 ; shru
6c 64 77 00 78 ; ldw
73 74 77 00 79 ; stw
6c 64 62 00 7a ; ldb
73 74 62 00 7b ; stb
69 6d 73 00 7c ; ims
6c 74 75 00 7d ; ltu
6a 7a 00 7e ; jz
; end of list
00
; ==========================================================
; const char error_incorrect_arguments[];
; ==========================================================
=error_incorrect_arguments
; "Incorrect arguments." '0A
; "Usage: <as> [input] -o [output]" '00
49 6e 63 6f 72 72 65 63 74
; I n c o r r e c t
20 61 72 67 75 6d 65 6e 74 73 2e 0a 55 73 61 67
; a r g u m e n t s . \n U s a g
65 3a 20 3c 61 73 3e 20 5b 69 6e 70 75 74 5d 20
; e : < a s > [ i n p u t ]
2d 6f 20 5b 6f 75 74 70 75 74 5d 00
; - o [ o u t p u t ] \0
;==========================================
; const char error_input_file[];
;==========================================
=error_input_file
; "Failed to read input file." '0A '00
46 61 69 6c 65 64 20 74 6f
; F a i l e d t o
20 72 65 61 64 20 69 6e 70 75 74 20 66 69 6c 65
; r e a d i n p u t f i l e
2e 00
; . \0
;==========================================
; const char error_output_file[];
;==========================================
=error_output_file
; "Failed to write output file." '0A '00
46 61 69 6c 65 64 20 74 6f
; F a i l e d t o
20 77 72 69 74 65 20 6f 75 74 70 75 74 20 66 69
; w r i t e o u t p u t f i
6c 65 2e 00
; l e . \0
;==========================================
; const char error_invalid_character[];
;==========================================
=error_invalid_character
; "Invalid character." '0A '00
49 6e 76 61 6c 69 64 20 63
; I n v a l i d c
68 61 72 61 63 74 65 72 2e 00
; h a r a c t e r . \0
;==========================================
; const char error_invalid_string[];
;==========================================
=error_invalid_string
; "Invalid string." '0A '00
49 6e 76 61 6c 69 64 20 73
; I n v a l i d s
74 72 69 6e 67 2e 00
; t r i n g . \0
;==========================================
; const char error_invalid_keyword[];
;==========================================
=error_invalid_keyword
; "Invalid keyword." '0A '00
49 6e 76 61 6c 69 64 20 6b
; I n v a l i d k
65 79 77 6f 72 64 2e 00
; e y w o r d . \0
; ==========================================================
; FILE* input_file;
; ==========================================================
=input_file
00 00 00 00
; ==========================================================
; FILE* output_file;
; ==========================================================
=output_file
00 00 00 00
; ==========================================================
; char current_char;
; ==========================================================
; The current character being processed.
;
; This is a full word because it's -1 if the end of the file has been reached.
; ==========================================================
=current_char
00 00 00 00
; ==========================================================
; char current_keyword[];
; ==========================================================
; The current keyword as a null-terminated string.
;
; We store at most 15 characters (plus null terminator) since all of our
; keywords are short. Label and symbol names and strings do not go through
; this; they are fed directly to the output.
; ==========================================================
=current_token
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
; ==========================================================
; int main(int argc, char** argv);
; ==========================================================
; The entry point of the assembler. We parse the arguments to determine the
; input and output filenames, then hand over control to open_files().
;
; params:
; - argc: r0
; - argv: r1
; vars:
; - input filename index: r2
; - output filename index: r3
; ==========================================================
=main
; set up a stack frame (without pushing rfp because we won't return)
70 8D 8C 00 ; add rfp rsp 0 ; mov rfp rsp
; check that we have exactly four arguments
71 8A 80 04 ; sub ra r0 4
7E 8A &main_four_args ; jz ra &main_four_args
7E 00 &main_incorrect_arguments ; jz 0 &main_incorrect_arguments
:main_four_args
; preserve r1 now
71 8C 8C 04 ; sub rsp rsp 4 ; push r1
79 81 00 8C ; stw r1 0 rsp ; ...
; push "-o" to the stack for comparison
7C 8A 00 00 ; ims ra '00 '00
7C 8A 2D 6F ; ims ra "-o"
71 8C 8C 04 ; sub rsp rsp 4
79 8A 00 8C ; stw ra 0 rsp
; check if -o is the first argument
78 80 81 04 ; ldw r0 r1 4 ; r0 = argv[1]
70 81 00 8C ; add r1 0 rsp ; r1 = rsp ("-o")
; call strcmp
7C 8A <strcmp ; ims ra <strcmp
7C 8A >strcmp ; ims ra >strcmp
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8 ; ...
79 8B 00 8C ; stw rb 0 rsp ; ...
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
78 81 8D FC ; ldw r1 rfp -4 ; restore r1
7E 80 &main_first_arg ; jz r0 &main_first_arg
7E 00 &main_check_second_arg ; jz 0 &main_check_second_arg
; -o is the first argument.
:main_first_arg
70 82 00 03 ; add r2 0 3
70 83 00 02 ; add r3 0 2
7E 00 &main_parsed_arguments ; jz 0 &main_parsed_arguments
; check if -o is the second argument
:main_check_second_arg
78 80 81 08 ; ldw r0 r1 8 ; r0 = argv[2]
70 81 00 8C ; add r1 0 rsp ; r1 = rsp ("-o")
; call strcmp
7C 8A <strcmp ; ims ra <strcmp
7C 8A >strcmp ; ims ra >strcmp
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8 ; ...
79 8B 00 8C ; stw rb 0 rsp ; ...
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
78 81 8D FC ; ldw r1 rfp -4 ; restore r1
7E 80 &main_second_arg ; jz r0 &main_second_arg
7E 00 &main_incorrect_arguments ; jz 0 &main_incorrect_arguments
; -o is the second argument.
:main_second_arg
70 82 00 01 ; add r2 0 1
70 83 00 03 ; add r3 0 3
7E 00 &main_parsed_arguments ; jz 0 &main_parsed_arguments
:main_incorrect_arguments
7C 80 <error_incorrect_arguments ; ims r0 <error_incorrect_arguments
7C 80 >error_incorrect_arguments ; ims r0 >error_incorrect_arguments
70 80 8E 80 ; add r0 rpp r0
7C 8A <fatal ; ims ra <fatal
7C 8A >fatal ; ims ra >fatal
70 8F 8E 8A ; add rip rpp ra
; argument parsing is done. get the filenames
:main_parsed_arguments
72 82 82 04 ; mul r2 r2 4
78 80 81 82 ; ldw r0 r1 r2
72 83 83 04 ; mul r3 r3 4
78 81 81 83 ; ldw r1 r1 r3
; clean up our stack and jump to open_files(). (it never returns.)
70 8C 8D 00 ; add rsp rfp 0 ; mov rsp rfp
7C 8A <open_files ; ims ra <open_files
7C 8A >open_files ; ims ra >open_files
70 8F 8E 8A ; add rip rpp ra
; ==========================================================
; [[noreturn]] void open_files(const char* input_filename, const char* output_filename);
; ==========================================================
; params/vars:
; - input_filename, input_file: r0, rfp-4
; - output_filename, output_file: r1, rfp-8
; - "r": rfp-10
; - "w": rfp-12
; ==========================================================
=open_files
; set up a stack frame (without pushing rfp because we won't return)
70 8D 8C 00 ; add rfp rsp 0 ; mov rfp rsp
71 8C 8C 0C ; sub rsp rsp 12
; preserve r0 and r1 on the stack
79 80 8D FC ; stw r0 rfp -4 ; push r0
79 81 8D F8 ; stw r1 rfp -8 ; push r1
; push "r" and "w" to the stack (in one word)
7C 8A 72 00 ; ims ra "r" '00
7C 8A 77 00 ; ims ra "w" '00
79 8A 8D F4 ; stw ra rfp -12 ; push "w\0r\0"
; call set_current_filename(input_filename)
78 80 8D FC ; ldw r0 rfp -4 ; input_filename
7C 8A <set_current_filename ; ims ra <set_current_filename
7C 8A >set_current_filename ; ims ra >set_current_filename
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
; open input: fopen(input_filename, "r")
78 80 8D FC ; ldw r0 rfp -4 ; input_filename
70 81 8D F6 ; add r1 rfp -10 ; "r"
7C 8A <fopen ; ims ra <fopen
7C 8A >fopen ; ims ra >fopen
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8 ; ...
79 8B 00 8C ; stw rb 0 rsp ; ...
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
; make sure it's good
71 8A 80 00 ; sub ra r0 0
7E 8A &open_files_input_error ; jz ra &open_files_input_error
7E 00 &open_files_input_ok ; jz 0 &open_files_input_ok
; opening input failed, fatal error
:open_files_input_error
7C 80 <error_input_file ; ims r0 <error_input_file
7C 80 >error_input_file ; ims r0 >error_input_file
70 80 8E 80 ; add r0 rpp r0
7C 8A <fatal ; ims ra <fatal
7C 8A >fatal ; ims ra >fatal
70 8F 8E 8A ; add rip rpp ra
; opening input succeeded, store it
:open_files_input_ok
7C 8A <input_file ; ims ra <input_file
7C 8A >input_file ; ims ra >input_file
79 80 8E 8A ; stw r0 rpp ra
; open output: fopen(output_filename, "w")
78 80 8D F8 ; ldw r0 rfp -8
70 81 8D F4 ; add r1 rfp -12
7C 8A <fopen ; ims ra <fopen
7C 8A >fopen ; ims ra >fopen
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8 ; ...
79 8B 00 8C ; stw rb 0 rsp ; ...
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
79 80 8D F8 ; stw r0 rfp -8
; make sure it's good
71 8A 80 00 ; sub ra r0 0
7E 8A &open_files_output_error ; jz ra &open_files_output_error
7E 00 &open_files_output_ok ; jz 0 &open_files_output_ok
; opening output failed, fatal error
:open_files_output_error
7C 80 <error_output_file ; ims r0 <error_output_file
7C 80 >error_output_file ; ims r0 >error_output_file
70 80 8E 80 ; add r0 rpp r0
7C 8A <fatal ; ims ra <fatal
7C 8A >fatal ; ims ra >fatal
70 8F 8E 8A ; add rip rpp ra
; opening output succeeded, store it
:open_files_output_ok
7C 8A <output_file ; ims ra <output_file
7C 8A >output_file ; ims ra >output_file
79 80 8E 8A ; stw r0 rpp ra
; set our starting line number to 1
7C 81 <current_line ; ims r0 <current_line
7C 81 >current_line ; ims r0 >current_line
78 80 8E 81 ; ldw r0 rpp r1
70 80 00 01 ; add r0 00 1 ; mov r0 1
79 80 8E 81 ; stw r0 rpp r1
; clean up our stack and jump to run. (it never returns.)
70 8C 8D 00 ; add rsp rfp 0 ; mov rsp rfp
7C 8A <run ; ims ra <run
7C 8A >run ; ims ra >run
70 8F 8E 8A ; add rip rpp ra
; ==========================================================
; [[noreturn]] void run(void);
; ==========================================================
; Parses until the input file is exhausted, then closes files and exits.
; ==========================================================
=run
; no stack frame
; read the first character
7C 8A <read_char ; ims ra <read_char
7C 8A >read_char ; ims ra >read_char
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
:run_loop
; see if it's EOF. if so we're done
7C 8A <current_char ; ims ra <current_char
7C 8A >current_char ; ims ra >current_char
78 8A 8E 8A ; ldw ra rpp ra
71 8A 8A FF ; sub ra ra -1
7E 8A &run_loop_done ; jz ra &run_loop_done
; Each of these "try" functions attempts to parse a construct, consuming as
; much as is necessary to parse it. If they successfully parse something
; they return true which sends us back to the top of the run loop.
; try to parse whitespace, call try_parse_whitespace()
7C 8A <try_parse_whitespace ; ims ra <try_parse_whitespace
7C 8A >try_parse_whitespace ; ims ra >try_parse_whitespace
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
71 8A 80 01 ; sub ra r0 1
7E 8A &run_loop ; jz ra run_loop
; try to parse a quoted byte, call try_parse_quoted_byte()
7C 8A <try_parse_quoted_byte ; ims ra <try_parse_quoted_byte
7C 8A >try_parse_quoted_byte ; ims ra >try_parse_quoted_byte
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
71 8A 80 01 ; sub ra r0 1
7E 8A &run_loop ; jz ra run_loop
; try to parse a keyword, call try_parse_keyword()
7C 8A <try_parse_keyword ; ims ra <try_parse_keyword
7C 8A >try_parse_keyword ; ims ra >try_parse_keyword
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
71 8A 80 01 ; sub ra r0 1
7E 8A &run_loop ; jz ra run_loop
; try to parse a comment, call try_parse_comment()
7C 8A <try_parse_comment ; ims ra <try_parse_comment
7C 8A >try_parse_comment ; ims ra >try_parse_comment
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
71 8A 80 01 ; sub ra r0 1
7E 8A &run_loop ; jz ra run_loop
; try to parse a debug line, call try_parse_debug()
7C 8A <try_parse_debug ; ims ra <try_parse_debug
7C 8A >try_parse_debug ; ims ra >try_parse_debug
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
71 8A 80 01 ; sub ra r0 1
7E 8A &run_loop ; jz ra run_loop
; try to parse a string, call try_parse_string()
7C 8A <try_parse_string ; ims ra <try_parse_string
7C 8A >try_parse_string ; ims ra >try_parse_string
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
71 8A 80 01 ; sub ra r0 1
7E 8A &run_loop ; jz ra run_loop
; try to parse a linker directive, call try_parse_linker()
7C 8A <try_parse_linker ; ims ra <try_parse_linker
7C 8A >try_parse_linker ; ims ra >try_parse_linker
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
71 8A 80 01 ; sub ra r0 1
7E 8A &run_loop ; jz ra run_loop
; parsing failed. fatal error
7C 80 <error_invalid_character ; ims r0 <error_invalid_character
7C 80 >error_invalid_character ; ims r0 >error_invalid_character
70 80 8E 80 ; add r0 rpp r0
7C 8A <fatal ; ims ra <fatal
7C 8A >fatal ; ims ra >fatal
70 8F 8E 8A ; add rip rpp ra
:run_loop_done
; get the output file
7C 8A <output_file ; ims ra <output_file
7C 8A >output_file ; ims ra >output_file
78 80 8E 8A ; ldw r0 rpp ra
; call fclose
7C 8A <fclose ; ims ra <fclose
7C 8A >fclose ; ims ra >fclose
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8 ; ...
79 8B 00 8C ; stw rb 0 rsp ; ...
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
; get the input file
7C 8A <input_file ; ims ra <input_file
7C 8A >input_file ; ims ra >input_file
78 80 8E 8A ; ldw r0 rpp ra
; call fclose
7C 8A <fclose ; ims ra <fclose
7C 8A >fclose ; ims ra >fclose
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8 ; ...
79 8B 00 8C ; stw rb 0 rsp ; ...
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
; tail-call exit(0)
70 80 00 00 ; add r0 0 0
7C 8A <exit ; ims ra <exit
7C 8A >exit ; ims ra >exit
70 8F 8E 8A ; add rip rpp ra ; jump
;==========================================
; void read_char(void);
;==========================================
; Reads a character, or -1 if the end of the file has been reached, into
; current_char.
;==========================================
=read_char
; don't bother to set up a stack frame
; get the input file
7C 8A <input_file ; ims ra <input_file
7C 8A >input_file ; ims ra >input_file
78 80 8E 8A ; ldw r0 rpp ra
; call fgetc
7C 8A <fgetc ; ims ra <fgetc
7C 8A >fgetc ; ims ra >fgetc
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
; TODO check feof()/ferror() on EOF
; store the result in current_char
7C 8A <current_char ; ims ra <current_char
7C 8A >current_char ; ims ra >current_char
79 80 8E 8A ; stw r0 rpp ra
78 8F 00 8C ; ldw rip 0 rsp ; ret
; ==========================================================
; void emit_byte(char c);
; ==========================================================
; Writes the given byte to the output file.
; ==========================================================
=emit_byte
; no stack frame
; push the byte to the stack
71 8C 8C 04 ; sub rsp rsp 4 ; push r0
79 80 00 8C ; stw r0 0 rsp ; ...
; prepare args for fwrite
70 80 00 8C ; add r0 0 rsp ; mov r0 rsp ; r0 = &c
70 81 00 01 ; add r1 0 1 ; mov r1 1 ; r1 = 1
70 82 00 01 ; add r2 0 1 ; mov r2 1 ; r2 = 1
7C 8A <output_file ; ims ra <output_file
7C 8A >output_file ; ims ra >output_file
78 83 8E 8A ; ldw r3 rpp ra ; r3 = output_file
; call fwrite
7C 8A <fwrite ; ims ra <fwrite
7C 8A >fwrite ; ims ra >fwrite
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
; TODO check for error
; done
70 8C 8C 04 ; add rsp rsp 4 ; ...
78 8F 00 8C ; ldw rip 0 rsp ; ret
; ==========================================================
; void emit_hex_bits(char c);
; ==========================================================
; Emits the given four bits as one hexadecimal character.
;
; TODO rename to emit_hex_char() to match as/1
; ==========================================================
=emit_hex_bits
; don't bother to set up a stack frame
; see if we need a letter or a number
7D 81 80 0A ; ltu r1 r0 10
7E 81 &emit_hex_bits_alpha ; jz r1 &emit_hex_bits_alpha
; number
70 80 80 30 ; add r0 r0 "0"
7E 00 &emit_hex_bits_done ; jz 0 &emit_hex_bits_done
; letter
:emit_hex_bits_alpha
70 80 80 37 ; add r0 r0 A-10
; tail-call emit_byte
:emit_hex_bits_done
7C 8A <emit_byte ; ims ra <emit_byte
7C 8A >emit_byte ; ims ra >emit_byte
70 8F 8E 8A ; add rip rpp ra
; ==========================================================
; void emit_as_hex(char c);
; ==========================================================
; Writes the hexadecimal representation of the given byte to the output file.
; ==========================================================
=emit_as_hex
; set up a stack frame
71 8C 8C 04 ; sub rsp rsp 4 ; push rfp
79 8D 00 8C ; stw rfp 0 rsp ; ...
70 8D 8C 00 ; add rfp rsp 0 ; mov rfp rsp
; collect the low bits and push them to the stack
74 81 80 0F ; and r1 r0 15
71 8C 8C 04 ; sub rsp rsp 4 ; push r1
79 81 00 8C ; stw r1 0 rsp
; collect the high bits and call emit_hex_bits
77 80 80 04 ; shru r0 r0 4
74 80 80 0F ; and r0 r0 15
7C 8A <emit_hex_bits ; ims ra <emit_hex_bits
7C 8A >emit_hex_bits ; ims ra >emit_hex_bits
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
; pop the low bits and call emit_hex_bits
78 80 00 8C ; ldw r0 0 rsp ; pop r0
70 8C 8C 04 ; add rsp rsp 4
7C 8A <emit_hex_bits ; ims ra <emit_hex_bits
7C 8A >emit_hex_bits ; ims ra >emit_hex_bits
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
; return
70 8C 8D 00 ; add rsp rfp 0 ; mov rsp rfp
78 8D 00 8C ; ldw rfp 0 rsp ; pop rfp
70 8C 8C 04 ; add rsp rsp 4 ; ...
78 8F 00 8C ; ldw rip 0 rsp ; ret
; ==========================================================
; void try_parse_whitespace(void);
; ==========================================================
=try_parse_whitespace
; get the current char
7C 8A <current_char ; ims ra <current_char
7C 8A >current_char ; ims ra >current_char
78 80 8E 8A ; ldw r0 rpp ra
; check if it's a newline. these are handled separately.
; TODO check for carriage returns
71 8A 80 0A ; sub ra r0 \n (line feed)
7E 8A &try_parse_whitespace_newline
; check if it's whitespace, call isspace()
7C 8A <isspace ; ims ra <isspace
7C 8A >isspace ; ims ra >isspace
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
; if false, return false
7E 80 &try_parse_whitespace_false
; consume the character, call read_char()
7C 8A <read_char ; ims ra <read_char
7C 8A >read_char ; ims ra >read_char
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
; return true
70 80 00 01 ; add r0 0 1 ; mov r0 1
:try_parse_whitespace_false
78 8F 00 8C ; ldw rip 0 rsp ; ret
:try_parse_whitespace_newline
; when we encounter a newline, we emit a newline. this helps us debug the
; output. we also increment the line number to improve debugging.
; call emit_byte('\n') ('\n' is already in r0)
7C 8A <emit_byte ; ims ra <emit_byte
7C 8A >emit_byte ; ims ra >emit_byte
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
; consume the newline, call read_char()
7C 8A <read_char ; ims ra <read_char
7C 8A >read_char ; ims ra >read_char
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
; increment the line number
7C 81 <current_line ; ims r0 <current_line
7C 81 >current_line ; ims r0 >current_line
78 80 8E 81 ; ldw r0 rpp r1
70 80 80 01 ; add r0 r0 1 ; inc r0
79 80 8E 81 ; stw r0 rpp r1
; return true
70 80 00 01 ; add r0 0 1 ; mov r0 1
78 8F 00 8C ; ldw rip 0 rsp ; ret
; ==========================================================
; void try_parse_quoted_byte(void);
; ==========================================================
=try_parse_quoted_byte
; get the current char
7C 8A <current_char ; ims ra <current_char
7C 8A >current_char ; ims ra >current_char
78 80 8E 8A ; ldw r0 rpp ra
; check if it's a single quote
71 8A 80 27 ; sub ra r0 "'"
7E 8A &try_parse_byte_found
; not a quote, return false
70 80 00 00 ; add r0 0 0
78 8F 00 8C ; ldw rip 0 rsp ; ret
:try_parse_byte_found
; consume the quote, call read_char()
7C 8A <read_char ; ims ra <read_char
7C 8A >read_char ; ims ra >read_char
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
; push a loop flag to the stack
71 8C 8C 04 ; sub rsp rsp 4 ; push 1
79 01 00 8C ; stw 1 0 rsp ; ^^^
:try_parse_byte_loop
; get the char
7C 8A <current_char ; ims ra <current_char
7C 8A >current_char ; ims ra >current_char
78 80 8E 8A ; ldw r0 rpp ra
; make sure it's hex, call isxdigit()
7C 8A <isxdigit ; ims ra <isxdigit
7C 8A >isxdigit ; ims ra >isxdigit
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
7E 80 &try_parse_byte_error ; jz r0 &try_parse_byte_error
; get the char again
7C 8A <current_char ; ims ra <current_char
7C 8A >current_char ; ims ra >current_char
78 80 8E 8A ; ldw r0 rpp ra
; emit it to the output
7C 8A <emit_byte ; ims ra <emit_byte
7C 8A >emit_byte ; ims ra >emit_byte
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
; consume the hex char, call read_char()
7C 8A <read_char ; ims ra <read_char
7C 8A >read_char ; ims ra >read_char
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
; get our loop flag. if it's zero, we're done
78 8A 00 8C ; ldw r0 0 rsp
7E 8A &try_parse_byte_done ; jz 0 &try_parse_byte_done
; clear the flag and loop
79 00 00 8C ; stw 0 0 rsp
7E 00 &try_parse_byte_loop ; jz 0 &try_parse_byte_loop
:try_parse_byte_done
; pop the loop flag
70 8C 8C 04 ; add rsp rsp 4
; return true
70 80 00 01 ; add r0 0 1
78 8F 00 8C ; ldw rip 0 rsp ; ret
:try_parse_byte_error
7C 80 <error_invalid_character ; ims r0 <error_invalid_character
7C 80 >error_invalid_character ; ims r0 >error_invalid_character
70 80 8E 80 ; add r0 rpp r0
7C 8A <fatal ; ims ra <fatal
7C 8A >fatal ; ims ra >fatal
70 8F 8E 8A ; add rip rpp ra
; ==========================================================
; void try_parse_comment(void);
; ==========================================================
=try_parse_comment
; get the current char
7C 8A <current_char ; ims ra <current_char
7C 8A >current_char ; ims ra >current_char
78 80 8E 8A ; ldw r0 rpp ra
; check if it's the start of a comment
71 8A 80 3B ; sub ra r0 ";"
7E 8A &try_parse_comment_found
; not a comment, return false
70 80 00 00 ; add r0 0 0
78 8F 00 8C ; ldw rip 0 rsp ; ret
:try_parse_comment_found
; tail-call parse_comment_impl()
7C 8A <parse_comment_impl ; ims ra <parse_comment_impl
7C 8A >parse_comment_impl ; ims ra >parse_comment_impl
70 8F 8E 8A ; add rip rpp ra
; ==========================================================
; void parse_comment_impl(void);
; ==========================================================
; Consumes a comment, when the first character is known to be a comment.
; ==========================================================
=parse_comment_impl
:parse_comment_impl_loop
; consume the current char, call read_char()
7C 8A <read_char ; ims ra <read_char
7C 8A >read_char ; ims ra >read_char
71 8C 8C 04 ; sub rsp rsp 4 ; push return address
70 8B 8F 08 ; add rb rip 8
79 8B 00 8C ; stw rb 0 rsp
70 8F 8E 8A ; add rip rpp ra ; jump
70 8C 8C 04 ; add rsp rsp 4 ; pop return address
; get the current char
7C 8A <current_char ; ims ra <current_char
7C 8A >current_char ; ims ra >current_char
78 80 8E 8A ; ldw r0 rpp ra
; check if it's the end of a line or the end of the file
71 8A 80 0A ; sub ra r0 "\n" (line feed)
7E 8A &parse_comment_impl_done ; jz ra &parse_comment_impl_done
71 8A 80 0D ; sub ra r0 "\r" (carriage return)
7E 8A &parse_comment_impl_done ; jz ra &parse_comment_impl_done
71 8A 80 FF ; sub ra r0 -1 (end-of-file)
7E 8A &parse_comment_impl_done ; jz ra &parse_comment_impl_done
; keep looping
7E 00 &parse_comment_impl_loop ; jz 0 &parse_comment_impl_loop
:parse_comment_impl_done
;return true
70 80 00 01 ; add r0 0 1
78 8F 00 8C ; ldw rip 0 rsp ; ret