146 """Union fields are emitted in declaration order, not reversed."""
147 uint8 = types.UIntType(
"ui8", 8)
148 sint16 = types.SIntType(
"si16", 16)
149 uint32 = types.UIntType(
"ui32", 32)
150 union_t = types.UnionType(
"!hw.union<z: ui8, m: si16, a: ui32>",
151 [(
"z", uint8), (
"m", sint16), (
"a", uint32)])
155 assert "_pad[3]" in hdr
156 assert "_pad[2]" in hdr
158 z_pad = hdr.index(
"_pad[3]")
159 z_field = hdr.index(
"uint8_t z;")
160 assert z_pad < z_field
161 m_pad = hdr.index(
"_pad[2]")
162 m_field = hdr.index(
"int16_t m;")
163 assert m_pad < m_field
165 union_body = hdr[hdr.index(
"union "):]
166 z_pos = union_body.index(
" z;")
167 m_pos = union_body.index(
" m;")
168 a_pos = union_body.index(
" a;")
169 assert z_pos < m_pos < a_pos
171 assert "uint32_t a;" in union_body
173 assert "_union_z_ui8_m_si16_a_ui32_z z;" in union_body
174 assert "_union_z_ui8_m_si16_a_ui32_m m;" in union_body
178 """Bulk-encoded list windows emit a SegmentedMessageData helper."""
179 uint16 = types.UIntType(
"ui16", 16)
180 uint32 = types.UIntType(
"ui32", 32)
181 coord_struct_id =
"!hw.struct<x: ui32, y: ui32>"
183 f
"!hw.typealias<@esi_runtime_codegen::@Coord, {coord_struct_id}>")
184 coord_list_id = f
"!esi.list<{coord_alias_id}>"
186 f
"!hw.struct<x_translation: ui32, y_translation: ui32, coords: "
189 "!hw.struct<x_translation: ui32, y_translation: ui32, coords_count: "
191 data_struct_id = f
"!hw.struct<coords: !hw.array<1x{coord_alias_id}>>"
192 lowered_id = f
"!hw.union<header: {header_struct_id}, data: {data_struct_id}>"
193 serial_args_id = (f
'!esi.window<"serial_coord_args", {arg_struct_id}, '
194 '[<"header", [<"x_translation">, <"y_translation">, '
195 '<"coords" countWidth 16>]>, <"data", [<"coords", 1>]>]>')
197 coord_inner = types.StructType(coord_struct_id, [(
"x", uint32),
199 coord = types.TypeAlias(coord_alias_id,
"Coord", coord_inner)
200 coord_list = types.ListType(coord_list_id, coord)
201 arg_struct = types.StructType(arg_struct_id, [(
"x_translation", uint32),
202 (
"y_translation", uint32),
203 (
"coords", coord_list)])
204 header_struct = types.StructType(header_struct_id, [(
"x_translation", uint32),
205 (
"y_translation", uint32),
206 (
"coords_count", uint16)])
207 data_struct = types.StructType(
209 [(
"coords", types.ArrayType(f
"!hw.array<1x{coord_alias_id}>", coord, 1))],
211 lowered = types.UnionType(lowered_id, [(
"header", header_struct),
212 (
"data", data_struct)])
213 serial_args = types.WindowType(
214 serial_args_id,
"serial_coord_args", arg_struct, lowered, [
215 types.WindowType.Frame(
218 types.WindowType.Field(
"x_translation", 0, 0),
219 types.WindowType.Field(
"y_translation", 0, 0),
220 types.WindowType.Field(
"coords", 0, 16),
223 types.WindowType.Frame(
225 [types.WindowType.Field(
"coords", 1, 0)],
230 assert "Unsupported type" not in hdr
231 assert "struct serial_coord_args : public esi::SegmentedMessageData" in hdr
232 assert "using value_type = Coord;" in hdr
233 assert "using count_type = uint16_t;" in hdr
234 assert "count_type coords_count;" in hdr
235 assert "uint8_t _pad[2];" in hdr
236 assert "Coord coords;" in hdr
237 assert hdr.index(
"struct data_frame {") < hdr.index(
238 "private:\n struct header_frame {")
239 assert "std::vector<data_frame> data_frames;" in hdr
240 assert "esi::Segment segment(size_t idx) const override" in hdr
241 assert "footer.coords_count = 0;" in hdr
242 assert "const std::vector<value_type> &coords" in hdr
243 assert "void construct(uint32_t x_translation, uint32_t y_translation, std::vector<data_frame> frames)" in hdr
244 assert "construct(x_translation, y_translation, std::move(frames));" in hdr
245 assert "auto &frame = frames.emplace_back();" in hdr
246 assert "for (const auto &element : coords) {" in hdr
247 assert "frame.coords = element;" in hdr
248 assert '!esi.window<\\"serial_coord_args\\"' in hdr
249 assert 'throw std::out_of_range("serial_coord_args: invalid segment index")' in hdr
253 """Headers pad out to the data frame width for count-only windows."""
254 uint16 = types.UIntType(
"ui16", 16)
255 uint32 = types.UIntType(
"ui32", 32)
256 element_id =
"!hw.struct<x: ui32, y: ui32>"
257 list_id = f
"!esi.list<{element_id}>"
258 arg_struct_id = f
"!hw.struct<coords: {list_id}>"
259 header_struct_id =
"!hw.struct<coords_count: ui16>"
260 data_struct_id = f
"!hw.struct<coords: !hw.array<1x{element_id}>>"
261 lowered_id = f
"!hw.union<header: {header_struct_id}, data: {data_struct_id}>"
262 window_id = (f
'!esi.window<"coords_only", {arg_struct_id}, '
263 '[<"header", [<"coords" countWidth 16>]>, '
264 '<"data", [<"coords", 1>]>]>')
266 element = types.StructType(element_id, [(
"x", uint32), (
"y", uint32)])
267 coord_list = types.ListType(list_id, element)
268 arg_struct = types.StructType(arg_struct_id, [(
"coords", coord_list)])
269 header_struct = types.StructType(header_struct_id, [(
"coords_count", uint16)])
270 data_struct = types.StructType(
272 [(
"coords", types.ArrayType(f
"!hw.array<1x{element_id}>", element, 1))],
274 lowered = types.UnionType(lowered_id, [(
"header", header_struct),
275 (
"data", data_struct)])
276 window = types.WindowType(window_id,
"coords_only", arg_struct, lowered, [
277 types.WindowType.Frame(
"header",
278 [types.WindowType.Field(
"coords", 0, 16)]),
279 types.WindowType.Frame(
"data", [types.WindowType.Field(
"coords", 1, 0)]),
283 assert "struct coords_only : public esi::SegmentedMessageData" in hdr
284 assert "struct header_frame {\n uint8_t _pad[6];\n count_type coords_count;\n };" in hdr
285 assert "header_frame footer{};" in hdr
286 assert "void construct(std::vector<data_frame> frames)" in hdr
290 """Window helpers copy array header fields and array-valued elements."""
291 uint8 = types.UIntType(
"ui8", 8)
292 uint16 = types.UIntType(
"ui16", 16)
293 header_array_id =
"!hw.array<2xui16>"
294 value_array_id =
"!hw.array<4xui8>"
295 list_id = f
"!esi.list<{value_array_id}>"
297 f
"!hw.struct<header_words: {header_array_id}, payloads: {list_id}>")
299 f
"!hw.struct<header_words: {header_array_id}, payloads_count: ui16>")
300 data_struct_id = f
"!hw.struct<payloads: !hw.array<1x{value_array_id}>>"
301 lowered_id = f
"!hw.union<header: {header_struct_id}, data: {data_struct_id}>"
302 window_id = (f
'!esi.window<"array_payloads", {arg_struct_id}, '
303 '[<"header", [<"header_words">, <"payloads" countWidth 16>]>, '
304 '<"data", [<"payloads", 1>]>]>')
306 header_array = types.ArrayType(header_array_id, uint16, 2)
307 value_array = types.ArrayType(value_array_id, uint8, 4)
308 payload_list = types.ListType(list_id, value_array)
309 arg_struct = types.StructType(arg_struct_id, [(
"header_words", header_array),
310 (
"payloads", payload_list)])
311 header_struct = types.StructType(header_struct_id,
312 [(
"header_words", header_array),
313 (
"payloads_count", uint16)])
314 data_struct = types.StructType(
317 types.ArrayType(f
"!hw.array<1x{value_array_id}>", value_array, 1))],
319 lowered = types.UnionType(lowered_id, [(
"header", header_struct),
320 (
"data", data_struct)])
321 window = types.WindowType(window_id,
"array_payloads", arg_struct, lowered, [
322 types.WindowType.Frame(
325 types.WindowType.Field(
"header_words", 0, 0),
326 types.WindowType.Field(
"payloads", 0, 16),
329 types.WindowType.Frame(
331 [types.WindowType.Field(
"payloads", 1, 0)],
336 assert "#include <cstring>" in hdr
337 assert "struct array_payloads : public esi::SegmentedMessageData" in hdr
338 assert "using value_type = uint8_t[4];" in hdr
339 assert "using count_type = uint16_t;" in hdr
340 assert "uint16_t header_words[2];" in hdr
341 assert "uint8_t payloads[4];" in hdr
342 assert "array_payloads(const uint16_t (&header_words)[2], const std::vector<value_type> &payloads)" in hdr
343 assert "void construct(const uint16_t (&header_words)[2], std::vector<data_frame> frames)" in hdr
344 assert "std::memcpy(&header.header_words, &header_words, sizeof(header.header_words));" in hdr
345 assert "std::memcpy(&frame.payloads, &element, sizeof(frame.payloads));" in hdr
346 assert 'throw std::out_of_range("array_payloads: invalid segment index")' in hdr