More formatting improvements

This commit is contained in:
gingerBill
2026-06-16 14:38:28 +01:00
parent 4029702af0
commit 5e53cba0dc
4 changed files with 62 additions and 32 deletions

View File

@@ -157,10 +157,10 @@ decode_one :: proc(
field := off
raw := read_uleb(data, &off) or_return
op := Operand{index = u32(raw), kind = .INDEX, idx_kind = idx_kind_for(m, ki)}
if lid, sym := reloc_label_at(relocs, field); sym {
op.index = lid
if lid, found := reloc_label_at(relocs, field); found {
op.index = lid
op.flags.symbolic = true
op.size = 5
op.size = 5
}
inst.ops[slot] = op
slot += 1
@@ -168,9 +168,7 @@ decode_one :: proc(
case .MEMARG:
align := read_uleb(data, &off) or_return
offset := read_uleb(data, &off) or_return
inst.ops[slot] = Operand{
memarg = Memarg{align = u32(align), offset = u32(offset)}, kind = .MEMARG,
}
inst.ops[slot] = Operand{memarg = Memarg{align = u32(align), offset = u32(offset)}, kind = .MEMARG}
slot += 1
case .REFTYPE:
@@ -178,7 +176,8 @@ decode_one :: proc(
next = pc
return
}
t := data[off]; off += 1
t := data[off]
off += 1
inst.ops[slot] = Operand{immediate = i64(t), kind = .IMMEDIATE, size = 1}
slot += 1
@@ -206,7 +205,8 @@ decode_one :: proc(
next = pc
return
}
l := data[off]; off += 1
l := data[off]
off += 1
inst.ops[slot] = Operand{immediate = i64(l), kind = .IMMEDIATE, size = 1}
slot += 1
@@ -254,5 +254,5 @@ reloc_label_at :: #force_inline proc "contextless" (relocs: []Relocation, offset
return r.label_id, true
}
}
return 0, false
return
}

View File

@@ -71,14 +71,14 @@ encode_one :: #force_inline proc(
) -> (size: u32, ok: bool) {
if inst.mnemonic == .INVALID {
append(errors, Error{inst_idx = u32(inst_idx), code = .INVALID_MNEMONIC})
return 0, false
return
}
form := encoding_form(inst.mnemonic)
need := encoded_size(inst, form)
if pc + need > u32(len(code)) {
append(errors, Error{inst_idx = u32(inst_idx), code = .BUFFER_OVERFLOW})
return 0, false
return
}
off := pc
@@ -156,34 +156,46 @@ encode_one :: #force_inline proc(
@(private="file")
encoded_size :: proc(inst: ^Instruction, form: ^Encoding) -> u32 {
size: u32 = form.prefix == PREFIX_NONE ? 1 : 1 + uleb_size(u64(form.opcode))
size: u32 = 1
if form.prefix != PREFIX_NONE {
size += uleb_size(u64(form.opcode))
}
opi := 0
for k in form.imm {
switch k {
case .NONE:
case .BLOCKTYPE, .I32, .I64:
size += sleb_size(inst.ops[opi].immediate); opi += 1
size += sleb_size(inst.ops[opi].immediate)
opi += 1
case .F32:
size += 4; opi += 1
size += 4
opi += 1
case .F64:
size += 8; opi += 1
size += 8
opi += 1
case .IDX:
op := &inst.ops[opi]
size += op.flags.symbolic ? 5 : uleb_size(u64(op.index))
opi += 1
opi += 1
case .MEMARG:
ma := inst.ops[opi].memarg
size += uleb_size(u64(ma.align)) + uleb_size(u64(ma.offset)); opi += 1
size += uleb_size(u64(ma.align)) + uleb_size(u64(ma.offset))
opi += 1
case .REFTYPE:
size += 1; opi += 1
size += 1
opi += 1
case .BR_TABLE:
size += uleb_size(u64(len(inst.targets)))
for t in inst.targets { size += uleb_size(u64(t)) }
size += uleb_size(u64(inst.ops[opi].index)); opi += 1
for t in inst.targets {
size += uleb_size(u64(t))
}
size += uleb_size(u64(inst.ops[opi].index))
opi += 1
case .ZERO_BYTE:
size += 1
case .LANE:
size += 1; opi += 1
size += 1
opi += 1
case .LANES16:
size += 16
}

View File

@@ -118,7 +118,9 @@ write_uleb_padded5 :: #force_inline proc "contextless" (code: []u8, offset: ^u32
uleb_size :: #force_inline proc "contextless" (value: u64) -> u32 {
v := value
n: u32 = 1
for v >= 0x80 { v >>= 7; n += 1 }
for /**/; v >= 0x80; n += 1 {
v >>= 7
}
return n
}
@@ -168,7 +170,8 @@ read_sleb :: #force_inline proc "contextless" (data: []u8, offset: ^u32) -> (val
if shift < 64 && (b & 0x40) != 0 {
value |= -(i64(1) << shift)
}
return value, true
ok = true
return
}
write_u32le :: #force_inline proc(code: []u8, offset: ^u32, v: u32) {

View File

@@ -356,7 +356,7 @@ write_label :: proc(
opts: ^Print_Options,
) {
if label_names != nil {
if name, has := label_names^[label_id]; has {
if name, ok := label_names^[label_id]; ok {
strings.write_string(sb, name)
return
}
@@ -367,12 +367,20 @@ write_label :: proc(
@(private="file")
write_decimal_u32 :: proc(sb: ^strings.Builder, v: u32) {
if v == 0 { strings.write_byte(sb, '0'); return }
if v == 0 {
strings.write_byte(sb, '0')
return
}
buf: [10]u8
i := 0
n := v
for n > 0 { buf[i] = '0' + u8(n % 10); n /= 10; i += 1 }
for j := i - 1; j >= 0; j -= 1 { strings.write_byte(sb, buf[j]) }
for n := v; n > 0; i += 1 {
buf[i] = '0' + u8(n % 10)
n /= 10
}
for j := i - 1; j >= 0; j -= 1 {
strings.write_byte(sb, buf[j])
}
}
@(private="file")
@@ -387,10 +395,17 @@ write_signed_decimal :: proc(sb: ^strings.Builder, v: i64) {
@(private="file")
write_decimal_u64 :: proc(sb: ^strings.Builder, v: u64) {
if v == 0 { strings.write_byte(sb, '0'); return }
if v == 0 {
strings.write_byte(sb, '0')
return
}
buf: [20]u8
i := 0
n := v
for n > 0 { buf[i] = '0' + u8(n % 10); n /= 10; i += 1 }
for j := i - 1; j >= 0; j -= 1 { strings.write_byte(sb, buf[j]) }
for n := v; n > 0; i += 1 {
buf[i] = '0' + u8(n % 10)
n /= 10
}
for j := i - 1; j >= 0; j -= 1 {
strings.write_byte(sb, buf[j])
}
}