* Removed all Id tags.
[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_classcast_check ********************************************************
353
354    Emit a ClassCastException check.
355
356 *******************************************************************************/
357
358 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
359 {
360         if (INSTRUCTION_MUST_CHECK(iptr)) {
361                 switch (condition) {
362                 case BRANCH_EQ:
363                         M_BNEZ(reg, 1);
364                         break;
365                 case BRANCH_LE:
366                         M_BGTZ(reg, 1);
367                         break;
368                 default:
369                         vm_abort("emit_classcast_check: unknown condition %d", condition);
370                 }
371                 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
372         }
373 }
374
375
376 /* emit_nullpointer_check ******************************************************
377
378    Emit a NullPointerException check.
379
380 *******************************************************************************/
381
382 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
383 {
384         if (INSTRUCTION_MUST_CHECK(iptr)) {
385                 M_BNEZ(reg, 1);
386                 /* Destination register must not be REG_ZERO, because then no
387                    SIGSEGV is thrown. */
388                 M_ALD_INTERN(reg, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
389         }
390 }
391
392
393 /* emit_exception_check ********************************************************
394
395    Emit an Exception check.
396
397 *******************************************************************************/
398
399 void emit_exception_check(codegendata *cd, instruction *iptr)
400 {
401         if (INSTRUCTION_MUST_CHECK(iptr)) {
402                 M_BNEZ(REG_RESULT, 1);
403                 /* Destination register must not be REG_ZERO, because then no
404                    SIGSEGV is thrown. */
405                 M_ALD_INTERN(REG_RESULT, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
406         }
407 }
408
409
410 /* emit_trap *******************************************************************
411
412    Emit a trap instruction and return the original machine code.
413
414 *******************************************************************************/
415
416 uint32_t emit_trap(codegendata *cd)
417 {
418         uint32_t mcode;
419
420         /* Get machine code which is patched back in later. The
421            trap is 1 instruction word long. */
422
423         mcode = *((u4 *) cd->mcodeptr);
424
425         /* Destination register must not be REG_ZERO, because then no
426            SIGSEGV is thrown. */
427         M_ALD_INTERN(REG_RESULT, REG_ZERO, EXCEPTION_HARDWARE_PATCHER);
428
429         return mcode;
430 }
431
432
433 /* emit_verbosecall_enter ******************************************************
434
435    Generates the code for the call trace.
436
437 *******************************************************************************/
438
439 #if !defined(NDEBUG)
440 void emit_verbosecall_enter(jitdata *jd)
441 {
442         methodinfo   *m;
443         codegendata  *cd;
444         registerdata *rd;
445         methoddesc   *md;
446         s4            disp;
447         s4            i, t;
448
449         /* get required compiler data */
450
451         m  = jd->m;
452         cd = jd->cd;
453         rd = jd->rd;
454
455         md = m->parseddesc;
456
457         /* mark trace code */
458
459         M_NOP;
460
461         M_LDA(REG_SP, REG_SP, -((ARG_CNT + TMP_CNT + 2) * 8));
462         M_AST(REG_RA, REG_SP, 1 * 8);
463
464         /* save argument registers */
465
466         for (i = 0; i < INT_ARG_CNT; i++)
467                 M_LST(abi_registers_integer_argument[i], REG_SP, (2 + i) * 8);
468
469         for (i = 0; i < FLT_ARG_CNT; i++)
470                 M_DST(abi_registers_float_argument[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
471
472         /* save temporary registers for leaf methods */
473
474         if (jd->isleafmethod) {
475                 for (i = 0; i < INT_TMP_CNT; i++)
476                         M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
477
478                 for (i = 0; i < FLT_TMP_CNT; i++)
479                         M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
480         }
481
482         /* load float arguments into integer registers */
483
484         for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
485                 t = md->paramtypes[i].type;
486
487                 if (IS_FLT_DBL_TYPE(t)) {
488                         if (IS_2_WORD_TYPE(t)) {
489                                 M_DST(abi_registers_float_argument[i], REG_SP, 0 * 8);
490                                 M_LLD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
491                         }
492                         else {
493                                 M_FST(abi_registers_float_argument[i], REG_SP, 0 * 8);
494                                 M_ILD(abi_registers_integer_argument[i], REG_SP, 0 * 8);
495                         }
496                 }
497         }
498
499         disp = dseg_add_address(cd, m);
500         M_ALD(REG_ITMP1, REG_PV, disp);
501         M_AST(REG_ITMP1, REG_SP, 0 * 8);
502         disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
503         M_ALD(REG_PV, REG_PV, disp);
504         M_JSR(REG_RA, REG_PV);
505         disp = (s4) (cd->mcodeptr - cd->mcodebase);
506         M_LDA(REG_PV, REG_RA, -disp);
507         M_ALD(REG_RA, REG_SP, 1 * 8);
508
509         /* restore argument registers */
510
511         for (i = 0; i < INT_ARG_CNT; i++)
512                 M_LLD(abi_registers_integer_argument[i], REG_SP, (2 + i) * 8);
513
514         for (i = 0; i < FLT_ARG_CNT; i++)
515                 M_DLD(abi_registers_float_argument[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
516
517         /* restore temporary registers for leaf methods */
518
519         if (jd->isleafmethod) {
520                 for (i = 0; i < INT_TMP_CNT; i++)
521                         M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
522
523                 for (i = 0; i < FLT_TMP_CNT; i++)
524                         M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
525         }
526
527         M_LDA(REG_SP, REG_SP, (ARG_CNT + TMP_CNT + 2) * 8);
528
529         /* mark trace code */
530
531         M_NOP;
532 }
533 #endif /* !defined(NDEBUG) */
534
535
536 /* emit_verbosecall_exit *******************************************************
537
538    Generates the code for the call trace.
539
540    void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
541
542 *******************************************************************************/
543
544 #if !defined(NDEBUG)
545 void emit_verbosecall_exit(jitdata *jd)
546 {
547         methodinfo   *m;
548         codegendata  *cd;
549         registerdata *rd;
550         s4            disp;
551
552         /* get required compiler data */
553
554         m  = jd->m;
555         cd = jd->cd;
556         rd = jd->rd;
557
558         /* mark trace code */
559
560         M_NOP;
561
562         M_ASUB_IMM(REG_SP, 4 * 8, REG_SP);
563         M_AST(REG_RA, REG_SP, 0 * 8);
564
565         M_LST(REG_RESULT, REG_SP, 1 * 8);
566         M_DST(REG_FRESULT, REG_SP, 2 * 8);
567
568         M_MOV(REG_RESULT, REG_A0);
569         M_FMOV(REG_FRESULT, REG_FA1);
570         M_FMOV(REG_FRESULT, REG_FA2);
571
572         disp = dseg_add_address(cd, m);
573         M_ALD(REG_A3, REG_PV, disp);
574
575         disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
576         M_ALD(REG_PV, REG_PV, disp);
577         M_JSR(REG_RA, REG_PV);
578         disp = (cd->mcodeptr - cd->mcodebase);
579         M_LDA(REG_PV, REG_RA, -disp);
580
581         M_DLD(REG_FRESULT, REG_SP, 2 * 8);
582         M_LLD(REG_RESULT, REG_SP, 1 * 8);
583
584         M_ALD(REG_RA, REG_SP, 0 * 8);
585         M_AADD_IMM(REG_SP, 4 * 8, REG_SP);
586
587         /* mark trace code */
588
589         M_NOP;
590 }
591 #endif /* !defined(NDEBUG) */
592
593
594 /*
595  * These are local overrides for various environment variables in Emacs.
596  * Please do not remove this and leave it at the end of the file, where
597  * Emacs will automagically detect them.
598  * ---------------------------------------------------------------------
599  * Local variables:
600  * mode: c
601  * indent-tabs-mode: t
602  * c-basic-offset: 4
603  * tab-width: 4
604  * End:
605  * vim:noexpandtab:sw=4:ts=4:
606  */