* src/vm/jit/powerpc/emit.c (emit_replacement_stubs): Prepared for
[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         s4           disp;
541         s4           i;
542 #if !defined(NDEBUG)
543         u1          *savedmcodeptr;
544 #endif
545
546         /* get required compiler data */
547
548         cd   = jd->cd;
549         code = jd->code;
550
551         rplp = code->rplpoints;
552
553         /* store beginning of replacement stubs */
554
555         code->replacementstubs = (u1*) (cd->mcodeptr - cd->mcodebase);
556
557         for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
558                 /* do not generate stubs for non-trappable points */
559
560                 if (rplp->flags & RPLPOINT_FLAG_NOTRAP)
561                         continue;
562
563                 /* check code segment size */
564
565                 MCODECHECK(100);
566
567 #if !defined(NDEBUG)
568                 savedmcodeptr = cd->mcodeptr;
569 #endif
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                 assert((cd->mcodeptr - savedmcodeptr) == 4*REPLACEMENT_STUB_SIZE);
589         }
590 }
591
592
593 /* emit_verbosecall_enter ******************************************************
594
595    Generates the code for the call trace.
596
597 *******************************************************************************/
598
599 #if !defined(NDEBUG)
600 void emit_verbosecall_enter(jitdata *jd)
601 {
602         methodinfo   *m;
603         codegendata  *cd;
604         registerdata *rd;
605         s4 s1, p, t, d;
606         int stack_off;
607         int stack_size;
608         methoddesc *md;
609
610         /* get required compiler data */
611
612         m  = jd->m;
613         cd = jd->cd;
614         rd = jd->rd;
615
616         md = m->parseddesc;
617         
618         /* Build up Stackframe for builtin_trace_args call (a multiple of 16) */
619         /* For Darwin:                                                        */
620         /* LA + TRACE_ARGS_NUM u8 args + methodinfo + LR                      */
621         /* LA_SIZE(=6*4) + 8*8         + 4          + 4  + 0(Padding)         */
622         /* 6 * 4 + 8 * 8 + 2 * 4 = 12 * 8 = 6 * 16                            */
623         /* For Linux:                                                         */
624         /* LA + (TRACE_ARGS_NUM - INT_ARG_CNT/2) u8 args + methodinfo         */
625         /* + INT_ARG_CNT * 4 ( save integer registers) + LR + 8 + 8 (Padding) */
626         /* LA_SIZE(=2*4) + 4 * 8 + 4 + 8 * 4 + 4 + 8                          */
627         /* 2 * 4 + 4 * 8 + 10 * 4 + 1 * 8 + 8= 12 * 8 = 6 * 16                */
628         
629         /* in nativestubs no Place to save the LR (Link Register) would be needed */
630         /* but since the stack frame has to be aligned the 4 Bytes would have to  */
631         /* be padded again */
632
633 #if defined(__DARWIN__)
634         stack_size = LA_SIZE + (TRACE_ARGS_NUM + 1) * 8;
635 #else
636         stack_size = 6 * 16;
637 #endif
638
639         /* mark trace code */
640
641         M_NOP;
642
643         M_MFLR(REG_ZERO);
644         M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
645         M_STWU(REG_SP, REG_SP, -stack_size);
646
647         M_CLR(REG_ITMP1);    /* clear help register */
648
649         /* save up to TRACE_ARGS_NUM arguments into the reserved stack space */
650 #if defined(__DARWIN__)
651         /* Copy Params starting from first to Stack                          */
652         /* since TRACE_ARGS == INT_ARG_CNT all used integer argument regs    */ 
653         /* are saved                                                         */
654         p = 0;
655 #else
656         /* Copy Params starting from fifth to Stack (INT_ARG_CNT/2) are in   */
657         /* integer argument regs                                             */
658         /* all integer argument registers have to be saved                   */
659         for (p = 0; p < 8; p++) {
660                 d = rd->argintregs[p];
661                 /* save integer argument registers */
662                 M_IST(d, REG_SP, LA_SIZE + 4 * 8 + 4 + p * 4);
663         }
664         p = 4;
665 #endif
666         stack_off = LA_SIZE;
667         for (; p < md->paramcount && p < TRACE_ARGS_NUM; p++, stack_off += 8) {
668                 t = md->paramtypes[p].type;
669                 if (IS_INT_LNG_TYPE(t)) {
670                         if (!md->params[p].inmemory) { /* Param in Arg Reg */
671                                 if (IS_2_WORD_TYPE(t)) {
672                                         M_IST(rd->argintregs[GET_HIGH_REG(md->params[p].regoff)]
673                                                   , REG_SP, stack_off);
674                                         M_IST(rd->argintregs[GET_LOW_REG(md->params[p].regoff)]
675                                                   , REG_SP, stack_off + 4);
676                                 } else {
677                                         M_IST(REG_ITMP1, REG_SP, stack_off);
678                                         M_IST(rd->argintregs[md->params[p].regoff]
679                                                   , REG_SP, stack_off + 4);
680                                 }
681                         } else { /* Param on Stack */
682                                 s1 = (md->params[p].regoff + cd->stackframesize) * 4 
683                                         + stack_size;
684                                 if (IS_2_WORD_TYPE(t)) {
685                                         M_ILD(REG_ITMP2, REG_SP, s1);
686                                         M_IST(REG_ITMP2, REG_SP, stack_off);
687                                         M_ILD(REG_ITMP2, REG_SP, s1 + 4);
688                                         M_IST(REG_ITMP2, REG_SP, stack_off + 4);
689                                 } else {
690                                         M_IST(REG_ITMP1, REG_SP, stack_off);
691                                         M_ILD(REG_ITMP2, REG_SP, s1);
692                                         M_IST(REG_ITMP2, REG_SP, stack_off + 4);
693                                 }
694                         }
695                 } else { /* IS_FLT_DBL_TYPE(t) */
696                         if (!md->params[p].inmemory) { /* in Arg Reg */
697                                 s1 = rd->argfltregs[md->params[p].regoff];
698                                 if (!IS_2_WORD_TYPE(t)) {
699                                         M_IST(REG_ITMP1, REG_SP, stack_off);
700                                         M_FST(s1, REG_SP, stack_off + 4);
701                                 } else {
702                                         M_DST(s1, REG_SP, stack_off);
703                                 }
704                         } else { /* on Stack */
705                                 /* this should not happen */
706                         }
707                 }
708         }
709
710         /* load first 4 (==INT_ARG_CNT/2) arguments into integer registers */
711 #if defined(__DARWIN__)
712         for (p = 0; p < 8; p++) {
713                 d = rd->argintregs[p];
714                 M_ILD(d, REG_SP, LA_SIZE + p * 4);
715         }
716 #else
717         /* LINUX */
718         /* Set integer and float argument registers vor trace_args call */
719         /* offset to saved integer argument registers                   */
720         stack_off = LA_SIZE + 4 * 8 + 4;
721         for (p = 0; (p < 4) && (p < md->paramcount); p++) {
722                 t = md->paramtypes[p].type;
723                 if (IS_INT_LNG_TYPE(t)) {
724                         /* "stretch" int types */
725                         if (!IS_2_WORD_TYPE(t)) {
726                                 M_CLR(rd->argintregs[2 * p]);
727                                 M_ILD(rd->argintregs[2 * p + 1], REG_SP,stack_off);
728                                 stack_off += 4;
729                         } else {
730                                 M_ILD(rd->argintregs[2 * p + 1], REG_SP,stack_off + 4);
731                                 M_ILD(rd->argintregs[2 * p], REG_SP,stack_off);
732                                 stack_off += 8;
733                         }
734                 } else { /* Float/Dbl */
735                         if (!md->params[p].inmemory) { /* Param in Arg Reg */
736                                 /* use reserved Place on Stack (sp + 5 * 16) to copy  */
737                                 /* float/double arg reg to int reg                    */
738                                 s1 = rd->argfltregs[md->params[p].regoff];
739                                 if (!IS_2_WORD_TYPE(t)) {
740                                         M_FST(s1, REG_SP, 5 * 16);
741                                         M_ILD(rd->argintregs[2 * p + 1], REG_SP, 5 * 16);
742                                         M_CLR(rd->argintregs[2 * p]);
743                                 } else {
744                                         M_DST(s1, REG_SP, 5 * 16);
745                                         M_ILD(rd->argintregs[2 * p + 1], REG_SP,  5 * 16 + 4);
746                                         M_ILD(rd->argintregs[2 * p], REG_SP, 5 * 16);
747                                 }
748                         }
749                 }
750         }
751 #endif
752
753         /* put methodinfo pointer on Stackframe */
754         p = dseg_add_address(cd, m);
755         M_ALD(REG_ITMP1, REG_PV, p);
756 #if defined(__DARWIN__)
757         M_AST(REG_ITMP1, REG_SP, LA_SIZE + TRACE_ARGS_NUM * 8); 
758 #else
759         M_AST(REG_ITMP1, REG_SP, LA_SIZE + 4 * 8);
760 #endif
761         p = dseg_add_functionptr(cd, builtin_trace_args);
762         M_ALD(REG_ITMP2, REG_PV, p);
763         M_MTCTR(REG_ITMP2);
764         M_JSR;
765
766 #if defined(__DARWIN__)
767         /* restore integer argument registers from the reserved stack space */
768
769         stack_off = LA_SIZE;
770         for (p = 0; p < md->paramcount && p < TRACE_ARGS_NUM; 
771                  p++, stack_off += 8) {
772                 t = md->paramtypes[p].type;
773
774                 if (IS_INT_LNG_TYPE(t)) {
775                         if (!md->params[p].inmemory) {
776                                 if (IS_2_WORD_TYPE(t)) {
777                                         M_ILD(rd->argintregs[GET_HIGH_REG(md->params[p].regoff)]
778                                                   , REG_SP, stack_off);
779                                         M_ILD(rd->argintregs[GET_LOW_REG(md->params[p].regoff)]
780                                                   , REG_SP, stack_off + 4);
781                                 } else {
782                                         M_ILD(rd->argintregs[md->params[p].regoff]
783                                                   , REG_SP, stack_off + 4);
784                                 }
785                         }
786                 }
787         }
788 #else
789         /* LINUX */
790         for (p = 0; p < 8; p++) {
791                 d = rd->argintregs[p];
792                 /* save integer argument registers */
793                 M_ILD(d, REG_SP, LA_SIZE + 4 * 8 + 4 + p * 4);
794         }
795 #endif
796
797         M_ALD(REG_ZERO, REG_SP, stack_size + LA_LR_OFFSET);
798         M_MTLR(REG_ZERO);
799         M_LDA(REG_SP, REG_SP, stack_size);
800
801         /* mark trace code */
802
803         M_NOP;
804 }
805 #endif /* !defined(NDEBUG) */
806
807
808 /* emit_verbosecall_exit *******************************************************
809
810    Generates the code for the call trace.
811
812 *******************************************************************************/
813
814 #if !defined(NDEBUG)
815 void emit_verbosecall_exit(jitdata *jd)
816 {
817         methodinfo   *m;
818         codegendata  *cd;
819         registerdata *rd;
820         methoddesc   *md;
821         s4            disp;
822
823         /* get required compiler data */
824
825         m  = jd->m;
826         cd = jd->cd;
827         rd = jd->rd;
828
829         md = m->parseddesc;
830         
831         /* mark trace code */
832
833         M_NOP;
834
835         M_MFLR(REG_ZERO);
836         M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
837         M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4));
838
839         /* save return registers */
840
841         M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
842         M_DST(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
843
844         /* keep this order */
845         switch (md->returntype.type) {
846         case TYPE_INT:
847         case TYPE_ADR:
848 #if defined(__DARWIN__)
849                 M_MOV(REG_RESULT, rd->argintregs[2]);
850                 M_CLR(rd->argintregs[1]);
851 #else
852                 M_MOV(REG_RESULT, rd->argintregs[3]);
853                 M_CLR(rd->argintregs[2]);
854 #endif
855                 break;
856
857         case TYPE_LNG:
858 #if defined(__DARWIN__)
859                 M_MOV(REG_RESULT2, rd->argintregs[2]);
860                 M_MOV(REG_RESULT, rd->argintregs[1]);
861 #else
862                 M_MOV(REG_RESULT2, rd->argintregs[3]);
863                 M_MOV(REG_RESULT, rd->argintregs[2]);
864 #endif
865                 break;
866         }
867
868         M_FLTMOVE(REG_FRESULT, rd->argfltregs[0]);
869         M_FLTMOVE(REG_FRESULT, rd->argfltregs[1]);
870
871         disp = dseg_add_address(cd, m);
872         M_ALD(rd->argintregs[0], REG_PV, disp);
873
874         disp = dseg_add_functionptr(cd, builtin_displaymethodstop);
875         M_ALD(REG_ITMP2, REG_PV, disp);
876         M_MTCTR(REG_ITMP2);
877         M_JSR;
878
879         /* restore return registers */
880
881         M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
882         M_DLD(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
883
884         M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4 + LA_LR_OFFSET);
885         M_MTLR(REG_ZERO);
886         M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4);
887
888         /* mark trace code */
889
890         M_NOP;
891 }
892 #endif /* !defined(NDEBUG) */
893
894
895 /*
896  * These are local overrides for various environment variables in Emacs.
897  * Please do not remove this and leave it at the end of the file, where
898  * Emacs will automagically detect them.
899  * ---------------------------------------------------------------------
900  * Local variables:
901  * mode: c
902  * indent-tabs-mode: t
903  * c-basic-offset: 4
904  * tab-width: 4
905  * End:
906  * vim:noexpandtab:sw=4:ts=4:
907  */