mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
Factor out cgen/expr.go.
This commit is contained in:
1 parent
0f86add17e
commit
dd12b469a3
3 files changed
+379
-368
No files matched your search
@@ -672,195 +672,3 @@ func (g *gen) writeCtorImpl(b *buffer, n *a.Struct) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeExprUnaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
|
||||
// TODO.
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeExprBinaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
|
||||
op := n.ID0()
|
||||
if op.Key() == t.KeyXBinaryAs {
|
||||
return g.writeExprAs(b, n.LHS().Expr(), n.RHS().TypeExpr(), rp, depth)
|
||||
}
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb('(')
|
||||
}
|
||||
if err := g.writeExpr(b, n.LHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO: does KeyXBinaryAmpHat need special consideration?
|
||||
b.writes(cOpNames[0xFF&op.Key()])
|
||||
if err := g.writeExpr(b, n.RHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb(')')
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeExprAs(b *buffer, lhs *a.Expr, rhs *a.TypeExpr, rp replacementPolicy, depth uint32) error {
|
||||
b.writes("((")
|
||||
// TODO: watch for passing an array type to writeCTypeName? In C, an array
|
||||
// type can decay into a pointer.
|
||||
if err := g.writeCTypeName(b, rhs, "", ""); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes(")(")
|
||||
if err := g.writeExpr(b, lhs, rp, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes("))")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeExprAssociativeOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb('(')
|
||||
}
|
||||
opName := cOpNames[0xFF&n.ID0().Key()]
|
||||
for i, o := range n.Args() {
|
||||
if i != 0 {
|
||||
b.writes(opName)
|
||||
}
|
||||
if err := g.writeExpr(b, o.Expr(), rp, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb(')')
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, varName string) error {
|
||||
// It may help to refer to http://unixwiz.net/techtips/reading-cdecl.html
|
||||
|
||||
// maxNumPointers is an arbitrary implementation restriction.
|
||||
const maxNumPointers = 16
|
||||
|
||||
x := n
|
||||
for ; x != nil && x.Decorator().Key() == t.KeyOpenBracket; x = x.Inner() {
|
||||
}
|
||||
|
||||
numPointers, innermost := 0, x
|
||||
for ; innermost != nil && innermost.Inner() != nil; innermost = innermost.Inner() {
|
||||
// TODO: "nptr T", not just "ptr T".
|
||||
if p := innermost.Decorator().Key(); p == t.KeyPtr {
|
||||
if numPointers == maxNumPointers {
|
||||
return fmt.Errorf("cannot convert Puffs type %q to C: too many ptr's", n.String(g.tm))
|
||||
}
|
||||
numPointers++
|
||||
continue
|
||||
}
|
||||
// TODO: fix this.
|
||||
return fmt.Errorf("cannot convert Puffs type %q to C", n.String(g.tm))
|
||||
}
|
||||
|
||||
fallback := true
|
||||
if key := innermost.Name().Key(); key < t.Key(len(cTypeNames)) {
|
||||
if s := cTypeNames[key]; s != "" {
|
||||
b.writes(s)
|
||||
fallback = false
|
||||
}
|
||||
}
|
||||
if fallback {
|
||||
b.printf("%s%s", g.pkgPrefix, innermost.Name().String(g.tm))
|
||||
}
|
||||
|
||||
for i := 0; i < numPointers; i++ {
|
||||
b.writeb('*')
|
||||
}
|
||||
|
||||
b.writeb(' ')
|
||||
b.writes(varNamePrefix)
|
||||
b.writes(varName)
|
||||
|
||||
x = n
|
||||
for ; x != nil && x.Decorator().Key() == t.KeyOpenBracket; x = x.Inner() {
|
||||
b.writeb('[')
|
||||
b.writes(x.ArrayLength().ConstValue().String())
|
||||
b.writeb(']')
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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},
|
||||
}
|
||||
|
||||
var cTypeNames = [...]string{
|
||||
t.KeyI8: "int8_t",
|
||||
t.KeyI16: "int16_t",
|
||||
t.KeyI32: "int32_t",
|
||||
t.KeyI64: "int64_t",
|
||||
t.KeyU8: "uint8_t",
|
||||
t.KeyU16: "uint16_t",
|
||||
t.KeyU32: "uint32_t",
|
||||
t.KeyU64: "uint64_t",
|
||||
t.KeyUsize: "size_t",
|
||||
t.KeyBool: "bool",
|
||||
t.KeyBuf1: "puffs_base_buf1",
|
||||
t.KeyBuf2: "puffs_base_buf2",
|
||||
t.KeyReader1: "puffs_base_reader1",
|
||||
t.KeyWriter1: "puffs_base_writer1",
|
||||
}
|
||||
|
||||
var cOpNames = [256]string{
|
||||
t.KeyEq: " = ",
|
||||
t.KeyPlusEq: " += ",
|
||||
t.KeyMinusEq: " -= ",
|
||||
t.KeyStarEq: " *= ",
|
||||
t.KeySlashEq: " /= ",
|
||||
t.KeyShiftLEq: " <<= ",
|
||||
t.KeyShiftREq: " >>= ",
|
||||
t.KeyAmpEq: " &= ",
|
||||
t.KeyAmpHatEq: " no_such_amp_hat_C_operator ",
|
||||
t.KeyPipeEq: " |= ",
|
||||
t.KeyHatEq: " ^= ",
|
||||
t.KeyPercentEq: " %= ",
|
||||
|
||||
t.KeyXUnaryPlus: "+",
|
||||
t.KeyXUnaryMinus: "-",
|
||||
t.KeyXUnaryNot: "!",
|
||||
|
||||
t.KeyXBinaryPlus: " + ",
|
||||
t.KeyXBinaryMinus: " - ",
|
||||
t.KeyXBinaryStar: " * ",
|
||||
t.KeyXBinarySlash: " / ",
|
||||
t.KeyXBinaryShiftL: " << ",
|
||||
t.KeyXBinaryShiftR: " >> ",
|
||||
t.KeyXBinaryAmp: " & ",
|
||||
t.KeyXBinaryAmpHat: " no_such_amp_hat_C_operator ",
|
||||
t.KeyXBinaryPipe: " | ",
|
||||
t.KeyXBinaryHat: " ^ ",
|
||||
t.KeyXBinaryPercent: " % ",
|
||||
t.KeyXBinaryNotEq: " != ",
|
||||
t.KeyXBinaryLessThan: " < ",
|
||||
t.KeyXBinaryLessEq: " <= ",
|
||||
t.KeyXBinaryEqEq: " == ",
|
||||
t.KeyXBinaryGreaterEq: " >= ",
|
||||
t.KeyXBinaryGreaterThan: " > ",
|
||||
t.KeyXBinaryAnd: " && ",
|
||||
t.KeyXBinaryOr: " || ",
|
||||
t.KeyXBinaryAs: " no_such_as_C_operator ",
|
||||
|
||||
t.KeyXAssociativePlus: " + ",
|
||||
t.KeyXAssociativeStar: " * ",
|
||||
t.KeyXAssociativeAmp: " & ",
|
||||
t.KeyXAssociativePipe: " | ",
|
||||
t.KeyXAssociativeHat: " ^ ",
|
||||
t.KeyXAssociativeAnd: " && ",
|
||||
t.KeyXAssociativeOr: " || ",
|
||||
}
|
||||
@@ -0,0 +1,366 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
package cgen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
a "github.com/google/puffs/lang/ast"
|
||||
t "github.com/google/puffs/lang/token"
|
||||
)
|
||||
|
||||
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 (g *gen) writeExprUnaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
|
||||
// TODO.
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeExprBinaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
|
||||
op := n.ID0()
|
||||
if op.Key() == t.KeyXBinaryAs {
|
||||
return g.writeExprAs(b, n.LHS().Expr(), n.RHS().TypeExpr(), rp, depth)
|
||||
}
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb('(')
|
||||
}
|
||||
if err := g.writeExpr(b, n.LHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO: does KeyXBinaryAmpHat need special consideration?
|
||||
b.writes(cOpNames[0xFF&op.Key()])
|
||||
if err := g.writeExpr(b, n.RHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb(')')
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeExprAs(b *buffer, lhs *a.Expr, rhs *a.TypeExpr, rp replacementPolicy, depth uint32) error {
|
||||
b.writes("((")
|
||||
// TODO: watch for passing an array type to writeCTypeName? In C, an array
|
||||
// type can decay into a pointer.
|
||||
if err := g.writeCTypeName(b, rhs, "", ""); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes(")(")
|
||||
if err := g.writeExpr(b, lhs, rp, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes("))")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeExprAssociativeOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb('(')
|
||||
}
|
||||
opName := cOpNames[0xFF&n.ID0().Key()]
|
||||
for i, o := range n.Args() {
|
||||
if i != 0 {
|
||||
b.writes(opName)
|
||||
}
|
||||
if err := g.writeExpr(b, o.Expr(), rp, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb(')')
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, varName string) error {
|
||||
// It may help to refer to http://unixwiz.net/techtips/reading-cdecl.html
|
||||
|
||||
// maxNumPointers is an arbitrary implementation restriction.
|
||||
const maxNumPointers = 16
|
||||
|
||||
x := n
|
||||
for ; x != nil && x.Decorator().Key() == t.KeyOpenBracket; x = x.Inner() {
|
||||
}
|
||||
|
||||
numPointers, innermost := 0, x
|
||||
for ; innermost != nil && innermost.Inner() != nil; innermost = innermost.Inner() {
|
||||
// TODO: "nptr T", not just "ptr T".
|
||||
if p := innermost.Decorator().Key(); p == t.KeyPtr {
|
||||
if numPointers == maxNumPointers {
|
||||
return fmt.Errorf("cannot convert Puffs type %q to C: too many ptr's", n.String(g.tm))
|
||||
}
|
||||
numPointers++
|
||||
continue
|
||||
}
|
||||
// TODO: fix this.
|
||||
return fmt.Errorf("cannot convert Puffs type %q to C", n.String(g.tm))
|
||||
}
|
||||
|
||||
fallback := true
|
||||
if key := innermost.Name().Key(); key < t.Key(len(cTypeNames)) {
|
||||
if s := cTypeNames[key]; s != "" {
|
||||
b.writes(s)
|
||||
fallback = false
|
||||
}
|
||||
}
|
||||
if fallback {
|
||||
b.printf("%s%s", g.pkgPrefix, innermost.Name().String(g.tm))
|
||||
}
|
||||
|
||||
for i := 0; i < numPointers; i++ {
|
||||
b.writeb('*')
|
||||
}
|
||||
|
||||
b.writeb(' ')
|
||||
b.writes(varNamePrefix)
|
||||
b.writes(varName)
|
||||
|
||||
x = n
|
||||
for ; x != nil && x.Decorator().Key() == t.KeyOpenBracket; x = x.Inner() {
|
||||
b.writeb('[')
|
||||
b.writes(x.ArrayLength().ConstValue().String())
|
||||
b.writeb(']')
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var cTypeNames = [...]string{
|
||||
t.KeyI8: "int8_t",
|
||||
t.KeyI16: "int16_t",
|
||||
t.KeyI32: "int32_t",
|
||||
t.KeyI64: "int64_t",
|
||||
t.KeyU8: "uint8_t",
|
||||
t.KeyU16: "uint16_t",
|
||||
t.KeyU32: "uint32_t",
|
||||
t.KeyU64: "uint64_t",
|
||||
t.KeyUsize: "size_t",
|
||||
t.KeyBool: "bool",
|
||||
t.KeyBuf1: "puffs_base_buf1",
|
||||
t.KeyBuf2: "puffs_base_buf2",
|
||||
t.KeyReader1: "puffs_base_reader1",
|
||||
t.KeyWriter1: "puffs_base_writer1",
|
||||
}
|
||||
|
||||
var cOpNames = [256]string{
|
||||
t.KeyEq: " = ",
|
||||
t.KeyPlusEq: " += ",
|
||||
t.KeyMinusEq: " -= ",
|
||||
t.KeyStarEq: " *= ",
|
||||
t.KeySlashEq: " /= ",
|
||||
t.KeyShiftLEq: " <<= ",
|
||||
t.KeyShiftREq: " >>= ",
|
||||
t.KeyAmpEq: " &= ",
|
||||
t.KeyAmpHatEq: " no_such_amp_hat_C_operator ",
|
||||
t.KeyPipeEq: " |= ",
|
||||
t.KeyHatEq: " ^= ",
|
||||
t.KeyPercentEq: " %= ",
|
||||
|
||||
t.KeyXUnaryPlus: "+",
|
||||
t.KeyXUnaryMinus: "-",
|
||||
t.KeyXUnaryNot: "!",
|
||||
|
||||
t.KeyXBinaryPlus: " + ",
|
||||
t.KeyXBinaryMinus: " - ",
|
||||
t.KeyXBinaryStar: " * ",
|
||||
t.KeyXBinarySlash: " / ",
|
||||
t.KeyXBinaryShiftL: " << ",
|
||||
t.KeyXBinaryShiftR: " >> ",
|
||||
t.KeyXBinaryAmp: " & ",
|
||||
t.KeyXBinaryAmpHat: " no_such_amp_hat_C_operator ",
|
||||
t.KeyXBinaryPipe: " | ",
|
||||
t.KeyXBinaryHat: " ^ ",
|
||||
t.KeyXBinaryPercent: " % ",
|
||||
t.KeyXBinaryNotEq: " != ",
|
||||
t.KeyXBinaryLessThan: " < ",
|
||||
t.KeyXBinaryLessEq: " <= ",
|
||||
t.KeyXBinaryEqEq: " == ",
|
||||
t.KeyXBinaryGreaterEq: " >= ",
|
||||
t.KeyXBinaryGreaterThan: " > ",
|
||||
t.KeyXBinaryAnd: " && ",
|
||||
t.KeyXBinaryOr: " || ",
|
||||
t.KeyXBinaryAs: " no_such_as_C_operator ",
|
||||
|
||||
t.KeyXAssociativePlus: " + ",
|
||||
t.KeyXAssociativeStar: " * ",
|
||||
t.KeyXAssociativeAmp: " & ",
|
||||
t.KeyXAssociativePipe: " | ",
|
||||
t.KeyXAssociativeHat: " ^ ",
|
||||
t.KeyXAssociativeAnd: " && ",
|
||||
t.KeyXAssociativeOr: " || ",
|
||||
}
|
||||
@@ -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},
|
||||
}
|
||||
Reference in new issue
Block a user