4 Commits
12 changed files with 477 additions and 8 deletions
+19
View File
@@ -26,6 +26,23 @@ target_include_directories(syntaxer PRIVATE
${GLOBAL_INC}
)
add_library(dyn-pipeline STATIC)
target_sources(dyn-pipeline PRIVATE
src/dyn-pipeline.cpp
)
target_include_directories(dyn-pipeline PRIVATE
${GLOBAL_INC}
)
add_library(assembler STATIC)
target_sources(assembler PRIVATE
src/assembler.cpp
)
target_include_directories(assembler PRIVATE
${GLOBAL_INC}
)
add_executable(tester)
target_sources(tester PRIVATE
src/test.cpp
@@ -51,4 +68,6 @@ target_include_directories(main PRIVATE
target_link_libraries(main PRIVATE
parser
syntaxer
assembler
dyn-pipeline
)
+5 -1
View File
@@ -1,9 +1,13 @@
#pragma once
#define DEBUG 1
#define PRINT_DEBUG 1
#define PRINT_INFO 1
#define PRINT_DISABLE_PREFIXES 1
#define MAX_TOKEN_STRING_LEN 256
#define LAB2_EXCLUDE_COMPTIME_OP_WRITES 1
#define LAB2_PREVENT_NON_FOLDED_DIV_BY_ZERO 1
#define DYN_PIPELINE_MEM_SIZE 512
#define DYN_PIPELINE_LAYERS 3
+16
View File
@@ -0,0 +1,16 @@
#pragma once
#include <vector>
#include "expression.hpp"
#include "dyn-pipeline.hpp"
namespace Assembler {
void assemble(
const Expression::TreeNode& root,
std::vector<DynamicPipeline::Command>& program,
std::array<double, DYN_PIPELINE_MEM_SIZE>& memory,
int& mem_used);
}
+61
View File
@@ -0,0 +1,61 @@
#pragma once
#include <array>
#include <vector>
#include <cstdint>
#include "config.h"
namespace DynamicPipeline {
enum CommandType : char
{
ADD,
SUB,
MUL,
DIV,
POW,
CMD_COUNT
};
struct Command
{
enum CommandType t;
uint16_t op1;
uint16_t op2;
uint16_t res;
Command(enum CommandType t, uint16_t op1, uint16_t op2, uint16_t res)
: t(t), op1(op1), op2(op2), res(res)
{
}
};
struct MemCell
{
double v;
int ready;
};
class System
{
private:
std::vector<Command> *program_memory;
std::array<MemCell, DYN_PIPELINE_MEM_SIZE> data_memory;
std::array<int, DYN_PIPELINE_LAYERS> layer_cmd {-1};
unsigned int pc {0};
unsigned int system_time;
void print_pipeline(void) const;
bool pipeline_empty(void) const;
bool next_instruction(void) const;
bool instruction_ready(void) const;
void step(void);
public:
void init(const std::vector<Command> * const prg, const std::array<double, DYN_PIPELINE_MEM_SIZE>& mem, const int init_mem_size);
void run(void);
};
}
+52 -2
View File
@@ -5,6 +5,8 @@
#include <memory>
#include <string>
#include "print.h"
namespace Expression {
class Token {
@@ -24,9 +26,35 @@ public:
SUB,
MUL,
DIV,
POW
POW,
NOP // not an operator
};
static int weight_op(Operator o)
{
switch (o)
{
case Expression::Token::OPA:
case Expression::Token::CPA:
case Expression::Token::NOP:
pr_warn("Unweightable operator - this is probably a bug!");
return 0;
case Expression::Token::ADD:
return 1;
case Expression::Token::SUB:
return 1;
case Expression::Token::MUL:
return 2;
case Expression::Token::DIV:
return 2;
case Expression::Token::POW:
return 3;
default:
pr_warn("Impossible operator - this is a bug!");
return 0;
}
}
union TokenValue {
std::shared_ptr<std::string> s;
double d;
@@ -358,7 +386,7 @@ public:
// if value, left and right hands should be null
explicit TreeNode(void)
: l(nullptr), r(nullptr), o()
: l(nullptr), r(nullptr), o(Token::NOP)
{
}
@@ -379,6 +407,28 @@ public:
delete r;
}
}
int weight()
{
int w = 0;
if (l != nullptr)
{
w += l->weight();
}
if (r != nullptr)
{
w += r->weight();
}
if (o.t == Token::OPERATOR)
{
Token::weight_op(o.v.o);
}
return w;
}
};
}
+18 -1
View File
@@ -1,9 +1,10 @@
#pragma once
#include <iostream>
#include <iomanip>
#include "config.h"
#if DEBUG == 1
#if PRINT_DEBUG == 1
#if PRINT_DISABLE_PREFIXES == 1
#define pr_debug(...) std::cout << "debug: " << __VA_ARGS__ << "\n"
#define pri_debug(...) std::cout << __VA_ARGS__
@@ -19,6 +20,22 @@
#define prs_debug(...) do {} while (0)
#endif
#if PRINT_INFO == 1
#if PRINT_DISABLE_PREFIXES == 1
#define pr_info(...) std::cout << "info: " << __VA_ARGS__ << "\n"
#define pri_info(...) std::cout << __VA_ARGS__
#define prs_info(...) std::cout << "info: " << __VA_ARGS__
#else
#define pr_info(...) std::cout << "[" << __FILE__ << ":" << __LINE__ << " " << __func__ << "] info: " << __VA_ARGS__ << "\n"
#define pri_info(...) std::cout << __VA_ARGS__
#define prs_info(...) std::cout << "[" << __FILE__ << ":" << __LINE__ << " " << __func__ << "] info: " << __VA_ARGS__
#endif
#else
#define pr_info(...) do {} while (0)
#define pri_info(...) do {} while (0)
#define prs_info(...) do {} while (0)
#endif
#if PRINT_DISABLE_PREFIXES == 1
#define pr_warn(...) std::cout << "warning: " << __VA_ARGS__ << "\n"
#else
+2 -2
View File
@@ -8,7 +8,7 @@ namespace Syntax
int prepare_token_vector(std::vector<Expression::Token>& tv);
std::vector<Expression::Operation> build_op_vector(const std::vector<Expression::Token>& tv);
Expression::TreeNode ov_to_tree(const std::vector<Expression::Token> tv, const std::vector<Expression::Operation>& ov);
void print_op_tree(const std::vector<Expression::Token> tv, const std::vector<Expression::Operation>& ov);
Expression::TreeNode ov_to_tree(const std::vector<Expression::Token>& tv, const std::vector<Expression::Operation>& ov);
void print_op_tree(const std::vector<Expression::Token>& tv, const std::vector<Expression::Operation>& ov);
}
+99
View File
@@ -0,0 +1,99 @@
#include "assembler.hpp"
namespace Assembler {
// operand source
enum OpSrc
{
PRELOAD, // static memory
DYNAMIC // dynamic memory
};
struct Command
{
DynamicPipeline::CommandType opcode;
OpSrc left_src;
int left_index;
OpSrc right_src;
int right_index;
Command(DynamicPipeline::CommandType opcode, OpSrc left_src, OpSrc right_src, int left_index, int right_index)
: opcode(opcode), left_src(left_src), left_index(left_index), right_src(right_src), right_index(right_index)
{
}
};
int descend_var(const Expression::TreeNode& node, int& preload_mem)
{
(void) node;
preload_mem++;
return preload_mem - 1; // this index in preload memory
}
int descend_operator(const Expression::TreeNode& node, int& preload_mem, std::vector<Command>& commands)
{
int left, right;
OpSrc lo, ro;
if (node.l->o.t == Expression::Token::OPERATOR)
{
left = descend_operator(*node.l, preload_mem, commands);
lo = DYNAMIC;
}
else
{
left = descend_var(*node.l, preload_mem);
lo = PRELOAD;
}
if (node.r->o.t == Expression::Token::OPERATOR)
{
right = descend_operator(*node.r, preload_mem, commands);
ro = DYNAMIC;
}
else
{
right = descend_var(*node.r, preload_mem);
ro = PRELOAD;
}
commands.emplace_back((DynamicPipeline::CommandType) (node.o.v.o - 2), lo, ro, left, right);
return commands.size() - 1; // this index in dynamic memory
}
void assemble(
const Expression::TreeNode& root,
std::vector<DynamicPipeline::Command>& program,
std::array<double, DYN_PIPELINE_MEM_SIZE>& memory,
int& mem_used)
{
(void) memory;
int preload_mem = 0;
std::vector<Command> commands;
if (root.o.t == Expression::Token::OPERATOR)
{
(void) descend_operator(root, preload_mem, commands);
}
else
{
(void) descend_var(root, preload_mem);
}
mem_used = preload_mem;
program.reserve(commands.size());
for (int i = 0, l = commands.size(); i < l; i++)
{
Command &cmd = commands[i];
program.emplace_back(cmd.opcode, cmd.left_src * preload_mem + cmd.left_index, cmd.right_src * preload_mem + cmd.right_index, preload_mem + i);
}
}
}
View File
+188
View File
@@ -0,0 +1,188 @@
#include "dyn-pipeline.hpp"
#include "print.h"
#include "expression.hpp"
namespace DynamicPipeline {
static int weight_op(DynamicPipeline::CommandType o)
{
return Expression::Token::weight_op(static_cast<Expression::Token::Operator>(o + 2));
}
static const char *op_to_str(DynamicPipeline::CommandType o)
{
switch (o)
{
case ADD:
return "ADD";
case SUB:
return "SUB";
case MUL:
return "MUL";
case DIV:
return "DIV";
case POW:
return "POW";
default:
pr_warn("Impossible token - this is a bug!");
return "Impossible token";
}
}
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"
<< prg->size() << " commands in program memory\n"
<< init_mem_size << " values in data memory");
program_memory = const_cast<std::vector<Command> *>(prg);
for (int i = 0; i < init_mem_size; i++)
{
data_memory[i].v = mem[i];
data_memory[i].ready = 1;
}
for (int i = 0; i < DYN_PIPELINE_LAYERS; i++)
{
layer_cmd[i] = -1;
}
pc = 0;
system_time = 0;
}
void System::print_pipeline(void) const
{
prs_info(" ");
for (int i = 0; i < DYN_PIPELINE_LAYERS - 1; i++)
{
if (layer_cmd[i] != -1)
{
pri_info(std::setw(3) << layer_cmd[i] << " (" << op_to_str((*program_memory)[layer_cmd[i]].t) << ") | ");
}
else
{
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");
}
else
{
pri_info("--- (---)\n");
}
}
bool System::pipeline_empty(void) const
{
for (int i = 0; i < DYN_PIPELINE_LAYERS; i++)
{
if (layer_cmd[i] != -1)
{
return false;
}
}
return true;
}
bool System::next_instruction(void) const
{
return pc < program_memory->size();
}
bool System::instruction_ready(void) const
{
if (!next_instruction())
{
return false;
}
Command &cmd = (*program_memory)[pc];
if (!data_memory[cmd.op1].ready)
{
return false;
}
if (!data_memory[cmd.op2].ready)
{
return false;
}
return true;
}
void System::step(void)
{
pr_info("Step start time: " << system_time << " ticks");
// count time
int time_taken = 1;
for (int i = 0; i < DYN_PIPELINE_LAYERS; i++)
{
if (layer_cmd[i] == -1)
{
continue;
}
if (const int d = weight_op((*program_memory)[layer_cmd[i]].t); d > time_taken)
{
time_taken = d;
}
}
system_time += time_taken;
// handle output
if (int prg_mem_addr = layer_cmd[DYN_PIPELINE_LAYERS - 1]; prg_mem_addr != -1)
{
Command &cmd = (*program_memory)[prg_mem_addr];
pr_info("Instruction " << prg_mem_addr << " (" << op_to_str(cmd.t) << ") completes, writes output to " << cmd.res);
data_memory[cmd.res].ready = 1;
}
// progress pipeline
for (int i = DYN_PIPELINE_LAYERS - 2; i >= 0; i--)
{
layer_cmd[i+1] = layer_cmd[i];
}
layer_cmd[0] = -1;
// load new operation if ready
if (instruction_ready())
{
Command &cmd = (*program_memory)[pc];
pr_info("Instruction " << pc << " (" << op_to_str(cmd.t) << ") reads " << cmd.op1 << " and " << cmd.op2 << " and enters the pipeline");
layer_cmd[0] = pc;
pc++;
}
pr_info("Step took " << time_taken << " ticks");
}
void System::run(void)
{
while (next_instruction() || !pipeline_empty())
{
print_pipeline();
pri_info("\n");
step();
pri_info("\n");
}
pr_info("System finished in " << system_time << " ticks");
}
}
+13
View File
@@ -1,5 +1,9 @@
#include "config.h"
#include "parser.hpp"
#include "syntaxer.hpp"
#include "assembler.hpp"
#include "dyn-pipeline.hpp"
int main(int argc, char **argv)
{
@@ -18,4 +22,13 @@ int main(int argc, char **argv)
std::vector<Expression::Operation> ov = Syntax::build_op_vector(tv);
Expression::TreeNode root = Syntax::ov_to_tree(tv, ov);
std::vector<DynamicPipeline::Command> program;
std::array<double, DYN_PIPELINE_MEM_SIZE> memory;
int mem_used;
Assembler::assemble(root, program, memory, mem_used);
DynamicPipeline::System machine;
machine.init(&program, memory, mem_used);
machine.run();
}
+4 -2
View File
@@ -322,7 +322,7 @@ static int write_operation(
return 1;
}
#endif
int this_op_weight = get_operator_weight(mv, location) + 1;
int this_op_weight = get_operator_weight(mv, location) + Expression::Token::weight_op(o.v.o);
int il = l.t == Expression::Token::CV ? l.v.cv.i : left_location;
int ir = r.t == Expression::Token::CV ? r.v.cv.i : right_location;
@@ -1388,7 +1388,7 @@ finish:
return ov;
}
Expression::TreeNode ov_to_tree(const std::vector<Expression::Token> tv, const std::vector<Expression::Operation>& ov)
Expression::TreeNode ov_to_tree(const std::vector<Expression::Token>& tv, const std::vector<Expression::Operation>& ov)
{
Expression::TreeNode root;
std::queue<std::pair<Expression::TreeNode *, int>> queued_ops;
@@ -1434,6 +1434,8 @@ Expression::TreeNode ov_to_tree(const std::vector<Expression::Token> tv, const s
void print_op_tree(const Expression::TreeNode& root)
{
(void) root;
std::vector<std::pair<Expression::TreeNode *, int>> items_to_display;
}