stage.mx raw
1 package iskra
2
3 import "git.smesh.lol/iskradb/lattice"
4
5 // Stage tags prepended to the key's high byte.
6 const (
7 StageSRC uint8 = 0x01
8 StageAST uint8 = 0x02
9 StageIR uint8 = 0x03
10 StageASM uint8 = 0x04
11 StageBIN uint8 = 0x05
12 )
13
14 // MakeKey encodes stage in the high 8 bits and the FNV hash in the lower 56
15 // bits of Key[0], with Key[1] = 0. This preserves existing stage-based
16 // semantics as a 128-bit lattice.Key (same collision rate as before, but
17 // compatible with the updated iskradb interface).
18 // MakeCodeKey encodes a code-lattice stage+FNV key.
19 // Renamed from MakeKey to make room for MakeKey(domain, coord, word) in coord.mx.
20 func MakeCodeKey(stage uint8, hash uint64) lattice.Key {
21 return lattice.Key{(uint64(stage) << 56) | (hash & 0x00FFFFFFFFFFFFFF), 0}
22 }
23
24 func KeyStage(key lattice.Key) uint8 {
25 return uint8(key[0] >> 56)
26 }
27
28 func KeyHash(key lattice.Key) uint64 {
29 return key[0] & 0x00FFFFFFFFFFFFFF
30 }
31
32 // NameKey builds a lattice.Key from stage and the segment's name.
33 func NameKey(stage uint8, name string) lattice.Key {
34 return MakeCodeKey(stage, HashBytes([]byte(name)))
35 }
36
37 func StageName(s uint8) string {
38 switch s {
39 case StageSRC:
40 return "SRC"
41 case StageAST:
42 return "AST"
43 case StageIR:
44 return "IR"
45 case StageASM:
46 return "ASM"
47 case StageBIN:
48 return "BIN"
49 }
50 return "?"
51 }
52