-
Notifications
You must be signed in to change notification settings - Fork 2
/
macros.S
1591 lines (1264 loc) · 16.6 KB
/
macros.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
# macros.S
# if registers and stacks
#
# data stack registers return stack
# d0 w r0
# d1 a r1
# d2 b r2
# x
# y
# dsp
# rsp
# wreg -> %eax # W register and also considered Top of Stack
# dsp -> %ebx # holds the Forth Parameter/Data stack pointer
# areg -> %esi # A register used for memory pointer operations - non-volatile: user must preserve
# breg -> %edi # B register used for general purpose operations - non-volatile: user must preserve
# xreg -> %ecx # X register used for general purpose operations
# yreg -> %edx # Y register used as a temp register
# sysvar -> %ebp # system variables base pointer
# header flags
.set HFLAGS, 0xFF
# low byte is word count : 0 - 255
# high byte is flags for word
# enable inlining of word
# bit 0 : enable inlinning
.set INLINE_OPT_FB, 0
.set INLINE_OPT, 1 << INLINE_OPT_FB
# bit 3 : disable interpret execution ie compile only
.set COMPILE_ONLY_FB, 3
# set immediate word as compile time only execution
# disable interpret execution : & with word count flag ie: 0xFF04 & COMPILE_ONLY
.set COMPILE_ONLY, 1 << COMPILE_ONLY_FB
# bit 7 : immediate execution when in compile state
# enable immediate execution : & with word count flag ie: 0xFF04 & IMMEDIATE_EN
.set IMMEDIATE_EN_FB, 7
.set IMMEDIATE_EN, 1 << IMMEDIATE_EN_FB
# forth flags
# bit 0 - Terminate Tasks
.set TERMINATE_F, 1
# bit 1 : call was last instruction
.set LAST_IS_CALL_FB, 1
# if bit 0 and 1 are cleared then last instruction was an inline or tail call optimization was disabled by word
# bit 2 : disable tail call optimization
# same bit in header flags
.set DIS_CALL_OPT_FB, 2
# bit 3,4 : word type
# 00 : standard
# 01 : litteral
# 10 : ALU
# 11 :
# bit 5, 6 ,7: register load
# 000 : WR
# 001 : Y
# 010 : X
# 011 : B
# 100 : A
# Inline macros
# push registers on return stack
.macro rpush_
pushl %eax
.endm
.macro rpusha_
pushl %esi
.endm
.macro rpushb_
pushl %edi
.endm
.macro rpushx_
pushl %ecx
.endm
.macro rpushy_
pushl %edx
.endm
.macro rpushdsp_
pushl %ebx
.endm
# pop value from return stack into register
.macro rpop_
popl %eax
.endm
.macro rpopa_
popl %esi
.endm
.macro rpopb_
popl %edi
.endm
.macro rpopx_
popl %ecx
.endm
.macro rpopy_
popl %edx
.endm
.macro rpopdsp_
popl %ebx
.endm
.macro rnip_
leal 4(%esp), %esp
.endm
.macro rnip2_
leal 8(%esp), %esp
.endm
.macro rnip3_
leal 12(%esp), %esp
.endm
# load value from return stack into register
# r0
.macro r0_ # load tos from stack
movl (%esp), %eax
.endm
# r1
.macro r1_ # load tos from stack
movl 4(%esp), %eax
.endm
# r2
.macro r2_ # load tos from stack
movl 8(%esp), %eax
.endm
# a=r0
.macro a_r0_
movl (%esp), %esi
.endm
# b=r0
.macro b_r0_
movl (%esp), %edi
.endm
# x=r0
.macro x_r0_
movl (%esp), %ecx
.endm
# y=r0
.macro y_r0_
movl (%esp), %edx
.endm
# a=r1
.macro a_r1_
movl 4(%esp), %esi
.endm
# b=r1
.macro b_r1_
movl 4(%esp), %edi
.endm
# x=r1
.macro x_r1_
movl 4(%esp), %ecx
.endm
# y=r1
.macro y_r1_
movl 4(%esp), %edx
.endm
# a=r2
.macro a_r2_
movl 8(%esp), %esi
.endm
# b=r2
.macro b_r2_
movl 8(%esp), %edi
.endm
# x=r2
.macro x_r2_
movl 8(%esp), %ecx
.endm
# y=r2
.macro y_r2_
movl 8(%esp), %edx
.endm
# store register value in return stack
# r0=w
.macro r0_w_
movl %eax, (%esp)
.endm
# r0=a
.macro r0_a_
movl %esi, (%esp)
.endm
# r0=b
.macro r0_b_
movl %edi, (%esp)
.endm
# r0=x
.macro r0_x_
movl %ecx, (%esp)
.endm
# r0=y
.macro r0_y_
movl %edx, (%esp)
.endm
# r1=w
.macro r1_w_
movl %eax, 4(%esp)
.endm
# r1=a
.macro r1_a_
movl %esi, 4(%esp)
.endm
# r1=b
.macro r1_b_
movl %edi, 4(%esp)
.endm
# r1=x
.macro r1_x_
movl %ecx, 4(%esp)
.endm
# r1=y
.macro r1_y_
movl %edx, 4(%esp)
.endm
# r2=w
.macro r2_w_
movl %eax, 8(%esp)
.endm
# r2=a
.macro r2_a_
movl %esi, 8(%esp)
.endm
# r2=b
.macro r2_b_
movl %edi, 8(%esp)
.endm
# r2=x
.macro r2_x_
movl %ecx, 8(%esp)
.endm
# r2=y
.macro r2_y_
movl %edx, 8(%esp)
.endm
# store return stack register in register
# rsp
.macro rsp_
movl %esp, %eax
.endm
# rsp=w
.macro rsp_w_
movl %eax, %esp
.endm
# move values between data stack locations and registers
# d0=w
.macro d0_w_
mov %eax, (%ebx)
.endm
# d0
.macro d0_ # load tos from stack
movl (%ebx), %eax
.endm
# d1=w
.macro d1_w_
mov %eax, 4(%ebx)
.endm
# d1
.macro d1_ # load tos from stack
movl 4(%ebx), %eax
.endm
# d1=w
.macro d2_w_
mov %eax, 8(%ebx)
.endm
# d2
.macro d2_ # load tos from stack
movl 8(%ebx), %eax
.endm
# d0=a
.macro d0_a_
mov %esi, (%ebx)
.endm
# d0=b
.macro d0_b_
mov %edi, (%ebx)
.endm
# d0=x
.macro d0_x_
mov %ecx, (%ebx)
.endm
# d0=y
.macro d0_y_
mov %edx, (%ebx)
.endm
# d1=a
.macro d1_a_
mov %esi, 4(%ebx)
.endm
# d1=b
.macro d1_b_
mov %edi, 4(%ebx)
.endm
# d1=x
.macro d1_x_
mov %ecx, 4(%ebx)
.endm
# d1=y
.macro d1_y_
mov %edx, 4(%ebx)
.endm
# d2=a
.macro d2_a_
mov %esi, 8(%ebx)
.endm
# d2=b
.macro d2_b_
mov %edi, 8(%ebx)
.endm
# d2=x
.macro d2_x_
mov %ecx, 8(%ebx)
.endm
# d2=y
.macro d2_y_
mov %edx, 8(%ebx)
.endm
# a=d0
.macro a_d0_
movl (%ebx), %esi
.endm
# a=d1
.macro a_d1_
movl 4(%ebx), %esi
.endm
# a=d2
.macro a_d2_
movl 8(%ebx), %esi
.endm
# b=d0
.macro b_d0_
movl (%ebx), %edi
.endm
# b=d1
.macro b_d1_
movl 4(%ebx), %edi
.endm
# b=d2
.macro b_d2_
movl 8(%ebx), %edi
.endm
# x=d0
.macro x_d0_
movl (%ebx), %ecx
.endm
# x=d1
.macro x_d1_
movl 4(%ebx), %ecx
.endm
# x=d2
.macro x_d2_
movl 8(%ebx), %ecx
.endm
# y=d0
.macro y_d0_
movl (%ebx), %edx
.endm
# y=d1
.macro y_d1_
movl 4(%ebx), %edx
.endm
# y=d2
.macro y_d2_
movl 8(%ebx), %edx
.endm
# make space on data stack
# put1
.macro put1_ # move stack pointer down one cell
leal -4(%ebx), %ebx
.endm
# put2
.macro put2_ # move stack pointer down two cells
leal -8(%ebx), %ebx
.endm
# put3
.macro put3_ # move stack pointer down three cells
leal -12(%ebx), %ebx
.endm
# push register on data stack
.macro push_ # save WR on data stack
put1_
d0_w_
.endm
# pusha
.macro pusha_ # save a on data stack
put1_
d0_a_
.endm
# pushb
.macro pushb_ # save b on data stack
put1_
d0_b_
.endm
# pushx
.macro pushx_ # save x on data stack
put1_
d0_x_
.endm
# pushy
.macro pushy_ # save y on data stack
put1_
d0_y_
.endm
.macro nip_
leal 4(%ebx), %ebx
.endm
.macro nip2_
leal 8(%ebx), %ebx
.endm
.macro nip3_
leal 12(%ebx), %ebx
.endm
# pop value from data stack into register
.macro pop_ # load WR from stack, increment stack pointer
d0_
nip_
.endm
.macro popa_
a_d0_
nip_
.endm
.macro popb_
b_d0_
nip_
.endm
.macro popx_
x_d0_
nip_
.endm
.macro popy_
y_d0_
nip_
.endm
# test with bit constant
.macro bittest_ v
test $\v, %eax
.endm
# dsp
.macro dsp_
movl %ebx, %eax
.endm
# dsp=w
.macro dsp_w_
movl %eax, %ebx
.endm
# w+=y
.macro w_plus_y_
addl %edx, %eax
.endm
# w+=x
.macro w_plus_x_
addl %ecx, %eax
.endm
# w+=a
.macro w_plus_a_
addl %esi, %eax
.endm
# w+=b
.macro w_plus_b_
addl %edi, %eax
.endm
# y+=w
.macro y_plus_w_
addl %eax, %edx
.endm
# y+=x
.macro y_plus_x_
addl %ecx, %edx
.endm
# y+=a
.macro y_plus_a_
addl %esi, %edx
.endm
# y+=b
.macro y_plus_b_
addl %edi, %edx
.endm
# x+=w
.macro x_plus_w_
addl %eax, %ecx
.endm
# x+=y
.macro x_plus_y_
addl %edx, %ecx
.endm
# x+=a
.macro x_plus_a_
addl %esi, %ecx
.endm
# x+=b
.macro x_plus_b_
addl %edi, %ecx
.endm
# a+=w
.macro a_plus_w_
addl %eax, %esi
.endm
# a+=b
.macro a_plus_b_
addl %edi, %esi
.endm
# a+=x
.macro a_plus_x_
addl %ecx, %esi
.endm
# a+=y
.macro a_plus_y_
addl %edx, %esi
.endm
# b+=w
.macro b_plus_w_
addl %eax, %edi
.endm
# b+=a
.macro b_plus_a_
addl %esi, %edi
.endm
# b+=x
.macro b_plus_x_
addl %ecx, %edi
.endm
# b+=y
.macro b_plus_y_
addl %edx, %edi
.endm
# w-=y
.macro w_minus_y_
sub %edx, %eax
.endm
# w-=x
.macro w_minus_x_
sub %ecx, %eax
.endm
# w-=a
.macro w_minus_a_
sub %esi, %eax
.endm
# w-=b
.macro w_minus_b_
sub %edi, %eax
.endm
# y-=w
.macro y_minus_w_
sub %eax, %edx
.endm
# y-=x
.macro y_minus_x_
sub %ecx, %edx
.endm
# y-=a
.macro y_minus_a_
sub %esi, %edx
.endm
# y-=b
.macro y_minus_b_
sub %edi, %edx
.endm
# x-=w
.macro x_minus_w_
sub %eax, %ecx
.endm
# x-=y
.macro x_minus_y_
sub %edx, %ecx
.endm
# x-=a
.macro x_minus_a_
sub %esi, %ecx
.endm
# x-=b
.macro x_minus_b_
sub %edi, %ecx
.endm
# a-=w
.macro a_minus_w_
sub %eax, %esi
.endm
# a-=b
.macro a_minus_b_
sub %edi, %esi
.endm
# a-=x
.macro a_minus_x_
sub %ecx, %esi
.endm
# a-=y
.macro a_minus_y_
sub %edx, %esi
.endm
# b-=w
.macro b_minus_w_
sub %eax, %edi
.endm
# b-=a
.macro b_minus_a_
sub %esi, %edi
.endm
# b-=x
.macro b_minus_x_
sub %ecx, %edi
.endm
# b-=y
.macro b_minus_y_
sub %edx, %edi
.endm
.macro neg_
neg %eax
.endm
.macro neg_y_
neg %edx
.endm
.macro neg_x_
neg %ecx
.endm
.macro neg_a_
neg %esi
.endm
.macro neg_b_
neg %edi
.endm
# w&=y
.macro w_and_y_
and %edx, %eax
.endm
# w&=x
.macro w_and_x_
and %ecx, %eax
.endm
# w&=a
.macro w_and_a_
and %esi, %eax
.endm
# w&=b
.macro w_and_b_
and %edi, %eax
.endm
# w|=y
.macro w_or_y_
or %edx, %eax
.endm
# w|=x
.macro w_or_x_
or %ecx, %eax
.endm
# w|=a
.macro w_or_a_
or %esi, %eax
.endm
# w|=b
.macro w_or_b_
or %edi, %eax
.endm
# w^=y
.macro w_xor_y_
xor %edx, %eax
.endm
# w^=x
.macro w_xor_x_
xor %ecx, %eax
.endm
# w^=a
.macro w_xor_a_
xor %esi, %eax
.endm
# w^=b
.macro w_xor_b_
xor %edi, %eax
.endm
# ~
.macro not_
not %eax
.endm
# !y
.macro not_y_
not %edx
.endm
# !x
.macro not_x_
not %ecx
.endm
# !a
.macro not_a_
not %esi
.endm
# !b
.macro not_b_
not %edi
.endm
.macro user_ sym
leal (\sym-sysvar_base)(%ebp), %eax
.endm
# load constant into register
.macro lit_ val
movl $\val, %eax
.endm
.macro lit_a_ val
movl $\val, %esi
.endm
.macro lit_b_ val
movl $\val, %edi
.endm
.macro lit_x_ val
movl $\val, %ecx
.endm
.macro lit_y_ val
movl $\val, %edx
.endm
# 1+
.macro oneplus_
inc %eax
.endm
# 1-
.macro oneminus_
dec %eax
.endm
# 2+
.macro twoplus_
add $2, %eax
.endm
# 4+
.macro fourplus_
add $4, %eax
.endm
# 2-
.macro twominus_
sub $2, %eax
.endm
# 4-
.macro fourminus_
sub $4, %eax
.endm
# 2/
.macro twoslash_
sar %eax
.endm
# 4/
.macro fourslash_
sar $2, %eax
.endm
# 2*
.macro twostar_
sal %eax
.endm
# 4*
.macro fourstar_
sal $2, %eax
.endm
# x/=2
.macro x_twoslash_
sar %ecx
.endm
# x/=4
.macro x_fourslash_
sar $2, %ecx
.endm
# x*=2
.macro x_twostar_
sal %ecx
.endm
# x*=4
.macro x_fourstar_
sal $2, %ecx
.endm
# y/=2
.macro y_twoslash_
sar %edx
.endm
# y/=4
.macro y_fourslash_
sar $2, %edx
.endm
# y*=2
.macro y_twostar_
sal %edx
.endm
# y*=4
.macro y_fourstar_
sal $2, %edx
.endm
# a/=2
.macro a_twoslash_
sar %esi
.endm
# a/=4
.macro a_fourslash_
sar $2, %esi
.endm
# a*=2
.macro a_twostar_
sal %esi
.endm
# a*=4
.macro a_fourstar_
sal $2, %esi
.endm
# b/=2
.macro b_twoslash_
sar %edi
.endm
# b/=4
.macro b_fourslash_
sar $2, %edi
.endm
# b*=2
.macro b_twostar_