forked from jkotlinski/durexforth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.asm
323 lines (278 loc) · 5.13 KB
/
compiler.asm
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
;{{{ The MIT License
;
;Copyright (c) 2008 Johan Kotlinski
;
;Permission is hereby granted, free of charge, to any person obtaining a copy
;of this software and associated documentation files (the "Software"), to deal
;in the Software without restriction, including without limitation the rights
;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;copies of the Software, and to permit persons to whom the Software is
;furnished to do so, subject to the following conditions:
;
;The above copyright notice and this permission notice shall be included in
;all copies or substantial portions of the Software.
;
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;THE SOFTWARE. }}}
; C, , ; IMMEDIATE [ ] STATE : HEADER LIT LITC COMPILE, LITERAL HERE DODOES
curr_word_no_tail_call_elimination
!byte 1
last_word_no_tail_call_elimination
!byte 1
+BACKLINK
!byte 2
!text "c,"
CCOMMA
lda HERE_LSB
sta W
lda HERE_MSB
sta W + 1
ldy #0
lda LSB, x
sta (W), y
; update HERE
inc HERE_LSB
bne +
inc HERE_MSB
+ inx
rts
+BACKLINK
!byte 1
!text ","
COMMA
lda HERE_LSB
sta W
lda HERE_MSB
sta W + 1
ldy #0
lda LSB, x
sta (W), y
iny
lda MSB, x
sta (W), y
; update HERE
lda HERE_LSB
clc
adc #2
sta HERE_LSB
bcc +
inc HERE_MSB
+
inx
rts
; -----
+BACKLINK
!byte 1 | F_IMMEDIATE
!text "["
LBRAC
lda #0
sta STATE
rts
; -----
+BACKLINK
; disable tail call elimination in case of inline assembly
!byte 1 | F_NO_TAIL_CALL_ELIMINATION
!text "]"
RBRAC
lda #1
sta STATE
rts
+BACKLINK
!byte 1 | F_IMMEDIATE
!text ";"
SEMICOLON
jsr EXIT
; unhide the word.
jsr TOGGLE_LATEST_HIDDEN
; go back to IMMEDIATE mode.
jmp LBRAC
+BACKLINK
!byte 9
!text "immediate"
lda _LATEST
sta W
lda _LATEST + 1
sta W + 1
ldy #2
lda (W), y
ora #F_IMMEDIATE
sta (W), y
rts
; STATE - Is the interpreter executing code (0) or compiling a word (non-zero)?
+BACKLINK
!byte 5
!text "state"
+VALUE STATE
STATE
!word 0
+BACKLINK
!byte 1 | F_NO_TAIL_CALL_ELIMINATION
!text ":"
COLON
jsr HEADER ; makes the dictionary entry / header
; hide word
jsr TOGGLE_LATEST_HIDDEN
jmp RBRAC ; enter compile mode
TOGGLE_LATEST_HIDDEN
lda _LATEST
sta W
lda _LATEST + 1
sta W + 1
ldy #2 ; skip link, point to flags
lda (W), y
eor #F_HIDDEN ; toggle hidden flag
sta (W), y
rts
; --- HEADER ( name -- )
+BACKLINK
!byte 6
!text "header"
HEADER
inc last_word_no_tail_call_elimination
jsr HERE
; Store backlink.
jsr LATEST
jsr FETCH
jsr COMMA
- jsr PARSE_NAME
lda LSB,x
bne +
jsr REFILL
jmp -
+
; Store length byte.
jsr DUP
jsr CCOMMA
- jsr SWAP
jsr DUP
jsr FETCHBYTE
lda LSB,x
jsr CHAR_TO_LOWERCASE
sta LSB,x
jsr CCOMMA
jsr ONEPLUS
jsr SWAP
jsr ONEMINUS
lda LSB,x
bne -
inx
inx
jsr LATEST
jmp STORE
+BACKLINK
!byte 3
!text "lit"
LIT
dex
; load IP
pla
sta W
pla
sta W + 1
; copy literal to stack
ldy #1
lda (W), y
sta LSB, x
iny
lda (W), y
sta MSB, x
lda W
clc
adc #3
sta + + 1
lda W + 1
adc #0
sta + + 2
+ jmp PLACEHOLDER_ADDRESS ; replaced with instruction pointer
+BACKLINK
!byte 4
!text "litc"
LITC
dex
; load IP
pla
sta W
pla
sta W + 1
inc W
bne +
inc W + 1
+
; copy literal to stack
ldy #0
lda (W), y
sta LSB, x
sty MSB, x
inc W
bne +
inc W + 1
+ jmp (W)
+BACKLINK
!byte 8
!text "compile,"
COMPILE_COMMA
lda #OP_JSR
jsr compile_a
jmp COMMA
+BACKLINK
!byte 7 | F_IMMEDIATE
!text "literal"
LITERAL
dex
lda MSB+1,x
bne +
lda #<LITC
sta LSB,x
lda #>LITC
sta MSB,x
jsr COMPILE_COMMA
jmp CCOMMA ; writes byte
+
lda #<LIT
sta LSB, x
lda #>LIT
sta MSB, x
jsr COMPILE_COMMA
jmp COMMA ; writes number
; HERE - points to the next free byte of memory. When compiling, compiled words go here.
+BACKLINK
!byte 4
!text "here"
HERE
HERE_LSB = * + 1
HERE_MSB = * + 3
+VALUE _LATEST + 2
!word LINK
!set LINK = * - 2
!byte 6
!text "dodoes"
; behavior pointer address => W
pla
sta W
pla
sta W + 1
inc W
bne +
inc W + 1
+
; push data pointer to param stack
dex
lda W
clc
adc #2
sta LSB,x
lda W + 1
adc #0
sta MSB,x
ldy #0
lda (W),y
sta W2
iny
lda (W),y
sta W2 + 1
jmp (W2)