Allow scalar constants in type expressions

This commit is contained in:
Nigel Tao committed 2019-02-02 07:53:59 +11:00
1 parent 278200f560
commit 8bbdcc4ce1
7 files changed
+58 -18

No files matched your search

+17 -4
View File
@@ -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 ")
+3
View File
@@ -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)
+17 -2
View File
@@ -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,
+2
View File
@@ -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
+11 -8
View File
@@ -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
}
+4 -2
View File
@@ -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
+4 -2
View File
@@ -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 {