* Removed all Id tags.
[cacao.git] / src / vm / jit / powerpc / asmpart.S
1 /* src/vm/jit/powerpc/asmpart.S - Java-C interface functions for PowerPC
2                 
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software.text;  you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation;  either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY ;  without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program;  if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include "md-abi.h"
31 #include "md-asm.h"
32
33 #include "vm/jit/abi-asm.h"
34 #include "vm/jit/methodheader.h"
35
36
37         .text
38
39         .align 2
40
41
42 /* export functions ***********************************************************/
43
44         .globl asm_vm_call_method
45         .globl asm_vm_call_method_int
46         .globl asm_vm_call_method_long
47         .globl asm_vm_call_method_float
48         .globl asm_vm_call_method_double
49
50         .globl asm_vm_call_method_exception_handler
51         .globl asm_vm_call_method_end
52
53         .globl asm_call_jit_compiler
54
55         .globl asm_handle_nat_exception
56         .globl asm_handle_exception
57
58         .globl asm_abstractmethoderror
59
60 #if defined(ENABLE_REPLACEMENT)
61         .globl asm_replacement_out
62         .globl asm_replacement_in
63 #endif
64
65         .globl asm_cacheflush
66
67         .globl asm_compare_and_swap
68         .globl asm_memory_barrier
69
70
71 /* asm_vm_call_method **********************************************************
72 *                                                                              *
73 *   This function calls a Java-method (which possibly needs compilation)       *
74 *   with up to 4 address parameters.                                           *
75 *                                                                              *
76 *   This functions calls the JIT-compiler which eventually translates the      *
77 *   method into machine code.                                                  *
78 *                                                                              *
79 *   C-prototype:                                                               *
80 *    javaobject_header *asm_calljavamethod (methodinfo *m,                     *
81 *         void *arg1, void *arg2, void *arg3, void *arg4);                     *
82 *                                                                              *
83 *******************************************************************************/
84
85         .align 2
86
87         .long   0                         /* catch type all                       */
88         .long   0                         /* exception handler pc                 */
89         .long   0                         /* end pc                               */
90         .long   0                         /* start pc                             */
91         .long   1                         /* extable size                         */
92         .long   0                         /* line number table start              */
93         .long   0                         /* line number table size               */
94         .long   0                         /* fltsave                              */
95         .long   0                         /* intsave                              */
96         .long   0                         /* IsLeaf                               */
97         .long   0                         /* IsSync                               */
98         .long   0                         /* frame size                           */
99         .long   0                         /* codeinfo pointer                     */
100
101 asm_vm_call_method:
102 asm_vm_call_method_int:
103 asm_vm_call_method_long:
104 asm_vm_call_method_float:
105 asm_vm_call_method_double:
106         mflr    r0
107         stw     r0,LA_LR_OFFSET(sp)
108         stwu    sp,-40*4(sp)              /* keep stack 16-byte aligned           */
109
110         stw     s0,8*4(sp)                /* save used callee saved registers     */
111         stw     a0,9*4(sp)                /* save method PV                       */
112
113 #if defined(__DARWIN__)
114         stw     itmp1,10*4(sp)            /* register r11 is callee saved         */
115 #endif
116         stw     pv,11*4(sp)               /* save PV register                     */
117
118         stw     itmp3,12*4(sp)            /* registers r14-r31 are callee saved   */
119         stfd    ftmp1,14*4(sp)            /* registers f14-f31 are callee saved   */
120         stfd    ftmp2,16*4(sp)
121
122 #if defined(__DARWIN__)
123         stw     t1,18*4(sp)
124         stw     t2,19*4(sp)
125         stw     t3,20*4(sp)
126         stw     t4,21*4(sp)
127         stw     t5,22*4(sp)
128         stw     t6,23*4(sp)
129         stw     t7,24*4(sp)
130
131         stfd    ft0,26*4(sp)
132         stfd    ft1,28*4(sp)
133         stfd    ft2,30*4(sp)
134         stfd    ft3,32*4(sp)
135         stfd    ft4,34*4(sp)
136         stfd    ft5,36*4(sp)
137 #else
138         SAVE_TEMPORARY_REGISTERS(18)        /* the offset has to be even          */
139 #endif
140
141         mr      t0,a1                       /* address of data structure          */
142         mr      t1,a2                       /* stack argument count               */
143
144         mr      s0,sp                       /* save SP                            */
145
146         lwz     a0,0*8+4(t0)                /* we are on big-endian               */
147         lwz     a1,1*8+4(t0)
148         lwz     a2,2*8+4(t0)
149         lwz     a3,3*8+4(t0)
150         lwz     a4,4*8+4(t0)
151         lwz     a5,5*8+4(t0)
152         lwz     a6,6*8+4(t0)
153         lwz     a7,7*8+4(t0)
154
155         lfd     fa0,8*8(t0)
156         lfd     fa1,9*8(t0)
157         lfd     fa2,10*8(t0)
158         lfd     fa3,11*8(t0)
159         lfd     fa4,12*8(t0)
160         lfd     fa5,13*8(t0)
161         lfd     fa6,14*8(t0)
162         lfd     fa7,15*8(t0)
163
164 #if defined(__DARWIN__)
165         lfd     fa8,16*8(t0)
166         lfd     fa9,17*8(t0)
167         lfd     fa10,18*8(t0)
168         lfd     fa11,19*8(t0)
169         lfd     fa12,20*8(t0)
170 #endif
171
172         mr.     t1,t1
173         beq     L_asm_vm_call_method_stack_copy_done
174
175         slwi    t2,t1,3                     /* calculate stackframe size (* 8)    */
176
177         sub     sp,sp,t2                    /* create stackframe                  */
178         mr      t2,sp                       /* temporary stack pointer            */
179
180 L_asm_vm_call_method_stack_copy_loop:
181 #if defined(__DARWIN__)
182         lwz     t3,21*8+0(t0)               /* load argument                      */
183         lwz     t4,21*8+4(t0)
184 #else
185         lwz     t3,16*8+0(t0)               /* load argument                      */
186         lwz     t4,16*8+4(t0)
187 #endif
188         stw     t3,0(t2)                    /* store argument on stack            */
189         stw     t4,4(t2)
190
191         addi    t0,t0,8                     /* load address of next argument      */
192         addi    t2,t2,8                     /* increase stack pointer             */
193         addi    t1,t1,-1                    /* subtract 1 argument                */
194         mr.     t1,t1
195         bgt     L_asm_vm_call_method_stack_copy_loop
196
197 L_asm_vm_call_method_stack_copy_done:
198         addi    mptr,s0,9*4                 /* get address of PV                  */
199         lwz     pv,0*4(mptr)                /* load PV                            */
200         mtctr   pv
201         bctrl
202 1:
203         mflr    itmp1
204 #if defined(__DARWIN__)
205         addi    pv,itmp1,lo16(asm_vm_call_method - 1b)
206 #else
207         addi    pv,itmp1,(asm_vm_call_method - 1b)@l
208 #endif
209
210 L_asm_vm_call_method_return:
211         mr      sp,s0                       /* restore the SP                     */
212
213         lwz     s0,8*4(sp)                  /* restore used callee saved registers*/
214
215 #if defined(__DARWIN__)
216         lwz     itmp1,10*4(sp)              /* register r11 is callee saved       */
217 #endif
218         lwz     pv,11*4(sp)                 /* save PV register                   */
219
220         lwz     itmp3,12*4(sp)
221         lfd     ftmp1,14*4(sp)              /* registers f14-f31 are callee saved */
222         lfd     ftmp2,16*4(sp)
223
224 #if defined(__DARWIN__)
225         lwz     t1,18*4(sp)
226         lwz     t2,19*4(sp)
227         lwz     t3,20*4(sp)
228         lwz     t4,21*4(sp)
229         lwz     t5,22*4(sp)
230         lwz     t6,23*4(sp)
231         lwz     t7,24*4(sp)
232
233         lfd     ft0,26*4(sp)
234         lfd     ft1,28*4(sp)
235         lfd     ft2,30*4(sp)
236         lfd     ft3,32*4(sp)
237         lfd     ft4,34*4(sp)
238         lfd     ft5,36*4(sp)
239 #else
240         RESTORE_TEMPORARY_REGISTERS(18)   /* the offset has to be even            */
241 #endif
242
243         lwz     r0,40*4+LA_LR_OFFSET(sp)
244         mtlr    r0
245         addi    sp,sp,40*4
246         blr
247
248 asm_vm_call_method_exception_handler:
249         mr      a0,itmp1
250         bl      builtin_throw_exception
251         b       L_asm_vm_call_method_return
252
253 asm_vm_call_method_end:
254         nop
255
256
257 /* asm_call_jit_compiler *******************************************************
258
259    Invokes the compiler for untranslated JavaVM methods.
260
261 *******************************************************************************/
262
263 asm_call_jit_compiler:
264 L_asm_call_jit_compiler:                /* required for PIC code              */
265         mflr    r0
266         stw     r0,LA_LR_OFFSET(sp)         /* save return address                */
267         stwu    sp,-(LA_SIZE + 4*4 + INT_ARG_CNT*4 + FLT_ARG_CNT*8)(sp)
268
269 #if defined(__DARWIN__)
270         stw     a0,LA_SIZE+(4+0)*4(sp)
271         stw     a1,LA_SIZE+(4+1)*4(sp)
272         stw     a2,LA_SIZE+(4+2)*4(sp)
273         stw     a3,LA_SIZE+(4+3)*4(sp)
274         stw     a4,LA_SIZE+(4+4)*4(sp)
275         stw     a5,LA_SIZE+(4+5)*4(sp)
276         stw     a6,LA_SIZE+(4+6)*4(sp)
277         stw     a7,LA_SIZE+(4+7)*4(sp)
278
279         stfd    fa0,LA_SIZE+(4+8)*4(sp)
280         stfd    fa1,LA_SIZE+(4+10)*4(sp)
281         stfd    fa2,LA_SIZE+(4+12)*4(sp)
282         stfd    fa3,LA_SIZE+(4+14)*4(sp)
283         stfd    fa4,LA_SIZE+(4+16)*4(sp)
284         stfd    fa5,LA_SIZE+(4+18)*4(sp)
285         stfd    fa6,LA_SIZE+(4+20)*4(sp)
286         stfd    fa7,LA_SIZE+(4+22)*4(sp)
287         stfd    fa8,LA_SIZE+(4+24)*4(sp)
288         stfd    fa9,LA_SIZE+(4+26)*4(sp)
289         stfd    fa10,LA_SIZE+(4+28)*4(sp)
290         stfd    fa11,LA_SIZE+(4+30)*4(sp)
291         stfd    fa12,LA_SIZE+(4+32)*4(sp)
292 #else
293         SAVE_ARGUMENT_REGISTERS(LA_SIZE_IN_POINTERS)
294 #endif
295
296         mr      a0,itmp1
297         mr      a1,mptr
298         addi    a2,sp,(LA_SIZE + 4*4 + INT_ARG_CNT*4 + FLT_ARG_CNT*8)
299         lwz     a3,(LA_SIZE + 4*4 + INT_ARG_CNT*4 + FLT_ARG_CNT*8)+LA_LR_OFFSET(sp)
300         bl      jit_asm_compile
301         mr      pv,v0                       /* move address to pv register        */
302
303 #if defined(__DARWIN__)
304         lwz     a0,LA_SIZE+(4+0)*4(sp)
305         lwz     a1,LA_SIZE+(4+1)*4(sp)
306         lwz     a2,LA_SIZE+(4+2)*4(sp)
307         lwz     a3,LA_SIZE+(4+3)*4(sp)
308         lwz     a4,LA_SIZE+(4+4)*4(sp)
309         lwz     a5,LA_SIZE+(4+5)*4(sp)
310         lwz     a6,LA_SIZE+(4+6)*4(sp)
311         lwz     a7,LA_SIZE+(4+7)*4(sp)
312
313         lfd     fa0,LA_SIZE+(4+8)*4(sp)
314         lfd     fa1,LA_SIZE+(4+10)*4(sp)
315         lfd     fa2,LA_SIZE+(4+12)*4(sp)
316         lfd     fa3,LA_SIZE+(4+14)*4(sp)
317         lfd     fa4,LA_SIZE+(4+16)*4(sp)
318         lfd     fa5,LA_SIZE+(4+18)*4(sp)
319         lfd     fa6,LA_SIZE+(4+20)*4(sp)
320         lfd     fa7,LA_SIZE+(4+22)*4(sp)
321         lfd     fa8,LA_SIZE+(4+24)*4(sp)
322         lfd     fa9,LA_SIZE+(4+26)*4(sp)
323         lfd     fa10,LA_SIZE+(4+28)*4(sp)
324         lfd     fa11,LA_SIZE+(4+30)*4(sp)
325         lfd     fa12,LA_SIZE+(4+32)*4(sp)
326 #else
327         RESTORE_ARGUMENT_REGISTERS(LA_SIZE_IN_POINTERS)
328 #endif
329
330         lwz     itmp1,(LA_SIZE + 4*4 + INT_ARG_CNT*4 + FLT_ARG_CNT*8)+LA_LR_OFFSET(sp)
331         mtlr    itmp1
332
333         addi    sp,sp,(LA_SIZE + 4*4 + INT_ARG_CNT*4 + FLT_ARG_CNT*8)
334
335         mr.     pv,pv                       /* test for exception                 */
336         beq     L_asm_call_jit_compiler_exception
337
338         mtctr   pv                          /* move method address to control reg */
339         bctr                                /* and call the Java method           */
340
341 L_asm_call_jit_compiler_exception:
342         mflr    r0
343         stw     r0,LA_LR_OFFSET(sp)
344         stwu    sp,-LA_SIZE_ALIGNED(sp)     /* preserve linkage area              */
345         bl      exceptions_get_and_clear_exception
346         lwz     xpc,LA_SIZE_ALIGNED+LA_LR_OFFSET(sp)
347         mtlr    xpc     
348         addi    sp,sp,LA_SIZE_ALIGNED
349
350         mr      xptr,v0                     /* get exception                      */
351         addi    xpc,xpc,-4                  /* exception address is ra - 4        */
352         b       L_asm_handle_nat_exception
353
354
355 /********************* function asm_handle_exception ***************************
356 *                                                                              *
357 *   This function handles an exception. It does not use the usual calling      *
358 *   conventions. The exception pointer is passed in REG_ITMP1 and the          *
359 *   pc from the exception raising position is passed in REG_ITMP2. It searches *
360 *   the local exception table for a handler. If no one is found, it unwinds    *
361 *   stacks and continues searching the callers.                                *
362 *                                                                              *
363 *   void asm_handle_exception (exceptionptr, exceptionpc);                     *
364 *                                                                              *
365 *******************************************************************************/
366                 
367 asm_handle_nat_exception:
368 L_asm_handle_nat_exception:             /* required for PIC code              */
369 L_asm_handle_exception_stack_loop:
370         mflr    r0
371         addi    sp,sp,-(LA_SIZE+((4+6)*4))  /* allocate stack (+4 for darwin)     */
372         stw     xptr,LA_SIZE+(4+0)*4(sp)    /* save exception pointer             */
373         stw     xpc,LA_SIZE+(4+1)*4(sp)     /* save exception pc                  */
374         stw     r0,LA_SIZE+(4+3)*4(sp)      /* save return address                */
375         li      itmp3,0
376         stw     itmp3,LA_SIZE+(4+4)*4(sp)   /* save maybe-leaf flag (cleared)     */
377
378         mr      a0,r0                       /* pass return address                */
379         bl      md_codegen_get_pv_from_pc   /* get PV from RA                     */
380         stw     v0,LA_SIZE+(4+2)*4(sp)      /* save data segment pointer          */
381
382         lwz     a0,LA_SIZE+(4+0)*4(sp)      /* pass xptr                          */
383         lwz     a1,LA_SIZE+(4+1)*4(sp)      /* pass xpc                           */
384         lwz     a2,LA_SIZE+(4+2)*4(sp)      /* pass PV (v0 == a0)                 */
385         addi    a3,sp,LA_SIZE+((4+6)*4)     /* pass Java SP                       */
386
387         b       L_asm_handle_exception_continue
388
389 asm_handle_exception:
390 L_asm_handle_exception:                 /* required for PIC code              */
391         addi    sp,sp,-(ARG_CNT+TMP_CNT)*8  /* create maybe-leaf stackframe       */
392
393 #if defined(__DARWIN__)
394 #else
395         SAVE_ARGUMENT_REGISTERS(0)          /* we save arg and temp registers in  */
396         SAVE_TEMPORARY_REGISTERS(ARG_CNT)   /* case this is a leaf method         */
397 #endif
398
399         addi    sp,sp,-(LA_SIZE+(4+6)*4)    /* allocate stack                     */
400         stw     xptr,LA_SIZE+(4+0)*4(sp)    /* save xptr                          */
401         stw     pv,LA_SIZE+(4+2)*4(sp)      /* save PV                            */
402         mflr    r0                          /* save RA                            */
403         stw     r0,LA_SIZE+(4+3)*4(sp)
404         li      t0,1                        /* set maybe-leaf flag                */
405         stw     t0,LA_SIZE+(4+4)*4(sp)      /* save maybe-leaf flag               */
406
407         mr      a0,xptr                     /* pass exception pointer             */
408         mr      a1,xpc                      /* pass exception pc                  */
409         mr      a2,pv                       /* pass data segment pointer          */
410         addi    a3,sp,LA_SIZE+(ARG_CNT+TMP_CNT)*8+(4+6)*4
411
412 L_asm_handle_exception_continue:
413         bl      exceptions_handle_exception
414
415         mr.     v0,v0
416         beq     L_asm_handle_exception_not_catched
417
418         mr      xpc,v0                      /* move handlerpc into xpc            */
419         lwz     xptr,LA_SIZE+(4+0)*4(sp)    /* restore xptr                       */
420         lwz     pv,LA_SIZE+(4+2)*4(sp)      /* restore PV                         */
421         lwz     r0,LA_SIZE+(4+3)*4(sp)      /* restore RA                         */
422         mtlr    r0
423         lwz     t0,LA_SIZE+(4+4)*4(sp)      /* get maybe-leaf flag                */
424         addi    sp,sp,LA_SIZE+(4+6)*4       /* free stack frame                   */
425
426         mr.     t0,t0
427         beq     L_asm_handle_exception_no_leaf
428
429 #if defined(__DARWIN__)
430 #else
431         RESTORE_ARGUMENT_REGISTERS(0)       /* if this is a leaf method, we have  */
432         RESTORE_TEMPORARY_REGISTERS(ARG_CNT)/* to restore arg and temp registers  */
433 #endif
434
435         addi    sp,sp,(ARG_CNT+TMP_CNT)*8   /* remove maybe-leaf stackframe       */
436
437 L_asm_handle_exception_no_leaf:
438         mtctr   xpc                         /* jump to the handler                */
439         bctr
440
441 L_asm_handle_exception_not_catched:
442         lwz     xptr,LA_SIZE+(4+0)*4(sp)    /* restore xptr                       */
443         lwz     pv,LA_SIZE+(4+2)*4(sp)      /* restore PV                         */
444         lwz     r0,LA_SIZE+(4+3)*4(sp)      /* restore RA                         */
445         mtlr    r0
446         lwz     t0,LA_SIZE+(4+4)*4(sp)      /* get maybe-leaf flag                */
447         addi    sp,sp,LA_SIZE+(4+6)*4       /* free stack frame                   */
448
449         mr.     t0,t0
450         beq     L_asm_handle_exception_no_leaf_stack
451
452         addi    sp,sp,(ARG_CNT+TMP_CNT)*8   /* remove maybe-leaf stackframe       */
453         li      t0,0                        /* clear the maybe-leaf flag          */
454
455 L_asm_handle_exception_no_leaf_stack:
456         lwz     t1,FrameSize(pv)            /* get frame size                     */
457         add     t1,sp,t1                    /* pointer to save area               */
458
459         lwz     t2,IsLeaf(pv)               /* is leaf procedure                  */
460         mr.     t2,t2
461         bne     L_asm_handle_exception_no_ra_restore
462
463         lwz     r0,LA_LR_OFFSET(t1)         /* restore ra                         */
464         mtlr    r0
465
466 L_asm_handle_exception_no_ra_restore:
467         mflr    xpc                         /* the new xpc is ra                  */
468         mr      t4,xpc                      /* save RA                            */
469         lwz     t2,IntSave(pv)              /* t2 = saved int register count      */
470         bl      ex_int1
471 ex_int1:
472         mflr    t3                          /* t3 = current pc                    */
473 #if defined(__DARWIN__)
474         addi    t3,t3,lo16(ex_int2-ex_int1)
475 #else
476         addi    t3,t3,(ex_int2-ex_int1)@l
477 #endif
478         slwi    t2,t2,2                     /* t2 = register count * 4            */
479         subf    t3,t2,t3                    /* t3 = IntSave - t2                  */
480         mtctr   t3
481         bctr
482
483         lwz     s0,-10*8(t1)
484         lwz     s1,-9*8(t1)
485         lwz     s2,-8*8(t1)
486         lwz     s3,-7*8(t1)
487         lwz     s4,-6*8(t1)
488         lwz     s5,-5*8(t1)
489         lwz     s6,-4*8(t1)
490         lwz     s7,-3*8(t1)
491         lwz     s8,-2*8(t1)
492         lwz     s9,-1*8(t1)
493
494 ex_int2:
495         subf    t1,t2,t1                    /* t1 = t1 - register count * 4       */
496
497         lwz     t2,FltSave(pv)
498         bl      ex_flt1
499 ex_flt1:
500         mflr    t3
501 #if defined(__DARWIN__)
502         addi    t3,t3,lo16(ex_flt2-ex_flt1)
503 #else
504         addi    t3,t3,(ex_flt2-ex_flt1)@l
505 #endif
506         slwi    t2,t2,2                     /* t2 = register count * 4            */
507         subf    t3,t2,t3                    /* t3 = FltSave - t2                  */
508         mtctr   t3
509         bctr
510
511         lfd     fs0,-10*8(t1)
512         lfd     fs1,-9*8(t1)
513         lfd     fs2,-8*8(t1)
514         lfd     fs3,-7*8(t1)
515         lfd     fs4,-6*8(t1)
516         lfd     fs5,-5*8(t1)
517         lfd     fs6,-4*8(t1)
518         lfd     fs7,-3*8(t1)
519         lfd     fs8,-2*8(t1)
520         lfd     fs9,-1*8(t1)
521
522 ex_flt2:
523         mtlr    t4                          /* restore RA                         */
524         lwz     t1,FrameSize(pv)            /* get frame size                     */
525         add     sp,sp,t1                    /* unwind stack                       */
526         b       L_asm_handle_exception_stack_loop
527
528
529 /* asm_abstractmethoderror *****************************************************
530
531    Creates and throws an AbstractMethodError.
532
533 *******************************************************************************/
534
535 asm_abstractmethoderror:
536         mflr    r0
537         stw     r0,LA_LR_OFFSET(sp)
538         stwu    sp,-LA_SIZE_ALIGNED(sp)     /* preserve linkage area              */
539         addi    a0,sp,LA_SIZE_ALIGNED       /* pass java sp                       */
540         mr      a1,r0                       /* pass exception address             */
541         bl      exceptions_asm_new_abstractmethoderror
542         lwz     r0,LA_SIZE_ALIGNED+LA_LR_OFFSET(sp)
543         mtlr    r0                          /* restore return address             */
544         addi    sp,sp,LA_SIZE_ALIGNED
545
546         mr      xptr,v0                     /* get exception pointer              */
547         mr      xpc,r0                      /* we can't use r0 directly in addi   */
548         addi    xpc,xpc,-4                  /* exception address is ra - 4        */
549         b       L_asm_handle_nat_exception
550
551
552 #if defined(ENABLE_REPLACEMENT)
553
554 /* asm_replacement_out *********************************************************
555
556    This code is jumped to from the replacement-out stubs that are executed
557    when a thread reaches an activated replacement point.
558
559    The purpose of asm_replacement_out is to read out the parts of the
560    execution state that cannot be accessed from C code, store this state,
561    and then call the C function replace_me.
562
563    Stack layout:
564       16                start of stack inside method to replace
565       0   rplpoint *    info on the replacement point that was reached
566
567    NOTE: itmp3 has been clobbered by the replacement-out stub!
568
569 *******************************************************************************/
570
571 /* some room to accomodate changes of the stack frame size during replacement */
572         /* XXX we should find a cleaner solution here */
573 #define REPLACEMENT_ROOM  512
574
575 #define sizeexecutionstate_ALIGNED  ((sizeexecutionstate + 15) & ~15)
576
577 asm_replacement_out:
578     /* create stack frame */
579         addi    sp,sp,-(sizeexecutionstate_ALIGNED + REPLACEMENT_ROOM)
580
581         /* save link register */
582         mflr    itmp3
583
584         /* save registers in execution state */
585         stw     r0 ,( 0*4+offes_intregs)(sp)
586         stw     r1 ,( 1*4+offes_intregs)(sp)
587         stw     r2 ,( 2*4+offes_intregs)(sp)
588         stw     r3 ,( 3*4+offes_intregs)(sp)
589         stw     r4 ,( 4*4+offes_intregs)(sp)
590         stw     r5 ,( 5*4+offes_intregs)(sp)
591         stw     r6 ,( 6*4+offes_intregs)(sp)
592         stw     r7 ,( 7*4+offes_intregs)(sp)
593         stw     r8 ,( 8*4+offes_intregs)(sp)
594         stw     r9 ,( 9*4+offes_intregs)(sp)
595         stw     r10,(10*4+offes_intregs)(sp)
596         stw     r11,(11*4+offes_intregs)(sp)
597         stw     r12,(12*4+offes_intregs)(sp)
598         stw     r13,(13*4+offes_intregs)(sp)
599         stw     r14,(14*4+offes_intregs)(sp)
600         stw     r15,(15*4+offes_intregs)(sp)
601         stw     r16,(16*4+offes_intregs)(sp) /* link register stored as itmp3 */
602         stw     r17,(17*4+offes_intregs)(sp)
603         stw     r18,(18*4+offes_intregs)(sp)
604         stw     r19,(19*4+offes_intregs)(sp)
605         stw     r20,(20*4+offes_intregs)(sp)
606         stw     r21,(21*4+offes_intregs)(sp)
607         stw     r22,(22*4+offes_intregs)(sp)
608         stw     r23,(23*4+offes_intregs)(sp)
609         stw     r24,(24*4+offes_intregs)(sp)
610         stw     r25,(25*4+offes_intregs)(sp)
611         stw     r26,(26*4+offes_intregs)(sp)
612         stw     r27,(27*4+offes_intregs)(sp)
613         stw     r28,(28*4+offes_intregs)(sp)
614         stw     r29,(29*4+offes_intregs)(sp)
615         stw     r30,(30*4+offes_intregs)(sp)
616         stw     r31,(31*4+offes_intregs)(sp)
617         
618         stfd    fr0 ,( 0*8+offes_fltregs)(sp)
619         stfd    fr1 ,( 1*8+offes_fltregs)(sp)
620         stfd    fr2 ,( 2*8+offes_fltregs)(sp)
621         stfd    fr3 ,( 3*8+offes_fltregs)(sp)
622         stfd    fr4 ,( 4*8+offes_fltregs)(sp)
623         stfd    fr5 ,( 5*8+offes_fltregs)(sp)
624         stfd    fr6 ,( 6*8+offes_fltregs)(sp)
625         stfd    fr7 ,( 7*8+offes_fltregs)(sp)
626         stfd    fr8 ,( 8*8+offes_fltregs)(sp)
627         stfd    fr9 ,( 9*8+offes_fltregs)(sp)
628         stfd    fr10,(10*8+offes_fltregs)(sp)
629         stfd    fr11,(11*8+offes_fltregs)(sp)
630         stfd    fr12,(12*8+offes_fltregs)(sp)
631         stfd    fr13,(13*8+offes_fltregs)(sp)
632         stfd    fr14,(14*8+offes_fltregs)(sp)
633         stfd    fr15,(15*8+offes_fltregs)(sp)
634         stfd    fr16,(16*8+offes_fltregs)(sp)
635         stfd    fr17,(17*8+offes_fltregs)(sp)
636         stfd    fr18,(18*8+offes_fltregs)(sp)
637         stfd    fr19,(19*8+offes_fltregs)(sp)
638         stfd    fr20,(20*8+offes_fltregs)(sp)
639         stfd    fr21,(21*8+offes_fltregs)(sp)
640         stfd    fr22,(22*8+offes_fltregs)(sp)
641         stfd    fr23,(23*8+offes_fltregs)(sp)
642         stfd    fr24,(24*8+offes_fltregs)(sp)
643         stfd    fr25,(25*8+offes_fltregs)(sp)
644         stfd    fr26,(26*8+offes_fltregs)(sp)
645         stfd    fr27,(27*8+offes_fltregs)(sp)
646         stfd    fr28,(28*8+offes_fltregs)(sp)
647         stfd    fr29,(29*8+offes_fltregs)(sp)
648         stfd    fr30,(30*8+offes_fltregs)(sp)
649         stfd    fr31,(31*8+offes_fltregs)(sp)
650         
651         /* calculate sp of method */
652         addi    itmp1,sp,(sizeexecutionstate_ALIGNED + REPLACEMENT_ROOM + 4*4)
653         stw     itmp1,(offes_sp)(sp)
654
655         /* store pv */
656         stw     pv,(offes_pv)(sp)
657
658         /* call replace_me */
659         lwz     a0,-(4*4)(itmp1)            /* arg0: rplpoint *                   */
660         mr      a1,sp                       /* arg1: execution state              */
661         addi    sp,sp,-(LA_SIZE_ALIGNED)
662         b       replace_me                  /* call C function replace_me         */
663
664 /* asm_replacement_in **********************************************************
665
666    This code writes the given execution state and jumps to the replacement
667    code.
668
669    This function never returns!
670
671    NOTE: itmp3 is not restored!
672
673    C prototype:
674       void asm_replacement_in(executionstate *es, replace_safestack_t *st);
675
676 *******************************************************************************/
677
678 asm_replacement_in:
679         /* a0 == executionstate *es      */
680         /* a1 == replace_safestack_t *st */
681
682         /* get arguments */
683         mr              s1,a1                       /* replace_safestack_t *st            */
684         mr              s2,a0                       /* executionstate *es == safe stack   */
685
686         /* switch to the safe stack */
687         mr              sp,s2
688
689         /* reserve linkage area */
690         addi    sp,sp,-(LA_SIZE_ALIGNED)
691
692         /* call replace_build_execution_state(st) */
693         mr              a0,s1
694         bl              replace_build_execution_state
695
696         /* set new sp */
697         lwz             sp,(offes_sp)(s2)
698
699         /* build stack frame */
700         addi    sp,sp,-(sizeexecutionstate_ALIGNED)
701
702         /* call replace_free_safestack(st,& of allocated executionstate_t) */
703         mr              a1,sp /* tmpes */
704         mr              a0,s1 /* st    */
705         addi    sp,sp,-(LA_SIZE_ALIGNED)  /* reserve linkage area */
706         bl              replace_free_safestack
707         addi    sp,sp,+(LA_SIZE_ALIGNED)  /* tear down linkage area */
708
709         /* set new pv */
710         lwz     pv,(offes_pv)(sp)
711         
712         /* copy registers from execution state */
713         lwz     r0 ,( 0*4+offes_intregs)(sp)
714         /* r1 is sp                       */
715         /* r2 is reserved                 */
716         lwz     a0 ,( 3*4+offes_intregs)(sp)
717         lwz     r4 ,( 4*4+offes_intregs)(sp)
718         lwz     r5 ,( 5*4+offes_intregs)(sp)
719         lwz     r6 ,( 6*4+offes_intregs)(sp)
720         lwz     r7 ,( 7*4+offes_intregs)(sp)
721         lwz     r8 ,( 8*4+offes_intregs)(sp)
722         lwz     r9 ,( 9*4+offes_intregs)(sp)
723         lwz     r10,(10*4+offes_intregs)(sp)
724         lwz     r11,(11*4+offes_intregs)(sp)
725         lwz     r12,(12*4+offes_intregs)(sp)
726         /* r13 is pv                      */
727         lwz     r14,(14*4+offes_intregs)(sp)
728         lwz     r15,(15*4+offes_intregs)(sp)
729         lwz     r16,(16*4+offes_intregs)(sp) /* itmp3, later to link register */
730         lwz     r17,(17*4+offes_intregs)(sp)
731         lwz     r18,(18*4+offes_intregs)(sp)
732         lwz     r19,(19*4+offes_intregs)(sp)
733         lwz     r20,(20*4+offes_intregs)(sp)
734         lwz     r21,(21*4+offes_intregs)(sp)
735         lwz     r22,(22*4+offes_intregs)(sp)
736         lwz     r23,(23*4+offes_intregs)(sp)
737         lwz     r24,(24*4+offes_intregs)(sp)
738         lwz     r25,(25*4+offes_intregs)(sp)
739         lwz     r26,(26*4+offes_intregs)(sp)
740         lwz     r27,(27*4+offes_intregs)(sp)
741         lwz     r28,(28*4+offes_intregs)(sp)
742         lwz     r29,(29*4+offes_intregs)(sp)
743         lwz     r30,(30*4+offes_intregs)(sp)
744         lwz     r31,(31*4+offes_intregs)(sp)
745         
746         lfd     fr0 ,( 0*8+offes_fltregs)(sp)
747         lfd     fr1 ,( 1*8+offes_fltregs)(sp)
748         lfd     fr2 ,( 2*8+offes_fltregs)(sp)
749         lfd     fr3 ,( 3*8+offes_fltregs)(sp)
750         lfd     fr4 ,( 4*8+offes_fltregs)(sp)
751         lfd     fr5 ,( 5*8+offes_fltregs)(sp)
752         lfd     fr6 ,( 6*8+offes_fltregs)(sp)
753         lfd     fr7 ,( 7*8+offes_fltregs)(sp)
754         lfd     fr8 ,( 8*8+offes_fltregs)(sp)
755         lfd     fr9 ,( 9*8+offes_fltregs)(sp)
756         lfd     fr10,(10*8+offes_fltregs)(sp)
757         lfd     fr11,(11*8+offes_fltregs)(sp)
758         lfd     fr12,(12*8+offes_fltregs)(sp)
759         lfd     fr13,(13*8+offes_fltregs)(sp)
760         lfd     fr14,(14*8+offes_fltregs)(sp)
761         lfd     fr15,(15*8+offes_fltregs)(sp)
762         lfd     fr16,(16*8+offes_fltregs)(sp)
763         lfd     fr17,(17*8+offes_fltregs)(sp)
764         lfd     fr18,(18*8+offes_fltregs)(sp)
765         lfd     fr19,(19*8+offes_fltregs)(sp)
766         lfd     fr20,(20*8+offes_fltregs)(sp)
767         lfd     fr21,(21*8+offes_fltregs)(sp)
768         lfd     fr22,(22*8+offes_fltregs)(sp)
769         lfd     fr23,(23*8+offes_fltregs)(sp)
770         lfd     fr24,(24*8+offes_fltregs)(sp)
771         lfd     fr25,(25*8+offes_fltregs)(sp)
772         lfd     fr26,(26*8+offes_fltregs)(sp)
773         lfd     fr27,(27*8+offes_fltregs)(sp)
774         lfd     fr28,(28*8+offes_fltregs)(sp)
775         lfd     fr29,(29*8+offes_fltregs)(sp)
776         lfd     fr30,(30*8+offes_fltregs)(sp)
777         lfd     fr31,(31*8+offes_fltregs)(sp)
778
779         /* restore link register */
780
781         mtlr    itmp3
782         
783         /* load new pc */
784
785         lwz     itmp3,offes_pc(sp)
786
787         /* remove stack frame */
788
789         addi    sp,sp,+(sizeexecutionstate_ALIGNED)
790
791         /* jump to new code */
792
793         mtctr   itmp3
794         bctr
795
796 #endif /* defined(ENABLE_REPLACEMENT) */
797
798 /*********************************************************************/
799
800 asm_cacheflush:
801         add     a1,a0,a1
802         rlwinm  a0,a0,0,0,26
803         addi    a1,a1,31
804         rlwinm  a1,a1,0,0,26
805         mr      a2,a0
806 1:
807         cmplw   a0,a1
808         bge     0f
809         dcbst   0,a0
810         addi    a0,a0,32
811         b       1b
812 0:
813         sync
814 1:
815         cmplw   a2,a1
816         bge     0f
817         icbi    0,a2
818         addi    a2,a2,32
819         b       1b
820 0:
821         sync
822         isync
823         blr
824
825
826 /* asm_compare_and_swap ********************************************************
827
828    XXX
829
830 *******************************************************************************/
831
832 asm_compare_and_swap:
833 1:
834         lwarx   a6,0,a0 
835         subf.   r0,a6,a1 
836         bne-    2f 
837         or      r0,a2,a2 
838         stwcx.  r0,0,a0 
839         bne-    1b 
840 2: 
841         mr      a0,a6
842         blr
843
844
845 /* asm_memory_barrier **********************************************************
846
847    XXX
848
849 *******************************************************************************/
850
851 asm_memory_barrier:
852         sync
853         blr
854
855
856 #if defined(__DARWIN__)
857
858 .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
859         .align 2
860 L_builtin_throw_exception$stub:
861         .indirect_symbol _builtin_throw_exception
862         mflr r0
863         bcl 20,31,L00$_builtin_throw_exception
864 L00$_builtin_throw_exception:
865         mflr r11
866         addis r11,r11,ha16(L_builtin_throw_exception$lazy_ptr - L00$_builtin_throw_exception)
867         mtlr r0
868         lwzu r12,lo16(L_builtin_throw_exception$lazy_ptr - L00$_builtin_throw_exception)(r11)
869         mtctr r12
870         bctr
871 .data
872 .lazy_symbol_pointer
873 L_builtin_throw_exception$lazy_ptr:
874         .indirect_symbol _builtin_throw_exception
875         .long dyld_stub_binding_helper
876
877
878 .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
879         .align 2
880 L_md_codegen_get_pv_from_pc$stub:
881         .indirect_symbol _md_codegen_get_pv_from_pc
882         mflr r0
883         bcl 20,31,L00$_md_codegen_get_pv_from_pc
884 L00$_md_codegen_get_pv_from_pc:
885         mflr r11
886         addis r11,r11,ha16(L_md_codegen_get_pv_from_pc$lazy_ptr - L00$_md_codegen_get_pv_from_pc)
887         mtlr r0
888         lwzu r12,lo16(L_md_codegen_get_pv_from_pc$lazy_ptr - L00$_md_codegen_get_pv_from_pc)(r11)
889         mtctr r12
890         bctr
891 .data
892 .lazy_symbol_pointer
893 L_md_codegen_get_pv_from_pc$lazy_ptr:
894         .indirect_symbol _md_codegen_get_pv_from_pc
895         .long dyld_stub_binding_helper
896
897
898 .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
899         .align 2
900 L_exceptions_handle_exception$stub:
901         .indirect_symbol _exceptions_handle_exception
902         mflr r0
903         bcl 20,31,L00$_exceptions_handle_exception
904 L00$_exceptions_handle_exception:
905         mflr r11
906         addis r11,r11,ha16(L_exceptions_handle_exception$lazy_ptr - L00$_exceptions_handle_exception)
907         mtlr r0
908         lwzu r12,lo16(L_exceptions_handle_exception$lazy_ptr - L00$_exceptions_handle_exception)(r11)
909         mtctr r12
910         bctr
911 .data
912 .lazy_symbol_pointer
913 L_exceptions_handle_exception$lazy_ptr:
914         .indirect_symbol _exceptions_handle_exception
915         .long dyld_stub_binding_helper
916
917
918 .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
919         .align 2
920 L_stacktrace_create_extern_stackframeinfo$stub:
921         .indirect_symbol _stacktrace_create_extern_stackframeinfo
922         mflr r0
923         bcl 20,31,L00$_stacktrace_create_extern_stackframeinfo
924 L00$_stacktrace_create_extern_stackframeinfo:
925         mflr r11
926         addis r11,r11,ha16(L_stacktrace_create_extern_stackframeinfo$lazy_ptr - L00$_stacktrace_create_extern_stackframeinfo)
927         mtlr r0
928         lwzu r12,lo16(L_stacktrace_create_extern_stackframeinfo$lazy_ptr - L00$_stacktrace_create_extern_stackframeinfo)(r11)
929         mtctr r12
930         bctr
931 .data
932 .lazy_symbol_pointer
933 L_stacktrace_create_extern_stackframeinfo$lazy_ptr:
934         .indirect_symbol _stacktrace_create_extern_stackframeinfo
935         .long dyld_stub_binding_helper
936
937
938 .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
939         .align 2
940 L_jit_asm_compile$stub:
941         .indirect_symbol _jit_asm_compile
942         mflr r0
943         bcl 20,31,L00$_jit_asm_compile
944 L00$_jit_asm_compile:
945         mflr r11
946         addis r11,r11,ha16(L_jit_asm_compile$lazy_ptr - L00$_jit_asm_compile)
947         mtlr r0
948         lwzu r12,lo16(L_jit_asm_compile$lazy_ptr - L00$_jit_asm_compile)(r11)
949         mtctr r12
950         bctr
951 .data
952 .lazy_symbol_pointer
953 L_jit_asm_compile$lazy_ptr:
954         .indirect_symbol _jit_asm_compile
955         .long dyld_stub_binding_helper
956
957
958 .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
959         .align 2
960 L_stacktrace_remove_stackframeinfo$stub:
961         .indirect_symbol _stacktrace_remove_stackframeinfo
962         mflr r0
963         bcl 20,31,L00$_stacktrace_remove_stackframeinfo
964 L00$_stacktrace_remove_stackframeinfo:
965         mflr r11
966         addis r11,r11,ha16(L_stacktrace_remove_stackframeinfo$lazy_ptr - L00$_stacktrace_remove_stackframeinfo)
967         mtlr r0
968         lwzu r12,lo16(L_stacktrace_remove_stackframeinfo$lazy_ptr - L00$_stacktrace_remove_stackframeinfo)(r11)
969         mtctr r12
970         bctr
971 .data
972 .lazy_symbol_pointer
973 L_stacktrace_remove_stackframeinfo$lazy_ptr:
974         .indirect_symbol _stacktrace_remove_stackframeinfo
975         .long dyld_stub_binding_helper
976
977
978 .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
979         .align 2
980 L_exceptions_get_and_clear_exception$stub:
981         .indirect_symbol _exceptions_get_and_clear_exception
982         mflr r0
983         bcl 20,31,L00$_exceptions_get_and_clear_exception
984 L00$_exceptions_get_and_clear_exception:
985         mflr r11
986         addis r11,r11,ha16(L_exceptions_get_and_clear_exception$lazy_ptr - L00$_exceptions_get_and_clear_exception)
987         mtlr r0
988         lwzu r12,lo16(L_exceptions_get_and_clear_exception$lazy_ptr - L00$_exceptions_get_and_clear_exception)(r11)
989         mtctr r12
990         bctr
991 .data
992 .lazy_symbol_pointer
993 L_exceptions_get_and_clear_exception$lazy_ptr:
994         .indirect_symbol _exceptions_get_and_clear_exception
995         .long dyld_stub_binding_helper
996
997
998 .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
999         .align 2
1000 L_exceptions_asm_new_abstractmethoderror$stub:
1001         .indirect_symbol _exceptions_asm_new_abstractmethoderror
1002         mflr r0
1003         bcl 20,31,L00$_exceptions_asm_new_abstractmethoderror
1004 L00$_exceptions_asm_new_abstractmethoderror:
1005         mflr r11
1006         addis r11,r11,ha16(L_exceptions_asm_new_abstractmethoderror$lazy_ptr - L00$_exceptions_asm_new_abstractmethoderror)
1007         mtlr r0
1008         lwzu r12,lo16(L_exceptions_asm_new_abstractmethoderror$lazy_ptr - L00$_exceptions_asm_new_abstractmethoderror)(r11)
1009         mtctr r12
1010         bctr
1011 .data
1012 .lazy_symbol_pointer
1013 L_exceptions_asm_new_abstractmethoderror$lazy_ptr:
1014         .indirect_symbol _exceptions_asm_new_abstractmethoderror
1015         .long dyld_stub_binding_helper
1016
1017
1018 # if defined(ENABLE_REPLACEMENT)
1019
1020 .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
1021         .align 2
1022 L_replace_me$stub:
1023         .indirect_symbol _replace_me
1024         mflr r0
1025         bcl 20,31,L00$_replace_me
1026 L00$_replace_me:
1027         mflr r11
1028         addis r11,r11,ha16(L_replace_me$lazy_ptr - L00$_replace_me)
1029         mtlr r0
1030         lwzu r12,lo16(L_replace_me$lazy_ptr - L00$_replace_me)(r11)
1031         mtctr r12
1032         bctr
1033 .data
1034 .lazy_symbol_pointer
1035 L_replace_me$lazy_ptr:
1036         .indirect_symbol _replace_me
1037         .long dyld_stub_binding_helper
1038
1039
1040 .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
1041         .align 2
1042 L_replace_build_execution_state$stub:
1043         .indirect_symbol _replace_build_execution_state
1044         mflr r0
1045         bcl 20,31,L00$_replace_build_execution_state
1046 L00$_replace_build_execution_state:
1047         mflr r11
1048         addis r11,r11,ha16(L_replace_build_execution_state$lazy_ptr - L00$_replace_build_execution_state)
1049         mtlr r0
1050         lwzu r12,lo16(L_replace_build_execution_state$lazy_ptr - L00$_replace_build_execution_state)(r11)
1051         mtctr r12
1052         bctr
1053 .data
1054 .lazy_symbol_pointer
1055 L_replace_build_execution_state$lazy_ptr:
1056         .indirect_symbol _replace_build_execution_state
1057         .long dyld_stub_binding_helper
1058
1059
1060 .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
1061         .align 2
1062 L_replace_free_safestack$stub:
1063         .indirect_symbol _replace_free_safestack
1064         mflr r0
1065         bcl 20,31,L00$_replace_free_safestack
1066 L00$_replace_free_safestack:
1067         mflr r11
1068         addis r11,r11,ha16(L_replace_free_safestack$lazy_ptr - L00$_replace_free_safestack)
1069         mtlr r0
1070         lwzu r12,lo16(L_replace_free_safestack$lazy_ptr - L00$_replace_free_safestack)(r11)
1071         mtctr r12
1072         bctr
1073 .data
1074 .lazy_symbol_pointer
1075 L_replace_free_safestack$lazy_ptr:
1076         .indirect_symbol _replace_free_safestack
1077         .long dyld_stub_binding_helper
1078
1079 # endif /* ENABLE_REPLACEMENT */
1080
1081 #endif /* defined(__DARWIN__) */
1082
1083
1084 /* disable exec-stacks ********************************************************/
1085
1086 #if defined(__linux__) && defined(__ELF__)
1087         .section .note.GNU-stack,"",%progbits
1088 #endif
1089
1090
1091 /*
1092  * These are local overrides for various environment variables in Emacs.
1093  * Please do not remove this and leave it at the end of the file, where
1094  * Emacs will automagically detect them.
1095  * ---------------------------------------------------------------------
1096  * Local variables:
1097  * mode: asm
1098  * indent-tabs-mode: t
1099  * c-basic-offset: 4
1100  * tab-width: 4
1101  * End:
1102  * vim:noexpandtab:sw=4:ts=4:
1103  */