Jlm
Loading...
Searching...
No Matches
tac.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 2015 Nico Reißmann <nico.reissmann@gmail.com>
3 * See COPYING for terms of redistribution.
4 */
5
6#include <jlm/llvm/ir/tac.hpp>
7
8#include <sstream>
9
10namespace jlm::llvm
11{
12
14
16{
17 for (const auto & tac : tacs_)
18 delete tac;
19}
20
21static void
23 const rvsdg::SimpleOperation & operation,
24 const std::vector<const Variable *> & operands)
25{
26 if (operands.size() != operation.narguments())
27 throw util::Error("invalid number of operands.");
28
29 for (size_t n = 0; n < operands.size(); n++)
30 {
31 if (operands[n]->type() != *operation.argument(n))
32 throw util::TypeError(
33 operands[n]->type().debug_string(),
34 operation.argument(n)->debug_string());
35 }
36}
37
38static void
40 const rvsdg::SimpleOperation & operation,
41 const std::vector<std::unique_ptr<ThreeAddressCodeVariable>> & results)
42{
43 if (results.size() != operation.nresults())
44 throw util::Error("invalid number of variables.");
45
46 for (size_t n = 0; n < results.size(); n++)
47 {
48 if (results[n]->type() != *operation.result(n))
49 throw util::Error("invalid type.");
50 }
51}
52
54 std::unique_ptr<rvsdg::SimpleOperation> operation,
55 const std::vector<const Variable *> & operands)
56 : operands_(operands),
57 operation_(std::move(operation))
58{
59 check_operands(this->operation(), operands);
60
61 auto names = create_names(this->operation().nresults());
62 create_results(this->operation(), names);
63}
64
66 std::unique_ptr<rvsdg::SimpleOperation> operation,
67 const std::vector<const Variable *> & operands,
68 const std::vector<std::string> & names)
69 : operands_(operands),
70 operation_(std::move(operation))
71{
72 check_operands(this->operation(), operands);
73
74 if (names.size() != this->operation().nresults())
75 throw util::Error("Invalid number of result names.");
76
77 create_results(this->operation(), names);
78}
79
81 std::unique_ptr<rvsdg::SimpleOperation> operation,
82 const std::vector<const Variable *> & operands,
83 std::vector<std::unique_ptr<ThreeAddressCodeVariable>> results)
84 : operands_(operands),
85 operation_(std::move(operation)),
86 results_(std::move(results))
87{
88 check_operands(this->operation(), operands);
90}
91
92void
94 const rvsdg::SimpleOperation & operation,
95 const std::vector<const Variable *> & operands)
96{
97 check_operands(operation, operands);
98
99 results_.clear();
100 operands_ = operands;
102
103 auto names = create_names(operation.nresults());
105}
106
107void
109 const rvsdg::SimpleOperation & operation,
110 const std::vector<const Variable *> & operands)
111{
112 check_operands(operation, operands);
114
115 operands_ = operands;
117}
118
119std::string
121{
122 std::string resultString;
123 for (size_t n = 0; n < threeAddressCode.nresults(); n++)
124 {
125 resultString += threeAddressCode.result(n)->debug_string();
126 if (n != threeAddressCode.nresults() - 1)
127 resultString += ", ";
128 }
129
130 std::string operandString;
131 for (size_t n = 0; n < threeAddressCode.noperands(); n++)
132 {
133 operandString += threeAddressCode.operand(n)->debug_string();
134 if (n != threeAddressCode.noperands() - 1)
135 operandString += ", ";
136 }
137
138 std::string operationString = threeAddressCode.operation().debug_string();
139 std::string resultOperationSeparator = resultString.empty() ? "" : " = ";
140 std::string operationOperandSeparator = operandString.empty() ? "" : " ";
141 return resultString + resultOperationSeparator + operationString + operationOperandSeparator
142 + operandString;
143}
144
145}
~ThreeAddressCodeVariable() noexcept override
void create_results(const rvsdg::SimpleOperation &operation, const std::vector< std::string > &names)
Definition tac.hpp:165
const rvsdg::SimpleOperation & operation() const noexcept
Definition tac.hpp:84
std::unique_ptr< rvsdg::Operation > operation_
Definition tac.hpp:188
std::vector< const Variable * > operands_
Definition tac.hpp:187
void replace(const rvsdg::SimpleOperation &operation, const std::vector< const Variable * > &operands)
Definition tac.cpp:108
ThreeAddressCode(std::unique_ptr< rvsdg::SimpleOperation > operation, const std::vector< const Variable * > &operands)
Definition tac.cpp:53
void convert(const rvsdg::SimpleOperation &operation, const std::vector< const Variable * > &operands)
Definition tac.cpp:93
size_t nresults() const noexcept
Definition tac.hpp:103
std::vector< std::unique_ptr< ThreeAddressCodeVariable > > results_
Definition tac.hpp:189
static std::string ToAscii(const ThreeAddressCode &threeAddressCode)
Definition tac.cpp:120
const Variable * operand(size_t index) const noexcept
Definition tac.hpp:96
static std::vector< std::string > create_names(size_t nnames)
Definition tac.hpp:177
const ThreeAddressCodeVariable * result(size_t index) const noexcept
Definition tac.hpp:109
size_t noperands() const noexcept
Definition tac.hpp:90
virtual std::string debug_string() const
Definition variable.cpp:14
virtual std::unique_ptr< Operation > copy() const =0
virtual std::string debug_string() const =0
const std::shared_ptr< const rvsdg::Type > & argument(size_t index) const noexcept
Definition operation.cpp:23
const std::shared_ptr< const rvsdg::Type > & result(size_t index) const noexcept
Definition operation.cpp:36
size_t nresults() const noexcept
Definition operation.cpp:30
size_t narguments() const noexcept
Definition operation.cpp:17
Global memory state passed between functions.
static void check_results(const rvsdg::SimpleOperation &operation, const std::vector< std::unique_ptr< ThreeAddressCodeVariable > > &results)
Definition tac.cpp:39
static void check_operands(const rvsdg::SimpleOperation &operation, const std::vector< const Variable * > &operands)
Definition tac.cpp:22