* src/vm/jit/alpha/emit.c (emit_verbosecall_enter): Adapted to use new tracer.
[cacao.git] / src / vm / jit / alpha / emit.c
1 /* src/vm/jit/alpha/emit.c - Alpha 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 */
26
27
28 #include "config.h"
29 #include "vm/types.h"
30
31 #include <assert.h>
32 #include <stdint.h>
33
34 #include "md-abi.h"
35
36 #include "vm/jit/alpha/codegen.h"
37
38 #include "mm/memory.h"
39
40 #include "threads/lock-common.h"
41
42 #include "vm/exceptions.h"
43
44 #include "vm/jit/abi.h"
45 #include "vm/jit/abi-asm.h"
46 #include "vm/jit/asmpart.h"
47 #include "vm/jit/dseg.h"
48 #include "vm/jit/emit-common.h"
49 #include "vm/jit/jit.h"
50 #include "vm/jit/patcher-common.h"
51 #include "vm/jit/replace.h"
52 #include "vm/jit/trace.h"
53
54 #include "vmcore/options.h"
55
56
57 /* emit_load *******************************************************************
58
59    Emits a possible load of an operand.
60
61 *******************************************************************************/
62
63 s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
64 {
65         codegendata  *cd;
66         s4            disp;
67         s4            reg;
68
69         /* get required compiler data */
70
71         cd = jd->cd;
72
73         if (IS_INMEMORY(src->flags)) {
74                 COUNT_SPILLS;
75
76                 disp = src->vv.regoff;
77
78                 switch (src->type) {
79                 case TYPE_INT:
80                 case TYPE_LNG:
81                 case TYPE_ADR:
82                         M_LLD(tempreg, REG_SP, disp);
83                         break;
84                 case TYPE_FLT:
85                 case TYPE_DBL:
86                         M_DLD(tempreg, REG_SP, disp);
87                         break;
88                 default:
89                         vm_abort("emit_load: unknown type %d", src->type);
90                 }
91
92                 reg = tempreg;
93         }
94         else
95                 reg = src->vv.regoff;
96
97         return reg;
98 }
99
100
101 /* emit_store ******************************************************************
102
103    Emit a possible store for the given variable.
104
105 *******************************************************************************/
106
107 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
108 {
109         codegendata  *cd;
110         s4            disp;
111
112         /* get required compiler data */
113
114         cd = jd->cd;
115
116         if (IS_INMEMORY(dst->flags)) {
117                 COUNT_SPILLS;
118
119                 disp = dst->vv.regoff;
120
121                 switch (dst->type) {
122                 case TYPE_INT:
123                 case TYPE_LNG:
124                 case TYPE_ADR:
125                         M_LST(d, REG_SP, disp);
126                         break;
127                 case TYPE_FLT:
128                 case TYPE_DBL:
129                         M_DST(d, REG_SP, disp);
130                         break;
131                 default:
132                         vm_abort("emit_store: unknown type %d", dst->type);
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)
145 {
146         codegendata *cd;
147         varinfo     *src;
148         varinfo     *dst;
149         s4           s1, d;
150
151         /* get required compiler data */
152
153         cd = jd->cd;
154
155         /* get source and destination variables */
156
157         src = VAROP(iptr->s1);
158         dst = VAROP(iptr->dst);
159
160         if ((src->vv.regoff != dst->vv.regoff) ||
161                 ((src->flags ^ dst->flags) & INMEMORY)) {
162
163                 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
164                         /* emit nothing, as the value won't be used anyway */
165                         return;
166                 }
167
168                 /* If one of the variables resides in memory, we can eliminate
169                    the register move from/to the temporary register with the
170                    order of getting the destination register and the load. */
171
172                 if (IS_INMEMORY(src->flags)) {
173                         d  = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
174                         s1 = emit_load(jd, iptr, src, d);
175                 }
176                 else {
177                         s1 = emit_load(jd, iptr, src, REG_IFTMP);
178                         d  = codegen_reg_of_var(iptr->opc, dst, s1);
179                 }
180
181                 if (s1 != d) {
182                         switch (src->type) {
183                         case TYPE_INT:
184                         case TYPE_LNG:
185                         case TYPE_ADR:
186                                 M_MOV(s1, d);
187                                 break;
188                         case TYPE_FLT:
189                         case TYPE_DBL:
190                                 M_FMOV(s1, d);
191                                 break;
192                         default:
193                                 vm_abort("emit_copy: unknown type %d", src->type);
194                         }
195                 }
196
197                 emit_store(jd, iptr, dst, d);
198         }
199 }
200
201
202 /* emit_iconst *****************************************************************
203
204    XXX
205
206 *******************************************************************************/
207
208 void emit_iconst(codegendata *cd, s4 d, s4 value)
209 {
210         s4 disp;
211
212         if ((value >= -32768) && (value <= 32767))
213                 M_LDA_INTERN(d, REG_ZERO, value);
214         else {
215                 disp = dseg_add_s4(cd, value);
216                 M_ILD(d, REG_PV, disp);
217         }
218 }
219
220
221 /* emit_lconst *****************************************************************
222
223    XXX
224
225 *******************************************************************************/
226
227 void emit_lconst(codegendata *cd, s4 d, s8 value)
228 {
229         s4 disp;
230
231         if ((value >= -32768) && (value <= 32767))
232                 M_LDA_INTERN(d, REG_ZERO, value);
233         else {
234                 disp = dseg_add_s8(cd, value);
235                 M_LLD(d, REG_PV, disp);
236         }
237 }
238
239
240 /* emit_branch *****************************************************************
241
242    Emits the code for conditional and unconditional branchs.
243
244 *******************************************************************************/
245
246 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
247 {
248         s4 checkdisp;
249         s4 branchdisp;
250
251         /* calculate the different displacements */
252
253         checkdisp  = (disp - 4);
254         branchdisp = (disp - 4) >> 2;
255
256         /* check which branch to generate */
257
258         if (condition == BRANCH_UNCONDITIONAL) {
259                 /* check displacement for overflow */
260
261                 if ((checkdisp < (s4) 0xffe00000) || (checkdisp > (s4) 0x001fffff)) {
262                         /* if the long-branches flag isn't set yet, do it */
263
264                         if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
265                                 log_println("setting error");
266                                 cd->flags |= (CODEGENDATA_FLAG_ERROR |
267                                                           CODEGENDATA_FLAG_LONGBRANCHES);
268                         }
269
270                         vm_abort("emit_branch: emit unconditional long-branch code");
271                 }
272                 else {
273                         M_BR(branchdisp);
274                 }
275         }
276         else {
277                 /* and displacement for overflow */
278
279                 if ((checkdisp < (s4) 0xffe00000) || (checkdisp > (s4) 0x001fffff)) {
280                         /* if the long-branches flag isn't set yet, do it */
281
282                         if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
283                                 log_println("setting error");
284                                 cd->flags |= (CODEGENDATA_FLAG_ERROR |
285                                                           CODEGENDATA_FLAG_LONGBRANCHES);
286                         }
287
288                         vm_abort("emit_branch: emit conditional long-branch code");
289                 }
290                 else {
291                         switch (condition) {
292                         case BRANCH_EQ:
293                                 M_BEQZ(reg, branchdisp);
294                                 break;
295                         case BRANCH_NE:
296                                 M_BNEZ(reg, branchdisp);
297                                 break;
298                         case BRANCH_LT:
299                                 M_BLTZ(reg, branchdisp);
300                                 break;
301                         case BRANCH_GE:
302                                 M_BGEZ(reg, branchdisp);
303                                 break;
304                         case BRANCH_GT:
305                                 M_BGTZ(reg, branchdisp);
306                                 break;
307                         case BRANCH_LE:
308                                 M_BLEZ(reg, branchdisp);
309                                 break;
310                         default:
311                                 vm_abort("emit_branch: unknown condition %d", condition);
312                         }
313                 }
314         }
315 }
316
317
318 /* emit_arithmetic_check *******************************************************
319
320    Emit an ArithmeticException check.
321
322 *******************************************************************************/
323
324 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
325 {
326         if (INSTRUCTION_MUST_CHECK(iptr)) {
327                 M_BNEZ(reg, 1);
328                 /* Destination register must not be REG_ZERO, because then no
329                    SIGSEGV is thrown. */
330                 M_ALD_INTERN(reg, REG_ZERO, EXCEPTION_HARDWARE_ARITHMETIC);
331         }
332 }
333
334
335 /* emit_arrayindexoutofbounds_check ********************************************
336
337    Emit an ArrayIndexOutOfBoundsException check.
338
339 *******************************************************************************/
340
341 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
342 {
343         if (INSTRUCTION_MUST_CHECK(iptr)) {
344                 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
345                 M_CMPULT(s2, REG_ITMP3, REG_ITMP3);
346                 M_BNEZ(REG_ITMP3, 1);
347                 M_ALD_INTERN(s2, REG_ZERO, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
348         }
349 }
350
351
352 /* emit_arraystore_check *******************************************************
353
354    Emit an ArrayStoreException check.
355
356 *******************************************************************************/
357
358 void emit_arraystore_check(codegendata *cd, instruction *iptr)
359 {
360         if (INSTRUCTION_MUST_CHECK(iptr)) {
361                 M_BNEZ(REG_RESULT, 1);
362                 /* Destination register must not be REG_ZERO, because then no
363                    SIGSEGV is thrown. */
364                 M_ALD_INTERN(REG_RESULT, REG_ZERO, EXCEPTION_HARDWARE_ARRAYSTORE);
365         }
366 }
367
368
369 /* emit_classcast_check ********************************************************
370
371    Emit a ClassCastException check.
372
373 *******************************************************************************/
374
375 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
376 {
377         if (INSTRUCTION_MUST_CHECK(iptr)) {
378                 switch (condition) {
379                 case BRANCH_EQ:
380                         M_BNEZ(reg, 1);
381                         break;
382                 case BRANCH_LE:
383                         M_BGTZ(reg, 1);
384                         break;
385                 default:
386                         vm_abort("emit_classcast_check: unknown condition %d", condition);
387                 }
388                 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
389         }
390 }
391
392
393 /* emit_nullpointer_check ******************************************************
394
395    Emit a NullPointerException check.
396
397 *******************************************************************************/
398
399 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
400 {
401         if (INSTRUCTION_MUST_CHECK(iptr)) {
402                 M_BNEZ(reg, 1);
403                 /* Destination register must not be REG_ZERO, because then no
404                    SIGSEGV is thrown. */
405                 M_ALD_INTERN(reg, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
406         }
407 }
408
409
410 /* emit_exception_check ********************************************************
411
412    Emit an Exception check.
413
414 *******************************************************************************/
415
416 void emit_exception_check(codegendata *cd, instruction *iptr)
417 {
418         if (INSTRUCTION_MUST_CHECK(iptr)) {
419                 M_BNEZ(REG_RESULT, 1);
420                 /* Destination register must not be REG_ZERO, because then no
421                    SIGSEGV is thrown. */
422                 M_ALD_INTERN(REG_RESULT, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
423         }
424 }
425
426
427 /* emit_trap_compiler **********************************************************
428
429    Emit a trap instruction which calls the JIT compiler.
430
431 *******************************************************************************/
432
433 void emit_trap_compiler(codegendata *cd)
434 {
435         M_ALD_INTERN(REG_METHODPTR, REG_ZERO, EXCEPTION_HARDWARE_COMPILER);
436 }
437
438
439 /* emit_trap *******************************************************************
440
441    Emit a trap instruction and return the original machine code.
442
443 *******************************************************************************/
444
445 uint32_t emit_trap(codegendata *cd)
446 {
447         uint32_t mcode;
448
449         /* Get machine code which is patched back in later. The
450            trap is 1 instruction word long. */
451
452         mcode = *((u4 *) cd->mcodeptr);
453
454         /* Destination register must not be REG_ZERO, because then no
455            SIGSEGV is thrown. */
456         M_ALD_INTERN(REG_RESULT, REG_ZERO, EXCEPTION_HARDWARE_PATCHER);
457
458         return mcode;
459 }
460
461
462 /* emit_verbosecall_enter ******************************************************
463
464    Generates the code for the call trace.
465
466 *******************************************************************************/
467
468 #if !defined(NDEBUG)
469 void emit_verbosecall_enter(jitdata *jd)
470 {
471         methodinfo   *m;
472         codegendata  *cd;
473         registerdata *rd;
474         methoddesc   *md;
475         int32_t       stackframesize;
476         s4            disp;
477         s4            i, j, s;
478
479         /* get required compiler data */
480
481         m  = jd->m;
482         cd = jd->cd;
483         rd = jd->rd;
484
485         md = m->parseddesc;
486
487         /* mark trace code */
488
489         M_NOP;
490
491         stackframesize = ARG_CNT + TMP_CNT + md->paramcount + 1;
492
493         M_LDA(REG_SP, REG_SP, -(stackframesize * 8));
494         M_AST(REG_RA, REG_SP, 0 * 8);
495
496         /* save all argument and temporary registers for leaf methods */
497
498         if (jd->isleafmethod) {
499                 j = 1 + md->paramcount;
500
501                 for (i = 0; i < INT_ARG_CNT; i++, j++)
502                         M_LST(abi_registers_integer_argument[i], REG_SP, j * 8);
503
504                 for (i = 0; i < FLT_ARG_CNT; i++, j++)
505                         M_DST(abi_registers_float_argument[i], REG_SP, j * 8);
506
507                 for (i = 0; i < INT_TMP_CNT; i++, j++)
508                         M_LST(rd->tmpintregs[i], REG_SP, j * 8);
509
510                 for (i = 0; i < FLT_TMP_CNT; i++, j++)
511                         M_DST(rd->tmpfltregs[i], REG_SP, j * 8);
512         }
513
514         /* save argument registers */
515
516         for (i = 0; i < md->paramcount; i++) {
517                 if (!md->params[i].inmemory) {
518                         s = md->params[i].regoff;
519
520                         switch (md->paramtypes[i].type) {
521                         case TYPE_ADR:
522                         case TYPE_INT:
523                         case TYPE_LNG:
524                                 M_LST(s, REG_SP, (1 + i) * 8);
525                                 break;
526                         case TYPE_FLT:
527                         case TYPE_DBL:
528                                 M_DST(s, REG_SP, (1 + i) * 8);
529                                 break;
530                         }
531                 }
532         }
533
534         disp = dseg_add_address(cd, m);
535         M_ALD(REG_A0, REG_PV, disp);
536         M_AADD_IMM(REG_SP, 1 * 8, REG_A1);
537         M_LDA(REG_A2, REG_SP, stackframesize * 8 + cd->stackframesize * 8);
538
539         disp = dseg_add_functionptr(cd, trace_java_call_enter);
540         M_ALD(REG_PV, REG_PV, disp);
541         M_JSR(REG_RA, REG_PV);
542         disp = (s4) (cd->mcodeptr - cd->mcodebase);
543         M_LDA(REG_PV, REG_RA, -disp);
544         M_ALD(REG_RA, REG_SP, 0 * 8);
545
546         /* restore argument registers */
547
548         for (i = 0; i < md->paramcount; i++) {
549                 if (!md->params[i].inmemory) {
550                         s = md->params[i].regoff;
551
552                         switch (md->paramtypes[i].type) {
553                         case TYPE_ADR:
554                         case TYPE_INT:
555                         case TYPE_LNG:
556                                 M_LLD(s, REG_SP, (1 + i) * 8);
557                                 break;
558                         case TYPE_FLT:
559                         case TYPE_DBL:
560                                 M_DLD(s, REG_SP, (1 + i) * 8);
561                                 break;
562                         }
563                 }
564         }
565
566         /* restore all argument and temporary registers for leaf methods */
567
568         if (jd->isleafmethod) {
569                 j = 1 + md->paramcount;
570
571                 for (i = 0; i < INT_ARG_CNT; i++, j++)
572                         M_LLD(abi_registers_integer_argument[i], REG_SP, j * 8);
573
574                 for (i = 0; i < FLT_ARG_CNT; i++, j++)
575                         M_DLD(abi_registers_float_argument[i], REG_SP, j * 8);
576
577                 for (i = 0; i < INT_TMP_CNT; i++, j++)
578                         M_LLD(rd->tmpintregs[i], REG_SP, j * 8);
579
580                 for (i = 0; i < FLT_TMP_CNT; i++, j++)
581                         M_DLD(rd->tmpfltregs[i], REG_SP, j * 8);
582         }
583
584         M_LDA(REG_SP, REG_SP, stackframesize * 8);
585
586         /* mark trace code */
587
588         M_NOP;
589 }
590 #endif /* !defined(NDEBUG) */
591
592
593 /* emit_verbosecall_exit *******************************************************
594
595    Generates the code for the call trace.
596
597 *******************************************************************************/
598
599 #if !defined(NDEBUG)
600 void emit_verbosecall_exit(jitdata *jd)
601 {
602         methodinfo   *m;
603         codegendata  *cd;
604         registerdata *rd;
605         methoddesc   *md;
606         s4            disp;
607
608         /* get required compiler data */
609
610         m  = jd->m;
611         cd = jd->cd;
612         rd = jd->rd;
613
614         md = m->parseddesc;
615
616         /* mark trace code */
617
618         M_NOP;
619
620         M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
621         M_AST(REG_RA, REG_SP, 0 * 8);
622
623         /* save return value */
624
625         switch (md->returntype.type) {
626         case TYPE_ADR:
627         case TYPE_INT:
628         case TYPE_LNG:
629                 M_LST(REG_RESULT, REG_SP, 1 * 8);
630                 break;
631         case TYPE_FLT:
632         case TYPE_DBL:
633                 M_DST(REG_FRESULT, REG_SP, 1 * 8);
634                 break;
635         }
636
637         disp = dseg_add_address(cd, m);
638         M_ALD(REG_A0, REG_PV, disp);
639         M_AADD_IMM(REG_SP, 1 * 8, REG_A1);
640
641         disp = dseg_add_functionptr(cd, trace_java_call_exit);
642         M_ALD(REG_PV, REG_PV, disp);
643         M_JSR(REG_RA, REG_PV);
644         disp = (cd->mcodeptr - cd->mcodebase);
645         M_LDA(REG_PV, REG_RA, -disp);
646
647         /* restore return value */
648
649         switch (md->returntype.type) {
650         case TYPE_ADR:
651         case TYPE_INT:
652         case TYPE_LNG:
653                 M_LLD(REG_RESULT, REG_SP, 1 * 8);
654                 break;
655         case TYPE_FLT:
656         case TYPE_DBL:
657                 M_DLD(REG_FRESULT, REG_SP, 1 * 8);
658                 break;
659         }
660
661         M_ALD(REG_RA, REG_SP, 0 * 8);
662         M_AADD_IMM(REG_SP, 2 * 8, REG_SP);
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  */