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