Add a reader1.skip32 method.

This commit is contained in:
Nigel Tao committed 2017-06-22 13:09:53 +10:00
1 parent 07e96d7055
commit 1a42706bf1
6 files changed
+76 -62

No files matched your search

+38 -7
View File
@@ -956,7 +956,7 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
// TODO: delete these hacks that only matches "in.src.read_u8?()" etc.
//
// TODO: check reader1.buf and writer1.buf is non-NULL.
if isInSrcReadU8(g.tm, n) {
if isInSrc(g.tm, n, t.KeyReadU8, 0) {
if g.perFunc.tempW > maxTemp {
return fmt.Errorf("too many temporary variables required")
}
@@ -964,9 +964,9 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
g.perFunc.tempW++
// TODO: suspend coroutine state.
g.printf("if (%ssrc.buf->ri >= %ssrc.buf->wi) { status = "+
"%ssrc.buf->closed ? puffs_%s_error_unexpected_eof : puffs_%s_status_short_read;",
aPrefix, aPrefix, aPrefix, g.pkgName, g.pkgName)
g.printf("if (%ssrc.buf->ri >= %ssrc.buf->wi) {", aPrefix, aPrefix)
g.printf("status = %ssrc.buf->closed ? puffs_%s_error_unexpected_eof : puffs_%s_status_short_read;",
aPrefix, g.pkgName, g.pkgName)
if g.perFunc.public && g.perFunc.suspendible {
g.writes("goto cleanup0;")
} else {
@@ -980,6 +980,37 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
}
g.printf(" = %ssrc.buf->ptr[%ssrc.buf->ri++];\n", aPrefix, aPrefix)
} else if isInSrc(g.tm, n, t.KeySkip32, 1) {
if g.perFunc.tempW > maxTemp {
return fmt.Errorf("too many temporary variables required")
}
temp := g.perFunc.tempW
g.perFunc.tempW++
g.perFunc.tempR++
g.printf("size_t %s%d = ", tPrefix, temp)
x := n.Args()[0].Arg().Value()
if err := g.writeExpr(x, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
return err
}
g.writes(";\n")
g.printf("if (%s%d > %ssrc.buf->wi - %ssrc.buf->ri) {\n", tPrefix, temp, aPrefix, aPrefix)
// TODO: save tPrefix+temp as coroutine state, and suspend.
g.printf("%s%d -= %ssrc.buf->wi - %ssrc.buf->ri;\n", tPrefix, temp, aPrefix, aPrefix)
g.printf("%ssrc.buf->ri = %ssrc.buf->wi;\n", aPrefix, aPrefix)
g.printf("status = %ssrc.buf->closed ? puffs_%s_error_unexpected_eof : puffs_%s_status_short_read;",
aPrefix, g.pkgName, g.pkgName)
if g.perFunc.public && g.perFunc.suspendible {
g.writes("goto cleanup0;")
} else {
g.writes("return status;")
}
g.writes("}\n")
g.printf("%ssrc.buf->ri += %s%d;\n", aPrefix, tPrefix, temp)
} else if isInDst(g.tm, n, t.KeyWrite) {
// TODO: suspend coroutine state.
//
@@ -1175,12 +1206,12 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro
return fmt.Errorf("unrecognized token.Key (0x%X) for writeExprOther", n.ID0().Key())
}
func isInSrcReadU8(tm *t.Map, n *a.Expr) bool {
if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != 0 {
func isInSrc(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != nArgs {
return false
}
n = n.LHS().Expr()
if n.ID0().Key() != t.KeyDot || n.ID1().Key() != t.KeyReadU8 {
if n.ID0().Key() != t.KeyDot || n.ID1().Key() != methodName {
return false
}
n = n.LHS().Expr()
+18 -28
View File
@@ -453,9 +453,7 @@ puffs_gif_status puffs_gif_decoder_decode_extension(puffs_gif_decoder* self,
puffs_gif_status status = self->private_impl.status;
uint8_t v_label;
uint32_t v_block_size;
uint32_t v_j;
uint8_t v_temp;
uint8_t v_block_size;
if (a_src.buf->ri >= a_src.buf->wi) {
status = a_src.buf->closed ? puffs_gif_error_unexpected_eof
@@ -482,18 +480,15 @@ puffs_gif_status puffs_gif_decoder_decode_extension(puffs_gif_decoder* self,
if (v_block_size == 0) {
goto label_0_break;
}
v_j = 0;
while (v_j < v_block_size) {
if (a_src.buf->ri >= a_src.buf->wi) {
status = a_src.buf->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
return status;
}
uint8_t t_2 = a_src.buf->ptr[a_src.buf->ri++];
v_temp = t_2;
v_temp *= 0;
v_j += 1;
size_t t_2 = ((uint32_t)(v_block_size));
if (t_2 > a_src.buf->wi - a_src.buf->ri) {
t_2 -= a_src.buf->wi - a_src.buf->ri;
a_src.buf->ri = a_src.buf->wi;
status = a_src.buf->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
return status;
}
a_src.buf->ri += t_2;
}
label_0_break:;
@@ -508,9 +503,7 @@ puffs_gif_status puffs_gif_decoder_decode_id(puffs_gif_decoder* self,
uint32_t v_i;
bool v_interlace;
uint8_t v_literal_width;
uint32_t v_block_size;
uint32_t v_j;
uint8_t v_temp;
uint8_t v_block_size;
for (size_t i = 0; i < 9; i++) {
v_c[i] = 0;
@@ -551,18 +544,15 @@ puffs_gif_status puffs_gif_decoder_decode_id(puffs_gif_decoder* self,
if (v_block_size == 0) {
goto label_0_break;
}
v_j = 0;
while (v_j < v_block_size) {
if (a_src.buf->ri >= a_src.buf->wi) {
status = a_src.buf->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
return status;
}
uint8_t t_3 = a_src.buf->ptr[a_src.buf->ri++];
v_temp = t_3;
v_temp *= 0;
v_j += 1;
size_t t_3 = ((uint32_t)(v_block_size));
if (t_3 > a_src.buf->wi - a_src.buf->ri) {
t_3 -= a_src.buf->wi - a_src.buf->ri;
a_src.buf->ri = a_src.buf->wi;
status = a_src.buf->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
return status;
}
a_src.buf->ri += t_3;
}
label_0_break:;
+2 -1
View File
@@ -599,7 +599,8 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
case t.KeyOpenParen:
// TODO: delete this hack that only matches "in.src.read_u8?()" etc.
if isInSrcReadU8(q.tm, n) || isInDst(q.tm, n, t.KeyWrite) || isInDst(q.tm, n, t.KeyWriteU8) ||
if isInSrc(q.tm, n, t.KeyReadU8, 0) || isInSrc(q.tm, n, t.KeySkip32, 1) ||
isInDst(q.tm, n, t.KeyWrite) || isInDst(q.tm, n, t.KeyWriteU8) ||
isThisMethod(q.tm, n, "decode_header") || isThisMethod(q.tm, n, "decode_lsd") ||
isThisMethod(q.tm, n, "decode_extension") || isThisMethod(q.tm, n, "decode_id") {
+5 -4
View File
@@ -327,7 +327,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
case t.KeyOpenParen:
// n is a function call.
// TODO: delete this hack that only matches "in.src.read_u8?()" etc.
if isInSrcReadU8(q.tm, n) || isInDst(q.tm, n, t.KeyWrite) || isInDst(q.tm, n, t.KeyWriteU8) ||
if isInSrc(q.tm, n, t.KeyReadU8, 0) || isInSrc(q.tm, n, t.KeySkip32, 1) ||
isInDst(q.tm, n, t.KeyWrite) || isInDst(q.tm, n, t.KeyWriteU8) ||
isThisMethod(q.tm, n, "decode_header") || isThisMethod(q.tm, n, "decode_lsd") ||
isThisMethod(q.tm, n, "decode_extension") || isThisMethod(q.tm, n, "decode_id") {
@@ -392,12 +393,12 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return fmt.Errorf("check: unrecognized token.Key (0x%X) for tcheckExprOther", n.ID0().Key())
}
func isInSrcReadU8(tm *t.Map, n *a.Expr) bool {
if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != 0 {
func isInSrc(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != nArgs {
return false
}
n = n.LHS().Expr()
if n.ID0().Key() != t.KeyDot || n.ID1().Key() != t.KeyReadU8 {
if n.ID0().Key() != t.KeyDot || n.ID1().Key() != methodName {
return false
}
n = n.LHS().Expr()
+9
View File
@@ -252,6 +252,9 @@ const (
KeyWriteU64BE = Key(IDWriteU64BE >> KeyShift)
KeyWriteU64LE = Key(IDWriteU64LE >> KeyShift)
KeySkip32 = Key(IDSkip32 >> KeyShift)
KeySkip64 = Key(IDSkip64 >> KeyShift)
KeyXUnaryPlus = Key(IDXUnaryPlus >> KeyShift)
KeyXUnaryMinus = Key(IDXUnaryMinus >> KeyShift)
KeyXUnaryNot = Key(IDXUnaryNot >> KeyShift)
@@ -405,6 +408,9 @@ const (
IDWriteU32LE = ID(0xBD<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDWriteU64BE = ID(0xBE<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDWriteU64LE = ID(0xBF<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDSkip32 = ID(0xC0<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDSkip64 = ID(0xC1<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
)
// The IDXFoo IDs are not returned by the tokenizer. They are used by the
@@ -581,6 +587,9 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyWriteU32LE: {"write_u32le", IDWriteU32LE},
KeyWriteU64BE: {"write_u64be", IDWriteU64BE},
KeyWriteU64LE: {"write_u64le", IDWriteU64LE},
KeySkip32: {"skip32", IDSkip32},
KeySkip64: {"skip64", IDSkip64},
}
var builtInsByName = map[string]ID{}
+4 -22
View File
@@ -104,20 +104,11 @@ pri func decoder.decode_extension?(src reader1)() {
// Skip the data blocks.
while true {
var block_size u32[..255] = in.src.read_u8?() as u32
var block_size u8 = in.src.read_u8?() as u32
if block_size == 0 {
break
}
var j u32
while j < block_size {
assert j < 255 via "a < b: a < c; c <= b"(c:block_size)
// TODO: we shouldn't need a temp variable.
//
// TODO: have an in.src.skip?(n:block_size) method?
var temp u8 = in.src.read_u8?()
temp *= 0 // Avoid "unused variable" C compiler warning.
j += 1
}
in.src.skip32?(n:block_size as u32)
}
}
@@ -151,19 +142,10 @@ pri func decoder.decode_id?(src reader1)() {
// TODO: pass the data blocks' payload to this.lzw.
while true {
var block_size u32[..255] = in.src.read_u8?() as u32
var block_size u8 = in.src.read_u8?() as u32
if block_size == 0 {
break
}
var j u32
while j < block_size {
assert j < 255 via "a < b: a < c; c <= b"(c:block_size)
// TODO: we shouldn't need a temp variable.
//
// TODO: have an in.src.skip?(n:block_size) method?
var temp u8 = in.src.read_u8?()
temp *= 0 // Avoid "unused variable" C compiler warning.
j += 1
}
in.src.skip32?(n:block_size as u32)
}
}