63f1fbab4c6dc194525b4f96f80bedfd04456bc0
[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         codeinfo     *code;
473         codegendata  *cd;
474         registerdata *rd;
475         methoddesc   *md;
476         int32_t       stackframesize;
477         s4            disp;
478         s4            i, j, s;
479
480         /* get required compiler data */
481
482         m    = jd->m;
483         code = jd->code;
484         cd   = jd->cd;
485         rd   = jd->rd;
486
487         md = m->parseddesc;
488
489         /* mark trace code */
490
491         M_NOP;
492
493         stackframesize = ARG_CNT + TMP_CNT + md->paramcount + 1;
494
495         M_LDA(REG_SP, REG_SP, -(stackframesize * 8));
496         M_AST(REG_RA, REG_SP, 0 * 8);
497
498         /* save all argument and temporary registers for leaf methods */
499
500         if (code_is_leafmethod(code)) {
501                 j = 1 + md->paramcount;
502
503                 for (i = 0; i < INT_ARG_CNT; i++, j++)
504                         M_LST(abi_registers_integer_argument[i], REG_SP, j * 8);
505
506                 for (i = 0; i < FLT_ARG_CNT; i++, j++)
507                         M_DST(abi_registers_float_argument[i], REG_SP, j * 8);
508
509                 for (i = 0; i < INT_TMP_CNT; i++, j++)
510                         M_LST(rd->tmpintregs[i], REG_SP, j * 8);
511
512                 for (i = 0; i < FLT_TMP_CNT; i++, j++)
513                         M_DST(rd->tmpfltregs[i], REG_SP, j * 8);
514         }
515
516         /* save argument registers */
517
518         for (i = 0; i < md->paramcount; i++) {
519                 if (!md->params[i].inmemory) {
520                         s = md->params[i].regoff;
521
522                         switch (md->paramtypes[i].type) {
523                         case TYPE_ADR:
524                         case TYPE_INT:
525                         case TYPE_LNG:
526                                 M_LST(s, REG_SP, (1 + i) * 8);
527                                 break;
528                         case TYPE_FLT:
529                         case TYPE_DBL:
530                                 M_DST(s, REG_SP, (1 + i) * 8);
531                                 break;
532                         }
533                 }
534         }
535
536         disp = dseg_add_address(cd, m);
537         M_ALD(REG_A0, REG_PV, disp);
538         M_AADD_IMM(REG_SP, 1 * 8, REG_A1);
539         M_LDA(REG_A2, REG_SP, stackframesize * 8 + cd->stackframesize * 8);
540
541         disp = dseg_add_functionptr(cd, trace_java_call_enter);
542         M_ALD(REG_PV, REG_PV, disp);
543         M_JSR(REG_RA, REG_PV);
544         disp = (s4) (cd->mcodeptr - cd->mcodebase);
545         M_LDA(REG_PV, REG_RA, -disp);
546         M_ALD(REG_RA, REG_SP, 0 * 8);
547
548         /* restore argument registers */
549
550         for (i = 0; i < md->paramcount; i++) {
551                 if (!md->params[i].inmemory) {
552                         s = md->params[i].regoff;
553
554                         switch (md->paramtypes[i].type) {
555                         case TYPE_ADR:
556                         case TYPE_INT:
557                         case TYPE_LNG:
558                                 M_LLD(s, REG_SP, (1 + i) * 8);
559                                 break;
560                         case TYPE_FLT:
561                         case TYPE_DBL:
562                                 M_DLD(s, REG_SP, (1 + i) * 8);
563                                 break;
564                         }
565                 }
566         }
567
568         /* restore all argument and temporary registers for leaf methods */
569
570         if (code_is_leafmethod(code)) {
571                 j = 1 + md->paramcount;
572
573                 for (i = 0; i < INT_ARG_CNT; i++, j++)
574                         M_LLD(abi_registers_integer_argument[i], REG_SP, j * 8);
575
576                 for (i = 0; i < FLT_ARG_CNT; i++, j++)
577                         M_DLD(abi_registers_float_argument[i], REG_SP, j * 8);
578
579                 for (i = 0; i < INT_TMP_CNT; i++, j++)
580                         M_LLD(rd->tmpintregs[i], REG_SP, j * 8);
581
582                 for (i = 0; i < FLT_TMP_CNT; i++, j++)
583                         M_DLD(rd->tmpfltregs[i], REG_SP, j * 8);
584         }
585
586         M_LDA(REG_SP, REG_SP, stackframesize * 8);
587
588         /* mark trace code */
589
590         M_NOP;
591 }
592 #endif /* !defined(NDEBUG) */
593
594
595 /* emit_verbosecall_exit *******************************************************
596
597    Generates the code for the call trace.
598
599 *******************************************************************************/
600
601 #if !defined(NDEBUG)
602 void emit_verbosecall_exit(jitdata *jd)
603 {
604         methodinfo   *m;
605         codegendata  *cd;
606         registerdata *rd;
607         methoddesc   *md;
608         s4            disp;
609
610         /* get required compiler data */
611
612         m  = jd->m;
613         cd = jd->cd;
614         rd = jd->rd;
615
616         md = m->parseddesc;
617
618         /* mark trace code */
619
620         M_NOP;
621
622         M_ASUB_IMM(REG_SP, 2 * 8, REG_SP);
623         M_AST(REG_RA, REG_SP, 0 * 8);
624
625         /* save return value */
626
627         switch (md->returntype.type) {
628         case TYPE_ADR:
629         case TYPE_INT:
630         case TYPE_LNG:
631                 M_LST(REG_RESULT, REG_SP, 1 * 8);
632                 break;
633         case TYPE_FLT:
634         case TYPE_DBL:
635                 M_DST(REG_FRESULT, REG_SP, 1 * 8);
636                 break;
637         }
638
639         disp = dseg_add_address(cd, m);
640         M_ALD(REG_A0, REG_PV, disp);
641         M_AADD_IMM(REG_SP, 1 * 8, REG_A1);
642
643         disp = dseg_add_functionptr(cd, trace_java_call_exit);
644         M_ALD(REG_PV, REG_PV, disp);
645         M_JSR(REG_RA, REG_PV);
646         disp = (cd->mcodeptr - cd->mcodebase);
647         M_LDA(REG_PV, REG_RA, -disp);
648
649         /* restore return value */
650
651         switch (md->returntype.type) {
652         case TYPE_ADR:
653         case TYPE_INT:
654         case TYPE_LNG:
655                 M_LLD(REG_RESULT, REG_SP, 1 * 8);
656                 break;
657         case TYPE_FLT:
658         case TYPE_DBL:
659                 M_DLD(REG_FRESULT, REG_SP, 1 * 8);
660                 break;
661         }
662
663         M_ALD(REG_RA, REG_SP, 0 * 8);
664         M_AADD_IMM(REG_SP, 2 * 8, REG_SP);
665
666         /* mark trace code */
667
668         M_NOP;
669 }
670 #endif /* !defined(NDEBUG) */
671
672
673 /*
674  * These are local overrides for various environment variables in Emacs.
675  * Please do not remove this and leave it at the end of the file, where
676  * Emacs will automagically detect them.
677  * ---------------------------------------------------------------------
678  * Local variables:
679  * mode: c
680  * indent-tabs-mode: t
681  * c-basic-offset: 4
682  * tab-width: 4
683  * End:
684  * vim:noexpandtab:sw=4:ts=4:
685  */