void dopsddef(void) { int numericarg = 0; INTPTR_T dummy; INTPTR_T dummy_array[16]; int i; if (amatch("pal",3)) { if (!match("(")) error("missing ("); readstr(); /* read the label name */ addglb_far(litq2, CINT); if (!match(",")) { error("missing ',' in #defpal"); kill_line(); return; } numericarg = 0; while (!match(")")) { number(&dummy_array[numericarg]); numericarg++; if (numericarg>16) error("No more than 16 colors can be defined at once"); match(","); } ol(".data"); prefix(); outstr(litq2); outstr(":"); ot(".defpal "); for (i = 0; i < numericarg; i++) { outhexfix(dummy_array[i],3); if (i < numericarg - 1) { outstr(","); if (i == 7) { outstr(" \\\n"); ot("\t"); } } } newl(); ol(".code"); kill_line(); } else if (amatch("chr",3)) { if (!match("(")) error("missing ("); ol(".data"); readstr(); /* read the label name */ prefix(); outstr(litq2); outstr(":"); addglb_far(litq2, CINT); if (!match(",")) { error("missing ,"); kill_line(); return; } ot(".defchr "); numericarg = 0; while (!match(")")) { numericarg++; number(&dummy); switch (numericarg) { case 1: outhexfix(dummy,4); outstr(","); break; case 2: outdec(dummy); outstr(",\\"); newl(); break; case 10: outhexfix(dummy,8); break; default: outhexfix(dummy,8); outstr(",\\"); newl(); } match(","); } newl(); ol(".code"); if (numericarg!=10) error("You must enter the VRAM address, the default palette and 8 values for pattern"); kill_line(); } else if (amatch("spr",3)) { if (!match("(")) error("missing ("); ol(".data"); readstr(); /* read the label name */ prefix(); outstr(litq2); outstr(":"); addglb_far(litq2, CINT); if (!match(",")) { error("missing ,"); kill_line(); return; } ot(".defspr "); numericarg = 0; while (!match(")")) { numericarg++; number(&dummy); switch (numericarg) { case 1: outhexfix(dummy,4); outstr(","); break; case 2: outdec(dummy); outstr(",\\"); newl(); break; case 34: outhexfix(dummy,8); break; default: outhexfix(dummy,8); outstr(","); if (!(numericarg & 1)) { outstr("\\"); newl(); } } match(","); } newl(); ol(".code"); if (numericarg!=34) error("You must enter the VRAM address, the default palette and 32 values for pattern"); kill_line(); } else { error("Unknown define directive"); kill_line(); } return; }
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); }
void dopsdinc(void) { INTPTR_T dummy; /* Used in the qstr function, I don't know its utility yet */ int numericarg = 0; /* Number of numeric arg to test validity */ if (amatch("pal",3)) { if (!match("(")) error("missing ("); ol(".data"); ol(".dw $0"); readstr(); /* read the label name */ prefix(); outstr(litq2); outstr(":\n"); addglb_far(litq2, CINT); if (!match(",")) { error("missing ,"); kill_line(); return; } ot(".incpal \""); if (readqstr() == 0) /* read the filename */ { error("bad filename in incpal"); kill_line(); return; } outstr(litq2); outstr("\""); if (match(",")) outstr(","); numericarg = 0; while (!match(")")) { numericarg++; number(&dummy); outdec(dummy); if (match(",")) outstr(","); } newl(); ol(".code"); if (numericarg>2) error("Maximum 2 numeric arg for incpal(name,\"filename\" [,start_pal] [,nb_pal])"); kill_line(); } else if (amatch("bin",3)) { if (!match("(")) error("missing ("); ol(".data"); ol(".dw $0"); readstr(); /* read the label name */ prefix(); outstr(litq2); outstr(":\n"); addglb_far(litq2, CCHAR); if (!match(",")) { error("missing ,"); kill_line(); return; } ot(".incbin \""); if (readqstr() == 0) /* read the filename */ { error("bad filename in incbin"); kill_line(); return; } outstr(litq2); outstr("\"\n"); if (!match(")")) error("missing )"); newl(); ol(".code"); kill_line(); } else if (amatch("bat",3)) { if (!match("(")) error("missing ("); ol(".data"); ol(".dw $0"); readstr(); /* read the label name */ prefix(); outstr(litq2); outstr(":\n"); addglb_far(litq2, CINT); if (!match(",")) { error("missing ,"); kill_line(); return; } ot(".incbat \""); if (readqstr() == 0) { error("bad filename in incbat"); kill_line(); return; } outstr(litq2); outstr("\""); if (match(",")) outstr(","); numericarg = 0; while (!match(")")) { numericarg++; number(&dummy); outdec(dummy); if (match(",")) outstr(","); } newl(); ol(".code"); if ((numericarg!=1) && (numericarg!=3) && (numericarg!=5)) error("Either 1,3 or 5 numeric arguments are needed for incbat statement"); kill_line(); } else if (amatch("spr",3)) { if (!match("(")) error("missing ("); ol(".data"); ol(".dw $0"); readstr(); /* read the label name */ prefix(); outstr(litq2); outstr(":\n"); addglb_far(litq2, CINT); if (!match(",")) { error("missing ,"); kill_line(); return; } ot(".incspr \""); if (readqstr() == 0) { error("bad filename in incspr"); kill_line(); return; } outstr(litq2); outstr("\""); if (match(",")) outstr(","); numericarg = 0; while (!match(")")) { numericarg++; number(&dummy); outdec(dummy); if (match(",")) outstr(","); } newl(); ol(".code"); if ((numericarg!=0) && (numericarg!=2) && (numericarg!=4)) error("Either 0,2 or 4 numeric arguments are needed for incspr statement"); kill_line(); } else if (amatch("chr",3)) { if (!match("(")) error("missing ("); ol(".data"); ol(".dw $0800"); readstr(); /* read the label name */ prefix(); outstr(litq2); outstr(":\n"); addglb_far(litq2, CINT); if (!match(",")) { error("missing ,"); kill_line(); return; } ot(".incchr \""); if (readqstr() == 0) { error("bad filename in incchr"); kill_line(); return; } outstr(litq2); outstr("\""); if (match(",")) outstr(","); numericarg = 0; while (!match(")")) { numericarg++; number(&dummy); outdec(dummy); if (match(",")) outstr(","); } newl(); ol(".code"); if ((numericarg!=0) && (numericarg!=2) && (numericarg!=4)) error("Either 0,2 or 4 numeric arguments are needed for incchr statement"); kill_line(); } else if (amatch("chr_ex",6)) { do_inc_ex(8); } else if (amatch("tile",4)) { if (!match("(")) error("missing ("); ol(".data"); ol(".dw $1000"); readstr(); /* read the label name */ prefix(); outstr(litq2); outstr(":\n"); addglb_far(litq2, CINT); if (!match(",")) { error("missing ,"); kill_line(); return; } ot(".inctile \""); if (readqstr() == 0) { error("bad filename in inctile"); kill_line(); return; } outstr(litq2); outstr("\""); if (match(",")) outstr(","); numericarg = 0; while (!match(")")) { numericarg++; number(&dummy); outdec(dummy); if (match(",")) outstr(","); } newl(); ol(".code"); if ((numericarg!=0) && (numericarg!=2) && (numericarg!=4)) error("Either 0,2 or 4 numeric arguments are needed for inctile statement"); kill_line(); } else if (amatch("tile_ex",7)) { do_inc_ex(16); } else { error("Unknown include directive"); kill_line(); } return; }