1 ! Copyright (C) 2007, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: bootstrap.image.private compiler.constants
4 compiler.codegen.relocation compiler.units cpu.x86.assembler
5 cpu.x86.assembler.operands kernel kernel.private layouts
6 locals.backend make math math.private namespaces sequences
7 slots.private strings.private vocabs ;
12 ! C to Factor entry point
14 ! Optimizing compiler's side of callback accesses
15 ! arguments that are on the stack via the frame pointer.
16 ! On x86-32 fastcall, and x86-64, some arguments are passed
17 ! in registers, and so the only registers that are safe for
18 ! use here are frame-reg, nv-reg and vm-reg.
20 frame-reg stack-reg MOV
22 ! Save all non-volatile registers
28 vm-reg 0 MOV 0 rc-absolute-cell rel-vm
31 nv-reg vm-reg vm-context-offset [+] MOV
34 ! Switch over to the spare context
35 nv-reg vm-reg vm-spare-context-offset [+] MOV
36 vm-reg vm-context-offset [+] nv-reg MOV
38 ! Save C callstack pointer
39 nv-reg context-callstack-save-offset [+] stack-reg MOV
41 ! Load Factor stack pointers
42 stack-reg nv-reg context-callstack-bottom-offset [+] MOV
46 rs-reg nv-reg context-retainstack-offset [+] MOV
47 ds-reg nv-reg context-datastack-offset [+] MOV
49 ! Call into Factor code
50 link-reg 0 MOV f rc-absolute-cell rel-word
53 ! Load VM into vm-reg; only needed on x86-32, but doesn't
55 vm-reg 0 MOV 0 rc-absolute-cell rel-vm
57 ! Load C callstack pointer
58 nv-reg vm-reg vm-context-offset [+] MOV
59 stack-reg nv-reg context-callstack-save-offset [+] MOV
63 vm-reg vm-context-offset [+] nv-reg MOV
65 ! Restore non-volatile registers
68 nv-regs <reversed> [ POP ] each
72 ! Callbacks which return structs, or use stdcall/fastcall/thiscall,
73 ! need a parameter here.
75 ! See the comment for M\ x86.32 stack-cleanup in cpu.x86.32
76 HEX: ffff RET f rc-absolute-2 rel-untagged
77 ] callback-stub jit-define
81 temp0 0 MOV f rc-absolute-cell rel-literal
82 ! Bump profiling counter
83 temp0 profile-count-offset [+] 1 tag-fixnum ADD
85 temp0 temp0 word-code-offset [+] MOV
86 ! Compute word entry point
87 temp0 compiled-header-size ADD
90 ] jit-profiling jit-define
94 temp0 0 MOV f rc-absolute-cell rel-literal
95 ! increment datastack pointer
96 ds-reg bootstrap-cell ADD
97 ! store literal on datastack
102 0 CALL f rc-relative rel-word-pic
103 ] jit-word-call jit-define
109 ds-reg bootstrap-cell SUB
110 ! compare boolean with f
111 temp0 \ f type-number CMP
112 ! jump to true branch if not equal
113 0 JNE f rc-relative rel-word
114 ! jump to false branch if equal
115 0 JMP f rc-relative rel-word
119 rs-reg bootstrap-cell ADD
121 ds-reg bootstrap-cell SUB
122 rs-reg [] temp0 MOV ;
125 rs-reg 2 bootstrap-cells ADD
127 temp1 ds-reg -1 bootstrap-cells [+] MOV
128 ds-reg 2 bootstrap-cells SUB
130 rs-reg -1 bootstrap-cells [+] temp1 MOV ;
133 rs-reg 3 bootstrap-cells ADD
135 temp1 ds-reg -1 bootstrap-cells [+] MOV
136 temp2 ds-reg -2 bootstrap-cells [+] MOV
137 ds-reg 3 bootstrap-cells SUB
139 rs-reg -1 bootstrap-cells [+] temp1 MOV
140 rs-reg -2 bootstrap-cells [+] temp2 MOV ;
143 ds-reg bootstrap-cell ADD
145 rs-reg bootstrap-cell SUB
146 ds-reg [] temp0 MOV ;
149 ds-reg 2 bootstrap-cells ADD
151 temp1 rs-reg -1 bootstrap-cells [+] MOV
152 rs-reg 2 bootstrap-cells SUB
154 ds-reg -1 bootstrap-cells [+] temp1 MOV ;
157 ds-reg 3 bootstrap-cells ADD
159 temp1 rs-reg -1 bootstrap-cells [+] MOV
160 temp2 rs-reg -2 bootstrap-cells [+] MOV
161 rs-reg 3 bootstrap-cells SUB
163 ds-reg -1 bootstrap-cells [+] temp1 MOV
164 ds-reg -2 bootstrap-cells [+] temp2 MOV ;
168 0 CALL f rc-relative rel-word
174 0 CALL f rc-relative rel-word
176 ] jit-2dip jit-define
180 0 CALL f rc-relative rel-word
182 ] jit-3dip jit-define
188 ds-reg bootstrap-cell SUB
190 [ temp0 word-entry-point-offset [+] CALL ]
191 [ temp0 word-entry-point-offset [+] JMP ]
192 \ (execute) define-combinator-primitive
196 ds-reg bootstrap-cell SUB
197 temp0 word-entry-point-offset [+] JMP
198 ] jit-execute jit-define
201 stack-reg stack-frame-size bootstrap-cell - ADD
202 ] jit-epilog jit-define
204 [ 0 RET ] jit-return jit-define
206 ! ! ! Polymorphic inline caches
208 ! The PIC stubs are not permitted to touch pic-tail-reg.
210 ! Load a value from a stack position
212 temp1 ds-reg HEX: 7f [+] MOV f rc-absolute-1 rel-untagged
213 ] pic-load jit-define
215 [ temp1 tag-mask get AND ] pic-tag jit-define
219 temp1 tag-mask get AND
220 temp1 tuple type-number CMP
222 [ temp1 temp0 tuple-class-offset [+] MOV ]
224 ] pic-tuple jit-define
227 temp1 HEX: 7f CMP f rc-absolute-1 rel-untagged
228 ] pic-check-tag jit-define
230 [ 0 JE f rc-relative rel-word ] pic-hit jit-define
232 ! ! ! Megamorphic caches
237 temp1 tag-mask get AND
238 temp1 tag-bits get SHL
239 temp1 tuple type-number tag-fixnum CMP
241 [ temp1 temp0 tuple-class-offset [+] MOV ]
244 temp0 0 MOV f rc-absolute-cell rel-literal
245 ! key = hashcode(class)
247 bootstrap-cell 4 = [ temp2 1 SHR ] when
248 ! key &= cache.length - 1
249 temp2 mega-cache-size get 1 - bootstrap-cell * AND
250 ! cache += array-start-offset
251 temp0 array-start-offset ADD
254 ! if(get(cache) == class)
258 ! megamorphic_cache_hits++
259 temp1 0 MOV rc-absolute-cell rel-megamorphic-cache-hits
261 ! goto get(cache + bootstrap-cell)
262 temp0 temp0 bootstrap-cell [+] MOV
263 temp0 word-entry-point-offset [+] JMP
264 ! fall-through on miss
266 ] mega-lookup jit-define
275 temp0 tag-mask get AND
277 temp0 tag-bits get SHL
280 ] \ tag define-sub-primitive
285 ! adjust stack pointer
286 ds-reg bootstrap-cell SUB
289 ! turn slot number into offset
292 temp1 tag-bits get SHR
293 temp1 tag-bits get SHL
295 temp0 temp1 temp0 [+] MOV
298 ] \ slot define-sub-primitive
301 ! load string index from stack
302 temp0 ds-reg bootstrap-cell neg [+] MOV
303 temp0 tag-bits get SHR
304 ! load string from stack
307 temp0 8-bit-version-of temp0 temp1 string-offset [++] MOV
308 temp0 temp0 8-bit-version-of MOVZX
309 temp0 tag-bits get SHL
310 ! store character to stack
311 ds-reg bootstrap-cell SUB
313 ] \ string-nth-fast define-sub-primitive
317 ds-reg bootstrap-cell SUB
318 ] \ drop define-sub-primitive
321 ds-reg 2 bootstrap-cells SUB
322 ] \ 2drop define-sub-primitive
325 ds-reg 3 bootstrap-cells SUB
326 ] \ 3drop define-sub-primitive
330 ds-reg bootstrap-cell ADD
332 ] \ dup define-sub-primitive
336 temp1 ds-reg bootstrap-cell neg [+] MOV
337 ds-reg 2 bootstrap-cells ADD
339 ds-reg bootstrap-cell neg [+] temp1 MOV
340 ] \ 2dup define-sub-primitive
344 temp1 ds-reg -1 bootstrap-cells [+] MOV
345 temp3 ds-reg -2 bootstrap-cells [+] MOV
346 ds-reg 3 bootstrap-cells ADD
348 ds-reg -1 bootstrap-cells [+] temp1 MOV
349 ds-reg -2 bootstrap-cells [+] temp3 MOV
350 ] \ 3dup define-sub-primitive
354 ds-reg bootstrap-cell SUB
356 ] \ nip define-sub-primitive
360 ds-reg 2 bootstrap-cells SUB
362 ] \ 2nip define-sub-primitive
365 temp0 ds-reg -1 bootstrap-cells [+] MOV
366 ds-reg bootstrap-cell ADD
368 ] \ over define-sub-primitive
371 temp0 ds-reg -2 bootstrap-cells [+] MOV
372 ds-reg bootstrap-cell ADD
374 ] \ pick define-sub-primitive
378 temp1 ds-reg -1 bootstrap-cells [+] MOV
380 ds-reg bootstrap-cell ADD
382 ] \ dupd define-sub-primitive
386 temp1 ds-reg bootstrap-cell neg [+] MOV
387 ds-reg bootstrap-cell neg [+] temp0 MOV
389 ] \ swap define-sub-primitive
392 temp0 ds-reg -1 bootstrap-cells [+] MOV
393 temp1 ds-reg -2 bootstrap-cells [+] MOV
394 ds-reg -2 bootstrap-cells [+] temp0 MOV
395 ds-reg -1 bootstrap-cells [+] temp1 MOV
396 ] \ swapd define-sub-primitive
400 temp1 ds-reg -1 bootstrap-cells [+] MOV
401 temp3 ds-reg -2 bootstrap-cells [+] MOV
402 ds-reg -2 bootstrap-cells [+] temp1 MOV
403 ds-reg -1 bootstrap-cells [+] temp0 MOV
405 ] \ rot define-sub-primitive
409 temp1 ds-reg -1 bootstrap-cells [+] MOV
410 temp3 ds-reg -2 bootstrap-cells [+] MOV
411 ds-reg -2 bootstrap-cells [+] temp0 MOV
412 ds-reg -1 bootstrap-cells [+] temp3 MOV
414 ] \ -rot define-sub-primitive
416 [ jit->r ] \ load-local define-sub-primitive
419 : jit-compare ( insn -- )
421 temp3 0 MOV t rc-absolute-cell rel-literal
423 temp1 \ f type-number MOV
426 ! adjust stack pointer
427 ds-reg bootstrap-cell SUB
428 ! compare with second value
431 [ temp1 temp3 ] dip execute( dst src -- )
433 ds-reg [] temp1 MOV ;
435 : define-jit-compare ( insn word -- )
436 [ [ jit-compare ] curry ] dip define-sub-primitive ;
438 \ CMOVE \ eq? define-jit-compare
439 \ CMOVGE \ fixnum>= define-jit-compare
440 \ CMOVLE \ fixnum<= define-jit-compare
441 \ CMOVG \ fixnum> define-jit-compare
442 \ CMOVL \ fixnum< define-jit-compare
445 : jit-math ( insn -- )
449 ds-reg bootstrap-cell SUB
451 [ ds-reg [] temp0 ] dip execute( dst src -- ) ;
453 [ \ ADD jit-math ] \ fixnum+fast define-sub-primitive
455 [ \ SUB jit-math ] \ fixnum-fast define-sub-primitive
461 ds-reg bootstrap-cell SUB
465 temp0 tag-bits get SAR
470 ] \ fixnum*fast define-sub-primitive
472 [ \ AND jit-math ] \ fixnum-bitand define-sub-primitive
474 [ \ OR jit-math ] \ fixnum-bitor define-sub-primitive
476 [ \ XOR jit-math ] \ fixnum-bitxor define-sub-primitive
482 ds-reg [] tag-mask get XOR
483 ] \ fixnum-bitnot define-sub-primitive
487 shift-arg ds-reg [] MOV
489 shift-arg tag-bits get SAR
490 ! adjust stack pointer
491 ds-reg bootstrap-cell SUB
496 ! compute positive shift value in temp2
499 ! compute negative shift value in temp3
501 temp3 tag-mask get bitnot AND
503 ! if shift count was negative, move temp0 to temp2
507 ] \ fixnum-shift-fast define-sub-primitive
509 : jit-fixnum-/mod ( -- )
510 ! load second parameter
512 ! load first parameter
513 div-arg ds-reg bootstrap-cell neg [+] MOV
517 mod-arg bootstrap-cell-bits 1 - SAR
523 ! adjust stack pointer
524 ds-reg bootstrap-cell SUB
526 ds-reg [] mod-arg MOV
527 ] \ fixnum-mod define-sub-primitive
531 ! adjust stack pointer
532 ds-reg bootstrap-cell SUB
534 div-arg tag-bits get SHL
536 ds-reg [] div-arg MOV
537 ] \ fixnum/i-fast define-sub-primitive
542 div-arg tag-bits get SHL
544 ds-reg [] mod-arg MOV
545 ds-reg bootstrap-cell neg [+] div-arg MOV
546 ] \ fixnum/mod-fast define-sub-primitive
550 ds-reg bootstrap-cell SUB
552 temp0 tag-mask get TEST
553 temp0 \ f type-number MOV
554 temp1 1 tag-fixnum MOV
557 ] \ both-fixnums? define-sub-primitive
562 ! turn local number into offset
565 temp0 rs-reg temp0 [+] MOV
568 ] \ get-local define-sub-primitive
573 ! adjust stack pointer
574 ds-reg bootstrap-cell SUB
575 ! turn local number into offset
577 ! decrement retain stack pointer
579 ] \ drop-locals define-sub-primitive
581 [ "bootstrap.x86" forget-vocab ] with-compilation-unit