home *** CD-ROM | disk | FTP | other *** search
- /*
- * @(#)main.c 1.8 2/23/90
- */
- #include <stdio.h>
- #include "assert.h"
- #include "nodes.h"
- #include "scan.h"
- #include "semantics.h"
- #include "builtins.h"
- #include "environment.h"
- #include "trace.h"
- #include "option.h"
-
- void yyparse();
-
- NodePtr Root;
- #include "tokenNames.c"
-
- char *Usage = "Usage:\tec <flags> <filename>\n\
- \tflags:\n\
- \t\t[-why] [-v] [-c] [-C[-]n] [-z] [-h] [-g[dtm]] [-i] [-Z]\n\
- \t\t[-T<tracename>[=level]] [-O<optionname>[=value]]\n\
- \t\t[-R <rootpath>] [-[Mm] <machinename>]\n\
- \tmaintenance flags:\n\
- \t\t[-b] [-t] [-d <oid>]\n";
-
- Boolean bflag = FALSE,
- tflag = FALSE,
- vflag = FALSE,
- dflag = FALSE,
- oflag = FALSE,
- cflag = FALSE,
- Cflag = FALSE,
- Dflag = FALSE,
- hflag = FALSE,
- zflag = FALSE,
- gdflag= FALSE,
- gtflag= FALSE,
- gmflag= FALSE,
- iflag = FALSE,
- Zflag = FALSE,
- mflag = FALSE;
-
- char *dstring = NULL;
- char *mdata = NULL;
- int Cvalue = -1;
-
- #ifndef EMDIR
- #define EMDIR "/scratch/eric/emerald/"
- #endif
-
- char *emDirectory = EMDIR;
- char *emdbDirectory = NULL;
-
- /*VARARGS1*/
- static void usagePanic(s, a)
- char *s;
- char *a;
- {
- if (s != NULL) {
- fprintf(stderr, s, a);
- putc('\n', stderr);
- }
- fprintf(stderr, Usage);
- exit(1);
- }
-
- parseFlags(argc, argv)
- int argc;
- char **argv;
- {
- register int i;
- char cwd[1024];
-
- for (i = 1; i < argc; i++) {
- if (argv[i][0] == '-') {
- switch (argv[i][1]) {
- case 'b':
- bflag = TRUE;
- break;
- case 'h':
- hflag = TRUE;
- break;
- case 'w':
- if (strcmp(&argv[i][1], "why")) {
- usagePanic("Unknown flag: %s", argv[i]);
- } else {
- if (traceconformfailure == 0) traceconformfailure = 1;
- }
- break;
- case 'v':
- vflag = TRUE;
- if (tracepasses == 0) tracepasses = 1;
- break;
- case 'T':
- if (! parseTraceFlag(argv[i])) usagePanic(NULL);
- break;
- case 'O':
- if (! parseOptionFlag(argv[i])) usagePanic(NULL);
- break;
- case 'o':
- oflag = 1;
- break;
- case 'g':
- switch (argv[i][2]) {
- case 'd':
- gdflag = 1;
- break;
- case 't':
- gtflag = 1;
- break;
- case 'm':
- gmflag = 1;
- break;
- default:
- usagePanic("Unknown flag: %s", argv[i]);
- break;
- }
- break;
- case 't':
- tflag = TRUE;
- break;
- case 'i':
- iflag = TRUE;
- break;
- case 'D':
- if (argv[i][2] == '\0') Dflag = TRUE;
- else Dflag = atoi(&argv[i][2]);
- break;
- case 'd':
- dflag = TRUE;
- dstring = &argv[i][2];
- break;
- case 'c':
- cflag = TRUE;
- break;
- case 'z':
- zflag = TRUE;
- break;
- case 'Z':
- Zflag = TRUE;
- break;
- case 'C':
- Cflag = TRUE;
- if (argv[i][2] != '\0') Cvalue = atoi(&argv[i][2]);
- else Cvalue = -1;
- break;
- case 'm':
- case 'M':
- if (argv[i][2] == '\0') {
- i++;
- if (i < argc) {
- mdata = argv[i];
- } else {
- usagePanic("-M must be followed by machine name", 0);
- }
- } else {
- mdata = &argv[i][2];
- }
- break;
- case 'R':
- if (argv[i][2] == '\0') {
- i++;
- if (i < argc) {
- emDirectory = argv[i];
- } else {
- usagePanic("-R must be followed by directory name", 0);
- }
- } else {
- emDirectory = &argv[i][2];
- }
- break;
- default:
- usagePanic("Unknown flag %s", argv[i]);
- break;
- }
- } else if (currentFileName == NULL) {
- currentFileName = argv[i];
- } else {
- fprintf(stderr, "ec can only compile a single source file\n");
- fprintf(stderr, "\tFirst: \"%s\"\n\tSecond: \"%s\"\n",
- currentFileName, argv[i]);
- usagePanic(NULL);
- }
- }
- if (currentFileName == NULL) {
- inputFile = stdin;
- currentFileName = "<stdin>";
- } else {
- inputFile = fopen(currentFileName, "r");
- if (inputFile == NULL) {
- fprintf(stderr, "Can't find \"%s\".\n", currentFileName);
- exit(1);
- }
- if (getwd(cwd) <= 0) {
- fprintf(stderr, "Can't get current directory: %s\n", cwd);
- exit(1);
- }
- strcat(cwd, "/");
- strcat(cwd, currentFileName);
- currentFileName = (char *)malloc(strlen(cwd)+1);
- strcpy(currentFileName, cwd);
- }
- IFOPTION(debugstack, 1) gdflag = 1;
- }
-
- void checkEnvironmentVariables()
- {
- if ((emdbDirectory = (char *)getenv("EMDBDIRECTORY")) == NULL) {
- emdbDirectory = (char *)malloc((unsigned)strlen(emDirectory) + 5);
- sprintf(emdbDirectory, "%s/EC/", emDirectory);
- }
- }
-
- extern void initialize(), Finalize();
- extern void DisplayTree();
- extern NodePtr readTree();
-
- static OID getIDFromString(s)
- char *s;
- {
- OID id;
- if (sscanf(s, "%*[^D]D%x", &id) != 1) assert(FALSE);
- return(id);
- }
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- umask(0000);
- parseFlags(argc, argv);
- checkEnvironmentVariables();
- if (hflag) {
- printf("%s\n", Usage);
- printf("The Emerald Compiler compiles a single Emerald source program and \n");
- printf("executes it on the local Emerald kernel. The flag arguments are:\n");
- printf(" -why Tell me why conforms checking fails.\n");
- printf(" -v Be verbose about the various compiler passes\n");
- printf(" -c Stop after type checking (except when doing a builtin \n");
- printf(" where it means stop after doing exports)\n");
- printf(" -C[n] Stop after n passes (N >= 0), or with n passes to go\n");
- printf(" (n < 0) -C-2 means stop after generating code, -C-1 means\n");
- printf(" stop before invoking the Emerald kernel to run the program\n");
- printf(" -z Leave the temporary files in the current directory\n");
- printf(" -h Prints this list.\n");
- printf(" -gd Compile code to assist in debugging.\n");
- printf(" -gt Compile code to enable line number tracing.\n");
- printf(" -gm Compile code to gather statistics.\n");
- printf(" -i Interactive - the compiler's stdin/out is passed to the\n");
- printf(" created object.\n");
- printf(" -Z Create mutable objects at compile time.\n");
- printf(" -T<traces> Turn on tracing. The traces argument is parsed as a comma \n");
- printf(" separated string of trace names and optional trace values\n");
- printf(" with an = between the name and value. A legal string is:\n");
- printf(" -Tassign,environment=5\n");
- printf(" Use -Thelp to see the available traces.\n");
- printf(" -O<options> Turn on various options. The options argument is parsed\n");
- printf(" as a comma separated string of option names and optional\n");
- printf(" option values with an = between the name and value.\n");
- printf(" A legal string is:\n");
- printf(" -Oinvokequeue,comment=5\n");
- printf(" Use -Thelp to see the available options.\n");
- printf(" -M <name> Run the compiled program on machine named \"name\".\n");
- printf(" -R <path> Use path instead of /usr/em as the root of the Emerald\n");
- printf(" Unix subtree.\n");
- printf("Maintainance flags:\n");
- printf(" -b Compile the description of a builtin type\n");
- printf(" -t Write the intermediate tree file at interesting \n");
- printf(" stages (currently undergoing change)\n");
- printf(" -d<oid> Read the intermediate tree file (internal format)\n");
- printf(" and display it\n");
- exit(0);
- }
- Root = F_NewNode(T_NONE, 0);
- assert(NodeSize(Root) == BODYSIZE);
- assert(sizeof(STEntry) == 28);
- assert(&((NodePtr)0)->b.oblit.name == &((NodePtr)0)->b.atlit.name);
-
- FreeNode(Root);
- TRACE0(passes, 1, "Initializing.");
- initialize();
- TRACE0(passes, 1, "Loading builtins.");
- loadBuiltins();
- if (loadedDummyBuiltins && !bflag) {
- fprintf(stdout, "Warning: Dummy builtins loaded\n");
- }
- if (dflag) {
- Root = loadObject(getIDFromString(dstring));
- if (!oflag) {
- doInput();
- if (Root->tag == P_OBLIT &&
- (Root->b.oblit.id & 0xff000000) == (OID)0xff000000 &&
- Root->b.oblit.id <= (OID)0xff000100) {
- Root->b.oblit.id = 0xff123000 + (Root->b.oblit.id & 0xff);
- }
- DisplayTree(stdout, Root, vflag ? 1 : 0, 9999);
- } else {
- DisplayGlobalRefs(Root);
- }
- } else {
- TRACE0(passes, 1, "Parsing.");
- yyparse();
- DoneParsing(Root);
- }
- Finalize();
- }
-
- static Map displayMap;
-
- dgr(t)
- register NodePtr t;
- {
- register Symbol st;
- register int i;
- if ((int)t < 0x200) return;
- if (Map_Lookup(displayMap, (int)t) != NIL) return;
- Map_Insert(displayMap, (int)t, 1);
- switch (t->tag) {
- case P_GLOBALREF:
- fprintf(stdout, "0x%8x\n", t->b.globalref.id);
- break;
- case P_SYMREF:
- case P_SYMDEF:
- case P_SYMBOL:
- if (t->tag == P_SYMBOL) {
- st = (Symbol) t;
- } else {
- st = t->b.symref.symbol;
- }
- dgr(st->value.ATinfo);
- dgr(st->value.CTinfo);
- dgr(st->value.value);
- break;
- }
- if (t->tag == P_SYMBOL) return;
- for (i = t->firstChild; i < t->nChildren; i++) {
- dgr(t->b.children[i]);
- }
- }
-
- DisplayGlobalRefs(p)
- NodePtr p;
- {
- displayMap = Map_Create();
- dgr(p);
- Map_Destroy(displayMap);
- }
-