-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.mini
275 lines (228 loc) · 4.09 KB
/
init.mini
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
flush Some useful procedures
flush --
newhdr extends-assembler
proc asm
begin
dict load
8 add
copy bload
128 or
swap bstore
return
end
flush --
flush Assembles the following word regardless of immediacy
newhdr '
proc asm
begin
prs_nxt drop
prs_wrd find de_name add calign asm
return
end extends-assembler
flush --
flush Nicer comment syntax
newhdr #
proc asm
begin
' flush
return
end extends-assembler
# Now we have comments syntax, so lets define function syntax
# --
newhdr fn:
proc asm
begin
newhdr
proc asm
begin
return
end
# --
fn: end-fn
lit return asm
' end
return
end extends-assembler
# Now loops
# -- target
fn: do
here load
end-fn extends-assembler
# target --
fn: loop
lit branch asm
here load 8 add sub asm
end-fn extends-assembler
# Some output helpers
# --
fn: defstr:
newhdr
str asm
end-fn
defstr: nl
1 basm
10 basm
0 basm
defstr: tab
4 basm
32 copy copy copy basm basm basm basm
0 basm
# str len --
fn: println
print nl print
end-fn
# And a dictionary walk
# ptr --
fn: walk
do
tab print
copy de_name println
load copy
loop
drop
end-fn
fn: const:
newhdr
const asm
asm
end-fn
92 const: backslash
# --
fn: \
do
in_ptr load backslash prs_ch in_adv
swap not or not
loop
in_ptr load 1 add in_adv drop drop
end-fn extends-assembler
\
The above magic little definition lets us do block comments
like these.
\
34 const: quotes
# start end -- start len
fn: string-from-range
over sub
end-fn
fn: var-add \ n var -- \ swap over load add swap store end-fn
fn: copy-pair \ a b -- a b a b \ over over end-fn
fn: assemble-string \ string length -- \ swap over here load scopy here var-add end-fn
fn: realign-arena \ -- \ here load calign here store end-fn
fn: patch-target
\ patch-address -- \
here load over 8 add sub
swap store
end-fn
fn: if
\ -- patch-address \
lit zeroes asm
lit eq asm
lit branch asm
here load
0 asm
end-fn extends-assembler
fn: end-if
\ patch-address -- \
patch-target
end-fn extends-assembler
fn: assemble-literal
\ value -- \
lit lit asm
asm
end-fn
# Multiline strings should be easy to implement; each go of the loop we `scopy` the stuff we've found
# so far into the arena, and finally return pointers.
# -- string length
fn: s"
\
Block comments even work inside assembly mode
\
mode load if
# This will be patched later so that an inline string can be skipped
lit jump asm
here load push
0 asm
end-if
here load
do
in_ptr load
copy quotes prs_ch
string-from-range
copy-pair assemble-string add in_adv swap not or not
loop
1 add # Trim leading space
in_ptr load 1 add in_adv drop drop # Consume closing quote
0 basm # Null-terminate
here load
realign-arena
string-from-range 1 sub
mode load if
pop patch-target
swap
assemble-literal
assemble-literal
end-if
end-fn extends-assembler
fn: next-line \ -- \ nl print end-fn
s" Mini (C) 2023 David Detweiler" println
next-line
s" The kernel defines only the following instructions:" println
kernel walk
\
Assembles the machine code for:
mov wp, [tp]
add tp, 8
jmp [wp]
\
fn: asm-next
\ -- \
77 basm
139 basm
55 basm
73 basm
131 basm
199 basm
8 basm
65 basm
255 basm
38 basm
end-fn
fn: const:
\ value -- \
newhdr
const asm
asm
end-fn
\
The following is the machine code for:
lea rax, [wp + 8]
sub dp, 8
mov [dp], rax
next
\
here load
73 basm
141 basm
70 basm
8 basm
73 basm
131 basm
236 basm
8 basm
73 basm
137 basm
4 basm
36 basm
asm-next
realign-arena
const: var
fn: var:
\ cells -- \
newhdr
drop var asm
0 asm
end-fn
1 var: foo
12 foo store
foo load copy sub foo store
foo load exit