view mc-tree.c @ 283:230a3b98b843 mips-done

MIPS jump work.
author kono
date Mon, 24 May 2004 13:21:22 +0900
parents f94ca1168520
children da2e3f2d127d
line wrap: on
line source

/* Micro-C tree print routine */
/* $Id$ */

#define EXTERN /**/
#include "mc.h"

typedef
struct tree_node {
    int tree_type;
    char *tree_name;
    char *tree_args;
} tree_node_type;

extern void tree_print(int e);
extern void tree_parse(int e);
extern void tree_print_t(int e,int t);
static tree_node_type * find_node(int e);
extern void type_print(int type,NMTBL *n,FILE *out);
extern void type_print1(int type,NMTBL *n,FILE *out,int cont);
extern void sym_print(int type,FILE *out);

/* ascendant order for binary search */

static
tree_node_type tree_nodes[] = {
    {(-45),"...",""},
    {(-44),"lmacro",""},
    {(-43),"fmacro",""},
    {(-42),"konst",""},
    {(-41),"defined",""},
    {(-40),"environment",""},
    {(-39),"code",""},
    {(-38),"register",""},
    {(-37),"void",""},
    {(-36),"extern",""},
    {(-35),"short",""},
    {(-34),"long",""},
    {(-33),"type",""},
    {(-32),"sizeof",""},
    {(-31),"typedef",""},
    {(-30),"flabel",""},
    {(-29),"blabel",""},
    {(-28),"macro",""},
    {(-27),"string",""},
    {(-26),"ident",""},
    {(-25),"field",""},
    {(-24),"tag",""},
    {(-23),"reserve",""},
    {(-22),"default",""},
    {(-21),"case",""},
    {(-20),"switch",""},
    {(-19),"while",""},
    {(-18),"do",""},
    {(-17),"for",""},
    {(-16),"else",""},
    {(-15),"if",""},
    {(-14),"continue",""},
    {(-13),"break",""},
    {(-12),"return",""},
    {(-11),"goto",""},
    {(-10),"static",""},
    {(-9),"empty",""},
    {(-8),"function","t"},
    {(-7),"union",""},
    {(-6),"struct","vt"},
    {(-5),"array","tv"},
    {(-4),"pointer","t"},
    {(-3),"unsigned",""},
    {(-2),"char",""},
    {(-1),"int",""},

    {1,"gvar","vs"},
    {2,"rgvar","vs"},
    {3,"crgvar","vs"},
    {4,"lvar","v"},
    {5,"rlvar","v"},
    {6,"crlvar","v"},
    {7,"const","v"},
    {8,"fname","n"},
    {9,"*","e"},
    {10,"rindirect","e"},
    {11,"crindirect","e"},
    {12,"&","e"},
    {13,"-","e"},
    {14,"!","e"},
    {15,"~","e"},
    {16,"++",""},
    {17,"postinc","e"},
    {18,"preinc","e"},
    {19,"cpostinc","e"},
    {20,"cpreinc","e"},
    {21,"--",""},
    {22,"cpostdec","e"},
    {23,"cpredec","e"},
    {24,"*","ee"},
    {25,"*","ee"},
    {26,"/","ee"},
    {27,"/","ee"},
    {28,"%","ee"},
    {29,"%","ee"},
    {30,"+","ee"},
    {31,"-","ee"},
    {32,">>","ee"},
    {33,">>","ee"},
    {34,"<<","ee"},
    {35,"<<","ee"},
    {36,">","ee"},
    {37,">","ee"},
    {38,">=","ee"},
    {39,">=","ee"},
    {40,"<","ee"},
    {41,"<","ee"},
    {42,"<=","ee"},
    {43,"<=","ee"},
    {44,"==","ee"},
    {45,"!=","ee"},
    {46,"&","ee"},
    {47,"^","ee"},
    {48,"|","ee"},
    {49,"&&","ee"},
    {50,"||","ee"},
    {51,"cond","eee"},
    {52," = ","ee"},
    {53," = ","ee"},
    {54,"assop","eev"},
    {55,"cassop","eev"},
    {56,",","ee"},
    {57,"(",""},
    {58,")",""},
    {59,"[",""},
    {60,"]",""},
    {61,"{",""},
    {62,"}",""},
    {63,":","ee"},
    {64,";",""},
    {65,".",""},
    {66,"->",""},
    {67,"cname",""},
    {68,"sass",""},
    {69,"rstruct",""},
    {00,"=",""},
    {AS+24,"*=","ee"},
    {AS+25,"*=","ee"},
    {AS+26,"/=","ee"},
    {AS+27,"/=","ee"},
    {AS+28,"%=","ee"},
    {AS+29,"%=","ee"},
    {AS+30,"+=","ee"},
    {AS+31,"-=","ee"},
    {AS+32,">>=","ee"},
    {AS+33,">>=","ee"},
    {AS+34,"<<=","ee"},
    {AS+35,"<<=","ee"},
    {AS+46,"&=","ee"},
    {AS+47,"^=","ee"},
    {AS+48,"|=","ee"},
};

void
tree_print_t(int e,int t)
{
    printf("# type: ");
    tree_print(t);
    printf("expr: ");
    tree_print(e);
    printf("\n");
}

void
tree_print(int e)
{
    printf("* generate code on type:\n* ");
    tree_parse(type);
    printf("\n* expr:\n* ");
    tree_parse(e);
    printf("\n");
}

static
int tree_level;

