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
202 stack-reg stack-frame-size bootstrap-cell - ADD
203 ] jit-epilog jit-define
205 [ 0 RET ] jit-return jit-define
207 ! ! ! Polymorphic inline caches
209 ! The PIC stubs are not permitted to touch pic-tail-reg.
211 ! Load a value from a stack position
213 temp1 ds-reg HEX: 7f [+] MOV f rc-absolute-1 rel-untagged
214 ] pic-load jit-define
216 [ temp1 tag-mask get AND ] pic-tag jit-define
220 temp1 tag-mask get AND
221 temp1 tuple type-number CMP
223 [ temp1 temp0 tuple-class-offset [+] MOV ]
225 ] pic-tuple jit-define
228 temp1 HEX: 7f CMP f rc-absolute-1 rel-untagged
229 ] pic-check-tag jit-define
231 [ 0 JE f rc-relative rel-word ] pic-hit jit-define
233 ! ! ! Megamorphic caches
238 temp1 tag-mask get AND
239 temp1 tag-bits get SHL
240 temp1 tuple type-number tag-fixnum CMP
242 [ temp1 temp0 tuple-class-offset [+] MOV ]
245 temp0 0 MOV f rc-absolute-cell rel-literal
246 ! key = hashcode(class)
248 bootstrap-cell 4 = [ temp2 1 SHR ] when
249 ! key &= cache.length - 1
250 temp2 mega-cache-size get 1 - bootstrap-cell * AND
251 ! cache += array-start-offset
252 temp0 array-start-offset ADD
255 ! if(get(cache) == class)
259 ! megamorphic_cache_hits++
260 temp1 0 MOV rc-absolute-cell rel-megamorphic-cache-hits
262 ! goto get(cache + bootstrap-cell)
263 temp0 temp0 bootstrap-cell [+] MOV
264 temp0 word-entry-point-offset [+] JMP
265 ! fall-through on miss
267 ] mega-lookup jit-define
276 temp0 tag-mask get AND
278 temp0 tag-bits get SHL
281 ] \ tag define-sub-primitive
286 ! adjust stack pointer
287 ds-reg bootstrap-cell SUB
290 ! turn slot number into offset
293 temp1 tag-bits get SHR
294 temp1 tag-bits get SHL
296 temp0 temp1 temp0 [+] MOV
299 ] \ slot define-sub-primitive
302 ! load string index from stack
303 temp0 ds-reg bootstrap-cell neg [+] MOV
304 temp0 tag-bits get SHR
305 ! load string from stack
308 temp0 8-bit-version-of temp0 temp1 string-offset [++] MOV
309 temp0 temp0 8-bit-version-of MOVZX
310 temp0 tag-bits get SHL
311 ! store character to stack
312 ds-reg bootstrap-cell SUB
314 ] \ string-nth-fast define-sub-primitive
318 ds-reg bootstrap-cell SUB
319 ] \ drop define-sub-primitive
322 ds-reg 2 bootstrap-cells SUB
323 ] \ 2drop define-sub-primitive
326 ds-reg 3 bootstrap-cells SUB
327 ] \ 3drop define-sub-primitive
331 ds-reg bootstrap-cell ADD
333 ] \ dup define-sub-primitive
337 temp1 ds-reg bootstrap-cell neg [+] MOV
338 ds-reg 2 bootstrap-cells ADD
340 ds-reg bootstrap-cell neg [+] temp1 MOV
341 ] \ 2dup define-sub-primitive
345 temp1 ds-reg -1 bootstrap-cells [+] MOV
346 temp3 ds-reg -2 bootstrap-cells [+] MOV
347 ds-reg 3 bootstrap-cells ADD
349 ds-reg -1 bootstrap-cells [+] temp1 MOV
350 ds-reg -2 bootstrap-cells [+] temp3 MOV
351 ] \ 3dup define-sub-primitive
355 ds-reg bootstrap-cell SUB
357 ] \ nip define-sub-primitive
361 ds-reg 2 bootstrap-cells SUB
363 ] \ 2nip define-sub-primitive
366 temp0 ds-reg -1 bootstrap-cells [+] MOV
367 ds-reg bootstrap-cell ADD
369 ] \ over define-sub-primitive
372 temp0 ds-reg -2 bootstrap-cells [+] MOV
373 ds-reg bootstrap-cell ADD
375 ] \ pick define-sub-primitive
379 temp1 ds-reg -1 bootstrap-cells [+] MOV
381 ds-reg bootstrap-cell ADD
383 ] \ dupd define-sub-primitive
387 temp1 ds-reg bootstrap-cell neg [+] MOV
388 ds-reg bootstrap-cell neg [+] temp0 MOV
390 ] \ swap define-sub-primitive
393 temp0 ds-reg -1 bootstrap-cells [+] MOV
394 temp1 ds-reg -2 bootstrap-cells [+] MOV
395 ds-reg -2 bootstrap-cells [+] temp0 MOV
396 ds-reg -1 bootstrap-cells [+] temp1 MOV
397 ] \ swapd define-sub-primitive
401 temp1 ds-reg -1 bootstrap-cells [+] MOV
402 temp3 ds-reg -2 bootstrap-cells [+] MOV
403 ds-reg -2 bootstrap-cells [+] temp1 MOV
404 ds-reg -1 bootstrap-cells [+] temp0 MOV
406 ] \ rot define-sub-primitive
410 temp1 ds-reg -1 bootstrap-cells [+] MOV
411 temp3 ds-reg -2 bootstrap-cells [+] MOV
412 ds-reg -2 bootstrap-cells [+] temp0 MOV
413 ds-reg -1 bootstrap-cells [+] temp3 MOV
415 ] \ -rot define-sub-primitive
417 [ jit->r ] \ load-local define-sub-primitive
420 : jit-compare ( insn -- )
422 temp3 0 MOV t rc-absolute-cell rel-literal
424 temp1 \ f type-number MOV
427 ! adjust stack pointer
428 ds-reg bootstrap-cell SUB
429 ! compare with second value
432 [ temp1 temp3 ] dip execute( dst src -- )
434 ds-reg [] temp1 MOV ;
436 : define-jit-compare ( insn word -- )
437 [ [ jit-compare ] curry ] dip define-sub-primitive ;
439 \ CMOVE \ eq? define-jit-compare
440 \ CMOVGE \ fixnum>= define-jit-compare
441 \ CMOVLE \ fixnum<= define-jit-compare
442 \ CMOVG \ fixnum> define-jit-compare
443 \ CMOVL \ fixnum< define-jit-compare
446 : jit-math ( insn -- )
450 ds-reg bootstrap-cell SUB
452 [ ds-reg [] temp0 ] dip execute( dst src -- ) ;
454 [ \ ADD jit-math ] \ fixnum+fast define-sub-primitive
456 [ \ SUB jit-math ] \ fixnum-fast define-sub-primitive
462 ds-reg bootstrap-cell SUB
466 temp0 tag-bits get SAR
471 ] \ fixnum*fast define-sub-primitive
473 [ \ AND jit-math ] \ fixnum-bitand define-sub-primitive
475 [ \ OR jit-math ] \ fixnum-bitor define-sub-primitive
477 [ \ XOR jit-math ] \ fixnum-bitxor define-sub-primitive
483 ds-reg [] tag-mask get XOR
484 ] \ fixnum-bitnot define-sub-primitive
488 shift-arg ds-reg [] MOV
490 shift-arg tag-bits get SAR
491 ! adjust stack pointer
492 ds-reg bootstrap-cell SUB
497 ! compute positive shift value in temp2
500 ! compute negative shift value in temp3
502 temp3 tag-mask get bitnot AND
504 ! if shift count was negative, move temp0 to temp2
508 ] \ fixnum-shift-fast define-sub-primitive
510 : jit-fixnum-/mod ( -- )
511 ! load second parameter
513 ! load first parameter
514 div-arg ds-reg bootstrap-cell neg [+] MOV
518 mod-arg bootstrap-cell-bits 1 - SAR
524 ! adjust stack pointer
525 ds-reg bootstrap-cell SUB
527 ds-reg [] mod-arg MOV
528 ] \ fixnum-mod define-sub-primitive
532 ! adjust stack pointer
533 ds-reg bootstrap-cell SUB
535 div-arg tag-bits get SHL
537 ds-reg [] div-arg MOV
538 ] \ fixnum/i-fast define-sub-primitive
543 div-arg tag-bits get SHL
545 ds-reg [] mod-arg MOV
546 ds-reg bootstrap-cell neg [+] div-arg MOV
547 ] \ fixnum/mod-fast define-sub-primitive
551 ds-reg bootstrap-cell SUB
553 temp0 tag-mask get TEST
554 temp0 \ f type-number MOV
555 temp1 1 tag-fixnum MOV
558 ] \ both-fixnums? define-sub-primitive
563 ! turn local number into offset
566 temp0 rs-reg temp0 [+] MOV
569 ] \ get-local define-sub-primitive
574 ! adjust stack pointer
575 ds-reg bootstrap-cell SUB
576 ! turn local number into offset
578 ! decrement retain stack pointer
580 ] \ drop-locals define-sub-primitive
582 [ "bootstrap.x86" forget-vocab ] with-compilation-unit