commit f621bed8f9bf20eca167d7cfa40840992c26da09
parent c1e767700fc14fbe385a9a8d9ad0cfa4d5315c35
Author: Louis Burda <quent.burda@gmail.com>
Date: Tue, 25 Jul 2023 03:07:09 +0200
Fix oob instructions access
Diffstat:
M | asm.c | | | 83 | +++++++++++++++++++++++++------------------------------------------------------ |
M | tpu.c | | | 7 | ++++--- |
2 files changed, 30 insertions(+), 60 deletions(-)
diff --git a/asm.c b/asm.c
@@ -34,6 +34,22 @@ struct asm_tokenizer {
char linebuf[256];
};
+static const char *tok_strs[] = {
+ /* Global */
+ "stdin", "stdout", "tpu", "end",
+
+ /* Operands (order like OP_*) */
+ "acc", "bak", "nil", "left", "right",
+ "up", "down", "any", "last", NULL, NULL,
+
+ /* Instructions (order like INST_*) */
+ "nop", "mov", "swp", "sav", "add", "sub",
+ "neg", "jmp", "jez", "jnz", "jgz", "jlz", "jro",
+
+ /* Misc */
+ NULL, NULL, NULL, NULL, NULL, NULL
+};
+
static const char *tok_reprs[] = {
/* Global */
"'STDIN'", "'STDOUT'", "'TPU'", "'END'",
@@ -120,6 +136,7 @@ tok_next(struct asm_tokenizer *tok)
{
size_t len;
char *s;
+ int i;
if (!tok->linebuf[tok->off]) {
if (feof(tok->file)) return TOK_EOF;
@@ -151,61 +168,12 @@ tok_next(struct asm_tokenizer *tok)
tok->off += 1;
}
- //printf("> %s\n", tok->tokstr);
-
- if (!strcasecmp(s, "stdin")) {
- return TOK_STDIN;
- } else if (!strcasecmp(s, "stdout")) {
- return TOK_STDOUT;
- } else if (!strcasecmp(s, "tpu")) {
- return TOK_TPU;
- } else if (!strcasecmp(s, "end")) {
- return TOK_END;
- } else if (!strcasecmp(s, "acc")) {
- return TOK_ACC;
- } else if (!strcasecmp(s, "bak")) {
- return TOK_BAK;
- } else if (!strcasecmp(s, "nil")) {
- return TOK_NIL;
- } else if (!strcasecmp(s, "left")) {
- return TOK_LEFT;
- } else if (!strcasecmp(s, "right")) {
- return TOK_RIGHT;
- } else if (!strcasecmp(s, "up")) {
- return TOK_UP;
- } else if (!strcasecmp(s, "down")) {
- return TOK_DOWN;
- } else if (!strcasecmp(s, "any")) {
- return TOK_ANY;
- } else if (!strcasecmp(s, "last")) {
- return TOK_LAST;
- } else if (!strcasecmp(s, "nop")) {
- return TOK_NOP;
- } else if (!strcasecmp(s, "mov")) {
- return TOK_MOV;
- } else if (!strcasecmp(s, "swp")) {
- return TOK_SWP;
- } else if (!strcasecmp(s, "sav")) {
- return TOK_SAV;
- } else if (!strcasecmp(s, "add")) {
- return TOK_ADD;
- } else if (!strcasecmp(s, "sub")) {
- return TOK_SUB;
- } else if (!strcasecmp(s, "neg")) {
- return TOK_NEG;
- } else if (!strcasecmp(s, "jmp")) {
- return TOK_JMP;
- } else if (!strcasecmp(s, "jez")) {
- return TOK_JEZ;
- } else if (!strcasecmp(s, "jnz")) {
- return TOK_JNZ;
- } else if (!strcasecmp(s, "jgz")) {
- return TOK_JGZ;
- } else if (!strcasecmp(s, "jlz")) {
- return TOK_JLZ;
- } else if (!strcasecmp(s, "jro")) {
- return TOK_JRO;
- } else if (is_lit(s)) {
+ for (i = 0; i <= TOK_EOF; i++) {
+ if (tok_strs[i] && !strcasecmp(s, tok_strs[i]))
+ return (enum asm_tok) i;
+ }
+
+ if (is_lit(s)) {
return TOK_LIT;
} else if (*s == '#') {
tok->off += strlen(tok->linebuf + tok->off);
@@ -264,8 +232,9 @@ tpu_validate(struct tpu *tpu)
size_t dst;
int i;
- for (i = 0; i < TPU_MAX_INST; i++) {
- if (tpu->insts[i].ops[0].type == OP_LABEL) {
+ for (i = 0; i < tpu->inst_cnt; i++) {
+ if (tpu->insts[i].opcnt >= 1
+ && tpu->insts[i].ops[0].type == OP_LABEL) {
dst = label_map_get(&tpu->label_map,
tpu->insts[i].ops[0].val.label);
if (dst == TPU_MAX_INST)
diff --git a/tpu.c b/tpu.c
@@ -181,11 +181,12 @@ tpu_deinit(struct tpu *tpu)
{
int i;
- label_map_deinit(&tpu->label_map);
- for (i = 0; i < TPU_MAX_INST; i++) {
- if (tpu->insts[i].ops[0].type == OP_LABEL)
+ for (i = 0; i < tpu->inst_cnt; i++) {
+ if (tpu->insts[i].opcnt >= 1
+ && tpu->insts[i].ops[0].type == OP_LABEL)
free(tpu->insts[i].ops[0].val.label);
}
+ label_map_deinit(&tpu->label_map);
}
struct tpu_inst *