-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.fs
5047 lines (4522 loc) · 132 KB
/
core.fs
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
\ Copyright (c) 2024 Travis Bemann
\
\ Permission is hereby granted, free of charge, to any person obtaining a copy
\ of this software and associated documentation files (the "Software"), to deal
\ in the Software without restriction, including without limitation the rights
\ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\ copies of the Software, and to permit persons to whom the Software is
\ furnished to do so, subject to the following conditions:
\
\ The above copyright notice and this permission notice shall be included in
\ all copies or substantial portions of the Software.
\
\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\ SOFTWARE.
begin-module zscript
armv6m import
\ We are out of memory
: x-out-of-memory ." out of memory" cr ;
\ We have a typing error
: x-incorrect-type ." incorrect type" cr ;
\ We have a size error
: x-incorrect-size ." incorrect size" cr ;
\ Offset out of range error
: x-offset-out-of-range ." offset out of range" cr ;
\ Null dereference
: x-null-dereference ." null dereference" cr ;
\ Unaligned dereference
: x-unaligned-access ." unaligned access" cr ;
\ Unsafe operation
: x-unsafe-op ." unsafe operation" cr ;
\ Value not small integer
: x-not-small-int ." not small int" cr ;
\ No sequence being defined
: x-no-seq-being-defined ." no sequence being defined" cr ;
\ Wrong sequence type
: x-wrong-seq-type ." wrong sequence type" cr ;
begin-module zscript-internal
\ Are we initialized
false value inited?
\ Are we in zeptoscript
false value in-zscript?
\ Default compile-time heap size
65536 constant default-compile-size
\ Default runtime heap size
65536 constant default-runtime-size
\ constant bytes syntax
254 constant syntax-const-bytes
\ Saved handle-number-hook
0 value saved-handle-number-hook
\ Saved find-hook
0 value saved-find-hook
\ Record syntax
255 constant syntax-record
\ The per-task zeptoscript structure
\ This will ultimately be a user variable, but for testing purposes it will
\ be a plain variable.
variable zscript-state
\ The zeptoscript structure
begin-structure zscript-size
\ The RAM globals array
field: ram-globals-array
\ The flash globals array
field: flash-globals-array
\ The bottom of the from semi-space
field: from-space-bottom
\ The top of the from semi-space
field: from-space-top
\ The current to semi-space address
field: to-space-current
\ The bottom of the to semi-space
field: to-space-bottom
\ The top of the to semi-space
field: to-space-top
end-structure
\ Get the RAM globals array
: ram-globals-array@ zscript-state @ ram-globals-array @ ;
\ Get the flash globals array
: flash-globals-array@ zscript-state @ flash-globals-array @ ;
\ Get the bottom of the from semi-space
: from-space-bottom@ zscript-state @ from-space-bottom @ ;
\ Get the top of the from semi-space
: from-space-top@ zscript-state @ from-space-top @ ;
\ Get the current to semi-space address
: to-space-current@ zscript-state @ to-space-current @ ;
\ Get the bottom of the to semi-space
: to-space-bottom@ zscript-state @ to-space-bottom @ ;
\ Get the top of the to semi-space
: to-space-top@ zscript-state @ to-space-top @ ;
\ Set the RAM globals array
: ram-globals-array! zscript-state @ ram-globals-array ! ;
\ Set the flash globals array
: flash-globals-array! zscript-state @ flash-globals-array ! ;
\ Set the bottom of the from semi-space
: from-space-bottom! zscript-state @ from-space-bottom ! ;
\ Set the top of the from semi-space
: from-space-top! zscript-state @ from-space-top ! ;
\ Set the current to semi-space address
: to-space-current! zscript-state @ to-space-current ! ;
\ Set the bottom of the to semi-space
: to-space-bottom! zscript-state @ to-space-bottom ! ;
\ Set the top of the to semi-space
: to-space-top! zscript-state @ to-space-top ! ;
\ Bit in the type indicating whether a value contains cells to GC
\ Note that this is after subtracting two from the type
8 constant has-values
\ Type shift
28 constant type-shift
\ Size mask
-1 32 type-shift - tuck lshift swap rshift constant size-mask
\ Set whether a value is an integer
: int? ( value -- int? ) [inlined] 1 and 0<> ;
\ Get whether a value is an address
: addr? ( value -- addr? ) [inlined] 1 and 0= ;
\ Value to address
: >addr ( value -- addr ) [inlined] 1 bic ;
\ Relocate a block of memory
: relocate ( orig -- new )
code[
0 tos cmp_,#_
ne bc>
pc 1 pop
>mark
1 r0 movs_,#_
r0 tos tst_,_
eq bc>
pc 1 pop
>mark
]code
from-space-bottom@
from-space-top@
code[
4 dp r0 ldr_,[_,#_]
tos r0 cmp_,_
lo bc>
4 dp adds_,#_
tos 1 dp ldm
pc 1 pop
>mark
tos 1 dp ldm
tos r0 cmp_,_
hs bc>
4 dp adds_,#_
r0 tos movs_,_
pc 1 pop
>mark
0 r0 tos ldr_,[_,#_]
1 r1 movs_,#_
r1 tos tst_,_
eq bc>
4 dp adds_,#_
r1 tos bics_,_
pc 1 pop
>mark
0 dp r0 str_,[_,#_]
]code
size-mask and 1 rshift cell align { size }
to-space-current@ { current }
current size + { next-current }
to-space-top@ next-current >= averts x-out-of-memory
dup current size move
current 1 or swap !
next-current to-space-current!
current
;
\ Relocate the stack
: relocate-stack ( -- )
sp@ stack-base @ swap begin 2dup > while
dup @ relocate over !
cell+
repeat
2drop
rp@ rstack-base @ swap begin 2dup > while
dup @ relocate over !
cell+
repeat
2drop
;
\ Swap spaces
: swap-spaces ( -- )
to-space-bottom@
to-space-top@
from-space-bottom@
from-space-top@
to-space-top!
dup to-space-current!
to-space-bottom!
from-space-top!
from-space-bottom!
;
\ Carry out a GC cycle
: gc ( -- )
swap-spaces
relocate-stack
ram-globals-array@ relocate ram-globals-array!
flash-globals-array@ relocate flash-globals-array!
to-space-bottom@ { gc-current }
begin gc-current to-space-current@ u< while
gc-current @ { header }
header size-mask and 1 rshift cell align { aligned-size }
header [ has-values type-shift lshift ] literal and if
gc-current aligned-size + { gc-current-end }
gc-current cell+ begin dup gc-current-end u< while
dup @ relocate over ! cell+
repeat
drop
then
aligned-size +to gc-current
repeat
;
\ Case cells to nulls, integers, but not words
: >small-int ( x -- 0 | int )
code[
0 tos cmp_,#_
ne bc>
pc 1 pop
>mark
30 tos r0 lsrs_,_,#_
1 r0 cmp_,#_
eq bc>
2 r0 cmp_,#_
eq bc>
1 tos tos lsls_,_,#_
1 r0 movs_,#_
r0 tos orrs_,_
pc 1 pop
>mark
>mark
]code
['] x-not-small-int ?raise
;
\ Get whether a value is not false
: flag> ( x -- flag )
code[
0 tos cmp_,#_
ne bc>
pc 1 pop
>mark
1 r0 movs_,#_
r0 tos tst_,_
eq bc>
r0 tos cmp_,_
eq bc>
0 tos movs_,#_
tos tos mvns_,_
pc 1 pop
>mark
0 tos movs_,#_
pc 1 pop
>mark
0 tos r0 ldr_,[_,#_]
type-shift r0 r0 lsrs_,_,#_
1 r0 cmp_,#_
eq bc>
0 tos movs_,#_
tos tos mvns_,_
pc 1 pop
>mark
4 tos r0 ldr_,[_,#_]
0 r0 cmp_,#_
eq bc>
0 tos movs_,#_
tos tos mvns_,_
pc 1 pop
>mark
r0 tos movs_,#_
]code
;
\ Types
0 >small-int constant null-type
1 >small-int constant int-type
2 >small-int constant bytes-type
3 >small-int constant big-int-type
4 >small-int constant double-type
5 >small-int constant const-bytes-type
6 >small-int constant xt-type
7 >small-int constant tagged-type
8 >small-int constant symbol-type
10 >small-int constant cells-type
11 >small-int constant closure-type
12 >small-int constant slice-type
13 >small-int constant save-type
14 >small-int constant ref-type
15 >small-int constant force-type
\ Tags
1 >small-int constant word-tag
\ Cast nulls, integers, and words to cells, validating them
: integral> ( 0 | int | addr -- x' )
code[
0 tos cmp_,#_
ne bc>
pc 1 pop
>mark
1 r0 movs_,#_
r0 tos tst_,_
eq bc>
1 tos tos asrs_,_,#_
pc 1 pop
>mark
0 tos r0 ldr_,[_,#_]
type-shift r0 r0 lsrs_,_,#_
1 r0 cmp_,#_
ne bc>
cell tos tos ldr_,[_,#_]
pc 1 pop
>mark
]code
['] x-incorrect-type ?raise
;
\ Cast doubles to cells, validating them
: double> ( double -- x0 x1 )
code[
0 tos cmp_,#_
eq bc>
1 r0 movs_,#_
r0 tos tst_,_
ne bc>
0 tos r0 ldr_,[_,#_]
type-shift r0 r0 lsrs_,_,#_
double-type integral> 2 - r0 cmp_,#_
ne bc>
4 r7 subs_,#_
8 r6 r0 ldr_,[_,#_]
0 r7 r0 str_,[_,#_]
4 r6 r6 ldr_,[_,#_]
pc 1 pop
>mark
>mark
>mark
]code
['] x-incorrect-type ?raise
;
\ Get an allocation's type
: >type ( value -- type )
code[
1 r0 movs_,#_
r0 tos tst_,_
eq bc>
int-type tos movs_,#_
pc 1 pop
>mark
0 tos cmp_,#_
ne bc>
null-type tos movs_,#_
pc 1 pop
>mark
0 tos tos ldr_,[_,#_]
type-shift tos tos lsrs_,_,#_
2 tos adds_,#_
1 tos tos lsls_,_,#_
r0 tos orrs_,_
]code
;
\ Get whether a value is null, an integer, or a word
: integral? ( value -- integral? )
code[
1 r0 movs_,#_
r0 tos tst_,_
eq bc>
31 tos tos lsls_,_,#_
31 tos tos asrs_,_,#_
pc 1 pop
>mark
0 tos cmp_,#_
ne bc>
31 r0 tos lsls_,_,#_
31 tos tos asrs_,_,#_
pc 1 pop
>mark
0 tos tos ldr_,[_,#_]
type-shift tos tos lsrs_,_,#_
big-int-type integral> 2 - tos cmp_,#_
ne bc>
31 r0 tos lsls_,_,#_
31 tos tos asrs_,_,#_
pc 1 pop
>mark
0 tos movs_,#_
]code
;
\ Validate whether a value is an integer
: validate-int ( value -- ) 1 and 0<> averts x-incorrect-type ;
\ Validate whether a value is a word
: validate-word ( value -- ) >type big-int-type = averts x-incorrect-type ;
\ Get whether a value is an xt or closure
: xt? ( value -- xt? )
>type dup xt-type = swap closure-type = or
;
\ Preemptively carry out a GC cycle to ensure that a certain amount of space
\ is available
: ensure { bytes -- }
bytes integral> to-space-current@ + to-space-top@ > if
gc
bytes integral> to-space-current@ + to-space-top@
<= averts x-out-of-memory
then
;
\ Low level routine used to check whether enough un-GC'ed space is available
: needs-gc? ( bytes -- )
to-space-current@ + to-space-top@ >
;
\ Allocate memory as cells
: allocate-cells { count type -- addr }
count integral> 1+ cells >small-int { bytes }
bytes integral> to-space-current@ + to-space-top@ > if
gc
bytes integral> to-space-current@ +
to-space-top@ <= averts x-out-of-memory
then
to-space-current@ { current }
bytes integral> to bytes
bytes current + to-space-current!
current
dup cell+ bytes cell - 0 fill
bytes 1 lshift type integral> 2 - type-shift lshift or over !
;
\ Allocate a reference
: allocate-ref { x -- addr }
to-space-current@ [ 2 cells ] literal + to-space-top@ > if
gc
to-space-current@ [ 2 cells ] literal + to-space-top@ <=
averts x-out-of-memory
then
to-space-current@ { current }
current [ 2 cells ] literal + to-space-current!
current
x over cell+ !
[ ref-type integral> 2 - type-shift lshift
2 cells 1 lshift or ] literal over !
;
\ Allocate memory as bytes
: allocate-bytes { count type -- addr }
count integral> cell+ cell align >small-int { bytes }
bytes integral> to-space-current@ + to-space-top@ > if
gc
bytes integral> to-space-current@ +
to-space-top@ <= averts x-out-of-memory
then
to-space-current@ { current }
bytes integral> to bytes
bytes current + to-space-current!
current
dup cell+ bytes cell - 0 fill
count integral> cell+ 1 lshift
type integral> 2 - type-shift lshift or over !
;
\ Allocate a word
: >big-int { x -- addr }
to-space-current@ [ 2 cells ] literal + to-space-top@ > if
x 1 and { lowest }
x 1 or to x
gc
x 1 bic lowest or to x
to-space-current@ [ 2 cells ] literal + to-space-top@ <=
averts x-out-of-memory
then
to-space-current@ { current }
current [ 2 cells ] literal + to-space-current!
current
x over cell+ !
[ big-int-type integral> 2 - type-shift lshift 2 cells 1 lshift or ]
literal over !
;
\ Allocate a cell
: allocate-cell { x type -- addr }
to-space-current@ [ 2 cells ] literal + to-space-top@ > if
x 1 and { lowest }
x 1 or to x
gc
x 1 bic lowest or to x
to-space-current@ [ 2 cells ] literal + to-space-top@ <=
averts x-out-of-memory
then
to-space-current@ { current }
current [ 2 cells ] literal + to-space-current!
current
x over cell+ !
type integral> 2 - type-shift lshift
[ 2 cells 1 lshift ] literal or over !
;
\ Allocate a double-word
: >double { x0 x1 -- addr }
to-space-current@ [ 3 cells ] literal + to-space-top@ > if
x0 1 and { lowest0 }
x1 1 and { lowest1 }
x0 1 or to x0
x1 1 or to x1
gc
x0 1 bic lowest0 or to x0
x1 1 bic lowest1 or to x1
to-space-current@ [ 3 cells ] literal + to-space-top@
<= averts x-out-of-memory
then
to-space-current@ { current }
current [ 3 cells ] literal + to-space-current!
x0 x1 current cell+ 2!
current
[ double-type integral> 2 - type-shift lshift 3 cells 1 lshift or ]
literal over !
;
\ Allocate a double-word
: allocate-2cell { x0 x1 type -- addr }
to-space-current@ [ 3 cells ] literal + to-space-top@ > if
x0 1 and { lowest0 }
x1 1 and { lowest1 }
x0 1 or to x0
x1 1 or to x1
gc
x0 1 bic lowest0 or to x0
x1 1 bic lowest1 or to x1
to-space-current@ [ 3 cells ] literal + to-space-top@
<= averts x-out-of-memory
then
to-space-current@ { current }
current [ 3 cells ] literal + to-space-current!
x0 current cell+ !
x1 current [ 2 cells ] literal + !
current
type integral> 2 - type-shift lshift
[ 3 cells 1 lshift ] literal or over !
;
\ Allocate a tagged value
: allocate-tagged { count tag -- addr }
count integral> [ 2 cells ] literal + cell align >small-int { bytes }
bytes integral> to-space-current@ + to-space-top@ > if
gc
bytes integral> to-space-current@ +
to-space-top@ <= averts x-out-of-memory
then
to-space-current@ { current }
bytes integral> to bytes
bytes current + to-space-current!
current
tag integral> over cell+ !
dup [ 2 cells ] literal + bytes [ 2 cells ] literal - 0 fill
count integral> [ 2 cells ] literal + 1 lshift
[ tagged-type integral> 2 - type-shift lshift ] literal or over !
;
\ Cast cells to nulls, integers, and words
: >integral ( x -- 0 | int | addr )
code[
0 tos cmp_,#_
ne bc>
pc 1 pop
>mark
30 tos r0 lsrs_,_,#_
1 r0 cmp_,#_
eq bc>
2 r0 cmp_,#_
eq bc>
1 tos tos lsls_,_,#_
1 r0 movs_,#_
r0 tos orrs_,_
pc 1 pop
>mark
>mark
]code
>big-int
;
\ Construct a saved state
: allocate-save ( -- save )
handler @ >integral { handler }
sp@ stack-base @ swap - cell -
rp@ rstack-base @ swap - [ 2 cells ] literal -
2dup >small-int swap >small-int { rstack-count stack-count }
+ [ 4 cells ] literal + >small-int { bytes }
bytes integral> to-space-current@ + to-space-top@ > if
gc
bytes integral> to-space-current@ +
to-space-top@ <= averts x-out-of-memory
then
to-space-current@ { current }
bytes integral> to bytes
bytes current + to-space-current!
current
bytes 1 lshift
[ save-type integral> 2 - type-shift lshift ] literal or over !
stack-count over cell+ !
rstack-count over [ 2 cells ] literal + !
handler over [ 3 cells ] literal + !
stack-count integral> to stack-count
rstack-count integral> to rstack-count
sp@ [ 2 cells ] literal +
over [ 4 cells ] literal + stack-count move
rp@ [ 6 cells ] literal +
over [ 4 cells ] literal + stack-count + rstack-count move
;
\ Convert a pair of cells to nulls, integers, or words
: 2>integral ( x0 x1 -- 0|int|addr 0|int|addr )
4 cells needs-gc? if
2 cells [: { buffer }
buffer 2!
gc
4 cells needs-gc? triggers x-out-of-memory
buffer 2@
;] with-aligned-allot
then
>integral swap >integral swap
;
\ Convert a pair of nulls, integers, or words to cells
: 2integral> ( 0|int|addr 0|int|addr -- x0 x1 )
integral> swap integral> swap
;
\ Convert a pair of cell-pairs into doubles
: 2>double ( x0 x1 x2 x3 -- double0 double1 )
6 cells needs-gc? if
4 cells [: { buffer }
buffer 2!
buffer [ 2 cells ] literal + 2!
gc
6 cells needs-gc? triggers x-out-of-memory
buffer [ 2 cells ] literal + 2@
buffer 2@
;] with-aligned-allot
then
>double -rot >double swap
;
\ Convert a pair of doubles into cell-pairs
: 2double> ( double0 double1 -- x0 x1 x2 x3 )
double> rot double> 2swap
;
\ Convert any number of cells to nulls, integers, or words
\ Note: do not call with a count of 0 or 1
: n>integral ( xn ... x0 count -- 0|int|addr ... 0|int|addr )
dup integral> cells [:
{ count buf }
buf count integral> cells + buf ?do i ! cell +loop
buf dup count integral> 1- cells + ?do
i @ >integral
[ cell negate ] literal +loop
;] with-aligned-allot
;
\ Convert any number of nulls, integers, or words to cells
\ Note: do not call with a count of 0 or 1
: nintegral> ( 0|int|addr ... 0|int|addr count -- xn ... x0 )
dup integral> cells [:
{ count buf }
buf count integral> cells + buf ?do i ! cell +loop
buf dup count integral> 1- cells + ?do
i @ integral>
[ cell negate ] literal +loop
;] with-aligned-allot
;
\ Cast cells to xts
: integral>xt ( x -- xt )
integral> xt-type allocate-cell
;
\ Convert an xt to an integral
: xt>integral ( xt -- integral )
dup >type xt-type = averts x-incorrect-type
forth::cell+ @ >integral
;
\ Raise an exception with an integral value
: integral-?raise ( integral-xt -- )
integral> ?raise
;
\ Normalize a value
: normalize ( value -- value' )
dup integral? if
dup if
dup 1 and if
1 arshift
else
cell+ @
then
then
>integral
then
;
\ Get whether an integer is a small integer
: small-int? ( value -- small? )
dup 1 and if
drop true
else
0=
then
;
\ Get the tag of a tagged value
: >tag ( value -- tag )
dup >type tagged-type = averts x-incorrect-type
cell+ @ >integral
;
\ Get an allocation's size - note that the return value is not an integral
\ value
: >size ( value -- size )
code[
0 tos cmp_,#_
ne bc>
pc 1 pop
>mark
1 r0 movs_,#_
r0 tos tst_,_
eq bc>
0 tos movs_,#_
pc 1 pop
>mark
0 tos tos ldr_,[_,#_]
32 type-shift - tos tos lsls_,_,#_
33 type-shift - tos tos lsrs_,_,#_
]code
;
\ Get a value in a tagged data structure
: t@+ ( index object -- value )
dup >type tagged-type = averts x-incorrect-type
swap integral> 2 + cells
over >size over u> averts x-offset-out-of-range
+ @ >integral
;
\ Set a value in a tagged data structure
: t!+ ( value index object -- )
dup >type tagged-type = averts x-incorrect-type
swap integral> 2 + cells
over >size over u> averts x-offset-out-of-range
+ swap integral> swap !
;
\ Handle a number
: do-handle-number ( addr bytes -- [value] -1 | 0 )
parse-integer if
>integral
state @ if
dup small-int? if
lit,
else
integral> lit, postpone >integral
then
then
true
else
drop false
then
;
\ Specify current flash global variable ID
: set-current-flash-global-id ( id -- )
get-current
swap
internal set-current
integral> s" *GLOBAL*" internal::constant-with-name
set-current
;
\ Get current flash global variable ID
: get-current-flash-global-id ( -- id )
s" *GLOBAL*" flash-latest find-all-dict dup if
forth::>xt execute >integral
else
drop 0
then
;
\ KEY? stack base
variable key?-stack-base
\ KEY stack base
variable key-stack-base
\ EMIT? stack base
variable emit?-stack-base
\ EMIT stack base
variable emit-stack-base
\ ERROR-EMIT? stack base
variable error-emit?-stack-base
\ ERROR-EMIT stack base
variable error-emit-stack-base
\ RAM global ID index
0 constant current-ram-global-id-index
\ Sequence definition index
1 >small-int constant seq-define-index
\ Evaluation stack
2 >small-int constant eval-stack-global-id
\ KEY? stack
3 >small-int constant key?-stack-global-id
\ KEY stack
4 >small-int constant key-stack-global-id
\ EMIT? stack
5 >small-int constant emit?-stack-global-id
\ EMIT stack
6 >small-int constant emit-stack-global-id
\ ERROR-EMIT? stack
7 >small-int constant error-emit?-stack-global-id
\ ERROR-EMIT stack
8 >small-int constant error-emit-stack-global-id
\ Initial RAM global ID
9 >small-int constant init-ram-global-id
\ Initial RAM globals count
9 >small-int constant init-ram-global-count
\ Cell sequence definition type
0 >small-int constant define-cells
\ Byte sequence definition type
1 >small-int constant define-bytes
end-module> import
\ Types
null-type constant null-type
int-type constant int-type
bytes-type constant bytes-type
big-int-type constant big-int-type
double-type constant double-type
const-bytes-type constant const-bytes-type
xt-type constant xt-type
tagged-type constant tagged-type
symbol-type constant symbol-type
cells-type constant cells-type
closure-type constant closure-type
slice-type constant slice-type
save-type constant save-type
ref-type constant ref-type
force-type constant force-type
\ Ensure that a number of bytes are available in the heap without using said
\ bytes triggering GC
: ensure ( bytes -- ) ensure ;
\ Get the raw LIT,
: raw-lit, ( x -- ) integral> lit, ;
\ Redefine LIT,
: lit, ( x -- )
dup small-int? if
lit,
else
dup >type case
big-int-type of
integral> lit, postpone >integral
endof
double-type of
double> swap lit, lit, postpone >double
endof
['] x-incorrect-type ?raise
endcase
then
;
\ Convert an address/length pair into constant bytes
: addr-len>const-bytes ( c-addr u -- const-bytes )
2integral> const-bytes-type allocate-2cell
;
\ Convert an address/length pair into bytes
: addr-len>bytes ( c-addr u -- bytes )
dup bytes-type allocate-bytes { bytes }
swap integral> bytes cell+ rot integral> move
bytes
;
\ Get the raw bytes of a cells, bytes, or slice value
: >raw ( x -- x' )
begin
dup >type case
cells-type of true endof
bytes-type of true endof
const-bytes-type of true endof
slice-type of cell+ @ false endof
['] x-incorrect-type ?raise
endcase
until
;
\ Get the raw offset of a cells, bytes, or slice value
: >raw-offset ( x -- offset )
0 { offset }
begin
dup >type case
cells-type of drop true endof
bytes-type of drop true endof
const-bytes-type of drop true endof
slice-type of
dup [ 2 cells ] literal + @
offset integral> swap integral> + >integral to offset
cell+ @ false
endof
['] x-incorrect-type ?raise
endcase
until
offset
;
\ Get the length of cells in entries or bytes in bytes
: >len ( cells | bytes -- len )
dup >type case
cells-type of
>size cell - 2 rshift >integral
endof
bytes-type of
>size cell - >integral