#include #include #include #include #include #include "c.h" #include "rcc.h" #if WIN32 #include #include #endif static void do_int(int x) { printf("%d", x); } static void do_scope(int x) { #define xx(c) if (x == c) { printf(#c); return; } xx(CONSTANTS) xx(LABELS) xx(GLOBAL) xx(PARAM) xx(LOCAL) #undef xx if (x > LOCAL) printf("LOCAL+%d", x-LOCAL); else do_int(x); } static void do_sclass(int x) { #define xx(c) if (x == c) { printf(#c); return; } xx(REGISTER) xx(AUTO) xx(EXTERN) xx(STATIC) xx(TYPEDEF) #undef xx do_int(x); } static void do_flags(int x) { char *bar = ""; #define xx(f,n) if ((x>>n)&1) { printf("%s" #f, bar); bar = "|"; } xx(structarg,0) xx(addressed,1) xx(computed,2) xx(temporary,3) xx(generated,4) #undef xx if (*bar == '\0') do_int(x); } static void do_seg(int x) { #define xx(s) if (x == s) { printf(#s); return; } xx(CODE) xx(BSS) xx(DATA) xx(LIT) #undef xx do_int(x); } #define xx(ptr,field,type) do { printf("
  • " #field " = "); do_##type(ptr->field); printf("
  • \n"); } while (0) static void do_op(int x) { static char *opnames[] = { "", "CNST", "ARG", "ASGN", "INDIR", "CVC", "CVD", "CVF", "CVI", "CVP", "CVS", "CVU", "NEG", "CALL", "*LOAD*", "RET", "ADDRG", "ADDRF", "ADDRL", "ADD", "SUB", "LSH", "MOD", "RSH", "BAND", "BCOM", "BOR", "BXOR", "DIV", "MUL", "EQ", "GE", "GT", "LE", "LT", "NE", "JUMP", "LABEL", "AND", "NOT", "OR", "COND", "RIGHT", "FIELD" }; int op = opindex(x); if (op < 1 || op >= sizeof opnames/sizeof opnames[0]) printf("%d", x); else printf("%s", opnames[op]); } static void do_define_uid(int x) { printf("%d", x, x); } static void do_define_label(int x) { printf("%d", x, x); } static void do_uid(int x) { printf("%d", x, x, x); } static void do_label(int x) { printf("%d", x, x, x); } static int nextid; static void do_list(list_ty x, void do_one(void *), char *type, char *listhtml, char *separator) { int count = Seq_length(x); if (count == 0) printf("empty %s list\n", type); else { int i; printf("%s list", type); if (listhtml != NULL) printf("<%s>\n", listhtml); for (i = 0; i < count; i++) { if (listhtml != NULL) printf("
  • "); printf(separator); do_one(Seq_get(x, i)); if (listhtml != NULL) printf("
  • \n"); } if (listhtml != NULL) printf("\n", listhtml); } } static void do_uid_list(list_ty x) { int i, count = Seq_length(x); if (count == 0) printf("empty int list\n"); else { int i; printf("int list"); for (i= 0; i < count; i++) { printf(" "); do_uid(*(int *)Seq_get(x, i)); } } } static void do_identifier(const char *x) { printf("%s", x); } static void do_real(rcc_real_ty x) { double d; unsigned *p = (unsigned *)&d; static union { int x; char endian; } little = { 1 }; p[1-little.endian] = x->msb; p[little.endian] = x->lsb; printf("(%#X,%#X) = %g", x->msb, x->lsb, d); } static void do_suffix(int x) { static char suffixes[] = "0F234IUPVB"; if (x < 0 || x >= (sizeof suffixes/sizeof suffixes[0]) - 1) printf("%d", x); else printf("%c", suffixes[x]); } static void do_enum(void *x) { rcc_enum__ty e = x; do_identifier(e->id); printf("="); do_int(e->value); } static void do_enum_list(list_ty x) { do_list(x, do_enum, "enum", NULL, " "); } static void do_field(void *x) { rcc_field_ty f = x; printf("field
      \n"); xx(f,id,identifier); xx(f,type,uid); xx(f,offset,int); xx(f,bitsize,int); xx(f,lsb,int); printf("
    \n"); } static void do_field_list(list_ty x) { do_list(x, do_field, "field", "ol", ""); } static void do_symbol(rcc_symbol_ty x) { printf("symbol
      \n"); xx(x,id,identifier); xx(x,type,uid); xx(x,scope,scope); xx(x,sclass,sclass); xx(x,ref,int); xx(x,flags,flags); printf("
    \n"); } #define caselabel(kind) case rcc_##kind##_enum: \ printf("" #kind " : %s", typename); \ printf("
      \n"); attributes #define yy(kind,field,type) xx((&x->v.rcc_##kind),field,type) static void do_type(rcc_type_ty x) { #define attributes xx(x,size,int); xx(x,align,int) switch (x->kind) { static char *typename = "type"; caselabel(INT); break; caselabel(UNSIGNED); break; caselabel(FLOAT); break; caselabel(VOID); break; caselabel(POINTER); yy(POINTER,type,uid); break; caselabel(ENUM); yy(ENUM,tag,identifier); yy(ENUM,ids,enum_list); break; caselabel(STRUCT); yy(STRUCT,tag,identifier); yy(STRUCT,fields,field_list); break; caselabel(UNION); yy(UNION,tag,identifier); yy(UNION,fields,field_list); break; caselabel(ARRAY); yy(ARRAY,type,uid); break; caselabel(FUNCTION); yy(FUNCTION,type,uid); yy(FUNCTION,formals,uid_list); break; caselabel(CONST); yy(CONST,type,uid); break; caselabel(VOLATILE); yy(VOLATILE,type,uid); break; default: assert(0); } #undef attributes printf("
    \n"); } static void do_item(rcc_item_ty x) { printf("", x->uid); #define attributes xx(x,uid,define_uid) printf(""); switch (x->kind) { static char *typename = "item"; caselabel(Symbol); yy(Symbol,symbol,symbol); break; caselabel(Type); yy(Type,type,type); break; default: assert(0); } #undef attributes printf("\n"); } static void do_item_list(list_ty x) { int count = Seq_length(x); if (count == 0) printf("empty item list\n"); else { int i; printf("item list"); printf("
      \n"); for (i = 0; i < count; i++) { rcc_item_ty item = Seq_get(x, i); printf("
    1. ", item->uid); do_item(item); printf("
    2. \n"); } printf("
    \n"); } } static void do_string(string_ty x) { printf("%d,'%s'", x.len, x.str); } static void do_generic_string(void *x) { do_string(*(string_ty *)x); } static void do_string_list(list_ty x) { do_list(x, do_generic_string, "string", "ol", ""); } static void do_node(void *y) { rcc_node_ty x = y; if (x->kind == rcc_LABEL_enum) printf("", x->v.rcc_LABEL.label); #define attributes xx(x,suffix,suffix); xx(x,size,int) switch (x->kind) { static char *typename = "node"; caselabel(CNST); yy(CNST,value,int); break; caselabel(CNSTF); yy(CNSTF,value,real); break; caselabel(ARG); yy(ARG,left,node); yy(ARG,len,int); yy(ARG,align,int); break; caselabel(ASGN); yy(ASGN,left,node); yy(ASGN,right,node); yy(ASGN,len,int); yy(ASGN,align,int); break; caselabel(CVT); yy(CVT,op,op); yy(CVT,left,node); yy(CVT,fromsize,int); break; caselabel(CALL); yy(CALL,left,node); yy(CALL,type,uid); break; caselabel(CALLB); yy(CALLB,left,node); yy(CALLB,right,node); yy(CALLB,type,uid); break; caselabel(RET); break; caselabel(ADDRG); yy(ADDRG,uid,uid); break; caselabel(ADDRL); yy(ADDRL,uid,uid); break; caselabel(ADDRF); yy(ADDRF,uid,uid); break; caselabel(Unary); yy(Unary,op,op); yy(Unary,left,node); break; caselabel(Binary); yy(Binary,op,op); yy(Binary,left,node); yy(Binary,right,node); break; caselabel(Compare); yy(Compare,op,op); yy(Compare,left,node); yy(Compare,right,node); yy(Compare,label,label); break; caselabel(LABEL); yy(LABEL,label,define_label); break; caselabel(BRANCH); yy(BRANCH,label,label); break; caselabel(CSE); yy(CSE,uid,uid); yy(CSE,node,node); break; default: assert(0); } #undef attributes printf(""); } static void do_node_list(list_ty x) { do_list(x, do_node, "node", "ol", ""); } static void do_interface(void *); static void do_interface_list(list_ty x) { do_list(x, do_interface, "interface", "ol", ""); } static void do_interface(void *y) { rcc_interface_ty x = y; if (x->kind == rcc_Address_enum) printf("", x->v.rcc_Address.uid); else if (x->kind == rcc_Local_enum) printf("", x->v.rcc_Local.uid); #define attributes switch (x->kind) { static char *typename = "interface"; caselabel(Export); yy(Export,p,uid); break; caselabel(Import); yy(Import,p,uid); break; caselabel(Global); yy(Global,p,uid); yy(Global,seg,seg); break; caselabel(Local); yy(Local,uid,define_uid); yy(Local,p,symbol); break; caselabel(Address); yy(Address,uid,define_uid); yy(Address,q,symbol); yy(Address,p,uid); yy(Address,n,int); break; caselabel(Segment); yy(Segment,seg,seg); break; caselabel(Defaddress); yy(Defaddress,p,uid); break; caselabel(Deflabel); yy(Deflabel,label,label); break; caselabel(Defconst); yy(Defconst,suffix,suffix); yy(Defconst,size,int); yy(Defconst,value,int); break; caselabel(Defconstf); yy(Defconstf,size,int); yy(Defconstf,value,real); break; caselabel(Defstring); yy(Defstring,s,string); break; caselabel(Space); yy(Space,n,int); break; caselabel(Function); yy(Function,f,uid); yy(Function,caller,uid_list); yy(Function,callee,uid_list); yy(Function,ncalls,int); yy(Function,codelist,interface_list); break; caselabel(Forest); yy(Forest,nodes,node_list); break; case rcc_Blockbeg_enum: printf("Blockbeg : %s", typename); return; case rcc_Blockend_enum: printf("Blockend : %s", typename); return; default: assert(0); } #undef attributes printf("\n"); } static void do_program(rcc_program_ty x) { printf("
      \n"); xx(x,nuids,int); xx(x,nlabels,int); xx(x,items,item_list); xx(x,interfaces,interface_list); xx(x,argc,int); xx(x,argv,string_list); printf("
    \n"); } int main(int argc, char *argv[]) { int i, version; float stamp = (assert(strstr(rcsid, ",v")), strtod(strstr(rcsid, ",v")+2, NULL)) ; char *infile = NULL, *outfile = NULL; rcc_program_ty pickle; for (i = 1; i < argc; i++) if (*argv[i] != '-' || strcmp(argv[i], "-") == 0) { if (infile == NULL) infile = argv[i]; else if (outfile == NULL) outfile = argv[i]; } if (infile != NULL && strcmp(infile, "-") != 0 && freopen(infile, "rb", stdin) == NULL) { fprintf(stderr, "%s: can't read `%s'\n", argv[0], infile); exit(EXIT_FAILURE); } if (infile == NULL || strcmp(infile, "-") == 0) infile = "Standard Input"; #if WIN32 else _setmode(_fileno(stdin), _O_BINARY); #endif if (outfile != NULL && strcmp(outfile, "-") != 0 && freopen(outfile, "w", stdout) == NULL) { fprintf(stderr, "%s: can't write `%s'\n", argv[0], outfile); exit(EXIT_FAILURE); } version = read_int(stdin); assert(version/100 == (int)stamp); pickle = rcc_read_program(stdin); printf("%s\n" "\n" "\n

    %s

    \n", infile, infile); printf("

    version = %d.%d

    ", version/100, version%100); do_program(pickle); { time_t t; time(&t); printf("
    %s
    \n", ctime(&t)); } printf("\n"); return EXIT_SUCCESS; }