vile / scheme / bytevector_typed.v
208 lines · 195 sloc · 6.11 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import encoding.binary
4
5// Endian windows borrow the fixed byte storage synchronously. No callback runs
6// between validation and a store; all failing checks precede the first write.
7fn bytevector_little(value Value) !bool {
8 if value.tag != .symbol { return failure('expected big or little endianness symbol') }
9 return match value.text() {
10 'big' { false }
11 'little' { true }
12 else { return failure('expected big or little endianness symbol') }
13 }
14}
15
16fn bytevector_native_little() bool {
17 $if little_endian {
18 return true
19 } $else {
20 return false
21 }
22}
23
24fn bytevector_window(value Value, index Value, width int, aligned bool) !int {
25 data := bytevector_data(value)!
26 k := index.as_integer()!
27 if width < 1 || width > data.bytes.len || k < 0 || k > data.bytes.len - width {
28 return failure('bytevector index out of range')
29 }
30 if aligned && k % width != 0 { return failure('native bytevector index must be aligned') }
31 return int(k)
32}
33
34@[direct_array_access]
35fn bytevector_word(bytes []u8, at int, width int, little bool) u64 {
36 return match width {
37 1 { u64(bytes[at]) }
38 2 {
39 if little {
40 u64(binary.little_endian_u16_at(bytes, at))
41 } else {
42 u64(binary.big_endian_u16_at(bytes, at))
43 }
44 }
45 4 {
46 if little {
47 u64(binary.little_endian_u32_at(bytes, at))
48 } else {
49 u64(binary.big_endian_u32_at(bytes, at))
50 }
51 }
52 else {
53 if little {
54 binary.little_endian_u64_at(bytes, at)
55 } else {
56 binary.big_endian_u64_at(bytes, at)
57 }
58 }
59 }
60}
61
62// Every caller validated [at, at+width). Tail slicing avoids the compiler's
63// max_i32 end sentinel; the borrowed header gets its exact length explicitly.
64// No mutable slice clone or escaping view is introduced.
65@[inline]
66fn bytevector_borrow_bytes(bytes []u8, at int, width int) []u8 {
67 mut view := unsafe { bytes[at..] }
68 unsafe {
69 view.len = width
70 view.cap = width
71 }
72 return view
73}
74
75@[direct_array_access]
76fn bytevector_put_word(mut bytes []u8, at int, width int, little bool, word u64) {
77 // A synchronous bounded view also keeps stdlib store indices small. The
78 // current compiler narrows array-store index temporaries to C int.
79 mut window := bytevector_borrow_bytes(bytes, at, width)
80 match width {
81 1 { unsafe { (&u8(window.data))[0] = u8(word) } }
82 2 {
83 if little {
84 binary.little_endian_put_u16_at(mut window, u16(word), 0)
85 } else {
86 binary.big_endian_put_u16_at(mut window, u16(word), 0)
87 }
88 }
89 4 {
90 if little {
91 binary.little_endian_put_u32_at(mut window, u32(word), 0)
92 } else {
93 binary.big_endian_put_u32_at(mut window, u32(word), 0)
94 }
95 }
96 else {
97 if little {
98 binary.little_endian_put_u64_at(mut window, word, 0)
99 } else {
100 binary.big_endian_put_u64_at(mut window, word, 0)
101 }
102 }
103 }
104}
105
106fn bytevector_signed_word(word u64, width int) i64 {
107 bits := width * 8
108 if word & (u64(1) << (bits - 1)) == 0 { return i64(word) }
109 mask := if bits == 64 { ~u64(0) } else { (u64(1) << bits) - 1 }
110 return -1 - i64((~word) & mask)
111}
112
113@[direct_array_access]
114fn bytevector_fixed_integer(args []Value, name string, width int, signed bool, native bool, store bool) !Value {
115 count := 2 + int(!native && width > 1) + int(store)
116 check_arity(name, args.len, count, count)!
117 at := bytevector_window(args[0], args[1], width, native)!
118 little := if native || width == 1 {
119 bytevector_native_little()
120 } else {
121 bytevector_little(args[args.len - 1])!
122 }
123 if !store {
124 bytes := args[0].bytevector_pointer().bytes
125 word := bytevector_word(bytes, at, width, little)
126 if signed { return integer(bytevector_signed_word(word, width)) }
127 if word <= u64(max_i64) { return integer(i64(word)) }
128 return bytevector_integer_read(bytes, at, width, little, false)
129 }
130 args[0].require_mutable(.bytevector)!
131 bytevector_integer_range(args[2], width, signed)!
132 word := bytevector_low_word(args[2])
133 mut data := args[0].bytevector_pointer()
134 bytevector_put_word(mut data.bytes, at, width, little, word)
135 return unspecified()
136}
137
138@[direct_array_access]
139fn bytevector_fixed_float(args []Value, name string, width int, native bool, store bool) !Value {
140 count := 2 + int(!native) + int(store)
141 check_arity(name, args.len, count, count)!
142 at := bytevector_window(args[0], args[1], width, native)!
143 little := if native {
144 bytevector_native_little()
145 } else {
146 bytevector_little(args[args.len - 1])!
147 }
148 mut data := args[0].bytevector_pointer()
149 if !store {
150 if width == 4 {
151 return real(f64(if little {
152 binary.little_endian_f32_at(data.bytes, at)
153 } else {
154 binary.big_endian_f32_at(data.bytes, at)
155 }))
156 }
157 return real(if little {
158 binary.little_endian_f64_at(data.bytes, at)
159 } else {
160 binary.big_endian_f64_at(data.bytes, at)
161 })
162 }
163 args[0].require_mutable(.bytevector)!
164 number := args[2].as_real()!
165 mut window := bytevector_borrow_bytes(data.bytes, at, width)
166 // Exact values retain the runtime's checked finite conversion policy. A finite
167 // binary64 value may round to binary32 infinity, as IEEE conversion requires.
168 if width == 4 {
169 narrowed := f32(number)
170 if little {
171 binary.little_endian_put_f32_at(mut window, narrowed, 0)
172 } else {
173 binary.big_endian_put_f32_at(mut window, narrowed, 0)
174 }
175 } else {
176 if little {
177 binary.little_endian_put_f64_at(mut window, number, 0)
178 } else {
179 binary.big_endian_put_f64_at(mut window, number, 0)
180 }
181 }
182 return unspecified()
183}
184
185fn bytevector_native_endianness(mut _vm VM, args []Value) !Value {
186 check_arity('native-endianness', args.len, 0, 0)!
187 return text_value(.symbol, if bytevector_native_little() { 'little' } else { 'big' })
188}
189
190@[direct_array_access]
191fn bytevector_equal_args(mut _vm VM, args []Value) !Value {
192 check_arity('bytevector=?', args.len, 2, 2)!
193 a, b := bytevector_data(args[0])!, bytevector_data(args[1])!
194 return boolean(a.bytes == b.bytes)
195}
196
197@[direct_array_access]
198fn bytevector_fill_args(mut _vm VM, args []Value) !Value {
199 check_arity('bytevector-fill!', args.len, 2, 2)!
200 args[0].require_mutable(.bytevector)!
201 fill := args[1].as_integer()!
202 if fill < -128 || fill > 255 {
203 return failure('bytevector fill must be an exact integer in -128..255')
204 }
205 mut data := args[0].bytevector_pointer()
206 if data.bytes.len > 0 { unsafe { vmemset(data.bytes.data, int(u8(fill)), data.bytes.len) } }
207 return unspecified()
208}
209