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