mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 08:52:18 +01:00
Generate C code for "in.dst.write_u8?(x:foo)".
This commit is contained in:
1 parent
c1dff0ca41
commit
f5af981f15
7 files changed
+128
-12
No files matched your search
+59
-7
@@ -687,10 +687,10 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
|
|||||||
|
|
||||||
case a.KAssign:
|
case a.KAssign:
|
||||||
n := n.Assign()
|
n := n.Assign()
|
||||||
if err := g.writeSuspendibles(n.LHS()); err != nil {
|
if err := g.writeSuspendibles(n.LHS(), depth); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := g.writeSuspendibles(n.RHS()); err != nil {
|
if err := g.writeSuspendibles(n.RHS(), depth); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := g.writeExpr(n.LHS(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
|
if err := g.writeExpr(n.LHS(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
|
||||||
@@ -704,6 +704,16 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
|
|||||||
g.writes(";\n")
|
g.writes(";\n")
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
|
case a.KExpr:
|
||||||
|
n := n.Expr()
|
||||||
|
if err := g.writeSuspendibles(n, depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n.CallSuspendible() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("TODO: generate code for foo() when foo is not a ? call-suspendible")
|
||||||
|
|
||||||
case a.KIf:
|
case a.KIf:
|
||||||
// TODO: consider suspendible calls.
|
// TODO: consider suspendible calls.
|
||||||
n := n.If()
|
n := n.If()
|
||||||
@@ -814,27 +824,32 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
|
|||||||
return fmt.Errorf("unrecognized ast.Kind (%s) for writeStatement", n.Kind())
|
return fmt.Errorf("unrecognized ast.Kind (%s) for writeStatement", n.Kind())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *gen) writeSuspendibles(n *a.Expr) error {
|
func (g *gen) writeSuspendibles(n *a.Expr, depth uint32) error {
|
||||||
if !n.Suspendible() {
|
if !n.Suspendible() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return g.writeCallSuspendibles(n)
|
return g.writeCallSuspendibles(n, depth)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *gen) writeCallSuspendibles(n *a.Expr) error {
|
func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
|
||||||
// The evaluation order for suspendible calls (which can have side effects)
|
// The evaluation order for suspendible calls (which can have side effects)
|
||||||
// is important here: LHS, MHS, RHS, Args and finally the node itself.
|
// is important here: LHS, MHS, RHS, Args and finally the node itself.
|
||||||
if !n.CallSuspendible() {
|
if !n.CallSuspendible() {
|
||||||
|
if depth > a.MaxExprDepth {
|
||||||
|
return fmt.Errorf("expression recursion depth too large")
|
||||||
|
}
|
||||||
|
depth++
|
||||||
|
|
||||||
for _, o := range n.Node().Raw().SubNodes() {
|
for _, o := range n.Node().Raw().SubNodes() {
|
||||||
if o != nil && o.Kind() == a.KExpr {
|
if o != nil && o.Kind() == a.KExpr {
|
||||||
if err := g.writeCallSuspendibles(o.Expr()); err != nil {
|
if err := g.writeCallSuspendibles(o.Expr(), depth); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, o := range n.Args() {
|
for _, o := range n.Args() {
|
||||||
if o != nil && o.Kind() == a.KExpr {
|
if o != nil && o.Kind() == a.KExpr {
|
||||||
if err := g.writeCallSuspendibles(o.Expr()); err != nil {
|
if err := g.writeCallSuspendibles(o.Expr(), depth); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -866,6 +881,26 @@ func (g *gen) writeCallSuspendibles(n *a.Expr) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
g.printf(" %s%d = %ssrc->ptr[%ssrc->ri++];\n", tPrefix, temp, aPrefix, aPrefix)
|
g.printf(" %s%d = %ssrc->ptr[%ssrc->ri++];\n", tPrefix, temp, aPrefix, aPrefix)
|
||||||
|
|
||||||
|
// TODO: delete this hack that only matches "in.dst.write_u8?()".
|
||||||
|
} else if isInDstWriteU8(g.tm, n) {
|
||||||
|
// TODO: suspend coroutine state.
|
||||||
|
g.printf("if (%sdst->wi >= %sdst->len) { status = puffs_%s_status_short_write;",
|
||||||
|
aPrefix, aPrefix, g.pkgName)
|
||||||
|
if g.perFunc.public && g.perFunc.suspendible {
|
||||||
|
g.writes("goto cleanup0;")
|
||||||
|
} else {
|
||||||
|
g.writes("return status;")
|
||||||
|
}
|
||||||
|
g.writes("}\n")
|
||||||
|
g.printf("%sdst->ptr[%sdst->wi++] = ", aPrefix, aPrefix)
|
||||||
|
x := n.Args()[0].Arg().Value()
|
||||||
|
if err := g.writeExpr(x, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
g.writes(";\n")
|
||||||
|
return nil
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// TODO: fix this.
|
// TODO: fix this.
|
||||||
//
|
//
|
||||||
@@ -1000,6 +1035,23 @@ func isInSrcReadU8(tm *t.Map, n *a.Expr) bool {
|
|||||||
return n.ID0() == 0 && n.ID1().Key() == t.KeyIn
|
return n.ID0() == 0 && n.ID1().Key() == t.KeyIn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isInDstWriteU8(tm *t.Map, n *a.Expr) bool {
|
||||||
|
// TODO: check that n.Args() is "(x:bar)".
|
||||||
|
if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != 1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
n = n.LHS().Expr()
|
||||||
|
if n.ID0().Key() != t.KeyDot || n.ID1().Key() != t.KeyWriteU8 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
n = n.LHS().Expr()
|
||||||
|
if n.ID0().Key() != t.KeyDot || n.ID1() != tm.ByName("dst") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
n = n.LHS().Expr()
|
||||||
|
return n.ID0() == 0 && n.ID1().Key() == t.KeyIn
|
||||||
|
}
|
||||||
|
|
||||||
func isLowBits(tm *t.Map, n *a.Expr) bool {
|
func isLowBits(tm *t.Map, n *a.Expr) bool {
|
||||||
// TODO: check that n.Args() is "(n:bar)".
|
// TODO: check that n.Args() is "(n:bar)".
|
||||||
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
|
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
|
||||||
|
|||||||
@@ -246,6 +246,11 @@ 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) {
|
||||||
|
if (a_dst->wi >= a_dst->len) {
|
||||||
|
status = puffs_gif_status_short_write;
|
||||||
|
goto cleanup0;
|
||||||
|
}
|
||||||
|
a_dst->ptr[a_dst->wi++] = ((uint8_t)(v_code));
|
||||||
} 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;
|
||||||
|
|||||||
@@ -509,6 +509,7 @@ const MaxBodyDepth = 255
|
|||||||
// Statement means one of:
|
// Statement means one of:
|
||||||
// - Assert
|
// - Assert
|
||||||
// - Assign
|
// - Assign
|
||||||
|
// - Expr
|
||||||
// - If
|
// - If
|
||||||
// - Jump
|
// - Jump
|
||||||
// - Return
|
// - Return
|
||||||
|
|||||||
+18
-2
@@ -169,6 +169,10 @@ func (q *checker) bcheckStatement(n *a.Node) error {
|
|||||||
n := n.Assign()
|
n := n.Assign()
|
||||||
return q.bcheckAssignment(n.LHS(), n.Operator(), n.RHS())
|
return q.bcheckAssignment(n.LHS(), n.Operator(), n.RHS())
|
||||||
|
|
||||||
|
case a.KExpr:
|
||||||
|
_, _, err := q.bcheckExpr(n.Expr(), 0)
|
||||||
|
return err
|
||||||
|
|
||||||
case a.KIf:
|
case a.KIf:
|
||||||
return q.bcheckIf(n.If())
|
return q.bcheckIf(n.If())
|
||||||
|
|
||||||
@@ -589,8 +593,20 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
|||||||
// No-op.
|
// No-op.
|
||||||
|
|
||||||
case t.KeyOpenParen:
|
case t.KeyOpenParen:
|
||||||
// TODO: delete this hack that only matches "in.src.read_u8?()".
|
// TODO: delete this hack that only matches "in.src.read_u8?()" and
|
||||||
if isInSrcReadU8(q.tm, n) {
|
// "in.dst.write_u8?(x:bar)".
|
||||||
|
if isInSrcReadU8(q.tm, n) || isInDstWriteU8(q.tm, n) {
|
||||||
|
for _, o := range n.Args() {
|
||||||
|
// TODO: check that the arg range at the caller and the
|
||||||
|
// signature are compatible.
|
||||||
|
oMin, oMax, err := q.bcheckExpr(o.Arg().Value(), depth)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
// TODO: check that oMin and oMax is within the function's
|
||||||
|
// declared arg bounds.
|
||||||
|
_, _ = oMin, oMax
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// TODO: delete this hack that only matches "foo.low_bits(etc)".
|
// TODO: delete this hack that only matches "foo.low_bits(etc)".
|
||||||
|
|||||||
+28
-2
@@ -50,6 +50,9 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case a.KExpr:
|
||||||
|
return q.tcheckExpr(n.Expr(), 0)
|
||||||
|
|
||||||
case a.KIf:
|
case a.KIf:
|
||||||
for n := n.If(); n != nil; n = n.ElseIf() {
|
for n := n.If(); n != nil; n = n.ElseIf() {
|
||||||
cond := n.Condition()
|
cond := n.Condition()
|
||||||
@@ -313,11 +316,17 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
|||||||
|
|
||||||
case t.KeyOpenParen:
|
case t.KeyOpenParen:
|
||||||
// n is a function call.
|
// n is a function call.
|
||||||
// TODO: delete this hack that only matches "in.src.read_u8?()".
|
// TODO: delete this hack that only matches "in.src.read_u8?()" and
|
||||||
if isInSrcReadU8(q.tm, n) {
|
// "in.dst.write_u8?(x:bar)".
|
||||||
|
if isInSrcReadU8(q.tm, n) || isInDstWriteU8(q.tm, n) {
|
||||||
if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil {
|
if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
for _, o := range n.Args() {
|
||||||
|
if err := q.tcheckArg(o.Arg(), depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
n.SetMType(TypeExprU8) // HACK.
|
n.SetMType(TypeExprU8) // HACK.
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -368,6 +377,23 @@ func isInSrcReadU8(tm *t.Map, n *a.Expr) bool {
|
|||||||
return n.ID0() == 0 && n.ID1().Key() == t.KeyIn
|
return n.ID0() == 0 && n.ID1().Key() == t.KeyIn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isInDstWriteU8(tm *t.Map, n *a.Expr) bool {
|
||||||
|
// TODO: check that n.Args() is "(x:bar)".
|
||||||
|
if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != 1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
n = n.LHS().Expr()
|
||||||
|
if n.ID0().Key() != t.KeyDot || n.ID1().Key() != t.KeyWriteU8 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
n = n.LHS().Expr()
|
||||||
|
if n.ID0().Key() != t.KeyDot || n.ID1() != tm.ByName("dst") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
n = n.LHS().Expr()
|
||||||
|
return n.ID0() == 0 && n.ID1().Key() == t.KeyIn
|
||||||
|
}
|
||||||
|
|
||||||
func isLowBits(tm *t.Map, n *a.Expr) bool {
|
func isLowBits(tm *t.Map, n *a.Expr) bool {
|
||||||
// TODO: check that n.Args() is "(n:bar)".
|
// TODO: check that n.Args() is "(n:bar)".
|
||||||
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
|
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
|
|||||||
n_bits -= width
|
n_bits -= width
|
||||||
|
|
||||||
if code < clear_code {
|
if code < clear_code {
|
||||||
|
in.dst.write_u8?(x:code as u8)
|
||||||
// TODO: if use_save_code { etc }
|
// TODO: if use_save_code { etc }
|
||||||
|
|
||||||
} else if code == clear_code {
|
} else if code == clear_code {
|
||||||
|
|||||||
+16
-1
@@ -30,7 +30,7 @@ void test_lzw_decode() {
|
|||||||
}
|
}
|
||||||
// That .giflzw file should be 13382 bytes long.
|
// That .giflzw file should be 13382 bytes long.
|
||||||
if (src.wi != 13382) {
|
if (src.wi != 13382) {
|
||||||
FAIL("test_lzw_decode: file size: got %d, want %d", (int)(src.wi), 13382);
|
FAIL("test_lzw_decode: src size: got %d, want %d", (int)(src.wi), 13382);
|
||||||
goto cleanup0;
|
goto cleanup0;
|
||||||
}
|
}
|
||||||
// The first byte in that file, the LZW literal width, should be 0x08.
|
// The first byte in that file, the LZW literal width, should be 0x08.
|
||||||
@@ -51,6 +51,21 @@ void test_lzw_decode() {
|
|||||||
puffs_gif_status_ok);
|
puffs_gif_status_ok);
|
||||||
goto cleanup1;
|
goto cleanup1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The decoded per-pixel indexes should be 3982 bytes long.
|
||||||
|
//
|
||||||
|
// TODO: s/3982/19200/ as 19200 = 160 * 120.
|
||||||
|
if (dst.wi != 3982) {
|
||||||
|
FAIL("test_lzw_decode: dst size: got %d, want %d", (int)(dst.wi), 3982);
|
||||||
|
goto cleanup1;
|
||||||
|
}
|
||||||
|
// The first decoded byte should be 0xDC.
|
||||||
|
if (dst.ptr[0] != 0xDC) {
|
||||||
|
FAIL("test_lzw_decode: first decoded byte: got 0x%02x, want 0x%02x",
|
||||||
|
(int)(dst.ptr[0]), 0xDC);
|
||||||
|
goto cleanup1;
|
||||||
|
}
|
||||||
|
|
||||||
cleanup1:
|
cleanup1:
|
||||||
puffs_gif_lzw_decoder_destructor(&dec);
|
puffs_gif_lzw_decoder_destructor(&dec);
|
||||||
cleanup0:;
|
cleanup0:;
|
||||||
|
|||||||
Reference in new issue
Block a user