Debian Codegraph @

cc2/cc2.nova.c

raw symbols SCIP
1 /* $Id: cc2.nova.c,v 1.2 2021/12/18 13:26:32 ragge Exp $ */
2
3 /*
4 * Copyright (c) 2021 Anders Magnusson. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <ctype.h>
28 #include <err.h>
29 #include <regex.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 #include "manifest.h"
35
36 /*
37 * Peephole optimizer for Nova.
38 * It is mandatory for this program to be run on the assembler file
39 * after compilation, to cleanup short-distance jumps/loads/stores.
40 *
41 */
42 #define BEGFTN "#BEGFTN"
43 #define ENDFTN "#ENDFTN"
44 #define BUFLEN 256
45
46 /* constant lines */
47 struct symtab {
48 DLIST_ENTRY(symtab) link;
49 struct symtab *next;
50 char *name; /* e.g. "L345" */
51 int off; /* position in memory */
52 };
53
54 /* Definistion for exactly one word, as linked list in function */
55 struct mblk {
56 DLIST_ENTRY(mblk) link;
57 char *name; /* input string */
58 int off; /* insn offset in memory */
59 int base; /* long address position in memory (if needed) */
60 struct symtab *sp; /* symbol this jmp wants */
61 int flags;
62 #define INSERTD 001 /* Is an inserted symbol */
63 #define FIRST 002 /* First block in function (before moves) */
64 #define LAST 004 /* Last block in function (before moves) */
65 };
66
67 struct mblk mpole;
68 struct symtab spole;
69
70 int lnum, vflag;
71 int curaddr, offb, offe;
72
73 #define VPRINT(...) if (vflag) fprintf(stderr, __VA_ARGS__)
74
75 static void readftn(void);
76 static void movecon(void);
77 static void renum(void);
78 static void jmpdist(void);
79 static void printftn(void);
80
81 int
82 main(int argc, char *argv[])
83 {
84 char buf[BUFLEN], *b, *pname;
85 int ch;
86
87 pname = argv[0];
88 while ((ch = getopt(argc, argv, "v")) != -1) {
89 switch (ch) {
90 case 'v':
91 vflag++;
92 break;
93 default:
94 fprintf(stderr,
95 "usage: %s [-v] [infile [outfile]]\n", pname);
96 }
97 }
98 argc -= optind;
99 argv += optind;
100
101 if (argc > 0)
102 if (freopen(argv[0], "r", stdin) == NULL)
103 err(1, "reopen stdin");
104 if (argc > 1)
105 if (freopen(argv[1], "w", stdout) == NULL)
106 err(1, "reopen stdout");
107
108 for (;;) {
109 while ((b = fgets(buf, BUFLEN, stdin)) != NULL) {
110 if (strncmp(b, BEGFTN, sizeof(BEGFTN) - 1) == 0)
111 break;
112 printf("%s", buf);
113 }
114 if (b == NULL)
115 break;
116
117 DLIST_INIT(&mpole,link);
118 DLIST_INIT(&spole,link);
119 curaddr = 0;
120
121 readftn(); /* fetch all function lines until end */
122 movecon(); /* move constants to before/after functions */
123 renum(); /* recalculate all offsets in mblk list */
124 jmpdist(); /* insert indirect jmp length words */
125
126 printftn();
127 }
128 return 0;
129 }
130
131 #define IMCODE(a,b) ((a << 8) | b)
132 #define IJMP 1
133 #define IJSR 2
134 #define IMREF 3
135 #define IALC 4
136 static struct im {
137 int code;
138 int type;
139 } imatch[] = {
140 { IMCODE('j','m'), IJMP }, { IMCODE('j','s'), IMREF },
141 { IMCODE('l','d'), IMREF }, { IMCODE('s','t'), IMREF },
142 { IMCODE('i','s'), IMREF }, { IMCODE('d','s'), IMREF },
143 { IMCODE('m','o'), IALC },
144 { IMCODE('a','d'), IALC },
145 { IMCODE('s','u'), IALC },
146 { IMCODE('c','o'), IALC },
147 { IMCODE('n','e'), IALC },
148 { IMCODE('i','n'), IALC },
149 { IMCODE('a','n'), IALC },
150 { 0, 0 }
151 };
152
153 static int
154 itype(char *s)
155 {
156 struct im *im = imatch;
157 int c = (s[0] << 8) | s[1];
158
159 for (; im->code; im++)
160 if (im->code == c)
161 return im->type;
162 return 0;
163 }
164
165 static struct mblk *
166 mkmblk(char *s, int o)
167 {
168 struct mblk *mb = malloc(sizeof(struct mblk));
169 mb->name = s;
170 mb->off = o;
171 mb->base = mb->flags = 0;
172 mb->sp = NULL;
173 return mb;
174 }
175
176 static struct symtab *
177 mksp(char *s)
178 {
179 struct symtab *sp = malloc(sizeof(struct symtab));
180 sp->name = s;
181 sp->off = 0;
182 sp->next = 0;
183 return sp;
184 }
185
186 /*
187 * Readin function and store in linked list.
188 * Check directives and do not save .globl.
189 * This function expects the output syntax that pcc has, do not
190 * try to parse anything else.
191 */
192 static void
193 readftn(void)
194 {
195 static struct symtab *savedsp;
196 struct symtab *sp;
197 struct mblk *mb;
198 char buf[BUFLEN];
199 char *b, *e;
200
201 for (;;) {
202 if ((b = fgets(buf, BUFLEN, stdin)) == NULL)
203 err(1, "fgets");
204 if (strncmp(b, ENDFTN, sizeof(ENDFTN)-1) == 0)
205 break;
206 while (*b == ' ' || *b == '\t')
207 b++;
208 for (e = b; *e; e++)
209 ;
210 if (*--e != '\n')
211 errx(1, "no lf line '%s'", buf);
212 *e = 0; /* remove trailing lf */
213 if (*b == '.') {
214 if (strncmp(b, ".globl", 6) == 0) {
215 printf("%s\n", buf);
216 continue;
217 } else if (strncmp(b, ".word", 5) != 0)
218 errx(1, "bad dot dir %s", buf);
219 }
220 if (e[-1] == ':') {
221 *--e = 0; /* remove : */
222 sp = mksp(strdup(b));
223 sp->next = savedsp;
224 savedsp = sp;
225 DLIST_INSERT_BEFORE(&spole, sp, link);
226 } else {
227 mb = mkmblk(strdup(b), curaddr);
228 mb->sp = savedsp;
229 if (curaddr == 0)
230 mb->flags |= FIRST;
231 curaddr++;
232 savedsp = NULL;
233 DLIST_INSERT_BEFORE(&mpole, mb, link);
234 }
235 }
236 DLIST_PREV(&mpole, link)->flags |= LAST;
237 }
238
239 static struct symtab *
240 findsym(char *s)
241 {
242 struct symtab *sp;
243
244 DLIST_FOREACH(sp, &spole, link)
245 if (strcmp(sp->name, s) == 0)
246 return sp;
247 return NULL;
248 }
249
250 static struct mblk *
251 findw(char *s, struct mblk *pole)
252 {
253 struct mblk *mb;
254
255 DLIST_FOREACH(mb, pole, link) {
256 if (strcmp(s, mb->name) == 0)
257 return mb;
258 }
259 return NULL;
260 }
261
262 static int
263 isskip(char *s)
264 {
265 while (*s && *s != ' ' && *s != '\t')
266 s++;
267 while (*s == ' ' || *s == '\t')
268 s++;
269 if (s[3] == ',')
270 return 1;
271 return 0;
272 }
273
274 /*
275 * Move constants to a reasonable place.
276 * The first 128 slots are put on mfirst list, the last 127 on mlast.
277 * If any place inbetween is needed it is inserted on the correct place
278 * in the mpole list.
279 */
280 static void
281 movecon(void)
282 {
283 struct mblk mfirst, mlast;
284 struct mblk *mb, *mb2, *mblow, *mbhigh, mtemp, *mbpout;
285 struct symtab *sp;
286 char buf[BUFLEN];
287 char *b, *s;
288 int curcnt;
289
290 DLIST_INIT(&mfirst,link);
291 if (curaddr > 128) {
292 DLIST_FOREACH(mb, &mpole, link)
293 if (mb->off == 128)
294 break;
295 } else
296 mb = DLIST_PREV(&mpole, link);
297 mblow = mb;
298 //printf("movecon1: mblow %p curaddr %d\n", mblow, curaddr);
299 /*
300 * First handle constants at the beginning of the function.
301 * iterate backwards to get constants in the correct order.
302 */
303 for (; mb != &mpole; mb = DLIST_PREV(mb, link)) {
304 if ((b = strchr(mb->name, '['))) {
305 s = strdup(b+1);
306 s[strlen(s)-1] = 0; /* remove trailing ] */
307 if ((mb2 = findw(s, &mfirst)) == 0) {
308 mb2 = mkmblk(s, 0);
309 mb2->flags = INSERTD;
310 DLIST_INSERT_AFTER(&mfirst, mb2, link);
311 }
312 if (mb2->sp == 0) {
313 sprintf(buf, "LP%d", lnum++);
314 sp = mksp(strdup(buf));
315 mb2->sp = sp;
316 DLIST_INSERT_BEFORE(&spole, sp, link);
317 }
318 strcpy(b, mb2->sp->name);
319 }
320 }
321 /* move to main list */
322 while (!DLIST_ISEMPTY(&mfirst, link)) {
323 mb = DLIST_PREV(&mfirst, link);
324 DLIST_REMOVE(mb, link);
325 DLIST_INSERT_AFTER(&mpole, mb, link);
326 }
327 if (curaddr <= 128)
328 return; /* <= 128 entries */
329 //printf("movecon2: mblow %p curaddr %d\n", mblow, curaddr);
330 /*
331 * Second; move constants at the end of the function.
332 */
333 DLIST_INIT(&mlast,link);
334 if (curaddr <= 255) {
335 mb = DLIST_NEXT(mblow, link);
336 } else {
337 DLIST_FOREACH_REVERSE(mb, &mpole, link)
338 if (mb->off == curaddr-127)
339 break;
340 }
341 mbhigh = mb;
342 //printf("movecon3: mbhigh %p curaddr %d\n", mbhigh, curaddr);
343 for (; mb != &mpole; mb = DLIST_NEXT(mb, link)) {
344 if ((b = strchr(mb->name, '['))) {
345 s = strdup(b+1);
346 s[strlen(s)-1] = 0; /* remove trailing ] */
347 if ((mb2 = findw(s, &mlast)) == 0) {
348 mb2 = mkmblk(s, 0);
349 mb2->flags = INSERTD;
350 DLIST_INSERT_BEFORE(&mlast, mb2, link);
351 }
352 if (mb2->sp == 0) {
353 sprintf(buf, "LP%d", lnum++);
354 sp = mksp(strdup(buf));
355 mb2->sp = sp;
356 DLIST_INSERT_BEFORE(&spole, sp, link);
357 }
358 strcpy(b, mb2->sp->name);
359 }
360 }
361 /* move to main list */
362 while (!DLIST_ISEMPTY(&mlast, link)) {
363 mb = DLIST_NEXT(&mlast, link);
364 DLIST_REMOVE(mb, link);
365 DLIST_INSERT_BEFORE(&mpole, mb, link);
366 }
367 //printf("movecon5: mb %p curaddr %d\n", mb, curaddr);
368 if (curaddr <= 255)
369 return;
370 //printf("movecon6: mb %p curaddr %d\n", mb, curaddr);
371 /*
372 * Here begins the tricky part.
373 * - Start at mblow. At the first memref insn start counting.
374 * - If a valid jmp is found, write out the collected data.
375 * - if passed 127 insns (or reached mbhigh) and no valid jmp,
376 * go back (at least) one instruction, insert a jmp,
377 * and write out the collected data. if passed mbhigh return.
378 * - go back and try again.
379 */
380 DLIST_INIT(&mtemp, link);
381 curcnt = 0;
382 mbpout = NULL;
383
384 //printf("movecon7: mb %p curaddr %d\n", mb, curaddr);
385 for (mb = DLIST_NEXT(mblow, link); mb != mbhigh;
386 mb = DLIST_NEXT(mb, link)) {
387 int ityp = itype(mb->name);
388
389 //printf("movecon4: mb %p mbnum %d lnum %d line %s\n", mb, mb->off, curcnt, mb->name);
390 if (curcnt)
391 curcnt++;
392 if (ityp == IMREF && (b = strchr(mb->name, '['))) {
393 s = strdup(b+1);
394 s[strlen(s)-1] = 0; /* remove trailing ] */
395 if ((mb2 = findw(s, &mtemp)) == 0) {
396 mb2 = mkmblk(s, 0);
397 mb2->flags = INSERTD;
398 DLIST_INSERT_AFTER(&mtemp, mb2, link);
399 }
400 if (mb2->sp == 0) {
401 sprintf(buf, "LP%d", lnum++);
402 sp = mksp(strdup(buf));
403 mb2->sp = sp;
404 DLIST_INSERT_BEFORE(&spole, sp, link);
405 }
406 strcpy(b, mb2->sp->name);
407 if (curcnt == 0)
408 curcnt++;
409 }
410 if (curcnt && ityp == IJMP) {
411 mb2 = DLIST_PREV(mb, link);
412 if (itype(mb2->name) != IALC || !isskip(mb2->name)) {
413 mbpout = DLIST_NEXT(mb, link);
414 /* safe to print out words after jmp */
415 while (!DLIST_ISEMPTY(&mtemp, link)) {
416 mb2 = DLIST_NEXT(&mtemp, link);
417 DLIST_REMOVE(mb2, link);
418 DLIST_INSERT_AFTER(mb, mb2, link);
419 }
420 curcnt = 0;
421 }
422 }
423 if (curcnt == 127)
424 errx(1, "curcnt == 127");
425 }
426 if (curcnt) {
427 if (mb->off - mbpout->off < 128) {
428 /* can put constants at last position */
429 while (!DLIST_ISEMPTY(&mtemp, link)) {
430 mb2 = DLIST_PREV(&mtemp, link);
431 DLIST_REMOVE(mb2, link);
432 DLIST_INSERT_BEFORE(mbpout, mb2, link);
433 }
434 } else
435 errx(1, "FIXME long distance");
436 }
437 //printf("movecon8: mb %p mbnum %d\n", mb, mb->off);
438 }
439
440 /*
441 * Update offset numbering in both words and symbols.
442 */
443 static void
444 renum()
445 {
446 struct symtab *sp;
447 struct mblk *mb;
448 int n = 0;
449
450 DLIST_FOREACH(mb, &mpole, link) {
451 if (mb->flags & FIRST)
452 offb = n;
453 if (mb->flags & LAST)
454 offe = n;
455 for (sp = mb->sp; sp; sp = sp->next)
456 sp->off = n;
457 mb->off = n++;
458 }
459 }
460
461 /*
462 * Check for all local jumps that the distance is inside offsets.
463 * If it is not, insert an indirect word at a reasonable place.
464 */
465 static void
466 jmpdist()
467 {
468 struct symtab *sp;
469 struct mblk *mb, *mb2, *mb3;
470 char buf[80];
471 int ityp;
472
473 DLIST_FOREACH(mb, &mpole, link) {
474 ityp = itype(mb->name);
475 if (ityp != IJMP)
476 continue;
477 if (mb->name[4] != 'L')
478 continue; /* not local jmp */
479 if ((sp = findsym(&mb->name[4])) == NULL)
480 errx(1, "sym '%s' undefined", &mb->name[4]);
481 if (sp->off < mb->off && mb->off-sp->off < 128)
482 continue;
483 if (sp->off > mb->off && sp->off-mb->off < 127)
484 continue;
485 VPRINT("symbol %s too far away from %s: s=%d m=%d\n",
486 sp->name, mb->name, sp->off, mb->off);
487
488 /* find where to insert the indirect word */
489 if (DLIST_NEXT(mb, link)->flags & INSERTD) {
490 /* hereafter */
491 mb2 = mb;
492 VPRINT("insert directly after\n");
493 } else {
494 /* search backwards for some place to insert word */
495 for (mb2 = DLIST_PREV(mb, link); mb2 != &mpole;
496 mb2 = DLIST_PREV(mb2, link))
497 if (mb2->flags & INSERTD)
498 break;
499 if (mb2 == &mpole || mb->off-mb2->off > 128) {
500 for (mb2 = DLIST_NEXT(mb, link); mb2 != &mpole;
501 mb2 = DLIST_NEXT(mb2, link))
502 if (mb2->flags & INSERTD)
503 break;
504 if (mb2 == &mpole || mb2->off-mb->off > 127) {
505 errx(1,"FIXME: too long distance %d %d",
506 mb->off, mb2->off);
507 continue;
508 }
509 }
510 }
511 sprintf(buf, ".word %s", &mb->name[4]);
512 mb3 = mkmblk(strdup(buf), 0);
513 mb3->flags = INSERTD;
514 DLIST_INSERT_AFTER(mb2, mb3, link);
515 sprintf(buf, "LP%d", lnum);
516 mb3->sp = mksp(strdup(buf));
517 sprintf(buf, "jmp @LP%d", lnum++);
518 mb->name = strdup(buf);
519 if (vflag)
520 fprintf(stderr, "inserted %s at %d\n",
521 mb3->sp->name, mb2->off);
522 }
523
524 }
525
526 /*
527 * When everything is modified; print out the new function.
528 */
529 static void
530 printftn(void)
531 {
532 struct symtab *sp;
533 struct mblk *mb;
534
535 DLIST_FOREACH(mb, &mpole, link) {
536 while ((sp = mb->sp) != NULL) {
537 printf("%s:\n", sp->name);
538 mb->sp = sp->next;
539 }
540 printf(" %s\n", mb->name);
541 }
542 }