Factor out cgen/expr.go.

This commit is contained in:
Nigel Tao committed 2017-09-02 09:54:31 +10:00
1 parent 0f86add17e
commit dd12b469a3
3 files changed
+379 -368

No files matched your search

+13 -176
View File
@@ -1285,182 +1285,6 @@ func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endia
return nil
}
func (g *gen) writeExpr(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
if depth > a.MaxExprDepth {
return fmt.Errorf("expression recursion depth too large")
}
depth++
if rp == replaceCallSuspendibles && n.CallSuspendible() {
if g.currFunk.tempR >= g.currFunk.tempW {
return fmt.Errorf("internal error: temporary variable count out of sync")
}
// TODO: check that this works with nested call-suspendibles:
// "foo?().bar().qux?()(p?(), q?())".
//
// Also be aware of evaluation order in the presence of side effects:
// in "foo(a?(), b!(), c?())", b should be called between a and c.
b.printf("%s%d", tPrefix, g.currFunk.tempR)
g.currFunk.tempR++
return nil
}
if cv := n.ConstValue(); cv != nil {
if !n.MType().IsBool() {
b.writes(cv.String())
} else if cv.Cmp(zero) == 0 {
b.writes("false")
} else if cv.Cmp(one) == 0 {
b.writes("true")
} else {
return fmt.Errorf("%v has type bool but constant value %v is neither 0 or 1", n.String(g.tm), cv)
}
return nil
}
switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
case 0:
if err := g.writeExprOther(b, n, rp, pp, depth); err != nil {
return err
}
case t.FlagsUnaryOp:
if err := g.writeExprUnaryOp(b, n, rp, pp, depth); err != nil {
return err
}
case t.FlagsBinaryOp:
if err := g.writeExprBinaryOp(b, n, rp, pp, depth); err != nil {
return err
}
case t.FlagsAssociativeOp:
if err := g.writeExprAssociativeOp(b, n, rp, pp, depth); err != nil {
return err
}
default:
return fmt.Errorf("unrecognized token.Key (0x%X) for writeExpr", n.ID0().Key())
}
return nil
}
func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
switch n.ID0().Key() {
case 0:
if id1 := n.ID1(); id1.Key() == t.KeyThis {
b.writes("self")
} else {
if n.GlobalIdent() {
b.writes(g.pkgPrefix)
} else {
b.writes(vPrefix)
}
b.writes(id1.String(g.tm))
}
return nil
case t.KeyOpenParen:
// n is a function call.
// 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()
b.writes("((")
if err := g.writeExpr(b, x, rp, parenthesesOptional, depth); err != nil {
return err
}
b.writes(") & ((1 << (")
if err := g.writeExpr(b, n.Args()[0].Arg().Value(), rp, parenthesesOptional, depth); err != nil {
return err
}
b.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()
b.writes("((")
if err := g.writeExpr(b, x, rp, parenthesesOptional, depth); err != nil {
return err
}
b.writes(") >> (")
if sz, err := g.sizeof(x.MType()); err != nil {
return err
} else {
b.printf("%d", 8*sz)
}
b.writes(" - (")
if err := g.writeExpr(b, n.Args()[0].Arg().Value(), rp, parenthesesOptional, depth); err != nil {
return err
}
b.writes(")))")
return nil
}
if isIsErrorOKSuspension(g.tm, n, t.KeyIsSuspension) {
if pp == parenthesesMandatory {
b.writeb('(')
}
x := n.LHS().Expr().LHS().Expr()
if err := g.writeExpr(b, x, rp, parenthesesMandatory, depth); err != nil {
return err
}
// TODO: write < or == instead of > for KeyIsError or KeyIsOK.
b.writes(" > 0")
if pp == parenthesesMandatory {
b.writeb(')')
}
return nil
}
// TODO.
case t.KeyOpenBracket:
// n is an index.
if err := g.writeExpr(b, n.LHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
return err
}
b.writeb('[')
if err := g.writeExpr(b, n.RHS().Expr(), rp, parenthesesOptional, depth); err != nil {
return err
}
b.writeb(']')
return nil
case t.KeyColon:
// n is a slice.
// TODO.
case t.KeyDot:
lhs := n.LHS().Expr()
if lhs.ID1().Key() == t.KeyIn {
b.writes(aPrefix)
b.writes(n.ID1().String(g.tm))
return nil
}
if err := g.writeExpr(b, lhs, rp, parenthesesMandatory, depth); err != nil {
return err
}
if key := lhs.MType().Decorator().Key(); key == t.KeyPtr || key == t.KeyNptr {
b.writes("->")
} else {
b.writes(".")
}
b.writes("private_impl." + fPrefix)
b.writes(n.ID1().String(g.tm))
return nil
case t.KeyLimit:
// TODO: don't hard code so much detail.
b.writes("(puffs_base_reader1) {")
b.writes(".buf = a_src.buf,")
b.writes(".limit = (puffs_base_limit1) {")
b.printf(".ptr_to_len = &%s%s,", lPrefix, g.currFunk.limitVarName)
b.writes(".next = &a_src.limit,")
b.writes("}}")
g.currFunk.limitVarName = ""
return nil
}
return fmt.Errorf("unrecognized token.Key (0x%X) for writeExprOther", n.ID0().Key())
}
func isInSrc(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != nArgs {
return false
@@ -1544,3 +1368,16 @@ func isDecode(tm *t.Map, n *a.Expr) bool {
n = n.LHS().Expr()
return n.ID0().Key() == t.KeyDot && n.ID1() == tm.ByName("decode")
}
var numTypeBounds = [256][2]*big.Int{
t.KeyI8: {big.NewInt(-1 << 7), big.NewInt(1<<7 - 1)},
t.KeyI16: {big.NewInt(-1 << 15), big.NewInt(1<<15 - 1)},
t.KeyI32: {big.NewInt(-1 << 31), big.NewInt(1<<31 - 1)},
t.KeyI64: {big.NewInt(-1 << 63), big.NewInt(1<<63 - 1)},
t.KeyU8: {zero, big.NewInt(0).SetUint64(1<<8 - 1)},
t.KeyU16: {zero, big.NewInt(0).SetUint64(1<<16 - 1)},
t.KeyU32: {zero, big.NewInt(0).SetUint64(1<<32 - 1)},
t.KeyU64: {zero, big.NewInt(0).SetUint64(1<<64 - 1)},
t.KeyUsize: {zero, zero},
t.KeyBool: {zero, one},
}