1 /* src/vm/jit/alpha/emit.c - Alpha code emitter functions
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
8 This file is part of CACAO.
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.
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.
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
25 Contact: cacao@cacaojvm.org
27 Authors: Christian Thalinger
31 $Id: emit.c 4398 2006-01-31 23:43:08Z twisti $
41 #include "vm/jit/alpha/codegen.h"
43 #if defined(ENABLE_THREADS)
44 # include "threads/native/lock.h"
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/dseg.h"
49 #include "vm/jit/emit.h"
50 #include "vm/jit/jit.h"
53 /* code generation functions **************************************************/
55 /* emit_load_s1 ****************************************************************
57 Emits a possible load of the first source operand.
59 *******************************************************************************/
61 s4 emit_load_s1(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
66 /* get required compiler data */
70 if (src->flags & INMEMORY) {
73 if (IS_FLT_DBL_TYPE(src->type))
74 M_DLD(tempreg, REG_SP, src->regoff * 8);
76 M_LLD(tempreg, REG_SP, src->regoff * 8);
86 /* emit_load_s2 ****************************************************************
88 Emits a possible load of the second source operand.
90 *******************************************************************************/
92 s4 emit_load_s2(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
97 /* get required compiler data */
101 if (src->flags & INMEMORY) {
104 if (IS_FLT_DBL_TYPE(src->type))
105 M_DLD(tempreg, REG_SP, src->regoff * 8);
107 M_LLD(tempreg, REG_SP, src->regoff * 8);
117 /* emit_load_s3 ****************************************************************
119 Emits a possible load of the third source operand.
121 *******************************************************************************/
123 s4 emit_load_s3(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
128 /* get required compiler data */
132 if (src->flags & INMEMORY) {
135 if (IS_FLT_DBL_TYPE(src->type))
136 M_DLD(tempreg, REG_SP, src->regoff * 8);
138 M_LLD(tempreg, REG_SP, src->regoff * 8);
148 /* emit_store ******************************************************************
152 *******************************************************************************/
154 void emit_store(jitdata *jd, instruction *iptr, stackptr dst, s4 d)
158 /* get required compiler data */
162 if (dst->flags & INMEMORY) {
165 if (IS_FLT_DBL_TYPE(dst->type))
166 M_DST(d, REG_SP, dst->regoff * 8);
168 M_LST(d, REG_SP, dst->regoff * 8);
173 void emit_copy(jitdata *jd, instruction *iptr, stackptr src, stackptr dst)
179 /* get required compiler data */
184 if ((src->regoff != dst->regoff) ||
185 ((src->flags ^ dst->flags) & INMEMORY)) {
186 d = codegen_reg_of_var(rd, iptr->opc, dst, REG_IFTMP);
187 s1 = emit_load_s1(jd, iptr, src, d);
189 if (IS_FLT_DBL_TYPE(src->type))
194 emit_store(jd, iptr, dst, d);
199 void emit_iconst(codegendata *cd, s4 d, s4 value)
203 if ((value >= -32768) && (value <= 32767)) {
204 M_LDA_INTERN(d, REG_ZERO, value);
206 disp = dseg_adds4(cd, value);
207 M_ILD(d, REG_PV, disp);
212 void emit_lconst(codegendata *cd, s4 d, s8 value)
216 if ((value >= -32768) && (value <= 32767)) {
217 M_LDA_INTERN(d, REG_ZERO, value);
219 disp = dseg_adds8(cd, value);
220 M_LLD(d, REG_PV, disp);
225 /* emit_exception_stubs ********************************************************
227 Generates the code for the exception stubs.
229 *******************************************************************************/
231 void emit_exception_stubs(jitdata *jd)
239 /* get required compiler data */
244 savedmcodeptr = NULL;
246 /* generate exception stubs */
248 for (eref = cd->exceptionrefs; eref != NULL; eref = eref->next) {
249 gen_resolvebranch(cd->mcodebase + eref->branchpos,
250 eref->branchpos, cd->mcodeptr - cd->mcodebase);
254 /* move index register into REG_ITMP1 */
256 /* Check if the exception is an
257 ArrayIndexOutOfBoundsException. If so, move index register
261 M_MOV(eref->reg, rd->argintregs[4]);
263 /* calcuate exception address */
265 M_LDA(rd->argintregs[3], REG_PV, eref->branchpos - 4);
267 /* move function to call into REG_ITMP3 */
269 disp = dseg_add_functionptr(cd, eref->function);
270 M_ALD(REG_ITMP3, REG_PV, disp);
272 if (savedmcodeptr != NULL) {
273 disp = ((u4 *) savedmcodeptr) - (((u4 *) cd->mcodeptr) + 1);
277 savedmcodeptr = cd->mcodeptr;
279 M_MOV(REG_PV, rd->argintregs[0]);
280 M_MOV(REG_SP, rd->argintregs[1]);
282 if (jd->isleafmethod)
283 M_MOV(REG_RA, rd->argintregs[2]);
285 M_ALD(rd->argintregs[2],
286 REG_SP, jd->stackframesize * 8 - SIZEOF_VOID_P);
288 M_LDA(REG_SP, REG_SP, -2 * 8);
289 M_AST(rd->argintregs[3], REG_SP, 0 * 8); /* store XPC */
291 if (jd->isleafmethod)
292 M_AST(REG_RA, REG_SP, 1 * 8);
294 M_MOV(REG_ITMP3, REG_PV);
295 M_JSR(REG_RA, REG_PV);
296 disp = (s4) (cd->mcodeptr - cd->mcodebase);
297 M_LDA(REG_PV, REG_RA, -disp);
299 M_MOV(REG_RESULT, REG_ITMP1_XPTR);
301 if (jd->isleafmethod)
302 M_ALD(REG_RA, REG_SP, 1 * 8);
304 M_ALD(REG_ITMP2_XPC, REG_SP, 0 * 8);
305 M_LDA(REG_SP, REG_SP, 2 * 8);
307 disp = dseg_add_functionptr(cd, asm_handle_exception);
308 M_ALD(REG_ITMP3, REG_PV, disp);
309 M_JMP(REG_ZERO, REG_ITMP3);
315 /* emit_patcher_stubs **********************************************************
317 Generates the code for the patcher stubs.
319 *******************************************************************************/
321 void emit_patcher_stubs(jitdata *jd)
330 /* get required compiler data */
334 /* generate code patching stub call code */
336 for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
337 /* check code segment size */
341 /* Get machine code which is patched back in later. The
342 call is 1 instruction word long. */
344 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
346 mcode = *((u4 *) tmpmcodeptr);
348 /* Patch in the call to call the following code (done at
351 savedmcodeptr = cd->mcodeptr; /* save current mcodeptr */
352 cd->mcodeptr = tmpmcodeptr; /* set mcodeptr to patch position */
354 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
355 M_BSR(REG_ITMP3, disp);
357 cd->mcodeptr = savedmcodeptr; /* restore the current mcodeptr */
359 /* create stack frame */
361 M_LSUB_IMM(REG_SP, 6 * 8, REG_SP);
363 /* move return address onto stack */
365 M_AST(REG_ITMP3, REG_SP, 5 * 8);
367 /* move pointer to java_objectheader onto stack */
369 #if defined(ENABLE_THREADS)
370 /* create a virtual java_objectheader */
372 (void) dseg_add_unique_address(cd, NULL); /* flcword */
373 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
374 disp = dseg_add_unique_address(cd, NULL); /* vftbl */
376 M_LDA(REG_ITMP3, REG_PV, disp);
377 M_AST(REG_ITMP3, REG_SP, 4 * 8);
379 M_AST(REG_ZERO, REG_SP, 4 * 8);
382 /* move machine code onto stack */
384 disp = dseg_add_s4(cd, mcode);
385 M_ILD(REG_ITMP3, REG_PV, disp);
386 M_IST(REG_ITMP3, REG_SP, 3 * 8);
388 /* move class/method/field reference onto stack */
390 disp = dseg_add_address(cd, pref->ref);
391 M_ALD(REG_ITMP3, REG_PV, disp);
392 M_AST(REG_ITMP3, REG_SP, 2 * 8);
394 /* move data segment displacement onto stack */
396 disp = dseg_add_s4(cd, pref->disp);
397 M_ILD(REG_ITMP3, REG_PV, disp);
398 M_IST(REG_ITMP3, REG_SP, 1 * 8);
400 /* move patcher function pointer onto stack */
402 disp = dseg_add_functionptr(cd, pref->patcher);
403 M_ALD(REG_ITMP3, REG_PV, disp);
404 M_AST(REG_ITMP3, REG_SP, 0 * 8);
406 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
407 M_ALD(REG_ITMP3, REG_PV, disp);
408 M_JMP(REG_ZERO, REG_ITMP3);
413 /* emit_replacement_stubs ******************************************************
415 Generates the code for the replacement stubs.
417 *******************************************************************************/
419 void emit_replacement_stubs(jitdata *jd)
428 /* get required compiler data */
433 rplp = code->rplpoints;
435 for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
436 /* check code segment size */
440 /* note start of stub code */
442 rplp->outcode = (u1 *) (ptrint) (cd->mcodeptr - cd->mcodebase);
444 /* make machine code for patching */
446 savedmcodeptr = cd->mcodeptr;
447 cd->mcodeptr = (u1 *) &(rplp->mcode);
449 disp = (ptrint) ((s4 *) rplp->outcode - (s4 *) rplp->pc) - 1;
452 cd->mcodeptr = savedmcodeptr;
454 /* create stack frame - 16-byte aligned */
456 M_LSUB_IMM(REG_SP, 2 * 8, REG_SP);
458 /* push address of `rplpoint` struct */
460 disp = dseg_add_address(cd, rplp);
461 M_ALD(REG_ITMP3, REG_PV, disp);
462 M_AST(REG_ITMP3, REG_SP, 0 * 8);
464 /* jump to replacement function */
466 disp = dseg_add_functionptr(cd, asm_replacement_out);
467 M_ALD(REG_ITMP3, REG_PV, disp);
468 M_JMP(REG_ZERO, REG_ITMP3);
474 * These are local overrides for various environment variables in Emacs.
475 * Please do not remove this and leave it at the end of the file, where
476 * Emacs will automagically detect them.
477 * ---------------------------------------------------------------------
480 * indent-tabs-mode: t
484 * vim:noexpandtab:sw=4:ts=4: