Unify multiple if-else branches' facts.

This commit is contained in:
Nigel Tao committed 2017-06-09 15:21:03 +10:00
1 parent ddbbdecce6
commit 711adf5273
3 files changed
+32 -27

No files matched your search

-7
View File
@@ -246,7 +246,6 @@ label_0_continue:;
v_bits >>= v_width;
v_n_bits -= v_width;
if (v_code < v_clear_code) {
goto label_0_break;
} else if (v_code == v_clear_code) {
v_use_save_code = false;
v_save_code = v_end_code;
@@ -256,8 +255,6 @@ label_0_continue:;
status = puffs_gif_status_ok;
goto cleanup0;
} else if (v_code <= v_save_code) {
status = puffs_gif_status_ok;
goto cleanup0;
} else {
status = puffs_gif_error_bad_gif_image;
goto cleanup0;
@@ -267,13 +264,9 @@ label_0_continue:;
v_save_code += 1;
if ((v_save_code == (((uint32_t)(1)) << v_width)) && (v_width < 12)) {
v_width += 1;
goto label_0_break;
}
goto label_0_break;
}
goto label_0_break;
}
label_0_break:;
cleanup0:
self->status = status;
+27 -15
View File
@@ -369,16 +369,33 @@ func snapshot(facts []*a.Expr) []*a.Expr {
return append([]*a.Expr(nil), facts...)
}
func unify(dst []*a.Expr, branches [][]*a.Expr) ([]*a.Expr, error) {
switch len(branches) {
case 0:
// No-op.
case 1:
dst = append(dst, branches[0]...)
default:
return nil, fmt.Errorf("TODO: unify multiple if branches")
func (q *checker) unify(branches [][]*a.Expr) error {
q.facts = q.facts[:0]
if len(branches) == 0 {
return nil
}
return dst, nil
q.facts = append(q.facts[:0], branches[0]...)
if len(branches) == 1 {
return nil
}
if len(branches) > 10000 {
return fmt.Errorf("too many if-else branches")
}
m := map[string]int{}
for _, b := range branches {
for _, f := range b {
m[f.String(q.tm)]++
}
}
q.facts.update(func(n *a.Expr) *a.Expr {
if m[n.String(q.tm)] == len(branches) {
return n
}
return nil
})
return nil
}
func (q *checker) bcheckIf(n *a.If) error {
@@ -427,12 +444,7 @@ func (q *checker) bcheckIf(n *a.If) error {
break
}
}
u, err := unify(q.facts[:0], branches)
if err != nil {
return err
}
q.facts = u
return nil
return q.unify(branches)
}
func (q *checker) bcheckWhile(n *a.While) error {
+5 -5
View File
@@ -44,16 +44,19 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
if code < clear_code {
// TODO: if use_save_code { etc }
break
} else if code == clear_code {
use_save_code = false
save_code = end_code
width = this.literal_width + 1
continue
} else if code == end_code {
return
} else if code <= save_code {
return
// TODO: implement.
} else {
return error "bad GIF image"
}
@@ -63,10 +66,7 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
save_code += 1
if (save_code == ((1 as u32) << width)) and (width < 12) {
width += 1
break // TODO: remove.
}
break // TODO: remove.
}
break // TODO: remove.
}
}