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