dd0743e9dee072f8213f0b27150bb3e003c9c316
[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    $Id: emit.c 4398 2006-01-31 23:43:08Z twisti $
26
27 */
28
29
30 #include "config.h"
31 #include "vm/types.h"
32
33 #include <assert.h>
34
35 #include "md-abi.h"
36
37 #include "vm/jit/alpha/codegen.h"
38
39 #include "mm/memory.h"
40
41 #if defined(ENABLE_THREADS)
42 # include "threads/native/lock.h"
43 #endif
44
45 #include "vm/builtin.h"
46
47 #include "vm/jit/abi-asm.h"
48 #include "vm/jit/asmpart.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            reg;
67
68         /* get required compiler data */
69
70         cd = jd->cd;
71
72         if (IS_INMEMORY(src->flags)) {
73                 COUNT_SPILLS;
74
75                 if (IS_FLT_DBL_TYPE(src->type))
76                         M_DLD(tempreg, REG_SP, src->vv.regoff * 8);
77                 else
78                         M_LLD(tempreg, REG_SP, src->vv.regoff * 8);
79
80                 reg = tempreg;
81         }
82         else
83                 reg = src->vv.regoff;
84
85         return reg;
86 }
87
88
89 /* emit_store ******************************************************************
90
91    Emit a possible store for the given variable.
92
93 *******************************************************************************/
94
95 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
96 {
97         codegendata  *cd;
98
99         /* get required compiler data */
100
101         cd = jd->cd;
102
103         if (IS_INMEMORY(dst->flags)) {
104                 COUNT_SPILLS;
105
106                 if (IS_FLT_DBL_TYPE(dst->type))
107                         M_DST(d, REG_SP, dst->vv.regoff * 8);
108                 else
109                         M_LST(d, REG_SP, dst->vv.regoff * 8);
110         }
111 }
112
113
114 /* emit_copy *******************************************************************
115
116    Generates a register/memory to register/memory copy.
117
118 *******************************************************************************/
119
120 void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
121 {
122         codegendata  *cd;
123         s4            s1, d;
124
125         /* get required compiler data */
126
127         cd = jd->cd;
128
129         if ((src->vv.regoff != dst->vv.regoff) ||
130                 ((src->flags ^ dst->flags) & INMEMORY)) {
131
132                 /* If one of the variables resides in memory, we can eliminate
133                    the register move from/to the temporary register with the
134                    order of getting the destination register and the load. */
135
136                 if (IS_INMEMORY(src->flags)) {
137                         d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
138                         s1 = emit_load(jd, iptr, src, d);
139                 }
140                 else {
141                         s1 = emit_load(jd, iptr, src, REG_IFTMP);
142                         d = codegen_reg_of_var(iptr->opc, dst, s1);
143                 }
144
145                 if (s1 != d) {
146                         if (IS_FLT_DBL_TYPE(src->type))
147                                 M_FMOV(s1, d);
148                         else
149                                 M_MOV(s1, d);
150                 }
151
152                 emit_store(jd, iptr, dst, d);
153         }
154 }
155
156
157 /* emit_iconst *****************************************************************
158
159    XXX
160
161 *******************************************************************************/
162
163 void emit_iconst(codegendata *cd, s4 d, s4 value)
164 {
165         s4 disp;
166
167         if ((value >= -32768) && (value <= 32767))
168                 M_LDA_INTERN(d, REG_ZERO, value);
169         else {
170                 disp = dseg_add_s4(cd, value);
171                 M_ILD(d, REG_PV, disp);
172         }
173 }
174
175
176 /* emit_lconst *****************************************************************
177
178    XXX
179
180 *******************************************************************************/
181
182 void emit_lconst(codegendata *cd, s4 d, s8 value)
183 {
184         s4 disp;
185
186         if ((value >= -32768) && (value <= 32767))
187                 M_LDA_INTERN(d, REG_ZERO, value);
188         else {
189                 disp = dseg_add_s8(cd, value);
190                 M_LLD(d, REG_PV, disp);
191         }
192 }
193
194
195 /* emit_arrayindexoutofbounds_check ********************************************
196
197    Emit an ArrayIndexOutOfBoundsException check.
198
199 *******************************************************************************/
200
201 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
202 {
203         if (INSTRUCTION_MUST_CHECK(iptr)) {
204                 M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
205                 M_CMPULT(s2, REG_ITMP3, REG_ITMP3);
206                 M_BEQZ(REG_ITMP3, 0);
207                 codegen_add_arrayindexoutofboundsexception_ref(cd, s2);
208         }
209 }
210
211
212 /* emit_arraystore_check *******************************************************
213
214    Emit an ArrayStoreException check.
215
216 *******************************************************************************/
217
218 void emit_arraystore_check(codegendata *cd, instruction *iptr, s4 reg)
219 {
220         M_BEQZ(reg, 0);
221         codegen_add_arraystoreexception_ref(cd);
222 }
223
224
225 /* emit_classcast_check ********************************************************
226
227    Emit a ClassCastException check.
228
229 *******************************************************************************/
230
231 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
232 {
233         if (INSTRUCTION_MUST_CHECK(iptr)) {
234                 M_BNEZ(reg, 0);
235                 codegen_add_classcastexception_ref(cd, s1);
236         }
237 }
238
239
240 /* emit_nullpointer_check ******************************************************
241
242    Emit a NullPointerException check.
243
244 *******************************************************************************/
245
246 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
247 {
248         if (INSTRUCTION_MUST_CHECK(iptr)) {
249                 M_BEQZ(reg, 0);
250                 codegen_add_nullpointerexception_ref(cd);
251         }
252 }
253
254
255 /* emit_exception_stubs ********************************************************
256
257    Generates the code for the exception stubs.
258
259 *******************************************************************************/
260
261 void emit_exception_stubs(jitdata *jd)
262 {
263         codegendata  *cd;
264         registerdata *rd;
265         exceptionref *er;
266         s4            branchmpc;
267         s4            targetmpc;
268         s4            targetdisp;
269         s4            disp;
270
271         /* get required compiler data */
272
273         cd = jd->cd;
274         rd = jd->rd;
275
276         /* generate exception stubs */
277
278         targetdisp = 0;
279
280         for (er = cd->exceptionrefs; er != NULL; er = er->next) {
281                 /* back-patch the branch to this exception code */
282
283                 branchmpc = er->branchpos;
284                 targetmpc = cd->mcodeptr - cd->mcodebase;
285
286                 md_codegen_patch_branch(cd, branchmpc, targetmpc);
287
288                 MCODECHECK(100);
289
290                 /* move index register into REG_ITMP1 */
291
292                 /* Check if the exception is an
293                    ArrayIndexOutOfBoundsException.  If so, move index register
294                    into a4. */
295
296                 if (er->reg != -1)
297                         M_MOV(er->reg, rd->argintregs[4]);
298
299                 /* calcuate exception address */
300
301                 M_LDA(rd->argintregs[3], REG_PV, er->branchpos - 4);
302
303                 /* move function to call into REG_ITMP3 */
304
305                 disp = dseg_add_functionptr(cd, er->function);
306                 M_ALD(REG_ITMP3, REG_PV, disp);
307
308                 if (targetdisp == 0) {
309                         targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
310
311                         M_MOV(REG_PV, rd->argintregs[0]);
312                         M_MOV(REG_SP, rd->argintregs[1]);
313
314                         if (jd->isleafmethod)
315                                 M_MOV(REG_RA, rd->argintregs[2]);
316                         else
317                                 M_ALD(rd->argintregs[2],
318                                           REG_SP, cd->stackframesize * 8 - SIZEOF_VOID_P);
319
320                         M_LDA(REG_SP, REG_SP, -2 * 8);
321                         M_AST(rd->argintregs[3], REG_SP, 0 * 8);             /* store XPC */
322
323                         if (jd->isleafmethod)
324                                 M_AST(REG_RA, REG_SP, 1 * 8);
325
326                         M_MOV(REG_ITMP3, REG_PV);
327                         M_JSR(REG_RA, REG_PV);
328                         disp = (s4) (cd->mcodeptr - cd->mcodebase);
329                         M_LDA(REG_PV, REG_RA, -disp);
330
331                         M_MOV(REG_RESULT, REG_ITMP1_XPTR);
332
333                         if (jd->isleafmethod)
334                                 M_ALD(REG_RA, REG_SP, 1 * 8);
335
336                         M_ALD(REG_ITMP2_XPC, REG_SP, 0 * 8);
337                         M_LDA(REG_SP, REG_SP, 2 * 8);
338
339                         disp = dseg_add_functionptr(cd, asm_handle_exception);
340                         M_ALD(REG_ITMP3, REG_PV, disp);
341                         M_JMP(REG_ZERO, REG_ITMP3);
342                 }
343                 else {
344                         disp = (((u4 *) cd->mcodebase) + targetdisp) -
345                                 (((u4 *) cd->mcodeptr) + 1);
346
347                         M_BR(disp);
348                 }
349         }
350 }
351
352
353 /* emit_patcher_stubs **********************************************************
354
355    Generates the code for the patcher stubs.
356
357 *******************************************************************************/
358
359 void emit_patcher_stubs(jitdata *jd)
360 {
361         codegendata *cd;
362         patchref    *pref;
363         u4           mcode;
364         u1          *savedmcodeptr;
365         u1          *tmpmcodeptr;
366         s4           targetdisp;
367         s4           disp;
368
369         /* get required compiler data */
370
371         cd = jd->cd;
372
373         /* generate code patching stub call code */
374
375         targetdisp = 0;
376
377         for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
378                 /* check code segment size */
379
380                 MCODECHECK(100);
381
382                 /* Get machine code which is patched back in later. The
383                    call is 1 instruction word long. */
384
385                 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
386
387                 mcode = *((u4 *) tmpmcodeptr);
388
389                 /* Patch in the call to call the following code (done at
390                    compile time). */
391
392                 savedmcodeptr = cd->mcodeptr;   /* save current mcodeptr              */
393                 cd->mcodeptr  = tmpmcodeptr;    /* set mcodeptr to patch position     */
394
395                 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
396                 M_BSR(REG_ITMP3, disp);
397
398                 cd->mcodeptr = savedmcodeptr;   /* restore the current mcodeptr       */
399
400                 /* create stack frame */
401
402                 M_LSUB_IMM(REG_SP, 6 * 8, REG_SP);
403
404                 /* move return address onto stack */
405
406                 M_AST(REG_ITMP3, REG_SP, 5 * 8);
407
408                 /* move pointer to java_objectheader onto stack */
409
410 #if defined(ENABLE_THREADS)
411                 /* create a virtual java_objectheader */
412
413                 (void) dseg_add_unique_address(cd, NULL);                  /* flcword */
414                 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
415                 disp = dseg_add_unique_address(cd, NULL);                  /* vftbl   */
416
417                 M_LDA(REG_ITMP3, REG_PV, disp);
418                 M_AST(REG_ITMP3, REG_SP, 4 * 8);
419 #else
420                 /* nothing to do */
421 #endif
422
423                 /* move machine code onto stack */
424
425                 disp = dseg_add_s4(cd, mcode);
426                 M_ILD(REG_ITMP3, REG_PV, disp);
427                 M_IST(REG_ITMP3, REG_SP, 3 * 8);
428
429                 /* move class/method/field reference onto stack */
430
431                 disp = dseg_add_address(cd, pref->ref);
432                 M_ALD(REG_ITMP3, REG_PV, disp);
433                 M_AST(REG_ITMP3, REG_SP, 2 * 8);
434
435                 /* move data segment displacement onto stack */
436
437                 disp = dseg_add_s4(cd, pref->disp);
438                 M_ILD(REG_ITMP3, REG_PV, disp);
439                 M_IST(REG_ITMP3, REG_SP, 1 * 8);
440
441                 /* move patcher function pointer onto stack */
442
443                 disp = dseg_add_functionptr(cd, pref->patcher);
444                 M_ALD(REG_ITMP3, REG_PV, disp);
445                 M_AST(REG_ITMP3, REG_SP, 0 * 8);
446
447                 if (targetdisp == 0) {
448                         targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
449
450                         disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
451                         M_ALD(REG_ITMP3, REG_PV, disp);
452                         M_JMP(REG_ZERO, REG_ITMP3);
453                 }
454                 else {
455                         disp = (((u4 *) cd->mcodebase) + targetdisp) -
456                                 (((u4 *) cd->mcodeptr) + 1);
457
458                         M_BR(disp);
459                 }
460         }
461 }
462
463
464 /* emit_replacement_stubs ******************************************************
465
466    Generates the code for the replacement stubs.
467
468 *******************************************************************************/
469
470 #if defined(ENABLE_REPLACEMENT)
471 void emit_replacement_stubs(jitdata *jd)
472 {
473         codegendata *cd;
474         codeinfo    *code;
475         rplpoint    *rplp;
476         s4           disp;
477         s4           i;
478 #if !defined(NDEBUG)
479         u1          *savedmcodeptr;
480 #endif
481
482         /* get required compiler data */
483
484         cd   = jd->cd;
485         code = jd->code;
486
487         rplp = code->rplpoints;
488
489         /* store beginning of replacement stubs */
490
491         code->replacementstubs = (u1*) (cd->mcodeptr - cd->mcodebase);
492
493         for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
494                 /* do not generate stubs for non-trappable points */
495
496                 if (rplp->flags & RPLPOINT_FLAG_NOTRAP)
497                         continue;
498
499                 /* check code segment size */
500
501                 MCODECHECK(100);
502
503 #if !defined(NDEBUG)
504                 savedmcodeptr = cd->mcodeptr;
505 #endif
506
507                 /* create stack frame - 16-byte aligned */
508
509                 M_LSUB_IMM(REG_SP, 2 * 8, REG_SP);
510
511                 /* push address of `rplpoint` struct */
512
513                 disp = dseg_add_address(cd, rplp);
514                 M_ALD(REG_ITMP3, REG_PV, disp);
515                 M_AST(REG_ITMP3, REG_SP, 0 * 8);
516
517                 /* jump to replacement function */
518
519                 disp = dseg_add_functionptr(cd, asm_replacement_out);
520                 M_ALD(REG_ITMP3, REG_PV, disp);
521                 M_JMP(REG_ZERO, REG_ITMP3);
522
523                 assert((cd->mcodeptr - savedmcodeptr) == 4*REPLACEMENT_STUB_SIZE);
524         }
525 }
526 #endif /* defined(ENABLE_REPLACEMENT) */
527
528
529 /* emit_verbosecall_enter ******************************************************
530
531    Generates the code for the call trace.
532
533 *******************************************************************************/
534
535 #if !defined(NDEBUG)
536 void emit_verbosecall_enter(jitdata *jd)
537 {
538         methodinfo   *m;
539         codegendata  *cd;
540         registerdata *rd;
541         methoddesc   *md;
542         s4            disp;
543         s4            i, t;
544
545         /* get required compiler data */
546
547         m  = jd->m;
548         cd = jd->cd;
549         rd = jd->rd;
550
551         md = m->parseddesc;
552
553         /* mark trace code */
554
555         M_NOP;
556
557         M_LDA(REG_SP, REG_SP, -((ARG_CNT + TMP_CNT + 2) * 8));
558         M_AST(REG_RA, REG_SP, 1 * 8);
559
560         /* save argument registers */
561
562         for (i = 0; i < INT_ARG_CNT; i++)
563                 M_LST(rd->argintregs[i], REG_SP, (2 + i) * 8);
564
565         for (i = 0; i < FLT_ARG_CNT; i++)
566                 M_DST(rd->argintregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
567
568         /* save temporary registers for leaf methods */
569
570         if (jd->isleafmethod) {
571                 for (i = 0; i < INT_TMP_CNT; i++)
572                         M_LST(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
573
574                 for (i = 0; i < FLT_TMP_CNT; i++)
575                         M_DST(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
576         }
577
578         /* load float arguments into integer registers */
579
580         for (i = 0; i < md->paramcount && i < INT_ARG_CNT; i++) {
581                 t = md->paramtypes[i].type;
582
583                 if (IS_FLT_DBL_TYPE(t)) {
584                         if (IS_2_WORD_TYPE(t)) {
585                                 M_DST(rd->argfltregs[i], REG_SP, 0 * 8);
586                                 M_LLD(rd->argintregs[i], REG_SP, 0 * 8);
587                         }
588                         else {
589                                 M_FST(rd->argfltregs[i], REG_SP, 0 * 8);
590                                 M_ILD(rd->argintregs[i], REG_SP, 0 * 8);
591                         }
592                 }
593         }
594
595         disp = dseg_add_address(cd, m);
596         M_ALD(REG_ITMP1, REG_PV, disp);
597         M_AST(REG_ITMP1, REG_SP, 0 * 8);
598         disp = dseg_add_functionptr(cd, builtin_verbosecall_enter);
599         M_ALD(REG_PV, REG_PV, disp);
600         M_JSR(REG_RA, REG_PV);
601         disp = (s4) (cd->mcodeptr - cd->mcodebase);
602         M_LDA(REG_PV, REG_RA, -disp);
603         M_ALD(REG_RA, REG_SP, 1 * 8);
604
605         /* restore argument registers */
606
607         for (i = 0; i < INT_ARG_CNT; i++)
608                 M_LLD(rd->argintregs[i], REG_SP, (2 + i) * 8);
609
610         for (i = 0; i < FLT_ARG_CNT; i++)
611                 M_DLD(rd->argintregs[i], REG_SP, (2 + INT_ARG_CNT + i) * 8);
612
613         /* restore temporary registers for leaf methods */
614
615         if (jd->isleafmethod) {
616                 for (i = 0; i < INT_TMP_CNT; i++)
617                         M_LLD(rd->tmpintregs[i], REG_SP, (2 + ARG_CNT + i) * 8);
618
619                 for (i = 0; i < FLT_TMP_CNT; i++)
620                         M_DLD(rd->tmpfltregs[i], REG_SP, (2 + ARG_CNT + INT_TMP_CNT + i) * 8);
621         }
622
623         M_LDA(REG_SP, REG_SP, (ARG_CNT + TMP_CNT + 2) * 8);
624
625         /* mark trace code */
626
627         M_NOP;
628 }
629 #endif /* !defined(NDEBUG) */
630
631
632 /* emit_verbosecall_exit *******************************************************
633
634    Generates the code for the call trace.
635
636    void builtin_verbosecall_exit(s8 l, double d, float f, methodinfo *m);
637
638 *******************************************************************************/
639
640 #if !defined(NDEBUG)
641 void emit_verbosecall_exit(jitdata *jd)
642 {
643         methodinfo   *m;
644         codegendata  *cd;
645         registerdata *rd;
646         s4            disp;
647
648         /* get required compiler data */
649
650         m  = jd->m;
651         cd = jd->cd;
652         rd = jd->rd;
653
654         /* mark trace code */
655
656         M_NOP;
657
658         M_ASUB_IMM(REG_SP, 4 * 8, REG_SP);
659         M_AST(REG_RA, REG_SP, 0 * 8);
660
661         M_LST(REG_RESULT, REG_SP, 1 * 8);
662         M_DST(REG_FRESULT, REG_SP, 2 * 8);
663
664         M_MOV(REG_RESULT, REG_A0);
665         M_FMOV(REG_FRESULT, REG_FA1);
666         M_FMOV(REG_FRESULT, REG_FA2);
667
668         disp = dseg_add_address(cd, m);
669         M_ALD(REG_A3, REG_PV, disp);
670
671         disp = dseg_add_functionptr(cd, builtin_verbosecall_exit);
672         M_ALD(REG_PV, REG_PV, disp);
673         M_JSR(REG_RA, REG_PV);
674         disp = (cd->mcodeptr - cd->mcodebase);
675         M_LDA(REG_PV, REG_RA, -disp);
676
677         M_DLD(REG_FRESULT, REG_SP, 2 * 8);
678         M_LLD(REG_RESULT, REG_SP, 1 * 8);
679
680         M_ALD(REG_RA, REG_SP, 0 * 8);
681         M_AADD_IMM(REG_SP, 4 * 8, REG_SP);
682
683         /* mark trace code */
684
685         M_NOP;
686 }
687 #endif /* !defined(NDEBUG) */
688
689
690 /*
691  * These are local overrides for various environment variables in Emacs.
692  * Please do not remove this and leave it at the end of the file, where
693  * Emacs will automagically detect them.
694  * ---------------------------------------------------------------------
695  * Local variables:
696  * mode: c
697  * indent-tabs-mode: t
698  * c-basic-offset: 4
699  * tab-width: 4
700  * End:
701  * vim:noexpandtab:sw=4:ts=4:
702  */