Merged revisions 8056-8122 via svnmerge from
[cacao.git] / src / vm / jit / s390 / emit.c
1 /* src/vm/jit/s390/emit.c - s390 code emitter functions
2
3    Copyright (C) 1996-2005, 2006, 2007 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    $Id: emit.c 8123 2007-06-20 23:50:55Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33
34 #include "vm/types.h"
35
36 #include "md-abi.h"
37
38 #include "vm/jit/s390/codegen.h"
39 #include "vm/jit/s390/emit.h"
40
41 #if defined(ENABLE_THREADS)
42 # include "threads/native/lock.h"
43 #endif
44
45 #include "vm/builtin.h"
46 #include "vm/jit/abi-asm.h"
47 #include "vm/jit/asmpart.h"
48 #include "vm/jit/codegen-common.h"
49 #include "vm/jit/emit-common.h"
50 #include "vm/jit/jit.h"
51 #include "vm/jit/replace.h"
52 #include "vm/jit/abi.h"
53 #include "vm/global.h"
54 #include "mm/memory.h"
55 #include "vm/exceptions.h"
56
57 #define __PORTED__
58
59 /* emit_load *******************************************************************
60
61    Emits a possible load of an operand.
62
63 *******************************************************************************/
64
65 __PORTED__ s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
66 {
67         codegendata *cd;
68         s4           disp;
69         s4           reg;
70
71         /* get required compiler data */
72
73         cd = jd->cd;
74
75         if (IS_INMEMORY(src->flags)) {
76                 COUNT_SPILLS;
77
78                 disp = src->vv.regoff;
79
80                 if (IS_FLT_DBL_TYPE(src->type)) {
81                         if (IS_2_WORD_TYPE(src->type))
82                                 M_DLD(tempreg, REG_SP, disp);
83                         else
84                                 M_FLD(tempreg, REG_SP, disp);
85                 }
86                 else {
87                         if (IS_2_WORD_TYPE(src->type))
88                                 M_LLD(tempreg, REG_SP, disp);
89                         else
90                                 M_ILD(tempreg, REG_SP, disp);
91                 }
92
93                 reg = tempreg;
94         }
95         else
96                 reg = src->vv.regoff;
97
98         return reg;
99 }
100
101
102 /* emit_store ******************************************************************
103
104    This function generates the code to store the result of an
105    operation back into a spilled pseudo-variable.  If the
106    pseudo-variable has not been spilled in the first place, this
107    function will generate nothing.
108     
109 *******************************************************************************/
110
111 __PORTED__ inline void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
112 {
113         codegendata *cd;
114
115         /* get required compiler data */
116
117         cd = jd->cd;
118
119         if (IS_INMEMORY(dst->flags)) {
120                 COUNT_SPILLS;
121
122                 if (IS_FLT_DBL_TYPE(dst->type)) {
123                         if (IS_2_WORD_TYPE(dst->type))
124                                 M_DST(d, REG_SP, dst->vv.regoff);
125                         else
126                                 M_FST(d, REG_SP, dst->vv.regoff);
127                 }
128                 else {
129                         if (IS_2_WORD_TYPE(dst->type))
130                                 M_LST(d, REG_SP, dst->vv.regoff);
131                         else
132                                 M_IST(d, REG_SP, dst->vv.regoff);
133                 }
134         }
135 }
136
137
138 /* emit_copy *******************************************************************
139
140    Generates a register/memory to register/memory copy.
141
142 *******************************************************************************/
143
144 __PORTED__ void emit_copy(jitdata *jd, instruction *iptr)
145 {
146         codegendata *cd;
147         varinfo     *src;
148         varinfo     *dst;
149         s4           s1, d;
150
151         /* get required compiler data */
152
153         cd = jd->cd;
154
155         /* get source and destination variables */
156
157         src = VAROP(iptr->s1);
158         dst = VAROP(iptr->dst);
159
160         if ((src->vv.regoff != dst->vv.regoff) ||
161                 ((src->flags ^ dst->flags) & INMEMORY)) {
162
163                 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
164                         /* emit nothing, as the value won't be used anyway */
165                         return;
166                 }
167
168                 /* If one of the variables resides in memory, we can eliminate
169                    the register move from/to the temporary register with the
170                    order of getting the destination register and the load. */
171
172                 if (IS_INMEMORY(src->flags)) {
173                         if (IS_FLT_DBL_TYPE(dst->type)) {
174                                 d = codegen_reg_of_var(iptr->opc, dst, REG_FTMP1);
175                         } else {
176                                 if (IS_2_WORD_TYPE(dst->type)) {
177                                         d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
178                                 } else {
179                                         d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP1);
180                                 }
181                         }
182                         s1 = emit_load(jd, iptr, src, d);
183                 }
184                 else {
185                         if (IS_FLT_DBL_TYPE(src->type)) {
186                                 s1 = emit_load(jd, iptr, src, REG_FTMP1);
187                         } else {
188                                 if (IS_2_WORD_TYPE(src->type)) {
189                                         s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
190                                 } else {
191                                         s1 = emit_load(jd, iptr, src, REG_ITMP1);
192                                 }
193                         }
194                         d = codegen_reg_of_var(iptr->opc, dst, s1);
195                 }
196
197                 if (s1 != d) {
198                         if (IS_FLT_DBL_TYPE(src->type)) {
199                                 M_FMOV(s1, d);
200                         } else {
201                                 if (IS_2_WORD_TYPE(src->type)) {
202                                         M_LNGMOVE(s1, d);
203                                 } else {
204                                         M_MOV(s1, d);
205                                 }
206                         }
207                 }
208
209                 emit_store(jd, iptr, dst, d);
210         }
211 }
212
213
214 /* emit_patcher_stubs **********************************************************
215
216    Generates the code for the patcher stubs.
217
218 *******************************************************************************/
219
220 __PORTED__ void emit_patcher_stubs(jitdata *jd)
221 {
222         
223         codegendata *cd;
224         patchref    *pref;
225         u4           mcode;
226         u1          *savedmcodeptr;
227         u1          *tmpmcodeptr;
228         s4           targetdisp;
229         s4           disp;
230         u1          *ref;
231
232         /* get required compiler data */
233
234         cd = jd->cd;
235
236         /* generate code patching stub call code */
237
238         targetdisp = 0;
239
240         for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
241                 /* check code segment size */
242
243                 MCODECHECK(100);
244
245                 /* Get machine code which is patched back in later. The
246                    call is 1 instruction word long. */
247
248                 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
249
250                 mcode = *((u4 *) tmpmcodeptr);
251
252                 /* Patch in the call to call the following code (done at
253                    compile time). */
254
255                 savedmcodeptr = cd->mcodeptr;   /* save current mcodeptr              */
256                 cd->mcodeptr  = tmpmcodeptr;    /* set mcodeptr to patch position     */
257
258                 disp = (savedmcodeptr) - (tmpmcodeptr);
259
260                 if (! N_VALID_BRANCH(disp)) {
261                         /* Displacement overflow */
262
263                         /* If LONGBRANCHES is not set, the flag and the error flag */
264                         
265                         if (! CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
266                                 cd->flags |= (CODEGENDATA_FLAG_ERROR |
267                                         CODEGENDATA_FLAG_LONGBRANCHES);
268                         }
269
270                         /* If error flag is set, do nothing. The method has to be recompiled. */
271
272                         if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd) && CODEGENDATA_HAS_FLAG_ERROR(cd)) {
273                                 return;
274                         }
275                 }
276
277                 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {    
278
279                         /* Generating long branches */
280
281                         disp = dseg_add_s4(cd, savedmcodeptr - cd->mcodebase - N_PV_OFFSET);
282         
283                         M_ILD_DSEG(REG_ITMP3, disp);
284                         M_AADD(REG_PV, REG_ITMP3);
285
286                         /* Do the branch at the end of NOP sequence.
287                          * This way the patch position is at a *fixed* offset 
288                          * (PATCHER_LONGBRANCHES_NOPS_SKIP) of the return address.
289                          */
290
291                         cd->mcodeptr = tmpmcodeptr + PATCHER_LONGBRANCHES_NOPS_SKIP - SZ_BASR;
292                         M_JMP(REG_ITMP3, REG_ITMP3);
293                 } else {
294
295                         /* Generating short branches */
296
297                         M_BSR(REG_ITMP3, disp);
298                 }
299
300                 cd->mcodeptr = savedmcodeptr;   /* restore the current mcodeptr       */
301
302                 /* create stack frame */
303
304                 M_ASUB_IMM(6 * 4, REG_SP);
305
306                 /* move return address onto stack */
307
308                 M_AST(REG_ITMP3, REG_SP, 5 * 4);
309
310                 /* move pointer to java_objectheader onto stack */
311
312 #if defined(ENABLE_THREADS)
313                 /* create a virtual java_objectheader */
314
315                 (void) dseg_add_unique_address(cd, NULL);                  /* flcword */
316                 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
317                 disp = dseg_add_unique_address(cd, NULL);                  /* vftbl   */
318
319                 M_LDA_DSEG(REG_ITMP3, disp);
320                 M_AST(REG_ITMP3, REG_SP, 4 * 4);
321 #else
322                 /* nothing to do */
323 #endif
324
325                 /* move machine code onto stack */
326
327                 disp = dseg_add_s4(cd, mcode);
328                 M_ILD_DSEG(REG_ITMP3, disp);
329                 M_IST(REG_ITMP3, REG_SP, 3 * 4);
330
331                 /* move class/method/field reference onto stack */
332
333                 disp = dseg_add_address(cd, pref->ref);
334                 M_ALD_DSEG(REG_ITMP3, disp);
335                 M_AST(REG_ITMP3, REG_SP, 2 * 4);
336
337                 /* move data segment displacement onto stack */
338
339                 disp = dseg_add_s4(cd, pref->disp);
340                 M_ILD_DSEG(REG_ITMP3, disp);
341                 M_IST(REG_ITMP3, REG_SP, 1 * 4);
342
343                 /* move patcher function pointer onto stack */
344
345                 disp = dseg_add_functionptr(cd, pref->patcher);
346                 M_ALD_DSEG(REG_ITMP3, disp);
347                 M_AST(REG_ITMP3, REG_SP, 0 * 4);
348
349                 if (targetdisp == 0) {
350                         targetdisp = (cd->mcodeptr) - (cd->mcodebase);
351
352                         disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
353                         M_ALD_DSEG(REG_ITMP3, disp);
354                         M_JMP(RN, REG_ITMP3);
355                 }
356                 else {
357                         disp = ((cd->mcodebase) + targetdisp) -
358                                 (( cd->mcodeptr) );
359
360                         emit_branch(cd, disp, BRANCH_UNCONDITIONAL, RN, 0);
361                 }
362         }
363 }
364
365
366 /* emit_replacement_stubs ******************************************************
367
368    Generates the code for the replacement stubs.
369
370 *******************************************************************************/
371
372 void emit_replacement_stubs(jitdata *jd)
373 {
374 #if 0
375         codegendata *cd;
376         codeinfo    *code;
377         rplpoint    *rplp;
378         s4           disp;
379         s4           i;
380
381         /* get required compiler data */
382
383         cd   = jd->cd;
384         code = jd->code;
385
386         rplp = code->rplpoints;
387
388         for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
389                 /* check code segment size */
390
391                 MCODECHECK(512);
392
393                 /* note start of stub code */
394
395                 rplp->outcode = (u1 *) (ptrint) (cd->mcodeptr - cd->mcodebase);
396
397                 /* make machine code for patching */
398
399                 disp = (ptrint) (rplp->outcode - rplp->pc) - 5;
400
401                 rplp->mcode = 0xe9 | ((u8) disp << 8);
402
403                 /* push address of `rplpoint` struct */
404                         
405                 M_MOV_IMM(rplp, REG_ITMP3);
406                 M_PUSH(REG_ITMP3);
407
408                 /* jump to replacement function */
409
410                 M_MOV_IMM(asm_replacement_out, REG_ITMP3);
411                 M_JMP(REG_ITMP3);
412         }
413 #endif
414 }
415         
416
417 /* emit_verbosecall_enter ******************************************************
418
419    Generates the code for the call trace.
420
421 *******************************************************************************/
422
423 #if !defined(NDEBUG)
424 void emit_verbosecall_enter(jitdata *jd)
425 {
426         
427         methodinfo   *m;
428         codegendata  *cd;
429         methoddesc   *md;
430         s4            i, j, k;
431         s4            stackframesize, off, foff, aoff, doff, t, iargctr, fargctr, disp;
432
433         /* get required compiler data */
434
435         m  = jd->m;
436         cd = jd->cd;
437
438         md = m->parseddesc;
439
440         /* mark trace code */
441
442         M_NOP;
443
444         stackframesize = 
445                 (6 * 8) + /* s8 on stack parameters x 6 */
446                 (1 * 4) + /* methodinfo on stack parameter */
447                 (ARG_CNT * 8) +
448                 (TMP_CNT * 8) 
449                 ;
450
451         M_ASUB_IMM(stackframesize, REG_SP); /* allocate stackframe */
452
453         /* save argument registers */
454
455         off = (6 * 8) + (1 * 4);
456
457         for (i = 0; i < INT_ARG_CNT; i++, off += 8)
458                 M_IST(abi_registers_integer_argument[i], REG_SP, off);
459
460         for (i = 0; i < FLT_ARG_CNT; i++, off += 8)
461                 M_DST(abi_registers_float_argument[i], REG_SP, off);
462
463         /* save temporary registers for leaf methods */
464
465         if (jd->isleafmethod) {
466                 for (i = 0; i < INT_TMP_CNT; i++, off += 8)
467                         M_LST(abi_registers_integer_temporary[i], REG_SP, off);
468
469                 for (i = 0; i < FLT_TMP_CNT; i++, off += 8)
470                         M_DST(abi_registers_float_temporary[i], REG_SP, off);
471         }
472
473         /* Load arguments to new locations */
474
475         /* First move all arguments to stack
476          *
477          * (s8) a7
478          * (s8) a2
479          *   ...
480          * (s8) a1 \ Auxilliary stack frame
481          * (s8) a0 /
482          * ------- <---- SP
483          */
484
485         M_ASUB_IMM(2 * 8, REG_SP);
486         
487         /* offset to where first integer arg is saved on stack */
488         off = (2 * 8) + (6 * 8) + (1 * 4); 
489         /* offset to where first float arg is saved on stack */
490         foff = off + (INT_ARG_CNT * 8); 
491         /* offset to where first argument is passed on stack */
492         aoff = (2 * 8) + stackframesize + (cd->stackframesize * 4);
493         /* offset to destination on stack */
494         doff = 0; 
495
496         iargctr = fargctr = 0;
497
498         ICONST(REG_ITMP1, 0);
499
500         for (i = 0; i < md->paramcount && i < 8; i++) {
501                 t = md->paramtypes[i].type;
502
503                 M_IST(REG_ITMP1, REG_SP, doff);
504                 M_IST(REG_ITMP1, REG_SP, doff + 4);
505
506                 if (IS_FLT_DBL_TYPE(t)) {
507                         if (fargctr < 2) { /* passed in register */
508                                 N_STD(abi_registers_float_argument[fargctr], doff, RN, REG_SP);
509                                 fargctr += 1;
510                         } else { /* passed on stack */
511                                 if (IS_2_WORD_TYPE(t)) {
512                                         N_MVC(doff, 8, REG_SP, aoff, REG_SP);
513                                         aoff += 8;
514                                 } else {
515                                         N_MVC(doff + 4, 4, REG_SP, aoff, REG_SP);
516                                         aoff += 4;
517                                 }
518                         }
519                 } else {
520                         if (IS_2_WORD_TYPE(t)) {
521                                 if (iargctr < 4) { /* passed in 2 registers */
522                                         N_STM(REG_A0 + iargctr, REG_A0 + iargctr + 1, doff, REG_SP);
523                                         iargctr += 2;
524                                 } else { /* passed on stack */
525                                         N_MVC(doff, 8, REG_SP, aoff, REG_SP);
526                                         aoff += 8;
527                                 }
528                         } else {
529                                 if (iargctr < 5) { /* passed in register */
530                                         N_ST(REG_A0 + iargctr, doff + 4, RN, REG_SP);
531                                         iargctr += 1;
532                                 } else { /* passed on stack */
533                                         N_MVC(doff + 4, 4, REG_SP, aoff, REG_SP);
534                                         aoff += 4;
535                                 }
536                         }
537                 }
538
539                 doff += 8;
540         }
541
542         /* Now move a0 and a1 to registers
543          *
544          * (s8) a7
545          *   ...
546          * (s8) a2
547          * ------- <- SP
548          * (s8) a0 ==> a0, a1
549          * (s8) a1 ==> a2, a3
550          */
551
552         N_LM(REG_A0, REG_A1, 0, REG_SP);
553         N_LM(REG_A2, REG_A3, 8, REG_SP);
554
555         M_AADD_IMM(2 * 8, REG_SP);
556
557         /* Finally load methodinfo argument */
558
559         disp = dseg_add_address(cd, m);
560         M_ALD_DSEG(REG_ITMP2, disp);    
561         M_AST(REG_ITMP2, REG_SP, 6 * 8);
562
563         /* Call builtin_verbosecall_enter */
564
565         disp = dseg_add_address(cd, builtin_verbosecall_enter);
566         M_ALD_DSEG(REG_ITMP2, disp);
567         M_ASUB_IMM(96, REG_SP);
568         M_CALL(REG_ITMP2);
569         M_AADD_IMM(96, REG_SP);
570
571         /* restore argument registers */
572
573         off = (6 * 8) + (1 * 4);
574
575         for (i = 0; i < INT_ARG_CNT; i++, off += 8)
576                 M_ILD(abi_registers_integer_argument[i], REG_SP, off);
577
578         for (i = 0; i < FLT_ARG_CNT; i++, off += 8)
579                 M_DLD(abi_registers_float_argument[i], REG_SP, off);
580
581         /* restore temporary registers for leaf methods */
582
583         if (jd->isleafmethod) {
584                 for (i = 0; i < INT_TMP_CNT; i++, off += 8)
585                         M_ILD(abi_registers_integer_temporary[i], REG_SP, off);
586
587                 for (i = 0; i < FLT_TMP_CNT; i++, off += 8)
588                         M_DLD(abi_registers_float_temporary[i], REG_SP, off);
589         }
590
591         /* remove stackframe */
592
593         M_AADD_IMM(stackframesize, REG_SP);
594
595         /* mark trace code */
596
597         M_NOP;
598 }
599 #endif /* !defined(NDEBUG) */
600
601
602 /* emit_verbosecall_exit *******************************************************
603
604    Generates the code for the call trace.
605
606 *******************************************************************************/
607
608 #if !defined(NDEBUG)
609 void emit_verbosecall_exit(jitdata *jd)
610 {
611         methodinfo   *m;
612         codegendata  *cd;
613         registerdata *rd;
614         s4            disp;
615
616         /* get required compiler data */
617
618         m  = jd->m;
619         cd = jd->cd;
620         rd = jd->rd;
621
622         /* mark trace code */
623
624         M_NOP;
625
626         M_ASUB_IMM(2 * 8, REG_SP);
627
628         N_STM(REG_RESULT, REG_RESULT2, 0 * 8, REG_SP);
629         M_DST(REG_FRESULT, REG_SP, 1 * 8);
630
631         if (IS_2_WORD_TYPE(m->parseddesc->returntype.type)) {
632                 /* (REG_A0, REG_A1) == (REG_RESULT, REG_RESULT2), se no need to move */
633         } else {
634                 M_INTMOVE(REG_RESULT, REG_A1);
635                 ICONST(REG_A0, 0);
636         }
637
638         disp = dseg_add_address(cd, m);
639         M_ALD_DSEG(REG_A2, disp);
640
641         /* REG_FRESULT is REG_FA0, so no need to move */
642         M_FLTMOVE(REG_FRESULT, REG_FA1);
643
644         disp = dseg_add_address(cd, builtin_verbosecall_exit);
645         M_ALD_DSEG(REG_ITMP1, disp);
646         M_ASUB_IMM(96, REG_SP);
647         M_CALL(REG_ITMP1);
648         M_AADD_IMM(96, REG_SP);
649
650         N_LM(REG_RESULT, REG_RESULT2, 0 * 8, REG_SP);
651         M_DLD(REG_FRESULT, REG_SP, 1 * 8);
652
653         M_AADD_IMM(2 * 8, REG_SP);
654
655         /* mark trace code */
656
657         M_NOP;
658 }
659 #endif /* !defined(NDEBUG) */
660
661
662 /* emit_load_high **************************************************************
663
664    Emits a possible load of the high 32-bits of an operand.
665
666 *******************************************************************************/
667
668 __PORTED__ s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
669 {
670         codegendata  *cd;
671         s4            disp;
672         s4            reg;
673
674         assert(src->type == TYPE_LNG);
675
676         /* get required compiler data */
677
678         cd = jd->cd;
679
680         if (IS_INMEMORY(src->flags)) {
681                 COUNT_SPILLS;
682
683                 disp = src->vv.regoff;
684
685                 M_ILD(tempreg, REG_SP, disp);
686
687                 reg = tempreg;
688         }
689         else
690                 reg = GET_HIGH_REG(src->vv.regoff);
691
692         return reg;
693 }
694
695 /* emit_load_low ***************************************************************
696
697    Emits a possible load of the low 32-bits of an operand.
698
699 *******************************************************************************/
700
701 __PORTED__ s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
702 {
703         codegendata  *cd;
704         s4            disp;
705         s4            reg;
706
707         assert(src->type == TYPE_LNG);
708
709         /* get required compiler data */
710
711         cd = jd->cd;
712
713         if (IS_INMEMORY(src->flags)) {
714                 COUNT_SPILLS;
715
716                 disp = src->vv.regoff;
717
718                 M_ILD(tempreg, REG_SP, disp + 4);
719
720                 reg = tempreg;
721         }
722         else
723                 reg = GET_LOW_REG(src->vv.regoff);
724
725         return reg;
726 }
727
728 s4 emit_load_s1_notzero(jitdata *jd, instruction *iptr, s4 tempreg) {
729         codegendata *cd = jd->cd;
730         s4 reg = emit_load_s1(jd, iptr, tempreg);
731         if (reg == 0) {
732                 M_MOV(reg, tempreg);
733                 return tempreg;
734         } else {
735                 return reg;
736         }
737 }
738
739 s4 emit_load_s2_notzero(jitdata *jd, instruction *iptr, s4 tempreg) {
740         codegendata *cd = jd->cd;
741         s4 reg = emit_load_s2(jd, iptr, tempreg);
742         if (reg == 0) {
743                 if (IS_FLT_DBL_TYPE(VAROP(iptr->sx.s23.s2)->type)) {
744                         M_FMOV(reg, tempreg);
745                 } else {
746                         M_MOV(reg, tempreg);
747                 }
748                 return tempreg;
749         } else {
750                 return reg;
751         }
752 }
753
754 s4 emit_load_s1_but(jitdata *jd, instruction *iptr, s4 tempreg, s4 notreg) {
755         codegendata *cd = jd->cd;
756         s4 reg = emit_load_s1(jd, iptr, tempreg);
757         if (reg == notreg) {
758                 if (IS_FLT_DBL_TYPE(VAROP(iptr->s1)->type)) {
759                         M_FMOV(reg, tempreg);
760                 } else {
761                         M_MOV(reg, tempreg);
762                 }
763                 return tempreg;
764         } else {
765                 return reg;
766         }
767 }
768
769 s4 emit_load_s2_but(jitdata *jd, instruction *iptr, s4 tempreg, s4 notreg) {
770         codegendata *cd = jd->cd;
771         s4 reg = emit_load_s2(jd, iptr, tempreg);
772         if (reg == notreg) {
773                 if (IS_FLT_DBL_TYPE(VAROP(iptr->sx.s23.s2)->type)) {
774                         M_FMOV(reg, tempreg);
775                 } else {
776                         M_MOV(reg, tempreg);
777                 }
778                 return tempreg;
779         } else {
780                 return reg;
781         }
782 }
783
784 s4 emit_alloc_dst_even_odd(jitdata *jd, instruction *iptr, s4 htmpreg, s4 ltmpreg, s4 breg) {
785         codegendata *cd;
786         s4           hr, lr;
787         varinfo     *dst;
788
789         /* (r0, r1)    
790          * (r2, r3)
791          * (r4, r5)
792          * (r6, r7)
793          * (r8, r9)
794          * (r10, r11)
795          * (r12, r13) Illegal, because r13 is PV
796          * (r14, r15) Illegal, because r15 is SP
797          */
798
799         cd = jd->cd;
800         dst = VAROP(iptr->dst);
801
802         if (IS_INMEMORY(dst->flags)) {
803                 if (! IS_REG_ITMP(ltmpreg)) {
804                         M_INTMOVE(ltmpreg, breg);
805                 }
806                 if (! IS_REG_ITMP(htmpreg)) {
807                         M_INTMOVE(htmpreg, breg);
808                 }
809                 return PACK_REGS(ltmpreg, htmpreg);
810         } else {
811                 hr = GET_HIGH_REG(dst->vv.regoff);
812                 lr = GET_LOW_REG(dst->vv.regoff);
813                 if (((hr % 2) == 0) && lr == (hr + 1)) {
814                         /* the result is already in a even-odd pair */
815                         return dst->vv.regoff;                  
816                 } else if (((hr % 2) == 0) && (hr < R12)) {
817                         /* the high register is at a even position */
818                         M_INTMOVE(hr + 1, breg);
819                         return PACK_REGS(hr + 1, hr);
820                 } else if (((lr % 2) == 1) && (lr < R12)) {
821                         /* the low register is at a odd position */
822                         M_INTMOVE(lr - 1, breg);
823                         return PACK_REGS(lr, lr - 1);
824                 } else {
825                         /* no way to create an even-odd pair by 1 copy operation,
826                          * Use the temporary register pair.
827                          */
828                         if (! IS_REG_ITMP(ltmpreg)) {
829                                 M_INTMOVE(ltmpreg, breg);
830                         }
831                         if (! IS_REG_ITMP(htmpreg)) {
832                                 M_INTMOVE(htmpreg, breg);
833                         }
834                         return PACK_REGS(ltmpreg, htmpreg);
835                 }
836         }
837 }
838
839 void emit_restore_dst_even_odd(jitdata *jd, instruction *iptr, s4 htmpreg, s4 ltmpreg, s4 breg) {
840         codegendata *cd;
841         s4           hr, lr;
842         varinfo     *dst;
843
844         cd = jd->cd;
845         dst = VAROP(iptr->dst);
846
847         if (IS_INMEMORY(dst->flags)) {
848                 if (! IS_REG_ITMP(ltmpreg)) {
849                         M_INTMOVE(breg, ltmpreg);
850                 }
851                 if (! IS_REG_ITMP(htmpreg)) {
852                         M_INTMOVE(breg, htmpreg);
853                 }
854         } else {
855                 hr = GET_HIGH_REG(dst->vv.regoff);
856                 lr = GET_LOW_REG(dst->vv.regoff);
857                 if (((hr % 2) == 0) && lr == (hr + 1)) {
858                         return;
859                 } else if (((hr % 2) == 0) && (hr < R12)) {
860                         M_INTMOVE(breg, hr + 1);
861                 } else if (((lr % 2) == 1) && (lr < R12)) {
862                         M_INTMOVE(breg, lr - 1);
863                 } else {
864                         if (! IS_REG_ITMP(ltmpreg)) {
865                                 M_INTMOVE(breg, ltmpreg);
866                         }
867                         if (! IS_REG_ITMP(htmpreg)) {
868                                 M_INTMOVE(breg, htmpreg);
869                         }
870                 }
871         }
872 }
873
874 void emit_copy_dst(jitdata *jd, instruction *iptr, s4 dtmpreg) {
875         codegendata *cd;
876         varinfo *dst;
877         cd = jd->cd;
878         dst = VAROP(iptr->dst);
879         if (! IS_INMEMORY(dst->flags)) {
880                 if (dst->vv.regoff != dtmpreg) {
881                         if (IS_FLT_DBL_TYPE(dst->type)) {
882                                 M_FLTMOVE(dtmpreg, dst->vv.regoff);
883                         } else if (IS_2_WORD_TYPE(dst->type)) {
884                                 M_LNGMOVE(dtmpreg, dst->vv.regoff);
885                         } else {
886                                 M_INTMOVE(dtmpreg, dst->vv.regoff);
887                         }
888                 }
889         }
890 }
891
892 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt) {
893
894         s4 branchdisp = disp;
895         s4 branchmpc;
896         u1 *ref;
897
898         if (N_VALID_BRANCH(branchdisp)) {
899
900                 /* valid displacement */
901
902                 switch (condition) {
903                         case BRANCH_EQ:
904                                 M_BEQ(branchdisp);
905                                 break;
906                         case BRANCH_NE:
907                                 M_BNE(branchdisp);
908                                 break;
909                         case BRANCH_LT:
910                                 M_BLT(branchdisp);
911                                 break;
912                         case BRANCH_GE:
913                                 M_BGE(branchdisp);
914                                 break;
915                         case BRANCH_GT:
916                                 M_BGT(branchdisp);
917                                 break;
918                         case BRANCH_LE:
919                                 M_BLE(branchdisp);
920                                 break;
921                         case BRANCH_UNCONDITIONAL:
922                                 M_BR(branchdisp);
923                                 break;
924                         default:
925                                 vm_abort("emit_branch: unknown condition %d", condition);
926                 }
927         } else {
928
929                 /* If LONGBRANCHES is not set, the flag and the error flag */
930
931                 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
932                         cd->flags |= (CODEGENDATA_FLAG_ERROR |
933                                 CODEGENDATA_FLAG_LONGBRANCHES);
934                 }
935
936                 /* If error flag is set, do nothing. The method has to be recompiled. */
937
938                 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd) && CODEGENDATA_HAS_FLAG_ERROR(cd)) {
939                         return;
940                 }
941
942                 /* Patch the displacement to branch over the actual branch manually
943                  * to not get yet more nops.
944                  */
945
946                 branchmpc = cd->mcodeptr - cd->mcodebase;
947                 ref = cd->mcodeptr;
948
949                 switch (condition) {
950                         case BRANCH_EQ:
951                                 M_BNE(0);
952                                 break;
953                         case BRANCH_NE:
954                                 M_BEQ(0);
955                                 break;
956                         case BRANCH_LT:
957                                 M_BGE(0);
958                                 break;
959                         case BRANCH_GE:
960                                 M_BLT(0);
961                                 break;
962                         case BRANCH_GT:
963                                 M_BLE(0);
964                                 break;
965                         case BRANCH_LE:
966                                 M_BGT(0);
967                                 break;
968                         case BRANCH_UNCONDITIONAL:
969                                 /* fall through, no displacement to patch */
970                                 ref = NULL;
971                                 break;
972                         default:
973                                 vm_abort("emit_branch: unknown condition %d", condition);
974                 }
975
976                 /* The actual long branch */
977
978                 disp = dseg_add_s4(cd, branchmpc + disp - N_PV_OFFSET);
979                 M_ILD_DSEG(REG_ITMP3, disp);
980                 M_AADD(REG_PV, REG_ITMP3);
981                 M_JMP(RN, REG_ITMP3);
982
983                 /* Patch back the displacement */
984
985                 if (ref != NULL) {
986                         *(u4 *)ref |= (u4)((cd->mcodeptr - ref) / 2);
987                 }
988         }
989 }
990
991 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg) {
992         if (INSTRUCTION_MUST_CHECK(iptr)) {
993                 M_TEST(reg);
994                 M_BNE(SZ_BRC + SZ_ILL);
995                 M_ILL(EXCEPTION_HARDWARE_ARITHMETIC);
996         }
997 }
998
999 /* emit_arrayindexoutofbounds_check ********************************************
1000
1001    Emit a ArrayIndexOutOfBoundsException check.
1002
1003 *******************************************************************************/
1004
1005 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
1006 {
1007         if (INSTRUCTION_MUST_CHECK(iptr)) {
1008                 /* Size is s4, >= 0
1009                  * Do unsigned comparison to catch negative indexes.
1010                  */
1011                 N_CL(s2, OFFSET(java_arrayheader, size), RN, s1);
1012         M_BLT(SZ_BRC + SZ_ILL);
1013                 M_ILL2(s2, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
1014         }
1015 }
1016
1017 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1) {
1018         if (INSTRUCTION_MUST_CHECK(iptr)) {
1019                 if (reg != RN) {
1020                         M_TEST(reg);
1021                 }
1022                 switch (condition) {
1023                         case BRANCH_LE:
1024                                 M_BGT(SZ_BRC + SZ_ILL);
1025                                 break;
1026                         case BRANCH_EQ:
1027                                 M_BNE(SZ_BRC + SZ_ILL);
1028                                 break;
1029                         case BRANCH_GT:
1030                                 M_BLE(SZ_BRC + SZ_ILL);
1031                                 break;
1032                         default:
1033                                 vm_abort("emit_classcast_check: unknown condition %d", condition);
1034                 }
1035                 M_ILL2(s1, EXCEPTION_HARDWARE_CLASSCAST);
1036         }
1037 }
1038
1039 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg) {
1040         if (INSTRUCTION_MUST_CHECK(iptr)) {
1041                 M_TEST(reg);
1042                 M_BNE(SZ_BRC + SZ_ILL);
1043                 M_ILL(EXCEPTION_HARDWARE_NULLPOINTER);
1044         }
1045 }
1046
1047 void emit_exception_check(codegendata *cd, instruction *iptr) {
1048         if (INSTRUCTION_MUST_CHECK(iptr)) {
1049                 M_TEST(REG_RESULT);
1050                 M_BNE(SZ_BRC + SZ_ILL);
1051                 M_ILL(EXCEPTION_HARDWARE_EXCEPTION);
1052         }
1053 }
1054
1055 void emit_restore_pv(codegendata *cd) {
1056         s4 offset, offset_imm;
1057
1058         /*
1059         N_BASR(REG_PV, RN);
1060         disp = (s4) (cd->mcodeptr - cd->mcodebase);
1061         M_ASUB_IMM32(disp, REG_ITMP1, REG_PV);
1062         */
1063
1064         /* If the offset from the method start does not fit into an immediate
1065          * value, we can't put it into the data segment!
1066          */
1067
1068         /* Displacement from start of method to here */
1069
1070         offset = (s4) (cd->mcodeptr - cd->mcodebase);
1071         offset_imm = -offset - SZ_BASR + N_PV_OFFSET;
1072
1073         if (N_VALID_IMM(offset_imm)) {
1074                 /* Get program counter */
1075                 N_BASR(REG_PV, RN);
1076                 /* Substract displacement */
1077                 M_AADD_IMM(offset_imm, REG_PV);
1078         } else {
1079                 /* Save program counter and jump over displacement in instruction flow */
1080                 N_BRAS(REG_PV, SZ_BRAS + SZ_LONG);
1081                 /* Place displacement here */
1082                 /* REG_PV points now exactly to this position */
1083                 N_LONG(-offset - SZ_BRAS + N_PV_OFFSET);
1084                 /* Substract *(REG_PV) from REG_PV */
1085                 N_A(REG_PV, 0, RN, REG_PV);
1086         }
1087 }
1088
1089 /*
1090  * These are local overrides for various environment variables in Emacs.
1091  * Please do not remove this and leave it at the end of the file, where
1092  * Emacs will automagically detect them.
1093  * ---------------------------------------------------------------------
1094  * Local variables:
1095  * mode: c
1096  * indent-tabs-mode: t
1097  * c-basic-offset: 4
1098  * tab-width: 4
1099  * End:
1100  */