* src/vm/jit/mips/emit.c (emit_copy): Fixed arguments to
[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, 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                 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->vv.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, varinfo *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->vv.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, varinfo *src, varinfo *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->vv.regoff != dst->vv.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(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(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], REG_SP, (cd->stackframesize - 1) * 8);
275
276                         M_MOV(REG_ITMP2_XPC, rd->argintregs[3]);
277                         M_MOV(REG_ITMP1, rd->argintregs[4]);
278
279                         M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
280                         M_AST(REG_ITMP2_XPC, REG_SP, 0 * 8);
281
282                         if (jd->isleafmethod)
283                                 M_AST(REG_RA, REG_SP, 1 * 8);
284
285                         M_JSR(REG_RA, REG_ITMP3);
286                         M_NOP;
287                         M_MOV(REG_RESULT, REG_ITMP1_XPTR);
288
289                         if (jd->isleafmethod)
290                                 M_ALD(REG_RA, REG_SP, 1 * 8);
291
292                         M_ALD(REG_ITMP2_XPC, REG_SP, 0 * 8);
293                         M_AADD_IMM(REG_SP, 2 * 8, REG_SP);
294
295                         disp = dseg_addaddress(cd, asm_handle_exception);
296                         M_ALD(REG_ITMP3, REG_PV, disp);
297                         M_JMP(REG_ITMP3);
298                         M_NOP;
299                 }
300                 else {
301                         disp = (((u4 *) cd->mcodebase) + targetdisp) -
302                                 (((u4 *) cd->mcodeptr) + 1);
303
304                         M_BR(disp);
305                         M_NOP;
306                 }
307         }
308 }
309
310
311 /* emit_patcher_stubs **********************************************************
312
313    Generates the code for the patcher stubs.
314
315 *******************************************************************************/
316
317 void emit_patcher_stubs(jitdata *jd)
318 {
319         codegendata *cd;
320         patchref    *pref;
321         u4           mcode[2];
322         u1          *savedmcodeptr;
323         u1          *tmpmcodeptr;
324         s4           targetdisp;
325         s4           disp;
326
327         /* get required compiler data */
328
329         cd = jd->cd;
330
331         /* generate code patching stub call code */
332
333         targetdisp = 0;
334
335         for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
336                 /* check code segment size */
337
338                 MCODECHECK(100);
339
340                 /* Get machine code which is patched back in later. The
341                    call is 2 instruction words long. */
342
343                 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
344
345                 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
346                    MIPS causes a SIGSEGV and using the same code for both
347                    architectures is much better. */
348
349                 mcode[0] = ((u4 *) tmpmcodeptr)[0];
350                 mcode[1] = ((u4 *) tmpmcodeptr)[1];
351
352                 /* Patch in the call to call the following code (done at
353                    compile time). */
354
355                 savedmcodeptr = cd->mcodeptr;   /* save current mcodeptr          */
356                 cd->mcodeptr  = tmpmcodeptr;    /* set mcodeptr to patch position */
357
358                 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
359
360                 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
361                         *exceptionptr =
362                                 new_internalerror("Jump offset is out of range: %d > +/-%d",
363                                                                   disp, 0x00007fff);
364                         return;
365                 }
366
367                 M_BR(disp);
368                 M_NOP;
369
370                 cd->mcodeptr = savedmcodeptr;   /* restore the current mcodeptr   */
371
372                 /* create stack frame */
373
374                 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
375
376                 /* calculate return address and move it onto the stack */
377
378                 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
379                 M_AST(REG_ITMP3, REG_SP, 5 * 8);
380
381                 /* move pointer to java_objectheader onto stack */
382
383 #if defined(ENABLE_THREADS)
384                 /* create a virtual java_objectheader */
385
386                 (void) dseg_addaddress(cd, NULL);                          /* flcword */
387                 (void) dseg_addaddress(cd, lock_get_initial_lock_word());
388                 disp = dseg_addaddress(cd, NULL);                          /* vftbl   */
389
390                 M_LDA(REG_ITMP3, REG_PV, disp);
391                 M_AST(REG_ITMP3, REG_SP, 4 * 8);
392 #else
393                 /* do nothing */
394 #endif
395
396                 /* move machine code onto stack */
397
398                 disp = dseg_adds4(cd, mcode[0]);
399                 M_ILD(REG_ITMP3, REG_PV, disp);
400                 M_IST(REG_ITMP3, REG_SP, 3 * 8);
401
402                 disp = dseg_adds4(cd, mcode[1]);
403                 M_ILD(REG_ITMP3, REG_PV, disp);
404                 M_IST(REG_ITMP3, REG_SP, 3 * 8 + 4);
405
406                 /* move class/method/field reference onto stack */
407
408                 disp = dseg_addaddress(cd, pref->ref);
409                 M_ALD(REG_ITMP3, REG_PV, disp);
410                 M_AST(REG_ITMP3, REG_SP, 2 * 8);
411
412                 /* move data segment displacement onto stack */
413
414                 disp = dseg_adds4(cd, pref->disp);
415                 M_ILD(REG_ITMP3, REG_PV, disp);
416                 M_IST(REG_ITMP3, REG_SP, 1 * 8);
417
418                 /* move patcher function pointer onto stack */
419
420                 disp = dseg_addaddress(cd, pref->patcher);
421                 M_ALD(REG_ITMP3, REG_PV, disp);
422                 M_AST(REG_ITMP3, REG_SP, 0 * 8);
423
424                 if (targetdisp == 0) {
425                         targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
426
427                         disp = dseg_addaddress(cd, asm_patcher_wrapper);
428                         M_ALD(REG_ITMP3, REG_PV, disp);
429                         M_JMP(REG_ITMP3);
430                         M_NOP;
431                 }
432                 else {
433                         disp = (((u4 *) cd->mcodebase) + targetdisp) -
434                                 (((u4 *) cd->mcodeptr) + 1);
435
436                         M_BR(disp);
437                         M_NOP;
438                 }
439         }
440 }
441
442
443 /* emit_replacement_stubs ******************************************************
444
445    Generates the code for the replacement stubs.
446
447 *******************************************************************************/
448
449 void emit_replacement_stubs(jitdata *jd)
450 {
451         codegendata *cd;
452         codeinfo    *code;
453         rplpoint    *rplp;
454         u1          *savedmcodeptr;
455         s4           disp;
456         s4           i;
457
458         /* get required compiler data */
459
460         cd   = jd->cd;
461         code = jd->code;
462
463         rplp = code->rplpoints;
464
465         for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
466                 /* check code segment size */
467
468                 MCODECHECK(100);
469
470                 /* note start of stub code */
471
472                 rplp->outcode = (u1 *) (ptrint) (cd->mcodeptr - cd->mcodebase);
473
474                 /* make machine code for patching */
475
476                 savedmcodeptr = cd->mcodeptr;
477                 cd->mcodeptr  = (u1 *) &(rplp->mcode);
478
479                 disp = (ptrint) ((s4 *) rplp->outcode - (s4 *) rplp->pc) - 1;
480
481                 if ((disp < (s4) 0xffff8000) || (disp > (s4) 0x00007fff)) {
482                         *exceptionptr =
483                                 new_internalerror("Jump offset is out of range: %d > +/-%d",
484                                                                   disp, 0x00007fff);
485                         return;
486                 }
487
488                 M_BR(disp);
489                 M_NOP; /* delay slot */
490
491                 cd->mcodeptr = savedmcodeptr;
492
493                 /* create stack frame - 16-byte aligned */
494
495                 M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
496
497                 /* push address of `rplpoint` struct */
498
499                 disp = dseg_addaddress(cd, rplp);
500                 M_ALD(REG_ITMP3, REG_PV, disp);
501                 M_AST(REG_ITMP3, REG_SP, 0 * 8);
502
503                 /* jump to replacement function */
504
505                 disp = dseg_addaddress(cd, asm_replacement_out);
506                 M_ALD(REG_ITMP3, REG_PV, disp);
507                 M_JMP(REG_ITMP3);
508                 M_NOP; /* delay slot */
509         }
510 }
511
512
513 /* emit_verbosecall_enter ******************************************************
514
515    Generates the code for the call trace.
516
517 *******************************************************************************/
518
519 #if !defined(NDEBUG)
520 void emit_verbosecall_enter(jitdata *jd)
521 {
522         methodinfo   *m;
523         codegendata  *cd;
524         registerdata *rd;
525         methoddesc   *md;
526         s4            disp;
527         s4            i, t;
528
529         /* get required compiler data */
530
531         m  = jd->m;
532         cd = jd->cd;
533         rd = jd->rd;
534
535         md = m->parseddesc;
536
537         /* mark trace code */
538
539         M_NOP;
540
541         M_LDA(REG_SP, REG_SP, -(2 + ARG_CNT + TMP_CNT) * 8);
542         M_AST(REG_RA, REG_SP, 1 * 8);
543
544         /* save argument registers */
545
546         for (i = 0; i < INT_ARG_CNT; i++)
547                 M_LST(rd->argintregs[i], REG_SP, (2 + i) * 8);
548
549         for (i = 0; i < FLT_ARG_CNT; i++)
550                 M_DST(rd->argfltregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
551
552         /* save temporary registers for leaf methods */
553
554         if (jd->isleafmethod) {
555                 for (i = 0; i < INT_TMP_CNT; i++)
556                         M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
557
558                 for (i = 0; i < FLT_TMP_CNT; i++)
559                         M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
560         }
561
562         /* load float arguments into integer registers */
563
564         for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
565                 t = md->paramtypes[i].type;
566
567                 if (IS_FLT_DBL_TYPE(t)) {
568                         if (IS_2_WORD_TYPE(t)) {
569                                 M_DST(rd->argfltregs[i], REG_SP, 0 * 8);
570                                 M_LLD(rd->argintregs[i], REG_SP, 0 * 8);
571                         }
572                         else {
573                                 M_FST(rd->argfltregs[i], REG_SP, 0 * 8);
574                                 M_ILD(rd->argintregs[i], REG_SP, 0 * 8);
575                         }
576                 }
577         }
578
579         disp = dseg_addaddress(cd, m);
580         M_ALD(REG_ITMP1, REG_PV, disp);
581         M_AST(REG_ITMP1, REG_SP, 0 * 8);
582         disp = dseg_addaddress(cd, builtin_trace_args);
583         M_ALD(REG_ITMP3, REG_PV, disp);
584         M_JSR(REG_RA, REG_ITMP3);
585         M_NOP;
586
587         /* restore argument registers */
588
589         for (i = 0; i < INT_ARG_CNT; i++)
590                 M_LLD(rd->argintregs[i], REG_SP, (2 + i) * 8);
591
592         for (i = 0; i < FLT_ARG_CNT; i++)
593                 M_DLD(rd->argfltregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
594
595         /* restore temporary registers for leaf methods */
596
597         if (jd->isleafmethod) {
598                 for (i = 0; i < INT_TMP_CNT; i++)
599                         M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
600
601                 for (i = 0; i < FLT_TMP_CNT; i++)
602                         M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
603         }
604
605         M_ALD(REG_RA, REG_SP, 1 * 8);
606         M_LDA(REG_SP, REG_SP, (2 + ARG_CNT + TMP_CNT) * 8);
607
608         /* mark trace code */
609
610         M_NOP;
611 }
612 #endif /* !defined(NDEBUG) */
613
614
615 /* emit_verbosecall_exit *******************************************************
616
617    Generates the code for the call trace.
618
619 *******************************************************************************/
620
621 #if !defined(NDEBUG)
622 void emit_verbosecall_exit(jitdata *jd)
623 {
624         methodinfo   *m;
625         codegendata  *cd;
626         registerdata *rd;
627         s4            disp;
628
629         /* get required compiler data */
630
631         m  = jd->m;
632         cd = jd->cd;
633         rd = jd->rd;
634
635         /* mark trace code */
636
637         M_NOP;
638
639         M_LDA(REG_SP, REG_SP, -4 * 8);              /* keep stack 16-byte aligned */
640         M_LST(REG_RA, REG_SP, 0 * 8);
641
642         M_LST(REG_RESULT, REG_SP, 1 * 8);
643         M_DST(REG_FRESULT, REG_SP, 2 * 8);
644
645         disp = dseg_addaddress(cd, m);
646         M_ALD(rd->argintregs[0], REG_PV, disp);
647
648         M_MOV(REG_RESULT, rd->argintregs[1]);
649         M_DMOV(REG_FRESULT, rd->argfltregs[2]);
650         M_FMOV(REG_FRESULT, rd->argfltregs[3]);
651
652         disp = dseg_addaddress(cd, builtin_displaymethodstop);
653         M_ALD(REG_ITMP3, REG_PV, disp);
654         M_JSR(REG_RA, REG_ITMP3);
655         M_NOP;
656
657         M_DLD(REG_FRESULT, REG_SP, 2 * 8);
658         M_LLD(REG_RESULT, REG_SP, 1 * 8);
659
660         M_LLD(REG_RA, REG_SP, 0 * 8);
661         M_LDA(REG_SP, REG_SP, 4 * 8);
662
663         /* mark trace code */
664
665         M_NOP;
666 }
667 #endif /* !defined(NDEBUG) */
668
669
670 /*
671  * These are local overrides for various environment variables in Emacs.
672  * Please do not remove this and leave it at the end of the file, where
673  * Emacs will automagically detect them.
674  * ---------------------------------------------------------------------
675  * Local variables:
676  * mode: c
677  * indent-tabs-mode: t
678  * c-basic-offset: 4
679  * tab-width: 4
680  * End:
681  * vim:noexpandtab:sw=4:ts=4:
682  */