scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

optm.c (1172B)


      1 #include <stddef.h>
      2 
      3 #include <scc/scc.h>
      4 
      5 #include "../../cc2.h"
      6 
      7 Node *
      8 optm_dep(Node *np)
      9 {
     10 	int op = np->op;
     11 	Node *p, *dst, *next = np->next;
     12 	Symbol *sym, *osym;
     13 
     14 	switch (op) {
     15 	case OEFUN:
     16 		/*
     17 		 * In QBE we need at the end of a basic block
     18 		 * a jump, so we have to ensure that the last
     19 		 * statement of the function is a ret, a jmp
     20 		 * or a branch. In the same way, QBE does
     21 		 * not accept labels at the end of a function
     22 		 * (ONOP is used for labels) so we have to add
     23 		 * a ret there, and in the case of branches
     24 		 * we need a label for the next statement
     25 		 */
     26 		op = (np->prev) ? np->prev->op : 0;
     27 		if (!op || op == ONOP || op == OBRANCH || (op != ORET && op != OJMP))
     28 			addstmt(node(ORET), KEEPCUR);
     29 		break;
     30 	case OBRANCH:
     31 		if (!next->label) {
     32 			sym = getsym(TMPSYM);
     33 			sym->kind = SLABEL;
     34 			next->label = sym;
     35 		}
     36 	case OJMP:
     37 		for (;;) {
     38 			dst = np->u.sym->u.stmt;
     39 			if (dst->op != OJMP)
     40 				break;
     41 			np->u.sym = dst->u.sym;
     42 		}
     43 		for (p = np->next; p; p = p->next) {
     44 			if (p == dst)
     45 				return NULL;
     46 			if (p->op == ONOP ||
     47 			    p->op == OBLOOP ||
     48 			    p->op == OELOOP) {
     49 				continue;
     50 			}
     51 			break;
     52 		}
     53 		break;
     54 	}
     55 	return np;
     56 }