void
tree_parse(int e)
{
    tree_node_type *t;
    int i,j;
    char *s;

    if(e<0) {
	t=find_node(e);
	if(t->tree_type==e) {
	    for(j=0;j<tree_level;j++) putchar(' ');
	    printf("list(%s)",t->tree_name);
	}
    } else {
	i = car(e);
	t=find_node(e);
	if(t->tree_type==i) {
	    tree_level++;
	    for(j=0;j<tree_level;j++) putchar(' ');
	    printf("list(%s",t->tree_name);
	    for(i=1,s=t->tree_args;*s;s++,i++) {
		switch(*s) {
		case 'e':
		case 't':
		    printf(",\n*");
		    tree_parse(heap[e+i]); break;
		case 'v':
		    printf(",%d",heap[e+i]); break;
		case 'n':
		    printf(",%s",((NMTBL *)heap[e+i])->nm); break;
		case 's':
		    printf(",%s",(char *)heap[e+i]); break;
		case 'i':
		    printf(",%d",heap[e+i]); break;
		}
	    }
	    tree_level--;
	    printf(")");
	}
    }
}

tree_node_type *
find_node(int e) {
    int e1,e2;
    int first=0;
    int last=sizeof(tree_nodes)/sizeof(tree_node_type);
    e2=-1;
    while (first!=last) {
	e1 = (first+last)/2;
	if(e2==e1) 
	    return 0;
	e2=e1;
	if (tree_nodes[e1].tree_type>e)
	    last = e1;
	else if (tree_nodes[e1].tree_type==e)
	    break;
	else if (tree_nodes[e1].tree_type<e)
	    first = e1;
    }
    return &tree_nodes[e1];
}

void struct_type_print(int type,FILE *out)
{
    NMTBL *n;
    int tags;
    if((n=(NMTBL*)cadddr(type))) {
	fprintf(out,"%s ",n->nm);
	return;
    }
    if((tags=caddr(type))) {
	fprintf(out,"{");
	while(tags) {
	    n=(NMTBL*)caddr(tags);
	    type_print(car(tags),n,out);
	    fprintf(out,";");
	    tags = cadr(tags);
	}
	fprintf(out,"}");
    }
}

void function_type_print1(int type,NMTBL *n,FILE *out,int cont)
{
    int args;
    type_print1(cadr(type),0,out,cont);
    if(n) fprintf(out," %s",n->nm);
    fprintf(out,"(");
    if((args=caddr(type))) {
	while (args) {
	    type_print(car(args),0,out);
	    args=cadr(args);
	    if (args) fprintf(out,",");
	}
    }
    fprintf(out,")");
}

void function_type_print(int type,NMTBL *n,FILE *out)
{
    function_type_print1(type,n,out,0);
}

void sym_print(int sym,FILE *out)
{
    tree_node_type *tn;
    if (!(tn=find_node(sym))) { error(-1); return; }
    fprintf(out,"%s",tn->tree_name);
}

NMTBL *
typedef_search(t,type) 
{
    while(t) {
	if (((NMTBL*)car(t))->ty==type)
	    return (NMTBL*)car(t);
	t=cadr(t);
    }
    return 0;
}

void type_print1(int type,NMTBL *n,FILE *out,int cont)
{
    int t; 
    tree_node_type *tn;
    NMTBL *td;
    int args;

    if(type>0&&(td=typedef_search(typedefed,type))) {
	if (!cont)
	    fprintf(out,"%s ",td->nm);
    } else if(type>0&&(td=typedef_search(gtypedefed,type))) {
	if (!cont)
	    fprintf(out,"%s ",td->nm);
    } else if (type<0) {
	t=type;
	if (!(tn=find_node(t))) { error(-1); return; }
	if (!cont)
	    fprintf(out,"%s ",tn->tree_name);
    } else if ((t=car(type))) {
	if (!(tn=find_node(t))) { error(-1); return; }
	if(t==STRUCT||t==UNION) {
	    if (!cont) {
		fprintf(out,"%s ",tn->tree_name);
		struct_type_print(type,out);
	    }
	} else if(t==CODE) {
	    if (!cont) {
		fprintf(out,"%s ",tn->tree_name);
	    }
	    function_type_print1(type,n,out,cont);
	    return;
	} else if(t==FUNCTION) {
	    function_type_print1(type,n,out,cont);
	    return;
	} else if(t==ARRAY) {
	    type_print1(cadr(type),n,out,cont);
	    if (caddr(type))
		fprintf(out,"[%d]",caddr(type));
	    else
		fprintf(out,"[]");
	    return;
	} else if(t==POINTER) {
	    t=cadr(type);
	    if(car(t)==FUNCTION) {
		type_print1(cadr(t),0,out,cont);
		fprintf(out,"(*");
		if(n) fprintf(out,"%s",n->nm);
		fprintf(out,")");
		fprintf(out,"(");
		if((args=caddr(t))) {
		    while (args) {
			type_print(car(args),0,out);
			args=cadr(args);
			if (args) fprintf(out,",");
		    }
		}
		fprintf(out,")");
		return;
	    } else if(car(t)==ARRAY) {
		fprintf(out,"(*");
		type_print(cadr(t),n,out);
		if (caddr(type))
		    fprintf(out,")[%d]",caddr(type));
		else
		    fprintf(out,")[]");
		return;
	    } else {
		type_print1(t,0,out,cont);
		fprintf(out,"*");
	    }
	}
    }
    if(n) fprintf(out,"%s",n->nm);
}

void type_print(int type,NMTBL *n,FILE *out)
{
    type_print1(type,n,out,0);
}

/* end */