1 ! Copyright (C) 2007, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: bootstrap.image.private kernel kernel.private namespaces
4 system cpu.x86.assembler layouts compiler.units math
5 math.private compiler.constants vocabs slots.private words
6 words.private locals.backend ;
16 ! Bump profiling counter
17 temp0 profile-count-offset [+] 1 tag-fixnum ADD
19 temp0 temp0 word-code-offset [+] MOV
21 temp0 compiled-header-size ADD
24 ] rc-absolute-cell rt-immediate 1 rex-length + jit-profiling jit-define
29 ! save stack frame size
34 stack-reg stack-frame-size 3 bootstrap-cells - SUB
35 ] rc-absolute-cell rt-label 1 rex-length + jit-prolog jit-define
40 ! increment datastack pointer
41 ds-reg bootstrap-cell ADD
42 ! store literal on datastack
44 ] rc-absolute-cell rt-immediate 1 rex-length + jit-push-immediate jit-define
48 ] rc-relative rt-xt 1 jit-word-jump jit-define
52 ] rc-relative rt-xt 1 jit-word-call jit-define
58 ds-reg bootstrap-cell SUB
59 ! compare boolean with f
60 temp0 \ f tag-number CMP
61 ! jump to true branch if not equal
63 ] rc-relative rt-xt 10 rex-length 3 * + jit-if-1 jit-define
66 ! jump to false branch if equal
68 ] rc-relative rt-xt 1 jit-if-2 jit-define
75 ! turn it into an array offset
78 ds-reg bootstrap-cell SUB
79 ! compute quotation location
82 arg temp0 array-start-offset [+] MOV
83 ! execute branch. the quot must be in arg, since it might
85 arg quot-xt-offset [+] JMP
86 ] rc-absolute-cell rt-immediate 1 rex-length + jit-dispatch jit-define
89 rs-reg bootstrap-cell ADD
91 ds-reg bootstrap-cell SUB
95 rs-reg 2 bootstrap-cells ADD
97 temp1 ds-reg -1 bootstrap-cells [+] MOV
98 ds-reg 2 bootstrap-cells SUB
100 rs-reg -1 bootstrap-cells [+] temp1 MOV ;
103 rs-reg 3 bootstrap-cells ADD
105 temp1 ds-reg -1 bootstrap-cells [+] MOV
106 temp2 ds-reg -2 bootstrap-cells [+] MOV
107 ds-reg 3 bootstrap-cells SUB
109 rs-reg -1 bootstrap-cells [+] temp1 MOV
110 rs-reg -2 bootstrap-cells [+] temp2 MOV ;
113 ds-reg bootstrap-cell ADD
115 rs-reg bootstrap-cell SUB
116 ds-reg [] temp0 MOV ;
119 ds-reg 2 bootstrap-cells ADD
121 temp1 rs-reg -1 bootstrap-cells [+] MOV
122 rs-reg 2 bootstrap-cells SUB
124 ds-reg -1 bootstrap-cells [+] temp1 MOV ;
127 ds-reg 3 bootstrap-cells ADD
129 temp1 rs-reg -1 bootstrap-cells [+] MOV
130 temp2 rs-reg -2 bootstrap-cells [+] MOV
131 rs-reg 3 bootstrap-cells SUB
133 ds-reg -1 bootstrap-cells [+] temp1 MOV
134 ds-reg -2 bootstrap-cells [+] temp2 MOV ;
140 ] rc-relative rt-xt 11 rex-length 4 * + jit-dip jit-define
146 ] rc-relative rt-xt 17 rex-length 6 * + jit-2dip jit-define
152 ] rc-relative rt-xt 23 rex-length 8 * + jit-3dip jit-define
156 stack-reg stack-frame-size bootstrap-cell - ADD
157 ] f f f jit-epilog jit-define
159 [ 0 RET ] f f f jit-return jit-define
163 ! Quotations and words
168 ds-reg bootstrap-cell SUB
170 arg quot-xt-offset [+] JMP
171 ] f f f \ (call) define-sub-primitive
177 ds-reg bootstrap-cell SUB
179 temp0 word-xt-offset [+] JMP
180 ] f f f \ (execute) define-sub-primitive
187 temp0 tag-mask get AND
189 temp0 tag-bits get SHL
192 ] f f f \ tag define-sub-primitive
197 ! adjust stack pointer
198 ds-reg bootstrap-cell SUB
201 ! turn slot number into offset
204 temp1 tag-bits get SHR
205 temp1 tag-bits get SHL
207 temp0 temp1 temp0 [+] MOV
210 ] f f f \ slot define-sub-primitive
214 ds-reg bootstrap-cell SUB
215 ] f f f \ drop define-sub-primitive
218 ds-reg 2 bootstrap-cells SUB
219 ] f f f \ 2drop define-sub-primitive
222 ds-reg 3 bootstrap-cells SUB
223 ] f f f \ 3drop define-sub-primitive
227 ds-reg bootstrap-cell ADD
229 ] f f f \ dup define-sub-primitive
233 temp1 ds-reg bootstrap-cell neg [+] MOV
234 ds-reg 2 bootstrap-cells ADD
236 ds-reg bootstrap-cell neg [+] temp1 MOV
237 ] f f f \ 2dup define-sub-primitive
241 temp1 ds-reg -1 bootstrap-cells [+] MOV
242 temp3 ds-reg -2 bootstrap-cells [+] MOV
243 ds-reg 3 bootstrap-cells ADD
245 ds-reg -1 bootstrap-cells [+] temp1 MOV
246 ds-reg -2 bootstrap-cells [+] temp3 MOV
247 ] f f f \ 3dup define-sub-primitive
251 ds-reg bootstrap-cell SUB
253 ] f f f \ nip define-sub-primitive
257 ds-reg 2 bootstrap-cells SUB
259 ] f f f \ 2nip define-sub-primitive
262 temp0 ds-reg -1 bootstrap-cells [+] MOV
263 ds-reg bootstrap-cell ADD
265 ] f f f \ over define-sub-primitive
268 temp0 ds-reg -2 bootstrap-cells [+] MOV
269 ds-reg bootstrap-cell ADD
271 ] f f f \ pick define-sub-primitive
275 temp1 ds-reg -1 bootstrap-cells [+] MOV
277 ds-reg bootstrap-cell ADD
279 ] f f f \ dupd define-sub-primitive
283 temp1 ds-reg -1 bootstrap-cells [+] MOV
284 ds-reg bootstrap-cell ADD
286 ds-reg -1 bootstrap-cells [+] temp1 MOV
287 ds-reg -2 bootstrap-cells [+] temp0 MOV
288 ] f f f \ tuck define-sub-primitive
292 temp1 ds-reg bootstrap-cell neg [+] MOV
293 ds-reg bootstrap-cell neg [+] temp0 MOV
295 ] f f f \ swap define-sub-primitive
298 temp0 ds-reg -1 bootstrap-cells [+] MOV
299 temp1 ds-reg -2 bootstrap-cells [+] MOV
300 ds-reg -2 bootstrap-cells [+] temp0 MOV
301 ds-reg -1 bootstrap-cells [+] temp1 MOV
302 ] f f f \ swapd define-sub-primitive
306 temp1 ds-reg -1 bootstrap-cells [+] MOV
307 temp3 ds-reg -2 bootstrap-cells [+] MOV
308 ds-reg -2 bootstrap-cells [+] temp1 MOV
309 ds-reg -1 bootstrap-cells [+] temp0 MOV
311 ] f f f \ rot define-sub-primitive
315 temp1 ds-reg -1 bootstrap-cells [+] MOV
316 temp3 ds-reg -2 bootstrap-cells [+] MOV
317 ds-reg -2 bootstrap-cells [+] temp0 MOV
318 ds-reg -1 bootstrap-cells [+] temp3 MOV
320 ] f f f \ -rot define-sub-primitive
322 [ jit->r ] f f f \ load-local define-sub-primitive
325 : jit-compare ( insn -- )
329 temp1 \ f tag-number MOV
332 ! adjust stack pointer
333 ds-reg bootstrap-cell SUB
334 ! compare with second value
337 [ temp1 temp3 ] dip execute
339 ds-reg [] temp1 MOV ;
341 : define-jit-compare ( insn word -- )
342 [ [ jit-compare ] curry rc-absolute-cell rt-immediate 1 rex-length + ] dip
343 define-sub-primitive ;
345 \ CMOVE \ eq? define-jit-compare
346 \ CMOVGE \ fixnum>= define-jit-compare
347 \ CMOVLE \ fixnum<= define-jit-compare
348 \ CMOVG \ fixnum> define-jit-compare
349 \ CMOVL \ fixnum< define-jit-compare
352 : jit-math ( insn -- )
356 ds-reg bootstrap-cell SUB
358 [ ds-reg [] temp0 ] dip execute ;
360 [ \ ADD jit-math ] f f f \ fixnum+fast define-sub-primitive
362 [ \ SUB jit-math ] f f f \ fixnum-fast define-sub-primitive
368 ds-reg bootstrap-cell SUB
372 temp0 tag-bits get SAR
377 ] f f f \ fixnum*fast define-sub-primitive
379 [ \ AND jit-math ] f f f \ fixnum-bitand define-sub-primitive
381 [ \ OR jit-math ] f f f \ fixnum-bitor define-sub-primitive
383 [ \ XOR jit-math ] f f f \ fixnum-bitxor define-sub-primitive
389 ds-reg [] tag-mask get XOR
390 ] f f f \ fixnum-bitnot define-sub-primitive
394 shift-arg ds-reg [] MOV
396 shift-arg tag-bits get SAR
397 ! adjust stack pointer
398 ds-reg bootstrap-cell SUB
403 ! compute positive shift value in temp1
406 ! compute negative shift value in temp3
408 temp3 tag-mask get bitnot AND
410 ! if shift count was negative, move temp0 to temp1
414 ] f f f \ fixnum-shift-fast define-sub-primitive
416 : jit-fixnum-/mod ( -- )
417 ! load second parameter
419 ! load first parameter
420 div-arg ds-reg bootstrap-cell neg [+] MOV
424 mod-arg bootstrap-cell-bits 1- SAR
430 ! adjust stack pointer
431 ds-reg bootstrap-cell SUB
433 ds-reg [] mod-arg MOV
434 ] f f f \ fixnum-mod define-sub-primitive
438 ! adjust stack pointer
439 ds-reg bootstrap-cell SUB
441 div-arg tag-bits get SHL
443 ds-reg [] div-arg MOV
444 ] f f f \ fixnum/i-fast define-sub-primitive
449 div-arg tag-bits get SHL
451 ds-reg [] mod-arg MOV
452 ds-reg bootstrap-cell neg [+] div-arg MOV
453 ] f f f \ fixnum/mod-fast define-sub-primitive
457 ds-reg bootstrap-cell SUB
459 temp0 tag-mask get AND
460 temp0 \ f tag-number MOV
461 temp1 1 tag-fixnum MOV
464 ] f f f \ both-fixnums? define-sub-primitive
469 ! turn local number into offset
472 temp0 rs-reg temp0 [+] MOV
475 ] f f f \ get-local define-sub-primitive
480 ! adjust stack pointer
481 ds-reg bootstrap-cell SUB
482 ! turn local number into offset
484 ! decrement retain stack pointer
486 ] f f f \ drop-locals define-sub-primitive
488 [ "bootstrap.x86" forget-vocab ] with-compilation-unit