Gyoji Compiler
Loading...
Searching...
No Matches
functions.hpp
1#pragma once
2
3#include <gyoji-misc/pointers.hpp>
4#include <gyoji-mir/types.hpp>
5#include <gyoji-mir/operations.hpp>
6
7#include <string>
8#include <map>
9#include <vector>
10
11namespace Gyoji::mir {
12 class Function;
13 class SimpleStatement;
14 class BasicBlock;
15
24 class Functions {
25 public:
32 Functions();
33
40 ~Functions();
41
51
68
76 void dump(FILE *out) const;
77
78 private:
80 };
81
104 public:
117 BasicBlock();
124 ~BasicBlock();
125
136
154 size_t size() const;
155
177 void insert_operation(size_t position, Gyoji::owned<Operation> operation);
178
195
203 void dump(FILE *out) const;
204
210 bool contains_terminator() const;
211 private:
213 };
214
226 public:
241 std::string & _name,
242 const Type *_type,
243 const Gyoji::context::SourceReference & _name_source_ref,
244 const Gyoji::context::SourceReference & _type_source_ref
245 );
254 FunctionArgument(const FunctionArgument & _other);
262
270 const std::string & get_name() const;
271
279 const Type * get_type() const;
280
281 const Gyoji::context::SourceReference & get_type_source_ref() const;
282 const Gyoji::context::SourceReference & get_name_source_ref() const;
283
284 private:
285 std::string name;
286 const Type * type;
287 const Gyoji::context::SourceReference & name_source_ref;
288 const Gyoji::context::SourceReference & type_source_ref;
289 };
290
303 class Function {
304 public:
317 Function(
318 std::string _name,
319 const Type *_return_type,
320 const std::vector<FunctionArgument> & _arguments,
321 const Gyoji::context::SourceReference & _source_ref
322 );
323 ~Function();
324
337 const std::string & get_name() const;
338
346 const Type *get_return_type() const;
347
356
365 const BasicBlock & get_basic_block(size_t blockid) const;
375 BasicBlock & get_basic_block(size_t blockid);
376
384 size_t add_block();
385
400
409 void dump(FILE *out) const;
410
421
436 const Type *tmpvar_get(size_t tmpvar_id) const;
445 size_t tmpvar_define(const Type *tmpvar_type);
446
461 size_t tmpvar_duplicate(size_t tmpvar_id);
462
463 private:
464 const std::string name;
465 const Type *return_type;
467
468 const Gyoji::context::SourceReference & source_ref;
469
470 // Holds the max blockid
471 // as we build them.
472 size_t blockid;
475 };
476};
References a location in the source-file.
Definition source-reference.hpp:16
Basic block of a function.
Definition functions.hpp:103
const std::vector< Gyoji::owned< Operation > > & get_operations() const
Access to list of Operation of basic block.
Definition functions.cpp:161
bool contains_terminator() const
Definition functions.cpp:165
void add_operation(Gyoji::owned< Operation > operation)
Add an Operation to the block.
Definition functions.cpp:148
BasicBlock()
Create a new basic block.
Definition functions.cpp:141
size_t size() const
Return the number of operations in this block.
Definition functions.cpp:174
~BasicBlock()
Move along, nothing to see here.
Definition functions.cpp:144
void dump(FILE *out) const
Dump block to file handle.
Definition functions.cpp:154
void insert_operation(size_t position, Gyoji::owned< Operation > operation)
Insert operations at a specific point.
Definition functions.cpp:178
A single named argument to a function.
Definition functions.hpp:225
const std::string & get_name() const
Name of the argument.
Definition functions.cpp:210
~FunctionArgument()
Move along, nothing to see here.
Definition functions.cpp:206
const Type * get_type() const
Type of the argument.
Definition functions.cpp:214
Function inside a translation unit.
Definition functions.hpp:303
size_t add_block()
Creates a new basic block and returns the ID.
Definition functions.cpp:85
void dump(FILE *out) const
Dump a function to the given file handle for debugging.
Definition functions.cpp:114
const Type * get_return_type() const
Return type of the function.
Definition functions.cpp:60
const Type * tmpvar_get(size_t tmpvar_id) const
Temporary values used by opcodes.
Definition functions.cpp:98
const Gyoji::context::SourceReference & get_source_ref() const
Reference to the source location where the function is defined.
Definition functions.cpp:68
size_t tmpvar_duplicate(size_t tmpvar_id)
Duplicate a temporary variable/reigster.
Definition functions.cpp:108
const BasicBlock & get_basic_block(size_t blockid) const
Get an immutable basic block from the function by ID.
Definition functions.cpp:72
size_t tmpvar_define(const Type *tmpvar_type)
Define a new temporary variable with the given type.
Definition functions.cpp:102
const std::string & get_name() const
Name of the function.
Definition functions.cpp:56
const std::map< size_t, Gyoji::owned< BasicBlock > > & get_blocks() const
Get the blocks of the function.
Definition functions.cpp:94
const std::vector< FunctionArgument > & get_arguments() const
Arguments to the function.
Definition functions.cpp:64
Container for functions.
Definition functions.hpp:24
Functions()
Construct an empty function table.
Definition functions.cpp:10
void dump(FILE *out) const
Dump the function table to a file handle.
Definition functions.cpp:28
const std::vector< Gyoji::owned< Function > > & get_functions() const
Returns the list of functions defined.
Definition functions.cpp:24
void add_function(Gyoji::owned< Function > _function)
Add a function to the MIR.
Definition functions.cpp:18
~Functions()
Move along, nothing to see here.
Definition functions.cpp:14
This represents a type as declared in a translation unit.
Definition types.hpp:299
Middle intermediate representation (MIR) of a translation unit.
Definition gyoji-mir.hpp:37