emulator: Fix M-extension corner cases

d9bc0eae48f8e89a518afd5b4ee425a663ce5ec1f53580bb7c1d713c9b8054d1
The interpreter and the JIT left mulh, mulhsu and mulhu
unimplemented. The JIT returned the dividend for any signed division
by -1 instead of negating it, and the interpreter trapped on the host
for INT_MIN / -1 and INT_MIN % -1.

Implement the three high-multiply instructions in both. Route the
signed division and remainder of the interpreter, including the W
forms, through one helper that gives the RISC-V results for a zero
divisor and for overflow, and negate the dividend in the JIT.

Assisted-by: Claude:sonnet-5.5
Assisted-by: Claude:opus-5.5
Alexis Sellier committed ago 1 parent 92cdb0c5
Makefile +1 -1
16 16
LDFLAGS := -fuse-ld=lld -Wl,-z,stack-size=33554432
17 17
EMULATOR_SRCS  := emulator.c jit.c riscv/debug.c io.c riscv.c
18 18
19 19
default: bin/emulator
20 20
21 -
bin/emulator: $(EMULATOR_SRCS) jit.h color.h types.h io.h riscv.h riscv/debug.h
21 +
bin/emulator: $(EMULATOR_SRCS) jit.h muldiv.h color.h types.h io.h riscv.h riscv/debug.h
22 22
	@echo "cc    emulator => $@"
23 23
	@mkdir -p bin
24 24
	@$(CC) $(CFLAGS) $(LDFLAGS) $(EMULATOR_SRCS) -o $@
