diff --git a/cmd/wuffs-c/internal/cgen/expr.go b/cmd/wuffs-c/internal/cgen/expr.go index d501e7c2..6a691d8e 100644 --- a/cmd/wuffs-c/internal/cgen/expr.go +++ b/cmd/wuffs-c/internal/cgen/expr.go @@ -462,27 +462,67 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare } func (g *gen) writeExprUnaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error { - b.writes(cOpNames[0xFF&n.Operator()]) + op := n.Operator() + opName := cOpNames[0xFF&op] + if opName == "" { + return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm)) + } + + b.writes(opName) return g.writeExpr(b, n.RHS().Expr(), rp, parenthesesMandatory, depth) } func (g *gen) writeExprBinaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error { + opName, tilde := "", false + op := n.Operator() - if op == t.IDXBinaryAs { + switch op { + case t.IDXBinaryTildeSatPlus, t.IDXBinaryTildeSatMinus: + uType := "" + qid := n.MType().QID() + switch qid[1] { + case t.IDU8: + uType = "8" + case t.IDU16: + uType = "16" + case t.IDU32: + uType = "32" + case t.IDU64: + uType = "64" + } + if qid[0] != t.IDBase || uType == "" { + return fmt.Errorf("unsupported tilde-operator type %q", n.MType().Str(g.tm)) + } + + uOp := "add" + if op != t.IDXBinaryTildeSatPlus { + uOp = "sub" + } + b.printf("wuffs_base__u%s__sat_%s", uType, uOp) + opName, tilde = ",", true + + case t.IDXBinaryAs: return g.writeExprAs(b, n.LHS().Expr(), n.RHS().TypeExpr(), rp, depth) + + default: + opName = cOpNames[0xFF&op] + if opName == "" { + return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm)) + } } - if pp == parenthesesMandatory { + + if pp == parenthesesMandatory || tilde { 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]) + b.writes(opName) if err := g.writeExpr(b, n.RHS().Expr(), rp, parenthesesMandatory, depth); err != nil { return err } - if pp == parenthesesMandatory { + if pp == parenthesesMandatory || tilde { b.writeb(')') } return nil @@ -504,10 +544,15 @@ func (g *gen) writeExprAs(b *buffer, lhs *a.Expr, rhs *a.TypeExpr, rp replacemen } func (g *gen) writeExprAssociativeOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error { + op := n.Operator() + opName := cOpNames[0xFF&op] + if opName == "" { + return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm)) + } + if pp == parenthesesMandatory { b.writeb('(') } - opName := cOpNames[0xFF&n.Operator()] for i, o := range n.Args() { if i != 0 { b.writes(opName) @@ -636,12 +681,14 @@ var cOpNames = [256]string{ t.IDShiftLEq: " <<= ", t.IDShiftREq: " >>= ", t.IDAmpEq: " &= ", - t.IDAmpHatEq: " no_such_amp_hat_C_operator ", + t.IDAmpHatEq: " no_such_C_operator ", t.IDPipeEq: " |= ", t.IDHatEq: " ^= ", t.IDPercentEq: " %= ", t.IDTildeModPlusEq: " += ", t.IDTildeModMinusEq: " -= ", + t.IDTildeSatPlusEq: " no_such_C_operator ", + t.IDTildeSatMinusEq: " no_such_C_operator ", t.IDXUnaryPlus: " + ", t.IDXUnaryMinus: " - ", @@ -656,12 +703,14 @@ var cOpNames = [256]string{ t.IDXBinaryShiftL: " << ", t.IDXBinaryShiftR: " >> ", t.IDXBinaryAmp: " & ", - t.IDXBinaryAmpHat: " no_such_amp_hat_C_operator ", + t.IDXBinaryAmpHat: " no_such_C_operator ", t.IDXBinaryPipe: " | ", t.IDXBinaryHat: " ^ ", t.IDXBinaryPercent: " % ", t.IDXBinaryTildeModPlus: " + ", t.IDXBinaryTildeModMinus: " - ", + t.IDXBinaryTildeSatPlus: " no_such_C_operator ", + t.IDXBinaryTildeSatMinus: " no_such_C_operator ", t.IDXBinaryNotEq: " != ", t.IDXBinaryLessThan: " < ", t.IDXBinaryLessEq: " <= ", @@ -670,7 +719,7 @@ var cOpNames = [256]string{ t.IDXBinaryGreaterThan: " > ", t.IDXBinaryAnd: " && ", t.IDXBinaryOr: " || ", - t.IDXBinaryAs: " no_such_as_C_operator ", + t.IDXBinaryAs: " no_such_C_operator ", t.IDXAssociativePlus: " + ", t.IDXAssociativeStar: " * ", diff --git a/cmd/wuffs-c/internal/cgen/statement.go b/cmd/wuffs-c/internal/cgen/statement.go index 2242a0b5..302d9a02 100644 --- a/cmd/wuffs-c/internal/cgen/statement.go +++ b/cmd/wuffs-c/internal/cgen/statement.go @@ -81,8 +81,13 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { if err := g.writeExpr(b, n.LHS(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { return err } - // TODO: does KeyAmpHatEq need special consideration? - b.writes(cOpNames[0xFF&n.Operator()]) + // TODO: `~sat+=`, `~sat-=` and `&^=` need special consideration. + op := n.Operator() + opName := cOpNames[0xFF&op] + if opName == "" { + return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm)) + } + b.writes(opName) if err := g.writeExpr(b, n.RHS(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { return err } diff --git a/doc/changelog.md b/doc/changelog.md index 1e201503..ae292207 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -11,7 +11,7 @@ - Spun `std/zlib` out of `std/flate`. - Let the `std/zlib` decoder ignore checksums. - Renamed `std/flate` to `std/deflate`. -- Renamed `~+` to `~mod+`; add `~mod-`. +- Renamed `~+` to `~mod+`; added `~mod-`, `~sat+` and `~sat-`. - Renamed `[N] T` and `[] T` types to `array[N] T` and `slice T`. - Renamed `iterate.N` to `iterate[N]`. - Renamed `buf1`, `reader1`, etc to `io_buffer`, `io_reader`, etc. diff --git a/lang/ast/string.go b/lang/ast/string.go index c4ac0b91..36bdfd9a 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -171,6 +171,8 @@ var opStrings = [256]string{ t.IDXBinaryHat: " ^ ", t.IDXBinaryTildeModPlus: " ~mod+ ", t.IDXBinaryTildeModMinus: " ~mod- ", + t.IDXBinaryTildeSatPlus: " ~sat+ ", + t.IDXBinaryTildeSatMinus: " ~sat- ", t.IDXBinaryNotEq: " != ", t.IDXBinaryLessThan: " < ", t.IDXBinaryLessEq: " <= ", diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 2d0396cc..a1bcf533 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -1003,6 +1003,32 @@ func (q *checker) bcheckExprUnaryOp(n *a.Expr, depth uint32) (*big.Int, *big.Int return nil, nil, fmt.Errorf("check: unrecognized token (0x%X) for bcheckExprUnaryOp", n.Operator()) } +func (q *checker) bcheckExprXBinaryPlus(lhs *a.Expr, lMin *big.Int, lMax *big.Int, rhs *a.Expr, rMin *big.Int, rMax *big.Int) (*big.Int, *big.Int, error) { + return big.NewInt(0).Add(lMin, rMin), big.NewInt(0).Add(lMax, rMax), nil +} + +func (q *checker) bcheckExprXBinaryMinus(lhs *a.Expr, lMin *big.Int, lMax *big.Int, rhs *a.Expr, rMin *big.Int, rMax *big.Int) (*big.Int, *big.Int, error) { + nMin := big.NewInt(0).Sub(lMin, rMax) + nMax := big.NewInt(0).Sub(lMax, rMin) + for _, x := range q.facts { + xOp, xLHS, xRHS := parseBinaryOp(x) + if !lhs.Eq(xLHS) || !rhs.Eq(xRHS) { + continue + } + switch xOp { + case t.IDXBinaryLessThan: + nMax = min(nMax, minusOne) + case t.IDXBinaryLessEq: + nMax = min(nMax, zero) + case t.IDXBinaryGreaterEq: + nMin = max(nMin, zero) + case t.IDXBinaryGreaterThan: + nMin = max(nMin, one) + } + } + return nMin, nMax, nil +} + func (q *checker) bcheckExprBinaryOp(op t.ID, lhs *a.Expr, rhs *a.Expr, depth uint32) (*big.Int, *big.Int, error) { lMin, lMax, err := q.bcheckExpr(lhs, depth) if err != nil { @@ -1019,28 +1045,10 @@ func (q *checker) bcheckExprBinaryOp1(op t.ID, lhs *a.Expr, lMin *big.Int, lMax switch op { case t.IDXBinaryPlus: - return big.NewInt(0).Add(lMin, rMin), big.NewInt(0).Add(lMax, rMax), nil + return q.bcheckExprXBinaryPlus(lhs, lMin, lMax, rhs, rMin, rMax) case t.IDXBinaryMinus: - nMin := big.NewInt(0).Sub(lMin, rMax) - nMax := big.NewInt(0).Sub(lMax, rMin) - for _, x := range q.facts { - xOp, xLHS, xRHS := parseBinaryOp(x) - if !lhs.Eq(xLHS) || !rhs.Eq(xRHS) { - continue - } - switch xOp { - case t.IDXBinaryLessThan: - nMax = min(nMax, minusOne) - case t.IDXBinaryLessEq: - nMax = min(nMax, zero) - case t.IDXBinaryGreaterEq: - nMin = max(nMin, zero) - case t.IDXBinaryGreaterThan: - nMin = max(nMin, one) - } - } - return nMin, nMax, nil + return q.bcheckExprXBinaryMinus(lhs, lMin, lMax, rhs, rMin, rMax) case t.IDXBinaryStar: // TODO: handle multiplication by negative numbers. Note that this @@ -1132,6 +1140,33 @@ func (q *checker) bcheckExprBinaryOp1(op t.ID, lhs *a.Expr, lMin *big.Int, lMax return b[0], b[1], nil } + case t.IDXBinaryTildeSatPlus, t.IDXBinaryTildeSatMinus: + typ := lhs.MType() + if typ.IsIdeal() { + typ = rhs.MType() + } + if qid := typ.QID(); qid[0] == t.IDBase { + b := numTypeBounds[qid[1]] + + nFunc := (*checker).bcheckExprXBinaryPlus + if op != t.IDXBinaryTildeSatPlus { + nFunc = (*checker).bcheckExprXBinaryMinus + } + nMin, nMax, err := nFunc(q, lhs, lMin, lMax, rhs, rMin, rMax) + if err != nil { + return nil, nil, err + } + + if op == t.IDXBinaryTildeSatPlus { + nMin = min(nMin, b[1]) + nMax = min(nMax, b[1]) + } else { + nMin = max(nMin, b[0]) + nMax = max(nMax, b[0]) + } + return nMin, nMax, nil + } + case t.IDXBinaryNotEq, t.IDXBinaryLessThan, t.IDXBinaryLessEq, t.IDXBinaryEqEq, t.IDXBinaryGreaterEq, t.IDXBinaryGreaterThan, t.IDXBinaryAnd, t.IDXBinaryOr: return zero, one, nil diff --git a/lang/check/type.go b/lang/check/type.go index 4287169b..b37cf6b2 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -282,7 +282,7 @@ func (q *checker) tcheckAssign(n *a.Assign) error { n.Operator().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm)) } return nil - case t.IDTildeModPlusEq, t.IDTildeModMinusEq: + case t.IDTildeModPlusEq, t.IDTildeModMinusEq, t.IDTildeSatPlusEq, t.IDTildeSatMinusEq: if !lTyp.IsUnsignedInteger() { return fmt.Errorf("check: assignment %q: %q, of type %q, does not have unsigned integer type", n.Operator().Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm)) @@ -810,7 +810,8 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error { } switch op { - case t.IDXBinaryTildeModPlus, t.IDXBinaryTildeModMinus: + case t.IDXBinaryTildeModPlus, t.IDXBinaryTildeModMinus, + t.IDXBinaryTildeSatPlus, t.IDXBinaryTildeSatMinus: typ := lTyp if typ.IsIdeal() { typ = rTyp @@ -890,7 +891,8 @@ func evalConstValueBinaryOp(tm *t.Map, n *a.Expr, l *big.Int, r *big.Int) (*big. return nil, fmt.Errorf("check: division by zero in const expression %q", n.Str(tm)) } return big.NewInt(0).Mod(l, r), nil - case t.IDXBinaryTildeModPlus, t.IDXBinaryTildeModMinus: + case t.IDXBinaryTildeModPlus, t.IDXBinaryTildeModMinus, + t.IDXBinaryTildeSatPlus, t.IDXBinaryTildeSatMinus: return nil, fmt.Errorf("check: cannot apply tilde-operators to ideal numbers") case t.IDXBinaryNotEq: return btoi(l.Cmp(r) != 0), nil diff --git a/lang/token/list.go b/lang/token/list.go index 21b47f48..e7ba75c2 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -209,6 +209,8 @@ const ( IDPercentEq = ID(0x1B) IDTildeModPlusEq = ID(0x1C) IDTildeModMinusEq = ID(0x1D) + IDTildeSatPlusEq = ID(0x1E) + IDTildeSatMinusEq = ID(0x1F) ) const ( @@ -232,6 +234,8 @@ const ( IDPercent = ID(0x2B) IDTildeModPlus = ID(0x2C) IDTildeModMinus = ID(0x2D) + IDTildeSatPlus = ID(0x2E) + IDTildeSatMinus = ID(0x2F) IDNotEq = ID(0x30) IDLessThan = ID(0x31) @@ -268,17 +272,19 @@ const ( IDXBinaryPipe = ID(0x50) IDXBinaryHat = ID(0x51) IDXBinaryPercent = ID(0x52) - IDXBinaryNotEq = ID(0x53) - IDXBinaryLessThan = ID(0x54) - IDXBinaryLessEq = ID(0x55) - IDXBinaryEqEq = ID(0x56) - IDXBinaryGreaterEq = ID(0x57) - IDXBinaryGreaterThan = ID(0x58) - IDXBinaryAnd = ID(0x59) - IDXBinaryOr = ID(0x5A) - IDXBinaryAs = ID(0x5B) - IDXBinaryTildeModPlus = ID(0x5C) - IDXBinaryTildeModMinus = ID(0x5D) + IDXBinaryTildeModPlus = ID(0x53) + IDXBinaryTildeModMinus = ID(0x54) + IDXBinaryTildeSatPlus = ID(0x55) + IDXBinaryTildeSatMinus = ID(0x56) + IDXBinaryNotEq = ID(0x57) + IDXBinaryLessThan = ID(0x58) + IDXBinaryLessEq = ID(0x59) + IDXBinaryEqEq = ID(0x5A) + IDXBinaryGreaterEq = ID(0x5B) + IDXBinaryGreaterThan = ID(0x5C) + IDXBinaryAnd = ID(0x5D) + IDXBinaryOr = ID(0x5E) + IDXBinaryAs = ID(0x5F) IDXAssociativePlus = ID(0x60) IDXAssociativeStar = ID(0x61) @@ -444,6 +450,8 @@ var builtInsByID = [nBuiltInIDs]string{ IDPercentEq: "%=", IDTildeModPlusEq: "~mod+=", IDTildeModMinusEq: "~mod-=", + IDTildeSatPlusEq: "~sat+=", + IDTildeSatMinusEq: "~sat-=", IDPlus: "+", IDMinus: "-", @@ -458,6 +466,8 @@ var builtInsByID = [nBuiltInIDs]string{ IDPercent: "%", IDTildeModPlus: "~mod+", IDTildeModMinus: "~mod-", + IDTildeSatPlus: "~sat+", + IDTildeSatMinus: "~sat-", IDNotEq: "!=", IDLessThan: "<", @@ -684,6 +694,10 @@ var lexers = [nBuiltInIDs][]suffixLexer{ {"mod+", IDTildeModPlus}, {"mod-=", IDTildeModMinusEq}, {"mod-", IDTildeModMinus}, + {"sat+=", IDTildeSatPlusEq}, + {"sat+", IDTildeSatPlus}, + {"sat-=", IDTildeSatMinusEq}, + {"sat-", IDTildeSatMinus}, }, } @@ -705,6 +719,10 @@ var ambiguousForms = [nBuiltInIDs]ID{ IDXBinaryPipe: IDPipe, IDXBinaryHat: IDHat, IDXBinaryPercent: IDPercent, + IDXBinaryTildeModPlus: IDTildeModPlus, + IDXBinaryTildeModMinus: IDTildeModMinus, + IDXBinaryTildeSatPlus: IDTildeSatPlus, + IDXBinaryTildeSatMinus: IDTildeSatMinus, IDXBinaryNotEq: IDNotEq, IDXBinaryLessThan: IDLessThan, IDXBinaryLessEq: IDLessEq, @@ -714,8 +732,6 @@ var ambiguousForms = [nBuiltInIDs]ID{ IDXBinaryAnd: IDAnd, IDXBinaryOr: IDOr, IDXBinaryAs: IDAs, - IDXBinaryTildeModPlus: IDTildeModPlus, - IDXBinaryTildeModMinus: IDTildeModMinus, IDXAssociativePlus: IDPlus, IDXAssociativeStar: IDStar, @@ -774,6 +790,8 @@ var binaryForms = [nBuiltInIDs]ID{ IDPercentEq: IDXBinaryPercent, IDTildeModPlusEq: IDXBinaryTildeModPlus, IDTildeModMinusEq: IDXBinaryTildeModMinus, + IDTildeSatPlusEq: IDXBinaryTildeSatPlus, + IDTildeSatMinusEq: IDXBinaryTildeSatMinus, IDPlus: IDXBinaryPlus, IDMinus: IDXBinaryMinus, @@ -788,6 +806,8 @@ var binaryForms = [nBuiltInIDs]ID{ IDPercent: IDXBinaryPercent, IDTildeModPlus: IDXBinaryTildeModPlus, IDTildeModMinus: IDXBinaryTildeModMinus, + IDTildeSatPlus: IDXBinaryTildeSatPlus, + IDTildeSatMinus: IDXBinaryTildeSatMinus, IDNotEq: IDXBinaryNotEq, IDLessThan: IDXBinaryLessThan, @@ -806,7 +826,7 @@ var associativeForms = [nBuiltInIDs]ID{ IDAmp: IDXAssociativeAmp, IDPipe: IDXAssociativePipe, IDHat: IDXAssociativeHat, - // TODO: IDTildeModPlus, IDTildeModMinus? + // TODO: IDTildeModPlus, IDTildeSatPlus? IDAnd: IDXAssociativeAnd, IDOr: IDXAssociativeOr, }