meta.mx raw

   1  package iskra
   2  
   3  // NodeKind classifies code constructs as a bitfield.
   4  type NodeKind uint16
   5  
   6  const (
   7  	KindUnknown  NodeKind = 0
   8  	KindPkg      NodeKind = 1 << 0
   9  	KindImport   NodeKind = 1 << 1
  10  	KindConst    NodeKind = 1 << 2
  11  	KindVar      NodeKind = 1 << 3
  12  	KindType     NodeKind = 1 << 4
  13  	KindFunc     NodeKind = 1 << 5
  14  	KindMethod   NodeKind = 1 << 6
  15  	KindField    NodeKind = 1 << 7
  16  	KindStmt     NodeKind = 1 << 8
  17  	KindExpr     NodeKind = 1 << 9
  18  	KindControl  NodeKind = 1 << 10
  19  	KindLiteral  NodeKind = 1 << 11
  20  )
  21  
  22  func (k NodeKind) Has(tag NodeKind) bool { return k&tag != 0 }
  23  func (k NodeKind) IsKnown() bool         { return k != KindUnknown }
  24  
  25  // MetaEntry is per-record semantic metadata, indexed by record index in Tree.RecMeta.
  26  // AdjCount/AdjOffset removed: cross-stage adjacency is key-implicit (same hash,
  27  // different stage prefix); cross-branch adjacency is in Record.Link[2].
  28  //
  29  // Extra layout:
  30  //   [0:4]   TritPath (uint32)
  31  //   [4]     low nibble: Rotation
  32  //   [5:8]   signature hash (24-bit, fast rejection)
  33  //   [8:12]  ContentOffset into Tree.TokenPool
  34  //   [12:16] ContentLen (token count)
  35  type MetaEntry struct {
  36  	Count    uint32
  37  	Kind     NodeKind
  38  	StageTag uint8
  39  	RefKind  uint8
  40  	_pad     [8]byte
  41  	Extra    [16]byte
  42  }
  43  
  44  func (m *MetaEntry) ContentOffset() uint32 {
  45  	return uint32(m.Extra[8]) |
  46  		uint32(m.Extra[9])<<8 |
  47  		uint32(m.Extra[10])<<16 |
  48  		uint32(m.Extra[11])<<24
  49  }
  50  
  51  func (m *MetaEntry) ContentLen() uint32 {
  52  	return uint32(m.Extra[12]) |
  53  		uint32(m.Extra[13])<<8 |
  54  		uint32(m.Extra[14])<<16 |
  55  		uint32(m.Extra[15])<<24
  56  }
  57  
  58  func (m *MetaEntry) SetContentRef(offset, length uint32) {
  59  	m.Extra[8] = byte(offset)
  60  	m.Extra[9] = byte(offset >> 8)
  61  	m.Extra[10] = byte(offset >> 16)
  62  	m.Extra[11] = byte(offset >> 24)
  63  	m.Extra[12] = byte(length)
  64  	m.Extra[13] = byte(length >> 8)
  65  	m.Extra[14] = byte(length >> 16)
  66  	m.Extra[15] = byte(length >> 24)
  67  }
  68  
  69  func (m *MetaEntry) SigHash() uint32 {
  70  	return uint32(m.Extra[5]) |
  71  		uint32(m.Extra[6])<<8 |
  72  		uint32(m.Extra[7])<<16
  73  }
  74  
  75  func (m *MetaEntry) SetSigHash(h uint32) {
  76  	m.Extra[5] = byte(h)
  77  	m.Extra[6] = byte(h >> 8)
  78  	m.Extra[7] = byte(h >> 16)
  79  }
  80