1a5b3cbd4aa9669593b60958d0f26f592b7e1804
[cacao.git] / src / vm / jit / mips / emit.c
1 /* src/vm/jit/mips/emit.c - MIPS code emitter functions
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: emit.c 4398 2006-01-31 23:43:08Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33
34 #include "vm/types.h"
35
36 #include "vm/jit/mips/codegen.h"
37 #include "vm/jit/mips/md-abi.h"
38
39 #include "mm/memory.h"
40
41 #include "threads/lock-common.h"
42
43 #include "vm/builtin.h"
44 #include "vm/exceptions.h"
45 #include "vm/stringlocal.h" /* XXX for gen_resolvebranch */
46
47 #include "vm/jit/abi.h"
48 #include "vm/jit/abi-asm.h"
49 #include "vm/jit/asmpart.h"
50 #include "vm/jit/dseg.h"
51 #include "vm/jit/emit-common.h"
52 #include "vm/jit/jit.h"
53 #include "vm/jit/replace.h"
54
55 #include "vmcore/options.h"
56
57
58 /* emit_load *******************************************************************
59
60    Emits a possible load of an operand.
61
62 *******************************************************************************/
63
64 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
65 {
66         codegendata  *cd;
67         s4            disp;
68         s4            reg;
69
70         /* get required compiler data */
71
72         cd = jd->cd;
73
74         if (src->flags & INMEMORY) {
75                 COUNT_SPILLS;
76
77                 disp = src->vv.regoff * 8;
78
79                 switch (src->type) {
80 #if SIZEOF_VOID_P == 8
81                 case TYPE_INT:
82                 case TYPE_LNG:
83                 case TYPE_ADR:
84                         M_LLD(tempreg, REG_SP, disp);
85                         break;
86 #else
87                 case TYPE_INT:
88                 case TYPE_ADR:
89                         M_ILD(tempreg, REG_SP, disp);
90                         break;
91                 case TYPE_LNG:
92                         M_LLD(tempreg, REG_SP, disp);
93                         break;
94 #endif
95                 case TYPE_FLT:
96                         M_FLD(tempreg, REG_SP, disp);
97                         break;
98                 case TYPE_DBL:
99                         M_DLD(tempreg, REG_SP, disp);
100                         break;
101                 default:
102                         vm_abort("emit_load: unknown type %d", src->type);
103                 }
104
105                 reg = tempreg;
106         }
107         else
108                 reg = src->vv.regoff;
109
110         return reg;
111 }
112
113
114 /* emit_load_low ***************************************************************
115
116    Emits a possible load of the low 32-bits of an operand.
117
118 *******************************************************************************/
119
120 #if SIZEOF_VOID_P == 4
121 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
122 {
123         codegendata  *cd;
124         s4            disp;
125         s4            reg;
126
127         assert(src->type == TYPE_LNG);
128
129         /* get required compiler data */
130
131         cd = jd->cd;
132
133         if (src->flags & INMEMORY) {
134                 COUNT_SPILLS;
135
136                 disp = src->vv.regoff * 8;
137
138 #if WORDS_BIGENDIAN == 1
139                 M_ILD(tempreg, REG_SP, disp + 4);
140 #else
141                 M_ILD(tempreg, REG_SP, disp);
142 #endif
143
144                 reg = tempreg;
145         }
146         else
147                 reg = GET_LOW_REG(src->vv.regoff);
148
149         return reg;
150 }
151 #endif /* SIZEOF_VOID_P == 4 */
152
153
154 /* emit_load_high **************************************************************
155
156    Emits a possible load of the high 32-bits of an operand.
157
158 *******************************************************************************/
159
160 #if SIZEOF_VOID_P == 4
161 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
162 {
163         codegendata  *cd;
164         s4            disp;
165         s4            reg;
166
167         assert(src->type == TYPE_LNG);
168
169         /* get required compiler data */
170
171         cd = jd->cd;
172
173         if (src->flags & INMEMORY) {
174                 COUNT_SPILLS;
175
176                 disp = src->vv.regoff * 8;
177
178 #if WORDS_BIGENDIAN == 1
179                 M_ILD(tempreg, REG_SP, disp);
180 #else
181                 M_ILD(tempreg, REG_SP, disp + 4);
182 #endif
183
184                 reg = tempreg;
185         }
186         else
187                 reg = GET_HIGH_REG(src->vv.regoff);
188
189         return reg;
190 }
191 #endif /* SIZEOF_VOID_P == 4 */
192
193
194 /* emit_store ******************************************************************
195
196    Emits a possible store to variable.
197
198 *******************************************************************************/
199
200 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
201 {
202         codegendata  *cd;
203         s4            disp;
204
205         /* get required compiler data */
206
207         cd = jd->cd;
208
209         if (dst->flags & INMEMORY) {
210                 COUNT_SPILLS;
211
212                 disp = dst->vv.regoff * 8;
213
214                 switch (dst->type) {
215 #if SIZEOF_VOID_P == 8
216                 case TYPE_INT:
217                 case TYPE_LNG:
218                 case TYPE_ADR:
219                         M_LST(d, REG_SP, disp);
220                         break;
221 #else
222                 case TYPE_INT:
223                 case TYPE_ADR:
224                         M_IST(d, REG_SP, disp);
225                         break;
226                 case TYPE_LNG:
227                         M_LST(d, REG_SP, disp);
228                         break;
229 #endif
230                 case TYPE_FLT:
231                         M_FST(d, REG_SP, disp);
232                         break;
233                 case TYPE_DBL:
234                         M_DST(d, REG_SP, disp);
235                         break;
236                 default:
237                         vm_abort("emit_store: unknown type %d", dst->type);
238                 }
239         }
240 }
241
242
243 /* emit_copy *******************************************************************
244
245    Generates a register/memory to register/memory copy.
246
247 *******************************************************************************/
248
249 void emit_copy(jitdata *jd, instruction *iptr)
250 {
251         codegendata *cd;
252         varinfo     *src;
253         varinfo     *dst;
254         s4           s1, d;
255
256         /* get required compiler data */
257
258         cd = jd->cd;
259
260         /* get source and destination variables */
261
262         src = VAROP(iptr->s1);
263         dst = VAROP(iptr->dst);
264
265         if ((src->vv.regoff != dst->vv.regoff) ||
266                 ((src->flags ^ dst->flags) & INMEMORY)) {
267
268                 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
269                         /* emit nothing, as the value won't be used anyway */
270                         return;
271                 }
272
273                 /* If one of the variables resides in memory, we can eliminate
274                    the register move from/to the temporary register with the
275                    order of getting the destination register and the load. */
276
277                 if (IS_INMEMORY(src->flags)) {
278 #if SIZEOF_VOID_P == 4
279                         if (IS_2_WORD_TYPE(src->type))
280                                 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
281                         else
282 #endif
283                                 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
284                         s1 = emit_load(jd, iptr, src, d);
285                 }
286                 else {
287                         s1 = emit_load(jd, iptr, src, REG_IFTMP);
288 #if SIZEOF_VOID_P == 4
289                         if (IS_2_WORD_TYPE(src->type))
290                                 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
291                         else
292 #endif
293                                 d = codegen_reg_of_var(iptr->opc, dst, s1);
294                 }
295
296                 if (s1 != d) {
297                         switch (dst->type) {
298 #if SIZEOF_VOID_P == 8
299                         case TYPE_INT:
300                         case TYPE_LNG:
301                         case TYPE_ADR:
302                                 M_MOV(s1, d);
303                                 break;
304 #else
305                         case TYPE_INT:
306                         case TYPE_ADR:
307                                 M_MOV(s1, d);
308                                 break;
309                         case TYPE_LNG:
310                                 M_LNGMOVE(s1, d);
311                                 break;
312 #endif
313                         case TYPE_FLT:
314                                 M_FMOV(s1, d);
315                                 break;
316                         case TYPE_DBL:
317                                 M_DMOV(s1, d);
318                                 break;
319                         default:
320                                 vm_abort("emit_copy: unknown type %d", dst->type);
321                         }
322                 }
323
324                 emit_store(jd, iptr, dst, d);
325         }
326 }
327
328
329 /* emit_iconst *****************************************************************
330
331    XXX
332
333 *******************************************************************************/
334
335 void emit_iconst(codegendata *cd, s4 d, s4 value)
336 {
337         s4 disp;
338
339     if ((value >= -32768) && (value <= 32767))
340         M_IADD_IMM(REG_ZERO, value, d);
341         else if ((value >= 0) && (value <= 0xffff))
342         M_OR_IMM(REG_ZERO, value, d);
343         else {
344         disp = dseg_add_s4(cd, value);
345         M_ILD(d, REG_PV, disp);
346     }
347 }
348
349
350 /* emit_lconst *****************************************************************
351
352    XXX
353
354 *******************************************************************************/
355
356 void emit_lconst(codegendata *cd, s4 d, s8 value)
357 {
358         s4 disp;
359
360 #if SIZEOF_VOID_P == 8
361         if ((value >= -32768) && (value <= 32767))
362                 M_LADD_IMM(REG_ZERO, value, d);
363         else if ((value >= 0) && (value <= 0xffff))
364                 M_OR_IMM(REG_ZERO, value, d);
365         else {
366                 disp = dseg_add_s8(cd, value);
367                 M_LLD(d, REG_PV, disp);
368         }
369 #else
370         disp = dseg_add_s8(cd, value);
371         M_LLD(d, REG_PV, disp);
372 #endif
373 }
374
375
376 /* emit_branch *****************************************************************
377
378    Emits the code for conditional and unconditional branchs.
379
380    NOTE: The reg argument may contain two packed registers.
381
382 *******************************************************************************/
383
384 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
385 {
386         s4 checkdisp;
387         s4 branchdisp;
388
389         /* calculate the different displacements */
390
391         checkdisp  = (disp - 4);
392         branchdisp = (disp - 4) >> 2;
393
394         /* check which branch to generate */
395
396         if (condition == BRANCH_UNCONDITIONAL) {
397                 /* check displacement for overflow */
398
399                 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
400                         /* if the long-branches flag isn't set yet, do it */
401
402                         if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
403                                 cd->flags |= (CODEGENDATA_FLAG_ERROR |
404                                                           CODEGENDATA_FLAG_LONGBRANCHES);
405                         }
406
407                         vm_abort("emit_branch: emit unconditional long-branch code");
408                 }
409                 else {
410                         M_BR(branchdisp);
411                         M_NOP;
412                 }
413         }
414         else {
415                 /* and displacement for overflow */
416
417                 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
418                         /* if the long-branches flag isn't set yet, do it */
419
420                         if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
421                                 cd->flags |= (CODEGENDATA_FLAG_ERROR |
422                                                           CODEGENDATA_FLAG_LONGBRANCHES);
423                         }
424
425                         switch (condition) {
426                         case BRANCH_EQ:
427                                 M_BNE(GET_HIGH_REG(reg), GET_LOW_REG(reg), 5);
428                                 break;
429                         case BRANCH_NE:
430                                 M_BEQ(GET_HIGH_REG(reg), GET_LOW_REG(reg), 5);
431                                 break;
432                         case BRANCH_LT:
433                                 M_BGEZ(reg, 5);
434                                 break;
435                         case BRANCH_GE:
436                                 M_BLTZ(reg, 5);
437                                 break;
438                         case BRANCH_GT:
439                                 M_BLEZ(reg, 5);
440                                 break;
441                         case BRANCH_LE:
442                                 M_BGTZ(reg, 5);
443                                 break;
444                         default:
445                                 vm_abort("emit_branch: unknown condition %d", condition);
446                         }
447
448                         /* The actual branch code which is over-jumped (NOTE: we
449                            don't use a branch delay slot here). */
450
451                         M_LUI(REG_ITMP3, branchdisp >> 16);
452                         M_OR_IMM(REG_ITMP3, branchdisp, REG_ITMP3);
453                         M_AADD(REG_PV, REG_ITMP3, REG_ITMP3);
454                         M_JMP(REG_ITMP3);
455                         M_NOP;
456
457                 }
458                 else {
459                         switch (condition) {
460                         case BRANCH_EQ:
461                                 M_BEQ(GET_HIGH_REG(reg), GET_LOW_REG(reg), branchdisp);
462                                 break;
463                         case BRANCH_NE:
464                                 M_BNE(GET_HIGH_REG(reg), GET_LOW_REG(reg), branchdisp);
465                                 break;
466                         case BRANCH_LT:
467                                 M_BLTZ(reg, branchdisp);
468                                 break;
469                         case BRANCH_GE:
470                                 M_BGEZ(reg, branchdisp);
471                                 break;
472                         case BRANCH_GT:
473                                 M_BGTZ(reg, branchdisp);
474                                 break;
475                         case BRANCH_LE:
476                                 M_BLEZ(reg, branchdisp);
477                                 break;
478                         default:
479                                 vm_abort("emit_branch: unknown condition %d", condition);
480                         }
481
482                         /* branch delay */
483                         M_NOP;
484                 }
485         }
486 }
487
488
489 /* emit_arithmetic_check *******************************************************
490
491    Emit an ArithmeticException check.
492
493 *******************************************************************************/
494
495 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
496 {
497         if (INSTRUCTION_MUST_CHECK(iptr)) {
498                 M_BNEZ(reg, 2);
499                 M_NOP;
500                 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_ARITHMETIC);
501         }
502 }
503
504
505 /* emit_arrayindexoutofbounds_check ********************************************
506
507    Emit an ArrayIndexOutOfBoundsException check.
508
509 *******************************************************************************/
510
511 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
512 {
513         if (INSTRUCTION_MUST_CHECK(iptr)) {
514                 M_ILD_INTERN(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
515                 M_CMPULT(s2, REG_ITMP3, REG_ITMP3);
516                 M_BNEZ(REG_ITMP3, 2);
517                 M_NOP;
518                 M_ALD_INTERN(s2, REG_ZERO, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
519         }
520 }
521
522
523 /* emit_classcast_check ********************************************************
524
525    Emit a ClassCastException check.
526
527 *******************************************************************************/
528
529 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
530 {
531         if (INSTRUCTION_MUST_CHECK(iptr)) {
532                 switch (condition) {
533                 case ICMD_IFEQ:
534                         M_BNEZ(reg, 2);
535                         break;
536
537                 case ICMD_IFNE:
538                         M_BEQZ(reg, 2);
539                         break;
540
541                 case ICMD_IFLE:
542                         M_BGTZ(reg, 2);
543                         break;
544
545                 default:
546                         vm_abort("emit_classcast_check: unknown condition %d", condition);
547                 }
548
549                 M_NOP;
550                 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
551         }
552 }
553
554
555 /* emit_nullpointer_check ******************************************************
556
557    Emit a NullPointerException check.
558
559 *******************************************************************************/
560
561 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
562 {
563         if (INSTRUCTION_MUST_CHECK(iptr)) {
564                 M_BNEZ(reg, 2);
565                 M_NOP;
566                 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
567         }
568 }
569
570
571 /* emit_exception_check ********************************************************
572
573    Emit an Exception check.
574
575 *******************************************************************************/
576
577 void emit_exception_check(codegendata *cd, instruction *iptr)
578 {
579         if (INSTRUCTION_MUST_CHECK(iptr)) {
580                 M_BNEZ(REG_RESULT, 2);
581                 M_NOP;
582                 M_ALD_INTERN(REG_RESULT, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
583         }
584 }
585
586
587 /* emit_patcher_stubs **********************************************************
588
589    Generates the code for the patcher stubs.
590
591 *******************************************************************************/
592
593 void emit_patcher_stubs(jitdata *jd)
594 {
595         codegendata *cd;
596         patchref    *pr;
597         u4           mcode[5];
598         u1          *savedmcodeptr;
599         u1          *tmpmcodeptr;
600         s4           targetdisp;
601         s4           disp;
602
603         /* get required compiler data */
604
605         cd = jd->cd;
606
607         /* generate code patching stub call code */
608
609         targetdisp = 0;
610
611 /*      for (pr = list_first_unsynced(cd->patchrefs); pr != NULL; */
612 /*               pr = list_next_unsynced(cd->patchrefs, pr)) { */
613         for (pr = cd->patchrefs; pr != NULL; pr = pr->next) {
614                 /* check code segment size */
615
616                 MCODECHECK(100);
617
618                 /* Get machine code which is patched back in later. The
619                    call is 2 instruction words long. */
620
621                 tmpmcodeptr = (u1 *) (cd->mcodebase + pr->branchpos);
622
623                 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
624                    MIPS causes a SIGSEGV and using the same code for both
625                    architectures is much better. */
626
627                 mcode[0] = ((u4 *) tmpmcodeptr)[0];
628                 mcode[1] = ((u4 *) tmpmcodeptr)[1];
629
630                 mcode[2] = ((u4 *) tmpmcodeptr)[2];
631                 mcode[3] = ((u4 *) tmpmcodeptr)[3];
632                 mcode[4] = ((u4 *) tmpmcodeptr)[4];
633
634                 /* Patch in the call to call the following code (done at
635                    compile time). */
636
637                 savedmcodeptr = cd->mcodeptr;   /* save current mcodeptr              */
638                 cd->mcodeptr  = tmpmcodeptr;    /* set mcodeptr to patch position     */
639
640                 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
641
642 /*              if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) { */
643                         /* Recalculate the displacement to be relative to PV. */
644
645                         disp = savedmcodeptr - cd->mcodebase;
646
647                         M_LUI(REG_ITMP3, disp >> 16);
648                         M_OR_IMM(REG_ITMP3, disp, REG_ITMP3);
649                         M_AADD(REG_PV, REG_ITMP3, REG_ITMP3);
650                         M_JMP(REG_ITMP3);
651                         M_NOP;
652 /*              } */
653 /*              else { */
654 /*                      M_BR(disp); */
655 /*                      M_NOP; */
656 /*                      M_NOP; */
657 /*                      M_NOP; */
658 /*                      M_NOP; */
659 /*              } */
660
661                 cd->mcodeptr = savedmcodeptr;   /* restore the current mcodeptr   */
662
663                 /* create stack frame */
664
665                 M_ASUB_IMM(REG_SP, 8 * 8, REG_SP);
666
667                 /* calculate return address and move it onto the stack */
668
669                 M_LDA(REG_ITMP3, REG_PV, pr->branchpos);
670                 M_AST(REG_ITMP3, REG_SP, 7 * 8);
671
672                 /* move pointer to java_objectheader onto stack */
673
674 #if defined(ENABLE_THREADS)
675                 /* create a virtual java_objectheader */
676
677                 (void) dseg_add_unique_address(cd, NULL);                  /* flcword */
678                 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
679                 disp = dseg_add_unique_address(cd, NULL);                  /* vftbl   */
680
681                 M_LDA(REG_ITMP3, REG_PV, disp);
682                 M_AST(REG_ITMP3, REG_SP, 6 * 8);
683 #else
684                 /* do nothing */
685 #endif
686
687                 /* move machine code onto stack */
688
689                 disp = dseg_add_s4(cd, mcode[0]);
690                 M_ILD(REG_ITMP3, REG_PV, disp);
691                 M_IST(REG_ITMP3, REG_SP, 3 * 8 + 0);
692
693                 disp = dseg_add_s4(cd, mcode[1]);
694                 M_ILD(REG_ITMP3, REG_PV, disp);
695                 M_IST(REG_ITMP3, REG_SP, 3 * 8 + 4);
696
697                 disp = dseg_add_s4(cd, mcode[2]);
698                 M_ILD(REG_ITMP3, REG_PV, disp);
699                 M_IST(REG_ITMP3, REG_SP, 4 * 8 + 0);
700
701                 disp = dseg_add_s4(cd, mcode[3]);
702                 M_ILD(REG_ITMP3, REG_PV, disp);
703                 M_IST(REG_ITMP3, REG_SP, 4 * 8 + 4);
704
705                 disp = dseg_add_s4(cd, mcode[4]);
706                 M_ILD(REG_ITMP3, REG_PV, disp);
707                 M_IST(REG_ITMP3, REG_SP, 5 * 8 + 0);
708
709                 /* move class/method/field reference onto stack */
710
711                 disp = dseg_add_address(cd, pr->ref);
712                 M_ALD(REG_ITMP3, REG_PV, disp);
713                 M_AST(REG_ITMP3, REG_SP, 2 * 8);
714
715                 /* move data segment displacement onto stack */
716
717                 disp = dseg_add_s4(cd, pr->disp);
718                 M_ILD(REG_ITMP3, REG_PV, disp);
719                 M_IST(REG_ITMP3, REG_SP, 1 * 8);
720
721                 /* move patcher function pointer onto stack */
722
723                 disp = dseg_add_functionptr(cd, pr->patcher);
724                 M_ALD(REG_ITMP3, REG_PV, disp);
725                 M_AST(REG_ITMP3, REG_SP, 0 * 8);
726
727                 if (targetdisp == 0) {
728                         targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
729
730                         disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
731                         M_ALD(REG_ITMP3, REG_PV, disp);
732                         M_JMP(REG_ITMP3);
733                         M_NOP;
734                 }
735                 else {
736                         disp = (((u4 *) cd->mcodebase) + targetdisp) -
737                                 (((u4 *) cd->mcodeptr) + 1);
738
739                         M_BR(disp);
740                         M_NOP;
741                 }
742         }
743 }
744
745
746 /* emit_replacement_stubs ******************************************************
747
748    Generates the code for the replacement stubs.
749
750 *******************************************************************************/
751
752 #if defined(ENABLE_REPLACEMENT)
753 void emit_replacement_stubs(jitdata *jd)
754 {
755         codegendata *cd;
756         codeinfo    *code;
757         rplpoint    *rplp;
758         s4           disp;
759         s4           i;
760 #if !defined(NDEBUG)
761         u1          *savedmcodeptr;
762 #endif
763
764         /* get required compiler data */
765
766         cd   = jd->cd;
767         code = jd->code;
768
769         rplp = code->rplpoints;
770
771         /* store beginning of replacement stubs */
772
773         code->replacementstubs = (u1*) (cd->mcodeptr - cd->mcodebase);
774
775         for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
776                 /* do not generate stubs for non-trappable points */
777
778                 if (rplp->flags & RPLPOINT_FLAG_NOTRAP)
779                         continue;
780
781                 /* check code segment size */
782
783                 MCODECHECK(100);
784
785 #if !defined(NDEBUG)
786                 savedmcodeptr = cd->mcodeptr;
787 #endif
788
789                 /* create stack frame - 16-byte aligned */
790
791                 M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
792
793                 /* push address of `rplpoint` struct */
794
795                 disp = dseg_add_address(cd, rplp);
796                 M_ALD(REG_ITMP3, REG_PV, disp);
797                 M_AST(REG_ITMP3, REG_SP, 0 * 8);
798
799                 /* jump to replacement function */
800
801                 disp = dseg_add_functionptr(cd, asm_replacement_out);
802                 M_ALD(REG_ITMP3, REG_PV, disp);
803                 M_JMP(REG_ITMP3);
804                 M_NOP; /* delay slot */
805
806                 assert((cd->mcodeptr - savedmcodeptr) == 4*REPLACEMENT_STUB_SIZE);
807         }
808 }
809 #endif /* defined(ENABLE_REPLACEMENT) */
810
811
812 /* emit_verbosecall_enter ******************************************************
813
814    Generates the code for the call trace.
815
816 *******************************************************************************/
817
818 #if !defined(NDEBUG)
819 void emit_verbosecall_enter(jitdata *jd)
820 {
821         methodinfo   *m;
822         codegendata  *cd;
823         registerdata *rd;
824         methoddesc   *md;
825         s4            disp;
826         s4            i, j, t;
827
828         /* get required compiler data */
829
830         m  = jd->m;
831         cd = jd->cd;
832         rd = jd->rd;
833
834         md = m->parseddesc;
835
836         /* mark trace code */
837
838         M_NOP;
839
840         M_LDA(REG_SP, REG_SP, -(PA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8));
841         M_AST(REG_RA, REG_SP, PA_SIZE + 1 * 8);
842
843         /* save argument registers (we store the registers as address
844            types, so it's correct for MIPS32 too) */
845
846         for (i = 0; i < INT_ARG_CNT; i++)
847                 M_AST(abi_registers_integer_argument[i], REG_SP, PA_SIZE + (2 + i) * 8);
848
849         for (i = 0; i < FLT_ARG_CNT; i++)
850                 M_DST(abi_registers_float_argument[i], REG_SP, PA_SIZE + (2 + INT_ARG_CNT + i) * 8);
851
852         /* save temporary registers for leaf methods */
853
854         if (jd->isleafmethod) {
855                 for (i = 0; i < INT_TMP_CNT; i++)
856                         M_AST(rd->tmpintregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + i) * 8);
857
858                 for (i = 0; i < FLT_TMP_CNT; i++)
859                         M_DST(rd->tmpfltregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
860         }
861
862         /* Load float arguments into integer registers.  MIPS32 has less
863            float argument registers than integer ones, we need to check
864            that. */
865
866         for (i = 0; i < md->paramcount && i < INT_ARG_CNT && i < FLT_ARG_CNT; i++) {
867                 t = md->paramtypes[i].type;
868
869                 if (IS_FLT_DBL_TYPE(t)) {
870                         if (IS_2_WORD_TYPE(t)) {
871                                 M_DST(abi_registers_float_argument[i], REG_SP, 0 * 8);
872                                 M_LLD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
873                         }
874                         else {
875                                 M_FST(abi_registers_float_argument[i], REG_SP, 0 * 8);
876                                 M_ILD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
877                         }
878                 }
879         }
880
881 #if SIZEOF_VOID_P == 4
882                 for (i = 0, j = 0; i < md->paramcount && i < TRACE_ARGS_NUM; i++) {
883                         t = md->paramtypes[i].type;
884
885                         if (IS_INT_LNG_TYPE(t)) {
886                                 if (IS_2_WORD_TYPE(t)) {
887                                         M_ILD(abi_registers_integer_argument[j], REG_SP, PA_SIZE + (2 + i) * 8);
888                                         M_ILD(abi_registers_integer_argument[j + 1], REG_SP, PA_SIZE + (2 + i) * 8 + 4);
889                                 }
890                                 else {
891 # if WORDS_BIGENDIAN == 1
892                                         M_MOV(REG_ZERO, abi_registers_integer_argument[j]);
893                                         M_ILD(abi_registers_integer_argument[j + 1], REG_SP, PA_SIZE + (2 + i) * 8);
894 # else
895                                         M_ILD(abi_registers_integer_argument[j], REG_SP, PA_SIZE + (2 + i) * 8);
896                                         M_MOV(REG_ZERO, abi_registers_integer_argument[j + 1]);
897 # endif
898                                 }
899                                 j += 2;
900                         }
901                 }
902 #endif
903
904         disp = dseg_add_address(cd, m);
905         M_ALD(REG_ITMP1, REG_PV, disp);
906         M_AST(REG_ITMP1, REG_SP, PA_SIZE + 0 * 8);
907         disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
908         M_ALD(REG_ITMP3, REG_PV, disp);
909         M_JSR(REG_RA, REG_ITMP3);
910         M_NOP;
911
912         /* restore argument registers */
913
914         for (i = 0; i < INT_ARG_CNT; i++)
915                 M_ALD(abi_registers_integer_argument[i], REG_SP, PA_SIZE + (2 + i) * 8);
916
917         for (i = 0; i < FLT_ARG_CNT; i++)
918                 M_DLD(abi_registers_float_argument[i], REG_SP, PA_SIZE + (2 + INT_ARG_CNT + i) * 8);
919
920         /* restore temporary registers for leaf methods */
921
922         if (jd->isleafmethod) {
923                 for (i = 0; i < INT_TMP_CNT; i++)
924                         M_ALD(rd->tmpintregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + i) * 8);
925
926                 for (i = 0; i < FLT_TMP_CNT; i++)
927                         M_DLD(rd->tmpfltregs[i], REG_SP, PA_SIZE + (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
928         }
929
930         M_ALD(REG_RA, REG_SP, PA_SIZE + 1 * 8);
931         M_LDA(REG_SP, REG_SP, PA_SIZE + (2 + ARG_CNT + TMP_CNT) * 8);
932
933         /* mark trace code */
934
935         M_NOP;
936 }
937 #endif /* !defined(NDEBUG) */
938
939
940 /* emit_verbosecall_exit *******************************************************
941
942    Generates the code for the call trace.
943
944    void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
945
946 *******************************************************************************/
947
948 #if !defined(NDEBUG)
949 void emit_verbosecall_exit(jitdata *jd)
950 {
951         methodinfo   *m;
952         codegendata  *cd;
953         registerdata *rd;
954         methoddesc   *md;
955         s4            disp;
956
957         /* get required compiler data */
958
959         m  = jd->m;
960         cd = jd->cd;
961         rd = jd->rd;
962
963         md = m->parseddesc;
964
965         /* mark trace code */
966
967         M_NOP;
968
969 #if SIZEOF_VOID_P == 8
970         M_ASUB_IMM(REG_SP, 4 * 8, REG_SP);          /* keep stack 16-byte aligned */
971         M_AST(REG_RA, REG_SP, 0 * 8);
972
973         M_LST(REG_RESULT, REG_SP, 1 * 8);
974         M_DST(REG_FRESULT, REG_SP, 2 * 8);
975
976         M_MOV(REG_RESULT, REG_A0);
977         M_DMOV(REG_FRESULT, REG_FA1);
978         M_FMOV(REG_FRESULT, REG_FA2);
979
980         disp = dseg_add_address(cd, m);
981         M_ALD(REG_A4, REG_PV, disp);
982 #else
983         M_ASUB_IMM(REG_SP, (8*4 + 4 * 8), REG_SP);
984         M_AST(REG_RA, REG_SP, 8*4 + 0 * 8);
985
986         M_LST(REG_RESULT_PACKED, REG_SP, 8*4 + 1 * 8);
987         M_DST(REG_FRESULT, REG_SP, 8*4 + 2 * 8);
988
989         switch (md->returntype.type) {
990         case TYPE_LNG:
991                 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
992                 break;
993
994         default:
995 # if WORDS_BIGENDIAN == 1
996                 M_MOV(REG_ZERO, REG_A0);
997                 M_MOV(REG_RESULT, REG_A1);
998 # else
999                 M_MOV(REG_RESULT, REG_A0);
1000                 M_MOV(REG_ZERO, REG_A1);
1001 # endif
1002         }
1003
1004         M_LLD(REG_A2_A3_PACKED, REG_SP, 8*4 + 2 * 8);
1005         M_FST(REG_FRESULT, REG_SP, 4*4 + 0 * 4);
1006
1007         disp = dseg_add_address(cd, m);
1008         M_ALD(REG_ITMP1, REG_PV, disp);
1009         M_AST(REG_ITMP1, REG_SP, 4*4 + 1 * 4);
1010 #endif
1011
1012         disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
1013         M_ALD(REG_ITMP3, REG_PV, disp);
1014         M_JSR(REG_RA, REG_ITMP3);
1015         M_NOP;
1016
1017 #if SIZEOF_VOID_P == 8
1018         M_DLD(REG_FRESULT, REG_SP, 2 * 8);
1019         M_LLD(REG_RESULT, REG_SP, 1 * 8);
1020
1021         M_ALD(REG_RA, REG_SP, 0 * 8);
1022         M_AADD_IMM(REG_SP, 4 * 8, REG_SP);
1023 #else
1024         M_DLD(REG_FRESULT, REG_SP, 8*4 + 2 * 8);
1025         M_LLD(REG_RESULT_PACKED, REG_SP, 8*4 + 1 * 8);
1026
1027         M_ALD(REG_RA, REG_SP, 8*4 + 0 * 8);
1028         M_AADD_IMM(REG_SP, 8*4 + 4 * 8, REG_SP);
1029 #endif
1030
1031         /* mark trace code */
1032
1033         M_NOP;
1034 }
1035 #endif /* !defined(NDEBUG) */
1036
1037
1038 /*
1039  * These are local overrides for various environment variables in Emacs.
1040  * Please do not remove this and leave it at the end of the file, where
1041  * Emacs will automagically detect them.
1042  * ---------------------------------------------------------------------
1043  * Local variables:
1044  * mode: c
1045  * indent-tabs-mode: t
1046  * c-basic-offset: 4
1047  * tab-width: 4
1048  * End:
1049  * vim:noexpandtab:sw=4:ts=4:
1050  */