5f2cbc5371577a7a7dc991756a7aaf32e9af1c8f
[cacao.git] / src / vm / jit / mips / asmpart.S
1 /* src/vm/jit/mips/asmpart.S - Java-C interface functions for MIPS
2
3    Copyright (C) 1996-2005, 2006 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; 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    Contact: cacao@cacaojvm.org
26
27    Authors: Andreas Krall
28
29    Changes: Christian Thalinger
30                         Edwin Steiner
31
32    $Id: asmpart.S 4706 2006-03-30 09:20:42Z twisti $
33
34 */
35
36
37 #include "config.h"
38
39 #include "vm/jit/mips/md-abi.h"
40 #include "vm/jit/mips/md-asm.h"
41 #include "vm/jit/mips/offsets.h"
42
43 #include "vm/jit/abi.h"
44 #include "vm/jit/methodheader.h"
45
46
47         .text
48         .set    noat
49
50
51 /* export functions ***********************************************************/
52
53         .globl asm_vm_call_method
54         .globl asm_vm_call_method_int
55         .globl asm_vm_call_method_long
56         .globl asm_vm_call_method_float
57         .globl asm_vm_call_method_double
58
59         .globl asm_call_jit_compiler
60         .globl asm_handle_exception
61         .globl asm_handle_nat_exception
62
63         .globl asm_wrapper_patcher
64
65         .globl asm_replacement_out
66         .globl asm_replacement_in
67
68         .globl asm_perform_threadswitch
69         .globl asm_initialize_thread_stack
70         .globl asm_switchstackandcall
71         .globl asm_getclassvalues_atomic
72         .globl asm_criticalsections
73
74         .globl compare_and_swap
75
76
77 /********************* function asm_calljavafunction ***************************
78 *                                                                              *
79 *   This function calls a Java-method (which possibly needs compilation)       *
80 *   with up to 4 address parameters.                                           *
81 *                                                                              *
82 *   This functions calls the JIT-compiler which eventually translates the      *
83 *   method into machine code.                                                  *
84 *                                                                              *
85 *   A possibly throwed exception will be returned to the caller as function    *
86 *   return value, so the java method cannot return a fucntion value (this      *
87 *   function usually calls 'main' and '<clinit>' which do not return a         *
88 *   function value).                                                           *
89 *                                                                              *
90 *   C-prototype:                                                               *
91 *    javaobject_header *asm_calljavafunction (methodinfo *m,                   *
92 *         void *arg1, void *arg2, void *arg3, void *arg4);                     *
93 *                                                                              *
94 *******************************************************************************/
95
96         .ent    asm_vm_call_method
97
98         .align  3
99
100         .dword  0                           /* catch type all                     */
101         .dword  calljava_xhandler2          /* handler pc                         */
102         .dword  calljava_xhandler2          /* end pc                             */
103         .dword  asm_vm_call_method          /* start pc                           */
104         .word   1                           /* extable size                       */
105         .word   0                           /* 4-byte ALIGNMENT PADDING           */
106         .dword  0                           /* line number table start            */
107         .dword  0                           /* line number table size             */
108         .word   0                           /* 4-byte ALIGNMENT PADDING           */
109         .word   0                           /* fltsave                            */
110         .word   0                           /* intsave                            */
111         .word   0                           /* isleaf                             */
112         .word   0                           /* IsSync                             */
113         .word   0                           /* frame size                         */
114         .dword  0                           /* method pointer (pointer to name)   */
115
116 asm_vm_call_method:
117 asm_vm_call_method_int:
118 asm_vm_call_method_long:
119 asm_vm_call_method_float:
120 asm_vm_call_method_double:
121         .set    noreorder                 /* XXX we need to recompute pv          */
122
123         aaddiu  sp,sp,-12*8               /* allocate stack space (only 11 needed)*/
124         ast     ra,0(sp)                  /* save return address                  */
125
126         bal     L_asm_vm_call_method_compute_pv
127         ast     pv,1*8(sp)                /* procedure vector                     */
128 L_asm_vm_call_method_compute_pv:
129         aaddiu  pv,ra,-4*4
130         ast     s7,3*8(sp)
131
132         sdc1    fss0,5*8(sp)              /* save non JavaABI saved flt registers */
133         sdc1    fss1,6*8(sp)
134         sdc1    fss2,7*8(sp)
135         sdc1    fss3,8*8(sp)
136         sdc1    fss4,9*8(sp)
137         sdc1    fss5,10*8(sp)
138
139         ast     a0,4*8(sp)                /* save method pointer for compiler     */
140
141         move    t0,a2
142         move    s7,a1
143         blez    s7,calljava_argsloaded
144         nop
145
146         ald     a0,offvmargdata(t0)
147         ldc1    fa0,offvmargdata(t0)
148         daddi   s7,s7,-1
149         blez    s7,calljava_argsloaded
150         nop
151
152         ald     a1,offvmargdata+sizevmarg*1(t0)
153         ldc1    fa1,offvmargdata+sizevmarg*1(t0)
154         daddi   s7,s7,-1
155         blez    s7,calljava_argsloaded
156         nop
157
158         ald     a2,offvmargdata+sizevmarg*2(t0)
159         ldc1    fa2,offvmargdata+sizevmarg*2(t0)
160         daddi   s7,s7,-1
161         blez    s7,calljava_argsloaded
162         nop
163
164         ald     a3,offvmargdata+sizevmarg*3(t0)
165         ldc1    fa3,offvmargdata+sizevmarg*3(t0)
166         daddi   s7,s7,-1
167         blez    s7,calljava_argsloaded
168         nop
169
170         ald     a4,offvmargdata+sizevmarg*4(t0)
171         ldc1    fa4,offvmargdata+sizevmarg*4(t0)
172         daddi   s7,s7,-1
173         blez    s7,calljava_argsloaded
174         nop
175
176         ald     a5,offvmargdata+sizevmarg*5(t0)
177         ldc1    fa5,offvmargdata+sizevmarg*5(t0)
178         daddi   s7,s7,-1
179         blez    s7,calljava_argsloaded
180         nop
181
182         ald     a6,offvmargdata+sizevmarg*6(t0)
183         ldc1    fa6,offvmargdata+sizevmarg*6(t0)
184         daddi   s7,s7,-1
185         blez    s7,calljava_argsloaded
186         nop
187
188         ald     a7,offvmargdata+sizevmarg*7(t0)
189         ldc1    fa7,offvmargdata+sizevmarg*7(t0)
190         daddi   s7,s7,-1
191                 
192 calljava_argsloaded:
193         move    t8,sp                      /* save stack pointer                  */
194         blez    s7,calljava_nocopy
195         nop
196         subu    t1,zero,s7
197         sll     t2,t1,3
198         aaddu   sp,sp,t2
199         aaddu   t2,t2,t8
200
201 calljava_copyloop:
202     ald     t3,offvmargdata+sizevmarg*8(t0)
203         ast     t3,0(t2)
204         ala     t1,1(t1)
205         ala     t0,sizevmarg(t0)
206         ala     t2,8(t2)
207         bnez    t1,calljava_copyloop
208         nop
209
210 calljava_nocopy:
211         ald     itmp1,4*8(t8)             /* pass method pointer via itmp1        */
212
213         ala     mptr,asm_call_jit_compiler/* fake virtual function call (2 instr) */
214         ast     mptr,2*8(t8)              /* store function address               */
215         ala     mptr,1*8(t8)              /* set method pointer                   */
216
217         ald     pv,1*8(mptr)              /* method call as in Java               */
218         jalr    pv                        /* call JIT compiler                    */
219         nop
220 L_asm_vm_call_method_recompute_pv:
221 /*      aaddiu  pv,ra,(asm_vm_call_method - L_asm_vm_call_method_recompute_pv)*/
222         aaddiu  pv,ra,-76*4               /* recompute procedure vector           */
223
224         .set    reorder                   /* XXX we need to recompute pv          */
225
226 calljava_return2:
227         ald     ra,0(sp)                  /* restore return address               */
228         ald     pv,8(sp)                  /* restore procedure vector             */
229         ald     s7,3*8(sp)
230
231         ldc1    fss0,5*8(sp)              /* restore non JavaABI saved flt regs   */
232         ldc1    fss1,6*8(sp)
233         ldc1    fss2,7*8(sp)
234         ldc1    fss3,8*8(sp)
235         ldc1    fss4,9*8(sp)
236         ldc1    fss5,10*8(sp)
237
238         aaddiu  sp,sp,12*8                /* free stack space                     */
239         j       ra                        /* return                               */
240
241 calljava_xhandler2:
242         asll    s7,s7,3
243         aaddu   sp,s7,sp
244         move    a0,itmp1                  
245         jal     builtin_throw_exception
246         move    v0,zero                   /* clear return value for exception     */
247         b       calljava_return2
248
249         .end    asm_vm_call_method
250
251
252 /****************** function asm_call_jit_compiler *****************************
253 *                                                                              *
254 *   invokes the compiler for untranslated JavaVM methods.                      *
255 *                                                                              *
256 *   Register REG_ITEMP1 contains a pointer to the method info structure        *
257 *   (prepared by createcompilerstub). Using the return address in R31 and the  *
258 *   offset in the LDA instruction or using the value in methodptr R25 the      *
259 *   patching address for storing the method address can be computed:           *
260 *                                                                              *
261 *   method address was either loaded using                                     *
262 *   M_ALD (REG_PV, REG_PV, a)        ; invokestatic/special    ($28)           *
263 *   M_JSR (REG_RA, REG_PV);                                                    *
264 *   M_NOP                                                                      *
265 *   M_LDA (REG_PV, REG_RA, val)                                                *
266 *   or                                                                         *
267 *   M_ALD (REG_PV, REG_METHODPTR, m) ; invokevirtual/interface ($25)           *
268 *   M_JSR (REG_RA, REG_PV);                                                    *
269 *   M_NOP                                                                      *
270 *   in the static case the method pointer can be computed using the            *
271 *   return address and the lda function following the jmp instruction          *
272 *                                                                              *
273 *******************************************************************************/
274
275
276         .ent    asm_call_jit_compiler
277
278 asm_call_jit_compiler:
279         aaddiu  sp,sp,-(20*8+sizestackframeinfo) /* allocate stack space          */
280
281         SAVE_ARGUMENT_REGISTERS(0)
282
283         ast     mptr,16*8(sp)         /* save method pointer                      */
284         ast     ra,17*8(sp)           /* save return address                      */
285         ast     itmp1,18*8(sp)        /* save methodinfo pointer                  */
286
287         aaddiu  a0,sp,20*8            /* create stackframe info                   */
288         move    a1,zero               /* we don't have pv handy                   */
289         aaddiu  a2,sp,(20*8+sizestackframeinfo) /* pass java sp                   */
290         ald     a3,17*8(sp)           /* pass java ra                             */
291         move    a4,a3                 /* xpc is equal to ra                       */
292         jal     stacktrace_create_extern_stackframeinfo
293
294         ald     a0,18*8(sp)           /* pass methodinfo pointer                  */
295         jal     jit_compile           /* jit compiler                             */
296         ast     v0,18*8(sp)           /* save return value                        */
297
298         aaddiu  a0,sp,20*8            /* remove stackframe info                   */
299         jal     stacktrace_remove_stackframeinfo
300
301         ald     a0,17*8(sp)           /* pass return address                      */
302         aaddiu  a1,sp,20*8            /* pass stackframeinfo (for PV)             */
303         ald     a2,16*8(sp)           /* pass method pointer                      */
304         jal     md_assembler_get_patch_address /* get address of patch position   */
305         move    t0,v0                 /* move offset to t0 for later use          */
306
307         RESTORE_ARGUMENT_REGISTERS(0)
308
309         ald     ra,17*8(sp)           /* restore return address                   */
310         ald     v0,18*8(sp)           /* restore return value                     */
311         aaddiu  sp,sp,20*8+sizestackframeinfo /* deallocate stack area            */
312
313         beqz    v0,L_asm_call_jit_compiler_exception
314
315         ast     v0,0(t0)              /* store new method address                 */
316         move    pv,v0                 /* move method address into pv              */
317         jr      pv                    /* and call method. The method returns      */
318                                       /* directly to the caller (ra).             */
319
320 L_asm_call_jit_compiler_exception:
321 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
322         aaddiu  sp,sp,-2*8
323         ast     ra,0*8(sp)
324         jal     builtin_asm_get_exceptionptrptr
325         ald     ra,0*8(sp)
326         aaddiu  sp,sp,2*8
327 #else
328         la      v0,_exceptionptr
329 #endif
330         ald     xptr,0(v0)            /* get the exception pointer                */
331         ast     zero,0(v0)            /* clear the exception pointer              */
332
333         aaddiu  xpc,ra,-4             /* faulting address is return adress - 4    */
334         b       asm_handle_nat_exception
335
336         .end    asm_call_jit_compiler
337
338
339 /* asm_handle_exception ********************************************************
340
341    This function handles an exception. It does not use the usual calling
342    conventions. The exception pointer is passed in REG_ITMP1 and the
343    pc from the exception raising position is passed in REG_ITMP2. It searches
344    the local exception table for a handler. If no one is found, it unwinds
345    stacks and continues searching the callers.
346
347 *******************************************************************************/
348
349         .ent    asm_handle_nat_exception
350
351 asm_handle_nat_exception:
352 L_asm_handle_exception_stack_loop:
353         aaddiu  sp,sp,-6*8                  /* allocate stack                     */
354         ast     xptr,0*8(sp)                /* save exception pointer             */
355         ast     xpc,1*8(sp)                 /* save exception pc                  */
356         ast     ra,3*8(sp)                  /* save return address                */
357         ast     zero,4*8(sp)                /* save maybe-leaf flag (cleared)     */
358
359         move    a0,ra                       /* pass return address                */
360         jal     md_codegen_findmethod       /* get PV from RA                     */
361         ast     v0,2*8(sp)                  /* save data segment pointer          */
362
363         ald     a0,0*8(sp)                  /* pass exception pointer             */
364         ald     a1,1*8(sp)                  /* pass exception pc                  */
365         move    a2,v0                       /* pass data segment pointer          */
366         aaddiu  a3,sp,6*8                   /* pass Java stack pointer            */
367
368         b       L_asm_handle_exception_continue
369
370         .aent    asm_handle_exception
371
372 asm_handle_exception:
373         aaddiu  sp,sp,-(ARG_CNT+TMP_CNT)*8  /* create maybe-leaf stackframe       */
374
375         SAVE_ARGUMENT_REGISTERS(0)          /* we save arg and temp registers in  */
376         SAVE_TEMPORARY_REGISTERS(ARG_CNT)   /* case this is a leaf method         */
377
378         aaddiu  sp,sp,-6*8                  /* allocate stack                     */
379         ast     xptr,0*8(sp)                /* save exception pointer             */
380         ast     xpc,1*8(sp)                 /* save exception pc                  */
381         ast     pv,2*8(sp)                  /* save data segment pointer          */
382         ast     ra,3*8(sp)                  /* save return address                */
383         addu    t0,zero,1                   /* set maybe-leaf flag                */
384         ast     t0,4*8(sp)                  /* save maybe-leaf flag               */
385
386         move    a0,xptr                     /* pass exception pointer             */
387         move    a1,xpc                      /* pass exception pc                  */
388         move    a2,pv                       /* pass data segment pointer          */
389         aaddiu  a3,sp,(ARG_CNT+TMP_CNT+6)*8 /* pass Java stack pointer            */
390
391 L_asm_handle_exception_continue:
392         jal     exceptions_handle_exception
393         
394         beqz    v0,L_asm_handle_exception_not_catched
395
396         move    xpc,v0                      /* move handlerpc into xpc            */
397         ald     xptr,0*8(sp)                /* restore exception pointer          */
398         ald     pv,2*8(sp)                  /* restore data segment pointer       */
399         ald     ra,3*8(sp)                  /* restore return address             */
400         ald     t0,4*8(sp)                  /* get maybe-leaf flag                */
401         aaddiu  sp,sp,6*8                   /* free stackframe                    */
402         
403         beqz    t0,L_asm_handle_exception_no_leaf
404
405         RESTORE_ARGUMENT_REGISTERS(0)       /* if this is a leaf method, we have  */
406         RESTORE_TEMPORARY_REGISTERS(ARG_CNT)/* to restore arg and temp registers  */
407         
408         aaddiu  sp,sp,(ARG_CNT+TMP_CNT)*8   /* remove maybe-leaf stackframe       */
409
410 L_asm_handle_exception_no_leaf:
411         jr      xpc                         /* jump to the handler                */
412
413 L_asm_handle_exception_not_catched:
414         ald     xptr,0*8(sp)                /* restore exception pointer          */
415         ald     pv,2*8(sp)                  /* restore data segment pointer       */
416         ald     ra,3*8(sp)                  /* restore return address             */
417         ald     t0,4*8(sp)                  /* get maybe-leaf flag                */
418         aaddiu  sp,sp,6*8                   /* free stackframe                    */
419         
420         beqz    t0,L_asm_handle_exception_no_leaf_stack
421
422         aaddiu  sp,sp,(ARG_CNT+TMP_CNT)*8   /* remove maybe-leaf stackframe       */
423         move    t0,zero                     /* clear the maybe-leaf flag          */
424
425 L_asm_handle_exception_no_leaf_stack:
426         lw      t1,FrameSize(pv)            /* get frame size                     */
427         aaddu   t1,sp,t1                    /* pointer to save area               */
428
429         lw      t2,IsLeaf(pv)               /* is leaf procedure                  */
430         bnez    t2,L_asm_handle_exception_no_ra_restore
431
432         ald     ra,-1*8(t1)                 /* restore ra                         */
433         aaddiu  t1,t1,-8                    /* t1--                               */
434
435 L_asm_handle_exception_no_ra_restore:
436         move    xpc,ra                      /* the new xpc is ra                  */
437         lw      t2,IntSave(pv)              /* t1 = saved int register count      */
438         ala     t3,ex_int2                  /* t3 = current pc                    */
439         sll     t2,t2,2                     /* t2 = register count * 4            */
440         asubu   t3,t3,t2                    /* t3 = IntSave - 4 * register count  */
441         jr      t3                          /* jump to save position              */
442
443         ald     s0,-8*8(t1)
444         ald     s1,-7*8(t1)
445         ald     s2,-6*8(t1)
446         ald     s3,-5*8(t1)
447         ald     s4,-4*8(t1)
448         ald     s5,-3*8(t1)
449         ald     s6,-2*8(t1)
450         ald     s7,-1*8(t1)
451 ex_int2:
452         sll     t2,t2,1               /* t2 = register count * 4 * 2              */
453         asubu   t1,t1,t2              /* t1 = t0 - 8 * register count             */
454
455         lw      t2,FltSave(pv)        /* t2 = saved flt register count            */
456         ala     t3,ex_flt2            /* t3 = current pc                          */
457         sll     t2,t2,2               /* t2 = register count * 4                  */
458         asubu   t3,t3,t2              /* t3 = ex_int_sav - 4 * register count     */
459         jr      t3                          /* jump to save position              */
460
461         ldc1    fs0,-4*8(t1)
462         ldc1    fs1,-3*8(t1)
463         ldc1    fs2,-2*8(t1)
464         ldc1    fs3,-1*8(t1)
465
466 ex_flt2:
467         lw      t1,FrameSize(pv)            /* get frame size                     */
468         aaddu   sp,sp,t1                    /* unwind stack                       */
469         b       L_asm_handle_exception_stack_loop
470
471         .end    asm_handle_nat_exception
472
473
474 /* asm_wrapper_patcher *********************************************************
475
476    XXX
477
478    Stack layout:
479      40   return address into JIT code (patch position)
480      32   pointer to virtual java_objectheader
481      24   machine code (which is patched back later)
482      16   unresolved class/method/field reference
483       8   data segment displacement from load instructions
484       0   patcher function pointer to call
485
486 *******************************************************************************/
487                 
488     .ent    asm_wrapper_patcher
489
490 asm_wrapper_patcher:
491         aaddiu  sp,sp,-((2+16+22+4)*8+sizestackframeinfo) /* create stack frame   */
492
493         SAVE_RETURN_REGISTERS(0)      /* save 1 int/1 float return registers      */
494         SAVE_ARGUMENT_REGISTERS(2)    /* save 8 int/8 float argument registers    */
495         SAVE_TEMPORARY_REGISTERS(18)  /* save 5 int/16 float temporary registers  */
496
497         ast     itmp1,(2+16+22+0)*8(sp) /* save itmp1                             */
498         ast     itmp2,(2+16+22+1)*8(sp) /* save itmp2                             */
499         ast     ra,(2+16+22+2)*8(sp)  /* save method return address (for leafs)   */
500         ast     pv,(2+16+22+3)*8(sp)  /* save pv of calling java function         */
501
502         aaddiu  a0,sp,(2+16+22+4)*8   /* create stackframe info                   */
503         move    a1,pv                 /* pass java pv                             */
504         aaddiu  a2,sp,((6+2+16+22+4)*8+sizestackframeinfo) /* pass java sp        */
505         move    a3,ra                 /* this is correct for leafs                */
506         ald     a4,((5+2+16+22+4)*8+sizestackframeinfo)(sp) /* pass xpc           */
507         jal     stacktrace_create_extern_stackframeinfo
508
509         aaddiu  a0,sp,((0+2+16+22+4)*8+sizestackframeinfo) /* pass sp             */
510         ald     itmp3,((0+2+16+22+4)*8+sizestackframeinfo)(sp) /* get function    */
511         ald     itmp1,(2+16+22+3)*8(sp) /* save pv to the position of fp          */
512         ast     itmp1,((0+2+16+22+4)*8+sizestackframeinfo)(sp)
513         jalr    itmp3
514         ast     v0,((0+2+16+22+4)*8+sizestackframeinfo)(sp) /* save return value  */
515
516         aaddiu  a0,sp,(2+16+22+4)*8   /* remove stackframe info                   */
517         jal     stacktrace_remove_stackframeinfo
518
519         RESTORE_RETURN_REGISTERS(0)   /* restore 1 int/1 float return registers   */
520         RESTORE_ARGUMENT_REGISTERS(2) /* restore 8 int/8 float argument registers */
521         RESTORE_TEMPORARY_REGISTERS(18) /* restore 5 int/16 float temporary reg.  */
522
523         ald     itmp1,(2+16+22+0)*8(sp) /* restore itmp1                          */
524         ald     itmp2,(2+16+22+1)*8(sp) /* restore itmp2                          */
525         ald     ra,(2+16+22+2)*8(sp)  /* restore method return address (for leafs)*/
526         ald     pv,(2+16+22+3)*8(sp)  /* restore pv of calling java function      */
527
528         ald     itmp3,((0+2+16+22+4)*8+sizestackframeinfo)(sp) /* get return value*/
529         beqz    itmp3,L_asm_wrapper_patcher_exception
530
531         ald     itmp3,((5+2+16+22+4)*8+sizestackframeinfo)(sp) /* get RA to JIT   */
532         aaddiu  sp,sp,((6+2+16+22+4)*8+sizestackframeinfo) /* remove stack frame  */
533
534         jr      itmp3                 /* jump to new patched code                 */
535
536 L_asm_wrapper_patcher_exception:
537         ald     xpc,((5+2+16+22+4)*8+sizestackframeinfo)(sp) /* RA to JIT is xpc  */
538         aaddiu  sp,sp,((6+2+16+22+4)*8+sizestackframeinfo) /* remove stack frame  */
539
540 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
541         daddiu  sp,sp,-4*8
542         sd      xpc,0*8(sp)
543         sd      ra,1*8(sp)
544         sd      pv,2*8(sp)
545         jal     builtin_asm_get_exceptionptrptr
546         ld      xpc,0*8(sp)
547         ld      ra,1*8(sp)
548         ld      pv,2*8(sp)
549         daddiu  sp,sp,4*8
550 #else
551         la      v0,_exceptionptr
552 #endif
553         ld      xptr,0(v0)            /* get the exception pointer                */
554         sd      zero,0(v0)            /* clear the exception pointer              */
555         b       asm_handle_exception
556
557         .end    asm_wrapper_patcher
558
559                 
560 /* asm_replacement_out *********************************************************
561
562    This code is jumped to from the replacement-out stubs that are executed
563    when a thread reaches an activated replacement point.
564
565    The purpose of asm_replacement_out is to read out the parts of the
566    execution state that cannot be accessed from C code, store this state,
567    and then call the C function replace_me.
568
569    Stack layout:
570      16                 start of stack inside method to replace
571       0   rplpoint *    info on the replacement point that was reached
572
573    NOTE: itmp3 has been clobbered by the replacement-out stub!
574
575 *******************************************************************************/
576
577 /* some room to accomodate changes of the stack frame size during replacement */
578         /* XXX we should find a cleaner solution here */
579 #define REPLACEMENT_ROOM  512
580
581 #define REPLACEMENT_STACK_OFFSET ((sizeexecutionstate + REPLACEMENT_ROOM + 0xf) & ~0xf)
582
583         .ent asm_replacement_out
584
585 asm_replacement_out:
586     /* create stack frame */
587         daddiu  sp,sp,-REPLACEMENT_STACK_OFFSET
588
589         /* save registers in execution state */
590         sd      $0 ,( 0*8+offes_intregs)(sp)
591         sd      $1 ,( 1*8+offes_intregs)(sp)
592         sd      $2 ,( 2*8+offes_intregs)(sp)
593         sd      $3 ,( 3*8+offes_intregs)(sp)
594         sd      $4 ,( 4*8+offes_intregs)(sp)
595         sd      $5 ,( 5*8+offes_intregs)(sp)
596         sd      $6 ,( 6*8+offes_intregs)(sp)
597         sd      $7 ,( 7*8+offes_intregs)(sp)
598         sd      $8 ,( 8*8+offes_intregs)(sp)
599         sd      $9 ,( 9*8+offes_intregs)(sp)
600         sd      $10,(10*8+offes_intregs)(sp)
601         sd      $11,(11*8+offes_intregs)(sp)
602         sd      $12,(12*8+offes_intregs)(sp)
603         sd      $13,(13*8+offes_intregs)(sp)
604         sd      $14,(14*8+offes_intregs)(sp)
605         sd      $15,(15*8+offes_intregs)(sp)
606         sd      $16,(16*8+offes_intregs)(sp)
607         sd      $17,(17*8+offes_intregs)(sp)
608         sd      $18,(18*8+offes_intregs)(sp)
609         sd      $19,(19*8+offes_intregs)(sp)
610         sd      $20,(20*8+offes_intregs)(sp)
611         sd      $21,(21*8+offes_intregs)(sp)
612         sd      $22,(22*8+offes_intregs)(sp)
613         sd      $23,(23*8+offes_intregs)(sp)
614         sd      $24,(24*8+offes_intregs)(sp)
615         sd      $25,(25*8+offes_intregs)(sp)
616         sd      $26,(26*8+offes_intregs)(sp)
617         sd      $27,(27*8+offes_intregs)(sp)
618         sd      $28,(28*8+offes_intregs)(sp)
619         sd      $29,(29*8+offes_intregs)(sp)
620         sd      $30,(30*8+offes_intregs)(sp)
621         sd      $31,(31*8+offes_intregs)(sp)
622         
623         sdc1    $f0 ,( 0*8+offes_fltregs)(sp)
624         sdc1    $f1 ,( 1*8+offes_fltregs)(sp)
625         sdc1    $f2 ,( 2*8+offes_fltregs)(sp)
626         sdc1    $f3 ,( 3*8+offes_fltregs)(sp)
627         sdc1    $f4 ,( 4*8+offes_fltregs)(sp)
628         sdc1    $f5 ,( 5*8+offes_fltregs)(sp)
629         sdc1    $f6 ,( 6*8+offes_fltregs)(sp)
630         sdc1    $f7 ,( 7*8+offes_fltregs)(sp)
631         sdc1    $f8 ,( 8*8+offes_fltregs)(sp)
632         sdc1    $f9 ,( 9*8+offes_fltregs)(sp)
633         sdc1    $f10,(10*8+offes_fltregs)(sp)
634         sdc1    $f11,(11*8+offes_fltregs)(sp)
635         sdc1    $f12,(12*8+offes_fltregs)(sp)
636         sdc1    $f13,(13*8+offes_fltregs)(sp)
637         sdc1    $f14,(14*8+offes_fltregs)(sp)
638         sdc1    $f15,(15*8+offes_fltregs)(sp)
639         sdc1    $f16,(16*8+offes_fltregs)(sp)
640         sdc1    $f17,(17*8+offes_fltregs)(sp)
641         sdc1    $f18,(18*8+offes_fltregs)(sp)
642         sdc1    $f19,(19*8+offes_fltregs)(sp)
643         sdc1    $f20,(20*8+offes_fltregs)(sp)
644         sdc1    $f21,(21*8+offes_fltregs)(sp)
645         sdc1    $f22,(22*8+offes_fltregs)(sp)
646         sdc1    $f23,(23*8+offes_fltregs)(sp)
647         sdc1    $f24,(24*8+offes_fltregs)(sp)
648         sdc1    $f25,(25*8+offes_fltregs)(sp)
649         sdc1    $f26,(26*8+offes_fltregs)(sp)
650         sdc1    $f27,(27*8+offes_fltregs)(sp)
651         sdc1    $f28,(28*8+offes_fltregs)(sp)
652         sdc1    $f29,(29*8+offes_fltregs)(sp)
653         sdc1    $f30,(30*8+offes_fltregs)(sp)
654         sdc1    $f31,(31*8+offes_fltregs)(sp)
655         
656         /* calculate sp of method */
657         daddiu  itmp1,sp,(REPLACEMENT_STACK_OFFSET + 2*8)
658         sd      itmp1,(offes_sp)(sp)
659
660         /* store pv */
661         sd      pv,(offes_pv)(sp)
662
663         /* call replace_me */
664         ld      a0,-(2*8)(itmp1)            /* arg0: rplpoint *                   */
665     move    a1,sp                       /* arg1: execution state              */
666     jal     replace_me                  /* call C function replace_me         */
667         jal     abort                       /* NEVER REACHED                      */
668
669         .end asm_replacement_out
670
671 /* asm_replacement_in **********************************************************
672
673    This code writes the given execution state and jumps to the replacement
674    code.
675
676    This function never returns!
677
678    NOTE: itmp3 is not restored!
679
680    C prototype:
681       void asm_replacement_in(executionstate *es);
682
683 *******************************************************************************/
684
685         .ent asm_replacement_in
686         
687 asm_replacement_in:
688         /* a0 == executionstate *es */
689
690         /* set new sp and pv */
691         ld      sp,(offes_sp)(a0)
692         ld      pv,(offes_pv)(a0)
693         
694         /* copy registers from execution state */
695         /* $0 is zero                     */
696         ld      $1 ,( 1*8+offes_intregs)(a0)
697         ld      $2 ,( 2*8+offes_intregs)(a0)
698         ld      $3 ,( 2*8+offes_intregs)(a0)
699         /* a0 is loaded below             */
700         ld      $5 ,( 5*8+offes_intregs)(a0)
701         ld      $6 ,( 6*8+offes_intregs)(a0)
702         ld      $7 ,( 7*8+offes_intregs)(a0)
703         ld      $8 ,( 8*8+offes_intregs)(a0)
704         ld      $9 ,( 9*8+offes_intregs)(a0)
705         ld      $10,(10*8+offes_intregs)(a0)
706         ld      $11,(11*8+offes_intregs)(a0)
707         ld      $12,(12*8+offes_intregs)(a0)
708         ld      $13,(13*8+offes_intregs)(a0)
709         ld      $14,(14*8+offes_intregs)(a0)
710         ld      $15,(15*8+offes_intregs)(a0)
711         ld      $16,(16*8+offes_intregs)(a0)
712         ld      $17,(17*8+offes_intregs)(a0)
713         ld      $18,(18*8+offes_intregs)(a0)
714         ld      $19,(19*8+offes_intregs)(a0)
715         ld      $20,(20*8+offes_intregs)(a0)
716         ld      $21,(21*8+offes_intregs)(a0)
717         ld      $22,(22*8+offes_intregs)(a0)
718         ld      $23,(23*8+offes_intregs)(a0)
719         ld      $24,(24*8+offes_intregs)(a0)
720         ld      $25,(25*8+offes_intregs)(a0)
721         ld      $26,(26*8+offes_intregs)(a0)
722         ld      $27,(27*8+offes_intregs)(a0)
723         ld      $28,(28*8+offes_intregs)(a0)
724         /* $29 is sp                      */
725         /* $30 is pv                      */
726         ld      $31,(31*8+offes_intregs)(a0)
727         
728         ldc1    $f0 ,( 0*8+offes_fltregs)(a0)
729         ldc1    $f1 ,( 1*8+offes_fltregs)(a0)
730         ldc1    $f2 ,( 2*8+offes_fltregs)(a0)
731         ldc1    $f3 ,( 3*8+offes_fltregs)(a0)
732         ldc1    $f4 ,( 4*8+offes_fltregs)(a0)
733         ldc1    $f5 ,( 5*8+offes_fltregs)(a0)
734         ldc1    $f6 ,( 6*8+offes_fltregs)(a0)
735         ldc1    $f7 ,( 7*8+offes_fltregs)(a0)
736         ldc1    $f8 ,( 8*8+offes_fltregs)(a0)
737         ldc1    $f9 ,( 9*8+offes_fltregs)(a0)
738         ldc1    $f10,(10*8+offes_fltregs)(a0)
739         ldc1    $f11,(11*8+offes_fltregs)(a0)
740         ldc1    $f12,(12*8+offes_fltregs)(a0)
741         ldc1    $f13,(13*8+offes_fltregs)(a0)
742         ldc1    $f14,(14*8+offes_fltregs)(a0)
743         ldc1    $f15,(15*8+offes_fltregs)(a0)
744         ldc1    $f16,(16*8+offes_fltregs)(a0)
745         ldc1    $f17,(17*8+offes_fltregs)(a0)
746         ldc1    $f18,(18*8+offes_fltregs)(a0)
747         ldc1    $f19,(19*8+offes_fltregs)(a0)
748         ldc1    $f20,(20*8+offes_fltregs)(a0)
749         ldc1    $f21,(21*8+offes_fltregs)(a0)
750         ldc1    $f22,(22*8+offes_fltregs)(a0)
751         ldc1    $f23,(23*8+offes_fltregs)(a0)
752         ldc1    $f24,(24*8+offes_fltregs)(a0)
753         ldc1    $f25,(25*8+offes_fltregs)(a0)
754         ldc1    $f26,(26*8+offes_fltregs)(a0)
755         ldc1    $f27,(27*8+offes_fltregs)(a0)
756         ldc1    $f28,(28*8+offes_fltregs)(a0)
757         ldc1    $f29,(29*8+offes_fltregs)(a0)
758         ldc1    $f30,(30*8+offes_fltregs)(a0)
759         ldc1    $f31,(31*8+offes_fltregs)(a0)
760
761         /* load new pc */
762
763         ld      itmp3,offes_pc(a0)
764
765         /* load a0 */
766         
767         ld      a0,(4*8+offes_intregs)(a0)
768
769         /* jump to new code */
770
771         jr      itmp3
772
773         .end asm_replacement_in
774
775 /******************* function asm_initialize_thread_stack **********************
776 *                                                                              *
777 *   u1* asm_initialize_thread_stack (void *func, u1 *stack);                   *
778 *                                                                              *
779 *   initialize a thread stack                                                  *
780 *                                                                              *
781 *******************************************************************************/
782
783         .ent    asm_initialize_thread_stack
784
785 asm_initialize_thread_stack:
786         aaddiu  a1,a1,-14*8     /* allocate save area                             */
787         sd      zero, 0*8(a1)   /* s0 initalize thread area                       */
788         sd      zero, 1*8(a1)   /* s1                                             */
789         sd      zero, 2*8(a1)   /* s2                                             */
790         sd      zero, 3*8(a1)   /* s3                                             */
791         sd      zero, 4*8(a1)   /* s4                                             */
792         sd      zero, 5*8(a1)   /* s5                                             */
793         sd      zero, 6*8(a1)   /* s6                                             */
794         sd      zero, 7*8(a1)   /* s7                                             */
795         sd      zero, 8*8(a1)   /* s8                                             */
796         sd      zero, 9*8(a1)   /* fs0                                            */
797         sd      zero,10*8(a1)   /* fs1                                            */
798         sd      zero,11*8(a1)   /* fs2                                            */
799         sd      zero,12*8(a1)   /* fs3                                            */
800         sd      a0, 13*8(a1)
801         move    v0,a1
802         j       ra              /* return                                         */
803
804         .end    asm_initialize_thread_stack
805
806
807 /******************* function asm_perform_threadswitch *************************
808 *                                                                              *
809 *   void asm_perform_threadswitch (u1 **from, u1 **to, u1 **stackTop);         *
810 *                                                                              *
811 *   performs a threadswitch                                                    *
812 *                                                                              *
813 *******************************************************************************/
814
815         .ent    asm_perform_threadswitch
816
817 asm_perform_threadswitch:
818         aaddiu  sp,sp,-14*8     /* allocate new stack                             */
819         sd      s0,  0*8(sp)    /* save saved registers of old thread             */
820         sd      s1,  1*8(sp)
821         sd      s2,  2*8(sp)
822         sd      s3,  3*8(sp)
823         sd      s4,  4*8(sp)
824         sd      s5,  5*8(sp)
825         sd      s6,  6*8(sp)
826         sd      s7,  7*8(sp)
827         sd      s8,  8*8(sp)
828         sdc1    fs0, 9*8(sp)
829         sdc1    fs1,10*8(sp)
830         sdc1    fs2,11*8(sp)
831         sdc1    fs3,12*8(sp)
832         sd      ra, 13*8(sp)
833         ast     sp,0(a0)        /* save old stack pointer                         */
834         ast     sp,0(a2)        /* stackTop = old stack pointer                   */
835         ald     sp,0(a1)        /* load new stack pointer                         */
836         ld      s0,  0*8(sp)    /* load saved registers of new thread             */
837         ld      s1,  1*8(sp)
838         ld      s2,  2*8(sp)
839         ld      s3,  3*8(sp)
840         ld      s4,  4*8(sp)
841         ld      s5,  5*8(sp)
842         ld      s6,  6*8(sp)
843         ld      s7,  7*8(sp)
844         ld      s8,  8*8(sp)
845         ldc1    fs0, 9*8(sp)
846         ldc1    fs1,10*8(sp)
847         ldc1    fs2,11*8(sp)
848         ldc1    fs3,12*8(sp)
849         ld      ra, 13*8(sp)
850         aaddiu  sp,sp,14*8      /* deallocate new stack                           */
851         move    itmp3, ra
852         j       ra              /* return                                         */
853
854         .end    asm_perform_threadswitch
855
856
857 /********************* function asm_switchstackandcall *************************
858 *                                                                              *
859 *  void asm_switchstackandcall (void *stack, void *func, void **stacktopsave); *
860 *                                                                              *
861 *   Switches to a new stack, calls a function and switches back.               *
862 *       a0      new stack pointer                                              *
863 *       a1      function pointer                                               *
864 *               a2              pointer to variable where stack top should be stored           *
865 *                                                                              *
866 *******************************************************************************/
867
868         .ent    asm_switchstackandcall
869
870 asm_switchstackandcall:
871         aaddiu  a0,a0,-16       /* allocate new stack                             */
872         sd      ra,0(a0)        /* save return address on new stack               */
873         sd      sp,8(a0)        /* save old stack pointer on new stack            */
874         sd      sp,0(a2)        /* save old stack pointer to variable             */
875         move    sp,a0           /* switch to new stack                            */
876         
877         move    itmp3,a1
878         move    a0,a3
879         jalr    itmp3           /* and call function                              */
880
881         ld      ra,0(sp)        /* load return address                            */
882         ld      sp,8(sp)        /* switch to old stack                            */
883
884         j       ra              /* return                                         */
885
886         .end    asm_switchstackandcall
887
888
889         .ent    asm_getclassvalues_atomic
890
891 asm_getclassvalues_atomic:
892 _crit_restart:
893 _crit_begin:
894         lw      t0,offbaseval(a0)
895         lw      t1,offdiffval(a0)
896         lw      t2,offbaseval(a1)
897 _crit_end:
898         sw      t0,offcast_super_baseval(a2)
899         sw      t1,offcast_super_diffval(a2)
900         sw      t2,offcast_sub_baseval(a2)
901         j       ra
902
903         .end    asm_getclassvalues_atomic
904
905     .data
906
907 asm_criticalsections:
908 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
909     .dword  _crit_begin
910     .dword  _crit_end
911     .dword  _crit_restart
912 #endif
913     .dword  0
914
915
916         .text
917
918         .ent    compare_and_swap
919
920 compare_and_swap:
921 1:
922         all     v0,0(a0)
923         bne     v0,a1,2f
924         move    t0,a2
925         asc     t0,0(a0)
926         beqz    t0,1b
927 2:
928         sync
929         j       ra
930
931         .end    compare_and_swap
932
933
934 /* Disable exec-stacks, required for Gentoo ***********************************/
935
936 #if defined(__GCC__) && defined(__ELF__)
937         .section .note.GNU-stack,"",@progbits
938 #endif
939
940
941 /*
942  * These are local overrides for various environment variables in Emacs.
943  * Please do not remove this and leave it at the end of the file, where
944  * Emacs will automagically detect them.
945  * ---------------------------------------------------------------------
946  * Local variables:
947  * mode: asm
948  * indent-tabs-mode: t
949  * c-basic-offset: 4
950  * tab-width: 4
951  * End:
952  * vim:noexpandtab:sw=4:ts=4:
953  */