-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.S
2005 lines (1781 loc) · 38 KB
/
main.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
#
# [TEXT]
#
.text
.globl _main
.set obj_type.nil, 1
.set obj_type.cons, 2
.set obj_type.symbol, 3
.set obj_type.integer, 4
.set obj_type.double, 5
.set obj_type.builtin, 6
.set obj_type.lambda, 7
.set obj_field.type, 0
.set obj_field.value, 8
.set obj.size, 16
.set cons_field.car, 0
.set cons_field.cdr, 8
.set cons.size, 16
.set return.ok, 0
.set return.syntax_error, 1
.set return.undefined_symbol, 2
.set return.wrong_arg_count, 3
.set return.type_mismatch, 4
_main:
push rbp
mov rbp, rsp
sub rsp, 32
push rbx
push r12
# create the environment
lea rdi, [rip + obj.nil]
call new_env
mov rbx, rax
# load builtins
mov rdi, rbx
call env_load_builtins
_main.read:
lea rdi, [rip + prompt]
call _readline
mov r12, rax
cmp rax, 0
je _main.read
# save the input string into the history
mov rdi, r12
call _add_history
# parse the input string
mov rdi, r12
mov [rbp - 8], rdi
lea rsi, [rbp - 8]
lea rdx, [rbp - 16]
call read_expr
cmp rax, return.ok
jne _main.error
# free the input buffer
mov rdi, r12
call _free
# evaluate and store the evalutation result
# onto the stack
mov rdi, [rbp - 16]
mov rsi, rbx
lea rdx, [rbp - 24]
call eval_expr
cmp rax, return.ok
jne _main.error
# print the evalutation result
mov rdi, [rbp - 24]
call print_expr
xor rax, rax
lea rdi, [rip + debug.newline]
call _printf
# again
jmp _main.read
_main.error:
lea r12, [rip + errors]
imul rax, 8
add r12, rax
xor rax, rax
mov rdi, [r12]
call _printf
jmp _main.read
_main.fail:
mov rax, 1
_main.done:
pop r12
pop rbx
add rsp, 32
leave
ret
/**
Evaluates an expression.
rdi: expression
rsi: env
rdx: result
*/
eval_expr:
push rbp
mov rbp, rsp
sub rsp, 8
push rbx
push r12
push r13
push r14
push r15
# save args
mov rbx, rdi
mov r12, rsi
mov r13, rdx
# get the expr type
movsx r14, byte ptr [rbx + obj_field.type]
# symbol
cmp r14, obj_type.symbol
jne eval_expr.check_cons
mov rdi, r12
mov rsi, rbx
mov rdx, r13
call env_get
jmp eval_expr.done
eval_expr.check_cons:
# cons
cmp r14, obj_type.cons
je eval_expr.cons
# other
mov [r13], rbx
mov rax, return.ok
jmp eval_expr.done
eval_expr.cons:
# check that the expression is a proper list
mov rdi, rbx
call list?
mov rax, return.syntax_error
jne eval_expr.done
# operator
mov rdi, rbx
call car
mov r14, rax
# args
mov rdi, rbx
call cdr
mov r15, rax
# make sure the operator is a symbol
cmp byte ptr [r14 + obj_field.type], obj_type.symbol
mov rax, return.syntax_error
jne eval_expr.function
# op: quote
mov rdi, [r14 + obj_field.value]
lea rsi, [rip + operator.quote]
call _strcmp
cmp rax, 0
je eval_expr.operator.quote
# op: def
mov rdi, [r14 + obj_field.value]
lea rsi, [rip + operator.def]
call _strcmp
cmp rax, 0
je eval_expr.operator.def
# op: lambda
mov rdi, [r14 + obj_field.value]
lea rsi, [rip + operator.lambda]
call _strcmp
cmp rax, 0
je eval_expr.operator.lambda
# op: function
jmp eval_expr.function
eval_expr.operator.quote:
# make sure the operator has 1 arg
mov rdi, r15
call list_length
cmp rax, 1
mov rax, return.wrong_arg_count
jne eval_expr.done
#
mov rdi, r15
call car
mov [r13], rax
mov rax, return.ok
jmp eval_expr.done
eval_expr.operator.def:
# make sure the operator has 2 args
mov rdi, r15
call list_length
cmp rax, 2
mov rax, return.wrong_arg_count
jne eval_expr.done
#
mov rdi, r15
call car
mov r14, rax
cmp byte ptr [r14 + obj_field.type], obj_type.symbol
mov rax, return.type_mismatch
jne eval_expr.done
mov rdi, r15
call cdr
mov rdi, rax
call car
mov rdi, rax
mov rsi, r12
lea rdx, [rbp - 8]
call eval_expr
cmp rax, return.ok
jne eval_expr.done
mov [r13], r14
mov rdi, r12
mov rsi, r14
mov rdx, [rbp - 8]
call env_set
jmp eval_expr.done
eval_expr.operator.lambda:
# make sure the operator has at least 2 args
mov rdi, r15
call list_length
cmp rax, 2
mov rax, return.wrong_arg_count
jl eval_expr.done
# lambda args
mov rdi, r15
call car
mov r14, rax
# lambda body
mov rdi, r15
call cdr
mov r15, rax
# make the lambda obj
mov rdi, r12
mov rsi, r14
mov rdx, r15
mov rcx, r13
call new_lambda
jmp eval_expr.done
eval_expr.function:
mov [rbp - 8], r14
# evalute operator
mov rdi, r14
mov rsi, r12
lea rdx, [rbp - 8]
call eval_expr
cmp rax, return.ok
jne eval_expr.done
mov rdi, r15
call copy_list
mov r15, rax
mov rbx, rax
mov rdi, rax
# evaluate args
eval_expr.function.next_arg:
call nil?
je eval_expr.function.apply
mov rdx, [rbx + obj_field.value]
mov rdi, [rdx + cons_field.car]
mov rsi, r12
lea rdx, [rdx + cons_field.car]
call eval_expr
cmp rax, return.ok
jne eval_expr.done
mov rdi, rbx
call cdr
mov rbx, rax
mov rdi, rax
jmp eval_expr.function.next_arg
eval_expr.function.apply:
mov rdi, [rbp - 8]
mov rsi, r15
mov rdx, r13
call apply
eval_expr.done:
pop r15
pop r14
pop r13
pop r12
pop rbx
add rsp, 8
leave
retn
/**
Calls a function.
rdi: obj
rsi: args
rdx: result pointer
*/
apply:
push rbp
mov rbp, rsp
sub rsp, 24
push rbx
push r12
push r13
push r14
push r15
mov rbx, rdi
mov r12, rsi
mov r13, rdx
cmp byte ptr [rbx + obj_field.type], obj_type.builtin
je apply.builtin
cmp byte ptr [rbx + obj_field.type], obj_type.lambda
je apply.lambda
mov rax, return.type_mismatch
jmp apply.done
apply.lambda:
mov rdi, rbx
call car
mov rdi, rax
call new_env
# env
mov r14, rax
# arg symbols
mov rdi, rbx
call cdr
mov rdi, rax
call car
mov r15, rax
# fn body
mov rdi, rbx
call cdr
mov rdi, rax
call cdr
mov [rbp - 8], rax
# bind the args
mov rdi, r15
apply.lambda_bind_next:
call nil?
je apply.lambda_evaluate
# no args left
mov rdi, r12
call nil?
mov rax, return.wrong_arg_count
je apply.done
# bind
# symbol
mov rdi, r15
call car
mov [rbp - 16], rax
# value
mov rdi, r12
call car
mov [rbp - 24], rax
# set env
mov rdi, r14
mov rsi, [rbp - 16]
mov rdx, [rbp - 24]
call env_set
# next symbol
mov rdi, r15
call cdr
mov r15, rax
# next arg
mov rdi, r12
call cdr
mov r12, rax
# loop
mov rdi, r15
jmp apply.lambda_bind_next
apply.lambda_evaluate:
# check that there's no arg value left
mov rdi, r12
call nil?
mov rax, return.wrong_arg_count
jne apply.done
# evaluate the body of the lambda
mov rdi, [rbp - 8]
apply.lambda_evaluate_body_next:
call nil?
mov rax, return.ok
je apply.done
# current statement
mov rdi, [rbp - 8]
call car
# eval
mov rdi, rax
mov rsi, r14
mov rdx, r13
call eval_expr
cmp rax, return.ok
jne apply.done
# next statement
mov rdi, [rbp - 8]
call cdr
mov [rbp - 8], rax
mov rdi, rax
jmp apply.lambda_evaluate_body_next
apply.builtin:
mov r8, [rbx + obj_field.value]
mov rdi, r12
mov rsi, r13
call r8
apply.done:
pop r15
pop r14
pop r13
pop r12
pop rbx
add rsp, 24
leave
ret
/**
Copies a list.
rdi: list
*/
copy_list:
push rbp
mov rbp, rsp
sub rsp, 8
push rbx
push r12
push r13
mov rbx, rdi
call nil?
je copy_list.nil
mov rdi, rbx
call car
mov rdi, rax
lea rsi, [rip + obj.nil]
call cons
mov r12, rax
mov r13, rax
copy_list.next:
mov rdi, rbx
call cdr
mov rbx, rax
mov rdi, rax
call nil?
je copy_list.exit
mov rdi, rbx
call car
mov rdi, rax
lea rsi, [rip + obj.nil]
call cons
mov r12, [r12 + obj_field.value]
mov [r12 + cons_field.cdr], rax
mov r12, [r12 + cons_field.cdr]
jmp copy_list.next
copy_list.exit:
mov rax, r13
jmp copy_list.done
copy_list.nil:
lea rax, [rip + obj.nil]
copy_list.done:
pop r13
pop r12
pop rbx
add rsp, 8
leave
ret
/**
Parses a list.
rdi: start
rsi: end
rdx: result
*/
read_list:
push rbp
mov rbp, rsp
sub rsp, 24
push rbx
push r12
push r13
push r14
push r15
# save args
mov rbx, rdi
mov r12, rsi
mov r13, rdx
# end = start
mov [rsi], rdi
# item_ptr = result = nil
lea rax, [rip + obj.nil]
mov [rbp - 24], rax
mov [r13], rax
read_list.continue:
# get the next token
mov rdi, [r12]
lea rsi, [rbp - 8]
mov rdx, r12
call next_token
cmp rax, return.ok
jne read_list.done
# a ) token means the list is over
mov r14, [rbp - 8]
cmp byte ptr [r14], ')'
je read_list.done
#
mov rdi, [rbp - 8]
mov rsi, r12
lea rdx, [rbp - 16]
call read_expr
cmp rax, return.ok
jne read_list.done
# 1st item?
mov rdi, [rbp - 24]
call nil?
jne read_list.nth_item
#
mov rdi, [rbp - 16]
lea rsi, [rip + obj.nil]
call cons
mov [r13], rax
mov [rbp - 24], rax
jmp read_list.continue
read_list.nth_item:
mov rdi, [rbp - 16]
lea rsi, [rip + obj.nil]
call cons
mov r15, [rbp - 24]
mov r15, [r15 + obj_field.value]
mov [r15 + cons_field.cdr], rax
mov [rbp - 24], rax
jmp read_list.continue
read_list.done:
pop r15
pop r14
pop r13
pop r12
pop rbx
add rsp, 24
leave
ret
/**
Reads an expression.
rdi: start
rsi: end
rdx: result
*/
read_expr:
push rbp
mov rbp, rsp
sub rsp, 16
push rbx
push r12
push r13
push r14
mov rbx, rdi
mov r12, rsi
mov r13, rdx
# move to the next token
mov rdi, rbx
lea rsi, [rbp - 8]
mov rdx, r12
call next_token
cmp rax, return.ok
jne read_expr.done
# *token == '(', new list
mov r14, [rbp - 8]
cmp byte ptr [r14], '('
je read_expr.list
# *token == ')', syntax error
cmp byte ptr [r14], ')'
mov rax, return.syntax_error
je read_expr.done
# the token is an expression
mov rdi, [rbp - 8]
mov rsi, [r12]
mov rdx, r13
call read_objs
jmp read_expr.done
read_expr.list:
mov rdi, [r12]
mov rsi, r12
mov rdx, r13
call read_list
read_expr.done:
pop r14
pop r13
pop r12
pop rbx
add rsp, 16
leave
ret
/**
Parses a non-list token.
rdi: start
rsi: end
rdx: result
*/
read_objs:
push rbp
mov rbp, rsp
sub rsp, 8
push rbx
push r12
push r13
mov rbx, rdi
mov r12, rsi
mov r13, rdx
# try to parse the token as a base 10 int
lea rsi, [rbp - 8]
mov rdx, 10
call _strtol
# make sure there isn't any non numeric character left
cmp [rbp - 8], r12
jne read_objs.test_double
# make an int
mov rdi, rax
call new_int
mov [r13], rax
jmp read_objs.done
read_objs.test_double:
# try to parse the token as a double
mov rdi, rbx
lea rsi, [rbp - 8]
call _strtod
# make sure there isn't any non numeric character left
cmp [rbp - 8], r12
jne read_objs.test_nil
# make a dobule
call new_double
mov [r13], rax
jmp read_objs.done
read_objs.test_nil:
# check that the token is "nil"
mov rdi, rbx
lea rsi, [rip + parser.nil]
call _strcasecmp
cmp rax, 0
je read_objs.nil
# it isn't nil, must be a new symbol
mov rdi, rbx
mov rsi, r12
sub rsi, rbx
call strncpyz
# make a symbol
mov rdi, rax
call new_symbol
mov [r13], rax
jmp read_objs.done
read_objs.nil:
lea r12, [rip + obj.nil]
mov [r13], r12
read_objs.done:
mov rax, return.ok
pop r13
pop r12
pop rbx
add rsp, 8
leave
ret
/**
Gets the next token in a string.
rdi: string
rsi: start ptr
rdx: end ptr
*/
next_token:
push rbp
mov rbp, rsp
sub rsp, 8
push rbx
push r12
push r13
# save the string pointer
mov rbx, rdi
mov r12, rsi
mov r13, rdx
# get the offset of the first non whitespace character in the input string
lea rsi, [rip + tokenizer.whitespaces]
call _strspn
# move the string pointer to the offset returned by strspn
add rbx, rax
# fail if the offset is out of bounds
cmp byte ptr [rbx], 0
je next_token.fail
# set the token start address
mov [r12], rbx
# the token is a ( or )
cmp byte ptr [rbx], '('
je next_token.skip_one
cmp byte ptr [rbx], ')'
je next_token.skip_one
# the token is something else
mov rdi, rbx
lea rsi, [rip + tokenizer.delimiters]
call _strcspn
add rbx, rax
jmp next_token.success
next_token.skip_one:
add rbx, 1
jmp next_token.success
next_token.fail:
mov rax, return.syntax_error
mov qword ptr [r12], 0
mov qword ptr [r13], 0
jmp next_token.done
next_token.success:
mov rax, return.ok
mov [r13], rbx
next_token.done:
pop r13
pop r12
pop rbx
add rsp, 8
leave
ret
/**
Creates an environment.
rdi: parent
*/
new_env:
push rbp
mov rbp, rsp
lea rsi, [rip + obj.nil]
call cons
leave
ret
/**
Sets a binding in an environment.
rdi: env
rsi: symbol
rdx: value
*/
env_set:
push rbp
mov rbp, rsp
sub rsp, 8
push rbx
push r12
push r13
push r14
push r15
mov [rbp - 8], rdi
mov rbx, rsi
mov r12, rdx
env_set.next:
call cdr
mov r13, rax
mov rdi, r13
call nil?
je env_set.not_found
mov rdi, r13
call car
mov r14, rax
mov rdi, rax
call car
mov r15, rax
mov rdi, [r15 + obj_field.value]
mov rsi, [rbx + obj_field.value]
call _strcmp
cmp rax, 0
je env_set.found
mov rdi, r13
jmp env_set.next
env_set.found:
mov rdi, [r14 + obj_field.value]
mov [rdi + cons_field.cdr], r12
mov rax, return.ok
jmp env_set.done
env_set.not_found:
mov rdi, rbx
mov rsi, r12
call cons
mov r12, rax
mov rdi, [rbp - 8]
call cdr
mov rdi, r12
mov rsi, rax
call cons
mov rdi, [rbp - 8]
mov rdi, [rdi + obj_field.value]
mov [rdi + cons_field.cdr], rax
mov rax, return.ok
env_set.done:
pop r15
pop r14
pop r13
pop r12
pop rbx
add rsp, 8
leave
ret
/**
Retrieves a binding in an environment.
rdi: env
rsi: symbol
rdx: result
*/
env_get:
push rbp
mov rbp, rsp
sub rsp, 8
push rbx
push r12
push r13
push r14
push r15
mov [rbp - 8], rdi
mov rbx, rsi
mov r12, rdx
env_get.next:
call cdr
mov r13, rax
mov rdi, r13
call nil?
je env_get.parent
mov rdi, r13
call car
mov r14, rax
mov rdi, rax
call car
mov r15, rax
mov rdi, [r15 + obj_field.value]
mov rsi, [rbx + obj_field.value]
call _strcmp
cmp rax, 0
je env_get.found
mov rdi, r13
jmp env_get.next
env_get.found:
mov rdi, r14
call cdr
mov [r12], rax
mov rax, return.ok
jmp env_get.done
env_get.parent:
mov rdi, [rbp - 8]
call car
mov r14, rax
mov rdi, r14
call nil?
mov rax, return.undefined_symbol
je env_get.done
mov rdi, r14
mov rsi, rbx
mov rdx, r12
call env_get
env_get.done:
pop r15
pop r14
pop r13
pop r12
pop rbx
add rsp, 8
leave
ret
/**
Creates a lambda obj.
rdi: env
rsi: args
rdx: body
rcx: pointer to the address of the resulting object
*/
new_lambda:
push rbp
mov rbp, rsp
push rbx
push r12
push r13
push r14
# save args
mov rbx, rdi
mov r12, rsi
mov r13, rdx
mov r14, rcx
# both rsi and rdx must point to a list
mov rdi, r12
call list?
mov rax, return.syntax_error
jne new_lambda.done
mov rdi, r13
call list?
mov rax, return.syntax_error
jne new_lambda.done
# each arg must be a symbol
mov rdi, r12
lea rdx, [rip + check_obj_symbol]
call list_fold
cmp rax, return.ok
jne new_lambda.done
# make the obj
mov rdi, r12
mov rsi, r13
call cons
mov rdi, rbx
mov rsi, rax
call cons
mov byte ptr [rax + obj_field.type], obj_type.lambda
mov [r14], rax
mov rax, return.ok
new_lambda.done:
pop r14
pop r13
pop r12
pop rbx
leave
ret
/**
*/
check_obj_symbol:
push rbp
mov rbp, rsp
cmp byte ptr [rdi + obj_field.type], obj_type.symbol
je check_obj_symbol.ok
mov rax, return.type_mismatch
jmp check_obj_symbol.done
check_obj_symbol.ok: