fix weight calculation, improve assembly output efficiency, improve logging

This commit is contained in:
2026-11-08 15:03:23 +02:00
parent 81deb637c0
commit e8f3f76117
4 changed files with 47 additions and 12 deletions
+2 -1
View File
@@ -8,7 +8,7 @@
namespace DynamicPipeline {
enum CommandType : char
enum CommandType : uint16_t
{
ADD,
SUB,
@@ -47,6 +47,7 @@ private:
unsigned int pc {0};
unsigned int system_time;
void print_program_memory(void) const;
void print_pipeline(void) const;
bool pipeline_empty(void) const;
bool next_instruction(void) const;
+3 -3
View File
@@ -30,7 +30,7 @@ public:
NOP // not an operator
};
static int weight_op(Operator o)
[[nodiscard]] static int weight_op(Operator o)
{
switch (o)
{
@@ -408,7 +408,7 @@ public:
}
}
int weight()
int weight() const
{
int w = 0;
@@ -424,7 +424,7 @@ public:
if (o.t == Token::OPERATOR)
{
Token::weight_op(o.v.o);
w += Token::weight_op(o.v.o);
}
return w;
+18 -4
View File
@@ -16,11 +16,12 @@ struct Command
int left_index;
OpSrc right_src;
int right_index;
const Expression::TreeNode * node;
Command(DynamicPipeline::CommandType opcode, OpSrc left_src, OpSrc right_src, int left_index, int right_index)
Command(DynamicPipeline::CommandType opcode, OpSrc left_src, OpSrc right_src, int left_index, int right_index, const Expression::TreeNode * const node)
: opcode(opcode), left_src(left_src), left_index(left_index), right_src(right_src), right_index(right_index)
{
this->node = const_cast<const Expression::TreeNode *>(node);
}
};
@@ -60,7 +61,7 @@ int descend_operator(const Expression::TreeNode& node, int& preload_mem, std::ve
ro = PRELOAD;
}
commands.emplace_back((DynamicPipeline::CommandType) (node.o.v.o - 2), lo, ro, left, right);
commands.emplace_back((DynamicPipeline::CommandType) (node.o.v.o - 2), lo, ro, left, right, &node);
return commands.size() - 1; // this index in dynamic memory
}
@@ -91,8 +92,21 @@ void assemble(
for (int i = 0, l = commands.size(); i < l; i++)
{
Command &cmd = commands[i];
int lowest_weight_idx = 0;
for (int j = 0, k = commands.size(); j < k; j++)
{
if (commands[j].node->weight() < commands[lowest_weight_idx].node->weight())
{
lowest_weight_idx = j;
}
}
Command &cmd = commands[lowest_weight_idx];
pr_debug("Selecting command idx=" << lowest_weight_idx << " with weight=" << cmd.node->weight());
program.emplace_back(cmd.opcode, cmd.left_src * preload_mem + cmd.left_index, cmd.right_src * preload_mem + cmd.right_index, preload_mem + i);
commands.erase(commands.begin() + lowest_weight_idx);
}
}
+24 -4
View File
@@ -29,6 +29,21 @@ static const char *op_to_str(DynamicPipeline::CommandType o)
}
}
void System::print_program_memory(void) const
{
pr_info("Program memory dump: " << program_memory->size() * sizeof(struct Command) << " bytes");
pr_info("### cmd op1 op2 res");
for (int i = 0, l = program_memory->size(); i < l; i++)
{
const struct Command &cmd = (*program_memory)[i];
pr_info(std::setw(3) << i << ". " << op_to_str(cmd.t) << ", "
<< std::setw(3) << cmd.op1 << ", "
<< std::setw(3) << cmd.op2 << ", "
<< std::setw(3) << cmd.res);
}
}
void System::init(const std::vector<Command> * const prg, const std::array<double, DYN_PIPELINE_MEM_SIZE>& mem, const int init_mem_size)
{
pr_info("Dynamic Pipeline System initialized with:\n"
@@ -50,6 +65,9 @@ void System::init(const std::vector<Command> * const prg, const std::array<doubl
pc = 0;
system_time = 0;
print_program_memory();
pri_info("\n");
}
void System::print_pipeline(void) const
@@ -60,22 +78,24 @@ void System::print_pipeline(void) const
{
if (layer_cmd[i] != -1)
{
pri_info(std::setw(3) << layer_cmd[i] << " (" << op_to_str((*program_memory)[layer_cmd[i]].t) << ") | ");
pri_info(std::setw(3) << layer_cmd[i] << " (" << op_to_str((*program_memory)[layer_cmd[i]].t) << ") "
<< std::setw(3) << (*program_memory)[layer_cmd[i]].res << " | ");
}
else
{
pri_info("--- (---) | ");
pri_info("--- (---) --- | ");
}
}
if (layer_cmd[DYN_PIPELINE_LAYERS - 1] != -1)
{
int addr = layer_cmd[DYN_PIPELINE_LAYERS - 1];
pri_info(std::setw(3) << addr << " (" << op_to_str((*program_memory)[addr].t) << ")\n");
pri_info(std::setw(3) << addr << " (" << op_to_str((*program_memory)[addr].t) << ") "
<< std::setw(3) << (*program_memory)[addr].res << "\n");
}
else
{
pri_info("--- (---)\n");
pri_info("--- (---) ---\n");
}
}