commit 63187ee6f2f465240f4c08c095ee834df6049baf
parent b5a94e81d55c06ada9fc70de3a50b8f843130447
Author: Christian Ermann <christianermann@gmail.com>
Date: Sun, 10 Nov 2024 13:44:14 -0800
Add 'inline' to define new primitive words
Diffstat:
| M | forth.f | | | 71 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | forth.s | | | 21 | +++++++++++++++++++++ |
2 files changed, 92 insertions(+), 0 deletions(-)
diff --git a/forth.f b/forth.f
@@ -149,3 +149,74 @@ t{ 1 1 + -> 2 }t
type cr
then
repeat ;
+
+: decimal 10 base ! ; immediate
+: hex 16 base ! ; immediate
+
+: true -1 ;
+: false 0 ;
+
+: next=
+ \ is 'next' stored at 'addr'?
+ ( addr -- flag )
+ dup @ hex 0004A503 = if
+ 1 cells + dup @ hex 00052583 = if
+ 1 cells + dup @ hex 00448493 = if
+ 1 cells + dup @ hex 00058067 = if
+ drop true exit
+ then then then then
+ drop false exit ;
+
+: opcode @ hex 7F and ;
+: jal= opcode hex 6F = ;
+
+: decode-immed-j
+ ( instr -- n )
+ dup hex 80000000 and
+ over hex 000FF000 and decimal 11 lshift +
+ over hex 00100000 and decimal 2 lshift +
+ over hex 7FE00000 and decimal 9 rshift +
+ 11 ashift nip ;
+
+: encode-immed-j
+ ( n -- immed )
+ hex 800FFFFF and \ clear imm[10:1], imm[11] destination
+ dup hex 7FE and decimal 20 lshift + \ imm[10:1]
+ dup hex 800 and decimal 9 lshift + \ imm[11]
+ hex FFFFF000 and ; \ clear junk from rd, opcode
+
+: reloc-jal
+ \ relocate a 'jal' instruction from 'addr' to 'here'
+ ( addr -- instr )
+ dup @
+ decode-immed-j
+ over + here -
+ encode-immed-j
+ over @ hex 0FFF and +
+ nip ;
+
+: :code
+ create
+ here -1 cells allot ,
+ latest @ hidden
+ ] ;
+
+: ;code
+ hex 0004A503 ,
+ hex 00052583 ,
+ hex 00448493 ,
+ hex 00058067 ,
+ latest @ hidden
+ postpone [ ; immediate
+
+: inline
+ parse-word find >cfa @
+ begin
+ dup next= 0=
+ while
+ dup jal= if dup reloc-jal else
+ dup @
+ then
+ , 1 cells +
+ repeat
+ drop ; immediate
diff --git a/forth.s b/forth.s
@@ -746,6 +746,27 @@ defcode "and", and_, 0x0F29C2A6
push w
next
+defcode "lshift", lshift, 0x8DA53719
+ pop w
+ pop x
+ sll x, x, w
+ push x
+ next
+
+defcode "rshift", rshift, 0x86294EF7
+ pop w
+ pop x
+ srl x, x, w
+ push x
+ next
+
+defcode "ashift", ashift, 0x46A0EC68
+ pop w
+ pop x
+ sra x, x, w
+ push x
+ next
+
defcode "aligned", aligned, 0xC73174DF
pop w
addi w, w, 3