]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/ppc/ppc.factor
Merge branch 'mongodb-changes' of git://github.com/x6j8x/factor
[factor.git] / basis / cpu / ppc / ppc.factor
1 ! Copyright (C) 2005, 2009 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.data literals cpu.architecture
6 cpu.ppc.assembler cpu.ppc.assembler.backend compiler.cfg.registers
7 compiler.cfg.instructions compiler.cfg.comparisons
8 compiler.codegen.fixup compiler.cfg.intrinsics
9 compiler.cfg.stack-frame compiler.cfg.build-stack-frame
10 compiler.units compiler.constants compiler.codegen vm ;
11 FROM: cpu.ppc.assembler => B ;
12 FROM: layouts => cell ;
13 FROM: math => float ;
14 IN: cpu.ppc
15
16 ! PowerPC register assignments:
17 ! r2-r12: integer vregs
18 ! r13: data stack
19 ! r14: retain stack
20 ! r15: VM pointer
21 ! r16-r29: integer vregs
22 ! r30: integer scratch
23 ! f0-f29: float vregs
24 ! f30: float scratch
25
26 ! Add some methods to the assembler that are useful to us
27 M: label (B) [ 0 ] 2dip (B) rc-relative-ppc-3 label-fixup ;
28 M: label BC [ 0 BC ] dip rc-relative-ppc-2 label-fixup ;
29
30 enable-float-intrinsics
31
32 <<
33 \ ##integer>float t frame-required? set-word-prop
34 \ ##float>integer t frame-required? set-word-prop
35 >>
36
37 M: ppc machine-registers
38     {
39         { int-regs $[ 2 12 [a,b] 16 29 [a,b] append ] }
40         { float-regs $[ 0 29 [a,b] ] }
41     } ;
42
43 CONSTANT: scratch-reg 30
44 CONSTANT: fp-scratch-reg 30
45
46 M: ppc %load-immediate ( reg n -- ) swap LOAD ;
47
48 M: ppc %load-reference ( reg obj -- )
49     [ 0 swap LOAD32 ] [ rc-absolute-ppc-2/2 rel-immediate ] bi* ;
50
51 M: ppc %alien-global ( register symbol dll -- )
52     [ 0 swap LOAD32 ] 2dip rc-absolute-ppc-2/2 rel-dlsym ;
53
54 CONSTANT: ds-reg 13
55 CONSTANT: rs-reg 14
56 CONSTANT: vm-reg 15
57
58 : %load-vm-addr ( reg -- ) vm-reg MR ;
59
60 : %load-vm-field-addr ( reg symbol -- )
61     [ vm-reg ] dip vm-field-offset ADDI ;
62
63 M: ppc %vm-field-ptr ( dst field -- ) %load-vm-field-addr ;
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 XT
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 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     "nursery" %load-vm-field-addr ;
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     2 1 stack-frame get total-size>> LWZ
569     [ 2 ] dip param@ ;
570
571 : store-to-frame ( src n rep -- )
572     {
573         { int-rep [ [ 1 ] dip STW ] }
574         { float-rep [ [ 1 ] dip STFS ] }
575         { double-rep [ [ 1 ] dip STFD ] }
576         { stack-params [ [ [ 0 ] dip next-param@ LWZ 0 1 ] dip STW ] }
577     } case ;
578
579 M: ppc %spill ( src rep dst -- )
580     swap [ n>> spill@ ] dip store-to-frame ;
581
582 M: ppc %reload ( dst rep src -- )
583     swap [ n>> spill@ ] dip load-from-frame ;
584
585 M: ppc %loop-entry ;
586
587 M: int-regs return-reg drop 3 ;
588 M: int-regs param-regs drop { 3 4 5 6 7 8 9 10 } ;
589 M: float-regs return-reg drop 1 ;
590
591 M:: ppc %save-param-reg ( stack reg rep -- )
592     reg stack local@ rep store-to-frame ;
593
594 M:: ppc %load-param-reg ( stack reg rep -- )
595     reg stack local@ rep load-from-frame ;
596
597 M: ppc %pop-stack ( n -- )
598     [ 3 ] dip <ds-loc> loc>operand LWZ ;
599
600 M: ppc %push-stack ( -- )
601     ds-reg ds-reg 4 ADDI
602     int-regs return-reg ds-reg 0 STW ;
603
604 :: %load-context-datastack ( dst -- )
605     ! Load context struct
606     dst "ctx" %vm-field-ptr
607     dst dst 0 LWZ
608     ! Load context datastack pointer
609     dst dst "datastack" context-field-offset ADDI ;
610
611 M: ppc %push-context-stack ( -- )
612     11 %load-context-datastack
613     12 11 0 LWZ
614     12 12 4 ADDI
615     12 11 0 STW
616     int-regs return-reg 12 0 STW ;
617
618 M: ppc %pop-context-stack ( -- )
619     11 %load-context-datastack
620     12 11 0 LWZ
621     int-regs return-reg 12 0 LWZ
622     12 12 4 SUBI
623     12 11 0 STW ;
624
625 M: ppc %unbox ( n rep func -- )
626     ! Value must be in r3
627     4 %load-vm-addr
628     ! Call the unboxer
629     f %alien-invoke
630     ! Store the return value on the C stack
631     over [ [ reg-class-of return-reg ] keep %save-param-reg ] [ 2drop ] if ;
632
633 M: ppc %unbox-long-long ( n func -- )
634     4 %load-vm-addr
635     ! Call the unboxer
636     f %alien-invoke
637     ! Store the return value on the C stack
638     [
639         [ [ 3 1 ] dip local@ STW ]
640         [ [ 4 1 ] dip cell + local@ STW ] bi
641     ] when* ;
642
643 M: ppc %unbox-large-struct ( n c-type -- )
644     ! Value must be in r3
645     ! Compute destination address and load struct size
646     [ [ 4 1 ] dip local@ ADDI ] [ heap-size 5 LI ] bi*
647     6 %load-vm-addr
648     ! Call the function
649     "to_value_struct" f %alien-invoke ;
650
651 M:: ppc %box ( n rep func -- )
652     ! If the source is a stack location, load it into freg #0.
653     ! If the source is f, then we assume the value is already in
654     ! freg #0.
655     n [ 0 rep reg-class-of param-reg rep %load-param-reg ] when*
656     rep double-rep? 5 4 ? %load-vm-addr
657     func f %alien-invoke ;
658
659 M: ppc %box-long-long ( n func -- )
660     [
661         [
662             [ [ 3 1 ] dip local@ LWZ ]
663             [ [ 4 1 ] dip cell + local@ LWZ ] bi
664         ] when*
665         5 %load-vm-addr
666     ] dip f %alien-invoke ;
667
668 : struct-return@ ( n -- n )
669     [ stack-frame get params>> ] unless* local@ ;
670
671 M: ppc %prepare-box-struct ( -- )
672     #! Compute target address for value struct return
673     3 1 f struct-return@ ADDI
674     3 1 0 local@ STW ;
675
676 M: ppc %box-large-struct ( n c-type -- )
677     ! If n = f, then we're boxing a returned struct
678     ! Compute destination address and load struct size
679     [ [ 3 1 ] dip struct-return@ ADDI ] [ heap-size 4 LI ] bi*
680     5 %load-vm-addr
681     ! Call the function
682     "from_value_struct" f %alien-invoke ;
683
684 M:: ppc %restore-context ( temp1 temp2 -- )
685     temp1 "ctx" %load-vm-field-addr
686     temp1 temp1 0 LWZ
687     temp2 1 stack-frame get total-size>> ADDI
688     temp2 temp1 "callstack-bottom" context-field-offset STW
689     ds-reg temp1 8 LWZ
690     rs-reg temp1 12 LWZ ;
691
692 M:: ppc %save-context ( temp1 temp2 -- )
693     temp1 "ctx" %load-vm-field-addr
694     temp1 temp1 0 LWZ
695     1 temp1 0 STW
696     ds-reg temp1 8 STW
697     rs-reg temp1 12 STW ;
698
699 M: ppc %alien-invoke ( symbol dll -- )
700     [ 11 ] 2dip %alien-global 11 MTLR BLRL ;
701
702 M: ppc %alien-callback ( quot -- )
703     3 4 %restore-context
704     3 swap %load-reference
705     4 3 quot-xt-offset LWZ
706     4 MTLR
707     BLRL
708     3 4 %save-context ;
709
710 M: ppc %prepare-alien-indirect ( -- )
711     3 ds-reg 0 LWZ
712     ds-reg ds-reg 4 SUBI
713     4 %load-vm-addr
714     "pinned_alien_offset" f %alien-invoke
715     16 3 MR ;
716
717 M: ppc %alien-indirect ( -- )
718     16 MTLR BLRL ;
719
720 M: ppc %callback-value ( ctype -- )
721     ! Save top of data stack
722     3 ds-reg 0 LWZ
723     3 1 0 local@ STW
724     3 %load-vm-addr
725     ! Restore data/call/retain stacks
726     "unnest_stacks" f %alien-invoke
727     ! Restore top of data stack
728     3 1 0 local@ LWZ
729     ! Unbox former top of data stack to return registers
730     unbox-return ;
731
732 M: ppc immediate-arithmetic? ( n -- ? ) -32768 32767 between? ;
733
734 M: ppc immediate-bitwise? ( n -- ? ) 0 65535 between? ;
735
736 M: ppc return-struct-in-registers? ( c-type -- ? )
737     c-type return-in-registers?>> ;
738
739 M: ppc %box-small-struct ( c-type -- )
740     #! Box a <= 16-byte struct returned in r3:r4:r5:r6
741     heap-size 7 LI
742     8 %load-vm-addr
743     "from_medium_struct" f %alien-invoke ;
744
745 : %unbox-struct-1 ( -- )
746     ! Alien must be in r3.
747     4 %load-vm-addr
748     "alien_offset" f %alien-invoke
749     3 3 0 LWZ ;
750
751 : %unbox-struct-2 ( -- )
752     ! Alien must be in r3.
753     4 %load-vm-addr
754     "alien_offset" f %alien-invoke
755     4 3 4 LWZ
756     3 3 0 LWZ ;
757
758 : %unbox-struct-4 ( -- )
759     ! Alien must be in r3.
760     4 %load-vm-addr
761     "alien_offset" f %alien-invoke
762     6 3 12 LWZ
763     5 3 8 LWZ
764     4 3 4 LWZ
765     3 3 0 LWZ ;
766
767 M: ppc %nest-stacks ( -- )
768     3 %load-vm-addr
769     "nest_stacks" f %alien-invoke ;
770
771 M: ppc %unnest-stacks ( -- )
772     3 %load-vm-addr
773     "unnest_stacks" f %alien-invoke ;
774
775 M: ppc %unbox-small-struct ( size -- )
776     heap-size cell align cell /i {
777         { 1 [ %unbox-struct-1 ] }
778         { 2 [ %unbox-struct-2 ] }
779         { 4 [ %unbox-struct-4 ] }
780     } case ;
781
782 enable-float-functions
783
784 USE: vocabs.loader
785
786 {
787     { [ os macosx? ] [ "cpu.ppc.macosx" require ] }
788     { [ os linux? ] [ "cpu.ppc.linux" require ] }
789 } cond
790
791 "complex-double" c-type t >>return-in-registers? drop