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