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