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