* src/vm/jit/powerpc/emit.c (emit_replacement_stubs): Do not
[cacao.git] / src / vm / jit / powerpc / emit.c
1 /* src/vm/jit/powerpc/emit.c - PowerPC code emitter functions
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    $Id: emit.c 4398 2006-01-31 23:43:08Z twisti $
30
31 */
32
33
34 #include "config.h"
35
36 #include <assert.h>
37
38 #include "vm/types.h"
39
40 #include "md-abi.h"
41
42 #include "vm/jit/powerpc/codegen.h"
43
44 #include "mm/memory.h"
45 #include "vm/builtin.h"
46 #include "vm/exceptions.h"
47 #include "vm/options.h"
48 #include "vm/jit/asmpart.h"
49 #include "vm/jit/dseg.h"
50 #include "vm/jit/emit-common.h"
51 #include "vm/jit/jit.h"
52 #include "vm/jit/replace.h"
53
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 * 4;
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_load_low ***************************************************************
99
100    Emits a possible load of the low 32-bits of an operand.
101
102 *******************************************************************************/
103
104 s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
105 {
106         codegendata  *cd;
107         s4            disp;
108         s4            reg;
109
110         assert(src->type == TYPE_LNG);
111
112         /* get required compiler data */
113
114         cd = jd->cd;
115
116         if (IS_INMEMORY(src->flags)) {
117                 COUNT_SPILLS;
118
119                 disp = src->vv.regoff * 4;
120
121                 M_ILD(tempreg, REG_SP, disp + 4);
122
123                 reg = tempreg;
124         }
125         else
126                 reg = GET_LOW_REG(src->vv.regoff);
127
128         return reg;
129 }
130
131
132 /* emit_load_high **************************************************************
133
134    Emits a possible load of the high 32-bits of an operand.
135
136 *******************************************************************************/
137
138 s4 emit_load_high(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
139 {
140         codegendata  *cd;
141         s4            disp;
142         s4            reg;
143
144         assert(src->type == TYPE_LNG);
145
146         /* get required compiler data */
147
148         cd = jd->cd;
149
150         if (IS_INMEMORY(src->flags)) {
151                 COUNT_SPILLS;
152
153                 disp = src->vv.regoff * 4;
154
155                 M_ILD(tempreg, REG_SP, disp);
156
157                 reg = tempreg;
158         }
159         else
160                 reg = GET_HIGH_REG(src->vv.regoff);
161
162         return reg;
163 }
164
165
166 /* emit_store ******************************************************************
167
168    XXX
169
170 *******************************************************************************/
171
172 void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
173 {
174         codegendata *cd;
175
176         /* get required compiler data */
177
178         cd = jd->cd;
179
180         if (IS_INMEMORY(dst->flags)) {
181                 COUNT_SPILLS;
182
183                 if (IS_FLT_DBL_TYPE(dst->type)) {
184                         if (IS_2_WORD_TYPE(dst->type))
185                                 M_DST(d, REG_SP, dst->vv.regoff * 4);
186                         else
187                                 M_FST(d, REG_SP, dst->vv.regoff * 4);
188                 }
189                 else {
190                         if (IS_2_WORD_TYPE(dst->type))
191                                 M_LST(d, REG_SP, dst->vv.regoff * 4);
192                         else
193                                 M_IST(d, REG_SP, dst->vv.regoff * 4);
194                 }
195         }
196 }
197
198
199 /* emit_copy *******************************************************************
200
201    Generates a register/memory to register/memory copy.
202
203 *******************************************************************************/
204
205 void emit_copy(jitdata *jd, instruction *iptr, varinfo *src, varinfo *dst)
206 {
207         codegendata  *cd;
208         s4            s1, d;
209
210         /* get required compiler data */
211
212         cd = jd->cd;
213
214         if ((src->vv.regoff != dst->vv.regoff) ||
215                 (IS_INMEMORY(src->flags ^ dst->flags))) {
216
217                 /* If one of the variables resides in memory, we can eliminate
218                    the register move from/to the temporary register with the
219                    order of getting the destination register and the load. */
220
221                 if (IS_INMEMORY(src->flags)) {
222                         if (IS_LNG_TYPE(src->type))
223                                 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
224                         else
225                                 d = codegen_reg_of_var(iptr->opc, dst, REG_IFTMP);
226
227                         s1 = emit_load(jd, iptr, src, d);
228                 }
229                 else {
230                         if (IS_LNG_TYPE(src->type))
231                                 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
232                         else
233                                 s1 = emit_load(jd, iptr, src, REG_IFTMP);
234
235                         d = codegen_reg_of_var(iptr->opc, dst, s1);
236                 }
237
238                 if (s1 != d) {
239                         if (IS_FLT_DBL_TYPE(src->type))
240                                 M_FMOV(s1, d);
241                         else {
242                                 if (IS_2_WORD_TYPE(src->type)) {
243                                         M_MOV(GET_LOW_REG(s1), GET_LOW_REG(d));
244                                         M_MOV(GET_HIGH_REG(s1), GET_HIGH_REG(d));
245                 }
246                                 else
247                     M_MOV(s1, d);
248                         }
249                 }
250
251                 emit_store(jd, iptr, dst, d);
252         }
253 }
254
255
256 /* emit_iconst *****************************************************************
257
258    XXX
259
260 *******************************************************************************/
261
262 void emit_iconst(codegendata *cd, s4 d, s4 value)
263 {
264         s4 disp;
265
266         if ((value >= -32768) && (value <= 32767))
267                 M_LDA_INTERN(d, REG_ZERO, value);
268         else {
269                 disp = dseg_add_s4(cd, value);
270                 M_ILD(d, REG_PV, disp);
271         }
272 }
273
274
275 /* emit_nullpointer_check ******************************************************
276
277    Emit a NullPointerException check.
278
279 *******************************************************************************/
280
281 void emit_nullpointer_check(codegendata *cd, s4 reg)
282 {
283         if (checknull) {
284                 M_TST(reg);
285                 M_BEQ(0);
286                 codegen_add_nullpointerexception_ref(cd);
287         }
288 }
289
290
291 /* emit_arrayindexoutofbounds_check ********************************************
292
293    Emit a ArrayIndexOutOfBoundsException check.
294
295 *******************************************************************************/
296
297 void emit_arrayindexoutofbounds_check(codegendata *cd, s4 s1, s4 s2)
298 {
299 #if 0
300         if (checkbounds) {
301                 M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
302                 M_CMPU(s2, REG_ITMP3);
303                 M_BGE(0);
304                 codegen_add_arrayindexoutofboundsexception_ref(cd, s2);
305         }
306 #else
307         M_ILD(REG_ITMP3, s1, OFFSET(java_arrayheader, size));
308         M_CMPU(s2, REG_ITMP3);
309         M_BLT(1);
310         M_ALD_INTERN(s2, REG_ZERO, EXCEPTION_LOAD_DISP_ARRAYINDEXOUTOFBOUNDS);
311 #endif
312 }
313
314
315 /* emit_exception_stubs ********************************************************
316
317    Generates the code for the exception stubs.
318
319 *******************************************************************************/
320
321 void emit_exception_stubs(jitdata *jd)
322 {
323         codegendata  *cd;
324         registerdata *rd;
325         exceptionref *er;
326         s4            branchmpc;
327         s4            targetmpc;
328         s4            targetdisp;
329         s4            disp;
330
331         /* get required compiler data */
332
333         cd = jd->cd;
334         rd = jd->rd;
335
336         /* generate exception stubs */
337
338         targetdisp = 0;
339
340         for (er = cd->exceptionrefs; er != NULL; er = er->next) {
341                 /* back-patch the branch to this exception code */
342
343                 branchmpc = er->branchpos;
344                 targetmpc = cd->mcodeptr - cd->mcodebase;
345
346                 md_codegen_patch_branch(cd, branchmpc, targetmpc);
347
348                 MCODECHECK(100);
349
350                 /* Move the value register to a temporary register, if
351                    there is the need for it. */
352
353                 if (er->reg != -1)
354                         M_MOV(er->reg, REG_ITMP1);
355
356                 /* calcuate exception address */
357
358                 M_LDA(REG_ITMP2_XPC, REG_PV, er->branchpos - 4);
359
360                 /* move function to call into REG_ITMP3 */
361
362                 disp = dseg_add_functionptr(cd, er->function);
363                 M_ALD(REG_ITMP3, REG_PV, disp);
364
365                 if (targetdisp == 0) {
366                     targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
367
368                         if (jd->isleafmethod) {
369                                 M_MFLR(REG_ZERO);
370                                 M_AST(REG_ZERO, REG_SP, cd->stackframesize * 4 + LA_LR_OFFSET);
371                         }
372
373                         M_MOV(REG_PV, rd->argintregs[0]);
374                         M_MOV(REG_SP, rd->argintregs[1]);
375
376                         if (jd->isleafmethod)
377                                 M_MOV(REG_ZERO, rd->argintregs[2]);
378                         else
379                                 M_ALD(rd->argintregs[2],
380                                           REG_SP, cd->stackframesize * 4 + LA_LR_OFFSET);
381
382                         M_MOV(REG_ITMP2_XPC, rd->argintregs[3]);
383                         M_MOV(REG_ITMP1, rd->argintregs[4]);
384
385                         M_STWU(REG_SP, REG_SP, -(LA_SIZE + 6 * 4));
386                         M_AST(REG_ITMP2_XPC, REG_SP, LA_SIZE + 5 * 4);
387
388                         M_MTCTR(REG_ITMP3);
389                         M_JSR;
390                         M_MOV(REG_RESULT, REG_ITMP1_XPTR);
391
392                         M_ALD(REG_ITMP2_XPC, REG_SP, LA_SIZE + 5 * 4);
393                         M_IADD_IMM(REG_SP, LA_SIZE + 6 * 4, REG_SP);
394
395                         if (jd->isleafmethod) {
396                                 /* XXX FIXME: REG_ZERO can cause problems here! */
397                                 assert(cd->stackframesize * 4 <= 32767);
398
399                                 M_ALD(REG_ZERO, REG_SP, cd->stackframesize * 4 + LA_LR_OFFSET);
400                                 M_MTLR(REG_ZERO);
401                         }
402
403                         disp = dseg_add_functionptr(cd, asm_handle_exception);
404                         M_ALD(REG_ITMP3, REG_PV, disp);
405                         M_MTCTR(REG_ITMP3);
406                         M_RTS;
407                 }
408                 else {
409                         disp = (((u4 *) cd->mcodebase) + targetdisp) -
410                                 (((u4 *) cd->mcodeptr) + 1);
411                         M_BR(disp);
412                 }
413         }
414 }
415
416
417 /* emit_patcher_stubs **********************************************************
418
419    Generates the code for the patcher stubs.
420
421 *******************************************************************************/
422
423 void emit_patcher_stubs(jitdata *jd)
424 {
425         codegendata *cd;
426         patchref    *pref;
427         u4           mcode;
428         u1          *savedmcodeptr;
429         u1          *tmpmcodeptr;
430         s4           targetdisp;
431         s4           disp;
432
433         /* get required compiler data */
434
435         cd = jd->cd;
436
437         /* generate code patching stub call code */
438
439         targetdisp = 0;
440
441         for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
442                 /* check code segment size */
443
444                 MCODECHECK(100);
445
446                 /* Get machine code which is patched back in later. The
447                    call is 1 instruction word long. */
448
449                 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
450
451                 mcode = *((u4 *) tmpmcodeptr);
452
453                 /* Patch in the call to call the following code (done at
454                    compile time). */
455
456                 savedmcodeptr = cd->mcodeptr;   /* save current mcodeptr          */
457                 cd->mcodeptr  = tmpmcodeptr;    /* set mcodeptr to patch position */
458
459                 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
460                 M_BR(disp);
461
462                 cd->mcodeptr = savedmcodeptr;   /* restore the current mcodeptr   */
463
464                 /* create stack frame - keep stack 16-byte aligned */
465
466                 M_AADD_IMM(REG_SP, -8 * 4, REG_SP);
467
468                 /* calculate return address and move it onto the stack */
469
470                 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
471                 M_AST_INTERN(REG_ITMP3, REG_SP, 5 * 4);
472
473                 /* move pointer to java_objectheader onto stack */
474
475 #if defined(ENABLE_THREADS)
476                 /* order reversed because of data segment layout */
477
478                 (void) dseg_add_unique_address(cd, NULL);                  /* flcword */
479                 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
480                 disp = dseg_add_unique_address(cd, NULL);                  /* vftbl   */
481
482                 M_LDA(REG_ITMP3, REG_PV, disp);
483                 M_AST_INTERN(REG_ITMP3, REG_SP, 4 * 4);
484 #else
485                 /* do nothing */
486 #endif
487
488                 /* move machine code onto stack */
489
490                 disp = dseg_add_s4(cd, mcode);
491                 M_ILD(REG_ITMP3, REG_PV, disp);
492                 M_IST_INTERN(REG_ITMP3, REG_SP, 3 * 4);
493
494                 /* move class/method/field reference onto stack */
495
496                 disp = dseg_add_address(cd, pref->ref);
497                 M_ALD(REG_ITMP3, REG_PV, disp);
498                 M_AST_INTERN(REG_ITMP3, REG_SP, 2 * 4);
499
500                 /* move data segment displacement onto stack */
501
502                 disp = dseg_add_s4(cd, pref->disp);
503                 M_ILD(REG_ITMP3, REG_PV, disp);
504                 M_IST_INTERN(REG_ITMP3, REG_SP, 1 * 4);
505
506                 /* move patcher function pointer onto stack */
507
508                 disp = dseg_add_functionptr(cd, pref->patcher);
509                 M_ALD(REG_ITMP3, REG_PV, disp);
510                 M_AST_INTERN(REG_ITMP3, REG_SP, 0 * 4);
511
512                 if (targetdisp == 0) {
513                         targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
514
515                         disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
516                         M_ALD(REG_ITMP3, REG_PV, disp);
517                         M_MTCTR(REG_ITMP3);
518                         M_RTS;
519                 }
520                 else {
521                         disp = (((u4 *) cd->mcodebase) + targetdisp) -
522                                 (((u4 *) cd->mcodeptr) + 1);
523                         M_BR(disp);
524                 }
525         }
526 }
527
528
529 /* emit_replacement_stubs ******************************************************
530
531    Generates the code for the replacement stubs.
532
533 *******************************************************************************/
534
535 void emit_replacement_stubs(jitdata *jd)
536 {
537         codegendata *cd;
538         codeinfo    *code;
539         rplpoint    *rplp;
540         u1          *savedmcodeptr;
541         s4           disp;
542         s4           i;
543         u1          *outcode;
544
545         /* get required compiler data */
546
547         cd   = jd->cd;
548         code = jd->code;
549
550         rplp = code->rplpoints;
551
552         for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
553                 /* check code segment size */
554
555                 MCODECHECK(100);
556
557                 /* note start of stub code */
558
559                 outcode = (u1 *) (cd->mcodeptr - cd->mcodebase);
560
561                 /* make machine code for patching */
562
563                 savedmcodeptr = cd->mcodeptr;
564                 cd->mcodeptr  = (u1 *) &(rplp->mcode) + 1;              /* big-endian */
565
566                 disp = (ptrint) ((s4 *) outcode - (s4 *) rplp->pc) - 1;
567                 M_BR(disp);
568
569                 cd->mcodeptr = savedmcodeptr;
570
571                 /* create stack frame - keep 16-byte aligned */
572
573                 M_AADD_IMM(REG_SP, -4 * 4, REG_SP);
574
575                 /* push address of `rplpoint` struct */
576
577                 disp = dseg_add_address(cd, rplp);
578                 M_ALD(REG_ITMP3, REG_PV, disp);
579                 M_AST_INTERN(REG_ITMP3, REG_SP, 0 * 4);
580
581                 /* jump to replacement function */
582
583                 disp = dseg_add_functionptr(cd, asm_replacement_out);
584                 M_ALD(REG_ITMP3, REG_PV, disp);
585                 M_MTCTR(REG_ITMP3);
586                 M_RTS;
587         }
588 }
589
590
591 /* emit_verbosecall_enter ******************************************************
592
593    Generates the code for the call trace.
594
595 *******************************************************************************/
596
597 #if !defined(NDEBUG)
598 void emit_verbosecall_enter(jitdata *jd)
599 {
600         methodinfo   *m;
601         codegendata  *cd;
602         registerdata *rd;
603         s4 s1, p, t, d;
604         int stack_off;
605         int stack_size;
606         methoddesc *md;
607
608         /* get required compiler data */
609
610         m  = jd->m;
611         cd = jd->cd;
612         rd = jd->rd;
613
614         md = m->parseddesc;
615         
616         /* Build up Stackframe for builtin_trace_args call (a multiple of 16) */
617         /* For Darwin:                                                        */
618         /* LA + TRACE_ARGS_NUM u8 args + methodinfo + LR                      */
619         /* LA_SIZE(=6*4) + 8*8         + 4          + 4  + 0(Padding)         */
620         /* 6 * 4 + 8 * 8 + 2 * 4 = 12 * 8 = 6 * 16                            */
621         /* For Linux:                                                         */
622         /* LA + (TRACE_ARGS_NUM - INT_ARG_CNT/2) u8 args + methodinfo         */
623         /* + INT_ARG_CNT * 4 ( save integer registers) + LR + 8 + 8 (Padding) */
624         /* LA_SIZE(=2*4) + 4 * 8 + 4 + 8 * 4 + 4 + 8                          */
625         /* 2 * 4 + 4 * 8 + 10 * 4 + 1 * 8 + 8= 12 * 8 = 6 * 16                */
626         
627         /* in nativestubs no Place to save the LR (Link Register) would be needed */
628         /* but since the stack frame has to be aligned the 4 Bytes would have to  */
629         /* be padded again */
630
631 #if defined(__DARWIN__)
632         stack_size = LA_SIZE + (TRACE_ARGS_NUM + 1) * 8;
633 #else
634         stack_size = 6 * 16;
635 #endif
636
637         /* mark trace code */
638
639         M_NOP;
640
641         M_MFLR(REG_ZERO);
642         M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
643         M_STWU(REG_SP, REG_SP, -stack_size);
644
645         M_CLR(REG_ITMP1);    /* clear help register */
646
647         /* save up to TRACE_ARGS_NUM arguments into the reserved stack space */
648 #if defined(__DARWIN__)
649         /* Copy Params starting from first to Stack                          */
650         /* since TRACE_ARGS == INT_ARG_CNT all used integer argument regs    */ 
651         /* are saved                                                         */
652         p = 0;
653 #else
654         /* Copy Params starting from fifth to Stack (INT_ARG_CNT/2) are in   */
655         /* integer argument regs                                             */
656         /* all integer argument registers have to be saved                   */
657         for (p = 0; p < 8; p++) {
658                 d = rd->argintregs[p];
659                 /* save integer argument registers */
660                 M_IST(d, REG_SP, LA_SIZE + 4 * 8 + 4 + p * 4);
661         }
662         p = 4;
663 #endif
664         stack_off = LA_SIZE;
665         for (; p < md->paramcount && p < TRACE_ARGS_NUM; p++, stack_off += 8) {
666                 t = md->paramtypes[p].type;
667                 if (IS_INT_LNG_TYPE(t)) {
668                         if (!md->params[p].inmemory) { /* Param in Arg Reg */
669                                 if (IS_2_WORD_TYPE(t)) {
670                                         M_IST(rd->argintregs[GET_HIGH_REG(md->params[p].regoff)]
671                                                   , REG_SP, stack_off);
672                                         M_IST(rd->argintregs[GET_LOW_REG(md->params[p].regoff)]
673                                                   , REG_SP, stack_off + 4);
674                                 } else {
675                                         M_IST(REG_ITMP1, REG_SP, stack_off);
676                                         M_IST(rd->argintregs[md->params[p].regoff]
677                                                   , REG_SP, stack_off + 4);
678                                 }
679                         } else { /* Param on Stack */
680                                 s1 = (md->params[p].regoff + cd->stackframesize) * 4 
681                                         + stack_size;
682                                 if (IS_2_WORD_TYPE(t)) {
683                                         M_ILD(REG_ITMP2, REG_SP, s1);
684                                         M_IST(REG_ITMP2, REG_SP, stack_off);
685                                         M_ILD(REG_ITMP2, REG_SP, s1 + 4);
686                                         M_IST(REG_ITMP2, REG_SP, stack_off + 4);
687                                 } else {
688                                         M_IST(REG_ITMP1, REG_SP, stack_off);
689                                         M_ILD(REG_ITMP2, REG_SP, s1);
690                                         M_IST(REG_ITMP2, REG_SP, stack_off + 4);
691                                 }
692                         }
693                 } else { /* IS_FLT_DBL_TYPE(t) */
694                         if (!md->params[p].inmemory) { /* in Arg Reg */
695                                 s1 = rd->argfltregs[md->params[p].regoff];
696                                 if (!IS_2_WORD_TYPE(t)) {
697                                         M_IST(REG_ITMP1, REG_SP, stack_off);
698                                         M_FST(s1, REG_SP, stack_off + 4);
699                                 } else {
700                                         M_DST(s1, REG_SP, stack_off);
701                                 }
702                         } else { /* on Stack */
703                                 /* this should not happen */
704                         }
705                 }
706         }
707
708         /* load first 4 (==INT_ARG_CNT/2) arguments into integer registers */
709 #if defined(__DARWIN__)
710         for (p = 0; p < 8; p++) {
711                 d = rd->argintregs[p];
712                 M_ILD(d, REG_SP, LA_SIZE + p * 4);
713         }
714 #else
715         /* LINUX */
716         /* Set integer and float argument registers vor trace_args call */
717         /* offset to saved integer argument registers                   */
718         stack_off = LA_SIZE + 4 * 8 + 4;
719         for (p = 0; (p < 4) && (p < md->paramcount); p++) {
720                 t = md->paramtypes[p].type;
721                 if (IS_INT_LNG_TYPE(t)) {
722                         /* "stretch" int types */
723                         if (!IS_2_WORD_TYPE(t)) {
724                                 M_CLR(rd->argintregs[2 * p]);
725                                 M_ILD(rd->argintregs[2 * p + 1], REG_SP,stack_off);
726                                 stack_off += 4;
727                         } else {
728                                 M_ILD(rd->argintregs[2 * p + 1], REG_SP,stack_off + 4);
729                                 M_ILD(rd->argintregs[2 * p], REG_SP,stack_off);
730                                 stack_off += 8;
731                         }
732                 } else { /* Float/Dbl */
733                         if (!md->params[p].inmemory) { /* Param in Arg Reg */
734                                 /* use reserved Place on Stack (sp + 5 * 16) to copy  */
735                                 /* float/double arg reg to int reg                    */
736                                 s1 = rd->argfltregs[md->params[p].regoff];
737                                 if (!IS_2_WORD_TYPE(t)) {
738                                         M_FST(s1, REG_SP, 5 * 16);
739                                         M_ILD(rd->argintregs[2 * p + 1], REG_SP, 5 * 16);
740                                         M_CLR(rd->argintregs[2 * p]);
741                                 } else {
742                                         M_DST(s1, REG_SP, 5 * 16);
743                                         M_ILD(rd->argintregs[2 * p + 1], REG_SP,  5 * 16 + 4);
744                                         M_ILD(rd->argintregs[2 * p], REG_SP, 5 * 16);
745                                 }
746                         }
747                 }
748         }
749 #endif
750
751         /* put methodinfo pointer on Stackframe */
752         p = dseg_add_address(cd, m);
753         M_ALD(REG_ITMP1, REG_PV, p);
754 #if defined(__DARWIN__)
755         M_AST(REG_ITMP1, REG_SP, LA_SIZE + TRACE_ARGS_NUM * 8); 
756 #else
757         M_AST(REG_ITMP1, REG_SP, LA_SIZE + 4 * 8);
758 #endif
759         p = dseg_add_functionptr(cd, builtin_trace_args);
760         M_ALD(REG_ITMP2, REG_PV, p);
761         M_MTCTR(REG_ITMP2);
762         M_JSR;
763
764 #if defined(__DARWIN__)
765         /* restore integer argument registers from the reserved stack space */
766
767         stack_off = LA_SIZE;
768         for (p = 0; p < md->paramcount && p < TRACE_ARGS_NUM; 
769                  p++, stack_off += 8) {
770                 t = md->paramtypes[p].type;
771
772                 if (IS_INT_LNG_TYPE(t)) {
773                         if (!md->params[p].inmemory) {
774                                 if (IS_2_WORD_TYPE(t)) {
775                                         M_ILD(rd->argintregs[GET_HIGH_REG(md->params[p].regoff)]
776                                                   , REG_SP, stack_off);
777                                         M_ILD(rd->argintregs[GET_LOW_REG(md->params[p].regoff)]
778                                                   , REG_SP, stack_off + 4);
779                                 } else {
780                                         M_ILD(rd->argintregs[md->params[p].regoff]
781                                                   , REG_SP, stack_off + 4);
782                                 }
783                         }
784                 }
785         }
786 #else
787         /* LINUX */
788         for (p = 0; p < 8; p++) {
789                 d = rd->argintregs[p];
790                 /* save integer argument registers */
791                 M_ILD(d, REG_SP, LA_SIZE + 4 * 8 + 4 + p * 4);
792         }
793 #endif
794
795         M_ALD(REG_ZERO, REG_SP, stack_size + LA_LR_OFFSET);
796         M_MTLR(REG_ZERO);
797         M_LDA(REG_SP, REG_SP, stack_size);
798
799         /* mark trace code */
800
801         M_NOP;
802 }
803 #endif /* !defined(NDEBUG) */
804
805
806 /* emit_verbosecall_exit *******************************************************
807
808    Generates the code for the call trace.
809
810 *******************************************************************************/
811
812 #if !defined(NDEBUG)
813 void emit_verbosecall_exit(jitdata *jd)
814 {
815         methodinfo   *m;
816         codegendata  *cd;
817         registerdata *rd;
818         methoddesc   *md;
819         s4            disp;
820
821         /* get required compiler data */
822
823         m  = jd->m;
824         cd = jd->cd;
825         rd = jd->rd;
826
827         md = m->parseddesc;
828         
829         /* mark trace code */
830
831         M_NOP;
832
833         M_MFLR(REG_ZERO);
834         M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
835         M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4));
836
837         /* save return registers */
838
839         M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
840         M_DST(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
841
842         /* keep this order */
843         switch (md->returntype.type) {
844         case TYPE_INT:
845         case TYPE_ADR:
846 #if defined(__DARWIN__)
847                 M_MOV(REG_RESULT, rd->argintregs[2]);
848                 M_CLR(rd->argintregs[1]);
849 #else
850                 M_MOV(REG_RESULT, rd->argintregs[3]);
851                 M_CLR(rd->argintregs[2]);
852 #endif
853                 break;
854
855         case TYPE_LNG:
856 #if defined(__DARWIN__)
857                 M_MOV(REG_RESULT2, rd->argintregs[2]);
858                 M_MOV(REG_RESULT, rd->argintregs[1]);
859 #else
860                 M_MOV(REG_RESULT2, rd->argintregs[3]);
861                 M_MOV(REG_RESULT, rd->argintregs[2]);
862 #endif
863                 break;
864         }
865
866         M_FLTMOVE(REG_FRESULT, rd->argfltregs[0]);
867         M_FLTMOVE(REG_FRESULT, rd->argfltregs[1]);
868
869         disp = dseg_add_address(cd, m);
870         M_ALD(rd->argintregs[0], REG_PV, disp);
871
872         disp = dseg_add_functionptr(cd, builtin_displaymethodstop);
873         M_ALD(REG_ITMP2, REG_PV, disp);
874         M_MTCTR(REG_ITMP2);
875         M_JSR;
876
877         /* restore return registers */
878
879         M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
880         M_DLD(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
881
882         M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4 + LA_LR_OFFSET);
883         M_MTLR(REG_ZERO);
884         M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4);
885
886         /* mark trace code */
887
888         M_NOP;
889 }
890 #endif /* !defined(NDEBUG) */
891
892
893 /*
894  * These are local overrides for various environment variables in Emacs.
895  * Please do not remove this and leave it at the end of the file, where
896  * Emacs will automagically detect them.
897  * ---------------------------------------------------------------------
898  * Local variables:
899  * mode: c
900  * indent-tabs-mode: t
901  * c-basic-offset: 4
902  * tab-width: 4
903  * End:
904  * vim:noexpandtab:sw=4:ts=4:
905  */