meta.go raw

   1  package lattice
   2  
   3  import "unsafe"
   4  
   5  // NodeKind classifies iskra code constructs as a bitfield. iskra/meta.mx.
   6  type NodeKind uint16
   7  
   8  const (
   9  	KindUnknown NodeKind = 0
  10  	KindPkg     NodeKind = 1 << 0
  11  	KindImport  NodeKind = 1 << 1
  12  	KindConst   NodeKind = 1 << 2
  13  	KindVar     NodeKind = 1 << 3
  14  	KindType    NodeKind = 1 << 4
  15  	KindFunc    NodeKind = 1 << 5
  16  	KindMethod  NodeKind = 1 << 6
  17  	KindField   NodeKind = 1 << 7
  18  	KindStmt    NodeKind = 1 << 8
  19  	KindExpr    NodeKind = 1 << 9
  20  	KindControl NodeKind = 1 << 10
  21  	KindLiteral NodeKind = 1 << 11
  22  )
  23  
  24  // Has reports whether any bit of tag is set.
  25  func (k NodeKind) Has(tag NodeKind) bool { return k&tag != 0 }
  26  
  27  // IsKnown reports whether any kind bit is set.
  28  func (k NodeKind) IsKnown() bool { return k != KindUnknown }
  29  
  30  // MetaEntry is the 26-byte per-record metadata of the .meta file, held in a
  31  // 32-byte struct so it stays aligned. iskra/meta.mx; the on-disk encoding is
  32  // lattice/store.go WriteMetaEntry.
  33  //
  34  // iskra's Count, Kind, StageTag and RefKind do not fit in the go-ref POS/Adj
  35  // layout below, so a parsed .meta record lives in Store.Meta, not here.
  36  //
  37  // Extra layout, as iskra/meta.mx and rotation.mx define it:
  38  //
  39  //	[0:4]   TritPath
  40  //	[4]     low nibble: Rotation
  41  //	[5:8]   signature hash, 24 bits
  42  //	[8:12]  content offset into the token pool
  43  //	[12:16] content length, in tokens
  44  type MetaEntry struct {
  45  	Frequency float32
  46  	PosTag    POS
  47  	AdjCount  uint16
  48  	AdjOffset uint32
  49  	LangTag   uint8
  50  	_pad1     [3]byte
  51  	Extra     [16]byte
  52  }
  53  
  54  var _ [32 - int(unsafe.Sizeof(MetaEntry{}))]byte
  55  var _ [int(unsafe.Sizeof(MetaEntry{})) - 32]byte
  56  
  57  // POS is a part-of-speech bitfield, from go-ref meta.go.
  58  type POS uint16
  59  
  60  const (
  61  	POSUnknown      POS = 0
  62  	POSNoun         POS = 1 << 0
  63  	POSVerb         POS = 1 << 1
  64  	POSAdjective    POS = 1 << 2
  65  	POSAdverb       POS = 1 << 3
  66  	POSPreposition  POS = 1 << 4
  67  	POSConjunction  POS = 1 << 5
  68  	POSPronoun      POS = 1 << 6
  69  	POSInterjection POS = 1 << 7
  70  	POSNumeral      POS = 1 << 8
  71  	POSParticle     POS = 1 << 9
  72  	POSPrefix       POS = 1 << 10
  73  	POSSuffix       POS = 1 << 11
  74  	POSPhrase       POS = 1 << 12
  75  	POSAbbreviation POS = 1 << 13
  76  	POSProperNoun   POS = 1 << 14
  77  	POSAuxiliary    POS = 1 << 15
  78  )
  79  
  80  // Has reports whether any bit of tag is set.
  81  func (p POS) Has(tag POS) bool { return p&tag != 0 }
  82  
  83  // IsKnown reports whether any POS bit is set.
  84  func (p POS) IsKnown() bool { return p != POSUnknown }
  85  
  86  // POSOverlap reports whether two bitfields share a tag.
  87  func POSOverlap(a, b POS) bool { return a&b != 0 }
  88  
  89  // ContentOffset reads Extra[8:12].
  90  func (m *MetaEntry) ContentOffset() uint32 {
  91  	return uint32(m.Extra[8]) | uint32(m.Extra[9])<<8 | uint32(m.Extra[10])<<16 | uint32(m.Extra[11])<<24
  92  }
  93  
  94  // ContentLen reads Extra[12:16].
  95  func (m *MetaEntry) ContentLen() uint32 {
  96  	return uint32(m.Extra[12]) | uint32(m.Extra[13])<<8 | uint32(m.Extra[14])<<16 | uint32(m.Extra[15])<<24
  97  }
  98  
  99  // SetContentRef writes a token-pool reference into Extra[8:16].
 100  func (m *MetaEntry) SetContentRef(offset, length uint32) {
 101  	m.Extra[8] = byte(offset)
 102  	m.Extra[9] = byte(offset >> 8)
 103  	m.Extra[10] = byte(offset >> 16)
 104  	m.Extra[11] = byte(offset >> 24)
 105  	m.Extra[12] = byte(length)
 106  	m.Extra[13] = byte(length >> 8)
 107  	m.Extra[14] = byte(length >> 16)
 108  	m.Extra[15] = byte(length >> 24)
 109  }
 110  
 111  // SigHash reads the 24-bit signature hash from Extra[5:8].
 112  func (m *MetaEntry) SigHash() uint32 {
 113  	return uint32(m.Extra[5]) | uint32(m.Extra[6])<<8 | uint32(m.Extra[7])<<16
 114  }
 115  
 116  // SetSigHash writes a 24-bit signature hash into Extra[5:8].
 117  func (m *MetaEntry) SetSigHash(h uint32) {
 118  	m.Extra[5] = byte(h)
 119  	m.Extra[6] = byte(h >> 8)
 120  	m.Extra[7] = byte(h >> 16)
 121  }
 122  
 123  // TritPath reads the packed coordinate from Extra[0:4].
 124  func (m *MetaEntry) TritPath() TritPath {
 125  	return TritPath(m.Extra[0]) | TritPath(m.Extra[1])<<8 | TritPath(m.Extra[2])<<16 | TritPath(m.Extra[3])<<24
 126  }
 127  
 128  // SetTritPath writes the packed coordinate into Extra[0:4].
 129  func (m *MetaEntry) SetTritPath(p TritPath) {
 130  	m.Extra[0] = byte(p)
 131  	m.Extra[1] = byte(p >> 8)
 132  	m.Extra[2] = byte(p >> 16)
 133  	m.Extra[3] = byte(p >> 24)
 134  }
 135  
 136  // Rotation reads the low nibble of Extra[4].
 137  func (m *MetaEntry) Rotation() Rotation { return Rotation(m.Extra[4] & 0x0F) }
 138  
 139  // SetRotation writes the low nibble of Extra[4].
 140  func (m *MetaEntry) SetRotation(r Rotation) {
 141  	m.Extra[4] = (m.Extra[4] & 0xF0) | byte(r&0x0F)
 142  }
 143  
 144  // SigHash reads the 24-bit signature hash from a .meta record's Extra[5:8].
 145  func (m *MetaRecord) SigHash() uint32 {
 146  	return uint32(m.Extra[5]) | uint32(m.Extra[6])<<8 | uint32(m.Extra[7])<<16
 147  }
 148  
 149  // SetSigHash writes a 24-bit signature hash into Extra[5:8].
 150  func (m *MetaRecord) SetSigHash(h uint32) {
 151  	m.Extra[5] = byte(h)
 152  	m.Extra[6] = byte(h >> 8)
 153  	m.Extra[7] = byte(h >> 16)
 154  }
 155  
 156  // ContentOffset reads Extra[8:12].
 157  func (m *MetaRecord) ContentOffset() uint32 {
 158  	return le.Uint32(m.Extra[8:12])
 159  }
 160  
 161  // ContentLen reads Extra[12:16].
 162  func (m *MetaRecord) ContentLen() uint32 { return le.Uint32(m.Extra[12:16]) }
 163  
 164  // SetContentRef writes a token-pool reference into Extra[8:16].
 165  func (m *MetaRecord) SetContentRef(offset, length uint32) {
 166  	le.PutUint32(m.Extra[8:12], offset)
 167  	le.PutUint32(m.Extra[12:16], length)
 168  }
 169  
 170  // TritPath reads the packed coordinate from Extra[0:4].
 171  func (m *MetaRecord) TritPath() TritPath { return TritPath(le.Uint32(m.Extra[0:4])) }
 172  
 173  // SetTritPath writes the packed coordinate into Extra[0:4].
 174  func (m *MetaRecord) SetTritPath(p TritPath) { le.PutUint32(m.Extra[0:4], uint32(p)) }
 175  
 176  // Rotation reads the low nibble of Extra[4].
 177  func (m *MetaRecord) Rotation() Rotation { return Rotation(m.Extra[4] & 0x0F) }
 178  
 179  // SetRotation writes the low nibble of Extra[4].
 180  func (m *MetaRecord) SetRotation(r Rotation) {
 181  	m.Extra[4] = (m.Extra[4] & 0xF0) | byte(r&0x0F)
 182  }
 183