-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.s
1705 lines (1545 loc) · 44 KB
/
codegen.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
# Role
# ----
# Turn an expression structure into executable code
#
# Expected
# --------
# 1. Address of expression structure is in %rdi
# 2. Address of labels array is in %rsi
# 3. Length of labels array is in %rdx
# 4. File descriptor of output file is in %r10
#
# Result
# ------
# 1. In the case of an error, -1 in %rax, the address of the error string in %rdi, the string length in %rsi
# 2. In the case of a success, 0 in %rax
#
# Modus Operandi
# --------------
# 1. Code the preliminaries, that is, the routines and functions that all mindbend programs
# must have.
# 2. Initialize the Data Landscape
#
# Runtime Info
# ------------
# The following are for the generated code, and not the code in this file
#
# The Data Landscape
# ------------------
# All Data Landscape values are stored on the stack and are to be accessed by the base pointer
# They are laid out in the following order
# The Current Gates State (CGS) - initialized to 0 (0 gates open). Position 0
# The gates Time To Stay Open (TTSO) - initialized to 5. Position -8
# The Current Region (CR) - initialized to 0 (Cells Region) Position -16
# The Representation of the Cells Region - 15 contiguous spaces. Don't need initialization Position -24
# The TTL table - 15 contiguous spaces. Initialized to 0. Position -144
# The TTL table ends at position -263
# The stack pointer should point to the position -264 after the Data Landscape initialization
# Each value has a size of 8 bytes
#
# Routines
# --------
# The routines begin with the assumption that the Data Landscape has been initialized
# A return value of 0 in %rax represents success
# A return value of -1 in %rax represents fail
.section .data
.equ TTL_OFFSET, 144
.equ CGS_OFFSET, 0
.equ TTSO_OFFSET, 8
.equ CR_OFFSET, 16
.equ CELLS_OFFSET, 24
.preamble_code:
.equ PREAMBLE_CODE_LEN, 13088
.asciz "
.section .data
.primitive_access_gates_not_open_err_msg:
.equ PRIMITIVE_ACCESS_GATES_NOT_OPEN_ERR_MSG_LEN, 89
.asciz \"Attempting to access primitive when gates aren't fully open at the nth position. Find n.\\n\"
.primitive_access_layers_not_region_err_msg:
.equ PRIMITIVE_ACCESS_LAYERS_NOT_REGION_ERR_MSG_LEN, 85
.asciz \"Attempting to access primitive outside the Layers Region at the nth position. Find n.\\n\"
.drill_gate_not_layers_err_msg:
.equ DRILL_GATE_NOT_LAYERS_ERR_MSG_LEN, 82
.asciz \"Attempting to drill gates when not in Layers Region at the nth position. Find n.\\n\"
.cell_access_routine_err_not_cells_region_err_msg:
.equ CELL_ACCESS_ROUTINE_ERR_NOT_CELLS_REGION_ERR_MSG_LEN, 81
.asciz \"Attempting to access Cell when not in Cells Region at the nth position. Find n.\\n\"
.expression_life_validation_routine_err_dead_cell_err_msg:
.equ EXPRESSION_LIFE_VALIDATION_ROUTINE_ERR_DEAD_CELL_ERR_MSG_LEN, 73
.asciz \"Attempting to make use of Death Expression at the nth position. Find n.\\n\"
.function_validation_routine_err_dead_cell_err_msg:
.equ FUNCTION_VALIDATION_ROUTINE_ERR_DEAD_CELL_ERR_MSG_LEN, 83
.asciz \"Attempting to use Death Expression to go on massacre at the nth position. Find n.\\n\"
.function_validation_routine_err_not_a_function_err_msg:
.equ FUNCTION_VALIDATION_ROUTINE_ERR_NOT_A_FUNCTION_ERR_MSG_LEN, 99
.asciz \"Attempting to use expression without rampage power to go on massacre at the nth position. Find n.\\n\"
.function_execution_routine_err_death_expr_arg_err_msg:
.equ FUNCTION_EXECUTION_ROUTINE_ERR_DEATH_EXPR_ARG_ERR_MSG_LEN, 74
.asciz \"Attempting to consume Death Expression in massacre at position n. Find n.\\n\"
.section .bss
.char_to_print:
.byte 0
.char_gotten:
.byte 0
.section .text
.globl _start
_start:
jmp init_data_landscape
# 1 represents the Layers Region
# 0 represents the Cells Region
primitive_access_routine:
cmp $1, -16(%rbp)
jne primitive_access_routine_err_not_layers_region
cmp $3, (%rbp)
jne primitive_access_routine_err_gates_not_open
movq $0, %rax
ret
primitive_access_routine_err_not_layers_region:
leaq .primitive_access_layers_not_region_err_msg(%rip), %rdi
movq $PRIMITIVE_ACCESS_LAYERS_NOT_REGION_ERR_MSG_LEN, %rsi
movq $-1, %rax
ret
primitive_access_routine_err_gates_not_open:
leaq .primitive_access_gates_not_open_err_msg(%rip), %rdi
movq $PRIMITIVE_ACCESS_GATES_NOT_OPEN_ERR_MSG_LEN, %rsi
movq $-1, %rax
ret
drill_gate_routine:
cmp $1, -16(%rbp)
jne drill_gate_routine_err_not_layers
cmp $3, (%rbp)
je drill_gate_routine_end
incq (%rbp)
cmp $3, (%rbp)
je drill_gate_routine_update_ttso
jmp drill_gate_routine_end
drill_gate_routine_err_not_layers:
leaq .drill_gate_not_layers_err_msg(%rip), %rdi
movq $DRILL_GATE_NOT_LAYERS_ERR_MSG_LEN, %rsi
movq $-1, %rax
ret
drill_gate_routine_update_ttso:
movq $5, -8(%rbp)
jmp drill_gate_routine_end
drill_gate_routine_end:
movq $0, %rax
ret
cell_access_routine:
cmp $0, -16(%rbp)
jne cell_access_routine_err_not_cells_region
movq $0, %rax
ret
cell_access_routine_err_not_cells_region:
leaq .cell_access_routine_err_not_cells_region_err_msg(%rip), %rdi
movq $CELL_ACCESS_ROUTINE_ERR_NOT_CELLS_REGION_ERR_MSG_LEN, %rsi
movq $-1, %rax
ret
# The cell number will be placed in %r15
expression_life_validation_routine:
pushq %r14
pushq %r15
movq %rbp, %r14
subq $144, %r14
imul $-1, %r15
cmp $0, (%r14, %r15, 8)
je expression_life_validation_routine_err_dead_cell
movq $0, %rax
popq %r15
popq %r14
ret
expression_life_validation_routine_err_dead_cell:
popq %r15
popq %r14
leaq .expression_life_validation_routine_err_dead_cell_err_msg(%rip), %rdi
movq $EXPRESSION_LIFE_VALIDATION_ROUTINE_ERR_DEAD_CELL_ERR_MSG_LEN, %rsi
movq $-1, %rax
ret
# The cell number of the expression being validated will be placed in %r15
function_validation_routine:
pushq %r15
pushq %r14
movq %rbp, %r14
subq $144, %r14
imul $-1, %r15
cmp $0, (%r14, %r15, 8)
je function_validation_routine_err_dead_cell
movq %rbp, %r14
subq $24, %r14
movq (%r14, %r15, 8), %r14
cmp $0, %r14
je function_validation_routine_success
cmp $1, %r14
je function_validation_routine_success
cmp $2, %r14
je function_validation_routine_success
cmp $3, %r14
je function_validation_routine_success
jmp function_validation_routine_err_not_a_function
function_validation_routine_err_dead_cell:
popq %r14
popq %r15
leaq .function_validation_routine_err_dead_cell_err_msg(%rip), %rdi
movq $FUNCTION_VALIDATION_ROUTINE_ERR_DEAD_CELL_ERR_MSG_LEN, %rsi
movq $-1, %rax
ret
function_validation_routine_err_not_a_function:
popq %r14
popq %r15
leaq .function_validation_routine_err_not_a_function_err_msg(%rip), %rdi
movq $FUNCTION_VALIDATION_ROUTINE_ERR_NOT_A_FUNCTION_ERR_MSG_LEN, %rsi
movq $-1, %rax
ret
function_validation_routine_success:
popq %r14
popq %r15
movq $0, %rax
ret
# Cell number, base address of args array on stack and number of args
# will be placed in %r15, %r14 and %r13 respectively
function_execution_routine:
pushq %r15 # Saving the cell number so the cell can be killed at the end
movq $0, %r12 # Starting index of args
movq %rbp, %r9
subq $24, %r9
imul $-1, %r15
movq (%r9, %r15, 8), %r15
cmp $0, %r15
je function_execute_subtraction
cmp $1, %r15
je function_execute_addition
cmp $2, %r15
je function_execute_output
jmp function_execute_input
function_execute_subtraction:
cmp $1, %r13
je function_execute_subtraction_one_arg
jmp function_execute_subtraction_multiple_args
function_execute_subtraction_one_arg:
movq (%r14, %r12, 8), %r11
movq %r11, %r15
call expression_life_validation_routine
cmp $-1, %rax
je function_execute_err_dead_arg
movq %rbp, %r9
subq $24, %r9
imul $-1, %r11
movq $0, (%r9, %r11, 8)
jmp function_execute_end
function_execute_subtraction_multiple_args:
movq (%r14, %r12, 8), %r11
movq %r11, %r15
call expression_life_validation_routine
cmp $-1, %rax
je function_execute_err_dead_arg
movq %rbp, %r9
subq $24, %r9 # Offset of cell value from base pointer
imul $-1, %r11
movq (%r9, %r11, 8), %r10 # Result initialized to first cell value
jmp function_execute_subtraction_multiple_args_loop
function_execute_subtraction_multiple_args_loop:
decq %r12
movq %r12, %rcx
addq %r13, %rcx
jz function_execute_end
movq (%r14, %r12, 8), %r11
movq %r11, %r15
call expression_life_validation_routine
cmp $-1, %rax
je function_execute_err_dead_arg
movq %rbp, %r9
subq $24, %r9
imul $-1, %r11
subq (%r9, %r11, 8), %r10
jmp function_execute_subtraction_multiple_args_loop
function_execute_addition:
cmp $1, %r13
je function_execute_addition_one_arg
jmp function_execute_addition_multiple_args
function_execute_addition_one_arg:
movq (%r14, %r12, 8), %r11
movq %r11, %r15
call expression_life_validation_routine
cmp $-1, %rax
je function_execute_err_dead_arg
movq %rbp, %r9
subq $24, %r9
imul $-1, %r11
movq (%r9, %r11, 8), %r10
addq %r10, %r10
movq %r10, (%r9, %r11, 8)
jmp function_execute_end
function_execute_addition_multiple_args:
movq (%r14, %r12, 8), %r11
movq %r11, %r15
call expression_life_validation_routine
cmp $-1, %rax
je function_execute_err_dead_arg
movq %rbp, %r9
subq $24, %r9
imul $-1, %r11
movq (%r9, %r11, 8), %r10
jmp function_execute_addition_multiple_args_loop
function_execute_addition_multiple_args_loop:
decq %r12
movq %r12, %rcx
addq %r13, %rcx
jz function_execute_end
movq (%r14, %r12, 8), %r11
movq %r11, %r15
call expression_life_validation_routine
cmp $-1, %rax
je function_execute_err_dead_arg
movq %rbp, %r9
subq $24, %r9
imul $-1, %r11
addq (%r9, %r11, 8), %r10
jmp function_execute_addition_multiple_args_loop
function_execute_output:
cmp $1, %r13
je function_execute_output_one_arg
jmp function_execute_output_multiple_args
function_execute_output_one_arg:
movq (%r14, %r12, 8), %r11
movq %r11, %r15
call expression_life_validation_routine
cmp $-1, %rax
je function_execute_err_dead_arg
movq %rbp, %r9
subq $24, %r9
imul $-1, %r11
movq (%r9, %r11, 8), %rdi
call putchar
jmp function_execute_end
function_execute_output_multiple_args:
movq (%r14, %r12, 8), %r11
movq %r11, %r15
call expression_life_validation_routine
cmp $-1, %rax
je function_execute_err_dead_arg
movq %rbp, %r9
subq $24, %r9
imul $-1, %r11
movq (%r9, %r11, 8), %r10
decq %r12
movq %r12, %rcx
addq %r13, %rcx
jz function_execute_output_one_arg
movq (%r14, %r12, 8), %r11
movq %r11, %r15
call expression_life_validation_routine
cmp $-1, %rax
je function_execute_err_dead_arg
imul $-1, %r11
movq (%r9, %r11, 8), %r8
imul $10, %r10
addq %r8, %r10
movq %r10, %rdi
call putchar
decq %r12
movq %r12, %rcx
addq %r13, %rcx
jz function_execute_end
jmp function_execute_output_multiple_args
function_execute_input:
decq %r12
movq %r12, %rcx
addq %r13, %r12
jz function_execute_input_end
jmp function_execute_input
function_execute_input_end:
call getchar
movq %r13, %r12
decq %r12
imul $-1, %r12
movq (%r14, %r12, 8), %r10
movq %rbp, %r9
subq $24, %r9
imul $-1, %r10
movq %rax, (%r9, %r10, 8)
jmp function_execute_end
function_execute_end:
movq $1, %r15
call state_update_routine
popq %r15
movq %rbp, %r9
subq $144, %r9
imul $-1, %r15
movq $0, (%r9, %r15, 8)
movq $0, %r12
movq %r13, %r10
decq %r10
imul $-1, %r10
movq (%r14, %r10, 8), %r11
movq %rbp, %r9
subq $144, %r9
imul $-1, %r11
movq $5, (%r9, %r11, 8)
jmp function_execute_kill_args_revive_last
function_execute_kill_args_revive_last:
cmp %r12, %r10
je function_execute_return
movq (%r14, %r12, 8), %r11
movq %rbp, %r9
subq $144, %r9
imul $-1, %r11
movq $0, (%r9, %r11, 8)
decq %r12
jmp function_execute_kill_args_revive_last
function_execute_err_dead_arg:
popq %r15
leaq .function_execution_routine_err_death_expr_arg_err_msg(%rip), %rdi
movq $FUNCTION_EXECUTION_ROUTINE_ERR_DEATH_EXPR_ARG_ERR_MSG_LEN, %rsi
ret
function_execute_return:
movq $0, %rax
ret
# The update TTSO arg is in %r15
state_update_routine:
cmp $0, %r15
je state_update_routine_update_ttl
cmp $0, -8(%rbp)
je state_update_routine_update_ttl
decq -8(%rbp)
cmp $0, -8(%rbp)
jne state_update_routine_update_ttl
movq $0, (%rbp)
jmp state_update_routine_update_ttl
state_update_routine_update_ttl:
pushq %r12
pushq %r13
movq $0, %r12
movq %rbp, %r13
subq $144, %r13
jmp state_update_routine_update_ttl_loop
state_update_routine_update_ttl_loop:
cmp $0, (%r13, %r12, 8)
je state_update_routine_update_ttl_loop_repeat
decq (%r13, %r12, 8)
jmp state_update_routine_update_ttl_loop_repeat
state_update_routine_update_ttl_loop_repeat:
decq %r12
cmp $-15, %r12
jne state_update_routine_update_ttl_loop
popq %r13
popq %r12
ret
init_data_landscape:
movq %rsp, %rbp
subq $264, %rsp
movq $0, (%rbp)
movq $5, -8(%rbp)
movq $0, -16(%rbp)
movq $0, -144(%rbp)
movq $0, -152(%rbp)
movq $0, -160(%rbp)
movq $0, -168(%rbp)
movq $0, -176(%rbp)
movq $0, -184(%rbp)
movq $0, -192(%rbp)
movq $0, -200(%rbp)
movq $0, -208(%rbp)
movq $0, -216(%rbp)
movq $0, -224(%rbp)
movq $0, -232(%rbp)
movq $0, -240(%rbp)
movq $0, -248(%rbp)
movq $0, -256(%rbp)
jmp main
print:
pushq %r8
pushq %r9
pushq %rax
pushq %rdx
pushq %rcx
pushq %r11
movq %rdi, %r8
movq %rsi, %r9
movq $1, %rax
movq $1, %rdi
movq %r8, %rsi
movq %r9, %rdx
syscall
popq %r11
popq %rcx
popq %rdx
popq %rax
popq %r9
popq %r8
ret
putchar:
pushq %r8
pushq %rsi
movq %rdi, %r8
leaq .char_to_print(%rip), %rdi
movb %r8b, (%rdi)
movq $1, %rsi
call print
popq %rsi
popq %r8
ret
getchar:
pushq %rdi
pushq %rsi
pushq %rdx
movq $2, %rdi # Standard input
leaq .char_gotten(%rip), %rsi
movq $1, %rdx
movq $0, %rax
syscall
movq (%rsi), %rax
popq %rdx
popq %rsi
popq %rdi
ret
print_err_and_exit:
call print
jmp end_fail
end_success:
movq $60, %rax
movq $0, %rdi
syscall
end_fail:
movq $60, %rax
movq $1, %rdi
syscall
main:
"
.call_par_code:
.equ CALL_PAR_CODE_LEN, 79
.asciz "
call primitive_access_routine
cmp $-1, %rax
je print_err_and_exit
"
.call_dgr_code:
.equ CALL_DGR_CODE_LEN, 73
.asciz "
call drill_gate_routine
cmp $-1, %rax
je print_err_and_exit
"
.call_car_code:
.equ CALL_CAR_CODE_LEN, 74
.asciz "
call cell_access_routine
cmp $-1, %rax
je print_err_and_exit
"
.call_elvr_code:
.equ CALL_ELVR_CODE_LEN, 89
.asciz "
call expression_life_validation_routine
cmp $-1, %rax
je print_err_and_exit
"
.call_fvr_code:
.equ CALL_FVR_CODE_LEN, 82
.asciz "
call function_validation_routine
cmp $-1, %rax
je print_err_and_exit
"
.call_fer_code:
.equ CALL_FER_CODE_LEN, 81
.asciz "
call function_execution_routine
cmp $-1, %rax
je print_err_and_exit
"
.call_sur_code:
.equ CALL_SUR_CODE_LEN, 49
.asciz "
movq $1, %r15
call state_update_routine
"
.call_sur_no_update_ttso_code:
.equ CALL_SUR_NO_UPDATE_TTSO_CODE_LEN, 49
.asciz "
movq $0, %r15
call state_update_routine
"
.change_region_to_cells_code:
.equ CHANGE_REGION_TO_CELLS_CODE_LEN, 23
.asciz "
movq $0, -16(%rbp)
"
.change_region_to_layers_code:
.equ CHANGE_REGION_TO_LAYERS_CODE_LEN, 23
.asciz "
movq $1, -16(%rbp)
"
.jump_to_label_code_start:
.equ JUMP_TO_LABEL_CODE_START_LEN, 9
.asciz "
jmp "
.jump_to_label_code_end:
.equ JUMP_TO_LABEL_CODE_END_LEN, 1
.asciz "\n"
.label_code_end:
.equ LABEL_CODE_END_LEN, 2
.asciz ":\n"
.jump_code_start:
.equ JUMP_CODE_START_LEN, 5
.asciz "\tjmp "
.jump_code_end:
.equ JUMP_CODE_END_LEN, 1
.asciz "\n"
.cond_jump_code_start:
.equ COND_JUMP_CODE_START_LEN, 23
.asciz "\tcmp $0, -24(%rbp)\n\tje "
.jump_end_success:
.equ JUMP_END_SUCCESS_LEN, 17
.asciz "\tjmp end_success\n"
.mov_0:
.equ MOV_0_LEN, 10
.asciz "\tmovq $0, "
.mov_1:
.equ MOV_1_LEN, 10
.asciz "\tmovq $1, "
.mov_2:
.equ MOV_2_LEN, 10
.asciz "\tmovq $2, "
.mov_3:
.equ MOV_3_LEN, 10
.asciz "\tmovq $3, "
.mov_4:
.equ MOV_4_LEN, 10
.asciz "\tmovq $4, "
.mov_5:
.equ MOV_5_LEN, 10
.asciz "\tmovq $5, "
.mov_6:
.equ MOV_6_LEN, 10
.asciz "\tmovq $6, "
.mov_7:
.equ MOV_7_LEN, 10
.asciz "\tmovq $7, "
.mov_8:
.equ MOV_8_LEN, 10
.asciz "\tmovq $8, "
.mov_9:
.equ MOV_9_LEN, 10
.asciz "\tmovq $9, "
.deref_base_pointer:
.equ DEREF_BASE_POINTER_LEN, 7
.asciz "(%rbp)\n"
.deref_base_pointer_no_newline:
.equ DEREF_BASE_POINTER_NO_NEWLINE_LEN, 6
.asciz "(%rbp)"
.mov_num_start:
.equ MOV_NUM_START_LEN, 7
.asciz "\tmovq $"
.mov_start:
.equ MOV_START_LEN, 6
.asciz "\tmovq "
.r15:
.equ R15_LEN, 4
.asciz "%r15"
.comma:
.equ COMMA_LEN, 2
.asciz ", "
.args_array_init_code:
.equ ARGS_ARRAY_INIT_CODE_LEN, 32
.asciz "\tmovq %rsp, %r14\n\tsubq $8, %r14\n"
.push_int_start_code:
.equ PUSH_INT_START_CODE_LEN, 8
.asciz "\tpushq $"
.r13:
.equ R13_LEN, 4
.asciz "%r13"
.pop_r15_code:
.equ POP_R15_CODE_LEN, 11
.asciz "\tpopq %r15\n"
.section .text
codegen_code:
pushq %rdi
pushq %rsi
pushq %rdx
movq %r10, %rdi
leaq .preamble_code(%rip), %rsi # Coding preliminaries
movq $PREAMBLE_CODE_LEN, %rdx
call utils_write_file
cmp $0, %rax
jl codegen_code_err_write
popq %rdx
popq %rsi
popq %rdi
jmp codegen_code_loop
ret
codegen_code_err_write:
popq %rax
popq %rax
popq %rax
movq $-1, %rax
ret # Return the error
codegen_code_loop:
movq ORG_EXPR_CHILD_OFFSET(%rdi), %r8 # Address of current Organism Expression child
cmpb $PRIMITIVE_EXPRESSION, (%r8)
je codegen_code_lone_primitive_expr
cmpb $REGION_EXPRESSION, (%r8)
je codegen_code_region_expr
cmpb $LABEL_EXPRESSION, (%r8)
je codegen_code_label_expr
cmpb $JUMP_EXPRESSION, (%r8)
je codegen_code_jump_expr
cmpb $DRILL_EXPRESSION, (%r8)
je codegen_code_drill_expr
cmpb $CELL_EXPRESSION, (%r8)
je codegen_code_lone_cell_expr
cmpb $LEACH_EXPRESSION, (%r8)
je codegen_code_leach_expr
ret
codegen_code_loop_repeat:
movq ORG_EXPR_NEXT_ORG_OFFSET(%rdi), %rdi
cmp $NULL_EXPRESSION, (%rdi)
je codegen_code_loop_end
jmp codegen_code_loop
codegen_code_loop_end:
leaq .jump_end_success(%rip), %rsi
movq $JUMP_END_SUCCESS_LEN, %rdx
movq %r10, %rdi
call utils_write_file
cmp $0, %rax
jl codegen_code_loop_end_err
movq $0, %rax
ret
codegen_code_loop_end_err:
movq $-1, %rax
ret
codegen_code_lone_primitive_expr:
pushq %rdi
pushq %rsi
pushq %rdx
movq %r10, %rdi
leaq .call_par_code(%rip), %rsi
movq $CALL_PAR_CODE_LEN, %rdx
call utils_write_file
cmp $0, %rax
jl codegen_code_lone_primitive_expr_err_write
popq %rdx
popq %rsi
popq %rdi
jmp codegen_code_loop_repeat
codegen_code_lone_primitive_expr_err_write:
popq %rax
popq %rax
popq %rax
movq $-1, %rax
ret
codegen_code_region_expr:
movb REGION_EXPR_IDENT_ADDR_OFFSET(%r8), %r15b # Region number
cmpb $CELLS_REGION, %r15b
je codegen_code_region_expr_cells
jmp codegen_code_region_expr_layers
codegen_code_region_expr_cells:
pushq %rdi
pushq %rsi
pushq %rdx
movq %r10, %rdi
leaq .change_region_to_cells_code(%rip), %rsi
movq $CHANGE_REGION_TO_CELLS_CODE_LEN, %rdx
call utils_write_file
cmp $0, %rax
jl codegen_code_region_expr_err_write
popq %rdx
popq %rsi
popq %rdi
call codegen_code_sur_call
cmp $0, %rax
jl codegen_code_region_expr_err_write
jmp codegen_code_loop_repeat
codegen_code_region_expr_layers:
pushq %rdi
pushq %rsi
pushq %rdx
movq %r10, %rdi
leaq .change_region_to_layers_code(%rip), %rsi
movq $CHANGE_REGION_TO_LAYERS_CODE_LEN, %rdx
call utils_write_file
cmp $0, %rax
jl codegen_code_region_expr_err_write
popq %rdx
popq %rsi
popq %rdi
call codegen_code_sur_call
cmp $0, %rax
jl codegen_code_region_expr_err_write
jmp codegen_code_loop_repeat
codegen_code_region_expr_err_write:
popq %rax
popq %rax
popq %rax
movq $-1, %rax
ret
codegen_code_label_expr:
pushq %rdi
pushq %rsi
pushq %rdx
movq %r10, %rdi
leaq .jump_to_label_code_start(%rip), %rsi
movq $JUMP_TO_LABEL_CODE_START_LEN, %rdx
call utils_write_file
cmp $0, %rax
jl codegen_code_label_expr_err_write
movq LABEL_EXPR_NAME_ADDR_OFFSET(%r8), %rdi
call utils_strlen
movq %rax, %rdx
movq %rax, %r11 # Saving string length for later use
movq %rdi, %rsi
movq %r10, %rdi
call utils_write_file
cmp $0, %rax
jl codegen_code_label_expr_err_write
movq %rsi, %rcx
popq %rdx # Length of labels array
popq %rsi # Address of labels array
call codegen_get_label_index_no
pushq %rsi
pushq %rdx
movq %rax, %rsi
movq %rsi, %r12 # Saving for later user
call utils_write_int_file
movq %r11, %r13 # Save the label length
leaq .jump_to_label_code_end(%rip), %rsi
movq $JUMP_TO_LABEL_CODE_END_LEN, %rdx
call utils_write_file
cmp $0, %rax
jl codegen_code_label_expr_err_write
movq LABEL_EXPR_NAME_ADDR_OFFSET(%r8), %rsi
movq %r13, %rdx
call utils_write_file
cmp $0, %rax
jl codegen_code_label_expr_err_write
movq %r12, %rsi
call utils_write_int_file
cmp $0, %rax
jl codegen_code_label_expr_err_write
leaq .label_code_end(%rip), %rsi
movq $LABEL_CODE_END_LEN, %rdx
call utils_write_file
cmp $0, %rax
jl codegen_code_label_expr_err_write
popq %rdx
popq %rsi
popq %rdi
jmp codegen_code_loop_repeat
codegen_code_label_expr_err_write:
popq %rax
popq %rax
popq %rax
movq $-1, %rax
ret
codegen_code_jump_expr:
pushq %rdi
pushq %rsi
pushq %rdx
movq %r10, %rdi
cmpb $1, JUMP_EXPR_IS_COND_OFFSET(%r8)
je codegen_code_cond_jump_expr
leaq .jump_code_start(%rip), %rsi
movq $JUMP_CODE_START_LEN, %rdx
call utils_write_file
cmp $0, %rax
jl codegen_code_jump_expr_err_write
jmp codegen_code_jump_expr_end
codegen_code_cond_jump_expr:
leaq .cond_jump_code_start(%rip), %rsi
movq $COND_JUMP_CODE_START_LEN, %rdx
call utils_write_file
cmp $0, %rax
jl codegen_code_jump_expr_err_write
jmp codegen_code_jump_expr_end
codegen_code_jump_expr_end:
movq JUMP_EXPR_NAME_ADDR_OFFSET(%r8), %rdi
call utils_strlen
movq %rdi, %rsi
movq %r10, %rdi
movq %rax, %rdx
call utils_write_file
cmp $0, %rax
jl codegen_code_jump_expr_err_write
movq %rsi, %rcx
popq %rdx # Length of labels array
popq %rsi # Address of labels array
call codegen_get_label_index_no
pushq %rsi
pushq %rdx
movq %rax, %rsi
movq %rsi, %r12 # Saving for later user
call utils_write_int_file
leaq .jump_code_end(%rip), %rsi
movq $JUMP_CODE_END_LEN, %rdx
call utils_write_file
popq %rdx
popq %rsi
popq %rdi
jmp codegen_code_loop_repeat
codegen_code_jump_expr_err_write:
popq %rax
popq %rax
popq %rax
movq $-1, %rax
ret
codegen_get_label_index_no:
pushq %rdi
pushq %r9
pushq %rsi
pushq %r11
movq %rsi, %r9 # Address of labels array
movq $0, %r11 # Initialize array index to 0
jmp codegen_get_label_index_no_loop