#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ast.h"
#include "ssa.h"
#define NEW(type) ((type *)malloc(sizeof(type)))
#define DBLINK(from, to) ((from)->next = (to))->prev = (from)
static CGraph cfg;
static CBlock_t blks[MAX_BLOCK];
static int bcnt; /* block counter */
static int tcnt; /* temporary counter */
static int gbbase;
CBlock_t cblock_create() {
CBlock_t cblk = NEW(CBlock);
CInst_t dummy = NEW(CInst);
dummy->prev = dummy;
dummy->next = dummy;
cblk->insts = dummy;
cblk->next = NULL;
cblk->id = (bcnt++) + gbbase;
cblk->ref = 0;
return cblk;
}
void cblock_append(CBlock_t cblk, CInst_t inst) {
CInst_t head = cblk->insts;
(inst->prev = head->prev)->next = inst;
(inst->next = head)->prev = inst;
}
void cblock_popback(CBlock_t cblk) {
CInst_t last = cblk->insts->prev;
last->next->prev = last->prev;
last->prev->next = last->next;
}
CInst_t cblock_getback(CBlock_t cblk) {
return cblk->insts->prev;
}
int cblock_isempty(CBlock_t cblk) {
return cblk->insts->prev == cblk->insts;
}
CVar_t ctmp_create(CType_t type) {
static char buff[MAX_NAMELEN];
sprintf(buff, "t%d", tcnt++);
return cvar_create(strdup(buff), type, NULL);
}
void ctmp_destroy(CVar_t type) {
free(type->name); /* allocated dynamically */
free(type);
}
void cfg_clear() {
int i;
for (i = 0; i < MAX_BLOCK; i++)
if (cfg.head[i])
{
CEdge *p, *np;
for (p = cfg.head[i]; p; p = np)
{
np = p->next;
free(p);
}
cfg.head[i] = NULL;
}
}
void cfg_add_edge(CBlock_t from, CBlock_t to) {
int id = from->id;
CEdge *e = NEW(CEdge);
e->to = to;
e->next = cfg.head[id];
cfg.head[id] = e;
}
void copr_print(COpr *opr) {
switch (opr->kind)
{
case VAR:
case TMP: fprintf(stderr, "%s", opr->info.var->name);
break