Debian Codegraph @

mip/mkext.c

raw symbols SCIP
1 /* $Id: mkext.c,v 1.54 2019/04/28 18:45:47 ragge Exp $ */
2
3 /*
4 * Generate defines for the needed hardops.
5 */
6 #include "pass2.h"
7 #include <stdlib.h>
8
9 #ifdef HAVE_STRING_H
10 #include <string.h>
11 #endif
12
13 #ifdef HAVE_C99_FORMAT
14 #define FMTdPTR "%td"
15 #else
16 #if defined(_WIN64) || defined(LP64)
17 #define FMTdPTR "%ld"
18 #else
19 #define FMTdPTR "%d"
20 #endif
21 #endif
22
23 int chkop[DSIZE];
24
25 void mktables(void);
26
27 char *ftitle;
28 char *cname = "external.c";
29 char *hname = "external.h";
30 FILE *fc, *fh;
31
32 /*
33 * masks for matching dope with shapes
34 */
35 int mamask[] = {
36 SIMPFLG, /* OPSIMP */
37 SIMPFLG|ASGFLG, /* ASG OPSIMP */
38 COMMFLG, /* OPCOMM */
39 COMMFLG|ASGFLG, /* ASG OPCOMM */
40 MULFLG, /* OPMUL */
41 MULFLG|ASGFLG, /* ASG OPMUL */
42 DIVFLG, /* OPDIV */
43 DIVFLG|ASGFLG, /* ASG OPDIV */
44 UTYPE, /* OPUNARY */
45 TYFLG, /* ASG OPUNARY is senseless */
46 LTYPE, /* OPLEAF */
47 TYFLG, /* ASG OPLEAF is senseless */
48 0, /* OPANY */
49 ASGOPFLG|ASGFLG, /* ASG OPANY */
50 LOGFLG, /* OPLOG */
51 TYFLG, /* ASG OPLOG is senseless */
52 FLOFLG, /* OPFLOAT */
53 FLOFLG|ASGFLG, /* ASG OPFLOAT */
54 SHFFLG, /* OPSHFT */
55 SHFFLG|ASGFLG, /* ASG OPSHIFT */
56 SPFLG, /* OPLTYPE */
57 TYFLG, /* ASG OPLTYPE is senseless */
58 };
59
60
61 struct checks {
62 int op, type;
63 char *name;
64 } checks[] = {
65 { MUL, TLONGLONG, "SMULLL", },
66 { DIV, TLONGLONG, "SDIVLL", },
67 { MOD, TLONGLONG, "SMODLL", },
68 { PLUS, TLONGLONG, "SPLUSLL", },
69 { MINUS, TLONGLONG, "SMINUSLL", },
70 { MUL, TULONGLONG, "UMULLL", },
71 { DIV, TULONGLONG, "UDIVLL", },
72 { MOD, TULONGLONG, "UMODLL", },
73 { PLUS, TULONGLONG, "UPLUSLL", },
74 { MINUS, TULONGLONG, "UMINUSLL", },
75 { 0, 0, 0, },
76 };
77
78 int rstatus[] = { RSTATUS };
79 int roverlay[MAXREGS][MAXREGS] = { ROVERLAP };
80 int regclassmap[CLASSG][MAXREGS]; /* CLASSG is highest class */
81
82 static void
83 compl(struct optab *q, char *str)
84 {
85 int op = q->op;
86 char *s;
87
88 if (op < OPSIMP) {
89 s = opst[op];
90 } else
91 switch (op) {
92 default: s = "Special op"; break;
93 case OPSIMP: s = "OPLSIMP"; break;
94 case OPCOMM: s = "OPCOMM"; break;
95 case OPMUL: s = "OPMUL"; break;
96 case OPDIV: s = "OPDIV"; break;
97 case OPUNARY: s = "OPUNARY"; break;
98 case OPLEAF: s = "OPLEAF"; break;
99 case OPANY: s = "OPANY"; break;
100 case OPLOG: s = "OPLOG"; break;
101 case OPFLOAT: s = "OPFLOAT"; break;
102 case OPSHFT: s = "OPSHFT"; break;
103 case OPLTYPE: s = "OPLTYPE"; break;
104 }
105
106 printf("table entry " FMTdPTR ", op %s: %s\n", q - table, s, str);
107 }
108
109 /*
110 * Find reg class for top of node.
111 */
112 #ifdef NEWNEED
113 char *
114 hasneed(char *w, int need)
115 {
116 for (; *w; w += NEEDADD(*w)) {
117 if (*w == need)
118 return w;
119 }
120 return 0;
121 }
122
123 static int
124 getrcl(struct optab *q)
125 {
126 static int c[] = { 0, INAREG, INBREG, INCREG, INDREG, INEREG,
127 INFREG, INGREG };
128 int r = q->rewrite & RESC1 ? 1 : q->rewrite & RESC2 ? 2 : 3;
129 int i = 0;
130 char *w;
131
132 if ((w = q->needs) == NULL)
133 return 0;
134 while ((w = hasneed(w, cNREG))) {
135 if ((i += w[2]) >= r)
136 return c[(int)w[1]];
137 w += 2;
138 }
139 return 0;
140 }
141
142 #else
143 static int
144 getrcl(struct optab *q)
145 {
146 int v = q->needs &
147 (NACOUNT|NBCOUNT|NCCOUNT|NDCOUNT|NECOUNT|NFCOUNT|NGCOUNT);
148 int r = q->rewrite & RESC1 ? 1 : q->rewrite & RESC2 ? 2 : 3;
149 int i = 0;
150
151 #define INCK(c) while (v & c##COUNT) { \
152 v -= c##REG, i++; if (i == r) return I##c##REG; }
153 INCK(NA)
154 INCK(NB)
155 INCK(NC)
156 INCK(ND)
157 INCK(NE)
158 INCK(NF)
159 INCK(NG)
160 return 0;
161 }
162 #endif
163
164 int
165 main(int argc, char *argv[])
166 {
167 struct optab *q;
168 struct checks *ch;
169 int i, j, areg, breg, creg, dreg, mx, ereg, freg, greg;
170 char *bitary;
171 int bitsz, rval, nelem;
172
173 if (argc == 2) {
174 i = atoi(argv[1]);
175 printf("Entry %d:\n%s\n", i, table[i].cstring);
176 return 0;
177 }
178
179 mkdope();
180
181 for (q = table; q->op != FREE; q++) {
182 if (q->op >= OPSIMP)
183 continue;
184 if ((q->ltype & TLONGLONG) &&
185 (q->rtype & TLONGLONG))
186 chkop[q->op] |= TLONGLONG;
187 if ((q->ltype & TULONGLONG) &&
188 (q->rtype & TULONGLONG))
189 chkop[q->op] |= TULONGLONG;
190 }
191 if ((fc = fopen(cname, "w")) == NULL) {
192 perror("open cfile");
193 return(1);
194 }
195 if ((fh = fopen(hname, "w")) == NULL) {
196 perror("open hfile");
197 return(1);
198 }
199 fprintf(fh, "#ifndef _EXTERNAL_H_\n#define _EXTERNAL_H_\n");
200
201 for (ch = checks; ch->op != 0; ch++) {
202 if ((chkop[ch->op] & ch->type) == 0)
203 fprintf(fh, "#define NEED_%s\n", ch->name);
204 }
205
206 fprintf(fc, "#include \"pass2.h\"\n");
207 /* create fast-lookup tables */
208 mktables();
209
210 /* create efficient bitset sizes */
211 if (sizeof(long) == 8) { /* 64-bit arch */
212 bitary = "long";
213 bitsz = 64;
214 } else {
215 bitary = "int";
216 bitsz = sizeof(int) == 4 ? 32 : 16;
217 }
218 fprintf(fh, "#define NUMBITS %d\n", bitsz);
219 fprintf(fh, "#define BIT2BYTE(bits) "
220 "((((bits)+NUMBITS-1)/NUMBITS)*(NUMBITS/8))\n");
221 fprintf(fh, "#define BITSET(arr, bit) "
222 "(arr[bit/NUMBITS] |= ((%s)1 << (bit & (NUMBITS-1))))\n",
223 bitary);
224 fprintf(fh, "#define BITCLEAR(arr, bit) "
225 "(arr[bit/NUMBITS] &= ~((%s)1 << (bit & (NUMBITS-1))))\n",
226 bitary);
227 fprintf(fh, "#define TESTBIT(arr, bit) "
228 "(arr[bit/NUMBITS] & ((%s)1 << (bit & (NUMBITS-1))))\n",
229 bitary);
230 fprintf(fh, "typedef %s bittype;\n", bitary);
231
232 /* register class definitions, used by graph-coloring */
233 /* TODO */
234
235 /* Sanity-check the table */
236 rval = 0;
237 for (q = table; q->op != FREE; q++) {
238 switch (q->op) {
239 case ASSIGN:
240 #define F(x) (q->visit & x && q->rewrite & (RLEFT|RRIGHT) && \
241 q->lshape & ~x && q->rshape & ~x)
242 if (F(INAREG) || F(INBREG) || F(INCREG) || F(INDREG) ||
243 F(INEREG) || F(INFREG) || F(INGREG)) {
244 compl(q, "may match without result register");
245 rval++;
246 }
247 #undef F
248 /* FALLTHROUGH */
249 case STASG:
250 if ((q->visit & INREGS) && !(q->rewrite & RDEST)) {
251 compl(q, "ASSIGN/STASG reclaim must be RDEST");
252 rval++;
253 }
254 break;
255 }
256 /* check that reclaim is not the wrong class */
257 if ((q->rewrite & (RESC1|RESC2|RESC3)) &&
258 #ifdef NEWNEED
259 hasneed(q->needs, cNREW) == 0) {
260 #else
261 !(q->needs & REWRITE)) {
262 #endif
263 if ((q->visit & getrcl(q)) == 0) {
264 compl(q, "wrong RESCx class");
265 rval++;
266 }
267 }
268 if (q->rewrite & (RESC1|RESC2|RESC3) && q->visit & FOREFF)
269 compl(q, "FOREFF may cause reclaim of wrong class");
270 }
271
272 /* print out list of scratched and permanent registers */
273 fprintf(fh, "extern int tempregs[], permregs[];\n");
274 fprintf(fc, "int tempregs[] = { ");
275 for (i = j = 0; i < MAXREGS; i++)
276 if (rstatus[i] & TEMPREG)
277 fprintf(fc, "%d, ", i), j++;
278 fprintf(fc, "-1 };\n");
279 fprintf(fh, "#define NTEMPREG %d\n", j+1);
280 fprintf(fh, "#define FREGS %d\n", j); /* XXX - to die */
281 fprintf(fc, "int permregs[] = { ");
282 for (i = j = 0; i < MAXREGS; i++)
283 if (rstatus[i] & PERMREG)
284 fprintf(fc, "%d, ", i), j++;
285 fprintf(fc, "-1 };\n");
286 fprintf(fh, "#define NPERMREG %d\n", j+1);
287 fprintf(fc, "bittype validregs[] = {\n");
288
289 if (bitsz == 64) {
290 for (j = 0; j < MAXREGS; j += bitsz) {
291 long cbit = 0;
292 for (i = 0; i < bitsz; i++) {
293 if (i+j == MAXREGS)
294 break;
295 if (rstatus[i+j] & INREGS)
296 cbit |= ((long)1 << i);
297 }
298 fprintf(fc, "\t0x%lx,\n", cbit);
299 }
300 } else {
301 for (j = 0; j < MAXREGS; j += bitsz) {
302 int cbit = 0;
303 for (i = 0; i < bitsz; i++) {
304 if (i+j == MAXREGS)
305 break;
306 if (rstatus[i+j] & INREGS)
307 cbit |= (1 << i);
308 }
309 fprintf(fc, "\t0x%08x,\n", cbit);
310 }
311 }
312
313 fprintf(fc, "};\n");
314 fprintf(fh, "extern bittype validregs[];\n");
315
316 /*
317 * The register allocator uses bitmasks of registers for each class.
318 */
319 areg = breg = creg = dreg = ereg = freg = greg = 0;
320 for (i = 0; i < MAXREGS; i++) {
321 for (j = 0; j < NUMCLASS; j++)
322 regclassmap[j][i] = -1;
323 if (rstatus[i] & SAREG) regclassmap[0][i] = areg++;
324 if (rstatus[i] & SBREG) regclassmap[1][i] = breg++;
325 if (rstatus[i] & SCREG) regclassmap[2][i] = creg++;
326 if (rstatus[i] & SDREG) regclassmap[3][i] = dreg++;
327 if (rstatus[i] & SEREG) regclassmap[4][i] = ereg++;
328 if (rstatus[i] & SFREG) regclassmap[5][i] = freg++;
329 if (rstatus[i] & SGREG) regclassmap[6][i] = greg++;
330 }
331 fprintf(fh, "#define AREGCNT %d\n", areg);
332 fprintf(fh, "#define BREGCNT %d\n", breg);
333 fprintf(fh, "#define CREGCNT %d\n", creg);
334 fprintf(fh, "#define DREGCNT %d\n", dreg);
335 fprintf(fh, "#define EREGCNT %d\n", ereg);
336 fprintf(fh, "#define FREGCNT %d\n", freg);
337 fprintf(fh, "#define GREGCNT %d\n", greg);
338 if (areg > bitsz)
339 printf("%d regs in class A (max %d)\n", areg, bitsz), rval++;
340 if (breg > bitsz)
341 printf("%d regs in class B (max %d)\n", breg, bitsz), rval++;
342 if (creg > bitsz)
343 printf("%d regs in class C (max %d)\n", creg, bitsz), rval++;
344 if (dreg > bitsz)
345 printf("%d regs in class D (max %d)\n", dreg, bitsz), rval++;
346 if (ereg > bitsz)
347 printf("%d regs in class E (max %d)\n", ereg, bitsz), rval++;
348 if (freg > bitsz)
349 printf("%d regs in class F (max %d)\n", freg, bitsz), rval++;
350 if (greg > bitsz)
351 printf("%d regs in class G (max %d)\n", greg, bitsz), rval++;
352
353 fprintf(fc, "static int amap[MAXREGS][NUMCLASS] = {\n");
354 for (i = 0; i < MAXREGS; i++) {
355 int ba, bb, bc, bd, r, be, bf, bg;
356 ba = bb = bc = bd = be = bf = bg = 0;
357 if (rstatus[i] & SAREG) ba = (1 << regclassmap[0][i]);
358 if (rstatus[i] & SBREG) bb = (1 << regclassmap[1][i]);
359 if (rstatus[i] & SCREG) bc = (1 << regclassmap[2][i]);
360 if (rstatus[i] & SDREG) bd = (1 << regclassmap[3][i]);
361 if (rstatus[i] & SEREG) be = (1 << regclassmap[4][i]);
362 if (rstatus[i] & SFREG) bf = (1 << regclassmap[5][i]);
363 if (rstatus[i] & SGREG) bg = (1 << regclassmap[6][i]);
364 for (j = 0; roverlay[i][j] >= 0; j++) {
365 r = roverlay[i][j];
366 if (rstatus[r] & SAREG)
367 ba |= (1 << regclassmap[0][r]);
368 if (rstatus[r] & SBREG)
369 bb |= (1 << regclassmap[1][r]);
370 if (rstatus[r] & SCREG)
371 bc |= (1 << regclassmap[2][r]);
372 if (rstatus[r] & SDREG)
373 bd |= (1 << regclassmap[3][r]);
374 if (rstatus[r] & SEREG)
375 be |= (1 << regclassmap[4][r]);
376 if (rstatus[r] & SFREG)
377 bf |= (1 << regclassmap[5][r]);
378 if (rstatus[r] & SGREG)
379 bg |= (1 << regclassmap[6][r]);
380 }
381 fprintf(fc, "\t/* %d */{ 0x%x", i, ba);
382 if (NUMCLASS > 1) fprintf(fc, ",0x%x", bb);
383 if (NUMCLASS > 2) fprintf(fc, ",0x%x", bc);
384 if (NUMCLASS > 3) fprintf(fc, ",0x%x", bd);
385 if (NUMCLASS > 4) fprintf(fc, ",0x%x", be);
386 if (NUMCLASS > 5) fprintf(fc, ",0x%x", bf);
387 if (NUMCLASS > 6) fprintf(fc, ",0x%x", bg);
388 fprintf(fc, " },\n");
389 }
390 fprintf(fc, "};\n");
391
392 fprintf(fh, "int aliasmap(int class, int regnum);\n");
393 fprintf(fc, "int\naliasmap(int class, int regnum)\n{\n");
394 fprintf(fc, " return amap[regnum][class-1];\n}\n");
395
396 /* routines to convert back from color to regnum */
397 mx = areg;
398 if (breg > mx) mx = breg;
399 if (creg > mx) mx = creg;
400 if (dreg > mx) mx = dreg;
401 if (ereg > mx) mx = ereg;
402 if (freg > mx) mx = freg;
403 if (greg > mx) mx = greg;
404 if (mx > (int)(sizeof(int)*8)-1) {
405 printf("too many regs in a class, use two classes instead\n");
406 #ifdef HAVE_C99_FORMAT
407 printf("%d > %zu\n", mx, (sizeof(int)*8)-1);
408 #else
409 printf("%d > %d\n", mx, (int)(sizeof(int)*8)-1);
410 #endif
411 rval++;
412 }
413 fprintf(fc, "static int rmap[NUMCLASS][%d] = {\n", mx);
414 for (j = 0; j < NUMCLASS; j++) {
415 int cl = SAREG << j;
416 if (j > 3)
417 cl = SEREG << (j - 4);
418 fprintf(fc, "\t{ ");
419 for (i = 0; i < MAXREGS; i++)
420 if (rstatus[i] & cl) fprintf(fc, "%d, ", i);
421 fprintf(fc, "},\n");
422 }
423 fprintf(fc, "};\n\n");
424
425 fprintf(fh, "int color2reg(int color, int class);\n");
426 fprintf(fc, "int\ncolor2reg(int color, int class)\n{\n");
427 fprintf(fc, " return rmap[class-1][color];\n}\n");
428
429 /* used by register allocator */
430 fprintf(fc, "int regK[] = { 0, %d, %d, %d, %d, %d, %d, %d };\n",
431 areg, breg, creg, dreg, ereg, freg, greg);
432 fprintf(fc, "int clregs[] = { 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x };\n",
433 (1 << areg)-1, (1 << breg)-1, (1 << creg)-1,
434 (1 << dreg)-1, (1 << ereg)-1, (1 << freg)-1, (1 << greg)-1);
435
436 fprintf(fc, "int\nclassmask(int class)\n{\n");
437 fprintf(fc, "\treturn clregs[class-1];\n}\n");
438
439 fprintf(fh, "int interferes(int reg1, int reg2);\n");
440 nelem = (MAXREGS+bitsz-1)/bitsz;
441 fprintf(fc, "static bittype ovlarr[MAXREGS][%d] = {\n", nelem);
442 for (i = 0; i < MAXREGS; i++) {
443 int el[10];
444 memset(el, 0, sizeof(el));
445 el[i/bitsz] = 1 << (i % bitsz);
446 for (j = 0; roverlay[i][j] >= 0; j++) {
447 int k = roverlay[i][j];
448 el[k/bitsz] |= (1 << (k % bitsz));
449 }
450 fprintf(fc, "{ ");
451 for (j = 0; j < MAXREGS; j += bitsz)
452 fprintf(fc, "0x%x, ", el[j/bitsz]);
453 fprintf(fc, " },\n");
454 }
455 fprintf(fc, "};\n");
456
457 fprintf(fc, "int\ninterferes(int reg1, int reg2)\n{\n");
458 fprintf(fc, "return (TESTBIT(ovlarr[reg1], reg2)) != 0;\n}\n");
459 fclose(fc);
460 fprintf(fh, "#endif /* _EXTERNAL_H_ */\n");
461 fclose(fh);
462 return rval;
463 }
464
465 #define P(x) fprintf x
466
467 void
468 mktables(void)
469 {
470 struct optab *op;
471 int mxalen = 0, curalen;
472 int i;
473
474 #if 0
475 P((fc, "#include \"pass2.h\"\n\n"));
476 #endif
477 for (i = 0; i <= MAXOP; i++) {
478 curalen = 0;
479 P((fc, "static int op%d[] = { ", i));
480 if (dope[i] != 0)
481 for (op = table; op->op != FREE; op++) {
482 if (op->op < OPSIMP) {
483 if (op->op == i) {
484 P((fc, FMTdPTR ", ", op - table));
485 curalen++;
486 }
487 } else {
488 int opmtemp;
489 if ((opmtemp=mamask[op->op - OPSIMP])&SPFLG) {
490 if (i==NAME || i==ICON || i==TEMP ||
491 i==OREG || i == REG || i == FCON) {
492 P((fc, FMTdPTR ", ",
493 op - table));
494 curalen++;
495 }
496 } else if ((dope[i]&(opmtemp|ASGFLG))==opmtemp){
497 P((fc, FMTdPTR ", ", op - table));
498 curalen++;
499 }
500 }
501 }
502 if (curalen > mxalen)
503 mxalen = curalen;
504 P((fc, "-1 };\n"));
505 }
506 P((fc, "\n"));
507
508 P((fc, "int *qtable[] = { \n"));
509 for (i = 0; i <= MAXOP; i++) {
510 P((fc, " op%d,\n", i));
511 }
512 P((fc, "};\n"));
513 P((fh, "#define MAXOPLEN %d\n", mxalen+1));
514 }