raz / conform / run / Views.raz

102 lines · 2.8 KiB · raw · history · last change 264716cb

1(* Views, v.(T): the bytes of a variable read and written as another
2 type (report section 6.9). The output holds on a target that puts a
3 word's low byte first, as appendix A lets a target choose; the
4 bytes are written here a byte at a time where order matters. *)
5module Views;
6import Out, ViewData;
7import SYSTEM;
8
9type
10 Header = record magic: U32; count: U32; end;
11 Pair = packed record tag: U8; value: I32; end;
12
13var
14 mem: [64]U8; (* a module's array *)
15 regs: [4]U64;
16
17proc show(label: []const U8; v: I64);
18begin
19 Out.str(label); Out.str(" "); Out.int(v); Out.ln;
20end;
21
22(* a var parameter, an array reached through a pointer, viewed *)
23proc fill(var b: [16]U8; v: I64);
24begin
25 b[8].(I64) := v;
26end;
27
28(* a slice parameter's element, viewed *)
29proc sum(s: []const U8): I64;
30var i, t: INT;
31begin
32 t := 0;
33 i := 0;
34 while i + 4 <= LEN(s) do
35 t := t + INT(s[i].(I32));
36 i := i + 4;
37 end;
38 return t;
39end;
40
41proc main*;
42var
43 n, a: I64; b: [16]U8; s: []U8;
44 p: ^I64; h: Header;
45begin
46 (* an I64 in an array of bytes, at an index known and one not *)
47 mem[16].(I64) := -1;
48 show("mem[16].(I64)", mem[16].(I64));
49 show("mem[23]", I64(mem[23]));
50 a := 3;
51 mem[a].(I64) := 1000;
52 INC(mem[a].(I64), 8);
53 show("mem[3].(I64), misaligned", mem[a].(I64));
54 mem[56].(I64) := 7;
55 show("the last 8 bytes", mem[56].(I64));
56
57 (* a view of a whole variable, a local kept in a register
58 otherwise: the first byte of a number, its low byte, low byte
59 first *)
60 n := 258;
61 show("258's first byte", I64(n.(U8)));
62 show("U8(258)", I64(U8(n)));
63
64 (* a record over bytes, and a field of it, read and written *)
65 b[0] := 1; b[4] := 2;
66 h := b[0].(Header);
67 show("h.count", I64(h.count));
68 b[0].(Header).count := 9;
69 show("b[4]", I64(b[4]));
70
71 (* an array as the type, indexed past the view *)
72 mem[32].([2]I32)[1] := -5;
73 show("mem[36].(I32)", I64(mem[36].(I32)));
74
75 (* a packed record, and a pointer read from bytes, then followed *)
76 mem[40].(Pair) := {tag: 1, value: 300};
77 show("Pair's value", I64(mem[41].(I32)));
78 p := SYSTEM.ADR(n);
79 mem[48].(^I64) := p;
80 mem[48].(^I64)^ := 77;
81 show("n through a pointer from bytes", n);
82
83 (* a var parameter's array, a slice parameter, a slice's element *)
84 fill(b, 123456789);
85 show("b[8].(I64)", b[8].(I64));
86 s := mem[0..16];
87 s[0].(I32) := 10; s[4].(I32) := 20; s[8].(I32) := 30; s[12].(I32) := -1;
88 show("sum", sum(s));
89
90 (* a module's array of U64s viewed as bytes, and an element of
91 bigger elements viewed from its start *)
92 regs[1] := 0;
93 regs[0].([32]U8)[8] := 5;
94 show("regs[1]", I64(regs[1]));
95 regs[2].([2]U64)[1] := 6;
96 show("regs[3]", I64(regs[3]));
97
98 (* another module's variable, read through a view *)
99 show("ViewData.bytes[0].(I64)", ViewData.bytes[0].(I64));
100end;
101
102end Views.