* src/vm/jit/powerpc/emit.c: Updated to new instruction format.
[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.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, stackptr 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 (src->flags & INMEMORY) {
71                 COUNT_SPILLS;
72
73                 disp = src->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->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, stackptr 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 (src->flags & INMEMORY) {
116                 COUNT_SPILLS;
117
118                 disp = src->regoff * 4;
119
120                 M_ILD(tempreg, REG_SP, disp + 4);
121
122                 reg = tempreg;
123         }
124         else
125                 reg = GET_LOW_REG(src->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, stackptr 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 (src->flags & INMEMORY) {
150                 COUNT_SPILLS;
151
152                 disp = src->regoff * 4;
153
154                 M_ILD(tempreg, REG_SP, disp);
155
156                 reg = tempreg;
157         }
158         else
159                 reg = GET_HIGH_REG(src->regoff);
160
161         return reg;
162 }
163
164
165 /* emit_load_s1 ****************************************************************
166
167    Emits a possible load of the first source operand.
168
169 *******************************************************************************/
170
171 s4 emit_load_s1(jitdata *jd, instruction *iptr, s4 tempreg)
172 {
173         stackptr src;
174         s4       reg;
175
176         src = iptr->s1.var;
177
178         reg = emit_load(jd, iptr, src, tempreg);
179
180         return reg;
181 }
182
183
184 /* emit_load_s2 ****************************************************************
185
186    Emits a possible load of the second source operand.
187
188 *******************************************************************************/
189
190 s4 emit_load_s2(jitdata *jd, instruction *iptr, s4 tempreg)
191 {
192         stackptr src;
193         s4       reg;
194
195         src = iptr->sx.s23.s2.var;
196
197         reg = emit_load(jd, iptr, src, tempreg);
198
199         return reg;
200 }
201
202
203 /* emit_load_s3 ****************************************************************
204
205    Emits a possible load of the third source operand.
206
207 *******************************************************************************/
208
209 s4 emit_load_s3(jitdata *jd, instruction *iptr, s4 tempreg)
210 {
211         stackptr src;
212         s4       reg;
213
214         src = iptr->sx.s23.s3.var;
215
216         reg = emit_load(jd, iptr, src, tempreg);
217
218         return reg;
219 }
220
221
222 /* emit_load_s1_low ************************************************************
223
224    Emits a possible load of the low 32-bits of the first long source
225    operand.
226
227 *******************************************************************************/
228
229 s4 emit_load_s1_low(jitdata *jd, instruction *iptr, s4 tempreg)
230 {
231         stackptr src;
232         s4       reg;
233
234         src = iptr->s1.var;
235
236         reg = emit_load_low(jd, iptr, src, tempreg);
237
238         return reg;
239 }
240
241
242 /* emit_load_s2_low ************************************************************
243
244    Emits a possible load of the low 32-bits of the second long source
245    operand.
246
247 *******************************************************************************/
248
249 s4 emit_load_s2_low(jitdata *jd, instruction *iptr, s4 tempreg)
250 {
251         stackptr src;
252         s4       reg;
253
254         src = iptr->sx.s23.s2.var;
255
256         reg = emit_load_low(jd, iptr, src, tempreg);
257
258         return reg;
259 }
260
261
262 /* emit_load_s3_low ************************************************************
263
264    Emits a possible load of the low 32-bits of the third long source
265    operand.
266
267 *******************************************************************************/
268
269 s4 emit_load_s3_low(jitdata *jd, instruction *iptr, s4 tempreg)
270 {
271         stackptr src;
272         s4       reg;
273
274         src = iptr->sx.s23.s3.var;
275
276         reg = emit_load_low(jd, iptr, src, tempreg);
277
278         return reg;
279 }
280
281
282 /* emit_load_s1_high ***********************************************************
283
284    Emits a possible load of the high 32-bits of the first long source
285    operand.
286
287 *******************************************************************************/
288
289 s4 emit_load_s1_high(jitdata *jd, instruction *iptr, s4 tempreg)
290 {
291         stackptr src;
292         s4       reg;
293
294         src = iptr->s1.var;
295
296         reg = emit_load_high(jd, iptr, src, tempreg);
297
298         return reg;
299 }
300
301
302 /* emit_load_s2_high ***********************************************************
303
304    Emits a possible load of the high 32-bits of the second long source
305    operand.
306
307 *******************************************************************************/
308
309 s4 emit_load_s2_high(jitdata *jd, instruction *iptr, s4 tempreg)
310 {
311         stackptr src;
312         s4       reg;
313
314         src = iptr->sx.s23.s2.var;
315
316         reg = emit_load_high(jd, iptr, src, tempreg);
317
318         return reg;
319 }
320
321
322 /* emit_load_s3_high ***********************************************************
323
324    Emits a possible load of the high 32-bits of the third long source
325    operand.
326
327 *******************************************************************************/
328
329 s4 emit_load_s3_high(jitdata *jd, instruction *iptr, s4 tempreg)
330 {
331         stackptr src;
332         s4       reg;
333
334         src = iptr->sx.s23.s3.var;
335
336         reg = emit_load_high(jd, iptr, src, tempreg);
337
338         return reg;
339 }
340
341
342 /* emit_store ******************************************************************
343
344    XXX
345
346 *******************************************************************************/
347
348 void emit_store(jitdata *jd, instruction *iptr, stackptr dst, s4 d)
349 {
350         codegendata *cd;
351
352         /* get required compiler data */
353
354         cd = jd->cd;
355
356         if (dst->flags & INMEMORY) {
357                 COUNT_SPILLS;
358
359                 if (IS_FLT_DBL_TYPE(dst->type)) {
360                         if (IS_2_WORD_TYPE(dst->type))
361                                 M_DST(d, REG_SP, dst->regoff * 4);
362                         else
363                                 M_FST(d, REG_SP, dst->regoff * 4);
364                 }
365                 else {
366                         if (IS_2_WORD_TYPE(dst->type))
367                                 M_LST(d, REG_SP, dst->regoff * 4);
368                         else
369                                 M_IST(d, REG_SP, dst->regoff * 4);
370                 }
371         }
372 }
373
374
375 /* emit_store_dst **************************************************************
376
377    This function generates the code to store the result of an
378    operation back into a spilled pseudo-variable.  If the
379    pseudo-variable has not been spilled in the first place, this
380    function will generate nothing.
381     
382 *******************************************************************************/
383
384 void emit_store_dst(jitdata *jd, instruction *iptr, s4 d)
385 {
386         emit_store(jd, iptr, iptr->dst.var, d);
387 }
388
389
390 /* emit_copy *******************************************************************
391
392    XXX
393
394 *******************************************************************************/
395
396 void emit_copy(jitdata *jd, instruction *iptr, stackptr src, stackptr dst)
397 {
398         codegendata  *cd;
399         registerdata *rd;
400         s4            s1, d;
401
402         /* get required compiler data */
403
404         cd = jd->cd;
405         rd = jd->rd;
406
407         if (src->type == TYPE_LNG)
408                 d = codegen_reg_of_var(rd, iptr->opc, dst, REG_ITMP12_PACKED);
409         else
410                 d = codegen_reg_of_var(rd, iptr->opc, dst, REG_IFTMP);
411
412         if ((src->regoff != dst->regoff) ||
413                 ((src->flags ^ dst->flags) & INMEMORY)) {
414                 s1 = emit_load(jd, iptr, src, d);
415
416                 if (s1 != d) {
417                         if (IS_FLT_DBL_TYPE(src->type))
418                                 M_FMOV(s1, d);
419                         else {
420                                 if (IS_2_WORD_TYPE(src->type)) {
421                                         M_MOV(GET_LOW_REG(s1), GET_LOW_REG(d));
422                                         M_MOV(GET_HIGH_REG(s1), GET_HIGH_REG(d));
423                 }
424                                 else
425                     M_MOV(s1, d);
426                         }
427                 }
428
429                 emit_store(jd, iptr, dst, d);
430         }
431 }
432
433
434 /* emit_iconst *****************************************************************
435
436    XXX
437
438 *******************************************************************************/
439
440 void emit_iconst(codegendata *cd, s4 d, s4 value)
441 {
442         s4 disp;
443
444         if ((value >= -32768) && (value <= 32767))
445                 M_LDA_INTERN(d, REG_ZERO, value);
446         else {
447                 disp = dseg_add_s4(cd, value);
448                 M_ILD(d, REG_PV, disp);
449         }
450 }
451
452
453 /* emit_exception_stubs ********************************************************
454
455    Generates the code for the exception stubs.
456
457 *******************************************************************************/
458
459 void emit_exception_stubs(jitdata *jd)
460 {
461         codegendata  *cd;
462         registerdata *rd;
463         exceptionref *eref;
464         s4            targetdisp;
465         s4            disp;
466
467         /* get required compiler data */
468
469         cd = jd->cd;
470         rd = jd->rd;
471
472         /* generate exception stubs */
473
474         targetdisp = 0;
475
476         for (eref = cd->exceptionrefs; eref != NULL; eref = eref->next) {
477                 gen_resolvebranch(cd->mcodebase + eref->branchpos, 
478                                                   eref->branchpos, cd->mcodeptr - cd->mcodebase);
479
480                 MCODECHECK(100);
481
482                 /* Move the value register to a temporary register, if
483                    there is the need for it. */
484
485                 if (eref->reg != -1)
486                         M_MOV(eref->reg, REG_ITMP1);
487
488                 /* calcuate exception address */
489
490                 M_LDA(REG_ITMP2_XPC, REG_PV, eref->branchpos - 4);
491
492                 /* move function to call into REG_ITMP3 */
493
494                 disp = dseg_add_functionptr(cd, eref->function);
495                 M_ALD(REG_ITMP3, REG_PV, disp);
496
497                 if (targetdisp == 0) {
498                     targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
499
500                         if (jd->isleafmethod) {
501                                 M_MFLR(REG_ZERO);
502                                 M_AST(REG_ZERO, REG_SP, cd->stackframesize * 4 + LA_LR_OFFSET);
503                         }
504
505                         M_MOV(REG_PV, rd->argintregs[0]);
506                         M_MOV(REG_SP, rd->argintregs[1]);
507
508                         if (jd->isleafmethod)
509                                 M_MOV(REG_ZERO, rd->argintregs[2]);
510                         else
511                                 M_ALD(rd->argintregs[2],
512                                           REG_SP, cd->stackframesize * 4 + LA_LR_OFFSET);
513
514                         M_MOV(REG_ITMP2_XPC, rd->argintregs[3]);
515                         M_MOV(REG_ITMP1, rd->argintregs[4]);
516
517                         M_STWU(REG_SP, REG_SP, -(LA_SIZE + 6 * 4));
518                         M_AST(REG_ITMP2_XPC, REG_SP, LA_SIZE + 5 * 4);
519
520                         M_MTCTR(REG_ITMP3);
521                         M_JSR;
522                         M_MOV(REG_RESULT, REG_ITMP1_XPTR);
523
524                         M_ALD(REG_ITMP2_XPC, REG_SP, LA_SIZE + 5 * 4);
525                         M_IADD_IMM(REG_SP, LA_SIZE + 6 * 4, REG_SP);
526
527                         if (jd->isleafmethod) {
528                                 /* XXX FIXME: REG_ZERO can cause problems here! */
529                                 assert(cd->stackframesize * 4 <= 32767);
530
531                                 M_ALD(REG_ZERO, REG_SP, cd->stackframesize * 4 + LA_LR_OFFSET);
532                                 M_MTLR(REG_ZERO);
533                         }
534
535                         disp = dseg_add_functionptr(cd, asm_handle_exception);
536                         M_ALD(REG_ITMP3, REG_PV, disp);
537                         M_MTCTR(REG_ITMP3);
538                         M_RTS;
539                 }
540                 else {
541                         disp = (((u4 *) cd->mcodebase) + targetdisp) -
542                                 (((u4 *) cd->mcodeptr) + 1);
543                         M_BR(disp);
544                 }
545         }
546 }
547
548
549 /* emit_patcher_stubs **********************************************************
550
551    Generates the code for the patcher stubs.
552
553 *******************************************************************************/
554
555 void emit_patcher_stubs(jitdata *jd)
556 {
557         codegendata *cd;
558         patchref    *pref;
559         u4           mcode;
560         u1          *savedmcodeptr;
561         u1          *tmpmcodeptr;
562         s4           targetdisp;
563         s4           disp;
564
565         /* get required compiler data */
566
567         cd = jd->cd;
568
569         /* generate code patching stub call code */
570
571         targetdisp = 0;
572
573         for (pref = cd->patchrefs; pref != NULL; pref = pref->next) {
574                 /* check code segment size */
575
576                 MCODECHECK(100);
577
578                 /* Get machine code which is patched back in later. The
579                    call is 1 instruction word long. */
580
581                 tmpmcodeptr = (u1 *) (cd->mcodebase + pref->branchpos);
582
583                 mcode = *((u4 *) tmpmcodeptr);
584
585                 /* Patch in the call to call the following code (done at
586                    compile time). */
587
588                 savedmcodeptr = cd->mcodeptr;   /* save current mcodeptr          */
589                 cd->mcodeptr  = tmpmcodeptr;    /* set mcodeptr to patch position */
590
591                 disp = ((u4 *) savedmcodeptr) - (((u4 *) tmpmcodeptr) + 1);
592                 M_BR(disp);
593
594                 cd->mcodeptr = savedmcodeptr;   /* restore the current mcodeptr   */
595
596                 /* create stack frame - keep stack 16-byte aligned */
597
598                 M_AADD_IMM(REG_SP, -8 * 4, REG_SP);
599
600                 /* calculate return address and move it onto the stack */
601
602                 M_LDA(REG_ITMP3, REG_PV, pref->branchpos);
603                 M_AST_INTERN(REG_ITMP3, REG_SP, 5 * 4);
604
605                 /* move pointer to java_objectheader onto stack */
606
607 #if defined(ENABLE_THREADS)
608                 /* order reversed because of data segment layout */
609
610                 (void) dseg_add_unique_address(cd, NULL);                  /* flcword */
611                 (void) dseg_add_unique_address(cd, lock_get_initial_lock_word());
612                 disp = dseg_add_unique_address(cd, NULL);                  /* vftbl   */
613
614                 M_LDA(REG_ITMP3, REG_PV, disp);
615                 M_AST_INTERN(REG_ITMP3, REG_SP, 4 * 4);
616 #else
617                 /* do nothing */
618 #endif
619
620                 /* move machine code onto stack */
621
622                 disp = dseg_add_s4(cd, mcode);
623                 M_ILD(REG_ITMP3, REG_PV, disp);
624                 M_IST_INTERN(REG_ITMP3, REG_SP, 3 * 4);
625
626                 /* move class/method/field reference onto stack */
627
628                 disp = dseg_add_address(cd, pref->ref);
629                 M_ALD(REG_ITMP3, REG_PV, disp);
630                 M_AST_INTERN(REG_ITMP3, REG_SP, 2 * 4);
631
632                 /* move data segment displacement onto stack */
633
634                 disp = dseg_add_s4(cd, pref->disp);
635                 M_ILD(REG_ITMP3, REG_PV, disp);
636                 M_IST_INTERN(REG_ITMP3, REG_SP, 1 * 4);
637
638                 /* move patcher function pointer onto stack */
639
640                 disp = dseg_add_functionptr(cd, pref->patcher);
641                 M_ALD(REG_ITMP3, REG_PV, disp);
642                 M_AST_INTERN(REG_ITMP3, REG_SP, 0 * 4);
643
644                 if (targetdisp == 0) {
645                         targetdisp = ((u4 *) cd->mcodeptr) - ((u4 *) cd->mcodebase);
646
647                         disp = dseg_add_functionptr(cd, asm_patcher_wrapper);
648                         M_ALD(REG_ITMP3, REG_PV, disp);
649                         M_MTCTR(REG_ITMP3);
650                         M_RTS;
651                 }
652                 else {
653                         disp = (((u4 *) cd->mcodebase) + targetdisp) -
654                                 (((u4 *) cd->mcodeptr) + 1);
655                         M_BR(disp);
656                 }
657         }
658 }
659
660
661 /* emit_replacement_stubs ******************************************************
662
663    Generates the code for the replacement stubs.
664
665 *******************************************************************************/
666
667 void emit_replacement_stubs(jitdata *jd)
668 {
669         codegendata *cd;
670         codeinfo    *code;
671         rplpoint    *rplp;
672         u1          *savedmcodeptr;
673         s4           disp;
674         s4           i;
675
676         /* get required compiler data */
677
678         cd   = jd->cd;
679         code = jd->code;
680
681         rplp = code->rplpoints;
682
683         for (i = 0; i < code->rplpointcount; ++i, ++rplp) {
684                 /* check code segment size */
685
686                 MCODECHECK(100);
687
688                 /* note start of stub code */
689
690                 rplp->outcode = (u1 *) (cd->mcodeptr - cd->mcodebase);
691
692                 /* make machine code for patching */
693
694                 savedmcodeptr = cd->mcodeptr;
695                 cd->mcodeptr  = (u1 *) &(rplp->mcode) + 1;              /* big-endian */
696
697                 disp = (ptrint) ((s4 *) rplp->outcode - (s4 *) rplp->pc) - 1;
698                 M_BR(disp);
699
700                 cd->mcodeptr = savedmcodeptr;
701
702                 /* create stack frame - keep 16-byte aligned */
703
704                 M_AADD_IMM(REG_SP, -4 * 4, REG_SP);
705
706                 /* push address of `rplpoint` struct */
707
708                 disp = dseg_add_address(cd, rplp);
709                 M_ALD(REG_ITMP3, REG_PV, disp);
710                 M_AST_INTERN(REG_ITMP3, REG_SP, 0 * 4);
711
712                 /* jump to replacement function */
713
714                 disp = dseg_add_functionptr(cd, asm_replacement_out);
715                 M_ALD(REG_ITMP3, REG_PV, disp);
716                 M_MTCTR(REG_ITMP3);
717                 M_RTS;
718         }
719 }
720
721
722 /* emit_verbosecall_enter ******************************************************
723
724    Generates the code for the call trace.
725
726 *******************************************************************************/
727
728 void emit_verbosecall_enter(jitdata *jd)
729 {
730         methodinfo   *m;
731         codegendata  *cd;
732         registerdata *rd;
733         s4 s1, p, t, d;
734         int stack_off;
735         int stack_size;
736         methoddesc *md;
737
738         /* get required compiler data */
739
740         m  = jd->m;
741         cd = jd->cd;
742         rd = jd->rd;
743
744         md = m->parseddesc;
745         
746         /* Build up Stackframe for builtin_trace_args call (a multiple of 16) */
747         /* For Darwin:                                                        */
748         /* LA + TRACE_ARGS_NUM u8 args + methodinfo + LR                      */
749         /* LA_SIZE(=6*4) + 8*8         + 4          + 4  + 0(Padding)         */
750         /* 6 * 4 + 8 * 8 + 2 * 4 = 12 * 8 = 6 * 16                            */
751         /* For Linux:                                                         */
752         /* LA + (TRACE_ARGS_NUM - INT_ARG_CNT/2) u8 args + methodinfo         */
753         /* + INT_ARG_CNT * 4 ( save integer registers) + LR + 8 + 8 (Padding) */
754         /* LA_SIZE(=2*4) + 4 * 8 + 4 + 8 * 4 + 4 + 8                          */
755         /* 2 * 4 + 4 * 8 + 10 * 4 + 1 * 8 + 8= 12 * 8 = 6 * 16                */
756         
757         /* in nativestubs no Place to save the LR (Link Register) would be needed */
758         /* but since the stack frame has to be aligned the 4 Bytes would have to  */
759         /* be padded again */
760
761 #if defined(__DARWIN__)
762         stack_size = LA_SIZE + (TRACE_ARGS_NUM + 1) * 8;
763 #else
764         stack_size = 6 * 16;
765 #endif
766
767         /* mark trace code */
768
769         M_NOP;
770
771         M_MFLR(REG_ZERO);
772         M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
773         M_STWU(REG_SP, REG_SP, -stack_size);
774
775         M_CLR(REG_ITMP1);    /* clear help register */
776
777         /* save up to TRACE_ARGS_NUM arguments into the reserved stack space */
778 #if defined(__DARWIN__)
779         /* Copy Params starting from first to Stack                          */
780         /* since TRACE_ARGS == INT_ARG_CNT all used integer argument regs    */ 
781         /* are saved                                                         */
782         p = 0;
783 #else
784         /* Copy Params starting from fifth to Stack (INT_ARG_CNT/2) are in   */
785         /* integer argument regs                                             */
786         /* all integer argument registers have to be saved                   */
787         for (p = 0; p < 8; p++) {
788                 d = rd->argintregs[p];
789                 /* save integer argument registers */
790                 M_IST(d, REG_SP, LA_SIZE + 4 * 8 + 4 + p * 4);
791         }
792         p = 4;
793 #endif
794         stack_off = LA_SIZE;
795         for (; p < md->paramcount && p < TRACE_ARGS_NUM; p++, stack_off += 8) {
796                 t = md->paramtypes[p].type;
797                 if (IS_INT_LNG_TYPE(t)) {
798                         if (!md->params[p].inmemory) { /* Param in Arg Reg */
799                                 if (IS_2_WORD_TYPE(t)) {
800                                         M_IST(rd->argintregs[GET_HIGH_REG(md->params[p].regoff)]
801                                                   , REG_SP, stack_off);
802                                         M_IST(rd->argintregs[GET_LOW_REG(md->params[p].regoff)]
803                                                   , REG_SP, stack_off + 4);
804                                 } else {
805                                         M_IST(REG_ITMP1, REG_SP, stack_off);
806                                         M_IST(rd->argintregs[md->params[p].regoff]
807                                                   , REG_SP, stack_off + 4);
808                                 }
809                         } else { /* Param on Stack */
810                                 s1 = (md->params[p].regoff + cd->stackframesize) * 4 
811                                         + stack_size;
812                                 if (IS_2_WORD_TYPE(t)) {
813                                         M_ILD(REG_ITMP2, REG_SP, s1);
814                                         M_IST(REG_ITMP2, REG_SP, stack_off);
815                                         M_ILD(REG_ITMP2, REG_SP, s1 + 4);
816                                         M_IST(REG_ITMP2, REG_SP, stack_off + 4);
817                                 } else {
818                                         M_IST(REG_ITMP1, REG_SP, stack_off);
819                                         M_ILD(REG_ITMP2, REG_SP, s1);
820                                         M_IST(REG_ITMP2, REG_SP, stack_off + 4);
821                                 }
822                         }
823                 } else { /* IS_FLT_DBL_TYPE(t) */
824                         if (!md->params[p].inmemory) { /* in Arg Reg */
825                                 s1 = rd->argfltregs[md->params[p].regoff];
826                                 if (!IS_2_WORD_TYPE(t)) {
827                                         M_IST(REG_ITMP1, REG_SP, stack_off);
828                                         M_FST(s1, REG_SP, stack_off + 4);
829                                 } else {
830                                         M_DST(s1, REG_SP, stack_off);
831                                 }
832                         } else { /* on Stack */
833                                 /* this should not happen */
834                         }
835                 }
836         }
837
838         /* load first 4 (==INT_ARG_CNT/2) arguments into integer registers */
839 #if defined(__DARWIN__)
840         for (p = 0; p < 8; p++) {
841                 d = rd->argintregs[p];
842                 M_ILD(d, REG_SP, LA_SIZE + p * 4);
843         }
844 #else
845         /* LINUX */
846         /* Set integer and float argument registers vor trace_args call */
847         /* offset to saved integer argument registers                   */
848         stack_off = LA_SIZE + 4 * 8 + 4;
849         for (p = 0; (p < 4) && (p < md->paramcount); p++) {
850                 t = md->paramtypes[p].type;
851                 if (IS_INT_LNG_TYPE(t)) {
852                         /* "stretch" int types */
853                         if (!IS_2_WORD_TYPE(t)) {
854                                 M_CLR(rd->argintregs[2 * p]);
855                                 M_ILD(rd->argintregs[2 * p + 1], REG_SP,stack_off);
856                                 stack_off += 4;
857                         } else {
858                                 M_ILD(rd->argintregs[2 * p + 1], REG_SP,stack_off + 4);
859                                 M_ILD(rd->argintregs[2 * p], REG_SP,stack_off);
860                                 stack_off += 8;
861                         }
862                 } else { /* Float/Dbl */
863                         if (!md->params[p].inmemory) { /* Param in Arg Reg */
864                                 /* use reserved Place on Stack (sp + 5 * 16) to copy  */
865                                 /* float/double arg reg to int reg                    */
866                                 s1 = rd->argfltregs[md->params[p].regoff];
867                                 if (!IS_2_WORD_TYPE(t)) {
868                                         M_FST(s1, REG_SP, 5 * 16);
869                                         M_ILD(rd->argintregs[2 * p + 1], REG_SP, 5 * 16);
870                                         M_CLR(rd->argintregs[2 * p]);
871                                 } else {
872                                         M_DST(s1, REG_SP, 5 * 16);
873                                         M_ILD(rd->argintregs[2 * p + 1], REG_SP,  5 * 16 + 4);
874                                         M_ILD(rd->argintregs[2 * p], REG_SP, 5 * 16);
875                                 }
876                         }
877                 }
878         }
879 #endif
880
881         /* put methodinfo pointer on Stackframe */
882         p = dseg_add_address(cd, m);
883         M_ALD(REG_ITMP1, REG_PV, p);
884 #if defined(__DARWIN__)
885         M_AST(REG_ITMP1, REG_SP, LA_SIZE + TRACE_ARGS_NUM * 8); 
886 #else
887         M_AST(REG_ITMP1, REG_SP, LA_SIZE + 4 * 8);
888 #endif
889         p = dseg_add_functionptr(cd, builtin_trace_args);
890         M_ALD(REG_ITMP2, REG_PV, p);
891         M_MTCTR(REG_ITMP2);
892         M_JSR;
893
894 #if defined(__DARWIN__)
895         /* restore integer argument registers from the reserved stack space */
896
897         stack_off = LA_SIZE;
898         for (p = 0; p < md->paramcount && p < TRACE_ARGS_NUM; 
899                  p++, stack_off += 8) {
900                 t = md->paramtypes[p].type;
901
902                 if (IS_INT_LNG_TYPE(t)) {
903                         if (!md->params[p].inmemory) {
904                                 if (IS_2_WORD_TYPE(t)) {
905                                         M_ILD(rd->argintregs[GET_HIGH_REG(md->params[p].regoff)]
906                                                   , REG_SP, stack_off);
907                                         M_ILD(rd->argintregs[GET_LOW_REG(md->params[p].regoff)]
908                                                   , REG_SP, stack_off + 4);
909                                 } else {
910                                         M_ILD(rd->argintregs[md->params[p].regoff]
911                                                   , REG_SP, stack_off + 4);
912                                 }
913                         }
914                 }
915         }
916 #else
917         /* LINUX */
918         for (p = 0; p < 8; p++) {
919                 d = rd->argintregs[p];
920                 /* save integer argument registers */
921                 M_ILD(d, REG_SP, LA_SIZE + 4 * 8 + 4 + p * 4);
922         }
923 #endif
924
925         M_ALD(REG_ZERO, REG_SP, stack_size + LA_LR_OFFSET);
926         M_MTLR(REG_ZERO);
927         M_LDA(REG_SP, REG_SP, stack_size);
928
929         /* mark trace code */
930
931         M_NOP;
932 }
933
934
935 /* emit_verbosecall_exit *******************************************************
936
937    Generates the code for the call trace.
938
939 *******************************************************************************/
940
941 void emit_verbosecall_exit(jitdata *jd)
942 {
943         methodinfo   *m;
944         codegendata  *cd;
945         registerdata *rd;
946         methoddesc   *md;
947         s4            disp;
948
949         /* get required compiler data */
950
951         m  = jd->m;
952         cd = jd->cd;
953         rd = jd->rd;
954
955         md = m->parseddesc;
956         
957         /* mark trace code */
958
959         M_NOP;
960
961         M_MFLR(REG_ZERO);
962         M_AST(REG_ZERO, REG_SP, LA_LR_OFFSET);
963         M_STWU(REG_SP, REG_SP, -(LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4));
964
965         /* save return registers */
966
967         M_LST(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
968         M_DST(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
969
970         /* keep this order */
971         switch (md->returntype.type) {
972         case TYPE_INT:
973         case TYPE_ADR:
974 #if defined(__DARWIN__)
975                 M_MOV(REG_RESULT, rd->argintregs[2]);
976                 M_CLR(rd->argintregs[1]);
977 #else
978                 M_MOV(REG_RESULT, rd->argintregs[3]);
979                 M_CLR(rd->argintregs[2]);
980 #endif
981                 break;
982
983         case TYPE_LNG:
984 #if defined(__DARWIN__)
985                 M_MOV(REG_RESULT2, rd->argintregs[2]);
986                 M_MOV(REG_RESULT, rd->argintregs[1]);
987 #else
988                 M_MOV(REG_RESULT2, rd->argintregs[3]);
989                 M_MOV(REG_RESULT, rd->argintregs[2]);
990 #endif
991                 break;
992         }
993
994         M_FLTMOVE(REG_FRESULT, rd->argfltregs[0]);
995         M_FLTMOVE(REG_FRESULT, rd->argfltregs[1]);
996
997         disp = dseg_add_address(cd, m);
998         M_ALD(rd->argintregs[0], REG_PV, disp);
999
1000         disp = dseg_add_functionptr(cd, builtin_displaymethodstop);
1001         M_ALD(REG_ITMP2, REG_PV, disp);
1002         M_MTCTR(REG_ITMP2);
1003         M_JSR;
1004
1005         /* restore return registers */
1006
1007         M_LLD(REG_RESULT_PACKED, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 0) * 4);
1008         M_DLD(REG_FRESULT, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 2) * 4);
1009
1010         M_ALD(REG_ZERO, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4 + LA_LR_OFFSET);
1011         M_MTLR(REG_ZERO);
1012         M_LDA(REG_SP, REG_SP, LA_SIZE + (1 + 2 + 2 + 1 + 4) * 4);
1013
1014         /* mark trace code */
1015
1016         M_NOP;
1017 }
1018
1019
1020 /*
1021  * These are local overrides for various environment variables in Emacs.
1022  * Please do not remove this and leave it at the end of the file, where
1023  * Emacs will automagically detect them.
1024  * ---------------------------------------------------------------------
1025  * Local variables:
1026  * mode: c
1027  * indent-tabs-mode: t
1028  * c-basic-offset: 4
1029  * tab-width: 4
1030  * End:
1031  * vim:noexpandtab:sw=4:ts=4:
1032  */