-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.asm
More file actions
2563 lines (2121 loc) · 54.7 KB
/
main.asm
File metadata and controls
2563 lines (2121 loc) · 54.7 KB
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; this is a forth written in assembly... or at least it tries to be
;; (C) 2022 Sergi Reyner
;; MIT License
bits 64
;; design goals:
;; -------------
;; designed to run under Linux x64
;; the native cell size is 64-bit
;; must be able to interpret most words
;; must produce reasonably fast code
;; must be implemented in an easy to understand way
;; will attempt to follow the Forth2012 Standard (latest at the time of implementation)
;; should... provide a C interface
;; The Forth 2012 Standard is considered a suggestion/guideline
;; most of the core words can be implemented as macros (immediate
;; words) that compile small snippets of machine code, somewhat
;; similar to machineforth
;; should make a decision on whether to have dual sets of words
;; (DUP/DUP,) for interpretation/regular compiling vs inlining
;; once file access is implemented, determine what should be a
;; primitive and what should be a high level definition, then move the
;; high level code to boot.fs
;; things that are used frequently or interface with the OS:
;; --------------------------------
;; math words
;; stack words
;; syscalls
;; writing "fast" code in Forth is possible given a basic set of words
;; capable of assembling code
;; Register Allocation
;; The System V ABI has just enough registers that we can avoid using
;; rax-rdx, which are the means to pass parameters to linux syscalls
;; All registers are equally capable (for the most part...)
;; Since this is an STC Forth, the IP register is also the IP register
;; of the machine, namely rip
;; The parameter stack pointer will reside on rbp, which is the stack
;; frame pointer and not really relevant as long as we stay on the
;; Forth and assembly side of code
;; rsi and rdi are used as source and destination pointers for a few
;; instructions
;; r8 r9 r10 r11 are scratch registers
;; r12 r13 r14 r15 are preserved between calls
;; rcx and r11 are destroyed by syscalls
;; should change Y to r8 and rewrite?
;; There's not really a need to respect the System V ABI, but in the
;; future we may want to interface with C code. Since there's not
;; really a cost to this future-proofing, because we have enough
;; preserved registers, I ended up with the following allocation,
;; assigned following the recommendations from Moving Forth:
;; %define IP rip
;; %define RSP rsp
%define PSP rbp
%define TOS r15
%define W r12
%define X r13
%define Y r11 ; it's going to be a scratch register anyway
%define UP r14
;; Possibly useful for compiling literals?
;; x64 provides a new rip-relative addressing mode. Instructions that
;; refer to a single constant address are encoded as offsets from
;; rip. For example, the mov rax, [addr] instruction moves 8 bytes
;; beginning at addr + rip to rax.
;; Constants
CELLSIZE equ 8
STACKSIZE equ 64
BUFFERSIZE equ 4096
TRUE equ -1
FALSE equ 0
MMAP_FLAGS equ 0x22 ; MAP_ANONYMOUS|MAP_PRIVATE
MMAP_PROTECTION equ 0x7 ; RWE
BLOCK_MMAP_FLAGS equ 0x22 ; MAP_SHARED|MAP_SYNC
BLOCK_MMAP_PROTECTION equ 0x3 ; RW?
;; static data stuff
SECTION .data
; align 4
BASEDIGITS db "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
helloStr db "RayForth v0", 10
helloLen equ $-helloStr
notFoundMsgStr db " not found"
notFoundMsgLen equ $-notFoundMsgStr
promptStr db " ok", 10
promptLen equ $-promptStr
bootfsStr db "boot.fs"
bootfsLen equ $-bootfsStr
timeval:
tv_sec dq 0
tv_usec dq 0
;; here's where these things go, apparently
SECTION .bss
; align 4
;; Parameter stack
DATASTACK resb CELLSIZE*STACKSIZE
DATASTACKBOTTOM equ $
;; Return Stack
RETURNSTACKBOTTOM resb 8
;; Parameter Stack Macros
%define NOS [PSP]
%define NIP add PSP, CELLSIZE
%macro DROP 0
mov TOS, NOS
NIP
%endmacro
%macro DUP 0
sub PSP, CELLSIZE
mov qword NOS, TOS
%endmacro
%macro DPUSH 1
DUP
mov TOS, qword %1
%endmacro
%macro DPOP 1
mov qword %1, TOS
mov TOS, NOS
NIP
%endmacro
%macro CLR 1
xor %1, %1
%endmacro
%macro SWAP 0-1 r8
mov %1, TOS
mov TOS, NOS
mov NOS, %1
%endmacro
;; Other memory zones
PADDATA:
resb BUFFERSIZE
TIBDATA:
resb BUFFERSIZE
WORDBUFFER:
resb BUFFERSIZE
;; dictionary here?
;; colon and code definitions have the same structure
;; LINK (8) FLAGS (1) COUNT (1) NAME (cnt) code follows...
;; Here we create some macros for easy creation of dictionary entries,
;; along with labels than can be used later to call code or address
;; data directly from assembly
;; inspired by Itsy Forth, modified for STC and additional
;; code. .CONSTANT and .VARIABLE compile their own code straight
;; away. .CODE is gone, since all definitions are the same under STC,
;; but may come back later if I find some utility to having two
;; different words.
%define link 0
%define IMM 0x80
%define LOCAL 0x40
%define SMUDGE 0x20
%macro head 3
%{2}_entry:
%%link dq link
%define link %%link
db %3
%strlen %%count %1
db %%count,%1
%endmacro
%macro .colon 2-3 0
head %1,%2,%3
%{2}:
%endmacro
; %macro .constant 3
%macro .constant 3-4 0
head %1,%2,%4
%{2}:
mov W, val_ %+ %2
mov X, [W]
DPUSH X
ret
val_ %+ %2 dq %3 ; value stored here
%endmacro
; %macro .variable 3
%macro .variable 3-4 0
head %1,%2,%4
%{2}:
DPUSH val_ %+ %2
ret
val_ %+ %2 dq %3 ; value stored here
%endmacro
SECTION .mysection exec
align 4
DICTIONARY:
;; primitives
.constant "TRUE", true, -1
.constant "FALSE", false, 0
.constant "BASEDIGITS", digits, BASEDIGITS
.colon "@", fetch
mov TOS, [TOS]
ret
.colon "!",store
mov r8, NOS
mov [TOS], r8
mov TOS, [PSP+CELLSIZE]
add PSP, CELLSIZE*2
ret
.colon "+!",plusstore
mov r8, NOS
add [TOS], r8
mov TOS, [PSP+CELLSIZE]
add PSP, CELLSIZE*2
ret
.colon "C@", cfetch
movzx TOS, byte [TOS]
ret
.colon "C!", cstore
movzx r8, byte NOS
mov byte [TOS], r8b
mov TOS, [PSP+CELLSIZE]
add PSP, CELLSIZE*2
ret
.colon "C+!", cplusstore
movzx r8, byte NOS
add byte [TOS], r8b
mov TOS, [PSP+CELLSIZE]
add PSP, CELLSIZE*2
ret
;; mostly for internal use
.colon "PSP", pointerOfNOS
DUP
mov TOS, PSP
ret
.colon "RP@", rpFetch
DUP
mov TOS, rsp
add TOS, CELLSIZE ; return value under this function's return
ret
.colon "RP0@", rpBaseFetch
DUP
mov TOS, [RETURNSTACKBOTTOM]
ret
.colon "R@", rfetch
DUP
mov TOS, [rsp+CELLSIZE]
ret
;; these words become shorter if code is inlined
.colon "R>", fromrstack
pop r8
pop r9
push r8
DPUSH r9
ret
.colon ">R", torstack
DPOP r8
pop r9
push r8
push r9
ret
.colon "2R>", twofromrstack
pop r10
pop r8
pop r9
push r10
DPUSH r9
DPUSH r8
ret
.colon "2>R", twotorstack
DPOP r8
DPOP r9
pop r10
push r9
push r8
push r10
ret
.colon "0=", zeroEqual
mov r8, TOS
CLR TOS
mov W, -1
test r8, r8
cmovz TOS, W
ret
.colon "0<>", zeroNotEqual
mov r8, TOS
CLR TOS
mov W, -1
test r8, r8
cmovnz TOS, W
ret
.colon "0<", zeroLess
mov r8, TOS
CLR TOS
mov W, -1
cmp r8, 0
cmovl TOS, W
ret
.colon "0>", zeroMore
mov r8, TOS
CLR TOS
mov W, -1
cmp r8, 0
cmovg TOS, W
ret
.colon "=", equal
mov r8, TOS
CLR TOS
mov W, -1
cmp NOS, r8
cmove TOS, W
NIP
ret
.colon "<>", different
mov r8, TOS
CLR TOS
mov W, -1
cmp NOS, r8
cmovne TOS, W
NIP
ret
.colon "<", lesserthan
mov r8, TOS
CLR TOS
mov W, -1
cmp NOS, r8
cmovl TOS, W
NIP
ret
.colon ">", greaterthan
mov r8, TOS
CLR TOS
mov W, -1
cmp NOS, r8
cmovg TOS, W
NIP
ret
.colon "<=", lesserthanorequal
mov r8, TOS
CLR TOS
mov W, -1
cmp NOS, r8
cmovle TOS, W
NIP
ret
.colon ">=", greaterthanorequal
mov r8, TOS
CLR TOS
mov W, -1
cmp NOS, r8
cmovge TOS, W
NIP
ret
.colon "U<", ulesserthan
mov r8, TOS
CLR TOS
mov W, -1
cmp NOS, r8
cmovb TOS, W
NIP
ret
.colon "U>", ugreaterthan
mov r8, TOS
CLR TOS
mov W, -1
cmp NOS, r8
cmova TOS, W
NIP
ret
.colon "U<=", ulesserthanorequal
mov r8, TOS
CLR TOS
mov W, -1
cmp NOS, r8
cmovbe TOS, W
NIP
ret
.colon "U>=", ugreaterthanorequal
mov r8, TOS
CLR TOS
mov W, -1
cmp NOS, r8
cmovae TOS, W
NIP
ret
.colon "+", plus
add TOS, NOS
NIP
ret
.colon "-", minus
sub NOS, TOS
mov TOS, NOS
NIP
ret
.colon "1+", increment
inc TOS
ret
.colon "1-", decrement
dec TOS
ret
.colon "*", multiply ; bit broken but works for reasonable numbers... xD
imul TOS, NOS
NIP
ret
;; Signed divide RDX:RAX by r/m64, with result stored in
;; RAX ← Quotient, RDX ← Remainder.
.colon "/MOD", dividemod
xor rdx, rdx
mov rax, NOS
idiv TOS
mov TOS, rax
mov NOS, rdx
ret
;; Unsigned divide RDX:RAX by r/m64, with result stored in
;; RAX ← Quotient, RDX ← Remainder.
;; ( ud u1 -- u2 u3 )
.colon "UM/MOD", umdividemod
mov rax, [PSP+CELLSIZE]
mov rdx, NOS ; which will be 0, but whatever...
div TOS
add PSP, CELLSIZE ; remove the high part of the double
mov TOS, rax
mov NOS, rdx
ret
.colon "/", divide
xor rdx, rdx
mov rax, NOS
idiv TOS
mov TOS, rax
NIP
ret
.colon "MOD", mod
xor rdx, rdx
mov rax, NOS
idiv TOS
mov TOS, rdx
NIP
ret
.colon "MIN", min
mov r8, NOS
mov r9, TOS
cmp r8, r9
cmovl TOS, r8
NIP
ret
.colon "MAX", max
mov r8, NOS
mov r9, TOS
cmp r8, r9
cmovg TOS, r8
NIP
ret
.colon "ABS", _abs
mov r8, TOS
neg r8
cmovns TOS, r8
ret
.colon "NEGATE", negate
neg TOS
ret
.colon "NAND", nand_
and TOS, NOS
not TOS
NIP
ret
.colon "NOR", nor_
or TOS, NOS
not TOS
NIP
ret
.colon "XNOR", xnor_
xor TOS, NOS
not TOS
NIP
ret
.colon "AND", and_
and TOS, NOS
NIP
ret
.colon "OR", or_
or TOS, NOS
NIP
ret
.colon "XOR", xor_
xor TOS, NOS
NIP
ret
.colon "INVERT", invert
not TOS
ret
.colon "2*", shift1left
shl TOS, 1
ret
.colon "2/", shift1right
shr TOS, 1
ret
.colon "LSHIFT", shiftleft
mov rcx, TOS
mov TOS, NOS
add PSP, CELLSIZE
shl TOS, cl
ret
.colon "RSHIFT", shiftright
mov rcx, TOS
mov TOS, NOS
add PSP, CELLSIZE
shr TOS, cl
ret
;; User-level applications use as integer registers for passing the
;; sequence %rdi, %rsi, %rdx, %rcx, %r8 and %r9. The kernel interface
;; uses %rdi, %rsi, %rdx, %r10, %r8 and %r9.
;; A system-call is done via the syscall instruction. The kernel
;; destroys registers %rcx and %r11.
;; The number of the syscall has to be passed in register %rax.
;; System-calls are limited to six arguments,no argument is passed
;; directly on the stack.
;; Returning from the syscall, register %rax contains the result of
;; the system-call. A value in the range between -4095 and -1
;; indicates an error, it is -errno.
;; Only values of class INTEGER or class MEMORY are passed to the
;; kernel.
.colon "MS", ms
xor rdx, rdx
mov rax, TOS
mov rbx, 1000
idiv rbx
mov qword [tv_sec], rax
mov rax, rdx
mul rbx
mul rbx
mov qword [tv_usec], rax
mov rdi, timeval
mov rsi, 0
mov rax, 0x23
syscall
DROP
ret
.colon "SYSCALL/0", colonsyscall ; ( int -- result )
DPOP rax
syscall
DPUSH rax
ret
.colon "SYSCALL/1", colonsyscall1 ; ( arg1 int -- result )
DPOP rax
DPOP rdi
syscall
DPUSH rax
ret
.colon "SYSCALL/2", colonsyscall2
DPOP rax
DPOP rdi
DPOP rsi
syscall
DPUSH rax
ret
.colon "SYSCALL/3", colonsyscall3
DPOP rax
DPOP rdi
DPOP rsi
DPOP rdx
syscall
DPUSH rax
ret
.colon "SYSCALL/4", colonsyscall4
DPOP rax
DPOP rdi
DPOP rsi
DPOP rdx
DPOP r10
syscall
DPUSH rax
ret
.colon "SYSCALL/5", colonsyscall5
DPOP rax
DPOP rdi
DPOP rsi
DPOP rdx
DPOP r10
DPOP r8
syscall
DPUSH rax
ret
.colon "SYSCALL/6", colonsyscall6
DPOP rax
DPOP rdi
DPOP rsi
DPOP rdx
DPOP r10
DPOP r8
DPOP r9
syscall
DPUSH rax
ret
;; TYPE
.colon "TYPE", type ; ( addr n -- )
call swap
DPUSH 1
DPUSH 1
call colonsyscall3
call drop
ret
.colon "EMIT", emit
; instead store the char on the return stack
DPOP r8
push r8
DPUSH rsp
DPUSH 1
call type
; drop the char on the return stack
pop r8
ret
.colon "KEY", key
;; ideally we should set the terminal to raw or something first
push 0
DPUSH 1
DPUSH rsp
DPUSH 0
DPUSH 0
call colonsyscall3
call drop
pop r8
DPUSH r8
ret
;; stack manipulation
.colon "DUP", dup ; ( a -- a a )
DUP
ret
.colon "?DUP", maybedup ; ( a -- a / a a )
test TOS, TOS
jz maybedup_end
DUP
maybedup_end:
ret
.colon "2DUP", _2dup ; ( a b -- a b a b )
mov r8, NOS
sub PSP, CELLSIZE*2
mov [PSP+CELLSIZE], TOS
mov NOS, r8
ret
.colon "SWAP", swap ; ( a b -- b a )
SWAP
ret
.colon "2SWAP", _2swap ; ( a b c d -- c d a b )
mov r8, [PSP+CELLSIZE*2]
mov r9, NOS
mov NOS, r8
mov [PSP+CELLSIZE*2], r9
mov r11, TOS
mov r12, [PSP+CELLSIZE]
mov [PSP+CELLSIZE], r11
mov TOS, r12
ret
.colon "DROP", drop ; ( a -- )
DROP
ret
.colon "2DROP", _2drop ; ( a b -- )
mov TOS, [PSP+CELLSIZE]
add PSP, CELLSIZE*2
ret
.colon "OVER", over ; ( a b -- a b a )
mov r8, NOS
DPUSH r8
ret
.colon "2OVER", _2over ; ( a b c d -- a b c d a b )
sub PSP, CELLSIZE*2
mov [PSP+CELLSIZE], TOS
mov r8, [PSP+CELLSIZE*4]
mov NOS, r8
mov TOS, [PSP+CELLSIZE*3]
ret
.colon "NIP", nip ; ( a b -- b )
NIP
ret
.colon "TUCK", tuck ; ( a b -- b a b )
mov r8, NOS
sub PSP, CELLSIZE
mov NOS, r8
mov [PSP+CELLSIZE], TOS
ret
.colon "ROT", rot ; ( a b c -- b c a )
mov r8, NOS
mov r9, [PSP+CELLSIZE]
mov NOS, TOS
mov [PSP+CELLSIZE], r8
mov TOS, r9
ret
.colon "-ROT", minusrot ; ( a b c -- c a b )
mov r8, NOS
mov r9, [PSP+CELLSIZE]
mov [PSP+CELLSIZE], TOS
mov NOS, r9
mov TOS, r8
ret
.colon "PICK", pick
shl TOS, 3 ; cell size is 8
add TOS, PSP
mov TOS, [TOS]
ret
.colon "ROLL", roll
; all the elements to be rotated are on the PSP area,
; TOS holds the index, overwrite it with the final value
mov r8, TOS
mov r9, r8
shl r9, 3 ; cell size is 8
add r9, PSP
mov TOS, [r9]
; now copy stack down (or up...?)
mov rdi, r9
sub r9, CELLSIZE
mov rsi, r9
mov rcx, r8
; IN REVERSE, stack grows downwards, so we want to start
; copying from the end, and decrement the pointers!!
std
rep movsq
cld
; finally adjust the stack pointer since we consumed the index
NIP
ret
.colon "CR", cr
DPUSH 10
call emit
ret
.colon "BYE", bye
DPUSH 0
DPUSH 60
call colonsyscall1
; not that we ever get here...
call drop
ret
.variable "BASE", base, 10 ; base is 10 by default
.variable "STATE", state, 0 ; 0 interpret, 1 compile
.variable "LATEST", latest, 0
.variable "TIB", TIB, TIBDATA
.variable ">IN", TOIN, 0
.variable "DP", dp, 0
.colon "HERE", here
mov r8, [val_dp]
DPUSH r8
ret
.colon "UNUSED", unused
mov r8, end_of_dictionary
sub r8, [val_dp]
DPUSH r8
ret
.colon "(", leftparen, IMM
DPUSH ')'
call word_
call drop
ret
.colon '\', backslash, IMM
;;; ' ; work around nasm-mode highlighting
DPUSH 10
call word_
call drop
ret
.colon "#!", shellsignature, IMM
DPUSH 10
call word_
call drop
ret
;; ( c-addr u1 fileid -- u2 flag ior )
.colon "READ-LINE", readline
DPOP W ; fid
DPOP X ; max
xor r8, r8 ; count
DPOP r9 ; c-addr
readline_next_char:
cmp r8, X
je readline_done
DPUSH 1
DPUSH r9
DPUSH W
DPUSH 0 ; read syscall
call colonsyscall3
DPOP rax
;; rax holds size (0/1) or -errno
test rax, rax
;; exit when either error
js readline_error
;; or EOF
jz readline_eof
;; if newline then done
cmp byte [r9], 10
je readline_done
;; move to next char
inc r9
inc r8
jmp readline_next_char
readline_done:
DPUSH r8
DPUSH -1
DPUSH 0
ret
readline_eof:
DPUSH r8
DPUSH 0
DPUSH 0
ret
readline_error:
DPUSH r8
DPUSH r8 ; return values don't matter
DPUSH -1 ; and ior
ret
.variable "<sourceaddr>", sourceaddr, TIBDATA, LOCAL
.variable "<sourcelen>", sourcelen, BUFFERSIZE, LOCAL
.variable "SOURCE-ID", sourceid, 0
.variable "BLK", blk, 0
.colon "SOURCE", source
mov r8, [val_sourceaddr]
mov r9, [val_sourcelen]
DPUSH r8
DPUSH r9
ret
;; ( -- f )
.colon "REFILL", refill
mov r8, [val_sourceid]
cmp r8, -1
je refill_error
; clear the input buffer
; mov rdi, TIBDATA
; mov rcx, BUFFERSIZE
mov rdi, [val_sourceaddr]
mov rcx, [val_sourcelen]
mov al, ' '
rep stosb
; read a... line
; DPUSH TIBDATA
; DPUSH BUFFERSIZE
mov r8, [val_sourceaddr]
mov r9, [val_sourcelen]
mov r10, [val_sourceid]
DPUSH r8
DPUSH r9
DPUSH r10
call readline
DPOP W ; test ior
test W, W
js refill_error2
DPOP X ; test flag
test X, X
jz refill_error
refill_done:
; no error, reset >IN and return true
DPOP r8
DPUSH 0
call TOIN
call store
DPUSH -1
ret
refill_error2: