diff options
Diffstat (limited to 'asm.c')
| -rw-r--r-- | asm.c | 34 |
1 files changed, 23 insertions, 11 deletions
@@ -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; |
