package lattice import "unsafe" // NodeKind classifies iskra code constructs as a bitfield. iskra/meta.mx. type NodeKind uint16 const ( KindUnknown NodeKind = 0 KindPkg NodeKind = 1 << 0 KindImport NodeKind = 1 << 1 KindConst NodeKind = 1 << 2 KindVar NodeKind = 1 << 3 KindType NodeKind = 1 << 4 KindFunc NodeKind = 1 << 5 KindMethod NodeKind = 1 << 6 KindField NodeKind = 1 << 7 KindStmt NodeKind = 1 << 8 KindExpr NodeKind = 1 << 9 KindControl NodeKind = 1 << 10 KindLiteral NodeKind = 1 << 11 ) // Has reports whether any bit of tag is set. func (k NodeKind) Has(tag NodeKind) bool { return k&tag != 0 } // IsKnown reports whether any kind bit is set. func (k NodeKind) IsKnown() bool { return k != KindUnknown } // MetaEntry is the 26-byte per-record metadata of the .meta file, held in a // 32-byte struct so it stays aligned. iskra/meta.mx; the on-disk encoding is // lattice/store.go WriteMetaEntry. // // iskra's Count, Kind, StageTag and RefKind do not fit in the go-ref POS/Adj // layout below, so a parsed .meta record lives in Store.Meta, not here. // // Extra layout, as iskra/meta.mx and rotation.mx define it: // // [0:4] TritPath // [4] low nibble: Rotation // [5:8] signature hash, 24 bits // [8:12] content offset into the token pool // [12:16] content length, in tokens type MetaEntry struct { Frequency float32 PosTag POS AdjCount uint16 AdjOffset uint32 LangTag uint8 _pad1 [3]byte Extra [16]byte } var _ [32 - int(unsafe.Sizeof(MetaEntry{}))]byte var _ [int(unsafe.Sizeof(MetaEntry{})) - 32]byte // POS is a part-of-speech bitfield, from go-ref meta.go. type POS uint16 const ( POSUnknown POS = 0 POSNoun POS = 1 << 0 POSVerb POS = 1 << 1 POSAdjective POS = 1 << 2 POSAdverb POS = 1 << 3 POSPreposition POS = 1 << 4 POSConjunction POS = 1 << 5 POSPronoun POS = 1 << 6 POSInterjection POS = 1 << 7 POSNumeral POS = 1 << 8 POSParticle POS = 1 << 9 POSPrefix POS = 1 << 10 POSSuffix POS = 1 << 11 POSPhrase POS = 1 << 12 POSAbbreviation POS = 1 << 13 POSProperNoun POS = 1 << 14 POSAuxiliary POS = 1 << 15 ) // Has reports whether any bit of tag is set. func (p POS) Has(tag POS) bool { return p&tag != 0 } // IsKnown reports whether any POS bit is set. func (p POS) IsKnown() bool { return p != POSUnknown } // POSOverlap reports whether two bitfields share a tag. func POSOverlap(a, b POS) bool { return a&b != 0 } // ContentOffset reads Extra[8:12]. func (m *MetaEntry) ContentOffset() uint32 { return uint32(m.Extra[8]) | uint32(m.Extra[9])<<8 | uint32(m.Extra[10])<<16 | uint32(m.Extra[11])<<24 } // ContentLen reads Extra[12:16]. func (m *MetaEntry) ContentLen() uint32 { return uint32(m.Extra[12]) | uint32(m.Extra[13])<<8 | uint32(m.Extra[14])<<16 | uint32(m.Extra[15])<<24 } // SetContentRef writes a token-pool reference into Extra[8:16]. func (m *MetaEntry) SetContentRef(offset, length uint32) { m.Extra[8] = byte(offset) m.Extra[9] = byte(offset >> 8) m.Extra[10] = byte(offset >> 16) m.Extra[11] = byte(offset >> 24) m.Extra[12] = byte(length) m.Extra[13] = byte(length >> 8) m.Extra[14] = byte(length >> 16) m.Extra[15] = byte(length >> 24) } // SigHash reads the 24-bit signature hash from Extra[5:8]. func (m *MetaEntry) SigHash() uint32 { return uint32(m.Extra[5]) | uint32(m.Extra[6])<<8 | uint32(m.Extra[7])<<16 } // SetSigHash writes a 24-bit signature hash into Extra[5:8]. func (m *MetaEntry) SetSigHash(h uint32) { m.Extra[5] = byte(h) m.Extra[6] = byte(h >> 8) m.Extra[7] = byte(h >> 16) } // TritPath reads the packed coordinate from Extra[0:4]. func (m *MetaEntry) TritPath() TritPath { return TritPath(m.Extra[0]) | TritPath(m.Extra[1])<<8 | TritPath(m.Extra[2])<<16 | TritPath(m.Extra[3])<<24 } // SetTritPath writes the packed coordinate into Extra[0:4]. func (m *MetaEntry) SetTritPath(p TritPath) { m.Extra[0] = byte(p) m.Extra[1] = byte(p >> 8) m.Extra[2] = byte(p >> 16) m.Extra[3] = byte(p >> 24) } // Rotation reads the low nibble of Extra[4]. func (m *MetaEntry) Rotation() Rotation { return Rotation(m.Extra[4] & 0x0F) } // SetRotation writes the low nibble of Extra[4]. func (m *MetaEntry) SetRotation(r Rotation) { m.Extra[4] = (m.Extra[4] & 0xF0) | byte(r&0x0F) } // SigHash reads the 24-bit signature hash from a .meta record's Extra[5:8]. func (m *MetaRecord) SigHash() uint32 { return uint32(m.Extra[5]) | uint32(m.Extra[6])<<8 | uint32(m.Extra[7])<<16 } // SetSigHash writes a 24-bit signature hash into Extra[5:8]. func (m *MetaRecord) SetSigHash(h uint32) { m.Extra[5] = byte(h) m.Extra[6] = byte(h >> 8) m.Extra[7] = byte(h >> 16) } // ContentOffset reads Extra[8:12]. func (m *MetaRecord) ContentOffset() uint32 { return le.Uint32(m.Extra[8:12]) } // ContentLen reads Extra[12:16]. func (m *MetaRecord) ContentLen() uint32 { return le.Uint32(m.Extra[12:16]) } // SetContentRef writes a token-pool reference into Extra[8:16]. func (m *MetaRecord) SetContentRef(offset, length uint32) { le.PutUint32(m.Extra[8:12], offset) le.PutUint32(m.Extra[12:16], length) } // TritPath reads the packed coordinate from Extra[0:4]. func (m *MetaRecord) TritPath() TritPath { return TritPath(le.Uint32(m.Extra[0:4])) } // SetTritPath writes the packed coordinate into Extra[0:4]. func (m *MetaRecord) SetTritPath(p TritPath) { le.PutUint32(m.Extra[0:4], uint32(p)) } // Rotation reads the low nibble of Extra[4]. func (m *MetaRecord) Rotation() Rotation { return Rotation(m.Extra[4] & 0x0F) } // SetRotation writes the low nibble of Extra[4]. func (m *MetaRecord) SetRotation(r Rotation) { m.Extra[4] = (m.Extra[4] & 0xF0) | byte(r&0x0F) }