| 1 : |
anton
|
1.16
|
\ converts primitives to, e.g., C code |
| 2 : |
|
|
|
| 3 : |
anton
|
1.134
|
\ Copyright (C) 1995,1996,1997,1998,2000,2003 Free Software Foundation, Inc. |
| 4 : |
anton
|
1.16
|
|
| 5 : |
|
|
\ This file is part of Gforth. |
| 6 : |
|
|
|
| 7 : |
|
|
\ Gforth is free software; you can redistribute it and/or |
| 8 : |
|
|
\ modify it under the terms of the GNU General Public License |
| 9 : |
|
|
\ as published by the Free Software Foundation; either version 2 |
| 10 : |
|
|
\ of the License, or (at your option) any later version. |
| 11 : |
|
|
|
| 12 : |
|
|
\ This program is distributed in the hope that it will be useful, |
| 13 : |
|
|
\ but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 : |
|
|
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 : |
|
|
\ GNU General Public License for more details. |
| 16 : |
|
|
|
| 17 : |
|
|
\ You should have received a copy of the GNU General Public License |
| 18 : |
|
|
\ along with this program; if not, write to the Free Software |
| 19 : |
anton
|
1.48
|
\ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
| 20 : |
anton
|
1.16
|
|
| 21 : |
|
|
|
| 22 : |
anton
|
1.71
|
\ This is not very nice (hard limits, no checking, assumes 1 chars = 1). |
| 23 : |
|
|
\ And it grew even worse when it aged. |
| 24 : |
anton
|
1.1
|
|
| 25 : |
|
|
\ Optimizations: |
| 26 : |
|
|
\ superfluous stores are removed. GCC removes the superfluous loads by itself |
| 27 : |
|
|
\ TOS and FTOS can be kept in register( variable)s. |
| 28 : |
|
|
\ |
| 29 : |
|
|
\ Problems: |
| 30 : |
|
|
\ The TOS optimization is somewhat hairy. The problems by example: |
| 31 : |
|
|
\ 1) dup ( w -- w w ): w=TOS; sp-=1; sp[1]=w; TOS=w; |
| 32 : |
|
|
\ The store is not superfluous although the earlier opt. would think so |
| 33 : |
|
|
\ Alternatively: sp[0]=TOS; w=TOS; sp-=1; TOS=w; |
| 34 : |
|
|
\ 2) ( -- .. ): sp[0] = TOS; ... /* This additional store is necessary */ |
| 35 : |
|
|
\ 3) ( .. -- ): ... TOS = sp[0]; /* as well as this load */ |
| 36 : |
|
|
\ 4) ( -- ): /* but here they are unnecessary */ |
| 37 : |
|
|
\ 5) Words that call NEXT themselves have to be done very carefully. |
| 38 : |
|
|
\ |
| 39 : |
|
|
\ To do: |
| 40 : |
pazsan
|
1.8
|
\ add the store optimization for doubles |
| 41 : |
anton
|
1.1
|
\ regarding problem 1 above: It would be better (for over) to implement |
| 42 : |
|
|
\ the alternative |
| 43 : |
anton
|
1.80
|
\ store optimization for combined instructions. |
| 44 : |
|
|
|
| 45 : |
|
|
\ Design Uglyness: |
| 46 : |
|
|
|
| 47 : |
|
|
\ - global state (values, variables) in connection with combined instructions. |
| 48 : |
|
|
|
| 49 : |
|
|
\ - index computation is different for instruction-stream and the |
| 50 : |
|
|
\ stacks; there are two mechanisms for dealing with that |
| 51 : |
|
|
\ (stack-in-index-xt and a test for stack==instruction-stream); there |
| 52 : |
|
|
\ should be only one. |
| 53 : |
anton
|
1.1
|
|
| 54 : |
pazsan
|
1.3
|
warnings off |
| 55 : |
|
|
|
| 56 : |
jwilke
|
1.97
|
[IFUNDEF] try |
| 57 : |
|
|
include startup.fs |
| 58 : |
|
|
[THEN] |
| 59 : |
|
|
|
| 60 : |
anton
|
1.49
|
: struct% struct ; \ struct is redefined in gray |
| 61 : |
|
|
|
| 62 : |
pazsan
|
1.98
|
warnings off |
| 63 : |
anton
|
1.110
|
\ warnings on |
| 64 : |
pazsan
|
1.98
|
|
| 65 : |
jwilke
|
1.39
|
include ./gray.fs |
| 66 : |
anton
|
1.133
|
128 constant max-effect \ number of things on one side of a stack effect |
| 67 : |
anton
|
1.71
|
4 constant max-stacks \ the max. number of stacks (including inst-stream). |
| 68 : |
anton
|
1.1
|
255 constant maxchar |
| 69 : |
|
|
maxchar 1+ constant eof-char |
| 70 : |
anton
|
1.17
|
#tab constant tab-char |
| 71 : |
|
|
#lf constant nl-char |
| 72 : |
anton
|
1.1
|
|
| 73 : |
anton
|
1.18
|
variable rawinput \ pointer to next character to be scanned |
| 74 : |
|
|
variable endrawinput \ pointer to the end of the input (the char after the last) |
| 75 : |
|
|
variable cookedinput \ pointer to the next char to be parsed |
| 76 : |
anton
|
1.17
|
variable line \ line number of char pointed to by input |
| 77 : |
anton
|
1.65
|
variable line-start \ pointer to start of current line (for error messages) |
| 78 : |
|
|
0 line ! |
| 79 : |
anton
|
1.17
|
2variable filename \ filename of original input file |
| 80 : |
|
|
0 0 filename 2! |
| 81 : |
anton
|
1.111
|
2variable out-filename \ filename of the output file (for sync lines) |
| 82 : |
|
|
0 0 out-filename 2! |
| 83 : |
pazsan
|
1.25
|
2variable f-comment |
| 84 : |
|
|
0 0 f-comment 2! |
| 85 : |
anton
|
1.17
|
variable skipsynclines \ are sync lines ("#line ...") invisible to the parser? |
| 86 : |
anton
|
1.111
|
skipsynclines on |
| 87 : |
|
|
variable out-nls \ newlines in output (for output sync lines) |
| 88 : |
|
|
0 out-nls ! |
| 89 : |
anton
|
1.112
|
variable store-optimization \ use store optimization? |
| 90 : |
|
|
store-optimization off |
| 91 : |
|
|
|
| 92 : |
anton
|
1.116
|
variable include-skipped-insts |
| 93 : |
|
|
\ does the threaded code for a combined instruction include the cells |
| 94 : |
|
|
\ for the component instructions (true) or only the cells for the |
| 95 : |
|
|
\ inline arguments (false) |
| 96 : |
|
|
include-skipped-insts off |
| 97 : |
anton
|
1.1
|
|
| 98 : |
anton
|
1.121
|
variable immarg \ values for immediate arguments (to be used in IMM_ARG macros) |
| 99 : |
|
|
$12340000 immarg ! |
| 100 : |
|
|
|
| 101 : |
anton
|
1.72
|
: th ( addr1 n -- addr2 ) |
| 102 : |
|
|
cells + ; |
| 103 : |
|
|
|
| 104 : |
|
|
: holds ( addr u -- ) |
| 105 : |
|
|
\ like HOLD, but for a string |
| 106 : |
|
|
tuck + swap 0 +do |
| 107 : |
|
|
1- dup c@ hold |
| 108 : |
|
|
loop |
| 109 : |
|
|
drop ; |
| 110 : |
anton
|
1.71
|
|
| 111 : |
anton
|
1.82
|
: insert-wordlist { c-addr u wordlist xt -- } |
| 112 : |
anton
|
1.81
|
\ adds name "addr u" to wordlist using defining word xt |
| 113 : |
|
|
\ xt may cause additional stack effects |
| 114 : |
|
|
get-current >r wordlist set-current |
| 115 : |
|
|
c-addr u nextname xt execute |
| 116 : |
|
|
r> set-current ; |
| 117 : |
|
|
|
| 118 : |
anton
|
1.1
|
: start ( -- addr ) |
| 119 : |
anton
|
1.18
|
cookedinput @ ; |
| 120 : |
anton
|
1.1
|
|
| 121 : |
|
|
: end ( addr -- addr u ) |
| 122 : |
anton
|
1.18
|
cookedinput @ over - ; |
| 123 : |
anton
|
1.1
|
|
| 124 : |
anton
|
1.71
|
: print-error-line ( -- ) |
| 125 : |
|
|
\ print the current line and position |
| 126 : |
|
|
line-start @ endrawinput @ over - 2dup nl-char scan drop nip ( start end ) |
| 127 : |
|
|
over - type cr |
| 128 : |
|
|
line-start @ rawinput @ over - typewhite ." ^" cr ; |
| 129 : |
|
|
|
| 130 : |
|
|
: ?print-error { f addr u -- } |
| 131 : |
|
|
f ?not? if |
| 132 : |
|
|
outfile-id >r try |
| 133 : |
|
|
stderr to outfile-id |
| 134 : |
|
|
filename 2@ type ." :" line @ 0 .r ." : " addr u type cr |
| 135 : |
|
|
print-error-line |
| 136 : |
|
|
0 |
| 137 : |
|
|
recover endtry |
| 138 : |
|
|
r> to outfile-id throw |
| 139 : |
anton
|
1.111
|
1 (bye) \ abort |
| 140 : |
anton
|
1.71
|
endif ; |
| 141 : |
|
|
|
| 142 : |
anton
|
1.63
|
: quote ( -- ) |
| 143 : |
|
|
[char] " emit ; |
| 144 : |
|
|
|
| 145 : |
anton
|
1.111
|
\ count output lines to generate sync lines for output |
| 146 : |
|
|
|
| 147 : |
|
|
: count-nls ( addr u -- ) |
| 148 : |
|
|
bounds u+do |
| 149 : |
|
|
i c@ nl-char = negate out-nls +! |
| 150 : |
|
|
loop ; |
| 151 : |
|
|
|
| 152 : |
|
|
:noname ( addr u -- ) |
| 153 : |
|
|
2dup count-nls |
| 154 : |
|
|
defers type ; |
| 155 : |
|
|
is type |
| 156 : |
|
|
|
| 157 : |
anton
|
1.72
|
variable output \ xt ( -- ) of output word for simple primitives |
| 158 : |
|
|
variable output-combined \ xt ( -- ) of output word for combined primitives |
| 159 : |
anton
|
1.1
|
|
| 160 : |
anton
|
1.49
|
struct% |
| 161 : |
anton
|
1.71
|
cell% field stack-number \ the number of this stack |
| 162 : |
anton
|
1.49
|
cell% 2* field stack-pointer \ stackpointer name |
| 163 : |
anton
|
1.74
|
cell% field stack-type \ name for default type of stack items |
| 164 : |
anton
|
1.53
|
cell% field stack-in-index-xt \ ( in-size item -- in-index ) |
| 165 : |
anton
|
1.126
|
cell% field stack-access-transform \ ( nitem -- index ) |
| 166 : |
anton
|
1.49
|
end-struct stack% |
| 167 : |
|
|
|
| 168 : |
anton
|
1.53
|
struct% |
| 169 : |
|
|
cell% 2* field item-name \ name, excluding stack prefixes |
| 170 : |
|
|
cell% field item-stack \ descriptor for the stack used, 0 is default |
| 171 : |
|
|
cell% field item-type \ descriptor for the item type |
| 172 : |
|
|
cell% field item-offset \ offset in stack items, 0 for the deepest element |
| 173 : |
anton
|
1.66
|
cell% field item-first \ true if this is the first occurence of the item |
| 174 : |
anton
|
1.53
|
end-struct item% |
| 175 : |
|
|
|
| 176 : |
|
|
struct% |
| 177 : |
|
|
cell% 2* field type-c-name |
| 178 : |
|
|
cell% field type-stack \ default stack |
| 179 : |
|
|
cell% field type-size \ size of type in stack items |
| 180 : |
|
|
cell% field type-fetch \ xt of fetch code generator ( item -- ) |
| 181 : |
|
|
cell% field type-store \ xt of store code generator ( item -- ) |
| 182 : |
|
|
end-struct type% |
| 183 : |
|
|
|
| 184 : |
anton
|
1.72
|
variable next-stack-number 0 next-stack-number ! |
| 185 : |
|
|
create stacks max-stacks cells allot \ array of stacks |
| 186 : |
|
|
|
| 187 : |
anton
|
1.53
|
: stack-in-index ( in-size item -- in-index ) |
| 188 : |
|
|
item-offset @ - 1- ; |
| 189 : |
|
|
|
| 190 : |
|
|
: inst-in-index ( in-size item -- in-index ) |
| 191 : |
|
|
nip dup item-offset @ swap item-type @ type-size @ + 1- ; |
| 192 : |
|
|
|
| 193 : |
anton
|
1.92
|
: make-stack ( addr-ptr u1 type "stack-name" -- ) |
| 194 : |
|
|
next-stack-number @ max-stacks < s" too many stacks" ?print-error |
| 195 : |
anton
|
1.49
|
create stack% %allot >r |
| 196 : |
anton
|
1.72
|
r@ stacks next-stack-number @ th ! |
| 197 : |
anton
|
1.92
|
next-stack-number @ r@ stack-number ! |
| 198 : |
|
|
1 next-stack-number +! |
| 199 : |
anton
|
1.74
|
r@ stack-type ! |
| 200 : |
anton
|
1.53
|
save-mem r@ stack-pointer 2! |
| 201 : |
anton
|
1.126
|
['] stack-in-index r@ stack-in-index-xt ! |
| 202 : |
|
|
['] noop r@ stack-access-transform ! |
| 203 : |
|
|
rdrop ; |
| 204 : |
anton
|
1.49
|
|
| 205 : |
anton
|
1.92
|
: map-stacks { xt -- } |
| 206 : |
anton
|
1.118
|
\ perform xt for all stacks |
| 207 : |
|
|
next-stack-number @ 0 +do |
| 208 : |
|
|
stacks i th @ xt execute |
| 209 : |
|
|
loop ; |
| 210 : |
|
|
|
| 211 : |
|
|
: map-stacks1 { xt -- } |
| 212 : |
anton
|
1.92
|
\ perform xt for all stacks except inst-stream |
| 213 : |
|
|
next-stack-number @ 1 +do |
| 214 : |
|
|
stacks i th @ xt execute |
| 215 : |
|
|
loop ; |
| 216 : |
|
|
|
| 217 : |
anton
|
1.49
|
\ stack items |
| 218 : |
|
|
|
| 219 : |
|
|
: init-item ( addr u addr1 -- ) |
| 220 : |
|
|
\ initialize item at addr1 with name addr u |
| 221 : |
|
|
\ !! remove stack prefix |
| 222 : |
|
|
dup item% %size erase |
| 223 : |
|
|
item-name 2! ; |
| 224 : |
|
|
|
| 225 : |
anton
|
1.64
|
: map-items { addr end xt -- } |
| 226 : |
|
|
\ perform xt for all items in array addr...end |
| 227 : |
|
|
end addr ?do |
| 228 : |
|
|
i xt execute |
| 229 : |
|
|
item% %size +loop ; |
| 230 : |
|
|
|
| 231 : |
anton
|
1.77
|
\ types |
| 232 : |
|
|
|
| 233 : |
|
|
: print-type-prefix ( type -- ) |
| 234 : |
|
|
body> >head name>string type ; |
| 235 : |
|
|
|
| 236 : |
anton
|
1.49
|
\ various variables for storing stuff of one primitive |
| 237 : |
anton
|
1.1
|
|
| 238 : |
anton
|
1.69
|
struct% |
| 239 : |
|
|
cell% 2* field prim-name |
| 240 : |
|
|
cell% 2* field prim-wordset |
| 241 : |
|
|
cell% 2* field prim-c-name |
| 242 : |
|
|
cell% 2* field prim-doc |
| 243 : |
|
|
cell% 2* field prim-c-code |
| 244 : |
|
|
cell% 2* field prim-forth-code |
| 245 : |
|
|
cell% 2* field prim-stack-string |
| 246 : |
anton
|
1.82
|
cell% field prim-num \ ordinal number |
| 247 : |
anton
|
1.75
|
cell% field prim-items-wordlist \ unique items |
| 248 : |
anton
|
1.69
|
item% max-effect * field prim-effect-in |
| 249 : |
|
|
item% max-effect * field prim-effect-out |
| 250 : |
|
|
cell% field prim-effect-in-end |
| 251 : |
|
|
cell% field prim-effect-out-end |
| 252 : |
anton
|
1.71
|
cell% max-stacks * field prim-stacks-in \ number of in items per stack |
| 253 : |
|
|
cell% max-stacks * field prim-stacks-out \ number of out items per stack |
| 254 : |
anton
|
1.69
|
end-struct prim% |
| 255 : |
|
|
|
| 256 : |
anton
|
1.70
|
: make-prim ( -- prim ) |
| 257 : |
|
|
prim% %alloc { p } |
| 258 : |
|
|
s" " p prim-doc 2! s" " p prim-forth-code 2! s" " p prim-wordset 2! |
| 259 : |
|
|
p ; |
| 260 : |
|
|
|
| 261 : |
anton
|
1.79
|
0 value prim \ in combined prims either combined or a part |
| 262 : |
|
|
0 value combined \ in combined prims the combined prim |
| 263 : |
|
|
variable in-part \ true if processing a part |
| 264 : |
|
|
in-part off |
| 265 : |
|
|
|
| 266 : |
anton
|
1.118
|
: prim-context ( ... p xt -- ... ) |
| 267 : |
|
|
\ execute xt with prim set to p |
| 268 : |
|
|
prim >r |
| 269 : |
|
|
swap to prim |
| 270 : |
|
|
catch |
| 271 : |
|
|
r> to prim |
| 272 : |
|
|
throw ; |
| 273 : |
|
|
|
| 274 : |
anton
|
1.79
|
1000 constant max-combined |
| 275 : |
|
|
create combined-prims max-combined cells allot |
| 276 : |
|
|
variable num-combined |
| 277 : |
anton
|
1.118
|
variable part-num \ current part number during process-combined |
| 278 : |
anton
|
1.79
|
|
| 279 : |
anton
|
1.114
|
: map-combined { xt -- } |
| 280 : |
|
|
\ perform xt for all components of the current combined instruction |
| 281 : |
|
|
num-combined @ 0 +do |
| 282 : |
|
|
combined-prims i th @ xt execute |
| 283 : |
|
|
loop ; |
| 284 : |
|
|
|
| 285 : |
anton
|
1.81
|
table constant combinations |
| 286 : |
|
|
\ the keys are the sequences of pointers to primitives |
| 287 : |
|
|
|
| 288 : |
anton
|
1.79
|
create current-depth max-stacks cells allot |
| 289 : |
|
|
create max-depth max-stacks cells allot |
| 290 : |
|
|
create min-depth max-stacks cells allot |
| 291 : |
anton
|
1.69
|
|
| 292 : |
anton
|
1.118
|
create sp-update-in max-stacks cells allot |
| 293 : |
|
|
\ where max-depth occured the first time |
| 294 : |
|
|
create max-depths max-stacks max-combined 1+ * cells allot |
| 295 : |
anton
|
1.119
|
\ maximum depth at start of each part: array[parts] of array[stack] |
| 296 : |
|
|
create max-back-depths max-stacks max-combined 1+ * cells allot |
| 297 : |
|
|
\ maximun depth from end of the combination to the start of the each part |
| 298 : |
anton
|
1.118
|
|
| 299 : |
|
|
: s-c-max-depth ( nstack ncomponent -- addr ) |
| 300 : |
|
|
max-stacks * + cells max-depths + ; |
| 301 : |
|
|
|
| 302 : |
anton
|
1.119
|
: s-c-max-back-depth ( nstack ncomponent -- addr ) |
| 303 : |
|
|
max-stacks * + cells max-back-depths + ; |
| 304 : |
|
|
|
| 305 : |
anton
|
1.71
|
wordlist constant primitives |
| 306 : |
|
|
|
| 307 : |
|
|
: create-prim ( prim -- ) |
| 308 : |
anton
|
1.82
|
dup prim-name 2@ primitives ['] constant insert-wordlist ; |
| 309 : |
anton
|
1.71
|
|
| 310 : |
|
|
: stack-in ( stack -- addr ) |
| 311 : |
|
|
\ address of number of stack items in effect in |
| 312 : |
|
|
stack-number @ cells prim prim-stacks-in + ; |
| 313 : |
|
|
|
| 314 : |
|
|
: stack-out ( stack -- addr ) |
| 315 : |
|
|
\ address of number of stack items in effect out |
| 316 : |
|
|
stack-number @ cells prim prim-stacks-out + ; |
| 317 : |
|
|
|
| 318 : |
anton
|
1.69
|
\ global vars |
| 319 : |
anton
|
1.17
|
variable c-line |
| 320 : |
|
|
2variable c-filename |
| 321 : |
|
|
variable name-line |
| 322 : |
|
|
2variable name-filename |
| 323 : |
|
|
2variable last-name-filename |
| 324 : |
pazsan
|
1.30
|
Variable function-number 0 function-number ! |
| 325 : |
anton
|
1.1
|
|
| 326 : |
|
|
\ a few more set ops |
| 327 : |
|
|
|
| 328 : |
|
|
: bit-equivalent ( w1 w2 -- w3 ) |
| 329 : |
|
|
xor invert ; |
| 330 : |
|
|
|
| 331 : |
|
|
: complement ( set1 -- set2 ) |
| 332 : |
|
|
empty ['] bit-equivalent binary-set-operation ; |
| 333 : |
|
|
|
| 334 : |
anton
|
1.121
|
\ forward declaration for inst-stream (breaks cycle in definitions) |
| 335 : |
|
|
defer inst-stream-f ( -- stack ) |
| 336 : |
|
|
|
| 337 : |
anton
|
1.80
|
\ stack access stuff |
| 338 : |
anton
|
1.79
|
|
| 339 : |
anton
|
1.126
|
: normal-stack-access0 { n stack -- } |
| 340 : |
|
|
n stack stack-access-transform @ execute ." [" 0 .r ." ]" ; |
| 341 : |
|
|
|
| 342 : |
|
|
: normal-stack-access1 { n stack -- } |
| 343 : |
|
|
stack stack-pointer 2@ type |
| 344 : |
|
|
n if |
| 345 : |
|
|
n stack normal-stack-access0 |
| 346 : |
anton
|
1.49
|
else |
| 347 : |
anton
|
1.126
|
." TOS" |
| 348 : |
anton
|
1.49
|
endif ; |
| 349 : |
anton
|
1.1
|
|
| 350 : |
anton
|
1.121
|
: normal-stack-access ( n stack -- ) |
| 351 : |
|
|
dup inst-stream-f = if |
| 352 : |
|
|
." IMM_ARG(" normal-stack-access1 ." ," immarg ? ." )" |
| 353 : |
|
|
1 immarg +! |
| 354 : |
|
|
else |
| 355 : |
|
|
normal-stack-access1 |
| 356 : |
|
|
endif ; |
| 357 : |
anton
|
1.80
|
|
| 358 : |
anton
|
1.118
|
: stack-depth { stack -- n } |
| 359 : |
|
|
current-depth stack stack-number @ th @ ; |
| 360 : |
|
|
|
| 361 : |
anton
|
1.79
|
: part-stack-access { n stack -- } |
| 362 : |
anton
|
1.80
|
\ print _<stack><x>, x=inst-stream? n : maxdepth-currentdepth-n-1 |
| 363 : |
anton
|
1.79
|
." _" stack stack-pointer 2@ type |
| 364 : |
|
|
stack stack-number @ { stack# } |
| 365 : |
anton
|
1.118
|
stack stack-depth n + { access-depth } |
| 366 : |
anton
|
1.80
|
stack inst-stream-f = if |
| 367 : |
|
|
access-depth |
| 368 : |
|
|
else |
| 369 : |
|
|
combined prim-stacks-in stack# th @ |
| 370 : |
|
|
assert( dup max-depth stack# th @ = ) |
| 371 : |
|
|
access-depth - 1- |
| 372 : |
|
|
endif |
| 373 : |
anton
|
1.79
|
0 .r ; |
| 374 : |
|
|
|
| 375 : |
anton
|
1.118
|
: part-stack-read { n stack -- } |
| 376 : |
|
|
stack stack-depth n + ( ndepth ) |
| 377 : |
|
|
stack stack-number @ part-num @ s-c-max-depth @ |
| 378 : |
|
|
\ max-depth stack stack-number @ th @ ( ndepth nmaxdepth ) |
| 379 : |
|
|
over <= if ( ndepth ) \ load from memory |
| 380 : |
|
|
stack normal-stack-access |
| 381 : |
|
|
else |
| 382 : |
|
|
drop n stack part-stack-access |
| 383 : |
|
|
endif ; |
| 384 : |
|
|
|
| 385 : |
anton
|
1.119
|
: stack-diff ( stack -- n ) |
| 386 : |
|
|
\ in-out |
| 387 : |
|
|
dup stack-in @ swap stack-out @ - ; |
| 388 : |
|
|
|
| 389 : |
|
|
: part-stack-write { n stack -- } |
| 390 : |
|
|
stack stack-depth n + |
| 391 : |
|
|
stack stack-number @ part-num @ s-c-max-back-depth @ |
| 392 : |
|
|
over <= if ( ndepth ) |
| 393 : |
|
|
stack combined ['] stack-diff prim-context - |
| 394 : |
|
|
stack normal-stack-access |
| 395 : |
|
|
else |
| 396 : |
|
|
drop n stack part-stack-access |
| 397 : |
|
|
endif ; |
| 398 : |
anton
|
1.118
|
|
| 399 : |
|
|
: stack-read ( n stack -- ) |
| 400 : |
|
|
\ print a stack access at index n of stack |
| 401 : |
|
|
in-part @ if |
| 402 : |
|
|
part-stack-read |
| 403 : |
|
|
else |
| 404 : |
|
|
normal-stack-access |
| 405 : |
|
|
endif ; |
| 406 : |
|
|
|
| 407 : |
|
|
: stack-write ( n stack -- ) |
| 408 : |
anton
|
1.79
|
\ print a stack access at index n of stack |
| 409 : |
|
|
in-part @ if |
| 410 : |
anton
|
1.118
|
part-stack-write |
| 411 : |
anton
|
1.79
|
else |
| 412 : |
|
|
normal-stack-access |
| 413 : |
|
|
endif ; |
| 414 : |
|
|
|
| 415 : |
anton
|
1.53
|
: item-in-index { item -- n } |
| 416 : |
anton
|
1.49
|
\ n is the index of item (in the in-effect) |
| 417 : |
anton
|
1.53
|
item item-stack @ dup >r stack-in @ ( in-size r:stack ) |
| 418 : |
|
|
item r> stack-in-index-xt @ execute ; |
| 419 : |
anton
|
1.1
|
|
| 420 : |
anton
|
1.78
|
: item-stack-type-name ( item -- addr u ) |
| 421 : |
|
|
item-stack @ stack-type @ type-c-name 2@ ; |
| 422 : |
|
|
|
| 423 : |
anton
|
1.1
|
: fetch-single ( item -- ) |
| 424 : |
anton
|
1.106
|
\ fetch a single stack item from its stack |
| 425 : |
|
|
>r |
| 426 : |
|
|
." vm_" r@ item-stack-type-name type |
| 427 : |
|
|
." 2" r@ item-type @ print-type-prefix ." (" |
| 428 : |
anton
|
1.118
|
r@ item-in-index r@ item-stack @ stack-read ." ," |
| 429 : |
anton
|
1.106
|
r@ item-name 2@ type |
| 430 : |
|
|
." );" cr |
| 431 : |
|
|
rdrop ; |
| 432 : |
anton
|
1.1
|
|
| 433 : |
|
|
: fetch-double ( item -- ) |
| 434 : |
anton
|
1.106
|
\ fetch a double stack item from its stack |
| 435 : |
|
|
>r |
| 436 : |
|
|
." vm_two" |
| 437 : |
|
|
r@ item-stack-type-name type ." 2" |
| 438 : |
|
|
r@ item-type @ print-type-prefix ." (" |
| 439 : |
anton
|
1.118
|
r@ item-in-index r@ item-stack @ 2dup ." (Cell)" stack-read |
| 440 : |
|
|
." , " -1 under+ ." (Cell)" stack-read |
| 441 : |
anton
|
1.106
|
." , " r@ item-name 2@ type |
| 442 : |
|
|
." )" cr |
| 443 : |
|
|
rdrop ; |
| 444 : |
anton
|
1.1
|
|
| 445 : |
anton
|
1.49
|
: same-as-in? ( item -- f ) |
| 446 : |
|
|
\ f is true iff the offset and stack of item is the same as on input |
| 447 : |
anton
|
1.1
|
>r |
| 448 : |
anton
|
1.74
|
r@ item-first @ if |
| 449 : |
|
|
rdrop false exit |
| 450 : |
|
|
endif |
| 451 : |
anton
|
1.75
|
r@ item-name 2@ prim prim-items-wordlist @ search-wordlist 0= abort" bug" |
| 452 : |
anton
|
1.1
|
execute @ |
| 453 : |
|
|
dup r@ = |
| 454 : |
|
|
if \ item first appeared in output |
| 455 : |
|
|
drop false |
| 456 : |
|
|
else |
| 457 : |
anton
|
1.49
|
dup item-stack @ r@ item-stack @ = |
| 458 : |
|
|
swap item-offset @ r@ item-offset @ = and |
| 459 : |
anton
|
1.1
|
endif |
| 460 : |
|
|
rdrop ; |
| 461 : |
|
|
|
| 462 : |
anton
|
1.49
|
: item-out-index ( item -- n ) |
| 463 : |
|
|
\ n is the index of item (in the in-effect) |
| 464 : |
|
|
>r r@ item-stack @ stack-out @ r> item-offset @ - 1- ; |
| 465 : |
pazsan
|
1.31
|
|
| 466 : |
anton
|
1.1
|
: really-store-single ( item -- ) |
| 467 : |
anton
|
1.106
|
>r |
| 468 : |
|
|
." vm_" |
| 469 : |
|
|
r@ item-type @ print-type-prefix ." 2" |
| 470 : |
|
|
r@ item-stack-type-name type ." (" |
| 471 : |
|
|
r@ item-name 2@ type ." ," |
| 472 : |
anton
|
1.118
|
r@ item-out-index r@ item-stack @ stack-write ." );" |
| 473 : |
anton
|
1.106
|
rdrop ; |
| 474 : |
anton
|
1.1
|
|
| 475 : |
|
|
: store-single ( item -- ) |
| 476 : |
anton
|
1.112
|
>r |
| 477 : |
anton
|
1.118
|
store-optimization @ in-part @ 0= and r@ same-as-in? and if |
| 478 : |
anton
|
1.112
|
r@ item-in-index 0= r@ item-out-index 0= xor if |
| 479 : |
|
|
." IF_" r@ item-stack @ stack-pointer 2@ type |
| 480 : |
|
|
." TOS(" r@ really-store-single ." );" cr |
| 481 : |
|
|
endif |
| 482 : |
|
|
else |
| 483 : |
|
|
r@ really-store-single cr |
| 484 : |
|
|
endif |
| 485 : |
|
|
rdrop ; |
| 486 : |
anton
|
1.1
|
|
| 487 : |
|
|
: store-double ( item -- ) |
| 488 : |
|
|
\ !! store optimization is not performed, because it is not yet needed |
| 489 : |
|
|
>r |
| 490 : |
anton
|
1.78
|
." vm_" |
| 491 : |
|
|
r@ item-type @ print-type-prefix ." 2two" |
| 492 : |
|
|
r@ item-stack-type-name type ." (" |
| 493 : |
|
|
r@ item-name 2@ type ." , " |
| 494 : |
anton
|
1.118
|
r@ item-out-index r@ item-stack @ 2dup stack-write |
| 495 : |
|
|
." , " -1 under+ stack-write |
| 496 : |
anton
|
1.106
|
." )" cr |
| 497 : |
anton
|
1.1
|
rdrop ; |
| 498 : |
|
|
|
| 499 : |
anton
|
1.54
|
: single ( -- xt1 xt2 n ) |
| 500 : |
|
|
['] fetch-single ['] store-single 1 ; |
| 501 : |
anton
|
1.1
|
|
| 502 : |
anton
|
1.54
|
: double ( -- xt1 xt2 n ) |
| 503 : |
|
|
['] fetch-double ['] store-double 2 ; |
| 504 : |
anton
|
1.1
|
|
| 505 : |
|
|
: s, ( addr u -- ) |
| 506 : |
|
|
\ allocate a string |
| 507 : |
|
|
here swap dup allot move ; |
| 508 : |
|
|
|
| 509 : |
anton
|
1.50
|
wordlist constant prefixes |
| 510 : |
|
|
|
| 511 : |
|
|
: declare ( addr "name" -- ) |
| 512 : |
|
|
\ remember that there is a stack item at addr called name |
| 513 : |
|
|
create , ; |
| 514 : |
|
|
|
| 515 : |
|
|
: !default ( w addr -- ) |
| 516 : |
|
|
dup @ if |
| 517 : |
|
|
2drop \ leave nonzero alone |
| 518 : |
|
|
else |
| 519 : |
|
|
! |
| 520 : |
|
|
endif ; |
| 521 : |
|
|
|
| 522 : |
|
|
: create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- ) |
| 523 : |
anton
|
1.49
|
\ describes a type |
| 524 : |
|
|
\ addr u specifies the C type name |
| 525 : |
|
|
\ stack effect entries of the type start with prefix |
| 526 : |
|
|
create type% %allot >r |
| 527 : |
|
|
addr u save-mem r@ type-c-name 2! |
| 528 : |
|
|
xt1 r@ type-fetch ! |
| 529 : |
|
|
xt2 r@ type-store ! |
| 530 : |
|
|
n r@ type-size ! |
| 531 : |
|
|
stack r@ type-stack ! |
| 532 : |
|
|
rdrop ; |
| 533 : |
anton
|
1.1
|
|
| 534 : |
anton
|
1.105
|
: type-prefix ( addr u xt1 xt2 n stack "prefix" -- ) |
| 535 : |
anton
|
1.94
|
get-current >r prefixes set-current |
| 536 : |
|
|
create-type r> set-current |
| 537 : |
anton
|
1.50
|
does> ( item -- ) |
| 538 : |
|
|
\ initialize item |
| 539 : |
|
|
{ item typ } |
| 540 : |
|
|
typ item item-type ! |
| 541 : |
|
|
typ type-stack @ item item-stack !default |
| 542 : |
anton
|
1.75
|
item item-name 2@ prim prim-items-wordlist @ search-wordlist 0= if |
| 543 : |
anton
|
1.66
|
item item-name 2@ nextname item declare |
| 544 : |
|
|
item item-first on |
| 545 : |
|
|
\ typ type-c-name 2@ type space type ." ;" cr |
| 546 : |
anton
|
1.50
|
else |
| 547 : |
|
|
drop |
| 548 : |
anton
|
1.66
|
item item-first off |
| 549 : |
anton
|
1.50
|
endif ; |
| 550 : |
|
|
|
| 551 : |
|
|
: execute-prefix ( item addr1 u1 -- ) |
| 552 : |
|
|
\ execute the word ( item -- ) associated with the longest prefix |
| 553 : |
|
|
\ of addr1 u1 |
| 554 : |
|
|
0 swap ?do |
| 555 : |
|
|
dup i prefixes search-wordlist |
| 556 : |
|
|
if \ ok, we have the type ( item addr1 xt ) |
| 557 : |
|
|
nip execute |
| 558 : |
|
|
UNLOOP EXIT |
| 559 : |
|
|
endif |
| 560 : |
|
|
-1 s+loop |
| 561 : |
|
|
\ we did not find a type, abort |
| 562 : |
anton
|
1.81
|
false s" unknown prefix" ?print-error ; |
| 563 : |
anton
|
1.1
|
|
| 564 : |
|
|
: declaration ( item -- ) |
| 565 : |
anton
|
1.50
|
dup item-name 2@ execute-prefix ; |
| 566 : |
anton
|
1.1
|
|
| 567 : |
anton
|
1.64
|
: declaration-list ( addr1 addr2 -- ) |
| 568 : |
|
|
['] declaration map-items ; |
| 569 : |
|
|
|
| 570 : |
|
|
: declarations ( -- ) |
| 571 : |
anton
|
1.75
|
wordlist dup prim prim-items-wordlist ! set-current |
| 572 : |
anton
|
1.69
|
prim prim-effect-in prim prim-effect-in-end @ declaration-list |
| 573 : |
|
|
prim prim-effect-out prim prim-effect-out-end @ declaration-list ; |
| 574 : |
anton
|
1.64
|
|
| 575 : |
anton
|
1.66
|
: print-declaration { item -- } |
| 576 : |
|
|
item item-first @ if |
| 577 : |
|
|
item item-type @ type-c-name 2@ type space |
| 578 : |
|
|
item item-name 2@ type ." ;" cr |
| 579 : |
|
|
endif ; |
| 580 : |
|
|
|
| 581 : |
|
|
: print-declarations ( -- ) |
| 582 : |
anton
|
1.69
|
prim prim-effect-in prim prim-effect-in-end @ ['] print-declaration map-items |
| 583 : |
|
|
prim prim-effect-out prim prim-effect-out-end @ ['] print-declaration map-items ; |
| 584 : |
anton
|
1.66
|
|
| 585 : |
anton
|
1.51
|
: stack-prefix ( stack "prefix" -- ) |
| 586 : |
anton
|
1.94
|
get-current >r prefixes set-current |
| 587 : |
anton
|
1.51
|
name tuck nextname create ( stack length ) 2, |
| 588 : |
anton
|
1.94
|
r> set-current |
| 589 : |
anton
|
1.51
|
does> ( item -- ) |
| 590 : |
|
|
2@ { item stack prefix-length } |
| 591 : |
|
|
item item-name 2@ prefix-length /string item item-name 2! |
| 592 : |
|
|
stack item item-stack ! |
| 593 : |
|
|
item declaration ; |
| 594 : |
anton
|
1.73
|
|
| 595 : |
anton
|
1.74
|
\ types pointed to by stacks for use in combined prims |
| 596 : |
anton
|
1.83
|
\ !! output-c-combined shouldn't use these names! |
| 597 : |
anton
|
1.92
|
: stack-type-name ( addr u "name" -- ) |
| 598 : |
|
|
single 0 create-type ; |
| 599 : |
|
|
|
| 600 : |
anton
|
1.93
|
wordlist constant type-names \ this is here just to meet the requirement |
| 601 : |
|
|
\ that a type be a word; it is never used for lookup |
| 602 : |
anton
|
1.83
|
|
| 603 : |
anton
|
1.93
|
: stack ( "name" "stack-pointer" "type" -- ) |
| 604 : |
|
|
\ define stack |
| 605 : |
|
|
name { d: stack-name } |
| 606 : |
|
|
name { d: stack-pointer } |
| 607 : |
|
|
name { d: stack-type } |
| 608 : |
|
|
get-current type-names set-current |
| 609 : |
|
|
stack-type 2dup nextname stack-type-name |
| 610 : |
|
|
set-current |
| 611 : |
|
|
stack-pointer lastxt >body stack-name nextname make-stack ; |
| 612 : |
|
|
|
| 613 : |
|
|
stack inst-stream IP Cell |
| 614 : |
anton
|
1.73
|
' inst-in-index inst-stream stack-in-index-xt ! |
| 615 : |
anton
|
1.80
|
' inst-stream <is> inst-stream-f |
| 616 : |
anton
|
1.73
|
\ !! initialize stack-in and stack-out |
| 617 : |
anton
|
1.1
|
|
| 618 : |
|
|
\ offset computation |
| 619 : |
|
|
\ the leftmost (i.e. deepest) item has offset 0 |
| 620 : |
|
|
\ the rightmost item has the highest offset |
| 621 : |
|
|
|
| 622 : |
anton
|
1.49
|
: compute-offset { item xt -- } |
| 623 : |
|
|
\ xt specifies in/out; update stack-in/out and set item-offset |
| 624 : |
|
|
item item-type @ type-size @ |
| 625 : |
|
|
item item-stack @ xt execute dup @ >r +! |
| 626 : |
|
|
r> item item-offset ! ; |
| 627 : |
|
|
|
| 628 : |
anton
|
1.64
|
: compute-offset-in ( addr1 addr2 -- ) |
| 629 : |
|
|
['] stack-in compute-offset ; |
| 630 : |
|
|
|
| 631 : |
|
|
: compute-offset-out ( addr1 addr2 -- ) |
| 632 : |
|
|
['] stack-out compute-offset ; |
| 633 : |
anton
|
1.49
|
|
| 634 : |
anton
|
1.1
|
: compute-offsets ( -- ) |
| 635 : |
anton
|
1.132
|
prim prim-stacks-in max-stacks cells erase |
| 636 : |
|
|
prim prim-stacks-out max-stacks cells erase |
| 637 : |
anton
|
1.69
|
prim prim-effect-in prim prim-effect-in-end @ ['] compute-offset-in map-items |
| 638 : |
|
|
prim prim-effect-out prim prim-effect-out-end @ ['] compute-offset-out map-items |
| 639 : |
anton
|
1.81
|
inst-stream stack-out @ 0= s" # can only be on the input side" ?print-error ; |
| 640 : |
|
|
|
| 641 : |
|
|
: process-simple ( -- ) |
| 642 : |
|
|
prim prim { W^ key } key cell |
| 643 : |
anton
|
1.82
|
combinations ['] constant insert-wordlist |
| 644 : |
anton
|
1.81
|
declarations compute-offsets |
| 645 : |
anton
|
1.82
|
output @ execute ; |
| 646 : |
anton
|
1.49
|
|
| 647 : |
|
|
: flush-a-tos { stack -- } |
| 648 : |
|
|
stack stack-out @ 0<> stack stack-in @ 0= and |
| 649 : |
|
|
if |
| 650 : |
|
|
." IF_" stack stack-pointer 2@ 2dup type ." TOS(" |
| 651 : |
anton
|
1.126
|
2dup type 0 stack normal-stack-access0 ." = " type ." TOS);" cr |
| 652 : |
anton
|
1.49
|
endif ; |
| 653 : |
anton
|
1.1
|
|
| 654 : |
|
|
: flush-tos ( -- ) |
| 655 : |
anton
|
1.118
|
['] flush-a-tos map-stacks1 ; |
| 656 : |
anton
|
1.49
|
|
| 657 : |
|
|
: fill-a-tos { stack -- } |
| 658 : |
|
|
stack stack-out @ 0= stack stack-in @ 0<> and |
| 659 : |
|
|
if |
| 660 : |
|
|
." IF_" stack stack-pointer 2@ 2dup type ." TOS(" |
| 661 : |
anton
|
1.126
|
2dup type ." TOS = " type 0 stack normal-stack-access0 ." );" cr |
| 662 : |
anton
|
1.49
|
endif ; |
| 663 : |
anton
|
1.1
|
|
| 664 : |
|
|
: fill-tos ( -- ) |
| 665 : |
anton
|
1.53
|
\ !! inst-stream for prefetching? |
| 666 : |
anton
|
1.118
|
['] fill-a-tos map-stacks1 ; |
| 667 : |
anton
|
1.49
|
|
| 668 : |
|
|
: fetch ( addr -- ) |
| 669 : |
anton
|
1.72
|
dup item-type @ type-fetch @ execute ; |
| 670 : |
anton
|
1.1
|
|
| 671 : |
|
|
: fetches ( -- ) |
| 672 : |
anton
|
1.69
|
prim prim-effect-in prim prim-effect-in-end @ ['] fetch map-items ; |
| 673 : |
anton
|
1.49
|
|
| 674 : |
anton
|
1.126
|
: stack-update-transform ( n1 stack -- n2 ) |
| 675 : |
|
|
\ n2 is the number by which the stack pointer should be |
| 676 : |
|
|
\ incremented to pop n1 items |
| 677 : |
|
|
stack-access-transform @ dup >r execute |
| 678 : |
|
|
0 r> execute - ; |
| 679 : |
|
|
|
| 680 : |
anton
|
1.49
|
: stack-pointer-update { stack -- } |
| 681 : |
anton
|
1.123
|
\ stacks grow downwards |
| 682 : |
anton
|
1.119
|
stack stack-diff |
| 683 : |
anton
|
1.49
|
?dup-if \ this check is not necessary, gcc would do this for us |
| 684 : |
anton
|
1.118
|
stack inst-stream = if |
| 685 : |
anton
|
1.120
|
." INC_IP(" 0 .r ." );" cr |
| 686 : |
anton
|
1.118
|
else |
| 687 : |
anton
|
1.126
|
stack stack-pointer 2@ type ." += " |
| 688 : |
|
|
stack stack-update-transform 0 .r ." ;" cr |
| 689 : |
anton
|
1.118
|
endif |
| 690 : |
anton
|
1.55
|
endif ; |
| 691 : |
|
|
|
| 692 : |
anton
|
1.1
|
: stack-pointer-updates ( -- ) |
| 693 : |
anton
|
1.92
|
['] stack-pointer-update map-stacks ; |
| 694 : |
anton
|
1.1
|
|
| 695 : |
|
|
: store ( item -- ) |
| 696 : |
|
|
\ f is true if the item should be stored |
| 697 : |
|
|
\ f is false if the store is probably not necessary |
| 698 : |
anton
|
1.49
|
dup item-type @ type-store @ execute ; |
| 699 : |
anton
|
1.1
|
|
| 700 : |
|
|
: stores ( -- ) |
| 701 : |
anton
|
1.69
|
prim prim-effect-out prim prim-effect-out-end @ ['] store map-items ; |
| 702 : |
pazsan
|
1.8
|
|
| 703 : |
anton
|
1.91
|
: print-debug-arg { item -- } |
| 704 : |
|
|
." fputs(" quote space item item-name 2@ type ." =" quote ." , vm_out); " |
| 705 : |
|
|
." printarg_" item item-type @ print-type-prefix |
| 706 : |
|
|
." (" item item-name 2@ type ." );" cr ; |
| 707 : |
|
|
|
| 708 : |
|
|
: print-debug-args ( -- ) |
| 709 : |
|
|
." #ifdef VM_DEBUG" cr |
| 710 : |
|
|
." if (vm_debug) {" cr |
| 711 : |
|
|
prim prim-effect-in prim prim-effect-in-end @ ['] print-debug-arg map-items |
| 712 : |
|
|
\ ." fputc('\n', vm_out);" cr |
| 713 : |
|
|
." }" cr |
| 714 : |
|
|
." #endif" cr ; |
| 715 : |
|
|
|
| 716 : |
|
|
: print-debug-result { item -- } |
| 717 : |
|
|
item item-first @ if |
| 718 : |
|
|
item print-debug-arg |
| 719 : |
|
|
endif ; |
| 720 : |
|
|
|
| 721 : |
|
|
: print-debug-results ( -- ) |
| 722 : |
|
|
cr |
| 723 : |
|
|
." #ifdef VM_DEBUG" cr |
| 724 : |
|
|
." if (vm_debug) {" cr |
| 725 : |
|
|
." fputs(" quote ." -- " quote ." , vm_out); " |
| 726 : |
|
|
prim prim-effect-out prim prim-effect-out-end @ ['] print-debug-result map-items |
| 727 : |
|
|
." fputc('\n', vm_out);" cr |
| 728 : |
|
|
." }" cr |
| 729 : |
|
|
." #endif" cr ; |
| 730 : |
|
|
|
| 731 : |
anton
|
1.86
|
: output-super-end ( -- ) |
| 732 : |
|
|
prim prim-c-code 2@ s" SET_IP" search if |
| 733 : |
|
|
." SUPER_END;" cr |
| 734 : |
|
|
endif |
| 735 : |
|
|
2drop ; |
| 736 : |
|
|
|
| 737 : |
anton
|
1.124
|
: output-nextp2 ( -- ) |
| 738 : |
|
|
." NEXT_P2;" cr ; |
| 739 : |
|
|
|
| 740 : |
|
|
variable tail-nextp2 \ xt to execute for printing NEXT_P2 in INST_TAIL |
| 741 : |
|
|
' output-nextp2 tail-nextp2 ! |
| 742 : |
|
|
|
| 743 : |
anton
|
1.120
|
: output-label2 ( -- ) |
| 744 : |
anton
|
1.121
|
." LABEL2(" prim prim-c-name 2@ type ." )" cr |
| 745 : |
|
|
." NEXT_P2;" cr ; |
| 746 : |
anton
|
1.120
|
|
| 747 : |
|
|
: output-c-tail1 { xt -- } |
| 748 : |
|
|
\ the final part of the generated C code, with xt printing LABEL2 or not. |
| 749 : |
anton
|
1.86
|
output-super-end |
| 750 : |
anton
|
1.91
|
print-debug-results |
| 751 : |
anton
|
1.120
|
." NEXT_P1;" cr |
| 752 : |
anton
|
1.52
|
stores |
| 753 : |
anton
|
1.119
|
fill-tos |
| 754 : |
anton
|
1.121
|
xt execute ; |
| 755 : |
anton
|
1.108
|
|
| 756 : |
anton
|
1.120
|
: output-c-tail1-no-stores { xt -- } |
| 757 : |
|
|
\ the final part of the generated C code for combinations |
| 758 : |
|
|
output-super-end |
| 759 : |
|
|
." NEXT_P1;" cr |
| 760 : |
anton
|
1.119
|
fill-tos |
| 761 : |
anton
|
1.121
|
xt execute ; |
| 762 : |
anton
|
1.120
|
|
| 763 : |
|
|
: output-c-tail ( -- ) |
| 764 : |
anton
|
1.124
|
tail-nextp2 @ output-c-tail1 ; |
| 765 : |
anton
|
1.52
|
|
| 766 : |
anton
|
1.108
|
: output-c-tail2 ( -- ) |
| 767 : |
anton
|
1.120
|
['] output-label2 output-c-tail1 ; |
| 768 : |
|
|
|
| 769 : |
|
|
: output-c-tail-no-stores ( -- ) |
| 770 : |
anton
|
1.124
|
tail-nextp2 @ output-c-tail1-no-stores ; |
| 771 : |
anton
|
1.119
|
|
| 772 : |
|
|
: output-c-tail2-no-stores ( -- ) |
| 773 : |
anton
|
1.120
|
['] output-label2 output-c-tail1-no-stores ; |
| 774 : |
anton
|
1.108
|
|
| 775 : |
anton
|
1.85
|
: type-c-code ( c-addr u xt -- ) |
| 776 : |
anton
|
1.109
|
\ like TYPE, but replaces "INST_TAIL;" with tail code produced by xt |
| 777 : |
anton
|
1.85
|
{ xt } |
| 778 : |
anton
|
1.111
|
." {" cr |
| 779 : |
|
|
." #line " c-line @ . quote c-filename 2@ type quote cr |
| 780 : |
anton
|
1.52
|
begin ( c-addr1 u1 ) |
| 781 : |
anton
|
1.109
|
2dup s" INST_TAIL;" search |
| 782 : |
anton
|
1.52
|
while ( c-addr1 u1 c-addr3 u3 ) |
| 783 : |
|
|
2dup 2>r drop nip over - type |
| 784 : |
anton
|
1.85
|
xt execute |
| 785 : |
anton
|
1.109
|
2r> 10 /string |
| 786 : |
anton
|
1.52
|
\ !! resync #line missing |
| 787 : |
|
|
repeat |
| 788 : |
anton
|
1.111
|
2drop type |
| 789 : |
|
|
." #line " out-nls @ 2 + . quote out-filename 2@ type quote cr |
| 790 : |
|
|
." }" cr ; |
| 791 : |
anton
|
1.63
|
|
| 792 : |
anton
|
1.72
|
: print-entry ( -- ) |
| 793 : |
anton
|
1.109
|
." LABEL(" prim prim-c-name 2@ type ." )" ; |
| 794 : |
anton
|
1.63
|
|
| 795 : |
jwilke
|
1.43
|
: output-c ( -- ) |
| 796 : |
anton
|
1.111
|
print-entry ." /* " prim prim-name 2@ type ." ( " prim prim-stack-string 2@ type ." ) */" cr |
| 797 : |
|
|
." /* " prim prim-doc 2@ type ." */" cr |
| 798 : |
|
|
." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging |
| 799 : |
|
|
." {" cr |
| 800 : |
|
|
." DEF_CA" cr |
| 801 : |
|
|
print-declarations |
| 802 : |
|
|
." NEXT_P0;" cr |
| 803 : |
|
|
flush-tos |
| 804 : |
|
|
fetches |
| 805 : |
|
|
print-debug-args |
| 806 : |
|
|
stack-pointer-updates |
| 807 : |
|
|
prim prim-c-code 2@ ['] output-c-tail type-c-code |
| 808 : |
|
|
output-c-tail2 |
| 809 : |
|
|
." }" cr |
| 810 : |
|
|
cr |
| 811 : |
anton
|
1.1
|
; |
| 812 : |
|
|
|
| 813 : |
anton
|
1.56
|
: disasm-arg { item -- } |
| 814 : |
|
|
item item-stack @ inst-stream = if |
| 815 : |
anton
|
1.107
|
." {" cr |
| 816 : |
|
|
item print-declaration |
| 817 : |
|
|
item fetch |
| 818 : |
|
|
item print-debug-arg |
| 819 : |
|
|
." }" cr |
| 820 : |
anton
|
1.56
|
endif ; |
| 821 : |
|
|
|
| 822 : |
|
|
: disasm-args ( -- ) |
| 823 : |
anton
|
1.69
|
prim prim-effect-in prim prim-effect-in-end @ ['] disasm-arg map-items ; |
| 824 : |
anton
|
1.56
|
|
| 825 : |
|
|
: output-disasm ( -- ) |
| 826 : |
|
|
\ generate code for disassembling VM instructions |
| 827 : |
anton
|
1.106
|
." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr |
| 828 : |
anton
|
1.69
|
." fputs(" quote prim prim-name 2@ type quote ." , vm_out);" cr |
| 829 : |
anton
|
1.56
|
disasm-args |
| 830 : |
|
|
." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr |
| 831 : |
anton
|
1.91
|
." goto _endif_;" cr |
| 832 : |
|
|
." }" cr ; |
| 833 : |
anton
|
1.56
|
|
| 834 : |
anton
|
1.86
|
: output-profile ( -- ) |
| 835 : |
|
|
\ generate code for postprocessing the VM block profile stuff |
| 836 : |
anton
|
1.87
|
." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr |
| 837 : |
anton
|
1.104
|
." add_inst(b, " quote prim prim-name 2@ type quote ." );" cr |
| 838 : |
anton
|
1.86
|
." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr |
| 839 : |
|
|
prim prim-c-code 2@ s" SET_IP" search nip nip |
| 840 : |
|
|
prim prim-c-code 2@ s" SUPER_END" search nip nip or if |
| 841 : |
|
|
." return;" cr |
| 842 : |
anton
|
1.91
|
else |
| 843 : |
|
|
." goto _endif_;" cr |
| 844 : |
anton
|
1.86
|
endif |
| 845 : |
anton
|
1.91
|
." }" cr ; |
| 846 : |
anton
|
1.86
|
|
| 847 : |
anton
|
1.114
|
: output-profile-part ( p ) |
| 848 : |
|
|
." add_inst(b, " quote |
| 849 : |
|
|
prim-name 2@ type |
| 850 : |
|
|
quote ." );" cr ; |
| 851 : |
|
|
|
| 852 : |
anton
|
1.104
|
: output-profile-combined ( -- ) |
| 853 : |
|
|
\ generate code for postprocessing the VM block profile stuff |
| 854 : |
|
|
." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr |
| 855 : |
anton
|
1.114
|
['] output-profile-part map-combined |
| 856 : |
anton
|
1.104
|
." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr |
| 857 : |
|
|
combined-prims num-combined @ 1- th @ prim-c-code 2@ s" SET_IP" search nip nip |
| 858 : |
|
|
combined-prims num-combined @ 1- th @ prim-c-code 2@ s" SUPER_END" search nip nip or if |
| 859 : |
|
|
." return;" cr |
| 860 : |
|
|
else |
| 861 : |
|
|
." goto _endif_;" cr |
| 862 : |
|
|
endif |
| 863 : |
|
|
." }" cr ; |
| 864 : |
|
|
|
| 865 : |
anton
|
1.103
|
: output-superend ( -- ) |
| 866 : |
|
|
\ output flag specifying whether the current word ends a dynamic superinst |
| 867 : |
|
|
prim prim-c-code 2@ s" SET_IP" search nip nip |
| 868 : |
|
|
prim prim-c-code 2@ s" SUPER_END" search nip nip or 0<> |
| 869 : |
|
|
prim prim-c-code 2@ s" SUPER_CONTINUE" search nip nip 0= and |
| 870 : |
|
|
negate 0 .r ." , /* " prim prim-name 2@ type ." */" cr ; |
| 871 : |
|
|
|
| 872 : |
anton
|
1.60
|
: gen-arg-parm { item -- } |
| 873 : |
|
|
item item-stack @ inst-stream = if |
| 874 : |
|
|
." , " item item-type @ type-c-name 2@ type space |
| 875 : |
|
|
item item-name 2@ type |
| 876 : |
|
|
endif ; |
| 877 : |
|
|
|
| 878 : |
|
|
: gen-args-parm ( -- ) |
| 879 : |
anton
|
1.69
|
prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-parm map-items ; |
| 880 : |
anton
|
1.60
|
|
| 881 : |
|
|
: gen-arg-gen { item -- } |
| 882 : |
|
|
item item-stack @ inst-stream = if |
| 883 : |
|
|
." genarg_" item item-type @ print-type-prefix |
| 884 : |
|
|
." (ctp, " item item-name 2@ type ." );" cr |
| 885 : |
|
|
endif ; |
| 886 : |
|
|
|
| 887 : |
|
|
: gen-args-gen ( -- ) |
| 888 : |
anton
|
1.69
|
prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-gen map-items ; |
| 889 : |
anton
|
1.60
|
|
| 890 : |
|
|
: output-gen ( -- ) |
| 891 : |
|
|
\ generate C code for generating VM instructions |
| 892 : |
anton
|
1.69
|
." void gen_" prim prim-c-name 2@ type ." (Inst **ctp" gen-args-parm ." )" cr |
| 893 : |
anton
|
1.60
|
." {" cr |
| 894 : |
|
|
." gen_inst(ctp, vm_prim[" function-number @ 0 .r ." ]);" cr |
| 895 : |
|
|
gen-args-gen |
| 896 : |
anton
|
1.68
|
." }" cr ; |
| 897 : |
anton
|
1.60
|
|
| 898 : |
anton
|
1.49
|
: stack-used? { stack -- f } |
| 899 : |
|
|
stack stack-in @ stack stack-out @ or 0<> ; |
| 900 : |
jwilke
|
1.44
|
|
| 901 : |
pazsan
|
1.30
|
: output-funclabel ( -- ) |
| 902 : |
anton
|
1.69
|
." &I_" prim prim-c-name 2@ type ." ," cr ; |
| 903 : |
pazsan
|
1.30
|
|
| 904 : |
|
|
: output-forthname ( -- ) |
| 905 : |
anton
|
1.69
|
'" emit prim prim-name 2@ type '" emit ." ," cr ; |
| 906 : |
pazsan
|
1.30
|
|
| 907 : |
anton
|
1.92
|
\ : output-c-func ( -- ) |
| 908 : |
|
|
\ \ used for word libraries |
| 909 : |
|
|
\ ." Cell * I_" prim prim-c-name 2@ type ." (Cell *SP, Cell **FP) /* " prim prim-name 2@ type |
| 910 : |
|
|
\ ." ( " prim prim-stack-string 2@ type ." ) */" cr |
| 911 : |
|
|
\ ." /* " prim prim-doc 2@ type ." */" cr |
| 912 : |
|
|
\ ." NAME(" quote prim prim-name 2@ type quote ." )" cr |
| 913 : |
|
|
\ \ debugging |
| 914 : |
|
|
\ ." {" cr |
| 915 : |
|
|
\ print-declarations |
| 916 : |
|
|
\ \ !! don't know what to do about that |
| 917 : |
|
|
\ inst-stream stack-used? IF ." Cell *ip=IP;" cr THEN |
| 918 : |
|
|
\ data-stack stack-used? IF ." Cell *sp=SP;" cr THEN |
| 919 : |
|
|
\ fp-stack stack-used? IF ." Cell *fp=*FP;" cr THEN |
| 920 : |
|
|
\ return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN |
| 921 : |
|
|
\ flush-tos |
| 922 : |
|
|
\ fetches |
| 923 : |
|
|
\ stack-pointer-updates |
| 924 : |
|
|
\ fp-stack stack-used? IF ." *FP=fp;" cr THEN |
| 925 : |
|
|
\ ." {" cr |
| 926 : |
|
|
\ ." #line " c-line @ . quote c-filename 2@ type quote cr |
| 927 : |
|
|
\ prim prim-c-code 2@ type |
| 928 : |
|
|
\ ." }" cr |
| 929 : |
|
|
\ stores |
| 930 : |
|
|
\ fill-tos |
| 931 : |
|
|
\ ." return (sp);" cr |
| 932 : |
|
|
\ ." }" cr |
| 933 : |
|
|
\ cr ; |
| 934 : |
pazsan
|
1.30
|
|
| 935 : |
jwilke
|
1.43
|
: output-label ( -- ) |
| 936 : |
anton
|
1.127
|
." INST_ADDR(" prim prim-c-name 2@ type ." )," cr ; |
| 937 : |
anton
|
1.1
|
|
| 938 : |
jwilke
|
1.43
|
: output-alias ( -- ) |
| 939 : |
anton
|
1.69
|
( primitive-number @ . ." alias " ) ." Primitive " prim prim-name 2@ type cr ; |
| 940 : |
anton
|
1.1
|
|
| 941 : |
anton
|
1.122
|
: output-c-prim-num ( -- ) |
| 942 : |
|
|
." #define N_" prim prim-c-name 2@ type prim prim-num @ 8 + 4 .r cr ; |
| 943 : |
anton
|
1.114
|
|
| 944 : |
jwilke
|
1.43
|
: output-forth ( -- ) |
| 945 : |
anton
|
1.69
|
prim prim-forth-code @ 0= |
| 946 : |
pazsan
|
1.30
|
IF \ output-alias |
| 947 : |
jwilke
|
1.28
|
\ this is bad for ec: an alias is compiled if tho word does not exist! |
| 948 : |
|
|
\ JAW |
| 949 : |
anton
|
1.69
|
ELSE ." : " prim prim-name 2@ type ." ( " |
| 950 : |
|
|
prim prim-stack-string 2@ type ." )" cr |
| 951 : |
|
|
prim prim-forth-code 2@ type cr |
| 952 : |
pazsan
|
1.30
|
THEN ; |
| 953 : |
anton
|
1.10
|
|
| 954 : |
anton
|
1.17
|
: output-tag-file ( -- ) |
| 955 : |
pazsan
|
1.117
|
name-filename 2@ last-name-filename 2@ compare if |
| 956 : |
anton
|
1.17
|
name-filename 2@ last-name-filename 2! |
| 957 : |
|
|
#ff emit cr |
| 958 : |
|
|
name-filename 2@ type |
| 959 : |
|
|
." ,0" cr |
| 960 : |
|
|
endif ; |
| 961 : |
|
|
|
| 962 : |
|
|
: output-tag ( -- ) |
| 963 : |
|
|
output-tag-file |
| 964 : |
anton
|
1.69
|
prim prim-name 2@ 1+ type |
| 965 : |
anton
|
1.17
|
127 emit |
| 966 : |
anton
|
1.69
|
space prim prim-name 2@ type space |
| 967 : |
anton
|
1.17
|
1 emit |
| 968 : |
|
|
name-line @ 0 .r |
| 969 : |
|
|
." ,0" cr ; |
| 970 : |
|
|
|
| 971 : |
pazsan
|
1.100
|
: output-vi-tag ( -- ) |
| 972 : |
|
|
name-filename 2@ type #tab emit |
| 973 : |
|
|
prim prim-name 2@ type #tab emit |
| 974 : |
|
|
." /^" prim prim-name 2@ type ." *(/" cr ; |
| 975 : |
|
|
|
| 976 : |
anton
|
1.10
|
[IFDEF] documentation |
| 977 : |
|
|
: register-doc ( -- ) |
| 978 : |
anton
|
1.82
|
prim prim-name 2@ documentation ['] create insert-wordlist |
| 979 : |
anton
|
1.69
|
prim prim-name 2@ 2, |
| 980 : |
|
|
prim prim-stack-string 2@ condition-stack-effect 2, |
| 981 : |
|
|
prim prim-wordset 2@ 2, |
| 982 : |
|
|
prim prim-c-name 2@ condition-pronounciation 2, |
| 983 : |
anton
|
1.82
|
prim prim-doc 2@ 2, ; |
| 984 : |
anton
|
1.10
|
[THEN] |
| 985 : |
anton
|
1.67
|
|
| 986 : |
|
|
|
| 987 : |
anton
|
1.69
|
\ combining instructions |
| 988 : |
|
|
|
| 989 : |
|
|
\ The input should look like this: |
| 990 : |
|
|
|
| 991 : |
|
|
\ lit_+ = lit + |
| 992 : |
|
|
|
| 993 : |
|
|
\ The output should look like this: |
| 994 : |
|
|
|
| 995 : |
|
|
\ I_lit_+: |
| 996 : |
|
|
\ { |
| 997 : |
|
|
\ DEF_CA |
| 998 : |
|
|
\ Cell _x_ip0; |
| 999 : |
|
|
\ Cell _x_sp0; |
| 1000 : |
|
|
\ Cell _x_sp1; |
| 1001 : |
|
|
\ NEXT_P0; |
| 1002 : |
|
|
\ _x_ip0 = (Cell) IPTOS; |
| 1003 : |
|
|
\ _x_sp0 = (Cell) spTOS; |
| 1004 : |
|
|
\ INC_IP(1); |
| 1005 : |
|
|
\ /* sp += 0; */ |
| 1006 : |
|
|
\ /* lit ( #w -- w ) */ |
| 1007 : |
|
|
\ /* */ |
| 1008 : |
|
|
\ NAME("lit") |
| 1009 : |
|
|
\ { |
| 1010 : |
|
|
\ Cell w; |
| 1011 : |
|
|
\ w = (Cell) _x_ip0; |
| 1012 : |
|
|
\ #ifdef VM_DEBUG |
| 1013 : |
|
|
\ if (vm_debug) { |
| 1014 : |
|
|
\ fputs(" w=", vm_out); printarg_w (w); |
| 1015 : |
|
|
\ fputc('\n', vm_out); |
| 1016 : |
|
|
\ } |
| 1017 : |
|
|
\ #endif |
| 1018 : |
|
|
\ { |
| 1019 : |
|
|
\ #line 136 "./prim" |
| 1020 : |
|
|
\ } |
| 1021 : |
|
|
\ _x_sp1 = (Cell)w; |
| 1022 : |
|
|
\ } |
| 1023 : |
|
|
\ I_plus: /* + ( n1 n2 -- n ) */ |
| 1024 : |
|
|
\ /* */ |
| 1025 : |
|
|
\ NAME("+") |
| 1026 : |
|
|
\ { |
| 1027 : |
|
|
\ DEF_CA |
| 1028 : |
|
|
\ Cell n1; |
| 1029 : |
|
|
\ Cell n2; |
| 1030 : |
|
|
\ Cell n; |
| 1031 : |
|
|
\ NEXT_P0; |
| 1032 : |
|
|
\ n1 = (Cell) _x_sp0; |
| 1033 : |
|
|
\ n2 = (Cell) _x_sp1; |
| 1034 : |
|
|
\ #ifdef VM_DEBUG |
| 1035 : |
|
|
\ if (vm_debug) { |
| 1036 : |
|
|
\ fputs(" n1=", vm_out); printarg_n (n1); |
| 1037 : |
|
|
\ fputs(" n2=", vm_out); printarg_n (n2); |
| 1038 : |
|
|
\ fputc('\n', vm_out); |
| 1039 : |
|
|
\ } |
| 1040 : |
|
|
\ #endif |
| 1041 : |
|
|
\ { |
| 1042 : |
|
|
\ #line 516 "./prim" |
| 1043 : |
|
|
\ n = n1+n2; |
| 1044 : |
|
|
\ } |
| 1045 : |
|
|
\ _x_sp0 = (Cell)n; |
| 1046 : |
|
|
\ } |
| 1047 : |
|
|
\ NEXT_P1; |
| 1048 : |
|
|
\ spTOS = (Cell)_x_sp0; |
| 1049 : |
|
|
\ NEXT_P2; |
| 1050 : |
|
|
|
| 1051 : |
anton
|
1.71
|
: init-combined ( -- ) |
| 1052 : |
anton
|
1.79
|
prim to combined |
| 1053 : |
anton
|
1.71
|
0 num-combined ! |
| 1054 : |
|
|
current-depth max-stacks cells erase |
| 1055 : |
anton
|
1.116
|
include-skipped-insts @ current-depth 0 th ! |
| 1056 : |
anton
|
1.72
|
max-depth max-stacks cells erase |
| 1057 : |
|
|
min-depth max-stacks cells erase |
| 1058 : |
|
|
prim prim-effect-in prim prim-effect-in-end ! |
| 1059 : |
|
|
prim prim-effect-out prim prim-effect-out-end ! ; |
| 1060 : |
anton
|
1.71
|
|
| 1061 : |
|
|
: max! ( n addr -- ) |
| 1062 : |
|
|
tuck @ max swap ! ; |
| 1063 : |
|
|
|
| 1064 : |
anton
|
1.72
|
: min! ( n addr -- ) |
| 1065 : |
|
|
tuck @ min swap ! ; |
| 1066 : |
|
|
|
| 1067 : |
anton
|
1.119
|
: inst-stream-adjustment ( nstack -- n ) |
| 1068 : |
|
|
\ number of stack items to add for each part |
| 1069 : |
|
|
0= include-skipped-insts @ and negate ; |
| 1070 : |
anton
|
1.116
|
|
| 1071 : |
anton
|
1.71
|
: add-depths { p -- } |
| 1072 : |
|
|
\ combine stack effect of p with *-depths |
| 1073 : |
|
|
max-stacks 0 ?do |
| 1074 : |
anton
|
1.72
|
current-depth i th @ |
| 1075 : |
anton
|
1.119
|
p prim-stacks-in i th @ + i inst-stream-adjustment + |
| 1076 : |
anton
|
1.72
|
dup max-depth i th max! |
| 1077 : |
|
|
p prim-stacks-out i th @ - |
| 1078 : |
|
|
dup min-depth i th min! |
| 1079 : |
|
|
current-depth i th ! |
| 1080 : |
anton
|
1.71
|
loop ; |
| 1081 : |
|
|
|
| 1082 : |
anton
|
1.118
|
: copy-maxdepths ( n -- ) |
| 1083 : |
|
|
max-depth max-depths rot max-stacks * th max-stacks cells move ; |
| 1084 : |
|
|
|
| 1085 : |
anton
|
1.71
|
: add-prim ( addr u -- ) |
| 1086 : |
|
|
\ add primitive given by "addr u" to combined-prims |
| 1087 : |
|
|
primitives search-wordlist s" unknown primitive" ?print-error |
| 1088 : |
|
|
execute { p } |
| 1089 : |
anton
|
1.72
|
p combined-prims num-combined @ th ! |
| 1090 : |
anton
|
1.118
|
num-combined @ copy-maxdepths |
| 1091 : |
anton
|
1.71
|
1 num-combined +! |
| 1092 : |
anton
|
1.118
|
p add-depths |
| 1093 : |
|
|
num-combined @ copy-maxdepths ; |
| 1094 : |
anton
|
1.71
|
|
| 1095 : |
|
|
: compute-effects { q -- } |
| 1096 : |
|
|
\ compute the stack effects of q from the depths |
| 1097 : |
|
|
max-stacks 0 ?do |
| 1098 : |
anton
|
1.72
|
max-depth i th @ dup |
| 1099 : |
|
|
q prim-stacks-in i th ! |
| 1100 : |
|
|
current-depth i th @ - |
| 1101 : |
|
|
q prim-stacks-out i th ! |
| 1102 : |
|
|
loop ; |
| 1103 : |
|
|
|
| 1104 : |
|
|
: make-effect-items { stack# items effect-endp -- } |
| 1105 : |
|
|
\ effect-endp points to a pointer to the end of the current item-array |
| 1106 : |
|
|
\ and has to be updated |
| 1107 : |
|
|
stacks stack# th @ { stack } |
| 1108 : |
|
|
items 0 +do |
| 1109 : |
|
|
effect-endp @ { item } |
| 1110 : |
|
|
i 0 <# #s stack stack-pointer 2@ holds [char] _ hold #> save-mem |
| 1111 : |
|
|
item item-name 2! |
| 1112 : |
|
|
stack item item-stack ! |
| 1113 : |
anton
|
1.74
|
stack stack-type @ item item-type ! |
| 1114 : |
anton
|
1.72
|
i item item-offset ! |
| 1115 : |
|
|
item item-first on |
| 1116 : |
|
|
item% %size effect-endp +! |
| 1117 : |
|
|
loop ; |
| 1118 : |
|
|
|
| 1119 : |
|
|
: init-effects { q -- } |
| 1120 : |
|
|
\ initialize effects field for FETCHES and STORES |
| 1121 : |
|
|
max-stacks 0 ?do |
| 1122 : |
|
|
i q prim-stacks-in i th @ q prim-effect-in-end make-effect-items |
| 1123 : |
|
|
i q prim-stacks-out i th @ q prim-effect-out-end make-effect-items |
| 1124 : |
anton
|
1.71
|
loop ; |
| 1125 : |
|
|
|
| 1126 : |
anton
|
1.119
|
: compute-stack-max-back-depths ( stack -- ) |
| 1127 : |
|
|
stack-number @ { stack# } |
| 1128 : |
|
|
current-depth stack# th @ dup |
| 1129 : |
|
|
dup stack# num-combined @ s-c-max-back-depth ! |
| 1130 : |
|
|
-1 num-combined @ 1- -do ( max-depth current-depth ) |
| 1131 : |
|
|
combined-prims i th @ { p } |
| 1132 : |
|
|
p prim-stacks-out stack# th @ + |
| 1133 : |
|
|
dup >r max r> |
| 1134 : |
|
|
over stack# i s-c-max-back-depth ! |
| 1135 : |
|
|
p prim-stacks-in stack# th @ - |
| 1136 : |
|
|
stack# inst-stream-adjustment - |
| 1137 : |
|
|
1 -loop |
| 1138 : |
|
|
assert( dup stack# inst-stream-adjustment negate = ) |
| 1139 : |
|
|
assert( over max-depth stack# th @ = ) |
| 1140 : |
|
|
2drop ; |
| 1141 : |
|
|
|
| 1142 : |
|
|
: compute-max-back-depths ( -- ) |
| 1143 : |
|
|
\ compute max-back-depths. |
| 1144 : |
|
|
\ assumes that current-depths is correct for the end of the combination |
| 1145 : |
|
|
['] compute-stack-max-back-depths map-stacks ; |
| 1146 : |
|
|
|
| 1147 : |
anton
|
1.71
|
: process-combined ( -- ) |
| 1148 : |
anton
|
1.81
|
combined combined-prims num-combined @ cells |
| 1149 : |
anton
|
1.82
|
combinations ['] constant insert-wordlist |
| 1150 : |
anton
|
1.86
|
combined-prims num-combined @ 1- th ( last-part ) |
| 1151 : |
|
|
@ prim-c-code 2@ prim prim-c-code 2! \ used by output-super-end |
| 1152 : |
anton
|
1.72
|
prim compute-effects |
| 1153 : |
|
|
prim init-effects |
| 1154 : |
anton
|
1.119
|
compute-max-back-depths |
| 1155 : |
anton
|
1.72
|
output-combined perform ; |
| 1156 : |
|
|
|
| 1157 : |
|
|
\ C output |
| 1158 : |
|
|
|
| 1159 : |
|
|
: print-item { n stack -- } |
| 1160 : |
|
|
\ print nth stack item name |
| 1161 : |
anton
|
1.79
|
stack stack-type @ type-c-name 2@ type space |
| 1162 : |
|
|
." _" stack stack-pointer 2@ type n 0 .r ; |
| 1163 : |
anton
|
1.72
|
|
| 1164 : |
|
|
: print-declarations-combined ( -- ) |
| 1165 : |
|
|
max-stacks 0 ?do |
| 1166 : |
|
|
max-depth i th @ min-depth i th @ - 0 +do |
| 1167 : |
|
|
i stacks j th @ print-item ." ;" cr |
| 1168 : |
|
|
loop |
| 1169 : |
|
|
loop ; |
| 1170 : |
anton
|
1.79
|
|
| 1171 : |
|
|
: part-fetches ( -- ) |
| 1172 : |
|
|
fetches ; |
| 1173 : |
|
|
|
| 1174 : |
|
|
: part-output-c-tail ( -- ) |
| 1175 : |
anton
|
1.91
|
print-debug-results |
| 1176 : |
anton
|
1.85
|
stores ; |
| 1177 : |
|
|
|
| 1178 : |
|
|
: output-combined-tail ( -- ) |
| 1179 : |
|
|
part-output-c-tail |
| 1180 : |
|
|
in-part @ >r in-part off |
| 1181 : |
anton
|
1.119
|
combined ['] output-c-tail-no-stores prim-context |
| 1182 : |
anton
|
1.118
|
r> in-part ! ; |
| 1183 : |
|
|
|
| 1184 : |
|
|
: part-stack-pointer-updates ( -- ) |
| 1185 : |
anton
|
1.123
|
next-stack-number @ 0 +do |
| 1186 : |
anton
|
1.118
|
i part-num @ 1+ s-c-max-depth @ dup |
| 1187 : |
|
|
i num-combined @ s-c-max-depth @ = \ final depth |
| 1188 : |
|
|
swap i part-num @ s-c-max-depth @ <> \ just reached now |
| 1189 : |
|
|
part-num @ 0= \ first part |
| 1190 : |
|
|
or and if |
| 1191 : |
|
|
stacks i th @ stack-pointer-update |
| 1192 : |
|
|
endif |
| 1193 : |
|
|
loop ; |
| 1194 : |
anton
|
1.79
|
|
| 1195 : |
|
|
: output-part ( p -- ) |
| 1196 : |
|
|
to prim |
| 1197 : |
|
|
." /* " prim prim-name 2@ type ." ( " prim prim-stack-string 2@ type ." ) */" cr |
| 1198 : |
|
|
." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging |
| 1199 : |
|
|
." {" cr |
| 1200 : |
|
|
print-declarations |
| 1201 : |
|
|
part-fetches |
| 1202 : |
|
|
print-debug-args |
| 1203 : |
anton
|
1.118
|
combined ['] part-stack-pointer-updates prim-context |
| 1204 : |
|
|
1 part-num +! |
| 1205 : |
anton
|
1.79
|
prim add-depths \ !! right place? |
| 1206 : |
anton
|
1.85
|
prim prim-c-code 2@ ['] output-combined-tail type-c-code |
| 1207 : |
anton
|
1.79
|
part-output-c-tail |
| 1208 : |
|
|
." }" cr ; |
| 1209 : |
|
|
|
| 1210 : |
anton
|
1.74
|
: output-parts ( -- ) |
| 1211 : |
anton
|
1.79
|
prim >r in-part on |
| 1212 : |
|
|
current-depth max-stacks cells erase |
| 1213 : |
anton
|
1.118
|
0 part-num ! |
| 1214 : |
anton
|
1.114
|
['] output-part map-combined |
| 1215 : |
anton
|
1.79
|
in-part off |
| 1216 : |
anton
|
1.74
|
r> to prim ; |
| 1217 : |
|
|
|
| 1218 : |
anton
|
1.72
|
: output-c-combined ( -- ) |
| 1219 : |
|
|
print-entry cr |
| 1220 : |
anton
|
1.74
|
\ debugging messages just in parts |
| 1221 : |
anton
|
1.72
|
." {" cr |
| 1222 : |
|
|
." DEF_CA" cr |
| 1223 : |
|
|
print-declarations-combined |
| 1224 : |
|
|
." NEXT_P0;" cr |
| 1225 : |
|
|
flush-tos |
| 1226 : |
anton
|
1.118
|
\ fetches \ now in parts |
| 1227 : |
anton
|
1.74
|
\ print-debug-args |
| 1228 : |
anton
|
1.118
|
\ stack-pointer-updates now in parts |
| 1229 : |
anton
|
1.74
|
output-parts |
| 1230 : |
anton
|
1.119
|
output-c-tail2-no-stores |
| 1231 : |
anton
|
1.74
|
." }" cr |
| 1232 : |
|
|
cr ; |
| 1233 : |
anton
|
1.72
|
|
| 1234 : |
|
|
: output-forth-combined ( -- ) |
| 1235 : |
anton
|
1.81
|
; |
| 1236 : |
|
|
|
| 1237 : |
|
|
|
| 1238 : |
anton
|
1.83
|
\ peephole optimization rules |
| 1239 : |
anton
|
1.81
|
|
| 1240 : |
anton
|
1.114
|
\ data for a simple peephole optimizer that always tries to combine |
| 1241 : |
|
|
\ the currently compiled instruction with the last one. |
| 1242 : |
|
|
|
| 1243 : |
anton
|
1.81
|
\ in order for this to work as intended, shorter combinations for each |
| 1244 : |
|
|
\ length must be present, and the longer combinations must follow |
| 1245 : |
|
|
\ shorter ones (this restriction may go away in the future). |
| 1246 : |
|
|
|
| 1247 : |
anton
|
1.83
|
: output-peephole ( -- ) |
| 1248 : |
anton
|
1.81
|
combined-prims num-combined @ 1- cells combinations search-wordlist |
| 1249 : |
anton
|
1.114
|
s" the prefix for this superinstruction must be defined earlier" ?print-error |
| 1250 : |
anton
|
1.82
|
." {" |
| 1251 : |
|
|
execute prim-num @ 5 .r ." ," |
| 1252 : |
|
|
combined-prims num-combined @ 1- th @ prim-num @ 5 .r ." ," |
| 1253 : |
|
|
combined prim-num @ 5 .r ." }, /* " |
| 1254 : |
|
|
combined prim-c-name 2@ type ." */" |
| 1255 : |
|
|
cr ; |
| 1256 : |
|
|
|
| 1257 : |
anton
|
1.114
|
|
| 1258 : |
anton
|
1.115
|
\ cost and superinstruction data for a sophisticated combiner (e.g., |
| 1259 : |
|
|
\ shortest path) |
| 1260 : |
anton
|
1.114
|
|
| 1261 : |
|
|
\ This is intended as initializer for a structure like this |
| 1262 : |
|
|
|
| 1263 : |
anton
|
1.116
|
\ struct cost { |
| 1264 : |
anton
|
1.114
|
\ int loads; /* number of stack loads */ |
| 1265 : |
|
|
\ int stores; /* number of stack stores */ |
| 1266 : |
|
|
\ int updates; /* number of stack pointer updates */ |
| 1267 : |
|
|
\ int length; /* number of components */ |
| 1268 : |
|
|
\ int *components; /* array of vm_prim indexes of components */ |
| 1269 : |
|
|
\ }; |
| 1270 : |
|
|
|
| 1271 : |
anton
|
1.115
|
\ How do you know which primitive or combined instruction this |
| 1272 : |
|
|
\ structure refers to? By the order of cost structures, as in most |
| 1273 : |
|
|
\ other cases. |
| 1274 : |
|
|
|
| 1275 : |
|
|
: compute-costs { p -- nloads nstores nupdates } |
| 1276 : |
|
|
\ compute the number of loads, stores, and stack pointer updates |
| 1277 : |
|
|
\ of a primitive or combined instruction; does not take TOS |
| 1278 : |
|
|
\ caching into account, nor that IP updates are combined with |
| 1279 : |
|
|
\ other stuff |
| 1280 : |
|
|
0 max-stacks 0 +do |
| 1281 : |
|
|
p prim-stacks-in i th @ + |
| 1282 : |
|
|
loop |
| 1283 : |
|
|
0 max-stacks 0 +do |
| 1284 : |
|
|
p prim-stacks-out i th @ + |
| 1285 : |
|
|
loop |
| 1286 : |
|
|
0 max-stacks 0 +do |
| 1287 : |
|
|
p prim-stacks-in i th @ p prim-stacks-out i th @ <> - |
| 1288 : |
|
|
loop ; |
| 1289 : |
anton
|
1.114
|
|
| 1290 : |
|
|
: output-num-part ( p -- ) |
| 1291 : |
|
|
prim-num @ 4 .r ." ," ; |
| 1292 : |
|
|
|
| 1293 : |
anton
|
1.115
|
: output-costs ( -- ) |
| 1294 : |
|
|
." {" prim compute-costs |
| 1295 : |
|
|
rot 2 .r ." ," swap 2 .r ." ," 2 .r ." ," |
| 1296 : |
|
|
combined if |
| 1297 : |
|
|
num-combined @ 2 .r |
| 1298 : |
|
|
." , ((int []){" ['] output-num-part map-combined ." })}, /* " |
| 1299 : |
|
|
else |
| 1300 : |
|
|
." 1, ((int []){" prim prim-num @ 4 .r ." })}, /* " |
| 1301 : |
|
|
endif |
| 1302 : |
|
|
prim prim-name 2@ type ." */" |
| 1303 : |
pazsan
|
1.90
|
cr ; |
| 1304 : |
anton
|
1.69
|
|
| 1305 : |
anton
|
1.67
|
\ the parser |
| 1306 : |
|
|
|
| 1307 : |
|
|
eof-char max-member \ the whole character set + EOF |
| 1308 : |
|
|
|
| 1309 : |
|
|
: getinput ( -- n ) |
| 1310 : |
|
|
rawinput @ endrawinput @ = |
| 1311 : |
|
|
if |
| 1312 : |
|
|
eof-char |
| 1313 : |
|
|
else |
| 1314 : |
|
|
cookedinput @ c@ |
| 1315 : |
|
|
endif ; |
| 1316 : |
|
|
|
| 1317 : |
|
|
:noname ( n -- ) |
| 1318 : |
|
|
dup bl > if |
| 1319 : |
|
|
emit space |
| 1320 : |
|
|
else |
| 1321 : |
|
|
. |
| 1322 : |
|
|
endif ; |
| 1323 : |
|
|
print-token ! |
| 1324 : |
|
|
|
| 1325 : |
|
|
: testchar? ( set -- f ) |
| 1326 : |
|
|
getinput member? ; |
| 1327 : |
|
|
' testchar? test-vector ! |
| 1328 : |
|
|
|
| 1329 : |
anton
|
1.130
|
: checksynclines ( -- ) |
| 1330 : |
anton
|
1.67
|
\ when input points to a newline, check if the next line is a |
| 1331 : |
|
|
\ sync line. If it is, perform the appropriate actions. |
| 1332 : |
anton
|
1.131
|
rawinput @ begin >r |
| 1333 : |
anton
|
1.130
|
s" #line " r@ over compare if |
| 1334 : |
|
|
rdrop 1 line +! EXIT |
| 1335 : |
|
|
endif |
| 1336 : |
|
|
0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr ) |
| 1337 : |
|
|
dup c@ bl = if |
| 1338 : |
|
|
char+ dup c@ [char] " <> 0= s" sync line syntax" ?print-error |
| 1339 : |
|
|
char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2! |
| 1340 : |
|
|
char+ |
| 1341 : |
|
|
endif |
| 1342 : |
|
|
dup c@ nl-char <> 0= s" sync line syntax" ?print-error |
| 1343 : |
|
|
skipsynclines @ if |
| 1344 : |
anton
|
1.131
|
char+ dup rawinput ! |
| 1345 : |
anton
|
1.130
|
rawinput @ c@ cookedinput @ c! |
| 1346 : |
|
|
endif |
| 1347 : |
|
|
again ; |
| 1348 : |
anton
|
1.67
|
|
| 1349 : |
|
|
: ?nextchar ( f -- ) |
| 1350 : |
anton
|
1.71
|
s" syntax error, wrong char" ?print-error |
| 1351 : |
anton
|
1.67
|
rawinput @ endrawinput @ <> if |
| 1352 : |
|
|
rawinput @ c@ |
| 1353 : |
|
|
1 chars rawinput +! |
| 1354 : |
|
|
1 chars cookedinput +! |
| 1355 : |
|
|
nl-char = if |
| 1356 : |
anton
|
1.130
|
checksynclines |
| 1357 : |
anton
|
1.67
|
rawinput @ line-start ! |
| 1358 : |
|
|
endif |
| 1359 : |
anton
|
1.130
|
rawinput @ c@ |
| 1360 : |
|
|
cookedinput @ c! |
| 1361 : |
anton
|
1.67
|
endif ; |
| 1362 : |
|
|
|
| 1363 : |
|
|
: charclass ( set "name" -- ) |
| 1364 : |
|
|
['] ?nextchar terminal ; |
| 1365 : |
|
|
|
| 1366 : |
|
|
: .. ( c1 c2 -- set ) |
| 1367 : |
|
|
( creates a set that includes the characters c, c1<=c<=c2 ) |
| 1368 : |
|
|
empty copy-set |
| 1369 : |
|
|
swap 1+ rot do |
| 1370 : |
|
|
i over add-member |
| 1371 : |
|
|
loop ; |
| 1372 : |
|
|
|
| 1373 : |
|
|
: ` ( -- terminal ) ( use: ` c ) |
| 1374 : |
|
|
( creates anonymous terminal for the character c ) |
| 1375 : |
|
|
char singleton ['] ?nextchar make-terminal ; |
| 1376 : |
|
|
|
| 1377 : |
|
|
char a char z .. char A char Z .. union char _ singleton union charclass letter |
| 1378 : |
|
|
char 0 char 9 .. charclass digit |
| 1379 : |
|
|
bl singleton tab-char over add-member charclass white |
| 1380 : |
|
|
nl-char singleton eof-char over add-member complement charclass nonl |
| 1381 : |
|
|
nl-char singleton eof-char over add-member |
| 1382 : |
|
|
char : over add-member complement charclass nocolonnl |
| 1383 : |
anton
|
1.110
|
nl-char singleton eof-char over add-member |
| 1384 : |
|
|
char } over add-member complement charclass nobracenl |
| 1385 : |
anton
|
1.67
|
bl 1+ maxchar .. char \ singleton complement intersection |
| 1386 : |
|
|
charclass nowhitebq |
| 1387 : |
|
|
bl 1+ maxchar .. charclass nowhite |
| 1388 : |
|
|
char " singleton eof-char over add-member complement charclass noquote |
| 1389 : |
|
|
nl-char singleton charclass nl |
| 1390 : |
|
|
eof-char singleton charclass eof |
| 1391 : |
anton
|
1.79
|
nl-char singleton eof-char over add-member charclass nleof |
| 1392 : |
anton
|
1.67
|
|
| 1393 : |
|
|
(( letter (( letter || digit )) ** |
| 1394 : |
|
|
)) <- c-ident ( -- ) |
| 1395 : |
|
|
|
| 1396 : |
anton
|
1.110
|
(( ` # ?? (( letter || digit || ` : )) ++ |
| 1397 : |
anton
|
1.67
|
)) <- stack-ident ( -- ) |
| 1398 : |
|
|
|
| 1399 : |
|
|
(( nowhitebq nowhite ** )) |
| 1400 : |
|
|
<- forth-ident ( -- ) |
| 1401 : |
|
|
|
| 1402 : |
|
|
Variable forth-flag |
| 1403 : |
|
|
Variable c-flag |
| 1404 : |
|
|
|
| 1405 : |
|
|
(( (( ` e || ` E )) {{ start }} nonl ** |
| 1406 : |
|
|
{{ end evaluate }} |
| 1407 : |
|
|
)) <- eval-comment ( ... -- ... ) |
| 1408 : |
|
|
|
| 1409 : |
|
|
(( (( ` f || ` F )) {{ start }} nonl ** |
| 1410 : |
|
|
{{ end forth-flag @ IF type cr ELSE 2drop THEN }} |
| 1411 : |
|
|
)) <- forth-comment ( -- ) |
| 1412 : |
|
|
|
| 1413 : |
|
|
(( (( ` c || ` C )) {{ start }} nonl ** |
| 1414 : |
|
|
{{ end c-flag @ IF type cr ELSE 2drop THEN }} |
| 1415 : |
|
|
)) <- c-comment ( -- ) |
| 1416 : |
|
|
|
| 1417 : |
|
|
(( ` - nonl ** {{ |
| 1418 : |
|
|
forth-flag @ IF ." [ELSE]" cr THEN |
| 1419 : |
|
|
c-flag @ IF ." #else" cr THEN }} |
| 1420 : |
|
|
)) <- else-comment |
| 1421 : |
|
|
|
| 1422 : |
|
|
(( ` + {{ start }} nonl ** {{ end |
| 1423 : |
|
|
dup |
| 1424 : |
|
|
IF c-flag @ |
| 1425 : |
|
|
IF ." #ifdef HAS_" bounds ?DO I c@ toupper emit LOOP cr |
| 1426 : |
|
|
THEN |
| 1427 : |
|
|
forth-flag @ |
| 1428 : |
|
|
IF ." has? " type ." [IF]" cr THEN |
| 1429 : |
|
|
ELSE 2drop |
| 1430 : |
|
|
c-flag @ IF ." #endif" cr THEN |
| 1431 : |
|
|
forth-flag @ IF ." [THEN]" cr THEN |
| 1432 : |
|
|
THEN }} |
| 1433 : |
|
|
)) <- if-comment |
| 1434 : |
|
|
|
| 1435 : |
pazsan
|
1.98
|
(( (( ` g || ` G )) {{ start }} nonl ** |
| 1436 : |
|
|
{{ end |
| 1437 : |
|
|
forth-flag @ IF ." group " type cr THEN |
| 1438 : |
pazsan
|
1.125
|
c-flag @ IF ." GROUP(" type ." , " function-number @ 0 .r ." )" cr THEN }} |
| 1439 : |
pazsan
|
1.98
|
)) <- group-comment |
| 1440 : |
|
|
|
| 1441 : |
|
|
(( (( eval-comment || forth-comment || c-comment || else-comment || if-comment || group-comment )) ?? nonl ** )) <- comment-body |
| 1442 : |
anton
|
1.67
|
|
| 1443 : |
anton
|
1.79
|
(( ` \ comment-body nleof )) <- comment ( -- ) |
| 1444 : |
anton
|
1.67
|
|
| 1445 : |
|
|
(( {{ start }} stack-ident {{ end 2 pick init-item item% %size + }} white ** )) ** |
| 1446 : |
|
|
<- stack-items |
| 1447 : |
|
|
|
| 1448 : |
anton
|
1.69
|
(( {{ prim prim-effect-in }} stack-items {{ prim prim-effect-in-end ! }} |
| 1449 : |
anton
|
1.67
|
` - ` - white ** |
| 1450 : |
anton
|
1.69
|
{{ prim prim-effect-out }} stack-items {{ prim prim-effect-out-end ! }} |
| 1451 : |
anton
|
1.67
|
)) <- stack-effect ( -- ) |
| 1452 : |
|
|
|
| 1453 : |
anton
|
1.71
|
(( {{ prim create-prim }} |
| 1454 : |
anton
|
1.69
|
` ( white ** {{ start }} stack-effect {{ end prim prim-stack-string 2! }} ` ) white ** |
| 1455 : |
|
|
(( {{ start }} forth-ident {{ end prim prim-wordset 2! }} white ** |
| 1456 : |
|
|
(( {{ start }} c-ident {{ end prim prim-c-name 2! }} )) ?? |
| 1457 : |
anton
|
1.79
|
)) ?? nleof |
| 1458 : |
|
|
(( ` " ` " {{ start }} (( noquote ++ ` " )) ++ {{ end 1- prim prim-doc 2! }} ` " white ** nleof )) ?? |
| 1459 : |
anton
|
1.110
|
{{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }} |
| 1460 : |
|
|
(( (( ` { nonl ** nleof (( (( nobracenl {{ line @ drop }} nonl ** )) ?? nleof )) ** ` } white ** nleof white ** )) |
| 1461 : |
|
|
|| (( nocolonnl nonl ** nleof white ** )) ** )) |
| 1462 : |
|
|
{{ end prim prim-c-code 2! skipsynclines on }} |
| 1463 : |
anton
|
1.79
|
(( ` : white ** nleof |
| 1464 : |
|
|
{{ start }} (( nonl ++ nleof white ** )) ++ {{ end prim prim-forth-code 2! }} |
| 1465 : |
anton
|
1.81
|
)) ?? {{ process-simple }} |
| 1466 : |
anton
|
1.79
|
nleof |
| 1467 : |
anton
|
1.69
|
)) <- simple-primitive ( -- ) |
| 1468 : |
|
|
|
| 1469 : |
anton
|
1.71
|
(( {{ init-combined }} |
| 1470 : |
anton
|
1.89
|
` = white ** (( {{ start }} forth-ident {{ end add-prim }} white ** )) ++ |
| 1471 : |
anton
|
1.79
|
nleof {{ process-combined }} |
| 1472 : |
anton
|
1.69
|
)) <- combined-primitive |
| 1473 : |
|
|
|
| 1474 : |
anton
|
1.79
|
(( {{ make-prim to prim 0 to combined |
| 1475 : |
anton
|
1.69
|
line @ name-line ! filename 2@ name-filename 2! |
| 1476 : |
anton
|
1.82
|
function-number @ prim prim-num ! |
| 1477 : |
anton
|
1.110
|
start }} [ifdef] vmgen c-ident [else] forth-ident [then] {{ end |
| 1478 : |
|
|
2dup prim prim-name 2! prim prim-c-name 2! }} white ** |
| 1479 : |
anton
|
1.104
|
(( ` / white ** {{ start }} c-ident {{ end prim prim-c-name 2! }} white ** )) ?? |
| 1480 : |
anton
|
1.82
|
(( simple-primitive || combined-primitive )) {{ 1 function-number +! }} |
| 1481 : |
anton
|
1.67
|
)) <- primitive ( -- ) |
| 1482 : |
|
|
|
| 1483 : |
|
|
(( (( comment || primitive || nl white ** )) ** eof )) |
| 1484 : |
|
|
parser primitives2something |
| 1485 : |
|
|
warnings @ [IF] |
| 1486 : |
|
|
.( parser generated ok ) cr |
| 1487 : |
|
|
[THEN] |
| 1488 : |
|
|
|
| 1489 : |
jwilke
|
1.95
|
|
| 1490 : |
jwilke
|
1.97
|
\ run with gforth-0.5.0 (slurp-file is missing) |
| 1491 : |
jwilke
|
1.95
|
[IFUNDEF] slurp-file |
| 1492 : |
|
|
: slurp-file ( c-addr1 u1 -- c-addr2 u2 ) |
| 1493 : |
|
|
\ c-addr1 u1 is the filename, c-addr2 u2 is the file's contents |
| 1494 : |
|
|
r/o bin open-file throw >r |
| 1495 : |
|
|
r@ file-size throw abort" file too large" |
| 1496 : |
|
|
dup allocate throw swap |
| 1497 : |
|
|
2dup r@ read-file throw over <> abort" could not read whole file" |
| 1498 : |
|
|
r> close-file throw ; |
| 1499 : |
|
|
[THEN] |
| 1500 : |
|
|
|
| 1501 : |
anton
|
1.69
|
: primfilter ( addr u -- ) |
| 1502 : |
|
|
\ process the string at addr u |
| 1503 : |
|
|
over dup rawinput ! dup line-start ! cookedinput ! |
| 1504 : |
|
|
+ endrawinput ! |
| 1505 : |
anton
|
1.130
|
checksynclines |
| 1506 : |
anton
|
1.69
|
primitives2something ; |
| 1507 : |
pazsan
|
1.8
|
|
| 1508 : |
anton
|
1.130
|
: unixify ( c-addr u1 -- c-addr u2 ) |
| 1509 : |
|
|
\ delete crs from the string |
| 1510 : |
|
|
bounds tuck tuck ?do ( c-addr1 ) |
| 1511 : |
|
|
i c@ dup #cr <> if |
| 1512 : |
|
|
over c! char+ |
| 1513 : |
|
|
else |
| 1514 : |
|
|
drop |
| 1515 : |
|
|
endif |
| 1516 : |
|
|
loop |
| 1517 : |
|
|
over - ; |
| 1518 : |
|
|
|
| 1519 : |
anton
|
1.72
|
: process-file ( addr u xt-simple x-combined -- ) |
| 1520 : |
|
|
output-combined ! output ! |
| 1521 : |
anton
|
1.61
|
save-mem 2dup filename 2! |
| 1522 : |
anton
|
1.130
|
slurp-file unixify |
| 1523 : |
anton
|
1.17
|
warnings @ if |
| 1524 : |
|
|
." ------------ CUT HERE -------------" cr endif |
| 1525 : |
anton
|
1.69
|
primfilter ; |
| 1526 : |
pazsan
|
1.30
|
|
| 1527 : |
anton
|
1.72
|
\ : process ( xt -- ) |
| 1528 : |
|
|
\ bl word count rot |
| 1529 : |
|
|
\ process-file ; |