Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

main.c

Go to the documentation of this file.
00001 #include "c.h"
00002 
00003 static char rcsid[] = "main.c - faked rcsid";
00004 
00005 static void typestab(Symbol, void *);
00006 
00007 static void stabline(Coordinate *);
00008 static void stabend(Coordinate *, Symbol, Coordinate **, Symbol *, Symbol *);
00009 Interface *IR = NULL;
00010 
00011 int Aflag;      /* >= 0 if -A specified */
00012 int Pflag;      /* != 0 if -P specified */
00013 int glevel;     /* == [0-9] if -g[0-9] specified */
00014 int xref;       /* != 0 for cross-reference data */
00015 Symbol YYnull;      /* _YYnull  symbol if -n or -nvalidate specified */
00016 Symbol YYcheck;     /* _YYcheck symbol if -nvalidate,check specified */
00017 
00018 static char *comment;
00019 static Interface stabIR;
00020 static char *currentfile;       /* current file name */
00021 static int currentline;     /* current line number */
00022 static FILE *srcfp;     /* stream for current file, if non-NULL */
00023 static int srcpos;      /* position of srcfp, if srcfp is non-NULL */
00024 int main(int argc, char *argv[]) {
00025     int i, j;
00026     for (i = argc - 1; i > 0; i--)
00027         if (strncmp(argv[i], "-target=", 8) == 0)
00028             break;
00029     if (i > 0) {
00030         char *s = strchr(argv[i], '\\');
00031         if (s != NULL)
00032             *s = '/';
00033         for (j = 0; bindings[j].name && bindings[j].ir; j++)
00034             if (strcmp(&argv[i][8], bindings[j].name) == 0) {
00035                 IR = bindings[j].ir;
00036                 break;
00037             }
00038         if (s != NULL)
00039             *s = '\\';
00040     }
00041     if (!IR) {
00042         fprint(stderr, "%s: unknown target", argv[0]);
00043         if (i > 0)
00044             fprint(stderr, " `%s'", &argv[i][8]);
00045         fprint(stderr, "; must specify one of\n");
00046         for (i = 0; bindings[i].name; i++)
00047             fprint(stderr, "\t-target=%s\n", bindings[i].name);
00048         exit(EXIT_FAILURE);
00049     }
00050     init(argc, argv);
00051     t = gettok();
00052     (*IR->progbeg)(argc, argv);
00053     if (glevel && IR->stabinit)
00054         (*IR->stabinit)(firstfile, argc, argv);
00055     program();
00056     if (events.end)
00057         apply(events.end, NULL, NULL);
00058     memset(&events, 0, sizeof events);
00059     if (glevel || xref) {
00060         Symbol symroot = NULL;
00061         Coordinate src;
00062         foreach(types,       GLOBAL, typestab, &symroot);
00063         foreach(identifiers, GLOBAL, typestab, &symroot);
00064         src.file = firstfile;
00065         src.x = 0;
00066         src.y = lineno;
00067         if ((glevel > 2 || xref) && IR->stabend)
00068             (*IR->stabend)(&src, symroot,
00069                 ltov(&loci,    PERM),
00070                 ltov(&symbols, PERM), NULL);
00071         else if (IR->stabend)
00072             (*IR->stabend)(&src, NULL, NULL, NULL, NULL);
00073     }
00074     finalize();
00075     (*IR->progend)();
00076     deallocate(PERM);
00077     return errcnt > 0;
00078 }
00079 /* main_init - process program arguments */
00080 void main_init(int argc, char *argv[]) {
00081     char *infile = NULL, *outfile = NULL;
00082     int i;
00083     static int inited;
00084 
00085     if (inited)
00086         return;
00087     inited = 1;
00088     type_init(argc, argv);
00089     for (i = 1; i < argc; i++)
00090         if (strcmp(argv[i], "-g") == 0 || strcmp(argv[i], "-g2") == 0)
00091             glevel = 2;
00092         else if (strncmp(argv[i], "-g", 2) == 0) {  /* -gn[,x] */
00093             char *p = strchr(argv[i], ',');
00094             glevel = atoi(argv[i]+2);
00095             if (p) {
00096                 comment = p + 1;
00097                 if (glevel == 0)
00098                     glevel = 1;
00099                 if (stabIR.stabline == NULL) {
00100                     stabIR.stabline = IR->stabline;
00101                     stabIR.stabend = IR->stabend;
00102                     IR->stabline = stabline;
00103                     IR->stabend = stabend;
00104                 }
00105             }   
00106         } else if (strcmp(argv[i], "-x") == 0)
00107             xref++;
00108         else if (strcmp(argv[i], "-A") == 0) {
00109             ++Aflag;
00110         } else if (strcmp(argv[i], "-P") == 0)
00111             Pflag++;
00112         else if (strcmp(argv[i], "-w") == 0)
00113             wflag++;
00114         else if (strcmp(argv[i], "-n") == 0) {
00115             if (!YYnull) {
00116                 YYnull = install(string("_YYnull"), &globals, GLOBAL, PERM);
00117                 YYnull->type = func(voidptype, NULL, 1);
00118                 YYnull->sclass = EXTERN;
00119                 (*IR->defsymbol)(YYnull);
00120             }
00121         } else if (strncmp(argv[i], "-n", 2) == 0) {    /* -nvalid[,check] */
00122             char *p = strchr(argv[i], ',');
00123             if (p) {
00124                 YYcheck = install(string(p+1), &globals, GLOBAL, PERM);
00125                 YYcheck->type = func(voidptype, NULL, 1);
00126                 YYcheck->sclass = EXTERN;
00127                 (*IR->defsymbol)(YYcheck);
00128                 p = stringn(argv[i]+2, p - (argv[i]+2));
00129             } else
00130                 p = string(argv[i]+2);
00131             YYnull = install(p, &globals, GLOBAL, PERM);
00132             YYnull->type = func(voidptype, NULL, 1);
00133             YYnull->sclass = EXTERN;
00134             (*IR->defsymbol)(YYnull);
00135         } else if (strcmp(argv[i], "-v") == 0)
00136             fprint(stderr, "%s %s\n", argv[0], rcsid);
00137         else if (strncmp(argv[i], "-s", 2) == 0)
00138             density = strtod(&argv[i][2], NULL);
00139         else if (strncmp(argv[i], "-errout=", 8) == 0) {
00140             FILE *f = fopen(argv[i]+8, "w");
00141             if (f == NULL) {
00142                 fprint(stderr, "%s: can't write errors to `%s'\n", argv[0], argv[i]+8);
00143                 exit(EXIT_FAILURE);
00144             }
00145             fclose(f);
00146             f = freopen(argv[i]+8, "w", stderr);
00147             assert(f);
00148         } else if (strncmp(argv[i], "-e", 2) == 0) {
00149             int x;
00150             if ((x = strtol(&argv[i][2], NULL, 0)) > 0)
00151                 errlimit = x;
00152         } else if (strncmp(argv[i], "-little_endian=", 15) == 0)
00153             IR->little_endian = argv[i][15] - '0';
00154         else if (strncmp(argv[i], "-mulops_calls=", 18) == 0)
00155             IR->mulops_calls = argv[i][18] - '0';
00156         else if (strncmp(argv[i], "-wants_callb=", 13) == 0)
00157             IR->wants_callb = argv[i][13] - '0';
00158         else if (strncmp(argv[i], "-wants_argb=", 12) == 0)
00159             IR->wants_argb = argv[i][12] - '0';
00160         else if (strncmp(argv[i], "-left_to_right=", 15) == 0)
00161             IR->left_to_right = argv[i][15] - '0';
00162         else if (strncmp(argv[i], "-wants_dag=", 11) == 0)
00163             IR->wants_dag = argv[i][11] - '0';
00164         else if (*argv[i] != '-' || strcmp(argv[i], "-") == 0) {
00165             if (infile == NULL)
00166                 infile = argv[i];
00167             else if (outfile == NULL)
00168                 outfile = argv[i];
00169         }
00170 
00171     if (infile != NULL && strcmp(infile, "-") != 0
00172     && freopen(infile, "r", stdin) == NULL) {
00173         fprint(stderr, "%s: can't read `%s'\n", argv[0], infile);
00174         exit(EXIT_FAILURE);
00175     }
00176     if (outfile != NULL && strcmp(outfile, "-") != 0
00177     && freopen(outfile, "w", stdout) == NULL) {
00178         fprint(stderr, "%s: can't write `%s'\n", argv[0], outfile);
00179         exit(EXIT_FAILURE);
00180     }
00181 }
00182 /* typestab - emit stab entries for p */
00183 static void typestab(Symbol p, void *cl) {
00184     if (*(Symbol *)cl == 0 && p->sclass && p->sclass != TYPEDEF)
00185         *(Symbol *)cl = p;
00186     if ((p->sclass == TYPEDEF || p->sclass == 0) && IR->stabtype)
00187         (*IR->stabtype)(p);
00188 }
00189 
00190 /* stabline - emit source code for source coordinate *cp */
00191 static void stabline(Coordinate *cp) {
00192     if (cp->file && cp->file != currentfile) {
00193         if (srcfp)
00194             fclose(srcfp);
00195         currentfile = cp->file;
00196         srcfp = fopen(currentfile, "r");
00197         srcpos = 0;
00198         currentline = 0;
00199     }
00200     if (currentline != cp->y && srcfp) {
00201         char buf[512];
00202         if (srcpos > cp->y) {
00203             rewind(srcfp);
00204             srcpos = 0;
00205         }
00206         for ( ; srcpos < cp->y; srcpos++)
00207             if (fgets(buf, sizeof buf, srcfp) == NULL) {
00208                 fclose(srcfp);
00209                 srcfp = NULL;
00210                 break;
00211             }
00212         if (srcfp && srcpos == cp->y)
00213             print("%s%s", comment, buf);
00214     }
00215     currentline = cp->y;
00216     if (stabIR.stabline)
00217         (*stabIR.stabline)(cp);
00218 }
00219 
00220 static void stabend(Coordinate *cp, Symbol p, Coordinate **cpp, Symbol *sp, Symbol *stab) {
00221     if (stabIR.stabend)
00222         (*stabIR.stabend)(cp, p, cpp, sp, stab);
00223     if (srcfp)
00224         fclose(srcfp);
00225 }

Generated on Thu Aug 25 12:38:14 2005 for Quake III Arena by  doxygen 1.3.9.1