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