aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

commit 6433edeae6d11a7e0a3b1f68d710b4bd52e3eed3
parent 4b5760995b79ffa4726432bada697d979f7583ba
Author: xplshn <anto@xplshn.com.ar>
Date:   Thu, 26 Nov 2026 02:05:29 -0300

no warnings: as is c99 with its unions named, cc switches and casts said, noreturn where it is, prototypes with void

Diffstat:
Mcmd/dev/as/arch/x64/asm_code.c | 676++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mcmd/dev/as/arch/x64/inst.h | 2+-
Mcmd/dev/as/arch/x64/parse_x64.c | 182++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mcmd/dev/as/arch/x64/reloc_x64.c | 80++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mcmd/dev/as/as.c | 4++--
Mcmd/dev/as/emit_macho.c | 3+++
Mcmd/dev/as/ir_asm.c | 65++++++++++++++++++++++++++++-------------------------------------
Mcmd/dev/as/ir_asm.h | 5+++--
Mcmd/dev/as/parse_asm.c | 56++++++++++++++++++++++++++++----------------------------
Mcmd/dev/as/parse_asm.h | 2+-
Mcmd/dev/cc/attr.c | 2++
Mcmd/dev/cc/cc.h | 3+++
Mcmd/dev/cc/decl.c | 24+++++++++++++++---------
Mcmd/dev/cc/driver.c | 4++--
Mcmd/dev/cc/eval.c | 14++++++++++----
Mcmd/dev/cc/expr.c | 23++++++++++++++++-------
Mcmd/dev/cc/init.c | 4+++-
Mcmd/dev/cc/pp.c | 16++++++++++------
Mcmd/dev/cc/qbe.c | 32++++++++++++++++++++++----------
Mcmd/dev/cc/token.c | 2+-
Mcmd/dev/cc/type.c | 21+++++++++++++++------
Mcmd/dev/cc/utf.c | 18+++++++++---------
Mcmd/dev/cc/util.h | 3+++
Mcmd/dev/ld/arch/aarch64/reloc_aarch64.c | 2--
Mcmd/dev/ld/arch/arm/reloc_arm.c | 2--
Mcmd/dev/ld/arch/riscv64/reloc_riscv64.c | 1-
Mcmd/extra/dc.c | 8+++-----
Mcmd/extra/partplan.c | 2+-
Mcmd/extra/yap/display.c | 2+-
Mcmd/extra/yap/getline.c | 8++++----
Mcmd/extra/yap/help.c | 2+-
Mcmd/extra/yap/keys.c | 2+-
Mcmd/extra/yap/main.c | 4++--
Mcmd/extra/yap/output.c | 4++--
Mcmd/extra/yap/prompt.c | 4++--
Mcmd/extra/yap/term.c | 38+++++++++++++++++++-------------------
Mcmd/linux/vtallow.c | 2+-
Mcmd/posix/awk/lib.c | 2+-
Mcmd/posix/awk/run.c | 11++---------
Mcmd/posix/bc.y | 2+-
Mcmd/posix/echo.c | 5+++--
Mcmd/posix/join.c | 2+-
Mcmd/posix/mesg.c | 2+-
Mcmd/posix/ps.c | 1-
Mcmd/posix/test.c | 4++--
Mcmd/posix/touch.c | 2+-
Mcmd/pseudo/tar.c | 2+-
Mshared/util.h | 4++--
48 files changed, 698 insertions(+), 661 deletions(-)

