Skip to content

Commit

Permalink
Revert "Store numerical offset in units instead of iterator for posit…
Browse files Browse the repository at this point in the history
…ion."

This reverts commit 755437a.

# Conflicts:
#	tests/Baseline/spicy.optimization.feature_requirements/noopt.hlt
#	tests/Baseline/spicy.optimization.feature_requirements/opt.hlt

(cherry picked from commit 0a918e3)
  • Loading branch information
bbannier committed Jul 4, 2023
1 parent aff2b9c commit e304c0d
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 241 deletions.
9 changes: 5 additions & 4 deletions spicy/toolchain/src/compiler/codegen/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,13 @@ struct VisitorPass2 : public hilti::visitor::PreOrder<void, VisitorPass2> {
}

result_t operator()(const operator_::unit::Offset& n, position_t p) {
replaceNode(&p, builder::member(n.op0(), ID("__offset")));
auto begin = builder::memberCall(builder::deref(builder::member(n.op0(), ID("__begin"))), "offset", {});
auto cur = builder::memberCall(builder::deref(builder::member(n.op0(), ID("__position"))), "offset", {});
replaceNode(&p, builder::grouping(builder::difference(cur, begin)));
}

result_t operator()(const operator_::unit::Position& n, position_t p) {
replaceNode(&p, builder::sum(builder::deref(builder::member(n.op0(), "__begin")),
builder::member(n.op0(), "__offset")));
replaceNode(&p, builder::member(n.op0(), ID("__position")));
}

