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