forked from davazp/eulex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
forth.S
1635 lines (1364 loc) · 37 KB
/
forth.S
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
/* forth.S --- -*- asm -*-
Minimal on-the-metal Forth implementation for x86
*/
/* Copyright (C) 2011, 2012 David Vázquez Púa */
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#define GDT_CS_SELECTOR 0x08
#define GDT_CS_BASE 0
#define GDT_CS_LIMIT 0xffff
#define GDT_CS_TYPE 0x9A
#define GDT_DS_DESCRIPTOR 0x10
#define GDT_DS_BASE 0
#define GDT_DS_LIMIT 0xffff
#define GDT_DS_TYPE 0x92
.data
gdtr:
.short gdt_table_end - gdt_table - 1
.long gdt_table
gdt_table:
/* NULL: {.base=0, .limit=0, .type=0}; */
.long 0
.long 0
/* CODE: {.base=0, .limit=0xffff, .type=0x9A}; */
.short GDT_CS_LIMIT
.short GDT_CS_BASE & 0xFFFF
.byte (GDT_CS_BASE >> 16) & 0xFF
.byte GDT_CS_TYPE
.byte 0xCF
.byte (GDT_CS_BASE >> 24) & 0xFF
/* DATA: {.base=0, .limit=0xffff, .type=0x92}; */
.short GDT_DS_LIMIT
.short GDT_DS_BASE & 0xFFFF
.byte (GDT_DS_BASE >> 16) & 0xFF
.byte GDT_DS_TYPE
.byte 0xCF
.byte (GDT_DS_BASE >> 24) & 0xFF
gdt_table_end:
/* Reload register segments, so the system will reload the cached
segments from the GDT table. */
.text
flush_gdt:
lgdt gdtr
movw $0x10, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
movw %ax, %ss
ljmp $8, $1f
1: ret
/* Registers with a meaning (almost) persistent across the code:
%esi --- Data stack.
%edi --- First free data in the dictionary.
*/
/* Reserved space for data and control stacks */
#define RSTACK_SIZE 4096
#define STACK_SIZE 4096
.lcomm stack, STACK_SIZE
.lcomm rstack, RSTACK_SIZE
/* Pointer to the last word which was defined in the built-in wordlist. */
.lcomm builtin_wordlist, 16
.data
/* Format of dictionary's entries
32-bit adreess of the previous entry
in the wordlist.
/
+----------------+ <--
| 0xffffffff |
+---+------------+ <<<---------- Entry's descriptior pointer
Size of the name -> | n | name | F | <--- Flags
+---+------------+
| CFA | <--- Code 32bit address where interpretation
+----------------+ semantic, it is to say, the code to be
| | executed lives. Therefore, we compile a
| Parameter | call to this word as a call to the CFA
| field | address.
| |
+----------------+
| .... |
+----------------+
*/
#define F_IMMEDIATE 1
#define F_COMPILE_TIME 2
/* Like `BUILTIN_WORD_NAME', but the name of the word will
be the symbol's identifier itself. */
#define BUILTIN_WORD(name) BUILTIN_WORD_NAME(name, # name)
/* Define a primitive word in the Forth environment. The native code
should be enclosed between the BUILTIN_WORD_NAME and END_WORD macros. NAME
is a symbol which could be used to call from assembler. STR is a string
which stands for the name of the word in the Forth environment.
Primitive words will be avalaible as part of the built-in wordlist. */
#define BUILTIN_WORD_NAME(name, str) \
BUILTIN_WORD_ENTRY(name, str, 0)
/* Define a word entry. Used to implement BUILTIN_WORD_NAME and BUILTIN_VARIABLE macros. */
#define BUILTIN_WORD_ENTRY(name, str, flag) \
begin_ ## name: \
.byte end_ ## name ## _str - . - 1; \
.ascii str; \
end_ ## name ## _str: \
.byte flag; \
name ## _cfa: \
.long name; \
.text; \
name:
/* End mark to built-in words.*/
#define END_WORD(name) \
ret; \
.data; \
.long begin_ ## name
/* Define a primitive variable word. When it is executed, it pushes to
the data stack the address of a reserved cell in memory. As above,
SYMBOL is the assembler name, NAME is the name within the Forth
environment. Finally, DEFAULT is the default value. */
#define BUILTIN_VARIABLE_NAME(symbol, name, default) \
BUILTIN_WORD_NAME(symbol ## _variable , name) \
subl $4, %esi; \
movl $symbol, (%esi); \
ret; \
.data; \
symbol: \
.long default; \
END_WORD(symbol ## _variable)
#define BUILTIN_VARIABLE(name, default) \
BUILTIN_VARIABLE_NAME(name, # name, default)
/* Internal variables */
/* Buffer of the counted strings read by the parser. */
read_word_buffer:
.fill 32
dictionary_start:
.long 0
/* Primitive Forth words
WARNING:
NOT TO WRITE ANYTHING BETWEEN TWO DEFINITIONS, THE DEFINITION MACROS ASSUME
THERE IS A END_WORD PREVIOUSLY. IT IS WHY YOU CAN FIND THE .LONG 0 ABOVE.
OTHERWISE, THE DICTIONARY STRUCTURE WILL BE CORRUPTED.
*/
/************************************ CORE ************************************/
BUILTIN_VARIABLE(stack_underflow_err_routine, 0)
BUILTIN_VARIABLE(stack_overflow_err_routine, 0)
BUILTIN_VARIABLE(unknown_word_err_routine, 0)
BUILTIN_VARIABLE(compile_only_err_routine, 0)
/* Interpreter's input sources */
/* The role of the following variables may vary according to the kind
of tye input source which is being processed. The input source type
is determined by the value of the variable INPUT_SOURCE_ID.
If the input source is an in-memory buffer. Then the input buffer
is set to cover all the buffer, so no refilling will be required.
On the other hand, if the input source is an user input device,
input buffer will be refilled by the routine `tib_fill_routine'. */
/* A value which determine what kind of source we are processing. The
values are: 0 (input user device, as keyboard or serial), -1 (a
in-memory buffer as a string being processed by evaluate). */
BUILTIN_VARIABLE(input_source_id, 0)
/* Address of the input buffer. */
BUILTIN_VARIABLE(input_buffer, 0)
/* Location in the input source. Useful for provide some debugging
information when an error occurs. */
BUILTIN_VARIABLE(input_source_line, 1)
BUILTIN_VARIABLE(input_source_column, 0)
/* Offset of the parsing area in the input buffer. It is to say, the
offset to the data which were not processed yet. */
BUILTIN_VARIABLE(input_buffer_in, 0)
/* Size of the input buffer. */
BUILTIN_VARIABLE(input_buffer_size, 0)
/* Routine which is called when the input source is a user input
device and the input buffer is empty. */
BUILTIN_VARIABLE(tib_fill_routine, 0)
BUILTIN_WORD_NAME(_read_word_buffer, "read_word_buffer")
subl $4, %esi
movl $read_word_buffer, (%esi)
ret
END_WORD(_read_word_buffer)
/* Search order stack */
#define SORDER_STACK_SIZE 64
.lcomm sorder_stack, SORDER_STACK_SIZE*4
/* Offset of the first element in the search order stack. */
BUILTIN_VARIABLE(sorder_tos, 0)
BUILTIN_WORD(sorder_size)
subl $4, %esi
movl $SORDER_STACK_SIZE, (%esi)
ret
END_WORD(sorder_size)
BUILTIN_WORD_NAME(_sorder_stack, "sorder_stack")
subl $4, %esi
movl $sorder_stack, (%esi)
ret
END_WORD(_sorder_stack)
BUILTIN_WORD(dp)
subl $4, %esi
movl %edi, (%esi)
ret
END_WORD(dp)
BUILTIN_WORD_NAME(dp_store, "dp!")
movl (%esi), %edi
addl $4, %esi
ret
END_WORD(dp_store)
BUILTIN_WORD_NAME(dp_base, "dp-base")
subl $4, %esi
movl $dictionary_start, (%esi)
ret
END_WORD(dp_base)
/* Push the value of the stack pointer _before_ of executing this word. */
BUILTIN_WORD(sp)
movl %esi, %eax
subl $4, %esi
movl %eax, (%esi)
ret
END_WORD(sp)
/* Set the value of the stack pointer to the tos's value. */
BUILTIN_WORD_NAME(sp_store, "sp!")
movl (%esi), %esi
ret
END_WORD(sp_store)
BUILTIN_WORD_NAME(sp_base, "sp-base")
subl $4, %esi
movl $stack, (%esi)
ret
END_WORD(sp_base)
BUILTIN_WORD_NAME(sp_limit, "sp-limit")
subl $4, %esi
movl $stack+STACK_SIZE, (%esi)
ret
END_WORD(sp_limit)
BUILTIN_WORD(rsp)
subl $4, %esi
movl %esp, (%esi)
/* Don't take in account the increment of the RSP pointer due to this word. */
addl $4, (%esi)
ret
END_WORD(rsp)
BUILTIN_WORD_NAME(rsp_store, "rsp!")
/* KLUDGE: A fake RET is used here. We should
include support for inlining soon. */
popl %eax
movl (%esi), %esp
addl $4, %esi
jmp *%eax
END_WORD(rsp_store)
BUILTIN_WORD_NAME(rsp_base, "rsp-base")
subl $4, %esi
movl $rstack, (%esi)
ret
END_WORD(rsp_base)
BUILTIN_WORD_NAME(rsp_limit, "rsp-limit")
subl $4, %esi
movl $(rstack + RSTACK_SIZE), (%esi)
ret
END_WORD(rsp_limit)
BUILTIN_WORD_NAME(gdt_cs_selector, "gdt-cs-selector")
subl $4, %esi
movl $GDT_CS_SELECTOR, (%esi)
ret
END_WORD(gdt_cs_selector)
BUILTIN_WORD_NAME(store, "!")
movl (%esi), %eax
movl 4(%esi), %edx
movl %edx, (%eax)
addl $8, %esi
ret
END_WORD(store)
BUILTIN_WORD_NAME(fetch, "@")
movl (%esi), %eax
movl (%eax),%edx
movl %edx, (%esi)
ret
END_WORD(fetch)
BUILTIN_WORD_NAME(w_store, "w!")
movl (%esi), %eax
movl 4(%esi), %edx
movw %dx, (%eax)
addl $8, %esi
ret
END_WORD(w_store)
BUILTIN_WORD_NAME(w_fetch, "w@")
xorl %edx, %edx
movl (%esi), %eax
movw (%eax), %dx
movl %edx, (%esi)
ret
END_WORD(w_fetch)
BUILTIN_WORD_NAME(c_store, "c!")
movl (%esi), %eax
movl 4(%esi), %edx
movb %dl, (%eax)
addl $8, %esi
ret
END_WORD(c_store)
BUILTIN_WORD_NAME(c_fetch, "c@")
xorl %edx, %edx
movl (%esi), %eax
movb (%eax), %dl
movl %edx, (%esi)
ret
END_WORD(c_fetch)
BUILTIN_WORD_NAME(cmove, "cmove")
/* from to u -- */
movl (%esi), %ecx
movl 4(%esi), %edx
movl 8(%esi), %ebp
xorl %ebx, %ebx
1: test %ecx, %ecx
jz 2f
decl %ecx
movb (%ebp, %ebx), %al
movb %al, (%edx, %ebx)
incl %ebx
jmp 1b
2: addl $12, %esi
ret
END_WORD(cmove)
BUILTIN_WORD_NAME(cmove_to, "cmove>")
/* from to u -- */
movl (%esi), %ecx
movl 4(%esi), %edx
movl 8(%esi), %ebp
1: test %ecx, %ecx
jz 2f
decl %ecx
movb (%ebp, %ecx), %al
movb %al, (%edx, %ecx)
jmp 1b
2: addl $12, %esi
ret
END_WORD(cmove_to)
BUILTIN_WORD(fill)
pushl %edi
movl (%esi), %eax
movl 4(%esi), %ecx
movl 8(%esi), %edi
rep stosb
popl %edi
addl $12, %esi
ret
END_WORD(fill)
/* port -- n */
BUILTIN_WORD(inputb)
xorl %eax, %eax
movl (%esi), %edx
inb %dx, %al
movl %eax, (%esi)
ret
END_WORD(inputb)
/* n port -- */
BUILTIN_WORD(outputb)
movl (%esi), %edx
movl 4(%esi), %eax
addl $8, %esi
outb %al, %dx
ret
END_WORD(outputb)
BUILTIN_WORD_NAME(io_wait, "io-wait")
/* I don't like being rude but, WTF? */
jmp 1f
1: jmp 2f
2: ret
END_WORD(io_wait)
BUILTIN_WORD(cli)
cli
ret
END_WORD(cli)
BUILTIN_WORD(sti)
sti
ret
END_WORD(sti)
/* SIDT and LIDT words use this location as temporary storage. */
.lcomm idtr, 6
/* Load IDT register from the stack. */
BUILTIN_WORD(lidt) /* addr limit -- */
movl (%esi), %eax
movw %ax, idtr
movl 4(%esi), %eax
movl %eax, idtr+2
addl $8, %esi
lidt idtr
ret
END_WORD(lidt)
/* Save the IDT register into the stack. */
BUILTIN_WORD(sidt) /* -- addr limit */
subl $8, %esi
sidt idtr
movl idtr+2, %eax
movl %eax, 4(%esi)
movzwl idtr, %eax
movl %eax, 0(%esi)
ret
END_WORD(sidt)
BUILTIN_WORD(halt)
hlt
ret
END_WORD(halt)
BUILTIN_WORD(cpuid)
/* request -- %ecx %edx %ebx %eax */
movl (%esi), %eax
cpuid
subl $12, %esi
movl %ecx, 12(%esi)
movl %edx, 8(%esi)
movl %ebx, 4(%esi)
movl %eax, 0(%esi)
ret
END_WORD(cpuid)
BUILTIN_WORD(eflags)
pushf
movl (%esp), %eax
subl $4, %esi
movl %eax, (%esi)
addl $4, %esp
ret
END_WORD(eflags)
BUILTIN_WORD_NAME(eflags_store, "eflags!")
subl $4, %esp
movl (%esi), %eax
movl %eax, (%esp)
addl $4, %esi
popf
ret
END_WORD(eflags_store)
BUILTIN_WORD_NAME(times, "*")
movl 4(%esi), %eax
movl (%esi), %ecx
addl $4, %esi
imull %ecx
movl %eax, (%esi)
ret
END_WORD(times)
BUILTIN_WORD_NAME(plus, "+")
movl (%esi), %eax
addl $4, %esi
addl %eax, (%esi)
ret
END_WORD(plus)
BUILTIN_WORD_NAME(minus, "-")
movl (%esi), %eax
addl $4, %esi
subl %eax, (%esi)
ret
END_WORD(minus)
BUILTIN_WORD_NAME(divide, "/")
movl 4(%esi), %eax
cdq
idivl (%esi)
addl $4, %esi
movl %eax, (%esi)
ret
END_WORD(divide)
BUILTIN_WORD_NAME(udivide, "u/")
xorl %edx, %edx
movl 4(%esi), %eax
divl (%esi)
addl $4, %esi
movl %eax, (%esi)
ret
END_WORD(udivide)
BUILTIN_WORD(mod)
movl 4(%esi), %eax
cdq
idivl (%esi)
addl $4, %esi
movl %edx, (%esi)
ret
END_WORD(mod)
BUILTIN_WORD(and)
movl (%esi), %eax
andl %eax, 4(%esi)
addl $4, %esi
ret
END_WORD(and)
BUILTIN_WORD(or)
movl (%esi), %eax
orl %eax, 4(%esi)
addl $4, %esi
ret
END_WORD(or)
BUILTIN_WORD(xor)
movl (%esi), %eax
xorl %eax, 4(%esi)
addl $4, %esi
ret
END_WORD(xor)
BUILTIN_WORD(invert)
notl (%esi)
ret
END_WORD(invert)
/* u n -- u */
BUILTIN_WORD(lshift)
movl (%esi), %ecx
addl $4, %esi
shll %cl, (%esi)
ret
END_WORD(lshift)
/* u n -- u */
BUILTIN_WORD(rshift)
movl (%esi), %ecx
addl $4, %esi
shrl %cl, (%esi)
ret
END_WORD(rshift)
BUILTIN_WORD_NAME(two_times, "2*")
sall $1, (%esi)
ret
END_WORD(two_times)
BUILTIN_WORD_NAME(two_divides, "2/")
sarl $1, (%esi)
ret
END_WORD(two_divides)
BUILTIN_WORD_ENTRY(to_r, ">r", F_COMPILE_TIME)
/* Skip the stored %eip. */
movl (%esi), %eax
addl $4, %esi
movl %esp, %ebp
subl $4, %esp
movl (%ebp), %edx
movl %edx, -4(%ebp)
movl %eax, (%ebp)
ret
END_WORD(to_r)
BUILTIN_WORD_ENTRY(r_from, "r>", F_COMPILE_TIME)
subl $4, %esi
movl 4(%esp), %eax
movl %eax, (%esi)
movl (%esp), %ebx
movl %ebx, 4(%esp)
addl $4, %esp
ret
END_WORD(r_from)
BUILTIN_WORD_ENTRY(r_fetch, "r@", F_COMPILE_TIME)
movl 4(%esp), %eax
subl $4, %esi
movl %eax, (%esi)
ret
END_WORD(r_fetch)
BUILTIN_WORD_ENTRY(exit, "exit", F_COMPILE_TIME)
addl $4, %esp
ret
END_WORD(exit)
BUILTIN_WORD_ENTRY(to_r_2, "2>r", F_COMPILE_TIME)
movl %esp, %ebp
subl $8, %esp
movl (%ebp), %eax
movl %eax, -8(%ebp)
movl 4(%esi), %eax
movl %eax, (%ebp)
movl (%esi), %eax
movl %eax, -4(%ebp)
addl $8, %esi
ret
END_WORD(to_r_2)
BUILTIN_WORD_ENTRY(r_from_2, "2r>", F_COMPILE_TIME)
subl $8, %esi
movl 8(%esp), %eax
movl %eax, 4(%esi)
movl 4(%esp), %eax
movl %eax, 0(%esi)
movl (%esp), %eax
movl %eax, 8(%esp)
addl $8, %esp
ret
END_WORD(r_from_2)
BUILTIN_WORD_ENTRY(r_fetch_2, "2r@", F_COMPILE_TIME)
subl $8, %esi
movl 8(%esp), %eax
movl %eax, 4(%esi)
movl 4(%esp), %eax
movl %eax, (%esi)
ret
END_WORD(r_fetch_2)
BUILTIN_WORD_NAME(less_than, "<")
xorl %eax, %eax
movl (%esi), %ebx
cmpl %ebx, 4(%esi)
jnl 1f
not %eax
1: movl %eax, 4(%esi)
addl $4, %esi
ret
END_WORD(less_than)
BUILTIN_WORD_NAME(uless_than, "u<")
xorl %eax, %eax
movl (%esi), %ebx
cmpl %ebx, 4(%esi)
jnb 1f
not %eax
1: movl %eax, 4(%esi)
addl $4, %esi
ret
END_WORD(uless_than)
BUILTIN_WORD_NAME(ugreater_than, "u>")
xorl %eax, %eax
movl (%esi), %ebx
cmpl %ebx, 4(%esi)
jna 1f
not %eax
1: movl %eax, 4(%esi)
addl $4, %esi
ret
END_WORD(ugreater_than)
BUILTIN_WORD_NAME(equals, "=")
xorl %eax, %eax
movl (%esi), %ebx
cmpl %ebx, 4(%esi)
jne 1f
not %eax
1: movl %eax, 4(%esi)
addl $4, %esi
ret
END_WORD(equals)
BUILTIN_WORD_NAME(greater_than, ">")
xorl %eax, %eax
movl (%esi), %ebx
cmpl %ebx, 4(%esi)
jng 1f
not %eax
1: movl %eax, 4(%esi)
addl $4, %esi
ret
END_WORD(greater_than)
/* w - */
BUILTIN_WORD(drop)
addl $4, %esi
ret
END_WORD(drop)
/* w1 w2 - w2 */
BUILTIN_WORD(nip)
movl (%esi), %eax
addl $4, %esi
movl %eax, (%esi)
ret
END_WORD(nip)
/* w - w w*/
BUILTIN_WORD(dup)
movl (%esi), %eax
subl $4, %esi
movl %eax, (%esi)
ret
END_WORD(dup)
/* w1 w2 - w1 w2 w1 */
BUILTIN_WORD(over)
movl 4(%esi), %eax
subl $4, %esi
movl %eax, (%esi)
ret
END_WORD(over)
/* w1 w2 - w2 w1*/
BUILTIN_WORD(swap)
movl (%esi), %eax
movl 4(%esi), %ebx
movl %eax, 4(%esi)
movl %ebx, (%esi)
ret
END_WORD(swap)
/* w1 w2 w3 - w2 w3 w1 */
BUILTIN_WORD(rot)
movl (%esi), %eax
movl 4(%esi), %ebx
movl 8(%esi), %ecx
movl %eax, 4(%esi)
movl %ebx, 8(%esi)
movl %ecx, (%esi)
ret
END_WORD(rot)
BUILTIN_WORD(execute)
movl (%esi), %eax
addl $4, %esi
call *%eax
ret
END_WORD(execute)
BUILTIN_WORD(jump)
movl (%esi), %eax
addl $4, %esi
addl $4, %esp
jmp *%eax
END_WORD(jump)
/********************************** INTERPRETER **********************************/
/* Parse a word from the input stream and push the counted string.
The counted string will valid while no other words are read. */
BUILTIN_WORD_NAME(parse_cname, "parse-cname")
call read_word
subl $4, %esi
movl %eax, (%esi)
ret
END_WORD(parse_cname)
/* Find the first avalaible NT whose name is the counted string on the stack. */
BUILTIN_WORD_NAME(find_cname, "find-cname")
movl (%esi), %eax
call find_word
movl %edx, (%esi)
ret
END_WORD(find_cname)
BUILTIN_WORD_ENTRY(paren, "(", F_IMMEDIATE)
call fgetchar
cmp $')', %al
jne paren
ret
END_WORD(paren)
BUILTIN_WORD_ENTRY(backslash, "\\", F_IMMEDIATE)
call fgetchar
cmp $10, %eax
je 1f
cmp $0, %eax
je 1f
jmp backslash
1: ret
END_WORD(backslash)
BUILTIN_VARIABLE(base, 10)
BUILTIN_WORD_NAME(parse_char, "parse-char")
call fgetchar
subl $4, %esi
movl %eax, (%esi)
ret
END_WORD(parse_char)
BUILTIN_WORD_NAME(peek_char, "peek-char")
call fpeekchar
subl $4, %esi
movl %eax, (%esi)
ret
END_WORD(peek_char)
/********************************** COMPILER **********************************/
/* If it is not null, the following call to read_word will return that
address indeed of read from the input stream. It could point to an
empty string, which is used to create anonymous words. */
BUILTIN_VARIABLE(compiling_nextname, 0)
/* Last word defined. Note that it could be anonymous or non-accesible
from the current search order. */
BUILTIN_VARIABLE(latest_word, 0)
BUILTIN_WORD(header)
call cread_word
movl $1f, %edx
call create_uninterned_word
1: ret
END_WORD(header)
BUILTIN_WORD(reveal)
movl latest_word, %eax
movb (%eax), %cl
test %cl, %cl
jz 1f
movl current, %ebp
movl %eax, (%ebp)
1: ret
END_WORD(reveal)
BUILTIN_WORD_NAME(colon, ":")
movl $-1, state
call cread_word
movzbl (%eax), %ecx
movl %ecx, %edx
addl %edi, %edx
addl $10, %edx
call create_uninterned_word
1: ret
END_WORD(colon)
BUILTIN_WORD_ENTRY(semicolon, ";", F_IMMEDIATE | F_COMPILE_TIME)
/* Compile a RET into dictionary and set up interpretation mode */
movb $0xC3, (%edi) /* RET */
incl %edi
movl $0, state
call reveal
ret
END_WORD(semicolon)
BUILTIN_VARIABLE(state, 0)
/* Search order.
The representation for a wordlist is the following:
WID --> +--------------+
| latest | <-- NT of the last word defined in this wordlist
+--------------+
| method | <-- XT of a word which finding words in the wordlist
+--------------+ relies on. It takes a counted-string from the
| reserved | data stack and push associated NT, or 0 if it is
+--------------+ not found. It could also be zero to indicate that
| reserved | the default search method will be used.
+--------------+
The wordlist's identifier (WID) is a pointer to that structure. The
two reserved cells are provided to implement VOCS and MARKER