1 ! Copyright (C) 2007, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: bootstrap.image.private compiler.constants
4 compiler.units cpu.x86.assembler cpu.x86.assembler.operands
5 kernel kernel.private layouts locals.backend make math
6 math.private namespaces sequences slots.private vocabs ;
13 temp0 0 MOV rc-absolute-cell rt-literal 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-literal jit-rel
27 ! increment datastack pointer
28 ds-reg bootstrap-cell ADD
29 ! store literal on datastack
34 temp3 0 MOV rc-absolute-cell rt-here jit-rel
35 0 JMP rc-relative rt-xt-pic-tail jit-rel
36 ] jit-word-jump jit-define
39 0 CALL rc-relative rt-xt-pic jit-rel
40 ] jit-word-call jit-define
46 ds-reg bootstrap-cell SUB
47 ! compare boolean with f
48 temp0 \ f type-number CMP
49 ! jump to true branch if not equal
50 0 JNE rc-relative rt-xt jit-rel
51 ! jump to false branch if equal
52 0 JMP rc-relative rt-xt jit-rel
56 rs-reg bootstrap-cell ADD
58 ds-reg bootstrap-cell SUB
62 rs-reg 2 bootstrap-cells ADD
64 temp1 ds-reg -1 bootstrap-cells [+] MOV
65 ds-reg 2 bootstrap-cells SUB
67 rs-reg -1 bootstrap-cells [+] temp1 MOV ;
70 rs-reg 3 bootstrap-cells ADD
72 temp1 ds-reg -1 bootstrap-cells [+] MOV
73 temp2 ds-reg -2 bootstrap-cells [+] MOV
74 ds-reg 3 bootstrap-cells SUB
76 rs-reg -1 bootstrap-cells [+] temp1 MOV
77 rs-reg -2 bootstrap-cells [+] temp2 MOV ;
80 ds-reg bootstrap-cell ADD
82 rs-reg bootstrap-cell SUB
86 ds-reg 2 bootstrap-cells ADD
88 temp1 rs-reg -1 bootstrap-cells [+] MOV
89 rs-reg 2 bootstrap-cells SUB
91 ds-reg -1 bootstrap-cells [+] temp1 MOV ;
94 ds-reg 3 bootstrap-cells ADD
96 temp1 rs-reg -1 bootstrap-cells [+] MOV
97 temp2 rs-reg -2 bootstrap-cells [+] MOV
98 rs-reg 3 bootstrap-cells SUB
100 ds-reg -1 bootstrap-cells [+] temp1 MOV
101 ds-reg -2 bootstrap-cells [+] temp2 MOV ;
105 0 CALL rc-relative rt-xt jit-rel
111 0 CALL rc-relative rt-xt jit-rel
113 ] jit-2dip jit-define
117 0 CALL rc-relative rt-xt jit-rel
119 ] jit-3dip jit-define
125 ds-reg bootstrap-cell SUB
127 arg2 0 MOV 0 rc-absolute-cell jit-vm
129 [ arg1 quot-xt-offset [+] CALL ]
130 [ arg1 quot-xt-offset [+] JMP ]
131 \ (call) define-sub-primitive*
137 ds-reg bootstrap-cell SUB
139 [ arg1 word-xt-offset [+] CALL ]
140 [ arg1 word-xt-offset [+] JMP ]
141 \ (execute) define-sub-primitive*
145 ds-reg bootstrap-cell SUB
146 arg1 word-xt-offset [+] JMP
147 ] jit-execute 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 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 tuple type-number tag-fixnum CMP
180 [ temp1 temp0 tuple type-number neg bootstrap-cell + [+] MOV ]
182 ] pic-tuple jit-define
185 temp1 HEX: ffffffff CMP rc-absolute rt-literal jit-rel
186 ] pic-check-tag jit-define
189 temp2 HEX: ffffffff MOV rc-absolute-cell rt-literal jit-rel
191 ] pic-check-tuple jit-define
193 [ 0 JE rc-relative rt-xt jit-rel ] pic-hit jit-define
195 ! ! ! Megamorphic caches
199 temp0 0 MOV rc-absolute-cell rt-literal jit-rel
200 ! key = hashcode(class)
202 bootstrap-cell 4 = [ temp2 1 SHR ] when
203 ! key &= cache.length - 1
204 temp2 mega-cache-size get 1 - bootstrap-cell * AND
205 ! cache += array-start-offset
206 temp0 array-start-offset ADD
209 ! if(get(cache) == class)
211 bootstrap-cell 4 = 14 22 ? JNE ! Yuck!
212 ! megamorphic_cache_hits++
213 temp1 0 MOV rc-absolute-cell rt-megamorphic-cache-hits jit-rel
215 ! goto get(cache + bootstrap-cell)
216 temp0 temp0 bootstrap-cell [+] MOV
217 temp0 word-xt-offset [+] JMP
218 ! fall-through on miss
219 ] mega-lookup jit-define
222 safe-reg 0 MOV rc-absolute-cell rt-xt jit-rel
224 ] callback-stub jit-define
233 temp0 tag-mask get AND
235 temp0 tag-bits get SHL
238 ] \ tag define-sub-primitive
243 ! adjust stack pointer
244 ds-reg bootstrap-cell SUB
247 ! turn slot number into offset
250 temp1 tag-bits get SHR
251 temp1 tag-bits get SHL
253 temp0 temp1 temp0 [+] MOV
256 ] \ slot define-sub-primitive
260 ds-reg bootstrap-cell SUB
261 ] \ drop define-sub-primitive
264 ds-reg 2 bootstrap-cells SUB
265 ] \ 2drop define-sub-primitive
268 ds-reg 3 bootstrap-cells SUB
269 ] \ 3drop define-sub-primitive
273 ds-reg bootstrap-cell ADD
275 ] \ dup define-sub-primitive
279 temp1 ds-reg bootstrap-cell neg [+] MOV
280 ds-reg 2 bootstrap-cells ADD
282 ds-reg bootstrap-cell neg [+] temp1 MOV
283 ] \ 2dup define-sub-primitive
287 temp1 ds-reg -1 bootstrap-cells [+] MOV
288 temp3 ds-reg -2 bootstrap-cells [+] MOV
289 ds-reg 3 bootstrap-cells ADD
291 ds-reg -1 bootstrap-cells [+] temp1 MOV
292 ds-reg -2 bootstrap-cells [+] temp3 MOV
293 ] \ 3dup define-sub-primitive
297 ds-reg bootstrap-cell SUB
299 ] \ nip define-sub-primitive
303 ds-reg 2 bootstrap-cells SUB
305 ] \ 2nip define-sub-primitive
308 temp0 ds-reg -1 bootstrap-cells [+] MOV
309 ds-reg bootstrap-cell ADD
311 ] \ over define-sub-primitive
314 temp0 ds-reg -2 bootstrap-cells [+] MOV
315 ds-reg bootstrap-cell ADD
317 ] \ pick define-sub-primitive
321 temp1 ds-reg -1 bootstrap-cells [+] MOV
323 ds-reg bootstrap-cell ADD
325 ] \ dupd define-sub-primitive
329 temp1 ds-reg bootstrap-cell neg [+] MOV
330 ds-reg bootstrap-cell neg [+] temp0 MOV
332 ] \ swap define-sub-primitive
335 temp0 ds-reg -1 bootstrap-cells [+] MOV
336 temp1 ds-reg -2 bootstrap-cells [+] MOV
337 ds-reg -2 bootstrap-cells [+] temp0 MOV
338 ds-reg -1 bootstrap-cells [+] temp1 MOV
339 ] \ swapd define-sub-primitive
343 temp1 ds-reg -1 bootstrap-cells [+] MOV
344 temp3 ds-reg -2 bootstrap-cells [+] MOV
345 ds-reg -2 bootstrap-cells [+] temp1 MOV
346 ds-reg -1 bootstrap-cells [+] temp0 MOV
348 ] \ rot define-sub-primitive
352 temp1 ds-reg -1 bootstrap-cells [+] MOV
353 temp3 ds-reg -2 bootstrap-cells [+] MOV
354 ds-reg -2 bootstrap-cells [+] temp0 MOV
355 ds-reg -1 bootstrap-cells [+] temp3 MOV
357 ] \ -rot define-sub-primitive
359 [ jit->r ] \ load-local define-sub-primitive
362 : jit-compare ( insn -- )
365 temp3 0 MOV rc-absolute-cell rt-literal jit-rel
367 temp1 \ f type-number MOV
370 ! adjust stack pointer
371 ds-reg bootstrap-cell SUB
372 ! compare with second value
375 [ temp1 temp3 ] dip execute( dst src -- )
377 ds-reg [] temp1 MOV ;
379 : define-jit-compare ( insn word -- )
380 [ [ jit-compare ] curry ] dip define-sub-primitive ;
382 \ CMOVE \ eq? define-jit-compare
383 \ CMOVGE \ fixnum>= define-jit-compare
384 \ CMOVLE \ fixnum<= define-jit-compare
385 \ CMOVG \ fixnum> define-jit-compare
386 \ CMOVL \ fixnum< define-jit-compare
389 : jit-math ( insn -- )
393 ds-reg bootstrap-cell SUB
395 [ ds-reg [] temp0 ] dip execute( dst src -- ) ;
397 [ \ ADD jit-math ] \ fixnum+fast define-sub-primitive
399 [ \ SUB jit-math ] \ fixnum-fast define-sub-primitive
405 ds-reg bootstrap-cell SUB
409 temp0 tag-bits get SAR
414 ] \ fixnum*fast define-sub-primitive
416 [ \ AND jit-math ] \ fixnum-bitand define-sub-primitive
418 [ \ OR jit-math ] \ fixnum-bitor define-sub-primitive
420 [ \ XOR jit-math ] \ fixnum-bitxor define-sub-primitive
426 ds-reg [] tag-mask get XOR
427 ] \ fixnum-bitnot define-sub-primitive
431 shift-arg ds-reg [] MOV
433 shift-arg tag-bits get SAR
434 ! adjust stack pointer
435 ds-reg bootstrap-cell SUB
440 ! compute positive shift value in temp1
443 ! compute negative shift value in temp3
445 temp3 tag-mask get bitnot AND
447 ! if shift count was negative, move temp0 to temp1
451 ] \ fixnum-shift-fast define-sub-primitive
453 : jit-fixnum-/mod ( -- )
454 ! load second parameter
456 ! load first parameter
457 div-arg ds-reg bootstrap-cell neg [+] MOV
461 mod-arg bootstrap-cell-bits 1 - SAR
467 ! adjust stack pointer
468 ds-reg bootstrap-cell SUB
470 ds-reg [] mod-arg MOV
471 ] \ fixnum-mod define-sub-primitive
475 ! adjust stack pointer
476 ds-reg bootstrap-cell SUB
478 div-arg tag-bits get SHL
480 ds-reg [] div-arg MOV
481 ] \ fixnum/i-fast define-sub-primitive
486 div-arg tag-bits get SHL
488 ds-reg [] mod-arg MOV
489 ds-reg bootstrap-cell neg [+] div-arg MOV
490 ] \ fixnum/mod-fast define-sub-primitive
494 ds-reg bootstrap-cell SUB
496 temp0 tag-mask get AND
497 temp0 \ f type-number MOV
498 temp1 1 tag-fixnum MOV
501 ] \ both-fixnums? define-sub-primitive
506 ! turn local number into offset
509 temp0 rs-reg temp0 [+] MOV
512 ] \ get-local define-sub-primitive
517 ! adjust stack pointer
518 ds-reg bootstrap-cell SUB
519 ! turn local number into offset
521 ! decrement retain stack pointer
523 ] \ drop-locals define-sub-primitive
525 [ "bootstrap.x86" forget-vocab ] with-compilation-unit