scc

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

cgen.c (1071B)


      1 static char sccsid[] = "@(#) ./cc2/arch/z80/cgen.c";
      2 
      3 #include "arch.h"
      4 #include "../../../inc/scc.h"
      5 #include "../../cc2.h"
      6 
      7 Node *
      8 cgen(Node *np)
      9 {
     10 }
     11 
     12 /*
     13  * This is strongly influenced by
     14  * http://plan9.bell-labs.com/sys/doc/compiler.ps (/sys/doc/compiler.ps)
     15  * calculate addresability as follows
     16  *     AUTO => 11          value+fp
     17  *     REG => 13           reg
     18  *     STATIC => 12        (value)
     19  *     CONST => 20         $value
     20  */
     21 Node *
     22 sethi(Node *np)
     23 {
     24 	Node *lp, *rp;
     25 
     26 	if (!np)
     27 		return np;
     28 
     29 	np->complex = 0;
     30 	np->address = 0;
     31 	lp = np->left;
     32 	rp = np->right;
     33 	switch (np->op) {
     34 	case OAUTO:
     35 		np->address = 11;
     36 		break;
     37 	case OREG:
     38 		np->address = 13;
     39 		break;
     40 	case OMEM:
     41 		np->address = 12;
     42 		break;
     43 	case OCONST:
     44 		np->address = 20;
     45 		break;
     46 	default:
     47 		sethi(lp);
     48 		sethi(rp);
     49 		break;
     50 	}
     51 
     52 	if (np->address > 10)
     53 		return np;
     54 	if (lp)
     55 		np->complex = lp->complex;
     56 	if (rp) {
     57 		int d = np->complex - rp->complex;
     58 
     59 		if (d == 0)
     60 			++np->complex;
     61 		else if (d < 0)
     62 			np->complex = rp->complex;
     63 	}
     64 	if (np->complex == 0)
     65 		++np->complex;
     66 	return np;
     67 }