Begin work adding bit_field

This commit is contained in:
gingerBill
2024-02-22 15:55:54 +00:00
parent b12ba1508e
commit a4b8c1ea17
16 changed files with 535 additions and 2 deletions
+3
View File
@@ -228,6 +228,9 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
case runtime.Type_Info_Matrix:
return .Unsupported_Type
case runtime.Type_Info_Bit_Field:
return .Unsupported_Type
case runtime.Type_Info_Array:
opt_write_start(w, opt, '[') or_return
for i in 0..<info.count {
+65
View File
@@ -2283,6 +2283,68 @@ fmt_matrix :: proc(fi: ^Info, v: any, verb: rune, info: runtime.Type_Info_Matrix
fmt_write_indent(fi)
}
}
fmt_bit_field :: proc(fi: ^Info, v: any, verb: rune, info: runtime.Type_Info_Bit_Field) {
read_bits :: proc(ptr: [^]byte, offset, size: uintptr) -> (res: u64) {
for i in 0..<size {
j := i+offset
B := ptr[j/8]
k := j&7
if B & (u8(1)<<k) != 0 {
res |= u64(1)<<u64(i)
}
}
return
}
io.write_string(fi.writer, "bit_field{", &fi.n)
hash := fi.hash; defer fi.hash = hash
indent := fi.indent; defer fi.indent -= 1
do_trailing_comma := hash
fi.indent += 1
if hash {
io.write_byte(fi.writer, '\n', &fi.n)
}
defer {
if hash {
for _ in 0..<indent { io.write_byte(fi.writer, '\t', &fi.n) }
}
io.write_byte(fi.writer, '}', &fi.n)
}
field_count := -1
for name, i in info.names {
_ = i
field_count += 1
if !do_trailing_comma && field_count > 0 {
io.write_string(fi.writer, ", ")
}
if hash {
fmt_write_indent(fi)
}
io.write_string(fi.writer, name, &fi.n)
io.write_string(fi.writer, " = ", &fi.n)
bit_offset := info.bit_offsets[i]
bit_size := info.bit_sizes[i]
value := read_bits(([^]byte)(v.data), bit_offset, bit_size)
fmt_value(fi, any{&value, info.types[i].id}, verb)
if do_trailing_comma { io.write_string(fi.writer, ",\n", &fi.n) }
}
}
// Formats a value based on its type and formatting verb
//
// Inputs:
@@ -2611,6 +2673,9 @@ fmt_value :: proc(fi: ^Info, v: any, verb: rune) {
case runtime.Type_Info_Matrix:
fmt_matrix(fi, v, verb, info)
case runtime.Type_Info_Bit_Field:
fmt_bit_field(fi, v, verb, info)
}
}
// Formats a complex number based on the given formatting verb
+10
View File
@@ -35,6 +35,7 @@ Type_Info_Relative_Pointer :: runtime.Type_Info_Relative_Pointer
Type_Info_Relative_Multi_Pointer :: runtime.Type_Info_Relative_Multi_Pointer
Type_Info_Matrix :: runtime.Type_Info_Matrix
Type_Info_Soa_Pointer :: runtime.Type_Info_Soa_Pointer
Type_Info_Bit_Field :: runtime.Type_Info_Bit_Field
Type_Info_Enum_Value :: runtime.Type_Info_Enum_Value
@@ -70,6 +71,7 @@ Type_Kind :: enum {
Relative_Multi_Pointer,
Matrix,
Soa_Pointer,
Bit_Field,
}
@@ -106,6 +108,7 @@ type_kind :: proc(T: typeid) -> Type_Kind {
case Type_Info_Relative_Multi_Pointer: return .Relative_Multi_Pointer
case Type_Info_Matrix: return .Matrix
case Type_Info_Soa_Pointer: return .Soa_Pointer
case Type_Info_Bit_Field: return .Bit_Field
}
}
@@ -1604,6 +1607,13 @@ equal :: proc(a, b: any, including_indirect_array_recursion := false, recursion_
}
}
return true
case Type_Info_Bit_Field:
x, y := a, b
x.id = v.backing_type.id
y.id = v.backing_type.id
return equal(x, y, including_indirect_array_recursion, recursion_level+0)
}
runtime.print_typeid(a.id)
+31
View File
@@ -174,6 +174,23 @@ are_types_identical :: proc(a, b: ^Type_Info) -> bool {
if x.row_count != y.row_count { return false }
if x.column_count != y.column_count { return false }
return are_types_identical(x.elem, y.elem)
case Type_Info_Bit_Field:
y := b.variant.(Type_Info_Bit_Field) or_return
if !are_types_identical(x.backing_type, y.backing_type) { return false }
if len(x.names) != len(y.names) { return false }
for _, i in x.names {
if x.names[i] != y.names[i] {
return false
}
if !are_types_identical(x.types[i], y.types[i]) {
return false
}
if x.bit_sizes[i] != y.bit_sizes[i] {
return false
}
}
return true
}
return false
@@ -639,6 +656,20 @@ write_type_writer :: proc(w: io.Writer, ti: ^Type_Info, n_written: ^int = nil) -
}
io.write_byte(w, ']', &n) or_return
case Type_Info_Bit_Field:
io.write_string(w, "bit_field ", &n) or_return
write_type(w, info.backing_type, &n) or_return
io.write_string(w, " {", &n) or_return
for name, i in info.names {
if i > 0 { io.write_string(w, ", ", &n) or_return }
io.write_string(w, name, &n) or_return
io.write_string(w, ": ", &n) or_return
write_type(w, info.types[i], &n) or_return
io.write_string(w, " | ", &n) or_return
io.write_u64(w, u64(info.bit_sizes[i]), 10, &n) or_return
}
io.write_string(w, "}", &n) or_return
case Type_Info_Simd_Vector:
io.write_string(w, "#simd[", &n) or_return
io.write_i64(w, i64(info.count), 10, &n) or_return