Improve formatting

This commit is contained in:
gingerBill
2026-06-16 14:21:09 +01:00
parent df133c007b
commit eab637f249
6 changed files with 52 additions and 76 deletions

View File

@@ -185,9 +185,9 @@ decode_one :: proc(
case .BR_TABLE:
count := read_uleb(data, &off) or_return
targets := make([]u32, int(count), targets_allocator)
for i in 0..<int(count) {
for &target in targets {
t := read_uleb(data, &off) or_return
targets[i] = u32(t)
target = u32(t)
}
def := read_uleb(data, &off) or_return
inst.targets = targets

View File

@@ -81,7 +81,9 @@ write_uleb :: #force_inline proc "contextless" (code: []u8, offset: ^u32, value:
if v != 0 { b |= 0x80 }
code[offset^] = b
offset^ += 1
if v == 0 { break }
if v == 0 {
break
}
}
}
@@ -95,7 +97,9 @@ write_sleb :: #force_inline proc "contextless" (code: []u8, offset: ^u32, value:
if !done { b |= 0x80 }
code[offset^] = b
offset^ += 1
if done { break }
if done {
break
}
}
}
@@ -125,7 +129,9 @@ sleb_size :: #force_inline proc "contextless" (value: i64) -> u32 {
b := u8(v & 0x7F)
v >>= 7
n += 1
if (v == 0 && (b & 0x40) == 0) || (v == -1 && (b & 0x40) != 0) { break }
if (v == 0 && (b & 0x40) == 0) || (v == -1 && (b & 0x40) != 0) {
break
}
}
return n
}
@@ -138,7 +144,9 @@ read_uleb :: #force_inline proc "contextless" (data: []u8, offset: ^u32) -> (val
b := data[offset^]
offset^ += 1
value |= u64(b & 0x7F) << shift
if b & 0x80 == 0 { return value, true }
if b & 0x80 == 0 {
return value, true
}
shift += 7
}
return 0, false
@@ -153,7 +161,9 @@ read_sleb :: #force_inline proc "contextless" (data: []u8, offset: ^u32) -> (val
offset^ += 1
value |= i64(b & 0x7F) << shift
shift += 7
if b & 0x80 == 0 { break }
if b & 0x80 == 0 {
break
}
}
if shift < 64 && (b & 0x40) != 0 {
value |= -(i64(1) << shift)

View File

@@ -32,9 +32,8 @@ package rexcode_wasm
Operand_Kind :: enum u8 {
NONE,
REGISTER, // vestigial -- WASM is register-less (never produced)
IMMEDIATE, // i32/i64/f32/f64 constant (floats stored as raw bits)
INDEX, // LEB128 unsigned index into one of the index spaces
INDEX, // LEB128 unsigned index into one of the index spaces
MEMARG, // load/store alignment + offset pair
BLOCK_TYPE, // block / loop / if signature
}
@@ -98,11 +97,6 @@ Operand :: struct #packed {
// Generic constructors (contract surface)
// -----------------------------------------------------------------------------
@(require_results)
op_reg :: #force_inline proc "contextless" (r: Register) -> Operand {
return Operand{reg = r, kind = .REGISTER}
}
@(require_results)
op_imm :: #force_inline proc "contextless" (v: i64, size: u8) -> Operand {
return Operand{immediate = v, kind = .IMMEDIATE, size = size}
@@ -171,14 +165,14 @@ op_index :: #force_inline proc "contextless" (kind: Index_Kind, value: u32) -> O
return Operand{index = value, kind = .INDEX, idx_kind = kind}
}
@(require_results) op_local :: #force_inline proc "contextless" (n: u32) -> Operand { return op_index(.LOCAL, n) }
@(require_results) op_global :: #force_inline proc "contextless" (n: u32) -> Operand { return op_index(.GLOBAL, n) }
@(require_results) op_func :: #force_inline proc "contextless" (n: u32) -> Operand { return op_index(.FUNC, n) }
@(require_results) op_type :: #force_inline proc "contextless" (n: u32) -> Operand { return op_index(.TYPE, n) }
@(require_results) op_table :: #force_inline proc "contextless" (n: u32) -> Operand { return op_index(.TABLE, n) }
@(require_results) op_memory :: #force_inline proc "contextless" (n: u32) -> Operand { return op_index(.MEMORY, n) }
@(require_results) op_data :: #force_inline proc "contextless" (n: u32) -> Operand { return op_index(.DATA, n) }
@(require_results) op_elem :: #force_inline proc "contextless" (n: u32) -> Operand { return op_index(.ELEM, n) }
@(require_results) op_local :: #force_inline proc "contextless" (n: u32) -> Operand { return op_index(.LOCAL, n) }
@(require_results) op_global :: #force_inline proc "contextless" (n: u32) -> Operand { return op_index(.GLOBAL, n) }
@(require_results) op_func :: #force_inline proc "contextless" (n: u32) -> Operand { return op_index(.FUNC, n) }
@(require_results) op_type :: #force_inline proc "contextless" (n: u32) -> Operand { return op_index(.TYPE, n) }
@(require_results) op_table :: #force_inline proc "contextless" (n: u32) -> Operand { return op_index(.TABLE, n) }
@(require_results) op_memory :: #force_inline proc "contextless" (n: u32) -> Operand { return op_index(.MEMORY, n) }
@(require_results) op_data :: #force_inline proc "contextless" (n: u32) -> Operand { return op_index(.DATA, n) }
@(require_results) op_elem :: #force_inline proc "contextless" (n: u32) -> Operand { return op_index(.ELEM, n) }
// Branch label depth (number of enclosing blocks to break out of).
@(require_results) op_labelidx :: #force_inline proc "contextless" (depth: u32) -> Operand { return op_index(.LABEL, depth) }

