| 1 | (* BYTES(x), the bytes of x as a []U8 (report section 6.8): of a |
| 2 | record, an array and a []I32 slice, read and written through; their |
| 3 | lengths, SIZE(T) or LEN(s) * SIZE(T); an empty slice's; and the |
| 4 | bytes of a field and an element, of what a pointer points at, of a |
| 5 | var parameter, and of a slice a call returns. Bytes are compared |
| 6 | with a view of the same place, which is the target's order too. *) |
| 7 | module BytesOf; |
| 8 | import Out; |
| 9 | import SYSTEM; |
| 10 | |
| 11 | type |
| 12 | Header = record magic: U32; count: U16; flags: U8; end; |
| 13 | |
| 14 | var g: [4]I32; |
| 15 | |
| 16 | (* the bytes of h, given as a var parameter, written zero *) |
| 17 | proc clear(var h: Header); |
| 18 | var b: []U8; i: INT; |
| 19 | begin |
| 20 | b := BYTES(h); |
| 21 | for i := 0 to LEN(b) - 1 do b[i] := 0; end; |
| 22 | end; |
| 23 | |
| 24 | (* the module's array, as a slice a call returns *) |
| 25 | proc whole: []I32; |
| 26 | begin |
| 27 | return g; |
| 28 | end; |
| 29 | |
| 30 | (* the bytes of the module's array, returned: they outlive the call *) |
| 31 | proc gBytes: []U8; |
| 32 | begin |
| 33 | return BYTES(g); |
| 34 | end; |
| 35 | |
| 36 | proc main*; |
| 37 | var h: Header; a: [3]I16; s, none: []I32; b: []U8; p: ^Header; n: I32; i: INT; |
| 38 | begin |
| 39 | h := {magic: 1, count: 2, flags: 3}; |
| 40 | b := BYTES(h); |
| 41 | Out.int(LEN(b)); Out.str(" "); Out.int(SIZE(Header)); Out.ln; |
| 42 | Out.int(LEN(BYTES(h.count))); Out.str(" "); Out.int(LEN(BYTES(h.flags))); Out.ln; |
| 43 | b[OFFSET(Header, magic)].(U32) := 0CAFEF00DH; (* written through the bytes *) |
| 44 | BYTES(h.count)[0].(U16) := 513; |
| 45 | Out.hex(U64(h.magic), 8); Out.str(" "); Out.int(I64(h.count)); Out.str(" "); Out.int(I64(h.flags)); Out.ln; |
| 46 | Out.int(I64(BYTES(h)[OFFSET(Header, flags)])); Out.ln; |
| 47 | |
| 48 | a := [1, -1, 256]; |
| 49 | b := BYTES(a); |
| 50 | Out.int(LEN(b)); Out.str(" "); Out.int(I64(b[2].(I16))); Out.str(" "); Out.int(I64(b[4].(I16))); Out.ln; |
| 51 | BYTES(a[1])[0] := 7; BYTES(a[1])[1] := 0; |
| 52 | Out.int(I64(a[1])); Out.ln; |
| 53 | |
| 54 | s := g[1..4]; |
| 55 | g := [10, 20, 30, 40]; |
| 56 | b := BYTES(s); |
| 57 | Out.int(LEN(b)); Out.str(" "); Out.int(LEN(s) * SIZE(I32)); Out.ln; |
| 58 | for i := 0 to LEN(s) - 1 do |
| 59 | b[i * SIZE(I32)].(I32) := b[i * SIZE(I32)].(I32) + 1; |
| 60 | end; |
| 61 | Out.int(I64(g[0])); Out.str(" "); Out.int(I64(g[1])); Out.str(" "); Out.int(I64(g[3])); Out.ln; |
| 62 | Out.int(LEN(BYTES(s[1..2]))); Out.str(" "); Out.int(LEN(BYTES(whole()))); Out.ln; |
| 63 | Out.int(LEN(BYTES(none))); Out.str(" "); Out.int(LEN(BYTES(g[2..2]))); Out.ln; |
| 64 | Out.int(LEN(gBytes())); Out.str(" "); Out.int(I64(gBytes()[4].(I32))); Out.ln; |
| 65 | |
| 66 | p := SYSTEM.ADR(h); |
| 67 | n := 0; |
| 68 | for i := 0 to OFFSET(Header, flags) do (* not the padding after *) |
| 69 | INC(n, I32(BYTES(p^)[i])); |
| 70 | end; |
| 71 | Out.int(I64(n)); Out.ln; |
| 72 | clear(h); |
| 73 | Out.int(I64(h.magic)); Out.str(" "); Out.int(I64(h.count)); Out.ln; |
| 74 | |
| 75 | n := 01020304H; |
| 76 | b := BYTES(n); |
| 77 | Out.int(LEN(b)); if b[0].(I32) = n then Out.str(" same"); end; Out.ln; |
| 78 | end; |
| 79 | |
| 80 | end BytesOf. |