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

Paul's avatar
Paul committed
4
#include <list>
Paul's avatar
Paul committed
5
#include <unordered_map>
Paul's avatar
Paul committed
6
#include <rtg/operation.hpp>
Paul's avatar
Paul committed
7
#include <rtg/literal.hpp>
Paul's avatar
Paul committed
8
#include <rtg/builtin.hpp>
Paul's avatar
Paul committed
9
#include <rtg/instruction_ref.hpp>
Paul's avatar
Paul committed
10
#include <algorithm>
Paul's avatar
Paul committed
11
#include <iostream>
Paul's avatar
Paul committed
12
13
14

namespace rtg {

Paul's avatar
Paul committed
15
16
struct program_impl;

Paul's avatar
Paul committed
17
18
19
/**
 * @brief Stores the instruction stream
 */
Paul's avatar
Paul committed
20
21
struct program
{
Paul's avatar
Paul committed
22
23
    program();
    program(program&&) noexcept;
Paul's avatar
Paul committed
24
25
    program& operator=(program&&) noexcept;
    ~program() noexcept;
Paul's avatar
Paul committed
26

Paul's avatar
Paul committed
27
    template <class... Ts>
Paul's avatar
Paul committed
28
    instruction_ref add_instruction(operation op, Ts... args)
Paul's avatar
Paul committed
29
    {
Paul's avatar
Paul committed
30
        return add_instruction(op, {args...});
Paul's avatar
Paul committed
31
    }
Paul's avatar
Paul committed
32
    instruction_ref add_instruction(operation op, std::vector<instruction_ref> args);
Paul's avatar
Paul committed
33

Paul's avatar
Paul committed
34
35
36
37
38
    template <class... Ts>
    instruction_ref insert_instruction(instruction_ref ins, operation op, Ts... args)
    {
        return insert_instruction(ins, op, {args...});
    }
Paul's avatar
Paul committed
39
40
    instruction_ref
    insert_instruction(instruction_ref ins, operation op, std::vector<instruction_ref> args);
Paul's avatar
Paul committed
41

Paul's avatar
Paul committed
42
43
44
45
46
47
48
49
    template <class... Ts>
    instruction_ref replace_instruction(instruction_ref ins, operation op, Ts... args)
    {
        return replace_instruction(ins, op, {args...});
    }
    instruction_ref
    replace_instruction(instruction_ref ins, operation op, std::vector<instruction_ref> args);

Paul's avatar
Paul committed
50
    template <class... Ts>
Paul's avatar
Paul committed
51
    instruction_ref add_literal(Ts&&... xs)
Paul's avatar
Paul committed
52
    {
Paul's avatar
Paul committed
53
        return add_literal(literal{std::forward<Ts>(xs)...});
Paul's avatar
Paul committed
54
55
    }

Paul's avatar
Paul committed
56
    instruction_ref add_literal(literal l);
Paul's avatar
Paul committed
57

Paul's avatar
Paul committed
58
    instruction_ref add_parameter(std::string name, shape s);
Paul's avatar
Paul committed
59
60

    literal eval(std::unordered_map<std::string, argument> params) const;
Paul's avatar
Paul committed
61

Paul's avatar
Paul committed
62
    friend std::ostream& operator<<(std::ostream& os, const program& p);
Paul's avatar
Paul committed
63

Paul's avatar
Paul committed
64
    bool has_instruction(instruction_ref ins) const;
Paul's avatar
Paul committed
65

66
67
68
    instruction_ref begin();
    instruction_ref end();

Paul's avatar
Paul committed
69
70
    instruction_ref validate() const;

Paul's avatar
Paul committed
71
    private:
Paul's avatar
Paul committed
72
    std::unique_ptr<program_impl> impl;
Paul's avatar
Paul committed
73
74
};

Paul's avatar
Paul committed
75
} // namespace rtg
Paul's avatar
Paul committed
76
77

#endif