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