We need to save temp registers in asm_check_clinit, because they can be
[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 1290 2004-07-09 13:19:56Z twisti $
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 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 #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    t8,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,t8
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(t8)              /* 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(t8)                /* 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      t8,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      t8,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,-22*8
799         sd      ra,0*8(sp)
800
801         sd      a0,1*8(sp)            /* save argument registers for leaf funcs   */
802         sd      a1,2*8(sp)
803         sd      a2,3*8(sp)
804         sd      a3,4*8(sp)
805         sd      a4,5*8(sp)
806         sd      a5,6*8(sp)
807         sd      a6,7*8(sp)
808         sd      a7,8*8(sp)
809
810         sd      t0,9*8(sp)
811         sd      t1,10*8(sp)
812         sd      t2,11*8(sp)
813         sd      t3,12*8(sp)
814         sd      t8,13*8(sp)
815
816         sdc1    fa0,14*8(sp)
817         sdc1    fa1,15*8(sp)
818         sdc1    fa2,16*8(sp)
819         sdc1    fa3,17*8(sp)
820         sdc1    fa4,18*8(sp)
821         sdc1    fa5,19*8(sp)
822         sdc1    fa6,20*8(sp)
823         sdc1    fa7,21*8(sp)
824
825         move    a0,itmp1
826         jal     class_init
827                 
828         ld      ra,0*8(sp)
829
830         ld      a0,1*8(sp)            /* restore argument registers               */
831         ld      a1,2*8(sp)
832         ld      a2,3*8(sp)
833         ld      a3,4*8(sp)
834         ld      a4,5*8(sp)
835         ld      a5,6*8(sp)
836         ld      a6,7*8(sp)
837         ld      a7,8*8(sp)
838
839         ld      t0,9*8(sp)
840         ld      t1,10*8(sp)
841         ld      t2,11*8(sp)
842         ld      t3,12*8(sp)
843         ld      t8,13*8(sp)
844
845         ldc1    fa0,14*8(sp)
846         ldc1    fa1,15*8(sp)
847         ldc1    fa2,16*8(sp)
848         ldc1    fa3,17*8(sp)
849         ldc1    fa4,18*8(sp)
850         ldc1    fa5,19*8(sp)
851         ldc1    fa6,20*8(sp)
852         ldc1    fa7,21*8(sp)
853
854         daddiu  sp,sp,22*8
855
856         beqz    v0,L_initializererror
857
858 L_is_initialized:
859 #if 0
860         move    itmp1,ra              /* now patch the calling code               */
861         daddiu  itmp1,itmp1,-4*4      /* go back 4 instructions                   */
862         lui     itmp2,0x1000          /* b                                        */
863         daddiu  itmp2,itmp2,3         /* jump over 3 instructions                 */
864         sw      itmp2,0(itmp1)        /* store the new branch: br +3              */
865         sw      zero,4(itmp1)         /* and a nop delay slot                     */
866 #endif
867
868         j       ra
869
870 L_initializererror:
871 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
872         daddiu  sp,sp,-1*8
873         sd      ra,0*8(sp)
874         jal     builtin_asm_get_exceptionptrptr
875         move    xptr,v0
876         ld      ra,0*8(sp)
877         daddiu  sp,sp,1*8
878 #else
879         la      itmp3,_exceptionptr
880         ld      xptr,0(itmp3)
881         sd      zero,0(itmp3)
882 #endif
883
884         aaddiu  xpc,ra,-4             /* faulting address is return adress - 4    */
885         b       asm_handle_exception
886
887         .end    asm_check_clinit
888
889                 
890 /********************* function asm_builtin_monitorenter ***********************
891 *                                                                              *
892 *   Does null check and calls monitorenter or throws an exception              *
893 *                                                                              *
894 *******************************************************************************/
895
896         .ent    asm_builtin_monitorenter
897
898 asm_builtin_monitorenter:
899         beqz    a0,nb_monitorenter        /* if (null) throw exception            */
900         ala     t9,builtin_monitorenter   /* else call builtin_monitorenter       */
901         j       t9
902
903 nb_monitorenter:
904         daddiu  sp,sp,-1*8
905         sd      ra,0*8(sp)
906         ald     a0,string_java_lang_NullPointerException
907         jal     new_exception
908         move    xptr,v0
909         ld      ra,0*8(sp)
910         daddiu  sp,sp,1*8
911
912         aaddiu  xpc,ra,-4                 /* faulting address is return adress - 4*/
913         b       asm_handle_nat_exception
914
915         .end    asm_builtin_monitorenter
916
917
918 /********************* function asm_builtin_monitorexit ************************
919 *                                                                              *
920 *   Does null check and calls monitorexit or throws an exception               *
921 *                                                                              *
922 *******************************************************************************/
923
924         .ent    asm_builtin_monitorexit
925
926 asm_builtin_monitorexit:
927         beqz    a0,nb_monitorexit         /* if (null) throw exception            */
928         ala     t9,builtin_monitorexit    /* else call builtin_monitorexit        */
929         j       t9
930
931 nb_monitorexit:
932         daddiu  sp,sp,-1*8
933         sd      ra,0*8(sp)
934         ald     a0,string_java_lang_NullPointerException
935         jal     new_exception
936         move    xptr,v0
937         ld      ra,0*8(sp)
938         daddiu  sp,sp,1*8
939
940         aaddiu  xpc,ra,-4                 /* faulting address is return adress - 4*/
941         b       asm_handle_nat_exception
942
943         .end    asm_builtin_monitorexit
944
945
946 /************************ function asm_builtin_idiv ****************************
947 *                                                                              *
948 *   Does null check and calls idiv or throws an exception                      *
949 *                                                                              *
950 *******************************************************************************/
951
952         .ent    asm_builtin_idiv
953
954 asm_builtin_idiv:
955         beqz    a1,nb_idiv                /* if (null) throw exception            */
956         ala     itmp3,builtin_idiv        /* else call builtin_idiv               */
957         j       itmp3
958
959 nb_idiv:
960         daddiu  sp,sp,-1*8
961         sd      ra,0*8(sp)
962         ald     a0,string_java_lang_ArithmeticException
963         ald     a1,string_java_lang_ArithmeticException_message
964         jal     new_exception_message
965         move    xptr,v0
966         ld      ra,0*8(sp)
967         daddiu  sp,sp,1*8
968
969         aaddiu  xpc,ra,-4                 /* faulting address is return adress - 4*/
970         b       asm_handle_nat_exception
971
972         .end    asm_builtin_idiv
973
974
975 /************************ function asm_builtin_ldiv ****************************
976 *                                                                              *
977 *   Does null check and calls ldiv or throws an exception                      *
978 *                                                                              *
979 *******************************************************************************/
980
981         .ent    asm_builtin_ldiv
982
983 asm_builtin_ldiv:
984         beqz    a1,nb_ldiv                /* if (null) throw exception            */
985         ala     itmp3,builtin_ldiv        /* else call builtin_ldiv               */
986         j       itmp3
987
988 nb_ldiv:
989         daddiu  sp,sp,-1*8
990         sd      ra,0*8(sp)
991         ald     a0,string_java_lang_ArithmeticException
992         ald     a1,string_java_lang_ArithmeticException_message
993         jal     new_exception_message
994         move    xptr,v0
995         ld      ra,0*8(sp)
996         daddiu  sp,sp,1*8
997
998         aaddiu  xpc,ra,-4                 /* faulting address is return adress - 4*/
999         b       asm_handle_nat_exception
1000
1001         .end    asm_builtin_ldiv
1002
1003
1004 /************************ function asm_builtin_irem ****************************
1005 *                                                                              *
1006 *   Does null check and calls irem or throws an exception                      *
1007 *                                                                              *
1008 *******************************************************************************/
1009
1010         .ent    asm_builtin_irem
1011
1012 asm_builtin_irem:
1013         beqz    a1,nb_irem                /* if (null) throw exception            */
1014         ala     t9,builtin_irem           /* else call builtin_irem               */
1015         j       t9
1016
1017 nb_irem:
1018         daddiu  sp,sp,-1*8
1019         sd      ra,0*8(sp)
1020         ald     a0,string_java_lang_ArithmeticException
1021         ald     a1,string_java_lang_ArithmeticException_message
1022         jal     new_exception_message
1023         move    xptr,v0
1024         ld      ra,0*8(sp)
1025         daddiu  sp,sp,1*8
1026
1027         aaddiu  xpc,ra,-4                 /* faulting address is return adress - 4*/
1028         b       asm_handle_nat_exception
1029
1030         .end    asm_builtin_irem
1031
1032
1033 /************************ function asm_builtin_lrem ****************************
1034 *                                                                              *
1035 *   Does null check and calls lrem or throws an exception                      *
1036 *                                                                              *
1037 *******************************************************************************/
1038
1039         .ent    asm_builtin_lrem
1040
1041 asm_builtin_lrem:
1042         beqz    a1,nb_lrem                /* if (null) throw exception            */
1043         ala     t9,builtin_lrem           /* else call builtin_lrem               */
1044         j       t9
1045
1046 nb_lrem:
1047         daddiu  sp,sp,-1*8
1048         sd      ra,0*8(sp)
1049         ald     a0,string_java_lang_ArithmeticException
1050         ald     a1,string_java_lang_ArithmeticException_message
1051         jal     new_exception_message
1052         move    xptr,v0
1053         ld      ra,0*8(sp)
1054         daddiu  sp,sp,1*8
1055
1056         aaddiu  xpc,ra,-4                 /* faulting address is return adress - 4*/
1057         b       asm_handle_nat_exception
1058
1059         .end    asm_builtin_lrem
1060
1061
1062 /******************* function asm_builtin_checkarraycast ***********************
1063 *                                                                              *
1064 *   Does the cast check and eventually throws an exception                     *
1065 *                                                                              *
1066 *******************************************************************************/
1067
1068         .ent    asm_builtin_checkarraycast
1069
1070 asm_builtin_checkarraycast:
1071         aaddiu  sp,sp,-16                 /* allocate stack space                 */
1072         sd      ra,0(sp)                  /* save return address                  */
1073         sd      a0,8(sp)                  /* save object pointer                  */
1074         jal     builtin_checkarraycast    /* builtin_checkarraycast               */
1075         beqz    v0,nb_carray_throw        /* if (false) throw exception           */
1076         ld      ra,0(sp)                  /* restore return address               */
1077         ld      v0,8(sp)                  /* return object pointer                */
1078         aaddiu  sp,sp,16                  /* deallocate stack                     */
1079         j       ra                        /* return                               */
1080
1081 nb_carray_throw:
1082         ald     a0,string_java_lang_ClassCastException
1083         jal     new_exception
1084         move    xptr,v0
1085
1086         ld      ra,0(sp)                  /* restore return address               */
1087         aaddiu  sp,sp,16                  /* free stack space                     */
1088         aaddiu  xpc,ra,-4                 /* faulting address is return adress - 4*/
1089         b       asm_handle_nat_exception
1090
1091         .end    asm_builtin_checkarraycast
1092
1093
1094 /********************* function asm_builtin_checkcast **************************
1095 *                                                                              *
1096 *   Does the cast check and eventually throws an exception                     *
1097 *                                                                              *
1098 *******************************************************************************/
1099
1100         .ent    asm_builtin_checkcast
1101
1102 asm_builtin_checkcast:
1103         aaddiu  sp,sp,-16                 /* allocate stack space                 */
1104         sd      ra,0(sp)                  /* save return address                  */
1105         sd      a0,8(sp)                  /* save object pointer                  */
1106         jal     builtin_checkcast         /* builtin_checkcast                    */
1107         beqz    v0,nb_ccast_throw         /* if (false) throw exception           */
1108         ld      ra,0(sp)                  /* restore return address               */
1109         ld      v0,8(sp)                  /* return object pointer                */
1110         aaddiu  sp,sp,16                  /* deallocate stack                     */
1111         j       ra                        /* return                               */
1112
1113 nb_ccast_throw:
1114         ald     a0,string_java_lang_ClassCastException
1115         jal     new_exception
1116         move    xptr,v0
1117
1118         ld      ra,0(sp)                  /* restore return address               */
1119         aaddiu  sp,sp,16                  /* free stack space                     */
1120         aaddiu  xpc,ra,-4                 /* faulting address is return adress - 4*/
1121         b       asm_handle_nat_exception
1122
1123         .end    asm_builtin_checkcast
1124
1125
1126 /******************* function asm_builtin_aastore ******************************
1127 *                                                                              *
1128 *   Does the cast check and eventually throws an exception                     *
1129 *   a0 = arrayref, a1 = index, a2 = value                                      *
1130 *                                                                              *
1131 *******************************************************************************/
1132
1133         .ent    asm_builtin_aastore
1134
1135 asm_builtin_aastore:
1136         beqz    a0,nb_aastore_null        /* if null pointer throw exception      */
1137         lw      t0,offarraysize(a0)       /* load size                            */
1138         aaddiu  sp,sp,-32                 /* allocate stack space                 */
1139         sd      ra,0(sp)                  /* save return address                  */
1140         asll    t1,a1,ashift              /* add index*8 to arrayref              */
1141         aaddu   t1,a0,t1                  /* add index * ashift to arrayref       */
1142         sltu    t0,a1,t0                  /* do bound check                       */
1143         beqz    t0,nb_aastore_bound       /* if out of bounds throw exception     */
1144         move    a1,a2                     /* object is second argument            */
1145         sd      t1,8(sp)                  /* save store position                  */
1146         sd      a1,16(sp)                 /* save object                          */
1147         jal     builtin_canstore          /* builtin_canstore(arrayref,object)    */
1148         ld      ra,0(sp)                  /* restore return address               */
1149         ld      a0,8(sp)                  /* restore store position               */
1150         ld      a1,16(sp)                 /* restore object                       */
1151         aaddiu  sp,sp,32                  /* free stack space                     */
1152         beqz    v0,nb_aastore_store       /* if (false) throw exception           */
1153         ast     a1,offobjarrdata(a0)      /* store objectptr in array             */
1154         j       ra                        /* return                               */
1155
1156 nb_aastore_null:
1157         daddiu  sp,sp,-1*8
1158         sd      ra,0*8(sp)
1159         ald     a0,string_java_lang_NullPointerException
1160         jal     new_exception
1161         move    xptr,v0
1162         ld      ra,0*8(sp)
1163         daddiu  sp,sp,1*8
1164         
1165         move    xpc,ra                    /* faulting address is return adress    */
1166         b       asm_handle_nat_exception
1167
1168 nb_aastore_bound:
1169         daddiu  sp,sp,-1*8
1170         sd      ra,0*8(sp)
1171         ald     a0,string_java_lang_ArrayIndexOutOfBoundsException
1172         jal     new_exception_int         /* a1 already contains the index        */
1173         move    xptr,v0
1174         ld      ra,0*8(sp)
1175         daddiu  sp,sp,1*8
1176
1177         aaddiu  sp,sp,32                  /* free stack space                     */
1178         move    xpc,ra                    /* faulting address is return adress    */
1179         b       asm_handle_nat_exception
1180
1181 nb_aastore_store:
1182         daddiu  sp,sp,-1*8
1183         sd      ra,0*8(sp)
1184         ald     a0,string_java_lang_ArrayStoreException
1185         jal     new_exception
1186         move    xptr,v0
1187         ld      ra,0*8(sp)
1188         daddiu  sp,sp,1*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     .quad   _crit_begin1
1331     .quad   _crit_end1
1332     .quad   _crit_restart1
1333     .quad   _crit_begin2
1334     .quad   _crit_end2
1335     .quad   _crit_restart2
1336 #endif
1337     .quad   0
1338
1339
1340         .text
1341
1342         .ent    atomic_swap
1343 atomic_swap:
1344 1:
1345         lld             v0,0(a0)
1346         move    t0,a1
1347         scd             t0,0(a0)
1348         beqz    t0,1b
1349         j               ra
1350         .end    atomic_swap
1351
1352         .ent    compare_and_swap
1353 compare_and_swap:
1354         move    v0,zero
1355 1:
1356         lld             t0,0(a0)
1357         bne             t0,a1,2f
1358         move    v0,a2
1359         scd             v0,0(a0)
1360         beqz    v0,1b
1361 2:
1362         j               ra
1363         .end    compare_and_swap
1364 /*
1365  * These are local overrides for various environment variables in Emacs.
1366  * Please do not remove this and leave it at the end of the file, where
1367  * Emacs will automagically detect them.
1368  * ---------------------------------------------------------------------
1369  * Local variables:
1370  * mode: asm
1371  * indent-tabs-mode: t
1372  * c-basic-offset: 4
1373  * tab-width: 4
1374  * End:
1375  */