Debian Codegraph @

arch/aarch64/local.c

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