From 7e9b3aab67ee9e2b8d9b32ba076548839280c9ea Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 13 Jul 2017 13:23:01 +1000 Subject: [PATCH] Add a high_bits method. --- cmd/puffs-gen-c/func.go | 31 ++++++++++++++++++++++++++----- cmd/puffs-gen-c/main.go | 16 ++++++++++++++++ lang/check/bounds.go | 6 +++--- lang/check/type.go | 8 ++++---- lang/token/list.go | 9 ++++++--- 5 files changed, 55 insertions(+), 15 deletions(-) diff --git a/cmd/puffs-gen-c/func.go b/cmd/puffs-gen-c/func.go index bceb370b..bc6f0362 100644 --- a/cmd/puffs-gen-c/func.go +++ b/cmd/puffs-gen-c/func.go @@ -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 { diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index c865f88d..6f62dd75 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -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) diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 919d07eb..36a6f505 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -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)) } diff --git a/lang/check/type.go b/lang/check/type.go index 23d87a4e..87c332c5 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -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 { diff --git a/lang/token/list.go b/lang/token/list.go index 51cac9a9..7f923c6f 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -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<