* src/vm/jit/alpha/emit.c (config.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/jit/asmpart.h"
48 #include "vm/jit/dseg.h"
49 #include "vm/jit/emit.h"
50 #include "vm/jit/jit.h"
51
52
53 /* code generation functions **************************************************/
54
55 /* emit_load_s1 ****************************************************************
56
57    Emits a possible load of the first source operand.
58
59 *******************************************************************************/
60
61 s4 emit_load_s1(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
62 {
63         codegendata  *cd;
64         s4            reg;
65
66         /* get required compiler data */
67
68         cd = jd->cd;
69
70         if (src->flags & INMEMORY) {
71                 COUNT_SPILLS;
72
73                 if (IS_FLT_DBL_TYPE(src->type))
74                         M_DLD(tempreg, REG_SP, src->regoff * 8);
75                 else
76                         M_LLD(tempreg, REG_SP, src->regoff * 8);
77
78                 reg = tempreg;
79         } else
80                 reg = src->regoff;
81
82         return reg;
83 }
84
85
86 /* emit_load_s2 ****************************************************************
87
88    Emits a possible load of the second source operand.
89
90 *******************************************************************************/
91
92 s4 emit_load_s2(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
93 {
94         codegendata  *cd;
95         s4            reg;
96
97         /* get required compiler data */
98
99         cd = jd->cd;
100
101         if (src->flags & INMEMORY) {
102                 COUNT_SPILLS;
103
104                 if (IS_FLT_DBL_TYPE(src->type))
105                         M_DLD(tempreg, REG_SP, src->regoff * 8);
106                 else
107                         M_LLD(tempreg, REG_SP, src->regoff * 8);
108
109                 reg = tempreg;
110         } else
111                 reg = src->regoff;
112
113         return reg;
114 }
115
116
117 /* emit_load_s3 ****************************************************************
118
119    Emits a possible load of the third source operand.
120
121 *******************************************************************************/
122
123 s4 emit_load_s3(jitdata *jd, instruction *iptr, stackptr src, s4 tempreg)
124 {
125         codegendata  *cd;
126         s4            reg;
127
128         /* get required compiler data */
129
130         cd = jd->cd;
131
132         if (src->flags & INMEMORY) {
133                 COUNT_SPILLS;
134
135                 if (IS_FLT_DBL_TYPE(src->type))
136                         M_DLD(tempreg, REG_SP, src->regoff * 8);
137                 else
138                         M_LLD(tempreg, REG_SP, src->regoff * 8);
139
140                 reg = tempreg;
141         } else
142                 reg = src->regoff;
143
144         return reg;
145 }
146
147
148 /* emit_store ******************************************************************
149
150    XXX
151
152 *******************************************************************************/
153
154 void emit_store(jitdata *jd, instruction *iptr, stackptr dst, s4 d)
155 {
156         codegendata  *cd;
157
158         /* get required compiler data */
159
160         cd = jd->cd;
161
162         if (dst->flags & INMEMORY) {
163                 COUNT_SPILLS;
164
165                 if (IS_FLT_DBL_TYPE(dst->type))
166                         M_DST(d, REG_SP, dst->regoff * 8);
167                 else
168                         M_LST(d, REG_SP, dst->regoff * 8);
169         }
170 }
171
172
173 void emit_copy(jitdata *jd, instruction *iptr, stackptr src, stackptr dst)
174 {
175         codegendata  *cd;
176         registerdata *rd;
177         s4            s1, d;
178
179         /* get required compiler data */
180
181         cd = jd->cd;
182         rd = jd->rd;
183
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);
188
189                 if (IS_FLT_DBL_TYPE(src->type))
190                         M_FLTMOVE(s1, d);
191                 else
192                         M_INTMOVE(s1, d);
193
194                 emit_store(jd, iptr, dst, d);
195         }
196 }
197
198
199 void emit_iconst(codegendata *cd, s4 d, s4 value)
200 {
201         s4 disp;
202
203         if ((value >= -32768) && (value <= 32767)) {
204                 M_LDA_INTERN(d, REG_ZERO, value);
205         } else {
206                 disp = dseg_adds4(cd, value);
207                 M_ILD(d, REG_PV, disp);
208         }
209 }
210
211
212 void emit_lconst(codegendata *cd, s4 d, s8 value)
213 {
214         s4 disp;
215
216         if ((value >= -32768) && (value <= 32767)) {
217                 M_LDA_INTERN(d, REG_ZERO, value);
218         } else {
219                 disp = dseg_adds8(cd, value);
220                 M_LLD(d, REG_PV, disp);
221         }
222 }
223
224
225 /* emit_exception_stubs ********************************************************
226
227    Generates the code for the exception stubs.
228
229 *******************************************************************************/
230
231 void emit_exception_stubs(jitdata *jd)
232 {
233         codegendata  *cd;
234         registerdata *rd;
235         exceptionref *eref;
236         u1           *savedmcodeptr;
237         s4            disp;
238
239         /* get required compiler data */
240
241         cd = jd->cd;
242         rd = jd->rd;
243
244         savedmcodeptr = NULL;
245
246         /* generate exception stubs */
247
248         for (eref = cd->exceptionrefs; eref != NULL; eref = eref->next) {
249                 gen_resolvebranch(cd->mcodebase + eref->branchpos, 
250                                                   eref->branchpos, cd->mcodeptr - cd->mcodebase);
251
252                 MCODECHECK(100);
253
254                 /* move index register into REG_ITMP1 */
255
256                 /* Check if the exception is an
257                    ArrayIndexOutOfBoundsException.  If so, move index register
258                    into a4. */
259
260                 if (eref->reg != -1)
261                         M_MOV(eref->reg, rd->argintregs[4]);
262
263                 /* calcuate exception address */
264
265                 M_LDA(rd->argintregs[3], REG_PV, eref->branchpos - 4);
266
267                 /* move function to call into REG_ITMP3 */
268
269                 disp = dseg_add_functionptr(cd, eref->function);
270                 M_ALD(REG_ITMP3, REG_PV, disp);
271
272                 if (savedmcodeptr != NULL) {
273                         disp = ((u4 *) savedmcodeptr) - (((u4 *) cd->mcodeptr) + 1);
274                         M_BR(disp);
275                 }
276                 else {
277                         savedmcodeptr = cd->mcodeptr;
278
279                         M_MOV(REG_PV, rd->argintregs[0]);
280                         M_MOV(REG_SP, rd->argintregs[1]);
281
282                         if (jd->isleafmethod)
283                                 M_MOV(REG_RA, rd->argintregs[2]);
284                         else
285                                 M_ALD(rd->argintregs[2],
286                                           REG_SP, jd->stackframesize * 8 - SIZEOF_VOID_P);
287
288                         M_LDA(REG_SP, REG_SP, -2 * 8);
289                         M_AST(rd->argintregs[3], REG_SP, 0 * 8);             /* store XPC */
290
291                         if (jd->isleafmethod)
292                                 M_AST(REG_RA, REG_SP, 1 * 8);
293
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);
298
299                         M_MOV(REG_RESULT, REG_ITMP1_XPTR);
300
301                         if (jd->isleafmethod)
302                                 M_ALD(REG_RA, REG_SP, 1 * 8);
303
304                         M_ALD(REG_ITMP2_XPC, REG_SP, 0 * 8);
305                         M_LDA(REG_SP, REG_SP, 2 * 8);
306
307                         disp = dseg_add_functionptr(cd, asm_handle_exception);
308                         M_ALD(REG_ITMP3, REG_PV, disp);
309                         M_JMP(REG_ZERO, REG_ITMP3);
310                 }
311         }
312 }
313
314
315 /* emit_patcher_stubs **********************************************************
316
317    Generates the code for the patcher stubs.
318
319 *******************************************************************************/
320
321 void emit_patcher_stubs(jitdata *jd)
322 {
323         codegendata *cd;
324         patchref    *pref;
325         u4           mcode;
326         u1          *savedmcodeptr;
327         u1          *tmpmcodeptr;
328         s4           disp;
329
330         /* get required compiler data */
331
332         cd = jd->cd;
333
334         /* generate code patching stub call code */
335
336         for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
337                 /* check code segment size */
338
339                 MCODECHECK(100);
340
341                 /* Get machine code which is patched back in later. The
342                    call is 1 instruction word long. */
343
344                 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
345
346                 mcode = *((u4 *) tmpmcodeptr);
347
348                 /* Patch in the call to call the following code (done at
349                    compile time). */
350
351                 savedmcodeptr = cd->mcodeptr;   /* save current mcodeptr              */
352                 cd->mcodeptr  = tmpmcodeptr;    /* set mcodeptr to patch position     */
353
354                 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
355                 M_BSR(REG_ITMP3, disp);
356
357                 cd->mcodeptr = savedmcodeptr;   /* restore the current mcodeptr       */
358
359                 /* create stack frame */
360
361                 M_LSUB_IMM(REG_SP, 6 * 8, REG_SP);
362
363                 /* move return address onto stack */
364
365                 M_AST(REG_ITMP3, REG_SP, 5 * 8);
366
367                 /* move pointer to java_objectheader onto stack */
368
369 #if defined(ENABLE_THREADS)
370                 /* create a virtual java_objectheader */
371
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   */
375
376                 M_LDA(REG_ITMP3, REG_PV, disp);
377                 M_AST(REG_ITMP3, REG_SP, 4 * 8);
378 #else
379                 M_AST(REG_ZERO, REG_SP, 4 * 8);
380 #endif
381
382                 /* move machine code onto stack */
383
384                 disp = dseg_add_s4(cd, mcode);
385                 M_ILD(REG_ITMP3, REG_PV, disp);
386                 M_IST(REG_ITMP3, REG_SP, 3 * 8);
387
388                 /* move class/method/field reference onto stack */
389
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);
393
394                 /* move data segment displacement onto stack */
395
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);
399
400                 /* move patcher function pointer onto stack */
401
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);
405
406                 disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
407                 M_ALD(REG_ITMP3, REG_PV, disp);
408                 M_JMP(REG_ZERO, REG_ITMP3);
409         }
410 }
411
412
413 /* emit_replacement_stubs ******************************************************
414
415    Generates the code for the replacement stubs.
416
417 *******************************************************************************/
418
419 void emit_replacement_stubs(jitdata *jd)
420 {
421         codegendata *cd;
422         codeinfo    *code;
423         rplpoint    *rplp;
424         u1          *savedmcodeptr;
425         s4           disp;
426         s4           i;
427
428         /* get required compiler data */
429
430         cd   = jd->cd;
431         code = jd->code;
432
433         rplp = code->rplpoints;
434
435         for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
436                 /* check code segment size */
437
438                 MCODECHECK(100);
439
440                 /* note start of stub code */
441
442                 rplp->outcode = (u1 *) (ptrint) (cd->mcodeptr - cd->mcodebase);
443
444                 /* make machine code for patching */
445
446                 savedmcodeptr = cd->mcodeptr;
447                 cd->mcodeptr  = (u1 *) &(rplp->mcode);
448
449                 disp = (ptrint) ((s4 *) rplp->outcode - (s4 *) rplp->pc) - 1;
450                 M_BR(disp);
451
452                 cd->mcodeptr = savedmcodeptr;
453
454                 /* create stack frame - 16-byte aligned */
455
456                 M_LSUB_IMM(REG_SP, 2 * 8, REG_SP);
457
458                 /* push address of `rplpoint` struct */
459
460                 disp = dseg_add_address(cd, rplp);
461                 M_ALD(REG_ITMP3, REG_PV, disp);
462                 M_AST(REG_ITMP3, REG_SP, 0 * 8);
463
464                 /* jump to replacement function */
465
466                 disp = dseg_add_functionptr(cd, asm_replacement_out);
467                 M_ALD(REG_ITMP3, REG_PV, disp);
468                 M_JMP(REG_ZERO, REG_ITMP3);
469         }
470 }
471
472
473 /*
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  * ---------------------------------------------------------------------
478  * Local variables:
479  * mode: c
480  * indent-tabs-mode: t
481  * c-basic-offset: 4
482  * tab-width: 4
483  * End:
484  * vim:noexpandtab:sw=4:ts=4:
485  */