scc

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

cgen.c (2486B)


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