Quote, and unquote,: Machine Forth Primitives

00 Title Card

quote, and unquote,

Machine Forth Primitives

For RISC and Position-Independent Architectures

Samuel A. Falvo II <kc5tja@arrl.net>

2024 September 28


01 The Problem

    defer, _start

    :, _emit ( n - )
        $2A x0 a7 ori,  ecall,  ;,

    :, .h  CHAR h x0 a0 ori, _emit ;,
    :, .e  CHAR e x0 a0 ori, _emit ;,
    :, .l  CHAR l x0 a0 ori, _emit ;,
    :, .o  CHAR o x0 a0 ori, _emit ;,
    
    :, .hello ( - )
        .h .e .l .l .o cr ;,

    ' .hello >BODY @ is, _start

02 Why

    ORI Xn,X0,$ABC  ; only 12 bit immediate
    LUI Xn,$ABCDF000    ; Upper 20 bits loaded as immediate
    ADDI Xn,Xn,$F12     ; Lower 12 bits merged in
    ; BTW, why $ABCDF000 and not $ABCDE000?  ;-)

03 Solution = Work Around

    JAL X0,branchTarget  ; Unconditional JUMP to branchTarget
    JAL X1,branchTarget  ; Unconditional CALL to branchTarget
    ...
    JAL X31,branchTarget  ; Unconditional CALL to branchTarget
    JAL X4,forwardLabel
    .word arbitrary-data-goes-here  ; guaranteed 4-byte alignment!
    .byte padding-if-required
forwardLabel:
    ORI X5,X0,size-of-data-goes-here

04 Solution = Work Around

: defer, ( - )                         : S", ( - )
  CREATE 0 quote, , ( JAL X0,... )       nextReg quote,
  DOES>  @ THERE - ra jal, ;             ( ..place string at HERE.. )
                                         nextReg unquote, ;
: is, ( a - )
  ' >BODY @ SWAP OVER ( aJAL a aJAL )
  -  ( aJAL disp )
  SWAP mergeU ;

05 Implementation

: quote, ( xr -- aJAL )
  ( Jumps AHEAD, except sets xr to point at code immediately following. )
  THERE SWAP 0 SWAP jal, ;
  
: unquote, ( aJAL xr -- )
  ( Resolves prior quote, and sets xr to length of block spanned. )
  OVER THERE SWAP - 4 - ( aJAL xr len; 4 accounts for JAL insn )
  TALIGN >R >R ( aJAL || xr len )
  THERE OVER - ( aJAL disp || xr len )
  OVER mergeU R> R> ( xr len )
  OVER IF
    SWAP x0 SWAP ori,
  ELSE
    2DROP
  THEN ;

06 Why Use Quote and Unquote

    S", Hello world" _type
    vectorAddr vectorLength (<< ( a b - f ) 2DUP U< >>) SortVector
    \     Code quotation ---^-------------------------^
:, myState ( - )
  gp quote, /MY-STATE TALLOT TALIGN x0 unquote, ;,

07 Alternatives


08 The End

Thank you for watching; I’ll be happy to answer questions via the Generalities mailing list.