Gyoji Compiler
Loading...
Searching...
No Matches
gyoji-codegen-private.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 "llvm/ADT/APFloat.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/IR/BasicBlock.h"
20#include "llvm/IR/Constants.h"
21#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Function.h"
23#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/LLVMContext.h"
26#include "llvm/IR/LegacyPassManager.h"
27#include "llvm/IR/Module.h"
28#include "llvm/IR/Type.h"
29#include "llvm/IR/Verifier.h"
30#include "llvm/MC/TargetRegistry.h"
31#include "llvm/Support/FileSystem.h"
32#include "llvm/Support/TargetSelect.h"
33#include "llvm/Support/raw_ostream.h"
34#include "llvm/Target/TargetMachine.h"
35#include "llvm/Target/TargetOptions.h"
36#include "llvm/TargetParser/Host.h"
37
38namespace Gyoji::codegen {
39 class CodeGeneratorLLVMContext {
40 public:
41 CodeGeneratorLLVMContext(
42 const Gyoji::context::CompilerContext & _context,
43 const Gyoji::mir::MIR & _mir
44 );
45 ~CodeGeneratorLLVMContext();
46 void initialize();
47 void generate();
48 int output(const std::string & filename);
49
50 private:
54
55 const Gyoji::context::CompilerContext & compiler_context;
56 const Gyoji::mir::MIR & mir;
57
64
65 void create_types(const Gyoji::mir::MIR & mir);
66 llvm::Type *create_type(const Gyoji::mir::Type * type);
67 llvm::Type *create_type_primitive(const Gyoji::mir::Type *primitive);
68 llvm::Type *create_type_enum(const Gyoji::mir::Type *enumtype);
69 llvm::Type *create_type_composite(const Gyoji::mir::Type *compositetype);
70 llvm::Type *create_type_pointer(const Gyoji::mir::Type *pointertype);
71 llvm::Type *create_type_reference(const Gyoji::mir::Type *referencetype);
72 llvm::Type *create_type_function(const Gyoji::mir::Type *function_type);
73 llvm::Type *create_type_function_pointer(const Gyoji::mir::Type *fptr_type);
74 llvm::Type *create_type_array(const Gyoji::mir::Type *array_type);
75
76 // Global symbols
77 void generate_operation_function_call(
78 const Gyoji::mir::Function & mir_function,
79 const Gyoji::mir::OperationFunctionCall & operation
80 );
81
82 void generate_operation_symbol(
83 const Gyoji::mir::Function & mir_function,
84 const Gyoji::mir::OperationSymbol & operation
85 );
86
87 // Cast operations
88 void generate_operation_widen_numeric(
89 const Gyoji::mir::Function & mir_function,
90 const Gyoji::mir::OperationCast & operation
91 );
92
93 // Indirect access
94 void generate_operation_array_index(
95 const Gyoji::mir::Function & mir_function,
96 const Gyoji::mir::OperationArrayIndex & operation
97 );
98 void generate_operation_dot(
99 const Gyoji::mir::Function & mir_function,
100 const Gyoji::mir::OperationDot & operation
101 );
102
103 // Variable access
104 void generate_operation_local_variable(
105 const Gyoji::mir::Function & mir_function,
106 const Gyoji::mir::OperationLocalVariable & operation
107 );
108 void generate_operation_local_declare(
109 const Gyoji::mir::Function & mir_function,
110 const Gyoji::mir::OperationLocalDeclare & operation
111 );
112 void generate_operation_local_undeclare(
113 const Gyoji::mir::Function & mir_function,
115 );
116
117 // Literals
118 void generate_operation_literal_char(
119 const Gyoji::mir::Function & mir_function,
120 const Gyoji::mir::OperationLiteralChar & operation
121 );
122 void generate_operation_literal_string(
123 const Gyoji::mir::Function & mir_function,
124 const Gyoji::mir::OperationLiteralString & operation
125 );
126 void generate_operation_literal_int(
127 const Gyoji::mir::Function & mir_function,
128 const Gyoji::mir::OperationLiteralInt & operation
129 );
130 void generate_operation_literal_float(
131 const Gyoji::mir::Function & mir_function,
132 const Gyoji::mir::OperationLiteralFloat & operation
133 );
134 void generate_operation_literal_bool(
135 const Gyoji::mir::Function & mir_function,
136 const Gyoji::mir::OperationLiteralBool & operation
137 );
138 void generate_operation_literal_null(
139 const Gyoji::mir::Function & mir_function,
140 const Gyoji::mir::OperationLiteralNull & operation
141 );
142 void generate_operation_anonymous_structure(
143 const Gyoji::mir::Function & mir_function,
145 );
146
147 // Unary operations
148 void generate_operation_addressof(
149 const Gyoji::mir::Function & mir_function,
150 const Gyoji::mir::OperationUnary & operation
151 );
152 void generate_operation_dereference(
153 const Gyoji::mir::Function & mir_function,
154 const Gyoji::mir::OperationUnary & operation
155 );
156 void generate_operation_arithmetic_negate(
157 const Gyoji::mir::Function & mir_function,
158 const Gyoji::mir::OperationUnary & operation
159 );
160 void generate_operation_bitwise_not(
161 const Gyoji::mir::Function & mir_function,
162 const Gyoji::mir::OperationUnary & operation
163 );
164 void generate_operation_logical_not(
165 const Gyoji::mir::Function & mir_function,
166 const Gyoji::mir::OperationUnary & operation
167 );
168
169 // Binary operations
170 void generate_operation_add(
171 const Gyoji::mir::Function & mir_function,
172 const Gyoji::mir::OperationBinary & operation
173 );
174 void generate_operation_subtract(
175 const Gyoji::mir::Function & mir_function,
176 const Gyoji::mir::OperationBinary & operation
177 );
178 void generate_operation_multiply(
179 const Gyoji::mir::Function & mir_function,
180 const Gyoji::mir::OperationBinary & operation
181 );
182 void generate_operation_divide(
183 const Gyoji::mir::Function & mir_function,
184 const Gyoji::mir::OperationBinary & operation
185 );
186 void generate_operation_modulo(
187 const Gyoji::mir::Function & mir_function,
188 const Gyoji::mir::OperationBinary & operation
189 );
190 void generate_operation_logical_and(
191 const Gyoji::mir::Function & mir_function,
192 const Gyoji::mir::OperationBinary & operation
193 );
194 void generate_operation_logical_or(
195 const Gyoji::mir::Function & mir_function,
196 const Gyoji::mir::OperationBinary & operation
197 );
198 void generate_operation_bitwise_and(
199 const Gyoji::mir::Function & mir_function,
200 const Gyoji::mir::OperationBinary & operation
201 );
202 void generate_operation_bitwise_or(
203 const Gyoji::mir::Function & mir_function,
204 const Gyoji::mir::OperationBinary & operation
205 );
206 void generate_operation_bitwise_xor(
207 const Gyoji::mir::Function & mir_function,
208 const Gyoji::mir::OperationBinary & operation
209 );
210 void generate_operation_shift(
211 const Gyoji::mir::Function & mir_function,
212 const Gyoji::mir::OperationBinary & operation
213 );
214
215 // Binary operations: comparisons
216 void generate_operation_comparison(
217 const Gyoji::mir::Function & mir_function,
218 const Gyoji::mir::OperationBinary & operation
219 );
220 void generate_operation_sizeof_type(
221 const Gyoji::mir::Function & mir_function,
222 const Gyoji::mir::OperationSizeofType & operation
223 );
224 // Binary operations: assignments
225 void generate_operation_assign(
226 const Gyoji::mir::Function & mir_function,
227 const Gyoji::mir::OperationBinary & operation
228 );
229
230 // Branch and flow control
231 void generate_operation_jump_conditional(
232 const Gyoji::mir::Function & mir_function,
234 );
235 void generate_operation_jump(
236 const Gyoji::mir::Function & mir_function,
237 const Gyoji::mir::OperationJump & operation
238 );
239 llvm::Value *generate_operation_return(
240 const Gyoji::mir::Function & mir_function,
241 const Gyoji::mir::OperationReturn & operation
242 );
243 llvm::Value *generate_operation_return_void(
244 const Gyoji::mir::Function & mir_function,
245 const Gyoji::mir::OperationReturnVoid & operation
246 );
247
248 llvm::Value *generate_basic_block(
249 const Gyoji::mir::Function & function,
250 const Gyoji::mir::BasicBlock & mir_block
251 );
252
253 void generate_function(const Gyoji::mir::Function & function);
254
255 llvm::Function * create_function(const Gyoji::mir::Function & function);
256 llvm::AllocaInst *CreateEntryBlockAlloca(
257 llvm::Function *TheFunction,
258 const llvm::StringRef & VarName
259 );
260
261 };
262
263};
constexpr void generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
Compiler Context.
Definition gyoji-context.hpp:44
Basic block of a function.
Definition functions.hpp:118
Function inside a translation unit.
Definition functions.hpp:342
The middle-tier intermediate representation (MIR) of a translation unit.
Definition gyoji-mir.hpp:71
This class represents an anonymous structure.
Definition operations.hpp:1694
This subclass of OperationBinary represents indexing an array.
Definition operations.hpp:1249
This subclass of Operation represents a binary operation.
Definition operations.hpp:1101
This subclass of OperationUnary represents a cast operation.
Definition operations.hpp:1054
This subclass of Operation is used to access member variables of classes and other aggregate types by...
Definition operations.hpp:1285
Function call (invoke) operation.
Definition operations.hpp:1175
This operation represents a conditional jump.
Definition operations.hpp:1757
Unconditional Jump.
Definition operations.hpp:1797
Literal bool.
Definition operations.hpp:1620
Literal character.
Definition operations.hpp:1364
Literal float.
Definition operations.hpp:1571
Literal integer.
Definition operations.hpp:1433
Literal null.
Definition operations.hpp:1657
Literal string.
Definition operations.hpp:1395
Declare a local variable in scope.
Definition operations.hpp:1886
Un-declare a variable (remove it from scope)
Definition operations.hpp:1924
Load a local variable.
Definition operations.hpp:1328
Return from a function without a value.
Definition operations.hpp:1858
Return from a function with a value.
Definition operations.hpp:1835
The operation for obtaining the storage size of a type.
Definition operations.hpp:1717
Symbol-table lookup.
Definition operations.hpp:1209
This subclass of Operation represents a unary operation.
Definition operations.hpp:1012
This represents a type as declared in a translation unit.
Definition types.hpp:313