* src/vm/jit/powerpc/emit.c (emit_verbosecall_exit) [__DARWIN__]: Fixed passing
[cacao.git] / src / vm / jit / powerpc / emit.c
1 /* src/vm/jit/powerpc/emit.c - PowerPC 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
30 #include <assert.h>
31 #include <stdint.h>
32
33 #include "vm/types.h"
34
35 #include "md-abi.h"
36
37 #include "vm/jit/powerpc/codegen.h"
38
39 #include "mm/memory.h"
40
41 #include "threads/lock-common.h"
42
43 #include "vm/builtin.h"
44 #include "vm/exceptions.h"
45
46 #include "vm/jit/abi.h"
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/codegen-common.h"
49 #include "vm/jit/dseg.h"
50 #include "vm/jit/emit-common.h"
51 #include "vm/jit/jit.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_ADR:
81                         M_ILD(tempreg, REG_SP, disp);
82                         break;
83                 case TYPE_LNG:
84                         M_LLD(tempreg, REG_SP, disp);
85                         break;
86                 case TYPE_FLT:
87                 case TYPE_DBL:
88                         M_DLD(tempreg, REG_SP, disp);
89                         break;
90                 default:
91                         vm_abort("emit_load: unknown type %d", src->type);
92                 }
93
94                 reg = tempreg;
95         }
96         else
97                 reg = src->vv.regoff;
98
99         return reg;
100 }
101
102
103 /* emit_load_low ***************************************************************
104
105    Emits a possible load of the low 32-bits of an operand.
106
107 *******************************************************************************/
108
109 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
110 {
111         codegendata  *cd;
112         s4            disp;
113         s4            reg;
114
115         assert(src->type == TYPE_LNG);
116
117         /* get required compiler data */
118
119         cd = jd->cd;
120
121         if (IS_INMEMORY(src->flags)) {
122                 COUNT_SPILLS;
123
124                 disp = src->vv.regoff;
125
126                 M_ILD(tempreg, REG_SP, disp + 4);
127
128                 reg = tempreg;
129         }
130         else
131                 reg = GET_LOW_REG(src->vv.regoff);
132
133         return reg;
134 }
135
136
137 /* emit_load_high **************************************************************
138
139    Emits a possible load of the high 32-bits of an operand.
140
141 *******************************************************************************/
142
143 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
144 {
145         codegendata  *cd;
146         s4            disp;
147         s4            reg;
148
149         assert(src->type == TYPE_LNG);
150
151         /* get required compiler data */
152
153         cd = jd->cd;
154
155         if (IS_INMEMORY(src->flags)) {
156                 COUNT_SPILLS;
157
158                 disp = src->vv.regoff;
159
160                 M_ILD(tempreg, REG_SP, disp);
161
162                 reg = tempreg;
163         }
164         else
165                 reg = GET_HIGH_REG(src->vv.regoff);
166
167         return reg;
168 }
169
170
171 /* emit_store ******************************************************************
172
173    Emit a possible store for the given variable.
174
175 *******************************************************************************/
176
177 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
178 {
179         codegendata *cd;
180         s4           disp;
181
182         /* get required compiler data */
183
184         cd = jd->cd;
185
186         if (IS_INMEMORY(dst->flags)) {
187                 COUNT_SPILLS;
188
189                 disp = dst->vv.regoff;
190
191                 switch (dst->type) {
192                 case TYPE_INT:
193                 case TYPE_ADR:
194                         M_IST(d, REG_SP, disp);
195                         break;
196                 case TYPE_LNG:
197                         M_LST(d, REG_SP, disp);
198                         break;
199                 case TYPE_FLT:
200                 case TYPE_DBL:
201                         M_DST(d, REG_SP, disp);
202                         break;
203                 default:
204                         vm_abort("emit_store: unknown type %d", dst->type);
205                 }
206         }
207 }
208
209
210 /* emit_copy *******************************************************************
211
212    Generates a register/memory to register/memory copy.
213
214 *******************************************************************************/
215
216 void emit_copy(jitdata *jd, instruction *iptr)
217 {
218         codegendata *cd;
219         varinfo     *src;
220         varinfo     *dst;
221         s4           s1, d;
222
223         /* get required compiler data */
224
225         cd = jd->cd;
226
227         /* get source and destination variables */
228
229         src = VAROP(iptr->s1);
230         dst = VAROP(iptr->dst);
231
232         if ((src->vv.regoff != dst->vv.regoff) ||
233                 (IS_INMEMORY(src->flags ^ dst->flags))) {
234
235                 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
236                         /* emit nothing, as the value won't be used anyway */
237                         return;
238                 }
239
240                 /* If one of the variables resides in memory, we can eliminate
241                    the register move from/to the temporary register with the
242                    order of getting the destination register and the load. */
243
244                 if (IS_INMEMORY(src->flags)) {
245                         if (IS_LNG_TYPE(src->type))
246                                 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
247                         else
248                                 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
249
250                         s1 = emit_load(jd, iptr, src, d);
251                 }
252                 else {
253                         if (IS_LNG_TYPE(src->type))
254                                 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
255                         else
256                                 s1 = emit_load(jd, iptr, src, REG_IFTMP);
257
258                         d = codegen_reg_of_var(iptr->opc, dst, s1);
259                 }
260
261                 if (s1 != d) {
262                         switch (src->type) {
263                         case TYPE_INT:
264                         case TYPE_ADR:
265                                 M_MOV(s1, d);
266                                 break;
267                         case TYPE_LNG:
268                                 M_MOV(GET_LOW_REG(s1), GET_LOW_REG(d));
269                                 M_MOV(GET_HIGH_REG(s1), GET_HIGH_REG(d));
270                                 break;
271                         case TYPE_FLT:
272                         case TYPE_DBL:
273                                 M_FMOV(s1, d);
274                                 break;
275                         default:
276                                 vm_abort("emit_copy: unknown type %d", src->type);
277                         }
278                 }
279
280                 emit_store(jd, iptr, dst, d);
281         }
282 }
283
284
285 /* emit_iconst *****************************************************************
286
287    XXX
288
289 *******************************************************************************/
290
291 void emit_iconst(codegendata *cd, s4 d, s4 value)
292 {
293         s4 disp;
294
295         if ((value >= -32768) && (value <= 32767))
296                 M_LDA_INTERN(d, REG_ZERO, value);
297         else {
298                 disp = dseg_add_s4(cd, value);
299                 M_ILD(d, REG_PV, disp);
300         }
301 }
302
303
304 /* emit_branch *****************************************************************
305
306    Emits the code for conditional and unconditional branchs.
307
308 *******************************************************************************/
309
310 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt)
311 {
312         s4 checkdisp;
313         s4 branchdisp;
314
315         /* calculate the different displacements */
316
317         checkdisp  =  disp + 4;
318         branchdisp = (disp - 4) >> 2;
319
320         /* check which branch to generate */
321
322         if (condition == BRANCH_UNCONDITIONAL) {
323                 /* check displacement for overflow */
324
325                 if ((checkdisp < (s4) 0xfe000000) || (checkdisp > (s4) 0x01fffffc)) {
326                         /* if the long-branches flag isn't set yet, do it */
327
328                         if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
329                                 cd->flags |= (CODEGENDATA_FLAG_ERROR |
330                                                           CODEGENDATA_FLAG_LONGBRANCHES);
331                         }
332
333                         vm_abort("emit_branch: emit unconditional long-branch code");
334                 }
335                 else {
336                         M_BR(branchdisp);
337                 }
338         }
339         else {
340                 /* and displacement for overflow */
341
342                 if ((checkdisp < (s4) 0xffff8000) || (checkdisp > (s4) 0x00007fff)) {
343                         /* if the long-branches flag isn't set yet, do it */
344
345                         if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
346                                 cd->flags |= (CODEGENDATA_FLAG_ERROR |
347                                                           CODEGENDATA_FLAG_LONGBRANCHES);
348                         }
349
350                         switch (condition) {
351                         case BRANCH_EQ:
352                                 M_BNE(1);
353                                 M_BR(branchdisp);
354                                 break;
355                         case BRANCH_NE:
356                                 M_BEQ(1);
357                                 M_BR(branchdisp);
358                                 break;
359                         case BRANCH_LT:
360                                 M_BGE(1);
361                                 M_BR(branchdisp);
362                                 break;
363                         case BRANCH_GE:
364                                 M_BLT(1);
365                                 M_BR(branchdisp);
366                                 break;
367                         case BRANCH_GT:
368                                 M_BLE(1);
369                                 M_BR(branchdisp);
370                                 break;
371                         case BRANCH_LE:
372                                 M_BGT(1);
373                                 M_BR(branchdisp);
374                                 break;
375                         case BRANCH_NAN:
376                                 vm_abort("emit_branch: long BRANCH_NAN");
377                                 break;
378                         default:
379                                 vm_abort("emit_branch: unknown condition %d", condition);
380                         }
381                 }
382                 else {
383                         switch (condition) {
384                         case BRANCH_EQ:
385                                 M_BEQ(branchdisp);
386                                 break;
387                         case BRANCH_NE:
388                                 M_BNE(branchdisp);
389                                 break;
390                         case BRANCH_LT:
391                                 M_BLT(branchdisp);
392                                 break;
393                         case BRANCH_GE:
394                                 M_BGE(branchdisp);
395                                 break;
396                         case BRANCH_GT:
397                                 M_BGT(branchdisp);
398                                 break;
399                         case BRANCH_LE:
400                                 M_BLE(branchdisp);
401                                 break;
402                         case BRANCH_NAN:
403                                 M_BNAN(branchdisp);
404                                 break;
405                         default:
406                                 vm_abort("emit_branch: unknown condition %d", condition);
407                         }
408                 }
409         }
410 }
411
412
413 /* emit_arithmetic_check *******************************************************
414
415    Emit an ArithmeticException check.
416
417 *******************************************************************************/
418
419 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
420 {
421         if (INSTRUCTION_MUST_CHECK(iptr)) {
422                 M_TST(reg);
423                 M_BNE(1);
424                 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_ARITHMETIC);
425         }
426 }
427
428
429 /* emit_arrayindexoutofbounds_check ********************************************
430
431    Emit a ArrayIndexOutOfBoundsException check.
432
433 *******************************************************************************/
434
435 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
436 {
437         if (INSTRUCTION_MUST_CHECK(iptr)) {
438                 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
439                 M_TRAPGEU(s2, REG_ITMP3);
440         }
441 }
442
443
444 /* emit_arraystore_check *******************************************************
445
446    Emit an ArrayStoreException check.
447
448 *******************************************************************************/
449
450 void emit_arraystore_check(codegendata *cd, instruction *iptr)
451 {
452         if (INSTRUCTION_MUST_CHECK(iptr)) {
453                 M_TST(REG_RESULT);
454                 M_BNE(1);
455                 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_ARRAYSTORE);
456         }
457 }
458
459
460 /* emit_classcast_check ********************************************************
461
462    Emit a ClassCastException check.
463
464 *******************************************************************************/
465
466 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
467 {
468         if (INSTRUCTION_MUST_CHECK(iptr)) {
469                 switch (condition) {
470                 case BRANCH_LE:
471                         M_BGT(1);
472                         break;
473                 case BRANCH_EQ:
474                         M_BNE(1);
475                         break;
476                 case BRANCH_GT:
477                         M_BLE(1);
478                         break;
479                 default:
480                         vm_abort("emit_classcast_check: unknown condition %d", condition);
481                 }
482                 M_ALD_INTERN(s1, REG_ZERO, EXCEPTION_HARDWARE_CLASSCAST);
483         }
484 }
485
486
487 /* emit_nullpointer_check ******************************************************
488
489    Emit a NullPointerException check.
490
491 *******************************************************************************/
492
493 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
494 {
495         if (INSTRUCTION_MUST_CHECK(iptr)) {
496                 M_TST(reg);
497                 M_BNE(1);
498                 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_NULLPOINTER);
499         }
500 }
501
502
503 /* emit_exception_check ********************************************************
504
505    Emit an Exception check.
506
507 *******************************************************************************/
508
509 void emit_exception_check(codegendata *cd, instruction *iptr)
510 {
511         if (INSTRUCTION_MUST_CHECK(iptr)) {
512                 M_TST(REG_RESULT);
513                 M_BNE(1);
514                 M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_EXCEPTION);
515         }
516 }
517
518
519 /* emit_trap *******************************************************************
520
521    Emit a trap instruction and return the original machine code.
522
523 *******************************************************************************/
524
525 uint32_t emit_trap(codegendata *cd)
526 {
527         uint32_t mcode;
528
529         /* Get machine code which is patched back in later. The
530            trap is 1 instruction word long. */
531
532         mcode = *((u4 *) cd->mcodeptr);
533
534         M_ALD_INTERN(REG_ZERO, REG_ZERO, EXCEPTION_HARDWARE_PATCHER);
535
536         return mcode;
537 }
538
539
540 /* emit_verbosecall_enter ******************************************************
541
542    Generates the code for the call trace.
543
544 *******************************************************************************/
545
546 void emit_verbosecall_enter(jitdata *jd)
547 {
548 #if !defined(NDEBUG)
549         methodinfo   *m;
550         codegendata  *cd;
551         registerdata *rd;
552         methoddesc   *md;
553         int32_t       disp;
554         int32_t       i;
555         int32_t       s, d;
556         int32_t       x;
557
558         if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
559                 return;
560
561         /* get required compiler data */
562
563         m  = jd->m;
564         cd = jd->cd;
565         rd = jd->rd;
566
567         md = m->parseddesc;
568
569         /* mark trace code */
570
571         M_NOP;
572
573         M_MFLR(REG_ZERO);
574         M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
575         M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8));
576
577         M_CLR(REG_ITMP1);                            /* prepare a "zero" register */
578
579         /* save argument registers */
580
581         for (i = 0; i < md->paramcount; i++) {
582                 if (!md->params[i].inmemory) {
583                         s = md->params[i].regoff;
584                         d = LA_SIZE + (1 + i) * 8;
585
586                         switch (md->paramtypes[i].type) {
587                         case TYPE_INT:
588                         case TYPE_ADR:
589                                 M_IST(REG_ITMP1, REG_SP, d);            /* high-bits are zero */
590                                 M_IST(s, REG_SP, d + 4);
591                                 break;
592                         case TYPE_LNG:
593                                 M_LST(s, REG_SP, d);
594                                 break;
595                         case TYPE_FLT:
596                                 M_IST(REG_ITMP1, REG_SP, d);            /* high-bits are zero */
597                                 M_FST(s, REG_SP, d + 4);
598                                 break;
599                         case TYPE_DBL:
600                                 M_DST(s, REG_SP, d);
601                                 break;
602                         }
603                 }
604         }
605
606         /* load arguments as longs */
607
608         d = 0;
609
610         for (i = 0; i < md->paramcount && i < TRACE_ARGS_NUM; i++) {
611                 s = LA_SIZE + (1 + i) * 8;
612                 x = PACK_REGS(abi_registers_integer_argument[d + 1],
613                                           abi_registers_integer_argument[d]);
614
615                 M_LLD(x, REG_SP, s);
616
617                 d += 2;
618         }
619
620         /* put methodinfo pointer as last argument on the stack */
621
622         disp = dseg_add_address(cd, m);
623         M_ALD(REG_ITMP1, REG_PV, disp);
624 #if defined(__DARWIN__)
625         M_AST(REG_ITMP1, REG_SP, LA_SIZE + TRACE_ARGS_NUM * 8); 
626 #else
627         M_AST(REG_ITMP1, REG_SP, LA_SIZE);
628 #endif
629         disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
630         M_ALD(REG_ITMP2, REG_PV, disp);
631         M_MTCTR(REG_ITMP2);
632         M_JSR;
633
634         /* restore argument registers */
635
636         for (i = 0; i < md->paramcount; i++) {
637                 if (!md->params[i].inmemory) {
638                         s = LA_SIZE + (1 + i) * 8;
639                         d = md->params[i].regoff;
640
641                         switch (md->paramtypes[i].type) {
642                         case TYPE_INT:
643                         case TYPE_ADR:
644                                 M_ILD(d, REG_SP, s + 4);                      /* get low-bits */
645                                 break;
646                         case TYPE_LNG:
647                                 M_LLD(d, REG_SP, s);
648                                 break;
649                         case TYPE_FLT:
650                                 M_FLD(d, REG_SP, s + 4);                      /* get low-bits */
651                                 break;
652                         case TYPE_DBL:
653                                 M_DLD(d, REG_SP, s);
654                                 break;
655                         }
656                 }
657         }
658
659         M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8 + LA_LR_OFFSET);
660         M_MTLR(REG_ZERO);
661         M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + ARG_CNT + TMP_CNT) * 8);
662
663         /* mark trace code */
664
665         M_NOP;
666 #endif /* !defined(NDEBUG) */
667 }
668
669
670 /* emit_verbosecall_exit *******************************************************
671
672    Generates the code for the call trace.
673
674    void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
675
676 *******************************************************************************/
677
678 void emit_verbosecall_exit(jitdata *jd)
679 {
680 #if !defined(NDEBUG)
681         methodinfo   *m;
682         codegendata  *cd;
683         registerdata *rd;
684         methoddesc   *md;
685         s4            disp;
686
687         if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
688                 return;
689
690         /* get required compiler data */
691
692         m  = jd->m;
693         cd = jd->cd;
694         rd = jd->rd;
695
696         md = m->parseddesc;
697         
698         /* mark trace code */
699
700         M_NOP;
701
702         M_MFLR(REG_ZERO);
703         M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
704         M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4));
705
706         /* save return registers */
707
708         M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
709         M_DST(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
710
711         /* keep this order */
712         switch (md->returntype.type) {
713         case TYPE_INT:
714         case TYPE_ADR:
715                 M_INTMOVE(REG_RESULT, REG_A1);
716                 M_CLR(REG_A0);
717                 break;
718
719         case TYPE_LNG:
720                 M_LNGMOVE(REG_RESULT_PACKED, REG_A0_A1_PACKED);
721                 break;
722         }
723
724         M_FLTMOVE(REG_FRESULT, REG_FA0);
725         M_FLTMOVE(REG_FRESULT, REG_FA1);
726
727         disp = dseg_add_address(cd, m);
728 #if defined(__DARWIN__)
729         M_ALD(REG_A2 + 3, REG_PV, disp);
730 #else
731         M_ALD(REG_A2, REG_PV, disp);
732 #endif
733
734         disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
735         M_ALD(REG_ITMP2, REG_PV, disp);
736         M_MTCTR(REG_ITMP2);
737         M_JSR;
738
739         /* restore return registers */
740
741         M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
742         M_DLD(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
743
744         M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4 + LA_LR_OFFSET);
745         M_MTLR(REG_ZERO);
746         M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4);
747
748         /* mark trace code */
749
750         M_NOP;
751 #endif /* !defined(NDEBUG) */
752 }
753
754
755 /*
756  * These are local overrides for various environment variables in Emacs.
757  * Please do not remove this and leave it at the end of the file, where
758  * Emacs will automagically detect them.
759  * ---------------------------------------------------------------------
760  * Local variables:
761  * mode: c
762  * indent-tabs-mode: t
763  * c-basic-offset: 4
764  * tab-width: 4
765  * End:
766  * vim:noexpandtab:sw=4:ts=4:
767  */