diff options
| author | 2023-02-11 20:49:39 +0000 | |
|---|---|---|
| committer | 2023-02-11 20:49:39 +0000 | |
| commit | 5f3275cf2d3e1f371595f3ef15831e2ee788f780 (patch) | |
| tree | 97b2bbe678cc5f45aaca568ff5d4c9edd3a41005 /creole.c | |
| parent | more test (diff) | |
add signed division IDIV
Diffstat (limited to 'creole.c')
| -rw-r--r-- | creole.c | 14 |
1 files changed, 13 insertions, 1 deletions
@@ -35,6 +35,7 @@ static const struct { defop(ADD, 3, TYPE_REG, TYPE_VAL, TYPE_VAL), defop(MUL, 3, TYPE_REG, TYPE_VAL, TYPE_VAL), defop(DIV, 3, TYPE_REG, TYPE_VAL, TYPE_VAL), + defop(IDIV, 3, TYPE_REG, TYPE_VAL, TYPE_VAL), defop(JL, 3, TYPE_LAB, TYPE_VAL, TYPE_VAL), defop(CLB, 1, TYPE_LAB, TYPE_NONE, TYPE_NONE), defop(SYS, 1, TYPE_VAL, TYPE_NONE, TYPE_NONE) @@ -323,8 +324,9 @@ creole_parse_line(struct creole_ins *ins, struct creole_reader *r) return CREOLE_OPCODE_READ_ERROR; ins->opcode = w.word; - if (w.word >= CREOLE_ARG_TYPE_LEN || w.len != 1) + if (w.word >= CREOLE_OPCODE_LEN || w.len != 1) { return CREOLE_OPCODE_MALFORMED; + } for (arg = 0; arg < opcode_info[ins->opcode].arglen; arg++) { if (!decode_seq(r, &w)) @@ -542,8 +544,18 @@ enum creole_run_ret creole_step(struct creole_env *env, creole_word *sc) case CREOLE_DIV: check(read_val(env, ins, 1, &a1)); check(read_val(env, ins, 2, &a2)); + if (a2 == 0) + return CREOLE_DIV_BY_ZERO; check(creole_reg_write(env, ins->w[0], a1 / a2)); break; + case CREOLE_IDIV: + check(read_val(env, ins, 1, &a1)); + check(read_val(env, ins, 2, &a2)); + if (a2 == 0) + return CREOLE_DIV_BY_ZERO; + check(creole_reg_write(env, ins->w[0], + (creole_signed)a1 / (creole_signed)a2)); + break; case CREOLE_JL: check(read_val(env, ins, 1, &a1)); check(read_val(env, ins, 2, &a2)); |
