scc

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

code.c (2041B)


      1 #include <stdlib.h>
      2 #include <string.h>
      3 
      4 #include <scc/scc.h>
      5 #include "cc2.h"
      6 
      7 Inst *pc, *prog;
      8 
      9 static void
     10 nextpc(void)
     11 {
     12         Inst *new;
     13 
     14         new = xcalloc(1, sizeof(*new)); /* TODO: create an arena */
     15 
     16         if (!pc) {
     17                 prog = new;
     18         } else {
     19                 new->next = pc->next;
     20                 pc->next = new;
     21         }
     22 
     23 	/* SNONE being 0, calloc initialized {from1,from2,to}.kind for us */
     24         new->prev = pc;
     25         pc = new;
     26 }
     27 
     28 static void
     29 addr(Node *np, Addr *addr)
     30 {
     31 	Symbol *sym;
     32 
     33 	switch (np->op) {
     34 	case OREG:
     35 		/* TODO:
     36 		 * At this moment this op is used also for register variables
     37 		 */
     38 		addr->kind = SREG;
     39 		addr->u.reg = np->u.reg;
     40 		break;
     41 	case OCONST:
     42 		addr->kind = SCONST;
     43 		/* TODO: Add support for more type of constants */
     44 		addr->u.i = np->u.i;
     45 		break;
     46 	case OTMP:
     47 	case OLABEL:
     48 	case OAUTO:
     49 	case OMEM:
     50 		sym = np->u.sym;
     51 		addr->kind = sym->kind;
     52 		addr->u.sym = sym;
     53 		break;
     54 	default:
     55 		abort();
     56 	}
     57 }
     58 
     59 Symbol *
     60 newlabel(void)
     61 {
     62 	Symbol *sym = getsym(TMPSYM);
     63 
     64 	sym->kind = SLABEL;
     65 	return sym;
     66 }
     67 
     68 Node *
     69 label2node(Node *np, Symbol *sym)
     70 {
     71 	if(!sym)
     72 		sym = newlabel();
     73 	if (!np)
     74 		np = node(OLABEL);
     75 	np->op = OLABEL;
     76 	np->u.sym = sym;
     77 
     78 	return np;
     79 }
     80 
     81 Node *
     82 constnode(Node *np, TUINT n, Type *tp)
     83 {
     84 	if (!np)
     85 		np = node(OCONST);
     86 	np->op = OCONST;
     87 	np->left = NULL;
     88 	np->right = NULL;
     89 	np->type = *tp;
     90 	np->u.i = n;
     91 	return np;
     92 }
     93 
     94 void
     95 setlabel(Symbol *sym)
     96 {
     97 	if (!sym)
     98 		return;
     99 	code(0, NULL, NULL, NULL);
    100 	pc->label = sym;
    101 	sym->u.inst = pc;
    102 }
    103 
    104 void
    105 code(int op, Node *to, Node *from1, Node *from2)
    106 {
    107 	nextpc();
    108 	if (from1)
    109 		addr(from1, &pc->from1);
    110 	if (from2)
    111 		addr(from2, &pc->from2);
    112 	if (to)
    113 		addr(to, &pc->to);
    114 	pc->op = op;
    115 }
    116 
    117 void
    118 delcode(void)
    119 {
    120         Inst *prev = pc->prev, *next = pc->next;
    121 
    122         free(pc);
    123         if (!prev) {
    124                 pc = next;
    125                 prog = NULL;
    126         } else {
    127                 pc = prev;
    128                 prev->next = next;
    129                 if (next)
    130                         next->prev = prev;
    131         }
    132 }