Gyoji Compiler
Loading...
Searching...
No Matches
function-lowering.hpp
1/* Copyright 2025 Jonathan S. Arney
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * https://github.com/jarney/gyoji/blob/master/LICENSE
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#pragma once
16
17#include <gyoji-mir.hpp>
18#include <gyoji-frontend.hpp>
19#include <gyoji-frontend/function-scope.hpp>
20
21
38 class TypeLowering;
39
52 public:
54 Gyoji::context::CompilerContext & _compiler_context,
55 const Gyoji::frontend::tree::FileStatementFunctionDefinition & _function_definition,
56 Gyoji::mir::MIR & _mir,
57 TypeLowering & _type_lowering
58 );
60
73 bool lower();
74
75 private:
76 // Private members
77 Gyoji::context::CompilerContext & compiler_context;
78 const Gyoji::frontend::tree::FileStatementFunctionDefinition & function_definition;
79 Gyoji::mir::MIR & mir;
80 TypeLowering & type_lowering;
81 ScopeTracker scope_tracker;
82 const Gyoji::mir::Type * class_type;
83 const Gyoji::mir::Type * class_pointer_type;
84 const Gyoji::mir::TypeMethod *method;
85
86 bool is_method() const;
87
89 size_t current_block;
90
91 // Private Methods
92
93 // Returns the current point in the current block
94 // where the 'unwindings' a 'goto' statement
95 // will be placed.
96 FunctionPoint get_current_point() const;
97
98 bool numeric_widen(
99 const Gyoji::context::SourceReference & _src_ref,
100 size_t & _widen_var,
101 const Gyoji::mir::Type *widen_to
102 );
103
104
105 bool create_constant_integer(
106 const Gyoji::frontend::integers::ParseLiteralIntResult & parse_result,
107 size_t & returned_tmpvar,
108 const Gyoji::context::SourceReference & _src_ref
109 );
110
111 bool create_constant_integer_one(
112 const Gyoji::mir::Type *type,
113 size_t & returned_tmpvar,
114 const Gyoji::context::SourceReference & _src_ref
115 );
116
117 bool create_incdec_operation(
118 const Gyoji::context::SourceReference & src_ref,
119 size_t & returned_tmpvar,
120 const size_t & operand_tmpvar,
121 bool is_increment,
122 bool is_postfix
123 );
124
125 bool local_declare_or_error(
126 const Gyoji::mir::Type *mir_type,
127 const std::string & name,
128 const Gyoji::context::SourceReference & source_ref
129 );
130
131 // Widen the shorter
132 // of the two operands to be the
133 // larger one.
134 bool numeric_widen_binary_operation(
135 const Gyoji::context::SourceReference & _src_ref,
136 size_t & a_tmpvar,
137 size_t & b_tmpvar,
138 const Gyoji::mir::Type *atype,
139 const Gyoji::mir::Type *btype,
140 const Gyoji::mir::Type **widened
141 );
142
143 bool handle_binary_operation_arithmetic(
144 const Gyoji::context::SourceReference & _src_ref,
146 size_t & returned_tmpvar,
147 size_t a_tmpvar,
148 size_t b_tmpvar
149 );
150
151 bool handle_binary_operation_logical(
152 const Gyoji::context::SourceReference & _src_ref,
154 size_t & returned_tmpvar,
155 size_t a_tmpvar,
156 size_t b_tmpvar
157 );
158
159 bool handle_binary_operation_bitwise(
160 const Gyoji::context::SourceReference & _src_ref,
162 size_t & returned_tmpvar,
163 size_t a_tmpvar,
164 size_t b_tmpvar
165 );
166
167 bool handle_binary_operation_shift(
168 const Gyoji::context::SourceReference & _src_ref,
170 size_t & returned_tmpvar,
171 size_t a_tmpvar,
172 size_t b_tmpvar
173 );
174
175 bool handle_binary_operation_compare(
176 const Gyoji::context::SourceReference & _src_ref,
178 size_t & returned_tmpvar,
179 size_t a_tmpvar,
180 size_t b_tmpvar
181 );
182
183 bool handle_binary_operation_assignment(
184 const Gyoji::context::SourceReference & _src_ref,
186 size_t & returned_tmpvar,
187 size_t a_tmpvar,
188 size_t b_tmpvar
189 );
190
191 bool extract_from_expression_primary_identifier(
192 size_t & returned_tmpvar,
193 const Gyoji::frontend::tree::ExpressionPrimaryIdentifier & expression);
194
195 bool extract_from_expression_primary_nested(
196 size_t & returned_tmpvar,
197 const Gyoji::frontend::tree::ExpressionPrimaryNested & expression);
198
199 bool extract_from_expression_primary_literal_char(
200 size_t & returned_tmpvar,
201 const Gyoji::frontend::tree::ExpressionPrimaryLiteralChar & expression);
202
203 bool extract_from_expression_primary_literal_string(
204 size_t & returned_tmpvar,
205 const Gyoji::frontend::tree::ExpressionPrimaryLiteralString & expression);
206
207 bool extract_from_expression_primary_literal_int(
208 size_t & returned_tmpvar,
209 const Gyoji::frontend::tree::ExpressionPrimaryLiteralInt & expression);
210
211 bool extract_from_expression_primary_literal_float(
212 size_t & returned_tmpvar,
213 const Gyoji::frontend::tree::ExpressionPrimaryLiteralFloat & expression);
214
215 bool extract_from_expression_postfix_array_index(
216 size_t & returned_tmpvar,
217 const Gyoji::frontend::tree::ExpressionPostfixArrayIndex & expression);
218
219 bool check_function_call_signature(
220 bool is_method,
221 const std::vector<size_t> & passed_arguments,
223 const Gyoji::mir::Type *function_pointer_type,
224 const Gyoji::context::SourceReference & src_ref
225 );
226
227 bool extract_from_expression_postfix_function_call(
228 size_t & returned_tmpvar,
229 const Gyoji::frontend::tree::ExpressionPostfixFunctionCall & expression);
230
231 bool extract_from_expression_postfix_dot(
232 size_t & returned_tmpvar,
233 const Gyoji::frontend::tree::ExpressionPostfixDot & expression);
234
235 bool extract_from_expression_postfix_arrow(
236 size_t & returned_tmpvar,
237 const Gyoji::frontend::tree::ExpressionPostfixArrow & expression);
238
239 bool extract_from_expression_postfix_incdec(
240 size_t & returned_tmpvar,
241 const Gyoji::frontend::tree::ExpressionPostfixIncDec & expression);
242
243 bool extract_from_expression_unary_prefix(
244 size_t & returned_tmpvar,
245 const Gyoji::frontend::tree::ExpressionUnaryPrefix & expression);
246
247 bool extract_from_expression_unary_sizeof_type(
248 size_t & returned_tmpvar,
249 const Gyoji::frontend::tree::ExpressionUnarySizeofType & expression);
250
251 bool extract_from_expression_binary(
252 size_t & returned_tmpvar,
253 const Gyoji::frontend::tree::ExpressionBinary & expression);
254
255 bool extract_from_expression_trinary(
256 size_t & returned_tmpvar,
257 const Gyoji::frontend::tree::ExpressionTrinary & expression);
258
259 bool extract_from_expression_cast(
260 size_t & returned_tmpvar,
261 const Gyoji::frontend::tree::ExpressionCast & expression);
262
263 bool extract_from_expression(
264 size_t & returned_tmpvar,
265 const Gyoji::frontend::tree::Expression & expression
266 );
267
268 bool extract_from_struct_initializer(
269 size_t & initial_value_tmpvar,
270 const Gyoji::frontend::tree::StructInitializerExpression & struct_initializer_expression
271 );
272
273 bool extract_from_statement_variable_declaration(
275 );
276
277 bool extract_from_statement_ifelse(
279 );
280
281 bool extract_from_statement_while(
283 );
284
285 bool extract_from_statement_for(
286 const Gyoji::frontend::tree::StatementFor & statement
287 );
288
289 bool extract_from_statement_switch(
290 const Gyoji::frontend::tree::StatementSwitch & statement
291 );
292
293 bool extract_from_statement_label(
294 const Gyoji::frontend::tree::StatementLabel & statement
295 );
296
297 bool extract_from_statement_break(
298 const Gyoji::frontend::tree::StatementBreak & statement
299 );
300
301 bool extract_from_statement_continue(
302 const Gyoji::frontend::tree::StatementContinue & statement
303 );
304
305 bool extract_from_statement_goto(
306 const Gyoji::frontend::tree::StatementGoto & statement
307 );
308
309 bool extract_from_statement_return(
310 const Gyoji::frontend::tree::StatementReturn & statement
311 );
312
318 size_t undeclare_local(
319 const std::string & variable_name,
320 const Gyoji::mir::Type *class_type,
321 size_t basic_block_id,
322 size_t location,
323 const Gyoji::context::SourceReference & src_ref
324 );
325
326 void leave_scope(
328 const Gyoji::context::SourceReference & src_ref
329 );
330
331 bool extract_from_statement_list(
332 bool automatic_unwind,
333 const Gyoji::frontend::tree::StatementList & statement_list
334 );
335
336 };
337
353 public:
364 Gyoji::context::CompilerContext & _compiler_context,
365 const Gyoji::frontend::ParseResult & _parse_result,
366 Gyoji::mir::MIR & _mir,
367 TypeLowering & _type_lowering
368 );
369
377
383 bool lower();
384 private:
385 Gyoji::context::CompilerContext & compiler_context;
386 const Gyoji::frontend::ParseResult & parse_result;
387 Gyoji::mir::MIR & mir;
388 TypeLowering & type_lowering;
389
390 bool extract_from_class_definition(const Gyoji::frontend::tree::ClassDefinition & definition);
391 bool extract_from_namespace(
392 const Gyoji::frontend::tree::FileStatementNamespace & namespace_declaration
393 );
394 bool extract_functions(const std::vector<Gyoji::owned<Gyoji::frontend::tree::FileStatement>> & statements);
395
396 };
397
398};
399
Compiler Context.
Definition gyoji-context.hpp:44
References a location in the source-file.
Definition source-reference.hpp:30
Definition parse-result.hpp:67
Lowering for a function.
Definition function-lowering.hpp:51
bool lower()
Resolve a single function definition.
Definition function-lowering.cpp:156
Resolves all functions found in the MIR.
Definition function-lowering.hpp:352
bool lower()
Definition function-lowering.cpp:44
~FunctionLowering()
Move along, nothing to see here.
Definition function-lowering.cpp:41
Location inside a specific basic block.
Definition function-scope.hpp:37
Tracks variables declared in each scope along with abels and goto statements in a highly simplified i...
Definition function-scope.hpp:326
The TypeLowering is the lowering process for types. This function reads the result of a parse and pro...
Definition type-lowering.hpp:35
This represents an if condition in one of several forms.
Definition tree.hpp:1292
Represents the declaration of a variable inside the scope of a function or block.
Definition tree.hpp:1115
Represents a 'while' statement in a function or scope.
Definition tree.hpp:1395
The middle-tier intermediate representation (MIR) of a translation unit.
Definition gyoji-mir.hpp:71
OperationType
Operations of the MIR virtual-machine.
Definition operations.hpp:86
Method of a class.
Definition types.hpp:269
This represents a type as declared in a translation unit.
Definition types.hpp:313
Converts the strongly-typed syntax tree into the MIR representation.
Definition function-lowering.hpp:37