Add built-in table T methods

This commit is contained in:
Nigel Tao committed 2018-05-18 12:50:53 +10:00
1 parent eda326726a
commit e4320c3c6d
21 files changed
+194 -5

No files matched your search

+8
View File
@@ -1076,4 +1076,12 @@ static inline uint8_t* wuffs_base__image_buffer__palette(
return b ? b->private_impl.palette : NULL;
}
static inline wuffs_base__table_u8 wuffs_base__image_buffer__plane(
wuffs_base__image_buffer* b,
uint32_t p) {
return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))
? b->private_impl.pixbuf.planes[p]
: ((wuffs_base__table_u8){});
}
#endif // WUFFS_BASE_HEADER_H
+26
View File
@@ -52,6 +52,8 @@ func (g *gen) writeBuiltinCall(b *buffer, n *a.Expr, rp replacementPolicy, depth
return nil
case t.IDSlice:
return g.writeBuiltinSlice(b, recv, method.Ident(), n.Args(), rp, depth)
case t.IDTable:
return g.writeBuiltinTable(b, recv, method.Ident(), n.Args(), rp, depth)
default:
return errNoSuchBuiltin
}
@@ -311,6 +313,30 @@ func (g *gen) writeBuiltinStatus(b *buffer, recv *a.Expr, method t.ID, args []*a
return nil
}
func (g *gen) writeBuiltinTable(b *buffer, recv *a.Expr, method t.ID, args []*a.Node, rp replacementPolicy, depth uint32) error {
field := ""
switch method {
case t.IDHeight:
field = "height"
case t.IDStride:
field = "stride"
case t.IDWidth:
field = "width"
}
if field != "" {
b.writes("((uint64_t)(")
if err := g.writeExpr(b, recv, rp, depth); err != nil {
return err
}
b.printf(".%s))", field)
return nil
}
return errNoSuchBuiltin
}
func (g *gen) writeArgs(b *buffer, args []*a.Node, rp replacementPolicy, depth uint32) error {
for i, o := range args {
if i > 0 {
+1 -1
View File
@@ -77,7 +77,7 @@ const baseHeader = "" +
"// --------\n\ntypedef struct {\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n wuffs_base__image_config config;\n uint32_t loop_count; // 0-based count of the current loop.\n wuffs_base__pixel_buffer pixbuf;\n // TODO: color spaces.\n wuffs_base__rect_ie_u32 dirty_rect;\n wuffs_base__flicks duration;\n uint8_t palette[1024];\n } private_impl;\n} wuffs_base__image_buffer;\n\nstatic inline void wuffs_base__image_buffer__set_from_pixbuf(\n wuffs_base__image_buffer* b,\n wuffs_base__image_config config,\n wuffs_base__pixel_buffer pixbuf) {\n if (!b) {\n return;\n }\n *b = ((wuffs_base__image_buffer){});\n b->private_impl.config = config;\n b->private_impl.pixbuf = pixbuf;\n}\n\n// TODO: Should this function return bool? An error type?\nstatic inline void wuffs_base__image_buffer__set_from_slice(\n wuffs_base__image_buffer* b,\n wuffs_base__image_config config,\n wuffs_base__slice_u8 pixbuf_memory) {\n " +
" if (!b) {\n return;\n }\n *b = ((wuffs_base__image_buffer){});\n // TODO: don't assume 1 byte per pixel. Don't assume packed.\n uint64_t wh = ((uint64_t)config.private_impl.width) *\n ((uint64_t)config.private_impl.height);\n if (wh > pixbuf_memory.len) {\n return;\n }\n b->private_impl.config = config;\n wuffs_base__table_u8* tab = &b->private_impl.pixbuf.planes[0];\n tab->ptr = pixbuf_memory.ptr;\n tab->width = config.private_impl.width;\n tab->height = config.private_impl.height;\n tab->stride = config.private_impl.width;\n}\n\nstatic inline void wuffs_base__image_buffer__update(\n wuffs_base__image_buffer* b,\n wuffs_base__rect_ie_u32 dirty_rect,\n wuffs_base__flicks duration,\n uint8_t* palette_ptr,\n size_t palette_len) {\n if (!b) {\n return;\n }\n b->private_impl.dirty_rect = dirty_rect;\n b->private_impl.duration = duration;\n if (palette_ptr) {\n memmove(b->private_impl.palette, palette_ptr,\n palette_len <= 1024 ? palette_len : 1024);\n }\n}\n\n// wuffs_base__im" +
"age_buffer__loop returns whether the image decoder should loop\n// back to the beginning of the animation, assuming that we've reached the end\n// of the encoded stream. If so, it increments b's count of the animation loops\n// played so far.\nstatic inline bool wuffs_base__image_buffer__loop(wuffs_base__image_buffer* b) {\n if (!b) {\n return false;\n }\n uint32_t n = b->private_impl.config.private_impl.num_loops;\n if (n == 0) {\n return true;\n }\n if (b->private_impl.loop_count < n - 1) {\n b->private_impl.loop_count++;\n return true;\n }\n return false;\n}\n\n// wuffs_base__image_buffer__dirty_rect returns an upper bound for what part of\n// this frame's pixels differs from the previous frame.\nstatic inline wuffs_base__rect_ie_u32 wuffs_base__image_buffer__dirty_rect(\n wuffs_base__image_buffer* b) {\n return b ? b->private_impl.dirty_rect : ((wuffs_base__rect_ie_u32){0});\n}\n\n// wuffs_base__image_buffer__duration returns the amount of time to display\n// this frame. Zero means to display forever - a sti" +
"ll (non-animated) image.\nstatic inline wuffs_base__flicks wuffs_base__image_buffer__duration(\n wuffs_base__image_buffer* b) {\n return b ? b->private_impl.duration : 0;\n}\n\n// wuffs_base__image_buffer__palette returns the palette that the pixel data\n// can index. The backing array is inside b and has length 1024.\nstatic inline uint8_t* wuffs_base__image_buffer__palette(\n wuffs_base__image_buffer* b) {\n return b ? b->private_impl.palette : NULL;\n}\n\n#endif // WUFFS_BASE_HEADER_H\n" +
"ll (non-animated) image.\nstatic inline wuffs_base__flicks wuffs_base__image_buffer__duration(\n wuffs_base__image_buffer* b) {\n return b ? b->private_impl.duration : 0;\n}\n\n// wuffs_base__image_buffer__palette returns the palette that the pixel data\n// can index. The backing array is inside b and has length 1024.\nstatic inline uint8_t* wuffs_base__image_buffer__palette(\n wuffs_base__image_buffer* b) {\n return b ? b->private_impl.palette : NULL;\n}\n\nstatic inline wuffs_base__table_u8 wuffs_base__image_buffer__plane(\n wuffs_base__image_buffer* b,\n uint32_t p) {\n return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))\n ? b->private_impl.pixbuf.planes[p]\n : ((wuffs_base__table_u8){});\n}\n\n#endif // WUFFS_BASE_HEADER_H\n" +
""
const baseImpl = "" +
+8
View File
@@ -1079,6 +1079,14 @@ static inline uint8_t* wuffs_base__image_buffer__palette(
return b ? b->private_impl.palette : NULL;
}
static inline wuffs_base__table_u8 wuffs_base__image_buffer__plane(
wuffs_base__image_buffer* b,
uint32_t p) {
return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))
? b->private_impl.pixbuf.planes[p]
: ((wuffs_base__table_u8){});
}
#endif // WUFFS_BASE_HEADER_H
// ---------------- Use Declarations
+8
View File
@@ -1079,6 +1079,14 @@ static inline uint8_t* wuffs_base__image_buffer__palette(
return b ? b->private_impl.palette : NULL;
}
static inline wuffs_base__table_u8 wuffs_base__image_buffer__plane(
wuffs_base__image_buffer* b,
uint32_t p) {
return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))
? b->private_impl.pixbuf.planes[p]
: ((wuffs_base__table_u8){});
}
#endif // WUFFS_BASE_HEADER_H
// ---------------- Use Declarations
+8
View File
@@ -1079,6 +1079,14 @@ static inline uint8_t* wuffs_base__image_buffer__palette(
return b ? b->private_impl.palette : NULL;
}
static inline wuffs_base__table_u8 wuffs_base__image_buffer__plane(
wuffs_base__image_buffer* b,
uint32_t p) {
return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))
? b->private_impl.pixbuf.planes[p]
: ((wuffs_base__table_u8){});
}
#endif // WUFFS_BASE_HEADER_H
// ---------------- Use Declarations
+14
View File
@@ -1079,6 +1079,14 @@ static inline uint8_t* wuffs_base__image_buffer__palette(
return b ? b->private_impl.palette : NULL;
}
static inline wuffs_base__table_u8 wuffs_base__image_buffer__plane(
wuffs_base__image_buffer* b,
uint32_t p) {
return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))
? b->private_impl.pixbuf.planes[p]
: ((wuffs_base__table_u8){});
}
#endif // WUFFS_BASE_HEADER_H
// ---------------- Use Declarations
@@ -2748,6 +2756,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
wuffs_gif__status v_z;
wuffs_base__slice_u8 v_pass_through;
uint64_t v_n_copied;
wuffs_base__table_u8 v_tab;
uint8_t* ioptr_dst = NULL;
uint8_t* iobounds0orig_dst = NULL;
@@ -2794,9 +2803,11 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
v_z = self->private_impl.c_decode_id[0].v_z;
v_pass_through = ((wuffs_base__slice_u8){});
v_n_copied = self->private_impl.c_decode_id[0].v_n_copied;
v_tab = ((wuffs_base__table_u8){});
} else {
v_w = ((wuffs_base__io_writer){});
v_pass_through = ((wuffs_base__slice_u8){});
v_tab = ((wuffs_base__table_u8){});
}
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
@@ -3056,6 +3067,9 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id(
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(15);
}
label_2_break:;
v_tab = wuffs_base__image_buffer__plane(a_ib, 0);
if (((uint64_t)(v_tab.width)) == ((uint64_t)(v_tab.stride))) {
}
if (v_z == WUFFS_GIF__SUSPENSION_SHORT_WRITE) {
goto label_1_continue;
}
+8
View File
@@ -1079,6 +1079,14 @@ static inline uint8_t* wuffs_base__image_buffer__palette(
return b ? b->private_impl.palette : NULL;
}
static inline wuffs_base__table_u8 wuffs_base__image_buffer__plane(
wuffs_base__image_buffer* b,
uint32_t p) {
return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))
? b->private_impl.pixbuf.planes[p]
: ((wuffs_base__table_u8){});
}
#endif // WUFFS_BASE_HEADER_H
// ---------------- Use Declarations
+8
View File
@@ -1079,6 +1079,14 @@ static inline uint8_t* wuffs_base__image_buffer__palette(
return b ? b->private_impl.palette : NULL;
}
static inline wuffs_base__table_u8 wuffs_base__image_buffer__plane(
wuffs_base__image_buffer* b,
uint32_t p) {
return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))
? b->private_impl.pixbuf.planes[p]
: ((wuffs_base__table_u8){});
}
#endif // WUFFS_BASE_HEADER_H
// ---------------- Use Declarations
+8
View File
@@ -1079,6 +1079,14 @@ static inline uint8_t* wuffs_base__image_buffer__palette(
return b ? b->private_impl.palette : NULL;
}
static inline wuffs_base__table_u8 wuffs_base__image_buffer__plane(
wuffs_base__image_buffer* b,
uint32_t p) {
return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))
? b->private_impl.pixbuf.planes[p]
: ((wuffs_base__table_u8){});
}
#endif // WUFFS_BASE_HEADER_H
// ---------------- Use Declarations
+8
View File
@@ -1079,6 +1079,14 @@ static inline uint8_t* wuffs_base__image_buffer__palette(
return b ? b->private_impl.palette : NULL;
}
static inline wuffs_base__table_u8 wuffs_base__image_buffer__plane(
wuffs_base__image_buffer* b,
uint32_t p) {
return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))
? b->private_impl.pixbuf.planes[p]
: ((wuffs_base__table_u8){});
}
#endif // WUFFS_BASE_HEADER_H
// ---------------- Use Declarations
+8
View File
@@ -1079,6 +1079,14 @@ static inline uint8_t* wuffs_base__image_buffer__palette(
return b ? b->private_impl.palette : NULL;
}
static inline wuffs_base__table_u8 wuffs_base__image_buffer__plane(
wuffs_base__image_buffer* b,
uint32_t p) {
return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))
? b->private_impl.pixbuf.planes[p]
: ((wuffs_base__table_u8){});
}
#endif // WUFFS_BASE_HEADER_H
// ---------------- Use Declarations
+8
View File
@@ -1079,6 +1079,14 @@ static inline uint8_t* wuffs_base__image_buffer__palette(
return b ? b->private_impl.palette : NULL;
}
static inline wuffs_base__table_u8 wuffs_base__image_buffer__plane(
wuffs_base__image_buffer* b,
uint32_t p) {
return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))
? b->private_impl.pixbuf.planes[p]
: ((wuffs_base__table_u8){});
}
#endif // WUFFS_BASE_HEADER_H
// ---------------- Use Declarations
+8
View File
@@ -1079,6 +1079,14 @@ static inline uint8_t* wuffs_base__image_buffer__palette(
return b ? b->private_impl.palette : NULL;
}
static inline wuffs_base__table_u8 wuffs_base__image_buffer__plane(
wuffs_base__image_buffer* b,
uint32_t p) {
return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))
? b->private_impl.pixbuf.planes[p]
: ((wuffs_base__table_u8){});
}
#endif // WUFFS_BASE_HEADER_H
// ---------------- Use Declarations
+8
View File
@@ -1079,6 +1079,14 @@ static inline uint8_t* wuffs_base__image_buffer__palette(
return b ? b->private_impl.palette : NULL;
}
static inline wuffs_base__table_u8 wuffs_base__image_buffer__plane(
wuffs_base__image_buffer* b,
uint32_t p) {
return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))
? b->private_impl.pixbuf.planes[p]
: ((wuffs_base__table_u8){});
}
#endif // WUFFS_BASE_HEADER_H
// ---------------- Use Declarations
+12 -4
View File
@@ -103,6 +103,8 @@ var Funcs = []string{
// ---- image_buffer
"image_buffer.plane(p u32[..3])(ret table u8)",
// ---- image_config
"image_config.initialize!(pixfmt u32, pixsub u32, width u32, height u32, num_loops u32)()",
@@ -155,10 +157,10 @@ var Funcs = []string{
"status.is_suspension()(ret bool)",
}
// The "T" types here are placeholders for generic "slice U" types. After
// tokenizing (but before parsing) these SliceFunc strings (e.g. in the
// lang/check package), replace the "T" receiver type with the "◊" diamond, to
// avoid collision with a user-defined "T" type.
// The "T" types here are placeholders for generic "slice U" or "table U"
// types. After tokenizing (but before parsing) these XxxFunc strings (e.g. in
// the lang/check package), replace the "T" receiver type with the "◊" diamond,
// to avoid collision with a user-defined "T" type.
const (
GenericOldName = t.IDCapitalT
@@ -171,3 +173,9 @@ var SliceFuncs = []string{
"T.prefix(up_to u64)(ret T)",
"T.suffix(up_to u64)(ret T)",
}
var TableFuncs = []string{
"T.height()(ret u64)",
"T.stride()(ret u64)",
"T.width()(ret u64)",
}
+1
View File
@@ -169,6 +169,7 @@ type Checker struct {
builtInFuncs map[t.QQID]*a.Func
builtInSliceFuncs map[t.QQID]*a.Func
builtInTableFuncs map[t.QQID]*a.Func
unsortedStructs []*a.Struct
}
+20
View File
@@ -98,6 +98,17 @@ func (c *Checker) builtInSliceFunc(qqid t.QQID) (*a.Func, error) {
return c.builtInSliceFuncs[qqid], nil
}
func (c *Checker) builtInTableFunc(qqid t.QQID) (*a.Func, error) {
if c.builtInTableFuncs == nil {
err := error(nil)
c.builtInTableFuncs, err = c.parseBuiltInFuncs(builtin.TableFuncs, true)
if err != nil {
return nil, err
}
}
return c.builtInTableFuncs[qqid], nil
}
func (c *Checker) parseBuiltInFuncs(ss []string, generic bool) (map[t.QQID]*a.Func, error) {
m := map[t.QQID]*a.Func{}
buf := []byte(nil)
@@ -157,6 +168,15 @@ func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) {
return f, nil
}
} else if lTyp.IsTableType() {
qqid[0] = t.IDBase
qqid[1] = t.IDDiamond
if f, err := c.builtInTableFunc(qqid); err != nil {
return nil, err
} else if f != nil {
return f, nil
}
} else {
if f, err := c.builtInFunc(qqid); err != nil {
return nil, err
+11
View File
@@ -617,6 +617,17 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
n.SetMType(a.NewTypeExpr(t.IDFunc, 0, n.Ident(), lTyp.Node(), nil, nil))
return nil
} else if lTyp.IsTableType() {
qqid[0] = t.IDBase
qqid[1] = t.IDDiamond
if f, err := q.c.builtInTableFunc(qqid); err != nil {
return err
} else if f == nil {
return fmt.Errorf("check: no table method %q", n.Ident().Str(q.tm))
}
n.SetMType(a.NewTypeExpr(t.IDFunc, 0, n.Ident(), lTyp.Node(), nil, nil))
return nil
} else if lTyp.Decorator() != 0 {
return fmt.Errorf("check: invalid type %q for dot-expression LHS %q", lTyp.Str(q.tm), lhs.Str(q.tm))
}
+6
View File
@@ -471,6 +471,9 @@ const (
IDMax = ID(0x213)
IDMin = ID(0x214)
IDSet = ID(0x215)
IDHeight = ID(0x216)
IDStride = ID(0x217)
IDWidth = ID(0x218)
)
var builtInsByID = [nBuiltInIDs]string{
@@ -671,6 +674,9 @@ var builtInsByID = [nBuiltInIDs]string{
IDMax: "max",
IDMin: "min",
IDSet: "set",
IDHeight: "height",
IDStride: "stride",
IDWidth: "width",
}
var builtInsByName = map[string]ID{}
+7
View File
@@ -373,6 +373,13 @@ pri func decoder.decode_id?(ib ptr base.image_buffer, dst base.io_writer, src ba
// TODO: "closed for writes" instead?
yield suspension "short write"
}
// TODO: don't assume a packed pixel format.
var tab table base.u8 = in.ib.plane(p:0)
if tab.width() == tab.stride() {
// TODO.
} else {
// TODO.
}
if z == suspension "short write" {
continue:inner