scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

cgen.c (1045B)


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