summaryrefslogtreecommitdiffstats
path: root/asm.c
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2023-12-27 14:12:17 +0100
committerLouis Burda <quent.burda@gmail.com>2023-12-27 14:12:17 +0100
commit11d75bf41c7d22b6a6a76fa2dd4843d23a75fffb (patch)
tree4821a231ac0dcee71296db75022aa6fc220a8159 /asm.c
parentc06c696a20fa8987bbb13da608153ccfc3cd55e7 (diff)
downloadtis100-11d75bf41c7d22b6a6a76fa2dd4843d23a75fffb.tar.gz
tis100-11d75bf41c7d22b6a6a76fa2dd4843d23a75fffb.zip
Fix instruction squishing to fit tpu row
Diffstat (limited to 'asm.c')
-rw-r--r--asm.c34
1 files changed, 23 insertions, 11 deletions
diff --git a/asm.c b/asm.c
index b0ac9a5..f18b1e4 100644
--- a/asm.c
+++ b/asm.c
@@ -187,9 +187,9 @@ strlcat_op_name(char *buf, struct tpu_inst_op *op, size_t n)
}
size_t
-asm_print_inst(char *buf, size_t n, struct tpu_inst *inst)
+asm_print_inst(char *buf, size_t n, struct tpu_inst *inst, size_t max)
{
- size_t len;
+ size_t len, op;
len = strdcpy(buf, inst_reprs[inst->type], n);
if (inst->opcnt >= 1) {
@@ -197,8 +197,14 @@ asm_print_inst(char *buf, size_t n, struct tpu_inst *inst)
len += strlcat_op_name(buf, &inst->ops[0], n);
}
if (inst->opcnt >= 2) {
- len += strdcat(buf, ", ", n);
- len += strlcat_op_name(buf, &inst->ops[1], n);
+ op = strdcat(buf, ", ", n);
+ op += strlcat_op_name(buf, &inst->ops[1], n);
+ if (len + op > max && len < n) {
+ buf[len] = '\0';
+ op = strdcat(buf, ",", n);
+ op += strlcat_op_name(buf, &inst->ops[1], n);
+ }
+ len += op;
}
return len;
@@ -437,10 +443,12 @@ tis_load_asm(struct tis *tis, const char *filepath)
TOK_NIL, TOK_LEFT, TOK_RIGHT, TOK_UP, TOK_DOWN,
TOK_ANY, TOK_LAST, TOK_LIT, TOK_NAME, TOK_NL, -1);
if (optok == TOK_NL) {
- if (!tpu_add_inst(tpu, inst_type, 0, op1, op2))
+ inst = tpu_add_inst(tpu, inst_type, 0, op1, op2);
+ if (!inst) {
die("load: line %lu, invalid instruction",
tokenizer.lineno-1);
- break;
+ }
+ goto inst_check;
}
op1 = tok_to_op(&tokenizer, optok);
@@ -448,10 +456,12 @@ tis_load_asm(struct tis *tis, const char *filepath)
TOK_NIL, TOK_LEFT, TOK_RIGHT, TOK_UP, TOK_DOWN,
TOK_ANY, TOK_LAST, TOK_LIT, TOK_NAME, TOK_NL, -1);
if (optok == TOK_NL) {
- if (!tpu_add_inst(tpu, inst_type, 1, op1, op2))
+ inst = tpu_add_inst(tpu, inst_type, 1, op1, op2);
+ if (!inst) {
die("load: line %lu, invalid instruction",
tokenizer.lineno-1);
- break;
+ }
+ goto inst_check;
}
op2 = tok_to_op(&tokenizer, optok);
@@ -461,15 +471,17 @@ tis_load_asm(struct tis *tis, const char *filepath)
if (!inst) die("load: line %lu, invalid instruction",
tokenizer.lineno-1);
+inst_check:
tpu->rows += 1;
if (tpu->inst_cnt > TPU_MAX_INST_CNT || tpu->rows > TPU_MAX_ROWS)
die("load: line %lu, tpu has too many rows",
tokenizer.lineno-1);
- len = asm_print_inst(rowbuf, sizeof(rowbuf), inst);
+ len = asm_print_inst(rowbuf, sizeof(rowbuf),
+ inst, TPU_MAX_COLS - colsused);
if (colsused + len > TPU_MAX_COLS)
- die("load: line %lu, tpu row is too long",
- tokenizer.lineno-1);
+ die("load: line %lu, tpu row is too long (%zu,%zu)",
+ tokenizer.lineno-1, colsused, len);
colsused = 0;
break;