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