View File

@@ -47,13 +47,6 @@ mnemonic_to_string :: proc(m: Mnemonic, lowercase: bool = true, allocator := con
return strings.to_string(sb)
}
// Vestigial -- WASM is register-less; provided for contract parity only.
register_name :: proc(r: Register, lowercase: bool = true, allocator := context.temp_allocator) -> string {
_ = r
_ = lowercase
return "<none>"
}
// =============================================================================
// Core sbprint
// =============================================================================
@@ -73,13 +66,8 @@ sbprint :: proc(
opts = &defaults
}
running: u32 = 0
for i in 0..<len(instructions) {
inst := &instructions[i]
offset := running
if i < len(inst_info) {
offset = inst_info[i].offset
}
for &inst, i in instructions {
offset := inst_info[i].offset if i < len(inst_info) else u32(0)
strings.write_string(sb, opts.indent)
if opts.show_offsets {
@@ -90,25 +78,26 @@ sbprint :: proc(
write_mnemonic(sb, inst.mnemonic, opts.uppercase)
// br_table prints its case vector followed by the default depth.
if inst.mnemonic == .BR_TABLE {
#partial switch inst.mnemonic {
case .BR_TABLE:
for t in inst.targets {
strings.write_byte(sb, ' ')
write_decimal_u32(sb, t)
}
strings.write_byte(sb, ' ')
write_decimal_u32(sb, inst.ops[0].index)
} else if inst.mnemonic == .V128_CONST {
case .V128_CONST:
strings.write_string(sb, " i8x16")
for bb in inst.bytes {
strings.write_byte(sb, ' ')
isa.print_hex(sb, u64(bb), opts)
}
} else if inst.mnemonic == .I8X16_SHUFFLE {
case .I8X16_SHUFFLE:
for bb in inst.bytes {
strings.write_byte(sb, ' ')
write_decimal_u32(sb, u32(bb))
}
} else if inst.operand_count > 0 {
case:
for slot in 0..<int(inst.operand_count) {
strings.write_byte(sb, ' ')
write_operand(sb, &inst.ops[slot], inst.mnemonic, label_names, opts)
@@ -264,8 +253,12 @@ write_mnemonic :: proc(sb: ^strings.Builder, m: Mnemonic, uppercase: bool) {
if name == "" { strings.write_string(sb, "<?>"); return }
if uppercase {
for i in 0..<len(name) {
c := name[i]
if c >= 'a' && c <= 'z' { strings.write_byte(sb, c - 32) } else { strings.write_byte(sb, c) }
c := name[i] // to force ASCII
if 'a' <= c && c <= 'z' {
strings.write_byte(sb, c - 32)
} else {
strings.write_byte(sb, c)
}
}
} else {
strings.write_string(sb, name)
@@ -283,9 +276,6 @@ write_operand :: proc(
switch op.kind {
case .NONE:
case .REGISTER:
strings.write_string(sb, "<none>") // vestigial
case .IMMEDIATE:
if mnemonic == .REF_NULL {
write_heap_type(sb, u8(op.immediate))

View File

@@ -33,13 +33,7 @@ ok :: proc(name: string, cond: bool) {
@(private="file")
eq_bytes :: proc(name: string, got, want: []u8) {
same := len(got) == len(want)
if same {
for i in 0..<len(got) {
if got[i] != want[i] { same = false; break }
}
}
if same {
if string(got) == string(want) {
fmt.printfln(" [ok] %s (% x)", name, got)
rpasses += 1
} else {

View File

@@ -49,8 +49,7 @@ main :: proc() {
defer delete(relocs)
defer delete(errors)
n, ok := w.encode(one, nil, code[:], &relocs, &errors)
if !ok { continue }
n := w.encode(one, nil, code[:], &relocs, &errors) or_continue
for i in 0..<n {
if i > 0 { strings.write_byte(&hex_buf, ',') }
@@ -82,28 +81,17 @@ synth :: proc(mn: w.Mnemonic, form: w.Encoding) -> w.Instruction {
switch k {
case .NONE, .ZERO_BYTE:
// no operand
case .BLOCKTYPE:
inst.ops[slot] = w.op_blocktype(.EMPTY); slot += 1
case .I32:
inst.ops[slot] = w.op_i32(1); slot += 1
case .I64:
inst.ops[slot] = w.op_i64(1); slot += 1
case .F32:
inst.ops[slot] = w.op_f32(1); slot += 1
case .F64:
inst.ops[slot] = w.op_f64(1); slot += 1
case .IDX:
inst.ops[slot] = w.op_func(0); slot += 1
case .MEMARG:
inst.ops[slot] = w.op_memarg(0, 0); slot += 1
case .REFTYPE:
inst.ops[slot] = w.op_reftype(.FUNCREF); slot += 1
case .LANE:
inst.ops[slot] = w.op_lane(0); slot += 1
case .LANES16:
// 16-byte value lives in inst.bytes (left zero), no operand
case .BR_TABLE:
// handled above
case .BLOCKTYPE: inst.ops[slot] = w.op_blocktype(.EMPTY); slot += 1
case .I32: inst.ops[slot] = w.op_i32(1); slot += 1
case .I64: inst.ops[slot] = w.op_i64(1); slot += 1
case .F32: inst.ops[slot] = w.op_f32(1); slot += 1
case .F64: inst.ops[slot] = w.op_f64(1); slot += 1
case .IDX: inst.ops[slot] = w.op_func(0); slot += 1
case .MEMARG: inst.ops[slot] = w.op_memarg(0, 0); slot += 1
case .REFTYPE: inst.ops[slot] = w.op_reftype(.FUNCREF); slot += 1
case .LANE: inst.ops[slot] = w.op_lane(0); slot += 1
case .LANES16: // 16-byte value lives in inst.bytes (left zero), no operand
case .BR_TABLE: // handled above
}
}
inst.operand_count = u8(slot)