9b804db9c0589f200b0a196dcbcd0be4be524e3c
[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/builtin.h"
43 #include "vm/exceptions.h"
44
45 #include "vm/jit/abi.h"
46 #include "vm/jit/abi-asm.h"
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/dseg.h"
49 #include "vm/jit/emit-common.h"
50 #include "vm/jit/jit.h"
51 #include "vm/jit/patcher-common.h"
52 #include "vm/jit/replace.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         s4            disp;
476         s4            i, t;
477
478         /* get required compiler data */
479
480         m  = jd->m;
481         cd = jd->cd;
482         rd = jd->rd;
483
484         md = m->parseddesc;
485
486         /* mark trace code */
487
488         M_NOP;
489
490         M_LDA(REG_SP, REG_SP, -((ARG_CNT + TMP_CNT + 2) * 8));
491         M_AST(REG_RA, REG_SP, 1 * 8);
492
493         /* save argument registers */
494
495         for (i = 0; i < INT_ARG_CNT; i++)
496                 M_LST(abi_registers_integer_argument[i], REG_SP, (2 + i) * 8);
497
498         for (i = 0; i < FLT_ARG_CNT; i++)
499                 M_DST(abi_registers_float_argument[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
500
501         /* save temporary registers for leaf methods */
502
503         if (jd->isleafmethod) {
504                 for (i = 0; i < INT_TMP_CNT; i++)
505                         M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
506
507                 for (i = 0; i < FLT_TMP_CNT; i++)
508                         M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
509         }
510
511         /* load float arguments into integer registers */
512
513         for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
514                 t = md->paramtypes[i].type;
515
516                 if (IS_FLT_DBL_TYPE(t)) {
517                         if (IS_2_WORD_TYPE(t)) {
518                                 M_DST(abi_registers_float_argument[i], REG_SP, 0 * 8);
519                                 M_LLD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
520                         }
521                         else {
522                                 M_FST(abi_registers_float_argument[i], REG_SP, 0 * 8);
523                                 M_ILD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
524                         }
525                 }
526         }
527
528         disp = dseg_add_address(cd, m);
529         M_ALD(REG_ITMP1, REG_PV, disp);
530         M_AST(REG_ITMP1, REG_SP, 0 * 8);
531         disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
532         M_ALD(REG_PV, REG_PV, disp);
533         M_JSR(REG_RA, REG_PV);
534         disp = (s4) (cd->mcodeptr - cd->mcodebase);
535         M_LDA(REG_PV, REG_RA, -disp);
536         M_ALD(REG_RA, REG_SP, 1 * 8);
537
538         /* restore argument registers */
539
540         for (i = 0; i < INT_ARG_CNT; i++)
541                 M_LLD(abi_registers_integer_argument[i], REG_SP, (2 + i) * 8);
542
543         for (i = 0; i < FLT_ARG_CNT; i++)
544                 M_DLD(abi_registers_float_argument[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
545
546         /* restore temporary registers for leaf methods */
547
548         if (jd->isleafmethod) {
549                 for (i = 0; i < INT_TMP_CNT; i++)
550                         M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
551
552                 for (i = 0; i < FLT_TMP_CNT; i++)
553                         M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
554         }
555
556         M_LDA(REG_SP, REG_SP, (ARG_CNT + TMP_CNT + 2) * 8);
557
558         /* mark trace code */
559
560         M_NOP;
561 }
562 #endif /* !defined(NDEBUG) */
563
564
565 /* emit_verbosecall_exit *******************************************************
566
567    Generates the code for the call trace.
568
569    void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
570
571 *******************************************************************************/
572
573 #if !defined(NDEBUG)
574 void emit_verbosecall_exit(jitdata *jd)
575 {
576         methodinfo   *m;
577         codegendata  *cd;
578         registerdata *rd;
579         s4            disp;
580
581         /* get required compiler data */
582
583         m  = jd->m;
584         cd = jd->cd;
585         rd = jd->rd;
586
587         /* mark trace code */
588
589         M_NOP;
590
591         M_ASUB_IMM(REG_SP, 4 * 8, REG_SP);
592         M_AST(REG_RA, REG_SP, 0 * 8);
593
594         M_LST(REG_RESULT, REG_SP, 1 * 8);
595         M_DST(REG_FRESULT, REG_SP, 2 * 8);
596
597         M_MOV(REG_RESULT, REG_A0);
598         M_FMOV(REG_FRESULT, REG_FA1);
599         M_FMOV(REG_FRESULT, REG_FA2);
600
601         disp = dseg_add_address(cd, m);
602         M_ALD(REG_A3, REG_PV, disp);
603
604         disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
605         M_ALD(REG_PV, REG_PV, disp);
606         M_JSR(REG_RA, REG_PV);
607         disp = (cd->mcodeptr - cd->mcodebase);
608         M_LDA(REG_PV, REG_RA, -disp);
609
610         M_DLD(REG_FRESULT, REG_SP, 2 * 8);
611         M_LLD(REG_RESULT, REG_SP, 1 * 8);
612
613         M_ALD(REG_RA, REG_SP, 0 * 8);
614         M_AADD_IMM(REG_SP, 4 * 8, REG_SP);
615
616         /* mark trace code */
617
618         M_NOP;
619 }
620 #endif /* !defined(NDEBUG) */
621
622
623 /*
624  * These are local overrides for various environment variables in Emacs.
625  * Please do not remove this and leave it at the end of the file, where
626  * Emacs will automagically detect them.
627  * ---------------------------------------------------------------------
628  * Local variables:
629  * mode: c
630  * indent-tabs-mode: t
631  * c-basic-offset: 4
632  * tab-width: 4
633  * End:
634  * vim:noexpandtab:sw=4:ts=4:
635  */