diff --git a/cmd/dev/as/arch/x64/asm_code.c b/cmd/dev/as/arch/x64/asm_code.c @@ -125,12 +125,12 @@ static unsigned char * asm_mov_rr(struct Inst *inst, struct Code *code) { unsigned char *p = code->buf; - enum RegSize size = inst->opr[0].reg.size; + enum RegSize size = inst->opr[0].u.reg.size; p = put_rex2( p, size, - opr_regno(&inst->opr[0].reg), - opr_regno(&inst->opr[1].reg), + opr_regno(&inst->opr[0].u.reg), + opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x88 : 0x89 ); return p; @@ -140,19 +140,19 @@ static unsigned char * asm_mov_imr(struct Inst *inst, struct Code *code) { unsigned char *p = code->buf; - enum RegSize size = inst->opr[1].reg.size; - if (size == REG64 && is_im32(inst->opr[0].immediate)) { - int d = inst->opr[1].reg.no; - int pre = !inst->opr[1].reg.x ? 0x48 : 0x49; - MAKE_CODE(inst, code, pre, 0xc7, 0xc0 | d, IM32(inst->opr[0].immediate)); + enum RegSize size = inst->opr[1].u.reg.size; + if (size == REG64 && is_im32(inst->opr[0].u.immediate)) { + int d = inst->opr[1].u.reg.no; + int pre = !inst->opr[1].u.reg.x ? 0x48 : 0x49; + MAKE_CODE(inst, code, pre, 0xc7, 0xc0 | d, IM32(inst->opr[0].u.immediate)); return code->buf; } short buf[] = { MAKE_REX0( - size, 0, opr_regno(&inst->opr[1].reg), 0xb0 | (size == REG8 ? 0 : 8) | inst->opr[1].reg.no + size, 0, opr_regno(&inst->opr[1].u.reg), 0xb0 | (size == REG8 ? 0 : 8) | inst->opr[1].u.reg.no ), - MAKE_IM(size, inst->opr[0].immediate), + MAKE_IM(size, inst->opr[0].u.immediate), }; p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); return p; @@ -161,12 +161,12 @@ asm_mov_imr(struct Inst *inst, struct Code *code) static unsigned char * asm_mov_ir(struct Inst *inst, struct Code *code) { - if (inst->opr[0].indirect.reg.no != RIP) { - if (inst->opr[0].indirect.offset.expr->kind == EX_FIXNUM) { - long offset = inst->opr[0].indirect.offset.expr->fixnum; - enum RegSize size = inst->opr[1].reg.size; - int sno = opr_regno(&inst->opr[0].indirect.reg); - int dno = opr_regno(&inst->opr[1].reg); + if (inst->opr[0].u.indirect.reg.no != RIP) { + if (inst->opr[0].u.indirect.offset.expr->kind == EX_FIXNUM) { + long offset = inst->opr[0].u.indirect.offset.expr->u.fixnum; + enum RegSize size = inst->opr[1].u.reg.size; + int sno = opr_regno(&inst->opr[0].u.indirect.reg); + int dno = opr_regno(&inst->opr[1].u.reg); int ofszero = offset == 0 && ((sno & 7) != RBP - RAX); short buf[] = { size == REG16 ? 0x66 : -1, @@ -194,9 +194,9 @@ asm_mov_ir(struct Inst *inst, struct Code *code) return p; } } else { - if (inst->opr[0].indirect.offset.expr->kind != EX_FIXNUM) { - int pre = !inst->opr[1].reg.x ? 0x48 : 0x4c; - int d = inst->opr[1].reg.no; + if (inst->opr[0].u.indirect.offset.expr->kind != EX_FIXNUM) { + int pre = !inst->opr[1].u.reg.x ? 0x48 : 0x4c; + int d = inst->opr[1].u.reg.no; MAKE_CODE(inst, code, pre, 0x8b, 0x05 | (d << 3), IM32(0)); return code->buf; } @@ -213,13 +213,13 @@ assemble_reg_to_mem( struct Inst *inst, struct Code *code, int escape, unsigned char op8, unsigned char opw ) { - if (inst->opr[1].indirect.offset.expr->kind != EX_FIXNUM || inst->opr[1].indirect.reg.no == RIP) + if (inst->opr[1].u.indirect.offset.expr->kind != EX_FIXNUM || inst->opr[1].u.indirect.reg.no == RIP) return NULL; - long offset = inst->opr[1].indirect.offset.expr->fixnum; - enum RegSize size = inst->opr[0].reg.size; - int sno = opr_regno(&inst->opr[0].reg); - int dno = opr_regno(&inst->opr[1].indirect.reg); + long offset = inst->opr[1].u.indirect.offset.expr->u.fixnum; + enum RegSize size = inst->opr[0].u.reg.size; + int sno = opr_regno(&inst->opr[0].u.reg); + int dno = opr_regno(&inst->opr[1].u.indirect.reg); int ofszero = offset == 0 && ((dno & 7) != RBP - RAX); short buf[] = { size == REG16 ? 0x66 : -1, @@ -257,19 +257,19 @@ asm_mov_ri(struct Inst *inst, struct Code *code) static unsigned char * asm_mov_iir(struct Inst *inst, struct Code *code) { - if (inst->opr[0].indirect_with_index.offset->kind == EX_FIXNUM) { - long offset = inst->opr[0].indirect_with_index.offset->fixnum; + if (inst->opr[0].u.indirect_with_index.offset->kind == EX_FIXNUM) { + long offset = inst->opr[0].u.indirect_with_index.offset->u.fixnum; assert(is_im32(offset)); short offset_bit = offset == 0 ? 0x04 : is_im8(offset) ? 0x44 : 0x84; - enum RegSize size = inst->opr[1].reg.size; - assert(inst->opr[0].indirect_with_index.base_reg.no != RIP); - int bno = opr_regno(&inst->opr[0].indirect_with_index.base_reg); - int ino = opr_regno(&inst->opr[0].indirect_with_index.index_reg); - int dno = opr_regno(&inst->opr[1].reg); - struct Expr *scale_expr = inst->opr[0].indirect_with_index.scale; + enum RegSize size = inst->opr[1].u.reg.size; + assert(inst->opr[0].u.indirect_with_index.base_reg.no != RIP); + int bno = opr_regno(&inst->opr[0].u.indirect_with_index.base_reg); + int ino = opr_regno(&inst->opr[0].u.indirect_with_index.index_reg); + int dno = opr_regno(&inst->opr[1].u.reg); + struct Expr *scale_expr = inst->opr[0].u.indirect_with_index.scale; char scale_bit = 0; if (scale_expr != NULL) { - int s = scale_expr->fixnum; + int s = scale_expr->u.fixnum; if (s <= 0 || s > 8 || !IS_POWER_OF_2(s)) return NULL; scale_bit = most_significant_bit(s); @@ -301,7 +301,7 @@ asm_mov_iir(struct Inst *inst, struct Code *code) static unsigned char * assemble_direct_mov(struct Inst *inst, struct Code *code, int load) { - struct Reg *reg = load ? &inst->opr[1].reg : &inst->opr[0].reg; + struct Reg *reg = load ? &inst->opr[1].u.reg : &inst->opr[0].u.reg; enum RegSize size = reg->size; int dno = opr_regno(reg); unsigned char opcode = load ? 0x8b : 0x89; @@ -346,20 +346,20 @@ asm_mov_rd(struct Inst *inst, struct Code *code) static unsigned char * asm_mov_sr(struct Inst *inst, struct Code *code) { - enum RegSize dsize = inst->opr[1].reg.size; - if (inst->opr[0].segment.reg != FS || (dsize != REG32 && dsize != REG64)) + enum RegSize dsize = inst->opr[1].u.reg.size; + if (inst->opr[0].u.segment.reg != FS || (dsize != REG32 && dsize != REG64)) return NULL; - struct Expr *offset = inst->opr[0].segment.offset.expr; - int tpoff = inst->opr[0].segment.offset.flag & LF_TPOFF; + struct Expr *offset = inst->opr[0].u.segment.offset.expr; + int tpoff = inst->opr[0].u.segment.offset.flag & LF_TPOFF; if (offset != NULL && offset->kind != EX_FIXNUM && !tpoff) return NULL; /* tpoff's value is a link-time tls-layout quantity the assembler cannot compute, 0 here is a placeholder patched in by the relocation x64_resolve_code_reloc registers below */ - uint64_t value = (offset != NULL && offset->kind == EX_FIXNUM) ? offset->fixnum : 0; - unsigned char dno = opr_regno(&inst->opr[1].reg); + uint64_t value = (offset != NULL && offset->kind == EX_FIXNUM) ? offset->u.fixnum : 0; + unsigned char dno = opr_regno(&inst->opr[1].u.reg); short buf[] = { 0x64, dsize == REG64 ? 0x48 : -1, @@ -377,9 +377,9 @@ asm_mov_sr(struct Inst *inst, struct Code *code) static unsigned char * asm_movbwlq_imi(struct Inst *inst, struct Code *code) { - long offset = inst->opr[1].indirect.offset.expr->fixnum; + long offset = inst->opr[1].u.indirect.offset.expr->u.fixnum; unsigned char sno = 0; - unsigned char dno = opr_regno(&inst->opr[1].indirect.reg); + unsigned char dno = opr_regno(&inst->opr[1].u.indirect.reg); unsigned char op = (offset == 0 && (dno & 7) != RBP - RAX) ? 0x00 : is_im8(offset) ? (unsigned char)0x40 : (unsigned char)0x80; @@ -404,7 +404,7 @@ asm_movbwlq_imi(struct Inst *inst, struct Code *code) p += 4; } - long value = inst->opr[0].immediate; + long value = inst->opr[0].u.immediate; switch (inst->op) { case MOVB_IMI: *p++ = IM8(value); @@ -428,8 +428,8 @@ asm_movbwlq_imi(struct Inst *inst, struct Code *code) static unsigned char * asm_movbwlq_imd(struct Inst *inst, struct Code *code) { - assert(inst->opr[1].direct.expr->kind == EX_FIXNUM); - int64_t dst = inst->opr[1].direct.expr->fixnum; + assert(inst->opr[1].u.direct.expr->kind == EX_FIXNUM); + int64_t dst = inst->opr[1].u.direct.expr->u.fixnum; assert(is_im32(dst)); short buf[] = { @@ -445,7 +445,7 @@ asm_movbwlq_imd(struct Inst *inst, struct Code *code) PUT_CODE(p, IM32(dst)); p += 4; - long value = inst->opr[0].immediate; + long value = inst->opr[0].u.immediate; switch (inst->op) { case MOVB_IMD: *p++ = IM8(value); @@ -469,31 +469,31 @@ asm_movbwlq_imd(struct Inst *inst, struct Code *code) static unsigned char * asm_movszx_rr(struct Inst *inst, struct Code *code) { - int s = inst->opr[0].reg.no; - int d = inst->opr[1].reg.no; + int s = inst->opr[0].u.reg.no; + int d = inst->opr[1].u.reg.no; unsigned char op = inst->op == MOVZX ? 0xb6 : 0xbe; unsigned char *p = code->buf; - switch (inst->opr[0].reg.size) { + switch (inst->opr[0].u.reg.size) { case REG8: - switch (inst->opr[1].reg.size) { + switch (inst->opr[1].u.reg.size) { case REG16: - if (opr_reg8(&inst->opr[0].reg) && !inst->opr[1].reg.x) { + if (opr_reg8(&inst->opr[0].u.reg) && !inst->opr[1].u.reg.x) { MAKE_CODE(inst, code, 0x66, 0x0f, op, 0xc0 + s + d * 8); } else { - int pre = ((inst->opr[0].reg.x & 1) == 0 ? 0x40 : 0x41) + (!inst->opr[1].reg.x ? 0 : 4); + int pre = ((inst->opr[0].u.reg.x & 1) == 0 ? 0x40 : 0x41) + (!inst->opr[1].u.reg.x ? 0 : 4); MAKE_CODE(inst, code, 0x66, pre, 0x0f, op, 0xc0 + s + d * 8); } return p; case REG32: - if (opr_reg8(&inst->opr[0].reg) && !inst->opr[1].reg.x) { + if (opr_reg8(&inst->opr[0].u.reg) && !inst->opr[1].u.reg.x) { MAKE_CODE(inst, code, 0x0f, op, 0xc0 + s + d * 8); } else { - int pre = ((inst->opr[0].reg.x & 1) == 0 ? 0x40 : 0x41) + (!inst->opr[1].reg.x ? 0 : 4); + int pre = ((inst->opr[0].u.reg.x & 1) == 0 ? 0x40 : 0x41) + (!inst->opr[1].u.reg.x ? 0 : 4); MAKE_CODE(inst, code, pre, 0x0f, op, 0xc0 + s + d * 8); } return p; case REG64: { - int pre = ((inst->opr[0].reg.x & 1) == 0 ? 0x48 : 0x49) + (!inst->opr[1].reg.x ? 0 : 4); + int pre = ((inst->opr[0].u.reg.x & 1) == 0 ? 0x48 : 0x49) + (!inst->opr[1].u.reg.x ? 0 : 4); MAKE_CODE(inst, code, pre, 0x0f, op, 0xc0 + s + d * 8); return p; } @@ -502,17 +502,17 @@ asm_movszx_rr(struct Inst *inst, struct Code *code) } break; case REG16: - switch (inst->opr[1].reg.size) { + switch (inst->opr[1].u.reg.size) { case REG32: - if (!inst->opr[0].reg.x && !inst->opr[1].reg.x) { + if (!inst->opr[0].u.reg.x && !inst->opr[1].u.reg.x) { MAKE_CODE(inst, code, 0x0f, op | 1, 0xc0 + s + d * 8); } else { - int pre = (!inst->opr[0].reg.x ? 0x40 : 0x41) + (!inst->opr[1].reg.x ? 0 : 4); + int pre = (!inst->opr[0].u.reg.x ? 0x40 : 0x41) + (!inst->opr[1].u.reg.x ? 0 : 4); MAKE_CODE(inst, code, pre, 0x0f, op | 1, 0xc0 + s + d * 8); } return p; case REG64: { - int pre = (!inst->opr[0].reg.x ? 0x48 : 0x49) + (!inst->opr[1].reg.x ? 0 : 4); + int pre = (!inst->opr[0].u.reg.x ? 0x48 : 0x49) + (!inst->opr[1].u.reg.x ? 0 : 4); MAKE_CODE(inst, code, pre, 0x0f, op | 1, 0xc0 + s + d * 8); return p; } @@ -522,8 +522,8 @@ asm_movszx_rr(struct Inst *inst, struct Code *code) break; case REG32: /* "MOVZX %32bit, %64bit" doesnt exist! */ - if (inst->opr[1].reg.size == REG64 && inst->op == MOVSX) { - int pre = (!inst->opr[0].reg.x ? 0x48 : 0x49) + (!inst->opr[1].reg.x ? 0 : 4); + if (inst->opr[1].u.reg.size == REG64 && inst->op == MOVSX) { + int pre = (!inst->opr[0].u.reg.x ? 0x48 : 0x49) + (!inst->opr[1].u.reg.x ? 0 : 4); MAKE_CODE(inst, code, pre, 0x63, 0xc0 + s + d * 8); return p; } @@ -538,8 +538,8 @@ static unsigned char * asm_movsds_xx(struct Inst *inst, struct Code *code, int single) { unsigned char prefix = single ? 0xf3 : 0xf2; - unsigned char sno = inst->opr[0].regxmm - XMM0; - unsigned char dno = inst->opr[1].regxmm - XMM0; + unsigned char sno = inst->opr[0].u.regxmm - XMM0; + unsigned char dno = inst->opr[1].u.regxmm - XMM0; short buf[] = { prefix, sno >= 8 || dno >= 8 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) : -1, @@ -567,16 +567,16 @@ asm_movsds_ix(struct Inst *inst, struct Code *code, int single) { long offset; unsigned char prefix = single ? 0xf3 : 0xf2; - unsigned char dno = inst->opr[1].regxmm - XMM0; + unsigned char dno = inst->opr[1].u.regxmm - XMM0; int d = dno & 7; unsigned char *p = code->buf; - if (inst->opr[0].indirect.reg.no != RIP) { - if (inst->opr[0].indirect.offset.expr->kind != EX_FIXNUM - || !(offset = inst->opr[0].indirect.offset.expr->fixnum, is_im32(offset))) + if (inst->opr[0].u.indirect.reg.no != RIP) { + if (inst->opr[0].u.indirect.offset.expr->kind != EX_FIXNUM + || !(offset = inst->opr[0].u.indirect.offset.expr->u.fixnum, is_im32(offset))) return NULL; { - unsigned char sno = opr_regno(&inst->opr[0].indirect.reg); + unsigned char sno = opr_regno(&inst->opr[0].u.indirect.reg); int s = sno & 7; unsigned char op = (offset == 0 && s != RBP - RAX) ? (unsigned char)0x00 : is_im8(offset) ? (unsigned char)0x40 @@ -607,7 +607,7 @@ asm_movsds_ix(struct Inst *inst, struct Code *code, int single) /* rip-relative with a symbolic offset: emit a placeholder disp32, the same convention asm_lea_ir uses, resolved to a pc32 relocation by the ir pass */ - if (inst->opr[0].indirect.offset.expr->kind == EX_FIXNUM) + if (inst->opr[0].u.indirect.offset.expr->kind == EX_FIXNUM) return NULL; { short buf[] = { @@ -638,12 +638,12 @@ static unsigned char * asm_movsds_xi(struct Inst *inst, struct Code *code, int single) { long offset; - if (inst->opr[1].indirect.offset.expr->kind == EX_FIXNUM - && (offset = inst->opr[1].indirect.offset.expr->fixnum, is_im32(offset))) { - if (inst->opr[1].indirect.reg.no != RIP) { + if (inst->opr[1].u.indirect.offset.expr->kind == EX_FIXNUM + && (offset = inst->opr[1].u.indirect.offset.expr->u.fixnum, is_im32(offset))) { + if (inst->opr[1].u.indirect.reg.no != RIP) { unsigned char prefix = single ? 0xf3 : 0xf2; - unsigned char sno = inst->opr[0].regxmm - XMM0; - unsigned char dno = opr_regno(&inst->opr[1].indirect.reg); + unsigned char sno = inst->opr[0].u.regxmm - XMM0; + unsigned char dno = opr_regno(&inst->opr[1].u.indirect.reg); int d = dno & 7; int s = sno & 7; unsigned char op = (offset == 0 && d != RBP - RAX) ? (unsigned char)0x00 @@ -691,8 +691,8 @@ assemble_bop_sd(struct Inst *inst, struct Code *code, int single, unsigned char unsigned char *p = code->buf; unsigned char prefix = single ? 0xf3 : 0xf2; if (inst->opr[0].type == REG_XMM && inst->opr[1].type == REG_XMM) { - unsigned char sno = inst->opr[0].regxmm - XMM0; - unsigned char dno = inst->opr[1].regxmm - XMM0; + unsigned char sno = inst->opr[0].u.regxmm - XMM0; + unsigned char dno = inst->opr[1].u.regxmm - XMM0; short buf[] = { prefix, sno >= 8 || dno >= 8 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) : -1, @@ -756,8 +756,8 @@ assemble_xorpd_xx(struct Inst *inst, struct Code *code, int single) unsigned char *p = code->buf; assert(inst->opr[0].type == REG_XMM); assert(inst->opr[1].type == REG_XMM); - unsigned char sno = inst->opr[0].regxmm - XMM0; - unsigned char dno = inst->opr[1].regxmm - XMM0; + unsigned char sno = inst->opr[0].u.regxmm - XMM0; + unsigned char dno = inst->opr[1].u.regxmm - XMM0; short buf[] = { single ? -1 : 0x66, sno >= 8 || dno >= 8 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) : -1, @@ -783,18 +783,18 @@ static unsigned char * assemble_xorpd_ix(struct Inst *inst, struct Code *code, int single) { long offset; - unsigned char dno = inst->opr[1].regxmm - XMM0; + unsigned char dno = inst->opr[1].u.regxmm - XMM0; int d = dno & 7; unsigned char *p = code->buf; - if (inst->opr[0].indirect.reg.no != RIP) { + if (inst->opr[0].u.indirect.reg.no != RIP) { unsigned char sno; int s; unsigned char op; - if (inst->opr[0].indirect.offset.expr->kind != EX_FIXNUM - || !(offset = inst->opr[0].indirect.offset.expr->fixnum, is_im32(offset))) + if (inst->opr[0].u.indirect.offset.expr->kind != EX_FIXNUM + || !(offset = inst->opr[0].u.indirect.offset.expr->u.fixnum, is_im32(offset))) return NULL; - sno = opr_regno(&inst->opr[0].indirect.reg); + sno = opr_regno(&inst->opr[0].u.indirect.reg); s = sno & 7; op = (offset == 0 && s != RBP - RAX) ? (unsigned char)0x00 : is_im8(offset) ? (unsigned char)0x40 @@ -823,7 +823,7 @@ assemble_xorpd_ix(struct Inst *inst, struct Code *code, int single) /* rip-relative with a symbolic offset, same convention as asm_movsds_ix: placeholder disp32, resolved by the ir pass */ - if (inst->opr[0].indirect.offset.expr->kind == EX_FIXNUM) + if (inst->opr[0].u.indirect.offset.expr->kind == EX_FIXNUM) return NULL; { short buf[] = { @@ -856,8 +856,8 @@ assemble_ucomisd(struct Inst *inst, struct Code *code, unsigned char opc, int si unsigned char *p = code->buf; assert(inst->opr[0].type == REG_XMM); assert(inst->opr[1].type == REG_XMM); - unsigned char sno = inst->opr[0].regxmm - XMM0; - unsigned char dno = inst->opr[1].regxmm - XMM0; + unsigned char sno = inst->opr[0].u.regxmm - XMM0; + unsigned char dno = inst->opr[1].u.regxmm - XMM0; short buf[] = { single ? -1 : 0x66, sno >= 8 || dno >= 8 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) : -1, @@ -897,18 +897,18 @@ static unsigned char * assemble_ucomisd_ix(struct Inst *inst, struct Code *code, unsigned char opc, int single) { long offset; - unsigned char dno = inst->opr[1].regxmm - XMM0; + unsigned char dno = inst->opr[1].u.regxmm - XMM0; int d = dno & 7; unsigned char *p = code->buf; - if (inst->opr[0].indirect.reg.no != RIP) { + if (inst->opr[0].u.indirect.reg.no != RIP) { unsigned char sno; int s; unsigned char op; - if (inst->opr[0].indirect.offset.expr->kind != EX_FIXNUM - || !(offset = inst->opr[0].indirect.offset.expr->fixnum, is_im32(offset))) + if (inst->opr[0].u.indirect.offset.expr->kind != EX_FIXNUM + || !(offset = inst->opr[0].u.indirect.offset.expr->u.fixnum, is_im32(offset))) return NULL; - sno = opr_regno(&inst->opr[0].indirect.reg); + sno = opr_regno(&inst->opr[0].u.indirect.reg); s = sno & 7; op = (offset == 0 && s != RBP - RAX) ? (unsigned char)0x00 : is_im8(offset) ? (unsigned char)0x40 @@ -938,7 +938,7 @@ assemble_ucomisd_ix(struct Inst *inst, struct Code *code, unsigned char opc, int /* rip-relative with a symbolic offset, same convention as asm_movsds_ix/assemble_xorpd_ix: placeholder disp32, resolved by the ir pass */ - if (inst->opr[0].indirect.offset.expr->kind == EX_FIXNUM) + if (inst->opr[0].u.indirect.offset.expr->kind == EX_FIXNUM) return NULL; { short buf[] = { @@ -980,13 +980,13 @@ assemble_cvtsi2sd(struct Inst *inst, struct Code *code, int single) { unsigned char *p = code->buf; unsigned char prefix = single ? 0xf3 : 0xf2; - unsigned char sno = opr_regno(&inst->opr[0].reg); - unsigned char dno = inst->opr[1].regxmm - XMM0; + unsigned char sno = opr_regno(&inst->opr[0].u.reg); + unsigned char dno = inst->opr[1].u.regxmm - XMM0; short buf[] = { prefix, - sno >= 8 || dno >= 8 || inst->opr[0].reg.size == REG64 + sno >= 8 || dno >= 8 || inst->opr[0].u.reg.size == REG64 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) - | (inst->opr[0].reg.size == REG64 ? 8 : 0) + | (inst->opr[0].u.reg.size == REG64 ? 8 : 0) : -1, 0x0f, 0x2a, @@ -1011,13 +1011,13 @@ assemble_cvttsd2si(struct Inst *inst, struct Code *code, int single) { unsigned char *p = code->buf; unsigned char prefix = single ? 0xf3 : 0xf2; - unsigned char sno = inst->opr[0].regxmm - XMM0; - unsigned char dno = opr_regno(&inst->opr[1].reg); + unsigned char sno = inst->opr[0].u.regxmm - XMM0; + unsigned char dno = opr_regno(&inst->opr[1].u.reg); short buf[] = { prefix, - sno >= 8 || dno >= 8 || inst->opr[1].reg.size == REG64 + sno >= 8 || dno >= 8 || inst->opr[1].u.reg.size == REG64 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) - | (inst->opr[1].reg.size == REG64 ? 8 : 0) + | (inst->opr[1].u.reg.size == REG64 ? 8 : 0) : -1, 0x0f, 0x2c, @@ -1042,8 +1042,8 @@ assemble_cvtsd2ss(struct Inst *inst, struct Code *code, int single) { unsigned char *p = code->buf; unsigned char prefix = single ? 0xf3 : 0xf2; - unsigned char sno = inst->opr[0].regxmm - XMM0; - unsigned char dno = inst->opr[1].regxmm - XMM0; + unsigned char sno = inst->opr[0].u.regxmm - XMM0; + unsigned char dno = inst->opr[1].u.regxmm - XMM0; short buf[] = { prefix, sno >= 8 || dno >= 8 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) : -1, @@ -1068,8 +1068,8 @@ asm_cvtss2sd_xx(struct Inst *inst, struct Code *code) static unsigned char * asm_sqrtsd_xx(struct Inst *inst, struct Code *code) { - unsigned char sno = inst->opr[0].regxmm - XMM0; - unsigned char dno = inst->opr[1].regxmm - XMM0; + unsigned char sno = inst->opr[0].u.regxmm - XMM0; + unsigned char dno = inst->opr[1].u.regxmm - XMM0; short buf[] = { 0xf2, sno >= 8 || dno >= 8 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) : -1, @@ -1089,8 +1089,8 @@ asm_sqrtsd_xx(struct Inst *inst, struct Code *code) static unsigned char * asm_movq_gpxmm(struct Inst *inst, struct Code *code) { - unsigned char sno = opr_regno(&inst->opr[0].reg); - unsigned char dno = inst->opr[1].regxmm - XMM0; + unsigned char sno = opr_regno(&inst->opr[0].u.reg); + unsigned char dno = inst->opr[1].u.regxmm - XMM0; short buf[] = { 0x66, (unsigned char)0x48 | ((sno & 8) >> 3) | ((dno & 8) >> 1), @@ -1108,8 +1108,8 @@ asm_movq_gpxmm(struct Inst *inst, struct Code *code) static unsigned char * asm_movq_xmmgp(struct Inst *inst, struct Code *code) { - unsigned char sno = inst->opr[0].regxmm - XMM0; - unsigned char dno = opr_regno(&inst->opr[1].reg); + unsigned char sno = inst->opr[0].u.regxmm - XMM0; + unsigned char dno = opr_regno(&inst->opr[1].u.reg); short buf[] = { 0x66, (unsigned char)0x48 | ((dno & 8) >> 3) | ((sno & 8) >> 1), @@ -1139,27 +1139,27 @@ asm_movq_ixmm(struct Inst *inst, struct Code *code) short buf[7]; unsigned char *p; - dno = inst->opr[1].regxmm - XMM0; + dno = inst->opr[1].u.regxmm - XMM0; d = dno & 7; if (inst->opr[0].type == INDIRECT) { - bno = opr_regno(&inst->opr[0].indirect.reg); - offset_expr = inst->opr[0].indirect.offset.expr; + bno = opr_regno(&inst->opr[0].u.indirect.reg); + offset_expr = inst->opr[0].u.indirect.offset.expr; } else if (inst->opr[0].type == INDIRECT_WITH_INDEX) { - bno = opr_regno(&inst->opr[0].indirect_with_index.base_reg); - ino = opr_regno(&inst->opr[0].indirect_with_index.index_reg); - offset_expr = inst->opr[0].indirect_with_index.offset; - scale_expr = inst->opr[0].indirect_with_index.scale; + bno = opr_regno(&inst->opr[0].u.indirect_with_index.base_reg); + ino = opr_regno(&inst->opr[0].u.indirect_with_index.index_reg); + offset_expr = inst->opr[0].u.indirect_with_index.offset; + scale_expr = inst->opr[0].u.indirect_with_index.scale; indexed = 1; } else { return NULL; } if (bno == RIP || offset_expr->kind != EX_FIXNUM - || !(offset = offset_expr->fixnum, is_im32(offset))) + || !(offset = offset_expr->u.fixnum, is_im32(offset))) return NULL; if (scale_expr != NULL) { if (scale_expr->kind != EX_FIXNUM) return NULL; - scale = scale_expr->fixnum; + scale = scale_expr->u.fixnum; if (scale <= 0 || scale > 8 || !IS_POWER_OF_2(scale)) return NULL; scale_bit = most_significant_bit(scale); @@ -1198,14 +1198,14 @@ asm_movq_xmmi(struct Inst *inst, struct Code *code) { long offset; - if (inst->opr[1].indirect.reg.no == RIP) + if (inst->opr[1].u.indirect.reg.no == RIP) return NULL; - if (inst->opr[1].indirect.offset.expr->kind != EX_FIXNUM - || !(offset = inst->opr[1].indirect.offset.expr->fixnum, is_im32(offset))) + if (inst->opr[1].u.indirect.offset.expr->kind != EX_FIXNUM + || !(offset = inst->opr[1].u.indirect.offset.expr->u.fixnum, is_im32(offset))) return NULL; { - unsigned char sno = inst->opr[0].regxmm - XMM0; - unsigned char dno = opr_regno(&inst->opr[1].indirect.reg); + unsigned char sno = inst->opr[0].u.regxmm - XMM0; + unsigned char dno = opr_regno(&inst->opr[1].u.indirect.reg); int d = dno & 7; int s = sno & 7; unsigned char op = (offset == 0 && d != RBP - RAX) ? (unsigned char)0x00 @@ -1268,30 +1268,30 @@ signed_immediate(long value, enum RegSize size) static unsigned char * asm_lea_ir(struct Inst *inst, struct Code *code) { - assert(inst->opr[0].indirect.offset.expr != NULL); + assert(inst->opr[0].u.indirect.offset.expr != NULL); unsigned char *p = code->buf; - if (inst->opr[0].indirect.reg.no != RIP) { - if (inst->opr[0].indirect.offset.expr->kind == EX_FIXNUM) { - long offset = inst->opr[0].indirect.offset.expr->fixnum; - enum RegSize size = inst->opr[1].reg.size; + if (inst->opr[0].u.indirect.reg.no != RIP) { + if (inst->opr[0].u.indirect.offset.expr->kind == EX_FIXNUM) { + long offset = inst->opr[0].u.indirect.offset.expr->u.fixnum; + enum RegSize size = inst->opr[1].u.reg.size; short buf[] = { MAKE_REX_INDIRECT( size, - opr_regno(&inst->opr[1].reg), - opr_regno(&inst->opr[0].indirect.reg), + opr_regno(&inst->opr[1].u.reg), + opr_regno(&inst->opr[0].u.indirect.reg), 0x8c, 0x00, offset ), - MAKE_OFFSET(offset, inst->opr[0].indirect.reg.no != RBP - RAX), + MAKE_OFFSET(offset, inst->opr[0].u.indirect.reg.no != RBP - RAX), }; p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); return p; } } else { - if (inst->opr[0].indirect.offset.expr->kind != EX_FIXNUM) { - int pre = !inst->opr[1].reg.x ? 0x48 : 0x4c; - int d = inst->opr[1].reg.no; + if (inst->opr[0].u.indirect.offset.expr->kind != EX_FIXNUM) { + int pre = !inst->opr[1].u.reg.x ? 0x48 : 0x4c; + int d = inst->opr[1].u.reg.no; MAKE_CODE(inst, code, pre, 0x8d, 0x05 | (d << 3), IM32(0)); return p; } @@ -1302,16 +1302,16 @@ asm_lea_ir(struct Inst *inst, struct Code *code) static unsigned char * asm_lea_iir(struct Inst *inst, struct Code *code) { - struct Expr *offset_expr = inst->opr[0].indirect_with_index.offset; - struct Expr *scale_expr = inst->opr[0].indirect_with_index.scale; + struct Expr *offset_expr = inst->opr[0].u.indirect_with_index.offset; + struct Expr *scale_expr = inst->opr[0].u.indirect_with_index.scale; if ((offset_expr == NULL || offset_expr->kind == EX_FIXNUM) && (scale_expr == NULL || scale_expr->kind == EX_FIXNUM)) { - long offset = offset_expr != NULL ? offset_expr->fixnum : 0; - long scale = scale_expr != NULL ? scale_expr->fixnum : 1; + long offset = offset_expr != NULL ? offset_expr->u.fixnum : 0; + long scale = scale_expr != NULL ? scale_expr->u.fixnum : 1; if (is_im32(offset) && 1 <= scale && scale <= 8 && IS_POWER_OF_2(scale)) { - int breg = opr_regno(&inst->opr[0].indirect_with_index.base_reg); - int ireg = opr_regno(&inst->opr[0].indirect_with_index.index_reg); - int dreg = opr_regno(&inst->opr[1].reg); + int breg = opr_regno(&inst->opr[0].u.indirect_with_index.base_reg); + int ireg = opr_regno(&inst->opr[0].u.indirect_with_index.index_reg); + int dreg = opr_regno(&inst->opr[1].u.reg); int noofs = offset == 0 && breg != RBP - RAX; int pow = most_significant_bit(scale); short buf[] = { @@ -1344,13 +1344,13 @@ asm_lea_iir(struct Inst *inst, struct Code *code) static unsigned char * asm_add_rr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; + enum RegSize size = inst->opr[0].u.reg.size; unsigned char *p = code->buf; p = put_rex2( p, size, - opr_regno(&inst->opr[0].reg), - opr_regno(&inst->opr[1].reg), + opr_regno(&inst->opr[0].u.reg), + opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x00 : 0x01 ); return p; @@ -1359,11 +1359,11 @@ asm_add_rr(struct Inst *inst, struct Code *code) static unsigned char * asm_add_imr(struct Inst *inst, struct Code *code) { - long value = inst->opr[0].immediate; + long value = inst->opr[0].u.immediate; if (is_im32(value)) { - enum RegSize size = inst->opr[1].reg.size; + enum RegSize size = inst->opr[1].u.reg.size; int im8 = is_im8(value) || size == REG8; - int d = opr_regno(&inst->opr[1].reg); + int d = opr_regno(&inst->opr[1].u.reg); unsigned char *p = code->buf; if (d == RAX - RAX && (size == REG8 || !im8)) p = put_rex0(p, size, 0, d, im8 ? 0x04 : 0x05); @@ -1396,21 +1396,21 @@ asm_add_imr(struct Inst *inst, struct Code *code) static unsigned char * asm_add_ir(struct Inst *inst, struct Code *code) { - if (inst->opr[0].indirect.reg.no != RIP) { - if (inst->opr[0].indirect.offset.expr->kind == EX_FIXNUM) { - long offset = inst->opr[0].indirect.offset.expr->fixnum; - enum RegSize size = inst->opr[1].reg.size; + if (inst->opr[0].u.indirect.reg.no != RIP) { + if (inst->opr[0].u.indirect.offset.expr->kind == EX_FIXNUM) { + long offset = inst->opr[0].u.indirect.offset.expr->u.fixnum; + enum RegSize size = inst->opr[1].u.reg.size; unsigned char *p = code->buf; short buf[] = { MAKE_REX_INDIRECT( size, - opr_regno(&inst->opr[1].reg), - opr_regno(&inst->opr[0].indirect.reg), + opr_regno(&inst->opr[1].u.reg), + opr_regno(&inst->opr[0].u.indirect.reg), 0x02, 0x00, offset ), - MAKE_OFFSET(offset, inst->opr[0].indirect.reg.no != RBP - RAX), + MAKE_OFFSET(offset, inst->opr[0].u.indirect.reg.no != RBP - RAX), }; p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); return p; @@ -1419,9 +1419,9 @@ asm_add_ir(struct Inst *inst, struct Code *code) /* sym(%rip)/sym@gottpoff(%rip): symbolic rip-relative operand, * disp32 patched later once the target address (or got slot) is * known, same shape as asm_mov_ir's rip branch */ - if (inst->opr[0].indirect.offset.expr->kind != EX_FIXNUM) { - int pre = !inst->opr[1].reg.x ? 0x48 : 0x4c; - int d = inst->opr[1].reg.no; + if (inst->opr[0].u.indirect.offset.expr->kind != EX_FIXNUM) { + int pre = !inst->opr[1].u.reg.x ? 0x48 : 0x4c; + int d = inst->opr[1].u.reg.no; MAKE_CODE(inst, code, pre, 0x03, 0x05 | (d << 3), IM32(0)); return code->buf; } @@ -1433,25 +1433,25 @@ static unsigned char * asm_add_iir(struct Inst *inst, struct Code *code) { assert( - inst->opr[0].indirect_with_index.offset->kind == EX_FIXNUM - && inst->opr[0].indirect_with_index.offset->fixnum == 0 + inst->opr[0].u.indirect_with_index.offset->kind == EX_FIXNUM + && inst->opr[0].u.indirect_with_index.offset->u.fixnum == 0 ); /* TODO */ unsigned char scale = 0; - struct Expr *scale_expr = inst->opr[0].indirect_with_index.scale; + struct Expr *scale_expr = inst->opr[0].u.indirect_with_index.scale; if (scale_expr != NULL) { assert(scale_expr->kind == EX_FIXNUM); - int s = scale_expr->fixnum; + int s = scale_expr->u.fixnum; if (s <= 0 || s > 8 || !IS_POWER_OF_2(s)) return NULL; scale = most_significant_bit(s); } - unsigned char bno = inst->opr[0].indirect_with_index.base_reg.no; - unsigned char ino = inst->opr[0].indirect_with_index.index_reg.no; - unsigned char dno = inst->opr[1].reg.no; + unsigned char bno = inst->opr[0].u.indirect_with_index.base_reg.no; + unsigned char ino = inst->opr[0].u.indirect_with_index.index_reg.no; + unsigned char dno = inst->opr[1].u.reg.no; short buf[] = { - (unsigned char)0x48 | (inst->opr[0].indirect_with_index.base_reg.x) - | (inst->opr[0].indirect_with_index.index_reg.x << 1) | (inst->opr[1].reg.x << 2), + (unsigned char)0x48 | (inst->opr[0].u.indirect_with_index.base_reg.x) + | (inst->opr[0].u.indirect_with_index.index_reg.x << 1) | (inst->opr[1].u.reg.x << 2), 0x03, 0x04 | (dno << 3), bno | (ino << 3) | (scale << 6), @@ -1464,12 +1464,12 @@ asm_add_iir(struct Inst *inst, struct Code *code) static unsigned char * asm_addq_imi(struct Inst *inst, struct Code *code) { - if (inst->opr[1].indirect.offset.expr->kind == EX_FIXNUM) { - long value = inst->opr[0].immediate; + if (inst->opr[1].u.indirect.offset.expr->kind == EX_FIXNUM) { + long value = inst->opr[0].u.immediate; if (is_im32(value)) { - long offset = inst->opr[1].indirect.offset.expr->fixnum; + long offset = inst->opr[1].u.indirect.offset.expr->u.fixnum; unsigned char sno = 0; - unsigned char dno = opr_regno(&inst->opr[1].indirect.reg); + unsigned char dno = opr_regno(&inst->opr[1].u.indirect.reg); unsigned char op = (offset == 0 && (dno & 7) != RBP - RAX) ? 0x00 : is_im8(offset) ? (unsigned char)0x40 : (unsigned char)0x80; @@ -1505,13 +1505,13 @@ asm_addq_imi(struct Inst *inst, struct Code *code) static unsigned char * asm_sub_rr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; + enum RegSize size = inst->opr[0].u.reg.size; unsigned char *p = code->buf; p = put_rex2( p, size, - opr_regno(&inst->opr[0].reg), - opr_regno(&inst->opr[1].reg), + opr_regno(&inst->opr[0].u.reg), + opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x28 : 0x29 ); return p; @@ -1520,11 +1520,11 @@ asm_sub_rr(struct Inst *inst, struct Code *code) static unsigned char * asm_sub_imr(struct Inst *inst, struct Code *code) { - long value = inst->opr[0].immediate; + long value = inst->opr[0].u.immediate; if (is_im32(value)) { - enum RegSize size = inst->opr[1].reg.size; + enum RegSize size = inst->opr[1].u.reg.size; int im8 = is_im8(value) || size == REG8; - int d = opr_regno(&inst->opr[1].reg); + int d = opr_regno(&inst->opr[1].u.reg); unsigned char *p = code->buf; if (d == RAX - RAX && (size == REG8 || !im8)) p = put_rex0(p, size, 0, d, im8 ? 0x2c : 0x2d); @@ -1557,20 +1557,20 @@ asm_sub_imr(struct Inst *inst, struct Code *code) static unsigned char * asm_sub_ir(struct Inst *inst, struct Code *code) { - if (inst->opr[0].indirect.offset.expr->kind == EX_FIXNUM && inst->opr[0].indirect.reg.no != RIP) { - long offset = inst->opr[0].indirect.offset.expr->fixnum; - enum RegSize size = inst->opr[1].reg.size; + if (inst->opr[0].u.indirect.offset.expr->kind == EX_FIXNUM && inst->opr[0].u.indirect.reg.no != RIP) { + long offset = inst->opr[0].u.indirect.offset.expr->u.fixnum; + enum RegSize size = inst->opr[1].u.reg.size; unsigned char *p = code->buf; short buf[] = { MAKE_REX_INDIRECT( size, - opr_regno(&inst->opr[1].reg), - opr_regno(&inst->opr[0].indirect.reg), + opr_regno(&inst->opr[1].u.reg), + opr_regno(&inst->opr[0].u.indirect.reg), 0x2a, 0x00, offset ), - MAKE_OFFSET(offset, inst->opr[0].indirect.reg.no != RBP - RAX), + MAKE_OFFSET(offset, inst->opr[0].u.indirect.reg.no != RBP - RAX), }; p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); return p; @@ -1582,25 +1582,25 @@ static unsigned char * asm_sub_iir(struct Inst *inst, struct Code *code) { assert( - inst->opr[0].indirect_with_index.offset->kind == EX_FIXNUM - && inst->opr[0].indirect_with_index.offset->fixnum == 0 + inst->opr[0].u.indirect_with_index.offset->kind == EX_FIXNUM + && inst->opr[0].u.indirect_with_index.offset->u.fixnum == 0 ); /* TODO */ unsigned char scale = 0; - struct Expr *scale_expr = inst->opr[0].indirect_with_index.scale; + struct Expr *scale_expr = inst->opr[0].u.indirect_with_index.scale; if (scale_expr != NULL) { assert(scale_expr->kind == EX_FIXNUM); - int s = scale_expr->fixnum; + int s = scale_expr->u.fixnum; if (s <= 0 || s > 8 || !IS_POWER_OF_2(s)) return NULL; scale = most_significant_bit(s); } - unsigned char bno = inst->opr[0].indirect_with_index.base_reg.no; - unsigned char ino = inst->opr[0].indirect_with_index.index_reg.no; - unsigned char dno = inst->opr[1].reg.no; + unsigned char bno = inst->opr[0].u.indirect_with_index.base_reg.no; + unsigned char ino = inst->opr[0].u.indirect_with_index.index_reg.no; + unsigned char dno = inst->opr[1].u.reg.no; short buf[] = { - (unsigned char)0x48 | (inst->opr[0].indirect_with_index.base_reg.x) - | (inst->opr[0].indirect_with_index.index_reg.x << 1) | (inst->opr[1].reg.x << 2), + (unsigned char)0x48 | (inst->opr[0].u.indirect_with_index.base_reg.x) + | (inst->opr[0].u.indirect_with_index.index_reg.x << 1) | (inst->opr[1].u.reg.x << 2), 0x2b, 0x04 | (dno << 3), bno | (ino << 3) | (scale << 6), @@ -1613,12 +1613,12 @@ asm_sub_iir(struct Inst *inst, struct Code *code) static unsigned char * asm_subq_imi(struct Inst *inst, struct Code *code) { - if (inst->opr[1].indirect.offset.expr->kind == EX_FIXNUM) { - long value = inst->opr[0].immediate; + if (inst->opr[1].u.indirect.offset.expr->kind == EX_FIXNUM) { + long value = inst->opr[0].u.immediate; if (is_im32(value)) { - long offset = inst->opr[1].indirect.offset.expr->fixnum; + long offset = inst->opr[1].u.indirect.offset.expr->u.fixnum; unsigned char sno = 0; - unsigned char dno = opr_regno(&inst->opr[1].indirect.reg); + unsigned char dno = opr_regno(&inst->opr[1].u.indirect.reg); unsigned char op = (offset == 0 && (dno & 7) != RBP - RAX) ? 0x28 : is_im8(offset) ? (unsigned char)0x40 : (unsigned char)0x80; @@ -1654,11 +1654,11 @@ asm_subq_imi(struct Inst *inst, struct Code *code) static unsigned char * asm_mul_r(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; + enum RegSize size = inst->opr[0].u.reg.size; unsigned char *p = code->buf; short buf[] = { - MAKE_REX0(size, 0, opr_regno(&inst->opr[0].reg), 0xf6 | (size == REG8 ? 0 : 1)), - 0xe0 | inst->opr[0].reg.no, + MAKE_REX0(size, 0, opr_regno(&inst->opr[0].u.reg), 0xf6 | (size == REG8 ? 0 : 1)), + 0xe0 | inst->opr[0].u.reg.no, }; p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); return p; @@ -1667,11 +1667,11 @@ asm_mul_r(struct Inst *inst, struct Code *code) static unsigned char * asm_div_r(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; + enum RegSize size = inst->opr[0].u.reg.size; unsigned char *p = code->buf; short buf[] = { - MAKE_REX0(size, 0, opr_regno(&inst->opr[0].reg), 0xf6 | (size == REG8 ? 0 : 1)), - 0xf0 | inst->opr[0].reg.no, + MAKE_REX0(size, 0, opr_regno(&inst->opr[0].u.reg), 0xf6 | (size == REG8 ? 0 : 1)), + 0xf0 | inst->opr[0].u.reg.no, }; p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); return p; @@ -1680,11 +1680,11 @@ asm_div_r(struct Inst *inst, struct Code *code) static unsigned char * asm_idiv_r(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; + enum RegSize size = inst->opr[0].u.reg.size; unsigned char *p = code->buf; short buf[] = { - MAKE_REX0(size, 0, opr_regno(&inst->opr[0].reg), 0xf6 | (size == REG8 ? 0 : 1)), - 0xf8 | inst->opr[0].reg.no, + MAKE_REX0(size, 0, opr_regno(&inst->opr[0].u.reg), 0xf6 | (size == REG8 ? 0 : 1)), + 0xf8 | inst->opr[0].u.reg.no, }; p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); return p; @@ -1694,7 +1694,7 @@ static unsigned char * asm_neg_r(struct Inst *inst, struct Code *code) { unsigned char *p = code->buf; - p = put_rex1(p, inst->opr[0].reg.size, 0xd8, opr_regno(&inst->opr[0].reg), 0xf7); + p = put_rex1(p, inst->opr[0].u.reg.size, 0xd8, opr_regno(&inst->opr[0].u.reg), 0xf7); return p; } @@ -1702,7 +1702,7 @@ static unsigned char * asm_not_r(struct Inst *inst, struct Code *code) { unsigned char *p = code->buf; - p = put_rex1(p, inst->opr[0].reg.size, 0xd0, opr_regno(&inst->opr[0].reg), 0xf7); + p = put_rex1(p, inst->opr[0].u.reg.size, 0xd0, opr_regno(&inst->opr[0].u.reg), 0xf7); return p; } @@ -1710,7 +1710,7 @@ static unsigned char * asm_inc_r(struct Inst *inst, struct Code *code) { unsigned char *p = code->buf; - struct Reg *reg = &inst->opr[0].reg; + struct Reg *reg = &inst->opr[0].u.reg; p = put_rex1(p, reg->size, 0xc0, opr_regno(reg), reg->size == REG8 ? 0xfe : 0xff); return p; } @@ -1718,13 +1718,13 @@ asm_inc_r(struct Inst *inst, struct Code *code) static unsigned char * asm_incbwlq_i(struct Inst *inst, struct Code *code) { - if (inst->opr[0].indirect.reg.no != RIP) { + if (inst->opr[0].u.indirect.reg.no != RIP) { enum RegSize size = inst->op + (REG8 - INCB); - long offset = inst->opr[0].indirect.offset.expr->fixnum; + long offset = inst->opr[0].u.indirect.offset.expr->u.fixnum; unsigned char *p = code->buf; short buf[] = { - MAKE_REX_INDIRECT(size, 0, opr_regno(&inst->opr[0].indirect.reg), 0xfe, 0x00, offset), - MAKE_OFFSET(offset, inst->opr[0].indirect.reg.no != RBP - RAX), + MAKE_REX_INDIRECT(size, 0, opr_regno(&inst->opr[0].u.indirect.reg), 0xfe, 0x00, offset), + MAKE_OFFSET(offset, inst->opr[0].u.indirect.reg.no != RBP - RAX), }; p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); return p; @@ -1736,7 +1736,7 @@ static unsigned char * asm_dec_r(struct Inst *inst, struct Code *code) { unsigned char *p = code->buf; - struct Reg *reg = &inst->opr[0].reg; + struct Reg *reg = &inst->opr[0].u.reg; p = put_rex1(p, reg->size, 0xc8, opr_regno(reg), reg->size == REG8 ? 0xfe : 0xff); return p; } @@ -1744,13 +1744,13 @@ asm_dec_r(struct Inst *inst, struct Code *code) static unsigned char * asm_decbwlq_i(struct Inst *inst, struct Code *code) { - if (inst->opr[0].indirect.reg.no != RIP) { + if (inst->opr[0].u.indirect.reg.no != RIP) { enum RegSize size = inst->op + (REG8 - DECB); - long offset = inst->opr[0].indirect.offset.expr->fixnum; + long offset = inst->opr[0].u.indirect.offset.expr->u.fixnum; unsigned char *p = code->buf; short buf[] = { - MAKE_REX_INDIRECT(size, 1, opr_regno(&inst->opr[0].indirect.reg), 0xfe, 0x08, offset), - MAKE_OFFSET(offset, inst->opr[0].indirect.reg.no != RBP - RAX), + MAKE_REX_INDIRECT(size, 1, opr_regno(&inst->opr[0].u.indirect.reg), 0xfe, 0x08, offset), + MAKE_OFFSET(offset, inst->opr[0].u.indirect.reg.no != RBP - RAX), }; p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); return p; @@ -1761,13 +1761,13 @@ asm_decbwlq_i(struct Inst *inst, struct Code *code) static unsigned char * asm_and_rr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[1].reg.size; + enum RegSize size = inst->opr[1].u.reg.size; unsigned char *p = code->buf; p = put_rex2( p, size, - opr_regno(&inst->opr[0].reg), - opr_regno(&inst->opr[1].reg), + opr_regno(&inst->opr[0].u.reg), + opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x20 : 0x21 ); return p; @@ -1776,18 +1776,18 @@ asm_and_rr(struct Inst *inst, struct Code *code) static unsigned char * asm_and_imr(struct Inst *inst, struct Code *code) { - long value = inst->opr[0].immediate; - enum RegSize size = inst->opr[1].reg.size; + long value = inst->opr[0].u.immediate; + enum RegSize size = inst->opr[1].u.reg.size; unsigned char *p = code->buf; - if (is_im8(value) && (size != REG8 || opr_regno(&inst->opr[1].reg) != AL - AL)) { - p = put_rex1(p, size, 0xe0, opr_regno(&inst->opr[1].reg), size == REG8 ? 0x80 : 0x83); + if (is_im8(value) && (size != REG8 || opr_regno(&inst->opr[1].u.reg) != AL - AL)) { + p = put_rex1(p, size, 0xe0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x80 : 0x83); *p++ = IM8(value); return p; } else if (size <= REG32 || is_im32(value)) { - if (opr_regno(&inst->opr[1].reg) == RAX - RAX) { - p = put_rex0(p, size, 0, opr_regno(&inst->opr[1].reg), size == REG8 ? 0x24 : 0x25); + if (opr_regno(&inst->opr[1].u.reg) == RAX - RAX) { + p = put_rex0(p, size, 0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x24 : 0x25); } else { - p = put_rex1(p, size, 0xe0, opr_regno(&inst->opr[1].reg), size == REG8 ? 0x80 : 0x81); + p = put_rex1(p, size, 0xe0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x80 : 0x81); } switch (size) { @@ -1813,13 +1813,13 @@ asm_and_imr(struct Inst *inst, struct Code *code) static unsigned char * asm_or_rr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[1].reg.size; + enum RegSize size = inst->opr[1].u.reg.size; unsigned char *p = code->buf; p = put_rex2( p, size, - opr_regno(&inst->opr[0].reg), - opr_regno(&inst->opr[1].reg), + opr_regno(&inst->opr[0].u.reg), + opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x08 : 0x09 ); return p; @@ -1828,18 +1828,18 @@ asm_or_rr(struct Inst *inst, struct Code *code) static unsigned char * asm_or_imr(struct Inst *inst, struct Code *code) { - long value = inst->opr[0].immediate; - enum RegSize size = inst->opr[1].reg.size; + long value = inst->opr[0].u.immediate; + enum RegSize size = inst->opr[1].u.reg.size; unsigned char *p = code->buf; - if (is_im8(value) && (size != REG8 || opr_regno(&inst->opr[1].reg) != AL - AL)) { - p = put_rex1(p, size, 0xc8, opr_regno(&inst->opr[1].reg), size == REG8 ? 0x80 : 0x83); + if (is_im8(value) && (size != REG8 || opr_regno(&inst->opr[1].u.reg) != AL - AL)) { + p = put_rex1(p, size, 0xc8, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x80 : 0x83); *p++ = IM8(value); return p; } else if (size <= REG32 || is_im32(value)) { - if (opr_regno(&inst->opr[1].reg) == RAX - RAX) { - p = put_rex0(p, size, 0, opr_regno(&inst->opr[1].reg), size == REG8 ? 0x0c : 0x0d); + if (opr_regno(&inst->opr[1].u.reg) == RAX - RAX) { + p = put_rex0(p, size, 0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x0c : 0x0d); } else { - p = put_rex1(p, size, 0xc8, opr_regno(&inst->opr[1].reg), size == REG8 ? 0x80 : 0x81); + p = put_rex1(p, size, 0xc8, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x80 : 0x81); } switch (size) { case REG8: @@ -1864,13 +1864,13 @@ asm_or_imr(struct Inst *inst, struct Code *code) static unsigned char * asm_xor_rr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[1].reg.size; + enum RegSize size = inst->opr[1].u.reg.size; unsigned char *p = code->buf; p = put_rex2( p, size, - opr_regno(&inst->opr[0].reg), - opr_regno(&inst->opr[1].reg), + opr_regno(&inst->opr[0].u.reg), + opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x30 : 0x31 ); return p; @@ -1879,18 +1879,18 @@ asm_xor_rr(struct Inst *inst, struct Code *code) static unsigned char * asm_xor_imr(struct Inst *inst, struct Code *code) { - long value = inst->opr[0].immediate; - enum RegSize size = inst->opr[1].reg.size; + long value = inst->opr[0].u.immediate; + enum RegSize size = inst->opr[1].u.reg.size; unsigned char *p = code->buf; - if (is_im8(value) && (size != REG8 || opr_regno(&inst->opr[1].reg) != AL - AL)) { - p = put_rex1(p, size, 0xf0, opr_regno(&inst->opr[1].reg), size == REG8 ? 0x80 : 0x83); + if (is_im8(value) && (size != REG8 || opr_regno(&inst->opr[1].u.reg) != AL - AL)) { + p = put_rex1(p, size, 0xf0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x80 : 0x83); *p++ = IM8(value); return p; } else if (size <= REG32 || is_im32(value)) { - if (opr_regno(&inst->opr[1].reg) == RAX - RAX) { - p = put_rex0(p, size, 0, opr_regno(&inst->opr[1].reg), size == REG8 ? 0x34 : 0x35); + if (opr_regno(&inst->opr[1].u.reg) == RAX - RAX) { + p = put_rex0(p, size, 0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x34 : 0x35); } else { - p = put_rex1(p, size, 0xf0, opr_regno(&inst->opr[1].reg), size == REG8 ? 0x80 : 0x81); + p = put_rex1(p, size, 0xf0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x80 : 0x81); } switch (size) { case REG8: @@ -1915,24 +1915,24 @@ asm_xor_imr(struct Inst *inst, struct Code *code) static unsigned char * asm_shl_rr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[1].reg.size; + enum RegSize size = inst->opr[1].u.reg.size; unsigned char *p = code->buf; - p = put_rex1(p, size, 0xe0, opr_regno(&inst->opr[1].reg), size == REG8 ? 0xd2 : 0xd3); + p = put_rex1(p, size, 0xe0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xd2 : 0xd3); return p; } static unsigned char * asm_shl_imr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[1].reg.size; + enum RegSize size = inst->opr[1].u.reg.size; unsigned char *p = code->buf; - uint64_t shift = inst->opr[0].immediate; + uint64_t shift = inst->opr[0].u.immediate; if (shift >= 256) return NULL; if (shift == 1) { - p = put_rex1(p, size, 0xe0, opr_regno(&inst->opr[1].reg), size == REG8 ? 0xd0 : 0xd1); + p = put_rex1(p, size, 0xe0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xd0 : 0xd1); } else { - p = put_rex1(p, size, 0xe0, opr_regno(&inst->opr[1].reg), size == REG8 ? 0xc0 : 0xc1); + p = put_rex1(p, size, 0xe0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xc0 : 0xc1); *p++ = IM8(shift); } return p; @@ -1941,24 +1941,24 @@ asm_shl_imr(struct Inst *inst, struct Code *code) static unsigned char * asm_shr_rr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[1].reg.size; + enum RegSize size = inst->opr[1].u.reg.size; unsigned char *p = code->buf; - p = put_rex1(p, size, 0xe8, opr_regno(&inst->opr[1].reg), size == REG8 ? 0xd2 : 0xd3); + p = put_rex1(p, size, 0xe8, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xd2 : 0xd3); return p; } static unsigned char * asm_shr_imr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[1].reg.size; + enum RegSize size = inst->opr[1].u.reg.size; unsigned char *p = code->buf; - uint64_t shift = inst->opr[0].immediate; + uint64_t shift = inst->opr[0].u.immediate; if (shift >= 256) return NULL; if (shift == 1) { - p = put_rex1(p, size, 0xe8, opr_regno(&inst->opr[1].reg), size == REG8 ? 0xd0 : 0xd1); + p = put_rex1(p, size, 0xe8, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xd0 : 0xd1); } else { - p = put_rex1(p, size, 0xe8, opr_regno(&inst->opr[1].reg), size == REG8 ? 0xc0 : 0xc1); + p = put_rex1(p, size, 0xe8, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xc0 : 0xc1); *p++ = IM8(shift); } return p; @@ -1967,24 +1967,24 @@ asm_shr_imr(struct Inst *inst, struct Code *code) static unsigned char * asm_sar_rr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[1].reg.size; + enum RegSize size = inst->opr[1].u.reg.size; unsigned char *p = code->buf; - p = put_rex1(p, size, 0xf8, opr_regno(&inst->opr[1].reg), size == REG8 ? 0xd2 : 0xd3); + p = put_rex1(p, size, 0xf8, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xd2 : 0xd3); return p; } static unsigned char * asm_sar_imr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[1].reg.size; + enum RegSize size = inst->opr[1].u.reg.size; unsigned char *p = code->buf; - uint64_t shift = inst->opr[0].immediate; + uint64_t shift = inst->opr[0].u.immediate; if (shift >= 256) return NULL; if (shift == 1) { - p = put_rex1(p, size, 0xf8, opr_regno(&inst->opr[1].reg), size == REG8 ? 0xd0 : 0xd1); + p = put_rex1(p, size, 0xf8, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xd0 : 0xd1); } else { - p = put_rex1(p, size, 0xf8, opr_regno(&inst->opr[1].reg), size == REG8 ? 0xc0 : 0xc1); + p = put_rex1(p, size, 0xf8, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xc0 : 0xc1); *p++ = IM8(shift); } return p; @@ -1993,13 +1993,13 @@ asm_sar_imr(struct Inst *inst, struct Code *code) static unsigned char * asm_cmp_rr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; + enum RegSize size = inst->opr[0].u.reg.size; unsigned char *p = code->buf; p = put_rex2( p, size, - opr_regno(&inst->opr[0].reg), - opr_regno(&inst->opr[1].reg), + opr_regno(&inst->opr[0].u.reg), + opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x38 : 0x39 ); return p; @@ -2008,11 +2008,11 @@ asm_cmp_rr(struct Inst *inst, struct Code *code) static unsigned char * asm_cmp_imr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[1].reg.size; - long value = signed_immediate(inst->opr[0].immediate, size); + enum RegSize size = inst->opr[1].u.reg.size; + long value = signed_immediate(inst->opr[0].u.immediate, size); if (is_im32(value) || size <= REG32) { int im8 = is_im8(value); - int d = opr_regno(&inst->opr[1].reg); + int d = opr_regno(&inst->opr[1].u.reg); unsigned char *p = code->buf; if (d == RAX - RAX && (size == REG8 || !im8)) p = put_rex0(p, size, 0, d, size == REG8 ? 0x3c : 0x3d); @@ -2045,13 +2045,13 @@ asm_cmp_imr(struct Inst *inst, struct Code *code) static unsigned char * asm_test_rr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; + enum RegSize size = inst->opr[0].u.reg.size; unsigned char *p = code->buf; p = put_rex2( p, size, - opr_regno(&inst->opr[0].reg), - opr_regno(&inst->opr[1].reg), + opr_regno(&inst->opr[0].u.reg), + opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x84 : 0x85 ); return p; @@ -2081,13 +2081,13 @@ asm_cqto(struct Inst *inst, struct Code *code) static unsigned char * asm_bsr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; - int sno = opr_regno(&inst->opr[0].reg); - int dno = opr_regno(&inst->opr[1].reg); + enum RegSize size = inst->opr[0].u.reg.size; + int sno = opr_regno(&inst->opr[0].u.reg); + int dno = opr_regno(&inst->opr[1].u.reg); short buf[] = { MAKE_REX0(size, dno, sno, 0x0f), 0xbd, - 0xc0 | (inst->opr[1].reg.no << 3) | inst->opr[0].reg.no, + 0xc0 | (inst->opr[1].u.reg.no << 3) | inst->opr[0].u.reg.no, }; unsigned char *p = code->buf; p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); @@ -2097,10 +2097,10 @@ asm_bsr(struct Inst *inst, struct Code *code) static unsigned char * asm_tzcnt(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; + enum RegSize size = inst->opr[0].u.reg.size; unsigned char prefix = 0xf3; - int sno = opr_regno(&inst->opr[0].reg); - int dno = opr_regno(&inst->opr[1].reg); + int sno = opr_regno(&inst->opr[0].u.reg); + int dno = opr_regno(&inst->opr[1].u.reg); short buf[] = { (size) == REG16 ? 0x66 : -1, prefix, @@ -2119,10 +2119,10 @@ asm_tzcnt(struct Inst *inst, struct Code *code) static unsigned char * asm_popcnt(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; + enum RegSize size = inst->opr[0].u.reg.size; unsigned char prefix = 0xf3; - int sno = opr_regno(&inst->opr[0].reg); - int dno = opr_regno(&inst->opr[1].reg); + int sno = opr_regno(&inst->opr[0].u.reg); + int dno = opr_regno(&inst->opr[1].u.reg); short buf[] = { (size) == REG16 ? 0x66 : -1, prefix, @@ -2143,9 +2143,9 @@ asm_set_r(struct Inst *inst, struct Code *code) { unsigned char *p = code->buf; short buf[] = { - MAKE_REX0(REG8, 0, opr_regno(&inst->opr[0].reg), 0x0f), + MAKE_REX0(REG8, 0, opr_regno(&inst->opr[0].u.reg), 0x0f), 0x90 | (inst->op - SETO), - 0xc0 | inst->opr[0].reg.no, + 0xc0 | inst->opr[0].u.reg.no, }; p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); return p; @@ -2156,7 +2156,7 @@ asm_push_r(struct Inst *inst, struct Code *code) { unsigned char *p = code->buf; short buf[] = { - MAKE_REX0(REG32, 0, opr_regno(&inst->opr[0].reg), 0x50 | inst->opr[0].reg.no), + MAKE_REX0(REG32, 0, opr_regno(&inst->opr[0].u.reg), 0x50 | inst->opr[0].u.reg.no), }; p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); return p; @@ -2166,7 +2166,7 @@ static unsigned char * asm_push_im(struct Inst *inst, struct Code *code) { unsigned char *p = code->buf; - long value = inst->opr[0].immediate; + long value = inst->opr[0].u.immediate; if (is_im8(value)) { *p++ = 0x6a; *p++ = IM8(value); @@ -2185,7 +2185,7 @@ asm_pop_r(struct Inst *inst, struct Code *code) { unsigned char *p = code->buf; short buf[] = { - MAKE_REX0(REG32, 0, opr_regno(&inst->opr[0].reg), 0x58 | inst->opr[0].reg.no), + MAKE_REX0(REG32, 0, opr_regno(&inst->opr[0].u.reg), 0x58 | inst->opr[0].u.reg.no), }; p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); return p; @@ -2201,9 +2201,9 @@ asm_jmp_d(struct Inst *inst, struct Code *code) static unsigned char * asm_jmp_der(struct Inst *inst, struct Code *code) { - int s = inst->opr[0].reg.no; + int s = inst->opr[0].u.reg.no; short buf[] = { - inst->opr[0].reg.x ? 0x41 : -1, + inst->opr[0].u.reg.x ? 0x41 : -1, 0xff, 0xe0 | s, }; @@ -2215,14 +2215,14 @@ asm_jmp_der(struct Inst *inst, struct Code *code) static unsigned char * asm_jmp_dei(struct Inst *inst, struct Code *code) { - struct Expr *offset_expr = inst->opr[0].indirect.offset.expr; + struct Expr *offset_expr = inst->opr[0].u.indirect.offset.expr; if ((offset_expr == NULL || offset_expr->kind == EX_FIXNUM)) { - long offset = offset_expr != NULL ? offset_expr->fixnum : 0; + long offset = offset_expr != NULL ? offset_expr->u.fixnum : 0; if (is_im32(offset)) { short offset_bit = offset == 0 ? 0x20 : is_im8(offset) ? 0x60 : 0xa0; - short b = inst->opr[0].indirect.reg.no; + short b = inst->opr[0].u.indirect.reg.no; short buf[] = { - inst->opr[0].indirect.reg.x ? 0x41 : -1, + inst->opr[0].u.indirect.reg.x ? 0x41 : -1, 0xff, offset_bit | b, }; @@ -2245,18 +2245,18 @@ asm_jmp_dei(struct Inst *inst, struct Code *code) static unsigned char * asm_jmp_deii(struct Inst *inst, struct Code *code) { - struct Expr *offset_expr = inst->opr[0].indirect_with_index.offset; - struct Expr *scale_expr = inst->opr[0].indirect_with_index.scale; + struct Expr *offset_expr = inst->opr[0].u.indirect_with_index.offset; + struct Expr *scale_expr = inst->opr[0].u.indirect_with_index.scale; if ((offset_expr == NULL || offset_expr->kind == EX_FIXNUM) && (scale_expr == NULL || scale_expr->kind == EX_FIXNUM)) { - long offset = offset_expr != NULL ? offset_expr->fixnum : 0; - long scale = scale_expr != NULL ? scale_expr->fixnum : 1; + long offset = offset_expr != NULL ? offset_expr->u.fixnum : 0; + long scale = scale_expr != NULL ? scale_expr->u.fixnum : 1; if (is_im32(offset) && 1 <= scale && scale <= 8 && IS_POWER_OF_2(scale)) { - short b = inst->opr[0].indirect_with_index.base_reg.no; + short b = inst->opr[0].u.indirect_with_index.base_reg.no; short scale_bit = most_significant_bit(scale); - short i = inst->opr[0].indirect_with_index.index_reg.no; - short prefix = inst->opr[0].indirect_with_index.base_reg.x - | (inst->opr[0].indirect_with_index.index_reg.x << 1); + short i = inst->opr[0].u.indirect_with_index.index_reg.no; + short prefix = inst->opr[0].u.indirect_with_index.base_reg.x + | (inst->opr[0].u.indirect_with_index.index_reg.x << 1); short offset_bit = offset == 0 && b != RBP - RAX ? 0x20 : is_im8(offset) ? 0x60 : 0xa0; short buf[] = { prefix != 0 ? 0x40 | prefix : -1, @@ -2297,8 +2297,8 @@ asm_call_d(struct Inst *inst, struct Code *code) static unsigned char * asm_call_der(struct Inst *inst, struct Code *code) { - int s = inst->opr[0].reg.no; - if (!inst->opr[0].reg.x) { + int s = inst->opr[0].u.reg.no; + if (!inst->opr[0].u.reg.x) { MAKE_CODE(inst, code, 0xff, 0xd0 + s); } else { MAKE_CODE(inst, code, 0x41, 0xff, 0xd0 + s); @@ -2316,7 +2316,7 @@ asm_ret(struct Inst *inst, struct Code *code) static unsigned char * asm_int_im(struct Inst *inst, struct Code *code) { - long value = inst->opr[0].immediate; + long value = inst->opr[0].u.immediate; MAKE_CODE(inst, code, 0xcd, IM8(value)); return code->buf; } @@ -2411,11 +2411,11 @@ asm_simple(struct Inst *inst, struct Code *code) static unsigned char * asm_imul_r(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; + enum RegSize size = inst->opr[0].u.reg.size; unsigned char *p = code->buf; short buf[] = { - MAKE_REX0(size, 0, opr_regno(&inst->opr[0].reg), 0xf6 | (size == REG8 ? 0 : 1)), - 0xe8 | inst->opr[0].reg.no, + MAKE_REX0(size, 0, opr_regno(&inst->opr[0].u.reg), 0xf6 | (size == REG8 ? 0 : 1)), + 0xe8 | inst->opr[0].u.reg.no, }; p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); return p; @@ -2428,10 +2428,10 @@ asm_imul_r(struct Inst *inst, struct Code *code) static unsigned char * asm_imul_rr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; + enum RegSize size = inst->opr[0].u.reg.size; unsigned char *p = code->buf; - int sno = opr_regno(&inst->opr[0].reg); - int dno = opr_regno(&inst->opr[1].reg); + int sno = opr_regno(&inst->opr[0].u.reg); + int dno = opr_regno(&inst->opr[1].u.reg); short buf[] = { MAKE_REX0(size, dno, sno, 0x0f), 0xaf, @@ -2445,9 +2445,9 @@ asm_imul_rr(struct Inst *inst, struct Code *code) static unsigned char * asm_imul_irr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[1].reg.size; - int dno = opr_regno(&inst->opr[1].reg); - long value = inst->opr[0].immediate; + enum RegSize size = inst->opr[1].u.reg.size; + int dno = opr_regno(&inst->opr[1].u.reg); + long value = inst->opr[0].u.immediate; int im8 = is_im8(value); short buf[] = { MAKE_REX0(size, dno, dno, im8 ? 0x6b : 0x69), @@ -2474,8 +2474,8 @@ asm_leave(struct Inst *inst, struct Code *code) static unsigned char * asm_enter(struct Inst *inst, struct Code *code) { - long size = inst->opr[0].immediate; - long depth = inst->opr[1].immediate; + long size = inst->opr[0].u.immediate; + long depth = inst->opr[1].u.immediate; MAKE_CODE(inst, code, 0xc8, IM16(size), IM8(depth)); return code->buf; } @@ -2484,13 +2484,13 @@ asm_enter(struct Inst *inst, struct Code *code) static unsigned char * asm_xchg_rr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; + enum RegSize size = inst->opr[0].u.reg.size; unsigned char *p = code->buf; p = put_rex2( p, size, - opr_regno(&inst->opr[0].reg), - opr_regno(&inst->opr[1].reg), + opr_regno(&inst->opr[0].u.reg), + opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x86 : 0x87 ); return p; @@ -2508,9 +2508,9 @@ asm_xchg_ri(struct Inst *inst, struct Code *code) static unsigned char * asm_bsf(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; - int sno = opr_regno(&inst->opr[0].reg); - int dno = opr_regno(&inst->opr[1].reg); + enum RegSize size = inst->opr[0].u.reg.size; + int sno = opr_regno(&inst->opr[0].u.reg); + int dno = opr_regno(&inst->opr[1].u.reg); short buf[] = { MAKE_REX0(size, dno, sno, 0x0f), 0xbc, @@ -2525,8 +2525,8 @@ asm_bsf(struct Inst *inst, struct Code *code) static unsigned char * asm_bswap(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; - int dno = opr_regno(&inst->opr[0].reg); + enum RegSize size = inst->opr[0].u.reg.size; + int dno = opr_regno(&inst->opr[0].u.reg); if (size != REG32 && size != REG64) return NULL; short buf[] = { @@ -2542,9 +2542,9 @@ asm_bswap(struct Inst *inst, struct Code *code) static unsigned char * asm_xadd_rr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; - int sno = opr_regno(&inst->opr[0].reg); - int dno = opr_regno(&inst->opr[1].reg); + enum RegSize size = inst->opr[0].u.reg.size; + int sno = opr_regno(&inst->opr[0].u.reg); + int dno = opr_regno(&inst->opr[1].u.reg); short buf[] = { MAKE_REX0(size, sno, dno, 0x0f), size == REG8 ? 0xc0 : 0xc1, @@ -2559,9 +2559,9 @@ asm_xadd_rr(struct Inst *inst, struct Code *code) static unsigned char * asm_cmpxchg_rr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; - int sno = opr_regno(&inst->opr[0].reg); - int dno = opr_regno(&inst->opr[1].reg); + enum RegSize size = inst->opr[0].u.reg.size; + int sno = opr_regno(&inst->opr[0].u.reg); + int dno = opr_regno(&inst->opr[1].u.reg); short buf[] = { MAKE_REX0(size, sno, dno, 0x0f), size == REG8 ? 0xb0 : 0xb1, @@ -2605,9 +2605,9 @@ asm_bt_family(struct Inst *inst, struct Code *code) int idx = inst->op - BT_RR; if (inst->opr[0].type == IMMEDIATE) { - enum RegSize size = inst->opr[1].reg.size; - int dno = opr_regno(&inst->opr[1].reg); - long value = inst->opr[0].immediate; + enum RegSize size = inst->opr[1].u.reg.size; + int dno = opr_regno(&inst->opr[1].u.reg); + long value = inst->opr[0].u.immediate; short buf[] = { MAKE_REX0(size, imm_digits[idx], dno, 0x0f), 0xba, @@ -2619,9 +2619,9 @@ asm_bt_family(struct Inst *inst, struct Code *code) return p; } - enum RegSize size = inst->opr[0].reg.size; - int sno = opr_regno(&inst->opr[0].reg); - int dno = opr_regno(&inst->opr[1].reg); + enum RegSize size = inst->opr[0].u.reg.size; + int sno = opr_regno(&inst->opr[0].u.reg); + int dno = opr_regno(&inst->opr[1].u.reg); short buf[] = { MAKE_REX0(size, sno, dno, 0x0f), rr_opcodes[idx], @@ -2636,9 +2636,9 @@ asm_bt_family(struct Inst *inst, struct Code *code) static unsigned char * asm_cmov_rr(struct Inst *inst, struct Code *code) { - enum RegSize size = inst->opr[0].reg.size; - int sno = opr_regno(&inst->opr[0].reg); - int dno = opr_regno(&inst->opr[1].reg); + enum RegSize size = inst->opr[0].u.reg.size; + int sno = opr_regno(&inst->opr[0].u.reg); + int dno = opr_regno(&inst->opr[1].u.reg); short buf[] = { MAKE_REX0(size, sno, dno, 0x0f), 0x40 + (inst->op - CMOVO), diff --git a/cmd/dev/as/arch/x64/inst.h b/cmd/dev/as/arch/x64/inst.h @@ -667,7 +667,7 @@ struct Operand { enum RegType reg; struct ExprWithFlag offset; } segment; - }; + } u; }; struct Inst { diff --git a/cmd/dev/as/arch/x64/parse_x64.c b/cmd/dev/as/arch/x64/parse_x64.c @@ -771,7 +771,7 @@ parse_direct_register(struct ParseInfo *info, struct Operand *operand) enum RegXmmType regxmm = find_xmm_register(&info->p); if (regxmm != NOREGXMM) { operand->type = REG_XMM; - operand->regxmm = regxmm; + operand->u.regxmm = regxmm; return XMM; } } @@ -789,9 +789,9 @@ parse_direct_register(struct ParseInfo *info, struct Operand *operand) } } operand->type = SEGMENT_OFFSET; - operand->segment.reg = reg; - operand->segment.offset.expr = offset; - operand->segment.offset.flag = flag; + operand->u.segment.reg = reg; + operand->u.segment.offset.expr = offset; + operand->u.segment.offset.flag = flag; return SEG; } @@ -815,9 +815,9 @@ parse_direct_register(struct ParseInfo *info, struct Operand *operand) } operand->type = REG; - operand->reg.size = size; - operand->reg.no = no & 7; - operand->reg.x = no >> 3; + operand->u.reg.size = size; + operand->u.reg.no = no & 7; + operand->u.reg.x = no >> 3; return (R8 << (size - REG8)) | (reg == CL ? R8CL : 0); } @@ -897,26 +897,26 @@ parse_indirect_register( if (index_reg == NOREG) { char no = base_reg - RAX; operand->type = INDIRECT; - operand->indirect.reg.size = REG64; - operand->indirect.reg.no = base_reg != RIP ? no & 7 : RIP; - operand->indirect.reg.x = (no & 8) >> 3; - operand->indirect.offset = *offset; + operand->u.indirect.reg.size = REG64; + operand->u.indirect.reg.no = base_reg != RIP ? no & 7 : RIP; + operand->u.indirect.reg.x = (no & 8) >> 3; + operand->u.indirect.offset = *offset; return IND; } else { if (!is_reg64(index_reg)) parse_error(info, "register expected"); operand->type = INDIRECT_WITH_INDEX; - operand->indirect_with_index.offset = offset->expr; - operand->indirect_with_index.scale = scale; + operand->u.indirect_with_index.offset = offset->expr; + operand->u.indirect_with_index.scale = scale; char base_no = base_reg - RAX; - operand->indirect_with_index.base_reg.size = REG64; - operand->indirect_with_index.base_reg.no = base_no & 7; - operand->indirect_with_index.base_reg.x = (base_no & 8) >> 3; + operand->u.indirect_with_index.base_reg.size = REG64; + operand->u.indirect_with_index.base_reg.no = base_no & 7; + operand->u.indirect_with_index.base_reg.x = (base_no & 8) >> 3; char index_no = index_reg - RAX; - operand->indirect_with_index.index_reg.size = REG64; - operand->indirect_with_index.index_reg.no = index_no & 7; - operand->indirect_with_index.index_reg.x = (index_no & 8) >> 3; + operand->u.indirect_with_index.index_reg.size = REG64; + operand->u.indirect_with_index.index_reg.no = index_no & 7; + operand->u.indirect_with_index.index_reg.x = (index_no & 8) >> 3; return IIND; } } @@ -930,9 +930,9 @@ parse_deref_register(struct ParseInfo *info, struct Operand *operand) char no = reg - RAX; operand->type = DEREF_REG; - operand->reg.size = REG64; - operand->reg.no = no & 7; - operand->reg.x = (no & 8) >> 3; + operand->u.reg.size = REG64; + operand->u.reg.no = no & 7; + operand->u.reg.x = (no & 8) >> 3; return 1; } @@ -980,26 +980,26 @@ parse_deref_indirect(struct ParseInfo *info, struct Operand *operand) if (index_reg == NOREG) { operand->type = DEREF_INDIRECT; - operand->indirect.offset.expr = offset; - operand->indirect.offset.flag = 0; + operand->u.indirect.offset.expr = offset; + operand->u.indirect.offset.flag = 0; char reg_no = base_reg - RAX; - operand->indirect.reg.size = REG64; - operand->indirect.reg.no = reg_no & 7; - operand->indirect.reg.x = (reg_no & 8) >> 3; + operand->u.indirect.reg.size = REG64; + operand->u.indirect.reg.no = reg_no & 7; + operand->u.indirect.reg.x = (reg_no & 8) >> 3; return DEI; } else { operand->type = DEREF_INDIRECT_WITH_INDEX; - operand->indirect_with_index.offset = offset; - operand->indirect_with_index.scale = scale; + operand->u.indirect_with_index.offset = offset; + operand->u.indirect_with_index.scale = scale; char base_no = base_reg - RAX; - operand->indirect_with_index.base_reg.size = REG64; - operand->indirect_with_index.base_reg.no = base_no & 7; - operand->indirect_with_index.base_reg.x = (base_no & 8) >> 3; - operand->indirect_with_index.index_reg.size = REG64; + operand->u.indirect_with_index.base_reg.size = REG64; + operand->u.indirect_with_index.base_reg.no = base_no & 7; + operand->u.indirect_with_index.base_reg.x = (base_no & 8) >> 3; + operand->u.indirect_with_index.index_reg.size = REG64; char index_no = index_reg - RAX; - operand->indirect_with_index.index_reg.size = REG64; - operand->indirect_with_index.index_reg.no = index_no & 7; - operand->indirect_with_index.index_reg.x = (index_no & 8) >> 3; + operand->u.indirect_with_index.index_reg.size = REG64; + operand->u.indirect_with_index.index_reg.no = index_no & 7; + operand->u.indirect_with_index.index_reg.x = (index_no & 8) >> 3; return DEII; } } @@ -1034,7 +1034,7 @@ parse_operand(struct ParseInfo *info, unsigned int opr_flag, struct Operand *ope if (opr_flag & IMM) { if (*p == '$') { info->p = p + 1; - if (!immediate(&info->p, &operand->immediate)) + if (!immediate(&info->p, &operand->u.immediate)) parse_error(info, "syntax error"); operand->type = IMMEDIATE; return IMM; @@ -1064,7 +1064,7 @@ parse_operand(struct ParseInfo *info, unsigned int opr_flag, struct Operand *ope if (expr_with_flag.expr->kind == EX_LABEL || expr_with_flag.expr->kind == EX_FIXNUM || expr_with_flag.expr->kind == EX_ADD || expr_with_flag.expr->kind == EX_SUB) { operand->type = DIRECT; - operand->direct.expr = expr_with_flag.expr; + operand->u.direct.expr = expr_with_flag.expr; return EXP; } parse_error(info, "direct number not implemented"); @@ -1075,7 +1075,7 @@ parse_operand(struct ParseInfo *info, unsigned int opr_flag, struct Operand *ope if (expr_with_flag.expr == NULL) { struct Expr *expr = calloc_or_die(sizeof(*expr)); expr->kind = EX_FIXNUM; - expr->fixnum = 0; + expr->u.fixnum = 0; expr_with_flag.expr = expr; } return parse_indirect_register(info, &expr_with_flag, operand); @@ -1307,9 +1307,9 @@ const struct ParseInstTable kParseInstTable[] = { &(struct ParseOpArray){TEST, {R32, R32}}, &(struct ParseOpArray){TEST, {R64, R64}}, }}, - [R_CWTL] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CWTL}}}, - [R_CLTD] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CLTD}}}, - [R_CQTO] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CQTO}}}, + [R_CWTL] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CWTL, {0}}}}, + [R_CLTD] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CLTD, {0}}}}, + [R_CQTO] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CQTO, {0}}}}, [R_BSR] = {3, (const struct ParseOpArray *[]){ @@ -1388,7 +1388,7 @@ const struct ParseInstTable kParseInstTable[] = { &(struct ParseOpArray){CALL_D, {EXP}}, &(struct ParseOpArray){CALL_DER, {DER}}, }}, - [R_RET] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){RET}}}, + [R_RET] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){RET, {0}}}}, [R_PUSH] = {2, (const struct ParseOpArray *[]){ @@ -1397,7 +1397,7 @@ const struct ParseInstTable kParseInstTable[] = { }}, [R_POP] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){POP, {R64}}}}, [R_INT] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){INT, {IMM}}}}, - [R_SYSCALL] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){SYSCALL}}}, + [R_SYSCALL] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){SYSCALL, {0}}}}, [R_MOVSD] = {3, @@ -1528,32 +1528,32 @@ const struct ParseInstTable kParseInstTable[] = { [R_ENDBR64] = {1, (const struct ParseOpArray *[]){ - &(struct ParseOpArray){ENDBR64}, + &(struct ParseOpArray){ENDBR64, {0}}, }}, /* no-operand instructions */ - [R_NOP] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){NOP}}}, - [R_PUSHFQ] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){PUSHFQ}}}, - [R_POPFQ] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){POPFQ}}}, - [R_PUSHF] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){PUSHF}}}, - [R_POPF] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){POPF}}}, - [R_CPUID] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CPUID}}}, - [R_RDTSC] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){RDTSC}}}, - [R_UD2] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){UD2}}}, - [R_PAUSE] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){PAUSE}}}, - [R_HLT] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){HLT}}}, - [R_CMC] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CMC}}}, - [R_CLC] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CLC}}}, - [R_STC] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){STC}}}, - [R_CLI] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CLI}}}, - [R_STI] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){STI}}}, - [R_CLD] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CLD}}}, - [R_STD] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){STD}}}, - [R_CLTS] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CLTS}}}, - [R_WBINVD] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){WBINVD}}}, - [R_MFENCE] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){MFENCE}}}, - [R_LFENCE] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){LFENCE}}}, - [R_SFENCE] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){SFENCE}}}, + [R_NOP] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){NOP, {0}}}}, + [R_PUSHFQ] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){PUSHFQ, {0}}}}, + [R_POPFQ] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){POPFQ, {0}}}}, + [R_PUSHF] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){PUSHF, {0}}}}, + [R_POPF] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){POPF, {0}}}}, + [R_CPUID] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CPUID, {0}}}}, + [R_RDTSC] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){RDTSC, {0}}}}, + [R_UD2] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){UD2, {0}}}}, + [R_PAUSE] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){PAUSE, {0}}}}, + [R_HLT] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){HLT, {0}}}}, + [R_CMC] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CMC, {0}}}}, + [R_CLC] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CLC, {0}}}}, + [R_STC] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){STC, {0}}}}, + [R_CLI] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CLI, {0}}}}, + [R_STI] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){STI, {0}}}}, + [R_CLD] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CLD, {0}}}}, + [R_STD] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){STD, {0}}}}, + [R_CLTS] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CLTS, {0}}}}, + [R_WBINVD] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){WBINVD, {0}}}}, + [R_MFENCE] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){MFENCE, {0}}}}, + [R_LFENCE] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){LFENCE, {0}}}}, + [R_SFENCE] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){SFENCE, {0}}}}, /* register instructions */ [R_IMUL] = @@ -1594,7 +1594,7 @@ const struct ParseInstTable kParseInstTable[] = { &(struct ParseOpArray){CMPXCHG_RR, {R64, R64}}, &(struct ParseOpArray){CMPXCHG_RI, {R32 | R64, IND}}, }}, - [R_LEAVE] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){LEAVE}}}, + [R_LEAVE] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){LEAVE, {0}}}}, [R_ENTER] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){ENTER, {IMM, IMM}}}}, [R_XCHG] = {5, @@ -1755,31 +1755,31 @@ const struct ParseInstTable kParseInstTable[] = { }}, /* string operations */ - [R_MOVSB] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){MOVSB}}}, - [R_MOVSW] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){MOVSW}}}, - [R_MOVSL] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){MOVSL}}}, - [R_MOVSQ] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){MOVSQ}}}, - [R_STOSB] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){STOSB}}}, - [R_STOSW] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){STOSW}}}, - [R_STOSL] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){STOSL}}}, - [R_STOSQ] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){STOSQ}}}, - [R_LODSB] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){LODSB}}}, - [R_LODSW] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){LODSW}}}, - [R_LODSL] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){LODSL}}}, - [R_LODSQ] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){LODSQ}}}, - [R_SCASB] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){SCASB}}}, - [R_SCASW] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){SCASW}}}, - [R_SCASL] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){SCASL}}}, - [R_SCASQ] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){SCASQ}}}, - [R_CMPSB] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CMPSB}}}, - [R_CMPSW] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CMPSW}}}, - [R_CMPSL] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CMPSL}}}, - [R_CMPSQ] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CMPSQ}}}, + [R_MOVSB] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){MOVSB, {0}}}}, + [R_MOVSW] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){MOVSW, {0}}}}, + [R_MOVSL] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){MOVSL, {0}}}}, + [R_MOVSQ] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){MOVSQ, {0}}}}, + [R_STOSB] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){STOSB, {0}}}}, + [R_STOSW] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){STOSW, {0}}}}, + [R_STOSL] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){STOSL, {0}}}}, + [R_STOSQ] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){STOSQ, {0}}}}, + [R_LODSB] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){LODSB, {0}}}}, + [R_LODSW] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){LODSW, {0}}}}, + [R_LODSL] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){LODSL, {0}}}}, + [R_LODSQ] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){LODSQ, {0}}}}, + [R_SCASB] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){SCASB, {0}}}}, + [R_SCASW] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){SCASW, {0}}}}, + [R_SCASL] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){SCASL, {0}}}}, + [R_SCASQ] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){SCASQ, {0}}}}, + [R_CMPSB] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CMPSB, {0}}}}, + [R_CMPSW] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CMPSW, {0}}}}, + [R_CMPSL] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CMPSL, {0}}}}, + [R_CMPSQ] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CMPSQ, {0}}}}, /* gas conversion aliases */ - [R_CBW] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CBW}}}, - [R_CDQE] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CDQE}}}, - [R_CWD] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CWD}}}, + [R_CBW] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CBW, {0}}}}, + [R_CDQE] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CDQE, {0}}}}, + [R_CWD] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){CWD, {0}}}}, /* condition code aliases */ [R_JC] = {1, (const struct ParseOpArray *[]){&(struct ParseOpArray){JB, {EXP}}}}, diff --git a/cmd/dev/as/arch/x64/reloc_x64.c b/cmd/dev/as/arch/x64/reloc_x64.c @@ -23,17 +23,17 @@ put_value(unsigned char *p, int64_t value, int size) static int make_jmp_long(struct IR *ir) { - if (ir->code.flag & INST_LONG_OFFSET) + if (ir->u.code.flag & INST_LONG_OFFSET) return 0; - struct Inst *inst = ir->code.inst; + struct Inst *inst = ir->u.code.inst; /* change to long offset and recalculate */ - ir->code.flag |= INST_LONG_OFFSET; - ir->code.len = 0; + ir->u.code.flag |= INST_LONG_OFFSET; + ir->u.code.len = 0; if (inst->op == JMP_D) - MAKE_CODE(inst, &ir->code, 0xe9, IM32(-1)); + MAKE_CODE(inst, &ir->u.code, 0xe9, IM32(-1)); else - MAKE_CODE(inst, &ir->code, 0x0f, 0x80 + (inst->op - JO), IM32(-1)); + MAKE_CODE(inst, &ir->u.code, 0x0f, 0x80 + (inst->op - JO), IM32(-1)); return 1; } @@ -47,21 +47,21 @@ resolve_direct_mov( struct Vector *unresolved ) { - struct Inst *inst = ir->code.inst; + struct Inst *inst = ir->u.code.inst; struct Operand *mem = inst->op == MOV_DR ? &inst->opr[0] : &inst->opr[1]; - struct Reg *reg = inst->op == MOV_DR ? &inst->opr[1].reg : &inst->opr[0].reg; - struct Value value = calc_expr(label_table, mem->direct.expr); + struct Reg *reg = inst->op == MOV_DR ? &inst->opr[1].u.reg : &inst->opr[0].u.reg; + struct Value value = calc_expr(label_table, mem->u.direct.expr); struct UnresolvedInfo *info; if (value.label == NULL) { - put_value(ir->code.buf + ir->code.len - 4, value.offset, sizeof(int32_t)); + put_value(ir->u.code.buf + ir->u.code.len - 4, value.offset, sizeof(int32_t)); return; } info = malloc_or_die(sizeof(*info)); info->kind = inst->op == MOV_DR && reg->size == REG32 ? UNRES_X64_ABS32S : UNRES_X64_ABS32; info->label = value.label; info->src_section = section; - info->offset = address + ir->code.len - 4 - start_address; + info->offset = address + ir->u.code.len - 4 - start_address; info->add = value.offset; vec_push(unresolved, info); } @@ -77,7 +77,7 @@ x64_resolve_code_reloc( int *size_upgraded ) { - struct Inst *inst = ir->code.inst; + struct Inst *inst = ir->u.code.inst; switch (inst->op) { case MOV_DR: case MOV_RD: @@ -88,8 +88,8 @@ x64_resolve_code_reloc( %reg), not sym's address, so it needs a relocation even when sym is local. unlike lea_ir there is no direct-address fallback to patch in place */ - if (inst->opr[0].indirect.reg.no == RIP) { - struct Value value = calc_expr(label_table, inst->opr[0].indirect.offset.expr); + if (inst->opr[0].u.indirect.reg.no == RIP) { + struct Value value = calc_expr(label_table, inst->opr[0].u.indirect.offset.expr); if (value.label != NULL) { struct UnresolvedInfo *info = malloc_or_die(sizeof(*info)); info->kind = UNRES_X64_GOT_LOAD; @@ -105,11 +105,11 @@ x64_resolve_code_reloc( /* addq sym@gottpoff(%rip), %reg: the second half of qbe's initial-exec tls sequence (movq %fs:0, %reg first). it loads a got-resident tp-offset, otherwise the same shape as mov_ir */ - if (inst->opr[0].indirect.reg.no == RIP) { - struct Value value = calc_expr(label_table, inst->opr[0].indirect.offset.expr); + if (inst->opr[0].u.indirect.reg.no == RIP) { + struct Value value = calc_expr(label_table, inst->opr[0].u.indirect.offset.expr); if (value.label != NULL) { struct UnresolvedInfo *info = malloc_or_die(sizeof(*info)); - info->kind = (inst->opr[0].indirect.offset.flag & LF_GOTTPOFF) ? UNRES_X64_GOTTPOFF + info->kind = (inst->opr[0].u.indirect.offset.flag & LF_GOTTPOFF) ? UNRES_X64_GOTTPOFF : UNRES_X64_GOT_LOAD; info->label = value.label; info->src_section = section; @@ -120,9 +120,9 @@ x64_resolve_code_reloc( } break; case LEA_IR: - if (inst->opr[0].indirect.reg.no == RIP - && inst->opr[0].indirect.offset.expr->kind != EX_FIXNUM) { - struct Value value = calc_expr(label_table, inst->opr[0].indirect.offset.expr); + if (inst->opr[0].u.indirect.reg.no == RIP + && inst->opr[0].u.indirect.offset.expr->kind != EX_FIXNUM) { + struct Value value = calc_expr(label_table, inst->opr[0].u.indirect.offset.expr); if (value.label != NULL) { struct UnresolvedInfo *info = malloc_or_die(sizeof(*info)); info->kind = UNRES_EXTERN_PC32; @@ -132,11 +132,11 @@ x64_resolve_code_reloc( info->add = value.offset - 4; vec_push(unresolved, info); #if TC_TARGET_PLATFORM == TC_PLATFORM_APPLE - put_value(ir->code.buf + 3, value.offset, sizeof(int32_t)); + put_value(ir->u.code.buf + 3, value.offset, sizeof(int32_t)); #endif } else { - int64_t offset = value.offset - (address + ir->code.len); - put_value(ir->code.buf + 3, offset, sizeof(int32_t)); + int64_t offset = value.offset - (address + ir->u.code.len); + put_value(ir->u.code.buf + 3, offset, sizeof(int32_t)); } } break; @@ -145,9 +145,9 @@ x64_resolve_code_reloc( pc-relative, so no address math here. the linker computes it once the tls layout is known, like ir_asm.c's unres_abs64 registration for .quad label */ - if (inst->opr[0].segment.offset.flag & LF_TPOFF) { - struct Value value = calc_expr(label_table, inst->opr[0].segment.offset.expr); - int reloc_offset = ir->code.len - 4; + if (inst->opr[0].u.segment.offset.flag & LF_TPOFF) { + struct Value value = calc_expr(label_table, inst->opr[0].u.segment.offset.expr); + int reloc_offset = ir->u.code.len - 4; struct UnresolvedInfo *info = malloc_or_die(sizeof(*info)); info->kind = UNRES_X64_TPOFF32; info->label = value.label; @@ -168,10 +168,10 @@ x64_resolve_code_reloc( /* rip-relative with a symbolic offset: disp32 is the last 4 bytes, but unlike lea_ir these encodings may or may not have a rex byte, so derive the offset from the encoded length */ - if (inst->opr[0].indirect.reg.no == RIP - && inst->opr[0].indirect.offset.expr->kind != EX_FIXNUM) { - struct Value value = calc_expr(label_table, inst->opr[0].indirect.offset.expr); - int reloc_offset = ir->code.len - 4; + if (inst->opr[0].u.indirect.reg.no == RIP + && inst->opr[0].u.indirect.offset.expr->kind != EX_FIXNUM) { + struct Value value = calc_expr(label_table, inst->opr[0].u.indirect.offset.expr); + int reloc_offset = ir->u.code.len - 4; if (value.label != NULL) { struct UnresolvedInfo *info = malloc_or_die(sizeof(*info)); info->kind = UNRES_EXTERN_PC32; @@ -181,8 +181,8 @@ x64_resolve_code_reloc( info->add = value.offset - 4; vec_push(unresolved, info); } else { - int64_t offset = value.offset - (address + ir->code.len); - put_value(ir->code.buf + reloc_offset, offset, sizeof(int32_t)); + int64_t offset = value.offset - (address + ir->u.code.len); + put_value(ir->u.code.buf + reloc_offset, offset, sizeof(int32_t)); } } break; @@ -204,7 +204,7 @@ x64_resolve_code_reloc( case JLE: case JG: if (inst->opr[0].type == DIRECT) { - struct Value value = calc_expr(label_table, inst->opr[0].direct.expr); + struct Value value = calc_expr(label_table, inst->opr[0].u.direct.expr); if (value.label != NULL) { struct LabelInfo *label_info = table_get(label_table, value.label); if (label_info == NULL) { @@ -223,26 +223,26 @@ x64_resolve_code_reloc( } } - int64_t offset = value.offset - (address + ir->code.len); - int long_offset = ir->code.flag & INST_LONG_OFFSET; + int64_t offset = value.offset - (address + ir->u.code.len); + int long_offset = ir->u.code.flag & INST_LONG_OFFSET; if (!long_offset) { if (!is_im8(offset)) { *size_upgraded |= make_jmp_long(ir); } else { - put_value(ir->code.buf + 1, offset, sizeof(int8_t)); + put_value(ir->u.code.buf + 1, offset, sizeof(int8_t)); } } else { if (!is_im32(offset)) error("jump offset too far (over 32bit)"); int d = inst->op == JMP_D ? 1 : 2; - put_value(ir->code.buf + d, offset, sizeof(int32_t)); + put_value(ir->u.code.buf + d, offset, sizeof(int32_t)); } } break; case CALL_D: if (inst->opr[0].type == DIRECT) { - struct Value value = calc_expr(label_table, inst->opr[0].direct.expr); + struct Value value = calc_expr(label_table, inst->opr[0].u.direct.expr); if (value.label != NULL) { struct UnresolvedInfo *info = malloc_or_die(sizeof(*info)); info->kind = UNRES_CALL; @@ -253,8 +253,8 @@ x64_resolve_code_reloc( vec_push(unresolved, info); break; } - int64_t offset = value.offset - (address + ir->code.len); - put_value(ir->code.buf + 1, offset, sizeof(int32_t)); + int64_t offset = value.offset - (address + ir->u.code.len); + put_value(ir->u.code.buf + 1, offset, sizeof(int32_t)); } break; default: diff --git a/cmd/dev/as/as.c b/cmd/dev/as/as.c @@ -202,11 +202,11 @@ main(int argc, char *argv[]) OPT_VERSION, }; static const struct option options[] = { - {"o", required_argument}, /* specify output filename */ + {"o", required_argument, 'o'}, /* specify output filename */ {"-help", no_argument, OPT_HELP}, {"v", no_argument, OPT_VERSION}, {"-version", no_argument, OPT_VERSION}, - {NULL}, + {NULL, 0, 0}, }; const char *ofn = NULL; diff --git a/cmd/dev/as/emit_macho.c b/cmd/dev/as/emit_macho.c @@ -558,4 +558,7 @@ emit_macho_obj( construct_load_commands(&work, off_p, section_count); return output_to_file(ofn, &work); } +#else +/* ISO C wants a declaration in every file, and off Apple this has none */ +extern int emit_macho_none; #endif diff --git a/cmd/dev/as/ir_asm.c b/cmd/dev/as/ir_asm.c @@ -20,7 +20,7 @@ new_ir_label(const struct Name *label) { struct IR *ir = calloc_or_die(sizeof(*ir)); ir->kind = IR_LABEL; - ir->label = label; + ir->u.label = label; return ir; } @@ -29,7 +29,7 @@ new_ir_code(const struct Code *code) { struct IR *ir = calloc_or_die(sizeof(*ir)); ir->kind = IR_CODE; - ir->code = *code; + ir->u.code = *code; return ir; } @@ -38,8 +38,8 @@ new_ir_data(const void *data, size_t size) { struct IR *ir = calloc_or_die(sizeof(*ir)); ir->kind = IR_DATA; - ir->data.len = size; - ir->data.buf = (unsigned char *)data; + ir->u.data.len = size; + ir->u.data.buf = (unsigned char *)data; return ir; } @@ -48,7 +48,7 @@ new_ir_bss(size_t size) { struct IR *ir = calloc_or_die(sizeof(*ir)); ir->kind = IR_BSS; - ir->bss = size; + ir->u.bss = size; return ir; } @@ -57,7 +57,7 @@ new_ir_zero(size_t size) { struct IR *ir = calloc_or_die(sizeof(*ir)); ir->kind = IR_ZERO; - ir->zero = size; + ir->u.zero = size; return ir; } @@ -66,7 +66,7 @@ new_ir_align(int align) { struct IR *ir = calloc_or_die(sizeof(*ir)); ir->kind = IR_ALIGN; - ir->align = align; + ir->u.align = align; return ir; } @@ -75,8 +75,8 @@ new_ir_expr(enum IrKind kind, const struct Expr *expr) { struct IR *ir = calloc_or_die(sizeof(*ir)); ir->kind = kind; - ir->expr.expr = expr; - ir->expr.addend = 0; + ir->u.expr.expr = expr; + ir->u.expr.addend = 0; return ir; } @@ -146,29 +146,29 @@ calc_label_address(uint64_t start_address, struct Vector *sections, struct Table switch (ir->kind) { case IR_LABEL: { struct LabelInfo *info; - if (!table_try_get(label_table, ir->label, (void **)&info)) { - fprintf(stderr, "[%.*s] not found\n", NAMES(ir->label)); + if (!table_try_get(label_table, ir->u.label, (void **)&info)) { + fprintf(stderr, "[%.*s] not found\n", NAMES(ir->u.label)); assert(!"unexpected"); } else { info->address = address; } } break; case IR_CODE: - address += ir->code.len; + address += ir->u.code.len; break; case IR_DATA: - address += ir->data.len; + address += ir->u.data.len; break; case IR_BSS: - address += ir->bss; + address += ir->u.bss; break; case IR_ZERO: - address += ir->zero; + address += ir->u.zero; break; case IR_ALIGN: - ir->address = address = ALIGN(address, ir->align); - if ((size_t)ir->align > section->align) { - section->align = ir->align; + ir->address = address = ALIGN(address, ir->u.align); + if ((size_t)ir->u.align > section->align) { + section->align = ir->u.align; settle = 0; } break; @@ -192,15 +192,6 @@ calc_label_address(uint64_t start_address, struct Vector *sections, struct Table /* relative address resolution */ -static void -put_value(unsigned char *p, int64_t value, int size) -{ - for (int i = 0; i < size; ++i) { - *p++ = value; - value >>= 8; - } -} - int resolve_relative_address( struct Vector *sections, struct Table *label_table, struct Vector *unresolved @@ -226,7 +217,7 @@ resolve_relative_address( case IR_EXPR_SHORT: case IR_EXPR_LONG: case IR_EXPR_QUAD: { - struct Value value = calc_expr(label_table, ir->expr.expr); + struct Value value = calc_expr(label_table, ir->u.expr.expr); assert(value.label != NULL); struct UnresolvedInfo *info = malloc_or_die(sizeof(*info)); info->kind = UNRES_ABS64; @@ -234,7 +225,7 @@ resolve_relative_address( info->src_section = section; info->offset = address - start_address; info->add = value.offset; - ir->expr.addend = value.offset; + ir->u.expr.addend = value.offset; vec_push(unresolved, info); } break; case IR_LABEL: @@ -264,31 +255,31 @@ emit_irs(struct Vector *sections) case IR_LABEL: break; case IR_CODE: - sec_add_code(section, ir->code.buf, ir->code.len); + sec_add_code(section, ir->u.code.buf, ir->u.code.len); break; case IR_DATA: - sec_add_data(section, ir->data.buf, ir->data.len); + sec_add_data(section, ir->u.data.buf, ir->u.data.len); break; case IR_BSS: - sec_add_bss(section, ir->bss); + sec_add_bss(section, ir->u.bss); break; case IR_ZERO: if (section->flag & SF_BSS) { - sec_add_bss(section, ir->zero); + sec_add_bss(section, ir->u.zero); } else { - void *buf = calloc_or_die(ir->zero); - sec_add_data(section, buf, ir->zero); + void *buf = calloc_or_die(ir->u.zero); + sec_add_data(section, buf, ir->u.zero); } break; case IR_ALIGN: - sec_align_size(section, ir->align); + sec_align_size(section, ir->u.align); break; case IR_EXPR_BYTE: case IR_EXPR_SHORT: case IR_EXPR_LONG: case IR_EXPR_QUAD: { #if TC_TARGET_PLATFORM == TC_PLATFORM_APPLE - int64_t value = ir->expr.addend; + int64_t value = ir->u.expr.addend; #else int64_t value = 0; #endif diff --git a/cmd/dev/as/ir_asm.h b/cmd/dev/as/ir_asm.h @@ -41,7 +41,8 @@ struct UnresolvedInfo { uintptr_t offset; struct SectionInfo *src_section; int add; - enum UnresolvedKind kind; + /* an enum UnresolvedKind, or an architecture's own from UNRES_ARCH_BASE */ + int kind; }; struct Data { @@ -76,7 +77,7 @@ struct IR { size_t zero; int align; int section; - }; + } u; uint64_t address; }; diff --git a/cmd/dev/as/parse_asm.c b/cmd/dev/as/parse_asm.c @@ -349,7 +349,7 @@ struct Token { #ifndef __NO_FLONUM Flonum flonum; #endif - }; + } u; }; static struct Token * @@ -375,7 +375,7 @@ read_flonum(struct ParseInfo *info, int base) Flonum val = strtold(start, &next); #endif struct Token *tok = new_token(TK_FLONUM); - tok->flonum = val; + tok->u.flonum = val; info->p = next; if (base == 16) { @@ -434,7 +434,7 @@ fetch_token(struct ParseInfo *info) } #endif struct Token *token = new_token(TK_FIXNUM); - token->fixnum = v; + token->u.fixnum = v; info->p = q; return token; } @@ -443,7 +443,7 @@ fetch_token(struct ParseInfo *info) const struct Name *label = parse_label(info); if (label != NULL) { struct Token *token = new_token(TK_LABEL); - token->label.name = label; + token->u.label.name = label; return token; } } break; @@ -488,7 +488,7 @@ fetch_token(struct ParseInfo *info) while (c = *++p, is_label_chr(c)) ; struct Token *token = new_token(TK_LABEL); - token->label.name = alloc_name(label, p, 0); + token->u.label.name = alloc_name(label, p, 0); info->p = p; return token; } @@ -527,14 +527,14 @@ prim(struct ParseInfo *info) const struct Token *tok; if ((tok = match(info, TK_LABEL)) != NULL) { expr = new_expr(EX_LABEL); - expr->label.name = tok->label.name; + expr->u.label.name = tok->u.label.name; } else if ((tok = match(info, TK_FIXNUM)) != NULL) { expr = new_expr(EX_FIXNUM); - expr->fixnum = tok->fixnum; + expr->u.fixnum = tok->u.fixnum; #ifndef __NO_FLONUM } else if ((tok = match(info, TK_FLONUM)) != NULL) { expr = new_expr(EX_FLONUM); - expr->flonum = tok->flonum; + expr->u.flonum = tok->u.flonum; #endif } return expr; @@ -554,7 +554,7 @@ unary(struct ParseInfo *info) return expr; default: { struct Expr *op = new_expr(EX_POS); - op->unary.sub = expr; + op->u.unary.sub = expr; return op; } } @@ -566,16 +566,16 @@ unary(struct ParseInfo *info) return NULL; switch (expr->kind) { case EX_FIXNUM: - expr->fixnum = -expr->fixnum; + expr->u.fixnum = -expr->u.fixnum; return expr; #ifndef __NO_FLONUM case EX_FLONUM: - expr->flonum = -expr->flonum; + expr->u.flonum = -expr->u.flonum; return expr; #endif default: { struct Expr *op = new_expr(EX_NEG); - op->unary.sub = expr; + op->u.unary.sub = expr; return op; } } @@ -603,10 +603,10 @@ parse_mul(struct ParseInfo *info) if (lhs->kind == EX_FIXNUM && rhs->kind == EX_FIXNUM) { switch (tok->kind) { case TK_MUL: - lhs->fixnum *= rhs->fixnum; + lhs->u.fixnum *= rhs->u.fixnum; break; case TK_DIV: - lhs->fixnum /= rhs->fixnum; + lhs->u.fixnum /= rhs->u.fixnum; break; default: assert(0); @@ -615,8 +615,8 @@ parse_mul(struct ParseInfo *info) } else { expr = new_expr((enum ExprKind)(tok->kind + (EX_MUL - TK_MUL))); /* assume exprkind is same * order with tokenkind */ - expr->bop.lhs = lhs; - expr->bop.rhs = rhs; + expr->u.bop.lhs = lhs; + expr->u.bop.rhs = rhs; } } return expr; @@ -641,10 +641,10 @@ parse_add(struct ParseInfo *info) if (lhs->kind == EX_FIXNUM && rhs->kind == EX_FIXNUM) { switch (tok->kind) { case TK_ADD: - lhs->fixnum += rhs->fixnum; + lhs->u.fixnum += rhs->u.fixnum; break; case TK_SUB: - lhs->fixnum -= rhs->fixnum; + lhs->u.fixnum -= rhs->u.fixnum; break; default: assert(0); @@ -653,8 +653,8 @@ parse_add(struct ParseInfo *info) } else { /* assume exprkind is same order with tokenkind */ expr = new_expr((enum ExprKind)(tok->kind + (EX_ADD - TK_ADD))); - expr->bop.lhs = lhs; - expr->bop.rhs = rhs; + expr->u.bop.lhs = lhs; + expr->u.bop.rhs = rhs; } } return expr; @@ -1080,7 +1080,7 @@ dir_bytes(struct ParseInfo *info, enum DirectiveType dir) assert(expr->kind != EX_FLONUM); if (expr->kind == EX_FIXNUM) { /* TODO: target endian */ - long value = expr->fixnum; + long value = expr->u.fixnum; int size = 1 << (dir - DT_BYTE); unsigned char *buf = malloc_or_die(size); for (int i = 0; i < size; ++i) @@ -1119,10 +1119,10 @@ dir_float(struct ParseInfo *info, enum DirectiveType dir) Flonum value; switch (expr->kind) { case EX_FIXNUM: - value = expr->fixnum; + value = expr->u.fixnum; break; case EX_FLONUM: - value = expr->flonum; + value = expr->u.flonum; break; default: assert(0); @@ -1497,15 +1497,15 @@ calc_expr(struct Table *label_table, const struct Expr *expr) assert(expr != NULL); switch (expr->kind) { case EX_LABEL: - return (struct Value){.label = expr->label.name, .offset = 0}; + return (struct Value){.label = expr->u.label.name, .offset = 0}; case EX_FIXNUM: - return (struct Value){.label = NULL, .offset = expr->fixnum}; + return (struct Value){.label = NULL, .offset = expr->u.fixnum}; case EX_ADD: case EX_SUB: case EX_MUL: case EX_DIV: { - struct Value lhs = calc_expr(label_table, expr->bop.lhs); - struct Value rhs = calc_expr(label_table, expr->bop.rhs); + struct Value lhs = calc_expr(label_table, expr->u.bop.lhs); + struct Value rhs = calc_expr(label_table, expr->u.bop.rhs); if (rhs.label != NULL) { if (expr->kind == EX_SUB && lhs.label != NULL) { struct LabelInfo *llabel, *rlabel; @@ -1553,7 +1553,7 @@ calc_expr(struct Table *label_table, const struct Expr *expr) case EX_POS: case EX_NEG: { - struct Value value = calc_expr(label_table, expr->unary.sub); + struct Value value = calc_expr(label_table, expr->u.unary.sub); if (value.label != NULL) { error("Illegal expression"); } diff --git a/cmd/dev/as/parse_asm.h b/cmd/dev/as/parse_asm.h @@ -170,7 +170,7 @@ struct Expr { #ifndef __NO_FLONUM Flonum flonum; #endif - }; + } u; }; int parse_line(struct Line *line, struct ParseInfo *info); diff --git a/cmd/dev/cc/attr.c b/cmd/dev/cc/attr.c @@ -101,6 +101,8 @@ parseattr(struct attr *a, enum attrkind allowed, enum attrprefix prefix) case TRPAREN: --paren; break; + default: + break; } } } diff --git a/cmd/dev/cc/cc.h b/cmd/dev/cc/cc.h @@ -5,6 +5,9 @@ #ifndef noreturn #if __STDC_VERSION__ >= 201112L #define noreturn _Noreturn +/* C99 has no word for it: the compilers that build us have their own */ +#elif defined(__GNUC__) +#define noreturn __attribute__((noreturn)) #else #define noreturn #endif diff --git a/cmd/dev/cc/decl.c b/cmd/dev/cc/decl.c @@ -314,7 +314,7 @@ tagspec(struct scope *s) else if (!typehasint(et, value, e->type->u.arith.issigned)) goto invalid; } else if ( - value == 0 && !et->u.arith.issigned || value == 1ull << 63 && et->u.arith.issigned + (value == 0 && !et->u.arith.issigned) || (value == 1ull << 63 && et->u.arith.issigned) ) { error( &tok.loc, @@ -334,12 +334,12 @@ tagspec(struct scope *s) ); } sign = et->u.arith.issigned; - for (i = 0; i < countof(inttypes); ++i) { + for (i = 0; (size_t)i < countof(inttypes); ++i) { et = inttypes[i][sign]; if (typehasint(et, value, sign)) break; } - assert(i < countof(inttypes)); + assert((size_t)i < countof(inttypes)); } d = mkdecl(name, DECLCONST, et, QUALNONE, LINKNONE); d->u.enumconst = value; @@ -362,7 +362,7 @@ tagspec(struct scope *s) t->base = min ? &typeint : &typeuint; } else { sign = min > 0; - for (i = 0; i < countof(inttypes); ++i) { + for (i = 0; (size_t)i < countof(inttypes); ++i) { et = inttypes[i][sign]; if (typehasint(et, max, false) && typehasint(et, -min, true)) break; @@ -382,6 +382,8 @@ tagspec(struct scope *s) t->u.arith.issigned = t->base->u.arith.issigned; t->u.arith.width = t->base->u.arith.width; } + default: + break; } t->incomplete = false; @@ -532,7 +534,7 @@ declspecs(struct scope *s, enum storageclass *sc, enum funcspec *fs, int *align) i = other ? other->align : intconstexpr(s, false); if (i & i - 1 || i > INT_MAX) error(&tok.loc, "invalid alignment: %llu", i); - if (i > *align) + if (i > (unsigned long long)*align) *align = i; expect(TRPAREN, "to close 'alignas' specifier"); break; @@ -653,7 +655,7 @@ done: if (other->base->kind == TYPEFUNC) error(&tok.loc, "'restrict' applied to function pointer"); } - if (!t && (tq || sc && *sc || fs && *fs)) + if (!t && (tq || (sc && *sc) || (fs && *fs))) error(&tok.loc, "declaration has no type specifier"); /* * TODO: consider delaying attribute parsing to declarator(), @@ -924,6 +926,8 @@ declarator( } } break; + default: + break; } base.type = t; base.qual = tq; @@ -985,7 +989,7 @@ addmember( if (mt.type->prop & PROPVM) error(&tok.loc, "struct member '%s' has variably modified type", name); assert(mt.type->align > 0); - if (name || width == -1) { + if (name || width == -1ull) { m = xmalloc(sizeof(*m)); m->type = mt.type; m->qual = mt.qual; @@ -996,7 +1000,7 @@ addmember( } else { m = NULL; } - if (width == -1) { + if (width == -1ull) { m->bits.before = 0; m->bits.after = 0; if (align < mt.type->align) { @@ -1038,7 +1042,7 @@ addmember( "declarator", name ); - if (width > mt.type->u.arith.width) + if (width > (unsigned long long)mt.type->u.arith.width) error(&tok.loc, "bit-field '%s' exceeds width of underlying type", name); align = mt.type->align; if (t->kind == TYPESTRUCT) { @@ -1460,6 +1464,8 @@ decl(struct scope *s, struct func *f) delscope(funcscope); } break; + default: + break; } if (consume(TSEMICOLON)) return true; diff --git a/cmd/dev/cc/driver.c b/cmd/dev/cc/driver.c @@ -359,7 +359,7 @@ resolvepath(const char *arg0, const char *binname, const char *devrelpath) struct stat st; n = proc_selfexe(self, sizeof(self) - 1); - if (n == -1) { + if (n == (size_t)-1) { n = strlen(arg0); if (n > sizeof(self) - 1) n = sizeof(self) - 1; @@ -650,7 +650,7 @@ main(int argc, char *argv[]) { if (!(input->stages & 1 << last)) continue; - input->stages &= (1 << last + 1) - 1; + input->stages &= (1 << (last + 1)) - 1; buildobj(input, output); } if (last == LINK) { diff --git a/cmd/dev/cc/eval.c b/cmd/dev/cc/eval.c @@ -16,7 +16,7 @@ cast(struct expr *expr) if (t->size == 4) expr->u.constant.f = (float)expr->u.constant.f; } else if (t->prop & PROPINT) { - m = 1ull << t->u.arith.width - 1; + m = 1ull << (t->u.arith.width - 1); expr->u.constant.u &= m | m - 1; if (t->u.arith.issigned) expr->u.constant.u = (expr->u.constant.u ^ m) - m; @@ -29,7 +29,8 @@ unary(struct expr *expr, enum tokenkind op, struct expr *l) expr->kind = EXPRCONST; if (l->type->prop & PROPFLOAT) op |= F; - switch (op) { + /* op carries flags beside the token: its cases are not the enum's */ + switch ((int)op) { case TSUB: expr->u.constant.u = -l->u.constant.u; break; @@ -50,7 +51,8 @@ binary(struct expr *expr, enum tokenkind op, struct expr *l, struct expr *r) op |= F; else if (l->type->prop & PROPINT && l->type->u.arith.issigned) op |= S; - switch (op) { + /* op carries flags beside the token: its cases are not the enum's */ + switch ((int)op) { case TMUL: case TMUL | S: expr->u.constant.u = l->u.constant.u * r->u.constant.u; @@ -204,6 +206,8 @@ eval(struct expr *expr) l->kind = EXPRIDENT; expr->base = l; break; + default: + break; } break; case TMUL: @@ -261,7 +265,7 @@ eval(struct expr *expr) * other forms of constant expressions (6.6p10), and some * programs expect this functionality */ - if (t->kind == TYPEPOINTER || t->prop & PROPINT && t->size == typelong.size) + if (t->kind == TYPEPOINTER || (t->prop & PROPINT && t->size == typelong.size)) expr = l; } break; @@ -304,6 +308,8 @@ eval(struct expr *expr) binary(expr, expr->op, l, r); } break; + default: + break; } return expr; diff --git a/cmd/dev/cc/expr.c b/cmd/dev/cc/expr.c @@ -35,7 +35,7 @@ delexpr(struct expr *e) switch (e->kind) { case EXPRCALL: delexpr(e->base); - while (sub = e->u.call.args) { + while ((sub = e->u.call.args)) { e->u.call.args = sub->next; delexpr(sub); } @@ -66,11 +66,13 @@ delexpr(struct expr *e) * break; */ case EXPRCOMMA: - while (sub = e->base) { + while ((sub = e->base)) { e->base = sub->next; delexpr(sub); } break; + default: + break; } free(e); } @@ -114,6 +116,8 @@ decay(struct expr *e) e = mkunaryexpr(TBAND, e); e->decayed = true; break; + default: + break; } return e; @@ -162,6 +166,8 @@ mkunaryexpr(enum tokenkind op, struct expr *base) expr->op = op; } return decay(expr); + default: + break; } /* other unary operators get compiled as equivalent binary ones */ fatal("internal error: unknown unary operator %d", op); @@ -431,7 +437,7 @@ mkbinaryexpr(struct location *loc, enum tokenkind op, struct expr *l, struct exp t = commonreal(&l, &r); break; } - if (l->type->kind != TYPEPOINTER || !(rp & PROPINT) && r->type->kind != TYPEPOINTER) + if (l->type->kind != TYPEPOINTER || (!(rp & PROPINT) && r->type->kind != TYPEPOINTER)) error(loc, "invalid operands to '-' operator"); if (l->type->base->incomplete || l->type->base->kind == TYPEFUNC) error( @@ -542,7 +548,7 @@ inttype(unsigned long long val, bool decimal, char *suffix) invalid: error(&tok.loc, "invalid integer constant suffix '%s'", suffix); } - if (flags & new || flags & ~U &&new & ~U) + if (flags & new || (flags & ~U && new & ~U)) goto invalid; flags |= new; } @@ -553,14 +559,14 @@ inttype(unsigned long long val, bool decimal, char *suffix) for (i = 1;; ++i) { if (i > 64 - sign) goto notype; - if (val <= 0xffffffffffffffff >> 64 - i) + if (val <= 0xffffffffffffffff >> (64 - i)) break; } return mkbitinttype(i + sign, sign); } - assert(flags < countof(types)); + assert((size_t)flags < countof(types)); step = !sign || decimal ? 2 : 1; - for (i = flags; i < countof(types); i += step) { + for (i = flags; (size_t)i < countof(types); i += step) { t = types[i]; if (typehasint(t, val, false)) return t; @@ -703,6 +709,7 @@ encodechar16(void *dst, uint_least32_t chr, bool hexoct) static size_t encodechar32(void *dst, uint_least32_t chr, bool hexoct) { + (void)hexoct; *(uint_least32_t *)dst = chr; return sizeof(uint_least32_t); } @@ -1625,6 +1632,8 @@ precedence(enum tokenkind t) case TDIV: case TMOD: return 9; + default: + break; } return -1; } diff --git a/cmd/dev/cc/init.c b/cmd/dev/cc/init.c @@ -43,7 +43,7 @@ initadd(struct initparser *p, struct init *new) struct init **init, *old; init = p->last; - for (; old = *init; init = &old->next) { + for (; (old = *init); init = &old->next) { if (old->end * 8 - old->bits.after <= new->start * 8 + new->bits.before) continue; /* no overlap, insert before `old` */ @@ -203,6 +203,8 @@ advance(struct initparser *p) return; } break; + default: + break; } if (p->sub == p->cur) error(&tok.loc, "too many initializers for type"); diff --git a/cmd/dev/cc/pp.c b/cmd/dev/cc/pp.c @@ -635,13 +635,13 @@ again: t = framenext(f); assert(t); i = macroparam(m, t); - assert(i != -1); + assert(i != (size_t)-1); f = ctxpush(&m->arg[i].str, 1, NULL, space); break; default: if (f->token->kind >= TPPIDENT) { i = macroparam(m, f->token); - if (i == -1) + if (i == (size_t)-1) break; framenext(f); if (m->arg[i].ntoken == 0) @@ -712,12 +712,12 @@ define(void) ); if (m->kind != MACROFUNC) continue; - if (i != -1) + if (i != (size_t)-1) m->param[i].flags |= PARAMTOK; i = macroparam(m, t); if (prev == THASH) { tokencheck(t, TPPIDENT, "after '#' operator"); - if (i == -1) + if (i == (size_t)-1) error(&t->loc, "'%s' is not a macro parameter name", t->lit); m->param[i].flags |= PARAMSTR; i = -1; @@ -1165,7 +1165,7 @@ expandfunc(struct macro *m) /* adjust current macro depth, in case it got * shallower */ depth = macrodepth; - if (paren == 0 && (t->kind == TRPAREN || t->kind == TCOMMA && !(p->flags & PARAMVAR))) + if (paren == 0 && (t->kind == TRPAREN || (t->kind == TCOMMA && !(p->flags & PARAMVAR)))) break; switch (t->kind) { case TLPAREN: @@ -1174,6 +1174,8 @@ expandfunc(struct macro *m) case TRPAREN: --paren; break; + default: + break; } if (p->flags & PARAMSTR) stringize(&str, t); @@ -1213,7 +1215,7 @@ next(void) do t = rawnext(); - while (expand(t) || t->kind == TNEWLINE && !(ppflags & PPNEWLINE)); + while (expand(t) || (t->kind == TNEWLINE && !(ppflags & PPNEWLINE))); tok = *t; /* keyword aliases */ @@ -1252,6 +1254,8 @@ next(void) case ALIAS__ASM: tok.kind = T__ASM__; break; + default: + break; } } diff --git a/cmd/dev/cc/qbe.c b/cmd/dev/cc/qbe.c @@ -25,7 +25,7 @@ struct value { } kind; unsigned id; union { - char *name; + const char *name; unsigned long long i; double f; } u; @@ -304,7 +304,7 @@ funcbits(struct func *f, struct type *t, struct value *v, struct bitfield b) if (!bits && t->kind == TYPEBITINT) bits = t->size * 8 - t->u.arith.width; if (bits) { - bits += (t->size + 3 & -4) - t->size << 3; + bits += (((t->size + 3) & -4) - t->size) << 3; v = funcinst(f, ISHL, class, v, mkintconst(bits)); } bits += b.before; @@ -872,7 +872,7 @@ funcstore(struct func *f, struct type *t, enum typequal tq, struct lvalue lval, qt = qbetype(t); bits = lval.bits.before + lval.bits.after; if (bits) { - mask = 0xffffffffffffffffu >> 64 - t->size * 8 + bits << lval.bits.before; + mask = 0xffffffffffffffffu >> (64 - t->size * 8 + bits) << lval.bits.before; v = funcinst(f, ISHL, qt.base, v, mkintconst(lval.bits.before)); r = funcbits(f, t, v, lval.bits); v = funcinst(f, IAND, qt.base, v, mkintconst(mask)); @@ -905,6 +905,8 @@ funcload(struct func *f, struct type *t, struct lvalue lval) case TYPEINT128: case TYPECOMPLEX: return lval.addr; + default: + break; } qt = qbetype(t); v = funcinst(f, qt.load, qt.base, lval.addr, NULL); @@ -938,7 +940,7 @@ mkfunc(struct decl *decl, char *name, struct type *t, struct scope *s) d->value = v; } else { funcalloc(f, d); - funcstore(f, d->type, QUALNONE, (struct lvalue){d->value}, v); + funcstore(f, d->type, QUALNONE, (struct lvalue){.addr = d->value}, v); } } @@ -960,7 +962,7 @@ delfunc(struct func *f) struct block *b; struct inst **inst; - while (b = f->start) { + while ((b = f->start)) { f->start = b->next; arrayforeach(&b->insts, inst) free(*inst); free(b->insts.val); @@ -1140,12 +1142,16 @@ funcbranch(struct func *f, struct expr *e, struct block *bt, struct block *bf) funclabel(f, b); funcbranch(f, r, bt, bf); return &one; + default: + break; } break; case EXPRCOMMA: for (e = e->base; e->next; e = e->next) funcexpr(f, e); return funcbranch(f, e, bt, bf); + default: + break; } v = funcexpr(f, e); funcjnz(f, v, e->type, bt, bf); @@ -1170,7 +1176,7 @@ funcexpr(struct func *f, struct expr *e) d = e->u.ident.decl; switch (d->kind) { case DECLOBJECT: - return funcload(f, e->type, (struct lvalue){d->value}); + return funcload(f, e->type, (struct lvalue){.addr = d->value}); case DECLCONST: return d->value; default: @@ -1248,7 +1254,7 @@ funcexpr(struct func *f, struct expr *e) return lval.addr; case TMUL: r = funcexpr(f, e->base); - return funcload(f, e->type, (struct lvalue){r}); + return funcload(f, e->type, (struct lvalue){.addr = r}); case TSUB: r = funcexpr(f, e->base); if (e->type->kind == TYPEINT128) { @@ -1275,6 +1281,8 @@ funcexpr(struct func *f, struct expr *e) ); } return funcinst(f, INEG, qbetype(e->type).base, r, NULL); + default: + break; } fatal("internal error; unknown unary expression"); break; @@ -1566,6 +1574,8 @@ funcexpr(struct func *f, struct expr *e) else op = t->prop & PROPFLOAT ? ICNED : ICNEL; break; + default: + break; } if (op == INONE) fatal( @@ -1714,6 +1724,8 @@ funcexpr(struct func *f, struct expr *e) if (e->base) funcexpr(f, e->base); return t->u.array.size; + default: + break; } fatal("unimplemented expression %d", e->kind); return NULL; @@ -1860,7 +1872,7 @@ emitname(struct value *v) int kind; kind = v->kind & 0xf; - if (kind >= countof(sigil) || !sigil[kind]) + if ((size_t)kind >= countof(sigil) || !sigil[kind]) fatal("invalid value"); putchar(sigil[kind]); if (kind == VALUE_GLOBAL && v->id) @@ -1942,8 +1954,8 @@ emittype(struct type *t) unsigned long long off; if (t->value - || t->kind != TYPESTRUCT && t->kind != TYPEUNION && t->kind != TYPEINT128 - && t->kind != TYPECOMPLEX) + || (t->kind != TYPESTRUCT && t->kind != TYPEUNION && t->kind != TYPEINT128 + && t->kind != TYPECOMPLEX)) return; if (t->kind == TYPEINT128) { emitpairtype(t, t->u.arith.issigned ? "i128" : "u128", 'l'); diff --git a/cmd/dev/cc/token.c b/cmd/dev/cc/token.c @@ -53,7 +53,7 @@ tokenget(const void *str, size_t len) fatal("token is too long"); if (INT_MAX < tokstr.len / sizeof(void *)) fatal("too many tokens"); - if (!pos || end - pos < len + 1) { + if (!pos || (size_t)(end - pos) < len + 1) { buf = xmalloc(8192); pos = buf; end = buf + 8192; diff --git a/cmd/dev/cc/type.c b/cmd/dev/cc/type.c @@ -131,7 +131,7 @@ mkbitinttype(int width, bool sign) /* calculate byte size */ t->size = 1; - while (t->size * 8 < width) + while (t->size * 8 < (unsigned long long)width) t->size <<= 1; t->align = t->size; @@ -160,13 +160,15 @@ typerank(struct type *t) case TYPEINT: return 0x201; case TYPELONG: - return t->size << 3 + 4 | 0x1; + return t->size << (3 + 4) | 0x1; case TYPELLONG: return 0x402; case TYPEBITINT: return t->u.arith.width << 4; case TYPEINT128: return 128 << 4; + default: + break; } fatal("internal error; unhandled integer type"); return -1; @@ -186,7 +188,7 @@ typecompatible(struct type *t1, struct type *t2) * type, but not with each other (unless they are the * same type) */ - return t1->kind == TYPEENUM && t2 == t1->base || t2->kind == TYPEENUM && t1 == t2->base; + return (t1->kind == TYPEENUM && t2 == t1->base) || (t2->kind == TYPEENUM && t1 == t2->base); } switch (t1->kind) { case TYPEBITINT: @@ -214,6 +216,8 @@ typecompatible(struct type *t1, struct type *t2) goto derived; derived: return t1->qual == t2->qual && typecompatible(t1->base, t2->base); + default: + break; } return false; } @@ -228,6 +232,7 @@ typesame(struct type *t1, struct type *t2) struct type * typecomposite(struct type *t1, struct type *t2) { + (void)t2; /* XXX: implement 6.2.7 */ /* XXX: merge with typecompatible? */ return t1; @@ -241,7 +246,7 @@ typepromote(struct type *t, unsigned width) if (t->kind == TYPEBITINT) return t; if (t->prop & PROPINT && (typerank(t) <= typerank(&typeint) || width <= typeint.size * 8)) { - if (width == -1) + if (width == -1u) width = t->size * 8; return width - t->u.arith.issigned < typeint.size * 8 ? &typeint : &typeuint; } @@ -284,6 +289,8 @@ typecommonreal(struct type *t1, unsigned w1, struct type *t2, unsigned w2) return &typeulong; case TYPELLONG: return &typellong; + default: + break; } fatal("internal error; could not find common real type"); return NULL; @@ -305,6 +312,8 @@ typeadjust(struct type *t, enum typequal *tq) assert(*tq == QUALNONE); t = mkpointertype(t, QUALNONE); break; + default: + break; } return t; @@ -338,6 +347,6 @@ typehasint(struct type *t, unsigned long long i, bool sign) { assert(t->prop & PROPINT); if (sign && i >= -1ull << 63) - return t->u.arith.issigned && i >= -1ull << (t->size << 3) - 1; - return i <= 0xffffffffffffffffull >> (8 - t->size << 3) + t->u.arith.issigned; + return t->u.arith.issigned && i >= -1ull << ((t->size << 3) - 1); + return i <= 0xffffffffffffffffull >> (((8 - t->size) << 3) + t->u.arith.issigned); } diff --git a/cmd/dev/cc/utf.c b/cmd/dev/cc/utf.c @@ -12,20 +12,20 @@ utf8enc(unsigned char *s, uint_least32_t c) } if (c < 0x800) { s[0] = 0xc0 | c >> 6; - s[1] = 0x80 | c & 0x3f; + s[1] = 0x80 | (c & 0x3f); return 2; } if (c < 0xd800 || c - 0xe000 < 0x2000) { s[0] = 0xe0 | c >> 12; - s[1] = 0x80 | c >> 6 & 0x3f; - s[2] = 0x80 | c & 0x3f; + s[1] = 0x80 | (c >> 6 & 0x3f); + s[2] = 0x80 | (c & 0x3f); return 3; } if (c - 0x10000 < 0x100000) { s[0] = 0xf0 | c >> 18; - s[1] = 0x80 | c >> 12 & 0x3f; - s[2] = 0x80 | c >> 6 & 0x3f; - s[3] = 0x80 | c & 0x3f; + s[1] = 0x80 | (c >> 12 & 0x3f); + s[2] = 0x80 | (c >> 6 & 0x3f); + s[3] = 0x80 | (c & 0x3f); return 4; } assert(0); @@ -62,7 +62,7 @@ utf8dec(uint_least32_t *c, const unsigned char *s, size_t n) b = *++s; if ((b & 0xc0) != 0x80) return -1; - x = x << 6 | b & 0x3f; + x = x << 6 | (b & 0x3f); } if (x >= 0x110000 || x - 0xd800 < 0x0200) return -1; @@ -79,8 +79,8 @@ utf16enc(uint_least16_t *s, uint_least32_t c) } c -= 0x10000; if (c < 0x100000) { - s[0] = 0xd800 | c >> 10 & 0x3ff; - s[1] = 0xdc00 | c & 0x3ff; + s[0] = 0xd800 | (c >> 10 & 0x3ff); + s[1] = 0xdc00 | (c & 0x3ff); return 2; } assert(0); diff --git a/cmd/dev/cc/util.h b/cmd/dev/cc/util.h @@ -6,6 +6,9 @@ #define noreturn [[noreturn]] #elif __STDC_VERSION__ >= 201112L #define noreturn _Noreturn +/* C99 has no word for it: the compilers that build us have their own */ +#elif defined(__GNUC__) +#define noreturn __attribute__((noreturn)) #else #define noreturn #endif diff --git a/cmd/dev/ld/arch/aarch64/reloc_aarch64.c b/cmd/dev/ld/arch/aarch64/reloc_aarch64.c @@ -5,8 +5,6 @@ void ld_aarch64_reloc_apply(struct ld_reloc *r) { - unsigned long addr; - switch (r->type) { case R_AARCH64_NONE: break; diff --git a/cmd/dev/ld/arch/arm/reloc_arm.c b/cmd/dev/ld/arch/arm/reloc_arm.c @@ -5,8 +5,6 @@ void ld_arm_reloc_apply(struct ld_reloc *r) { - unsigned long addr; - switch (r->type) { case R_ARM_NONE: break; diff --git a/cmd/dev/ld/arch/riscv64/reloc_riscv64.c b/cmd/dev/ld/arch/riscv64/reloc_riscv64.c @@ -5,7 +5,6 @@ void ld_riscv_reloc_apply(struct ld_reloc *r) { - unsigned long addr; long off; long hi, lo; unsigned int *insn; diff --git a/cmd/extra/dc.c b/cmd/extra/dc.c @@ -93,7 +93,7 @@ static int col; /* error is not called from the implementation of the * arithmetic functions because that can drive to memory * leaks very easily */ -static void +static void __attribute__((noreturn)) error(char *fmt, ...) { va_list va; @@ -665,7 +665,7 @@ divmod(struct num *odivd, struct num *odivr, struct num **remp) { struct num *acc, *divd, *divr, *res; int divsign, remsign; - int under, magic, ndig, diff; + int under, magic, diff; int d, q, carry, divcarry; long dr, dd, cc; @@ -706,7 +706,7 @@ divmod(struct num *odivd, struct num *odivr, struct num **remp) dr /= 25; } - for (ndig = 0; diff >= 0; ++ndig) { + while (diff >= 0) { last(divd); dd = peek(divd); dd = dd * 100 + (prev(divd) ? peek(divd) : 0); @@ -1831,7 +1831,6 @@ aryidx(void) { int n; int i; - struct val *vp; struct reg *rp = lookup(regname()); struct ary *ap = &rp->ary; @@ -1903,7 +1902,6 @@ static void relational(int ch) { int r; - char *s; struct num *a, *b; struct reg *rp = lookup(regname()); diff --git a/cmd/extra/partplan.c b/cmd/extra/partplan.c @@ -11,7 +11,7 @@ #define SECTORS_ZEROED 16384 /* 8 MiB at 512 byte sectors: old signatures that would be seen again */ -static void +static void __attribute__((noreturn)) usage(void) { eprintf("usage: %s guided [-s swap_mib] [-f root_fs] disk | disks | layout | show plan | check [-rb] plan | write plan |\n" diff --git a/cmd/extra/yap/display.c b/cmd/extra/yap/display.c @@ -666,7 +666,7 @@ exgmark(long cnt) } void -d_clean() +d_clean(void) { /* clean up */ struct scr_info *p = &scr_info; diff --git a/cmd/extra/yap/getline.c b/cmd/extra/yap/getline.c @@ -38,7 +38,7 @@ static char *re_alloc(char *ptr, unsigned newsize); static struct block *getblock(long n, int disable_interrupt); static struct block * -new_block() +new_block(void) { struct block *pblock = maxblocklist - 1; @@ -153,7 +153,7 @@ getline(long n, int disable_interrupt) */ long -to_lastline() +to_lastline(void) { for (;;) { if (!getline(lastreadline + 1, 0)) { @@ -352,7 +352,7 @@ nextblock(struct block *pblock) */ void -do_clean() +do_clean(void) { struct block *pblock; char *p; @@ -382,7 +382,7 @@ do_clean() */ int -getch() +getch(void) { int flags, bytes_ready, bytes_read; struct stat buf; diff --git a/cmd/extra/yap/help.c b/cmd/extra/yap/help.c @@ -23,7 +23,7 @@ static struct state *origin; /* keep track of startstate */ */ static void -pr_comm() +pr_comm(void) { struct state *p = origin; char *pb; diff --git a/cmd/extra/yap/keys.c b/cmd/extra/yap/keys.c @@ -126,7 +126,7 @@ compile(char *map, struct keymap *commtable) */ void -initkeys() +initkeys(void) { char *p; static struct keymap xx[2]; diff --git a/cmd/extra/yap/main.c b/cmd/extra/yap/main.c @@ -127,7 +127,7 @@ sigquit(int signo) */ void -suspend() +suspend(void) { nflush(); resettty(); @@ -168,7 +168,7 @@ suspsig(int signo) */ int -quit() +quit(void) { clrbline(); resettty(); diff --git a/cmd/extra/yap/output.c b/cmd/extra/yap/output.c @@ -16,7 +16,7 @@ char *_optr; static char _outbuf[OBUFSIZ]; void -flush() +flush(void) { /* flush output buffer, by writing it */ char *p = _outbuf; @@ -27,7 +27,7 @@ flush() } void -nflush() +nflush(void) { /* flush output buffer, ignoring it */ _ocnt = OBUFSIZ; diff --git a/cmd/extra/yap/prompt.c b/cmd/extra/yap/prompt.c @@ -33,7 +33,7 @@ copy(char *p, const char *ep, char *s) */ void -give_prompt() +give_prompt(void) { char **name; struct scr_info *p = &scr_info; @@ -140,7 +140,7 @@ error(char *str) } void -ret_to_continue() +ret_to_continue(void) { /* obvious */ int c; static char buf[2]; diff --git a/cmd/extra/yap/term.c b/cmd/extra/yap/term.c @@ -58,7 +58,7 @@ handle(cc_t *c) */ void -inittty() +inittty(void) { struct termios *p = &_tty; @@ -78,73 +78,73 @@ inittty() } void -backspace() +backspace(void) { putline(BC); } void -clrscreen() +clrscreen(void) { tputs(CL, LINES, fputch); } void -clrtoeol() +clrtoeol(void) { tputs(CE, 1, fputch); } void -scrollreverse() +scrollreverse(void) { tputs(SR, LINES, fputch); } void -standout() +standout(void) { tputs(SO, 1, fputch); } void -standend() +standend(void) { tputs(SE, 1, fputch); } void -underline() +underline(void) { tputs(US, 1, fputch); } void -end_underline() +end_underline(void) { tputs(UE, 1, fputch); } void -bold() +bold(void) { tputs(MD, 1, fputch); } void -end_bold() +end_bold(void) { tputs(ME, 1, fputch); } void -underchar() +underchar(void) { tputs(UC, 1, fputch); } void -givetab() +givetab(void) { tputs(TA, 1, fputch); } @@ -154,7 +154,7 @@ givetab() */ void -resettty() +resettty(void) { tcsetattr(0, TCSANOW, &_svtty); putline(TE); @@ -200,7 +200,7 @@ getnumcap(char *cap) */ void -ini_terminal() +ini_terminal(void) { char *s; struct linelist *lp, *lp1; @@ -388,7 +388,7 @@ mgoto(int n) */ void -clrbline() +clrbline(void) { if (stupid) { putline("\r\n"); @@ -412,13 +412,13 @@ ins_line(int l) } void -home() +home(void) { tputs(HO, 1, fputch); } void -bottom() +bottom(void) { tputs(BO, 1, fputch); if (!*BO) @@ -426,7 +426,7 @@ bottom() } int -window() +window(void) { #ifdef TIOCGWINSZ if (ioctl(1, TIOCGWINSZ, &w) < 0) diff --git a/cmd/linux/vtallow.c b/cmd/linux/vtallow.c @@ -17,7 +17,7 @@ #define VT_LOCKSWITCH 0x560B /* disallow vt switching */ #define VT_UNLOCKSWITCH 0x560C /* allow vt switching */ -static void +static void __attribute__((noreturn)) usage(void) { eprintf("usage: %s n | y\n", argv0); diff --git a/cmd/posix/awk/lib.c b/cmd/posix/awk/lib.c @@ -780,7 +780,7 @@ WARNING(const char *fmt, ...) } void -error() +error(void) { extern Node *curnode; diff --git a/cmd/posix/awk/run.c b/cmd/posix/awk/run.c @@ -2101,7 +2101,6 @@ nawk_convert(const char *s, int (*fun_c)(int), wint_t (*fun_wc)(wint_t)) size_t n = 0; wchar_t wc; const size_t sz = awk_mb_cur_max; - int unused; if (sz == 1) { buf = tostring(s); @@ -2115,14 +2114,8 @@ nawk_convert(const char *s, int (*fun_c)(int), wint_t (*fun_wc)(wint_t)) buf = tostringN(s, strlen(s) * sz + 1); (void)mbtowc(NULL, NULL, 0); /* reset internal state */ - /* - * reset internal state here too - * assign result to avoid a compiler warning. (casting to void - * doesnt work.) - * increment said variable to avoid a different warning - */ - unused = wctomb(NULL, L'\0'); - unused++; + /* the ! keeps warn_unused_result quiet, as (void) alone does not */ + (void)!wctomb(NULL, L'\0'); /* reset internal state */ ps = s; pbuf = buf; diff --git a/cmd/posix/bc.y b/cmd/posix/bc.y @@ -680,7 +680,7 @@ iden(int ch) {"else", ELSE}, {"halt", HALT}, {"read", READ}, - {NULL} + {NULL, 0} }; struct keyword *p; char *bp; diff --git a/cmd/posix/echo.c b/cmd/posix/echo.c @@ -11,11 +11,12 @@ main(int argc, char *argv[]) { int nflag = 0; - argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; + (void)argc; + argv0 = *argv, argv0 ? argv++ : (void *)0; if (*argv && !strcmp(*argv, "-n")) { nflag = 1; - argc--, argv++; + argv++; } for (; *argv; argc--, argv++) diff --git a/cmd/posix/join.c b/cmd/posix/join.c @@ -63,7 +63,7 @@ static int pairs = 1; static size_t seplen; static struct outlist output; -static void +static void __attribute__((noreturn)) usage(void) { eprintf( diff --git a/cmd/posix/mesg.c b/cmd/posix/mesg.c @@ -9,7 +9,7 @@ #include "util.h" -static void +static void __attribute__((noreturn)) usage(void) { eprintf("usage: %s [n|y]\n", argv0); diff --git a/cmd/posix/ps.c b/cmd/posix/ps.c @@ -72,7 +72,6 @@ static void put_user(const struct procinfo *p, char *o, size_t n) { username(p static void put_ruser(const struct procinfo *p, char *o, size_t n) { username(p->ruid, o, n); } static void put_group(const struct procinfo *p, char *o, size_t n) { groupname(p->egid, o, n); } static void put_rgroup(const struct procinfo *p, char *o, size_t n) { groupname(p->rgid, o, n); } -static void put_uid(const struct procinfo *p, char *o, size_t n) { snprintf(o, n, "%u", (unsigned)p->euid); } static void put_pid(const struct procinfo *p, char *o, size_t n) { snprintf(o, n, "%d", (int)p->pid); } static void put_ppid(const struct procinfo *p, char *o, size_t n) { snprintf(o, n, "%d", (int)p->ppid); } static void put_pgid(const struct procinfo *p, char *o, size_t n) { snprintf(o, n, "%d", (int)p->pgid); } diff --git a/cmd/posix/test.c b/cmd/posix/test.c @@ -297,7 +297,7 @@ static struct test unary[] = { {"-x", {.u = unary_x}}, {"-z", {.u = unary_z}}, - {NULL}, + {NULL, {0}}, }; static struct test binary[] = { @@ -313,7 +313,7 @@ static struct test binary[] = { {"-ot", {.b = binary_ot}}, {"-nt", {.b = binary_nt}}, - {NULL}, + {NULL, {0}}, }; static struct test * diff --git a/cmd/posix/touch.c b/cmd/posix/touch.c @@ -107,7 +107,7 @@ parsetime(char *str) return mktime(&t); } -static void +static void __attribute__((noreturn)) usage(void) { eprintf( diff --git a/cmd/pseudo/tar.c b/cmd/pseudo/tar.c @@ -767,7 +767,7 @@ xt(int argc, char *argv[], int mode) * NULL) */ if (!q) { m = sizeof h->prefix, n = sizeof h->name; - p = "/" + !h->prefix[0]; + p = h->prefix[0] ? "/" : ""; snprintf(fname, sizeof fname, "%.*s%s%.*s", m, h->prefix, p, n, h->name); } q = NULL; diff --git a/shared/util.h b/shared/util.h @@ -47,8 +47,8 @@ void enfshut(int, FILE *, const char *); void efshut(FILE *, const char *); int fshut(FILE *, const char *); -void enprintf(int, const char *, ...); -void eprintf(const char *, ...); +void enprintf(int, const char *, ...) __attribute__((noreturn)); +void eprintf(const char *, ...) __attribute__((noreturn)); void weprintf(const char *, ...); void xvprintf(const char *, va_list);