Debian Codegraph @

arch/aarch64/local2.c

raw symbols SCIP
1 /* $Id: local2.c,v 1.2 2021/09/04 10:38:37 gmcgarry Exp $ */
2
3 /*
4 * Copyright (c) 2020 Puresoftware Ltd.
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <assert.h>
20 #include <ctype.h>
21 #include <string.h>
22 #include <stdlib.h>
23
24 #include "pass2.h"
25
26 extern void defalign(int);
27
28 int isConverstion=0;
29 int addStack=0;
30 #define exname(x) x
31
32 /*
33 In a 32-bit context,registers are specified by using w0-w30 in A64 instruction set
34 */
35 char *wnames[] = {
36 "w0", "w1", "w2", "w3","w4","w5", "w6", "w7",
37 "w8", "w9", "w10","w11","w12","w13","w14",
38 "w15", "w16","w17","w18","w19","w20", "w21",
39 "w22","w23","w24","w25","w26","w27","w28",
40 "w29","w30",
41 };
42
43 /*
44 In a 64-bit context,registers are specified by using x0-x30 in A64 instruction set
45 */
46 char *rnames[] = {
47 "x0", "x1", "x2", "x3","x4","x5", "x6", "x7",
48 "x8", "x9", "x10","x11","x12","x13","x14",
49 "x15", "x16","x17","x18","x19","x20", "x21",
50 "x22","x23","x24","x25","x26","x27","x28",
51 "x29","x30","sp",
52 };
53
54 /*
55 * Handling of integer constants. We have 8 bits + an even
56 * number of rotates available as a simple immediate.
57 * If a constant isn't trivially representable, use an ldr
58 * and a subsequent sequence of orr operations.
59 */
60
61 static int
62 trepresent(const unsigned int val)
63 {
64 int i;
65 #define rotate_left(v, n) (v << n | v >> (32 - n))
66
67 for (i = 0; i < 32; i += 2)
68 if (rotate_left(val, i) <= 0xff)
69 return 1;
70 return 0;
71 }
72
73 /*
74 * Return values are:
75 * 0 - output constant as is (should be covered by trepresent() above)
76 * 1 - 4 generate 1-4 instructions as needed.
77 */
78 static int
79 encode_constant(int constant, int *values)
80 {
81 int tmp = constant;
82 int i = 0;
83 int first_bit, value;
84
85 while (tmp) {
86 first_bit = ffs(tmp);
87 first_bit -= 1; /* ffs indexes from 1, not 0 */
88 first_bit &= ~1; /* must use even bit offsets */
89
90 value = tmp & (0xff << first_bit);
91 values[i++] = value;
92 tmp &= ~value;
93 }
94 return i;
95 }
96
97 /*
98 * Use W registers to load value in 32-bit context
99 */
100 static void
101 load_constant_into_reg(int reg, int v)
102 {
103 printf("\tmov %s,#%d\n", wnames[reg], v);
104 }
105
106 /*
107 * Use X registers to load value in 64-bit context
108 */
109 static void
110 load_64constant_into_reg(int reg, long long v)
111 {
112 printf("\tmov %s,#%lld\n", rnames[reg], v);
113 }
114
115 static TWORD ftype;
116
117 /*
118 * calculate stack size and offsets
119 */
120 static int
121 offcalc(struct interpass_prolog *ipp)
122 {
123 int addto;
124
125 #ifdef PCC_DEBUG
126 if (x2debug)
127 printf("offcalc: p2maxautooff=%d\n", p2maxautooff);
128 #endif
129
130 addto = p2maxautooff;
131
132 #ifdef PCC_DEBUG
133 if (x2debug)
134 printf("offcalc: addto=%d\n", addto);
135 #endif
136
137 addto -= AUTOINIT / SZCHAR;
138
139 return addto;
140 }
141
142 void
143 prologue(struct interpass_prolog *ipp)
144 {
145 int addto;
146 int vals[4], nc, i;
147
148 #ifdef PCC_DEBUG
149 if (x2debug)
150 printf("prologue: type=%d, lineno=%d, name=%s, vis=%d, ipptype=%d, autos=%d, tmpnum=%d, lblnum=%d\n",
151 ipp->ipp_ip.type,
152 ipp->ipp_ip.lineno,
153 ipp->ipp_name,
154 ipp->ipp_vis,
155 ipp->ipp_type,
156 ipp->ipp_autos,
157 ipp->ip_tmpnum,
158 ipp->ip_lblnum);
159 #endif
160
161 ftype = ipp->ipp_type;
162 printf("%s:\n", exname(ipp->ipp_name));
163 addto = offcalc(ipp);
164 if (addto < 64){
165 addto = 64;
166 }
167 if((addto % 16)){
168 addto = addto + (16 - (addto % 16));
169 }
170 if (trepresent(addto)) {
171 printf("\tsub %s,%s,#%d\n", rnames[SP], rnames[SP], addto);
172 } else {
173 nc = encode_constant(addto, vals);
174 for (i = 0; i < nc; i++)
175 printf("\tsub %s,%s,#%d\n",
176 rnames[SP], rnames[SP], vals[i]);
177 }
178 printf("\tstp %s,%s,[%s]\n", rnames[FP],rnames[LR],rnames[SP]);
179 printf("\tmov %s,%s\n", rnames[FP], rnames[SP]);
180 addStack=addto;
181 }
182
183 void
184 eoftn(struct interpass_prolog *ipp)
185 {
186 if (ipp->ipp_ip.ip_lbl == 0)
187 return; /* no code needs to be generated */
188
189 /* struct return needs special treatment */
190 if (ftype == STRTY || ftype == UNIONTY) {
191 assert(0);
192 } else {
193 if (addStack == 0){
194 printf("\tldp %s,%s,[%s],#%d \n", rnames[FP], rnames[LR],
195 rnames[SP], 64);
196 printf("\tadd %s,%s,#%d\n", rnames[SP], rnames[SP], 64);
197 }
198 else{
199 printf("\tldp %s,%s,[%s] \n", rnames[FP], rnames[LR],
200 rnames[SP]);
201 printf("\tadd %s,%s,%d\n", rnames[SP], rnames[SP], addStack);
202 }
203 }
204 printf("\tret\n");
205 printf("\t.size %s,.-%s\n", exname(ipp->ipp_name),
206 exname(ipp->ipp_name));
207 }
208
209
210 /*
211 * these mnemonics match the order of the preprocessor decls
212 * EQ, NE, LE, LT, GE, GT, ULE, ULT, UGE, UGT
213 */
214
215 static char *
216 ccbranches[] = {
217 "beq", /* branch if equal */
218 "bne", /* branch if not-equal */
219 "ble", /* branch if less-than-or-equal */
220 "blt", /* branch if less-than */
221 "bge", /* branch if greater-than-or-equal */
222 "bgt", /* branch if greater-than */
223 /* what should these be ? */
224 "bls", /* branch if lower-than-or-same */
225 "blo", /* branch if lower-than */
226 "bhs", /* branch if higher-than-or-same */
227 "bhi", /* branch if higher-than */
228 };
229
230 /*
231 * add/sub/...
232 *
233 * Param given:
234 */
235 void
236 hopcode(int f, int o)
237 {
238 char *str;
239
240 switch (o) {
241 case PLUS:
242 str = "add";
243 break;
244 case MINUS:
245 str = "sub";
246 break;
247 case AND:
248 str = "and";
249 break;
250 case OR:
251 str = "orr";
252 break;
253 case ER:
254 str = "eor";
255 break;
256 default:
257 comperr("hopcode2: %d", o);
258 str = 0; /* XXX gcc */
259 }
260 printf("%s%c", str, f);
261 }
262
263 /*
264 * Return type size in bytes. Used by R2REGS, arg 2 to offset().
265 */
266 int
267 tlen(NODE *p)
268 {
269 switch(p->n_type) {
270 case CHAR:
271 case UCHAR:
272 return(1);
273
274 case SHORT:
275 case USHORT:
276 return(SZSHORT/SZCHAR);
277
278 case DOUBLE:
279 return(SZDOUBLE/SZCHAR);
280
281 case INT:
282 case UNSIGNED:
283 return(SZINT/SZCHAR);
284
285 case ULONG:
286 case LONG:
287 case LONGLONG:
288 case ULONGLONG:
289 return SZLONGLONG/SZCHAR;
290 default:
291 if (!ISPTR(p->n_type))
292 comperr("tlen type %d not pointer");
293 return SZPOINT(p->n_type)/SZCHAR;
294 }
295 }
296
297 int
298 fldexpand(NODE *p, int cookie, char **cp)
299 {
300 CONSZ val;
301 int shft;
302
303 if (p->n_op == ASSIGN)
304 p = p->n_left;
305
306 if (features(FEATURE_BIGENDIAN))
307 shft = SZINT - UPKFSZ(p->n_rval) - UPKFOFF(p->n_rval);
308 else
309 shft = UPKFOFF(p->n_rval);
310
311 switch (**cp) {
312 case 'S':
313 printf("#%d", UPKFSZ(p->n_rval));
314 break;
315 case 'H':
316 printf("#%d", shft);
317 break;
318 case 'M':
319 case 'N':
320 val = (CONSZ)1 << UPKFSZ(p->n_rval);
321 --val;
322 val <<= shft;
323 printf("%lld", (**cp == 'M' ? val : ~val) & 0xffffffff);
324 break;
325 default:
326 comperr("fldexpand");
327 }
328 return 1;
329 }
330
331
332 /*
333 * Structure assignment.
334 */
335 static void
336 stasg(NODE *p)
337 {
338 NODE *l = p->n_left;
339 int val = getlval(l);
340
341 /* R0 = dest, R1 = src, R2 = len */
342 load_constant_into_reg(R2, attr_find(p->n_ap, ATTR_P2STRUCT)->iarg(0));
343 if (l->n_op == OREG) {
344 if (R2TEST(regno(l))) {
345 int r = regno(l);
346 printf("\tadd %s,%s,lsl #%d\n",
347 rnames[R0], rnames[R2UPK2(r)], R2UPK3(r)+1);
348 printf("\tadd %s,%s,%s\n", rnames[R0], rnames[R0],
349 rnames[R2UPK1(r)]);
350 } else {
351 if (trepresent(val)) {
352 printf("\tadd %s,%s,#%d\n",
353 rnames[R0], rnames[regno(l)], val);
354 } else {
355 load_constant_into_reg(R0, val);
356 printf("\tadd %s,%s,%s\n", rnames[R0],
357 rnames[R0], rnames[regno(l)]);
358 }
359 }
360 } else if (l->n_op == NAME) {
361 cerror("not implemented");
362 }
363
364 printf("\tbl %s\n", exname("memcpy"));
365 }
366
367 /*
368 * http://gcc.gnu.org/onlinedocs/gccint/Soft-float-library-routines.html#Soft-float-library-routines
369 */
370 static void
371 fpemul(NODE *p)
372 {
373 NODE *l = p->n_left;
374 char *ch = NULL;
375
376 if (p->n_op == PLUS && p->n_type == FLOAT) ch = "addsf3";
377 else if (p->n_op == PLUS && p->n_type == DOUBLE) ch = "adddf3";
378 else if (p->n_op == PLUS && p->n_type == LDOUBLE) ch = "adddf3";
379
380 else if (p->n_op == MINUS && p->n_type == FLOAT) ch = "subsf3";
381 else if (p->n_op == MINUS && p->n_type == DOUBLE) ch = "subdf3";
382 else if (p->n_op == MINUS && p->n_type == LDOUBLE) ch = "subdf3";
383
384 else if (p->n_op == MUL && p->n_type == FLOAT) ch = "mulsf3";
385 else if (p->n_op == MUL && p->n_type == DOUBLE) ch = "muldf3";
386 else if (p->n_op == MUL && p->n_type == LDOUBLE) ch = "muldf3";
387
388 else if (p->n_op == DIV && p->n_type == FLOAT) ch = "divsf3";
389 else if (p->n_op == DIV && p->n_type == DOUBLE) ch = "divdf3";
390 else if (p->n_op == DIV && p->n_type == LDOUBLE) ch = "divdf3";
391
392 else if (p->n_op == UMINUS && p->n_type == FLOAT) ch = "negsf2";
393 else if (p->n_op == UMINUS && p->n_type == DOUBLE) ch = "negdf2";
394 else if (p->n_op == UMINUS && p->n_type == LDOUBLE) ch = "negdf2";
395
396 else if (p->n_op == EQ && l->n_type == FLOAT) ch = "eqsf2";
397 else if (p->n_op == EQ && l->n_type == DOUBLE) ch = "eqdf2";
398 else if (p->n_op == EQ && l->n_type == LDOUBLE) ch = "eqdf2";
399
400 else if (p->n_op == NE && l->n_type == FLOAT) ch = "nesf2";
401 else if (p->n_op == NE && l->n_type == DOUBLE) ch = "nedf2";
402 else if (p->n_op == NE && l->n_type == LDOUBLE) ch = "nedf2";
403
404 else if (p->n_op == GE && l->n_type == FLOAT) ch = "gesf2";
405 else if (p->n_op == GE && l->n_type == DOUBLE) ch = "gedf2";
406 else if (p->n_op == GE && l->n_type == LDOUBLE) ch = "gedf2";
407
408 else if (p->n_op == LE && l->n_type == FLOAT) ch = "lesf2";
409 else if (p->n_op == LE && l->n_type == DOUBLE) ch = "ledf2";
410 else if (p->n_op == LE && l->n_type == LDOUBLE) ch = "ledf2";
411
412 else if (p->n_op == GT && l->n_type == FLOAT) ch = "gtsf2";
413 else if (p->n_op == GT && l->n_type == DOUBLE) ch = "gtdf2";
414 else if (p->n_op == GT && l->n_type == LDOUBLE) ch = "gtdf2";
415
416 else if (p->n_op == LT && l->n_type == FLOAT) ch = "ltsf2";
417 else if (p->n_op == LT && l->n_type == DOUBLE) ch = "ltdf2";
418 else if (p->n_op == LT && l->n_type == LDOUBLE) ch = "ltdf2";
419
420 else if (p->n_op == SCONV && p->n_type == FLOAT) {
421 if (l->n_type == DOUBLE) ch = "truncdfsf2";
422 else if (l->n_type == LDOUBLE) ch = "truncdfsf2";
423 else if (l->n_type == ULONGLONG) ch = "floatunsdisf";
424 else if (l->n_type == LONGLONG) ch = "floatdisf";
425 else if (l->n_type == LONG) ch = "floatsisf";
426 else if (l->n_type == ULONG) ch = "floatunsisf";
427 else if (l->n_type == INT) ch = "floatsisf";
428 else if (l->n_type == UNSIGNED) ch = "floatunsisf";
429 } else if (p->n_op == SCONV && p->n_type == DOUBLE) {
430 if (l->n_type == FLOAT) ch = "extendsfdf2";
431 else if (l->n_type == LDOUBLE) ch = "trunctfdf2";
432 else if (l->n_type == ULONGLONG) ch = "floatunsdidf";
433 else if (l->n_type == LONGLONG) ch = "floatdidf";
434 else if (l->n_type == LONG) ch = "floatsidf";
435 else if (l->n_type == ULONG) ch = "floatunsidf";
436 else if (l->n_type == INT) ch = "floatsidf";
437 else if (l->n_type == UNSIGNED) ch = "floatunsidf";
438 } else if (p->n_op == SCONV && p->n_type == LDOUBLE) {
439 if (l->n_type == FLOAT) ch = "extendsfdf2";
440 else if (l->n_type == DOUBLE) ch = "extenddftd2";
441 else if (l->n_type == ULONGLONG) ch = "floatunsdidf";
442 else if (l->n_type == LONGLONG) ch = "floatdidf";
443 else if (l->n_type == LONG) ch = "floatsidf";
444 else if (l->n_type == ULONG) ch = "floatunsidf";
445 else if (l->n_type == INT) ch = "floatsidf";
446 else if (l->n_type == UNSIGNED) ch = "floatunsidf";
447 } else if (p->n_op == SCONV && p->n_type == ULONGLONG) {
448 if (l->n_type == FLOAT) ch = "fixunssfdi";
449 else if (l->n_type == DOUBLE) ch = "fixunsdfdi";
450 else if (l->n_type == LDOUBLE) ch = "fixunsdfdi";
451 } else if (p->n_op == SCONV && p->n_type == LONGLONG) {
452 if (l->n_type == FLOAT) ch = "fixsfdi";
453 else if (l->n_type == DOUBLE) ch = "fixdfdi";
454 else if (l->n_type == LDOUBLE) ch = "fixdfdi";
455 } else if (p->n_op == SCONV && p->n_type == LONG) {
456 if (l->n_type == FLOAT) ch = "fixsfsi";
457 else if (l->n_type == DOUBLE) ch = "fixdfsi";
458 else if (l->n_type == LDOUBLE) ch = "fixdfsi";
459 } else if (p->n_op == SCONV && p->n_type == ULONG) {
460 if (l->n_type == FLOAT) ch = "fixunssfdi";
461 else if (l->n_type == DOUBLE) ch = "fixunsdfdi";
462 else if (l->n_type == LDOUBLE) ch = "fixunsdfdi";
463 } else if (p->n_op == SCONV && p->n_type == INT) {
464 if (l->n_type == FLOAT) ch = "fixsfsi";
465 else if (l->n_type == DOUBLE) ch = "fixdfsi";
466 else if (l->n_type == LDOUBLE) ch = "fixdfsi";
467 } else if (p->n_op == SCONV && p->n_type == UNSIGNED) {
468 if (l->n_type == FLOAT) ch = "fixunssfsi";
469 else if (l->n_type == DOUBLE) ch = "fixunsdfsi";
470 else if (l->n_type == LDOUBLE) ch = "fixunsdfsi";
471 }
472
473 if (ch == NULL) comperr("ZF: op=0x%x (%d)\n", p->n_op, p->n_op);
474
475 printf("\tbl __%s" COM "softfloat operation\n", exname(ch));
476
477 if (p->n_op >= EQ && p->n_op <= GT)
478 printf("\tcmp %s,#0\n", rnames[R0]);
479 }
480
481
482 /*
483 * http://gcc.gnu.org/onlinedocs/gccint/Integer-library-routines.html#Integer-library-routines
484 */
485
486 static void
487 emul(NODE *p)
488 {
489 char *ch = NULL;
490
491 if (p->n_op == LS && DEUNSIGN(p->n_type) == LONGLONG) ch = "ashldi3";
492 else if (p->n_op == LS && DEUNSIGN(p->n_type) == LONG) ch = "ashlsi3";
493 else if (p->n_op == LS && DEUNSIGN(p->n_type) == INT) ch = "ashlsi3";
494
495 else if (p->n_op == RS && p->n_type == ULONGLONG) ch = "lshrdi3";
496 else if (p->n_op == RS && p->n_type == ULONG) ch = "lshrsi3";
497 else if (p->n_op == RS && p->n_type == UNSIGNED) ch = "lshrsi3";
498
499 else if (p->n_op == RS && p->n_type == LONGLONG) ch = "ashrdi3";
500 else if (p->n_op == RS && p->n_type == LONG) ch = "ashrsi3";
501 else if (p->n_op == RS && p->n_type == INT) ch = "ashrsi3";
502
503 else if (p->n_op == MUL && p->n_type == LONGLONG) ch = "muldi3";
504 else if (p->n_op == MUL && p->n_type == LONG) ch = "mulsi3";
505 else if (p->n_op == MUL && p->n_type == INT) ch = "mulsi3";
506
507 else if (p->n_op == UMINUS && p->n_type == LONGLONG) ch = "negdi2";
508 else if (p->n_op == UMINUS && p->n_type == LONG) ch = "negsi2";
509 else if (p->n_op == UMINUS && p->n_type == INT) ch = "negsi2";
510
511 else ch = 0, comperr("ZE");
512 printf("\tbl __%s" COM "emulated operation\n", exname(ch));
513 }
514
515 static void
516 halfword(NODE *p)
517 {
518 NODE *r = getlr(p, 'R');
519 NODE *l = getlr(p, 'L');
520 int idx0 = 0;
521 CONSZ lval;
522
523 if (features(FEATURE_BIGENDIAN)) {
524 idx0 = 1;
525 }
526
527 if (p->n_op == ASSIGN && r->n_op == OREG) {
528 /* load */
529 lval = getlval(r);
530 expand(p, 0, "\tldrh A1,");
531 printf("[%s,#" CONFMT "]\n", rnames[r->n_rval], lval+idx0);
532 } else if (p->n_op == ASSIGN && l->n_op == OREG) {
533 /* store */
534 lval = getlval(l);
535 expand(p, 0, "\tstrh AR,");
536 printf("[%s,#" CONFMT "]\n", rnames[l->n_rval], lval+idx0);
537 } else if (p->n_op == SCONV || p->n_op == UMUL) {
538 /* load */
539 lval = getlval(l);
540 expand(p, 0, "\tldrh A1,");
541 printf("[%s,#" CONFMT "]\n", rnames[l->n_rval], lval+idx0);
542 } else if (p->n_op == NAME || p->n_op == ICON || p->n_op == OREG) {
543 /* load */
544 lval = getlval(p);
545 switch (p->n_type) {
546 case SHORT:
547 expand(p, 0, "\tldrsh A1,");
548 break;
549 default:
550 expand(p, 0, "\tldrh A1,");
551 }
552 printf("[%s,#" CONFMT "]\n", rnames[p->n_rval], lval+idx0);
553 } else {
554 comperr("halfword");
555 }
556 }
557
558 static void
559 bfext(NODE *p)
560 {
561 int sz;
562 if (ISUNSIGNED(p->n_right->n_type))
563 return;
564 sz = 32 - UPKFSZ(p->n_left->n_rval);
565 expand(p, 0, "\tmov AD,AD,lsl ");
566 printf("#%d\n", sz);
567 expand(p, 0, "\tmov AD,AD,lsr ");
568 printf("#%d\n", sz);
569 }
570
571 static int
572 argsiz(NODE *p)
573 {
574 TWORD t = p->n_type;
575
576 if (t < LONGLONG || t == FLOAT || t > BTMASK)
577 return 4;
578 if (t == LONGLONG || t == ULONGLONG || t == LONG || t == ULONG )
579 return 8;
580 if (t == DOUBLE || t == LDOUBLE)
581 return 8;
582 if (t == STRTY || t == UNIONTY)
583 return attr_find(p->n_ap, ATTR_P2STRUCT)->iarg(0);
584 comperr("argsiz");
585 return 0;
586 }
587
588 void
589 zzzcode(NODE *p, int c)
590 {
591 int pr;
592
593 switch (c) {
594 case 'B': /* bit-field sign extension */
595 bfext(p);
596 break;
597
598 case 'C': /* remove from stack after subroutine call */
599 pr = p->n_qual;
600 if (p->n_op == UCALL)
601 return; /* XXX remove ZC from UCALL */
602 if (pr > 0)
603 printf("\tadd %s,%s,#%d\n", rnames[SP], rnames[SP], pr);
604 break;
605 case 'D':
606 isConverstion= 1;
607 break;
608 case 'E': /* print out emulated ops */
609 emul(p);
610 break;
611
612 case 'F': /* print out emulated floating-point ops */
613 fpemul(p);
614 break;
615
616 case 'H': /* do halfword access */
617 halfword(p);
618 break;
619
620 case 'I': /* init constant */
621 if (p->n_name[0] != '\0')
622 comperr("named init");
623 load_constant_into_reg(DECRA(p->n_reg, 1),
624 getlval(p) & 0xffffffff);
625 break;
626 case 'J': /* init longlong constant */
627 load_64constant_into_reg(DECRA(p->n_reg, 1)-R16,
628 getlval(p) & 0xffffffffffffffff);
629 break;
630
631 case 'Q': /* emit struct assign */
632 stasg(p);
633 break;
634
635 default:
636 comperr("zzzcode %c", c);
637 }
638 }
639
640 /*ARGSUSED*/
641 int
642 rewfld(NODE *p)
643 {
644 return(1);
645 }
646
647 /*
648 * Does the bitfield shape match?
649 */
650 int
651 flshape(NODE *p)
652 {
653 int o = p->n_op;
654
655 if (o == OREG || o == REG || o == NAME)
656 return SRDIR; /* Direct match */
657 if (o == UMUL && shumul(p->n_left, SOREG))
658 return SROREG; /* Convert into oreg */
659 return SRREG; /* put it into a register */
660 }
661
662 /* INTEMP shapes must not contain any temporary registers */
663 /* XXX should this go away now? */
664 int
665 shtemp(NODE *p)
666 {
667 return 0;
668 }
669
670 void
671 adrcon(CONSZ val)
672 {
673 printf("#" CONFMT, val);
674 }
675
676 void
677 conput(FILE *fp, NODE *p)
678 {
679 char *s;
680 int val = getlval(p);
681
682 switch (p->n_op) {
683 case ICON:
684 #ifdef notdef /* ICON cannot ever use sp here */
685 /* If it does, it's a giant bug */
686 if (p->n_sp == NULL || (
687 (p->n_sp->sclass == STATIC && p->n_sp->slevel > 0)))
688 s = p->n_name;
689 else
690 s = exname(p->n_name);
691 #else
692 s = p->n_name;
693 #endif
694 if (*s != '\0') {
695 fprintf(fp, "%s", s);
696 if (val > 0)
697 fprintf(fp, "+%d", val);
698 else if (val < 0)
699 fprintf(fp, "-%d", -val);
700 } else
701 fprintf(fp, "#" CONFMT, (CONSZ)val);
702 return;
703
704 default:
705 comperr("illegal conput, p %p", p);
706 }
707 }
708
709 /*ARGSUSED*/
710 void
711 insput(NODE *p)
712 {
713 comperr("insput");
714 }
715
716 /*
717 * Write out the upper address, like the upper register of a 2-register
718 * reference, or the next memory location.
719 */
720 void
721 upput(NODE *p, int size)
722 {
723
724 size /= SZCHAR;
725 switch (p->n_op) {
726 case REG:
727 printf("%s", rnames[p->n_rval-R16+1]);
728 break;
729
730 case NAME:
731 case OREG:
732 setlval(p, getlval(p) + size);
733 adrput(stdout, p);
734 setlval(p, getlval(p) - size);
735 break;
736 case ICON:
737 printf("#" CONFMT, getlval(p) >> 32);
738 break;
739 default:
740 comperr("upput bad op %d size %d", p->n_op, size);
741 }
742 }
743
744 void
745 adrput(FILE *io, NODE *p)
746 {
747 int r;
748 /* output an address, with offsets, from p */
749
750 if (p->n_op == FLD)
751 p = p->n_left;
752 switch (p->n_op) {
753 case NAME:
754 if (p->n_name[0] != '\0') {
755 fputs(p->n_name, io);
756 if (getlval(p) != 0)
757 fprintf(io, "+%lld", getlval(p));
758 } else
759 fprintf(io, "#" CONFMT, getlval(p));
760 return;
761
762 case OREG:
763 r = p->n_rval;
764 if (R2TEST(r))
765 fprintf(io, "[%s, %s, lsl #%d]",rnames[R2UPK1(r)],rnames[R2UPK2(r)],R2UPK3(r));
766 else
767 fprintf(io, "[%s,#%d]", rnames[p->n_rval], (int)getlval(p));
768 return;
769
770 case ICON:
771 /* addressable value of the constant */
772 conput(io, p);
773 return;
774
775 case REG:
776 switch (p->n_type) {
777 case CHAR:
778 case UCHAR:
779 case INT:
780 case SHORT:
781 case USHORT:
782 case BOOL:
783 case UNSIGNED:
784 if (!ISPTR(p->n_type)) {
785 if (isConverstion) {
786 fprintf(io, "%s", wnames[0]);
787 isConverstion = 0;
788 }
789 else
790 fprintf(io, "%s", wnames[p->n_rval]);
791 }
792 else
793 fprintf(io, "%s", rnames[p->n_rval]);
794 break;
795 case DOUBLE:
796 case LDOUBLE:
797 if (features(FEATURE_HARDFLOAT)) {
798 fprintf(io, "%s", rnames[p->n_rval]);
799 break;
800 }
801 /* FALLTHROUGH */
802 case LONGLONG:
803 case ULONGLONG:
804 fprintf(io, "%s", rnames[p->n_rval-R16]);
805 break;
806 default:
807 fprintf(io, "%s", rnames[p->n_rval]);
808 }
809 return;
810
811 default:
812 comperr("illegal address, op %d, node %p", p->n_op, p);
813 return;
814
815 }
816 }
817
818 /* printf conditional and unconditional branches */
819 void
820 cbgen(int o, int lab)
821 {
822 if (o < EQ || o > UGT)
823 comperr("bad conditional branch: %s", opst[o]);
824 printf("\t%s " LABFMT COM "conditional branch\n",
825 ccbranches[o-EQ], lab);
826 }
827
828 /*
829 * The arm can only address 4k to get a NAME, so there must be some
830 * rewriting here. Strategy:
831 * For first 1000 nodes found, print out the word directly.
832 * For the following 1000 nodes, group them together in asm statements
833 * and create a jump over.
834 * For the last <1000 statements, print out the words last.
835 */
836 struct addrsymb {
837 SLIST_ENTRY(addrsymb) link;
838 char *name; /* symbol name */
839 int num; /* symbol offset */
840 char *str; /* replace label */
841 };
842 SLIST_HEAD(, addrsymb) aslist;
843 static struct interpass *ipbase;
844 static int prtnumber, nodcnt, notfirst;
845 #define PRTLAB ".LY%d" /* special for here */
846
847 static struct interpass *
848 anode(char *p)
849 {
850 extern int thisline;
851 struct interpass *ip = tmpalloc(sizeof(struct interpass));
852
853 ip->ip_asm = p;
854 ip->type = IP_ASM;
855 ip->lineno = thisline;
856 return ip;
857 }
858
859 static void
860 flshlab(void)
861 {
862 struct interpass *ip;
863 struct addrsymb *el;
864 int lab = prtnumber++;
865 char *c;
866
867 if (SLIST_FIRST(&aslist) == NULL)
868 return;
869
870 snprintf(c = tmpalloc(32), 32, "\tb " PRTLAB "\n", lab);
871 ip = anode(c);
872 DLIST_INSERT_BEFORE(ipbase, ip, qelem);
873
874 SLIST_FOREACH(el, &aslist, link) {
875 /* insert each node as asm */
876 int l = 32+strlen(el->name);
877 c = tmpalloc(l);
878 if (el->num)
879 snprintf(c, l, "%s:\n\t.dword %s+%d\n",
880 el->str, el->name, el->num);
881 else
882 snprintf(c, l, "%s:\n\t.dword %s\n", el->str, el->name);
883 ip = anode(c);
884 DLIST_INSERT_BEFORE(ipbase, ip, qelem);
885 }
886 /* generate asm label */
887 snprintf(c = tmpalloc(32), 32, PRTLAB ":\n", lab);
888 ip = anode(c);
889 DLIST_INSERT_BEFORE(ipbase, ip, qelem);
890 }
891
892 static void
893 prtaddr(NODE *p, void *arg)
894 {
895 NODE *l = p->n_left;
896 struct addrsymb *el;
897 int found = 0;
898 int lab;
899
900 nodcnt++;
901
902 if (p->n_op == ASSIGN && p->n_right->n_op == ICON &&
903 p->n_right->n_name[0] != '\0') {
904 /* named constant */
905 p = p->n_right;
906
907 /* Restore addrof */
908 l = mklnode(NAME, getlval(p), 0, 0);
909 l->n_name = p->n_name;
910 p->n_left = l;
911 p->n_op = ADDROF;
912 }
913
914 if (p->n_op != ADDROF || l->n_op != NAME)
915 return;
916
917 /* if we passed 1k nodes printout list */
918 if (nodcnt > 1000) {
919 if (notfirst)
920 flshlab();
921 SLIST_INIT(&aslist);
922 notfirst = 1;
923 nodcnt = 0;
924 }
925
926 /* write address to byte stream */
927
928 SLIST_FOREACH(el, &aslist, link) {
929 if (el->num == getlval(l) && el->name[0] == l->n_name[0] &&
930 strcmp(el->name, l->n_name) == 0) {
931 found = 1;
932 break;
933 }
934 }
935 if (!found) {
936 /* we know that this is text segment */
937 lab = prtnumber++;
938 if (nodcnt <= 1000 && notfirst == 0) {
939 if (getlval(l))
940 printf(PRTLAB ":\n\t.dword %s+%lld\n",
941 lab, l->n_name, getlval(l));
942 else
943 printf(PRTLAB ":\n\t.dword %s\n",
944 lab, l->n_name);
945 }
946 el = tmpalloc(sizeof(struct addrsymb));
947 el->num = getlval(l);
948 el->name = l->n_name;
949 el->str = tmpalloc(32);
950 snprintf(el->str, 32, PRTLAB, lab);
951 SLIST_INSERT_LAST(&aslist, el, link);
952 }
953
954 nfree(l);
955 p->n_op = NAME;
956 setlval(p, 0);
957 p->n_name = el->str;
958 }
959
960 void
961 myreader(struct interpass *ipole)
962 {
963 struct interpass *ip;
964
965 SLIST_INIT(&aslist);
966 notfirst = nodcnt = 0;
967
968 DLIST_FOREACH(ip, ipole, qelem) {
969 switch (ip->type) {
970 case IP_NODE:
971 lineno = ip->lineno;
972 ipbase = ip;
973 walkf(ip->ip_node, prtaddr, 0);
974 break;
975 case IP_EPILOG:
976 ipbase = ip;
977 if (notfirst)
978 flshlab();
979 break;
980 default:
981 break;
982 }
983 }
984 if (x2debug)
985 printip(ipole);
986 }
987
988 /*
989 * Remove some PCONVs after OREGs are created.
990 */
991 static void
992 pconv2(NODE *p, void *arg)
993 {
994 NODE *q;
995
996 if (p->n_op == PLUS) {
997 if (p->n_type == (PTR+SHORT) || p->n_type == (PTR+USHORT)) {
998 if (p->n_right->n_op != ICON)
999 return;
1000 if (p->n_left->n_op != PCONV)
1001 return;
1002 if (p->n_left->n_left->n_op != OREG)
1003 return;
1004 q = p->n_left->n_left;
1005 nfree(p->n_left);
1006 p->n_left = q;
1007 /*
1008 * This will be converted to another OREG later.
1009 */
1010 }
1011 }
1012 }
1013
1014 void
1015 mycanon(NODE *p)
1016 {
1017 walkf(p, pconv2, 0);
1018 }
1019
1020 void
1021 myoptim(struct interpass *ipp)
1022 {
1023 }
1024
1025 /*
1026 * Register move: move contents of register 's' to register 'r'.
1027 */
1028 void
1029 rmove(int s, int d, TWORD t)
1030 {
1031 switch (t) {
1032 case DOUBLE:
1033 case LDOUBLE:
1034 if (features(FEATURE_HARDFLOAT)) {
1035 printf("\tfmr %s,%s" COM "rmove\n",rnames[d], rnames[s]);
1036 break;
1037 }
1038 /* FALLTHROUGH */
1039 case LONGLONG:
1040 case ULONGLONG:
1041 #define LONGREG(x, y) rnames[(x)-(R16-(y))]
1042 printf("\tmov %s,%s\n",LONGREG(d,0), LONGREG(s,0));
1043 break;
1044 case FLOAT:
1045 if (features(FEATURE_HARDFLOAT)) {
1046 printf("\tmr %s,%s" COM "rmove\n",rnames[d], rnames[s]);
1047 break;
1048 }
1049 /* FALLTHROUGH */
1050 default:
1051 if (!ISPTR(t)){
1052 printf("\tmov %s,%s" COM "rmove\n", wnames[d], wnames[s]);
1053 }
1054 else{
1055 printf("\tmov %s,%s" COM "rmove\n", rnames[d], rnames[s]);
1056 }
1057 }
1058 }
1059
1060 /*
1061 * Can we assign a register from class 'c', given the set
1062 * of number of assigned registers in each class 'r'.
1063 *
1064 * On ARM, we have:
1065 * 11 CLASSA registers (32-bit hard registers)
1066 * 10 CLASSB registers (64-bit composite registers)
1067 * 8 or 32 CLASSC registers (floating-point)
1068 *
1069 * There is a problem calculating the available composite registers
1070 * (ie CLASSB). The algorithm below assumes that given any two
1071 * registers, we can make a composite register. But this isn't true
1072 * here (or with other targets), since the number of combinations
1073 * of register pairs could become very large. Additionally,
1074 * having so many combinations really isn't so practical, since
1075 * most register pairs cannot be used to pass function arguments.
1076 * Consequently, when there is pressure composite registers,
1077 * "beenhere" compilation failures are common.
1078 *
1079 * [We need to know which registers are allocated, not simply
1080 * the number in each class]
1081 */
1082 int
1083 COLORMAP(int c, int *r)
1084 {
1085 int num = 0; /* number of registers used */
1086 switch (c) {
1087 case CLASSA:
1088 num += r[CLASSA];
1089 num += 2*r[CLASSB];
1090 return num < 11;
1091 case CLASSB:
1092 num += 2*r[CLASSB];
1093 num += r[CLASSA];
1094 return num < 6; /* XXX see comments above */
1095 case CLASSC:
1096 num += r[CLASSC];
1097 return num < 8;
1098 }
1099 cerror("colormap 2");
1100 return 0; /* XXX gcc */
1101 }
1102
1103 /*
1104 * Return a class suitable for a specific type.
1105 */
1106 int
1107 gclass(TWORD t)
1108 {
1109 if (t == DOUBLE || t == LDOUBLE) {
1110 if (features(FEATURE_HARDFLOAT))
1111 return CLASSC;
1112 else
1113 return CLASSB;
1114 }
1115 if (t == FLOAT) {
1116 if (features(FEATURE_HARDFLOAT))
1117 return CLASSC;
1118 else
1119 return CLASSA;
1120 }
1121 if (DEUNSIGN(t) == LONGLONG || DEUNSIGN(t) == LONG || t == LONGLONG || t == LONG)
1122 return CLASSB;
1123 return CLASSA;
1124 }
1125
1126 int
1127 retreg(int t)
1128 {
1129 int c = gclass(t);
1130 if (c == CLASSB)
1131 return R16;
1132 return R0;
1133 }
1134
1135 /*
1136 * Calculate argument sizes.
1137 */
1138 void
1139 lastcall(NODE *p)
1140 {
1141 NODE *op = p;
1142 int size = 0;
1143
1144 p->n_qual = 0;
1145 if (p->n_op != CALL && p->n_op != FORTCALL && p->n_op != STCALL)
1146 return;
1147 for (p = p->n_right; p->n_op == CM; p = p->n_left)
1148 size += argsiz(p->n_right);
1149 size += argsiz(p);
1150 op->n_qual = size - 16; /* XXX */
1151 }
1152
1153 /*
1154 * Special shapes.
1155 */
1156 int
1157 special(NODE *p, int shape)
1158 {
1159 return SRNOPE;
1160 }
1161
1162 /*
1163 * default to ARMv2
1164 */
1165 #ifdef TARGET_BIG_ENDIAN
1166 #define DEFAULT_FEATURES FEATURE_BIGENDIAN | FEATURE_MUL
1167 #else
1168 #define DEFAULT_FEATURES FEATURE_MUL
1169 #endif
1170
1171 static int fset = DEFAULT_FEATURES;
1172
1173 /*
1174 * Target-dependent command-line options.
1175 */
1176 void
1177 mflags(char *str)
1178 {
1179 //Handling needs to be done for ARMv8
1180 }
1181 int
1182 features(int mask)
1183 {
1184 if (mask == FEATURE_HARDFLOAT)
1185 return ((fset & mask) != 0);
1186 return ((fset & mask) == mask);
1187 }
1188
1189 /*
1190 * Define the current location as an internal label.
1191 */
1192 void
1193 deflab(int label)
1194 {
1195 printf(LABFMT ":\n", label);
1196 }
1197
1198 /*
1199 * Do something target-dependent for xasm arguments.
1200 * Supposed to find target-specific constraints and rewrite them.
1201 */
1202 int
1203 myxasm(struct interpass *ip, NODE *p)
1204 {
1205 return 0;
1206 }