mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Generate C struct fields to save coroutine state.
This commit is contained in:
1 parent
09c59dfade
commit
e21c68a006
3 files changed
+131
-13
No files matched your search
+39
-13
@@ -34,6 +34,7 @@ var (
|
||||
// "double" being a valid Puffs variable name but not a valid C one.
|
||||
const (
|
||||
aPrefix = "a_" // Function argument.
|
||||
cPrefix = "c_" // Coroutine state.
|
||||
fPrefix = "f_" // Struct field.
|
||||
lPrefix = "l_" // Limit length.
|
||||
tPrefix = "t_" // Temporary local variable.
|
||||
@@ -175,9 +176,9 @@ func (g *gen) generate() error {
|
||||
// Make a topologically sorted list of structs.
|
||||
unsortedStructs := []*a.Struct(nil)
|
||||
for _, file := range g.files {
|
||||
for _, n := range file.TopLevelDecls() {
|
||||
if n.Kind() == a.KStruct {
|
||||
unsortedStructs = append(unsortedStructs, n.Struct())
|
||||
for _, tld := range file.TopLevelDecls() {
|
||||
if tld.Kind() == a.KStruct {
|
||||
unsortedStructs = append(unsortedStructs, tld.Struct())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -330,13 +331,13 @@ func (g *gen) genImpl() error {
|
||||
|
||||
func (g *gen) forEachFunc(v visibility, f func(*gen, *a.Func) error) error {
|
||||
for _, file := range g.files {
|
||||
for _, n := range file.TopLevelDecls() {
|
||||
if n.Kind() != a.KFunc ||
|
||||
(v == pubOnly && n.Raw().Flags()&a.FlagsPublic == 0) ||
|
||||
(v == priOnly && n.Raw().Flags()&a.FlagsPublic != 0) {
|
||||
for _, tld := range file.TopLevelDecls() {
|
||||
if tld.Kind() != a.KFunc ||
|
||||
(v == pubOnly && tld.Raw().Flags()&a.FlagsPublic == 0) ||
|
||||
(v == priOnly && tld.Raw().Flags()&a.FlagsPublic != 0) {
|
||||
continue
|
||||
}
|
||||
if err := f(g, n.Func()); err != nil {
|
||||
if err := f(g, tld.Func()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -346,13 +347,13 @@ func (g *gen) forEachFunc(v visibility, f func(*gen, *a.Func) error) error {
|
||||
|
||||
func (g *gen) forEachStatus(v visibility, f func(*gen, *a.Status) error) error {
|
||||
for _, file := range g.files {
|
||||
for _, n := range file.TopLevelDecls() {
|
||||
if n.Kind() != a.KStatus ||
|
||||
(v == pubOnly && n.Raw().Flags()&a.FlagsPublic == 0) ||
|
||||
(v == priOnly && n.Raw().Flags()&a.FlagsPublic != 0) {
|
||||
for _, tld := range file.TopLevelDecls() {
|
||||
if tld.Kind() != a.KStatus ||
|
||||
(v == pubOnly && tld.Raw().Flags()&a.FlagsPublic == 0) ||
|
||||
(v == priOnly && tld.Raw().Flags()&a.FlagsPublic != 0) {
|
||||
continue
|
||||
}
|
||||
if err := f(g, n.Status()); err != nil {
|
||||
if err := f(g, tld.Status()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -418,6 +419,7 @@ func (g *gen) writeStruct(n *a.Struct) error {
|
||||
g.printf("puffs_%s_status status;\n", g.pkgName)
|
||||
g.printf("uint32_t magic;\n")
|
||||
}
|
||||
|
||||
for _, o := range n.Fields() {
|
||||
o := o.Field()
|
||||
if err := g.writeCTypeName(o.XType(), fPrefix, o.Name().String(g.tm)); err != nil {
|
||||
@@ -425,6 +427,30 @@ func (g *gen) writeStruct(n *a.Struct) error {
|
||||
}
|
||||
g.writes(";\n")
|
||||
}
|
||||
|
||||
if n.Suspendible() {
|
||||
g.writeb('\n')
|
||||
for _, file := range g.files {
|
||||
for _, tld := range file.TopLevelDecls() {
|
||||
if tld.Kind() != a.KFunc {
|
||||
continue
|
||||
}
|
||||
o := tld.Func()
|
||||
if o.Receiver() != n.Name() || !o.Suspendible() {
|
||||
continue
|
||||
}
|
||||
// TODO: allow max depth > 1 for recursive coroutines.
|
||||
const maxDepth = 1
|
||||
g.writes("struct {\n")
|
||||
g.writes("uint32_t coro_state;\n")
|
||||
if err := g.writeVars(o.Body(), 0); err != nil {
|
||||
return err
|
||||
}
|
||||
g.printf("} %s%s[%d];\n", cPrefix, o.Name().String(g.tm), maxDepth)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
g.printf("} private_impl;\n } puffs_%s_%s;\n\n", g.pkgName, structName)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -121,6 +121,21 @@ typedef struct {
|
||||
uint8_t f_stack[4096];
|
||||
uint8_t f_suffixes[4096];
|
||||
uint16_t f_prefixes[4096];
|
||||
|
||||
struct {
|
||||
uint32_t coro_state;
|
||||
uint32_t v_clear_code;
|
||||
uint32_t v_end_code;
|
||||
bool v_use_save_code;
|
||||
uint32_t v_save_code;
|
||||
uint32_t v_prev_code;
|
||||
uint32_t v_width;
|
||||
uint32_t v_bits;
|
||||
uint32_t v_n_bits;
|
||||
uint32_t v_code;
|
||||
uint32_t v_s;
|
||||
uint32_t v_c;
|
||||
} c_decode[1];
|
||||
} private_impl;
|
||||
} puffs_gif_lzw_decoder;
|
||||
|
||||
@@ -140,6 +155,37 @@ typedef struct {
|
||||
uint8_t f_background_color_index;
|
||||
uint8_t f_gct[768];
|
||||
puffs_gif_lzw_decoder f_lzw;
|
||||
|
||||
struct {
|
||||
uint32_t coro_state;
|
||||
uint8_t v_c;
|
||||
} c_decode[1];
|
||||
struct {
|
||||
uint32_t coro_state;
|
||||
uint8_t v_c[6];
|
||||
uint32_t v_i;
|
||||
} c_decode_header[1];
|
||||
struct {
|
||||
uint32_t coro_state;
|
||||
uint8_t v_c[7];
|
||||
uint32_t v_i;
|
||||
uint32_t v_gct_size;
|
||||
} c_decode_lsd[1];
|
||||
struct {
|
||||
uint32_t coro_state;
|
||||
uint8_t v_label;
|
||||
uint8_t v_block_size;
|
||||
} c_decode_extension[1];
|
||||
struct {
|
||||
uint32_t coro_state;
|
||||
uint8_t v_c[9];
|
||||
uint32_t v_i;
|
||||
bool v_interlace;
|
||||
uint8_t v_lw;
|
||||
uint8_t v_block_size;
|
||||
uint64_t l_lzw_src;
|
||||
puffs_base_reader1 v_lzw_src;
|
||||
} c_decode_id[1];
|
||||
} private_impl;
|
||||
} puffs_gif_decoder;
|
||||
|
||||
|
||||
@@ -121,6 +121,21 @@ typedef struct {
|
||||
uint8_t f_stack[4096];
|
||||
uint8_t f_suffixes[4096];
|
||||
uint16_t f_prefixes[4096];
|
||||
|
||||
struct {
|
||||
uint32_t coro_state;
|
||||
uint32_t v_clear_code;
|
||||
uint32_t v_end_code;
|
||||
bool v_use_save_code;
|
||||
uint32_t v_save_code;
|
||||
uint32_t v_prev_code;
|
||||
uint32_t v_width;
|
||||
uint32_t v_bits;
|
||||
uint32_t v_n_bits;
|
||||
uint32_t v_code;
|
||||
uint32_t v_s;
|
||||
uint32_t v_c;
|
||||
} c_decode[1];
|
||||
} private_impl;
|
||||
} puffs_gif_lzw_decoder;
|
||||
|
||||
@@ -140,6 +155,37 @@ typedef struct {
|
||||
uint8_t f_background_color_index;
|
||||
uint8_t f_gct[768];
|
||||
puffs_gif_lzw_decoder f_lzw;
|
||||
|
||||
struct {
|
||||
uint32_t coro_state;
|
||||
uint8_t v_c;
|
||||
} c_decode[1];
|
||||
struct {
|
||||
uint32_t coro_state;
|
||||
uint8_t v_c[6];
|
||||
uint32_t v_i;
|
||||
} c_decode_header[1];
|
||||
struct {
|
||||
uint32_t coro_state;
|
||||
uint8_t v_c[7];
|
||||
uint32_t v_i;
|
||||
uint32_t v_gct_size;
|
||||
} c_decode_lsd[1];
|
||||
struct {
|
||||
uint32_t coro_state;
|
||||
uint8_t v_label;
|
||||
uint8_t v_block_size;
|
||||
} c_decode_extension[1];
|
||||
struct {
|
||||
uint32_t coro_state;
|
||||
uint8_t v_c[9];
|
||||
uint32_t v_i;
|
||||
bool v_interlace;
|
||||
uint8_t v_lw;
|
||||
uint8_t v_block_size;
|
||||
uint64_t l_lzw_src;
|
||||
puffs_base_reader1 v_lzw_src;
|
||||
} c_decode_id[1];
|
||||
} private_impl;
|
||||
} puffs_gif_decoder;
|
||||
|
||||
|
||||
Reference in new issue
Block a user