diff --git a/src/rose/program_runtime.h b/src/rose/program_runtime.h index 7172f6aa..d67c307f 100644 --- a/src/rose/program_runtime.h +++ b/src/rose/program_runtime.h @@ -103,7 +103,7 @@ void rosePushDelayedMatch(const struct RoseEngine *t, static rose_inline void recordAnchoredLiteralMatch(const struct RoseEngine *t, - struct hs_scratch *scratch, u32 literal_id, + struct hs_scratch *scratch, u32 anch_id, u64a end) { assert(end); @@ -113,7 +113,7 @@ void recordAnchoredLiteralMatch(const struct RoseEngine *t, struct fatbit **anchoredLiteralRows = getAnchoredLiteralLog(scratch); - DEBUG_PRINTF("record %u @ %llu\n", literal_id, end); + DEBUG_PRINTF("record %u (of %u) @ %llu\n", anch_id, t->anchored_count, end); if (!bf64_set(&scratch->al_log_sum, end - 1)) { // first time, clear row @@ -121,11 +121,8 @@ void recordAnchoredLiteralMatch(const struct RoseEngine *t, fatbit_clear(anchoredLiteralRows[end - 1]); } - u32 rel_idx = literal_id - t->anchored_base_id; - DEBUG_PRINTF("record %u @ %llu index %u/%u\n", literal_id, end, rel_idx, - t->anchored_count); - assert(rel_idx < t->anchored_count); - fatbit_set(anchoredLiteralRows[end - 1], t->anchored_count, rel_idx); + assert(anch_id < t->anchored_count); + fatbit_set(anchoredLiteralRows[end - 1], t->anchored_count, anch_id); } static rose_inline diff --git a/src/rose/rose_build_bytecode.cpp b/src/rose/rose_build_bytecode.cpp index da0195e9..c050e683 100644 --- a/src/rose/rose_build_bytecode.cpp +++ b/src/rose/rose_build_bytecode.cpp @@ -246,6 +246,9 @@ struct build_context : boost::noncopyable { /** \brief Mapping from final ID to the set of literals it is used for. */ map> final_id_to_literal; + + /** \brief Mapping from final ID to anchored program index. */ + map anchored_programs; }; /** \brief subengine info including built engine and @@ -4269,7 +4272,14 @@ void makeRecordAnchoredInstruction(const RoseBuildImpl &build, return; } - program.add_before_end(make_unique(final_id)); + auto it = bc.anchored_programs.find(final_id); + if (it == bc.anchored_programs.end()) { + u32 anch_id = verify_u32(bc.anchored_programs.size()); + it = bc.anchored_programs.emplace(final_id, anch_id).first; + DEBUG_PRINTF("added anch_id=%u for final_id %u\n", anch_id, final_id); + } + u32 anch_id = it->second; + program.add_before_end(make_unique(anch_id)); } static @@ -4757,11 +4767,14 @@ u32 buildAnchoredPrograms(RoseBuildImpl &build, build_context &bc) { auto lit_edge_map = findEdgesByLiteral(build); vector programs; + programs.resize(bc.anchored_programs.size(), ROSE_INVALID_PROG_OFFSET); - for (u32 final_id = build.anchored_base_id; - final_id < build.delay_base_id; final_id++) { + for (const auto &m : bc.anchored_programs) { + u32 final_id = m.first; + u32 anch_id = m.second; u32 offset = writeLiteralProgram(build, bc, {final_id}, lit_edge_map); - programs.push_back(offset); + DEBUG_PRINTF("final_id %u -> anch prog at %u\n", final_id, offset); + programs[anch_id] = offset; } DEBUG_PRINTF("%zu anchored programs\n", programs.size()); @@ -5704,7 +5717,7 @@ aligned_unique_ptr RoseBuildImpl::buildFinalEngine(u32 minWidth) { engine->delay_fatbit_size = fatbit_size(engine->delay_count); engine->delay_base_id = delay_base_id; engine->anchored_base_id = anchored_base_id; - engine->anchored_count = delay_base_id - anchored_base_id; + engine->anchored_count = bc.anchored_programs.size(); engine->anchored_fatbit_size = fatbit_size(engine->anchored_count); engine->rosePrefixCount = rosePrefixCount;