summaryrefslogtreecommitdiffstats
path: root/qemu/roms/SLOF/slof/fs/fcode/core.fs
blob: 8fd98ec1986182b9e597e9faa7718b85ca8ac5d0 (plain)
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
\ *****************************************************************************
\ * Copyright (c) 2004, 2008 IBM Corporation
\ * All rights reserved.
\ * This program and the accompanying materials
\ * are made available under the terms of the BSD License
\ * which accompanies this distribution, and is available at
\ * http://www.opensource.org/licenses/bsd-license.php
\ *
\ * Contributors:
\ *     IBM Corporation - initial implementation
\ ****************************************************************************/

: ?offset16 ( -- true|false )
  fcode-offset 2 =
  ;

: ?arch64 ( -- true|false )
  cell 8 =
  ;

: ?bigendian ( -- true|false )
  deadbeef fcode-num !
  fcode-num ?arch64 IF 4 + THEN
  c@ de =
  ;

: reset-fcode-end ( -- )
  false fcode-end !
  ;

: get-ip ( -- n )
  ip @
  ;

: set-ip ( n -- )
  ip !
  ;

: next-ip ( -- )
  get-ip 1+ set-ip
  ;

: jump-n-ip ( n -- )
  get-ip + set-ip
  ;

: read-byte ( -- n )
  get-ip fcode-rb@
  ;

: ?compile-mode ( -- on|off )
  state @
  ;

: save-evaluator-state
  get-ip               eva-debug? IF ." saved ip "           dup . cr THEN
  fcode-end @          eva-debug? IF ." saved fcode-end "    dup . cr THEN
  fcode-offset         eva-debug? IF ." saved fcode-offset " dup . cr THEN
\ local fcodes are currently NOT saved!
  fcode-spread         eva-debug? IF ." saved fcode-spread " dup . cr THEN
  ['] fcode@ behavior  eva-debug? IF ." saved fcode@ "       dup . cr THEN
  ;

: restore-evaluator-state
  eva-debug? IF ." restored fcode@ "       dup . cr THEN  to fcode@
  eva-debug? IF ." restored fcode-spread " dup . cr THEN  to fcode-spread
\ local fcodes are currently NOT restored!
  eva-debug? IF ." restored fcode-offset " dup . cr THEN  to fcode-offset
  eva-debug? IF ." restored fcode-end "    dup . cr THEN  fcode-end !
  eva-debug? IF ." restored ip "           dup . cr THEN  set-ip
  ;

: token-table-index ( fcode# -- addr )
  cells token-table +
  ;

: join-immediate ( xt immediate? addr -- xt+immediate? addr )
  -rot + swap
  ;

: split-immediate ( xt+immediate? -- xt immediate? )
  dup 1 and 2dup - rot drop swap
  ;

: literal, ( n -- )
  postpone literal
  ;

: fc-string,
  postpone sliteral
  dup c, bounds ?do i c@ c, loop
  ;

: set-token ( xt immediate? fcode# -- )
  token-table-index join-immediate !
  ;

: get-token ( fcode# -- xt immediate? )
  token-table-index @ split-immediate
  ;

( ---------------------------------------------------- )

#include "little-big.fs"

( ---------------------------------------------------- )

: read-fcode# ( -- FCode# )
  read-byte
  dup 01 0F between IF drop read-fcode-num16 THEN
  ;

: read-header ( adr -- )
  next-ip read-byte        drop
  next-ip read-fcode-num16 drop
  next-ip read-fcode-num32 drop
  ;

: read-fcode-string ( -- str len )
  read-byte            \ get string length ( -- len )
  next-ip get-ip       \ get string addr   ( -- len str )
  swap                 \ type needs the parameters swapped ( -- str len )
  dup 1- jump-n-ip     \ jump to the end of the string in FCode
  ;


-1 VALUE break-fcode-addr
0 VALUE break-fcode-steps

: evaluate-fcode ( -- )
   BEGIN
      get-ip break-fcode-addr = IF
         TRUE fcode-end !
      THEN
      fcode-end @ 0=
   WHILE
      fcode@                               ( fcode# )
      eva-debug? IF
         dup
         get-ip 8 u.r ." : "
         ." [" 3 u.r ." ] "
      THEN
      \ When it is not immediate and in compile-mode, then compile
      get-token 0= ?compile-mode AND IF    ( xt )
         compile,
      ELSE                                 \ immediate or "interpretation" mode
         eva-debug? IF dup xt>name type space THEN
         execute
      THEN
      eva-debug? IF .s cr THEN
      break-fcode-steps IF
         break-fcode-steps 1- TO break-fcode-steps
         break-fcode-steps 0= IF
            TRUE fcode-end !
         THEN
      THEN
      next-ip
   REPEAT
;

\ Run FCODE for n steps
: steps-fcode  ( n -- )
   to break-fcode-steps
   break-fcode-addr >r -1 to break-fcode-addr
   reset-fcode-end
   evaluate-fcode
   r> to break-fcode-addr
;

\ Step through one FCODE instruction
: step-fcode  ( -- )
   1 steps-fcode
;