GCC Code Coverage Report


Directory: src/
File: src/mir/functions.cpp
Date: 2025-10-15 09:43:47
Exec Total Coverage
Lines: 104 113 92.0%
Functions: 30 34 88.2%
Branches: 14 14 100.0%

Line Branch Exec Source
1 #include <gyoji-mir/functions.hpp>
2 #include <variant>
3 #include <stdio.h>
4
5 using namespace Gyoji::mir;
6
7 /////////////////////////////////////
8 // Functions
9 /////////////////////////////////////
10 32 Functions::Functions()
11 {
12 32 }
13
14 32 Functions::~Functions()
15 32 {}
16
17 void
18 266 Functions::add_function(Gyoji::owned<Function> _function)
19 {
20 266 functions.push_back(std::move(_function));
21 266 }
22
23 const std::vector<Gyoji::owned<Function>> &
24 90 Functions::get_functions() const
25 90 { return functions; }
26
27 void
28 30 Functions::dump(FILE *out) const
29 {
30
2/2
✓ Branch 5 taken 266 times.
✓ Branch 6 taken 30 times.
296 for (const auto & function : functions) {
31 266 function->dump(out);
32 }
33 30 }
34
35 /////////////////////////////////////
36 // Function
37 /////////////////////////////////////
38 266 Function::Function(
39 std::string _name,
40 const Type *_return_type,
41 const std::vector<FunctionArgument> & _arguments,
42 const Gyoji::context::SourceReference & _source_ref
43 266 )
44 266 : name(_name)
45 266 , return_type(_return_type)
46 266 , arguments(_arguments)
47 266 , source_ref(_source_ref)
48 266 , blockid(0)
49 {
50 266 }
51
52 532 Function::~Function()
53 266 {}
54
55 const std::string &
56 798 Function::get_name() const
57 798 { return name; }
58
59 const Type *
60 266 Function::get_return_type() const
61 266 { return return_type; }
62
63 const std::vector<FunctionArgument> &
64 532 Function::get_arguments() const
65 532 { return arguments; }
66
67 const Gyoji::context::SourceReference &
68 Function::get_source_ref() const
69 { return source_ref; }
70
71 const BasicBlock &
72 Function::get_basic_block(size_t blockid) const
73 {
74 const auto & it = blocks.find(blockid);
75 return *(it->second);
76 }
77
78 BasicBlock &
79 4038 Function::get_basic_block(size_t blockid)
80 {
81 4038 const auto & it = blocks.find(blockid);
82 4038 return *(it->second);
83 }
84 size_t
85 362 Function::add_block()
86 {
87 362 blocks[blockid] = std::make_unique<BasicBlock>();
88 362 size_t blockid_created = blockid;
89 362 blockid++;
90 362 return blockid_created;
91 }
92
93 const std::map<size_t, Gyoji::owned<BasicBlock>> &
94 1064 Function::get_blocks() const
95 1064 { return blocks; }
96
97 const Type *
98 2300 Function::tmpvar_get(size_t tmpvar_id) const
99 2300 { return tmpvars.at(tmpvar_id); }
100
101 size_t
102 2416 Function::tmpvar_define(const Type* type)
103 {
104 2416 tmpvars.push_back(type);
105 2416 return tmpvars.size()-1;
106 }
107 size_t
108 6 Function::tmpvar_duplicate(size_t tempvar_id)
109 {
110 6 return tmpvar_define(tmpvar_get(tempvar_id));
111 }
112
113 void
114 266 Function::dump(FILE *out) const
115 {
116 266 fprintf(out, " %s\n", get_name().c_str());
117 266 fprintf(out, " return-value : %s\n", return_type->get_name().c_str());
118
2/2
✓ Branch 5 taken 520 times.
✓ Branch 6 taken 266 times.
786 for (const auto & arg : arguments) {
119 520 fprintf(out, " arg %s : %s\n", arg.get_name().c_str(), arg.get_type()->get_name().c_str());
120 }
121 266 fprintf(out, " temporary variables\n");
122 266 size_t varid = 0;
123
2/2
✓ Branch 4 taken 2416 times.
✓ Branch 5 taken 266 times.
2682 for (const auto & value : tmpvars) {
124 2416 fprintf(out, " _%ld : %s\n", varid, value->get_name().c_str());
125 2416 varid++;
126 }
127
128 266 fprintf(out, " {\n");
129
2/2
✓ Branch 5 taken 362 times.
✓ Branch 6 taken 266 times.
628 for (const auto & block_it : blocks) {
130 362 fprintf(out, " BB%ld:\n", block_it.first);
131 362 const BasicBlock & block = *block_it.second;
132 362 block.dump(out);
133 }
134 266 fprintf(out, " }\n");
135 266 }
136
137 /////////////////////////////////////
138 // BasicBlock
139 /////////////////////////////////////
140
141 362 BasicBlock::BasicBlock()
142 362 {}
143
144 362 BasicBlock::~BasicBlock()
145 362 {}
146
147 void
148 3990 BasicBlock::add_operation(Gyoji::owned<Operation> _operation)
149 {
150 3990 operations.push_back(std::move(_operation));
151 3990 }
152
153 void
154 362 BasicBlock::dump(FILE *out) const
155 {
156
2/2
✓ Branch 5 taken 3994 times.
✓ Branch 6 taken 362 times.
4356 for (const auto & operation : operations) {
157 3994 operation->dump(out);
158 }
159 362 }
160 const std::vector<Gyoji::owned<Operation>> &
161 1448 BasicBlock::get_operations() const
162 1448 { return operations; }
163
164 bool
165 42 BasicBlock::contains_terminator() const
166 {
167
2/2
✓ Branch 5 taken 160 times.
✓ Branch 6 taken 24 times.
184 for (const auto & op : operations) {
168
2/2
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 142 times.
160 if (op->is_terminating()) return true;
169 }
170 24 return false;
171 }
172
173 size_t
174 2 BasicBlock::size() const
175 2 { return operations.size(); }
176
177 void
178 4 BasicBlock::insert_operation(size_t position, Gyoji::owned<Operation> operation)
179 {
180 4 operations.insert(operations.begin() + position, std::move(operation));
181 4 }
182
183
184 /////////////////////////////////////
185 // FunctionArgument
186 /////////////////////////////////////
187 520 FunctionArgument::FunctionArgument(
188 std::string & _name,
189 const Type * _type,
190 const Gyoji::context::SourceReference & _name_source_ref,
191 const Gyoji::context::SourceReference & _type_source_ref
192 520 )
193 520 : name(_name)
194 520 , type(_type)
195 520 , name_source_ref(_name_source_ref)
196 520 , type_source_ref(_type_source_ref)
197 520 {}
198
199 1298 FunctionArgument::FunctionArgument(const FunctionArgument & _other)
200 1298 : name(_other.name)
201 1298 , type(_other.type)
202 1298 , name_source_ref(_other.name_source_ref)
203 1298 , type_source_ref(_other.type_source_ref)
204 1298 {}
205
206 1818 FunctionArgument::~FunctionArgument()
207 1818 {}
208
209 const std::string &
210 2080 FunctionArgument::get_name() const
211 2080 { return name; }
212
213 const Type*
214 2080 FunctionArgument::get_type() const
215 2080 { return type; }
216
217 const Gyoji::context::SourceReference &
218 FunctionArgument::get_name_source_ref() const
219 { return name_source_ref; }
220
221 const Gyoji::context::SourceReference &
222 FunctionArgument::get_type_source_ref() const
223 { return type_source_ref; }
224