Use montiorenter/exit only with threads.
[cacao.git] / src / vm / jit / mips / asmpart.S
1 /* vm/jit/mips/asmpart.S - Java-C interface functions for mips
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Andreas Krall
28
29    $Id: asmpart.S 1814 2004-12-22 12:37:58Z twisti $
30
31 */
32
33
34 #include "config.h"
35 #include "vm/jit/mips/offsets.h"
36 #include "vm/jit/mips/asmoffsets.h"
37
38
39 #define zero    $0
40 #define itmp1   $1
41 #define v0      $2
42 #define itmp2   $3
43 #define a0      $4
44 #define a1      $5
45 #define a2      $6
46 #define a3      $7
47
48 #define a4      $8
49 #define a5      $9
50 #define a6      $10
51 #define a7      $11
52 #define t0      $12
53 #define t1      $13
54 #define t2      $14
55 #define t3      $15
56
57 #define s0      $16
58 #define s1      $17
59 #define s2      $18
60 #define s3      $19
61 #define s4      $20
62 #define s5      $21
63 #define s6      $22
64 #define s7      $23
65
66 #define t8      $24
67 #define itmp3   $25
68 #define k0      $26
69 #define k1      $27
70
71 #define gp      $28
72 #define sp      $29
73 #define s8      $30
74 #define ra      $31
75
76 #define pv      s8
77 #define t9      itmp3
78
79 #define xptr    itmp1
80 #define xpc     itmp2
81 #define mptr    itmp3
82 #define mptrreg 25
83
84 #define fv0     $f0
85 #define ft0     $f1
86 #define ft1     $f2
87 #define ft2     $f3
88 #define ft3     $f4
89 #define ft4     $f5
90 #define ft5     $f6
91 #define ft6     $f7
92
93 #define ft7     $f8
94 #define ft8     $f9
95 #define ft9     $f10
96 #define ft10    $f11
97 #define fa0     $f12
98 #define fa1     $f13
99 #define fa2     $f14
100 #define fa3     $f15
101
102 #define fa4     $f16
103 #define fa5     $f17
104 #define fa6     $f18
105 #define fa7     $f19
106 #define ft11    $f20
107 #define ft12    $f21
108 #define ft13    $f22
109 #define ft14    $f23
110
111 #define fs0     $f24
112 #define ft15    $f25
113 #define fs1     $f26
114 #define ft16    $f27
115 #define fs2     $f28
116 #define ft17    $f29
117 #define fs3     $f30
118 #define ft18    $f31
119
120 #define fss0    $f20
121 #define fss1    $f22
122 #define fss2    $f25
123 #define fss3    $f27
124 #define fss4    $f29
125 #define fss5    $f31
126
127 #define aaddu   daddu
128 #define asubu   dsubu
129 #define aaddiu  daddiu
130 #define ald     ld
131 #define ast     sd
132 #define ala     dla
133 #define asll    dsll
134 #define ashift  3
135
136
137         .text
138         .set    noat
139
140
141 /********************* exported functions and variables ***********************/
142
143         .globl asm_calljavafunction
144         .globl asm_calljavafunction2
145         .globl asm_calljavafunction2long
146         .globl asm_calljavafunction2double
147         .globl asm_call_jit_compiler
148         .globl asm_dumpregistersandcall
149         .globl asm_handle_exception
150         .globl asm_handle_nat_exception
151         .globl asm_check_clinit
152         .globl asm_builtin_checkarraycast
153         .globl asm_builtin_checkcast
154         .globl asm_builtin_aastore
155
156 #if defined(USE_THREADS)
157         .globl asm_builtin_monitorenter
158         .globl asm_builtin_monitorexit
159 #endif
160
161         .globl asm_builtin_idiv
162         .globl asm_builtin_irem
163         .globl asm_builtin_ldiv
164         .globl asm_builtin_lrem
165         .globl asm_perform_threadswitch
166         .globl asm_initialize_thread_stack
167         .globl asm_switchstackandcall
168         .globl asm_builtin_trace
169         .globl asm_builtin_exittrace
170         .globl asm_getclassvalues_atomic
171         .globl asm_criticalsections
172
173         .globl compare_and_swap
174
175
176 /********************* function asm_calljavafunction ***************************
177 *                                                                              *
178 *   This function calls a Java-method (which possibly needs compilation)       *
179 *   with up to 4 address parameters.                                           *
180 *                                                                              *
181 *   This functions calls the JIT-compiler which eventually translates the      *
182 *   method into machine code.                                                  *
183 *                                                                              *
184 *   A possibly throwed exception will be returned to the caller as function    *
185 *   return value, so the java method cannot return a fucntion value (this      *
186 *   function usually calls 'main' and '<clinit>' which do not return a         *
187 *   function value).                                                           *
188 *                                                                              *
189 *   C-prototype:                                                               *
190 *    javaobject_header *asm_calljavafunction (methodinfo *m,                   *
191 *         void *arg1, void *arg2, void *arg3, void *arg4);                     *
192 *                                                                              *
193 *******************************************************************************/
194
195         .ent    asm_calljavafunction
196
197 call_name:
198         .align  3
199
200         .dword  0                         /* catch type all                       */
201         .dword  calljava_xhandler         /* handler pc                           */
202         .dword  calljava_xhandler         /* end pc                               */
203         .dword  asm_calljavafunction      /* start pc                             */
204         .word   1                         /* extable size                         */
205         .word   0                         /* fltsave                              */
206         .word   0                         /* intsave                              */
207         .word   0                         /* isleaf                               */
208         .word   0                         /* IsSync                               */
209         .word   10*8                      /* frame size                           */
210         .dword  0                         /* method pointer (pointer to name)     */
211
212 asm_calljavafunction:
213         aaddiu  sp,sp,-10*8               /* allocate stack space                 */
214         sd      ra,0(sp)                  /* save return address                  */
215
216         .set    noreorder
217         bal     call_java_pc
218         sd      pv,3*8(sp)                /* procedure vector                     */
219 call_java_pc:
220         aaddiu  pv,ra,-4*4
221
222         .set    reorder
223         
224         sdc1    fss0,4*8(sp)              /* save non JavaABI saved flt registers */
225         sdc1    fss1,5*8(sp)
226         sdc1    fss2,6*8(sp)
227         sdc1    fss3,7*8(sp)
228         sdc1    fss4,8*8(sp)
229         sdc1    fss5,9*8(sp)
230         sd      a0,2*8(sp)                /* save method pointer for compiler     */
231         aaddiu  itmp1,sp,16               /* pass pointer to methodptr via itmp1  */
232
233         move    a0,a1                     /* pass the remaining parameters        */
234         move    a1,a2
235         move    a2,a3
236         move    a3,a4
237
238         ala     mptr,asm_call_jit_compiler/* fake virtual function call (2 instr) */
239         ast     mptr,1*8(sp)              /* store function address               */
240         move    mptr,sp                   /* set method pointer                   */
241
242         .set    noreorder
243         
244         ald     pv,1*8(mptr)              /* method call as in Java               */
245         jalr    pv                        /* call JIT compiler                    */
246         nop
247         aaddiu  pv,ra,-23*4               /* recompute procedure vector           */
248 #if 0
249         move    v0,zero                   /* clear return value (exception ptr)   */
250 #else
251         nop
252 #endif
253
254 calljava_return:
255         ld      ra,0(sp)                  /* restore return address               */
256         ld      pv,3*8(sp)                /* restore procedure vector             */
257
258         ldc1    fss0,4*8(sp)              /* restore non JavaABI saved flt regs   */
259         ldc1    fss1,5*8(sp)
260         ldc1    fss2,6*8(sp)
261         ldc1    fss3,7*8(sp)
262         ldc1    fss4,8*8(sp)
263         ldc1    fss5,9*8(sp)
264         j       ra                        /* return                               */
265         aaddiu  sp,sp,10*8                /* free stack space                     */
266
267         .set    reorder
268         
269 calljava_xhandler:
270         move    a0,itmp1                  
271         jal     builtin_throw_exception
272         b       calljava_return
273
274         .end    asm_calljavafunction
275
276
277         .ent    asm_calljavafunction2
278
279 call_name2:
280         .align  3
281
282         .dword  0                         /* catch type all                       */
283         .dword  calljava_xhandler2        /* handler pc                           */
284         .dword  calljava_xhandler2        /* end pc                               */
285         .dword  asm_calljavafunction2     /* start pc                             */
286         .word   1                         /* extable size                         */
287         .word   0                         /* fltsave                              */
288         .word   1                         /* intsave                              */
289         .word   0                         /* isleaf                               */
290         .word   0                         /* IsSync                               */
291         .word   12*8                      /* frame size                           */
292         .dword  0                         /* method pointer (pointer to name)     */
293
294 asm_calljavafunction2:
295 asm_calljavafunction2double:
296 asm_calljavafunction2long:
297         aaddiu  sp,sp,-12*8               /* allocate stack space (only 11 needed)*/
298         sd      ra,0(sp)                  /* save return address                  */
299
300         .set    noreorder
301         bal     call_java_pc2
302         sd      pv,1*8(sp)                /* procedure vector                     */
303 call_java_pc2:
304         aaddiu  pv,ra,-4*4
305         sd      s7,3*8(sp)
306
307         .set    reorder
308         
309         sdc1    fss0,5*8(sp)              /* save non JavaABI saved flt registers */
310         sdc1    fss1,6*8(sp)
311         sdc1    fss2,7*8(sp)
312         sdc1    fss3,8*8(sp)
313         sdc1    fss4,9*8(sp)
314         sdc1    fss5,10*8(sp)
315         sd      a0,4*8(sp)                /* save method pointer for compiler     */
316         move    t0,a3
317         move    s7,a1
318
319         blez    s7,calljava_argsloaded
320         ald     a0,offjniitem(t0)
321         ldc1    fa0,offjniitem(t0)
322         daddi   s7,s7,-1
323         blez    s7,calljava_argsloaded
324
325         ald     a1,offjniitem+sizejniblock*1(t0)
326         ldc1    fa1,offjniitem+sizejniblock*1(t0)
327         daddi   s7,s7,-1
328         blez    s7,calljava_argsloaded
329
330         ald     a2,offjniitem+sizejniblock*2(t0)
331         ldc1    fa2,offjniitem+sizejniblock*2(t0)
332         daddi   s7,s7,-1
333         blez    s7,calljava_argsloaded
334
335         ald     a3,offjniitem+sizejniblock*3(t0)
336         ldc1    fa3,offjniitem+sizejniblock*3(t0)
337         daddi   s7,s7,-1
338         blez    s7,calljava_argsloaded
339
340         ald     a4,offjniitem+sizejniblock*4(t0)
341         ldc1    fa4,offjniitem+sizejniblock*4(t0)
342         daddi   s7,s7,-1
343         blez    s7,calljava_argsloaded
344
345         ald     a5,offjniitem+sizejniblock*5(t0)
346         ldc1    fa5,offjniitem+sizejniblock*5(t0)
347         daddi   s7,s7,-1
348         blez    s7,calljava_argsloaded
349
350         ald     a6,offjniitem+sizejniblock*6(t0)
351         ldc1    fa6,offjniitem+sizejniblock*6(t0)
352         daddi   s7,s7,-1
353         blez    s7,calljava_argsloaded
354
355         ald     a7,offjniitem+sizejniblock*7(t0)
356         ldc1    fa7,offjniitem+sizejniblock*7(t0)
357         daddi   s7,s7,-1
358                 
359 calljava_argsloaded:
360     move    t8,sp
361         blez    s7,calljava_nocopy
362         subu    t1,zero,s7
363         sll     t2,t1,3
364         daddu   sp,sp,t2
365         daddu   t2,t2,t8
366
367 calljava_copyloop:
368     ald     t3,offjniitem+sizejniblock*8(t0)
369         ast     t3,0(t2)
370         ala     t1,1(t1)
371         ala     t0,sizejniblock(t0)
372         ala     t2,8(t2)
373         bnez    t1,calljava_copyloop
374
375 calljava_nocopy:
376         ala     itmp1,32(t8)              /* pass pointer to methodptr via itmp1  */
377
378         ala     mptr,asm_call_jit_compiler/* fake virtual function call (2 instr) */
379         ast     mptr,16(sp)               /* store function address               */
380         ala     mptr,8(t8)                /* set method pointer                   */
381
382         .set    noreorder
383         
384         ald     pv,8(mptr)                /* method call as in Java               */
385         jalr    pv                        /* call JIT compiler                    */
386         nop
387         aaddiu  pv,ra,-76*4               /* recompute procedure vector           */
388
389 calljava_return2:
390         ld      ra,0(sp)                  /* restore return address               */
391         ld      pv,8(sp)                  /* restore procedure vector             */
392         ld      s7,24(sp)
393
394         ldc1    fss0,5*8(sp)              /* restore non JavaABI saved flt regs   */
395         ldc1    fss1,6*8(sp)
396         ldc1    fss2,7*8(sp)
397         ldc1    fss3,8*8(sp)
398         ldc1    fss4,9*8(sp)
399         ldc1    fss5,10*8(sp)
400         j       ra                        /* return                               */
401         aaddiu  sp,sp,12*8                /* free stack space                     */
402
403         .set    reorder
404         
405 calljava_xhandler2:
406     sll     s7,s7,3
407         aaddu   sp,s7,sp
408         move    a0,itmp1                  
409         jal     builtin_throw_exception
410         b       calljava_return2
411
412         .end    asm_calljavafunction2
413
414
415 /****************** function asm_call_jit_compiler *****************************
416 *                                                                              *
417 *   invokes the compiler for untranslated JavaVM methods.                      *
418 *                                                                              *
419 *   Register REG_ITEMP1 contains a pointer to the method info structure        *
420 *   (prepared by createcompilerstub). Using the return address in R31 and the  *
421 *   offset in the LDA instruction or using the value in methodptr R25 the      *
422 *   patching address for storing the method address can be computed:           *
423 *                                                                              *
424 *   method address was either loaded using                                     *
425 *   M_ALD (REG_PV, REG_PV, a)        ; invokestatic/special    ($28)           *
426 *   M_JSR (REG_RA, REG_PV);                                                    *
427 *   M_NOP                                                                      *
428 *   M_LDA (REG_PV, REG_RA, val)                                                *
429 *   or                                                                         *
430 *   M_ALD (REG_PV, REG_METHODPTR, m) ; invokevirtual/interface ($25)           *
431 *   M_JSR (REG_RA, REG_PV);                                                    *
432 *   M_NOP                                                                      *
433 *   in the static case the method pointer can be computed using the            *
434 *   return address and the lda function following the jmp instruction          *
435 *                                                                              *
436 *******************************************************************************/
437
438
439         .ent    asm_call_jit_compiler
440
441 asm_call_jit_compiler:
442         lw      t0,-12(ra)            /* load instruction LD PV,xxx($y)           */
443         srl     t0,t0,21              /* shift right register number $y           */
444         and     t0,t0,31              /* isolate register number                  */
445         addiu   t0,t0,-mptrreg        /* test for REG_METHODPTR                   */
446         beqz    t0,noregchange       
447
448         lw      t0,0(ra)              /* load instruction LDA PV,xxx(RA)          */
449         sll     t0,t0,16
450         sra     t0,t0,16              /* isolate offset                           */
451         aaddu   mptr,t0,ra            /* compute update address                   */
452
453 noregchange:
454         aaddiu  sp,sp,-18*8           /* allocate stack space                     */
455         sd      a0,0*8(sp)            /* save all argument registers              */
456         sd      a1,1*8(sp)            /* they could be used by method             */
457         sd      a2,2*8(sp)
458         sd      a3,3*8(sp)
459         sd      a4,4*8(sp)
460         sd      a5,5*8(sp)
461         sd      a6,6*8(sp)
462         sd      a7,7*8(sp)
463         sdc1    fa0,8*8(sp)
464         sdc1    fa1,9*8(sp)
465         sdc1    fa2,10*8(sp)
466         sdc1    fa3,11*8(sp)
467         sdc1    fa4,12*8(sp)
468         sdc1    fa5,13*8(sp)
469         sdc1    fa6,14*8(sp)
470         sdc1    fa7,15*8(sp)
471         sd      mptr,16*8(sp)         /* save method pointer                      */
472         sd      ra,17*8(sp)           /* save return address                      */
473
474         ald     a0,0(itmp1)           /* pass 'methodinfo' pointer to             */
475         jal     jit_compile           /* jit compiler                             */
476
477         ld      a0,0*8(sp)            /* restore argument registers               */
478         ld      a1,1*8(sp)            /* they could be used by method             */
479         ld      a2,2*8(sp)
480         ld      a3,3*8(sp)
481         ld      a4,4*8(sp)
482         ld      a5,5*8(sp)
483         ld      a6,6*8(sp)
484         ld      a7,7*8(sp)
485         ldc1    fa0,8*8(sp)
486         ldc1    fa1,9*8(sp)
487         ldc1    fa2,10*8(sp)
488         ldc1    fa3,11*8(sp)
489         ldc1    fa4,12*8(sp)
490         ldc1    fa5,13*8(sp)
491         ldc1    fa6,14*8(sp)
492         ldc1    fa7,15*8(sp)
493         ld      mptr,16*8(sp)         /* restore method pointer                   */
494         ld      ra,17*8(sp)           /* restore return address                   */
495         aaddiu  sp,sp,18*8            /* deallocate stack area                    */
496
497         lw      t0,-12(ra)            /* load instruction LDQ PV,xxx($yy)         */
498         sll     t0,t0,16
499         sra     t0,t0,16              /* isolate offset                           */
500
501         aaddu   t0,t0,mptr            /* compute update address via method pointer*/
502         ast     v0,0(t0)              /* save new method address there            */
503
504         move    pv,v0                 /* move method address into pv              */
505
506         jr      pv                    /* and call method. The method returns      */
507                                       /* directly to the caller (ra).             */
508
509         .end    asm_call_jit_compiler
510
511
512 /********************* function asm_handle_exception ***************************
513 *                                                                              *
514 *   This function handles an exception. It does not use the usual calling      *
515 *   conventions. The exception pointer is passed in REG_ITMP1 and the          *
516 *   pc from the exception raising position is passed in REG_ITMP2. It searches *
517 *   the local exception table for a handler. If no one is found, it unwinds    *
518 *   stacks and continues searching the callers.                                *
519 *                                                                              *
520 *   void asm_handle_exception (exceptionptr, exceptionpc);                     *
521 *                                                                              *
522 *******************************************************************************/
523
524         .ent    asm_handle_nat_exception
525
526 asm_handle_nat_exception:
527         lw      t0,0(ra)              /* load instruction LDA PV,xxx(RA)          */
528         sll     t0,t0,16
529         sra     t0,t0,16              /* isolate offset                           */
530         aaddu   pv,t0,ra              /* compute update address                   */
531
532         .aent    asm_handle_exception
533
534 asm_handle_exception:
535         aaddiu  sp,sp,-14*8           /* allocate stack                           */
536         sd      v0,0*8(sp)            /* save possible used registers             */
537         sd      t0,1*8(sp)            /* also registers used by trace_exception   */
538         sd      t1,2*8(sp)
539         sd      t2,3*8(sp)
540         sd      t3,4*8(sp)
541         sd      t8,5*8(sp)
542         sd      a0,6*8(sp)
543         sd      a1,7*8(sp)
544         sd      a2,8*8(sp)
545         sd      a3,9*8(sp)
546         sd      a4,10*8(sp)
547         sd      a5,11*8(sp)
548         sd      a6,12*8(sp)
549         sd      a7,13*8(sp)
550
551         addu    t3,zero,1             /* set no unwind flag                       */
552 ex_stack_loop:
553         aaddiu  sp,sp,-6*8            /* allocate stack                           */
554         sd      xptr,0*8(sp)          /* save used registers                      */
555         sd      xpc,1*8(sp)
556         sd      pv,2*8(sp)
557         sd      ra,3*8(sp)
558         sd      t3,4*8(sp)
559
560         move    a0,xptr
561         ald     a1,MethodPointer(pv)
562         move    a2,xpc
563 /*      move    a3,t3 */
564         move    a3,zero
565         addu    a4,zero,1
566         jal     builtin_trace_exception /* trace_exception(xptr,methodptr)        */
567         
568         ld      xptr,0*8(sp)          /* restore used register                    */
569         ld      xpc,1*8(sp)
570         ld      pv,2*8(sp)
571         ld      ra,3*8(sp)
572         ld      t3,4*8(sp)
573         aaddiu  sp,sp,6*8             /* deallocate stack                         */
574         
575         lw      t0,ExTableSize(pv)    /* t0 = exception table size                */
576         beqz    t0,empty_table        /* if empty table skip                      */
577         aaddiu  t1,pv,ExTableStart    /* t1 = start of exception table            */
578
579 ex_table_loop:
580         ald     t2,ExStartPC(t1)      /* t2 = exception start pc                  */
581         sle     t2,t2,xpc             /* t2 = (startpc <= xpc)                    */
582         beqz    t2,ex_table_cont      /* if (false) continue                      */
583         ald     t2,ExEndPC(t1)        /* t2 = exception end pc                    */
584         slt     t2,xpc,t2             /* t2 = (xpc < endpc)                       */
585         beqz    t2,ex_table_cont      /* if (false) continue                      */
586         ald     a1,ExCatchType(t1)    /* arg1 = exception catch type              */
587         beqz    a1,ex_handle_it       /* NULL catches everything                  */
588
589         lw      itmp3,offclassloaded(a1)
590         bnez    itmp3,L_class_loaded
591
592         aaddiu  sp,sp,-8*8            /* allocate stack                           */
593         sd      t0,0*8(sp)            /* save used register                       */
594         sd      t1,1*8(sp)
595         sd      t3,2*8(sp)
596         sd      xptr,3*8(sp)
597         sd      xpc,4*8(sp)
598         sd      pv,5*8(sp)
599         sd      ra,6*8(sp)
600         sd      a1,7*8(sp)
601                 
602         move    a0,a1
603         jal     class_load
604                 
605         ld      t0,0*8(sp)            /* restore used register                    */
606         ld      t1,1*8(sp)
607         ld      t3,2*8(sp)
608         ld      xptr,3*8(sp)
609         ld      xpc,4*8(sp)
610         ld      pv,5*8(sp)
611         ld      ra,6*8(sp)
612         ld      a1,7*8(sp)
613         aaddiu  sp,sp,8*8             /* deallocate stack                         */
614         
615 L_class_loaded:
616         lw      itmp3,offclasslinked(a1)
617         aaddiu  sp,sp,-8*8            /* allocate stack                           */
618         sd      a1,7*8(sp)
619         bnez    itmp3,L_class_linked
620
621         sd      t0,0*8(sp)            /* save used register                       */
622         sd      t1,1*8(sp)
623         sd      t3,2*8(sp)
624         sd      xptr,3*8(sp)
625         sd      xpc,4*8(sp)
626         sd      pv,5*8(sp)
627         sd      ra,6*8(sp)
628                 
629         move    a0,a1
630         jal     class_link
631                 
632         ld      t0,0*8(sp)            /* restore used register                    */
633         ld      t1,1*8(sp)
634         ld      t3,2*8(sp)
635         ld      xptr,3*8(sp)
636         ld      xpc,4*8(sp)
637         ld      pv,5*8(sp)
638         ld      ra,6*8(sp)
639
640 L_class_linked:
641 _crit_restart1:
642         ld      a1,7*8(sp)
643 _crit_begin1:
644         ald     a0,offobjvftbl(xptr)  /* a0 = vftblptr(xptr)                      */
645         ald     a1,offclassvftbl(a1)  /* a1 = vftblptr(catchtype) class (not obj) */
646         lw      a0,offbaseval(a0)     /* a0 = baseval(xptr)                       */
647         lw      v0,offbaseval(a1)     /* a2 = baseval(catchtype)                  */
648         lw      a1,offdiffval(a1)     /* a1 = diffval(catchtype)                  */
649 _crit_end1:
650         subu    a0,a0,v0              /* a0 = baseval(xptr) - baseval(catchtype)  */
651         sltu    v0,a1,a0              /* v0 = xptr is instanceof catchtype        */
652         aaddiu  sp,sp,8*8             /* deallocate stack                         */
653         bnez    v0,ex_table_cont      /* if (false) continue                      */
654
655 ex_handle_it:
656         ald     xpc,ExHandlerPC(t1)   /* xpc = exception handler pc               */
657
658         beqz    t3,ex_jump            /* if (!(no stack unwinding) skip           */
659
660         ld      v0,0*8(sp)            /* restore possible used registers          */
661         ld      t0,1*8(sp)            /* also registers used by trace_exception   */
662         ld      t1,2*8(sp)
663         ld      t2,3*8(sp)
664         ld      t3,4*8(sp)
665         ld      t8,5*8(sp)
666         ld      a0,6*8(sp)
667         ld      a1,7*8(sp)
668         ld      a2,8*8(sp)
669         ld      a3,9*8(sp)
670         ld      a4,10*8(sp)
671         ld      a5,11*8(sp)
672         ld      a6,12*8(sp)
673         ld      a7,13*8(sp)
674         
675         aaddiu  sp,sp,14*8            /* deallocate stack                         */
676
677 ex_jump:
678         jr      xpc                   /* jump to the handler                      */
679
680 ex_table_cont:
681         aaddiu  t1,t1,ExEntrySize     /* next exception table entry               */
682         addiu   t0,t0,-1              /* decrement entry counter                  */
683         bgtz    t0,ex_table_loop      /* if (t0 > 0) next entry                   */
684
685 empty_table:
686         beqz    t3,ex_already_cleared /* if here the first time, then             */
687         aaddiu  sp,sp,14*8            /* deallocate stack and                     */
688         move    t3,zero               /* clear the no unwind flag                 */
689 ex_already_cleared:
690         lw      t0,IsSync(pv)         /* t0 = SyncOffset                          */
691         beqz    t0,no_monitor_exit    /* if zero no monitorexit                   */
692
693 #if defined(USE_THREADS)
694         aaddu   t0,sp,t0              /* add stackptr to Offset                   */
695         ald     a0,-8(t0)             /* load monitorexit pointer                 */
696
697         aaddiu  sp,sp,-8*8            /* allocate stack                           */
698         sd      t0,0*8(sp)            /* save used register                       */
699         sd      t1,1*8(sp)
700         sd      t3,2*8(sp)
701         sd      xptr,3*8(sp)
702         sd      xpc,4*8(sp)
703         sd      pv,5*8(sp)
704         sd      ra,6*8(sp)
705
706         jal     builtin_monitorexit   /* builtin_monitorexit(objectptr)           */
707         
708         ld      t0,0*8(sp)            /* restore used register                    */
709         ld      t1,1*8(sp)
710         ld      t3,2*8(sp)
711         ld      xptr,3*8(sp)
712         ld      xpc,4*8(sp)
713         ld      pv,5*8(sp)
714         ld      ra,6*8(sp)
715         aaddiu  sp,sp,8*8             /* deallocate stack                         */
716 #endif
717
718 no_monitor_exit:
719         lw      t0,FrameSize(pv)      /* t0 = frame size                          */
720         aaddu   sp,sp,t0              /* unwind stack                             */
721         move    t0,sp                 /* t0 = pointer to save area                */
722         lw      t1,IsLeaf(pv)         /* t1 = is leaf procedure                   */
723         bnez    t1,ex_no_restore      /* if (leaf) skip                           */
724         ld      ra,-8(t0)             /* restore ra                               */
725         aaddiu  t0,t0,-8              /* t0--                                     */
726 ex_no_restore:
727         move    xpc,ra                /* the new xpc is ra                        */
728         lw      t1,IntSave(pv)        /* t1 = saved int register count            */
729         ala     t2,ex_int2            /* t2 = current pc                          */
730         sll     t1,t1,2               /* t1 = register count * 4                  */
731         asubu   t2,t2,t1              /* t2 = ex_int_sav - 4 * register count     */
732         jr      t2                    /* jump to save position                    */
733         ld      s0,-8*8(t0)
734         ld      s1,-7*8(t0)
735         ld      s2,-6*8(t0)
736         ld      s3,-5*8(t0)
737         ld      s4,-4*8(t0)
738         ld      s5,-3*8(t0)
739         ld      s6,-2*8(t0)
740         ld      s7,-1*8(t0)
741 ex_int2:
742         sll     t1,t1,1               /* t1 = register count * 4 * 2              */
743         asubu   t0,t0,t1              /* t0 = t0 - 8 * register count             */
744
745         lw      t1,FltSave(pv)        /* t1 = saved flt register count            */
746         ala     t2,ex_flt2            /* t2 = current pc                          */
747         sll     t1,t1,2               /* t1 = register count * 4                  */
748         asubu   t2,t2,t1              /* t2 = ex_int_sav - 4 * register count     */
749         jr      t2                    /* jump to save position                    */
750         ldc1    fs0,-4*8(t0)
751         ldc1    fs1,-3*8(t0)
752         ldc1    fs2,-2*8(t0)
753         ldc1    fs3,-1*8(t0)
754 ex_flt2:
755         lw      t0,0(ra)              /* load instruction LDA PV,xxx(RA)          */
756         sll     t0,t0,16
757         sra     t0,t0,16              /* isolate offset                           */
758         aaddu   pv,t0,ra              /* compute update address                   */
759         b       ex_stack_loop
760
761         .end    asm_handle_nat_exception
762
763
764 /********************* asm_check_clinit ****************************************
765 *                                                                              *
766 *   Checks if class is initialized. If not, do it right now.                   *
767 *                                                                              *
768 *******************************************************************************/
769                 
770     .ent    asm_check_clinit
771
772 asm_check_clinit:
773         daddiu  sp,sp,-24*8
774
775         sd      ra,0*8(sp)            /* save return address                      */
776
777         sd      a0,1*8(sp)            /* save argument registers for leaf funcs   */
778         sd      a1,2*8(sp)
779         sd      a2,3*8(sp)
780         sd      a3,4*8(sp)
781         sd      a4,5*8(sp)
782         sd      a5,6*8(sp)
783         sd      a6,7*8(sp)
784         sd      a7,8*8(sp)
785
786         sd      t0,9*8(sp)
787         sd      t1,10*8(sp)
788         sd      t2,11*8(sp)
789         sd      t3,12*8(sp)
790         sd      t8,13*8(sp)
791
792         sdc1    fa0,14*8(sp)
793         sdc1    fa1,15*8(sp)
794         sdc1    fa2,16*8(sp)
795         sdc1    fa3,17*8(sp)
796         sdc1    fa4,18*8(sp)
797         sdc1    fa5,19*8(sp)
798         sdc1    fa6,20*8(sp)
799         sdc1    fa7,21*8(sp)
800
801         sd      itmp2,22*8(sp)        /* save machine code                        */
802
803                                       /* check if class is initialized            */
804         lw      itmp3,offclassinit(itmp1)
805         bnez    itmp3,L_is_initialized
806         
807         move    a0,itmp1              /* move class pointer to a0                 */
808         jal     class_init
809                 
810         beqz    v0,L_initializererror
811
812 L_is_initialized:
813         ld      ra,0*8(sp)            /* get return address                       */
814         ld      itmp1,22*8(sp)        /* get machine code                         */
815
816         daddiu  ra,ra,-2*4            /* go back 2 instructions (jal + nop delay) */
817         sw      itmp1,0(ra)           /* patch first instruction                  */
818         dsrl32  itmp1,itmp1,0         /* get high 32 bit                          */
819         sw      itmp1,4(ra)           /* patch second instruction                 */
820
821         move    a0,ra                 /* start of flush area                      */
822         addiu   a1,zero,2*4           /* 2 instruction words long                 */
823         jal     docacheflush          /* flush!                                   */
824         
825         ld      ra,0*8(sp)            /* restore return address                   */
826
827         ld      a0,1*8(sp)            /* restore argument registers               */
828         ld      a1,2*8(sp)
829         ld      a2,3*8(sp)
830         ld      a3,4*8(sp)
831         ld      a4,5*8(sp)
832         ld      a5,6*8(sp)
833         ld      a6,7*8(sp)
834         ld      a7,8*8(sp)
835
836         ld      t0,9*8(sp)
837         ld      t1,10*8(sp)
838         ld      t2,11*8(sp)
839         ld      t3,12*8(sp)
840         ld      t8,13*8(sp)
841
842         ldc1    fa0,14*8(sp)
843         ldc1    fa1,15*8(sp)
844         ldc1    fa2,16*8(sp)
845         ldc1    fa3,17*8(sp)
846         ldc1    fa4,18*8(sp)
847         ldc1    fa5,19*8(sp)
848         ldc1    fa6,20*8(sp)
849         ldc1    fa7,21*8(sp)
850
851         daddiu  sp,sp,24*8
852
853         daddiu  ra,ra,-2*4            /* go back 2 instructions (jal + nop delay) */
854         jr      ra
855
856 L_initializererror:
857 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
858         jal     builtin_asm_get_exceptionptrptr
859 #else
860         la      v0,_exceptionptr
861 #endif
862         ld      xptr,0(v0)            /* get the exception pointer                */
863         sd      zero,0(v0)            /* clear the exception pointer              */
864
865         ld      ra,0*8(sp)            /* restore return address                   */
866
867         ld      a0,1*8(sp)            /* restore argument registers               */
868         ld      a1,2*8(sp)
869         ld      a2,3*8(sp)
870         ld      a3,4*8(sp)
871         ld      a4,5*8(sp)
872         ld      a5,6*8(sp)
873         ld      a6,7*8(sp)
874         ld      a7,8*8(sp)
875
876         ld      t0,9*8(sp)
877         ld      t1,10*8(sp)
878         ld      t2,11*8(sp)
879         ld      t3,12*8(sp)
880         ld      t8,13*8(sp)
881
882         ldc1    fa0,14*8(sp)
883         ldc1    fa1,15*8(sp)
884         ldc1    fa2,16*8(sp)
885         ldc1    fa3,17*8(sp)
886         ldc1    fa4,18*8(sp)
887         ldc1    fa5,19*8(sp)
888         ldc1    fa6,20*8(sp)
889         ldc1    fa7,21*8(sp)
890
891         daddiu  sp,sp,24*8
892
893         aaddiu  xpc,ra,-4             /* faulting address is return adress - 4    */
894         b       asm_handle_exception
895
896         .end    asm_check_clinit
897
898                 
899 /********************* function asm_builtin_monitorenter ***********************
900 *                                                                              *
901 *   Does null check and calls monitorenter or throws an exception              *
902 *                                                                              *
903 *******************************************************************************/
904
905 #if defined(USE_THREADS)
906         .ent    asm_builtin_monitorenter
907
908 asm_builtin_monitorenter:
909         beqz    a0,nb_monitorenter        /* if (null) throw exception            */
910         ala     t9,builtin_monitorenter   /* else call builtin_monitorenter       */
911         j       t9
912
913 nb_monitorenter:
914         daddiu  sp,sp,-2*8
915         sd      ra,0*8(sp)
916         ald     a0,string_java_lang_NullPointerException
917         jal     new_exception
918         move    xptr,v0
919         ld      ra,0*8(sp)
920         daddiu  sp,sp,2*8
921
922         aaddiu  xpc,ra,-4                 /* faulting address is return adress - 4*/
923         b       asm_handle_nat_exception
924
925         .end    asm_builtin_monitorenter
926 #endif
927
928
929 /********************* function asm_builtin_monitorexit ************************
930 *                                                                              *
931 *   Does null check and calls monitorexit or throws an exception               *
932 *                                                                              *
933 *******************************************************************************/
934
935 #if defined(USE_THREADS)
936         .ent    asm_builtin_monitorexit
937
938 asm_builtin_monitorexit:
939         beqz    a0,nb_monitorexit         /* if (null) throw exception            */
940         ala     t9,builtin_monitorexit    /* else call builtin_monitorexit        */
941         j       t9
942
943 nb_monitorexit:
944         daddiu  sp,sp,-2*8
945         sd      ra,0*8(sp)
946         jal     new_nullpointerexception
947         move    xptr,v0
948         ld      ra,0*8(sp)
949         daddiu  sp,sp,2*8
950
951         aaddiu  xpc,ra,-4                 /* faulting address is return adress - 4*/
952         b       asm_handle_nat_exception
953
954         .end    asm_builtin_monitorexit
955 #endif
956
957
958 /************************ function asm_builtin_idiv ****************************
959 *                                                                              *
960 *   Does null check and calls idiv or throws an exception                      *
961 *                                                                              *
962 *******************************************************************************/
963
964         .ent    asm_builtin_idiv
965
966 asm_builtin_idiv:
967         beqz    a1,nb_idiv                /* if (null) throw exception            */
968         ala     itmp3,builtin_idiv        /* else call builtin_idiv               */
969         j       itmp3
970
971 nb_idiv:
972         daddiu  sp,sp,-2*8
973         sd      ra,0*8(sp)
974         jal     new_arithmeticexception
975         move    xptr,v0
976         ld      ra,0*8(sp)
977         daddiu  sp,sp,2*8
978
979         aaddiu  xpc,ra,-4                 /* faulting address is return adress - 4*/
980         b       asm_handle_nat_exception
981
982         .end    asm_builtin_idiv
983
984
985 /************************ function asm_builtin_ldiv ****************************
986 *                                                                              *
987 *   Does null check and calls ldiv or throws an exception                      *
988 *                                                                              *
989 *******************************************************************************/
990
991         .ent    asm_builtin_ldiv
992
993 asm_builtin_ldiv:
994         beqz    a1,nb_ldiv                /* if (null) throw exception            */
995         ala     itmp3,builtin_ldiv        /* else call builtin_ldiv               */
996         j       itmp3
997
998 nb_ldiv:
999         daddiu  sp,sp,-2*8
1000         sd      ra,0*8(sp)
1001         jal     new_arithmeticexception
1002         move    xptr,v0
1003         ld      ra,0*8(sp)
1004         daddiu  sp,sp,2*8
1005
1006         aaddiu  xpc,ra,-4                 /* faulting address is return adress - 4*/
1007         b       asm_handle_nat_exception
1008
1009         .end    asm_builtin_ldiv
1010
1011
1012 /************************ function asm_builtin_irem ****************************
1013 *                                                                              *
1014 *   Does null check and calls irem or throws an exception                      *
1015 *                                                                              *
1016 *******************************************************************************/
1017
1018         .ent    asm_builtin_irem
1019
1020 asm_builtin_irem:
1021         beqz    a1,nb_irem                /* if (null) throw exception            */
1022         ala     t9,builtin_irem           /* else call builtin_irem               */
1023         j       t9
1024
1025 nb_irem:
1026         daddiu  sp,sp,-2*8
1027         sd      ra,0*8(sp)
1028         jal     new_arithmeticexception
1029         move    xptr,v0
1030         ld      ra,0*8(sp)
1031         daddiu  sp,sp,2*8
1032
1033         aaddiu  xpc,ra,-4                 /* faulting address is return adress - 4*/
1034         b       asm_handle_nat_exception
1035
1036         .end    asm_builtin_irem
1037
1038
1039 /************************ function asm_builtin_lrem ****************************
1040 *                                                                              *
1041 *   Does null check and calls lrem or throws an exception                      *
1042 *                                                                              *
1043 *******************************************************************************/
1044
1045         .ent    asm_builtin_lrem
1046
1047 asm_builtin_lrem:
1048         beqz    a1,nb_lrem                /* if (null) throw exception            */
1049         ala     t9,builtin_lrem           /* else call builtin_lrem               */
1050         j       t9
1051
1052 nb_lrem:
1053         daddiu  sp,sp,-2*8
1054         sd      ra,0*8(sp)
1055         jal     new_arithmeticexception
1056         move    xptr,v0
1057         ld      ra,0*8(sp)
1058         daddiu  sp,sp,2*8
1059
1060         aaddiu  xpc,ra,-4                 /* faulting address is return adress - 4*/
1061         b       asm_handle_nat_exception
1062
1063         .end    asm_builtin_lrem
1064
1065
1066 /******************* function asm_builtin_checkarraycast ***********************
1067 *                                                                              *
1068 *   Does the cast check and eventually throws an exception                     *
1069 *                                                                              *
1070 *******************************************************************************/
1071
1072         .ent    asm_builtin_checkarraycast
1073
1074 asm_builtin_checkarraycast:
1075         aaddiu  sp,sp,-16                 /* allocate stack space                 */
1076         sd      ra,0(sp)                  /* save return address                  */
1077         sd      a0,8(sp)                  /* save object pointer                  */
1078         jal     builtin_checkarraycast    /* builtin_checkarraycast               */
1079         beqz    v0,nb_carray_throw        /* if (false) throw exception           */
1080         ld      ra,0(sp)                  /* restore return address               */
1081         ld      v0,8(sp)                  /* return object pointer                */
1082         aaddiu  sp,sp,16                  /* deallocate stack                     */
1083         j       ra                        /* return                               */
1084
1085 nb_carray_throw:
1086         jal     new_classcastexception
1087         move    xptr,v0
1088
1089         ld      ra,0(sp)                  /* restore return address               */
1090         aaddiu  sp,sp,16                  /* free stack space                     */
1091         aaddiu  xpc,ra,-4                 /* faulting address is return adress - 4*/
1092         b       asm_handle_nat_exception
1093
1094         .end    asm_builtin_checkarraycast
1095
1096
1097 /********************* function asm_builtin_checkcast **************************
1098 *                                                                              *
1099 *   Does the cast check and eventually throws an exception                     *
1100 *                                                                              *
1101 *******************************************************************************/
1102
1103         .ent    asm_builtin_checkcast
1104
1105 asm_builtin_checkcast:
1106         aaddiu  sp,sp,-16                 /* allocate stack space                 */
1107         sd      ra,0(sp)                  /* save return address                  */
1108         sd      a0,8(sp)                  /* save object pointer                  */
1109         jal     builtin_checkcast         /* builtin_checkcast                    */
1110         beqz    v0,nb_ccast_throw         /* if (false) throw exception           */
1111         ld      ra,0(sp)                  /* restore return address               */
1112         ld      v0,8(sp)                  /* return object pointer                */
1113         aaddiu  sp,sp,16                  /* deallocate stack                     */
1114         j       ra                        /* return                               */
1115
1116 nb_ccast_throw:
1117         jal     new_classcastexception
1118         move    xptr,v0
1119
1120         ld      ra,0(sp)                  /* restore return address               */
1121         aaddiu  sp,sp,16                  /* free stack space                     */
1122         aaddiu  xpc,ra,-4                 /* faulting address is return adress - 4*/
1123         b       asm_handle_nat_exception
1124
1125         .end    asm_builtin_checkcast
1126
1127
1128 /******************* function asm_builtin_aastore ******************************
1129 *                                                                              *
1130 *   Does the cast check and eventually throws an exception                     *
1131 *   a0 = arrayref, a1 = index, a2 = value                                      *
1132 *                                                                              *
1133 *******************************************************************************/
1134
1135         .ent    asm_builtin_aastore
1136
1137 asm_builtin_aastore:
1138         beqz    a0,nb_aastore_null        /* if null pointer throw exception      */
1139         lw      t0,offarraysize(a0)       /* load size                            */
1140         aaddiu  sp,sp,-32                 /* allocate stack space                 */
1141         sd      ra,0(sp)                  /* save return address                  */
1142         asll    t1,a1,ashift              /* add index*8 to arrayref              */
1143         aaddu   t1,a0,t1                  /* add index * ashift to arrayref       */
1144         sltu    t0,a1,t0                  /* do bound check                       */
1145         beqz    t0,nb_aastore_bound       /* if out of bounds throw exception     */
1146         move    a1,a2                     /* object is second argument            */
1147         sd      t1,8(sp)                  /* save store position                  */
1148         sd      a1,16(sp)                 /* save object                          */
1149         jal     builtin_canstore          /* builtin_canstore(arrayref,object)    */
1150         ld      ra,0(sp)                  /* restore return address               */
1151         ld      a0,8(sp)                  /* restore store position               */
1152         ld      a1,16(sp)                 /* restore object                       */
1153         aaddiu  sp,sp,32                  /* free stack space                     */
1154         beqz    v0,nb_aastore_store       /* if (false) throw exception           */
1155         ast     a1,offobjarrdata(a0)      /* store objectptr in array             */
1156         j       ra                        /* return                               */
1157
1158 nb_aastore_null:
1159         daddiu  sp,sp,-2*8
1160         sd      ra,0*8(sp)
1161         jal     new_nullpointerexception
1162         move    xptr,v0
1163         ld      ra,0*8(sp)
1164         daddiu  sp,sp,2*8
1165         
1166         move    xpc,ra                    /* faulting address is return adress    */
1167         b       asm_handle_nat_exception
1168
1169 nb_aastore_bound:
1170         daddiu  sp,sp,-2*8
1171         sd      ra,0*8(sp)
1172         move    a0,a1                     /* move index into a0                   */
1173         jal     new_arrayindexoutofboundsexception
1174         move    xptr,v0
1175         ld      ra,0*8(sp)
1176         daddiu  sp,sp,2*8
1177
1178         aaddiu  sp,sp,32                  /* free stack space                     */
1179         move    xpc,ra                    /* faulting address is return adress    */
1180         b       asm_handle_nat_exception
1181
1182 nb_aastore_store:
1183         daddiu  sp,sp,-2*8
1184         sd      ra,0*8(sp)
1185         jal     new_arraystoreexception
1186         move    xptr,v0
1187         ld      ra,0*8(sp)
1188         daddiu  sp,sp,2*8
1189
1190         move    xpc,ra                    /* faulting address is return adress    */
1191         b       asm_handle_nat_exception
1192
1193         .end    asm_builtin_aastore
1194
1195
1196 /******************* function asm_initialize_thread_stack **********************
1197 *                                                                              *
1198 *   u1* asm_initialize_thread_stack (void *func, u1 *stack);                   *
1199 *                                                                              *
1200 *   initialize a thread stack                                                  *
1201 *                                                                              *
1202 *******************************************************************************/
1203
1204         .ent    asm_initialize_thread_stack
1205
1206 asm_initialize_thread_stack:
1207         aaddiu  a1,a1,-14*8     /* allocate save area                             */
1208         sd      zero, 0*8(a1)   /* s0 initalize thread area                       */
1209         sd      zero, 1*8(a1)   /* s1                                             */
1210         sd      zero, 2*8(a1)   /* s2                                             */
1211         sd      zero, 3*8(a1)   /* s3                                             */
1212         sd      zero, 4*8(a1)   /* s4                                             */
1213         sd      zero, 5*8(a1)   /* s5                                             */
1214         sd      zero, 6*8(a1)   /* s6                                             */
1215         sd      zero, 7*8(a1)   /* s7                                             */
1216         sd      zero, 8*8(a1)   /* s8                                             */
1217         sd      zero, 9*8(a1)   /* fs0                                            */
1218         sd      zero,10*8(a1)   /* fs1                                            */
1219         sd      zero,11*8(a1)   /* fs2                                            */
1220         sd      zero,12*8(a1)   /* fs3                                            */
1221         sd      a0, 13*8(a1)
1222         move    v0,a1
1223         j       ra              /* return                                         */
1224
1225         .end    asm_initialize_thread_stack
1226
1227
1228 /******************* function asm_perform_threadswitch *************************
1229 *                                                                              *
1230 *   void asm_perform_threadswitch (u1 **from, u1 **to, u1 **stackTop);         *
1231 *                                                                              *
1232 *   performs a threadswitch                                                    *
1233 *                                                                              *
1234 *******************************************************************************/
1235
1236         .ent    asm_perform_threadswitch
1237
1238 asm_perform_threadswitch:
1239         aaddiu  sp,sp,-14*8     /* allocate new stack                             */
1240         sd      s0,  0*8(sp)    /* save saved registers of old thread             */
1241         sd      s1,  1*8(sp)
1242         sd      s2,  2*8(sp)
1243         sd      s3,  3*8(sp)
1244         sd      s4,  4*8(sp)
1245         sd      s5,  5*8(sp)
1246         sd      s6,  6*8(sp)
1247         sd      s7,  7*8(sp)
1248         sd      s8,  8*8(sp)
1249         sdc1    fs0, 9*8(sp)
1250         sdc1    fs1,10*8(sp)
1251         sdc1    fs2,11*8(sp)
1252         sdc1    fs3,12*8(sp)
1253         sd      ra, 13*8(sp)
1254         ast     sp,0(a0)        /* save old stack pointer                         */
1255         ast     sp,0(a2)        /* stackTop = old stack pointer                   */
1256         ald     sp,0(a1)        /* load new stack pointer                         */
1257         ld      s0,  0*8(sp)    /* load saved registers of new thread             */
1258         ld      s1,  1*8(sp)
1259         ld      s2,  2*8(sp)
1260         ld      s3,  3*8(sp)
1261         ld      s4,  4*8(sp)
1262         ld      s5,  5*8(sp)
1263         ld      s6,  6*8(sp)
1264         ld      s7,  7*8(sp)
1265         ld      s8,  8*8(sp)
1266         ldc1    fs0, 9*8(sp)
1267         ldc1    fs1,10*8(sp)
1268         ldc1    fs2,11*8(sp)
1269         ldc1    fs3,12*8(sp)
1270         ld      ra, 13*8(sp)
1271         aaddiu  sp,sp,14*8      /* deallocate new stack                           */
1272         move    itmp3, ra
1273         j       ra              /* return                                         */
1274
1275         .end    asm_perform_threadswitch
1276
1277
1278 /********************* function asm_switchstackandcall *************************
1279 *                                                                              *
1280 *  void asm_switchstackandcall (void *stack, void *func, void **stacktopsave); *
1281 *                                                                              *
1282 *   Switches to a new stack, calls a function and switches back.               *
1283 *       a0      new stack pointer                                              *
1284 *       a1      function pointer                                               *
1285 *               a2              pointer to variable where stack top should be stored           *
1286 *                                                                              *
1287 *******************************************************************************/
1288
1289         .ent    asm_switchstackandcall
1290
1291 asm_switchstackandcall:
1292         aaddiu  a0,a0,-16       /* allocate new stack                             */
1293         sd      ra,0(a0)        /* save return address on new stack               */
1294         sd      sp,8(a0)        /* save old stack pointer on new stack            */
1295         sd      sp,0(a2)        /* save old stack pointer to variable             */
1296         move    sp,a0           /* switch to new stack                            */
1297         
1298         move    itmp3,a1
1299         move    a0,a3
1300         jalr    itmp3           /* and call function                              */
1301
1302         ld      ra,0(sp)        /* load return address                            */
1303         ld      sp,8(sp)        /* switch to old stack                            */
1304
1305         j       ra              /* return                                         */
1306
1307         .end    asm_switchstackandcall
1308
1309
1310         .ent    asm_getclassvalues_atomic
1311
1312 asm_getclassvalues_atomic:
1313 _crit_restart2:
1314 _crit_begin2:
1315         lw      t0,offbaseval(a0)
1316         lw      t1,offdiffval(a0)
1317         lw      t2,offbaseval(a1)
1318 _crit_end2:
1319         sw      t0,offcast_super_baseval(a2)
1320         sw      t1,offcast_super_diffval(a2)
1321         sw      t2,offcast_sub_baseval(a2)
1322         j       ra
1323
1324         .end    asm_getclassvalues_atomic
1325
1326     .data
1327
1328 asm_criticalsections:
1329 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
1330     .dword  _crit_begin1
1331     .dword  _crit_end1
1332     .dword  _crit_restart1
1333     .dword  _crit_begin2
1334     .dword  _crit_end2
1335     .dword  _crit_restart2
1336 #endif
1337     .dword  0
1338
1339
1340         .text
1341
1342         .ent    compare_and_swap
1343 compare_and_swap:
1344 1:
1345         lld             v0,0(a0)
1346         bne             v0,a1,2f
1347         move    t0,a2
1348         scd             t0,0(a0)
1349         beqz    t0,1b
1350 2:
1351         sync
1352         j               ra
1353         .end    compare_and_swap
1354 /*
1355  * These are local overrides for various environment variables in Emacs.
1356  * Please do not remove this and leave it at the end of the file, where
1357  * Emacs will automagically detect them.
1358  * ---------------------------------------------------------------------
1359  * Local variables:
1360  * mode: asm
1361  * indent-tabs-mode: t
1362  * c-basic-offset: 4
1363  * tab-width: 4
1364  * End:
1365  */