Merged revisions 8187-8244 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 8240 2007-07-29 20:36:47Z pm $
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 #if defined(ENABLE_REPLACEMENT)
372 void emit_replacement_stubs(jitdata *jd)
373 {
374         codegendata *cd;
375         codeinfo    *code;
376         rplpoint    *rplp;
377         s4           disp;
378         s4           i, remain;
379         u1          *savedmcodeptr;
380
381         /* get required compiler data */
382
383         cd   = jd->cd;
384         code = jd->code;
385
386         rplp = code->rplpoints;
387
388         /* store beginning of replacement stubs */
389
390         code->replacementstubs = (u1*) (cd->mcodeptr - cd->mcodebase);
391
392         for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
393                 /* do not generate stubs for non-trappable points */
394
395                 if (rplp->flags & RPLPOINT_FLAG_NOTRAP)
396                         continue;
397
398                 /* check code segment size */
399
400                 MCODECHECK(512);
401
402 #if !defined(NDEBUG)
403                 savedmcodeptr = cd->mcodeptr;
404 #endif
405
406                 /* create stack frame - 8-byte aligned */
407
408                 M_ASUB_IMM(REG_SP, 2 * 4);
409
410                 /* push address of `rplpoint` struct, will be used in asm_replacement_out */
411
412                 disp = dseg_add_address(cd, rplp);
413                 M_ALD_DSEG(REG_ITMP3, disp);
414                 M_AST(REG_ITMP3, REG_SP, 0 * 4);
415
416                 /* jump to replacement function */
417
418                 disp = dseg_add_functionptr(cd, asm_replacement_out);
419                 M_ALD_DSEG(REG_ITMP3, disp);
420                 M_JMP(RN, REG_ITMP3);
421
422                 assert((cd->mcodeptr - savedmcodeptr) <= REPLACEMENT_STUB_SIZE);
423
424                 /* pad with NOPs */
425
426                 for (remain = REPLACEMENT_STUB_SIZE - (cd->mcodeptr - savedmcodeptr); remain > 0;) {
427                         if (remain >= 4) {
428                                 M_NOP;
429                                 remain -= 4;
430                         } else {
431                                 M_NOP2;
432                                 remain -= 2;
433                         }
434                 }
435
436                 assert((cd->mcodeptr - savedmcodeptr) == REPLACEMENT_STUB_SIZE);
437         }
438 }
439 #endif
440
441 /* emit_verbosecall_enter ******************************************************
442
443    Generates the code for the call trace.
444
445 *******************************************************************************/
446
447 #if !defined(NDEBUG)
448 void emit_verbosecall_enter(jitdata *jd)
449 {
450         
451         methodinfo   *m;
452         codegendata  *cd;
453         methoddesc   *md;
454         s4            i, j, k;
455         s4            stackframesize, off, foff, aoff, doff, t, iargctr, fargctr, disp;
456
457         /* get required compiler data */
458
459         m  = jd->m;
460         cd = jd->cd;
461
462         md = m->parseddesc;
463
464         /* mark trace code */
465
466         M_NOP;
467
468         stackframesize = 
469                 (6 * 8) + /* s8 on stack parameters x 6 */
470                 (1 * 4) + /* methodinfo on stack parameter */
471                 (ARG_CNT * 8) +
472                 (TMP_CNT * 8) 
473                 ;
474
475         M_ASUB_IMM(stackframesize, REG_SP); /* allocate stackframe */
476
477         /* save argument registers */
478
479         off = (6 * 8) + (1 * 4);
480
481         for (i = 0; i < INT_ARG_CNT; i++, off += 8)
482                 M_IST(abi_registers_integer_argument[i], REG_SP, off);
483
484         for (i = 0; i < FLT_ARG_CNT; i++, off += 8)
485                 M_DST(abi_registers_float_argument[i], REG_SP, off);
486
487         /* save temporary registers for leaf methods */
488
489         if (jd->isleafmethod) {
490                 for (i = 0; i < INT_TMP_CNT; i++, off += 8)
491                         M_LST(abi_registers_integer_temporary[i], REG_SP, off);
492
493                 for (i = 0; i < FLT_TMP_CNT; i++, off += 8)
494                         M_DST(abi_registers_float_temporary[i], REG_SP, off);
495         }
496
497         /* Load arguments to new locations */
498
499         /* First move all arguments to stack
500          *
501          * (s8) a7
502          * (s8) a2
503          *   ...
504          * (s8) a1 \ Auxilliary stack frame
505          * (s8) a0 /
506          * ------- <---- SP
507          */
508
509         M_ASUB_IMM(2 * 8, REG_SP);
510         
511         /* offset to where first integer arg is saved on stack */
512         off = (2 * 8) + (6 * 8) + (1 * 4); 
513         /* offset to where first float arg is saved on stack */
514         foff = off + (INT_ARG_CNT * 8); 
515         /* offset to where first argument is passed on stack */
516         aoff = (2 * 8) + stackframesize + (cd->stackframesize * 4);
517         /* offset to destination on stack */
518         doff = 0; 
519
520         iargctr = fargctr = 0;
521
522         ICONST(REG_ITMP1, 0);
523
524         for (i = 0; i < md->paramcount && i < 8; i++) {
525                 t = md->paramtypes[i].type;
526
527                 M_IST(REG_ITMP1, REG_SP, doff);
528                 M_IST(REG_ITMP1, REG_SP, doff + 4);
529
530                 if (IS_FLT_DBL_TYPE(t)) {
531                         if (fargctr < 2) { /* passed in register */
532                                 N_STD(abi_registers_float_argument[fargctr], doff, RN, REG_SP);
533                                 fargctr += 1;
534                         } else { /* passed on stack */
535                                 if (IS_2_WORD_TYPE(t)) {
536                                         N_MVC(doff, 8, REG_SP, aoff, REG_SP);
537                                         aoff += 8;
538                                 } else {
539                                         N_MVC(doff + 4, 4, REG_SP, aoff, REG_SP);
540                                         aoff += 4;
541                                 }
542                         }
543                 } else {
544                         if (IS_2_WORD_TYPE(t)) {
545                                 if (iargctr < 4) { /* passed in 2 registers */
546                                         N_STM(REG_A0 + iargctr, REG_A0 + iargctr + 1, doff, REG_SP);
547                                         iargctr += 2;
548                                 } else { /* passed on stack */
549                                         N_MVC(doff, 8, REG_SP, aoff, REG_SP);
550                                         aoff += 8;
551                                 }
552                         } else {
553                                 if (iargctr < 5) { /* passed in register */
554                                         N_ST(REG_A0 + iargctr, doff + 4, RN, REG_SP);
555                                         iargctr += 1;
556                                 } else { /* passed on stack */
557                                         N_MVC(doff + 4, 4, REG_SP, aoff, REG_SP);
558                                         aoff += 4;
559                                 }
560                         }
561                 }
562
563                 doff += 8;
564         }
565
566         /* Now move a0 and a1 to registers
567          *
568          * (s8) a7
569          *   ...
570          * (s8) a2
571          * ------- <- SP
572          * (s8) a0 ==> a0, a1
573          * (s8) a1 ==> a2, a3
574          */
575
576         N_LM(REG_A0, REG_A1, 0, REG_SP);
577         N_LM(REG_A2, REG_A3, 8, REG_SP);
578
579         M_AADD_IMM(2 * 8, REG_SP);
580
581         /* Finally load methodinfo argument */
582
583         disp = dseg_add_address(cd, m);
584         M_ALD_DSEG(REG_ITMP2, disp);    
585         M_AST(REG_ITMP2, REG_SP, 6 * 8);
586
587         /* Call builtin_verbosecall_enter */
588
589         disp = dseg_add_address(cd, builtin_verbosecall_enter);
590         M_ALD_DSEG(REG_ITMP2, disp);
591         M_ASUB_IMM(96, REG_SP);
592         M_CALL(REG_ITMP2);
593         M_AADD_IMM(96, REG_SP);
594
595         /* restore argument registers */
596
597         off = (6 * 8) + (1 * 4);
598
599         for (i = 0; i < INT_ARG_CNT; i++, off += 8)
600                 M_ILD(abi_registers_integer_argument[i], REG_SP, off);
601
602         for (i = 0; i < FLT_ARG_CNT; i++, off += 8)
603                 M_DLD(abi_registers_float_argument[i], REG_SP, off);
604
605         /* restore temporary registers for leaf methods */
606
607         if (jd->isleafmethod) {
608                 for (i = 0; i < INT_TMP_CNT; i++, off += 8)
609                         M_ILD(abi_registers_integer_temporary[i], REG_SP, off);
610
611                 for (i = 0; i < FLT_TMP_CNT; i++, off += 8)
612                         M_DLD(abi_registers_float_temporary[i], REG_SP, off);
613         }
614
615         /* remove stackframe */
616
617         M_AADD_IMM(stackframesize, REG_SP);
618
619         /* mark trace code */
620
621         M_NOP;
622 }
623 #endif /* !defined(NDEBUG) */
624
625
626 /* emit_verbosecall_exit *******************************************************
627
628    Generates the code for the call trace.
629
630 *******************************************************************************/
631
632 #if !defined(NDEBUG)
633 void emit_verbosecall_exit(jitdata *jd)
634 {
635         methodinfo   *m;
636         codegendata  *cd;
637         registerdata *rd;
638         s4            disp;
639
640         /* get required compiler data */
641
642         m  = jd->m;
643         cd = jd->cd;
644         rd = jd->rd;
645
646         /* mark trace code */
647
648         M_NOP;
649
650         M_ASUB_IMM(2 * 8, REG_SP);
651
652         N_STM(REG_RESULT, REG_RESULT2, 0 * 8, REG_SP);
653         M_DST(REG_FRESULT, REG_SP, 1 * 8);
654
655         if (IS_2_WORD_TYPE(m->parseddesc->returntype.type)) {
656                 /* (REG_A0, REG_A1) == (REG_RESULT, REG_RESULT2), se no need to move */
657         } else {
658                 M_INTMOVE(REG_RESULT, REG_A1);
659                 ICONST(REG_A0, 0);
660         }
661
662         disp = dseg_add_address(cd, m);
663         M_ALD_DSEG(REG_A2, disp);
664
665         /* REG_FRESULT is REG_FA0, so no need to move */
666         M_FLTMOVE(REG_FRESULT, REG_FA1);
667
668         disp = dseg_add_address(cd, builtin_verbosecall_exit);
669         M_ALD_DSEG(REG_ITMP1, disp);
670         M_ASUB_IMM(96, REG_SP);
671         M_CALL(REG_ITMP1);
672         M_AADD_IMM(96, REG_SP);
673
674         N_LM(REG_RESULT, REG_RESULT2, 0 * 8, REG_SP);
675         M_DLD(REG_FRESULT, REG_SP, 1 * 8);
676
677         M_AADD_IMM(2 * 8, REG_SP);
678
679         /* mark trace code */
680
681         M_NOP;
682 }
683 #endif /* !defined(NDEBUG) */
684
685
686 /* emit_load_high **************************************************************
687
688    Emits a possible load of the high 32-bits of an operand.
689
690 *******************************************************************************/
691
692 __PORTED__ s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
693 {
694         codegendata  *cd;
695         s4            disp;
696         s4            reg;
697
698         assert(src->type == TYPE_LNG);
699
700         /* get required compiler data */
701
702         cd = jd->cd;
703
704         if (IS_INMEMORY(src->flags)) {
705                 COUNT_SPILLS;
706
707                 disp = src->vv.regoff;
708
709                 M_ILD(tempreg, REG_SP, disp);
710
711                 reg = tempreg;
712         }
713         else
714                 reg = GET_HIGH_REG(src->vv.regoff);
715
716         return reg;
717 }
718
719 /* emit_load_low ***************************************************************
720
721    Emits a possible load of the low 32-bits of an operand.
722
723 *******************************************************************************/
724
725 __PORTED__ s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
726 {
727         codegendata  *cd;
728         s4            disp;
729         s4            reg;
730
731         assert(src->type == TYPE_LNG);
732
733         /* get required compiler data */
734
735         cd = jd->cd;
736
737         if (IS_INMEMORY(src->flags)) {
738                 COUNT_SPILLS;
739
740                 disp = src->vv.regoff;
741
742                 M_ILD(tempreg, REG_SP, disp + 4);
743
744                 reg = tempreg;
745         }
746         else
747                 reg = GET_LOW_REG(src->vv.regoff);
748
749         return reg;
750 }
751
752 s4 emit_load_s1_notzero(jitdata *jd, instruction *iptr, s4 tempreg) {
753         codegendata *cd = jd->cd;
754         s4 reg = emit_load_s1(jd, iptr, tempreg);
755         if (reg == 0) {
756                 M_MOV(reg, tempreg);
757                 return tempreg;
758         } else {
759                 return reg;
760         }
761 }
762
763 s4 emit_load_s2_notzero(jitdata *jd, instruction *iptr, s4 tempreg) {
764         codegendata *cd = jd->cd;
765         s4 reg = emit_load_s2(jd, iptr, tempreg);
766         if (reg == 0) {
767                 if (IS_FLT_DBL_TYPE(VAROP(iptr->sx.s23.s2)->type)) {
768                         M_FMOV(reg, tempreg);
769                 } else {
770                         M_MOV(reg, tempreg);
771                 }
772                 return tempreg;
773         } else {
774                 return reg;
775         }
776 }
777
778 s4 emit_load_s1_but(jitdata *jd, instruction *iptr, s4 tempreg, s4 notreg) {
779         codegendata *cd = jd->cd;
780         s4 reg = emit_load_s1(jd, iptr, tempreg);
781         if (reg == notreg) {
782                 if (IS_FLT_DBL_TYPE(VAROP(iptr->s1)->type)) {
783                         M_FMOV(reg, tempreg);
784                 } else {
785                         M_MOV(reg, tempreg);
786                 }
787                 return tempreg;
788         } else {
789                 return reg;
790         }
791 }
792
793 s4 emit_load_s2_but(jitdata *jd, instruction *iptr, s4 tempreg, s4 notreg) {
794         codegendata *cd = jd->cd;
795         s4 reg = emit_load_s2(jd, iptr, tempreg);
796         if (reg == notreg) {
797                 if (IS_FLT_DBL_TYPE(VAROP(iptr->sx.s23.s2)->type)) {
798                         M_FMOV(reg, tempreg);
799                 } else {
800                         M_MOV(reg, tempreg);
801                 }
802                 return tempreg;
803         } else {
804                 return reg;
805         }
806 }
807
808 s4 emit_alloc_dst_even_odd(jitdata *jd, instruction *iptr, s4 htmpreg, s4 ltmpreg, s4 breg) {
809         codegendata *cd;
810         s4           hr, lr;
811         varinfo     *dst;
812
813         /* (r0, r1)    
814          * (r2, r3)
815          * (r4, r5)
816          * (r6, r7)
817          * (r8, r9)
818          * (r10, r11)
819          * (r12, r13) Illegal, because r13 is PV
820          * (r14, r15) Illegal, because r15 is SP
821          */
822
823         cd = jd->cd;
824         dst = VAROP(iptr->dst);
825
826         if (IS_INMEMORY(dst->flags)) {
827                 if (! IS_REG_ITMP(ltmpreg)) {
828                         M_INTMOVE(ltmpreg, breg);
829                 }
830                 if (! IS_REG_ITMP(htmpreg)) {
831                         M_INTMOVE(htmpreg, breg);
832                 }
833                 return PACK_REGS(ltmpreg, htmpreg);
834         } else {
835                 hr = GET_HIGH_REG(dst->vv.regoff);
836                 lr = GET_LOW_REG(dst->vv.regoff);
837                 if (((hr % 2) == 0) && lr == (hr + 1)) {
838                         /* the result is already in a even-odd pair */
839                         return dst->vv.regoff;                  
840                 } else if (((hr % 2) == 0) && (hr < R12)) {
841                         /* the high register is at a even position */
842                         M_INTMOVE(hr + 1, breg);
843                         return PACK_REGS(hr + 1, hr);
844                 } else if (((lr % 2) == 1) && (lr < R12)) {
845                         /* the low register is at a odd position */
846                         M_INTMOVE(lr - 1, breg);
847                         return PACK_REGS(lr, lr - 1);
848                 } else {
849                         /* no way to create an even-odd pair by 1 copy operation,
850                          * Use the temporary register pair.
851                          */
852                         if (! IS_REG_ITMP(ltmpreg)) {
853                                 M_INTMOVE(ltmpreg, breg);
854                         }
855                         if (! IS_REG_ITMP(htmpreg)) {
856                                 M_INTMOVE(htmpreg, breg);
857                         }
858                         return PACK_REGS(ltmpreg, htmpreg);
859                 }
860         }
861 }
862
863 void emit_restore_dst_even_odd(jitdata *jd, instruction *iptr, s4 htmpreg, s4 ltmpreg, s4 breg) {
864         codegendata *cd;
865         s4           hr, lr;
866         varinfo     *dst;
867
868         cd = jd->cd;
869         dst = VAROP(iptr->dst);
870
871         if (IS_INMEMORY(dst->flags)) {
872                 if (! IS_REG_ITMP(ltmpreg)) {
873                         M_INTMOVE(breg, ltmpreg);
874                 }
875                 if (! IS_REG_ITMP(htmpreg)) {
876                         M_INTMOVE(breg, htmpreg);
877                 }
878         } else {
879                 hr = GET_HIGH_REG(dst->vv.regoff);
880                 lr = GET_LOW_REG(dst->vv.regoff);
881                 if (((hr % 2) == 0) && lr == (hr + 1)) {
882                         return;
883                 } else if (((hr % 2) == 0) && (hr < R12)) {
884                         M_INTMOVE(breg, hr + 1);
885                 } else if (((lr % 2) == 1) && (lr < R12)) {
886                         M_INTMOVE(breg, lr - 1);
887                 } else {
888                         if (! IS_REG_ITMP(ltmpreg)) {
889                                 M_INTMOVE(breg, ltmpreg);
890                         }
891                         if (! IS_REG_ITMP(htmpreg)) {
892                                 M_INTMOVE(breg, htmpreg);
893                         }
894                 }
895         }
896 }
897
898 void emit_copy_dst(jitdata *jd, instruction *iptr, s4 dtmpreg) {
899         codegendata *cd;
900         varinfo *dst;
901         cd = jd->cd;
902         dst = VAROP(iptr->dst);
903         if (! IS_INMEMORY(dst->flags)) {
904                 if (dst->vv.regoff != dtmpreg) {
905                         if (IS_FLT_DBL_TYPE(dst->type)) {
906                                 M_FLTMOVE(dtmpreg, dst->vv.regoff);
907                         } else if (IS_2_WORD_TYPE(dst->type)) {
908                                 M_LNGMOVE(dtmpreg, dst->vv.regoff);
909                         } else {
910                                 M_INTMOVE(dtmpreg, dst->vv.regoff);
911                         }
912                 }
913         }
914 }
915
916 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt) {
917
918         s4 branchdisp = disp;
919         s4 branchmpc;
920         u1 *ref;
921
922         if (N_VALID_BRANCH(branchdisp)) {
923
924                 /* valid displacement */
925
926                 switch (condition) {
927                         case BRANCH_EQ:
928                                 M_BEQ(branchdisp);
929                                 break;
930                         case BRANCH_NE:
931                                 M_BNE(branchdisp);
932                                 break;
933                         case BRANCH_LT:
934                                 M_BLT(branchdisp);
935                                 break;
936                         case BRANCH_GE:
937                                 M_BGE(branchdisp);
938                                 break;
939                         case BRANCH_GT:
940                                 M_BGT(branchdisp);
941                                 break;
942                         case BRANCH_LE:
943                                 M_BLE(branchdisp);
944                                 break;
945                         case BRANCH_UNCONDITIONAL:
946                                 M_BR(branchdisp);
947                                 break;
948                         default:
949                                 vm_abort("emit_branch: unknown condition %d", condition);
950                 }
951         } else {
952
953                 /* If LONGBRANCHES is not set, the flag and the error flag */
954
955                 if (!CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
956                         cd->flags |= (CODEGENDATA_FLAG_ERROR |
957                                 CODEGENDATA_FLAG_LONGBRANCHES);
958                 }
959
960                 /* If error flag is set, do nothing. The method has to be recompiled. */
961
962                 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd) && CODEGENDATA_HAS_FLAG_ERROR(cd)) {
963                         return;
964                 }
965
966                 /* Patch the displacement to branch over the actual branch manually
967                  * to not get yet more nops.
968                  */
969
970                 branchmpc = cd->mcodeptr - cd->mcodebase;
971                 ref = cd->mcodeptr;
972
973                 switch (condition) {
974                         case BRANCH_EQ:
975                                 M_BNE(0);
976                                 break;
977                         case BRANCH_NE:
978                                 M_BEQ(0);
979                                 break;
980                         case BRANCH_LT:
981                                 M_BGE(0);
982                                 break;
983                         case BRANCH_GE:
984                                 M_BLT(0);
985                                 break;
986                         case BRANCH_GT:
987                                 M_BLE(0);
988                                 break;
989                         case BRANCH_LE:
990                                 M_BGT(0);
991                                 break;
992                         case BRANCH_UNCONDITIONAL:
993                                 /* fall through, no displacement to patch */
994                                 ref = NULL;
995                                 break;
996                         default:
997                                 vm_abort("emit_branch: unknown condition %d", condition);
998                 }
999
1000                 /* The actual long branch */
1001
1002                 disp = dseg_add_s4(cd, branchmpc + disp - N_PV_OFFSET);
1003                 M_ILD_DSEG(REG_ITMP3, disp);
1004                 M_AADD(REG_PV, REG_ITMP3);
1005                 M_JMP(RN, REG_ITMP3);
1006
1007                 /* Patch back the displacement */
1008
1009                 if (ref != NULL) {
1010                         *(u4 *)ref |= (u4)((cd->mcodeptr - ref) / 2);
1011                 }
1012         }
1013 }
1014
1015 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg) {
1016         if (INSTRUCTION_MUST_CHECK(iptr)) {
1017                 M_TEST(reg);
1018                 M_BNE(SZ_BRC + SZ_ILL);
1019                 M_ILL(EXCEPTION_HARDWARE_ARITHMETIC);
1020         }
1021 }
1022
1023 /* emit_arrayindexoutofbounds_check ********************************************
1024
1025    Emit a ArrayIndexOutOfBoundsException check.
1026
1027 *******************************************************************************/
1028
1029 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
1030 {
1031         if (INSTRUCTION_MUST_CHECK(iptr)) {
1032                 /* Size is s4, >= 0
1033                  * Do unsigned comparison to catch negative indexes.
1034                  */
1035                 N_CL(s2, OFFSET(java_arrayheader, size), RN, s1);
1036         M_BLT(SZ_BRC + SZ_ILL);
1037                 M_ILL2(s2, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
1038         }
1039 }
1040
1041 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1) {
1042         if (INSTRUCTION_MUST_CHECK(iptr)) {
1043                 if (reg != RN) {
1044                         M_TEST(reg);
1045                 }
1046                 switch (condition) {
1047                         case BRANCH_LE:
1048                                 M_BGT(SZ_BRC + SZ_ILL);
1049                                 break;
1050                         case BRANCH_EQ:
1051                                 M_BNE(SZ_BRC + SZ_ILL);
1052                                 break;
1053                         case BRANCH_GT:
1054                                 M_BLE(SZ_BRC + SZ_ILL);
1055                                 break;
1056                         default:
1057                                 vm_abort("emit_classcast_check: unknown condition %d", condition);
1058                 }
1059                 M_ILL2(s1, EXCEPTION_HARDWARE_CLASSCAST);
1060         }
1061 }
1062
1063 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg) {
1064         if (INSTRUCTION_MUST_CHECK(iptr)) {
1065                 M_TEST(reg);
1066                 M_BNE(SZ_BRC + SZ_ILL);
1067                 M_ILL(EXCEPTION_HARDWARE_NULLPOINTER);
1068         }
1069 }
1070
1071 void emit_exception_check(codegendata *cd, instruction *iptr) {
1072         if (INSTRUCTION_MUST_CHECK(iptr)) {
1073                 M_TEST(REG_RESULT);
1074                 M_BNE(SZ_BRC + SZ_ILL);
1075                 M_ILL(EXCEPTION_HARDWARE_EXCEPTION);
1076         }
1077 }
1078
1079 void emit_restore_pv(codegendata *cd) {
1080         s4 offset, offset_imm;
1081
1082         /*
1083         N_BASR(REG_PV, RN);
1084         disp = (s4) (cd->mcodeptr - cd->mcodebase);
1085         M_ASUB_IMM32(disp, REG_ITMP1, REG_PV);
1086         */
1087
1088         /* If the offset from the method start does not fit into an immediate
1089          * value, we can't put it into the data segment!
1090          */
1091
1092         /* Displacement from start of method to here */
1093
1094         offset = (s4) (cd->mcodeptr - cd->mcodebase);
1095         offset_imm = -offset - SZ_BASR + N_PV_OFFSET;
1096
1097         if (N_VALID_IMM(offset_imm)) {
1098                 /* Get program counter */
1099                 N_BASR(REG_PV, RN);
1100                 /* Substract displacement */
1101                 M_AADD_IMM(offset_imm, REG_PV);
1102         } else {
1103                 /* Save program counter and jump over displacement in instruction flow */
1104                 N_BRAS(REG_PV, SZ_BRAS + SZ_LONG);
1105                 /* Place displacement here */
1106                 /* REG_PV points now exactly to this position */
1107                 N_LONG(-offset - SZ_BRAS + N_PV_OFFSET);
1108                 /* Substract *(REG_PV) from REG_PV */
1109                 N_A(REG_PV, 0, RN, REG_PV);
1110         }
1111 }
1112
1113 /*
1114  * These are local overrides for various environment variables in Emacs.
1115  * Please do not remove this and leave it at the end of the file, where
1116  * Emacs will automagically detect them.
1117  * ---------------------------------------------------------------------
1118  * Local variables:
1119  * mode: c
1120  * indent-tabs-mode: t
1121  * c-basic-offset: 4
1122  * tab-width: 4
1123  * End:
1124  */