program.cpp 4.91 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
23
24
{
    return insert_instruction(impl->instructions.end(), std::move(op), std::move(args));
}
Paul's avatar
Paul committed
25
26
instruction_ref
program::insert_instruction(instruction_ref ins, operation op, std::vector<instruction_ref> args)
Paul's avatar
Paul committed
27
{
Paul's avatar
Paul committed
28
29
30
    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
31
32
    // TODO: Use move
    shape r     = compute_shape(op, args);
Paul's avatar
Paul committed
33
    auto result = impl->instructions.insert(ins, {op, r, args});
Paul's avatar
Paul committed
34
    backreference(result);
Paul's avatar
Paul committed
35
    assert(result->arguments == args);
Paul's avatar
Paul committed
36
    return result;
Paul's avatar
Paul committed
37
38
}

Paul's avatar
Paul committed
39
40
41
42
43
44
45
instruction_ref
program::replace_instruction(instruction_ref ins, operation op, std::vector<instruction_ref> args)
{
    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
46
    shape r = compute_shape(op, args);
Paul's avatar
Paul committed
47
48
49
50
51
    ins->replace(op, r, args);
    backreference(ins);
    return ins;
}

Paul's avatar
Paul committed
52
instruction_ref program::add_literal(literal l)
Paul's avatar
Paul committed
53
54
{
    impl->instructions.emplace_back(std::move(l));
Paul's avatar
Paul committed
55
    return std::prev(impl->instructions.end());
Paul's avatar
Paul committed
56
57
}

Paul's avatar
Paul committed
58
instruction_ref program::add_parameter(std::string name, shape s)
Paul's avatar
Paul committed
59
60
{
    impl->instructions.push_back({builtin::param{std::move(name)}, s, {}});
Paul's avatar
Paul committed
61
    return std::prev(impl->instructions.end());
Paul's avatar
Paul committed
62
63
}

Paul's avatar
Paul committed
64
bool program::has_instruction(instruction_ref ins) const
Paul's avatar
Paul committed
65
{
Paul's avatar
Paul committed
66
67
68
69
    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
70
71
}

Paul's avatar
Paul committed
72
73
instruction_ref program::begin() { return impl->instructions.begin(); }
instruction_ref program::end() { return impl->instructions.end(); }
74

Paul's avatar
Paul committed
75
76
instruction_ref program::validate() const
{
Paul's avatar
Paul committed
77
78
79
    return std::find_if(impl->instructions.begin(),
                        impl->instructions.end(),
                        [](const instruction& i) { return i.valid(); });
Paul's avatar
Paul committed
80
81
}

Paul's avatar
Paul committed
82
83
84
85
86
87
88
89
void program::compile(const target& t)
{
    assert(this->validate() != impl->instructions.end());
    t.apply(*this);
    if(this->validate() == impl->instructions.end())
        RTG_THROW("Invalid program from compilation");
}

Paul's avatar
Paul committed
90
literal program::eval(std::unordered_map<std::string, argument> params) const
Paul's avatar
Paul committed
91
{
Paul's avatar
Paul committed
92
    assert(this->validate() != impl->instructions.end());
Paul's avatar
Paul committed
93
94
    std::unordered_map<const instruction*, argument> results;
    argument result;
Paul's avatar
Paul committed
95
    for(auto& ins : impl->instructions)
Paul's avatar
Paul committed
96
    {
Paul's avatar
Paul committed
97
        if(ins.op.name() == "@literal")
Paul's avatar
Paul committed
98
99
100
        {
            result = ins.lit.get_argument();
        }
Paul's avatar
Paul committed
101
        else if(starts_with(ins.op.name(), "@param"))
Paul's avatar
Paul committed
102
        {
Paul's avatar
Paul committed
103
            result = params.at(ins.op.name().substr(7));
Paul's avatar
Paul committed
104
        }
Paul's avatar
Paul committed
105
106
107
        else
        {
            std::vector<argument> values(ins.arguments.size());
Paul's avatar
Paul committed
108
109
110
            std::transform(ins.arguments.begin(),
                           ins.arguments.end(),
                           values.begin(),
Paul's avatar
Paul committed
111
                           [&](instruction_ref i) { return results.at(std::addressof(*i)); });
Paul's avatar
Paul committed
112
            result = ins.op.compute(values);
Paul's avatar
Paul committed
113
114
115
        }
        results.emplace(std::addressof(ins), result);
    }
Paul's avatar
Paul committed
116
    return literal{result.get_shape(), result.data()};
Paul's avatar
Paul committed
117
118
}

Paul's avatar
Paul committed
119
std::ostream& operator<<(std::ostream& os, const program& p)
Paul's avatar
Paul committed
120
121
122
123
{
    std::unordered_map<const instruction*, std::string> names;
    int count = 0;

Paul's avatar
Paul committed
124
    for(auto& ins : p.impl->instructions)
Paul's avatar
Paul committed
125
126
127
128
129
130
131
    {
        std::string var_name = "@" + std::to_string(count);
        if(starts_with(ins.op.name(), "@param"))
        {
            var_name = ins.op.name().substr(7);
        }

Paul's avatar
Paul committed
132
        os << var_name << " = ";
Paul's avatar
Paul committed
133

Paul's avatar
Paul committed
134
        os << ins.op.name();
Paul's avatar
Paul committed
135
136
137

        if(ins.op.name() == "@literal")
        {
Paul's avatar
Paul committed
138
            if(ins.lit.get_shape().elements() > 10)
Paul's avatar
Paul committed
139
                os << "{ ... }";
Paul's avatar
Paul committed
140
            else
Paul's avatar
Paul committed
141
                os << "{" << ins.lit << "}";
Paul's avatar
Paul committed
142
143
144
145
146
        }

        if(!ins.arguments.empty())
        {
            char delim = '(';
Paul's avatar
Paul committed
147
            for(auto&& arg : ins.arguments)
Paul's avatar
Paul committed
148
            {
Paul's avatar
Paul committed
149
150
                assert(p.has_instruction(arg) && "Instruction not found");
                os << delim << names.at(std::addressof(*arg));
Paul's avatar
Paul committed
151
152
                delim = ',';
            }
Paul's avatar
Paul committed
153
            os << ")";
Paul's avatar
Paul committed
154
155
        }

Paul's avatar
Paul committed
156
        os << " -> " << ins.result;
Paul's avatar
Paul committed
157

Paul's avatar
Paul committed
158
        os << std::endl;
Paul's avatar
Paul committed
159
160

        names.emplace(std::addressof(ins), var_name);
Paul's avatar
Paul committed
161
        count++;
Paul's avatar
Paul committed
162
    }
Paul's avatar
Paul committed
163
    return os;
Paul's avatar
Paul committed
164
165
}

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