From fad2ddce68a2f12dbb22153b243157326b1ce218 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 9 Dec 2018 11:15:10 +1100 Subject: [PATCH] Generate less code for foo.since_mark().length() --- cmd/wuffs-c/internal/cgen/builtin.go | 52 +++++++++++++++++---- lang/token/list.go | 30 ++++++------ release/c/wuffs-unsupported-snapshot.h | 64 +++++--------------------- 3 files changed, 70 insertions(+), 76 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/builtin.go b/cmd/wuffs-c/internal/cgen/builtin.go index 47bec71f..d81b8818 100644 --- a/cmd/wuffs-c/internal/cgen/builtin.go +++ b/cmd/wuffs-c/internal/cgen/builtin.go @@ -141,11 +141,17 @@ func (g *gen) writeBuiltinIOReader(b *buffer, recv *a.Expr, method t.ID, args [] 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.mark, "+ - ".len = (size_t)(iop_a_src - %ssrc.private_impl.mark), })", - aPrefix, aPrefix) + case t.IDSinceMark, t.IDSinceMarkLength: + if method == t.IDSinceMark { + b.printf("((wuffs_base__slice_u8){ "+ + ".ptr = %ssrc.private_impl.mark, "+ + ".len = (size_t)(", + aPrefix) + } + b.printf("iop_a_src - %ssrc.private_impl.mark", aPrefix) + if method == t.IDSinceMark { + b.writes("), })") + } return nil case t.IDSkipFast: @@ -223,15 +229,22 @@ func (g *gen) writeBuiltinIOWriter(b *buffer, recv *a.Expr, method t.ID, args [] b.printf("wuffs_base__io_writer__set_mark(&%sdst, iop_a_dst)", aPrefix) return nil - case t.IDSinceMark: + case t.IDSinceMark, t.IDSinceMarkLength: prefix, name := aPrefix, "dst" if recv.Operator() == 0 { prefix, name = vPrefix, recv.Ident().Str(g.tm) } - b.printf("((wuffs_base__slice_u8){ "+ - ".ptr = %s%s.private_impl.mark, "+ - ".len = (size_t)(iop_%s%s - %s%s.private_impl.mark), })", - prefix, name, prefix, name, prefix, name) + + if method == t.IDSinceMark { + b.printf("((wuffs_base__slice_u8){ "+ + ".ptr = %s%s.private_impl.mark, "+ + ".len = (size_t)(", + prefix, name) + } + b.printf("iop_%s%s - %s%s.private_impl.mark", prefix, name, prefix, name) + if method == t.IDSinceMark { + b.writes("), })") + } return nil } @@ -362,6 +375,25 @@ func (g *gen) writeBuiltinSlice(b *buffer, recv *a.Expr, method t.ID, args []*a. return g.writeArgs(b, args, depth) case t.IDLength: + if recv.Operator() == t.IDOpenParen { + if method := recv.LHS().AsExpr(); method.Operator() == t.IDDot && method.Ident() == t.IDSinceMark { + if lhs := method.LHS().AsExpr(); lhs.MType().IsIOType() { + b.writes("((uint64_t)(") + if lhs.MType().QID()[1] == t.IDIOReader { + if err := g.writeBuiltinIOReader(b, lhs, t.IDSinceMarkLength, nil, depth); err != nil { + return err + } + } else { + if err := g.writeBuiltinIOWriter(b, lhs, t.IDSinceMarkLength, nil, depth); err != nil { + return err + } + } + b.writes("))") + return nil + } + } + } + b.writes("((uint64_t)(") if err := g.writeExpr(b, recv, depth); err != nil { return err diff --git a/lang/token/list.go b/lang/token/list.go index 6d623f1c..cfdcbe2c 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -449,13 +449,14 @@ const ( IDDecodeFrameOptions = ID(0x168) - IDCanUndoByte = ID(0x170) - IDPosition = ID(0x171) - IDSetLimit = ID(0x172) - IDSetMark = ID(0x173) - IDSinceMark = ID(0x174) - IDSkip = ID(0x175) - IDSkipFast = ID(0x176) + IDCanUndoByte = ID(0x170) + IDPosition = ID(0x171) + IDSetLimit = ID(0x172) + IDSetMark = ID(0x173) + IDSinceMark = ID(0x174) + IDSinceMarkLength = ID(0x175) + IDSkip = ID(0x176) + IDSkipFast = ID(0x177) IDCopyFromSlice = ID(0x178) IDCopyNFromHistory = ID(0x179) @@ -777,13 +778,14 @@ var builtInsByID = [nBuiltInIDs]string{ IDDecodeFrameOptions: "decode_frame_options", - IDCanUndoByte: "can_undo_byte", - IDPosition: "position", - IDSetLimit: "set_limit", - IDSetMark: "set_mark", - IDSinceMark: "since_mark", - IDSkip: "skip", - IDSkipFast: "skip_fast", + IDCanUndoByte: "can_undo_byte", + IDPosition: "position", + IDSetLimit: "set_limit", + IDSetMark: "set_mark", + IDSinceMark: "since_mark", + IDSinceMarkLength: "since_mark_length", + IDSkip: "skip", + IDSkipFast: "skip_fast", IDCopyFromSlice: "copy_from_slice", IDCopyNFromHistory: "copy_n_from_history", diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index 4a69da4b..cb265f07 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -6445,20 +6445,11 @@ label_0_continue:; v_n_copied = 0; while (true) { if (((uint64_t)((v_dist_minus_1 + 1))) > - ((uint64_t)(((wuffs_base__slice_u8){ - .ptr = a_dst.private_impl.mark, - .len = (size_t)(iop_a_dst - a_dst.private_impl.mark), - }) - .len))) { + ((uint64_t)(iop_a_dst - a_dst.private_impl.mark))) { v_hlen = 0; - v_hdist = ((uint32_t)( - (((uint64_t)((v_dist_minus_1 + 1))) - - ((uint64_t)( - ((wuffs_base__slice_u8){ - .ptr = a_dst.private_impl.mark, - .len = (size_t)(iop_a_dst - a_dst.private_impl.mark), - }) - .len))))); + v_hdist = + ((uint32_t)((((uint64_t)((v_dist_minus_1 + 1))) - + ((uint64_t)(iop_a_dst - a_dst.private_impl.mark))))); if (v_length > v_hdist) { v_length -= v_hdist; v_hlen = v_hdist; @@ -6497,12 +6488,7 @@ label_0_continue:; goto label_0_continue; } if (((uint64_t)((v_dist_minus_1 + 1))) > - ((uint64_t)( - ((wuffs_base__slice_u8){ - .ptr = a_dst.private_impl.mark, - .len = (size_t)(iop_a_dst - a_dst.private_impl.mark), - }) - .len))) { + ((uint64_t)(iop_a_dst - a_dst.private_impl.mark))) { status = wuffs_deflate__error__internal_error_inconsistent_distance; goto exit; } @@ -6858,21 +6844,11 @@ wuffs_deflate__decoder__decode_huffman_slow(wuffs_deflate__decoder* self, v_n_copied = 0; while (true) { if (((uint64_t)((v_dist_minus_1 + 1))) > - ((uint64_t)( - ((wuffs_base__slice_u8){ - .ptr = a_dst.private_impl.mark, - .len = (size_t)(iop_a_dst - a_dst.private_impl.mark), - }) - .len))) { + ((uint64_t)(iop_a_dst - a_dst.private_impl.mark))) { v_hlen = 0; - v_hdist = ((uint32_t)( - (((uint64_t)((v_dist_minus_1 + 1))) - - ((uint64_t)( - ((wuffs_base__slice_u8){ - .ptr = a_dst.private_impl.mark, - .len = (size_t)(iop_a_dst - a_dst.private_impl.mark), - }) - .len))))); + v_hdist = + ((uint32_t)((((uint64_t)((v_dist_minus_1 + 1))) - + ((uint64_t)(iop_a_dst - a_dst.private_impl.mark))))); if (v_length > v_hdist) { v_length -= v_hdist; v_hlen = v_hdist; @@ -9430,22 +9406,12 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self, } v_wi = wuffs_base__u64__sat_add( ((uint64_t)(self->private_impl.f_uncompressed_wi)), - ((uint64_t)( - ((wuffs_base__slice_u8){ - .ptr = v_w.private_impl.mark, - .len = (size_t)(iop_v_w - v_w.private_impl.mark), - }) - .len))); + ((uint64_t)(iop_v_w - v_w.private_impl.mark))); self->private_impl.f_uncompressed_wi = ((uint32_t)(wuffs_base__u64__min(v_wi, 4096))); wuffs_base__u64__sat_sub_indirect( &v_block_size, - ((uint64_t)( - ((wuffs_base__slice_u8){ - .ptr = a_src.private_impl.mark, - .len = (size_t)(iop_a_src - a_src.private_impl.mark), - }) - .len))); + ((uint64_t)(iop_a_src - a_src.private_impl.mark))); a_src = o_1_a_src; } v_w = o_0_v_w; @@ -9991,13 +9957,7 @@ wuffs_gzip__decoder__decode(wuffs_gzip__decoder* self, .len = (size_t)(iop_a_dst - a_dst.private_impl.mark), })); v_decoded_length_got += ((uint32_t)( - (((uint64_t)( - ((wuffs_base__slice_u8){ - .ptr = a_dst.private_impl.mark, - .len = (size_t)(iop_a_dst - a_dst.private_impl.mark), - }) - .len)) & - 4294967295))); + (((uint64_t)(iop_a_dst - a_dst.private_impl.mark)) & 4294967295))); } if (wuffs_base__status__is_ok(v_z)) { goto label_2_break;