From 8bbdcc4ce1ef9e67e3648ebb853dbe44d30bb6d7 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 2 Feb 2019 07:53:59 +1100 Subject: [PATCH] Allow scalar constants in type expressions --- cmd/wuffs-c/internal/cgen/cgen.go | 21 +++++++++++++++++---- cmd/wuffs-c/internal/cgen/expr.go | 3 +++ lang/check/type.go | 19 +++++++++++++++++-- release/c/wuffs-unsupported-snapshot.c | 2 ++ std/deflate/decode_deflate.wuffs | 19 +++++++++++-------- std/deflate/decode_huffman_fast.wuffs | 6 ++++-- std/deflate/decode_huffman_slow.wuffs | 6 ++++-- 7 files changed, 58 insertions(+), 18 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/cgen.go b/cmd/wuffs-c/internal/cgen/cgen.go index 1381f23e..f966a1c2 100644 --- a/cmd/wuffs-c/internal/cgen/cgen.go +++ b/cmd/wuffs-c/internal/cgen/cgen.go @@ -318,10 +318,11 @@ type gen struct { // generated C code (due to line numbers changing) when editing Wuffs code. genlinenum bool - statusList []status - statusMap map[t.QID]status - structList []*a.Struct - structMap map[t.QID]*a.Struct + scalarConstsMap map[t.QID]*a.Const + statusList []status + statusMap map[t.QID]status + structList []*a.Struct + structMap map[t.QID]*a.Struct currFunk funk funks map[t.QQID]funk @@ -350,6 +351,11 @@ func (g *gen) generate() ([]byte, error) { } } + g.scalarConstsMap = map[t.QID]*a.Const{} + if err := g.forEachConst(b, bothPubPri, (*gen).gatherScalarConsts); err != nil { + return nil, err + } + // Make a topologically sorted list of structs. unsortedStructs := []*a.Struct(nil) for _, file := range g.files { @@ -667,6 +673,13 @@ func (g *gen) addStatus(qid t.QID, msg string, public bool) error { return nil } +func (g *gen) gatherScalarConsts(b *buffer, n *a.Const) error { + if cv := n.Value().ConstValue(); cv != nil { + g.scalarConstsMap[n.QID()] = n + } + return nil +} + func (g *gen) writeConst(b *buffer, n *a.Const) error { if n.Public() { b.writes("WUFFS_BASE__MAYBE_STATIC ") diff --git a/cmd/wuffs-c/internal/cgen/expr.go b/cmd/wuffs-c/internal/cgen/expr.go index a71ca4b8..a43586cb 100644 --- a/cmd/wuffs-c/internal/cgen/expr.go +++ b/cmd/wuffs-c/internal/cgen/expr.go @@ -75,6 +75,9 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, depth uint32) error { return fmt.Errorf("unrecognized status %s", n.StatusQID().Str(g.tm)) } + } else if c, ok := g.scalarConstsMap[t.QID{0, n.Ident()}]; ok { + b.writes(c.Value().ConstValue().String()) + } else { if n.GlobalIdent() { b.writes(g.pkgPrefix) diff --git a/lang/check/type.go b/lang/check/type.go index f76ac6af..eedc54ec 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -932,7 +932,7 @@ swtch: if err := q.tcheckExpr(b, 0); err != nil { return err } - if b.ConstValue() == nil { + if q.exprConstValue(b) == nil { return fmt.Errorf("check: %q is not constant", b.Str(q.tm)) } } @@ -958,7 +958,7 @@ swtch: if err := q.tcheckExpr(aLen, 0); err != nil { return err } - if aLen.ConstValue() == nil { + if q.exprConstValue(aLen) == nil { return fmt.Errorf("check: %q is not constant", aLen.Str(q.tm)) } fallthrough @@ -975,6 +975,21 @@ swtch: return nil } +func (q *checker) exprConstValue(n *a.Expr) *big.Int { + if cv := n.ConstValue(); cv != nil { + return cv + } + if n.Operator() == 0 { + if c, ok := q.c.consts[t.QID{0, n.Ident()}]; ok { + if cv := c.Value().ConstValue(); cv != nil { + n.SetConstValue(cv) + return cv + } + } + } + return nil +} + var comparisonOps = [...]bool{ t.IDXBinaryNotEq: true, t.IDXBinaryLessThan: true, diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index f52ed587..56d8dcab 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -5436,6 +5436,8 @@ static const uint32_t wuffs_deflate__dcode_magic_numbers[32] = { 134217728, 134217728, }; +static const uint32_t wuffs_deflate__huffs_table_size = 1234; + // ---------------- Private Initializer Prototypes // ---------------- Private Function Prototypes diff --git a/std/deflate/decode_deflate.wuffs b/std/deflate/decode_deflate.wuffs index 4244b849..d9ef6712 100644 --- a/std/deflate/decode_deflate.wuffs +++ b/std/deflate/decode_deflate.wuffs @@ -53,6 +53,10 @@ pri const dcode_magic_numbers array[32] base.u32 = [ 0x401000B0, 0x401800B0, 0x402000C0, 0x403000C0, 0x404000D0, 0x406000D0, 0x08000000, 0x08000000, ] +// TODO: replace the magic "big enough" 1234 with something more principled, +// perhaps discovered via an exhaustive search. +pri const huffs_table_size base.u32 = 1234 + pub struct decoder?( // These fields yield src's bits in Least Significant Bits order. bits base.u32, @@ -88,10 +92,7 @@ pub struct decoder?( // - bits 3 .. 0 are the number of decoder.bits to consume. // // Exactly one of the eight bits 31-24 should be set. - // - // TODO: replace the magic "big enough" 1234 with something more - // principled, perhaps discovered via an exhaustive search. - huffs array[2] array[1234] base.u32, + huffs array[2] array[huffs_table_size] base.u32, n_huffs_bits array[2] base.u32[..9], // history holds up to the last 32KiB of decoded output, if the decoding @@ -495,8 +496,8 @@ pri func decoder.init_huff!(which base.u32[..1], n_codes0 base.u32[..320], n_cod var initial_high_bits base.u32 var prev_cl base.u32[..15] var prev_redirect_key base.u32 - var top base.u32[..1234] - var next_top base.u32[..1234] + var top base.u32[..huffs_table_size] + var next_top base.u32[..huffs_table_size] var code base.u32 var key base.u32 var value base.u32 @@ -767,9 +768,10 @@ pri func decoder.init_huff!(which base.u32[..1], n_codes0 base.u32[..320], n_cod initial_high_bits = (1 as base.u32) << j top = next_top - if (top + ((1 as base.u32) << j)) > 1234 { + if (top + ((1 as base.u32) << j)) > huffs_table_size { return "?internal error: inconsistent Huffman decoder state" } + assert (top + ((1 as base.u32) << j)) <= 1234 via "a <= b: a <= c; c <= b"(c:huffs_table_size) next_top = top + ((1 as base.u32) << j) redirect_key = (reverse8[redirect_key >> 1] as base.u32) | ((redirect_key & 1) << 8) @@ -814,9 +816,10 @@ pri func decoder.init_huff!(which base.u32[..1], n_codes0 base.u32[..320], n_cod inv n_symbols <= 288, { high_bits -= delta - if (top + ((high_bits | reversed_key) & 511)) >= 1234 { + if (top + ((high_bits | reversed_key) & 511)) >= huffs_table_size { return "?internal error: inconsistent Huffman decoder state" } + assert (top + ((high_bits | reversed_key) & 511)) < 1234 via "a < b: a < c; c <= b"(c:huffs_table_size) this.huffs[args.which][top + ((high_bits | reversed_key) & 511)] = value } diff --git a/std/deflate/decode_huffman_fast.wuffs b/std/deflate/decode_huffman_fast.wuffs index e9ee4d08..d8e22b15 100644 --- a/std/deflate/decode_huffman_fast.wuffs +++ b/std/deflate/decode_huffman_fast.wuffs @@ -130,9 +130,10 @@ pri func decoder.decode_huffman_fast!(dst base.io_writer, src base.io_reader) ba redir_top = (table_entry >> 8) & 0xFFFF redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1 - if (redir_top + (bits & redir_mask)) >= 1234 { + if (redir_top + (bits & redir_mask)) >= huffs_table_size { return "?internal error: inconsistent Huffman decoder state" } + assert (redir_top + (bits & redir_mask)) < 1234 via "a < b: a < c; c <= b"(c:huffs_table_size) table_entry = this.huffs[0][redir_top + (bits & redir_mask)] table_entry_n_bits = table_entry & 0x0F bits >>= table_entry_n_bits @@ -243,9 +244,10 @@ pri func decoder.decode_huffman_fast!(dst base.io_writer, src base.io_reader) ba redir_top = (table_entry >> 8) & 0xFFFF redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1 - if (redir_top + (bits & redir_mask)) >= 1234 { + if (redir_top + (bits & redir_mask)) >= huffs_table_size { return "?internal error: inconsistent Huffman decoder state" } + assert (redir_top + (bits & redir_mask)) < 1234 via "a < b: a < c; c <= b"(c:huffs_table_size) table_entry = this.huffs[1][redir_top + (bits & redir_mask)] table_entry_n_bits = table_entry & 0x0F bits >>= table_entry_n_bits diff --git a/std/deflate/decode_huffman_slow.wuffs b/std/deflate/decode_huffman_slow.wuffs index fd3fcc5a..07c4ebe6 100644 --- a/std/deflate/decode_huffman_slow.wuffs +++ b/std/deflate/decode_huffman_slow.wuffs @@ -79,9 +79,10 @@ pri func decoder.decode_huffman_slow?(dst base.io_writer, src base.io_reader) { redir_top = (table_entry >> 8) & 0xFFFF redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1 while true { - if (redir_top + (bits & redir_mask)) >= 1234 { + if (redir_top + (bits & redir_mask)) >= huffs_table_size { return "?internal error: inconsistent Huffman decoder state" } + assert (redir_top + (bits & redir_mask)) < 1234 via "a < b: a < c; c <= b"(c:huffs_table_size) table_entry = this.huffs[0][redir_top + (bits & redir_mask)] table_entry_n_bits = table_entry & 0x0F if n_bits >= table_entry_n_bits { @@ -160,9 +161,10 @@ pri func decoder.decode_huffman_slow?(dst base.io_writer, src base.io_reader) { redir_top = (table_entry >> 8) & 0xFFFF redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1 while true { - if (redir_top + (bits & redir_mask)) >= 1234 { + if (redir_top + (bits & redir_mask)) >= huffs_table_size { return "?internal error: inconsistent Huffman decoder state" } + assert (redir_top + (bits & redir_mask)) < 1234 via "a < b: a < c; c <= b"(c:huffs_table_size) table_entry = this.huffs[1][redir_top + (bits & redir_mask)] table_entry_n_bits = table_entry & 0x0F if n_bits >= table_entry_n_bits {