-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_call.s
971 lines (834 loc) · 15.1 KB
/
system_call.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
.include "types.s"
.text
.p2align 2
.globl _system_call
.equ FUNCTION_NAMES_COUNT, 108
; expected to be called pointing to expression that can be evaluated
; tokens in x0
; cursor pointer in x1
; return expression evaluated parameter in x1
; return success in x0
_get_parameter:
ldr x9, [x1]
ldr x8, [x0, x9, lsl 3] ; load current token
ldr x8, [x8] ; type
cmp x8, TS_CLOSE_PAREN
bne 0f
mov x1, xzr ; return zero if parenthesis found
ret
0:
stp fp, lr, [sp, -32]!
stp x19, x20, [sp, 16]
mov x19, x0
mov x20, x1
bl _expression_eval ; evaluate parameter
ldr x8, [x20] ; load cursor
ldr x9, [x19, x8, lsl 3] ; load token
ldr x9, [x9] ; type
cmp x9, TS_COMMA
bne 1f
; if comma, advance cursor
add x8, x8, 1
str x8, [x20] ; update cursor
1:
mov x1, 1
ldp x19, x20, [sp, 16]
ldp fp, lr, [sp], 32
ret
; tokens in x0
; cursor pointer in x1
; return system function call return value in x0
; return system call performed or not in x1
_system_call: ; called with cursor pointing to identifier
stp fp, lr, [sp, -64]!
str x23, [sp, 48]
stp x21, x22, [sp, 32]
stp x19, x20, [sp, 16]
mov x19, x0
mov x20, x1
ldr x21, [x20]
adrp x22, function_names@page
add x22, x22, function_names@pageoff
ldr x8, [x19, x21, lsl 3] ; load token
ldr x23, [x8, 8] ; load value
mov x24, xzr ; counter
0:
mov x0, x23
ldr x1, [x22, x24, lsl 3] ; arg1
;stp x0, x1, [sp, -16]!
;adrp x0, message1@page
;add x0, x0, message1@pageoff
;bl _printf
;ldp x0, x1, [sp], 16
bl _strcmp
cbz x0, 1f
add x24, x24, 1 ; increment counter
cmp x24, FUNCTION_NAMES_COUNT
blt 0b
; here we didn't find anything so return 0
mov x1, xzr
b 2f
1:
; use offset in x24 to load the jump address
adrp x8, jump_table@page
add x8, x8, jump_table@pageoff
; skip open parenthesis
add x21, x21, 2
str x21, [x20]
ldr x8, [x8, x24, lsl 3] ; load jump address
mov x0, x19 ; tokens
mov x1, x20 ; token cursor
blr x8 ; branch and link the appropriate function
; cursor should be on close paren so need to skip that
ldr x8, [x20]
add x8, x8, 1
str x8, [x20]
; return value in x0
mov x1, 1 ; true
2:
ldp x19, x20, [sp, 16]
ldp x21, x22, [sp, 32]
ldr x23, [sp, 48]
ldp fp, lr, [sp], 64
ret
; ---- NOTE -----
; all of the following procedures accept
; token array pointer in x0
; cursor pointer in x1
b_remove:
ret
b_rename:
ret
b_tmpfile:
ret
b_tmpnam:
ret
b_fclose:
stp fp, lr, [sp, -16]!
bl _get_parameter ; file handle
bl _fclose
ldp fp, lr, [sp], 16
ret
b_fflush:
ret
b_fopen:
stp fp, lr, [sp, -48]!
str x21, [sp, 32]
stp x19, x20, [sp, 16]
mov x19, x0; tokens
mov x20, x1 ; cursor ref
bl _get_parameter ; get filename
mov x21, x0 ; save
mov x0, x19
mov x1, x20
bl _get_parameter ; get mode string
mov x1, x0 ; arg1
mov x0, x21 ; arg0
bl _fopen ; return file handle in x0
ldp x19, x20, [sp, 16]
ldr x21, [sp, 32]
ldp fp, lr, [sp], 48
ret
b_freopen:
ret
b_setbuf:
ret
b_setvbuf:
ret
b_fprintf:
stp fp, lr, [sp, -64]!
stp x23, x24, [sp, 48]
stp x21, x22, [sp, 32]
stp x19, x20, [sp, 16]
mov x19, x0 ; tokens
mov x20, x1 ; cursor reference
bl _get_parameter ; get file pointer
mov x21, x0
mov x0, x19
mov x1, x20
bl _get_parameter ; get parameter for format string
mov x22, x0 ; save parameter
mov x24, xzr ; keep track of stack offset
0:
mov x0, x19
mov x1, x20
bl _get_parameter
cbz x1, 2f
mov x23, x0 ; save parameter
mov x0, x19
mov x1, x20
bl _get_parameter
stp x23, x0, [sp, -16]!
add x24, x24, 16
b 0b
2:
mov x0, x21 ; file pointer
mov x1, x22 ; format string
bl _fprintf
add sp, sp, x24 ; restore stack pointer
ldp x19, x20, [sp, 16]
ldp x21, x22, [sp, 32]
ldp x23, x24, [sp, 48]
ldp fp, lr, [sp], 64
ret
b_fscanf:
stp fp, lr, [sp, -64]!
stp x23, x24, [sp, 48]
stp x21, x22, [sp, 32]
stp x19, x20, [sp, 16]
mov x19, x0 ; tokens
mov x20, x1 ; cursor reference
bl _get_parameter ; get file pointer
mov x21, x0
mov x0, x19
mov x1, x20
bl _get_parameter ; get parameter for format string
mov x22, x0 ; save parameter
mov x24, xzr ; keep track of stack offset
0:
mov x0, x19
mov x1, x20
bl _get_parameter
cbz x1, 2f
mov x23, x0 ; save parameter
mov x0, x19
mov x1, x20
bl _get_parameter
cbz x0, 1f
stp x23, x0, [sp, -16]!
add x24, x24, 16
b 0b
1:
str x23, [sp, -16]!
add x24, x24, 16
2:
mov x0, x21 ; file pointer
mov x1, x22 ; format string
bl _fscanf
add sp, sp, x24 ; restore stack pointer
ldp x19, x20, [sp, 16]
ldp x21, x22, [sp, 32]
ldp x23, x24, [sp, 48]
ldp fp, lr, [sp], 64
ret
; tokens x0
; token cursor x1
b_printf:
stp fp, lr, [sp, -64]!
stp x23, x24, [sp, 48]
stp x21, x22, [sp, 32]
stp x19, x20, [sp, 16]
mov x19, x0 ; tokens
mov x20, x1 ; cursor reference
bl _get_parameter ; get parameter for format string
mov x22, x0 ; save parameter
mov x24, xzr ; keep track of stack offset
0:
mov x0, x19
mov x1, x20
bl _get_parameter
cbz x1, 2f
mov x23, x0 ; save parameter
mov x0, x19
mov x1, x20
bl _get_parameter
cbz x0, 1f
stp x23, x0, [sp, -16]!
add x24, x24, 16
b 0b
1:
str x23, [sp, -16]!
add x24, x24, 16
2:
mov x0, x22
bl _printf
add sp, sp, x24 ; restore stack pointer
ldp x19, x20, [sp, 16]
ldp x21, x22, [sp, 32]
ldp x23, x24, [sp, 48]
ldp fp, lr, [sp], 64
ret
b_scanf:
stp fp, lr, [sp, -64]!
stp x23, x24, [sp, 48]
stp x21, x22, [sp, 32]
stp x19, x20, [sp, 16]
mov x19, x0 ; tokens
mov x20, x1 ; cursor reference
bl _get_parameter ; get parameter for format string
mov x22, x0 ; save parameter
mov x24, xzr ; keep track of stack offset
0:
mov x0, x19
mov x1, x20
bl _get_parameter
cbz x1, 2f
mov x23, x0 ; save parameter
mov x0, x19
mov x1, x20
bl _get_parameter
cbz x0, 1f
stp x23, x0, [sp, -16]!
add x24, x24, 16
b 0b
1:
str x23, [sp, -16]!
add x24, x24, 16
2:
mov x0, x22
bl _scanf
add sp, sp, x24 ; restore stack pointer
ldp x19, x20, [sp, 16]
ldp x21, x22, [sp, 32]
ldp x23, x24, [sp, 48]
ldp fp, lr, [sp], 64
ret
b_sprintf:
ret
b_sscanf:
ret
b_vfprintf:
ret
b_vprintf:
ret
b_vsprintf:
ret
b_fgetc:
ret
b_fgets:
ret
b_fputc:
ret
b_fputs:
ret
b_getc:
ret
b_getchar:
ret
b_gets:
ret
b_putc:
ret
b_putchar:
ret
b_puts:
ret
b_ungetc:
ret
b_fread:
ret
b_fwrite:
ret
b_fgetpos:
ret
b_fseek:
ret
b_fsetpos:
ret
b_ftell:
ret
b_rewind:
ret
b_clearerr:
ret
b_feof:
ret
b_ferror:
ret
b_perror:
ret
; stdio
b_isalnum:
ret
b_isalpha:
ret
b_iscntrl:
ret
b_isdigit:
ret
b_isgraph:
ret
b_islower:
ret
b_isprint:
ret
b_ispunct:
ret
b_isspace:
ret
b_isupper:
ret
b_isxdigit:
ret
b_tolower:
ret
b_toupper:
ret
; cmath
b_cos:
ret
b_sin:
ret
b_tan:
ret
b_acos:
ret
b_asin:
ret
b_atan:
ret
b_atan2:
ret
b_cosh:
ret
b_sinh:
ret
b_tanh:
ret
b_exp:
ret
b_frexp:
ret
b_ldexp:
ret
b_log:
ret
b_log10:
ret
b_modf:
ret
b_pow:
ret
b_sqrt:
ret
b_ceil:
ret
b_floor:
ret
b_fmod:
ret
b_fabs:
ret
b_abs:
ret
; cstring
b_memcpy:
ret
b_memmove:
ret
b_strcpy:
ret
b_strncpy:
ret
b_strcat:
ret
b_strncat:
ret
b_memcmp:
ret
b_strcmp:
ret
b_strcoll:
ret
b_strncmp:
ret
b_strxfrm:
ret
b_memchr:
ret
b_strchr:
ret
b_strcspn:
ret
b_strpbrk:
ret
b_strrchr:
ret
b_strspn:
ret
b_strstr:
ret
b_strtok:
ret
b_memset:
ret
b_strerror:
ret
b_strlen:
ret
; stdlib
b_atof:
ret
b_atoi:
ret
b_atol:
ret
b_strtod:
ret
b_strtol:
ret
b_strtoul:
ret
b_rand:
ret
b_srand:
ret
b_bsearch:
ret
b_qsort:
ret
.data
.p2align 3
function_names:
; stdio from https://www.cplusplus.com/reference/cstdio/
.quad remove
.quad rename
.quad tmpfile
.quad tmpnam
.quad fclose
.quad fflush
.quad fopen
.quad freopen
.quad setbuf
.quad setvbuf
.quad fprintf
.quad fscanf
.quad printf
.quad scanf
.quad sprintf
.quad sscanf
.quad vfprintf
.quad vprintf
.quad vsprintf
.quad fgetc
.quad fgets
.quad fputc
.quad fputs
.quad getc
.quad getchar
.quad gets
.quad putc
.quad putchar
.quad puts
.quad ungetc
; 30
.quad fread
.quad fwrite
.quad fgetpos
.quad fseek
.quad fsetpos
.quad ftell
.quad rewind
.quad clearerr
.quad feof
.quad ferror
.quad perror
; 41
; ctype.h from https://www.cplusplus.com/reference/cctype/
.quad isalnum
.quad isalpha
.quad iscntrl
.quad isdigit
.quad isgraph
.quad islower
.quad isprint
.quad ispunct
.quad isspace
.quad isupper
.quad isxdigit
.quad tolower
.quad toupper
; 54
; cmath https://www.cplusplus.com/reference/cmath/
.quad cos
.quad sin
.quad tan
.quad acos
.quad asin
.quad atan
.quad atan2
.quad cosh
.quad sinh
.quad tanh
; 64
.quad exp
.quad frexp
.quad ldexp
.quad log
.quad log10
.quad modf
.quad pow
.quad sqrt
.quad ceil
.quad floor
.quad fmod
.quad fabs
.quad abs
; cstring https://www.cplusplus.com/reference/cstring/
.quad memcpy
.quad memmove
.quad strcpy
.quad strncpy
; 81
.quad strcat
.quad strncat
.quad memcmp
.quad strcmp
.quad strcoll
.quad strncmp
.quad strxfrm
.quad memchr
.quad strchr
.quad strcspn
.quad strpbrk
.quad strrchr
.quad strspn
.quad strstr
.quad strtok
; 95
.quad memset
.quad strerror
.quad strlen
; cstdlib
.quad atof
.quad atoi
.quad atol
.quad strtod
.quad strtol
.quad strtoul
.quad rand
.quad srand
.quad bsearch
.quad qsort
; 108 I think
jump_table:
.quad b_remove
.quad b_rename
.quad b_tmpfile
.quad b_tmpnam
.quad b_fclose
.quad b_fflush
.quad b_fopen
.quad b_freopen
.quad b_setbuf
.quad b_setvbuf
.quad b_fprintf
.quad b_fscanf
.quad b_printf
.quad b_scanf
.quad b_sprintf
.quad b_sscanf
.quad b_vfprintf
.quad b_vprintf
.quad b_vsprintf
.quad b_fgetc
.quad b_fgets
.quad b_fputc
.quad b_fputs
.quad b_getc
.quad b_getchar
.quad b_gets
.quad b_putc
.quad b_putchar
.quad b_puts
.quad b_ungetc
.quad b_fread
.quad b_fwrite
.quad b_fgetpos
.quad b_fseek
.quad b_fsetpos
.quad b_ftell
.quad b_rewind
.quad b_clearerr
.quad b_feof
.quad b_ferror
.quad b_perror
; stdio
.quad b_isalnum
.quad b_isalpha
.quad b_iscntrl
.quad b_isdigit
.quad b_isgraph
.quad b_islower
.quad b_isprint
.quad b_ispunct
.quad b_isspace
.quad b_isupper
.quad b_isxdigit
.quad b_tolower
.quad b_toupper
; cmath
.quad b_cos
.quad b_sin
.quad b_tan
.quad b_acos
.quad b_asin
.quad b_atan
.quad b_atan2
.quad b_cosh
.quad b_sinh
.quad b_tanh
.quad b_exp
.quad b_frexp
.quad b_ldexp
.quad b_log
.quad b_log10
.quad b_modf
.quad b_pow
.quad b_sqrt
.quad b_ceil
.quad b_floor
.quad b_fmod
.quad b_fabs
.quad b_abs
; cstring
.quad b_memcpy
.quad b_memmove
.quad b_strcpy
.quad b_strncpy
.quad b_strcat
.quad b_strncat
.quad b_memcmp
.quad b_strcmp
.quad b_strcoll
.quad b_strncmp
.quad b_strxfrm
.quad b_memchr
.quad b_strchr
.quad b_strcspn
.quad b_strpbrk
.quad b_strrchr
.quad b_strspn
.quad b_strstr
.quad b_strtok
.quad b_memset
.quad b_strerror
.quad b_strlen
; cstdlib
.quad b_atof
.quad b_atoi
.quad b_atol
.quad b_strtod
.quad b_strtol
.quad b_strtoul
.quad b_rand
.quad b_srand
.quad b_bsearch
.quad b_qsort
.section __text,__cstring,cstring_literals
remove: .asciz "remove"
rename: .asciz "rename"
tmpfile: .asciz "tmpfile"
tmpnam: .asciz "tmpnam"
fclose: .asciz "fclose"
fflush: .asciz "fflush"
fopen: .asciz "fopen"
freopen: .asciz "freopen"
setbuf: .asciz "setbuf"
setvbuf: .asciz "setvbuf"
fprintf: .asciz "fprintf"
fscanf: .asciz "fscanf"
printf: .asciz "printf"
scanf: .asciz "scanf"
sprintf: .asciz "sprintf"
sscanf: .asciz "sscanf"
vfprintf: .asciz "vfprintf"
vprintf: .asciz "vprintf"
vsprintf: .asciz "vsprintf"
fgetc: .asciz "fgetc"
fgets: .asciz "fgets"
fputc: .asciz "fputc"
fputs: .asciz "fputs"
getc: .asciz "getc"
getchar: .asciz "getchar"
gets: .asciz "gets"
putc: .asciz "putc"
putchar: .asciz "putchar"
puts: .asciz "puts"
ungetc: .asciz "ungetc"
fread: .asciz "fread"
fwrite: .asciz "fwrite"
fgetpos: .asciz "fgetpos"
fseek: .asciz "fseek"
fsetpos: .asciz "fsetpos"
ftell: .asciz "ftell"
rewind: .asciz "rewind"
clearerr: .asciz "clearerr"
feof: .asciz "feof"
ferror: .asciz "ferror"
perror: .asciz "perror"
; ctype
isalnum: .asciz "isalnum"
isalpha: .asciz "isalpha"
iscntrl: .asciz "iscntrl"
isdigit: .asciz "isdigit"
isgraph: .asciz "isgraph"
islower: .asciz "islower"
isprint: .asciz "isprint"
ispunct: .asciz "ispunct"
isspace: .asciz "isspace"
isupper: .asciz "isupper"
isxdigit: .asciz "isxdigit"
tolower: .asciz "tolower"
toupper: .asciz "tolower"
; cmath
cos: .asciz "cos"
sin: .asciz "sin"
tan: .asciz "tan"
acos: .asciz "acos"
asin: .asciz "asin"
atan: .asciz "atan"
atan2: .asciz "atan2"
cosh: .asciz "cosh"
sinh: .asciz "sinh"
tanh: .asciz "tanh"
exp: .asciz "exp"
frexp: .asciz "frexp"
ldexp: .asciz "ldexp"
log: .asciz "log"
log10: .asciz "log10"
modf: .asciz "modf"
pow: .asciz "pow"
sqrt: .asciz "sqrt"
ceil: .asciz "ceil"
floor: .asciz "floor"
fmod: .asciz "fmod"
fabs: .asciz "fabs"
abs: .asciz "abs"
; cstring
memcpy: .asciz "memcpy"
memmove: .asciz "memmove"
strcpy: .asciz "strcpy"
strncpy: .asciz "strncpy"
strcat: .asciz "strcat"
strncat: .asciz "strncat"
memcmp: .asciz "memcmp"
strcmp: .asciz "strcmp"
strcoll: .asciz "strcoll"
strncmp: .asciz "strncmp"
strxfrm: .asciz "strxfrm"
memchr: .asciz "memchr"
strchr: .asciz "strchr"
strcspn: .asciz "strcspn"
strpbrk: .asciz "strpbrk"
strrchr: .asciz "strrchr"
strspn: .asciz "strspn"
strstr: .asciz "strstr"
strtok: .asciz "strtok"
memset: .asciz "memset"
strerror: .asciz "strerror"
strlen: .asciz "strle"
; cstdlib
atof: .asciz "atof"
atoi: .asciz "atoi"
atol: .asciz "atol"
strtod: .asciz "strtod"
strtol: .asciz "strtol"
strtoul: .asciz "strtoul"
rand: .asciz "rand"
srand: .asciz "srand"
bsearch: .asciz "bsearch"
qsort: .asciz "qsort"
message: .asciz "printf called!!"
message1: .asciz "system call: comparing identifier %s and table entry %s\n"
debug_message: .asciz "expression evaluate returned on token %lu\n"