diff --git a/inc/dyn-pipeline.hpp b/inc/dyn-pipeline.hpp index ba6267c..42a3cf9 100644 --- a/inc/dyn-pipeline.hpp +++ b/inc/dyn-pipeline.hpp @@ -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; diff --git a/inc/expression.hpp b/inc/expression.hpp index 7aa7e60..4689f7c 100644 --- a/inc/expression.hpp +++ b/inc/expression.hpp @@ -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; diff --git a/src/assembler.cpp b/src/assembler.cpp index ad8f1cf..2219da3 100644 --- a/src/assembler.cpp +++ b/src/assembler.cpp @@ -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(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); } } diff --git a/src/dyn-pipeline.cpp b/src/dyn-pipeline.cpp index 3ed98b7..78afea0 100644 --- a/src/dyn-pipeline.cpp +++ b/src/dyn-pipeline.cpp @@ -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 * const prg, const std::array& mem, const int init_mem_size) { pr_info("Dynamic Pipeline System initialized with:\n" @@ -50,6 +65,9 @@ void System::init(const std::vector * const prg, const std::array