Generate C code for if-else-if.

Also remove redundant parentheses in "if ((x bin_op y))", to avoid
triggering a clang -Wparentheses-equality warning.
This commit is contained in:
Nigel Tao committed 2017-06-08 16:22:07 +10:00
1 parent a81752647c
commit 2d0f9d7669
4 files changed
+43 -18

No files matched your search

+32 -16
View File
@@ -79,6 +79,18 @@ const (
replaceCallSuspendibles = replacementPolicy(true)
)
// parenthesesPolicy controls whether to print the outer parentheses in an
// expression like "(x + y)". An "if" or "while" will print their own
// parentheses for "if (expr)" because they need to be able to say "if (x)".
// But a double-parenthesized expression like "if ((x == y))" is a clang
// warning (-Wparentheses-equality) and we like to compile with -Wall -Werror.
type parenthesesPolicy bool
const (
parenthesesMandatory = parenthesesPolicy(false)
parenthesesOptional = parenthesesPolicy(true)
)
type visibility uint32
const (
@@ -584,12 +596,12 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
if err := g.writeSuspendibles(n.RHS()); err != nil {
return err
}
if err := g.writeExpr(n.LHS(), replaceCallSuspendibles, depth); err != nil {
if err := g.writeExpr(n.LHS(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
return err
}
// TODO: does KeyAmpHatEq need special consideration?
g.writes(cOpNames[0xFF&n.Operator().Key()])
if err := g.writeExpr(n.RHS(), replaceCallSuspendibles, depth); err != nil {
if err := g.writeExpr(n.RHS(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
return err
}
g.writes(";\n")
@@ -600,7 +612,7 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
n := n.If()
for {
g.writes("if (")
if err := g.writeExpr(n.Condition(), replaceCallSuspendibles, 0); err != nil {
if err := g.writeExpr(n.Condition(), replaceCallSuspendibles, parenthesesOptional, 0); err != nil {
return err
}
g.writes(") {\n")
@@ -645,7 +657,7 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
// TODO: consider suspendible calls.
g.printf("%s%s = ", vPrefix, n.Name().String(g.tm))
if v := n.Value(); v != nil {
if err := g.writeExpr(v, replaceCallSuspendibles, 0); err != nil {
if err := g.writeExpr(v, replaceCallSuspendibles, parenthesesMandatory, 0); err != nil {
return err
}
} else {
@@ -666,7 +678,7 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
g.printf("label_%d_continue:;\n", jt)
}
g.writes("while (")
if err := g.writeExpr(n.Condition(), replaceCallSuspendibles, 0); err != nil {
if err := g.writeExpr(n.Condition(), replaceCallSuspendibles, parenthesesOptional, 0); err != nil {
return err
}
g.writes(") {\n")
@@ -750,7 +762,7 @@ func (g *gen) writeCallSuspendibles(n *a.Expr) error {
return nil
}
func (g *gen) writeExpr(n *a.Expr, rp replacementPolicy, depth uint32) error {
func (g *gen) writeExpr(n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
if depth > a.MaxExprDepth {
return fmt.Errorf("expression recursion depth too large")
}
@@ -786,7 +798,7 @@ func (g *gen) writeExpr(n *a.Expr, rp replacementPolicy, depth uint32) error {
return err
}
case t.FlagsBinaryOp:
if err := g.writeExprBinaryOp(n, rp, depth); err != nil {
if err := g.writeExprBinaryOp(n, rp, pp, depth); err != nil {
return err
}
case t.FlagsAssociativeOp:
@@ -817,11 +829,11 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro
// TODO: delete this hack that only matches "foo.low_bits(etc)".
if isLowBits(g.tm, n.LHS().Expr()) && !n.CallImpure() && len(n.Args()) == 1 {
g.printf("PUFFS_LOW_BITS(")
if err := g.writeExpr(n.LHS().Expr().LHS().Expr(), rp, depth); err != nil {
if err := g.writeExpr(n.LHS().Expr().LHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
return err
}
g.writes(",")
if err := g.writeExpr(n.Args()[0].Arg().Value(), rp, depth); err != nil {
if err := g.writeExpr(n.Args()[0].Arg().Value(), rp, parenthesesMandatory, depth); err != nil {
return err
}
g.writes(")")
@@ -838,7 +850,7 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro
// TODO.
case t.KeyDot:
if err := g.writeExpr(n.LHS().Expr(), rp, depth); err != nil {
if err := g.writeExpr(n.LHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
return err
}
// TODO: choose between . vs -> operators.
@@ -874,21 +886,25 @@ func (g *gen) writeExprUnaryOp(n *a.Expr, rp replacementPolicy, depth uint32) er
return nil
}
func (g *gen) writeExprBinaryOp(n *a.Expr, rp replacementPolicy, depth uint32) error {
func (g *gen) writeExprBinaryOp(n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
op := n.ID0()
if op.Key() == t.KeyXBinaryAs {
return g.writeExprAs(n.LHS().Expr(), n.RHS().TypeExpr(), rp, depth)
}
g.writeb('(')
if err := g.writeExpr(n.LHS().Expr(), rp, depth); err != nil {
if pp == parenthesesMandatory {
g.writeb('(')
}
if err := g.writeExpr(n.LHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
return err
}
// TODO: does KeyXBinaryAmpHat need special consideration?
g.writes(cOpNames[0xFF&op.Key()])
if err := g.writeExpr(n.RHS().Expr(), rp, depth); err != nil {
if err := g.writeExpr(n.RHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
return err
}
g.writeb(')')
if pp == parenthesesMandatory {
g.writeb(')')
}
return nil
}
@@ -898,7 +914,7 @@ func (g *gen) writeExprAs(lhs *a.Expr, rhs *a.TypeExpr, rp replacementPolicy, de
return err
}
g.writes(")(")
if err := g.writeExpr(lhs, rp, depth); err != nil {
if err := g.writeExpr(lhs, rp, parenthesesMandatory, depth); err != nil {
return err
}
g.writes("))")
+5 -2
View File
@@ -217,8 +217,9 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
v_n_bits = 0;
v_clear_code = (((uint32_t)(1)) << self->f_literal_width);
v_width = (self->f_literal_width + 1);
label_0_continue:;
while (1) {
while ((v_n_bits < v_width)) {
while (v_n_bits < v_width) {
if (a_src->ri >= a_src->wi) {
status = puffs_gif_status_short_read;
goto cleanup0;
@@ -230,8 +231,10 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
v_code = PUFFS_LOW_BITS(v_bits, v_width);
v_bits >>= v_width;
v_n_bits -= v_width;
if ((v_code < v_clear_code)) {
if (v_code < v_clear_code) {
goto label_0_break;
} else if (v_code == v_clear_code) {
goto label_0_continue;
}
goto label_0_break;
}
+4
View File
@@ -71,6 +71,10 @@ func (q *checker) tcheckStatement(n *a.Node) error {
}
}
}
for n := n.If(); n != nil; n = n.ElseIf() {
n.Node().SetTypeChecked()
}
return nil
case a.KJump:
n := n.Jump()
+2
View File
@@ -32,6 +32,8 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
if code < clear_code {
break
} else if code == clear_code {
continue
}
// TODO: implement.