main() { glbptr=startglb; /* clear global symbols */ locptr=startloc; /* clear local symbols */ wqptr=wq; /* clear while queue */ macptr= /* clear the macro pool */ litptr= /* clear literal pool */ sp = /* stack ptr (relative) */ errcnt= /* no errors */ eof= /* not eof yet */ input= /* no input file */ input2= /* or include file */ output= /* no open units */ ncmp= /* no open compound states */ lastst= /* no last statement yet */ quote[1]= 0; /* ...all set to zero.... */ quote[0]='"'; /* fake a quote literal */ cmode=1; /* enable preprocessing */ /* */ /* compiler body */ /* */ ask(); /* get user options */ openout(); /* get an output file */ openin(); /* and initial input file */ header(); /* intro code */ parse(); /* process ALL input */ dumplits(); /* then dump literal pool */ dumpglbs(); /* and all static memory */ errorsummary(); /* summarize errors */ trailer(); /* follow-up code */ closeout(); /* close the output (if any) */ return; /* then exit to system */ }
/** * compile one file if filename is NULL redirect do to stdin/stdout * @param file filename * @return */ void compile(char *file) { if (file == NULL || filename_typeof(file) == 'c') { global_table_index = 0; local_table_index = NUMBER_OF_GLOBALS; while_table_index = 0; tag_table_index = 0; inclsp = iflevel = skiplevel = swstp = litptr = stkp = errcnt = ncmp = lastst = //quote[1] = 0; input2 = -1; //quote[0] = '"'; cmode = 1; glbflag = 1; nxtlab = 0; litlab = getlabel(); defmac("end\tmemory"); //add_global("memory", ARRAY, CCHAR, 0, EXTERN); //add_global("stack", ARRAY, CCHAR, 0, EXTERN); rglobal_table_index = global_table_index; //rglbptr = glbptr; //add_global("etext", ARRAY, CCHAR, 0, EXTERN); //add_global("edata", ARRAY, CCHAR, 0, EXTERN); defmac("short\tint"); initmac(); // compiler body if (file == NULL) { input = 0; } else if (!openin(file)) return; if (file == NULL) { output = 1; } else if (!openout()) return; header(); code_segment_gtext(); parse(); close(input); data_segment_gdata(); dumplits(); dumpglbs(); errorsummary(); trailer(); oflush(); close(output); pl(""); errs = errs || errfile; } else { writee("Don't understand file "); writee(file); errs = 1; } }
int main (int argc, char *argv[]) { char *p, *pp, *bp; char **oldargv = argv; char **link_lib; long smacptr; int first = 1; char *asmdefs_global_end; macptr = 0; ctext = 0; argc--; argv++; errs = 0; sflag = 0; cdflag = 0; verboseflag = 0; startup_incl = 0; optimize = 2; /* -O2 by default */ overlayflag = 0; asmdefs[0] = '\0'; while ((p = *argv++)) { if (*p == '-') { while (*++p) switch (*p) { case 't': case 'T': ctext = 1; break; case 'c': if ((*(p + 1) == 'd')) { cdflag = 1; /* pass '-cd' to assembler */ p++; break; } else { usage(oldargv[0]); break; } case 's': if (strncmp(p, "scd", 3) == 0) { cdflag = 2; /* pass '-scd' to assembler */ p += 2; break; } else if (strncmp(p, "sgx", 3) == 0) { strcat(asmdefs, "_SGX = 1\n"); defmac("_SGX"); p += 2; break; } /* fallthrough */ case 'S': sflag = 1; break; /* defines to pass to assembler */ case 'a': if (strncmp(p, "acd", 3) == 0) { cdflag = 2; /* pass '-scd' to assembler */ strcat(asmdefs, "_AC = 1\n"); defmac("_AC"); p += 2; break; } /* fallthrough */ case 'A': bp = ++p; if (!*p) usage(oldargv[0]); while (*p && *p != '=') p++; strncat(asmdefs, bp, (p - bp)); /* if (*p == '=') *p = '\t'; */ bp = ++p; strcat(asmdefs, "\t= "); if (*bp == '\0') strcat(asmdefs, "1\n"); else { strcat(asmdefs, bp); strcat(asmdefs, "\n"); } break; case 'v': verboseflag++; if (verboseflag > 1) ctext = 1; /* "C" code in asm output */ break; case 'd': case 'D': bp = ++p; if (!*p) usage(oldargv[0]); while (*p && *p != '=') p++; if (*p == '=') *p = '\t'; while (*p) p++; p--; defmac(bp); break; case 'o': if (strncmp(p, "over", 4) == 0) { overlayflag = 1; if (strncmp(p, "overlay", 7) == 0) p += 6; else p += 3; } else { bp = ++p; while (*p && *p != ' ' && *p != '\t') p++; memcpy(user_outfile, bp, p - bp); user_outfile[p - bp] = 0; p--; } break; case 'O': /* David, made -O equal to -O2 * I'm too lazy to tape -O2 each time :) */ if (!p[1]) optimize = 2; else optimize = atoi(++p); break; case 'f': p++; if (!strcmp(p, "no-recursive")) { user_norecurse = 1; p += 11; } else if (!strcmp(p, "recursive")) { user_norecurse = 0; p += 8; } else if (!strcmp(p, "no-short-enums")) { user_short_enums = 0; p += 13; } else if (!strcmp(p, "short-enums")) { user_short_enums = 1; p += 10; } else goto unknown_option; break; case 'l': bp = ++p; while (*p && *p != ' ' && *p != '\t') p++; link_libs = realloc(link_libs, (link_lib_ptr + 2) * sizeof(*link_libs)); link_libs[link_lib_ptr] = malloc(p - bp + 1); memcpy(link_libs[link_lib_ptr], bp, p - bp); link_libs[link_lib_ptr][p - bp] = 0; strcat(asmdefs, "LINK_"); strcat(asmdefs, link_libs[link_lib_ptr]); strcat(asmdefs, "\t= 1\n"); link_libs[++link_lib_ptr] = 0; p--; break; case 'm': if (!strcmp(p + 1, "small")) { strcat(asmdefs, "SMALL\t= 1\n"); p += 5; } else { unknown_option: fprintf(stderr, "unknown option %s\n", p); exit(1); } break; default: usage(oldargv[0]); } } else { infiles = realloc(infiles, (infile_ptr + 2) * sizeof(*infiles)); infiles[infile_ptr++] = p; infiles[infile_ptr] = 0; } } smacptr = macptr; if (!infiles) usage(oldargv[0]); printf(HUC_VERSION); printf("\n"); init_path(); /* Remember the first file, it will be used as the base for the output file name unless there is a user-specified outfile. */ p = pp = infiles[0]; /* Labels count is not reset for each file because labels are global and conflicts would arise. */ nxtlab = 0; link_lib = link_libs; infile_ptr = 1; /* Remember where the global assembler defines end so we can reset to that point for each file. */ /* XXX: Even if we don't repeat the local asm defines, they are still defined because we compile everything into one assembly file. */ asmdefs_global_end = asmdefs + strlen(asmdefs); while (p) { errfile = 0; /* Truncate asm defines to the point where global defines end. */ asmdefs_global_end[0] = 0; if (extension(p) == 'c' || extension(p) == 'C') { glbptr = STARTGLB; locptr = STARTLOC; wsptr = ws; inclsp = iflevel = skiplevel = swstp = litptr = stkp = errcnt = ncmp = lastst = quote[1] = const_nb = line_number = 0; macptr = smacptr; input2 = NULL; quote[0] = '"'; cmode = 1; glbflag = 1; litlab = getlabel(); member_table_index = 0; memset(member_table, 0, sizeof(member_table)); tag_table_index = 0; norecurse = user_norecurse; typedef_ptr = 0; enum_ptr = 0; enum_type_ptr = 0; memset(fastcall_tbl, 0, sizeof(fastcall_tbl)); defpragma(); /* Macros and globals have to be reset for each file, so we have to define the defaults all over each time. */ defmac("__end\t__memory"); addglb("__memory", ARRAY, CCHAR, 0, EXTERN, 0); addglb("stack", ARRAY, CCHAR, 0, EXTERN, 0); rglbptr = glbptr; addglb("etext", ARRAY, CCHAR, 0, EXTERN, 0); addglb("edata", ARRAY, CCHAR, 0, EXTERN, 0); /* PCE specific externs */ addglb("font_base", VARIABLE, CINT, 0, EXTERN, 0); addglb_far("vdc", CINT); addglb_far("vram", CCHAR); /* end specific externs */ defmac("huc6280\t1"); defmac("huc\t1"); if (cdflag == 1) defmac("_CD\t1"); else if (cdflag == 2) defmac("_SCD\t1"); else defmac("_ROM\t1"); if (overlayflag == 1) defmac("_OVERLAY\t1"); // initmac(); /* * compiler body */ if (!openin(p)) exit(1); if (first && !openout()) exit(1); if (first) header(); asmdefines(); // gtext (); parse(); fclose(input); // gdata (); dumplits(); dumpglbs(); errorsummary(); // trailer (); pl(""); errs = errs || errfile; } else { fputs("Don't understand file ", stderr); fputs(p, stderr); fputc('\n', stderr); exit(1); } p = infiles[infile_ptr]; if (!p && link_lib && *link_lib) { /* No more command-line files, continue with libraries. */ p = lib_to_file(*link_lib); if (!p) { fprintf(stderr, "cannot find library %s\n", *link_lib); exit(1); } link_lib++; } else infile_ptr++; first = 0; } dumpfinal(); fclose(output); if (!errs && !sflag) { if (user_outfile[0]) errs = errs || assemble(user_outfile); else errs = errs || assemble(pp); } exit(errs != 0); }