result_t operator()(const operator_::unit::Input& n, position_t p) {
Expand All @@ -221,7 +222,7 @@ struct VisitorPass2 : public hilti::visitor::PreOrder<void, VisitorPass2> {

result_t operator()(const operator_::unit::Find& n, position_t p) {
auto begin = builder::deref(builder::member(n.op0(), ID("__begin")));
auto end = builder::sum(begin, builder::member(n.op0(), ID("__offset")));
auto end = builder::deref(builder::member(n.op0(), ID("__position")));
auto needle = argument(n.op2(), 0);
auto direction = argument(n.op2(), 1, builder::id("spicy::Direction::Forward"));
auto i = argument(n.op2(), 2, builder::null());
Expand Down
26 changes: 6 additions & 20 deletions spicy/toolchain/src/compiler/codegen/parser-builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -626,9 +626,7 @@ struct ProductionVisitor
std::optional<Expression> pre_container_offset;
if ( field && field->isContainer() ) {
pre_container_offset =
builder()->addTmp("pre_container_offset",
builder::sum(builder::deref(builder::member(state().self, "__begin")),
builder::member(state().self, "__offset")));
builder()->addTmp("pre_container_offset", builder::member(state().self, "__position"));
}

if ( field && field->convertExpression() ) {
Expand Down Expand Up @@ -722,13 +720,8 @@ struct ProductionVisitor
// elements inside e.g., this unit's fields hooks. Temporarily restore the previously stored offset.
std::optional<Expression> prev;
if ( pre_container_offset ) {
prev = builder()->addTmp("prev", builder::sum(builder::deref(builder::member(state().self, "__begin")),
builder::member(state().self, "__offset")));
builder()->addAssign(builder::member(state().self, "__offset"),
builder::cast(builder::difference(*pre_container_offset,
builder::deref(
builder::member(state().self, "__begin"))),
type::UnsignedInteger(64)));
prev = builder()->addTmp("prev", builder::member(state().self, "__position"));
builder()->addAssign(builder::member(state().self, "__position"), *pre_container_offset);
}


Expand Down Expand Up @@ -809,10 +802,7 @@ struct ProductionVisitor
popState();

if ( prev )
builder()->addAssign(builder::member(state().self, "__offset"),
builder::cast(builder::difference(*prev, builder::deref(builder::member(state().self,
"__begin"))),
type::UnsignedInteger(64)));
builder()->addAssign(builder::member(state().self, "__position"), *prev);

if ( field->condition() )
popBuilder();
Expand Down Expand Up @@ -2271,7 +2261,7 @@ void ParserBuilder::initializeUnit(const Location& l) {
guardFeatureCode(unit, {"uses_random_access"}, [&]() {
// Save the current input offset for the raw access methods.
builder()->addAssign(builder::member(state().self, ID("__begin")), builder::begin(state().cur));
builder()->addAssign(builder::member(state().self, ID("__offset")), builder::integer(0));
builder()->addAssign(builder::member(state().self, ID("__position")), builder::begin(state().cur));
});

beforeHook();
Expand Down Expand Up @@ -2431,11 +2421,7 @@ void ParserBuilder::afterHook() {
void ParserBuilder::saveParsePosition() {
const auto& unit = state().unit.get();
guardFeatureCode(unit, {"uses_random_access"}, [&]() {
builder()->addAssign(builder::member(state().self, ID("__offset")),
builder::difference(builder::memberCall(builder::begin(state().cur), "offset", {}),
builder::memberCall(builder::deref(
builder::member(state().self, "__begin")),
"offset", {})));
builder()->addAssign(builder::member(state().self, ID("__position")), builder::begin(state().cur));
});
}

Expand Down
9 changes: 5 additions & 4 deletions spicy/toolchain/src/compiler/codegen/unit-builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Type CodeGen::compileUnit(const type::Unit& unit, bool declare_only) {

add_hook("0x25_init", {});
add_hook("0x25_done", {});
add_hook("0x25_error", {builder::parameter("__except", type::String())});
add_hook("0x25_error", { builder::parameter("__except", type::String()) });
add_hook("0x25_print", {});
add_hook("0x25_finally", {});

Expand Down Expand Up @@ -202,7 +202,7 @@ Type CodeGen::compileUnit(const type::Unit& unit, bool declare_only) {
auto attr_random_access = Attribute("&needed-by-feature", builder::string("uses_random_access"));
auto f1 = hilti::declaration::Field(ID("__begin"), hilti::type::Optional(hilti::type::stream::Iterator()),
AttributeSet({Attribute("&internal"), attr_random_access}));
auto f2 = hilti::declaration::Field(ID("__offset"), hilti::type::UnsignedInteger(64),
auto f2 = hilti::declaration::Field(ID("__position"), hilti::type::Optional(hilti::type::stream::Iterator()),
AttributeSet({Attribute("&internal"), attr_random_access}));
auto f3 = hilti::declaration::Field(ID("__position_update"), hilti::type::Optional(hilti::type::stream::Iterator()),
AttributeSet({Attribute("&internal"), attr_random_access}));
Expand Down Expand Up @@ -343,8 +343,9 @@ Type CodeGen::compileUnit(const type::Unit& unit, bool declare_only) {
_pb.builder()->addAssign(builder::id(ID(*unit.id(), "__parser")), parser);

_pb.builder()->addExpression(
builder::call("spicy_rt::registerParser", {builder::id(ID(*unit.id(), "__parser")), builder::scope(),
builder::strong_reference(unit)}));
builder::call("spicy_rt::registerParser",
{builder::id(ID(*unit.id(), "__parser")), builder::scope(),
builder::strong_reference(unit)}));
});

auto block = _pb.popBuilder()->block();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,18 @@
[debug/optimizer] removing field for unused method foo::__register_foo_P2::<anon-struct-9>::parse3
[debug/optimizer] removing unused member 'foo::P0::__begin'
[debug/optimizer] removing unused member 'foo::P0::__filters'
[debug/optimizer] removing unused member 'foo::P0::__offset'
[debug/optimizer] removing unused member 'foo::P0::__parser'
[debug/optimizer] removing unused member 'foo::P0::__position'
[debug/optimizer] removing unused member 'foo::P0::__position_update'
[debug/optimizer] removing unused member 'foo::P0::__sink'
[debug/optimizer] removing unused member 'foo::P1::__begin'
[debug/optimizer] removing unused member 'foo::P1::__filters'
[debug/optimizer] removing unused member 'foo::P1::__offset'
[debug/optimizer] removing unused member 'foo::P1::__position'
[debug/optimizer] removing unused member 'foo::P1::__position_update'
[debug/optimizer] removing unused member 'foo::P1::__sink'
[debug/optimizer] removing unused member 'foo::P2::__begin'
[debug/optimizer] removing unused member 'foo::P2::__filters'
[debug/optimizer] removing unused member 'foo::P2::__offset'
[debug/optimizer] removing unused member 'foo::P2::__position'
[debug/optimizer] removing unused member 'foo::P2::__position_update'
[debug/optimizer] removing unused member 'foo::P2::__sink'
[debug/optimizer] removing unused member 'spicy_rt::Sink::connect_mime_type'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import hilti;

type P0 = struct {
optional<iterator<stream>> __begin &internal &needed-by-feature="uses_random_access";
uint<64> __offset &internal &needed-by-feature="uses_random_access";
optional<iterator<stream>> __position &internal &needed-by-feature="uses_random_access";
optional<iterator<stream>> __position_update &internal &needed-by-feature="uses_random_access";
spicy_rt::Parser __parser &static &internal &needed-by-feature="supports_filters" &needed-by-feature="supports_sinks";
spicy_rt::SinkState __sink &internal &needed-by-feature="supports_sinks";
Expand All @@ -32,7 +32,7 @@ type P0 = struct {
} &on-heap;
public type P1 = struct {
optional<iterator<stream>> __begin &internal &needed-by-feature="uses_random_access";
uint<64> __offset &internal &needed-by-feature="uses_random_access";
optional<iterator<stream>> __position &internal &needed-by-feature="uses_random_access";
optional<iterator<stream>> __position_update &internal &needed-by-feature="uses_random_access";
spicy_rt::Parser __parser &static &internal &needed-by-feature="supports_filters" &always-emit;
spicy_rt::SinkState __sink &internal &needed-by-feature="supports_sinks";
Expand Down Expand Up @@ -60,7 +60,7 @@ public type P2 = struct {
uint<8> x &optional;
uint<8> y &optional;
optional<iterator<stream>> __begin &internal &needed-by-feature="uses_random_access";
uint<64> __offset &internal &needed-by-feature="uses_random_access";
optional<iterator<stream>> __position &internal &needed-by-feature="uses_random_access";
optional<iterator<stream>> __position_update &internal &needed-by-feature="uses_random_access";
spicy_rt::Parser __parser &static &internal &needed-by-feature="supports_filters" &always-emit;
spicy_rt::SinkState __sink &internal &needed-by-feature="supports_sinks";
Expand Down Expand Up @@ -115,7 +115,7 @@ method method tuple<view<stream>, int<64>, iterator<stream>, optional<hilti::Rec

if ( __feat%foo__P0%uses_random_access ) {
(*self).__begin = begin(__cur);
(*self).__offset = 0;
(*self).__position = begin(__cur);
}

(*self).__error = __error;
Expand Down Expand Up @@ -158,7 +158,7 @@ method method tuple<view<stream>, int<64>, iterator<stream>, optional<hilti::Rec
catch ( hilti::SystemException __except ) {

if ( __feat%foo__P0%uses_random_access )
(*self).__offset = begin(__cur).offset() - (*(*self).__begin).offset();
(*self).__position = begin(__cur);

(*self).__on_0x25_error(hilti::exception_what(__except));

Expand Down Expand Up @@ -208,7 +208,7 @@ method method tuple<view<stream>, int<64>, iterator<stream>, optional<hilti::Rec
local tuple<view<stream>, int<64>, iterator<stream>, optional<hilti::RecoverableFailure>> __result;

if ( __feat%foo__P0%uses_random_access )
(*self).__offset = begin(__cur).offset() - (*(*self).__begin).offset();
(*self).__position = begin(__cur);

(*self).__error = __error;

Expand Down Expand Up @@ -249,7 +249,7 @@ method extern method view<stream> foo::P0::parse1(inout value_ref<stream> data,
# End parsing production: Unit: foo_P0 ->

if ( __feat%foo__P0%uses_random_access )
(*unit).__offset = begin(ncur).offset() - (*(*unit).__begin).offset();
(*unit).__position = begin(ncur);


if ( error )
Expand All @@ -273,7 +273,7 @@ method extern method view<stream> foo::P0::parse3(inout value_ref<spicy_rt::Pars
# End parsing production: Unit: foo_P0 ->

if ( __feat%foo__P0%uses_random_access )
(*unit).__offset = begin(ncur).offset() - (*(*unit).__begin).offset();
(*unit).__position = begin(ncur);


if ( error )
Expand All @@ -295,7 +295,7 @@ method extern method view<stream> foo::P0::parse2(inout value_ref<P0> unit, inou
# End parsing production: Unit: foo_P0 ->

if ( __feat%foo__P0%uses_random_access )
(*unit).__offset = begin(ncur).offset() - (*(*unit).__begin).offset();
(*unit).__position = begin(ncur);


if ( error )
Expand Down Expand Up @@ -325,7 +325,7 @@ method method tuple<view<stream>, int<64>, iterator<stream>, optional<hilti::Rec

if ( __feat%foo__P1%uses_random_access ) {
(*self).__begin = begin(__cur);
(*self).__offset = 0;
(*self).__position = begin(__cur);
}

(*self).__error = __error;
Expand Down Expand Up @@ -368,7 +368,7 @@ method method tuple<view<stream>, int<64>, iterator<stream>, optional<hilti::Rec
catch ( hilti::SystemException __except ) {

if ( __feat%foo__P1%uses_random_access )
(*self).__offset = begin(__cur).offset() - (*(*self).__begin).offset();
(*self).__position = begin(__cur);

(*self).__on_0x25_error(hilti::exception_what(__except));

Expand Down Expand Up @@ -418,7 +418,7 @@ method method tuple<view<stream>, int<64>, iterator<stream>, optional<hilti::Rec
local tuple<view<stream>, int<64>, iterator<stream>, optional<hilti::RecoverableFailure>> __result;

if ( __feat%foo__P1%uses_random_access )
(*self).__offset = begin(__cur).offset() - (*(*self).__begin).offset();
(*self).__position = begin(__cur);

(*self).__error = __error;

Expand Down Expand Up @@ -459,7 +459,7 @@ method extern method view<stream> foo::P1::parse1(inout value_ref<stream> data,
# End parsing production: Unit: foo_P1 ->

if ( __feat%foo__P1%uses_random_access )
(*unit).__offset = begin(ncur).offset() - (*(*unit).__begin).offset();
(*unit).__position = begin(ncur);


if ( error )
Expand All @@ -483,7 +483,7 @@ method extern method view<stream> foo::P1::parse3(inout value_ref<spicy_rt::Pars
# End parsing production: Unit: foo_P1 ->

if ( __feat%foo__P1%uses_random_access )
(*unit).__offset = begin(ncur).offset() - (*(*unit).__begin).offset();
(*unit).__position = begin(ncur);


if ( error )
Expand All @@ -505,7 +505,7 @@ method extern method view<stream> foo::P1::parse2(inout value_ref<P1> unit, inou
# End parsing production: Unit: foo_P1 ->

if ( __feat%foo__P1%uses_random_access )
(*unit).__offset = begin(ncur).offset() - (*(*unit).__begin).offset();
(*unit).__position = begin(ncur);


if ( error )
Expand Down Expand Up @@ -537,7 +537,7 @@ method method tuple<view<stream>, int<64>, iterator<stream>, optional<hilti::Rec

if ( __feat%foo__P2%uses_random_access ) {
(*self).__begin = begin(__cur);
(*self).__offset = 0;
(*self).__position = begin(__cur);
}

(*self).__error = __error;
Expand Down Expand Up @@ -580,7 +580,7 @@ method method tuple<view<stream>, int<64>, iterator<stream>, optional<hilti::Rec
catch ( hilti::SystemException __except ) {

if ( __feat%foo__P2%uses_random_access )
(*self).__offset = begin(__cur).offset() - (*(*self).__begin).offset();
(*self).__position = begin(__cur);

(*self).__on_0x25_error(hilti::exception_what(__except));

Expand Down Expand Up @@ -657,7 +657,7 @@ method method tuple<view<stream>, int<64>, iterator<stream>, optional<hilti::Rec
__error = (*self).__error;

if ( __feat%foo__P2%uses_random_access )
(*self).__offset = begin(__cur).offset() - (*(*self).__begin).offset();
(*self).__position = begin(__cur);

# "<...>/default-parser-functions.spicy:18:8"

Expand Down Expand Up @@ -688,11 +688,11 @@ method method tuple<view<stream>, int<64>, iterator<stream>, optional<hilti::Rec
__error = (*self).__error;

if ( __feat%foo__P2%uses_random_access )
(*self).__offset = begin(__cur).offset() - (*(*self).__begin).offset();
(*self).__position = begin(__cur);


if ( __feat%foo__P2%uses_random_access )
(*self).__offset = begin(__cur).offset() - (*(*self).__begin).offset();
(*self).__position = begin(__cur);

(*self).__error = __error;

Expand Down Expand Up @@ -733,7 +733,7 @@ method extern method view<stream> foo::P2::parse1(inout value_ref<stream> data,
# End parsing production: Unit: foo_P2 -> x y

if ( __feat%foo__P2%uses_random_access )
(*unit).__offset = begin(ncur).offset() - (*(*unit).__begin).offset();
(*unit).__position = begin(ncur);


if ( error )
Expand All @@ -757,7 +757,7 @@ method extern method view<stream> foo::P2::parse3(inout value_ref<spicy_rt::Pars
# End parsing production: Unit: foo_P2 -> x y

if ( __feat%foo__P2%uses_random_access )
(*unit).__offset = begin(ncur).offset() - (*(*unit).__begin).offset();
(*unit).__position = begin(ncur);


if ( error )
Expand All @@ -779,7 +779,7 @@ method extern method view<stream> foo::P2::parse2(inout value_ref<P2> unit, inou
# End parsing production: Unit: foo_P2 -> x y

if ( __feat%foo__P2%uses_random_access )
(*unit).__offset = begin(ncur).offset() - (*(*unit).__begin).offset();
(*unit).__position = begin(ncur);


if ( error )
Expand Down
10 changes: 5 additions & 5 deletions tests/Baseline/spicy.optimization.feature_requirements/log
Original file line number Diff line number Diff line change
Expand Up @@ -325,29 +325,29 @@
[debug/optimizer] removing unused member 'foo::X1::__sink'
[debug/optimizer] removing unused member 'foo::X2::__begin'
[debug/optimizer] removing unused member 'foo::X2::__filters'
[debug/optimizer] removing unused member 'foo::X2::__offset'
[debug/optimizer] removing unused member 'foo::X2::__parser'
[debug/optimizer] removing unused member 'foo::X2::__position'
[debug/optimizer] removing unused member 'foo::X2::__position_update'
[debug/optimizer] removing unused member 'foo::X2::__sink'
[debug/optimizer] removing unused member 'foo::X3::__begin'
[debug/optimizer] removing unused member 'foo::X3::__filters'
[debug/optimizer] removing unused member 'foo::X3::__forward'
[debug/optimizer] removing unused member 'foo::X3::__offset'
[debug/optimizer] removing unused member 'foo::X3::__parser'
[debug/optimizer] removing unused member 'foo::X3::__position'
[debug/optimizer] removing unused member 'foo::X3::__position_update'
[debug/optimizer] removing unused member 'foo::X3::__sink'
[debug/optimizer] removing unused member 'foo::X4::__begin'
[debug/optimizer] removing unused member 'foo::X4::__filters'
[debug/optimizer] removing unused member 'foo::X4::__offset'
[debug/optimizer] removing unused member 'foo::X4::__position'
[debug/optimizer] removing unused member 'foo::X4::__position_update'
[debug/optimizer] removing unused member 'foo::X4::__sink'
[debug/optimizer] removing unused member 'foo::X5::__begin'
[debug/optimizer] removing unused member 'foo::X5::__offset'
[debug/optimizer] removing unused member 'foo::X5::__position'
[debug/optimizer] removing unused member 'foo::X5::__position_update'
[debug/optimizer] removing unused member 'foo::X5::__sink'
[debug/optimizer] removing unused member 'foo::X6::__begin'
[debug/optimizer] removing unused member 'foo::X6::__filters'
[debug/optimizer] removing unused member 'foo::X6::__offset'
[debug/optimizer] removing unused member 'foo::X6::__position'
[debug/optimizer] removing unused member 'foo::X6::__position_update'
[debug/optimizer] removing unused member 'spicy_rt::Sink::connect_mime_type'
[debug/optimizer] removing unused member 'spicy_rt::Sink::connect_mime_type'
Expand Down
Loading

0 comments on commit e304c0d

Please sign in to comment.