scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

optm.c (1234B)


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