forked from organix/pijFORTHos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjonesforth-aarch64.s
1889 lines (1638 loc) · 57.8 KB
/
jonesforth-aarch64.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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// jonesforth-aarch64.s - a port of Jonesforth to AArch64
//
// Based on jonesforth-arm.s. Just a dumb conversion today. We can do better.
//
// Copyright (C) 2015 Andrei Warkentin <[email protected]>
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
.set JONES_VERSION,47
//
// Reserve three special registers. These are reserved out of the
// callee-saved registers, making interop with C a bit easier.
// DSP (x19) points to the top of the data stack.
// RSP (x20) points to the top of the return stack
// FIP (x21) points to the next FORTH word that will be executed
//
DSP .req x19
RSP .req x20
FIP .req x21
lr .req x30
//
// Temporary scratch storage (also out of callee-saved regs,
// to make C interop a bit easer.
//
// SCRATCH0 (x22) is temporary storage
// SCRATCH1 (x23) is temporary storage
// SCRATCH2 (x24) is temporary storage
// SCRATCH3 (x25) is temporary storage
// SCRATCH4 (x26) is temporary storage
// SCRATCH5 (x27) is temporary storage
// SCRATCH6 (x28) is temporary storage
//
SCRATCH0 .req x22
SCRATCH1 .req x23
SCRATCH2 .req x24
SCRATCH3 .req x25
SCRATCH4 .req x26
SCRATCH5 .req x27
SCRATCH6 .req x28
// Define macros to push and pop from the data and return stacks
.macro PUSHRSP reg
str \reg, [RSP, #-8]!
.endm
.macro POPRSP reg
ldr \reg, [RSP], #8
.endm
.macro PUSHDSP reg
str \reg, [DSP, #-8]!
.endm
.macro POPDSP reg
ldr \reg, [DSP], #8
.endm
.macro PUSH2 reg // ( -- x1 x0 )
str x0, [\reg, #-8]!
str x1, [\reg, #-8]!
.endm
.macro POP2 reg // ( x1 x0 -- )
ldr x1, [\reg], #8
ldr x0, [\reg], #8
.endm
.macro PUSH3 reg // ( -- x2 x1 x0 )
str x0, [\reg, #-8]!
str x1, [\reg, #-8]!
str x2, [\reg, #-8]!
.endm
.macro POP3 reg // ( x2 x1 x0 -- )
ldr x2, [\reg], #8
ldr x1, [\reg], #8
ldr x0, [\reg], #8
.endm
.macro PUSH4 reg // ( -- x3 x2 x1 x0 )
str x0, [\reg, #-8]!
str x1, [\reg, #-8]!
str x2, [\reg, #-8]!
str x3, [\reg, #-8]!
.endm
.macro POP4 reg // ( x3 x2 x1 x0 -- )
ldr x3, [\reg], #8
ldr x2, [\reg], #8
ldr x1, [\reg], #8
ldr x0, [\reg], #8
.endm
.macro DSP_TO_SP_FOR_ABI_CALL
mov SCRATCH0, DSP
tst SCRATCH0, #0xF
beq 1f
sub SCRATCH0, SCRATCH0, #0x8
1: mov sp, SCRATCH0
.endm
// _NEXT is the assembly subroutine that is called
// at the end of every FORTH word execution.
// The NEXT macro is defined to simply call _NEXT
.macro NEXT
b _NEXT
.endm
// jonesforth is the entry point for the FORTH environment
.text
.align 2 // alignment 2^n (2^2 = 4 byte alignment)
.global jonesforth
jonesforth:
ldr x0, =var_S0
mov DSP, sp
str x1, [x0] // Save the original stack position in S0
ldr RSP, =return_stack_top // Set the initial return stack position
ldr x0, =data_segment // Get the initial data segment address
ldr x1, =var_HERE // Initialize HERE to point at
str x0, [x1] // the beginning of data segment
ldr FIP, =cold_start // Make the FIP point to cold_start
NEXT // Start the interpreter
// _DOCOL is the assembly subroutine that is called
// at the start of every FORTH word execution, which:
// 0. expects the CFA of a FORTH word in x0
// 1. saves the old FIP on the return stack
// 2. makes FIP point to the DFA (first codeword)
// 3. uses _NEXT to start interpreting the word
_DOCOL:
PUSHRSP FIP
add FIP, x0, #8
// _NEXT is the assembly subroutine that is called
// at the end of every FORTH word execution, which:
// 1. finds the CFA of the FORTH word to execute
// by dereferencing the FIP
// 2. increments FIP
// 3. begins executing the routine pointed to
// by the CFA, with the CFA in x0
_NEXT:
// This is done like so that ASMNEXT doesn't need to
// be kept in sync with _NEXT definition.
.macro NEXT_BODY, wrap_insn:vararg=
\wrap_insn ldr x0, [FIP], #8
\wrap_insn ldr x1, [x0]
\wrap_insn br x1
.endm
NEXT_BODY
// cold_start is used to bootstrap the interpreter,
// the first word executed is QUIT
.section .rodata
cold_start:
.quad QUIT
//// Now we define a set of helper macros that are syntactic sugar
//// to ease the declaration of FORTH words, Native words, FORTH variables
//// and FORTH constants.
// define the word flags
.set F_IMM, 0x80
.set F_HID, 0x20
.set F_LEN, 0x1f
// link is used to chain the words in the dictionary as they are defined
.set link, 0
// defword macro helps defining new FORTH words in assembly
.macro defword name, flags=0, label
.section .rodata
.align 3
.global name_\label
name_\label :
.quad link // link
.set link,name_\label
.byte \flags+(str_end_\label-str_\label) // flags + length of "\name"
str_\label :
.ascii "\name" // the name
str_end_\label :
.align 3 // padding to next 4 byte boundary
.global \label
\label :
.quad _DOCOL // codeword - the interpreter
// list of word pointers follow
.endm
// defcode macro helps defining new native words in assembly
.macro defcode name, flags=0, label
.section .rodata
.align 3
.globl name_\label
name_\label :
.quad link // link
.set link,name_\label
.byte \flags+(str_end_\label-str_\label) // flags + length of "\name"
str_\label :
.ascii "\name" // the name
str_end_\label :
.align 3 // padding to next 8 byte boundary
.global \label
\label :
.quad code_\label // codeword
.text
.global code_\label
code_\label : // assembler code follows
.endm
// EXIT is the last codeword of a FORTH word.
// It restores the FIP and returns to the caller using NEXT.
// (See _DOCOL)
defcode "EXIT",,EXIT
POPRSP FIP
NEXT
// defvar macro helps defining FORTH variables in assembly
.macro defvar name, flags=0, label, initial=0
defcode \name,\flags,\label
ldr x0, =var_\name
PUSHDSP x0
NEXT
.data
.align 3
.global var_\name
var_\name :
.quad \initial
.endm
// The built-in variables are:
// STATE Is the interpreter executing code (0) or compiling a word (non-zero)?
defvar "STATE",,STATE
// HERE Points to the next free byte of memory. When compiling, compiled words go here.
defvar "HERE",,HERE
// LATEST Points to the latest (most recently defined) word in the dictionary.
defvar "LATEST",,LATEST,name_EXECUTE // The last word defined in assembly is EXECUTE
// S0 Stores the address of the top of the parameter stack.
defvar "S0",,S0
// BASE The current base for printing and reading numbers.
defvar "BASE",,BASE,10
// defconst macro helps defining FORTH constants in assembly
.macro defconst name, flags=0, label, value
defcode \name,\flags,\label
ldr x0, =\value
PUSHDSP x0
NEXT
.endm
// The built-in constants are:
// VERSION Is the current version of this FORTH.
defconst "VERSION",,VERSION,JONES_VERSION
// R0 The address of the top of the return stack.
defconst "R0",,R0,return_stack_top
// DOCOL Pointer to _DOCOL.
defconst "DOCOL",,DOCOL,_DOCOL
// PAD Pointer to scratch-pad buffer.
defconst "PAD",,PAD,scratch_pad
// F_IMMED The IMMEDIATE flag's actual value.
defconst "F_IMMED",,F_IMMED,F_IMM
// F_HIDDEN The HIDDEN flag's actual value.
defconst "F_HIDDEN",,F_HIDDEN,F_HID
// F_LENMASK The length mask in the flags/len byte.
defconst "F_LENMASK",,F_LENMASK,F_LEN
// FALSE Boolean predicate False (0)
defcode "FALSE",,FALSE
mov x0, #0
PUSHDSP x0
NEXT
// TRUE Boolean predicate True (-1)
defcode "TRUE",,TRUE
mvn x0, xzr
PUSHDSP x0
NEXT
// DROP ( a -- ) drops the top element of the stack
defcode "DROP",,DROP
add DSP, DSP, #8 // ( )
NEXT
// DUP ( a -- a a ) duplicates the top element
defcode "DUP",,DUP
ldr x0, [DSP] // ( a ), x0 = a
PUSHDSP x0 // ( a a ), x0 = a
NEXT
// SWAP ( a b -- b a ) swaps the two top elements
defcode "SWAP",,SWAP
POP2 DSP // ( ), x1 = a, x0 = b
PUSHDSP x0 // ( b ), x1 = a, x0 = b
PUSHDSP x1 // ( b a ), x1 = a, x0 = b
NEXT
// OVER ( a b -- a b a ) push copy of second element on top
defcode "OVER",,OVER
ldr x0, [DSP, #8] // ( a b ), x0 = a
PUSHDSP x0 // ( a b a )
NEXT
// ROT ( a b c -- b c a ) rotation
defcode "ROT",,ROT
POPDSP x1 // ( a b ), x1 = c
POPDSP x2 // ( a ), x2 = b
POPDSP x0 // ( ), x0 = a
PUSH3 DSP // ( b c a ), x2 = b, x1 = c, x0 = a
NEXT
// -ROT ( a b c -- c a b ) backwards rotation
defcode "-ROT",,NROT
POP3 DSP // ( ), x2 = a, x1 = b, x0 = c
PUSHDSP x0 // ( c )
PUSHDSP x2 // ( c a )
PUSHDSP x1 // ( c a b )
NEXT
// 2DROP ( a b -- ) drops the top two elements of the stack
defcode "2DROP",,TWODROP
add DSP, DSP, #16 // ( )
NEXT
// 2DUP ( a b -- a b a b ) duplicate top two elements of stack
// : 2DUP OVER OVER ;
defcode "2DUP",,TWODUP
ldp x1, x0, [DSP] // ( a b ), x1 = a, x0 = b
PUSH2 DSP // ( a b a b ), x1 = a, x0 = b
NEXT
// 2SWAP ( a b c d -- c d a b ) swap top two pairs of elements of stack
// : 2SWAP >R -ROT R> -ROT ;
defcode "2SWAP",,TWOSWAP
POP4 DSP // ( ), x3 = a, x2 = b, x1 = c, x0 = d
PUSH2 DSP // ( c d ), x3 = a, x2 = b, x1 = c, x0 = d
PUSHDSP x3 // ( c d a ), x3 = a, x2 = b, x1 = c, x0 = d
PUSHDSP x2 // ( c d a b ), x3 = a, x2 = b, x1 = c, x0 = d
NEXT
// 2OVER ( a b c d -- a b c d a b ) copy second pair of stack elements
defcode "2OVER",,TWOOVER
ldr x0, [DSP, #16] // ( a b c d ), x0 = b
ldr x1, [DSP, #24] // ( a b c d ), x1 = a, x0 = b
PUSH2 DSP // ( a b c d a b ), x1 = a, x0 = b
NEXT
// NIP ( a b -- b ) drop the second element of the stack
// : NIP SWAP DROP ;
defcode "NIP",,NIP
POP2 DSP // ( ), x1 = a, x0 = b
PUSHDSP x0 // ( b ), x1 = a, x0 = b
NEXT
// TUCK ( a b -- b a b ) push copy of top element below second
// : TUCK SWAP OVER ;
defcode "TUCK",,TUCK
POP2 DSP // ( ), x1 = a, x0 = b
PUSHDSP x0 // ( b ), x1 = a, x0 = b
PUSH2 DSP // ( b a b ), x1 = a, x0 = b
NEXT
// PICK ( a_n ... a_0 n -- a_n ... a_0 a_n ) copy n-th stack item
// : PICK 1+ 4* DSP@ + @ ;
defcode "PICK",,PICK
POPDSP x0 // ( a_n ... a_0 ), x0 = n
ldr x1, [DSP,x0,LSL #3] // ( a_n ... a_0 ), x0 = n, x1 = a_n
PUSHDSP x1 // ( a_n ... a_0 a_n ), x0 = n, x1 = a_n
NEXT
// ?DUP ( 0 -- 0 | a -- a a ) duplicates if non-zero
defcode "?DUP",,QDUP
ldr x0, [DSP] // x0 = a
cbz x0, 1f
str x0, [DSP, #-8]! // copy if a!=0
1: NEXT // ( a a | 0 )
// : 1+ ( n -- n+1 ) 1 + ; \ increments the top element
defcode "1+",,INCR
POPDSP x0
add x0, x0, #1
PUSHDSP x0
NEXT
// : 1- ( n -- n-1 ) 1 - ; \ decrements the top element
defcode "1-",,DECR
POPDSP x0
sub x0, x0, #1
PUSHDSP x0
NEXT
// : 2+ ( n -- n+2 ) 2 + ; \ increments by 2 the top element
defcode "2+",,INCX2
POPDSP x0
add x0, x0, #2
PUSHDSP x0
NEXT
// : 2- ( n -- n-2 ) 2 - ; \ decrements by 2 the top element
defcode "2-",,DECX2
POPDSP x0
sub x0, x0, #2
PUSHDSP x0
NEXT
// : 4+ ( n -- n+4 ) 4 + ; \ increments by 4 the top element
defcode "4+",,INCX4
POPDSP x0
add x0, x0, #4
PUSHDSP x0
NEXT
// : 4- ( n -- n-4 ) 4 - ; \ decrements by 4 the top element
defcode "4-",,DECX4
POPDSP x0
sub x0, x0, #4
PUSHDSP x0
NEXT
// + ( a b -- a+b )
defcode "+",,ADD
POP2 DSP // ( ), x1 = a, x0 = b
add x0, x0, x1
PUSHDSP x0
NEXT
// - ( a b -- a-b )
defcode "-",,SUB
POP2 DSP // ( ), x1 = a, x0 = b
sub x0, x1, x0
PUSHDSP x0
NEXT
// 2* ( a -- a*2 )
defcode "2*",,MUL2
POPDSP x0
lsl x0, x0, #1
PUSHDSP x0
NEXT
// 2/ ( a -- a/2 )
defcode "2/",,DIV2
POPDSP x0
asr x0, x0, #1
PUSHDSP x0
NEXT
// 4* ( a -- a*4 )
defcode "4*",,MUL4
POPDSP x0
lsl x0, x0, #2
PUSHDSP x0
NEXT
// 4/ ( a -- a/4 )
defcode "4/",,DIV4
POPDSP x0
asr x0, x0, #2
PUSHDSP x0
NEXT
// 8* ( a -- a*8 )
defcode "4*",,MUL8
POPDSP x0
lsl x0, x0, #3
PUSHDSP x0
NEXT
// 8/ ( a -- a/8 )
defcode "4/",,DIV8
POPDSP x0
asr x0, x0, #3
PUSHDSP x0
NEXT
// LSHIFT ( a b -- a<<b )
defcode "LSHIFT",,LSHIFT
POP2 DSP // ( ), x1 = a, x0 = b
lsl x0, x1, x0
PUSHDSP x0
NEXT
// RSHIFT ( a b -- a>>b )
defcode "RSHIFT",,RSHIFT
POP2 DSP // ( ), x1 = a, x0 = b
lsr x0, x1, x0
PUSHDSP x0
NEXT
// * ( a b -- a*b )
defcode "*",,MUL
POP2 DSP // ( ), x1 = a, x0 = b
mul x2, x1, x0
PUSHDSP x2
NEXT
// / ( n m -- q ) integer division quotient (see /MOD)
// : / /MOD SWAP DROP ;
defcode "/",,DIV
POPDSP x1 // ( n ), x1 = m
POPDSP x0 // ( ), x0 = n, x1 = m
udiv x2, x0, x1
PUSHDSP x2 // ( q ), x0 = r, x1 = m, x2 = q
NEXT
// MOD ( n m -- r ) integer division remainder (see /MOD)
// : MOD /MOD DROP ;
defcode "MOD",,MOD
POPDSP x1 // ( n ), x1 = m
POPDSP x0 // ( ), x0 = n, x1 = m
udiv x2, x0, x1
mul x1, x2, x1
sub x0, x0, x1
PUSHDSP x0 // ( r ), x0 = r, x1 = m, x2 = q
NEXT
// NEGATE ( n -- -n ) integer negation
// : NEGATE 0 SWAP - ;
defcode "NEGATE",,NEGATE
POPDSP x0
neg x0, x0
PUSHDSP x0
NEXT
// = ( a b -- p ) where p is 1 when a and b are equal (0 otherwise)
defcode "=",,EQ
POP2 DSP // ( ), x1 = a, x0 = b
cmp x1, x0
bne 1f
mov x0, #-1
b 2f
1: mov x0, #0
2: PUSHDSP x0
NEXT
// <> ( a b -- p ) where p = a <> b
defcode "<>",,NEQ
POP2 DSP // ( ), x1 = a, x0 = b
cmp x1, x0
beq 1f
mov x0, #-1
b 2f
1: mov x0, 0
2: PUSHDSP x0
NEXT
// < ( a b -- p ) where p = a < b
defcode "<",,LT
POP2 DSP // ( ), x1 = a, x0 = b
cmp x1, x0
bge 1f
mov x0, #-1
b 2f
1: mov x0, #0
2: PUSHDSP x0
NEXT
// > ( a b -- p ) where p = a > b
defcode ">",,GT
POP2 DSP // ( ), x1 = a, x0 = b
cmp x1, x0
ble 1f
mov x0, #-1
b 2f
1: mov x0, #0
2: PUSHDSP x0
NEXT
// <= ( a b -- p ) where p = a <= b
defcode "<=",,LE
POP2 DSP // ( ), x1 = a, x0 = b
cmp x1, x0
bgt 1f
mov x0, #-1
b 2f
1: mov x0, #0
2: PUSHDSP x0
NEXT
// >= ( a b -- p ) where p = a >= b
defcode ">=",,GE
POP2 DSP // ( ), x1 = a, x0 = b
cmp x1, x0
blt 1f
mov x0, #-1
b 2f
1: mov x0, #0
2: PUSHDSP x0
NEXT
// : 0= 0 = ;
defcode "0=",,ZEQ
POPDSP x0
cmp x0, xzr
bne 1f
mov x0, #-1
b 2f
1: mov x0, #0
2: PUSHDSP x0
NEXT
// : 0<> 0 <> ;
defcode "0<>",,ZNEQ
POPDSP x0
cmp x0, xzr
beq 1f
mov x0, #-1
b 2f
1: mov x0, #0
2: PUSHDSP x0
NEXT
// : 0< 0 < ;
defcode "0<",,ZLT
POPDSP x0
cmp x0, xzr
bge 1f
mov x0, #-1
b 2f
1: mov x0, #0
2: PUSHDSP x0
NEXT
// : 0> 0 > ;
defcode "0>",,ZGT
POPDSP x0
cmp x0, xzr
ble 1f
mov x0, #-1
b 2f
1: mov x0, #0
2: PUSHDSP x0
NEXT
// : 0<= 0 <= ;
defcode "0<=",,ZLE
POPDSP x0
cmp x0, xzr
bgt 1f
mov x0, #-1
b 2f
1: mov x0, #0
2: PUSHDSP x0
NEXT
// : 0>= 0 >= ;
defcode "0>=",,ZGE
POPDSP x0
cmp x0, xzr
blt 1f
mov x0, #-1
b 2f
1: mov x0, #0
2: PUSHDSP x0
NEXT
// : NOT 0= ;
defcode "NOT",,NOT
b code_ZEQ // same at 0=
// AND ( a b -- a&b ) bitwise and
defcode "AND",,AND
POP2 DSP // ( ), x1 = a, x0 = b
and x0, x1, x0
PUSHDSP x0
NEXT
// OR ( a b -- a|b ) bitwise or
defcode "OR",,OR
POP2 DSP // ( ), x1 = a, x0 = b
orr x0, x1, x0
PUSHDSP x0
NEXT
// XOR ( a b -- a^b ) bitwise xor
defcode "XOR",,XOR
POP2 DSP // ( ), x1 = a, x0 = b
eor x0, x1, x0
PUSHDSP x0
NEXT
// INVERT ( a -- ~a ) bitwise not
defcode "INVERT",,INVERT
POPDSP x0
mvn x0, x0
PUSHDSP x0
NEXT
// LIT is used to compile literals in FORTH word.
// When LIT is executed it pushes the literal (which is the next codeword)
// into the stack and skips it (since the literal is not executable).
defcode "LIT",, LIT
ldr x1, [FIP], #8
PUSHDSP x1
NEXT
// ! ( value address -- ) write value at address
defcode "!",,STORE
POP2 DSP // ( ), x1 = value, x0 = address
str x1, [x0]
NEXT
// // ( address -- value ) reads value from address
defcode "//",,FETCH
POPDSP x1
ldr x0, [x1]
PUSHDSP x0
NEXT
// +! ( amount address -- ) add amount to value at address
defcode "+!",,ADDSTORE
POP2 DSP // ( ), x1 = amount, x0 = address
ldr x2, [x0]
add x2, x2, x1
str x2, [x0]
NEXT
// -! ( amount address -- ) subtract amount to value at address
defcode "-!",,SUBSTORE
POP2 DSP // ( ), x1 = amount, x0 = address
ldr x2, [x0]
sub x2, x2, x1
str x2, [x0]
NEXT
// C! ( c addr -- ) write byte c at addr
defcode "C!",,STOREBYTE
POP2 DSP // ( ), x1 = c, x0 = addr
strb w1, [x0]
NEXT
// C// ( addr -- c ) read byte from addr
defcode "C//",,FETCHBYTE
POPDSP x1
ldrb w0, [x1]
PUSHDSP w0
NEXT
// CMOVE ( source dest length -- ) copy length bytes from source to dest
defcode "CMOVE",,CMOVE
POP3 DSP // ( ), x2 = source, x1 = dest, x0 = length
cmp x2, x1 // account for potential overlap
bge 2f // copy forward if s >= d, backward otherwise
sub x3, x0, #1 // (length - 1)
add x2, x2, x3 // end of source
add x1, x1, x3 // end of dest
1:
cmp x0, #0 // while length > 0
ble 3f
ldrb w3, [x2], #-1 // read character from source
strb w3, [x1], #-1 // and write it to dest (decrement both pointers)
sub x0, x0, #1 // decrement length
b 1b
2:
cmp x0, #0 // while length > 0
ble 3f
ldrb w3, [x2], #1 // read character from source
strb w3, [x1], #1 // and write it to dest (increment both pointers)
sub x0, x0, #1 // decrement length
b 2b
3:
NEXT
// COUNT ( addr -- addr+1 c ) extract first byte (len) of counted string
defcode "COUNT",,COUNT
POPDSP x0
ldrb w1, [x0], #1 // get byte and increment pointer
PUSHDSP x0
PUSHDSP x1
NEXT
// >R ( a -- ) move the top element from the data stack to the return stack
defcode ">R",,TOR
POPDSP x0
PUSHRSP x0
NEXT
// R> ( -- a ) move the top element from the return stack to the data stack
defcode "R>",,FROMR
POPRSP x0
PUSHDSP x0
NEXT
// RDROP drops the top element from the return stack
defcode "RDROP",,RDROP
add RSP,RSP,#8
NEXT
// RSP//, RSP!, DSP//, DSP! manipulate the return and data stack pointers
defcode "RSP//",,RSPFETCH
PUSHDSP RSP
NEXT
defcode "RSP!",,RSPSTORE
POPDSP RSP
NEXT
defcode "DSP//",,DSPFETCH
mov x0, DSP
PUSHDSP x0
NEXT
defcode "DSP!",,DSPSTORE
POPDSP x0
mov DSP, x0
NEXT
// KEY ( -- c ) Reads a character from stdin
defcode "KEY",,KEY
DSP_TO_SP_FOR_ABI_CALL
bl getchar // x0 = getchar();
PUSHDSP x0 // push the return value on the stack
NEXT
// EMIT ( c -- ) Writes character c to stdout
defcode "EMIT",,EMIT
POPDSP x0
DSP_TO_SP_FOR_ABI_CALL
bl putchar // putchar(x0);
NEXT
// CR ( -- ) print newline
// : CR '\n' EMIT ;
defcode "CR",,CR
mov x0, #10
DSP_TO_SP_FOR_ABI_CALL
bl putchar // putchar('\n');
NEXT
// SPACE ( -- ) print space
// : SPACE BL EMIT ; \ print space
defcode "SPACE",,SPACE
mov x0, #32
DSP_TO_SP_FOR_ABI_CALL
bl putchar // putchar(' ');
NEXT
// WORD ( -- addr length ) reads next word from stdin
// skips spaces, control-characters and comments, limited to 32 characters
defcode "WORD",,WORD
bl _WORD
PUSHDSP x0 // address
PUSHDSP x1 // length
NEXT
_WORD:
PUSHDSP x6
PUSHDSP lr
DSP_TO_SP_FOR_ABI_CALL
1:
bl getchar // read a character
cmp x0, #'\\'
beq 3f // skip comments until end of line
cmp x0, #' '
ble 1b // skip blank character
ldr x6, =word_buffer
2:
strb w0, [x6], #1 // store character in word buffer
bl getchar // read more characters until a space is found
cmp x0, #' '
bgt 2b
ldr x0, =word_buffer // x0, address of word
sub x1, x6, x0 // x1, length of word
POPDSP lr
POPDSP x6
ret
3:
bl getchar // skip all characters until end of line
cmp x0, #'\n'
bne 3b
b 1b
// word_buffer for WORD
.data
.align 5 // align to cache-line size
word_buffer:
.space 32 // FIXME: what about overflow!?
// NUMBER ( addr length -- n e ) converts string to number
// n is the parsed number
// e is the number of unparsed characters
defcode "NUMBER",,NUMBER
POPDSP x1
POPDSP x0
bl _NUMBER
PUSHDSP x0
PUSHDSP x1
NEXT
_NUMBER:
PUSHDSP x4
PUSHDSP x5
PUSHDSP x6
PUSHDSP lr
// Save address of the string.
mov x2, x0
// x0 will store the result after conversion.
mov x0, #0
// Check if length is positive, otherwise this is an error.
cmp x1, #0
ble 5f
// Load current base.
ldr x3, =var_BASE
ldr x3, [x3]
// Load first character and increment pointer.
ldrb w4, [x2], #1
// Check trailing '-'.
mov x5, #0
cmp x4, #45 // 45 in '-' en ASCII
// Number is positive.
bne 2f
// Number is negative.
mov x5, #1
sub x1, x1, #1
// Check if we have more than just '-' in the string.
cmp x1, #0
// No, proceed with conversion.
bgt 1f
// Error.
mov x1, #1
b 5f
1:
// number *= BASE
// Arithmetic shift right.
// On ARM we need to use an additional register for MUL.
mul x6, x0, x3
mov x0, x6
// Load the next character.
ldrb w4, [x2], #1
2:
// Convert the character into a digit.
sub x4, x4, #48 // x4 = x4 - '0'
cmp x4, #0
blt 4f // End, < 0
cmp x4, #9
ble 3f // chiffre compris entre 0 et 9
// Test if hexadecimal character.
sub x4, x4, #17 // 17 = 'A' - '0'
cmp x4, #0
blt 4f // End, < 'A'
add x4, x4, #10
3:
// Compare to the current base.
cmp x4, x3
bge 4f // End, > BASE
// Everything is fine.
// Add the digit to the result.
add x0, x0, x4
sub x1, x1, #1
// Continue processing while there are still characters to read.
cmp x1, #0
bgt 1b
4:
// Negate result if we had a '-'.
cmp x5, #1
bne 5f
sub x0, xzr, x0
5:
// Back to the caller.
POPDSP lr
POPDSP x6
POPDSP x5
POPDSP x4
ret
// FIND ( addr length -- dictionary_address )