mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 16:55:11 +01:00
Unify multiple if-else branches' facts.
This commit is contained in:
1 parent
ddbbdecce6
commit
711adf5273
3 files changed
+32
-27
No files matched your search
@@ -246,7 +246,6 @@ label_0_continue:;
|
|||||||
v_bits >>= v_width;
|
v_bits >>= v_width;
|
||||||
v_n_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) {
|
} else if (v_code == v_clear_code) {
|
||||||
v_use_save_code = false;
|
v_use_save_code = false;
|
||||||
v_save_code = v_end_code;
|
v_save_code = v_end_code;
|
||||||
@@ -256,8 +255,6 @@ label_0_continue:;
|
|||||||
status = puffs_gif_status_ok;
|
status = puffs_gif_status_ok;
|
||||||
goto cleanup0;
|
goto cleanup0;
|
||||||
} else if (v_code <= v_save_code) {
|
} else if (v_code <= v_save_code) {
|
||||||
status = puffs_gif_status_ok;
|
|
||||||
goto cleanup0;
|
|
||||||
} else {
|
} else {
|
||||||
status = puffs_gif_error_bad_gif_image;
|
status = puffs_gif_error_bad_gif_image;
|
||||||
goto cleanup0;
|
goto cleanup0;
|
||||||
@@ -267,13 +264,9 @@ label_0_continue:;
|
|||||||
v_save_code += 1;
|
v_save_code += 1;
|
||||||
if ((v_save_code == (((uint32_t)(1)) << v_width)) && (v_width < 12)) {
|
if ((v_save_code == (((uint32_t)(1)) << v_width)) && (v_width < 12)) {
|
||||||
v_width += 1;
|
v_width += 1;
|
||||||
goto label_0_break;
|
|
||||||
}
|
}
|
||||||
goto label_0_break;
|
|
||||||
}
|
}
|
||||||
goto label_0_break;
|
|
||||||
}
|
}
|
||||||
label_0_break:;
|
|
||||||
|
|
||||||
cleanup0:
|
cleanup0:
|
||||||
self->status = status;
|
self->status = status;
|
||||||
|
|||||||
+27
-15
@@ -369,16 +369,33 @@ func snapshot(facts []*a.Expr) []*a.Expr {
|
|||||||
return append([]*a.Expr(nil), facts...)
|
return append([]*a.Expr(nil), facts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func unify(dst []*a.Expr, branches [][]*a.Expr) ([]*a.Expr, error) {
|
func (q *checker) unify(branches [][]*a.Expr) error {
|
||||||
switch len(branches) {
|
q.facts = q.facts[:0]
|
||||||
case 0:
|
if len(branches) == 0 {
|
||||||
// No-op.
|
return nil
|
||||||
case 1:
|
|
||||||
dst = append(dst, branches[0]...)
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("TODO: unify multiple if branches")
|
|
||||||
}
|
}
|
||||||
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 {
|
func (q *checker) bcheckIf(n *a.If) error {
|
||||||
@@ -427,12 +444,7 @@ func (q *checker) bcheckIf(n *a.If) error {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
u, err := unify(q.facts[:0], branches)
|
return q.unify(branches)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
q.facts = u
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *checker) bcheckWhile(n *a.While) error {
|
func (q *checker) bcheckWhile(n *a.While) error {
|
||||||
|
|||||||
@@ -44,16 +44,19 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
|
|||||||
|
|
||||||
if code < clear_code {
|
if code < clear_code {
|
||||||
// TODO: if use_save_code { etc }
|
// TODO: if use_save_code { etc }
|
||||||
break
|
|
||||||
} else if code == clear_code {
|
} else if code == clear_code {
|
||||||
use_save_code = false
|
use_save_code = false
|
||||||
save_code = end_code
|
save_code = end_code
|
||||||
width = this.literal_width + 1
|
width = this.literal_width + 1
|
||||||
continue
|
continue
|
||||||
|
|
||||||
} else if code == end_code {
|
} else if code == end_code {
|
||||||
return
|
return
|
||||||
|
|
||||||
} else if code <= save_code {
|
} else if code <= save_code {
|
||||||
return
|
// TODO: implement.
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return error "bad GIF image"
|
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
|
save_code += 1
|
||||||
if (save_code == ((1 as u32) << width)) and (width < 12) {
|
if (save_code == ((1 as u32) << width)) and (width < 12) {
|
||||||
width += 1
|
width += 1
|
||||||
break // TODO: remove.
|
|
||||||
}
|
}
|
||||||
break // TODO: remove.
|
|
||||||
}
|
}
|
||||||
break // TODO: remove.
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user