<commit_before>#include "sequence.h"
#include "details/ir/isa/printer.inc.hpp"
#include "details/atom/atom.h"
#include <cstdlib>

using namespace Yuni;


namespace ny {
namespace ir {


Sequence::~Sequence() {
	free(m_body);
}


void Sequence::moveCursorFromBlueprintToEnd(const Instruction*& cursor) const {
	assert((*cursor).opcodes[0] == static_cast<uint32_t>(ir::isa::Op::blueprint));
	if ((*cursor).opcodes[0] == static_cast<uint32_t>(ir::isa::Op::blueprint)) {
		// next opcode, which should be blueprint.size
		(cursor)++;
		// getting the size and moving the cursor
		auto& blueprintsize = (*cursor).to<ir::isa::Op::pragma>();
		assert(blueprintsize.opcode == (uint32_t) ir::isa::Op::pragma);
		assert(blueprintsize.value.blueprintsize >= 2);
		cursor += blueprintsize.value.blueprintsize - 2; // -2: blueprint:class+blueprint:size
		assert((*cursor).opcodes[0] == (uint32_t) ir::isa::Op::end);
	}
}


void Sequence::moveCursorFromBlueprintToEnd(Instruction*& cursor) const {
	assert((*cursor).opcodes[0] == static_cast<uint32_t>(ir::isa::Op::blueprint));
	if ((*cursor).opcodes[0] == static_cast<uint32_t>(ir::isa::Op::blueprint)) {
		// next opcode, which should be blueprint.size
		(cursor)++;
		// getting the size and moving the cursor
		auto& blueprintsize = (*cursor).to<ir::isa::Op::pragma>();
		assert(blueprintsize.opcode == (uint32_t) ir::isa::Op::pragma);
		assert(blueprintsize.value.blueprintsize >= 2);
		cursor += blueprintsize.value.blueprintsize - 2; // -2: blueprint:class+blueprint:size
		assert((*cursor).opcodes[0] == (uint32_t) ir::isa::Op::end);
	}
}


void Sequence::clear() {
	m_size = 0;
	stringrefs.clear();
	free(m_body);
	m_capacity = 0;
	m_body = nullptr;
}


void Sequence::grow(uint32_t count) {
	assert(count > 0);
	auto newcapa = m_capacity;
	do {
		newcapa += 1000u;
	}
	while (newcapa < count);
	auto* newbody = (Instruction*) realloc(m_body, sizeof(Instruction) * newcapa);
	if (unlikely(nullptr == newbody))
		throw std::bad_alloc();
	m_body = newbody;
	m_capacity = newcapa;
}


void Sequence::print(YString& out, const AtomMap* atommap, uint32_t offset) const {
	out.reserve(out.size() + (m_size * 100)); // arbitrary
	ir::isa::Printer<String> printer{out, *this};
	printer.atommap = atommap;
	printer.offset = offset;
	each(printer, offset);
}


namespace {


struct WalkerIncreaseLVID final {
	WalkerIncreaseLVID(ir::Sequence& sequence, uint32_t inc, uint32_t greaterThan)
		: greaterThan(greaterThan)
		, inc(inc)
		, sequence(sequence) {
	}

	void visit(ir::isa::Operand<ir::isa::Op::stacksize>& operands) {
		operands.add += inc;
	}

	void visit(ir::isa::Operand<ir::isa::Op::blueprint>& operands) {
		auto kind = static_cast<ir::isa::Blueprint>(operands.kind);
		switch (kind) {
			case ir::isa::Blueprint::funcdef:
			case ir::isa::Blueprint::classdef:
				sequence.moveCursorFromBlueprintToEnd(*cursor);
				break;
			default:
				operands.eachLVID(*this);
				break;
		}
	}

	void visit(ir::isa::Operand<ir::isa::Op::scope>&) {
		++depth;
	}

	void visit(ir::isa::Operand<ir::isa::Op::end>&) {
		if (depth-- == 0)
			sequence.invalidateCursor(*cursor);
	}

	template<ir::isa::Op O> void visit(ir::isa::Operand<O>& operands) {
		// ask to the opcode datatype to update its own lvid
		// (see operator() below)
		operands.eachLVID(*this);
	}

	inline void operator () (uint32_t& lvid) const {
		if (lvid > greaterThan)
			lvid += inc;
	}
	inline void operator () (uint32_t& lvid1, uint32_t& lvid2) const {
		if (lvid1 > greaterThan)
			lvid1 += inc;
		if (lvid2 > greaterThan)
			lvid2 += inc;
	}
	inline void operator () (uint32_t& lvid1, uint32_t& lvid2, uint32_t& lvid3) const {
		if (lvid1 > greaterThan)
			lvid1 += inc;
		if (lvid2 > greaterThan)
			lvid2 += inc;
		if (lvid3 > greaterThan)
			lvid3 += inc;
	}

	uint32_t greaterThan;
	uint32_t inc;
	uint32_t depth = 0;
	Instruction** cursor = nullptr;
	Sequence& sequence;
};


} // anonymous namespace


void Sequence::increaseAllLVID(uint32_t inc, uint32_t greaterThan, uint32_t offset) {
	assert(inc > 0 and "this method should not be called if nothing to do");
	WalkerIncreaseLVID walker{*this, inc, greaterThan};
	each(walker, offset);
}


} // namespace ir
} // namespace ny
<commit_msg>sequence: rename internal variable newCapacity<commit_after>#include "sequence.h"
#include "details/ir/isa/printer.inc.hpp"
#include "details/atom/atom.h"
#include <cstdlib>

