program.hpp 2.07 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 <rtg/target.hpp>
Paul's avatar
Paul committed
11
#include <algorithm>
Paul's avatar
Paul committed
12
#include <iostream>
Paul's avatar
Paul committed
13
14
15

namespace rtg {

Paul's avatar
Paul committed
16
17
struct program_impl;

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

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

Paul's avatar
Paul committed
35
36
37
38
39
    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
40
41
    instruction_ref
    insert_instruction(instruction_ref ins, operation op, std::vector<instruction_ref> args);
Paul's avatar
Paul committed
42

Paul's avatar
Paul committed
43
44
45
46
47
48
49
50
    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
51
    template <class... Ts>
Paul's avatar
Paul committed
52
    instruction_ref add_literal(Ts&&... xs)
Paul's avatar
Paul committed
53
    {
Paul's avatar
Paul committed
54
        return add_literal(literal{std::forward<Ts>(xs)...});
Paul's avatar
Paul committed
55
56
    }

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

Paul's avatar
Paul committed
59
    instruction_ref add_outline(shape s);
Paul's avatar
Paul committed
60

Paul's avatar
Paul committed
61
    instruction_ref add_parameter(std::string name, shape s);
Paul's avatar
Paul committed
62

Paul's avatar
Paul committed
63
64
    shape get_parameter_shape(std::string name);

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

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

Paul's avatar
Paul committed
69
    bool has_instruction(instruction_ref ins) const;
Paul's avatar
Paul committed
70

71
72
73
    instruction_ref begin();
    instruction_ref end();

Paul's avatar
Paul committed
74
75
    instruction_ref validate() const;

Paul's avatar
Paul committed
76
77
    void compile(const target& t);

Paul's avatar
Paul committed
78
    private:
Paul's avatar
Paul committed
79
    std::unique_ptr<program_impl> impl;
Paul's avatar
Paul committed
80
81
};

Paul's avatar
Paul committed
82
} // namespace rtg
Paul's avatar
Paul committed
83
84

#endif