25 25
26 26
install: bin/emulator
emulator.c +23 -24
10 10
#include <unistd.h>
11 11
12 12
#include "color.h"
13 13
#include "io.h"
14 14
#include "jit.h"
15 +
#include "muldiv.h"
15 16
#include "riscv.h"
16 17
#include "riscv/debug.h"
17 18
#include "types.h"
18 19
19 20
#ifndef PATH_MAX
2028 2029
            switch (ins.r.funct3) {
2029 2030
            case FUNCT3_ADD: /* mul.  */
2030 2031
                cpu->regs[ins.r.rd] =
2031 2032
                    cpu->regs[ins.r.rs1] * cpu->regs[ins.r.rs2];
2032 2033
                break;
2034 +
            case FUNCT3_SLL: /* mulh.  */
2035 +
                cpu->regs[ins.r.rd] =
2036 +
                    mulh64(cpu->regs[ins.r.rs1], cpu->regs[ins.r.rs2]);
2037 +
                break;
2038 +
            case FUNCT3_SLT: /* mulhsu. */
2039 +
                cpu->regs[ins.r.rd] =
2040 +
                    mulhsu64(cpu->regs[ins.r.rs1], cpu->regs[ins.r.rs2]);
2041 +
                break;
2042 +
            case FUNCT3_SLTU: /* mulhu. */
2043 +
                cpu->regs[ins.r.rd] =
2044 +
                    mulhu64(cpu->regs[ins.r.rs1], cpu->regs[ins.r.rs2]);
2045 +
                break;
2033 2046
            case FUNCT3_XOR: /* div.  */
2034 -
                if (cpu->regs[ins.r.rs2] != 0) {
2035 -
                    cpu->regs[ins.r.rd] = (u64)((i64)cpu->regs[ins.r.rs1] /
2036 -
                                                (i64)cpu->regs[ins.r.rs2]);
2037 -
                } else {
2038 -
                    cpu->regs[ins.r.rd] = (u64)-1; /* Division by zero. */
2039 -
                }
2047 +
                cpu->regs[ins.r.rd] =
2048 +
                    divs64(cpu->regs[ins.r.rs1], cpu->regs[ins.r.rs2], false);
2040 2049
                break;
2041 2050
            case FUNCT3_SRL: /* divu. */
2042 2051
                if (cpu->regs[ins.r.rs2] != 0) {
2043 2052
                    cpu->regs[ins.r.rd] =
2044 2053
                        cpu->regs[ins.r.rs1] / cpu->regs[ins.r.rs2];
2045 2054
                } else {
2046 2055
                    cpu->regs[ins.r.rd] = (u64)-1; /* Division by zero. */
2047 2056
                }
2048 2057
                break;
2049 2058
            case FUNCT3_OR: /* rem.  */
2050 -
                if (cpu->regs[ins.r.rs2] != 0) {
2051 -
                    cpu->regs[ins.r.rd] = (u64)((i64)cpu->regs[ins.r.rs1] %
2052 -
                                                (i64)cpu->regs[ins.r.rs2]);
2053 -
                } else {
2054 -
                    cpu->regs[ins.r.rd] = cpu->regs[ins.r.rs1];
2055 -
                }
2059 +
                cpu->regs[ins.r.rd] =
2060 +
                    divs64(cpu->regs[ins.r.rs1], cpu->regs[ins.r.rs2], true);
2056 2061
                break;
2057 2062
            case FUNCT3_AND: /* remu. */
2058 2063
                if (cpu->regs[ins.r.rs2] != 0) {
2059 2064
                    cpu->regs[ins.r.rd] =
2060 2065
                        cpu->regs[ins.r.rs1] % cpu->regs[ins.r.rs2];
2121 2126
                i32 result          = (i32)(rs1_32 * rs2_32);
2122 2127
                cpu->regs[ins.r.rd] = (u64)(i64)result;
2123 2128
                break;
2124 2129
            }
2125 2130
            case FUNCT3_XOR: { /* divw.  */
2126 -
                if (rs2_32 != 0) {
2127 -
                    i32 result          = (i32)rs1_32 / (i32)rs2_32;
2128 -
                    cpu->regs[ins.r.rd] = (u64)(i64)result;
2129 -
                } else {
2130 -
                    cpu->regs[ins.r.rd] = (u64)(i64)(i32)-1;
2131 -
                }
2131 +
                u64 result =
2132 +
                    divs64((u64)(i64)(i32)rs1_32, (u64)(i64)(i32)rs2_32, false);
2133 +
                cpu->regs[ins.r.rd] = (u64)(i64)(i32)result;
2132 2134
                break;
2133 2135
            }
2134 2136
            case FUNCT3_SRL: { /* divuw. */
2135 2137
                if (rs2_32 != 0) {
2136 2138
                    i32 result          = (i32)(rs1_32 / rs2_32);
2139 2141
                    cpu->regs[ins.r.rd] = (u64)(i64)(i32)-1;
2140 2142
                }
2141 2143
                break;
2142 2144
            }
2143 2145
            case FUNCT3_OR: { /* remw.  */
2144 -
                if (rs2_32 != 0) {
2145 -
                    i32 result          = (i32)rs1_32 % (i32)rs2_32;
2146 -
                    cpu->regs[ins.r.rd] = (u64)(i64)result;
2147 -
                } else {
2148 -
                    cpu->regs[ins.r.rd] = (u64)(i64)(i32)rs1_32;
2149 -
                }
2146 +
                u64 result =
2147 +
                    divs64((u64)(i64)(i32)rs1_32, (u64)(i64)(i32)rs2_32, true);
2148 +
                cpu->regs[ins.r.rd] = (u64)(i64)(i32)result;
2150 2149
                break;
2151 2150
            }
2152 2151
            case FUNCT3_AND: { /* remuw. */
2153 2152
                if (rs2_32 != 0) {
2154 2153
                    i32 result          = (i32)(rs1_32 % rs2_32);
jit.c +51 -13
372 372
    emit_rexw(e);
373 373
    emit_u8(e, 0x89);
374 374
    emit_u8(e, modrm(3, X_RDX, X_RAX));
375 375
}
376 376
377 +
/*
378 +
 * High 64 bits of rax * rcx, left in rax. `kind` is the funct3: 1 = mulh,
379 +
 * 2 = mulhsu, 3 = mulhu. Clobbers rdx and, for mulhsu, r11.
380 +
 */
381 +
static void emit_mulh(struct emitter *e, u32 kind) {
382 +
    if (kind == 2) {
383 +
        /* mov r11, rax (the signed operand) */
384 +
        emit_u8(e, rex(true, false, false, true));
385 +
        emit_u8(e, 0x89);
386 +
        emit_u8(e, modrm(3, X_RAX, X_R11 & 7));
387 +
    }
388 +
    emit_rexw(e);
389 +
    emit_u8(e, 0xF7);
390 +
    emit_u8(e, modrm(3, kind == 1 ? 5 : 4, X_RCX)); /* imul rcx / mul rcx */
391 +
    if (kind == 2) {
392 +
        /* hi_su = hi_uu - (a < 0 ? b : 0) */
393 +
        emit_u8(e, rex(true, false, false, true));
394 +
        emit_u8(e, 0xC1);
395 +
        emit_u8(e, modrm(3, 7, X_R11 & 7));
396 +
        emit_u8(e, 63); /* sar r11, 63 */
397 +
        emit_u8(e, rex(true, false, false, true));
398 +
        emit_u8(e, 0x21);
399 +
        emit_u8(e, modrm(3, X_RCX, X_R11 & 7)); /* and r11, rcx */
400 +
        emit_u8(e, rex(true, true, false, false));
401 +
        emit_u8(e, 0x29);
402 +
        emit_u8(e, modrm(3, X_R11 & 7, X_RDX)); /* sub rdx, r11 */
403 +
    }
404 +
    emit_mov_rax_rdx(e);
405 +
}
406 +
377 407
/* cmp rax, rcx (64-bit) */
378 408
static void emit_cmp_rax_rcx(struct emitter *e) {
379 409
    emit_rexw(e);
380 410
    emit_u8(e, 0x39);
381 411
    emit_u8(e, modrm(3, X_RCX, X_RAX));
401 431
}
402 432
403 433
/* ---------- Division helpers ------------------------------------------- */
404 434
405 435
/*
406 -
 * Shared skeleton for 64-bit div/rem with RISC-V corner cases:
407 -
 *   - divisor == 0: emit_zero_case (custom per variant)
408 -
 *   - signed overflow (INT_MIN / -1): emit_overflow_case
409 -
 *   - otherwise: normal division
436 +
 * 64-bit div/rem of rax by rcx, leaving the result in rax, with the RISC-V
437 +
 * results where x86 division would trap:
438 +
 *   - divisor == 0: quotient -1, remainder the dividend;
439 +
 *   - signed, divisor == -1: quotient -dividend (INT_MIN stays INT_MIN),
440 +
 *     remainder 0.
410 441
 *
411 -
 * `is_signed`: whether to check for INT_MIN/-1 overflow.
412 -
 * `is_rem`:    whether to move rdx->rax after division (remainder result).
413 -
 * `save_dividend`: whether to save rax to r11 before testing (needed for
414 -
 *                  rem(x,0)=x semantics).
442 +
 * `is_signed`: signed division.
443 +
 * `is_rem`:    return the remainder instead of the quotient.
415 444
 */
416 445
static void emit_div64(struct emitter *e, bool is_signed, bool is_rem) {
417 446
    /* For rem: save dividend to r11 (needed if divisor==0). */
418 447
    if (is_rem) {
419 448
        /* mov r11, rax */
438 467
    u32 jne_safe = 0, jmp_end2 = 0;
439 468
440 469
    if (is_signed) {
441 470
        emit_cmp_rcx_neg1_64(e);
442 471
        jne_safe = emit_jne_placeholder(e);
443 -
        /* rcx == -1: overflow case. */
472 +
        /* rcx == -1: idiv would trap on INT_MIN / -1. */
444 473
        if (is_rem) {
445 -
            /* result = 0 */
446 474
            emit_rexw(e);
447 475
            emit_u8(e, 0x31);
448 -
            emit_u8(e, modrm(3, X_RAX, X_RAX));
476 +
            emit_u8(e, modrm(3, X_RAX, X_RAX)); /* xor rax, rax */
477 +
        } else {
478 +
            emit_rexw(e);
479 +
            emit_u8(e, 0xF7);
480 +
            emit_u8(e, modrm(3, 3, X_RAX)); /* neg rax */
449 481
        }
450 -
        /* else: result = rax (already INT_MIN, which is correct) */
451 482
        jmp_end2 = emit_jmp_placeholder(e);
452 483
    }
453 484
454 485
    /* .safe: perform the actual division. */
455 486
    u32 safe = e->pos;
507 538
        jne_safe = emit_jne_placeholder(e);
508 539
        if (is_rem) {
509 540
            /* result = 0 */
510 541
            emit_u8(e, 0x31);
511 542
            emit_u8(e, modrm(3, X_RAX, X_RAX));
543 +
        } else {
544 +
            emit_u8(e, 0xF7);
545 +
            emit_u8(e, modrm(3, 3, X_RAX)); /* neg eax */
512 546
        }
513 -
        /* else: result = eax (INT_MIN stays INT_MIN) */
514 547
        jmp_end2 = emit_jmp_placeholder(e);
515 548
    }
516 549
517 550
    /* .safe: */
518 551
    u32 safe = e->pos;
973 1006
        case FUNCT7_MUL:
974 1007
            switch (ins.r.funct3) {
975 1008
            case 0x0:
976 1009
                emit_imul_rax_rcx(e);
977 1010
                break; /* mul */
1011 +
            case 0x1:
1012 +
            case 0x2:
1013 +
            case 0x3:
1014 +
                emit_mulh(e, ins.r.funct3);
1015 +
                break; /* mulh, mulhsu, mulhu */
978 1016
            case 0x4:
979 1017
                emit_div64(e, true, false);
980 1018
                break; /* div */
981 1019
            case 0x5:
982 1020
                emit_div64(e, false, false);
muldiv.h added +34 -0
1 +
#ifndef MULDIV_H
2 +
#define MULDIV_H
3 +
4 +
#include "types.h"
5 +
6 +
__extension__ typedef unsigned __int128 u128;
7 +
8 +
/* High 64 bits of the unsigned 128-bit product. */
9 +
static inline u64 mulhu64(u64 a, u64 b) {
10 +
    return (u64)(((u128)a * b) >> 64);
11 +
}
12 +
13 +
/* High 64 bits of the signed x signed product. */
14 +
static inline u64 mulh64(u64 a, u64 b) {
15 +
    return mulhu64(a, b) - ((i64)a < 0 ? b : 0) - ((i64)b < 0 ? a : 0);
16 +
}
17 +
18 +
/* High 64 bits of the signed x unsigned product. */
19 +
static inline u64 mulhsu64(u64 a, u64 b) {
20 +
    return mulhu64(a, b) - ((i64)a < 0 ? b : 0);
21 +
}
22 +
23 +
/* Signed division with the RISC-V results for a zero divisor (quotient -1,
24 +
 * remainder a) and for overflow (INT_MIN / -1: quotient INT_MIN, remainder 0).
25 +
 * Operands are sign-extended, so the same helper serves the W forms. */
26 +
static inline u64 divs64(u64 a, u64 b, bool remainder) {
27 +
    if (b == 0)
28 +
        return remainder ? a : (u64)-1;
29 +
    if (b == (u64)-1)
30 +
        return remainder ? 0 : -a;
31 +
    return remainder ? (u64)((i64)a % (i64)b) : (u64)((i64)a / (i64)b);
32 +
}
33 +
34 +
#endif /* MULDIV_H */