]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/ppc/ppc.factor
Merge branch 'master' of git://factorcode.org/git/factor into abi-symbols
[factor.git] / basis / cpu / ppc / ppc.factor
1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs sequences kernel combinators make math
4 math.order math.ranges system namespaces locals layouts words
5 alien alien.accessors alien.c-types alien.complex alien.data
6 literals cpu.architecture cpu.ppc.assembler
7 cpu.ppc.assembler.backend compiler.cfg.registers
8 compiler.cfg.instructions compiler.cfg.comparisons
9 compiler.codegen.fixup compiler.cfg.intrinsics
10 compiler.cfg.stack-frame compiler.cfg.build-stack-frame
11 compiler.units compiler.constants compiler.codegen vm ;
12 FROM: cpu.ppc.assembler => B ;
13 FROM: layouts => cell ;
14 FROM: math => float ;
15 IN: cpu.ppc
16
17 ! PowerPC register assignments:
18 ! r2-r12: integer vregs
19 ! r13: data stack
20 ! r14: retain stack
21 ! r15: VM pointer
22 ! r16-r29: integer vregs
23 ! r30: integer scratch
24 ! f0-f29: float vregs
25 ! f30: float scratch
26
27 ! Add some methods to the assembler that are useful to us
28 M: label (B) [ 0 ] 2dip (B) rc-relative-ppc-3 label-fixup ;
29 M: label BC [ 0 BC ] dip rc-relative-ppc-2 label-fixup ;
30
31 enable-float-intrinsics
32
33 <<
34 \ ##integer>float t frame-required? set-word-prop
35 \ ##float>integer t frame-required? set-word-prop
36 >>
37
38 M: ppc machine-registers
39     {
40         { int-regs $[ 2 12 [a,b] 16 29 [a,b] append ] }
41         { float-regs $[ 0 29 [a,b] ] }
42     } ;
43
44 CONSTANT: scratch-reg 30
45 CONSTANT: fp-scratch-reg 30
46
47 M: ppc %load-immediate ( reg n -- ) swap LOAD ;
48
49 M: ppc %load-reference ( reg obj -- )
50     [ 0 swap LOAD32 ] [ rc-absolute-ppc-2/2 rel-immediate ] bi* ;
51
52 M: ppc %alien-global ( register symbol dll -- )
53     [ 0 swap LOAD32 ] 2dip rc-absolute-ppc-2/2 rel-dlsym ;
54
55 CONSTANT: ds-reg 13
56 CONSTANT: rs-reg 14
57 CONSTANT: vm-reg 15
58
59 : %load-vm-addr ( reg -- ) vm-reg MR ;
60
61 M: ppc %vm-field ( dst field -- ) [ vm-reg ] dip LWZ ;
62
63 M: ppc %set-vm-field ( src field -- ) [ vm-reg ] dip STW ;
64
65 GENERIC: loc-reg ( loc -- reg )
66
67 M: ds-loc loc-reg drop ds-reg ;
68 M: rs-loc loc-reg drop rs-reg ;
69
70 : loc>operand ( loc -- reg n )
71     [ loc-reg ] [ n>> cells neg ] bi ; inline
72
73 M: ppc %peek loc>operand LWZ ;
74 M: ppc %replace loc>operand STW ;
75
76 :: (%inc) ( n reg -- ) reg reg n cells ADDI ; inline
77
78 M: ppc %inc-d ( n -- ) ds-reg (%inc) ;
79 M: ppc %inc-r ( n -- ) rs-reg (%inc) ;
80
81 HOOK: reserved-area-size os ( -- n )
82
83 ! The start of the stack frame contains the size of this frame
84 ! as well as the currently executing code block
85 : factor-area-size ( -- n ) 2 cells ; foldable
86 : next-save ( n -- i ) cell - ; foldable
87 : xt-save ( n -- i ) 2 cells - ; foldable
88
89 ! Next, we have the spill area as well as the FFI parameter area.
90 ! It is safe for them to overlap, since basic blocks with FFI calls
91 ! will never spill -- indeed, basic blocks with FFI calls do not
92 ! use vregs at all, and the FFI call is a stack analysis sync point.
93 ! In the future this will change and the stack frame logic will
94 ! need to be untangled somewhat.
95
96 : param@ ( n -- x ) reserved-area-size + ; inline
97
98 : param-save-size ( -- n ) 8 cells ; foldable
99
100 : local@ ( n -- x )
101     reserved-area-size param-save-size + + ; inline
102
103 : spill@ ( n -- offset )
104     spill-offset local@ ;
105
106 ! Some FP intrinsics need a temporary scratch area in the stack
107 ! frame, 8 bytes in size. This is in the param-save area so it
108 ! does not overlap with spill slots.
109 : scratch@ ( n -- offset )
110     factor-area-size + ;
111
112 ! GC root area
113 : gc-root@ ( n -- offset )
114     gc-root-offset local@ ;
115
116 ! Finally we have the linkage area
117 HOOK: lr-save os ( -- n )
118
119 M: ppc stack-frame-size ( stack-frame -- i )
120     (stack-frame-size)
121     param-save-size +
122     reserved-area-size +
123     factor-area-size +
124     4 cells align ;
125
126 M: ppc %call ( word -- ) 0 BL rc-relative-ppc-3 rel-word-pic ;
127
128 M: ppc %jump ( word -- )
129     0 6 LOAD32 4 rc-absolute-ppc-2/2 rel-here
130     0 B rc-relative-ppc-3 rel-word-pic-tail ;
131
132 M: ppc %jump-label ( label -- ) B ;
133 M: ppc %return ( -- ) BLR ;
134
135 M:: ppc %dispatch ( src temp -- )
136     0 temp LOAD32
137     3 cells rc-absolute-ppc-2/2 rel-here
138     temp temp src LWZX
139     temp MTCTR
140     BCTR ;
141
142 M: ppc %slot ( dst obj slot -- ) swapd LWZX ;
143 M: ppc %slot-imm ( dst obj slot tag -- ) slot-offset LWZ ;
144 M: ppc %set-slot ( src obj slot -- ) swapd STWX ;
145 M: ppc %set-slot-imm ( src obj slot tag -- ) slot-offset STW ;
146
147 M:: ppc %string-nth ( dst src index temp -- )
148     [
149         "end" define-label
150         temp src index ADD
151         dst temp string-offset LBZ
152         0 dst HEX: 80 CMPI
153         "end" get BLT
154         temp src string-aux-offset LWZ
155         temp temp index ADD
156         temp temp index ADD
157         temp temp byte-array-offset LHZ
158         temp temp 7 SLWI
159         dst dst temp XOR
160         "end" resolve-label
161     ] with-scope ;
162
163 M:: ppc %set-string-nth-fast ( ch obj index temp -- )
164     temp obj index ADD
165     ch temp string-offset STB ;
166
167 M: ppc %add     ADD ;
168 M: ppc %add-imm ADDI ;
169 M: ppc %sub     swap SUBF ;
170 M: ppc %sub-imm SUBI ;
171 M: ppc %mul     MULLW ;
172 M: ppc %mul-imm MULLI ;
173 M: ppc %and     AND ;
174 M: ppc %and-imm ANDI ;
175 M: ppc %or      OR ;
176 M: ppc %or-imm  ORI ;
177 M: ppc %xor     XOR ;
178 M: ppc %xor-imm XORI ;
179 M: ppc %shl     SLW ;
180 M: ppc %shl-imm swapd SLWI ;
181 M: ppc %shr     SRW ;
182 M: ppc %shr-imm swapd SRWI ;
183 M: ppc %sar     SRAW ;
184 M: ppc %sar-imm SRAWI ;
185 M: ppc %not     NOT ;
186 M: ppc %neg     NEG ;
187
188 :: overflow-template ( label dst src1 src2 insn -- )
189     0 0 LI
190     0 MTXER
191     dst src2 src1 insn call
192     label BO ; inline
193
194 M: ppc %fixnum-add ( label dst src1 src2 -- )
195     [ ADDO. ] overflow-template ;
196
197 M: ppc %fixnum-sub ( label dst src1 src2 -- )
198     [ SUBFO. ] overflow-template ;
199
200 M: ppc %fixnum-mul ( label dst src1 src2 -- )
201     [ MULLWO. ] overflow-template ;
202
203 M: ppc %add-float FADD ;
204 M: ppc %sub-float FSUB ;
205 M: ppc %mul-float FMUL ;
206 M: ppc %div-float FDIV ;
207
208 M:: ppc %integer>float ( dst src -- )
209     HEX: 4330 scratch-reg LIS
210     scratch-reg 1 0 scratch@ STW
211     scratch-reg src MR
212     scratch-reg dup HEX: 8000 XORIS
213     scratch-reg 1 4 scratch@ STW
214     dst 1 0 scratch@ LFD
215     scratch-reg 4503601774854144.0 %load-reference
216     fp-scratch-reg scratch-reg float-offset LFD
217     dst dst fp-scratch-reg FSUB ;
218
219 M:: ppc %float>integer ( dst src -- )
220     fp-scratch-reg src FCTIWZ
221     fp-scratch-reg 1 0 scratch@ STFD
222     dst 1 4 scratch@ LWZ ;
223
224 M: ppc %copy ( dst src rep -- )
225     2over eq? [ 3drop ] [
226         {
227             { int-rep [ MR ] }
228             { double-rep [ FMR ] }
229         } case
230     ] if ;
231
232 GENERIC: float-function-param* ( dst src -- )
233
234 M: spill-slot float-function-param* [ 1 ] dip n>> spill@ LFD ;
235 M: integer float-function-param* FMR ;
236
237 : float-function-param ( i src -- )
238     [ float-regs cdecl param-regs nth ] dip float-function-param* ;
239
240 : float-function-return ( reg -- )
241     float-regs return-reg double-rep %copy ;
242
243 M:: ppc %unary-float-function ( dst src func -- )
244     0 src float-function-param
245     func f %alien-invoke
246     dst float-function-return ;
247
248 M:: ppc %binary-float-function ( dst src1 src2 func -- )
249     0 src1 float-function-param
250     1 src2 float-function-param
251     func f %alien-invoke
252     dst float-function-return ;
253
254 ! Internal format is always double-precision on PowerPC
255 M: ppc %single>double-float double-rep %copy ;
256 M: ppc %double>single-float FRSP ;
257
258 M: ppc %unbox-alien ( dst src -- )
259     alien-offset LWZ ;
260
261 M:: ppc %unbox-any-c-ptr ( dst src -- )
262     [
263         "end" define-label
264         0 dst LI
265         ! Is the object f?
266         0 src \ f type-number CMPI
267         "end" get BEQ
268         ! Compute tag in dst register
269         dst src tag-mask get ANDI
270         ! Is the object an alien?
271         0 dst alien type-number CMPI
272         ! Add an offset to start of byte array's data
273         dst src byte-array-offset ADDI
274         "end" get BNE
275         ! If so, load the offset and add it to the address
276         dst src alien-offset LWZ
277         "end" resolve-label
278     ] with-scope ;
279
280 : alien@ ( n -- n' ) cells alien type-number - ;
281
282 M:: ppc %box-alien ( dst src temp -- )
283     [
284         "f" define-label
285         dst \ f type-number %load-immediate
286         0 src 0 CMPI
287         "f" get BEQ
288         dst 5 cells alien temp %allot
289         temp \ f type-number %load-immediate
290         temp dst 1 alien@ STW
291         temp dst 2 alien@ STW
292         src dst 3 alien@ STW
293         src dst 4 alien@ STW
294         "f" resolve-label
295     ] with-scope ;
296
297 M:: ppc %box-displaced-alien ( dst displacement base temp base-class -- )
298     ! This is ridiculous
299     [
300         "end" define-label
301         "not-f" define-label
302         "not-alien" define-label
303
304         ! If displacement is zero, return the base
305         dst base MR
306         0 displacement 0 CMPI
307         "end" get BEQ
308
309         ! Displacement is non-zero, we're going to be allocating a new
310         ! object
311         dst 5 cells alien temp %allot
312
313         ! Set expired to f
314         temp \ f type-number %load-immediate
315         temp dst 2 alien@ STW
316
317         ! Is base f?
318         0 base \ f type-number CMPI
319         "not-f" get BNE
320
321         ! Yes, it is f. Fill in new object
322         base dst 1 alien@ STW
323         displacement dst 3 alien@ STW
324         displacement dst 4 alien@ STW
325
326         "end" get B
327
328         "not-f" resolve-label
329
330         ! Check base type
331         temp base tag-mask get ANDI
332
333         ! Is base an alien?
334         0 temp alien type-number CMPI
335         "not-alien" get BNE
336
337         ! Yes, it is an alien. Set new alien's base to base.base
338         temp base 1 alien@ LWZ
339         temp dst 1 alien@ STW
340
341         ! Compute displacement
342         temp base 3 alien@ LWZ
343         temp temp displacement ADD
344         temp dst 3 alien@ STW
345
346         ! Compute address
347         temp base 4 alien@ LWZ
348         temp temp displacement ADD
349         temp dst 4 alien@ STW
350
351         ! We are done
352         "end" get B
353
354         ! Is base a byte array? It has to be, by now...
355         "not-alien" resolve-label
356
357         base dst 1 alien@ STW
358         displacement dst 3 alien@ STW
359         temp base byte-array-offset ADDI
360         temp temp displacement ADD
361         temp dst 4 alien@ STW
362
363         "end" resolve-label
364     ] with-scope ;
365
366 M: ppc %alien-unsigned-1 LBZ ;
367 M: ppc %alien-unsigned-2 LHZ ;
368
369 M: ppc %alien-signed-1 [ dup ] 2dip LBZ dup EXTSB ;
370 M: ppc %alien-signed-2 LHA ;
371
372 M: ppc %alien-cell LWZ ;
373
374 M: ppc %alien-float LFS ;
375 M: ppc %alien-double LFD ;
376
377 M: ppc %set-alien-integer-1 -rot STB ;
378 M: ppc %set-alien-integer-2 -rot STH ;
379
380 M: ppc %set-alien-cell -rot STW ;
381
382 M: ppc %set-alien-float -rot STFS ;
383 M: ppc %set-alien-double -rot STFD ;
384
385 : load-zone-ptr ( reg -- )
386     vm-reg "nursery" vm-field-offset ADDI ;
387
388 : load-allot-ptr ( nursery-ptr allot-ptr -- )
389     [ drop load-zone-ptr ] [ swap 0 LWZ ] 2bi ;
390
391 :: inc-allot-ptr ( nursery-ptr allot-ptr n -- )
392     scratch-reg allot-ptr n data-alignment get align ADDI
393     scratch-reg nursery-ptr 0 STW ;
394
395 :: store-header ( dst class -- )
396     class type-number tag-header scratch-reg LI
397     scratch-reg dst 0 STW ;
398
399 : store-tagged ( dst tag -- )
400     dupd type-number ORI ;
401
402 M:: ppc %allot ( dst size class nursery-ptr -- )
403     nursery-ptr dst load-allot-ptr
404     nursery-ptr dst size inc-allot-ptr
405     dst class store-header
406     dst class store-tagged ;
407
408 : load-cards-offset ( dst -- )
409     0 swap LOAD32 rc-absolute-ppc-2/2 rel-cards-offset ;
410
411 : load-decks-offset ( dst -- )
412     0 swap LOAD32 rc-absolute-ppc-2/2 rel-decks-offset ;
413
414 :: (%write-barrier) ( temp1 temp2 -- )
415     card-mark scratch-reg LI
416
417     ! Mark the card
418     temp1 temp1 card-bits SRWI
419     temp2 load-cards-offset
420     temp1 scratch-reg temp2 STBX
421
422     ! Mark the card deck
423     temp1 temp1 deck-bits card-bits - SRWI
424     temp2 load-decks-offset
425     temp1 scratch-reg temp2 STBX ;
426
427 M:: ppc %write-barrier ( src slot temp1 temp2 -- )
428     temp1 src slot ADD
429     temp1 temp2 (%write-barrier) ;
430
431 M:: ppc %write-barrier-imm ( src slot temp1 temp2 -- )
432     temp1 src slot ADDI
433     temp1 temp2 (%write-barrier) ;
434
435 M:: ppc %check-nursery ( label size temp1 temp2 -- )
436     temp2 load-zone-ptr
437     temp1 temp2 0 LWZ
438     temp2 temp2 2 cells LWZ
439     temp1 temp1 size ADDI
440     ! is here >= end?
441     temp1 0 temp2 CMP
442     label BLE ;
443
444 M:: ppc %save-gc-root ( gc-root register -- )
445     register 1 gc-root gc-root@ STW ;
446
447 M:: ppc %load-gc-root ( gc-root register -- )
448     register 1 gc-root gc-root@ LWZ ;
449
450 M:: ppc %call-gc ( gc-root-count temp -- )
451     3 1 gc-root-base local@ ADDI
452     gc-root-count 4 LI
453     5 %load-vm-addr
454     "inline_gc" f %alien-invoke ;
455
456 M: ppc %prologue ( n -- )
457     0 11 LOAD32 rc-absolute-ppc-2/2 rel-this
458     0 MFLR
459     {
460         [ [ 1 1 ] dip neg ADDI ]
461         [ [ 11 1 ] dip xt-save STW ]
462         [ 11 LI ]
463         [ [ 11 1 ] dip next-save STW ]
464         [ [ 0 1 ] dip lr-save + STW ]
465     } cleave ;
466
467 M: ppc %epilogue ( n -- )
468     #! At the end of each word that calls a subroutine, we store
469     #! the previous link register value in r0 by popping it off
470     #! the stack, set the link register to the contents of r0,
471     #! and jump to the link register.
472     [ [ 0 1 ] dip lr-save + LWZ ]
473     [ [ 1 1 ] dip ADDI ] bi
474     0 MTLR ;
475
476 :: (%boolean) ( dst temp branch1 branch2 -- )
477     "end" define-label
478     dst \ f type-number %load-immediate
479     "end" get branch1 execute( label -- )
480     branch2 [ "end" get branch2 execute( label -- ) ] when
481     dst \ t %load-reference
482     "end" get resolve-label ; inline
483
484 :: %boolean ( dst cc temp -- )
485     cc negate-cc order-cc {
486         { cc<  [ dst temp \ BLT f (%boolean) ] }
487         { cc<= [ dst temp \ BLE f (%boolean) ] }
488         { cc>  [ dst temp \ BGT f (%boolean) ] }
489         { cc>= [ dst temp \ BGE f (%boolean) ] }
490         { cc=  [ dst temp \ BEQ f (%boolean) ] }
491         { cc/= [ dst temp \ BNE f (%boolean) ] }
492     } case ;
493
494 : (%compare) ( src1 src2 -- ) [ 0 ] dip CMP ; inline
495 : (%compare-imm) ( src1 src2 -- ) [ 0 ] 2dip CMPI ; inline
496 : (%compare-float-unordered) ( src1 src2 -- ) [ 0 ] dip FCMPU ; inline
497 : (%compare-float-ordered) ( src1 src2 -- ) [ 0 ] dip FCMPO ; inline
498
499 :: (%compare-float) ( src1 src2 cc compare -- branch1 branch2 )
500     cc {
501         { cc<    [ src1 src2 \ compare execute( a b -- ) \ BLT f     ] }
502         { cc<=   [ src1 src2 \ compare execute( a b -- ) \ BLT \ BEQ ] }
503         { cc>    [ src1 src2 \ compare execute( a b -- ) \ BGT f     ] }
504         { cc>=   [ src1 src2 \ compare execute( a b -- ) \ BGT \ BEQ ] }
505         { cc=    [ src1 src2 \ compare execute( a b -- ) \ BEQ f     ] }
506         { cc<>   [ src1 src2 \ compare execute( a b -- ) \ BLT \ BGT ] }
507         { cc<>=  [ src1 src2 \ compare execute( a b -- ) \ BNO f     ] }
508         { cc/<   [ src1 src2 \ compare execute( a b -- ) \ BGE f     ] }
509         { cc/<=  [ src1 src2 \ compare execute( a b -- ) \ BGT \ BO  ] }
510         { cc/>   [ src1 src2 \ compare execute( a b -- ) \ BLE f     ] }
511         { cc/>=  [ src1 src2 \ compare execute( a b -- ) \ BLT \ BO  ] }
512         { cc/=   [ src1 src2 \ compare execute( a b -- ) \ BNE f     ] }
513         { cc/<>  [ src1 src2 \ compare execute( a b -- ) \ BEQ \ BO  ] }
514         { cc/<>= [ src1 src2 \ compare execute( a b -- ) \ BO  f     ] }
515     } case ; inline
516
517 M: ppc %compare [ (%compare) ] 2dip %boolean ;
518
519 M: ppc %compare-imm [ (%compare-imm) ] 2dip %boolean ;
520
521 M:: ppc %compare-float-ordered ( dst src1 src2 cc temp -- )
522     src1 src2 cc negate-cc \ (%compare-float-ordered) (%compare-float) :> ( branch1 branch2 )
523     dst temp branch1 branch2 (%boolean) ;
524
525 M:: ppc %compare-float-unordered ( dst src1 src2 cc temp -- )
526     src1 src2 cc negate-cc \ (%compare-float-unordered) (%compare-float) :> ( branch1 branch2 )
527     dst temp branch1 branch2 (%boolean) ;
528
529 :: %branch ( label cc -- )
530     cc order-cc {
531         { cc<  [ label BLT ] }
532         { cc<= [ label BLE ] }
533         { cc>  [ label BGT ] }
534         { cc>= [ label BGE ] }
535         { cc=  [ label BEQ ] }
536         { cc/= [ label BNE ] }
537     } case ;
538
539 M:: ppc %compare-branch ( label src1 src2 cc -- )
540     src1 src2 (%compare)
541     label cc %branch ;
542
543 M:: ppc %compare-imm-branch ( label src1 src2 cc -- )
544     src1 src2 (%compare-imm)
545     label cc %branch ;
546
547 :: (%branch) ( label branch1 branch2 -- )
548     label branch1 execute( label -- )
549     branch2 [ label branch2 execute( label -- ) ] when ; inline
550
551 M:: ppc %compare-float-ordered-branch ( label src1 src2 cc -- )
552     src1 src2 cc \ (%compare-float-ordered) (%compare-float) :> ( branch1 branch2 )
553     label branch1 branch2 (%branch) ;
554
555 M:: ppc %compare-float-unordered-branch ( label src1 src2 cc -- )
556     src1 src2 cc \ (%compare-float-unordered) (%compare-float) :> ( branch1 branch2 )
557     label branch1 branch2 (%branch) ;
558
559 : load-from-frame ( dst n rep -- )
560     {
561         { int-rep [ [ 1 ] dip LWZ ] }
562         { float-rep [ [ 1 ] dip LFS ] }
563         { double-rep [ [ 1 ] dip LFD ] }
564         { stack-params [ [ 0 1 ] dip LWZ [ 0 1 ] dip param@ STW ] }
565     } case ;
566
567 : next-param@ ( n -- reg x )
568     [ 17 ] dip param@ ;
569
570 : store-to-frame ( src n rep -- )
571     {
572         { int-rep [ [ 1 ] dip STW ] }
573         { float-rep [ [ 1 ] dip STFS ] }
574         { double-rep [ [ 1 ] dip STFD ] }
575         { stack-params [ [ [ 0 ] dip next-param@ LWZ 0 1 ] dip STW ] }
576     } case ;
577
578 M: ppc %spill ( src rep dst -- )
579     swap [ n>> spill@ ] dip store-to-frame ;
580
581 M: ppc %reload ( dst rep src -- )
582     swap [ n>> spill@ ] dip load-from-frame ;
583
584 M: ppc %loop-entry ;
585
586 M: int-regs return-reg drop 3 ;
587 M: int-regs param-regs 2drop { 3 4 5 6 7 8 9 10 } ;
588 M: float-regs return-reg drop 1 ;
589
590 M:: ppc %save-param-reg ( stack reg rep -- )
591     reg stack local@ rep store-to-frame ;
592
593 M:: ppc %load-param-reg ( stack reg rep -- )
594     reg stack local@ rep load-from-frame ;
595
596 M: ppc %pop-stack ( n -- )
597     [ 3 ] dip <ds-loc> loc>operand LWZ ;
598
599 M: ppc %push-stack ( -- )
600     ds-reg ds-reg 4 ADDI
601     int-regs return-reg ds-reg 0 STW ;
602
603 M: ppc %push-context-stack ( -- )
604     11 %context
605     12 11 "datastack" context-field-offset LWZ
606     12 12 4 ADDI
607     12 11 "datastack" context-field-offset STW
608     int-regs return-reg 12 0 STW ;
609
610 M: ppc %pop-context-stack ( -- )
611     11 %context
612     12 11 "datastack" context-field-offset LWZ
613     int-regs return-reg 12 0 LWZ
614     12 12 4 SUBI
615     12 11 "datastack" context-field-offset STW ;
616
617 M: ppc %unbox ( n rep func -- )
618     ! Value must be in r3
619     4 %load-vm-addr
620     ! Call the unboxer
621     f %alien-invoke
622     ! Store the return value on the C stack
623     over [ [ reg-class-of return-reg ] keep %save-param-reg ] [ 2drop ] if ;
624
625 M: ppc %unbox-long-long ( n func -- )
626     4 %load-vm-addr
627     ! Call the unboxer
628     f %alien-invoke
629     ! Store the return value on the C stack
630     [
631         [ [ 3 1 ] dip local@ STW ]
632         [ [ 4 1 ] dip cell + local@ STW ] bi
633     ] when* ;
634
635 M: ppc %unbox-large-struct ( n c-type -- )
636     ! Value must be in r3
637     ! Compute destination address and load struct size
638     [ [ 4 1 ] dip local@ ADDI ] [ heap-size 5 LI ] bi*
639     6 %load-vm-addr
640     ! Call the function
641     "to_value_struct" f %alien-invoke ;
642
643 M:: ppc %box ( n rep func -- )
644     ! If the source is a stack location, load it into freg #0.
645     ! If the source is f, then we assume the value is already in
646     ! freg #0.
647     n [ 0 rep reg-class-of cdecl param-reg rep %load-param-reg ] when*
648     rep double-rep? 5 4 ? %load-vm-addr
649     func f %alien-invoke ;
650
651 M: ppc %box-long-long ( n func -- )
652     [
653         [
654             [ [ 3 1 ] dip local@ LWZ ]
655             [ [ 4 1 ] dip cell + local@ LWZ ] bi
656         ] when*
657         5 %load-vm-addr
658     ] dip f %alien-invoke ;
659
660 : struct-return@ ( n -- n )
661     [ stack-frame get params>> ] unless* local@ ;
662
663 M: ppc %prepare-box-struct ( -- )
664     #! Compute target address for value struct return
665     3 1 f struct-return@ ADDI
666     3 1 0 local@ STW ;
667
668 M: ppc %box-large-struct ( n c-type -- )
669     ! If n = f, then we're boxing a returned struct
670     ! Compute destination address and load struct size
671     [ [ 3 1 ] dip struct-return@ ADDI ] [ heap-size 4 LI ] bi*
672     5 %load-vm-addr
673     ! Call the function
674     "from_value_struct" f %alien-invoke ;
675
676 M:: ppc %restore-context ( temp1 temp2 -- )
677     temp1 %context
678     ds-reg temp1 "datastack" context-field-offset LWZ
679     rs-reg temp1 "retainstack" context-field-offset LWZ ;
680
681 M:: ppc %save-context ( temp1 temp2 -- )
682     temp1 %context
683     1 temp1 "callstack-top" context-field-offset STW
684     ds-reg temp1 "datastack" context-field-offset STW
685     rs-reg temp1 "retainstack" context-field-offset STW ;
686
687 M: ppc %alien-invoke ( symbol dll -- )
688     [ 11 ] 2dip %alien-global 11 MTLR BLRL ;
689
690 M: ppc %prepare-alien-indirect ( -- )
691     3 ds-reg 0 LWZ
692     ds-reg ds-reg 4 SUBI
693     4 %load-vm-addr
694     "pinned_alien_offset" f %alien-invoke
695     16 3 MR ;
696
697 M: ppc %alien-indirect ( -- )
698     16 MTLR BLRL ;
699
700 M: ppc immediate-arithmetic? ( n -- ? ) -32768 32767 between? ;
701
702 M: ppc immediate-bitwise? ( n -- ? ) 0 65535 between? ;
703
704 M: ppc struct-return-pointer-type void* ;
705
706 M: ppc return-struct-in-registers? ( c-type -- ? )
707     c-type return-in-registers?>> ;
708
709 M: ppc %box-small-struct ( c-type -- )
710     #! Box a <= 16-byte struct returned in r3:r4:r5:r6
711     heap-size 7 LI
712     8 %load-vm-addr
713     "from_medium_struct" f %alien-invoke ;
714
715 : %unbox-struct-1 ( -- )
716     ! Alien must be in r3.
717     4 %load-vm-addr
718     "alien_offset" f %alien-invoke
719     3 3 0 LWZ ;
720
721 : %unbox-struct-2 ( -- )
722     ! Alien must be in r3.
723     4 %load-vm-addr
724     "alien_offset" f %alien-invoke
725     4 3 4 LWZ
726     3 3 0 LWZ ;
727
728 : %unbox-struct-4 ( -- )
729     ! Alien must be in r3.
730     4 %load-vm-addr
731     "alien_offset" f %alien-invoke
732     6 3 12 LWZ
733     5 3 8 LWZ
734     4 3 4 LWZ
735     3 3 0 LWZ ;
736
737 M: ppc %begin-callback ( -- )
738     3 %load-vm-addr
739     "begin_callback" f %alien-invoke ;
740
741 M: ppc %alien-callback ( quot -- )
742     3 4 %restore-context
743     3 swap %load-reference
744     4 3 quot-entry-point-offset LWZ
745     4 MTLR
746     BLRL
747     3 4 %save-context ;
748
749 M: ppc %end-callback ( -- )
750     3 %load-vm-addr
751     "end_callback" f %alien-invoke ;
752
753 M: ppc %end-callback-value ( ctype -- )
754     ! Save top of data stack
755     16 ds-reg 0 LWZ
756     %end-callback
757     ! Restore top of data stack
758     3 16 MR
759     ! Unbox former top of data stack to return registers
760     unbox-return ;
761
762 M: ppc %unbox-small-struct ( size -- )
763     heap-size cell align cell /i {
764         { 1 [ %unbox-struct-1 ] }
765         { 2 [ %unbox-struct-2 ] }
766         { 4 [ %unbox-struct-4 ] }
767     } case ;
768
769 enable-float-functions
770
771 USE: vocabs.loader
772
773 {
774     { [ os macosx? ] [ "cpu.ppc.macosx" require ] }
775     { [ os linux? ] [ "cpu.ppc.linux" require ] }
776 } cond
777
778 complex-double c-type t >>return-in-registers? drop