Merged changes from trunk.
[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 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    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: emitfuncs.c 4398 2006-01-31 23:43:08Z twisti $
32
33 */
34
35
36 #include "config.h"
37
38 #include "vm/types.h"
39
40 #include "md-abi.h"
41
42 #include "vm/jit/mips/codegen.h"
43
44 #if defined(ENABLE_THREADS)
45 # include "threads/native/lock.h"
46 #endif
47
48 #include "vm/exceptions.h"
49 #include "vm/stringlocal.h" /* XXX for gen_resolvebranch */
50 #include "vm/jit/abi-asm.h"
51 #include "vm/jit/asmpart.h"
52 #include "vm/jit/dseg.h"
53 #include "vm/jit/emit.h"
54 #include "vm/jit/jit.h"
55 #include "vm/jit/replace.h"
56
57
58 /* code generation functions **************************************************/
59
60 /* emit_load_s1 ****************************************************************
61
62    Emits a possible load of the first source operand.
63
64 *******************************************************************************/
65
66 s4 emit_load_s1(jitdata *jd, instruction *iptr, stackptr 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->regoff * 8;
80
81                 if (IS_FLT_DBL_TYPE(src->type)) {
82                         if (IS_2_WORD_TYPE(src->type))
83                                 M_DLD(tempreg, REG_SP, disp);
84                         else
85                                 M_FLD(tempreg, REG_SP, disp);
86                 } else
87                         M_LLD(tempreg, REG_SP, disp);
88
89                 reg = tempreg;
90         } else
91                 reg = src->regoff;
92
93         return reg;
94 }
95
96
97 /* emit_load_s2 ****************************************************************
98
99    Emits a possible load of the second source operand.
100
101 *******************************************************************************/
102
103 s4 emit_load_s2(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
104 {
105         codegendata  *cd;
106         s4            disp;
107         s4            reg;
108
109         /* get required compiler data */
110
111         cd = jd->cd;
112
113         if (src->flags & INMEMORY) {
114                 COUNT_SPILLS;
115
116                 disp = src->regoff * 8;
117
118                 if (IS_FLT_DBL_TYPE(src->type)) {
119                         if (IS_2_WORD_TYPE(src->type))
120                                 M_DLD(tempreg, REG_SP, disp);
121                         else
122                                 M_FLD(tempreg, REG_SP, disp);
123                 } else
124                         M_LLD(tempreg, REG_SP, disp);
125
126                 reg = tempreg;
127         } else
128                 reg = src->regoff;
129
130         return reg;
131 }
132
133
134 /* emit_load_s3 ****************************************************************
135
136    Emits a possible load of the third source operand.
137
138 *******************************************************************************/
139
140 s4 emit_load_s3(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
141 {
142         codegendata  *cd;
143         s4            disp;
144         s4            reg;
145
146         /* get required compiler data */
147
148         cd = jd->cd;
149
150         if (src->flags & INMEMORY) {
151                 COUNT_SPILLS;
152
153                 disp = src->regoff * 8;
154
155                 if (IS_FLT_DBL_TYPE(src->type)) {
156                         if (IS_2_WORD_TYPE(src->type))
157                                 M_DLD(tempreg, REG_SP, disp);
158                         else
159                                 M_FLD(tempreg, REG_SP, disp);
160                 } else
161                         M_LLD(tempreg, REG_SP, disp);
162
163                 reg = tempreg;
164         } else
165                 reg = src->regoff;
166
167         return reg;
168 }
169
170
171 /* emit_store ******************************************************************
172
173    XXX
174
175 *******************************************************************************/
176
177 void emit_store(jitdata *jd, instruction *iptr, stackptr dst, s4 d)
178 {
179         codegendata  *cd;
180         s4            disp;
181
182         /* get required compiler data */
183
184         cd = jd->cd;
185
186         if (dst->flags & INMEMORY) {
187                 COUNT_SPILLS;
188
189                 disp = dst->regoff * 8;
190
191                 if (IS_FLT_DBL_TYPE(dst->type)) {
192                         if (IS_2_WORD_TYPE(dst->type))
193                                 M_DST(d, REG_SP, disp);
194                         else
195                                 M_FST(d, REG_SP, disp);
196                 } else
197                         M_LST(d, REG_SP, disp);
198         }
199 }
200
201
202 /* emit_copy *******************************************************************
203
204    XXX
205
206 *******************************************************************************/
207
208 void emit_copy(jitdata *jd, instruction *iptr, stackptr src, stackptr dst)
209 {
210         codegendata  *cd;
211         registerdata *rd;
212         s4            s1, d;
213
214         /* get required compiler data */
215
216         cd = jd->cd;
217         rd = jd->rd;
218
219         if ((src->regoff != dst->regoff) ||
220                 ((src->flags ^ dst->flags) & INMEMORY)) {
221                 d = codegen_reg_of_var(rd, iptr->opc, dst, REG_IFTMP);
222                 s1 = emit_load_s1(jd, iptr, src, d);
223
224                 if (s1 != d) {
225                         if (IS_FLT_DBL_TYPE(src->type)) {
226                                 if (IS_2_WORD_TYPE(src->type))
227                                         M_DMOV(s1, d);
228                                 else
229                                         M_FMOV(s1, d);
230                         } else
231                                 M_MOV(s1, d);
232                 }
233
234                 emit_store(jd, iptr, dst, d);
235         }
236 }
237
238
239 /* emit_iconst *****************************************************************
240
241    XXX
242
243 *******************************************************************************/
244
245 void emit_iconst(codegendata *cd, s4 d, s4 value)
246 {
247         s4 disp;
248
249     if ((value >= -32768) && (value <= 32767))
250         M_IADD_IMM(REG_ZERO, value, d);
251         else if ((value >= 0) && (value <= 0xffff))
252         M_OR_IMM(REG_ZERO, value, d);
253         else {
254         disp = dseg_adds4(cd, value);
255         M_ILD(d, REG_PV, disp);
256     }
257 }
258
259
260 /* emit_lconst *****************************************************************
261
262    XXX
263
264 *******************************************************************************/
265
266 void emit_lconst(codegendata *cd, s4 d, s8 value)
267 {
268         s4 disp;
269
270         if ((value >= -32768) && (value <= 32767))
271                 M_LADD_IMM(REG_ZERO, value, d);
272         else if ((value >= 0) && (value <= 0xffff))
273                 M_OR_IMM(REG_ZERO, value, d);
274         else {
275                 disp = dseg_adds8(cd, value);
276                 M_LLD(d, REG_PV, disp);
277         }
278 }
279
280
281 /* emit_exception_stubs ********************************************************
282
283    Generates the code for the exception stubs.
284
285 *******************************************************************************/
286
287 void emit_exception_stubs(jitdata *jd)
288 {
289         codegendata  *cd;
290         registerdata *rd;
291         exceptionref *eref;
292         s4            targetdisp;
293         s4            disp;
294
295         /* get required compiler data */
296
297         cd = jd->cd;
298         rd = jd->rd;
299
300         /* generate exception stubs */
301
302         targetdisp = 0;
303
304         for (eref = cd->exceptionrefs; eref != NULL; eref = eref->next) {
305                 gen_resolvebranch(cd->mcodebase + eref->branchpos, 
306                                                   eref->branchpos, cd->mcodeptr - cd->mcodebase);
307
308                 MCODECHECK(100);
309
310                 /* Check if the exception is an
311                    ArrayIndexOutOfBoundsException.  If so, move index register
312                    into REG_ITMP1. */
313
314                 if (eref->reg != -1)
315                         M_MOV(eref->reg, REG_ITMP1);
316
317                 /* calcuate exception address */
318
319                 M_LDA(REG_ITMP2_XPC, REG_PV, eref->branchpos - 4);
320
321                 /* move function to call into REG_ITMP3 */
322
323                 disp = dseg_addaddress(cd, eref->function);
324                 M_ALD(REG_ITMP3, REG_PV, disp);
325
326                 if (targetdisp == 0) {
327                         targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
328
329                         M_MOV(REG_PV, rd->argintregs[0]);
330                         M_MOV(REG_SP, rd->argintregs[1]);
331
332                         if (jd->isleafmethod)
333                                 M_MOV(REG_RA, rd->argintregs[2]);
334                         else
335                                 M_ALD(rd->argintregs[2],
336                                           REG_SP, cd->stackframesize * 8 - SIZEOF_VOID_P);
337
338                         M_MOV(REG_ITMP2_XPC, rd->argintregs[3]);
339                         M_MOV(REG_ITMP1, rd->argintregs[4]);
340
341                         M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
342                         M_AST(REG_ITMP2_XPC, REG_SP, 0 * 8);
343
344                         if (jd->isleafmethod)
345                                 M_AST(REG_RA, REG_SP, 1 * 8);
346
347                         M_JSR(REG_RA, REG_ITMP3);
348                         M_NOP;
349                         M_MOV(REG_RESULT, REG_ITMP1_XPTR);
350
351                         if (jd->isleafmethod)
352                                 M_ALD(REG_RA, REG_SP, 1 * 8);
353
354                         M_ALD(REG_ITMP2_XPC, REG_SP, 0 * 8);
355                         M_AADD_IMM(REG_SP, 2 * 8, REG_SP);
356
357                         disp = dseg_addaddress(cd, asm_handle_exception);
358                         M_ALD(REG_ITMP3, REG_PV, disp);
359                         M_JMP(REG_ITMP3);
360                         M_NOP;
361                 }
362                 else {
363                         disp = (((u4 *) cd->mcodebase) + targetdisp) -
364                                 (((u4 *) cd->mcodeptr) + 1);
365
366                         M_BR(disp);
367                         M_NOP;
368                 }
369         }
370 }
371
372
373 /* emit_patcher_stubs **********************************************************
374
375    Generates the code for the patcher stubs.
376
377 *******************************************************************************/
378
379 void emit_patcher_stubs(jitdata *jd)
380 {
381         codegendata *cd;
382         patchref    *pref;
383         u4           mcode[2];
384         u1          *savedmcodeptr;
385         u1          *tmpmcodeptr;
386         s4           targetdisp;
387         s4           disp;
388
389         /* get required compiler data */
390
391         cd = jd->cd;
392
393         /* generate code patching stub call code */
394
395         targetdisp = 0;
396
397         for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
398                 /* check code segment size */
399
400                 MCODECHECK(100);
401
402                 /* Get machine code which is patched back in later. The
403                    call is 2 instruction words long. */
404
405                 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
406
407                 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
408                    MIPS causes a SIGSEGV and using the same code for both
409                    architectures is much better. */
410
411                 mcode[0] = ((u4 *) tmpmcodeptr)[0];
412                 mcode[1] = ((u4 *) tmpmcodeptr)[1];
413
414                 /* Patch in the call to call the following code (done at
415                    compile time). */
416
417                 savedmcodeptr = cd->mcodeptr;   /* save current mcodeptr          */
418                 cd->mcodeptr  = tmpmcodeptr;    /* set mcodeptr to patch position */
419
420                 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
421
422                 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
423                         *exceptionptr =
424                                 new_internalerror("Jump offset is out of range: %d > +/-%d",
425                                                                   disp, 0x00007fff);
426                         return;
427                 }
428
429                 M_BR(disp);
430                 M_NOP;
431
432                 cd->mcodeptr = savedmcodeptr;   /* restore the current mcodeptr   */
433
434                 /* create stack frame */
435
436                 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
437
438                 /* calculate return address and move it onto the stack */
439
440                 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
441                 M_AST(REG_ITMP3, REG_SP, 5 * 8);
442
443                 /* move pointer to java_objectheader onto stack */
444
445 #if defined(ENABLE_THREADS)
446                 /* create a virtual java_objectheader */
447
448                 (void) dseg_addaddress(cd, NULL);                          /* flcword */
449                 (void) dseg_addaddress(cd, lock_get_initial_lock_word());
450                 disp = dseg_addaddress(cd, NULL);                          /* vftbl   */
451
452                 M_LDA(REG_ITMP3, REG_PV, disp);
453                 M_AST(REG_ITMP3, REG_SP, 4 * 8);
454 #else
455                 /* do nothing */
456 #endif
457
458                 /* move machine code onto stack */
459
460                 disp = dseg_adds4(cd, mcode[0]);
461                 M_ILD(REG_ITMP3, REG_PV, disp);
462                 M_IST(REG_ITMP3, REG_SP, 3 * 8);
463
464                 disp = dseg_adds4(cd, mcode[1]);
465                 M_ILD(REG_ITMP3, REG_PV, disp);
466                 M_IST(REG_ITMP3, REG_SP, 3 * 8 + 4);
467
468                 /* move class/method/field reference onto stack */
469
470                 disp = dseg_addaddress(cd, pref->ref);
471                 M_ALD(REG_ITMP3, REG_PV, disp);
472                 M_AST(REG_ITMP3, REG_SP, 2 * 8);
473
474                 /* move data segment displacement onto stack */
475
476                 disp = dseg_adds4(cd, pref->disp);
477                 M_ILD(REG_ITMP3, REG_PV, disp);
478                 M_IST(REG_ITMP3, REG_SP, 1 * 8);
479
480                 /* move patcher function pointer onto stack */
481
482                 disp = dseg_addaddress(cd, pref->patcher);
483                 M_ALD(REG_ITMP3, REG_PV, disp);
484                 M_AST(REG_ITMP3, REG_SP, 0 * 8);
485
486                 if (targetdisp == 0) {
487                         targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
488
489                         disp = dseg_addaddress(cd, asm_patcher_wrapper);
490                         M_ALD(REG_ITMP3, REG_PV, disp);
491                         M_JMP(REG_ITMP3);
492                         M_NOP;
493                 }
494                 else {
495                         disp = (((u4 *) cd->mcodebase) + targetdisp) -
496                                 (((u4 *) cd->mcodeptr) + 1);
497
498                         M_BR(disp);
499                         M_NOP;
500                 }
501         }
502 }
503
504
505 /* emit_replacement_stubs ******************************************************
506
507    Generates the code for the replacement stubs.
508
509 *******************************************************************************/
510
511 void emit_replacement_stubs(jitdata *jd)
512 {
513         codegendata *cd;
514         codeinfo    *code;
515         rplpoint    *rplp;
516         u1          *savedmcodeptr;
517         s4           disp;
518         s4           i;
519
520         /* get required compiler data */
521
522         cd   = jd->cd;
523         code = jd->code;
524
525         rplp = code->rplpoints;
526
527         for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
528                 /* check code segment size */
529
530                 MCODECHECK(100);
531
532                 /* note start of stub code */
533
534                 rplp->outcode = (u1 *) (ptrint) (cd->mcodeptr - cd->mcodebase);
535
536                 /* make machine code for patching */
537
538                 savedmcodeptr = cd->mcodeptr;
539                 cd->mcodeptr  = (u1 *) &(rplp->mcode);
540
541                 disp = (ptrint) ((s4 *) rplp->outcode - (s4 *) rplp->pc) - 1;
542
543                 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
544                         *exceptionptr =
545                                 new_internalerror("Jump offset is out of range: %d > +/-%d",
546                                                                   disp, 0x00007fff);
547                         return;
548                 }
549
550                 M_BR(disp);
551                 M_NOP; /* delay slot */
552
553                 cd->mcodeptr = savedmcodeptr;
554
555                 /* create stack frame - 16-byte aligned */
556
557                 M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
558
559                 /* push address of `rplpoint` struct */
560
561                 disp = dseg_addaddress(cd, rplp);
562                 M_ALD(REG_ITMP3, REG_PV, disp);
563                 M_AST(REG_ITMP3, REG_SP, 0 * 8);
564
565                 /* jump to replacement function */
566
567                 disp = dseg_addaddress(cd, asm_replacement_out);
568                 M_ALD(REG_ITMP3, REG_PV, disp);
569                 M_JMP(REG_ITMP3);
570                 M_NOP; /* delay slot */
571         }
572 }
573
574
575 /* emit_verbosecall_enter ******************************************************
576
577    Generates the code for the call trace.
578
579 *******************************************************************************/
580
581 #if !defined(NDEBUG)
582 void emit_verbosecall_enter(jitdata *jd)
583 {
584         methodinfo   *m;
585         codegendata  *cd;
586         registerdata *rd;
587         methoddesc   *md;
588         s4            disp;
589         s4            i, t;
590
591         /* get required compiler data */
592
593         m  = jd->m;
594         cd = jd->cd;
595         rd = jd->rd;
596
597         md = m->parseddesc;
598
599         /* mark trace code */
600
601         M_NOP;
602
603         M_LDA(REG_SP, REG_SP, -(2 + ARG_CNT + TMP_CNT) * 8);
604         M_AST(REG_RA, REG_SP, 1 * 8);
605
606         /* save argument registers */
607
608         for (i = 0; i < INT_ARG_CNT; i++)
609                 M_LST(rd->argintregs[i], REG_SP, (2 + i) * 8);
610
611         for (i = 0; i < FLT_ARG_CNT; i++)
612                 M_DST(rd->argfltregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
613
614         /* save temporary registers for leaf methods */
615
616         if (jd->isleafmethod) {
617                 for (i = 0; i < INT_TMP_CNT; i++)
618                         M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
619
620                 for (i = 0; i < FLT_TMP_CNT; i++)
621                         M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
622         }
623
624         /* load float arguments into integer registers */
625
626         for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
627                 t = md->paramtypes[i].type;
628
629                 if (IS_FLT_DBL_TYPE(t)) {
630                         if (IS_2_WORD_TYPE(t)) {
631                                 M_DST(rd->argfltregs[i], REG_SP, 0 * 8);
632                                 M_LLD(rd->argintregs[i], REG_SP, 0 * 8);
633                         }
634                         else {
635                                 M_FST(rd->argfltregs[i], REG_SP, 0 * 8);
636                                 M_ILD(rd->argintregs[i], REG_SP, 0 * 8);
637                         }
638                 }
639         }
640
641         disp = dseg_addaddress(cd, m);
642         M_ALD(REG_ITMP1, REG_PV, disp);
643         M_AST(REG_ITMP1, REG_SP, 0 * 8);
644         disp = dseg_addaddress(cd, builtin_trace_args);
645         M_ALD(REG_ITMP3, REG_PV, disp);
646         M_JSR(REG_RA, REG_ITMP3);
647         M_NOP;
648
649         /* restore argument registers */
650
651         for (i = 0; i < INT_ARG_CNT; i++)
652                 M_LLD(rd->argintregs[i], REG_SP, (2 + i) * 8);
653
654         for (i = 0; i < FLT_ARG_CNT; i++)
655                 M_DLD(rd->argfltregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
656
657         /* restore temporary registers for leaf methods */
658
659         if (jd->isleafmethod) {
660                 for (i = 0; i < INT_TMP_CNT; i++)
661                         M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
662
663                 for (i = 0; i < FLT_TMP_CNT; i++)
664                         M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
665         }
666
667         M_ALD(REG_RA, REG_SP, 1 * 8);
668         M_LDA(REG_SP, REG_SP, (2 + ARG_CNT + TMP_CNT) * 8);
669
670         /* mark trace code */
671
672         M_NOP;
673 }
674 #endif /* !defined(NDEBUG) */
675
676
677 /* emit_verbosecall_exit *******************************************************
678
679    Generates the code for the call trace.
680
681 *******************************************************************************/
682
683 #if !defined(NDEBUG)
684 void emit_verbosecall_exit(jitdata *jd)
685 {
686         methodinfo   *m;
687         codegendata  *cd;
688         registerdata *rd;
689         s4            disp;
690
691         /* get required compiler data */
692
693         m  = jd->m;
694         cd = jd->cd;
695         rd = jd->rd;
696
697         /* mark trace code */
698
699         M_NOP;
700
701         M_LDA(REG_SP, REG_SP, -4 * 8);              /* keep stack 16-byte aligned */
702         M_LST(REG_RA, REG_SP, 0 * 8);
703
704         M_LST(REG_RESULT, REG_SP, 1 * 8);
705         M_DST(REG_FRESULT, REG_SP, 2 * 8);
706
707         disp = dseg_addaddress(cd, m);
708         M_ALD(rd->argintregs[0], REG_PV, disp);
709
710         M_MOV(REG_RESULT, rd->argintregs[1]);
711         M_DMOV(REG_FRESULT, rd->argfltregs[2]);
712         M_FMOV(REG_FRESULT, rd->argfltregs[3]);
713
714         disp = dseg_addaddress(cd, builtin_displaymethodstop);
715         M_ALD(REG_ITMP3, REG_PV, disp);
716         M_JSR(REG_RA, REG_ITMP3);
717         M_NOP;
718
719         M_DLD(REG_FRESULT, REG_SP, 2 * 8);
720         M_LLD(REG_RESULT, REG_SP, 1 * 8);
721
722         M_LLD(REG_RA, REG_SP, 0 * 8);
723         M_LDA(REG_SP, REG_SP, 4 * 8);
724
725         /* mark trace code */
726
727         M_NOP;
728 }
729 #endif /* !defined(NDEBUG) */
730
731
732 /*
733  * These are local overrides for various environment variables in Emacs.
734  * Please do not remove this and leave it at the end of the file, where
735  * Emacs will automagically detect them.
736  * ---------------------------------------------------------------------
737  * Local variables:
738  * mode: c
739  * indent-tabs-mode: t
740  * c-basic-offset: 4
741  * tab-width: 4
742  * End:
743  * vim:noexpandtab:sw=4:ts=4:
744  */