Permalink
Browse files

jonesforth.s: add ;CODE

Now we can make machine code definitions in Forth. I've
decided to make all machine code emitters start with $
(such as $NEXT), although this may change in the future.

Signed-off-by: Andrei Warkentin <andrey.warkentin@gmail.com>
  • Loading branch information...
andreiw committed Jun 24, 2015
1 parent 79ab55e commit ac99b06e4b722926f0b5a5d31e97ba91eba6f4e4
Showing with 36 additions and 3 deletions.
  1. +2 −0 doc/forth.md
  2. +34 −3 jonesforth.s
  3. BIN kernel.img
View
@@ -192,6 +192,8 @@ so you can see related words together.
| `]` | ( -- ) | change interpreter state to Compilation mode |
| `: name` | ( -- ) | define (compile) a new FORTH word |
| `;` | ( -- ) | end FORTH word definition |
+| `;CODE` | ( -- ) end FORTH machine code word definition |
+| `$NEXT` | ( -- ) emit machine code to run the next word |
| `IMMEDIATE` | ( -- ) | set IMMEDIATE flag of last defined word |
| `HIDDEN` | ( entry -- ) | toggle HIDDEN flag of a word |
| `HIDE word` | ( -- ) | hide definition of following word |
View
@@ -126,9 +126,14 @@ _DOCOL:
@ 3. begins executing the routine pointed to
@ by the CFA, with the CFA in r0
_NEXT:
- ldr r0, [FIP], #4
- ldr r1, [r0]
- bx r1
+@ This is done like so that ASMNEXT doesn't need to
+@ be kept in sync with _NEXT definition.
+ .macro NEXT_BODY, wrap_insn:vararg=
+ \wrap_insn ldr r0, [FIP], #4
+ \wrap_insn ldr r1, [r0]
+ \wrap_insn bx r1
+ .endm
+ NEXT_BODY
@ cold_start is used to bootstrap the interpreter,
@ the first word executed is QUIT
@@ -1742,6 +1747,32 @@ defcode "MONITOR",,MONITOR
bl monitor @ monitor();
NEXT
+@
+@ $NEXT ( -- ) emits the _NEXT body at HERE, to be used
+@ in ;CODE or ;CODE-defined words.
+@
+defword "$NEXT",F_IMM,ASMNEXT
+ .macro COMPILE_INSN, insn:vararg
+ .int LIT
+ \insn
+ .int COMMA
+ .endm
+ NEXT_BODY COMPILE_INSN
+ .int EXIT
+ .purgem COMPILE_INSN
+
+@
+@ Finishes a machine code colon definition in Forth, as
+@ really basic assembler.
+@
+defword ";CODE",F_IMM,SEMICODE
+ .int ASMNEXT @ end the word with NEXT macro
+ .int LATEST, FETCH, DUP @ LATEST points to the compiled word
+ .int HIDDEN @ unhide the compiled word
+ .int DUP, TDFA, SWAP, TCFA, STORE @ set codeword to data instead of DOCOL
+ .int LBRAC @ just like ";" exit compile mode
+ .int EXIT
+
@ EXECUTE ( xt -- ) jump to the address on the stack
@-- WARNING! THIS MUST BE THE LAST WORD DEFINED IN ASSEMBLY (see LATEST) --@
defcode "EXECUTE",,EXECUTE
View
Binary file not shown.

0 comments on commit ac99b06

Please sign in to comment.