shitty bitfield packing code

This commit is contained in:
adman
2026-03-02 22:24:26 +01:00
parent 085b83cb71
commit 8d1a4b581b
10 changed files with 41961 additions and 41813 deletions
+2 -1
View File
@@ -270,7 +270,8 @@ func (g *Generator) GenerateProtocol(protocol Protocol, version string) (err err
v := protocol.Types.Types[k]
if strings.Contains(k, "packet") {
delete(protocol.Types.Types, k)
for prefix, types := range prefixTypeMap {
for _, prefix := range OrderedKeys(prefixTypeMap) {
types := prefixTypeMap[prefix]
if prefix == "" {
continue
}
+40 -2
View File
@@ -3,6 +3,8 @@ package main
import (
"go/ast"
"go/token"
"slices"
"strconv"
"github.com/admin-else/strom/proto_generator/protodef"
"github.com/go-viper/mapstructure/v2"
@@ -156,11 +158,47 @@ func BitFieldEncoder(g *Generator, varToSet ast.Expr, dataRaw any, name string)
for _, field := range data {
totalSize += field.Size
}
p, err := BitSizeToUnsignedProtodefName(totalSize)
packedVarName := name + "Packed"
packedExpr := Ident(packedVarName)
packedTypeStr := "uint" + strconv.Itoa(totalSize)
s = append(s, VarStmt(packedVarName, Ident(packedTypeStr)))
slices.Reverse(data)
shiftBy := 0
for _, field := range data {
boolFix := packedTypeStr
if field.Size == 1 {
boolFix = "proto_base.Bool2" + boolFix
}
s = append(s, OrAssign( // lisp ahh
packedExpr,
LeftShift(
Grouping(
BinAnd(
Call(
Ident(boolFix),
SelectorExprAndStr(
varToSet,
CamelCase(field.Name),
),
),
NumLitHex((1<<field.Size)-1),
),
),
NumLitHex(shiftBy),
),
))
shiftBy += field.Size
}
encodePacked, err := g.VisitEncoder(packedExpr, "u"+strconv.Itoa(totalSize), name)
if err != nil {
return
}
return g.VisitEncoder(varToSet, p, name)
s = append(s, encodePacked...)
return
}
func SwitchEncoder(g *Generator, varToSet ast.Expr, dataRaw any, name string) (s []ast.Stmt, err error) {
+6 -3
View File
@@ -60,13 +60,16 @@ func VisitBufferType(_ *Generator, dataRaw any) (e ast.Expr, err error) {
return
}
func VisitArrayTypeVisitorType(g *Generator, dataRaw any) (ast.Expr, error) {
func VisitArrayTypeVisitorType(g *Generator, dataRaw any) (e ast.Expr, err error) {
var data struct {
CountType any
Type any
}
must(mapstructure.Decode(dataRaw, &data))
e, err := g.VisitType(data.Type)
err = mapstructure.Decode(dataRaw, &data)
if err != nil {
return
}
e, err = g.VisitType(data.Type)
if err != nil {
return nil, err
}
+15 -1
View File
@@ -95,7 +95,7 @@ func NumLit(n int) *ast.BasicLit {
}
func NumLitHex(n int) *ast.BasicLit {
return NumLitStr(fmt.Sprintf("%X", n))
return NumLitStr(fmt.Sprintf("0x%X", n))
}
func NumLitStr(n string) *ast.BasicLit {
@@ -194,6 +194,14 @@ func SubAssign(lhs, rhs ast.Expr) (s *ast.AssignStmt) {
}
}
func OrAssign(lhs, rhs ast.Expr) (s *ast.AssignStmt) {
return &ast.AssignStmt{
Lhs: []ast.Expr{lhs},
Tok: token.OR_ASSIGN,
Rhs: []ast.Expr{rhs},
}
}
func Exprs(exprs ...ast.Expr) []ast.Expr {
return exprs
}
@@ -511,3 +519,9 @@ func LeftShift(lhs, rhs ast.Expr) *ast.BinaryExpr {
Y: rhs,
}
}
func Grouping(e ast.Expr) *ast.ParenExpr {
return &ast.ParenExpr{
X: e,
}
}
-6
View File
@@ -49,12 +49,6 @@ func ReverseMap[M map[K]V, K cmp.Ordered, V cmp.Ordered](m M) map[V]K {
return ret
}
func must(err error) {
if err != nil {
panic(err)
}
}
func PopFront[T any](s []T) (T, []T, bool) {
if len(s) == 0 {
var zero T