1 /* src/vm/jit/powerpc64/emit.c - PowerPC 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: emitfuncs.c 4398 2006-01-31 23:43:08Z twisti $
44 #include "vm/jit/emit.h"
45 #include "vm/jit/jit.h"
46 #include "vm/jit/powerpc64/codegen.h"
47 #include "vm/builtin.h"
50 /* code generation functions **************************************************/
52 /* emit_load *******************************************************************
54 Emits a possible load of an operand.
56 *******************************************************************************/
58 s4 emit_load(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
64 /* get required compiler data */
68 if (src->flags & INMEMORY) {
71 disp = src->regoff * 8;
73 if (IS_FLT_DBL_TYPE(src->type)) {
74 if (IS_2_WORD_TYPE(src->type))
75 M_DLD(tempreg, REG_SP, disp);
77 M_FLD(tempreg, REG_SP, disp);
80 if (IS_2_WORD_TYPE(src->type))
81 M_LLD(tempreg, REG_SP, disp);
83 M_ILD(tempreg, REG_SP, disp);
94 /* emit_load_s1 ****************************************************************
96 Emits a possible load of the first source operand.
98 *******************************************************************************/
100 s4 emit_load_s1(jitdata *jd, instruction *iptr, s4 tempreg)
107 reg = emit_load(jd, iptr, src, tempreg);
113 /* emit_load_s2 ****************************************************************
115 Emits a possible load of the second source operand.
117 *******************************************************************************/
119 s4 emit_load_s2(jitdata *jd, instruction *iptr, s4 tempreg)
124 src = iptr->sx.s23.s2.var;
126 reg = emit_load(jd, iptr, src, tempreg);
132 /* emit_load_s3 ****************************************************************
134 Emits a possible load of the third source operand.
136 *******************************************************************************/
138 s4 emit_load_s3(jitdata *jd, instruction *iptr, s4 tempreg)
143 src = iptr->sx.s23.s3.var;
145 reg = emit_load(jd, iptr, src, tempreg);
152 /* emit_store ******************************************************************
154 Emits a possible store to a variable.
156 *******************************************************************************/
158 void emit_store(jitdata *jd, instruction *iptr, stackptr dst, s4 d)
162 /* get required compiler data */
166 if (dst->flags & INMEMORY) {
169 if (IS_FLT_DBL_TYPE(dst->type)) {
170 if (IS_2_WORD_TYPE(dst->type))
171 M_DST(d, REG_SP, dst->regoff * 8);
173 M_FST(d, REG_SP, dst->regoff * 8);
176 M_LST(d, REG_SP, dst->regoff * 8);
182 /* emit_store_dst **************************************************************
184 Emits a possible store to the destination operand of an instruction.
186 *******************************************************************************/
188 void emit_store_dst(jitdata *jd, instruction *iptr, s4 d)
190 emit_store(jd, iptr, iptr->dst.var, d);
194 /* emit_copy *******************************************************************
198 *******************************************************************************/
200 void emit_copy(jitdata *jd, instruction *iptr, stackptr src, stackptr dst)
206 /* get required compiler data */
211 if ((src->regoff != dst->regoff) ||
212 ((src->flags ^ dst->flags) & INMEMORY)) {
214 d = codegen_reg_of_var(rd, iptr->opc, dst, REG_IFTMP);
215 s1 = emit_load_s1(jd, iptr, src, d);
217 if (IS_FLT_DBL_TYPE(src->type))
222 emit_store(jd, iptr, dst, d);
227 /* emit_iconst *****************************************************************
231 *******************************************************************************/
233 void emit_iconst(codegendata *cd, s4 d, s4 value)
237 if ((value >= -32768) && (value <= 32767))
238 M_LDA_INTERN(d, REG_ZERO, value);
240 disp = dseg_adds4(cd, value);
241 M_ILD(d, REG_PV, disp);
246 /* emit_verbosecall_enter ******************************************************
248 * Generates the code for the call trace.
250 ********************************************************************************/
252 void emit_verbosecall_enter (jitdata *jd)
262 /* get required compiler data */
270 /* Build up Stackframe for builtin_trace_args call (a multiple of 16) */
274 /* setup stack for TRACE_ARGS_NUM registers */
275 /* == LA_SIZE + PA_SIZE + 8 (methodinfo argument) + TRACE_ARGS_NUM*8 + 8 (itmp1) */
277 /* in nativestubs no Place to save the LR (Link Register) would be needed */
278 /* but since the stack frame has to be aligned the 4 Bytes would have to */
279 /* be padded again */
281 #if defined(__DARWIN__)
282 stack_size = LA_SIZE + (TRACE_ARGS_NUM + 1) * 8;
284 stack_size = LA_SIZE + PA_SIZE + 8 + TRACE_ARGS_NUM * 8 + 8;
287 /* mark trace code */
290 /* save up to TRACE_ARGS_NUM arguments into the reserved stack space */
292 #if defined(__DARWIN__)
293 /* Copy Params starting from first to Stack */
294 /* since TRACE_ARGS == INT_ARG_CNT all used integer argument regs */
298 /* Copy Params starting from fifth to Stack (INT_ARG_CNT/2) are in */
299 /* integer argument regs */
300 /* all integer argument registers have to be saved */
301 for (p = 0; p < 8; p++) {
302 d = rd->argintregs[p];
303 /* save integer argument registers */
304 M_LST(d, REG_SP, LA_SIZE + PA_SIZE + 4 * 8 + 8 + p * 8);
310 M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
311 M_STDU(REG_SP, REG_SP, -stack_size);
313 for (p = 0; p < md->paramcount && p < TRACE_ARGS_NUM; p++) {
314 t = md->paramtypes[p].type;
315 if (IS_INT_LNG_TYPE(t)) {
316 if (!md->params[p].inmemory) { /* Param in Arg Reg */
317 M_LST(rd->argintregs[md->params[p].regoff], REG_SP, LA_SIZE + PA_SIZE + 8 + p * 8);
318 } else { /* Param on Stack */
319 s1 = (md->params[p].regoff + cd->stackframesize) * 8 + stack_size;
320 M_LLD(REG_ITMP2, REG_SP, s1);
321 M_LST(REG_ITMP2, REG_SP, LA_SIZE + PA_SIZE + 8 + p * 8);
323 } else { /* IS_FLT_DBL_TYPE(t) */
324 if (!md->params[p].inmemory) { /* in Arg Reg */
325 s1 = rd->argfltregs[md->params[p].regoff];
326 M_DST(s1, REG_SP, LA_SIZE + PA_SIZE + 8 + p * 8);
327 } else { /* on Stack */
328 /* this should not happen */
334 /* load first 4 (==INT_ARG_CNT/2) arguments into integer registers */
335 #if defined(__DARWIN__)
336 for (p = 0; p < 8; p++) {
337 d = rd->argintregs[p];
338 M_ILD(d, REG_SP, LA_SIZE + p * 4);
342 /* Set integer and float argument registers for trace_args call */
343 /* offset to saved integer argument registers */
344 for (p = 0; (p < TRACE_ARGS_NUM) && (p < md->paramcount); p++) {
345 t = md->paramtypes[p].type;
346 if (IS_INT_LNG_TYPE(t)) {
347 M_LLD(rd->argintregs[p], REG_SP,LA_SIZE + PA_SIZE + 8 + p * 8);
348 } else { /* Float/Dbl */
349 if (!md->params[p].inmemory) { /* Param in Arg Reg */
350 /* use reserved Place on Stack (sp + 5 * 16) to copy */
351 /* float/double arg reg to int reg */
352 s1 = rd->argfltregs[md->params[p].regoff];
353 M_MOV(s1, rd->argintregs[p]);
361 /* put methodinfo pointer on Stackframe */
362 p = dseg_addaddress(cd, m);
363 M_ALD(REG_ITMP1, REG_PV, p);
364 #if defined(__DARWIN__)
365 M_AST(REG_ITMP1, REG_SP, LA_SIZE + TRACE_ARGS_NUM * 8);
367 if (TRACE_ARGS_NUM == 8) {
368 /* need to pass via stack */
369 M_AST(REG_ITMP1, REG_SP, LA_SIZE + PA_SIZE);
371 /* pass via register, reg 3 is the first */
372 M_MOV(REG_ITMP1, 3 + TRACE_ARGS_NUM);
375 /* call via function descriptor */
376 /* XXX: what about TOC? */
377 p = dseg_addaddress(cd, builtin_trace_args);
378 M_ALD(REG_ITMP2, REG_PV, p);
379 M_ALD(REG_ITMP1, REG_ITMP2, 0);
383 #if defined(__DARWIN__)
384 /* restore integer argument registers from the reserved stack space */
387 for (p = 0; p < md->paramcount && p < TRACE_ARGS_NUM; p++, stack_off += 8) {
388 t = md->paramtypes[p].type;
390 if (IS_INT_LNG_TYPE(t)) {
391 if (!md->params[p].inmemory) {
392 M_LLD(rd->argintregs[md->params[p].regoff], REG_SP, stack_off);
400 for (p = 0; p < md->paramcount && p < TRACE_ARGS_NUM; p++) {
401 d = rd->argintregs[p];
402 /* restore integer argument registers */
403 M_LLD(d, REG_SP, LA_SIZE + PA_SIZE + 8 + p * 8);
406 M_ALD(REG_ZERO, REG_SP, stack_size + LA_LR_OFFSET);
408 M_LDA(REG_SP, REG_SP, stack_size);
410 /* mark trace code */
415 /* emit_verbosecall_exit ******************************************************
417 * Generates the code for the call trace.
419 ********************************************************************************/
421 void emit_verbosecall_exit(jitdata *jd)
423 codegendata *cd = jd->cd;
426 /* mark trace code */
430 M_LDA(REG_SP, REG_SP, -(LA_SIZE+PA_SIZE+10*8));
431 M_DST(REG_FRESULT, REG_SP, LA_SIZE+PA_SIZE+0*8);
432 M_LST(REG_RESULT, REG_SP, LA_SIZE+PA_SIZE+1*8);
433 M_AST(REG_ZERO, REG_SP, LA_SIZE+PA_SIZE+2*8);
435 #if defined(__DARWIN__)
436 M_MOV(REG_RESULT, jd->rd->argintregs[1]);
438 M_MOV(REG_RESULT, jd->rd->argintregs[1]);
441 disp = dseg_addaddress(cd, jd->m);
442 M_ALD(jd->rd->argintregs[0], REG_PV, disp);
444 M_FLTMOVE(REG_FRESULT, jd->rd->argfltregs[0]);
445 M_FLTMOVE(REG_FRESULT, jd->rd->argfltregs[1]);
446 disp = dseg_addaddress(cd, builtin_displaymethodstop);
447 /* call via function descriptor, XXX: what about TOC ? */
448 M_ALD(REG_ITMP2, REG_PV, disp);
449 M_ALD(REG_ITMP2, REG_ITMP2, 0);
453 M_DLD(REG_FRESULT, REG_SP, LA_SIZE+PA_SIZE+0*8);
454 M_LLD(REG_RESULT, REG_SP, LA_SIZE+PA_SIZE+1*8);
455 M_ALD(REG_ZERO, REG_SP, LA_SIZE+PA_SIZE+2*8);
456 M_LDA(REG_SP, REG_SP, LA_SIZE+PA_SIZE+10*8);
459 /* mark trace code */
466 * These are local overrides for various environment variables in Emacs.
467 * Please do not remove this and leave it at the end of the file, where
468 * Emacs will automagically detect them.
469 * ---------------------------------------------------------------------
472 * indent-tabs-mode: t
476 * vim:noexpandtab:sw=4:ts=4: