~remexre/stahlos

ref: 7bf2eb256167be5b7093a32d8819707d1bca1286 stahlos/src/kernel-aarch64/forth/builtins.s -rw-r--r-- 13.2 KiB
7bf2eb25Nathan Ringo ... 2 months ago
                                                                                
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
.section .text

.macro defword label, name, flags=0
.global \label
\label\().padding:
.fill (258 - (\label - \label\().name)) % 4
\label\().header:
	.quad last_defword
	.byte \flags
	.byte \label - \label\().name
\label\().name:
	.ascii "\name"
\label:
.set last_defword, \label\().header
.endm
.set last_defword, 0

.macro pop
	cbz x12, forth_throw.stack_underflow
	sub x12, x12, 8
	ldr x10, [x11, x12]
.endm

.macro push
	cmp x12, x13
	b.ge forth_throw.stack_overflow
	str x10, [x11, x12]
	add x12, x12, 8
.endm

.macro rpop
	cbz x16, forth_throw.return_stack_underflow
	sub x16, x16, 8
	ldr x14, [x15, x16]
.endm

.macro rpush
	cmp x16, x17
	b.ge forth_throw.return_stack_overflow
	str x14, [x15, x16]
	add x16, x16, 8
.endm

.macro next
	ldr x0, [x18], #8
	br x0
.endm

defword forth_allot, "ALLOT"
	ldr x0, =data_space_ptr
	ldr x1, [x0]
	ldr x2, =kernel_end
	add x1, x10, x1
	cmp x1, x2
	b.ge panic.out_of_data_space
	pop
	str x1, [x0]
	next

defword forth_and, "AND"
	mov x0, x10
	pop
	and x10, x10, x0
	next

defword forth_bp, "BP"
	next

defword forth_cells, "CELLS"
	/* TODO(safety): Check stack depth */
	lsl x10, x10, #3
	next

defword forth_clz, "CLZ"
	/* TODO(safety): Check stack depth */
	clz x10, x10
	next

defword forth_comma, ","
	ldr x0, =data_space_ptr
	ldr x1, [x0]
	ldr x2, =kernel_end
	str x10, [x1], #8
	cmp x1, x2
	b.ge panic.out_of_data_space
	pop
	str x1, [x0]
	next

defword forth_comma_8, "C,"
	ldr x0, =data_space_ptr
	ldr x1, [x0]
	ldr x2, =kernel_end
	strb w10, [x1], #1
	cmp x1, x2
	b.ge panic.out_of_data_space
	pop
	str x1, [x0]
	next

defword forth_compile_comma, "COMPILE,"
	/* TODO(safety): alignment check */
	ldr x0, =data_space_ptr
	ldr x1, [x0]
	ldr x2, =kernel_end
	str x10, [x1], #8
	cmp x1, x2
	b.ge panic.out_of_data_space
	pop
	str x1, [x0]
	next

defword forth_cr, "CR"
	bl format_write_newline
	next

defword forth_ctz, "CTZ"
	/* TODO(safety): Check stack depth */
	rbit x10, x10
	clz x10, x10
	next

defword forth_depth, "DEPTH"
	lsr x0, x12, #3
	push
	mov x10, x0
	next

defword forth_div_mod, "/MOD"
	/* TODO(safety): Check stack depth */
	mov x1, x10
	pop
	mov x0, x10
	sdiv x2, x0, x1
	msub x10, x2, x1, x0
	push
	mov x10, x2
	next

defword forth_dot_decimal, ".DECIMAL"
	mov x0, x10
	pop
	bl format_write_num_sd
	next

defword forth_dot_hex, ".HEX"
	mov x0, x10
	pop
	bl format_write_num_ux
	next

defword forth_drop, "DROP"
	pop
	next

defword forth_dup, "DUP"
	push
	next

