Debian Codegraph @

arch/arm/code.c

raw symbols SCIP
1 /* $Id: code.c,v 1.30 2016/03/09 18:19:56 ragge Exp $ */
2 /*
3 * Copyright (c) 2007 Gregory McGarry ([email protected]).
4 * Copyright (c) 2003 Anders Magnusson ([email protected]).
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * Stuff for pass1.
32 */
33
34 #include <assert.h>
35
36 #include "pass1.h"
37 #include "pass2.h"
38
39 #ifdef LANG_CXX
40 #define p1listf listf
41 #define p1tfree tfree
42 #else
43 #define NODE P1ND
44 #define talloc p1alloc
45 #define tcopy p1tcopy
46 #define nfree p1nfree
47 #endif
48
49 static int rvnr;
50
51 /*
52 * Print out assembler segment name.
53 */
54 void
55 setseg(int seg, char *name)
56 {
57 switch (seg) {
58 case PROG: name = ".text"; break;
59 case DATA:
60 case LDATA: name = ".data"; break;
61 case UDATA: break;
62 case PICLDATA: name = ".section .data.rel.local,\"aw\",@progbits";break;
63 case PICDATA: name = ".section .data.rel.rw,\"aw\",@progbits"; break;
64 case PICRDATA: name = ".section .data.rel.ro,\"aw\",@progbits"; break;
65 case STRNG:
66 case RDATA: name = ".section .rodata"; break;
67 case TLSDATA: name = ".section .tdata,\"awT\",@progbits"; break;
68 case TLSUDATA: name = ".section .tbss,\"awT\",@nobits"; break;
69 case CTORS: name = ".section\t.ctors,\"aw\",@progbits"; break;
70 case DTORS: name = ".section\t.dtors,\"aw\",@progbits"; break;
71 case NMSEG:
72 printf("\t.section %s,\"a%c\",@progbits\n", name,
73 cftnsp ? 'x' : 'w');
74 return;
75 }
76 printf("\t%s\n", name);
77 }
78
79 /*
80 * Define everything needed to print out some data (or text).
81 * This means segment, alignment, visibility, etc.
82 */
83 void
84 defloc(struct symtab *sp)
85 {
86 char *n;
87
88 n = getexname(sp);
89 #ifdef USE_GAS
90 if (ISFTN(t))
91 printf("\t.type %s,%%function\n", n);
92 #endif
93 if (sp->sclass == EXTDEF)
94 printf("\t.global %s\n", n);
95 if (sp->slevel == 0)
96 printf("%s:\n", n);
97 else
98 printf(LABFMT ":\n", sp->soffset);
99 }
100
101 /* Put a symbol in a temporary
102 * used by bfcode() and its helpers
103 */
104 static void
105 putintemp(struct symtab *sym)
106 {
107 NODE *p;
108
109 p = tempnode(0, sym->stype, sym->sdf, sym->sap);
110 p = buildtree(ASSIGN, p, nametree(sym));
111 sym->soffset = regno(p->n_left);
112 sym->sflags |= STNODE;
113 ecomp(p);
114 }
115
116 /* setup a 64-bit parameter (double/ldouble/longlong)
117 * used by bfcode() */
118 static void
119 param_64bit(struct symtab *sym, int *argofsp, int dotemps)
120 {
121 int argofs = *argofsp;
122 NODE *p, *q;
123 int navail;
124
125 #if ALLONGLONG == 64
126 /* alignment */
127 ++argofs;
128 argofs &= ~1;
129 *argofsp = argofs;
130 #endif
131
132 navail = NARGREGS - argofs;
133
134 if (navail < 2) {
135 /* half in and half out of the registers */
136 if (features(FEATURE_BIGENDIAN)) {
137 cerror("param_64bit");
138 p = q = NULL;
139 } else {
140 q = block(REG, NIL, NIL, INT, 0, 0);
141 regno(q) = R0 + argofs;
142 if (dotemps) {
143 q = block(SCONV, q, NIL,
144 ULONGLONG, 0, 0);
145 p = nametree(sym);
146 p->n_type = ULONGLONG;
147 p->n_df = 0;
148 p->n_ap = NULL;
149 p = block(LS, p, bcon(32), ULONGLONG, 0, 0);
150 q = block(PLUS, p, q, ULONGLONG, 0, 0);
151 p = tempnode(0, ULONGLONG, 0, 0);
152 sym->soffset = regno(p);
153 sym->sflags |= STNODE;
154 } else {
155 p = nametree(sym);
156 regno(p) = sym->soffset;
157 p->n_type = INT;
158 p->n_df = 0;
159 p->n_ap = NULL;
160 }
161 }
162 p = buildtree(ASSIGN, p, q);
163 ecomp(p);
164 *argofsp = argofs + 2;
165 return;
166 }
167
168 q = block(REG, NIL, NIL, sym->stype, sym->sdf, sym->sap);
169 regno(q) = R0R1 + argofs;
170 if (dotemps) {
171 p = tempnode(0, sym->stype, sym->sdf, sym->sap);
172 sym->soffset = regno(p);
173 sym->sflags |= STNODE;
174 } else {
175 p = nametree(sym);
176 }
177 p = buildtree(ASSIGN, p, q);
178 ecomp(p);
179 *argofsp = argofs + 2;
180 }
181
182 /* setup a 32-bit param on the stack
183 * used by bfcode() */
184 static void
185 param_32bit(struct symtab *sym, int *argofsp, int dotemps)
186 {
187 NODE *p, *q;
188
189 q = block(REG, NIL, NIL, sym->stype, sym->sdf, sym->sap);
190 regno(q) = R0 + (*argofsp)++;
191 if (dotemps) {
192 p = tempnode(0, sym->stype, sym->sdf, sym->sap);
193 sym->soffset = regno(p);
194 sym->sflags |= STNODE;
195 } else {
196 p = nametree(sym);
197 }
198 p = buildtree(ASSIGN, p, q);
199 ecomp(p);
200 }
201
202 /* setup a double param on the stack
203 * used by bfcode() */
204 static void
205 param_double(struct symtab *sym, int *argofsp, int dotemps)
206 {
207 NODE *p, *q, *t;
208 int tmpnr;
209
210 /*
211 * we have to dump the float from the general register
212 * into a temp, since the register allocator doesn't like
213 * floats to be in CLASSA. This may not work for -xtemps.
214 */
215
216 t = tempnode(0, ULONGLONG, 0, 0);
217 tmpnr = regno(t);
218 q = block(REG, NIL, NIL, INT, 0, 0);
219 q->n_rval = R0R1 + (*argofsp)++;
220 p = buildtree(ASSIGN, t, q);
221 ecomp(p);
222
223 if (dotemps) {
224 sym->soffset = tmpnr;
225 sym->sflags |= STNODE;
226 } else {
227 q = tempnode(tmpnr, sym->stype, sym->sdf, sym->sap);
228 p = nametree(sym);
229 p = buildtree(ASSIGN, p, q);
230 ecomp(p);
231 }
232 }
233
234 /* setup a float param on the stack
235 * used by bfcode() */
236 static void
237 param_float(struct symtab *sym, int *argofsp, int dotemps)
238 {
239 NODE *p, *q, *t;
240 int tmpnr;
241
242 /*
243 * we have to dump the float from the general register
244 * into a temp, since the register allocator doesn't like
245 * floats to be in CLASSA. This may not work for -xtemps.
246 */
247
248 t = tempnode(0, INT, 0, 0);
249 tmpnr = regno(t);
250 q = block(REG, NIL, NIL, INT, 0, 0);
251 q->n_rval = R0 + (*argofsp)++;
252 p = buildtree(ASSIGN, t, q);
253 ecomp(p);
254
255 if (dotemps) {
256 sym->soffset = tmpnr;
257 sym->sflags |= STNODE;
258 } else {
259 q = tempnode(tmpnr, sym->stype, sym->sdf, sym->sap);
260 p = nametree(sym);
261 p = buildtree(ASSIGN, p, q);
262 ecomp(p);
263 }
264 }
265
266 /* setup the hidden pointer to struct return parameter
267 * used by bfcode() */
268 static void
269 param_retstruct(void)
270 {
271 NODE *p, *q;
272
273 p = tempnode(0, PTR-FTN+cftnsp->stype, 0, cftnsp->sap);
274 rvnr = regno(p);
275 q = block(REG, NIL, NIL, PTR+STRTY, 0, cftnsp->sap);
276 regno(q) = R0;
277 p = buildtree(ASSIGN, p, q);
278 ecomp(p);
279 }
280
281
282 /* setup struct parameter
283 * push the registers out to memory
284 * used by bfcode() */
285 static void
286 param_struct(struct symtab *sym, int *argofsp)
287 {
288 int argofs = *argofsp;
289 NODE *p, *q;
290 int navail;
291 int sz;
292 int off;
293 int num;
294 int i;
295
296 navail = NARGREGS - argofs;
297 sz = tsize(sym->stype, sym->sdf, sym->sap) / SZINT;
298 off = ARGINIT/SZINT + argofs;
299 num = sz > navail ? navail : sz;
300 for (i = 0; i < num; i++) {
301 q = block(REG, NIL, NIL, INT, 0, 0);
302 regno(q) = R0 + argofs++;
303 p = block(REG, NIL, NIL, INT, 0, 0);
304 regno(p) = SP;
305 p = block(PLUS, p, bcon(4*off++), INT, 0, 0);
306 p = block(UMUL, p, NIL, INT, 0, 0);
307 p = buildtree(ASSIGN, p, q);
308 ecomp(p);
309 }
310
311 *argofsp = argofs;
312 }
313
314
315 /*
316 * Beginning-of-function code:
317 *
318 * 'sp' is an array of indices in symtab for the arguments
319 * 'cnt' is the number of arguments
320 */
321 void
322 bfcode(struct symtab **sp, int cnt)
323 {
324 union arglist *usym;
325 int saveallargs = 0;
326 int i, argofs = 0;
327
328 /*
329 * Detect if this function has ellipses and save all
330 * argument registers onto stack.
331 */
332 usym = cftnsp->sdf->dfun;
333 while (usym && usym->type != TNULL) {
334 if (usym->type == TELLIPSIS) {
335 saveallargs = 1;
336 break;
337 }
338 ++usym;
339 }
340
341 /* if returning a structure, move the hidden argument into a TEMP */
342 if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) {
343 param_retstruct();
344 ++argofs;
345 }
346
347 /* recalculate the arg offset and create TEMP moves */
348 for (i = 0; i < cnt; i++) {
349
350 if (sp[i] == NULL)
351 continue;
352
353 if ((argofs >= NARGREGS) && !xtemps)
354 break;
355
356 if (argofs > NARGREGS) {
357 putintemp(sp[i]);
358 } else if (sp[i]->stype == STRTY || sp[i]->stype == UNIONTY) {
359 param_struct(sp[i], &argofs);
360 } else if (DEUNSIGN(sp[i]->stype) == LONGLONG) {
361 param_64bit(sp[i], &argofs, xtemps && !saveallargs);
362 } else if (sp[i]->stype == DOUBLE || sp[i]->stype == LDOUBLE) {
363 if (features(FEATURE_HARDFLOAT))
364 param_double(sp[i], &argofs,
365 xtemps && !saveallargs);
366 else
367 param_64bit(sp[i], &argofs,
368 xtemps && !saveallargs);
369 } else if (sp[i]->stype == FLOAT) {
370 if (features(FEATURE_HARDFLOAT))
371 param_float(sp[i], &argofs,
372 xtemps && !saveallargs);
373 else
374 param_32bit(sp[i], &argofs,
375 xtemps && !saveallargs);
376 } else {
377 param_32bit(sp[i], &argofs, xtemps && !saveallargs);
378 }
379 }
380
381 /* if saveallargs, save the rest of the args onto the stack */
382 while (saveallargs && argofs < NARGREGS) {
383 NODE *p, *q;
384 int off = ARGINIT/SZINT + argofs;
385 q = block(REG, NIL, NIL, INT, 0, 0);
386 regno(q) = R0 + argofs++;
387 p = block(REG, NIL, NIL, INT, 0, 0);
388 regno(p) = FPREG;
389 p = block(PLUS, p, bcon(4*off), INT, 0, 0);
390 p = block(UMUL, p, NIL, INT, 0, 0);
391 p = buildtree(ASSIGN, p, q);
392 ecomp(p);
393 }
394
395 }
396
397 /*
398 * End-of-Function code:
399 */
400 void
401 efcode(void)
402 {
403 NODE *p, *q;
404 int tempnr;
405
406 if (cftnsp->stype != STRTY+FTN && cftnsp->stype != UNIONTY+FTN)
407 return;
408
409 /*
410 * At this point, the address of the return structure on
411 * has been FORCEd to RETREG, which is R0.
412 * We want to copy the contents from there to the address
413 * we placed into the tempnode "rvnr".
414 */
415
416 /* move the pointer out of R0 to a tempnode */
417 q = block(REG, NIL, NIL, PTR+STRTY, 0, cftnsp->sap);
418 q->n_rval = R0;
419 p = tempnode(0, PTR+STRTY, 0, cftnsp->sap);
420 tempnr = regno(p);
421 p = buildtree(ASSIGN, p, q);
422 ecomp(p);
423
424 /* get the address from the tempnode */
425 q = tempnode(tempnr, PTR+STRTY, 0, cftnsp->sap);
426 q = buildtree(UMUL, q, NIL);
427
428 /* now, get the structure destination */
429 p = tempnode(rvnr, PTR+STRTY, 0, cftnsp->sap);
430 p = buildtree(UMUL, p, NIL);
431
432 /* struct assignment */
433 p = buildtree(ASSIGN, p, q);
434 ecomp(p);
435 }
436
437 /*
438 * End-of-job: called just before final exit.
439 */
440 void
441 ejobcode(int flag)
442 {
443 printf("\t.ident \"PCC: %s\"\n", VERSSTR);
444 }
445
446 /*
447 * Beginning-of-job: called before compilation starts
448 *
449 * Initialise data structures specific for the local machine.
450 */
451 void
452 bjobcode(void)
453 {
454 }
455
456 /*
457 * fix up type of field p
458 */
459 void
460 fldty(struct symtab *p)
461 {
462 }
463
464 /*
465 * Build target-dependent switch tree/table.
466 *
467 * Return 1 if successfull, otherwise return 0 and the
468 * target-independent tree will be used.
469 */
470 int
471 mygenswitch(int num, TWORD type, struct swents **p, int n)
472 {
473 return 0;
474 }
475
476
477 /*
478 * Straighten a chain of CM ops so that the CM nodes
479 * only appear on the left node.
480 *
481 * CM CM
482 * CM CM CM b
483 * x y a b CM a
484 * x y
485 */
486 static NODE *
487 straighten(NODE *p)
488 {
489 NODE *r = p->n_right;
490
491 if (p->n_op != CM || r->n_op != CM)
492 return p;
493
494 p->n_right = r->n_left;
495 r->n_left = p;
496
497 return r;
498 }
499
500 static NODE *
501 reverse1(NODE *p, NODE *a)
502 {
503 NODE *l = p->n_left;
504 NODE *r = p->n_right;
505
506 a->n_right = r;
507 p->n_left = a;
508
509 if (l->n_op == CM) {
510 return reverse1(l, p);
511 } else {
512 p->n_right = l;
513 return p;
514 }
515 }
516
517 /*
518 * Reverse a chain of CM ops
519 */
520 static NODE *
521 reverse(NODE *p)
522 {
523 NODE *l = p->n_left;
524 NODE *r = p->n_right;
525
526 p->n_left = r;
527
528 if (l->n_op == CM)
529 return reverse1(l, p);
530
531 p->n_right = l;
532
533 return p;
534 }
535
536
537 /* push arg onto the stack */
538 /* called by moveargs() */
539 static NODE *
540 pusharg(NODE *p, int *regp)
541 {
542 NODE *q;
543 int sz;
544
545 /* convert to register size, if smaller */
546 sz = tsize(p->n_type, p->n_df, p->n_ap);
547 if (sz < SZINT)
548 p = block(SCONV, p, NIL, INT, 0, 0);
549
550 q = block(REG, NIL, NIL, INT, 0, 0);
551 regno(q) = SP;
552
553 if (szty(p->n_type) == 1) {
554 ++(*regp);
555 q = block(MINUSEQ, q, bcon(4), INT, 0, 0);
556 } else {
557 (*regp) += 2;
558 q = block(MINUSEQ, q, bcon(8), INT, 0, 0);
559 }
560
561 q = block(UMUL, q, NIL, p->n_type, p->n_df, p->n_ap);
562
563 return buildtree(ASSIGN, q, p);
564 }
565
566 /* setup call stack with 32-bit argument */
567 /* called from moveargs() */
568 static NODE *
569 movearg_32bit(NODE *p, int *regp)
570 {
571 int reg = *regp;
572 NODE *q;
573
574 q = block(REG, NIL, NIL, p->n_type, p->n_df, p->n_ap);
575 regno(q) = reg++;
576 q = buildtree(ASSIGN, q, p);
577
578 *regp = reg;
579 return q;
580 }
581
582 /* setup call stack with 64-bit argument */
583 /* called from moveargs() */
584 static NODE *
585 movearg_64bit(NODE *p, int *regp)
586 {
587 int reg = *regp;
588 NODE *q, *r;
589
590 #if ALLONGLONG == 64
591 /* alignment */
592 ++reg;
593 reg &= ~1;
594 *regp = reg;
595 #endif
596
597 if (reg > R3) {
598 q = pusharg(p, regp);
599 } else if (reg == R3) {
600 /* half in and half out of the registers */
601 r = tcopy(p);
602 if (!features(FEATURE_BIGENDIAN)) {
603 q = block(SCONV, p, NIL, INT, 0, 0);
604 q = movearg_32bit(q, regp); /* little-endian */
605 r = buildtree(RS, r, bcon(32));
606 r = block(SCONV, r, NIL, INT, 0, 0);
607 r = pusharg(r, regp); /* little-endian */
608 } else {
609 q = buildtree(RS, p, bcon(32));
610 q = block(SCONV, q, NIL, INT, 0, 0);
611 q = movearg_32bit(q, regp); /* big-endian */
612 r = block(SCONV, r, NIL, INT, 0, 0);
613 r = pusharg(r, regp); /* big-endian */
614 }
615 q = straighten(block(CM, q, r, p->n_type, p->n_df, p->n_ap));
616 } else {
617 q = block(REG, NIL, NIL, p->n_type, p->n_df, p->n_ap);
618 regno(q) = R0R1 + (reg - R0);
619 q = buildtree(ASSIGN, q, p);
620 *regp = reg + 2;
621 }
622
623 return q;
624 }
625
626 /* setup call stack with float/double argument */
627 /* called from moveargs() */
628 static NODE *
629 movearg_float(NODE *p, int *regp)
630 {
631 NODE *q, *r;
632 TWORD ty = INCREF(p->n_type);
633 int tmpnr;
634
635 /*
636 * Floats are passed in the general registers for
637 * compatibily with libraries compiled to handle soft-float.
638 */
639
640 if (xtemps) {
641 /* bounce on TOS */
642 r = block(REG, NIL, NIL, ty, p->n_df, p->n_ap);
643 regno(r) = SP;
644 r = block(PLUS, r, bcon(-4), ty, p->n_df, p->n_ap);
645 r = block(UMUL, r, NIL, p->n_type, p->n_df, p->n_ap);
646 r = buildtree(ASSIGN, r, p);
647 ecomp(r);
648
649 /* bounce into temp */
650 r = block(REG, NIL, NIL, PTR+INT, 0, 0);
651 regno(r) = SP;
652 r = block(PLUS, r, bcon(-8), PTR+INT, 0, 0);
653 r = block(UMUL, r, NIL, INT, 0, 0);
654 q = tempnode(0, INT, 0, 0);
655 tmpnr = regno(q);
656 r = buildtree(ASSIGN, q, r);
657 ecomp(r);
658 } else {
659 /* copy directly into temp */
660 q = tempnode(0, p->n_type, p->n_df, p->n_ap);
661 tmpnr = regno(q);
662 r = buildtree(ASSIGN, q, p);
663 ecomp(r);
664 }
665
666 /* copy from temp to register parameter */
667 r = tempnode(tmpnr, INT, 0, 0);
668 q = block(REG, NIL, NIL, INT, 0, 0);
669 regno(q) = (*regp)++;
670 p = buildtree(ASSIGN, q, r);
671
672 return p;
673 }
674
675 /* setup call stack with float/double argument */
676 /* called from moveargs() */
677 static NODE *
678 movearg_double(NODE *p, int *regp)
679 {
680 NODE *q, *r;
681 TWORD ty = INCREF(p->n_type);
682 int tmpnr;
683
684 if (xtemps) {
685 /* bounce on TOS */
686 r = block(REG, NIL, NIL, ty, p->n_df, p->n_ap);
687 regno(r) = SP;
688 r = block(PLUS, r, bcon(-8), ty, p->n_df, p->n_ap);
689 r = block(UMUL, r, NIL, p->n_type, p->n_df, p->n_ap);
690 r = buildtree(ASSIGN, r, p);
691 ecomp(r);
692
693 /* bounce into temp */
694 r = block(REG, NIL, NIL, PTR+LONGLONG, 0, 0);
695 regno(r) = SP;
696 r = block(PLUS, r, bcon(-8), PTR+LONGLONG, 0, 0);
697 r = block(UMUL, r, NIL, LONGLONG, 0, 0);
698 q = tempnode(0, LONGLONG, 0, 0);
699 tmpnr = regno(q);
700 r = buildtree(ASSIGN, q, r);
701 ecomp(r);
702 } else {
703 /* copy directly into temp */
704 q = tempnode(0, p->n_type, p->n_df, p->n_ap);
705 tmpnr = regno(q);
706 r = buildtree(ASSIGN, q, p);
707 ecomp(r);
708 }
709
710 /* copy from temp to register parameter */
711 r = tempnode(tmpnr, LONGLONG, 0, 0);
712 q = block(REG, NIL, NIL, LONGLONG, 0, 0);
713 regno(q) = R0R1 - R0 + (*regp);
714 p = buildtree(ASSIGN, q, r);
715
716 (*regp) += 2;
717
718 return p;
719 }
720
721
722 /* setup call stack with a structure */
723 /* called from moveargs() */
724 static NODE *
725 movearg_struct(NODE *p, int *regp)
726 {
727 int reg = *regp;
728 NODE *l, *q, *t, *r;
729 int tmpnr;
730 int navail;
731 int num;
732 int sz;
733 int ty;
734 int i;
735
736 assert(p->n_op == STARG);
737
738 navail = NARGREGS - (reg - R0);
739 navail = navail < 0 ? 0 : navail;
740 sz = tsize(p->n_type, p->n_df, p->n_ap) / SZINT;
741 num = sz > navail ? navail : sz;
742
743 /* remove STARG node */
744 l = p->n_left;
745 nfree(p);
746 ty = l->n_type;
747
748 /*
749 * put it into a TEMP, rather than tcopy(), since the tree
750 * in p may have side-affects
751 */
752 t = tempnode(0, ty, l->n_df, l->n_ap);
753 tmpnr = regno(t);
754 q = buildtree(ASSIGN, t, l);
755
756 /* copy structure into registers */
757 for (i = 0; i < num; i++) {
758 t = tempnode(tmpnr, ty, 0, 0);
759 t = block(SCONV, t, NIL, PTR+INT, 0, 0);
760 t = block(PLUS, t, bcon(4*i), PTR+INT, 0, 0);
761 t = buildtree(UMUL, t, NIL);
762
763 r = block(REG, NIL, NIL, INT, 0, 0);
764 regno(r) = reg++;
765 r = buildtree(ASSIGN, r, t);
766
767 q = block(CM, q, r, INT, 0, 0);
768 }
769
770 /* put the rest of the structure on the stack */
771 for (i = num; i < sz; i++) {
772 t = tempnode(tmpnr, ty, 0, 0);
773 t = block(SCONV, t, NIL, PTR+INT, 0, 0);
774 t = block(PLUS, t, bcon(4*i), PTR+INT, 0, 0);
775 t = buildtree(UMUL, t, NIL);
776 r = pusharg(t, &reg);
777 q = block(CM, q, r, INT, 0, 0);
778 }
779
780 q = reverse(q);
781
782 *regp = reg;
783 return q;
784 }
785
786
787 static NODE *
788 moveargs(NODE *p, int *regp)
789 {
790 NODE *r, **rp;
791 int reg;
792
793 if (p->n_op == CM) {
794 p->n_left = moveargs(p->n_left, regp);
795 r = p->n_right;
796 rp = &p->n_right;
797 } else {
798 r = p;
799 rp = &p;
800 }
801
802 reg = *regp;
803
804 if (reg > R3 && r->n_op != STARG) {
805 *rp = pusharg(r, regp);
806 } else if (r->n_op == STARG) {
807 *rp = movearg_struct(r, regp);
808 } else if (DEUNSIGN(r->n_type) == LONGLONG) {
809 *rp = movearg_64bit(r, regp);
810 } else if (r->n_type == DOUBLE || r->n_type == LDOUBLE) {
811 *rp = movearg_double(r, regp);
812 } else if (r->n_type == FLOAT) {
813 *rp = movearg_float(r, regp);
814 } else {
815 *rp = movearg_32bit(r, regp);
816 }
817
818 return straighten(p);
819 }
820
821 /*
822 * Fixup arguments to pass pointer-to-struct as first argument.
823 *
824 * called from funcode().
825 */
826 static NODE *
827 retstruct(NODE *p)
828 {
829 NODE *l, *r, *t, *q;
830 TWORD ty;
831
832 l = p->n_left;
833 r = p->n_right;
834
835 ty = DECREF(l->n_type) - FTN;
836
837 // assert(tsize(ty, l->n_df, l->n_ap) == SZINT);
838
839 /* structure assign */
840 q = tempnode(0, ty, l->n_df, l->n_ap);
841 q = buildtree(ADDROF, q, NIL);
842
843 /* insert hidden assignment at beginning of list */
844 if (r->n_op != CM) {
845 p->n_right = block(CM, q, r, INCREF(ty), l->n_df, l->n_ap);
846 } else {
847 for (t = r; t->n_left->n_op == CM; t = t->n_left)
848 ;
849 t->n_left = block(CM, q, t->n_left, INCREF(ty),
850 l->n_df, l->n_ap);
851 }
852
853 return p;
854 }
855
856 NODE *
857 builtin_frame_address(const struct bitable *bt, NODE *a)
858 {
859 assert(0);
860 return NULL;
861 }
862
863 NODE *
864 builtin_return_address(const struct bitable *bt, NODE *a)
865 {
866 assert(0);
867 return NULL;
868 }
869
870 NODE *
871 builtin_cfa(const struct bitable *bt, NODE *a)
872 {
873 assert(0);
874 return NULL;
875 }
876
877 /*
878 * Called with a function call with arguments as argument.
879 * This is done early in buildtree() and only done once.
880 */
881 NODE *
882 funcode(NODE *p)
883 {
884 int reg = R0;
885
886 if (p->n_type == STRTY+FTN || p->n_type == UNIONTY+FTN) {
887 p = retstruct(p);
888 reg = R1;
889 }
890
891 p->n_right = moveargs(p->n_right, &reg);
892
893 if (p->n_right == NULL)
894 p->n_op += (UCALL - CALL);
895
896 return p;
897 }