-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzmforth.S
2133 lines (1863 loc) · 52.3 KB
/
zmforth.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
// ZmForth! -- an ANS Forth implementation for the Z-machine
// Copyright (c) 2009 Marshall Vandegrift
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#define IMMEDIATE (0x80)
#define HIDDEN (0x20)
#define LENMASK (0x1f)
.section data
header: .fill 0x40
// We're using the highest global registers as the forth interpreter pointers
globals:
.globals globals
.fill 236, 2, 0
#define WINDOW %g232
#define KIN %g233
#define KOUT %g234
#define TIMEOUTS %g235
#define UP %g236
.word uvar
#define RSP %g237
.word rstack
#define PSP %g238
.word pstack
#define IP %g239
.word 0
#define KBUFSZ 32
_kbuf: // Single-key input-buffer
.fill KBUFSZ, 1, 0
_abuf: // ACCEPT buffer
.fill 1024, 1, 0
uvar: // Enough space for 8 tasks with 32 user variables each.
.set _uoff, 0 // Current user variable offset
.set _unext, uvar // Address of next user variable
.fill 32 * 8, 2, 0
tibtop: // TIB grows downwards
.fill 1024, 1, 0
pstack: // Parameter stack grows upwards
.fill 4, 2, 0 // Small underflow buffer
dict: // The dictionary, where all the Forth words live
.set _link, 0 // Dictionary last-link pointer
// Hop down to the very end of allowed dynamic memory
.fill (header + 0xfffa) - dict
rstack: // Return stack grows upwards
.word 0
// Go back to the beginning of the dictionary
.org dict
// Get the Forth party started
.section text
_start: .start _start
calln forth
quit
// Stack-manipulation helpers.
#define RS_PUSH(arg) \
sub RSP, 2, RSP; \
storew RSP, 0, arg
#define RS_POP(dst) \
loadw RSP, 0, dst; \
add RSP, 2, RSP
#define PS_PUSH(arg) \
sub PSP, 2, PSP; \
storew PSP, 0, arg
#define PS_POP(dst) \
loadw PSP, 0, dst; \
add PSP, 2, PSP
// Basic dictionary linked-list header
#define HEAD(name, flags, label1) \
.section data; \
.label entry_ ## label1; \
.word _link; \
.set _link, entry_ ## label1; \
.set _name, .; \
.byte 0; \
.ascii name; \
.set _len, (. - _name) - 1; \
.align 2; \
.set _here, .; \
.org _name; \
.byte _len | flags; \
.org _here; \
.label label1
// end macro definition
// Macro to define a machine-code Forth word. Adds the word to the dictionary
// and leaves the assembler ready for the word code to follow. We're using
// indirect threaded code, so the word still needs an executor, but for machine
// code words the executor is just the word's code itself.
#define CODE(name, flags, label1) \
HEAD(name, flags, label1); \
.word @code_ ## label1; \
.section text; \
.align 4; \
.label code_ ## label1
// end macro definition
//////
// Basic definitions for the Forth "inner interpreter." The inner interpreter
// executes the indirect threaded code, but also needs to be able to support
// methods for encoding literal values and branching.
// Core Forth inner interpreter for indirect threaded code. Executes the Forth
// word pointed at by the Forth instruction pointer via its word-specific
// executor function and advances the Forth IP. Passes the address of the
// definition being executed in %l0 to avoid extra return stack operations
// when executing native code definitions.
//
// This inner interpreter implementation uses a vectored `jump' instruction to
// transfer control to the code implementing the target Forth word. The
// Z-machine `jump' instruction is always relative, which means we need to do
// some address arithmetic in order to get to the right place. Vectored `call'
// instructions are a bit easier and work just as well, but the Z-machine
// routine model would mean needing to be careful to always correctly `ret' and
// losing all local registers across calls.
//
.section text
.routine forth, 15
store _boot, IP
_next: loadw IP, 0, %l0
add IP, 2, IP
_exec: loadw %l0, 0, %l1
sub %l1, @1f, %l1
art_shift %l1, 2, %l1
add %l1, 2, %l1
jump %l1
1: // Nothing -- we just want the address after the jump.
// Executor for Forth words coded as indirect threaded code. This handles
// getting the interpreter pointer pointed at the right place then lets _next
// handle the rest.
.align 4
docol: RS_PUSH(IP)
add %l0, 2, IP
jump _next
// Finish executing a Forth word. Pops the IP off the return stack then lets
// _next handle the rest.
CODE("exit", 0, exit)
RS_POP(IP)
jump _next
// Read a literal word from the Forth definition and push it on the stack.
CODE("(literal)", 0, dolit) // ( -- n|u )
loadw IP, 0, %l0
PS_PUSH(%l0)
add IP, 2, IP
jump _next
// Read a literal string from the Forth definition and push it on the stack
CODE("(sliteral)", 0, dosliteral) // ( -- c-addr u )
loadb IP, 0, %l1
add IP, 1, %l0
PS_PUSH(%l0)
PS_PUSH(%l1)
add %l0, %l1, IP
and IP, 1, %l2
add IP, %l2, IP
jump _next
// Ditto, but push the string on as a counted string
CODE("(csliteral)", 0, docsliteral) // ( -- c-addr )
PS_PUSH(IP)
loadb IP, 0, %l0
add IP, %l0, IP
inc IP
and IP, 1, %l1
add IP, %l1, IP
jump _next
// Unconditional branch.
CODE("branch", 0, branch)
loadw IP, 0, IP
jump _next
// Conditional branch, only if flag is 0.
CODE("?branch", 0, qbranch)
PS_POP(%l0)
jz %l0, 1f
add IP, 2, IP
jump _next
1: loadw IP, 0, IP
jump _next
// Optimized loop constructs
CODE("(loop)", 0, doloop)
loadw RSP, 0, %l0
loadw RSP, 1, %l1
inc %l0
je %l0, %l1, 1f
storew RSP, 0, %l0
loadw IP, 0, IP
jump _next
1: add RSP, 4, RSP
add IP, 2, IP
jump _next
// ANS Forth +loop needs to terminate the loop when the loop index "cross[es]
// the boundary between the loop limit minus one and the loop limit." This
// allows +loop to be used with signed and unsigned numbers and with positive
// and negative increments, but it's a pain in the ass to implement correctly.
CODE("(+loop)", 0, doplusloop)
loadw RSP, 0, %l0
loadw RSP, 1, %l1
PS_POP(%l3)
store 0, %l4
jge %l0, %l1, 1f
jl %l3, 0, 2f
store 1, %l4
jump 2f
1: jg %l3, 0, 2f
store 2, %l4
2: add %l0, %l3, %l0
jl %l0, %l1, 3f
and %l4, 1, %l4
jump 4f
3: and %l4, 2, %l4
4: jnz %l4, 5f
storew RSP, 0, %l0
loadw IP, 0, IP
jump _next
5: add RSP, 4, RSP
add IP, 2, IP
jump _next
// Execute a Forth word on the parameter stack.
CODE("execute", 0, execute)
PS_POP(%l0)
jump _exec
CODE("@execute", 0, fetchexecute)
PS_POP(%l0)
loadw %l0, 0, %l0
jump _exec
// Macro to set up the dictionary entry etc. for hand-compiled Forth. Now that
// we have the inner interpreter written, we can write Forth-wise when
// convenient.
#define COLON(name, flags, label1) \
HEAD(name, flags, label1); \
.word @docol
// end macro definition
////
// Basic return stack manipulation operations.
CODE("rp!", 0, rpstore)
PS_POP(RSP)
jump _next
CODE("rp@", 0, rpfetch)
PS_PUSH(RSP)
jump _next
CODE("r>", 0, fromr)
RS_POP(%l0)
PS_PUSH(%l0)
jump _next
CODE("2r>", 0, twofromr)
RS_POP(%l0)
RS_POP(%l1)
PS_PUSH(%l1)
PS_PUSH(%l0)
jump _next
CODE("r@", 0, rfetch)
CODE("i", 0, loopi)
loadw RSP, 0, %l0
PS_PUSH(%l0)
jump _next
CODE("j", 0, loopj)
loadw RSP, 2, %l0
PS_PUSH(%l0)
jump _next
CODE("2r@", 0, tworfetch)
loadw RSP, 0, %l0
loadw RSP, 1, %l1
PS_PUSH(%l1)
PS_PUSH(%l0)
jump _next
CODE(">r", 0, tor)
PS_POP(%l0)
RS_PUSH(%l0)
jump _next
CODE("2>r", 0, twotor)
PS_POP(%l0)
PS_POP(%l1)
RS_PUSH(%l1)
RS_PUSH(%l0)
jump _next
CODE("rpick", 0, rpick)
loadw PSP, 0, %l0
loadw RSP, %l0, %l0
storew PSP, 0, %l0
jump _next
////
// Variable and user variable handling. Now that we have >R we can implement
// variable handling in a Forth-wise reproducible fashion.
// Interpreter for variables.
.section text
.align 4
dovar: add %l0, 2, %l0
PS_PUSH(%l0)
jump _next
// Macro to set up the dictionary entry etc. for a variable.
#define VARIABLE(name, flags, label1) \
HEAD(name, flags, label1); \
.word @dovar; \
.label var_ ## label1
// end macro definition
////
// Constants.
// Interpreter for constants.
.section text
.align 4
doconst:
loadw %l0, 1, %l0
PS_PUSH(%l0)
jump _next
// Macro to set up the dictionary entry etc. for a constant.
#define CONSTANT(name, flags, label1, value) \
HEAD(name, flags, label1); \
.word @doconst; \
.label const_ ## label1; \
.word value
// end macro definition
// Interpreter for 2constants.
.section text
.align 4
do2const:
loadw %l0, 1, %l1
loadw %l0, 2, %l0
PS_PUSH(%l0)
PS_PUSH(%l1)
jump _next
// Macro to set up the dictionary entry etc. for a 2constant.
#define TWOCONSTANT(name, flags, label1, value1, value2) \
HEAD(name, flags, label1); \
.word @do2const; \
.label const_ ## label1; \
.word value1; \
.word value2
// end macro definition
CONSTANT("0", 0, zero, 0)
CONSTANT("1", 0, one, 1)
CONSTANT("2", 0, two, 2)
CONSTANT("-1", 0, negativeone, -1)
CONSTANT("true", 0, true, -1)
CONSTANT("false", 0, false, 0)
CONSTANT("bl", 0, bl, 32)
CONSTANT("'\"'", 0, chardquote, "\"")
CONSTANT("')'", 0, charcloseparen, ")")
CONSTANT(".:", 0, dotcolon, @docol)
CONSTANT(".variable", 0, dotvariable, @dovar)
CONSTANT(".constant", 0, dotconstant, @doconst)
CONSTANT(".2constant", 0, dot2constant, @do2const)
CONSTANT(".user", 0, dotuser, @douser)
////
// User variable handling
CODE("up!", 0, upstore)
PS_POP(UP)
jump _next
CODE("up@", 0, upfetch)
PS_PUSH(UP)
jump _next
// Forth-level interpreter for user variables.
.section text
.align 4
douser:
loadw %l0, 1, %l0
add UP, %l0, %l0
PS_PUSH(%l0)
jump _next
// Macro to set up the dictionary entry etc. for a variable.
#define USER(name, flags, label1, initial) \
HEAD(name, flags, label1); \
.word @douser; \
.word _uoff; \
.set index_ ## label1, _uoff >> 1; \
.set _uoff, _uoff + 2; \
.set _here, .; \
.org _unext; \
.word initial; \
.set _unext, .; \
.org _here
// Macros to access user variables from assembly routines
#define USER_FETCH(label1, dst) \
loadw UP, index_ ## label1, dst
#define USER_STORE(label1, arg) \
storew UP, index_ ## label1, arg
USER("sp0", 0, sp0, pstack)
USER("rp0", 0, rp0, rstack)
USER("base", 0, base, 10)
USER("$source-id", 0, dsourceid, 0)
USER("'?key", 0, xtqkey, qrx)
USER("'accept", 0, xtaccept, acceptln)
USER("'emit", 0, xtemit, txstore)
USER("'echo", 0, xtecho, emit)
USER("'prompt", 0, xtprompt, ok)
USER("state", 0, state, 0)
USER("#tib", 0, ntib, 0)
HEAD("span", 0, span)
.word @douser
.word index_ntib << 1
USER("ctib", 0, ctib, tibtop)
USER(">in", 0, toin, 0)
USER("cp", 0, cp, dict_end)
USER("latest", 0, latest, entry_boot)
USER("$recurse", 0, drecurse, 0)
USER("state", 0, state, 0)
USER("$word", 0, dword, 0)
USER("#$word", 0, ndword, 0)
USER("hld", 0, hld, 0)
USER("handler", 0, handler, 0)
USER("bt", 0, bt, 0)
USER("$abort", 0, dabort, 0)
VARIABLE(">user", 0, touser)
.word _uoff
////
// Core parameter stack manipulation operations.
CODE("sp!", 0, spstore)
loadw PSP, 0, PSP;
jump _next
CODE("sp@", 0, spfetch)
store PSP, %l0
PS_PUSH(%l0)
jump _next
CODE("depth", 0, depth) // ( -- +n )
USER_FETCH(sp0, %l0)
sub %l0, PSP, %l0
art_shift %l0, -1, %l0
PS_PUSH(%l0)
jump _next
CODE("drop", 0, drop) // ( x1 -- )
add PSP, 2, PSP
jump _next
CODE("dup", 0, dup) // ( x1 -- x1 x1 )
loadw PSP, 0, %l0
PS_PUSH(%l0)
jump _next
CODE("?dup", 0, qdup) // ( x1 flag -- x1 | )
loadw PSP, 0, %l0
jz %l0, 1f
PS_PUSH(%l0)
1: jump _next
CODE("swap", 0, swap) // ( x1 x2 -- x2 x1 )
PS_POP(%l0)
loadw PSP, 0, %l1
storew PSP, 0, %l0
PS_PUSH(%l1)
jump _next
CODE("over", 0, over) // ( x1 x2 -- x1 x2 x1 )
loadw PSP, 1, %l0
PS_PUSH(%l0)
jump _next
CODE("rot", 0, rot) // ( x1 x2 x3 -- x2 x3 x1 )
PS_POP(%l2)
PS_POP(%l1)
loadw PSP, 0, %l0
storew PSP, 0, %l1
PS_PUSH(%l2)
PS_PUSH(%l0)
jump _next
CODE("rot-", 0, rotminus) // ( x1 x2 x3 -- x3 x1 x2 )
PS_POP(%l2)
PS_POP(%l1)
loadw PSP, 0, %l0
storew PSP, 0, %l2
PS_PUSH(%l0)
PS_PUSH(%l1)
jump _next
CODE("nip", 0, nip) // ( x1 x2 -- x2 )
PS_POP(%l0)
storew PSP, 0, %l0
jump _next
CODE("tuck", 0, tuck) // ( x1 x2 -- x2 x1 x2 )
loadw PSP, 0, %l1
loadw PSP, 1, %l0
storew PSP, 1, %l1
storew PSP, 0, %l0
PS_PUSH(%l1)
jump _next
CODE("roll", 0, roll) // ( xu xu-1 .. x0 u -- xu-1 .. x0 xu )
CODE("cs-roll", 0, csroll)
PS_POP(%l0)
je %l0, 0, _next
loadw PSP, %l0, %l3
1: store %l0, %l1
dec %l0
loadw PSP, %l0, %l2
storew PSP, %l1, %l2
jne %l0, 0, 1b
2: storew PSP, 0, %l3
jump _next
CODE("pick", 0, pick) // ( xu .. x0 u -- xu .. x0 xu )
CODE("cs-pick", 0, cspick)
loadw PSP, 0, %l0
inc %l0
loadw PSP, %l0, %l0
storew PSP, 0, %l0
jump _next
CODE("2drop", 0, twodrop) // ( x1 x2 -- )
add PSP, 4, PSP
jump _next
CODE("2dup", 0, twodup) // ( x1 x2 -- x1 x2 x1 x2 )
loadw PSP, 1, %l0
PS_PUSH(%l0)
loadw PSP, 1, %l0
PS_PUSH(%l0)
jump _next
CODE("2over", 0, twoover) // ( x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2 )
loadw PSP, 2, %l1
loadw PSP, 3, %l0
PS_PUSH(%l0)
PS_PUSH(%l1)
jump _next
CODE("2swap", 0, twoswap) // ( x1 x2 x3 x4 -- x3 x4 x1 x2 )
loadw PSP, 0, %l3
loadw PSP, 1, %l2
loadw PSP, 2, %l1
loadw PSP, 3, %l0
storew PSP, 3, %l2
storew PSP, 2, %l3
storew PSP, 1, %l0
storew PSP, 0, %l1
jump _next
CODE("2rot", 0, tworot) // ( xd1 xd2 xd3 -- xd2 xd3 xd1 )
loadw PSP, 0, %l5
loadw PSP, 1, %l4
loadw PSP, 2, %l3
loadw PSP, 3, %l2
loadw PSP, 4, %l1
loadw PSP, 5, %l0
storew PSP, 5, %l2
storew PSP, 4, %l3
storew PSP, 3, %l4
storew PSP, 2, %l5
storew PSP, 1, %l0
storew PSP, 0, %l1
jump _next
////
// Basic memory manipulation operations.
CODE("!", 0, store) // ( x a-addr -- )
PS_POP(%l1)
PS_POP(%l0)
storew %l1, 0, %l0
jump _next
CODE("2!", 0, twostore) // ( x1 x2 a-addr -- )
PS_POP(%l2)
PS_POP(%l1)
PS_POP(%l0)
storew %l2, 0, %l1
storew %l2, 1, %l0
jump _next
CODE("@", 0, fetch)
loadw PSP, 0, %l0
loadw %l0, 0, %l0
storew PSP, 0, %l0
jump _next
CODE("2@", 0, twofetch)
loadw PSP, 0, %l0
loadw %l0, 0, %l1
loadw %l0, 1, %l0
storew PSP, 0, %l0
PS_PUSH(%l1)
jump _next
CODE("c!", 0, cstore) // ( char c-addr -- )
PS_POP(%l0)
PS_POP(%l1)
storeb %l0, 0, %l1
jump _next
CODE("c@", 0, cfetch)
loadw PSP, 0, %l0
loadb %l0, 0, %l0
storew PSP, 0, %l0
jump _next
CODE("cmove", 0, cmove) // ( c-addr1 c-addr2 u -- )
PS_POP(%l2)
PS_POP(%l1)
PS_POP(%l0)
_cmove: store 0, %l3
je %l3, %l2, _next
1: loadb %l0, %l3, %l4
storeb %l1, %l3, %l4
inc %l3
jne %l3, %l2, 1b
jump _next
CODE("cmove>", 0, cmovehigh) // ( c-addr1 c-addr2 u -- )
PS_POP(%l2)
PS_POP(%l1)
PS_POP(%l0)
_cmovehigh:
jz %l2, _next
1: dec %l2
loadb %l0, %l2, %l3
storeb %l1, %l2, %l3
jnz %l2, 1b
jump _next
CODE("move", 0, move) // ( addr1 addr2 u -- )
PS_POP(%l2)
PS_POP(%l1)
PS_POP(%l0)
_move: jz %l1, 1f
jl %l2, 0, 1f
copy_table %l0, %l1, %l2
jump _next
1: sub %l0, 32768, %l3
sub %l1, 32768, %l4
jl %l3, %l4, _cmovehigh
jump _cmove
CODE("+!", 0, plusstore) // ( n|u addr -- )
PS_POP(%l1)
PS_POP(%l0)
loadw %l1, 0, %l2
add %l0, %l2, %l0
storew %l1, 0, %l0
jump _next
////
// Bitwise operations.
CODE("invert", 0, invert)
loadw PSP, 0, %l0
not %l0, %l0
storew PSP, 0, %l0
jump _next
CODE("and", 0, and)
PS_POP(%l0)
loadw PSP, 0, %l1
and %l0, %l1, %l0
storew PSP, 0, %l0
jump _next
CODE("or", 0, or)
PS_POP(%l0)
loadw PSP, 0, %l1
or %l0, %l1, %l0
storew PSP, 0, %l0
jump _next
// Bitwise exclusive or. For some reason, the Z-machine does not have a native
// operation for xor, so we need to synthesize it.
CODE("xor", 0, xor)
PS_POP(%l0)
not %l0, %l1
loadw PSP, 0, %l2
not %l2, %l3
and %l0, %l3, %l0
and %l2, %l1, %l2
or %l0, %l2, %l0
storew PSP, 0, %l0
jump _next
CODE("lshift", 0, lshift)
PS_POP(%l1)
loadw PSP, 0, %l0
log_shift %l0, %l1, %l0
storew PSP, 0, %l0
jump _next
CODE("rshift", 0, rshift)
PS_POP(%l1)
sub 0, %l1, %l1
loadw PSP, 0, %l0
log_shift %l0, %l1, %l0
storew PSP, 0, %l0
jump _next
////
// Logical comparisons.
CODE("0=", 0, zeroeq)
CODE("not", 0, not)
loadw PSP, 0, %l0
jz %l0, 1f
storew PSP, 0, 0
jump _next
1: storew PSP, 0, -1
jump _next
CODE("=", 0, eq)
PS_POP(%l0)
loadw PSP, 0, %l1
je %l0, %l1, 1f
storew PSP, 0, 0
jump _next
1: storew PSP, 0, -1
jump _next
CODE("0<>", 0, zeroneq)
loadw PSP, 0, %l0
jnz %l0, 1f
storew PSP, 0, 0
jump _next
1: storew PSP, 0, -1
jump _next
CODE("<>", 0, neq)
PS_POP(%l0)
loadw PSP, 0, %l1
jne %l0, %l1, 1f
storew PSP, 0, 0
jump _next
1: storew PSP, 0, -1
jump _next
CODE("0<", 0, zerolt)
loadw PSP, 0, %l0
jl %l0, 0, 1f
storew PSP, 0, 0
jump _next
1: storew PSP, 0, -1
jump _next
CODE("<", 0, lt)
PS_POP(%l1)
loadw PSP, 0, %l0
jl %l0, %l1, 1f
storew PSP, 0, 0
jump _next
1: storew PSP, 0, -1
jump _next
CODE("u<", 0, ult)
PS_POP(%l1)
loadw PSP, 0, %l0
_ult: sub %l0, 32768, %l2
sub %l1, 32768, %l3
jl %l2, %l3, 1f
storew PSP, 0, 0
jump _next
1: storew PSP, 0, -1
jump _next
CODE("0>", 0, zerogt)
loadw PSP, 0, %l0
jg %l0, 0, 1f
storew PSP, 0, 0
jump _next
1: storew PSP, 0, -1
jump _next
CODE(">", 0, gt)
PS_POP(%l1)
loadw PSP, 0, %l0
jg %l0, %l1, 1f
storew PSP, 0, 0
jump _next
1: storew PSP, 0, -1
jump _next
CODE("u>", 0, ugt)
PS_POP(%l1)
loadw PSP, 0, %l0
_ugt: sub %l0, 32768, %l2
sub %l1, 32768, %l3
jg %l2, %l3, 1f
storew PSP, 0, 0
jump _next
1: storew PSP, 0, -1
jump _next
CODE("0<=", 0, zerolteq)
loadw PSP, 0, %l0
jle %l0, 0, 1f
storew PSP, 0, 0
jump _next
1: storew PSP, 0, -1
jump _next
CODE("<=", 0, lteq)
PS_POP(%l1)
loadw PSP, 0, %l0
jle %l0, %l1, 1f
storew PSP, 0, 0
jump _next
1: storew PSP, 0, -1
jump _next
CODE("0>=", 0, zerogteq)
loadw PSP, 0, %l0
jge %l0, 0, 1f
storew PSP, 0, 0
jump _next
1: storew PSP, 0, -1
jump _next
CODE(">=", 0, gteq)
PS_POP(%l1)
loadw PSP, 0, %l0
jge %l0, %l1, 1f
storew PSP, 0, 0
jump _next
1: storew PSP, 0, -1
jump _next
CODE("min", 0, min)
PS_POP(%l1)
loadw PSP, 0, %l0
jge %l1, %l0, _next
storew PSP, 0, %l1
jump _next
CODE("max", 0, max)
PS_POP(%l0)
loadw PSP, 0, %l1
jle %l0, %l1, _next
storew PSP, 0, %l0
jump _next
// Check if first arg is between the other two.
CODE("within", 0, within) // ( n1|u1 n2|u2 n3|u3 -- flag)
PS_POP(%l2)
PS_POP(%l1)
loadw PSP, 0, %l0
sub %l0, %l1, %l0
sub %l2, %l1, %l1
jump _ult
////
// Arithmetic
CODE("+", 0, plus)
PS_POP(%l1)
loadw PSP, 0, %l0
add %l0, %l1, %l0
storew PSP, 0, %l0
jump _next
CODE("1+", 0, oneplus)
CODE("char+", 0, charplus)
loadw PSP, 0, %l0
add %l0, 1, %l0
storew PSP, 0, %l0
jump _next
CODE("-", 0, minus)
PS_POP(%l1)
loadw PSP, 0, %l0
sub %l0, %l1, %l0
storew PSP, 0, %l0
jump _next
CODE("1-", 0, oneminus)
loadw PSP, 0, %l0
sub %l0, 1, %l0
storew PSP, 0, %l0
jump _next
CODE("*", 0, mul)
PS_POP(%l0)
loadw PSP, 0, %l1
mul %l0, %l1, %l0
storew PSP, 0, %l0
jump _next
CODE("/", 0, slash)
PS_POP(%l1)
loadw PSP, 0, %l0
div %l0, %l1, %l2
storew PSP, 0, %l2
jump _next
CODE("mod", 0, mod)
PS_POP(%l1)
loadw PSP, 0, %l0
mod %l0, %l1, %l0
storew PSP, 0, %l0
jump _next
CODE("/mod", 0, slashmod)
PS_POP(%l1)
loadw PSP, 0, %l0
div %l0, %l1, %l2
mod %l0, %l1, %l0
storew PSP, 0, %l0
PS_PUSH(%l2)
jump _next
CODE("negate", 0, negate)
loadw PSP, 0, %l0
sub 0, %l0, %l0
storew PSP, 0, %l0
jump _next
CODE("abs", 0, abs)
loadw PSP, 0, %l0
jge %l0, 0, 1f
sub 0, %l0, %l0
storew PSP, 0, %l0
1: jump _next
CODE("2*", 0, twomul)
loadw PSP, 0, %l0
art_shift %l0, 1, %l0
storew PSP, 0, %l0
jump _next
CODE("2/", 0, twoslash)
loadw PSP, 0, %l0
art_shift %l0, -1, %l0
storew PSP, 0, %l0
jump _next
CODE("4*", 0, fourmul)
loadw PSP, 0, %l0
art_shift %l0, 2, %l0
storew PSP, 0, %l0
jump _next
CODE("4/", 0, fourslash)
loadw PSP, 0, %l0
art_shift %l0, -2, %l0
storew PSP, 0, %l0
jump _next
////
// Double-length integer arithmetic (and helpers)
CODE("d>s", 0, dtos) // ( d -- n )