view mc-tree.c @ 68:0266905063b5

*** empty log message ***
author kono
date Mon, 24 Feb 2003 02:15:15 +0900
parents 254a0c576114
children dba8d111b7a0
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 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,"gt","ee"},
    {37,"ugt","ee"},
    {38,"ge","ee"},
    {39,"uge","ee"},
    {40,"lt","ee"},
    {41,"ult","ee"},
    {42,"le","ee"},
    {43,"ule","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",""},
    {100,"=",""},
    {124,"*=","ee"},
    {125,"*=","ee"},
    {126,"/=","ee"},
    {127,"/=","ee"},
    {128,"%=","ee"},
    {129,"%=","ee"},
    {130,"+=","ee"},
    {131,"-=","ee"},
    {132,">>=","ee"},
    {133,">>=","ee"},
    {134,"<<=","ee"},
    {135,"<<=","ee"},
    {146,"&=","ee"},
    {147,"^=","ee"},
    {148,"|=","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_print(int type,NMTBL *n,FILE *out)
{
    int args;
    type_print(cadr(type),0,out);
    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 sym_print(int sym,FILE *out)
{
    tree_node_type *tn;
    if (!(tn=find_node(sym))) { error(-1); return; }
    fprintf(out,"%s",tn->tree_name);
}

void type_print(int type,NMTBL *n,FILE *out)
{
    int t; 
    tree_node_type *tn;
    if (type<0) {
	t=type;
	if (!(tn=find_node(t))) { error(-1); return; }
	fprintf(out,"%s ",tn->tree_name);
    } else if ((t=car(type))) {
	if (!(tn=find_node(t))) { error(-1); return; }
	if(t==STRUCT||t==UNION) {
	    fprintf(out,"%s ",tn->tree_name);
	    struct_type_print(type,out);
	} else if(t==CODE) {
	    fprintf(out,"%s ",tn->tree_name);
	    function_type_print(type,n,out);
	    return;
	} else if(t==FUNCTION) {
	    function_type_print(type,n,out);
	    return;
	} else if(t==ARRAY) {
	    type_print(cadr(type),n,out);
	    fprintf(out,"[%d]",caddr(type));
	    return;
	} else if(t==POINTER) {
	    t=cadr(type);
	    if(car(t)==FUNCTION) {
		fprintf(out,"(");
		type_print(t,0,out);
		fprintf(out,")");
	    } else
		type_print(t,0,out);
	    fprintf(out,"*");
	}
    }
    if(n) fprintf(out,"%s",n->nm);
}

/* end */