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