Don't resume/suspend vars containing pointers.

This commit is contained in:
Nigel Tao committed 2017-09-23 15:16:43 +10:00
1 parent d5c8e20268
commit 10b7d5a031
7 files changed
+78 -38

No files matched your search

+1 -1
View File
@@ -566,7 +566,7 @@ func (g *gen) writeStruct(b *buffer, n *a.Struct) error {
b.writes("struct {\n")
if k.coroSuspPoint != 0 {
b.writes("uint32_t coro_susp_point;\n")
if err := g.writeVars(b, o.Body()); err != nil {
if err := g.writeVars(b, o.Body(), true); err != nil {
return err
}
}
+9
View File
@@ -384,6 +384,15 @@ func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, var
return nil
}
var builtInPointerTypes = [...]bool{
t.KeyBuf1: true,
t.KeyBuf1Mark: true,
// TODO: uncomment, after figuring out how limit's should really work.
// t.KeyReader1: true,
t.KeyWriter1: true,
t.KeyBuf2: true,
}
var cTypeNames = [...]string{
t.KeyI8: "int8_t",
t.KeyI16: "int16_t",
+1 -1
View File
@@ -176,7 +176,7 @@ func (g *gen) writeFuncImplHeader(b *buffer) error {
b.writes("\n")
// Generate the local variables.
if err := g.writeVars(b, g.currFunk.astFunc.Body()); err != nil {
if err := g.writeVars(b, g.currFunk.astFunc.Body(), false); err != nil {
return err
}
b.writes("\n")
+62 -25
View File
@@ -230,33 +230,53 @@ func (g *gen) visitVars(b *buffer, block []*a.Node, depth uint32, f func(*gen, *
func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, prefix string, suspend bool) error {
local := fmt.Sprintf("%s%s", prefix, n.Name().String(g.tm))
lhs := local
// TODO: don't hard-code [0], and allow recursive coroutines.
rhs := fmt.Sprintf("self->private_impl.%s%s[0].%s", cPrefix, g.currFunk.astFunc.Name().String(g.tm), lhs)
if suspend {
lhs, rhs = rhs, lhs
}
typ := n.XType()
switch typ.Decorator().Key() {
case 0:
b.printf("%s = %s;\n", lhs, rhs)
return nil
case t.KeyOpenBracket:
inner := typ.Inner()
if inner.Decorator() != 0 {
break
}
switch inner.Name().Key() {
case t.KeyU8, t.KeyU16, t.KeyU32, t.KeyU64:
b.printf("memcpy(%s, %s, sizeof(%s));\n", lhs, rhs, local)
// TODO: explicitly handle prefix == lPrefix?
if typ := n.XType(); prefix == vPrefix && typeHasPointers(typ) {
if suspend {
return nil
}
case t.KeyColon:
// TODO: should we be able to resume and suspend slices?
// TODO: don't assume that the slice is a slice of u8.
b.printf("%s = ((puffs_base_slice_u8){});\n", lhs)
return nil
rhs := ""
switch typ.Decorator().Key() {
case 0:
if key := typ.Name().Key(); key < t.Key(len(cTypeNames)) {
rhs = cTypeNames[key]
}
case t.KeyColon:
// TODO: don't assume that the slice is a slice of u8.
rhs = "puffs_base_slice_u8"
}
if rhs != "" {
b.printf("%s = ((%s){});\n", local, rhs)
return nil
}
} else {
lhs := local
// TODO: don't hard-code [0], and allow recursive coroutines.
rhs := fmt.Sprintf("self->private_impl.%s%s[0].%s", cPrefix, g.currFunk.astFunc.Name().String(g.tm), lhs)
if suspend {
lhs, rhs = rhs, lhs
}
switch typ.Decorator().Key() {
case 0:
b.printf("%s = %s;\n", lhs, rhs)
return nil
case t.KeyOpenBracket:
inner := typ.Inner()
if inner.Decorator() != 0 {
break
}
switch inner.Name().Key() {
case t.KeyU8, t.KeyU16, t.KeyU32, t.KeyU64:
b.printf("memcpy(%s, %s, sizeof(%s));\n", lhs, rhs, local)
return nil
}
}
}
return fmt.Errorf("cannot resume or suspend a local variable %q of type %q",
n.Name().String(g.tm), n.XType().String(g.tm))
}
@@ -272,11 +292,14 @@ func (g *gen) writeResumeSuspend(b *buffer, block []*a.Node, suspend bool) error
})
}
func (g *gen) writeVars(b *buffer, block []*a.Node) error {
func (g *gen) writeVars(b *buffer, block []*a.Node, skipPointerTypes bool) error {
return g.visitVars(b, block, 0, func(g *gen, b *buffer, n *a.Var) error {
if v := n.Value(); v != nil && v.ID0().Key() == t.KeyLimit {
b.printf("uint64_t %s%v;\n", lPrefix, n.Name().String(g.tm))
}
if skipPointerTypes && typeHasPointers(n.XType()) {
return nil
}
if err := g.writeCTypeName(b, n.XType(), vPrefix, n.Name().String(g.tm)); err != nil {
return err
}
@@ -284,3 +307,17 @@ func (g *gen) writeVars(b *buffer, block []*a.Node) error {
return nil
})
}
func typeHasPointers(typ *a.TypeExpr) bool {
for ; typ != nil; typ = typ.Inner() {
switch typ.Decorator().Key() {
case 0:
if key := typ.Name().Key(); key < t.Key(len(builtInPointerTypes)) && builtInPointerTypes[key] {
return true
}
case t.KeyPtr, t.KeyColon:
return true
}
}
return false
}
+2 -8
View File
@@ -177,10 +177,7 @@ typedef struct {
struct {
uint32_t coro_susp_point;
puffs_base_buf1_mark v_m0;
puffs_flate_status v_z;
puffs_base_buf1_mark v_m1;
puffs_base_slice_u8 v_written;
} c_decode[1];
struct {
uint32_t coro_susp_point;
@@ -637,9 +634,9 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self,
uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point;
if (coro_susp_point) {
v_m0 = self->private_impl.c_decode[0].v_m0;
v_m0 = ((puffs_base_buf1_mark){});
v_z = self->private_impl.c_decode[0].v_z;
v_m1 = self->private_impl.c_decode[0].v_m1;
v_m1 = ((puffs_base_buf1_mark){});
v_written = ((puffs_base_slice_u8){});
}
switch (coro_susp_point) {
@@ -694,10 +691,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self,
goto suspend;
suspend:
self->private_impl.c_decode[0].coro_susp_point = coro_susp_point;
self->private_impl.c_decode[0].v_m0 = v_m0;
self->private_impl.c_decode[0].v_z = v_z;
self->private_impl.c_decode[0].v_m1 = v_m1;
self->private_impl.c_decode[0].v_written = ((puffs_base_slice_u8){});
exit:
if (a_dst.buf) {
-3
View File
@@ -177,10 +177,7 @@ typedef struct {
struct {
uint32_t coro_susp_point;
puffs_base_buf1_mark v_m0;
puffs_flate_status v_z;
puffs_base_buf1_mark v_m1;
puffs_base_slice_u8 v_written;
} c_decode[1];
struct {
uint32_t coro_susp_point;
+3
View File
@@ -151,6 +151,9 @@ pri func decoder.decode_id?(dst writer1, src reader1)() {
if block_size == 0 {
break
}
// TODO: how should this reader1-typed variable (which is implemented
// by containing pointers) work across resume/suspend? We don't want to
// have a dangling pointer to an earlier src buffer.
var lzw_src reader1 = limit (block_size as u64) in.src
this.lzw.decode?(dst:in.dst, src:lzw_src)
}