* src/vm/jit/sparc64/emit.c: emit_{load,store,copy} changed to switch/case for handli...
[cacao.git] / src / vm / jit / sparc64 / emit.c
1 /* src/vm/jit/sparc64/emit.c - Sparc 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 #include "vm/types.h"
32
33 #include "vm/jit/sparc64/codegen.h"
34 #include "vm/jit/sparc64/md-abi.h"
35
36 #include "mm/memory.h"
37
38 #include "vm/stringlocal.h" /* XXX for gen_resolvebranch */
39 #include "vm/jit/abi-asm.h"
40 #include "vm/jit/asmpart.h"
41 #include "vm/builtin.h"
42 #include "vm/jit/dseg.h"
43 #include "vm/jit/emit-common.h"
44 #include "vm/jit/jit.h"
45 #include "vm/jit/replace.h"
46
47 #include "vmcore/options.h"
48
49 /* how to leaf optimization in the emitted stubs?? */
50 #define REG_PV REG_PV_CALLEE
51
52
53 /* emit_load *******************************************************************
54
55    Emits a possible load of an operand.
56
57 *******************************************************************************/
58
59 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
60 {
61         codegendata  *cd;
62         s4            disp;
63         s4            reg;
64
65         /* get required compiler data */
66
67         cd = jd->cd;
68
69         if (src->flags & INMEMORY) {
70                 COUNT_SPILLS;
71
72                 disp = JITSTACK + src->vv.regoff * 8;
73
74                 switch(src->type)
75                 {
76                 case TYPE_INT:
77                 case TYPE_LNG:
78                 case TYPE_ADR:
79                         M_LDX(tempreg, REG_SP, disp);
80                         break;
81                 case TYPE_FLT:
82                 case TYPE_DBL:
83                         M_DLD(tempreg, REG_SP, disp);
84                         break;
85                 default:
86                         vm_abort("emit_load: unknown type %d", src->type);
87                         break;
88                 }
89
90                 reg = tempreg;
91         }
92         else
93                 reg = src->vv.regoff;
94
95         return reg;
96 }
97
98
99 /* emit_store ******************************************************************
100
101    Emits a possible store to variable.
102
103 *******************************************************************************/
104
105 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
106 {
107         codegendata  *cd;
108         s4            disp;
109
110         /* get required compiler data */
111
112         cd = jd->cd;
113
114         if (dst->flags & INMEMORY) {
115                 COUNT_SPILLS;
116
117                 disp = JITSTACK + dst->vv.regoff * 8;                   
118                         
119                 switch(dst->type)
120                 {
121                 case TYPE_INT:
122                 case TYPE_LNG:
123                 case TYPE_ADR:
124                         M_STX(d, REG_SP, disp);
125                         break;
126                 case TYPE_FLT:
127                 case TYPE_DBL:
128                         M_DST(d, REG_SP, disp);
129                         break;
130                 default:
131                         vm_abort("emit_store: unknown type %d", dst->type);
132                         break;
133                 }
134         }
135 }
136
137
138 /* emit_copy *******************************************************************
139
140    Generates a register/memory to register/memory copy.
141
142 *******************************************************************************/
143
144 void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
145 {
146         codegendata  *cd;
147         registerdata *rd;
148         s4            s1, d;
149
150         /* get required compiler data */
151
152         cd = jd->cd;
153         rd = jd->rd;
154
155         if ((src->vv.regoff != dst->vv.regoff) ||
156                 ((src->flags ^ dst->flags) & INMEMORY)) {
157
158                 /* If one of the variables resides in memory, we can eliminate
159                    the register move from/to the temporary register with the
160                    order of getting the destination register and the load. */
161
162                 if (IS_INMEMORY(src->flags)) {
163                         d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
164                         s1 = emit_load(jd, iptr, src, d);
165                 }
166                 else {
167                         s1 = emit_load(jd, iptr, src, REG_IFTMP);
168                         d = codegen_reg_of_var(iptr->opc, dst, s1);
169                 }
170
171                 if (s1 != d) {          
172                         switch(src->type)
173                         {
174                         case TYPE_INT:
175                         case TYPE_LNG:
176                         case TYPE_ADR:
177                                 M_MOV(s1, d);
178                                 break;
179                         case TYPE_FLT:
180                         case TYPE_DBL:
181                                 M_DMOV(s1, d);
182                                 break;
183                         default:
184                                 vm_abort("emit_copy: unknown type %d", src->type);
185                                 break;
186                         }
187                 }
188
189                 emit_store(jd, iptr, dst, d);
190         }
191 }
192
193
194 /* emit_iconst *****************************************************************
195
196    XXX
197
198 *******************************************************************************/
199
200 void emit_iconst(codegendata *cd, s4 d, s4 value)
201 {
202         s4 disp;
203
204         if ((value >= -4096) && (value <= 4095)) {
205                 M_XOR_IMM(REG_ZERO, value, d);
206         } else {
207                 disp = dseg_add_s4(cd, value);
208                 M_ILD(d, REG_PV_CALLEE, disp);
209         }
210 }
211
212
213 /* emit_lconst *****************************************************************
214
215    XXX
216
217 *******************************************************************************/
218
219 void emit_lconst(codegendata *cd, s4 d, s8 value)
220 {
221         s4 disp;
222
223         if ((value >= -4096) && (value <= 4095)) {
224                 M_XOR_IMM(REG_ZERO, value, d);  
225         } else {
226                 disp = dseg_add_s8(cd, value);
227                 M_LDX(d, REG_PV_CALLEE, disp);
228         }
229 }
230
231
232 /* emit_arithmetic_check *******************************************************
233
234    Emit an ArithmeticException check.
235
236 *******************************************************************************/
237
238 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
239 {
240         if (INSTRUCTION_MUST_CHECK(iptr)) {
241                 M_BEQZ(reg, 0);
242                 codegen_add_arithmeticexception_ref(cd);
243                 M_NOP;
244         }
245 }
246
247
248 /* emit_arrayindexoutofbounds_check ********************************************
249
250    Emit an ArrayIndexOutOfBoundsException check.
251
252 *******************************************************************************/
253
254 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
255 {
256         if (INSTRUCTION_MUST_CHECK(iptr)) {
257                 M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
258                 M_CMP(s2, REG_ITMP3);
259                 M_XBUGE(0);
260                 codegen_add_arrayindexoutofboundsexception_ref(cd, s2);
261                 M_NOP;
262         }
263 }
264
265 /* emit_nullpointer_check ******************************************************
266
267    Emit a NullPointerException check.
268
269 *******************************************************************************/
270
271 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
272 {
273         if (INSTRUCTION_MUST_CHECK(iptr)) {
274                 M_BEQZ(reg, 0);
275                 codegen_add_nullpointerexception_ref(cd);
276                 M_NOP;
277         }
278 }
279
280 /* emit_exception_stubs ********************************************************
281
282    Generates the code for the exception stubs.
283
284 *******************************************************************************/
285
286 void emit_exception_stubs(jitdata *jd)
287 {
288 }
289
290 /* emit_patcher_stubs **********************************************************
291
292    Generates the code for the patcher stubs.
293
294 *******************************************************************************/
295
296 void emit_patcher_stubs(jitdata *jd)
297 {
298         codegendata *cd;
299         patchref    *pref;
300         u4           mcode[2];
301         u1          *savedmcodeptr;
302         u1          *tmpmcodeptr;
303         s4           targetdisp;
304         s4           disp;
305
306         /* get required compiler data */
307
308         cd = jd->cd;
309
310         /* generate code patching stub call code */
311
312         targetdisp = 0;
313
314         for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
315                 /* check code segment size */
316
317                 MCODECHECK(100);
318
319                 /* Get machine code which is patched back in later. The
320                    call is 2 instruction words long. */
321
322                 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
323
324                 /* We use 2 loads here as an unaligned 8-byte read on 64-bit
325                    SPARC causes a SIGSEGV */
326
327                 mcode[0] = ((u4 *) tmpmcodeptr)[0];
328                 mcode[1] = ((u4 *) tmpmcodeptr)[1];
329
330                 /* Patch in the call to call the following code (done at
331                    compile time). */
332
333                 savedmcodeptr = cd->mcodeptr;   /* save current mcodeptr          */
334                 cd->mcodeptr  = tmpmcodeptr;    /* set mcodeptr to patch position */
335
336                 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) );
337
338                 if ((disp < (s4) 0xfffc0000) || (disp > (s4) 0x003ffff)) {
339                         vm_abort("Jump offset is out of range: %d > +/-%d",
340                                          disp, 0x003ffff);
341                         return;
342                 }
343
344                 M_BR(disp);
345                 M_NOP;
346
347                 cd->mcodeptr = savedmcodeptr;   /* restore the current mcodeptr   */
348
349                 /* extend stack frame for wrapper data */
350
351                 M_ASUB_IMM(REG_SP, 6 * 8, REG_SP);
352
353                 /* calculate return address and move it onto the stack */
354
355                 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
356                 M_AST(REG_ITMP3, REG_SP, JITSTACK + 5 * 8);
357
358                 /* move pointer to java_objectheader onto stack */
359
360 #if defined(ENABLE_THREADS)
361                 /* create a virtual java_objectheader */
362
363                 (void) dseg_add_unique_address(cd, NULL);                  /* flcword */
364                 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
365                 disp = dseg_add_unique_address(cd, NULL);                  /* vftbl   */
366
367                 M_LDA(REG_ITMP3, REG_PV, disp);
368                 M_AST(REG_ITMP3, REG_SP, JITSTACK + 4 * 8);
369 #else
370                 /* do nothing */
371 #endif
372
373                 /* move machine code onto stack */
374
375                 disp = dseg_add_s4(cd, mcode[0]);
376                 M_ILD(REG_ITMP3, REG_PV, disp);
377                 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8);
378
379                 disp = dseg_add_s4(cd, mcode[1]);
380                 M_ILD(REG_ITMP3, REG_PV, disp);
381                 M_IST(REG_ITMP3, REG_SP, JITSTACK + 3 * 8 + 4);
382
383                 /* move class/method/field reference onto stack */
384
385                 disp = dseg_add_address(cd, pref->ref);
386                 M_ALD(REG_ITMP3, REG_PV, disp);
387                 M_AST(REG_ITMP3, REG_SP, JITSTACK + 2 * 8);
388
389         /* move data segment displacement onto stack */
390
391                 disp = dseg_add_s4(cd, pref->disp);
392                 M_ILD(REG_ITMP3, REG_PV, disp);
393                 M_IST(REG_ITMP3, REG_SP, JITSTACK + 1 * 8);
394
395                 /* move patcher function pointer onto stack */
396
397                 disp = dseg_add_functionptr(cd, pref->patcher);
398                 M_ALD(REG_ITMP3, REG_PV, disp);
399                 M_AST(REG_ITMP3, REG_SP, JITSTACK + 0 * 8);
400
401                 if (targetdisp == 0) {
402                         targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
403
404                         disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
405                         M_ALD(REG_ITMP3, REG_PV, disp);
406                         M_JMP(REG_ZERO, REG_ITMP3, REG_ZERO);
407                         M_NOP;
408                 }
409                 else {
410                         disp = (((u4 *) cd->mcodebase) + targetdisp) -
411                                 (((u4 *) cd->mcodeptr));
412
413                         M_BR(disp);
414                         M_NOP;
415                 }
416         }
417 }
418
419
420 /* emit_replacement_stubs ******************************************************
421
422    Generates the code for the replacement stubs.
423
424 *******************************************************************************/
425
426 #if defined(ENABLE_REPLACEMENT)
427 void emit_replacement_stubs(jitdata *jd)
428 {
429 }
430 #endif /* defined(ENABLE_REPLACEMENT) */
431
432 /* emit_verbosecall_enter ******************************************************
433
434    Generates the code for the call trace.
435
436 *******************************************************************************/
437
438 #if !defined(NDEBUG)
439 void emit_verbosecall_enter(jitdata *jd)
440 {
441         methodinfo   *m;
442         codegendata  *cd;
443         registerdata *rd;
444         methoddesc   *md;
445         s4            disp;
446         s4            i, t;
447
448         /* get required compiler data */
449
450         m  = jd->m;
451         cd = jd->cd;
452         rd = jd->rd;
453
454         md = m->parseddesc;
455
456         /* mark trace code */
457
458         M_NOP;
459
460         /* XXX jit-c-call */
461         M_LDA(REG_SP, REG_SP, -(1 + FLT_ARG_CNT) * 8);
462
463         /* save float argument registers */
464
465         for (i = 0; i < FLT_ARG_CNT; i++)
466                 M_DST(rd->argfltregs[i], REG_SP, JITSTACK + (1 + i) * 8);
467
468         /* save temporary registers for leaf methods */
469 /* XXX no leaf optimization yet
470         if (jd->isleafmethod) {
471                 for (i = 0; i < INT_TMP_CNT; i++)
472                         M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
473
474                 for (i = 0; i < FLT_TMP_CNT; i++)
475                         M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
476         }
477 */
478         /* load int/float arguments into integer argument registers */
479
480         for (i = 0; i < md->paramcount && i < INT_NATARG_CNT; i++) {
481                 t = md->paramtypes[i].type;
482
483                 /* using all available argument registers, this adds complexity */
484                 
485                 if (IS_INT_LNG_TYPE(t)) {
486                         if (i < INT_ARG_CNT) {
487                                 M_INTMOVE(REG_WINDOW_TRANSPOSE(rd->argintregs[i]), rd->argintregs[i]);
488                         }
489                         else {
490                                 assert(i == 5);
491                                 M_LDX(REG_OUT5, REG_FP, JITSTACK);
492                         }
493                 }
494                 else {
495                         assert(i < 4); /* XXX only 4 float reg args right now! */
496                         if (IS_2_WORD_TYPE(t)) {
497                                 M_DST(rd->argfltregs[i], REG_SP, JITSTACK);
498                                 M_LDX(rd->argintregs[i], REG_SP, JITSTACK);
499                         }
500                         else {
501                                 M_FST(rd->argfltregs[i], REG_SP, JITSTACK);
502                                 M_ILD(rd->argintregs[i], REG_SP, JITSTACK);
503                         }
504                 }
505         }
506         
507         
508         /* method info pointer is passed in argument register 5 */
509         disp = dseg_add_address(cd, m);
510         M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
511         M_AST(REG_ITMP1, REG_SP, CSTACK);
512         disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
513         M_ALD(REG_ITMP1, REG_PV_CALLEE, disp);
514         M_JMP(REG_RA_CALLER, REG_ITMP1, REG_ZERO);
515         M_NOP;
516
517         /* restore float argument registers */
518
519         for (i = 0; i < FLT_ARG_CNT; i++)
520                 M_DLD(rd->argfltregs[i], REG_SP, JITSTACK + (1 + i) * 8);
521
522         /* restore temporary registers for leaf methods */
523 /* XXX no leaf optimization yet
524         if (jd->isleafmethod) {
525                 for (i = 0; i < INT_TMP_CNT; i++)
526                         M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
527
528                 for (i = 0; i < FLT_TMP_CNT; i++)
529                         M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
530         }
531 */
532         M_LDA(REG_SP, REG_SP, (1 + FLT_ARG_CNT) * 8);
533
534         /* mark trace code */
535
536         M_NOP;
537 }
538 #endif /* !defined(NDEBUG) */
539
540
541 /* emit_verbosecall_exit *******************************************************
542
543    Generates the code for the call trace.
544
545 *******************************************************************************/
546
547 #if !defined(NDEBUG)
548 void emit_verbosecall_exit(jitdata *jd)
549 {
550         methodinfo   *m;
551         codegendata  *cd;
552         registerdata *rd;
553         s4            disp;
554
555         /* get required compiler data */
556
557         m  = jd->m;
558         cd = jd->cd;
559         rd = jd->rd;
560
561         /* mark trace code */
562
563         M_NOP;
564         
565         /* XXX jit-c-call */
566         M_LDA(REG_SP, REG_SP, -(1 * 8));
567
568         M_DST(REG_FRESULT, REG_SP, JITSTACK);
569
570         M_MOV(REG_RESULT_CALLEE, rd->argintregs[0]);
571         M_DMOV(REG_FRESULT, 1); /* logical dreg 1 => f2 */
572         M_FMOV(REG_FRESULT, 2); /* logical freg 2 => f5 */
573
574         disp = dseg_add_functionptr(cd, m);
575         M_ALD(rd->argintregs[3], REG_PV_CALLEE, disp);
576
577         disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
578         M_ALD(REG_ITMP3, REG_PV_CALLEE, disp);
579         M_JMP(REG_RA_CALLER, REG_ITMP3, REG_ZERO);
580         M_NOP;
581
582         M_DLD(REG_FRESULT, REG_SP, JITSTACK);
583
584         M_LDA(REG_SP, REG_SP, 1 * 8);
585
586         /* mark trace code */
587
588         M_NOP;
589 }
590 #endif /* !defined(NDEBUG) */
591
592
593 /*
594  * These are local overrides for various environment variables in Emacs.
595  * Please do not remove this and leave it at the end of the file, where
596  * Emacs will automagically detect them.
597  * ---------------------------------------------------------------------
598  * Local variables:
599  * mode: c
600  * indent-tabs-mode: t
601  * c-basic-offset: 4
602  * tab-width: 4
603  * End:
604  * vim:noexpandtab:sw=4:ts=4:
605  */