Gyoji Compiler
Loading...
Searching...
No Matches
functions.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-misc/pointers.hpp>
18#include <gyoji-mir/types.hpp>
19#include <gyoji-mir/operations.hpp>
20
21#include <string>
22#include <map>
23#include <vector>
24
25namespace Gyoji::mir {
26 class Function;
27 class SimpleStatement;
28 class BasicBlock;
29 class OperationVisitor;
30
39 class Functions {
40 public:
47 Functions();
48
55 ~Functions();
56
66
83
91 void dump(FILE *out) const;
92
93 private:
95 };
96
119 public:
132 BasicBlock();
139 ~BasicBlock();
140
151
169 size_t size() const;
170
192 void insert_operation(size_t position, Gyoji::owned<Operation> operation);
193
210
218 void dump(FILE *out) const;
219
225 bool contains_terminator() const;
226
232
241
242 bool is_reachable() const;
247 void add_reachable_from(size_t other_block);
248 private:
250 std::vector<size_t> reachable_from;
251 bool start_block;
252 };
253
265 public:
280 std::string & _name,
281 const Type *_type,
282 const Gyoji::context::SourceReference & _name_source_ref,
283 const Gyoji::context::SourceReference & _type_source_ref
284 );
293 FunctionArgument(const FunctionArgument & _other);
301
309 const std::string & get_name() const;
310
318 const Type * get_type() const;
319
320 const Gyoji::context::SourceReference & get_type_source_ref() const;
321 const Gyoji::context::SourceReference & get_name_source_ref() const;
322
323 private:
324 std::string name;
325 const Type * type;
326 const Gyoji::context::SourceReference & name_source_ref;
327 const Gyoji::context::SourceReference & type_source_ref;
328 };
329
342 class Function {
343 public:
356 Function(
357 std::string _name,
358 const Type *_return_type,
359 const std::vector<FunctionArgument> & _arguments,
360 bool _is_unsafe,
361 const Gyoji::context::SourceReference & _source_ref
362 );
363 ~Function();
364
377 const std::string & get_name() const;
378
386 const Type *get_return_type() const;
387
396
405 const BasicBlock & get_basic_block(size_t blockid) const;
406
407 void add_operation(size_t blockid, Gyoji::owned<Operation> operation);
408
409 void insert_operation(size_t blockid, size_t operation_index, Gyoji::owned<Operation> operation);
410
418 size_t add_block();
419
434
443 void dump(FILE *out) const;
444
455
459 bool is_unsafe() const;
460
475 const Type *tmpvar_get(size_t tmpvar_id) const;
484 size_t tmpvar_define(const Type *tmpvar_type);
485
486 const Operation * tmpvar_get_operation(size_t tmpvar) const;
487
502 size_t tmpvar_duplicate(size_t tmpvar_id);
503
525
533 void iterate_operations(OperationVisitor & visitor) const;
534
535 private:
536 const std::string name;
537 const Type *return_type;
539 bool m_is_unsafe;
540
541 const Gyoji::context::SourceReference & source_ref;
542
543 // Holds the max blockid
544 // as we build them.
545 size_t blockid;
548 std::map<size_t, Operation*> tmpvar_operations;
549 };
550
551 class OperationVisitor {
552 public:
553 OperationVisitor();
554 ~OperationVisitor();
562 virtual void visit(
563 size_t block_id,
564 const BasicBlock & block,
565 size_t operation_index,
566 const Operation & operation
567 ) = 0;
568 };
569
570};
References a location in the source-file.
Definition source-reference.hpp:30
Basic block of a function.
Definition functions.hpp:118
const std::vector< Gyoji::owned< Operation > > & get_operations() const
Access to list of Operation of basic block.
Definition functions.cpp:296
bool contains_terminator() const
Definition functions.cpp:300
void add_operation(Gyoji::owned< Operation > operation)
Add an Operation to the block.
Definition functions.cpp:281
BasicBlock()
Create a new basic block.
Definition functions.cpp:274
void add_reachable_from(size_t other_block)
Definition functions.cpp:328
size_t size() const
Return the number of operations in this block.
Definition functions.cpp:332
~BasicBlock()
Move along, nothing to see here.
Definition functions.cpp:277
void dump(FILE *out) const
Dump block to file handle.
Definition functions.cpp:287
void insert_operation(size_t position, Gyoji::owned< Operation > operation)
Insert operations at a specific point.
Definition functions.cpp:336
std::vector< size_t > get_connections() const
Definition functions.cpp:309
const std::vector< size_t > & get_reachable_from() const
Definition functions.cpp:324
A single named argument to a function.
Definition functions.hpp:264
const std::string & get_name() const
Name of the argument.
Definition functions.cpp:368
~FunctionArgument()
Move along, nothing to see here.
Definition functions.cpp:364
const Type * get_type() const
Type of the argument.
Definition functions.cpp:372
Function inside a translation unit.
Definition functions.hpp:342
size_t add_block()
Creates a new basic block and returns the ID.
Definition functions.cpp:122
void dump(FILE *out) const
Dump a function to the given file handle for debugging.
Definition functions.cpp:247
bool is_unsafe() const
Definition functions.cpp:88
const Type * get_return_type() const
Return type of the function.
Definition functions.cpp:76
const Type * tmpvar_get(size_t tmpvar_id) const
Temporary values used by opcodes.
Definition functions.cpp:135
void calculate_block_reachability()
Definition functions.cpp:161
const Gyoji::context::SourceReference & get_source_ref() const
Reference to the source location where the function is defined.
Definition functions.cpp:84
size_t tmpvar_duplicate(size_t tmpvar_id)
Duplicate a temporary variable/reigster.
Definition functions.cpp:155
const BasicBlock & get_basic_block(size_t blockid) const
Get an immutable basic block from the function by ID.
Definition functions.cpp:92
size_t tmpvar_define(const Type *tmpvar_type)
Define a new temporary variable with the given type.
Definition functions.cpp:149
const std::string & get_name() const
Name of the function.
Definition functions.cpp:72
const std::map< size_t, Gyoji::owned< BasicBlock > > & get_blocks() const
Get the blocks of the function.
Definition functions.cpp:131
void iterate_operations(OperationVisitor &visitor) const
Definition functions.cpp:229
const std::vector< FunctionArgument > & get_arguments() const
Arguments to the function.
Definition functions.cpp:80
Container for functions.
Definition functions.hpp:39
Functions()
Construct an empty function table.
Definition functions.cpp:24
void dump(FILE *out) const
Dump the function table to a file handle.
Definition functions.cpp:42
const std::vector< Gyoji::owned< Function > > & get_functions() const
Returns the list of functions defined.
Definition functions.cpp:38
void add_function(Gyoji::owned< Function > _function)
Add a function to the MIR.
Definition functions.cpp:32
~Functions()
Move along, nothing to see here.
Definition functions.cpp:28
Operations inside basic blocks, the virtual instruction-set of the MIR.
Definition operations.hpp:67
This represents a type as declared in a translation unit.
Definition types.hpp:313
Middle intermediate representation (MIR) of a translation unit.
Definition gyoji-mir.hpp:51