-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathkerncode.fth
2592 lines (2312 loc) · 63.8 KB
/
kerncode.fth
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
purpose: Kernel Primitives for ARM64 Processors
\ See license at end of file
\ Allocate and clear the initial user area image
mlabel init-user-area setup-user-area
\ We create the shared code for the "next" routine so that:
\ a) It will be in RAM for speed (ROM is often slow)
\ b) We can use the user pointer as its base address, for quick jumping
also forth
compilation-base here-t \ Save meta dictionary pointer
\ Use the first version if the user area is separate from the dictionary
\ 0 dp-t ! userarea-t is compilation-base \ Point it to user area
userarea-t dp-t ! \ Point it to user area
previous
code-field: (next) \ Shared code for next; will be copied into user area
[ifdef] trace-next
mov x1,up
ldr x0,[x1,#-8]!
cmp x0,up
b.ne $20 \ L# do-next
psh lr,sp
ldr x0,[x1,#-8]!
blr x0
pop lr,sp
[then]
[ifdef] count-nexts
ldr x0,[org,#0x80]
inc x0,#1
str x0,[org,#0x80]
[then]
[ifdef] check-stack-ordering
\ Test for checking the relative ordering of rp, sp and the xsp
mov x0, rp
mov x1, sp
mov x2, xsp
cmp x0, x1
0< if
begin again
then
cmp x1, x2
0< if
begin again
then
cmp x0, x2
0< if
begin again
then
[then] \ check-stack-ordering
ldr w0,[ip],#/token \ Fetch next token and update IP
\+ itc add lr,x0,org \ token + origin = cfa
\+ itc ldr w0,[lr],#/token \ x0 = token of runtime word, lr = pfa
add x0,x0,org \ token + origin = cfa
br x0 \ executable code
end-code
also forth
dp-t ! is compilation-base previous \ Restore meta dict. pointer
d# 64 equ #user-init \ Leaves space for the shared "next"
hex meta assembler definitions
\ New: the following 3 definitions aren't in this file
:-h next " br up" evaluate ;-h
:-h c; next end-code ;-h
caps on
\ Run-time actions for defining words:
\ In the ARM64 implementation all words but code definitions are
\ called by a branch+link instruction. It branches to a relative-inline-
\ address and leaves the pfa in the link register, X30.
\ The pfa of the word is just after the branch+link instruction.
\ NOTE: Cell size and token size need not be the same (e.g., a 64 bit
\ cell size and a 32 bit token size). And therefore, colon definitions
\ don't necessary need to align the pointer to the token stream before
\ using them. However, variables and other data stores must be cell size
\ aligned. This is true for two reasons: 1) locks require exclusive
\ operations and those instructions cannot work on unaligned data;
\ 2) support for unaligned accesses may be disabled when Forth is
\ running and would therefore cause exceptions.
meta definitions
code-field: douser
push tos,sp
ldr w0,[lr]
add tos,x0,up
c;
code-field: dodoes
push tos,sp
mov tos,x0 \ x0 has pfa of child
push ip,rp
mov ip,lr \ ip has pfa of parent
c;
code-field: dovalue
push tos,sp
ldr w0,[lr]
ldr tos,[up,x0]
c;
code-field: docolon
push ip,rp
mov ip,lr
c;
code-field: doconstant
push tos,sp
ldr tos,[lr]
c;
code-field: dodefer
ldr w0,[lr] \ Get user#
ldr w0,[x0,up] \ Fetch target token
\+ itc add lr,x0,org \ CFA of target
\+ itc ldr w0,[lr],#/token \ Fetch do-word token; LR = PFA
add x0,x0,org
br x0
end-code
code-field: do2constant
ldp x1,x2,[lr]
push2 x1,tos,sp
mov tos, x2
c;
code-field: dolabel \ tag for subroutines
push tos,sp
mov tos,lr
c;
code-field: docreate
push tos,sp
mov tos,lr
c;
code-field: dovariable
push tos,sp
mov tos,lr
c;
\ New: dopointer (identical to doconstant)
\ New: dobuffer (identical to doconstant)
:-h push-pfa ( -- ) also assembler " mov x0,lr" evaluate previous ;-h
[ifdef] itc
:-h compvoc compile-t <vocabulary> ;-h
\ for itc move lr to x0 for use by dodoes
code-field: dovocabulary
mov x0,lr
ldr w1,$+12 \ Snag compvoc after the end of this word
add x1,x1,org \ CFA of target
br x1
end-code
compvoc \ later we search for a word named <vocabulary> and compile its token here
\ compvoc points to
\ mov x0,lr
\ bl dodoes
\ which will call dodoes with lr: pfa of compvoc, and x0: pfa of wordlist
\ Meta compiler words to compile code fields for child words
:-h place-cf-t \ ( adr -- ) really token!
origin - l,-t
;-h
\ XXX This will need to change when code words are placed in a code segment.
:-h code-cf ( -- ) here-t /token 2* + place-cf-t
also assembler " add xsp,xsp,#0" evaluate previous ;-h
:-h start;code ( -- ) ;-h \ ???
:-h colon-cf ( -- ) docolon place-cf-t ;-h
:-h constant-cf ( -- ) doconstant place-cf-t ;-h
:-h label-cf ( -- ) dolabel place-cf-t ;-h
:-h create-cf ( -- ) docreate place-cf-t ;-h
:-h variable-cf ( -- ) dovariable place-cf-t ;-h
:-h user-cf ( -- ) douser place-cf-t ;-h
:-h value-cf ( -- ) dovalue place-cf-t ;-h
:-h defer-cf ( -- ) dodefer place-cf-t ;-h
:-h 2constant-cf ( -- ) do2constant place-cf-t ;-h
:-h vocabulary-cf ( -- ) dovocabulary place-cf-t ;-h
:-h place-does-t \ ( adr -- ) compile a branch+link to adr
here-t - 2/ 2/ h# 03ff.ffff and h# 9400.0000 or l,-t
;-h
:-h startdoes ( -- ) push-pfa dodoes place-does-t ;-h
[else] \ not itc
:-h compvoc compile-t <vocabulary> ;-h
code-field: dovocabulary
ldr w0,$+12 \ Snag compvoc after the end of this word
add x0,x0,org
br x0
end-code
compvoc \ later we search for a word named <vocabulary> and compile its token here
\ Meta compiler words to compile code fields for child words
:-h place-cf-t \ ( adr -- ) compile a branch+link to adr
here-t - 2/ 2/ h# 03ff.ffff and h# 9400.0000 or l,-t
;-h
:-h code-cf ( -- ) also assembler " add xsp,xsp,#0" evaluate previous ;-h
:-h startdoes ( -- ) push-pfa
dodoes place-cf-t ;-h
:-h start;code ( -- ) ;-h \ ???
:-h colon-cf ( -- ) docolon place-cf-t ;-h
:-h constant-cf ( -- ) doconstant place-cf-t ;-h
\ New: :-h buffer-cf ( -- ) dobuffer place-cf-t ;-h
\ New: :-h pointer-cf ( -- ) dopointer place-cf-t ;-h
:-h label-cf ( -- ) dolabel place-cf-t ;-h
:-h create-cf ( -- ) docreate place-cf-t ;-h
:-h variable-cf ( -- ) dovariable place-cf-t ;-h
:-h user-cf ( -- ) douser place-cf-t ;-h
:-h value-cf ( -- ) dovalue place-cf-t ;-h
:-h defer-cf ( -- ) dodefer place-cf-t ;-h
:-h 2constant-cf ( -- ) do2constant place-cf-t ;-h
:-h vocabulary-cf ( -- ) dovocabulary place-cf-t ;-h
[then] \ itc
\ Start adding named words to the target dictionary.
\ =======================================
\
\ IS___ Assignment Words
\ 'is' will compile one of these run-time words
\ XXX the metacompiler verson of 'is' compiles the wrong thing.
\ It reolves at run time, too slow!
\
\ =======================================
code isdefer ( xt -- )
ldr w0,[ip],#/token \ Get token of target word
add x0,x0,org
ldr w0,[x0,#/token] \ Get user number
sub x1,tos,org \ make it a token
str w1,[x0,up] \ Store 32-bit value
pop tos,sp \ Fix stack
c;
code isvalue ( n -- )
ldr w0,[ip],#/token \ Get token of target word
add x0,x0,org
ldr w0,[x0,#/token] \ Get user number
str tos,[x0,up] \ Store value
pop tos,sp \ Fix stack
c;
code isuser ( n -- )
ldr w0,[ip],#/token \ Get token of target word
add x0,x0,org
ldr w0,[x0,#/token] \ Get user number
str tos,[x0,up] \ Store value
pop tos,sp \ Fix stack
c;
code isconstant ( n -- )
ldr w0,[ip],#/token \ Get token of target word
add x0,x0,org
inc x0,#4 \ Advance to pfa
str tos,[x0] \ Store value
pop tos,sp \ Fix stack
c;
code isvariable ( n -- )
ldr w0,[ip],#/token \ Get token of target word
add x0,x0,org
inc x0,#4 \ Advance to pfa
str tos,[x0] \ Store value
pop tos,sp \ Fix stack
c;
\ =======================================
\
\ IMMEDIATE VALUES
\
\ =======================================
\ 32-bit literal
\ value was incremented when stored;
\ decrement to provide a range of small positive integers, 0, and -1
code (llit) ( -- lit )
psh tos,sp
ldr wtos,[ip],#4
dec tos,#1
c;
\ a cell-sized literal in a stream of tokens may not be aligned
\ so fetch it as two longs
code (lit) ( -- lit )
push tos,sp
ldr w0,[ip],#4
ldr w1,[ip],#4
orr tos,x0,x1,lsl #32
c;
\ and a double takes four longs
code (dlit) ( -- d )
push tos,sp
ldr w0,[ip],#4
ldr w1,[ip],#4
orr tos,x0,x1,lsl #32
push tos,sp
ldr w0,[ip],#4
ldr w1,[ip],#4
orr tos,x0,x1,lsl #32
c;
\ push a pair of 32-bit in-line values
code (1x0) ( -- mask value )
push tos,sp
ldr w0,[ip],#4
push x0,sp
ldr wtos,[ip],#4
c;
\ =======================================
\
\ EXECUTE
\
\ =======================================
\ branch to a code field
code execute ( cfa -- )
\- itc mov x0,tos
\+ itc mov lr,tos
pop tos,sp
\+ itc ldr w0,[lr],#/token
\+ itc add x0,x0,org
br x0
end-code
\ execute unless 0
code ?execute ( cfa|0 -- )
\- itc ands x0,tos,tos \ tos: len
\+ itc ands lr,tos,tos
pop tos,sp
0<> if
\+ itc ldr w0,[lr],#/token
\+ itc add x0,x0,org
br x0
then
c;
\ fetch-execute
code @execute ( adr -- )
\- itc ldr x0,[tos]
\+ itc ldr lr,[tos]
pop tos,sp
\+ itc ldr w0,[lr],#/token
\+ itc add x0,x0,org
br x0
end-code
\ execute-ip This word will call a block of Forth words given the address
\ of the first word. It's used, for example, in try blocks where the
\ a word calls 'try' and then the words that follow it are called repeatedly.
\ This word, execute-ip, is used to transfer control back to the caller of
\ try and execute the words that follow the call to try.
\ see forth/lib/try.fth for more details.
code execute-ip ( word-list-ip -- )
push ip,rp \ nest
mov ip,tos \ interpret list of tokens, until unnest or throw
pop tos,sp
c;
\ =======================================
\
\ BRANCHES
\
\ =======================================
\ Run-time actions for structured-conditionals
\ always branch: else
code branch ( -- )
ldrsw x0,[ip]
add ip,ip,x0
c;
\ branch if false: if
code ?branch ( flag -- )
cmp tos,#0
pop tos,sp
0<> if
inc ip,#/token
else
ldrsw x0,[ip]
add ip,ip,x0
then
c;
\ branch if true: 0= if
code ?0=branch ( flag -- )
cmp tos,#0
pop tos,sp
0= if
inc ip,#/token
else
ldrsw x0,[ip]
add ip,ip,x0
then
c;
\ =======================================
\
\ DO LOOP etc.
\
\ =======================================
code (next) ( -- )
ldr x0,[rp]
subs x0,x0,#1
0>= if
str x0,[rp]
ldrsw x0,[ip]
add ip,ip,x0
ldr w0,[ip],#/token
\+ itc add lr,x0,org
\+ itc ldr w0,[lr],#/token
add x0,x0,org
br x0
then
inc rp,#3cells
inc ip,#/token
c;
code (for) ( n -- )
push ip,rp \ save the do offset address
inc ip,#/token
push2 tos,xzr,rp
pop tos,sp
c;
code (loop) ( -- )
ldr x0,[rp]
incs x0,#1
vc if
str x0,[rp]
ldrsw x0,[ip]
add ip,ip,x0
ldr w0,[ip],#/token
\+ itc add lr,x0,org
\+ itc ldr w0,[lr],#/token
add x0,x0,org
br x0
then
inc rp,#3cells
inc ip,#/token
c;
code (+loop) ( n -- )
ldr x0,[rp]
adds x0,x0,tos
vc if
str x0,[rp]
then
pop tos,sp
vc if
ldrsw x0,[ip]
add ip,ip,x0
ldr w0,[ip],#/token
\+ itc add lr,x0,org
\+ itc ldr w0,[lr],#/token
add x0,x0,org
br x0
then
inc rp,#3cells
inc ip,#/token
c;
code (do) ( l i -- )
mov x0,tos
pop2 x1,tos,sp
push ip,rp \ save the do offset address
inc ip,#/token
eor x1,x1,#0x8000.0000.0000.0000 \ bit63 ^ l
sub x0,x0,x1 \ bit63 ^ (i-l)
push2 x0,x1,rp
c;
code (?do) ( l i -- )
mov x0,tos
pop2 x1,tos,sp
cmp x1,x0
= if
ldrsw x0,[ip]
add ip,ip,x0
ldr w0,[ip],#/token
\+ itc add lr,x0,org
\+ itc ldr w0,[lr],#/token
add x0,x0,org
br x0
( r: loop-end-offset l+0x8000 i-l-0x8000 )
then
push ip,rp \ save the do offset address
inc ip,#/token
eor x1,x1,#0x8000.0000.0000.0000
sub x0,x0,x1
push2 x0,x1,rp
c;
code i ( -- n )
push tos,sp
ldp x0,x1,[rp]
add tos,x1,x0
c;
code ilimit ( -- n )
push tos,sp
ldr tos,[rp,#1cell]
eor tos,tos,#0x8000.0000.0000.0000
c;
code j ( -- n )
push tos,sp
add x2,rp,#3cells
ldp x0,x1,[x2]
add tos,x1,x0
c;
code jlimit ( -- n )
push tos,sp
ldr tos,[rp,#4cells]
eor tos,tos,#0x8000.0000.0000.0000
c;
code k ( -- n )
push tos,sp
add x2,rp,#6cells
ldp x0,x1,[x2]
add tos,x1,x0
c;
code klimit ( -- n )
push tos,sp
ldr tos,[rp,#7cells]
eor tos,tos,#0x8000.0000.0000.0000
c;
code (leave) ( -- )
inc rp,#2cells \ get rid of the loop indices
ldr ip,[rp],#1cell
ldrsw x0,[ip] \ branch
add ip,ip,x0
c;
code (?leave) ( f -- )
cmp tos,#0
pop tos,sp
= if
ldr w0,[ip],#/token
\+ itc add lr,x0,org
\+ itc ldr w0,[lr],#/token
add x0,x0,org
br x0
then
inc rp,#2cells \ get rid of the loop indices
ldr ip,[rp],#1cell
ldrsw x0,[ip] \ branch
add ip,ip,x0
c;
code unloop ( -- ) inc rp,#3cells c; \ Discard the loop indices
\ =======================================
\
\ MISC WORDS
\
\ =======================================
\ returns the following token as a code field address
code (') ( -- acf )
push tos,sp
ldr wtos,[ip],#/token
add tos,tos,org
c;
\ Modifies caller's ip to skip over an in-line string
code skipstr ( -- adr len)
push tos,sp
ldr x0,[rp]
ldrb wtos,[x0],#1
push x0,sp
add x0,x0,tos
inc x0,#/token
and x0,x0,#-/token
str x0,[rp]
c;
\ runtime code for an inline string with leading 8-bit count
code (") ( -- adr len)
push tos,sp
ldrb wtos,[ip],#1
push ip,sp
add ip,ip,tos
inc ip,#/token
and ip,ip,#-/token
c;
\ runtime code for an inline string with leading 32-bit count
code (l") ( -- adr len)
push tos,sp
ldr wtos,[ip],#4
push ip,sp
add ip,ip,tos
inc ip,#/token
and ip,ip,#-/token
c;
\ runtime code for an inline string with leading cell-sized count
code (n") ( -- adr len)
push tos,sp
ldr tos,[ip],#8
push ip,sp
add ip,ip,tos
inc ip,#/token
and ip,ip,#-/token
c;
\ =======================================
\
\ CASE / OF / ENDOF / ENDCASE
\
\ =======================================
\ Run time code for the case statement
code (of) ( selector test -- [ selector ] )
mov x0,tos
pop tos,sp
cmp tos,x0
<> if
ldrsw x0,[ip]
add ip,ip,x0
next
then
pop tos,sp
inc ip,#/token
c;
\ (endof) is the same as branch, and (endcase) is the same as drop,
\ but redefining them this way makes the decompiler much easier.
code (endof) ( -- ) \ branch
ldrsw x0,[ip]
add ip,ip,x0
c;
code (endcase) ( n -- ) pop tos,sp c; \ drop
\ "and of"
\ essentially " selector mask and value = if drop "
code (af) ( selector mask val -- [ selector ] )
mov x0,tos \ val
pop x1,sp \ mask
pop tos,sp \ selector
and x1,x1,tos
cmp x1,x0
<> if
ldrsw x0,[ip]
add ip,ip,x0
next
then
pop tos,sp
inc ip,#/token
c;
\ =======================================
\
\ $CASE
\
\ =======================================
\ ($endof) is the same as branch, and ($endcase) is a 2drop,
\ but redefining them this way makes the decompiler much easier.
\ $of is written completely in Forth.
\ code ($case) ( $ -- $ ) c;
code ($endof) ( -- ) \ branch
ldrsw x0,[ip]
add ip,ip,x0
c;
code ($endcase) ( -- ) inc sp,#1cell pop tos,sp c; \ 2drop
\ code ($endcase) ( -- ) c;
\ =======================================
\
\ DIGIT
\ convert a character to a digit in the given base
\
\ =======================================
code digit ( char base -- digit true | char false )
mov x0,tos \ x0 base
ldr x1,[sp] \ x1 char
and x1,x1,#0xff
cmp x1,#0x41 \ ascii A
>= if
cmp x1,#0x5b \ ascii [
< if
inc x1,#0x20
then
then
movz tos,#0 \ tos false
subs x1,x1,#0x30
< if
next
then
cmp x1,#10
>= if
cmp x1,#0x31
< if
next
then
dec x1,#0x27
then
cmp x1,x0
>= if
next
then
str x1,[sp]
movn tos,#0 \ tos true
c;
\ =======================================
\
\ MOVE and FILL
\
\ =======================================
code cmove ( from to cnt -- ) \ move cnt bytes using incrementing source address
adds x0,tos,#0 \ x0 cnt
ldr x1,[sp],#1cell
pop2 x2,tos,sp
0= if \ optimize zero-length move
next
then
cmp x1,x2 \ optimize zero-distance move
= if
next
then
begin
ldrb w3,[x2],#1
strb w3,[x1],#1
decs x0,#1
0= until
c;
code cmove> ( from to cnt -- ) \ move cnt bytes using decrementing source address
adds x0,tos,#0 \ x0 cnt
pop2 x1,x2,sp
ldr tos,[sp],#1cell
0= if \ optimize zero-length move
next
then
cmp x1,x2 \ optimize zero-distance move
= if
next
then
begin
decs x0,#1
ldrb w3,[x2,x0]
strb w3,[x1,x0]
0= until
c;
\ sometimes byte writes are not allowed
code lmove ( from to cnt -- ) \ move cnt bytes using incrementing source address
adds x0,tos,#0 \ x0 cnt
set x3,#3
bic x0,x0,x3 \ drop the last few bytes if not a multiple of 4
ldr x1,[sp],#1cell
pop2 x2,tos,sp
0= if \ optimize zero-length move
next
then
cmp x1,x2 \ optimize zero-distance move
= if
next
then
begin
ldr w3,[x2],#4
str w3,[x1],#4
decs x0,#4
0= until
c;
\
\ move This could use some significant performance enhancments. :-)
\
code move ( src dst len -- )
adds x0,tos,#0 \ x0 cnt
pop2 x1,x2,sp \ x1 = dst, x2 = src
ldr tos,[sp],#1cell
0= if \ optimize zero-length move
next
then
cmp x1,x2 \ optimize zero-distance move
= if
next
then
u< if
\ cmove
orr x3,x0,x1
orr x3,x3,x2
ands xzr,x3,#31
0= if
\ qmove
begin
ldp x4,x5,[x2],#16
ldp x6,x7,[x2],#16
stp x4,x5,[x1],#16
stp x6,x7,[x1],#16
subs x0,x0,#32
0= until
next
then
ands xzr,x3,#3
0= if
begin
ldr w3,[x2],#4
str w3,[x1],#4
decs x0,#4
0= until
next
then
\ Move by byte
begin
ldrb w3,[x2],#1
strb w3,[x1],#1
decs x0,#1
0= until
else
orr x3,x0,x1
orr x3,x3,x2
ands xzr,x3,#31
0= if
\ qmove>
add x2, x2, x0
add x1, x1, x0
begin
subs x0,x0,#32
ldp x4,x5,[x2, #-16]!
ldp x6,x7,[x2, #-16]!
stp x4,x5,[x1, #-16]!
stp x6,x7,[x1, #-16]!
0= until
next
then
\ cmove>
orr x3,x0,x1
orr x3,x3,x2
ands xzr,x3,#3
0= if
begin
decs x0,#4
ldr w3,[x2,x0]
str w3,[x1,x0]
0= until
next
then
begin
decs x0,#1
ldrb w3,[x2,x0]
strb w3,[x1,x0]
0= until
then
c;
\ fill slowly. I'm sure you can figure out six different optimizations for this one.
\ fill a range with a 8-bit value
code fill ( adr cnt char -- )
pop2 x0,x1,sp \ x0 = cnt, x1 = adr, tos = char
ands xzr,x1,#7
0= if \ Quad aligned?
and tos,tos,#255
orr x7,tos,tos, lsl #8
orr x7,x7,x7, lsl #16
orr x7,x7,x7, lsl #32
begin
cmp x0,#128
>= while
stp x7,x7,[x1],#16
stp x7,x7,[x1],#16
stp x7,x7,[x1],#16
stp x7,x7,[x1],#16
stp x7,x7,[x1],#16
stp x7,x7,[x1],#16
stp x7,x7,[x1],#16
stp x7,x7,[x1],#16
sub x0,x0,#128
repeat
then
begin
decs x0,#1
>= if
strb wtos,[x1],#1
then
< until
pop tos,sp
c;
\ fill a range with a 16-bit value
code wfill ( adr cnt w -- )
pop2 x0,x1,sp \ x0 = cnt, x1 = adr, tos = w
begin
decs x0,#1
>= if
strh wtos,[x1],#2
then
< until
pop tos,sp
c;
\ fill a range with a 24-bit value
code tfill ( adr cnt t -- )
pop2 x0,x1,sp \ x0 = cnt, x1 = adr, tos = t
begin
decs x0,#1
>= if
strh wtos,[x1],#2
lsr tos,tos,#16
strb wtos,[x1],#1
then
< until
pop tos,sp
c;
\ fill a range with a 32-bit value
code lfill ( adr cnt l -- )
pop2 x0,x1,sp \ x0 = cnt, x1 = adr, tos = char
begin
decs x0,#4
>= if
str wtos,[x1],#4
then
< until
pop tos,sp
c;
\ fill a range with a 64-bit value
code qfill ( adr cnt l -- )
pop2 x0,x1,sp \ x0 = cnt, x1 = adr, tos = char
begin
decs x0,#8
>= if
str tos,[x1],#8
then
< until
pop tos,sp
c;
\ =======================================
\
\ STRING PRIMITIVES
\
\ =======================================
\ remove cnt chars from start of string
code /string ( adr len cnt -- adr' len' )
\ tuck - -rot + swap
pop2 x0,x1,sp \ x0 len, x1 adr, tos cnt
add x1,x1,tos
push x1,sp
sub tos,x0,tos
c;
\ Skip initial occurrences of bvalue, returning the residual length
code bskip ( adr len bvalue -- residue )
pop2 x0,x1,sp \ r0-len r1-adr tos-bvalue
mov x2,tos \ r2-bvalue
mov tos,x0 \ tos: len
ands xzr,tos,tos
= if next then \ Bail out if len=0
begin
ldrb w0,[x1],#1
cmp x0,x2
<> if next then
decs tos,#1
= until
c;
\ Skip initial occurrences of lvalue, returning the residual length
code lskip ( adr len lvalue -- residue )
pop2 x0,x1,sp \ x0-len x1-adr tos-bvalue
mov x2,tos \ r2-lvalue
mov tos,x0 \ tos: len
ands xzr,tos,tos
= if next then \ Bail out if len=0
begin
ldr w0,[x1],#4
cmp x0,x2
<> if next then
decs tos,#4
= until
c;
\ Find the first occurence of bvalue, returning the residual string
code bscan ( adr len bvalue -- adr' len' )
pop2 x0,x1,sp \ r0-len r1-adr tos-bvalue
mov x2,tos \ r2-bvalue
mov tos,x0 \ tos: len
ands xzr,tos,tos
0= if push x1,sp next then \ Bail out if len=0
begin