scc

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

cgen.c (2341B)


      1 #include <stdlib.h>
      2 
      3 #include <scc/scc.h>
      4 
      5 #include "arch.h"
      6 #include "../cc2.h"
      7 
      8 static void
      9 swtch(Node *idx)
     10 {
     11 }
     12 
     13 static Node *
     14 rhs(Node *np)
     15 {
     16 }
     17 
     18 static Node *
     19 field(Node *np, int islhs)
     20 {
     21 }
     22 
     23 static Node *
     24 lhs(Node *np)
     25 {
     26 	switch (np->op) {
     27 	case OMEM:
     28 	case OAUTO:
     29 		return np;
     30 	case OPTR:
     31 		return rhs(np->left);
     32 	case OFIELD:
     33 		return field(np, 1);
     34 	default:
     35 		abort();
     36 	}
     37 }
     38 
     39 static void
     40 bool(Node *np, Symbol *true, Symbol *false)
     41 {
     42 	Node *l = np->left, *r = np->right;
     43 	Node ret, ifyes, ifno;
     44 	Symbol *label;
     45 
     46 	switch (np->op) {
     47 	case ONEG:
     48 		bool(l, false, true);
     49 		break;
     50 	case OAND:
     51 		label = newlabel();
     52 		bool(l, label, false);
     53 		setlabel(label);
     54 		bool(r, true, false);
     55 		break;
     56 	case OOR:
     57 		label = newlabel();
     58 		bool(l, true, label);
     59 		setlabel(label);
     60 		bool(r, true, false);
     61 		break;
     62 	default:
     63 		label2node(&ifyes, true);
     64 		label2node(&ifno, false);
     65 		code(ASBRANCH, rhs(np), &ifyes, &ifno);
     66 		break;
     67 	}
     68 }
     69 
     70 Node *
     71 cgen(Node *np)
     72 {
     73 	Node aux, *p, *next;
     74 
     75 	setlabel(np->label);
     76 	switch (np->op) {
     77 	case OJMP:
     78 		label2node(&aux, np->u.sym);
     79 		code(ASJMP, NULL, &aux, NULL);
     80 		break;
     81 	case OBRANCH:
     82 		next = np->next;
     83 		if (!next->label)
     84 			next->label = newlabel();
     85 		bool(np->left, np->u.sym, next->label);
     86 		break;
     87 	case ORET:
     88 		p = (np->left) ? rhs(np->left) : NULL;
     89 		code(ASRET, NULL, p, NULL);
     90 		break;
     91 	case OBSWITCH:
     92 		swtch(rhs(np->left));
     93 		break;
     94 	default:
     95 		rhs(np);
     96 		break;
     97 	}
     98 	return NULL;
     99 }
    100 
    101 /*
    102  * This is strongly influenced by
    103  * http://plan9.bell-labs.com/sys/doc/compiler.ps (/sys/doc/compiler.ps)
    104  * calculate addresability as follows
    105  *     AUTO => 11          value+fp
    106  *     REG => 13           reg
    107  *     STATIC => 12        (value)
    108  *     CONST => 20         $value
    109  */
    110 Node *
    111 sethi(Node *np)
    112 {
    113 	Node *lp, *rp;
    114 
    115 	if (!np)
    116 		return np;
    117 
    118 	np->complex = 0;
    119 	np->address = 0;
    120 	lp = np->left;
    121 	rp = np->right;
    122 	switch (np->op) {
    123 	case OAUTO:
    124 		np->address = 11;
    125 		break;
    126 	case OREG:
    127 		np->address = 13;
    128 		break;
    129 	case OMEM:
    130 		np->address = 12;
    131 		break;
    132 	case OCONST:
    133 		np->address = 20;
    134 		break;
    135 	default:
    136 		sethi(lp);
    137 		sethi(rp);
    138 		break;
    139 	}
    140 
    141 	if (np->address > 10)
    142 		return np;
    143 	if (lp)
    144 		np->complex = lp->complex;
    145 	if (rp) {
    146 		int d = np->complex - rp->complex;
    147 
    148 		if (d == 0)
    149 			++np->complex;
    150 		else if (d < 0)
    151 			np->complex = rp->complex;
    152 	}
    153 	if (np->complex == 0)
    154 		++np->complex;
    155 	return np;
    156 }