program.cpp 3.82 KB
Newer Older
Paul's avatar
Paul committed
1
#include <rtg/program.hpp>
Paul's avatar
Paul committed
2
#include <rtg/stringutils.hpp>
Paul's avatar
Paul committed
3
#include <rtg/instruction.hpp>
Paul's avatar
Paul committed
4
#include <iostream>
Paul's avatar
Paul committed
5
6
7
8
#include <algorithm>

namespace rtg {

Paul's avatar
Paul committed
9
10
11
12
13
14
struct program_impl
{
    // A list is used to keep references to an instruction stable
    std::list<instruction> instructions;
};

Paul's avatar
Paul committed
15
program::program() : impl(std::make_unique<program_impl>()) {}
Paul's avatar
Paul committed
16

Paul's avatar
Paul committed
17
program::program(program&&) noexcept = default;
Paul's avatar
Paul committed
18
19
program& program::operator=(program&&) noexcept = default;
program::~program() noexcept                    = default;
Paul's avatar
Paul committed
20

Paul's avatar
Paul committed
21
instruction_ref program::add_instruction(operation op, std::vector<instruction_ref> args)
Paul's avatar
Paul committed
22
{
Paul's avatar
Paul committed
23
24
25
    assert(std::all_of(
               args.begin(), args.end(), [&](instruction_ref x) { return has_instruction(x); }) &&
           "Argument is not an exisiting instruction");
Paul's avatar
Paul committed
26
27
    std::vector<shape> shapes(args.size());
    std::transform(
Paul's avatar
Paul committed
28
        args.begin(), args.end(), shapes.begin(), [](instruction_ref ins) { return ins->result; });
Paul's avatar
Paul committed
29
30
31
    shape r = op.compute_shape(shapes);
    impl->instructions.push_back({op, r, args});
    assert(impl->instructions.back().arguments == args);
Paul's avatar
Paul committed
32
    return std::prev(impl->instructions.end());
Paul's avatar
Paul committed
33
34
}

Paul's avatar
Paul committed
35
instruction_ref program::add_literal(literal l)
Paul's avatar
Paul committed
36
37
{
    impl->instructions.emplace_back(std::move(l));
Paul's avatar
Paul committed
38
    return std::prev(impl->instructions.end());
Paul's avatar
Paul committed
39
40
}

Paul's avatar
Paul committed
41
instruction_ref program::add_parameter(std::string name, shape s)
Paul's avatar
Paul committed
42
43
{
    impl->instructions.push_back({builtin::param{std::move(name)}, s, {}});
Paul's avatar
Paul committed
44
    return std::prev(impl->instructions.end());
Paul's avatar
Paul committed
45
46
}

Paul's avatar
Paul committed
47
bool program::has_instruction(instruction_ref ins) const
Paul's avatar
Paul committed
48
{
Paul's avatar
Paul committed
49
50
51
52
    return std::find_if(
               impl->instructions.begin(), impl->instructions.end(), [&](const instruction& x) {
                   return std::addressof(*ins) == std::addressof(x);
               }) != impl->instructions.end();
Paul's avatar
Paul committed
53
54
}

Paul's avatar
Paul committed
55
literal program::eval(std::unordered_map<std::string, argument> params) const
Paul's avatar
Paul committed
56
57
58
{
    std::unordered_map<const instruction*, argument> results;
    argument result;
Paul's avatar
Paul committed
59
    for(auto& ins : impl->instructions)
Paul's avatar
Paul committed
60
    {
Paul's avatar
Paul committed
61
        if(ins.op.name() == "@literal")
Paul's avatar
Paul committed
62
63
64
        {
            result = ins.lit.get_argument();
        }
Paul's avatar
Paul committed
65
        else if(starts_with(ins.op.name(), "@param"))
Paul's avatar
Paul committed
66
        {
Paul's avatar
Paul committed
67
            result = params.at(ins.op.name().substr(7));
Paul's avatar
Paul committed
68
        }
Paul's avatar
Paul committed
69
70
71
        else
        {
            std::vector<argument> values(ins.arguments.size());
Paul's avatar
Paul committed
72
73
74
            std::transform(ins.arguments.begin(),
                           ins.arguments.end(),
                           values.begin(),
Paul's avatar
Paul committed
75
                           [&](instruction_ref i) { return results.at(std::addressof(*i)); });
Paul's avatar
Paul committed
76
            result = ins.op.compute(values);
Paul's avatar
Paul committed
77
78
79
        }
        results.emplace(std::addressof(ins), result);
    }
Paul's avatar
Paul committed
80
    return literal{result.get_shape(), result.data()};
Paul's avatar
Paul committed
81
82
}

Paul's avatar
Paul committed
83
84
85
86
87
void program::print() const
{
    std::unordered_map<const instruction*, std::string> names;
    int count = 0;

Paul's avatar
Paul committed
88
    for(auto& ins : impl->instructions)
Paul's avatar
Paul committed
89
90
91
92
93
94
95
96
97
98
99
100
101
    {
        std::string var_name = "@" + std::to_string(count);
        if(starts_with(ins.op.name(), "@param"))
        {
            var_name = ins.op.name().substr(7);
        }

        std::cout << var_name << " = ";

        std::cout << ins.op.name();

        if(ins.op.name() == "@literal")
        {
Paul's avatar
Paul committed
102
            if(ins.lit.get_shape().elements() > 10)
Paul's avatar
Paul committed
103
104
105
                std::cout << "{ ... }";
            else
                std::cout << "{" << ins.lit << "}";
Paul's avatar
Paul committed
106
107
108
109
110
        }

        if(!ins.arguments.empty())
        {
            char delim = '(';
Paul's avatar
Paul committed
111
            for(auto&& arg : ins.arguments)
Paul's avatar
Paul committed
112
            {
Paul's avatar
Paul committed
113
                assert(this->has_instruction(arg) && "Instruction not found");
Paul's avatar
Paul committed
114
                std::cout << delim << names.at(std::addressof(*arg));
Paul's avatar
Paul committed
115
116
117
118
119
120
121
122
123
124
                delim = ',';
            }
            std::cout << ")";
        }

        std::cout << " -> " << ins.result;

        std::cout << std::endl;

        names.emplace(std::addressof(ins), var_name);
Paul's avatar
Paul committed
125
        count++;
Paul's avatar
Paul committed
126
127
128
    }
}

Paul's avatar
Paul committed
129
} // namespace rtg