mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Add a high_bits method.
This commit is contained in:
1 parent
73a4f2b263
commit
7e9b3aab67
5 files changed
+55
-15
No files matched your search
+26
-5
@@ -1055,11 +1055,12 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro
|
||||
|
||||
case t.KeyOpenParen:
|
||||
// n is a function call.
|
||||
// TODO: delete this hack that only matches "foo.low_bits(etc)".
|
||||
if isLowBits(g.tm, n) {
|
||||
// TODO: delete this hack that only matches "foo.bar_bits(etc)".
|
||||
if isLowHighBits(g.tm, n, t.KeyLowBits) {
|
||||
// "x.low_bits(n:etc)" in C is "((x) & ((1 << (n)) - 1))".
|
||||
x := n.LHS().Expr().LHS().Expr()
|
||||
g.writes("((")
|
||||
if err := g.writeExpr(n.LHS().Expr().LHS().Expr(), rp, parenthesesOptional, depth); err != nil {
|
||||
if err := g.writeExpr(x, rp, parenthesesOptional, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
g.writes(") & ((1 << (")
|
||||
@@ -1069,6 +1070,26 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro
|
||||
g.writes(")) - 1))")
|
||||
return nil
|
||||
}
|
||||
if isLowHighBits(g.tm, n, t.KeyHighBits) {
|
||||
// "x.high_bits(n:etc)" in C is "((x) >> (8*sizeof(x) - (n)))".
|
||||
x := n.LHS().Expr().LHS().Expr()
|
||||
g.writes("((")
|
||||
if err := g.writeExpr(x, rp, parenthesesOptional, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
g.writes(") >> (")
|
||||
if sz, err := g.sizeof(x.MType()); err != nil {
|
||||
return err
|
||||
} else {
|
||||
g.printf("%d", 8*sz)
|
||||
}
|
||||
g.writes(" - (")
|
||||
if err := g.writeExpr(n.Args()[0].Arg().Value(), rp, parenthesesOptional, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
g.writes(")))")
|
||||
return nil
|
||||
}
|
||||
// TODO.
|
||||
|
||||
case t.KeyOpenBracket:
|
||||
@@ -1165,13 +1186,13 @@ func isThisMethod(tm *t.Map, n *a.Expr, methodName string, nArgs int) bool {
|
||||
return n.ID0() == 0 && n.ID1().Key() == t.KeyThis
|
||||
}
|
||||
|
||||
func isLowBits(tm *t.Map, n *a.Expr) bool {
|
||||
func isLowHighBits(tm *t.Map, n *a.Expr, methodName t.Key) bool {
|
||||
// TODO: check that n.Args() is "(n:bar)".
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == t.KeyLowBits
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName
|
||||
}
|
||||
|
||||
func isSetLiteralWidth(tm *t.Map, n *a.Expr) bool {
|
||||
|
||||
@@ -402,6 +402,22 @@ func (g *gen) cName(name string) string {
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func (g *gen) sizeof(typ *a.TypeExpr) (uint32, error) {
|
||||
if typ.Decorator() == 0 {
|
||||
switch typ.Name().Key() {
|
||||
case t.KeyU8:
|
||||
return 1, nil
|
||||
case t.KeyU16:
|
||||
return 2, nil
|
||||
case t.KeyU32:
|
||||
return 4, nil
|
||||
case t.KeyU64:
|
||||
return 8, nil
|
||||
}
|
||||
}
|
||||
return 0, fmt.Errorf("unknown sizeof for %q", typ.String(g.tm))
|
||||
}
|
||||
|
||||
func (g *gen) gatherStatuses(n *a.Status) error {
|
||||
raw := n.Message().String(g.tm)
|
||||
msg, ok := t.Unescape(raw)
|
||||
|
||||
@@ -638,8 +638,8 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
}
|
||||
break
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.low_bits(etc)".
|
||||
if isLowBits(q.tm, n) {
|
||||
// TODO: delete this hack that only matches "foo.bar_bits(etc)".
|
||||
if isLowHighBits(q.tm, n, t.KeyLowBits) || isLowHighBits(q.tm, n, t.KeyHighBits) {
|
||||
a := n.Args()[0].Arg().Value()
|
||||
aMin, aMax, err := q.bcheckExpr(a, depth)
|
||||
if err != nil {
|
||||
@@ -650,7 +650,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
// a.MType() is u32. Checking this properly should fall out
|
||||
// when a *a.TypeExpr can express function types.
|
||||
}
|
||||
// TODO: sixtyFour should actually be sizeof(n.LHS().Expr()).
|
||||
// TODO: sixtyFour should actually be 8 * sizeof(n.LHS().Expr()).
|
||||
if aMax.Cmp(sixtyFour) > 0 {
|
||||
return nil, nil, fmt.Errorf("check: low_bits argument %q is possibly too large", a.String(q.tm))
|
||||
}
|
||||
|
||||
+4
-4
@@ -354,8 +354,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
n.SetMType(typeExprPlaceholder) // HACK.
|
||||
return nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.low_bits(etc)".
|
||||
if isLowBits(q.tm, n) {
|
||||
// TODO: delete this hack that only matches "foo.bar_bits(etc)".
|
||||
if isLowHighBits(q.tm, n, t.KeyLowBits) || isLowHighBits(q.tm, n, t.KeyHighBits) {
|
||||
foo := n.LHS().Expr().LHS().Expr()
|
||||
if err := q.tcheckExpr(foo, depth); err != nil {
|
||||
return err
|
||||
@@ -510,13 +510,13 @@ func isThisMethod(tm *t.Map, n *a.Expr, methodName string, nArgs int) bool {
|
||||
return n.ID0() == 0 && n.ID1().Key() == t.KeyThis
|
||||
}
|
||||
|
||||
func isLowBits(tm *t.Map, n *a.Expr) bool {
|
||||
func isLowHighBits(tm *t.Map, n *a.Expr, methodName t.Key) bool {
|
||||
// TODO: check that n.Args() is "(n:bar)".
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == t.KeyLowBits
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName
|
||||
}
|
||||
|
||||
func isSetLiteralWidth(tm *t.Map, n *a.Expr) bool {
|
||||
|
||||
+6
-3
@@ -236,7 +236,8 @@ const (
|
||||
KeyIn = Key(IDIn >> KeyShift)
|
||||
KeyOut = Key(IDOut >> KeyShift)
|
||||
|
||||
KeyLowBits = Key(IDLowBits >> KeyShift)
|
||||
KeyLowBits = Key(IDLowBits >> KeyShift)
|
||||
KeyHighBits = Key(IDHighBits >> KeyShift)
|
||||
|
||||
KeyReadU8 = Key(IDReadU8 >> KeyShift)
|
||||
KeyReadU16BE = Key(IDReadU16BE >> KeyShift)
|
||||
@@ -397,7 +398,8 @@ const (
|
||||
IDIn = ID(0xA2<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDOut = ID(0xA3<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
|
||||
IDLowBits = ID(0xA8<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDLowBits = ID(0xA8<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDHighBits = ID(0xA9<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
|
||||
IDReadU8 = ID(0xB1<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDReadU16BE = ID(0xB2<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
@@ -580,7 +582,8 @@ var builtInsByKey = [nBuiltInKeys]struct {
|
||||
KeyIn: {"in", IDIn},
|
||||
KeyOut: {"out", IDOut},
|
||||
|
||||
KeyLowBits: {"low_bits", IDLowBits},
|
||||
KeyLowBits: {"low_bits", IDLowBits},
|
||||
KeyHighBits: {"high_bits", IDHighBits},
|
||||
|
||||
KeyReadU8: {"read_u8", IDReadU8},
|
||||
KeyReadU16BE: {"read_u16be", IDReadU16BE},
|
||||
|
||||
Reference in new issue
Block a user