raz / conform / run / Offsets.raz

48 lines · 1.9 KiB · raw · history · last change 264716cb

1(* OFFSET(T, f) and SIZE(T.f), where a field lies in a record type and
2 how big it is (report sections 5.5 and 6.8): fields aligned to their
3 size, with padding before them; a nested record's field; a packed
4 record's, with none; another module's record, M.T; and both in a
5 constant. *)
6module Offsets;
7import Out, Layout;
8
9type
10 Mixed = record a: U8; b: I32; c: U16; d: I64; e: U8; end;
11 Pair = record lo, hi: U16; end;
12 Outer = record flag: U8; inner: Pair; tail: [3]I32; end;
13 Tight = packed record a: U8; b: I32; c: U16; end;
14
15const
16 EndOfD = OFFSET(Mixed, d) + SIZE(Mixed.d);
17 HiAt = OFFSET(Outer, inner) + OFFSET(Pair, hi);
18 LenAt = OFFSET(Layout.Head, len);
19 IdSize = SIZE(Layout.Head.id);
20 Typed = U8(OFFSET(Mixed, e) + 250); (* 24 + 250 wraps to 18 *)
21
22(* name, then the offset and the size of a field *)
23proc row(name: []const U8; off, size: INT);
24begin
25 Out.str(name); Out.str(" "); Out.int(off); Out.str(" "); Out.int(size); Out.ln;
26end;
27
28proc main*;
29begin
30 row("Mixed.a", OFFSET(Mixed, a), SIZE(Mixed.a));
31 row("Mixed.b", OFFSET(Mixed, b), SIZE(Mixed.b));
32 row("Mixed.c", OFFSET(Mixed, c), SIZE(Mixed.c));
33 row("Mixed.d", OFFSET(Mixed, d), SIZE(Mixed.d));
34 row("Mixed.e", OFFSET(Mixed, e), SIZE(Mixed.e));
35 row("Mixed", 0, SIZE(Mixed));
36 row("Outer.flag", OFFSET(Outer, flag), SIZE(Outer.flag));
37 row("Outer.inner", OFFSET(Outer, inner), SIZE(Outer.inner));
38 row("Outer.tail", OFFSET(Outer, tail), SIZE(Outer.tail));
39 row("Pair.hi", OFFSET(Pair, hi), SIZE(Pair.hi));
40 row("Tight.b", OFFSET(Tight, b), SIZE(Tight.b));
41 row("Tight.c", OFFSET(Tight, c), SIZE(Tight.c));
42 row("Head.len", OFFSET(Layout.Head, len), SIZE(Layout.Head.len));
43 row("Head.id", OFFSET(Layout.Head, id), SIZE(Layout.Head.id));
44 Out.int(EndOfD); Out.str(" "); Out.int(HiAt); Out.str(" "); Out.int(LenAt); Out.str(" ");
45 Out.int(IdSize); Out.str(" "); Out.int(I64(Typed)); Out.ln;
46end;
47
48end Offsets.