Merged revisions 7797-7917 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 7848 2007-05-01 21:40:26Z 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 * 4;
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 * 4);
125                         else
126                                 M_FST(d, REG_SP, dst->vv.regoff * 4);
127                 }
128                 else {
129                         if (IS_2_WORD_TYPE(dst->type))
130                                 M_LST(d, REG_SP, dst->vv.regoff * 4);
131                         else
132                                 M_IST(d, REG_SP, dst->vv.regoff * 4);
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
231         /* get required compiler data */
232
233         cd = jd->cd;
234
235         /* generate code patching stub call code */
236
237         targetdisp = 0;
238
239         for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
240                 /* check code segment size */
241
242                 MCODECHECK(100);
243
244                 /* Get machine code which is patched back in later. The
245                    call is 1 instruction word long. */
246
247                 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
248
249                 mcode = *((u4 *) tmpmcodeptr);
250
251                 /* Patch in the call to call the following code (done at
252                    compile time). */
253
254                 savedmcodeptr = cd->mcodeptr;   /* save current mcodeptr              */
255                 cd->mcodeptr  = tmpmcodeptr;    /* set mcodeptr to patch position     */
256
257                 disp = (savedmcodeptr) - (tmpmcodeptr);
258                 M_BSR(REG_ITMP3, disp);
259
260                 cd->mcodeptr = savedmcodeptr;   /* restore the current mcodeptr       */
261
262                 /* create stack frame */
263
264                 M_ASUB_IMM(6 * 4, REG_SP);
265
266                 /* move return address onto stack */
267
268                 M_AST(REG_ITMP3, REG_SP, 5 * 4);
269
270                 /* move pointer to java_objectheader onto stack */
271
272 #if defined(ENABLE_THREADS)
273                 /* create a virtual java_objectheader */
274
275                 (void) dseg_add_unique_address(cd, NULL);                  /* flcword */
276                 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
277                 disp = dseg_add_unique_address(cd, NULL);                  /* vftbl   */
278
279                 M_LDA(REG_ITMP3, REG_PV, disp);
280                 M_AST(REG_ITMP3, REG_SP, 4 * 4);
281 #else
282                 /* nothing to do */
283 #endif
284
285                 /* move machine code onto stack */
286
287                 disp = dseg_add_s4(cd, mcode);
288                 M_ILD(REG_ITMP3, REG_PV, disp);
289                 M_IST(REG_ITMP3, REG_SP, 3 * 4);
290
291                 /* move class/method/field reference onto stack */
292
293                 disp = dseg_add_address(cd, pref->ref);
294                 M_ALD(REG_ITMP3, REG_PV, disp);
295                 M_AST(REG_ITMP3, REG_SP, 2 * 4);
296
297                 /* move data segment displacement onto stack */
298
299                 disp = dseg_add_s4(cd, pref->disp);
300                 M_ILD(REG_ITMP3, REG_PV, disp);
301                 M_IST(REG_ITMP3, REG_SP, 1 * 4);
302
303                 /* move patcher function pointer onto stack */
304
305                 disp = dseg_add_functionptr(cd, pref->patcher);
306                 M_ALD(REG_ITMP3, REG_PV, disp);
307                 M_AST(REG_ITMP3, REG_SP, 0 * 4);
308
309                 if (targetdisp == 0) {
310                         targetdisp = (cd->mcodeptr) - (cd->mcodebase);
311
312                         disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
313                         M_ALD(REG_ITMP3, REG_PV, disp);
314                         M_JMP(RN, REG_ITMP3);
315                 }
316                 else {
317                         disp = ((cd->mcodebase) + targetdisp) -
318                                 (( cd->mcodeptr) );
319
320                         M_BR(disp);
321                 }
322         }
323 }
324
325
326 /* emit_replacement_stubs ******************************************************
327
328    Generates the code for the replacement stubs.
329
330 *******************************************************************************/
331
332 void emit_replacement_stubs(jitdata *jd)
333 {
334 #if 0
335         codegendata *cd;
336         codeinfo    *code;
337         rplpoint    *rplp;
338         s4           disp;
339         s4           i;
340
341         /* get required compiler data */
342
343         cd   = jd->cd;
344         code = jd->code;
345
346         rplp = code->rplpoints;
347
348         for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
349                 /* check code segment size */
350
351                 MCODECHECK(512);
352
353                 /* note start of stub code */
354
355                 rplp->outcode = (u1 *) (ptrint) (cd->mcodeptr - cd->mcodebase);
356
357                 /* make machine code for patching */
358
359                 disp = (ptrint) (rplp->outcode - rplp->pc) - 5;
360
361                 rplp->mcode = 0xe9 | ((u8) disp << 8);
362
363                 /* push address of `rplpoint` struct */
364                         
365                 M_MOV_IMM(rplp, REG_ITMP3);
366                 M_PUSH(REG_ITMP3);
367
368                 /* jump to replacement function */
369
370                 M_MOV_IMM(asm_replacement_out, REG_ITMP3);
371                 M_JMP(REG_ITMP3);
372         }
373 #endif
374 }
375         
376
377 /* emit_verbosecall_enter ******************************************************
378
379    Generates the code for the call trace.
380
381 *******************************************************************************/
382
383 #if !defined(NDEBUG)
384 void emit_verbosecall_enter(jitdata *jd)
385 {
386         
387         methodinfo   *m;
388         codegendata  *cd;
389         methoddesc   *md;
390         s4            i, j, k;
391         s4            stackframesize, off, foff, aoff, doff, t, iargctr, fargctr, disp;
392
393         /* get required compiler data */
394
395         m  = jd->m;
396         cd = jd->cd;
397
398         md = m->parseddesc;
399
400         /* mark trace code */
401
402         M_NOP;
403
404         stackframesize = 
405                 (6 * 8) + /* s8 on stack parameters x 6 */
406                 (1 * 4) + /* methodinfo on stack parameter */
407                 (ARG_CNT * 8) +
408                 (TMP_CNT * 8) 
409                 ;
410
411         M_ASUB_IMM(stackframesize, REG_SP); /* allocate stackframe */
412
413         /* save argument registers */
414
415         off = (6 * 8) + (1 * 4);
416
417         for (i = 0; i < INT_ARG_CNT; i++, off += 8)
418                 M_IST(abi_registers_integer_argument[i], REG_SP, off);
419
420         for (i = 0; i < FLT_ARG_CNT; i++, off += 8)
421                 M_DST(abi_registers_float_argument[i], REG_SP, off);
422
423         /* save temporary registers for leaf methods */
424
425         if (jd->isleafmethod) {
426                 for (i = 0; i < INT_TMP_CNT; i++, off += 8)
427                         M_LST(abi_registers_integer_temporary[i], REG_SP, off);
428
429                 for (i = 0; i < FLT_TMP_CNT; i++, off += 8)
430                         M_DST(abi_registers_float_temporary[i], REG_SP, off);
431         }
432
433         /* Load arguments to new locations */
434
435         /* First move all arguments to stack
436          *
437          * (s8) a7
438          * (s8) a2
439          *   ...
440          * (s8) a1 \ Auxilliary stack frame
441          * (s8) a0 /
442          * ------- <---- SP
443          */
444
445         M_ASUB_IMM(2 * 8, REG_SP);
446         
447         /* offset to where first integer arg is saved on stack */
448         off = (2 * 8) + (6 * 8) + (1 * 4); 
449         /* offset to where first float arg is saved on stack */
450         foff = off + (INT_ARG_CNT * 8); 
451         /* offset to where first argument is passed on stack */
452         aoff = (2 * 8) + stackframesize + (cd->stackframesize * 4);
453         /* offset to destination on stack */
454         doff = 0; 
455
456         iargctr = fargctr = 0;
457
458         ICONST(REG_ITMP1, 0);
459
460         for (i = 0; i < md->paramcount && i < 8; i++) {
461                 t = md->paramtypes[i].type;
462
463                 M_IST(REG_ITMP1, REG_SP, doff);
464                 M_IST(REG_ITMP1, REG_SP, doff + 4);
465
466                 if (IS_FLT_DBL_TYPE(t)) {
467                         if (fargctr < 2) { /* passed in register */
468                                 N_STD(REG_FA0 + fargctr, doff, RN, REG_SP);
469                                 fargctr += 1;
470                         } else { /* passed on stack */
471                                 if (IS_2_WORD_TYPE(t)) {
472                                         N_MVC(doff, 8, REG_SP, aoff, REG_SP);
473                                         aoff += 8;
474                                 } else {
475                                         N_MVC(doff + 4, 4, REG_SP, aoff, REG_SP);
476                                         aoff += 4;
477                                 }
478                         }
479                 } else {
480                         if (IS_2_WORD_TYPE(t)) {
481                                 if (iargctr < 4) { /* passed in 2 registers */
482                                         N_STM(REG_A0 + iargctr, REG_A0 + iargctr + 1, doff, REG_SP);
483                                         iargctr += 2;
484                                 } else { /* passed on stack */
485                                         N_MVC(doff, 8, REG_SP, aoff, REG_SP);
486                                         aoff += 8;
487                                 }
488                         } else {
489                                 if (iargctr < 5) { /* passed in register */
490                                         N_ST(REG_A0 + iargctr, doff + 4, RN, REG_SP);
491                                         iargctr += 1;
492                                 } else { /* passed on stack */
493                                         N_MVC(doff + 4, 4, REG_SP, aoff, REG_SP);
494                                         aoff += 4;
495                                 }
496                         }
497                 }
498
499                 doff += 8;
500         }
501
502         /* Now move a0 and a1 to registers
503          *
504          * (s8) a7
505          *   ...
506          * (s8) a2
507          * ------- <- SP
508          * (s8) a0 ==> a0, a1
509          * (s8) a1 ==> a2, a3
510          */
511
512         N_LM(REG_A0, REG_A1, 0, REG_SP);
513         N_LM(REG_A2, REG_A3, 8, REG_SP);
514
515         M_AADD_IMM(2 * 8, REG_SP);
516
517         /* Finally load methodinfo argument */
518
519         disp = dseg_add_address(cd, m);
520         M_ALD(REG_ITMP2, REG_PV, disp); 
521         M_AST(REG_ITMP2, REG_SP, 6 * 8);
522
523         /* Call builtin_verbosecall_enter */
524
525         disp = dseg_add_address(cd, builtin_verbosecall_enter);
526         M_ALD(REG_ITMP2, REG_PV, disp);
527         M_ASUB_IMM(96, REG_SP);
528         M_CALL(REG_ITMP2);
529         M_AADD_IMM(96, REG_SP);
530
531         /* restore argument registers */
532
533         off = (6 * 8) + (1 * 4);
534
535         for (i = 0; i < INT_ARG_CNT; i++, off += 8)
536                 M_ILD(abi_registers_integer_argument[i], REG_SP, off);
537
538         for (i = 0; i < FLT_ARG_CNT; i++, off += 8)
539                 M_DLD(abi_registers_float_argument[i], REG_SP, off);
540
541         /* restore temporary registers for leaf methods */
542
543         if (jd->isleafmethod) {
544                 for (i = 0; i < INT_TMP_CNT; i++, off += 8)
545                         M_ILD(abi_registers_integer_temporary[i], REG_SP, off);
546
547                 for (i = 0; i < FLT_TMP_CNT; i++, off += 8)
548                         M_DLD(abi_registers_float_temporary[i], REG_SP, off);
549         }
550
551         /* remove stackframe */
552
553         M_AADD_IMM(stackframesize, REG_SP);
554
555         /* mark trace code */
556
557         M_NOP;
558 }
559 #endif /* !defined(NDEBUG) */
560
561
562 /* emit_verbosecall_exit *******************************************************
563
564    Generates the code for the call trace.
565
566 *******************************************************************************/
567
568 #if !defined(NDEBUG)
569 void emit_verbosecall_exit(jitdata *jd)
570 {
571         methodinfo   *m;
572         codegendata  *cd;
573         registerdata *rd;
574         s4            disp;
575
576         /* get required compiler data */
577
578         m  = jd->m;
579         cd = jd->cd;
580         rd = jd->rd;
581
582         /* mark trace code */
583
584         M_NOP;
585
586         M_ASUB_IMM(2 * 8, REG_SP);
587
588         N_STM(REG_RESULT, REG_RESULT2, 0 * 8, REG_SP);
589         M_DST(REG_FRESULT, REG_SP, 1 * 8);
590
591         if (IS_2_WORD_TYPE(m->parseddesc->returntype.type)) {
592                 /* (REG_A0, REG_A1) == (REG_RESULT, REG_RESULT2), se no need to move */
593         } else {
594                 M_INTMOVE(REG_RESULT, REG_A1);
595                 ICONST(REG_A0, 0);
596         }
597
598         disp = dseg_add_address(cd, m);
599         M_ALD(REG_A2, REG_PV, disp);
600
601         /* REG_FRESULT is REG_FA0, so no need to move */
602         M_FLTMOVE(REG_FRESULT, REG_FA1);
603
604         disp = dseg_add_address(cd, builtin_verbosecall_exit);
605         M_ALD(REG_ITMP1, REG_PV, disp);
606         M_ASUB_IMM(96, REG_SP);
607         M_CALL(REG_ITMP1);
608         M_AADD_IMM(96, REG_SP);
609
610         N_LM(REG_RESULT, REG_RESULT2, 0 * 8, REG_SP);
611         M_DLD(REG_FRESULT, REG_SP, 1 * 8);
612
613         M_AADD_IMM(2 * 8, REG_SP);
614
615         /* mark trace code */
616
617         M_NOP;
618 }
619 #endif /* !defined(NDEBUG) */
620
621
622 /* emit_load_high **************************************************************
623
624    Emits a possible load of the high 32-bits of an operand.
625
626 *******************************************************************************/
627
628 __PORTED__ s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
629 {
630         codegendata  *cd;
631         s4            disp;
632         s4            reg;
633
634         assert(src->type == TYPE_LNG);
635
636         /* get required compiler data */
637
638         cd = jd->cd;
639
640         if (IS_INMEMORY(src->flags)) {
641                 COUNT_SPILLS;
642
643                 disp = src->vv.regoff * 4;
644
645                 M_ILD(tempreg, REG_SP, disp);
646
647                 reg = tempreg;
648         }
649         else
650                 reg = GET_HIGH_REG(src->vv.regoff);
651
652         return reg;
653 }
654
655 /* emit_load_low ***************************************************************
656
657    Emits a possible load of the low 32-bits of an operand.
658
659 *******************************************************************************/
660
661 __PORTED__ s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
662 {
663         codegendata  *cd;
664         s4            disp;
665         s4            reg;
666
667         assert(src->type == TYPE_LNG);
668
669         /* get required compiler data */
670
671         cd = jd->cd;
672
673         if (IS_INMEMORY(src->flags)) {
674                 COUNT_SPILLS;
675
676                 disp = src->vv.regoff * 4;
677
678                 M_ILD(tempreg, REG_SP, disp + 4);
679
680                 reg = tempreg;
681         }
682         else
683                 reg = GET_LOW_REG(src->vv.regoff);
684
685         return reg;
686 }
687
688 s4 emit_load_s1_notzero(jitdata *jd, instruction *iptr, s4 tempreg) {
689         codegendata *cd = jd->cd;
690         s4 reg = emit_load_s1(jd, iptr, tempreg);
691         if (reg == 0) {
692                 M_MOV(reg, tempreg);
693                 return tempreg;
694         } else {
695                 return reg;
696         }
697 }
698
699 s4 emit_load_s2_notzero(jitdata *jd, instruction *iptr, s4 tempreg) {
700         codegendata *cd = jd->cd;
701         s4 reg = emit_load_s2(jd, iptr, tempreg);
702         if (reg == 0) {
703                 M_MOV(reg, tempreg);
704                 return tempreg;
705         } else {
706                 return reg;
707         }
708 }
709
710 s4 emit_load_s1_but(jitdata *jd, instruction *iptr, s4 tempreg, s4 notreg) {
711         codegendata *cd = jd->cd;
712         s4 reg = emit_load_s1(jd, iptr, tempreg);
713         if (reg == notreg) {
714                 M_MOV(reg, tempreg);
715                 return tempreg;
716         } else {
717                 return reg;
718         }
719 }
720
721 s4 emit_load_s2_but(jitdata *jd, instruction *iptr, s4 tempreg, s4 notreg) {
722         codegendata *cd = jd->cd;
723         s4 reg = emit_load_s2(jd, iptr, tempreg);
724         if (reg == notreg) {
725                 M_MOV(reg, tempreg);
726                 return tempreg;
727         } else {
728                 return reg;
729         }
730 }
731
732 s4 emit_alloc_dst_even_odd(jitdata *jd, instruction *iptr, s4 htmpreg, s4 ltmpreg, s4 breg) {
733         codegendata *cd;
734         s4           hr, lr;
735         varinfo     *dst;
736
737         /* (r0, r1)    
738          * (r2, r3)
739          * (r4, r5)
740          * (r6, r7)
741          * (r8, r9)
742          * (r10, r11)
743          * (r12, r13) Illegal, because r13 is PV
744          * (r14, r15) Illegal, because r15 is SP
745          */
746
747         cd = jd->cd;
748         dst = VAROP(iptr->dst);
749
750         if (IS_INMEMORY(dst->flags)) {
751                 if (! IS_REG_ITMP(ltmpreg)) {
752                         M_INTMOVE(ltmpreg, breg);
753                 }
754                 if (! IS_REG_ITMP(htmpreg)) {
755                         M_INTMOVE(htmpreg, breg);
756                 }
757                 return PACK_REGS(ltmpreg, htmpreg);
758         } else {
759                 hr = GET_HIGH_REG(dst->vv.regoff);
760                 lr = GET_LOW_REG(dst->vv.regoff);
761                 if (((hr % 2) == 0) && lr == (hr + 1)) {
762                         /* the result is already in a even-odd pair */
763                         return dst->vv.regoff;                  
764                 } else if (((hr % 2) == 0) && (hr < R12)) {
765                         /* the high register is at a even position */
766                         M_INTMOVE(hr + 1, breg);
767                         return PACK_REGS(hr + 1, hr);
768                 } else if (((lr % 2) == 1) && (lr < R12)) {
769                         /* the low register is at a odd position */
770                         M_INTMOVE(lr - 1, breg);
771                         return PACK_REGS(lr, lr - 1);
772                 } else {
773                         /* no way to create an even-odd pair by 1 copy operation,
774                          * Use the temporary register pair.
775                          */
776                         if (! IS_REG_ITMP(ltmpreg)) {
777                                 M_INTMOVE(ltmpreg, breg);
778                         }
779                         if (! IS_REG_ITMP(htmpreg)) {
780                                 M_INTMOVE(htmpreg, breg);
781                         }
782                         return PACK_REGS(ltmpreg, htmpreg);
783                 }
784         }
785 }
786
787 void emit_restore_dst_even_odd(jitdata *jd, instruction *iptr, s4 htmpreg, s4 ltmpreg, s4 breg) {
788         codegendata *cd;
789         s4           hr, lr;
790         varinfo     *dst;
791
792         cd = jd->cd;
793         dst = VAROP(iptr->dst);
794
795         if (IS_INMEMORY(dst->flags)) {
796                 if (! IS_REG_ITMP(ltmpreg)) {
797                         M_INTMOVE(breg, ltmpreg);
798                 }
799                 if (! IS_REG_ITMP(htmpreg)) {
800                         M_INTMOVE(breg, htmpreg);
801                 }
802         } else {
803                 hr = GET_HIGH_REG(dst->vv.regoff);
804                 lr = GET_LOW_REG(dst->vv.regoff);
805                 if (((hr % 2) == 0) && lr == (hr + 1)) {
806                         return;
807                 } else if (((hr % 2) == 0) && (hr < R12)) {
808                         M_INTMOVE(breg, hr + 1);
809                 } else if (((lr % 2) == 1) && (lr < R12)) {
810                         M_INTMOVE(breg, lr - 1);
811                 } else {
812                         if (! IS_REG_ITMP(ltmpreg)) {
813                                 M_INTMOVE(breg, ltmpreg);
814                         }
815                         if (! IS_REG_ITMP(htmpreg)) {
816                                 M_INTMOVE(breg, htmpreg);
817                         }
818                 }
819         }
820 }
821
822 void emit_copy_dst(jitdata *jd, instruction *iptr, s4 dtmpreg) {
823         codegendata *cd;
824         varinfo *dst;
825         cd = jd->cd;
826         dst = VAROP(iptr->dst);
827         if (! IS_INMEMORY(dst->flags)) {
828                 if (dst->vv.regoff != dtmpreg) {
829                         if (IS_FLT_DBL_TYPE(dst->type)) {
830                                 M_FLTMOVE(dtmpreg, dst->vv.regoff);
831                         } else if (IS_2_WORD_TYPE(dst->type)) {
832                                 M_LNGMOVE(dtmpreg, dst->vv.regoff);
833                         } else {
834                                 M_INTMOVE(dtmpreg, dst->vv.regoff);
835                         }
836                 }
837         }
838 }
839
840 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 opt) {
841
842         s4 branchdisp = disp;
843
844         switch (condition) {
845                 case BRANCH_EQ:
846                         M_BEQ(branchdisp);
847                         break;
848                 case BRANCH_NE:
849                         M_BNE(branchdisp);
850                         break;
851                 case BRANCH_LT:
852                         M_BLT(branchdisp);
853                         break;
854                 case BRANCH_GE:
855                         M_BGE(branchdisp);
856                         break;
857                 case BRANCH_GT:
858                         M_BGT(branchdisp);
859                         break;
860                 case BRANCH_LE:
861                         M_BLE(branchdisp);
862                         break;
863                 case BRANCH_UNCONDITIONAL:
864                         M_BR(branchdisp);
865                         break;
866                 default:
867                         vm_abort("emit_branch: unknown condition %d", condition);
868         }
869 }
870
871 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg) {
872         if (INSTRUCTION_MUST_CHECK(iptr)) {
873                 M_TEST(reg);
874                 M_BNE(SZ_BRC + SZ_ILL);
875                 M_ILL(EXCEPTION_HARDWARE_ARITHMETIC);
876         }
877 }
878
879 /* emit_arrayindexoutofbounds_check ********************************************
880
881    Emit a ArrayIndexOutOfBoundsException check.
882
883 *******************************************************************************/
884
885 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
886 {
887         if (INSTRUCTION_MUST_CHECK(iptr)) {
888                 /* Size is s4, >= 0
889                  * Do unsigned comparison to catch negative indexes.
890                  */
891                 N_CL(s2, OFFSET(java_arrayheader, size), RN, s1);
892         M_BLT(SZ_BRC + SZ_ILL);
893                 M_ILL2(s2, EXCEPTION_HARDWARE_ARRAYINDEXOUTOFBOUNDS);
894         }
895 }
896
897 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1) {
898         if (INSTRUCTION_MUST_CHECK(iptr)) {
899                 if (reg != RN) {
900                         M_TEST(reg);
901                 }
902                 switch (condition) {
903                         case BRANCH_LE:
904                                 M_BGT(SZ_BRC + SZ_ILL);
905                                 break;
906                         case BRANCH_EQ:
907                                 M_BNE(SZ_BRC + SZ_ILL);
908                                 break;
909                         case BRANCH_GT:
910                                 M_BLE(SZ_BRC + SZ_ILL);
911                                 break;
912                         default:
913                                 vm_abort("emit_classcast_check: unknown condition %d", condition);
914                 }
915                 M_ILL2(s1, EXCEPTION_HARDWARE_CLASSCAST);
916         }
917 }
918
919 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg) {
920         if (INSTRUCTION_MUST_CHECK(iptr)) {
921                 M_TEST(reg);
922                 M_BNE(SZ_BRC + SZ_ILL);
923                 M_ILL(EXCEPTION_HARDWARE_NULLPOINTER);
924         }
925 }
926
927 void emit_exception_check(codegendata *cd, instruction *iptr) {
928         if (INSTRUCTION_MUST_CHECK(iptr)) {
929                 M_TEST(REG_RESULT);
930                 M_BNE(SZ_BRC + SZ_ILL);
931                 M_ILL(EXCEPTION_HARDWARE_EXCEPTION);
932         }
933 }
934
935 void emit_restore_pv(codegendata *cd) {
936         s4 offset;
937
938         /*
939         N_BASR(REG_PV, RN);
940         disp = (s4) (cd->mcodeptr - cd->mcodebase);
941         M_ASUB_IMM32(disp, REG_ITMP1, REG_PV);
942         */
943
944         /* If the offset from the method start does not fit into an immediate
945          * value, we can't put it into the data segment!
946          */
947
948         /* Displacement from start of method to here */
949
950         offset = (s4) (cd->mcodeptr - cd->mcodebase);
951
952         if (N_VALID_IMM(-(offset + SZ_BASR))) {
953                 /* Get program counter */
954                 N_BASR(REG_PV, RN);
955                 /* Substract displacement */
956                 M_ASUB_IMM(offset + SZ_BASR, REG_PV);
957         } else {
958                 /* Save program counter and jump over displacement in instruction flow */
959                 N_BRAS(REG_PV, SZ_BRAS + SZ_LONG);
960                 /* Place displacement here */
961                 /* REG_PV points now exactly to this position */
962                 N_LONG(offset + SZ_BRAS);
963                 /* Substract *(REG_PV) from REG_PV */
964                 N_S(REG_PV, 0, RN, REG_PV);
965         }
966 }
967
968 /*
969  * These are local overrides for various environment variables in Emacs.
970  * Please do not remove this and leave it at the end of the file, where
971  * Emacs will automagically detect them.
972  * ---------------------------------------------------------------------
973  * Local variables:
974  * mode: c
975  * indent-tabs-mode: t
976  * c-basic-offset: 4
977  * tab-width: 4
978  * End:
979  */