#define ULAS_PPSTR_ENDMACRO "#endmacro"
#define ULAS_PPSTR_UNDEF "#undefine"
+#define ULAS_ASMSTR_ORG ".org"
+#define ULAS_ASMSTR_SET ".set"
+#define ULAS_ASMSTR_BYTE ".db"
+#define ULAS_ASMSTR_STR ".str"
+#define ULAS_ASMSTR_FILL ".fill"
+#define ULAS_ASMSTR_PAD ".pad"
+#define ULAS_ASMSTR_INCBIN ".incbin"
+
// configurable tokens
#define ULAS_TOK_COMMENT ';'
// start of as directives such as .org
* asm
*/
enum ulas_asmdir {
+ ULAS_ASMDIR_NONE = 0,
// .org <address>
ULAS_ASMDIR_ORG,
// .set name = <expr>
goto cleanup;
}
- fseek(preprocdst, 0, SEEK_SET);
- rc = ulas_asm(ulasout, preprocdst);
-
cleanup:
if (!cfg.preproc_only) {
fclose(preprocdst);
}
int ulas_preproc(FILE *dst, FILE *src) {
+ FILE *asmsrc = dst;
char buf[ULAS_LINEMAX];
memset(buf, 0, ULAS_LINEMAX);
int rc = 0;
// init
struct ulas_preproc pp = ulas_preprocinit();
+ long prevseek = 0;
// preproc
while ((rc = ulas_preprocnext(&pp, dst, src, buf, ULAS_LINEMAX)) > 0) {
+ if (ulascfg.preproc_only) {
+ continue;
+ }
+
+ // after each preproc line we assembly it by reading it back
+ // from the temporary buffer
+ fseek(asmsrc, prevseek, SEEK_SET);
+ if (ulas_asm(ulasout, asmsrc) == -1) {
+ rc = -1;
+ goto fail;
+ }
+ prevseek = ftell(asmsrc);
}
+fail:
// cleanup
ulas_preprocfree(&pp);
ulas_tok(&ulas.tok, &line, n);
if (ulas.tok.buf[0] == ULAS_TOK_ASMDIR_BEGIN) {
- // is directive!
- puts("Directive");
+ const char *dirstrs[] = {
+ ULAS_ASMSTR_ORG, ULAS_ASMSTR_SET, ULAS_ASMSTR_BYTE, ULAS_ASMSTR_STR,
+ ULAS_ASMSTR_FILL, ULAS_ASMSTR_PAD, ULAS_ASMSTR_INCBIN, NULL};
+ enum ulas_asmdir dirs[] = {
+ ULAS_ASMDIR_ORG, ULAS_ASMDIR_SET, ULAS_ASMDIR_BYTE, ULAS_ASMDIR_STR,
+ ULAS_ASMDIR_FILL, ULAS_ASMDIR_PAD, ULAS_ASMDIR_INCBIN};
+
+ enum ulas_asmdir dir = ULAS_ASMDIR_NONE;
+
+ for (long i = 0; dirstrs[i]; i++) {
+ if (strncmp(ulas.tok.buf, dirstrs[i], n) == 0) {
+ dir = dirs[i];
+ break;
+ }
+ }
+
+ if (!dir) {
+ ULASERR("Unexpected directive\n");
+ rc = -1;
+ goto fail;
+ }
+
} else {
// is regular line in form of [label:] instruction ; comment
}
+fail:
return rc;
}
int ulas_asmnext(FILE *dst, FILE *src, char *buf, int n) {
int rc = 1;
if (fgets(buf, n, src) != NULL) {
- ulas.line++;
-
size_t buflen = strlen(buf);
if (ulas_asmline(dst, src, buf, buflen) == -1) {
rc = -1;
memset(buf, 0, ULAS_LINEMAX);
int rc = 0;
- // init
- struct ulas_preproc pp = ulas_preprocinit();
-
// preproc
while ((rc = ulas_asmnext(dst, src, buf, ULAS_LINEMAX)) > 0) {
}
- // cleanup
- ulas_preprocfree(&pp);
-
return rc;
}