Debian Codegraph @

arch/arm/local.c

raw symbols SCIP
1 /* $Id: local.c,v 1.36 2021/08/09 00:23:24 gmcgarry 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * We define location operations which operate on the expression tree
30 * during the first pass (before sending to the backend for code generation.)
31 */
32
33 #include <assert.h>
34
35 #include "pass1.h"
36
37 #undef NIL
38 #define NIL NULL
39
40 #ifdef LANG_CXX
41 #define p1listf listf
42 #define p1tfree tfree
43 #else
44 #define NODE P1ND
45 #define talloc p1alloc
46 #define tcopy p1tcopy
47 #define nfree p1nfree
48 #endif
49
50 extern void defalign(int);
51
52 static char *
53 getsoname(struct symtab *sp)
54 {
55 struct attr *ap;
56 return (ap = attr_find(sp->sap, ATTR_SONAME)) ?
57 ap->sarg(0) : sp->sname;
58 }
59
60
61 /*
62 * clocal() is called to do local transformations on
63 * an expression tree before being sent to the backend.
64 */
65 NODE *
66 clocal(NODE *p)
67 {
68 struct symtab *q;
69 NODE *l, *r, *t;
70 int o;
71 int ty;
72 int tmpnr, isptrvoid = 0;
73 char *n;
74
75 o = p->n_op;
76 switch (o) {
77
78 case STASG:
79
80 l = p->n_left;
81 r = p->n_right;
82 if (r->n_op != STCALL && r->n_op != USTCALL)
83 return p;
84
85 /* assign left node as first argument to function */
86 nfree(p);
87 t = block(REG, NIL, NIL, r->n_type, r->n_df, r->n_ap);
88 l->n_rval = R0;
89 l = buildtree(ADDROF, l, NIL);
90 l = buildtree(ASSIGN, t, l);
91
92 if (r->n_right->n_op != CM) {
93 r->n_right = block(CM, l, r->n_right, INT, 0, 0);
94 } else {
95 for (t = r->n_right; t->n_left->n_op == CM;
96 t = t->n_left)
97 ;
98 t->n_left = block(CM, l, t->n_left, INT, 0, 0);
99 }
100 return r;
101
102 case CALL:
103 case STCALL:
104 case USTCALL:
105 if (p->n_type == VOID)
106 break;
107 /*
108 * if the function returns void*, ecode() invokes
109 * delvoid() to convert it to uchar*.
110 * We just let this happen on the ASSIGN to the temp,
111 * and cast the pointer back to void* on access
112 * from the temp.
113 */
114 if (p->n_type == PTR+VOID)
115 isptrvoid = 1;
116 r = tempnode(0, p->n_type, p->n_df, p->n_ap);
117 tmpnr = regno(r);
118 r = block(ASSIGN, r, p, p->n_type, p->n_df, p->n_ap);
119
120 p = tempnode(tmpnr, r->n_type, r->n_df, r->n_ap);
121 if (isptrvoid) {
122 p = block(PCONV, p, NIL, PTR+VOID, p->n_df, 0);
123 }
124 p = buildtree(COMOP, r, p);
125 break;
126
127 case NAME:
128 if ((q = p->n_sp) == NULL)
129 return p;
130 if (blevel == 0)
131 return p;
132
133 switch (q->sclass) {
134 case PARAM:
135 case AUTO:
136 /* fake up a structure reference */
137 r = block(REG, NIL, NIL, PTR+STRTY, 0, 0);
138 slval(r, 0);
139 r->n_rval = FPREG;
140 p = stref(block(STREF, r, p, 0, 0, 0));
141 break;
142 case REGISTER:
143 p->n_op = REG;
144 slval(p, 0);
145 p->n_rval = q->soffset;
146 break;
147 case STATIC:
148 if (q->slevel > 0) {
149 slval(p, 0);
150 p->n_sp = q;
151 }
152 /* FALL-THROUGH */
153 default:
154 ty = p->n_type;
155 n = getsoname(p->n_sp);
156 if (strncmp(n, "__builtin", 9) == 0)
157 break;
158 p = block(ADDROF, p, NIL, INCREF(ty), p->n_df, p->n_ap);
159 p = block(UMUL, p, NIL, ty, p->n_df, p->n_ap);
160 break;
161 }
162 break;
163
164 case STNAME:
165 if ((q = p->n_sp) == NULL)
166 return p;
167 if (q->sclass != STNAME)
168 return p;
169 ty = p->n_type;
170 p = block(ADDROF, p, NIL, INCREF(ty),
171 p->n_df, p->n_ap);
172 p = block(UMUL, p, NIL, ty, p->n_df, p->n_ap);
173 break;
174
175 case FORCE:
176 /* put return value in return reg */
177 p->n_op = ASSIGN;
178 p->n_right = p->n_left;
179 p->n_left = block(REG, NIL, NIL, p->n_type, 0, 0);
180 p->n_left->n_rval = p->n_left->n_type == BOOL ?
181 RETREG(BOOL_TYPE) : RETREG(p->n_type);
182 break;
183
184 case SCONV:
185 l = p->n_left;
186 if (p->n_type == l->n_type) {
187 nfree(p);
188 return l;
189 }
190 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
191 tsize(p->n_type, p->n_df, p->n_ap) == tsize(l->n_type, l->n_df, l->n_ap)) {
192 if (p->n_type != FLOAT && p->n_type != DOUBLE &&
193 l->n_type != FLOAT && l->n_type != DOUBLE &&
194 l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
195 if (l->n_op == NAME || l->n_op == UMUL ||
196 l->n_op == TEMP) {
197 l->n_type = p->n_type;
198 nfree(p);
199 return l;
200 }
201 }
202 }
203
204 if (l->n_op == ICON) {
205 CONSZ val = glval(l);
206 CONSZ lval;
207
208 if (!ISPTR(p->n_type)) /* Pointers don't need to be conv'd */
209 switch (p->n_type) {
210 case BOOL:
211 slval(l, glval(l) != 0);
212 break;
213 case CHAR:
214 lval = (char)val;
215 break;
216 case UCHAR:
217 lval = val & 0377;
218 break;
219 case SHORT:
220 lval = (short)val;
221 break;
222 case USHORT:
223 lval = val & 0177777;
224 break;
225 case ULONG:
226 case UNSIGNED:
227 lval = val & 0xffffffff;
228 break;
229 case LONG:
230 case INT:
231 lval = (int)val;
232 break;
233 case LONGLONG:
234 lval = (long long)val;
235 break;
236 case ULONGLONG:
237 lval = val;
238 break;
239 case VOID:
240 break;
241 case LDOUBLE:
242 case DOUBLE:
243 case FLOAT:
244 #if 0
245 l->n_op = FCON;
246 ((FLT *)l->n_dcon)->fp = val;
247 #endif
248 break;
249 default:
250 cerror("unknown type %d", l->n_type);
251 }
252 if (p->n_type < FLOAT)
253 slval(l, lval);
254 l->n_type = p->n_type;
255 l->n_ap = 0;
256 nfree(p);
257 return l;
258 } else if (p->n_op == FCON) {
259 #if 0
260 slval(l, ((FLT *)l->n_dcon)->fp);
261 l->n_sp = NULL;
262 l->n_op = ICON;
263 l->n_type = p->n_type;
264 l->n_ap = 0;
265 nfree(p);
266 return clocal(l);
267 #endif
268 cerror("SCONV FCON");
269 }
270 if ((DEUNSIGN(p->n_type) == CHAR ||
271 DEUNSIGN(p->n_type) == SHORT) &&
272 (l->n_type == FLOAT || l->n_type == DOUBLE ||
273 l->n_type == LDOUBLE)) {
274 p = block(SCONV, p, NIL, p->n_type, p->n_df, p->n_ap);
275 p->n_left->n_type = INT;
276 return p;
277 }
278 break;
279
280 case PCONV:
281 l = p->n_left;
282 if (l->n_op == ICON) {
283 slval(l, (unsigned)glval(l));
284 goto delp;
285 }
286 if (l->n_type < INT || DEUNSIGN(l->n_type) == LONGLONG) {
287 p->n_left = block(SCONV, l, NIL, UNSIGNED, 0, 0);
288 break;
289 }
290 if (l->n_op == SCONV)
291 break;
292 if (l->n_op == ADDROF && l->n_left->n_op == TEMP)
293 goto delp;
294 if (p->n_type > BTMASK && l->n_type > BTMASK)
295 goto delp;
296 break;
297
298 delp:
299 l->n_type = p->n_type;
300 l->n_qual = p->n_qual;
301 l->n_df = p->n_df;
302 l->n_ap = p->n_ap;
303 nfree(p);
304 p = l;
305 break;
306 }
307
308 return p;
309 }
310
311 /*
312 * Called before sending the tree to the backend.
313 */
314 void
315 myp2tree(NODE *p)
316 {
317 struct symtab *sp;
318
319 if (p->n_op != FCON)
320 return;
321
322 #define IALLOC(sz) (isinlining ? permalloc(sz) : tmpalloc(sz))
323
324 sp = IALLOC(sizeof(struct symtab));
325 sp->sclass = STATIC;
326 sp->sap = 0;
327 sp->slevel = 1; /* fake numeric label */
328 sp->soffset = getlab();
329 sp->sflags = 0;
330 sp->stype = p->n_type;
331 sp->squal = (CON >> TSHIFT);
332
333 defloc(sp);
334 ninval(0, tsize(sp->stype, sp->sdf, sp->sap), p);
335
336 p->n_op = NAME;
337 slval(p, 0);
338 p->n_sp = sp;
339 }
340
341 /*
342 * Called during the first pass to determine if a NAME can be addressed.
343 *
344 * Return nonzero if supported, otherwise return 0.
345 */
346 int
347 andable(NODE *p)
348 {
349 if (blevel == 0)
350 return 1;
351 if (ISFTN(p->n_type))
352 return 1;
353 return 0;
354 }
355
356 /*
357 * Return 1 if a variable of type 't' is OK to put in register.
358 */
359 int
360 cisreg(TWORD t)
361 {
362 if (t == FLOAT || t == DOUBLE || t == LDOUBLE)
363 return 0; /* not yet */
364 return 1;
365 }
366
367 /*
368 * Allocate bits from the stack for dynamic-sized arrays.
369 *
370 * 'p' is the tree which represents the type being allocated.
371 * 'off' is the number of 'p's to be allocated.
372 * 't' is the storeable node where the address is written.
373 */
374 void
375 spalloc(NODE *t, NODE *p, OFFSZ off)
376 {
377 NODE *sp;
378
379 p = buildtree(MUL, p, bcon(off/SZCHAR)); /* XXX word alignment? */
380
381 /* sub the size from sp */
382 sp = block(REG, NIL, NIL, p->n_type, 0, 0);
383 slval(sp, 0);
384 sp->n_rval = SP;
385 ecomp(buildtree(MINUSEQ, sp, p));
386
387 /* save the address of sp */
388 sp = block(REG, NIL, NIL, PTR+INT, t->n_df, t->n_ap);
389 slval(sp, 0);
390 sp->n_rval = SP;
391 t->n_type = sp->n_type;
392 ecomp(buildtree(ASSIGN, t, sp));
393 }
394
395 /*
396 * Print an integer constant node, may be associated with a label.
397 * Do not free the node after use.
398 * 'off' is bit offset from the beginning of the aggregate
399 * 'fsz' is the number of bits this is referring to
400 */
401 int
402 ninval(CONSZ off, int fsz, NODE *p)
403 {
404 struct symtab *q;
405 TWORD t;
406 int i, j;
407
408 t = p->n_type;
409 if (t > BTMASK)
410 t = p->n_type = INT; /* pointer */
411
412 if (p->n_op == ICON && p->n_sp != NULL && DEUNSIGN(t) != INT)
413 uerror("element not constant");
414
415 switch (t) {
416 case LONGLONG:
417 case ULONGLONG:
418 i = (glval(p) >> 32);
419 j = (glval(p) & 0xffffffff);
420 p->n_type = INT;
421 if (features(FEATURE_BIGENDIAN)) {
422 slval(p, i);
423 ninval(off+32, 32, p);
424 slval(p, j);
425 ninval(off, 32, p);
426 } else {
427 slval(p, j);
428 ninval(off, 32, p);
429 slval(p, i);
430 ninval(off+32, 32, p);
431 }
432 break;
433 case INT:
434 case UNSIGNED:
435 printf("\t.word 0x%x", (int)glval(p));
436 if ((q = p->n_sp) != NULL) {
437 if ((q->sclass == STATIC && q->slevel > 0)) {
438 printf("+" LABFMT, q->soffset);
439 } else
440 printf("+%s", getexname(q));
441 }
442 printf("\n");
443 break;
444 #if 0
445 case LDOUBLE:
446 case DOUBLE:
447 u.d = (double)((FLT *)p->n_dcon)->fp;
448 #if defined(HOST_BIG_ENDIAN)
449 if (features(FEATURE_BIGENDIAN))
450 #else
451 if (!features(FEATURE_BIGENDIAN))
452 #endif
453 printf("\t.word\t0x%x\n\t.word\t0x%x\n",
454 u.i[0], u.i[1]);
455 else
456 printf("\t.word\t0x%x\n\t.word\t0x%x\n",
457 u.i[1], u.i[0]);
458 break;
459 case FLOAT:
460 u.f = (float)((FLT *)p->n_dcon)->fp;
461 printf("\t.word\t0x%x\n", u.i[0]);
462 break;
463 #endif
464 default:
465 return 0;
466 }
467 return 1;
468 }
469
470 /*
471 * Prefix a leading underscore to a global variable (if necessary).
472 */
473 char *
474 exname(char *p)
475 {
476 return (p == NULL ? "" : p);
477 }
478
479 /*
480 * Map types which are not defined on the local machine.
481 */
482 TWORD
483 ctype(TWORD type)
484 {
485 switch (BTYPE(type)) {
486 case LONG:
487 MODTYPE(type,INT);
488 break;
489 case ULONG:
490 MODTYPE(type,UNSIGNED);
491 break;
492 }
493 return (type);
494 }
495
496 /*
497 * Before calling a function do any tree re-writing for the local machine.
498 *
499 * 'p' is the function tree (NAME)
500 * 'q' is the CM-separated list of arguments.
501 */
502 void
503 calldec(NODE *p, NODE *q)
504 {
505 }
506
507 /*
508 * While handling uninitialised variables, handle variables marked extern.
509 */
510 void
511 extdec(struct symtab *q)
512 {
513 }
514
515 /* make a common declaration for id, if reasonable */
516 void
517 defzero(struct symtab *sp)
518 {
519 int off;
520
521 off = tsize(sp->stype, sp->sdf, sp->sap);
522 off = (off+(SZCHAR-1))/SZCHAR;
523 printf(" .%scomm ", sp->sclass == STATIC ? "l" : "");
524 if (sp->slevel == 0)
525 printf("%s,0%o\n", getexname(sp), off);
526 else
527 printf(LABFMT ",0%o\n", sp->soffset, off);
528 }
529
530 /*
531 * va_start(ap, last) implementation.
532 *
533 * f is the NAME node for this builtin function.
534 * a is the argument list containing:
535 * CM
536 * ap last
537 */
538 NODE *
539 arm_builtin_stdarg_start(const struct bitable *bt, NODE *a)
540 {
541 NODE *p, *q;
542 int sz = 1;
543
544 /* check num args and type */
545 if (a == NULL || a->n_op != CM || a->n_left->n_op == CM ||
546 !ISPTR(a->n_left->n_type))
547 goto bad;
548
549 /* must first deal with argument size; use int size */
550 p = a->n_right;
551 if (p->n_type < INT) {
552 /* round up to word */
553 sz = SZINT / tsize(p->n_type, p->n_df, p->n_ap);
554 }
555
556 p = buildtree(ADDROF, p, NIL); /* address of last arg */
557 p = optim(buildtree(PLUS, p, bcon(sz)));
558 q = block(NAME, NIL, NIL, PTR+VOID, 0, 0);
559 q = buildtree(CAST, q, p);
560 p = q->n_right;
561 nfree(q->n_left);
562 nfree(q);
563 p = buildtree(ASSIGN, a->n_left, p);
564 nfree(a);
565
566 return p;
567
568 bad:
569 uerror("bad argument to __builtin_stdarg_start");
570 return bcon(0);
571 }
572
573 NODE *
574 arm_builtin_va_arg(const struct bitable *bt, NODE *a)
575 {
576 NODE *p, *q, *r;
577 int sz, tmpnr;
578
579 /* check num args and type */
580 if (a == NULL || a->n_op != CM || a->n_left->n_op == CM ||
581 !ISPTR(a->n_left->n_type) || a->n_right->n_op != TYPE)
582 goto bad;
583
584 r = a->n_right;
585
586 /* get type size */
587 sz = tsize(r->n_type, r->n_df, r->n_ap) / SZCHAR;
588 if (sz < SZINT/SZCHAR) {
589 werror("%s%s promoted to int when passed through ...",
590 ISUNSIGNED(r->n_type) ? "unsigned " : "",
591 DEUNSIGN(r->n_type) == SHORT ? "short" : "char");
592 sz = SZINT/SZCHAR;
593 }
594
595 /* alignment */
596 p = tcopy(a->n_left);
597 if (sz > SZINT/SZCHAR && r->n_type != UNIONTY && r->n_type != STRTY) {
598 p = buildtree(PLUS, p, bcon(ALSTACK/8 - 1));
599 p = block(AND, p, bcon(-ALSTACK/8), p->n_type, p->n_df, p->n_ap);
600 }
601
602 /* create a copy to a temp node */
603 q = tempnode(0, p->n_type, p->n_df, p->n_ap);
604 tmpnr = regno(q);
605 p = buildtree(ASSIGN, q, p);
606
607 q = tempnode(tmpnr, p->n_type, p->n_df,p->n_ap);
608 q = buildtree(PLUS, q, bcon(sz));
609 q = buildtree(ASSIGN, a->n_left, q);
610
611 q = buildtree(COMOP, p, q);
612
613 nfree(a->n_right);
614 nfree(a);
615
616 p = tempnode(tmpnr, INCREF(r->n_type), r->n_df, r->n_ap);
617 p = buildtree(UMUL, p, NIL);
618 p = buildtree(COMOP, q, p);
619
620 return p;
621
622 bad:
623 uerror("bad argument to __builtin_va_arg");
624 return bcon(0);
625 }
626
627 NODE *
628 arm_builtin_va_end(const struct bitable *bt, NODE *a)
629 {
630 p1tfree(a);
631
632 return bcon(0);
633 }
634
635 NODE *
636 arm_builtin_va_copy(const struct bitable *bt, NODE *a)
637 {
638 NODE *f;
639
640 if (a == NULL || a->n_op != CM || a->n_left->n_op == CM)
641 goto bad;
642 f = buildtree(ASSIGN, a->n_left, a->n_right);
643 nfree(a);
644 return f;
645
646 bad:
647 uerror("bad argument to __buildtin_va_copy");
648 return bcon(0);
649 }
650
651 char *nextsect;
652 static int constructor;
653 static int destructor;
654
655 /*
656 * Give target the opportunity of handling pragmas.
657 */
658 int
659 mypragma(char *str)
660 {
661 char *a2 = pragtok(NULL);
662
663 if (strcmp(str, "tls") == 0) {
664 uerror("thread-local storage not supported for this target");
665 return 1;
666 }
667 if (strcmp(str, "constructor") == 0 || strcmp(str, "init") == 0) {
668 constructor = 1;
669 return 1;
670 }
671 if (strcmp(str, "destructor") == 0 || strcmp(str, "fini") == 0) {
672 destructor = 1;
673 return 1;
674 }
675 if (strcmp(str, "section") == 0 && a2 != NULL) {
676 nextsect = newstring(a2, strlen(a2));
677 return 1;
678 }
679
680 return 0;
681 }
682
683 /*
684 * Called when a identifier has been declared, to give target last word.
685 */
686 void
687 fixdef(struct symtab *sp)
688 {
689 if ((constructor || destructor) && (sp->sclass != PARAM)) {
690 printf("\t.section .%ctors,\"aw\",@progbits\n",
691 constructor ? 'c' : 'd');
692 printf("\t.p2align 2\n");
693 printf("\t.long %s\n", exname(sp->sname));
694 printf("\t.previous\n");
695 constructor = destructor = 0;
696 }
697 }
698
699 void
700 pass1_lastchance(struct interpass *ip)
701 {
702 }