1 ! Copyright (C) 2007, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: bootstrap.image.private kernel kernel.private namespaces system
4 layouts compiler.units math math.private compiler.constants vocabs
5 slots.private words locals.backend make sequences combinators arrays
6 cpu.x86.assembler cpu.x86.assembler.operands ;
13 temp0 0 MOV rc-absolute-cell rt-immediate jit-rel
14 ! Bump profiling counter
15 temp0 profile-count-offset [+] 1 tag-fixnum ADD
17 temp0 temp0 word-code-offset [+] MOV
19 temp0 compiled-header-size ADD
22 ] jit-profiling jit-define
26 temp0 0 MOV rc-absolute-cell rt-this jit-rel
27 ! save stack frame size
32 stack-reg stack-frame-size 3 bootstrap-cells - SUB
33 ] jit-prolog jit-define
37 temp0 0 MOV rc-absolute-cell rt-immediate jit-rel
38 ! increment datastack pointer
39 ds-reg bootstrap-cell ADD
40 ! store literal on datastack
42 ] jit-push-immediate jit-define
45 temp3 0 MOV rc-absolute-cell rt-here jit-rel
46 0 JMP rc-relative rt-xt-pic-tail jit-rel
47 ] jit-word-jump jit-define
50 0 CALL rc-relative rt-xt-pic jit-rel
51 ] jit-word-call jit-define
54 0 JMP rc-relative rt-xt jit-rel
55 ] jit-word-special jit-define
61 ds-reg bootstrap-cell SUB
62 ! compare boolean with f
63 temp0 \ f tag-number CMP
64 ! jump to true branch if not equal
65 0 JNE rc-relative rt-xt jit-rel
66 ! jump to false branch if equal
67 0 JMP rc-relative rt-xt jit-rel
71 rs-reg bootstrap-cell ADD
73 ds-reg bootstrap-cell SUB
77 rs-reg 2 bootstrap-cells ADD
79 temp1 ds-reg -1 bootstrap-cells [+] MOV
80 ds-reg 2 bootstrap-cells SUB
82 rs-reg -1 bootstrap-cells [+] temp1 MOV ;
85 rs-reg 3 bootstrap-cells ADD
87 temp1 ds-reg -1 bootstrap-cells [+] MOV
88 temp2 ds-reg -2 bootstrap-cells [+] MOV
89 ds-reg 3 bootstrap-cells SUB
91 rs-reg -1 bootstrap-cells [+] temp1 MOV
92 rs-reg -2 bootstrap-cells [+] temp2 MOV ;
95 ds-reg bootstrap-cell ADD
97 rs-reg bootstrap-cell SUB
101 ds-reg 2 bootstrap-cells ADD
103 temp1 rs-reg -1 bootstrap-cells [+] MOV
104 rs-reg 2 bootstrap-cells SUB
106 ds-reg -1 bootstrap-cells [+] temp1 MOV ;
109 ds-reg 3 bootstrap-cells ADD
111 temp1 rs-reg -1 bootstrap-cells [+] MOV
112 temp2 rs-reg -2 bootstrap-cells [+] MOV
113 rs-reg 3 bootstrap-cells SUB
115 ds-reg -1 bootstrap-cells [+] temp1 MOV
116 ds-reg -2 bootstrap-cells [+] temp2 MOV ;
120 0 CALL rc-relative rt-xt jit-rel
126 0 CALL rc-relative rt-xt jit-rel
128 ] jit-2dip jit-define
132 0 CALL rc-relative rt-xt jit-rel
134 ] jit-3dip jit-define
136 : prepare-(execute) ( -- operand )
140 ds-reg bootstrap-cell SUB
142 temp0 word-xt-offset [+] ;
144 [ prepare-(execute) JMP ] jit-execute-jump jit-define
146 [ prepare-(execute) CALL ] jit-execute-call jit-define
150 stack-reg stack-frame-size bootstrap-cell - ADD
151 ] jit-epilog jit-define
153 [ 0 RET ] jit-return jit-define
155 ! ! ! Polymorphic inline caches
157 ! The PIC and megamorphic code stubs are not permitted to touch temp3.
159 ! Load a value from a stack position
161 temp1 ds-reg HEX: ffffffff [+] MOV rc-absolute rt-untagged jit-rel
162 ] pic-load jit-define
166 temp1 tag-mask get AND
167 temp1 tag-bits get SHL ;
169 [ load-tag ] pic-tag jit-define
171 ! The 'make' trick lets us compute the jump distance for the
172 ! conditional branches there
178 temp1 object tag-number tag-fixnum CMP
179 [ temp1 temp0 object tag-number neg [+] MOV ] { } make
180 [ length JNE ] [ % ] bi
181 ] pic-hi-tag jit-define
187 temp1 tuple tag-number tag-fixnum CMP
188 [ temp1 temp0 tuple tag-number neg bootstrap-cell + [+] MOV ] { } make
189 [ length JNE ] [ % ] bi
190 ] pic-tuple jit-define
196 ! If bits 2 and 3 are set, the tag is either 6 (object) or 7 (tuple)
197 temp1 BIN: 110 tag-fixnum CMP
200 temp0 tag-mask get bitnot AND
201 ! Set temp1 to 0 for objects, and bootstrap-cell for tuples
202 temp1 1 tag-fixnum AND
203 bootstrap-cell 4 = [ temp1 1 SHR ] when
204 ! Load header cell or tuple layout cell
205 temp1 temp0 temp1 [+] MOV
206 ] [ ] make [ length JL ] [ % ] bi
207 ] pic-hi-tag-tuple jit-define
210 temp1 HEX: ffffffff CMP rc-absolute rt-immediate jit-rel
211 ] pic-check-tag jit-define
214 temp2 HEX: ffffffff MOV rc-absolute-cell rt-immediate jit-rel
216 ] pic-check jit-define
218 [ 0 JE rc-relative rt-xt jit-rel ] pic-hit jit-define
220 ! ! ! Megamorphic caches
224 temp0 0 MOV rc-absolute-cell rt-immediate jit-rel
227 bootstrap-cell 8 = [ temp2 1 SHL ] when
228 ! key &= cache.length - 1
229 temp2 mega-cache-size get 1 - bootstrap-cell * AND
230 ! cache += array-start-offset
231 temp0 array-start-offset ADD
234 ! if(get(cache) == class)
236 bootstrap-cell 4 = 14 22 ? JNE ! Yuck!
237 ! megamorphic_cache_hits++
238 temp1 0 MOV rc-absolute-cell rt-megamorphic-cache-hits jit-rel
240 ! goto get(cache + bootstrap-cell)
241 temp0 temp0 bootstrap-cell [+] MOV
242 temp0 word-xt-offset [+] JMP
243 ! fall-through on miss
244 ] mega-lookup jit-define
248 ! Quotations and words
253 ds-reg bootstrap-cell SUB
255 arg quot-xt-offset [+] JMP
256 ] \ (call) define-sub-primitive
263 temp0 tag-mask get AND
265 temp0 tag-bits get SHL
268 ] \ tag define-sub-primitive
273 ! adjust stack pointer
274 ds-reg bootstrap-cell SUB
277 ! turn slot number into offset
280 temp1 tag-bits get SHR
281 temp1 tag-bits get SHL
283 temp0 temp1 temp0 [+] MOV
286 ] \ slot define-sub-primitive
290 ds-reg bootstrap-cell SUB
291 ] \ drop define-sub-primitive
294 ds-reg 2 bootstrap-cells SUB
295 ] \ 2drop define-sub-primitive
298 ds-reg 3 bootstrap-cells SUB
299 ] \ 3drop define-sub-primitive
303 ds-reg bootstrap-cell ADD
305 ] \ dup define-sub-primitive
309 temp1 ds-reg bootstrap-cell neg [+] MOV
310 ds-reg 2 bootstrap-cells ADD
312 ds-reg bootstrap-cell neg [+] temp1 MOV
313 ] \ 2dup define-sub-primitive
317 temp1 ds-reg -1 bootstrap-cells [+] MOV
318 temp3 ds-reg -2 bootstrap-cells [+] MOV
319 ds-reg 3 bootstrap-cells ADD
321 ds-reg -1 bootstrap-cells [+] temp1 MOV
322 ds-reg -2 bootstrap-cells [+] temp3 MOV
323 ] \ 3dup define-sub-primitive
327 ds-reg bootstrap-cell SUB
329 ] \ nip define-sub-primitive
333 ds-reg 2 bootstrap-cells SUB
335 ] \ 2nip define-sub-primitive
338 temp0 ds-reg -1 bootstrap-cells [+] MOV
339 ds-reg bootstrap-cell ADD
341 ] \ over define-sub-primitive
344 temp0 ds-reg -2 bootstrap-cells [+] MOV
345 ds-reg bootstrap-cell ADD
347 ] \ pick define-sub-primitive
351 temp1 ds-reg -1 bootstrap-cells [+] MOV
353 ds-reg bootstrap-cell ADD
355 ] \ dupd define-sub-primitive
359 temp1 ds-reg -1 bootstrap-cells [+] MOV
360 ds-reg bootstrap-cell ADD
362 ds-reg -1 bootstrap-cells [+] temp1 MOV
363 ds-reg -2 bootstrap-cells [+] temp0 MOV
364 ] \ tuck define-sub-primitive
368 temp1 ds-reg bootstrap-cell neg [+] MOV
369 ds-reg bootstrap-cell neg [+] temp0 MOV
371 ] \ swap define-sub-primitive
374 temp0 ds-reg -1 bootstrap-cells [+] MOV
375 temp1 ds-reg -2 bootstrap-cells [+] MOV
376 ds-reg -2 bootstrap-cells [+] temp0 MOV
377 ds-reg -1 bootstrap-cells [+] temp1 MOV
378 ] \ swapd define-sub-primitive
382 temp1 ds-reg -1 bootstrap-cells [+] MOV
383 temp3 ds-reg -2 bootstrap-cells [+] MOV
384 ds-reg -2 bootstrap-cells [+] temp1 MOV
385 ds-reg -1 bootstrap-cells [+] temp0 MOV
387 ] \ rot define-sub-primitive
391 temp1 ds-reg -1 bootstrap-cells [+] MOV
392 temp3 ds-reg -2 bootstrap-cells [+] MOV
393 ds-reg -2 bootstrap-cells [+] temp0 MOV
394 ds-reg -1 bootstrap-cells [+] temp3 MOV
396 ] \ -rot define-sub-primitive
398 [ jit->r ] \ load-local define-sub-primitive
401 : jit-compare ( insn -- )
403 temp3 0 MOV rc-absolute-cell rt-immediate jit-rel
405 temp1 \ f tag-number MOV
408 ! adjust stack pointer
409 ds-reg bootstrap-cell SUB
410 ! compare with second value
413 [ temp1 temp3 ] dip execute( dst src -- )
415 ds-reg [] temp1 MOV ;
417 : define-jit-compare ( insn word -- )
418 [ [ jit-compare ] curry ] dip define-sub-primitive ;
420 \ CMOVE \ eq? define-jit-compare
421 \ CMOVGE \ fixnum>= define-jit-compare
422 \ CMOVLE \ fixnum<= define-jit-compare
423 \ CMOVG \ fixnum> define-jit-compare
424 \ CMOVL \ fixnum< define-jit-compare
427 : jit-math ( insn -- )
431 ds-reg bootstrap-cell SUB
433 [ ds-reg [] temp0 ] dip execute( dst src -- ) ;
435 [ \ ADD jit-math ] \ fixnum+fast define-sub-primitive
437 [ \ SUB jit-math ] \ fixnum-fast define-sub-primitive
443 ds-reg bootstrap-cell SUB
447 temp0 tag-bits get SAR
452 ] \ fixnum*fast define-sub-primitive
454 [ \ AND jit-math ] \ fixnum-bitand define-sub-primitive
456 [ \ OR jit-math ] \ fixnum-bitor define-sub-primitive
458 [ \ XOR jit-math ] \ fixnum-bitxor define-sub-primitive
464 ds-reg [] tag-mask get XOR
465 ] \ fixnum-bitnot define-sub-primitive
469 shift-arg ds-reg [] MOV
471 shift-arg tag-bits get SAR
472 ! adjust stack pointer
473 ds-reg bootstrap-cell SUB
478 ! compute positive shift value in temp1
481 ! compute negative shift value in temp3
483 temp3 tag-mask get bitnot AND
485 ! if shift count was negative, move temp0 to temp1
489 ] \ fixnum-shift-fast define-sub-primitive
491 : jit-fixnum-/mod ( -- )
492 ! load second parameter
494 ! load first parameter
495 div-arg ds-reg bootstrap-cell neg [+] MOV
499 mod-arg bootstrap-cell-bits 1 - SAR
505 ! adjust stack pointer
506 ds-reg bootstrap-cell SUB
508 ds-reg [] mod-arg MOV
509 ] \ fixnum-mod define-sub-primitive
513 ! adjust stack pointer
514 ds-reg bootstrap-cell SUB
516 div-arg tag-bits get SHL
518 ds-reg [] div-arg MOV
519 ] \ fixnum/i-fast define-sub-primitive
524 div-arg tag-bits get SHL
526 ds-reg [] mod-arg MOV
527 ds-reg bootstrap-cell neg [+] div-arg MOV
528 ] \ fixnum/mod-fast define-sub-primitive
532 ds-reg bootstrap-cell SUB
534 temp0 tag-mask get AND
535 temp0 \ f tag-number MOV
536 temp1 1 tag-fixnum MOV
539 ] \ both-fixnums? define-sub-primitive
544 ! turn local number into offset
547 temp0 rs-reg temp0 [+] MOV
550 ] \ get-local define-sub-primitive
555 ! adjust stack pointer
556 ds-reg bootstrap-cell SUB
557 ! turn local number into offset
559 ! decrement retain stack pointer
561 ] \ drop-locals define-sub-primitive
563 [ "bootstrap.x86" forget-vocab ] with-compilation-unit