summaryrefslogtreecommitdiffstats
path: root/qemu/roms/openbios/forth/device/fcode.fs
blob: 9083ed0e0c3792f41760417af0a017dc8b3fb491 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
\ tag: FCode implementation functions
\ 
\ this code implements IEEE 1275-1994 ch. 5.3.3
\ 
\ Copyright (C) 2003 Stefan Reinauer
\ 
\ See the file "COPYING" for further information about
\ the copyright and warranty status of this work.
\ 

hex 

0    value fcode-sys-table \ table with built-in fcodes (0-0x7ff)

true value ?fcode-offset16 \ fcode offsets are 16 instead of 8 bit?
1    value fcode-spread    \ fcode spread (1, 2 or 4)
0    value fcode-table     \ pointer to fcode table
false value ?fcode-verbose  \ do verbose fcode execution?

defer _fcode-debug?        \ If true, save names for FCodes with headers
true value fcode-headers?  \ If true, possibly save names for FCodes.

0 value fcode-stream-start \ start address of fcode stream
0 value fcode-stream       \ current fcode stream address

variable fcode-end         \ state variable, if true, fcode program terminates.
defer fcode-c@             \ get byte

: fcode-push-state ( -- <state information> )
  ?fcode-offset16
  fcode-spread
  fcode-table
  fcode-headers?
  fcode-stream-start
  fcode-stream
  fcode-end @
  ['] fcode-c@ behavior
;

: fcode-pop-state ( <state information> -- )
  to fcode-c@
  fcode-end !
  to fcode-stream
  to fcode-stream-start
  to fcode-headers?
  to fcode-table
  to fcode-spread
  to ?fcode-offset16
;
  
\ 
\ fcode access helper functions
\ 

\ fcode-ptr
\   convert FCode number to pointer to xt in FCode table.

: fcode-ptr ( u16 -- *xt )
  cells
  fcode-table ?dup if + exit then
  
  \ we are not parsing fcode at the moment
  dup 800 cells u>= abort" User FCODE# referenced."
  fcode-sys-table +
;
  
\ fcode>xt
\   get xt according to an FCode#

: fcode>xt ( u16 -- xt )
  fcode-ptr @
  ;

\ fcode-num8
\   get 8bit from FCode stream, taking spread into regard.

: fcode-num8 ( -- c ) ( F: c -- )
  fcode-stream
  dup fcode-spread + to fcode-stream 
  fcode-c@
  ;

\ fcode-num8-signed ( -- c ) ( F: c -- )
\   get 8bit signed from FCode stream

: fcode-num8-signed
  fcode-num8
  dup 80 and 0> if
     ff invert or
  then
  ;

\ fcode-num16
\   get 16bit from FCode stream

: fcode-num16 ( -- num16 )
  fcode-num8 fcode-num8 swap bwjoin
  ;

\ fcode-num16-signed ( -- c ) ( F: c -- )
\   get 16bit signed from FCode stream

: fcode-num16-signed
  fcode-num16
  dup 8000 and 0> if
     ffff invert or
  then
  ;

\ fcode-num32
\   get 32bit from FCode stream

: fcode-num32 ( -- num32 )
  fcode-num8 fcode-num8
  fcode-num8 fcode-num8
  swap 2swap swap bljoin
  ;
 
\ fcode#
\   Get an FCode# from FCode stream

: fcode# ( -- fcode# )
  fcode-num8
  dup 1 f between if
    fcode-num8 swap bwjoin
  then
  ;

\ fcode-offset
\   get offset from FCode stream.

: fcode-offset ( -- offset )
  ?fcode-offset16 if
    fcode-num16-signed
  else
    fcode-num8-signed
  then

  \ Display offset in verbose mode
  ?fcode-verbose if
    dup ."        (offset) " . cr
  then
  ;

\ fcode-string
\   get a string from FCode stream, store in pocket.

: fcode-string ( -- addr len )
  pocket dup
  fcode-num8
  dup rot c!
  2dup bounds ?do
    fcode-num8 i c!
  loop

  \ Display string in verbose mode
  ?fcode-verbose if
    2dup ."        (const) " type cr
  then
  ;
    
\ fcode-header
\   retrieve FCode header from FCode stream

: fcode-header
  fcode-num8
  fcode-num16
  fcode-num32
  ?fcode-verbose if
    ." Found FCode header:" cr rot
    ."   Format   : " u. cr swap
    ."   Checksum : " u. cr
    ."   Length   : " u. cr
  else
    3drop
  then
  \ TODO checksum
  ;

\ writes currently created word as fcode# read from stream
\ 

: fcode! ( F:FCode# -- )
  here fcode#

  \ Display fcode# in verbose mode
  ?fcode-verbose if
    dup ."        (fcode#) " . cr
  then
  fcode-ptr !
  ;

  
\ 
\ 5.3.3.1 Defining new FCode functions.
\ 

\ instance ( -- )   
\   Mark next defining word as instance specific.
\  (defined in bootstrap.fs)

\ instance-init ( wid buffer -- )
\   Copy template from specified wordlist to instance
\ 

: instance-init
  swap
  begin @ dup 0<> while
    dup /n + @ instance-cfa? if         \ buffer dict
      2dup 2 /n* + @ +                  \ buffer dict dest
      over 3 /n* + @                    \ buffer dict dest size
      2 pick 4 /n* +                    \ buffer dict dest size src
      -rot
      move
    then
  repeat
  2drop
  ;


\ new-token ( F:/FCode#/ -- ) 
\   Create a new unnamed FCode function

: new-token 
  0 0 header
  fcode!
  ;

  
\ named-token (F:FCode-string FCode#/ -- )
\   Create a new possibly named FCode function.

: named-token 
  fcode-string
  _fcode-debug? not if
    2drop 0 0
  then
  header
  fcode!
  ;

  
\ external-token (F:/FCode-string FCode#/ -- )
\   Create a new named FCode function

: external-token 
  fcode-string header
  fcode!
  ;

  
\ b(;) ( -- ) 
\   End an FCode colon definition.

: b(;)
  ['] ; execute
  ; immediate


\ b(:) ( -- ) ( E: ... -- ??? )
\   Defines type of new FCode function as colon definition.

: b(:)
  1 , ]
  ;


\ b(buffer:) ( size -- ) ( E:  -- a-addr )  
\   Defines type of new FCode function as buffer:.

: b(buffer:)
  4 , allot
  reveal
  ;

\ b(constant) ( nl -- ) ( E: -- nl )
\   Defines type of new FCode function as constant.

: b(constant)
  3 , , 
  reveal
  ;


\ b(create) ( -- ) ( E: -- a-addr )
\   Defines type of new FCode function as create word.

: b(create)
  6 , 
  ['] noop ,
  reveal
  ;


\ b(defer) ( -- ) ( E: ... -- ??? )  
\   Defines type of new FCode function as defer word.

: b(defer)
  5 ,
  ['] (undefined-defer) ,
  ['] (semis) ,
  reveal
  ;


\ b(field) ( offset size -- offset+size ) ( E: addr -- addr+offset )
\   Defines type of new FCode function as field.

: b(field)
  6 ,
  ['] noop ,
  reveal
    over ,
    +
  does>
    @ +
  ;

  
\ b(value) ( x -- ) (E: -- x )
\   Defines type of new FCode function as value.
  
: b(value)
  3 , , reveal
  ;


\ b(variable) ( -- ) ( E: -- a-addr )
\   Defines type of new FCode function as variable.

: b(variable)
  4 , 0 ,
  reveal
  ;
  
  
\ (is-user-word) ( name-str name-len xt -- ) ( E: ... -- ??? )
\   Create a new named user interface command.

: (is-user-word)
  ;

  
\ get-token ( fcode# -- xt immediate? )
\   Convert FCode number to function execution token.

: get-token
  fcode>xt dup immediate?
  ;


\ set-token ( xt immediate? fcode# -- )
\   Assign FCode number to existing function.
  
: set-token
  nip \ TODO we use the xt's immediate state for now.
  fcode-ptr !
  ;

  
  

\ 
\ 5.3.3.2 Literals
\ 


\ b(lit) ( -- n1 ) 
\   Numeric literal FCode. Followed by FCode-num32.

64bit? [IF]
: b(lit)
  fcode-num32 32>64
  state @ if
    ['] (lit) , ,
  then
  ; immediate
[ELSE]
: b(lit)
  fcode-num32 
  state @ if
    ['] (lit) , ,
  then
  ; immediate
[THEN]


\ b(') ( -- xt )  
\   Function literal FCode. Followed by FCode#

: b(')
  fcode# fcode>xt
  state @ if
    ['] (lit) , , 
  then
  ; immediate

  
\ b(") ( -- str len )
\   String literal FCode. Followed by FCode-string.
  
: b(")
  fcode-string
  state @ if
    \ only run handle-text in compile-mode,
    \ otherwise we would waste a pocket.
    handle-text
  then
  ; immediate


\ 
\ 5.3.3.3 Controlling values and defers
\ 

\ behavior ( defer-xt -- contents-xt )
\ defined in bootstrap.fs

\ b(to) ( new-value -- )
\   FCode for setting values and defers. Followed by FCode#.

: b(to)
  fcode# fcode>xt 
  1 handle-lit
  ['] (to)
  state @ if 
    ,
  else
    execute
  then
  ; immediate



\ 
\ 5.3.3.4 Control flow
\ 


\ offset16 ( -- )
\   Makes subsequent FCode-offsets use 16-bit (not 8-bit) form.

: offset16
  true to ?fcode-offset16
  ;


\ bbranch ( -- )
\   Unconditional branch FCode. Followed by FCode-offset.
  
: bbranch
  fcode-offset 0< if \ if we jump backwards, we can forsee where it goes
    ['] dobranch ,
    resolve-dest
    execute-tmp-comp
  else
    setup-tmp-comp ['] dobranch ,
    here 0
    0 ,
    2swap
  then
  ; immediate


\ b?branch ( continue? -- )
\   Conditional branch FCode. Followed by FCode-offset.

: b?branch
  fcode-offset 0< if \ if we jump backwards, we can forsee where it goes
    ['] do?branch ,
    resolve-dest
    execute-tmp-comp
  else
    setup-tmp-comp ['] do?branch ,
    here 0
    0 ,
  then 
  ; immediate

  
\ b(<mark) ( -- )
\   Target of backward branches.

: b(<mark)
  setup-tmp-comp
  here 1
  ; immediate

  
\ b(>resolve) ( -- )
\   Target of forward branches.

: b(>resolve)
  resolve-orig
  execute-tmp-comp
  ; immediate

  
\ b(loop) ( -- )
\   End FCode do..loop. Followed by FCode-offset.

: b(loop)
  fcode-offset drop
  postpone loop
  ; immediate

  
\ b(+loop) ( delta -- )
\   End FCode do..+loop. Followed by FCode-offset.

: b(+loop)
  fcode-offset drop
  postpone +loop
  ; immediate

  
\ b(do) ( limit start -- )
\   Begin FCode do..loop. Followed by FCode-offset.

: b(do)
  fcode-offset drop
  postpone do
  ; immediate

  
\ b(?do) ( limit start -- )
\   Begin FCode ?do..loop. Followed by FCode-offset.

: b(?do)
  fcode-offset drop
  postpone ?do
  ; immediate

  
\ b(leave) ( -- )
\   Exit from a do..loop.
  
: b(leave)
  postpone leave
  ; immediate

  
\ b(case) ( sel -- sel )
\   Begin a case (multiple selection) statement.

: b(case)
  postpone case
  ; immediate

  
\ b(endcase) ( sel | <nothing> -- )
\   End a case (multiple selection) statement.

: b(endcase)
  postpone endcase
  ; immediate
  

\ b(of) ( sel of-val -- sel | <nothing> )
\   FCode for of in case statement. Followed by FCode-offset.

: b(of)
  fcode-offset drop
  postpone of
  ; immediate

\ b(endof) ( -- )
\   FCode for endof in case statement. Followed by FCode-offset.

: b(endof)
  fcode-offset drop
  postpone endof
  ; immediate