defword forth_emit, "EMIT"
	str x10, [x11] /* kinda an evil hack... should this be in dataspace instead? */
	pop
	mov x0, x11
	mov x1, 1
	bl format_write_string
	next

defword forth_equals, "="
	/* TODO(safety): Check stack depth */
	mov x0, x10
	pop
	cmp x10, x0
	cinv x10, xzr, eq
	next

defword forth_execute, "EXECUTE"
	mov x0, x10
	pop
	br x0

defword forth_false, "FALSE"
	push
	mov x10, 0
	next

defword forth_find_header, "FIND-HEADER"
	/* TODO(safety): Check stack depth
	 * x0: target name length
	 * x1: target name pointer
	 * x2: current header name chars remaining
	 * x3: target name current position
	 * x4: current header name current position
	 * x5: target current char
	 * x6: current header name current char
	 * x10: current dictionary link pointer
	 */
	mov x0, x10
	pop
	mov x1, x10
	mov x10, x19
forth_find_header.next_header:
	cbz x10, forth_find_header.end
	ldr x10, [x10]

	ldrb w2, [x10, #9]
	cmp x2, x0
	b.ne forth_find_header.next_header

	mov x3, x1
	add x4, x10, #10

forth_find_header.strcmp_loop:
	cbz x2, forth_find_header.end
	ldrb w5, [x3], #1
	ldrb w6, [x4], #1
	cmp w5, w6
	b.ne forth_find_header.next_header
	sub x2, x2, 1
	b forth_find_header.strcmp_loop

forth_find_header.end:
	next

defword forth_flags, "FLAGS"
	push
	ldr x10, [x19, #0x28]
	next

defword forth_from_rstack, "R>"
	push
	mov x10, x14
	rpop
	next

defword forth_gt, ">"
	/* TODO(safety): Check stack depth */
	mov x0, x10
	pop
	cmp x10, x0
	cinv x10, xzr, gt
	next

defword forth_header_to_flags, "HEADER>FLAGS"
	/* TODO(safety): Check stack depth */
	ldrb w10, [x10, #8]
	next

defword forth_header_to_xt, "HEADER>XT"
	/* TODO(safety): Check stack depth */
	ldrb w0, [x10, #9]
	add x10, x10, x0
	add x10, x10, #10
	next

defword forth_here, "HERE"
	push
	ldr x0, =data_space_ptr
	ldr x10, [x0]
	next

defword forth_immediate, "IMMEDIATE"
	ldr x0, [x19]
	ldrb w1, [x0, #8]
	orr w1, w1, #1
	strb w1, [x0, #8]
	next

defword forth_impl_branch, "(BRANCH)"
	ldr x18, [x18]
	next

defword forth_impl_branch_zero, "(BRANCH0)"
	mov x0, x10
	ldr x1, [x18], #8
	pop
	cmp x0, xzr
	csel x18, x1, x18, eq
	next

defword forth_impl_colon, "(:)" /* aka enter, aka docolon */
	rpush
	mov x14, x18
	add x18, x0, #16
	next

defword forth_impl_constant, "(CONSTANT)"
	push
	ldr x10, [x0, #16]
	next

defword forth_impl_does, "(DOES)"
	push
	rpush
	add x10, x0, #16
	mov x14, x18
	add x18, lr, #8
	next

defword forth_impl_literal, "(LITERAL)"
	push
	ldr x10, [x18], #8
	next

defword forth_impl_semicolon, "(;)" /* aka exit */
	mov x18, x14
	rpop
	next

defword forth_impl_value, "(VALUE)"
	push
	ldr x10, [x0, #16]
	next

defword forth_impl_variable, "(VARIABLE)"
	push
	add x10, x0, #16
	next

defword forth_initial_x2, "INITIAL-X2"
	push
	ldr x0, =initial_x2
	ldr x10, [x0]
	next

defword forth_invert, "INVERT"
	/* TODO(safety): Check stack depth */
	mvn x10, x10
	next

defword forth_kernel_end, "KERNEL-END"
	push
	ldr x10, =kernel_end
	next

defword forth_kernel_start, "KERNEL-START"
	push
	ldr x10, =kernel_start
	next

defword forth_load_8, "C@"
	ldrb w10, [x10]
	next

defword forth_load_16, "H@"
	/* TODO(safety): Check stack depth */
	ldrh w10, [x10]
	next

defword forth_load_32, "W@"
	/* TODO(safety): Check stack depth */
	ldr w10, [x10]
	next

defword forth_load_64, "@"
	/* TODO(safety): Check stack depth */
	ldr x10, [x10]
	next

defword forth_lshift, "LSHIFT"
	/* TODO(safety): Check stack depth */
	mov x0, x10
	pop
	lsl x10, x10, x0
	next

defword forth_lt, "<"
	/* TODO(safety): Check stack depth */
	mov x0, x10
	pop
	cmp x10, x0
	cinv x10, xzr, lt
	next

defword forth_max, "MAX"
	/* TODO(safety): Check stack depth */
	mov x0, x10
	pop
	cmp x0, x10
	csel x10, x0, x10, gt
	next

defword forth_min, "MIN"
	/* TODO(safety): Check stack depth */
	mov x0, x10
	pop
	cmp x0, x10
	csel x10, x0, x10, lt
	next

defword forth_minus, "-"
	/* TODO(safety): Check stack depth */
	mov x0, x10
	pop
	sub x10, x10, x0
	next

defword forth_mode_compile, "]"
	ldr x0, [x19, #0x28]
	and x0, x0, 254
	str x0, [x19, #0x28]
	next

defword forth_mode_interpret, "[", 1
	ldr x0, [x19, #0x28]
	orr x0, x0, 1
	str x0, [x19, #0x28]
	next

defword forth_nonzero, "0<>"
	/* TODO(safety): Check stack depth */
	cmp x10, xzr
	csetm x10, ne
	next

defword forth_not, "NOT"
	/* TODO(safety): Check stack depth */
	cmp x10, xzr
	csetm x10, eq
	next

defword forth_not_equals, "<>"
	/* TODO(safety): Check stack depth */
	mov x0, x10
	pop
	cmp x10, x0
	cinv x10, xzr, ne
	next

defword forth_one_minus, "1-"
	sub x10, x10, 1
	next

defword forth_one_plus, "1+"
	add x10, x10, 1
	next

defword forth_or, "OR"
	mov x0, x10
	pop
	orr x10, x10, x0
	next

defword forth_over, "OVER"
	/* TODO(safety): Check stack depth */
	push
	add x0, x11, x12
	ldr x10, [x0, #-16]
	next

defword forth_panic, "PANIC"
	b panic.forth

defword forth_parse, "PARSE"
	/* x0: delim char
	 * x1: string base
	 * x2: string total length
	 * x3: string initial offset
	 * x4: current string pointer
	 * x5: characters remaining in string
	 * x6: current character
	 */
	and x0, x10, #0xff
	ldr x1, [x19, #8]
	ldr x2, [x19, #16]
	ldr x3, [x19, #24]
	add x4, x1, x3
	sub x5, x2, x3
	mov x10, x4
	push
forth_parse.loop:
	cbz x5, forth_parse.loopend
	sub x5, x5, #1
	ldrb w6, [x4], #1
	cmp w6, w0
	b.ne forth_parse.loop
forth_parse.loopend:
	sub x10, x4, x10
	add x3, x3, x10
	str x3, [x19, #24]
	cbz x10, forth_parse.end
	sub x10, x10, #1
forth_parse.end:
	next

defword forth_parse_name, "PARSE-NAME"
	/* x0: string base
	 * x2: string initial offset
	 * x3: current character
	 * x4: string total length
	 */
	ldr x0, [x19, #8]
	ldr x4, [x19, #16]
	ldr x2, [x19, #24]
forth_parse_name.skip_space_loop:
	cmp x4, x2
	b.eq forth_parse_name.spaces_skipped
	ldrb w3, [x0, x2]
	cmp w3, #0x21
	b.hs forth_parse_name.spaces_skipped
	add x2, x2, 1
	b forth_parse_name.skip_space_loop
forth_parse_name.spaces_skipped:
	/* x0: rest of string
	 * x1: remaining length
	 * x2: initial offset of start of string
	 * x3: current character
	 * x4: string total length
	 * x10: length so far
	 */
	sub x1, x4, x2
	push
	add x10, x0, x2
	mov x0, x10
	push
	mov x10, xzr
forth_parse_name.loop:
	cbz x1, forth_parse_name.end
	ldrb w3, [x0], #1
	cmp w3, #0x21
	b.lo forth_parse_name.end
	sub x1, x1, 1
	add x10, x10, 1
	b forth_parse_name.loop
forth_parse_name.end:
	add x2, x2, x10
	add x2, x2, 1
	cmp x2, x4
	csel x2, x2, x4, lt
	str x2, [x19, #24]
	/* TODO(safety): error on name too long */
	next

defword forth_pick, "PICK"
	/* TODO(safety): Check stack depth */
	sub x0, x12, x10, lsl #3
	ldr x10, [x11, x0]
	next

defword forth_plus, "+"
	/* TODO(safety): Check stack depth */
	mov x0, x10
	pop
	add x10, x0, x10
	next

defword forth_process_table, "PROCESS-TABLE"
	push
	mov x10, x19
	next

defword forth_qdup, "?DUP"
	/* TODO(safety): Check stack depth */
	cbz x10, forth_qdup.end
	push
forth_qdup.end:
	next

defword forth_rdepth, "RDEPTH"
	push
	lsr x10, x16, #3
	next

defword forth_read_line, "READ-LINE"
	/* TODO(safety): Check stack depth */
	mov x1, x10
	pop
	mov x0, x10
	bl format_read_line
	mov x10, x1
	next

defword forth_rev_32, "WREV"
	/* TODO(safety): Check stack depth */
	rev w10, w10
	next

defword forth_rev_64, "REV"
	/* TODO(safety): Check stack depth */
	rev x10, x10
	next

defword forth_rshift, "RSHIFT"
	/* TODO(safety): Check stack depth */
	mov x0, x10
	pop
	lsr x10, x10, x0
	next

defword forth_rot, "ROT" /* a b c -- b c a */
	/* TODO(safety): Check stack depth */
	sub x0, x12, #8
	sub x1, x12, #16
	ldr x2, [x11, x0]
	str x10, [x11, x0]
	ldr x10, [x11, x1]
	str x2, [x11, x1]
	next

defword forth_rot_rev, "-ROT" /* a b c -- c a b */
	/* TODO(safety): Check stack depth */
	sub x0, x12, #8
	sub x1, x12, #16
	ldr x2, [x11, x1]
	str x10, [x11, x1]
	ldr x10, [x11, x0]
	str x2, [x11, x0]
	next

defword forth_rstack_copy, "R@"
	/* TODO(safety): Check rstack depth */
	push
	mov x10, x14
	next

defword forth_rstack_drop, "RDROP"
	rpop
	next

defword forth_rswap, "RSWAP"
	/* TODO(safety): Check stack depth, rstack depth */
	mov x0, x10
	mov x10, x14
	mov x14, x0
	next

defword forth_set_depth, "SET-DEPTH"
	lsl x12, x10, #3
	add x12, x12, #8
	pop
	next

defword forth_set_does, "SET-DOES"
	/* x0: Address of DOES word's CFA / DOES word's XT
	 * x1: Snippet to do jump to absolute addr
	 * x2: Address of word, later addr of CFA
	 * x3: Length of word name (DOES, then target)
	 */
	mov x0, x10
	pop
	ldr x2, [x19]
	ldrb w3, [x2, #9]!
	add x2, x2, #1
	add x2, x2, x3
	/* TODO: Store a direct BL instruction when possible? */
	ldr x1, =0xd63f012058000049 /* ldr x9, [pc, 8]; blr x9 */
	str x1, [x2]
	str x0, [x2, #8]

	/* TODO: Probably clear the icaches? */

	next

defword forth_set_flags, "SET-FLAGS"
	mov x0, x10
	pop
	str x0, [x19, #0x28]
	next

defword forth_set_rdepth, "SET-RDEPTH"
	/* TODO(safety): Check stack depth */
	rpush
	lsl x0, x10, #3
	add x16, x0, #8
	rpop
	pop
	next

defword forth_set_source, "SET-SOURCE"
	str x10, [x19, #24]
	pop
	str x10, [x19, #16]
	pop
	str x10, [x19, #8]
	pop
	next

defword forth_source, "SOURCE"
	push
	ldr x10, [x19, #8]
	push
	ldr x10, [x19, #16]
	push
	ldr x10, [x19, #24]
	next

defword forth_store_8, "C!"
	mov x0, x10
	pop
	mov x1, x10
	pop
	strb w1, [x0]
	next

defword forth_store_64, "!"
	mov x0, x10
	pop
	mov x1, x10
	pop
	str x1, [x0]
	next

defword forth_swap, "SWAP"
	/* TODO(safety): Check stack depth */
	add x2, x11, x12
	ldr x1, [x2, #-8]
	str x10, [x2, #-8]
	mov x10, x1
	next

.global forth_throw.stack_overflow
.global forth_throw.stack_underflow
.global forth_throw.return_stack_overflow
.global forth_throw.return_stack_underflow
defword forth_throw, "THROW"
	cbz x10, forth_throw.zero
	ldr x18, [x19, #0x30]
	next
forth_throw.zero:
	pop
	next
forth_throw.stack_overflow:
	/* We don't have any room left, so we can't push! We also pop a bit so
	 * CATCH can run at all... */
	sub x12, x12, 32
	mov x10, -3
	b forth_throw
forth_throw.stack_underflow:
	push
	mov x10, -4
	b forth_throw
forth_throw.return_stack_overflow:
	/* We rpop so CATCH can run... */
	push
	sub x16, x16, 8
	mov x10, -5
	b forth_throw
forth_throw.return_stack_underflow:
	push
	mov x10, -6
	b forth_throw

defword forth_times, "*"
	/* TODO(safety): Check stack depth */
	mov x0, x10
	pop
	mul x10, x0, x10
	next

defword forth_to_number, ">NUMBER"
	/* TODO(safety): Check stack depth */
	/* Not the ANS >NUMBER! */
	/* ( c-addr len -- num valid? ) */
	mov x1, x10
	pop
	mov x0, x10
	ldrb w2, [x19, #0x28]
	and x2, x2, #2
	bl to_number
	mov x10, x3
	push
	mov x10, x4
	next

defword forth_to_rstack, ">R"
	rpush
	mov x14, x10
	pop
	next

defword forth_todo, "TODO"
	b panic.todo

defword forth_true, "TRUE"
	push
	mvn x10, xzr
	next

defword forth_two_from_rstack, "2R>"
	push
	mov x0, x14
	rpop
	mov x10, x14
	push
	rpop
	mov x10, x0
	next

defword forth_two_to_rstack, "2>R"
	rpush
	mov x0, x10
	pop
	mov x14, x10
	rpush
	pop
	mov x14, x0
	next

defword forth_xor, "XOR"
	mov x0, x10
	pop
	eor x10, x10, x0
	next

.global forth.last_builtin_header
.equiv forth.last_builtin_header, last_defword

.section .bss

.comm data_space_ptr, 8

/* vi: set ft=arm64asm : */