Rename ioptr_etc cgen variables

This commit is contained in:
Nigel Tao committed 2018-08-11 08:32:49 +10:00
1 parent 338f552c82
commit 9c0b2d2f5c
6 files changed
+683 -650

No files matched your search

+46 -46
View File
@@ -85,14 +85,14 @@ func (g *gen) writeBuiltinIO(b *buffer, recv *a.Expr, method t.ID, args []*a.Nod
// TODO: don't hard-code these.
switch recv.Str(g.tm) {
case "args.dst":
p0 = "iobounds1_dst"
p1 = "ioptr_dst"
p0 = "io1_a_dst"
p1 = "iop_a_dst"
case "args.src":
p0 = "iobounds1_src"
p1 = "ioptr_src"
p0 = "io1_a_src"
p1 = "iop_a_src"
case "w":
p0 = "iobounds1_w"
p1 = "ioptr_w"
p0 = "io1_v_w"
p1 = "iop_v_w"
}
if p0 == "" {
return fmt.Errorf(`TODO: cgen a "foo.available" expression`)
@@ -106,7 +106,7 @@ func (g *gen) writeBuiltinIO(b *buffer, recv *a.Expr, method t.ID, args []*a.Nod
typ = "writer"
}
// TODO: don't hard-code v_w and u_w.
b.printf("wuffs_base__io_%s__set(&v_w, &u_w, &ioptr_w, &iobounds1_w,", typ)
b.printf("wuffs_base__io_%s__set(&v_w, &u_w, &iop_v_w, &io1_v_w,", typ)
return g.writeArgs(b, args, rp, depth)
}
@@ -117,26 +117,26 @@ func (g *gen) writeBuiltinIOReader(b *buffer, recv *a.Expr, method t.ID, args []
// TODO: don't hard-code the recv being a_src.
switch method {
case t.IDUndoByte:
b.writes("(ioptr_src--, wuffs_base__return_empty_struct())")
b.writes("(iop_a_src--, wuffs_base__return_empty_struct())")
return nil
case t.IDCanUndoByte:
b.writes("(ioptr_src > iobounds0orig_src)")
b.writes("(iop_a_src > io0_a_src)")
return nil
case t.IDSetLimit:
b.printf("wuffs_base__io_reader__set_limit(&%ssrc, ioptr_src,", aPrefix)
// TODO: update the ioptr variables?
b.printf("wuffs_base__io_reader__set_limit(&%ssrc, iop_a_src,", aPrefix)
// TODO: update the iop variables?
return g.writeArgs(b, args, rp, depth)
case t.IDSetMark:
b.printf("wuffs_base__io_reader__set_mark(&%ssrc, ioptr_src)", aPrefix)
b.printf("wuffs_base__io_reader__set_mark(&%ssrc, iop_a_src)", aPrefix)
return nil
case t.IDSinceMark:
b.printf("((wuffs_base__slice_u8){ "+
".ptr = %ssrc.private_impl.bounds[0], "+
".len = (size_t)(ioptr_src - %ssrc.private_impl.bounds[0]), })",
".len = (size_t)(iop_a_src - %ssrc.private_impl.bounds[0]), })",
aPrefix, aPrefix)
return nil
@@ -145,7 +145,7 @@ func (g *gen) writeBuiltinIOReader(b *buffer, recv *a.Expr, method t.ID, args []
// increment, return_empty_struct call)". The final part is a function
// call (to a static inline function) instead of a struct literal, to
// avoid a "expression result unused" compiler error.
b.writes("(ioptr_src += ")
b.writes("(iop_a_src += ")
if err := g.writeExpr(b, args[1].AsArg().Value(), rp, depth); err != nil {
return err
}
@@ -156,7 +156,7 @@ func (g *gen) writeBuiltinIOReader(b *buffer, recv *a.Expr, method t.ID, args []
if method >= peekMethodsBase {
if m := method - peekMethodsBase; m < t.ID(len(peekMethods)) {
if p := peekMethods[m]; p.n != 0 {
b.printf("wuffs_base__load_u%d%ce(ioptr_src)", p.n, p.endianness)
b.printf("wuffs_base__load_u%d%ce(iop_a_src)", p.n, p.endianness)
return nil
}
}
@@ -174,7 +174,7 @@ func (g *gen) writeBuiltinIOWriter(b *buffer, recv *a.Expr, method t.ID, args []
suffix = "_fast"
}
b.printf("wuffs_base__io_writer__copy_n_from_history%s("+
"&ioptr_dst, %sdst.private_impl.bounds[0], iobounds1_dst",
"&iop_a_dst, %sdst.private_impl.bounds[0], io1_a_dst",
suffix, aPrefix)
for _, o := range args {
b.writeb(',')
@@ -186,20 +186,20 @@ func (g *gen) writeBuiltinIOWriter(b *buffer, recv *a.Expr, method t.ID, args []
return nil
case t.IDCopyNFromReader:
b.printf("wuffs_base__io_writer__copy_n_from_reader(&ioptr_dst, iobounds1_dst,")
b.printf("wuffs_base__io_writer__copy_n_from_reader(&iop_a_dst, io1_a_dst,")
if err := g.writeExpr(b, args[0].AsArg().Value(), rp, depth); err != nil {
return err
}
// TODO: don't assume that the last argument is "args.src".
b.printf(", &ioptr_src, iobounds1_src)")
b.printf(", &iop_a_src, io1_a_src)")
return nil
case t.IDCopyFromSlice:
b.printf("wuffs_base__io_writer__copy_from_slice(&ioptr_dst, iobounds1_dst,")
b.printf("wuffs_base__io_writer__copy_from_slice(&iop_a_dst, io1_a_dst,")
return g.writeArgs(b, args, rp, depth)
case t.IDCopyNFromSlice:
b.printf("wuffs_base__io_writer__copy_n_from_slice(&ioptr_dst, iobounds1_dst,")
b.printf("wuffs_base__io_writer__copy_n_from_slice(&iop_a_dst, io1_a_dst,")
return g.writeArgs(b, args, rp, depth)
case t.IDSetMark:
@@ -207,13 +207,13 @@ func (g *gen) writeBuiltinIOWriter(b *buffer, recv *a.Expr, method t.ID, args []
// if the function is passed a (ptr io_writer) instead of a
// (io_writer)? Do we still want to have that mark live outside of
// the function scope?
b.printf("wuffs_base__io_writer__set_mark(&%sdst, ioptr_dst)", aPrefix)
b.printf("wuffs_base__io_writer__set_mark(&%sdst, iop_a_dst)", aPrefix)
return nil
case t.IDSinceMark:
b.printf("((wuffs_base__slice_u8){ "+
".ptr = %sdst.private_impl.bounds[0], "+
".len = (size_t)(ioptr_dst - %sdst.private_impl.bounds[0]), })",
".len = (size_t)(iop_a_dst - %sdst.private_impl.bounds[0]), })",
aPrefix, aPrefix)
return nil
}
@@ -226,11 +226,11 @@ func (g *gen) writeBuiltinIOWriter(b *buffer, recv *a.Expr, method t.ID, args []
// final part is a function call (to a static inline function)
// instead of a struct literal, to avoid a "expression result
// unused" compiler error.
b.printf("(wuffs_base__store_u%d%ce(ioptr_dst,", p.n, p.endianness)
b.printf("(wuffs_base__store_u%d%ce(iop_a_dst,", p.n, p.endianness)
if err := g.writeExpr(b, args[0].AsArg().Value(), rp, depth); err != nil {
return err
}
b.printf("), ioptr_dst += %d, wuffs_base__return_empty_struct())", p.n/8)
b.printf("), iop_a_dst += %d, wuffs_base__return_empty_struct())", p.n/8)
return nil
}
}
@@ -429,7 +429,7 @@ func (g *gen) writeBuiltinCallSuspendibles(b *buffer, n *a.Expr, depth uint32) e
temp := g.currFunk.tempW
g.currFunk.tempW++
b.printf("if (WUFFS_BASE__UNLIKELY(ioptr_src == iobounds1_src)) {" +
b.printf("if (WUFFS_BASE__UNLIKELY(iop_a_src == io1_a_src)) {" +
"status = WUFFS_BASE__SUSPENSION_SHORT_READ; goto suspend; }")
// TODO: watch for passing an array type to writeCTypeName? In C, an
@@ -437,37 +437,37 @@ func (g *gen) writeBuiltinCallSuspendibles(b *buffer, n *a.Expr, depth uint32) e
if err := g.writeCTypeName(b, n.MType(), tPrefix, fmt.Sprint(temp)); err != nil {
return err
}
b.printf(" = *ioptr_src++;\n")
b.printf(" = *iop_a_src++;\n")
return nil
case t.IDReadU16BE:
return g.writeReadUXX(b, n, "src", 16, "be")
return g.writeReadUXX(b, n, "a_src", 16, "be")
case t.IDReadU16LE:
return g.writeReadUXX(b, n, "src", 16, "le")
return g.writeReadUXX(b, n, "a_src", 16, "le")
case t.IDReadU24BE:
return g.writeReadUXX(b, n, "src", 24, "be")
return g.writeReadUXX(b, n, "a_src", 24, "be")
case t.IDReadU24LE:
return g.writeReadUXX(b, n, "src", 24, "le")
return g.writeReadUXX(b, n, "a_src", 24, "le")
case t.IDReadU32BE:
return g.writeReadUXX(b, n, "src", 32, "be")
return g.writeReadUXX(b, n, "a_src", 32, "be")
case t.IDReadU32LE:
return g.writeReadUXX(b, n, "src", 32, "le")
return g.writeReadUXX(b, n, "a_src", 32, "le")
case t.IDReadU40BE:
return g.writeReadUXX(b, n, "src", 40, "be")
return g.writeReadUXX(b, n, "a_src", 40, "be")
case t.IDReadU40LE:
return g.writeReadUXX(b, n, "src", 40, "le")
return g.writeReadUXX(b, n, "a_src", 40, "le")
case t.IDReadU48BE:
return g.writeReadUXX(b, n, "src", 48, "be")
return g.writeReadUXX(b, n, "a_src", 48, "be")
case t.IDReadU48LE:
return g.writeReadUXX(b, n, "src", 48, "le")
return g.writeReadUXX(b, n, "a_src", 48, "le")
case t.IDReadU56BE:
return g.writeReadUXX(b, n, "src", 56, "be")
return g.writeReadUXX(b, n, "a_src", 56, "be")
case t.IDReadU56LE:
return g.writeReadUXX(b, n, "src", 56, "le")
return g.writeReadUXX(b, n, "a_src", 56, "le")
case t.IDReadU64BE:
return g.writeReadUXX(b, n, "src", 64, "be")
return g.writeReadUXX(b, n, "a_src", 64, "be")
case t.IDReadU64LE:
return g.writeReadUXX(b, n, "src", 64, "le")
return g.writeReadUXX(b, n, "a_src", 64, "le")
case t.IDSkip:
g.currFunk.usesScratch = true
@@ -487,21 +487,21 @@ func (g *gen) writeBuiltinCallSuspendibles(b *buffer, n *a.Expr, depth uint32) e
return err
}
b.printf("if (%s > ((uint64_t)(iobounds1_src - ioptr_src))) {\n", scratchName)
b.printf("%s -= iobounds1_src - ioptr_src;\n", scratchName)
b.printf("ioptr_src = iobounds1_src;\n")
b.printf("if (%s > ((uint64_t)(io1_a_src - iop_a_src))) {\n", scratchName)
b.printf("%s -= io1_a_src - iop_a_src;\n", scratchName)
b.printf("iop_a_src = io1_a_src;\n")
b.writes("status = WUFFS_BASE__SUSPENSION_SHORT_READ; goto suspend; }\n")
b.printf("ioptr_src += %s;\n", scratchName)
b.printf("iop_a_src += %s;\n", scratchName)
return nil
}
} else {
switch method.Ident() {
case t.IDWriteU8:
b.writes("if (ioptr_dst == iobounds1_dst) {\n" +
b.writes("if (iop_a_dst == io1_a_dst) {\n" +
"status = WUFFS_BASE__SUSPENSION_SHORT_WRITE; goto suspend; }\n" +
"*ioptr_dst++ = ")
"*iop_a_dst++ = ")
x := n.Args()[0].AsArg().Value()
if err := g.writeExpr(b, x, replaceCallSuspendibles, depth); err != nil {
return err
+30
View File
@@ -59,6 +59,36 @@ const (
vPrefix = "v_" // Local variable.
)
// I/O (reader/writer) prefixes. In the generated C code, the variables with
// these prefixes all have type uint8_t*. The iop_etc variables are the key
// ones. For an io_reader or io_writer function argument a_src or a_dst,
// reading or writing the next byte (and advancing the stream) is essentially
// "etc = *iop_a_src++" or "*io_a_dst++ = etc".
//
// The other two prefixes, giving names like io0_etc and io1_etc, are
// auxilliary pointers: lower and upper inclusive bounds. As an iop_etc pointer
// advances, it cannot advance past io1_etc. In the rarer case that an iop_etc
// pointer retreats, undoing a read or write, it cannot retreat past io0_etc.
//
// At the start of a function, these pointers are initialized from an
// io_buffer's fields (ptr, ri, wi, len), or possibly a limit field. For an
// io_reader:
// - io0_etc = ptr + ri
// - iop_etc = ptr + ri
// - io1_etc = ptr + wi or limit
// and for an io_writer:
// - io0_etc = ptr + wi
// - iop_etc = ptr + wi
// - io1_etc = ptr + len or limit
//
// TODO: discuss marks and limits, and how (if at all) auxilliary pointers can
// change over a function's lifetime.
const (
io0Prefix = "io0_" // Lower bound.
io1Prefix = "io1_" // Upper bound.
iopPrefix = "iop_" // Pointer.
)
// Do transpiles a Wuffs program to a C program.
//
// The arguments list the source Wuffs files. If no arguments are given, it
+2 -2
View File
@@ -264,7 +264,7 @@ func (g *gen) writeFuncImplHeader(b *buffer) error {
g.findDerivedVars()
for _, o := range g.currFunk.astFunc.In().Fields() {
o := o.AsField()
if err := g.writeLoadDerivedVar(b, "", o.Name(), o.XType(), true); err != nil {
if err := g.writeLoadDerivedVar(b, "", aPrefix, o.Name(), o.XType(), true); err != nil {
return err
}
}
@@ -333,7 +333,7 @@ func (g *gen) writeFuncImplFooter(b *buffer) error {
for _, o := range g.currFunk.astFunc.In().Fields() {
o := o.AsField()
if err := g.writeSaveDerivedVar(b, "", o.Name(), o.XType()); err != nil {
if err := g.writeSaveDerivedVar(b, "", aPrefix, o.Name(), o.XType()); err != nil {
return err
}
}
+19 -19
View File
@@ -176,14 +176,14 @@ func (g *gen) writeStatementIOBind(b *buffer, n *a.IOBind, depth uint32) error {
b.printf("wuffs_base__io_%s %s%d_%s%s = %s%s;\n",
cTyp, oPrefix, ioBindNum, prefix, name, prefix, name)
// TODO: save / restore all ioptr vars, not just for local IO vars? How
// TODO: save / restore all iop vars, not just for local IO vars? How
// does this work if the io_bind body advances these pointers, either
// directly or by calling other funcs?
if e.Operator() == 0 {
b.printf("uint8_t *%s%d_ioptr_%s%s = ioptr_%s;\n",
oPrefix, ioBindNum, prefix, name, name)
b.printf("uint8_t *%s%d_iobounds1_%s%s = iobounds1_%s;\n",
oPrefix, ioBindNum, prefix, name, name)
b.printf("uint8_t *%s%d_%s%s%s = %s%s%s;\n",
oPrefix, ioBindNum, iopPrefix, prefix, name, iopPrefix, prefix, name)
b.printf("uint8_t *%s%d_%s%s%s = %s%s%s;\n",
oPrefix, ioBindNum, io1Prefix, prefix, name, io1Prefix, prefix, name)
}
}
@@ -203,10 +203,10 @@ func (g *gen) writeStatementIOBind(b *buffer, n *a.IOBind, depth uint32) error {
b.printf("%s%s = %s%d_%s%s;\n",
prefix, name, oPrefix, ioBindNum, prefix, name)
if e.Operator() == 0 {
b.printf("ioptr_%s = %s%d_ioptr_%s%s;\n",
name, oPrefix, ioBindNum, prefix, name)
b.printf("iobounds1_%s = %s%d_iobounds1_%s%s;\n",
name, oPrefix, ioBindNum, prefix, name)
b.printf("%s%s%s = %s%d_%s%s%s;\n",
iopPrefix, prefix, name, oPrefix, ioBindNum, iopPrefix, prefix, name)
b.printf("%s%s%s = %s%d_%s%s%s;\n",
io1Prefix, prefix, name, oPrefix, ioBindNum, io1Prefix, prefix, name)
}
}
b.writes("}\n")
@@ -533,7 +533,7 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
return nil
}
func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endianness string) error {
func (g *gen) writeReadUXX(b *buffer, n *a.Expr, preName string, size uint32, endianness string) error {
if (size&7 != 0) || (size < 16) || (size > 64) {
return fmt.Errorf("internal error: bad writeReadUXX size %d", size)
}
@@ -560,9 +560,9 @@ func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endia
scratchName := fmt.Sprintf("self->private_impl.%s%s[0].scratch",
cPrefix, g.currFunk.astFunc.FuncName().Str(g.tm))
b.printf("if (WUFFS_BASE__LIKELY(iobounds1_src - ioptr_src >= %d)) {", size/8)
b.printf("%s%d = wuffs_base__load_u%d%s(ioptr_src);\n", tPrefix, temp1, size, endianness)
b.printf("ioptr_src += %d;\n", size/8)
b.printf("if (WUFFS_BASE__LIKELY(io1_a_src - iop_a_src >= %d)) {", size/8)
b.printf("%s%d = wuffs_base__load_u%d%s(iop_a_src);\n", tPrefix, temp1, size, endianness)
b.printf("iop_a_src += %d;\n", size/8)
b.printf("} else {")
b.printf("%s = 0;\n", scratchName)
if err := g.writeCoroSuspPoint(b, false); err != nil {
@@ -570,21 +570,21 @@ func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endia
}
b.printf("while (true) {")
b.printf("if (WUFFS_BASE__UNLIKELY(ioptr_%s == iobounds1_%s)) {"+
b.printf("if (WUFFS_BASE__UNLIKELY(iop_%s == io1_%s)) {"+
"status = WUFFS_BASE__SUSPENSION_SHORT_READ; goto suspend; }",
name, name)
preName, preName)
b.printf("uint64_t *scratch = &%s;", scratchName)
b.printf("uint32_t %s%d = *scratch", tPrefix, temp0)
switch endianness {
case "be":
b.writes("& 0xFF; *scratch >>= 8; *scratch <<= 8;")
b.printf("*scratch |= ((uint64_t)(*ioptr_%s++)) << (56 - %s%d);",
name, tPrefix, temp0)
b.printf("*scratch |= ((uint64_t)(*%s%s++)) << (56 - %s%d);",
iopPrefix, preName, tPrefix, temp0)
case "le":
b.writes(">> 56; *scratch <<= 8; *scratch >>= 8;")
b.printf("*scratch |= ((uint64_t)(*ioptr_%s++)) << %s%d;",
name, tPrefix, temp0)
b.printf("*scratch |= ((uint64_t)(*%s%s++)) << %s%d;",
iopPrefix, preName, tPrefix, temp0)
}
b.printf("if (%s%d == %d) {", tPrefix, temp0, size-8)
+39 -32
View File
@@ -79,7 +79,7 @@ func (g *gen) findDerivedVars() {
}
}
func (g *gen) writeLoadDerivedVar(b *buffer, hack string, name t.ID, typ *a.TypeExpr, header bool) error {
func (g *gen) writeLoadDerivedVar(b *buffer, hack string, prefix string, name t.ID, typ *a.TypeExpr, header bool) error {
// TODO: remove this hack. We're picking up the wrong name for "src:r,
// dummy:args.src".
if name.Str(g.tm) == "dummy" {
@@ -87,7 +87,7 @@ func (g *gen) writeLoadDerivedVar(b *buffer, hack string, name t.ID, typ *a.Type
}
// TODO: also remove this hack.
if hack == "w" {
b.printf("ioptr_w = %sw.ptr + %sw.wi;\n", uPrefix, uPrefix)
b.printf("%s%sw = %sw.ptr + %sw.wi;\n", iopPrefix, vPrefix, uPrefix, uPrefix)
return nil
}
@@ -100,47 +100,48 @@ func (g *gen) writeLoadDerivedVar(b *buffer, hack string, name t.ID, typ *a.Type
if _, ok := g.currFunk.derivedVars[name]; !ok {
return nil
}
nameStr := name.Str(g.tm)
preName := prefix + name.Str(g.tm)
i0, i1 := "ri", "wi"
if typ.QID()[1] == t.IDIOWriter {
i0, i1 = "wi", "len"
}
if header {
b.printf("uint8_t* ioptr_%s = NULL;", nameStr)
b.printf("uint8_t* iobounds0orig_%s = NULL;", nameStr)
b.printf("uint8_t* iobounds1_%s = NULL;", nameStr)
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(iobounds0orig_%s);", nameStr)
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(iobounds1_%s);", nameStr)
b.printf("uint8_t* %s%s = NULL;", iopPrefix, preName)
b.printf("uint8_t* %s%s = NULL;", io0Prefix, preName)
b.printf("uint8_t* %s%s = NULL;", io1Prefix, preName)
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%s%s);", io0Prefix, preName)
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%s%s);", io1Prefix, preName)
}
b.printf("if (%s%s.private_impl.buf) {", aPrefix, nameStr)
b.printf("if (%s.private_impl.buf) {", preName)
b.printf("ioptr_%s = %s%s.private_impl.buf->ptr + %s%s.private_impl.buf->%s;",
nameStr, aPrefix, nameStr, aPrefix, nameStr, i0)
b.printf("%s%s = %s.private_impl.buf->ptr + %s.private_impl.buf->%s;",
iopPrefix, preName, preName, preName, i0)
if header {
b.printf("if (!%s%s.private_impl.bounds[0]) {", aPrefix, nameStr)
b.printf("%s%s.private_impl.bounds[0] = ioptr_%s;", aPrefix, nameStr, nameStr)
b.printf("%s%s.private_impl.bounds[1] = %s%s.private_impl.buf->ptr + %s%s.private_impl.buf->%s;",
aPrefix, nameStr, aPrefix, nameStr, aPrefix, nameStr, i1)
b.printf("if (!%s.private_impl.bounds[0]) {", preName)
b.printf("%s.private_impl.bounds[0] = %s%s;", preName, iopPrefix, preName)
b.printf("%s.private_impl.bounds[1] = %s.private_impl.buf->ptr + %s.private_impl.buf->%s;",
preName, preName, preName, i1)
b.printf("}\n")
if typ.QID()[1] == t.IDIOWriter {
b.printf("if (%s%s.private_impl.buf->closed) {", aPrefix, nameStr)
b.printf("%s%s.private_impl.bounds[1] = ioptr_%s;", aPrefix, nameStr, nameStr)
b.printf("if (%s.private_impl.buf->closed) {", preName)
b.printf("%s.private_impl.bounds[1] = %s%s;", preName, iopPrefix, preName)
b.printf("}\n")
}
b.printf("iobounds0orig_%s = %s%s.private_impl.bounds[0];", nameStr, aPrefix, nameStr)
b.printf("iobounds1_%s = %s%s.private_impl.bounds[1];", nameStr, aPrefix, nameStr)
b.printf("%s%s = %s.private_impl.bounds[0];", io0Prefix, preName, preName)
b.printf("%s%s = %s.private_impl.bounds[1];", io1Prefix, preName, preName)
}
b.printf("}\n")
return nil
}
func (g *gen) writeSaveDerivedVar(b *buffer, hack string, name t.ID, typ *a.TypeExpr) error {
func (g *gen) writeSaveDerivedVar(b *buffer, hack string, prefix string, name t.ID, typ *a.TypeExpr) error {
// TODO: remove this hack. We're picking up the wrong name for "src:r,
// dummy:args.src".
if name.Str(g.tm) == "dummy" {
@@ -148,7 +149,7 @@ func (g *gen) writeSaveDerivedVar(b *buffer, hack string, name t.ID, typ *a.Type
}
// TODO: also remove this hack.
if hack == "w" {
b.printf("%sw.wi = ioptr_w - %sw.ptr;\n", uPrefix, uPrefix)
b.printf("%sw.wi = %s%sw - %sw.ptr;\n", uPrefix, iopPrefix, vPrefix, uPrefix)
return nil
}
@@ -161,15 +162,16 @@ func (g *gen) writeSaveDerivedVar(b *buffer, hack string, name t.ID, typ *a.Type
if _, ok := g.currFunk.derivedVars[name]; !ok {
return nil
}
nameStr := name.Str(g.tm)
preName := prefix + name.Str(g.tm)
i0 := "ri"
if typ.QID()[1] == t.IDIOWriter {
i0 = "wi"
}
b.printf("if (%s%s.private_impl.buf) {", aPrefix, nameStr)
b.printf("%s%s.private_impl.buf->%s = ioptr_%s - %s%s.private_impl.buf->ptr;",
aPrefix, nameStr, i0, nameStr, aPrefix, nameStr)
b.printf("if (%s.private_impl.buf) {", preName)
b.printf("%s.private_impl.buf->%s = %s%s - %s.private_impl.buf->ptr;",
preName, i0, iopPrefix, preName, preName)
b.printf("}\n")
return nil
}
@@ -183,14 +185,16 @@ func (g *gen) writeLoadExprDerivedVars(b *buffer, n *a.Expr) error {
}
for _, o := range n.Args() {
o := o.AsArg()
prefix := aPrefix
// TODO: don't hard-code these.
hack := ""
if s := o.Value().Str(g.tm); s != "args.dst" && s != "args.src" && s != "w" {
continue
} else if s == "w" {
prefix = vPrefix
hack = "w"
}
if err := g.writeLoadDerivedVar(b, hack, o.Name(), o.Value().MType(), false); err != nil {
if err := g.writeLoadDerivedVar(b, hack, prefix, o.Name(), o.Value().MType(), false); err != nil {
return err
}
}
@@ -206,14 +210,16 @@ func (g *gen) writeSaveExprDerivedVars(b *buffer, n *a.Expr) error {
}
for _, o := range n.Args() {
o := o.AsArg()
prefix := aPrefix
// TODO: don't hard-code these.
hack := ""
if s := o.Value().Str(g.tm); s != "args.dst" && s != "args.src" && s != "w" {
continue
} else if s == "w" {
prefix = vPrefix
hack = "w"
}
if err := g.writeSaveDerivedVar(b, hack, o.Name(), o.Value().MType()); err != nil {
if err := g.writeSaveDerivedVar(b, hack, prefix, o.Name(), o.Value().MType()); err != nil {
return err
}
}
@@ -379,12 +385,13 @@ func (g *gen) writeVars(b *buffer, block []*a.Node, skipPointerTypes bool, skipI
b.writes(";\n")
if typ.IsIOType() {
b.printf("wuffs_base__io_buffer %s%s;\n", uPrefix, name)
// TODO: iobounds0_%s or iobounds0orig_%s variables?
b.printf("uint8_t* ioptr_%s = NULL;\n", name)
b.printf("uint8_t* iobounds1_%s = NULL;\n", name)
preName := vPrefix + name
// TODO: io0_etc variables?
b.printf("uint8_t* %s%s = NULL;\n", iopPrefix, preName)
b.printf("uint8_t* %s%s = NULL;\n", io1Prefix, preName)
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%s%s);\n", uPrefix, name)
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(ioptr_%s);\n", name)
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(iobounds1_%s);\n", name)
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%s%s);\n", iopPrefix, preName)
b.printf("WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(%s%s);\n", io1Prefix, preName)
}
return nil
})