program.hpp 1.14 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
#ifndef GUARD_RTGLIB_PROGRAM_HPP
#define GUARD_RTGLIB_PROGRAM_HPP

Paul's avatar
Paul committed
4
#include <list>
Paul's avatar
Paul committed
5
6
#include <unordered_map>
#include <rtg/instruction.hpp>
Paul's avatar
Paul committed
7
#include <rtg/operand.hpp>
Paul's avatar
Paul committed
8
9
10
11
12

namespace rtg {

struct program
{
Paul's avatar
Paul committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    template<class... Ts>
    instruction * add_instruction(std::string name, Ts*... args)
    {
        auto&& op = ops.at(name);
        shape r = op.compute_shape({args->result...});
        instructions.push_back({name, r, {args...}});
        return std::addressof(instructions.back());
    }
    template<class... Ts>
    instruction * add_literal(Ts&&... xs)
    {
        instructions.emplace_back(literal{std::forward<Ts>(xs)...});
        return std::addressof(instructions.back());
    }

    template<class Op, class Shape>
    void add_operator(std::string name, Op op, Shape s)
    {
        operand result;
        result.name = name;
        result.compute = op;
        result.compute_shape = s;
        ops.emplace(name, result);
    }

    literal eval() const;

private:
    // A list is used to keep references to an instruction stable
    std::list<instruction> instructions;
Paul's avatar
Paul committed
43
44
45
46
47
48
49
50

    std::unordered_map<std::string, operand> ops;

};

}

#endif