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