using namespace Yuni;


namespace ny {
namespace ir {


Sequence::~Sequence() {
	free(m_body);
}


void Sequence::moveCursorFromBlueprintToEnd(const Instruction*& cursor) const {
	assert((*cursor).opcodes[0] == static_cast<uint32_t>(ir::isa::Op::blueprint));
	if ((*cursor).opcodes[0] == static_cast<uint32_t>(ir::isa::Op::blueprint)) {
		// next opcode, which should be blueprint.size
		(cursor)++;
		// getting the size and moving the cursor
		auto& blueprintsize = (*cursor).to<ir::isa::Op::pragma>();
		assert(blueprintsize.opcode == (uint32_t) ir::isa::Op::pragma);
		assert(blueprintsize.value.blueprintsize >= 2);
		cursor += blueprintsize.value.blueprintsize - 2; // -2: blueprint:class+blueprint:size
		assert((*cursor).opcodes[0] == (uint32_t) ir::isa::Op::end);
	}
}


void Sequence::moveCursorFromBlueprintToEnd(Instruction*& cursor) const {
	assert((*cursor).opcodes[0] == static_cast<uint32_t>(ir::isa::Op::blueprint));
	if ((*cursor).opcodes[0] == static_cast<uint32_t>(ir::isa::Op::blueprint)) {
		// next opcode, which should be blueprint.size
		(cursor)++;
		// getting the size and moving the cursor
		auto& blueprintsize = (*cursor).to<ir::isa::Op::pragma>();
		assert(blueprintsize.opcode == (uint32_t) ir::isa::Op::pragma);
		assert(blueprintsize.value.blueprintsize >= 2);
		cursor += blueprintsize.value.blueprintsize - 2; // -2: blueprint:class+blueprint:size
		assert((*cursor).opcodes[0] == (uint32_t) ir::isa::Op::end);
	}
}


void Sequence::clear() {
	m_size = 0;
	stringrefs.clear();
	free(m_body);
	m_capacity = 0;
	m_body = nullptr;
}


void Sequence::grow(uint32_t count) {
	assert(count > 0);
	auto newCapacity = m_capacity;
	do {
		newCapacity += 1000u;
	}
	while (newCapacity < count);
	auto* newbody = (Instruction*) realloc(m_body, sizeof(Instruction) * newCapacity);
	if (unlikely(nullptr == newbody))
		throw std::bad_alloc();
	m_body = newbody;
	m_capacity = newCapacity;
}


void Sequence::print(YString& out, const AtomMap* atommap, uint32_t offset) const {
	out.reserve(out.size() + (m_size * 100)); // arbitrary
	ir::isa::Printer<String> printer{out, *this};
	printer.atommap = atommap;
	printer.offset = offset;
	each(printer, offset);
}


namespace {


struct WalkerIncreaseLVID final {
	WalkerIncreaseLVID(ir::Sequence& sequence, uint32_t inc, uint32_t greaterThan)
		: greaterThan(greaterThan)
		, inc(inc)
		, sequence(sequence) {
	}

	void visit(ir::isa::Operand<ir::isa::Op::stacksize>& operands) {
		operands.add += inc;
	}

	void visit(ir::isa::Operand<ir::isa::Op::blueprint>& operands) {
		auto kind = static_cast<ir::isa::Blueprint>(operands.kind);
		switch (kind) {
			case ir::isa::Blueprint::funcdef:
			case ir::isa::Blueprint::classdef:
				sequence.moveCursorFromBlueprintToEnd(*cursor);
				break;
			default:
				operands.eachLVID(*this);
				break;
		}
	}

	void visit(ir::isa::Operand<ir::isa::Op::scope>&) {
		++depth;
	}

	void visit(ir::isa::Operand<ir::isa::Op::end>&) {
		if (depth-- == 0)
			sequence.invalidateCursor(*cursor);
	}

	template<ir::isa::Op O> void visit(ir::isa::Operand<O>& operands) {
		// ask to the opcode datatype to update its own lvid
		// (see operator() below)
		operands.eachLVID(*this);
	}

	inline void operator () (uint32_t& lvid) const {
		if (lvid > greaterThan)
			lvid += inc;
	}
	inline void operator () (uint32_t& lvid1, uint32_t& lvid2) const {
		if (lvid1 > greaterThan)
			lvid1 += inc;
		if (lvid2 > greaterThan)
			lvid2 += inc;
	}
	inline void operator () (uint32_t& lvid1, uint32_t& lvid2, uint32_t& lvid3) const {
		if (lvid1 > greaterThan)
			lvid1 += inc;
		if (lvid2 > greaterThan)
			lvid2 += inc;
		if (lvid3 > greaterThan)
			lvid3 += inc;
	}

	uint32_t greaterThan;
	uint32_t inc;
	uint32_t depth = 0;
	Instruction** cursor = nullptr;
	Sequence& sequence;
};


} // anonymous namespace


void Sequence::increaseAllLVID(uint32_t inc, uint32_t greaterThan, uint32_t offset) {
	assert(inc > 0 and "this method should not be called if nothing to do");
	WalkerIncreaseLVID walker{*this, inc, greaterThan};
	each(walker, offset);
}


} // namespace ir
} // namespace ny

{'fraction_non_alphanumeric': 0.16499350419274833, 'fraction_numerical': 0.020786583205385616, 'mean_word_length': 4.9760056457304165, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5514742', 'n_tokens_mistral': 3355, 'n_tokens_neox': 3091, 'n_words': 640}