-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathself.asm
13098 lines (10349 loc) · 274 KB
/
self.asm
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
bits 64
org 0x00400000 ; このあたりにマッピングしておきたい
segment_align: equ 0x1000
app_max_code_buffer_size: equ 0x100000
app_max_asm_buffer_size: equ 0xff00000
app_max_heap: equ 0x2f000000
app_heap_page_size: equ 0x100000
app_max_asm_inst_size: equ 0x100000
;;; ELF Header
;; e_ident
db 0x7f, "ELF" ; Magic number
db 0x02 ; EI_CLASS, ELFCLASS64
db 0x01 ; EI_DATA, ELFDATA2LSB
db 0x01 ; EI_VERSION, EV_CURRENT
db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ; padding
;; e_type, ET_EXEC
dw 0x0002
;; e_machine, EM_X86_64
dw 0x003e
;; e_version, EV_CURRENT
dd 0x00000001
;; e_entry
dq _start
;; e_phoff
dq phdrs - $$
;; e_shoff,
dq shdrs - $$
;; e_flags
dd 0x00000000
;; e_ehsize, (64)
dw 0x0040
;; e_phentsize, (56)
dw 0x0038
;; e_phnum
dw 0x0003
;; e_shentsize, (64)
dw 0x0040
;; e_shnum
dw 0x0005
;; e_shstrndx
dw 0x0001
;;; Program headers
phdrs:
;; [0]
;; p_type, PT_LOAD
dd 0x00000001
;; p_flags, R(4)
dd 0x00000004
;; p_offset
dq 0x0000000000000000
;; p_vaddr,
dq $$
;; p_paddr
dq 0
;; p_filesz
dq rest_size
;; p_memsz
dq rest_size
;; p_align
dq segment_align
;; [1]
;; p_type, PT_LOAD
dd 0x00000001
;; p_flags, R(4)X(1)
dd 0x00000005
;; p_offset
dq code_segment_begin-$$
;; p_vaddr,
dq code_segment_begin
;; p_paddr
dq 0
;; p_filesz
dq code_segment_rest_size
;; p_memsz
dq code_segment_rest_size
;; p_align
dq segment_align
;; [2]
;; p_type, PT_LOAD
dd 0x00000001
;; p_flags, R(4)W(2)
dd 0x00000006
;; p_offset
dq data_segment_begin-$$
;; p_vaddr,
dq data_segment_begin
;; p_paddr
dq 0
;; p_filesz
dq data_segment_rest_size
;; p_memsz
dq data_segment_rest_size
;; p_align
dq segment_align
;;; Segments
;; Rest
section_strtab_begin:
sym_null: db 0
sym_name_sym: db ".sym", 0
sym_name_text: db ".text", 0
sym_name_bss: db ".bss", 0
sym_name_rodata: db ".rodata", 0
section_strtab_end:
rest_size: equ $-$$
align segment_align
initial_segment_end:
;; Code
code_segment_begin:
;; --> text
section_text_begin:
_start:
push rbp
mov rbp, rsp
sub rsp, 56
;; parser {
;; char* buffer , 0
;; u64 offset , 8
;; u64 status = 2 finished , 16
;: | 1 failed
;: | 0 running
;; sexp* last-label , 24
;; }
mov qword [rbp-48], g_code_buffer ; char*, buffer
mov qword [rbp-40], 0 ; u64, offset
mov qword [rbp-32], 0 ; u64, status
mov qword [rbp-24], 0 ; sexp*, last-label
mov qword [rbp-8], 0 ; statements
call runtime_init
call load_code_from_stdin
;; call print_loaded_code_to_stdout
lea rdi, [rbp-48] ; parser*
call parse_code
mov [rbp-8], rax
; ;; debug
; mov rdi, [rbp-8]
; call sexp_print
; call runtime_print_newline
; ;; debug
;; initialize pre-defined symbols
;; $
mov rdi, str_g_symbol_doller
call sexp_alloc_string
mov [g_sexp_symbol_doller], rax
;; $$
mov rdi, str_g_symbol_doller_doller
call sexp_alloc_string
mov [g_sexp_symbol_doller_doller], rax
;;
mov rdi, [rbp-8]
call asm_process_statements
mov rdi, 0
call runtime_exit
load_code_from_stdin:
xor rax, rax ; 0 - read
xor rdi, rdi ; fd 0 - stdin
mov rsi, g_code_buffer
mov rdx, app_max_code_buffer_size
syscall
mov [g_code_size], rax
ret
print_loaded_code_to_stdout:
mov rax, 1 ; 1 - write
mov rdi, 1 ; fd 1 - stdout
mov rsi, g_code_buffer
mov rdx, [g_code_size]
syscall
ret
;;; rdi: parser*
parse_code:
push rbp
mov rbp, rsp
sub rsp, 80
mov qword [rbp-56], 0 ; statements : (new-statement . nil)
mov qword [rbp-48], 0 ; last-statement : (new-statement . nil)
mov qword [rbp-40], 0 ; statement
mov [rbp-8], rdi ; parser*
.loop:
mov rdi, [rbp-8]
call parse_statement
mov [rbp-40], rax ; statement
mov rdi, [rbp-8]
call parser_is_failed
cmp rax, 0
jne .failed
mov rax, [rbp-40] ; statement
cmp rax, 0 ; nil-statement
je .loop_skip_updated
;; update statements-list
mov rdi, [rbp-40] ; statement
mov rsi, 0 ; nil
call sexp_alloc_cons ; (new-statement . nil)
cmp qword [rbp-56], 0 ; statements
jne .loop_skip_init
mov [rbp-56], rax ; statements : (new-statement . nil)
mov [rbp-48], rax ; last-statement : (new-statement . nil)
jmp .loop_skip_updated
.loop_skip_init:
;; chain
mov rdi, [rbp-48] ; last-statement : (last-statement . nil)
mov rsi, rax ; (new-statement . nil)
call sexp_update_cdr ; (last-statement . (new-statement . nil))
mov [rbp-48], rax ; last-statement : (new-statement . nil)
.loop_skip_updated:
; ;; debug, -> expr
; call runtime_print_newline
; mov rdi, str_debug_arrow_r
; call runtime_print_string
; mov rdi, [rbp-40]
; call sexp_print
; call runtime_print_newline
; ;;
mov rdi, [rbp-8]
call parser_is_finished
cmp rax, 0
jne .finished
jmp .loop
.failed:
mov rdi, text_error_failed_to_parse
call runtime_print_string
call runtime_print_newline
mov rdi, [rbp-8]
call parser_get_index
mov rdi, rax
call runtime_print_uint64
call runtime_print_newline
mov rdi, 2
call runtime_exit
.finished:
mov rax, [rbp-56] ; statements
.ok:
leave
ret
;;; rdi: *parser
parse_statement:
push rbp
mov rbp, rsp
sub rsp, 56
mov qword [rbp-56], 0 ; tmp-arg
mov qword [rbp-48], 0 ; last-args
mov qword [rbp-40], 0 ; args
mov qword [rbp-32], 0 ; second-or-later-flag
mov qword [rbp-24], 0 ; return
mov qword [rbp-16], 0 ; u64, initial offset
mov [rbp-8], rdi ; *parser
;;
call parser_get_index
mov [rbp-16], rax
;;
mov rdi, [rbp-8]
call parser_skip_space
;; empty-statement
mov rdi, [rbp-8]
call parse_newline_or_term
cmp rax, 0
jne .succeeded
;; label-statement
mov rdi, [rbp-8]
call parse_label_statement
mov [rbp-24], rax
mov rdi, [rbp-8]
call parser_is_failed
cmp rax, 0
je .label_found
;; operation-statement
;; Reset failure of label-parsing
mov rdi, [rbp-8]
call parser_reset_failed
mov rdi, [rbp-8]
call parse_symbol
mov [rbp-24], rax
mov rdi, [rbp-8]
call parser_is_failed
cmp rax, 0
jne .failed
; mov rdi, [rbp-24]
; call sexp_print
.loop_args:
mov rdi, [rbp-8]
call parser_skip_space
mov rdi, [rbp-8]
call parse_newline_or_term
cmp rax, 0
jne .inst_found
;; Reset failure of newline parsing
mov rdi, [rbp-8]
call parser_reset_failed
mov rdi, [rbp-32] ; second-or-later-flag
cmp rdi, 0
je .loop_args_skip_comma
;;
mov rdi, [rbp-8]
call parse_comma
cmp rax, 0
je .failed
.loop_args_skip_comma:
mov rdi, [rbp-8]
call parser_skip_space
mov qword [rbp-32], 1
mov rdi, [rbp-8]
call parse_expr
mov [rbp-56], rax ; tmp-arg
mov rdi, [rbp-8]
call parser_is_failed
cmp rax, 0
jne .failed
;; append to args
mov rdi, [rbp-56]
mov rsi, 0
call sexp_alloc_cons
mov [rbp-56], rax ; (value, nil)
mov rax, [rbp-40] ; args
cmp rax, 0
jne .update_args
mov rax, [rbp-56] ; (value, nil)
mov [rbp-40], rax ; args
mov [rbp-48], rax ; last-args
jmp .loop_args
.update_args:
mov rdi, [rbp-48] ; last-args
mov rsi, [rbp-56] ; (value, nil)
call sexp_update_cdr
mov rax, [rbp-56] ; (value, nil)
mov [rbp-48], rax ; last-args
jmp .loop_args
.inst_found:
;; ((1, value), (args...) | nil)
mov rdi, 1
mov rsi, 0
call sexp_alloc_int
mov rdi, rax ; 1
mov rsi, [rbp-24] ; value
call sexp_alloc_cons
mov rdi, rax ; (1, value)
mov rsi, [rbp-40] ; (args...) | nil
call sexp_alloc_cons
mov [rbp-24], rax ; ((1, value), (args...) | nil)
jmp .succeeded
.label_found:
mov rdi, [rbp-24] ; (type . value)
call tnode_label_is_local
cmp rax, 0
jne .skip_set_last_label
mov rdi, [rbp-24] ; (type . value)
call tnode_value
mov rdi, [rbp-8] ; parser*
mov rsi, rax
call parser_set_last_label
.skip_set_last_label:
mov rdi, [rbp-24] ; (type . value)
call tnode_value
mov [rbp-24], rax ; value
;; ((0, value), nil)
mov rdi, 0
mov rsi, 0
call sexp_alloc_int
mov rdi, rax ; 0
mov rsi, [rbp-24] ; value
call sexp_alloc_cons
mov rdi, rax ; (0, value)
mov rsi, 0 ; nil
call sexp_alloc_cons
mov [rbp-24], rax ; ((0, value), nil)
.succeeded:
mov rax, [rbp-24]
jmp .ok
.failed:
;; revert
;;mov rdi, [rbp-8]
;;mov rsi, [rbp-16]
;;call parser_move_offset
.ok:
leave
ret
;;; rdi: *parser
parse_expr:
push rbp
mov rbp, rsp
sub rsp, 32
mov qword [rbp-32], 0 ; tmp_return
mov qword [rbp-24], 0 ; return
mov qword [rbp-16], 0 ; u64, initial offset
mov [rbp-8], rdi ; *parser
call parser_get_index
mov [rbp-16], rax
mov rdi, [rbp-8]
call parser_skip_space
;; addressing
mov rdi, [rbp-8]
call parse_addressing
mov [rbp-24], rax
mov rdi, [rbp-8]
call parser_is_failed
cmp rax, 0
je .break
;; reset
mov rdi, [rbp-8]
call parser_reset_failed
;; constant-expr
mov rdi, [rbp-8]
call parse_expr_add_sub
mov [rbp-24], rax
mov rdi, [rbp-8]
call parser_is_failed
cmp rax, 0
je .break
;; failed...
jne .failed
.break:
; mov rdi, [rbp-24]
; call sexp_print
mov rax, [rbp-24]
jmp .ok
.failed:
;; revert
;mov rdi, [rbp-8]
;mov rsi, [rbp-16]
;call parser_move_offset
mov rdi, [rbp-8]
call parser_set_failed
.ok:
leave
ret
;;; rdi:
util_size_num:
cmp rdi, 1
je .size_1
cmp rdi, 2
je .size_2
cmp rdi, 4
je .size_4
cmp rdi, 8
je .size_8
;; debug
mov rdi, 104
call runtime_exit
.size_1:
mov rax, 0
jmp .break
.size_2:
mov rax, 1
jmp .break
.size_4:
mov rax, 2
jmp .break
.size_8:
mov rax, 3
jmp .break
.break:
ret
;; [REG] | [DISP]
const_addr_pat_0_0_0_r: equ 0x01
const_addr_pat_0_0_0_d: equ 0x02
;; [PAT*N] | [REG+PAT] | [REG+DISP]
const_addr_pat_0_0_p_i: equ 0x03
const_addr_pat_0_0_r_p: equ 0x04
const_addr_pat_0_0_r_d: equ 0x05
const_addr_pat_0_0_r_d_signed: equ 0x15
;; [PAT*N+REG] | [REG+PAT+DISP] | [REG+PAT*N]
const_addr_pat_0_r_p_d: equ 0x06
const_addr_pat_0_r_p_d_signed: equ 0x16
const_addr_pat_0_p_i_r: equ 0x07
const_addr_pat_0_r_p_i: equ 0x08
;; [PAT*N+REG+DISP]
const_addr_pat_p_i_r_d: equ 0x0e
const_addr_pat_p_i_r_d_signed: equ 0x1e
g_parse_addressing_pattern_size: equ 18
g_parse_addressing_pattern:
;; [REG]
db 1, const_addr_pat_0_0_0_r
db 0, 1 ; REG
db 0, 0
db 0, 0
db 0, 0
;; [DISP]
db 1, const_addr_pat_0_0_0_d
db 0, 4 ; IMM
db 0, 0
db 0, 0
db 0, 0
db 1, const_addr_pat_0_0_0_d
db 0, 2 ; LABEL
db 0, 0
db 0, 0
db 0, 0
;; [REG+PAT]
db 2, const_addr_pat_0_0_r_p
db 0, 1 ; REG
db 0, 1 ; + PAT
db 0, 0
db 0, 0
;; [REG+DISP]
db 2, const_addr_pat_0_0_r_d
db 0, 1 ; REG
db 0, 4 ; + IMM
db 0, 0
db 0, 0
db 2, const_addr_pat_0_0_r_d_signed
db 0, 1 ; REG
db 1, 4 ; - IMM
db 0, 0
db 0, 0
db 2, const_addr_pat_0_0_r_d
db 0, 1 ; REG
db 0, 2 ; + LABEL
db 0, 0
db 0, 0
db 2, const_addr_pat_0_0_r_d_signed
db 0, 1 ; REG
db 1, 2 ; - LABEL
db 0, 0
db 0, 0
;; [PAT+REG+DISP]
db 3, const_addr_pat_0_r_p_d
db 0, 1 ; PAT
db 0, 1 ; + REG
db 0, 4 ; + IMM
db 0, 0
db 3, const_addr_pat_0_r_p_d_signed
db 0, 1 ; PAT
db 0, 1 ; + REG
db 1, 4 ; - IMM
db 0, 0
db 3, const_addr_pat_0_r_p_d
db 0, 1 ; PAT
db 0, 1 ; + REG
db 0, 2 ; + LABEL
db 0, 0
db 3, const_addr_pat_0_r_p_d_signed
db 0, 1 ; PAT
db 0, 1 ; + REG
db 1, 2 ; - LABEL
db 0, 0
;; [PAT*N+REG]
db 3, const_addr_pat_0_p_i_r
db 0, 1 ; PAT
db 2, 4 ; * IMM
db 0, 1 ; + REG
db 0, 0
;; [REG+PAT*N]
db 3, const_addr_pat_0_r_p_i
db 0, 1 ; REG
db 0, 1 ; + PAT
db 2, 4 ; * IMM
db 0, 0
;; [PAT*N+REG+DISP]
db 4, const_addr_pat_p_i_r_d
db 0, 1 ; REG
db 2, 4 ; * IMM
db 0, 1 ; + REG
db 0, 4 ; + IMM
db 4, const_addr_pat_p_i_r_d_signed
db 0, 1 ; REG
db 2, 4 ; * IMM
db 0, 1 ; + REG
db 1, 4 ; - IMM
db 4, const_addr_pat_p_i_r_d
db 0, 1 ; REG
db 2, 4 ; * IMM
db 0, 1 ; + REG
db 0, 2 ; + IMM
db 4, const_addr_pat_p_i_r_d_signed
db 0, 1 ; REG
db 2, 4 ; * IMM
db 0, 1 ; + REG
db 1, 2 ; - IMM
;;; rdi:
;;; rsi:
;;; rcx: counter
parse_check_addressing_pattern:
push rbp
mov rbp, rsp
sub rsp, 144
mov byte [rbp-48], 0 ; result
mov qword [rbp-40], 0 ; counter
mov [rbp-32], rcx ; number
mov [rbp-24], rdx ; pattern*
mov [rbp-16], rsi ; op*
mov [rbp-8], rdi ; sexp**
;; check length
mov rdi, [rbp-24] ; pattern*
mov al, [rdi] ; length
inc qword [rbp-24] ; pattern* ++
mov rcx, [rbp-32] ; number
cmp al, cl
jne .failed
;; get pattern-enum
mov rdi, [rbp-24] ; pattern*
mov al, [rdi] ; length
inc qword [rbp-24] ; pattern* ++
mov [rbp-48], al ; result
.loop:
mov rax, [rbp-40] ; counter
cmp rax, [rbp-32] ; number
je .matched
;; op-loaded
mov rax, [rbp-16]
mov rcx, [rbp-40] ; counter
mov cl, [rcx+rax] ; op[]
;; pattern-op
mov rdi, [rbp-24] ; pattern*
mov dl, [rdi] ; length
inc qword [rbp-24] ; pattern* ++
cmp dl, cl ; pattern-op != op
jne .failed
; mov rax, [rbp-8]
; mov rcx, [rbp-40] ; counter
; mov rdi, [rcx*8+rax] ; sexp[]
; call tnode_print
; call runtime_print_newline
;; sexp*-loaded
mov rax, [rbp-8]
mov rcx, [rbp-40] ; counter
mov rdi, [rcx*8+rax] ; sexp[]
call tnode_type_tag
;; pattern-type
mov rdi, [rbp-24] ; pattern*
mov dl, [rdi] ; length
inc qword [rbp-24] ; pattern* ++
cmp dl, al ; pattern-op != op
jne .failed
inc qword [rbp-40] ; counter
jmp .loop
.matched:
xor rax, rax
mov al, [rbp-48] ; result
jmp .break
.failed:
xor rax, rax
.break:
leave
ret
;;; rdi: *parser
parse_addressing:
push rbp
mov rbp, rsp
sub rsp, 144
mov qword [rbp-144], 0 ; table-offset
mov qword [rbp-136], 0 ; pattern-index
mov qword [rbp-128], 0 ; kind
;; [scale-reg * scale-N + base-reg +/- disp]
mov qword [rbp-120], 0 ; sexp*, scale-reg
mov qword [rbp-112], 0 ; sexp*, scale-N
mov qword [rbp-104], 0 ; sexp*, base-reg
mov qword [rbp-96], 0 ; sexp*, disp
mov qword [rbp-88], 0 ; sexp*, exprs[0]
mov qword [rbp-80], 0 ; sexp*, exprs[1]
mov qword [rbp-72], 0 ; sexp*, exprs[2]
mov qword [rbp-64], 0 ; sexp*, exprs[3]
;; 0 = plus, 1 = minus, 2 = multiply
mov byte [rbp-56], 0 ; ops[0], not-used
mov byte [rbp-55], 0 ; ops[1]
mov byte [rbp-54], 0 ; ops[2]
mov byte [rbp-53], 0 ; ops[3]
mov qword [rbp-48], 0 ; counter
mov qword [rbp-40], 0 ; expected-size. 0 is unknown
mov qword [rbp-32], 0 ; last-as-signed
mov qword [rbp-24], 0 ; sexp*, candidate
mov qword [rbp-16], 0 ; u64, initial offset
mov [rbp-8], rdi ; *parser
call parser_get_index
mov [rbp-16], rax
;; addressing
;; normal register
mov rdi, [rbp-8]
call parse_reg_value
mov [rbp-24], rax ; candidate
mov rdi, [rbp-8]
call parser_is_failed
cmp rax, 0
je .succeeded
;;
;; reset failure
mov rdi, [rbp-8]
call parser_reset_failed
;; size-prefix
mov rdi, [rbp-8]
call parse_size
mov qword [rbp-40], rax ; size = .
mov rdi, [rbp-8]
call parser_is_failed
cmp rax, 0
jne .failed
;;
mov rdi, [rbp-8]
call parser_skip_space
mov qword [rbp-48], 0 ; count = 0
;; [
mov rdi, [rbp-8]
call parse_bracket_l
cmp rax, 0
je .failed
;; constant / reg
mov rdi, [rbp-8]
mov rsi, 0
call parse_reg_or_constant
mov rcx, [rbp-48] ; counter
lea rdx, [rbp-88] ; sexp**
mov [rcx*8+rdx], rax ; exprs[counter] = expr
mov rdi, [rbp-8]
call parser_is_failed
cmp rax, 0
jne .failed
inc qword [rbp-48] ; counter++
.loop:
mov rdi, [rbp-8]
call parser_skip_space
;; + / - / *
mov rdi, [rbp-8]
call parse_plus
cmp rax, 0
jne .op_plus
;; + / - / *
mov rdi, [rbp-8]
call parse_minus
cmp rax, 0
jne .op_minus
;; + / - / *
mov rdi, [rbp-8]
call parse_multiply
cmp rax, 0
jne .op_multiply
jmp .enclose
.op_plus:
mov rcx, [rbp-48] ; counter
lea rdx, [rbp-56] ; ops*
mov byte [rcx+rdx], 0 ; ops[counter] = plus
mov qword [rbp-32], 0 ; last-as-signed
jmp .next_value
.op_minus:
mov rcx, [rbp-48] ; counter
lea rdx, [rbp-56] ; ops*
mov byte [rcx+rdx], 0 ; ops[counter] = plus (means, X + "minus-value")
mov qword [rbp-32], 1 ; last-as-signed
jmp .next_value
.op_multiply:
mov rcx, [rbp-48] ; counter
lea rdx, [rbp-56] ; ops*
mov byte [rcx+rdx], 2 ; ops[counter] = multiply
mov qword [rbp-32], 0 ; last-as-signed
jmp .next_value
.next_value:
mov rdi, [rbp-8]
call parser_skip_space
;; constant / reg
mov rdi, [rbp-8]
mov rsi, [rbp-32] ; last-as-signed
call parse_reg_or_constant
mov rcx, [rbp-48] ; counter
lea rdx, [rbp-88] ; sexp**
mov [rcx*8+rdx], rax ; exprs[counter] = expr
mov rdi, [rbp-8]
call parser_is_failed
cmp rax, 0
jne .failed
inc qword [rbp-48] ; counter++
jmp .loop
.enclose:
mov rdi, [rbp-8]
call parser_skip_space
;; ]
mov rdi, [rbp-8]
call parse_bracket_r
cmp rax, 0
je .failed
.parsed:
; ;; <--
; mov rdi, str_debug_parse_addressing
; call runtime_print_string
; call runtime_print_newline
;
; mov rdi, [rbp-88] ; 0
; call sexp_print
; call runtime_print_newline
;
; mov rdi, [rbp-80] ; 1
; call sexp_print
; call runtime_print_newline
;
; mov rdi, [rbp-72] ; 2
; call sexp_print
; call runtime_print_newline
;
; mov rdi, [rbp-64] ; 3
; call sexp_print
; call runtime_print_newline
; ;; -->
mov qword [rbp-144], g_parse_addressing_pattern
mov qword [rbp-136], 0 ; match-index
.match_loop:
cmp qword [rbp-136], g_parse_addressing_pattern_size
je .failed
lea rdi, [rbp-88]
lea rsi, [rbp-56]
mov rdx, [rbp-144]
mov rcx, [rbp-48]
call parse_check_addressing_pattern
cmp rax, 0
jne .pattern_matched
add qword [rbp-144], 10 ; template += 10
inc qword [rbp-136]
jmp .match_loop
.pattern_matched:
mov [rbp-128], rax ; kind
and rax, 0x0f ; kind.type-mask