Generate C code for "self.foo?()".

This commit is contained in:
Nigel Tao committed 2017-06-16 15:38:40 +10:00
1 parent e6f5fd7f53
commit cd526ccafe
5 files changed
+113 -55

No files matched your search

+20 -1
View File
@@ -125,6 +125,7 @@ type status struct {
}
type perFunc struct {
funk *a.Func
jumpTargets map[*a.While]uint32
tempW uint32
tempR uint32
@@ -552,7 +553,7 @@ func (g *gen) writeFuncPrototype(n *a.Func) error {
}
func (g *gen) writeFuncImpl(n *a.Func) error {
g.perFunc = perFunc{}
g.perFunc = perFunc{funk: n}
if err := g.writeFuncSignature(n); err != nil {
return err
}
@@ -1032,6 +1033,11 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
}
g.writes(";\n")
} else if isThisDecodeHeader(g.tm, n) {
g.printf("status = puffs_%s_%s_decode_header(self, %ssrc);\n",
g.pkgName, g.perFunc.funk.Receiver().String(g.tm), aPrefix)
g.writes("if (status) { goto cleanup0; }\n")
} else {
// TODO: fix this.
//
@@ -1197,6 +1203,19 @@ func isInDst(tm *t.Map, n *a.Expr, methodName t.Key) bool {
return n.ID0() == 0 && n.ID1().Key() == t.KeyIn
}
func isThisDecodeHeader(tm *t.Map, n *a.Expr) bool {
// TODO: check that n.Args() is "(src:in.src)".
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() != tm.ByName("decode_header") {
return false
}
n = n.LHS().Expr()
return n.ID0() == 0 && n.ID1().Key() == t.KeyThis
}
func isLowBits(tm *t.Map, n *a.Expr) bool {
// TODO: check that n.Args() is "(n:bar)".
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
+61 -49
View File
@@ -200,6 +200,9 @@ const char* puffs_gif_status_string(puffs_gif_status s) {
// ---------------- Private Function Prototypes
puffs_gif_status puffs_gif_decoder_decode_header(puffs_gif_decoder* self,
puffs_base_buf1* a_src);
// ---------------- Constructor and Destructor Implementations
// PUFFS_MAGIC is a magic number to check that constructors are called. It's
@@ -285,55 +288,8 @@ puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self,
goto cleanup0;
}
uint8_t v_c;
if (a_src->ri >= a_src->wi) {
status = a_src->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
goto cleanup0;
}
uint8_t t_0 = a_src->ptr[a_src->ri++];
if (a_src->ri >= a_src->wi) {
status = a_src->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
goto cleanup0;
}
uint8_t t_1 = a_src->ptr[a_src->ri++];
if (a_src->ri >= a_src->wi) {
status = a_src->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
goto cleanup0;
}
uint8_t t_2 = a_src->ptr[a_src->ri++];
if (a_src->ri >= a_src->wi) {
status = a_src->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
goto cleanup0;
}
uint8_t t_3 = a_src->ptr[a_src->ri++];
if ((t_0 != 71) || (t_1 != 73) || (t_2 != 70) || (t_3 != 56)) {
status = puffs_gif_error_bad_gif_header;
goto cleanup0;
}
if (a_src->ri >= a_src->wi) {
status = a_src->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
goto cleanup0;
}
uint8_t t_4 = a_src->ptr[a_src->ri++];
v_c = t_4;
if ((v_c != 55) && (v_c != 57)) {
status = puffs_gif_error_bad_gif_header;
goto cleanup0;
}
if (a_src->ri >= a_src->wi) {
status = a_src->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
goto cleanup0;
}
uint8_t t_5 = a_src->ptr[a_src->ri++];
if (t_5 != 97) {
status = puffs_gif_error_bad_gif_header;
status = puffs_gif_decoder_decode_header(self, a_src);
if (status) {
goto cleanup0;
}
@@ -342,6 +298,62 @@ cleanup0:
return status;
}
puffs_gif_status puffs_gif_decoder_decode_header(puffs_gif_decoder* self,
puffs_base_buf1* a_src) {
puffs_gif_status status = self->private_impl.status;
uint8_t v_c;
if (a_src->ri >= a_src->wi) {
status = a_src->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
return status;
}
uint8_t t_0 = a_src->ptr[a_src->ri++];
if (a_src->ri >= a_src->wi) {
status = a_src->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
return status;
}
uint8_t t_1 = a_src->ptr[a_src->ri++];
if (a_src->ri >= a_src->wi) {
status = a_src->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
return status;
}
uint8_t t_2 = a_src->ptr[a_src->ri++];
if (a_src->ri >= a_src->wi) {
status = a_src->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
return status;
}
uint8_t t_3 = a_src->ptr[a_src->ri++];
if ((t_0 != 71) || (t_1 != 73) || (t_2 != 70) || (t_3 != 56)) {
return puffs_gif_error_bad_gif_header;
}
if (a_src->ri >= a_src->wi) {
status = a_src->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
return status;
}
uint8_t t_4 = a_src->ptr[a_src->ri++];
v_c = t_4;
if ((v_c != 55) && (v_c != 57)) {
return puffs_gif_error_bad_gif_header;
}
if (a_src->ri >= a_src->wi) {
status = a_src->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
return status;
}
uint8_t t_5 = a_src->ptr[a_src->ri++];
if (t_5 != 97) {
return puffs_gif_error_bad_gif_header;
}
return status;
}
void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self,
uint32_t a_lw) {
if (!self) {
+3 -1
View File
@@ -599,7 +599,9 @@ 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 isInSrcReadU8(q.tm, n) || isInDst(q.tm, n, t.KeyWrite) || isInDst(q.tm, n, t.KeyWriteU8) ||
isThisDecodeHeader(q.tm, n) {
for _, o := range n.Args() {
// TODO: check that the arg range at the caller and the
// signature are compatible.
+24 -3
View File
@@ -327,7 +327,9 @@ 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 isInSrcReadU8(q.tm, n) || isInDst(q.tm, n, t.KeyWrite) || isInDst(q.tm, n, t.KeyWriteU8) ||
isThisDecodeHeader(q.tm, n) {
if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil {
return err
}
@@ -422,6 +424,19 @@ func isInDst(tm *t.Map, n *a.Expr, methodName t.Key) bool {
return n.ID0() == 0 && n.ID1().Key() == t.KeyIn
}
func isThisDecodeHeader(tm *t.Map, n *a.Expr) bool {
// TODO: check that n.Args() is "(src:in.src)".
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() != tm.ByName("decode_header") {
return false
}
n = n.LHS().Expr()
return n.ID0() == 0 && n.ID1().Key() == t.KeyThis
}
func isLowBits(tm *t.Map, n *a.Expr) bool {
// TODO: check that n.Args() is "(n:bar)".
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
@@ -442,7 +457,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
if lTyp.Decorator() != 0 {
// TODO.
return fmt.Errorf("check: unsupported package-or-decorator for tcheckDot")
return fmt.Errorf("check: unsupported decorator for tcheckDot")
}
s := (*a.Struct)(nil)
@@ -476,7 +491,13 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
return nil
}
}
return fmt.Errorf("check: no field named %q found in struct type %q for expression %q",
if f := q.c.funcs[t.QID{lTyp.Name(), n.ID1()}]; f.Func != nil {
n.SetMType(typeExprPlaceholder) // HACK.
return nil
}
return fmt.Errorf("check: no field or method named %q found in struct type %q for expression %q",
n.ID1().String(q.tm), lTyp.Name().String(q.tm), n.String(q.tm))
}
+5 -1
View File
@@ -10,7 +10,11 @@ pub struct decoder?(
)
pub func decoder.decode?(dst ptr buf1, src ptr buf1)() {
// Read either "GIF87a" or "GIF89a".
this.decode_header?(src:in.src)
}
// decode_header reads either "GIF87a" or "GIF89a".
pri func decoder.decode_header?(src ptr buf1)() {
if (in.src.read_u8?() != 0x47) or (in.src.read_u8?() != 0x49) or
(in.src.read_u8?() != 0x46) or (in.src.read_u8?() != 0x38) {
return error "bad GIF header"