forked from organix/pijFORTHos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jonesforth.s
1770 lines (1529 loc) · 57 KB
/
jonesforth.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
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@
@ pijFORTHos -- Raspberry Pi JonesFORTH Operating System
@
@ A bare-metal FORTH operating system for Raspberry Pi
@ Copyright (C) 2014 Dale Schumacher and Tristan Slominski
@
@ based on Jones' Forth port for ARM EABI
@ Copyright (C) 2013 M2IHP'13 class
@
@ Original x86 and FORTH code: Richard W.M. Jones <[email protected]>
@
@ See AUTHORS for the full list of contributors.
@
@ The extensive comments from Jones' x86 version have been removed. You should
@ check them out, they are really detailed, well written and pedagogical.
@ The original sources (with full comments) are in the /annexia/ directory.
@
@ DIVMOD routine taken from the ARM Software Development Toolkit User Guide 2.50
@
@ 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:
@ DSP (r13) points to the top of the data stack
@ RSP (r11) points to the top of the return stack
@ FIP (r10) points to the next FORTH word that will be executed
@ Note: r12 is often considered a "scratch" register
DSP .req r13
RSP .req r11
FIP .req r10
@ Define macros to push and pop from the data and return stacks
.macro PUSHRSP reg
str \reg, [RSP, #-4]!
.endm
.macro POPRSP reg
ldr \reg, [RSP], #4
.endm
.macro PUSHDSP reg
str \reg, [DSP, #-4]!
.endm
.macro POPDSP reg
ldr \reg, [DSP], #4
.endm
.macro PUSH2 reg
stmdb \reg!, {r0-r1} @ ( -- r1 r0 )
.endm
.macro POP2 reg
ldmia \reg!, {r0-r1} @ ( r1 r0 -- )
.endm
.macro PUSH3 reg
stmdb \reg!, {r0-r2} @ ( -- r2 r1 r0 )
.endm
.macro POP3 reg
ldmia \reg!, {r0-r2} @ ( r2 r1 r0 -- )
.endm
.macro PUSH4 reg
stmdb \reg!, {r0-r3} @ ( -- r3 r2 r1 r0 )
.endm
.macro POP4 reg
ldmia \reg!, {r0-r3} @ ( r3 r2 r1 r0 -- )
.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 r0, =var_S0
str DSP, [r0] @ Save the original stack position in S0
ldr RSP, =return_stack_top @ Set the initial return stack position
ldr r0, =data_segment @ Get the initial data segment address
ldr r1, =var_HERE @ Initialize HERE to point at
str r0, [r1] @ 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 r0
@ 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, r0, #4
@ _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 r0
_NEXT:
ldr r0, [FIP], #4
ldr r1, [r0]
bx r1
@ cold_start is used to bootstrap the interpreter,
@ the first word executed is QUIT
.section .rodata
cold_start:
.int 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, namelen, flags=0, label
.section .rodata
.align 2
.global name_\label
name_\label :
.int link @ link
.set link,name_\label
.byte \flags+\namelen @ flags + length byte
.ascii "\name" @ the name
.align 2 @ padding to next 4 byte boundary
.global \label
\label :
.int _DOCOL @ codeword - the interpreter
@ list of word pointers follow
.endm
@ defcode macro helps defining new native words in assembly
.macro defcode name, namelen, flags=0, label
.section .rodata
.align 2
.globl name_\label
name_\label :
.int link @ link
.set link,name_\label
.byte \flags+\namelen @ flags + length byte
.ascii "\name" @ the name
.align 2 @ padding to next 4 byte boundary
.global \label
\label :
.int 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",4,,EXIT
POPRSP FIP
NEXT
@ defvar macro helps defining FORTH variables in assembly
.macro defvar name, namelen, flags=0, label, initial=0
defcode \name,\namelen,\flags,\label
ldr r0, =var_\name
PUSHDSP r0
NEXT
.data
.align 2
.global var_\name
var_\name :
.int \initial
.endm
@ The built-in variables are:
@ STATE Is the interpreter executing code (0) or compiling a word (non-zero)?
defvar "STATE",5,,STATE
@ HERE Points to the next free byte of memory. When compiling, compiled words go here.
defvar "HERE",4,,HERE
@ LATEST Points to the latest (most recently defined) word in the dictionary.
defvar "LATEST",6,,LATEST,name_EXECUTE @ The last word defined in assembly is EXECUTE
@ S0 Stores the address of the top of the parameter stack.
defvar "S0",2,,S0
@ BASE The current base for printing and reading numbers.
defvar "BASE",4,,BASE,10
@ defconst macro helps defining FORTH constants in assembly
.macro defconst name, namelen, flags=0, label, value
defcode \name,\namelen,\flags,\label
ldr r0, =\value
PUSHDSP r0
NEXT
.endm
@ The built-in constants are:
@ VERSION Is the current version of this FORTH.
defconst "VERSION",7,,VERSION,JONES_VERSION
@ R0 The address of the top of the return stack.
defconst "R0",2,,R0,return_stack_top
@ DOCOL Pointer to _DOCOL.
defconst "DOCOL",5,,DOCOL,_DOCOL
@ PAD Pointer to scratch-pad buffer.
defconst "PAD",3,,PAD,scratch_pad
@ F_IMMED The IMMEDIATE flag's actual value.
defconst "F_IMMED",7,,F_IMMED,F_IMM
@ F_HIDDEN The HIDDEN flag's actual value.
defconst "F_HIDDEN",8,,F_HIDDEN,F_HID
@ F_LENMASK The length mask in the flags/len byte.
defconst "F_LENMASK",9,,F_LENMASK,F_LEN
@ FALSE Boolean predicate False (0)
defcode "FALSE",5,,FALSE
mov r0, #0
PUSHDSP r0
NEXT
@ TRUE Boolean predicate True (-1)
defcode "TRUE",4,,TRUE
mvn r0, #0
PUSHDSP r0
NEXT
@ DROP ( a -- ) drops the top element of the stack
defcode "DROP",4,,DROP
add DSP, DSP, #4 @ ( )
NEXT
@ DUP ( a -- a a ) duplicates the top element
defcode "DUP",3,,DUP
ldr r0, [DSP] @ ( a ), r0 = a
PUSHDSP r0 @ ( a a ), r0 = a
NEXT
@ SWAP ( a b -- b a ) swaps the two top elements
defcode "SWAP",4,,SWAP
POP2 DSP @ ( ), r1 = a, r0 = b
PUSHDSP r0 @ ( b ), r1 = a, r0 = b
PUSHDSP r1 @ ( b a ), r1 = a, r0 = b
NEXT
@ OVER ( a b -- a b a ) push copy of second element on top
defcode "OVER",4,,OVER
ldr r0, [DSP, #4] @ ( a b ), r0 = a
PUSHDSP r0 @ ( a b a )
NEXT
@ ROT ( a b c -- b c a ) rotation
defcode "ROT",3,,ROT
POPDSP r1 @ ( a b ), r1 = c
POPDSP r2 @ ( a ), r2 = b
POPDSP r0 @ ( ), r0 = a
PUSH3 DSP @ ( b c a ), r2 = b, r1 = c, r0 = a
NEXT
@ -ROT ( a b c -- c a b ) backwards rotation
defcode "-ROT",4,,NROT
POP3 DSP @ ( ), r2 = a, r1 = b, r0 = c
PUSHDSP r0 @ ( c )
PUSHDSP r2 @ ( c a )
PUSHDSP r1 @ ( c a b )
NEXT
@ 2DROP ( a b -- ) drops the top two elements of the stack
defcode "2DROP",5,,TWODROP
add DSP, DSP, #8 @ ( )
NEXT
@ 2DUP ( a b -- a b a b ) duplicate top two elements of stack
@ : 2DUP OVER OVER ;
defcode "2DUP",4,,TWODUP
ldmia DSP, {r0,r1} @ ( a b ), r1 = a, r0 = b
PUSH2 DSP @ ( a b a b ), r1 = a, r0 = 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",5,,TWOSWAP
POP4 DSP @ ( ), r3 = a, r2 = b, r1 = c, r0 = d
PUSH2 DSP @ ( c d ), r3 = a, r2 = b, r1 = c, r0 = d
PUSHDSP r3 @ ( c d a ), r3 = a, r2 = b, r1 = c, r0 = d
PUSHDSP r2 @ ( c d a b ), r3 = a, r2 = b, r1 = c, r0 = d
NEXT
@ 2OVER ( a b c d -- a b c d a b ) copy second pair of stack elements
defcode "2OVER",5,,TWOOVER
ldr r0, [DSP, #8] @ ( a b c d ), r0 = b
ldr r1, [DSP, #12] @ ( a b c d ), r1 = a, r0 = b
PUSH2 DSP @ ( a b c d a b ), r1 = a, r0 = b
NEXT
@ NIP ( a b -- b ) drop the second element of the stack
@ : NIP SWAP DROP ;
defcode "NIP",3,,NIP
POP2 DSP @ ( ), r1 = a, r0 = b
PUSHDSP r0 @ ( b ), r1 = a, r0 = b
NEXT
@ TUCK ( a b -- b a b ) push copy of top element below second
@ : TUCK SWAP OVER ;
defcode "TUCK",4,,TUCK
POP2 DSP @ ( ), r1 = a, r0 = b
PUSHDSP r0 @ ( b ), r1 = a, r0 = b
PUSH2 DSP @ ( b a b ), r1 = a, r0 = 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",4,,PICK
POPDSP r0 @ ( a_n ... a_0 ), r0 = n
ldr r1, [DSP,r0,LSL #2] @ ( a_n ... a_0 ), r0 = n, r1 = a_n
PUSHDSP r1 @ ( a_n ... a_0 a_n ), r0 = n, r1 = a_n
NEXT
@ ?DUP ( 0 -- 0 | a -- a a ) duplicates if non-zero
defcode "?DUP", 4,,QDUP
ldr r0, [DSP] @ r0 = a
cmp r0, #0 @ test if a==0
strne r0, [DSP, #-4]! @ copy if a!=0
NEXT @ ( a a | 0 )
@ : 1+ ( n -- n+1 ) 1 + ; \ increments the top element
defcode "1+",2,,INCR
POPDSP r0
add r0, r0, #1
PUSHDSP r0
NEXT
@ : 1- ( n -- n-1 ) 1 - ; \ decrements the top element
defcode "1-",2,,DECR
POPDSP r0
sub r0, r0, #1
PUSHDSP r0
NEXT
@ : 2+ ( n -- n+2 ) 2 + ; \ increments by 2 the top element
defcode "2+",2,,INCR2
POPDSP r0
add r0, r0, #2
PUSHDSP r0
NEXT
@ : 2- ( n -- n-2 ) 2 - ; \ decrements by 2 the top element
defcode "2-",2,,DECR2
POPDSP r0
sub r0, r0, #2
PUSHDSP r0
NEXT
@ : 4+ ( n -- n+4 ) 4 + ; \ increments by 4 the top element
defcode "4+",2,,INCR4
POPDSP r0
add r0, r0, #4
PUSHDSP r0
NEXT
@ : 4- ( n -- n-4 ) 4 - ; \ decrements by 4 the top element
defcode "4-",2,,DECR4
POPDSP r0
sub r0, r0, #4
PUSHDSP r0
NEXT
@ + ( a b -- a+b )
defcode "+",1,,ADD
POP2 DSP @ ( ), r1 = a, r0 = b
add r0, r0, r1
PUSHDSP r0
NEXT
@ - ( a b -- a-b )
defcode "-",1,,SUB
POP2 DSP @ ( ), r1 = a, r0 = b
sub r0, r1, r0
PUSHDSP r0
NEXT
@ 2* ( a -- a*2 )
defcode "2*",2,,MUL2
POPDSP r0
mov r0, r0, LSL #1
PUSHDSP r0
NEXT
@ 2/ ( a -- a/2 )
defcode "2/",2,,DIV2
POPDSP r0
mov r0, r0, ASR #1
PUSHDSP r0
NEXT
@ 4* ( a -- a*4 )
defcode "4*",2,,MUL4
POPDSP r0
mov r0, r0, LSL #2
PUSHDSP r0
NEXT
@ 4/ ( a -- a/4 )
defcode "4/",2,,DIV4
POPDSP r0
mov r0, r0, ASR #2
PUSHDSP r0
NEXT
@ LSHIFT ( a b -- a<<b )
defcode "LSHIFT",6,,LSHIFT
POP2 DSP @ ( ), r1 = a, r0 = b
mov r0, r1, LSL r0
PUSHDSP r0
NEXT
@ RSHIFT ( a b -- a>>b )
defcode "RSHIFT",6,,RSHIFT
POP2 DSP @ ( ), r1 = a, r0 = b
mov r0, r1, LSR r0
PUSHDSP r0
NEXT
@ * ( a b -- a*b )
defcode "*",1,,MUL
POP2 DSP @ ( ), r1 = a, r0 = b
mul r2, r1, r0
PUSHDSP r2
NEXT
@ / ( n m -- q ) integer division quotient (see /MOD)
@ : / /MOD SWAP DROP ;
defcode "/",1,,DIV
POPDSP r1 @ ( n ), r1 = m
POPDSP r0 @ ( ), r0 = n, r1 = m
bl _DIVMOD
PUSHDSP r2 @ ( q ), r0 = r, r1 = m, r2 = q
NEXT
@ MOD ( n m -- r ) integer division remainder (see /MOD)
@ : MOD /MOD DROP ;
defcode "MOD",3,,MOD
POPDSP r1 @ ( n ), r1 = m
POPDSP r0 @ ( ), r0 = n, r1 = m
bl _DIVMOD
PUSHDSP r0 @ ( r ), r0 = r, r1 = m, r2 = q
NEXT
@ NEGATE ( n -- -n ) integer negation
@ : NEGATE 0 SWAP - ;
defcode "NEGATE",6,,NEGATE
POPDSP r0
rsb r0, r0, #0
PUSHDSP r0
NEXT
@ = ( a b -- p ) where p is 1 when a and b are equal (0 otherwise)
defcode "=",1,,EQ
POP2 DSP @ ( ), r1 = a, r0 = b
cmp r1, r0
mvneq r0, #0
movne r0, #0
PUSHDSP r0
NEXT
@ <> ( a b -- p ) where p = a <> b
defcode "<>",2,,NEQ
POP2 DSP @ ( ), r1 = a, r0 = b
cmp r1, r0
mvnne r0, #0
moveq r0, #0
PUSHDSP r0
NEXT
@ < ( a b -- p ) where p = a < b
defcode "<",1,,LT
POP2 DSP @ ( ), r1 = a, r0 = b
cmp r1, r0
mvnlt r0, #0
movge r0, #0
PUSHDSP r0
NEXT
@ > ( a b -- p ) where p = a > b
defcode ">",1,,GT
POP2 DSP @ ( ), r1 = a, r0 = b
cmp r1, r0
mvngt r0, #0
movle r0, #0
PUSHDSP r0
NEXT
@ <= ( a b -- p ) where p = a <= b
defcode "<=",2,,LE
POP2 DSP @ ( ), r1 = a, r0 = b
cmp r1, r0
mvnle r0, #0
movgt r0, #0
PUSHDSP r0
NEXT
@ >= ( a b -- p ) where p = a >= b
defcode ">=",2,,GE
POP2 DSP @ ( ), r1 = a, r0 = b
cmp r1, r0
mvnge r0, #0
movlt r0, #0
PUSHDSP r0
NEXT
@ : 0= 0 = ;
defcode "0=",2,,ZEQ
POPDSP r1
mov r0, #0
cmp r1, r0
mvneq r0, #0
PUSHDSP r0
NEXT
@ : 0<> 0 <> ;
defcode "0<>",3,,ZNEQ
POPDSP r1
mov r0, #0
cmp r1, r0
mvnne r0, #0
PUSHDSP r0
NEXT
@ : 0< 0 < ;
defcode "0<",2,,ZLT
POPDSP r1
mov r0, #0
cmp r1, r0
mvnlt r0, #0
PUSHDSP r0
NEXT
@ : 0> 0 > ;
defcode "0>",2,,ZGT
POPDSP r1
mov r0, #0
cmp r1, r0
mvngt r0, #0
PUSHDSP r0
NEXT
@ : 0<= 0 <= ;
defcode "0<=",3,,ZLE
POPDSP r1
mov r0, #0
cmp r1, r0
mvnle r0, #0
PUSHDSP r0
NEXT
@ : 0>= 0 >= ;
defcode "0>=",3,,ZGE
POPDSP r1
mov r0, #0
cmp r1, r0
mvnge r0, #0
PUSHDSP r0
NEXT
@ : NOT 0= ;
defcode "NOT",3,,NOT
b code_ZEQ @ same at 0=
@ AND ( a b -- a&b ) bitwise and
defcode "AND",3,,AND
POP2 DSP @ ( ), r1 = a, r0 = b
and r0, r1, r0
PUSHDSP r0
NEXT
@ OR ( a b -- a|b ) bitwise or
defcode "OR",2,,OR
POP2 DSP @ ( ), r1 = a, r0 = b
orr r0, r1, r0
PUSHDSP r0
NEXT
@ XOR ( a b -- a^b ) bitwise xor
defcode "XOR",3,,XOR
POP2 DSP @ ( ), r1 = a, r0 = b
eor r0, r1, r0
PUSHDSP r0
NEXT
@ INVERT ( a -- ~a ) bitwise not
defcode "INVERT",6,,INVERT
POPDSP r0
mvn r0, r0
PUSHDSP r0
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", 3,, LIT
ldr r1, [FIP], #4
PUSHDSP r1
NEXT
@ ! ( value address -- ) write value at address
defcode "!",1,,STORE
POP2 DSP @ ( ), r1 = value, r0 = address
str r1, [r0]
NEXT
@ @ ( address -- value ) reads value from address
defcode "@",1,,FETCH
POPDSP r1
ldr r0, [r1]
PUSHDSP r0
NEXT
@ +! ( amount address -- ) add amount to value at address
defcode "+!",2,,ADDSTORE
POP2 DSP @ ( ), r1 = amount, r0 = address
ldr r2, [r0]
add r2, r1
str r2, [r0]
NEXT
@ -! ( amount address -- ) subtract amount to value at address
defcode "-!",2,,SUBSTORE
POP2 DSP @ ( ), r1 = amount, r0 = address
ldr r2, [r0]
sub r2, r1
str r2, [r0]
NEXT
@ C! ( c addr -- ) write byte c at addr
defcode "C!",2,,STOREBYTE
POP2 DSP @ ( ), r1 = c, r0 = addr
strb r1, [r0]
NEXT
@ C@ ( addr -- c ) read byte from addr
defcode "C@",2,,FETCHBYTE
POPDSP r1
ldrb r0, [r1]
PUSHDSP r0
NEXT
@ CMOVE ( source dest length -- ) copy length bytes from source to dest
defcode "CMOVE",5,,CMOVE
POP3 DSP @ ( ), r2 = source, r1 = dest, r0 = length
cmp r2, r1 @ account for potential overlap
bge 2f @ copy forward if s >= d, backward otherwise
sub r3, r0, #1 @ (length - 1)
add r2, r3 @ end of source
add r1, r3 @ end of dest
1:
cmp r0, #0 @ while length > 0
ble 3f
ldrb r3, [r2], #-1 @ read character from source
strb r3, [r1], #-1 @ and write it to dest (decrement both pointers)
sub r0, r0, #1 @ decrement length
b 1b
2:
cmp r0, #0 @ while length > 0
ble 3f
ldrb r3, [r2], #1 @ read character from source
strb r3, [r1], #1 @ and write it to dest (increment both pointers)
sub r0, r0, #1 @ decrement length
b 2b
3:
NEXT
@ COUNT ( addr -- addr+1 c ) extract first byte (len) of counted string
defcode "COUNT",5,,COUNT
POPDSP r0
ldrb r1, [r0], #1 @ get byte and increment pointer
PUSHDSP r0
PUSHDSP r1
NEXT
@ >R ( a -- ) move the top element from the data stack to the return stack
defcode ">R",2,,TOR
POPDSP r0
PUSHRSP r0
NEXT
@ R> ( -- a ) move the top element from the return stack to the data stack
defcode "R>",2,,FROMR
POPRSP r0
PUSHDSP r0
NEXT
@ RDROP drops the top element from the return stack
defcode "RDROP",5,,RDROP
add RSP,RSP,#4
NEXT
@ RSP@, RSP!, DSP@, DSP! manipulate the return and data stack pointers
defcode "RSP@",4,,RSPFETCH
PUSHDSP RSP
NEXT
defcode "RSP!",4,,RSPSTORE
POPDSP RSP
NEXT
defcode "DSP@",4,,DSPFETCH
mov r0, DSP
PUSHDSP r0
NEXT
defcode "DSP!",4,,DSPSTORE
POPDSP r0
mov DSP, r0
NEXT
@ KEY ( -- c ) Reads a character from stdin
defcode "KEY",3,,KEY
bl getchar @ r0 = getchar();
PUSHDSP r0 @ push the return value on the stack
NEXT
@ EMIT ( c -- ) Writes character c to stdout
defcode "EMIT",4,,EMIT
POPDSP r0
bl putchar @ putchar(r0);
NEXT
@ CR ( -- ) print newline
@ : CR '\n' EMIT ;
defcode "CR",2,,CR
mov r0, #10
bl putchar @ putchar('\n');
NEXT
@ SPACE ( -- ) print space
@ : SPACE BL EMIT ; \ print space
defcode "SPACE",5,,SPACE
mov r0, #32
bl putchar @ putchar(' ');
NEXT
@ WORD ( -- addr length ) reads next word from stdin
@ skips spaces, control-characters and comments, limited to 32 characters
defcode "WORD",4,,WORD
bl _WORD
PUSHDSP r0 @ address
PUSHDSP r1 @ length
NEXT
_WORD:
stmfd sp!, {r6,lr} @ preserve r6 and lr
1:
bl getchar @ read a character
cmp r0, #'\\'
beq 3f @ skip comments until end of line
cmp r0, #' '
ble 1b @ skip blank character
ldr r6, =word_buffer
2:
strb r0, [r6], #1 @ store character in word buffer
bl getchar @ read more characters until a space is found
cmp r0, #' '
bgt 2b
ldr r0, =word_buffer @ r0, address of word
sub r1, r6, r0 @ r1, length of word
ldmfd sp!, {r6,lr} @ restore r6 and lr
bx lr
3:
bl getchar @ skip all characters until end of line
cmp r0, #'\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",6,,NUMBER
POPDSP r1
POPDSP r0
bl _NUMBER
PUSHDSP r0
PUSHDSP r1
NEXT
_NUMBER:
stmfd sp!, {r4-r6, lr}
@ Save address of the string.
mov r2, r0
@ r0 will store the result after conversion.
mov r0, #0
@ Check if length is positive, otherwise this is an error.
cmp r1, #0
ble 5f
@ Load current base.
ldr r3, =var_BASE
ldr r3, [r3]
@ Load first character and increment pointer.
ldrb r4, [r2], #1
@ Check trailing '-'.
mov r5, #0
cmp r4, #45 @ 45 in '-' en ASCII
@ Number is positive.
bne 2f
@ Number is negative.
mov r5, #1
sub r1, r1, #1
@ Check if we have more than just '-' in the string.
cmp r1, #0
@ No, proceed with conversion.
bgt 1f
@ Error.
mov r1, #1
b 5f
1:
@ number *= BASE
@ Arithmetic shift right.
@ On ARM we need to use an additional register for MUL.
mul r6, r0, r3
mov r0, r6
@ Load the next character.
ldrb r4, [r2], #1
2:
@ Convert the character into a digit.
sub r4, r4, #48 @ r4 = r4 - '0'
cmp r4, #0
blt 4f @ End, < 0
cmp r4, #9
ble 3f @ chiffre compris entre 0 et 9
@ Test if hexadecimal character.
sub r4, r4, #17 @ 17 = 'A' - '0'
cmp r4, #0
blt 4f @ End, < 'A'
add r4, r4, #10
3:
@ Compare to the current base.
cmp r4, r3
bge 4f @ End, > BASE
@ Everything is fine.
@ Add the digit to the result.
add r0, r0, r4
sub r1, r1, #1
@ Continue processing while there are still characters to read.
cmp r1, #0
bgt 1b
4:
@ Negate result if we had a '-'.
cmp r5, #1
rsbeq r0, r0, #0
5:
@ Back to the caller.
ldmfd sp!, {r4-r6, pc}
@ FIND ( addr length -- dictionary_address )
@ Tries to find a word in the dictionary and returns its address.
@ If the word is not found, NULL is returned.
defcode "FIND",4,,FIND
POPDSP r1 @ length
POPDSP r0 @ addr
bl _FIND
PUSHDSP r0
NEXT
_FIND:
stmfd sp!, {r5,r6,r8,r9} @ save callee save registers
ldr r2, =var_LATEST
ldr r3, [r2] @ get the last defined word address
1:
cmp r3, #0 @ did we check all the words ?
beq 4f @ then exit
ldrb r2, [r3, #4] @ read the length field
and r2, r2, #(F_HID|F_LEN) @ keep only length + hidden bits
cmp r2, r1 @ do the lengths match ?
@ (note that if a word is hidden,
@ the test will be always negative)
bne 3f @ branch if they do not match
@ Now we compare strings characters
mov r5, r0 @ r5 contains searched string
mov r6, r3 @ r6 contains dict string
add r6, r6, #5 @ (we skip link and length fields)
@ r2 contains the length
2:
ldrb r8, [r5], #1 @ compare character per character
ldrb r9, [r6], #1
cmp r8,r9
bne 3f @ if they do not match, branch to 3
subs r2,r2,#1 @ decrement length
bne 2b @ loop
@ here, strings are equal
b 4f @ branch to 4
3:
ldr r3, [r3] @ Mismatch, follow link to the next
b 1b @ dictionary word
4:
mov r0, r3 @ move result to r0
ldmfd sp!, {r5,r6,r8,r9} @ restore callee save registers
bx lr
@ >CFA ( dictionary_address -- executable_address )
@ Transformat a dictionary address into a code field address
defcode ">CFA",4,,TCFA
POPDSP r0
bl _TCFA
PUSHDSP r0
NEXT
_TCFA:
add r0,r0,#4 @ skip link field
ldrb r1, [r0], #1 @ load and skip the length field
and r1,r1,#F_LEN @ keep only the length
add r0,r0,r1 @ skip the name field
add r0,r0,#3 @ find the next 4-byte boundary
and r0,r0,#~3
bx lr
@ >DFA ( dictionary_address -- data_field_address )
@ Return the address of the first data field
defcode ">DFA",4,,TDFA
POPDSP r0
bl _TCFA
add r0,r0,#4 @ DFA follows CFA
PUSHDSP r0
NEXT
@ CREATE ( address length -- ) Creates a new dictionary entry
@ in the data segment.
defcode "CREATE",6,,CREATE
POPDSP r1 @ length of the word to insert into the dictionnary
POPDSP r0 @ address of the word to insert into the dictionnary
ldr r2,=var_HERE
ldr r3,[r2] @ load into r3 and r8 the location of the header
mov r8,r3
ldr r4,=var_LATEST
ldr r5,[r4] @ load into r5 the link pointer
str r5,[r3] @ store link here -> last
add r3,r3,#4 @ skip link adress
strb r1,[r3] @ store the length of the word
add r3,r3,#1 @ skip the length adress
mov r7,#0 @ initialize the incrementation