Skip to content

Commit ced646f

Browse files
committed
Add a bunch of codegen infrastructure
1 parent 62fb1a9 commit ced646f

File tree

11 files changed

+393
-63
lines changed

11 files changed

+393
-63
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
4848
add_compile_options(/permissive-)
4949
add_compile_options(/Zc:inline)
5050
add_compile_options(/Gy) # Function-level linking
51+
add_compile_options(/D_SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING)
5152

5253
# Ignore warnings in external headers
5354
add_compile_options(/experimental:external /external:anglebrackets /external:W0)

include/slang/codegen/CodeGenerator.h

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,77 @@
77
//------------------------------------------------------------------------------
88
#pragma once
99

10+
#include <flat_hash_map.hpp>
1011
#include <memory>
1112
#include <string>
13+
#include <vector>
1214

1315
#include "slang/util/Util.h"
1416

1517
namespace llvm {
1618

19+
class BasicBlock;
20+
class Constant;
21+
class Function;
1722
class LLVMContext;
1823
class Module;
24+
class Type;
25+
class Value;
1926

2027
} // namespace llvm
2128

2229
namespace slang {
2330

2431
class Compilation;
32+
class ConstantValue;
33+
class Expression;
34+
class InstanceSymbol;
35+
class ProceduralBlockSymbol;
36+
class Statement;
37+
class SubroutineSymbol;
38+
class SVInt;
2539
class Symbol;
40+
class SystemSubroutine;
41+
class Type;
42+
class VariableSymbol;
43+
44+
struct CodegenOptions {
45+
uint32_t maxIntBits = 128;
46+
bool flattenFourState = false;
47+
};
2648

2749
class CodeGenerator {
2850
public:
2951
CodeGenerator(Compilation& compilation);
3052
~CodeGenerator();
3153

32-
std::string run(const Symbol& symbol);
54+
std::string finish();
55+
56+
void genInstance(const InstanceSymbol& instance);
57+
void genStmt(llvm::BasicBlock* bb, const Statement& stmt);
58+
59+
llvm::Constant* genConstant(const Type& type, const ConstantValue& cv);
60+
llvm::Constant* genConstant(const Type& type, const SVInt& integer);
61+
llvm::Type* genType(const Type& type);
62+
llvm::Value* genExpr(llvm::BasicBlock* bb, const Expression& expr);
63+
llvm::Function* genSubroutine(const SubroutineSymbol& subroutine);
64+
llvm::Function* genSubroutine(const SystemSubroutine& subroutine);
3365

3466
private:
67+
void genGlobal(const VariableSymbol& variable);
68+
void genBlock(const ProceduralBlockSymbol& variable);
69+
3570
std::unique_ptr<llvm::LLVMContext> ctx;
3671
std::unique_ptr<llvm::Module> module;
72+
flat_hash_map<const Type*, llvm::Type*> typeMap;
73+
flat_hash_map<const Symbol*, llvm::Value*> globalMap;
74+
flat_hash_map<const SubroutineSymbol*, llvm::Function*> userSubroutineMap;
75+
flat_hash_map<const SystemSubroutine*, llvm::Function*> sysSubroutineMap;
76+
llvm::BasicBlock* globalInitBlock;
77+
std::vector<llvm::BasicBlock*> initialBlocks;
78+
llvm::Function* mainFunc;
3779
Compilation& compilation;
80+
CodegenOptions options;
3881
};
3982

4083
} // namespace slang
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//------------------------------------------------------------------------------
2+
//! @file ExpressionEmitter.h
3+
//! @brief Code emitter for expression trees
4+
//! @note Only included if slang is configured to use LLVM
5+
//
6+
// File is under the MIT license; see LICENSE for details
7+
//------------------------------------------------------------------------------
8+
#pragma once
9+
10+
#include <llvm/IR/BasicBlock.h>
11+
#include <llvm/IR/IRBuilder.h>
12+
13+
#include "slang/symbols/ASTVisitor.h"
14+
15+
namespace slang {
16+
17+
class CodeGenerator;
18+
19+
class ExpressionEmitter {
20+
public:
21+
ExpressionEmitter(CodeGenerator& generator, llvm::BasicBlock* bb);
22+
23+
llvm::Value* emit(const Expression& expr);
24+
25+
// TODO: remove once all expressions are handled
26+
template<typename T>
27+
llvm::Value* visit(const T& expr) { return getUndef(*expr.type); }
28+
29+
llvm::Value* visit(const IntegerLiteral& expr);
30+
llvm::Value* visit(const CallExpression& expr);
31+
32+
llvm::Value* visitInvalid(const Expression&) { return ir.CreateUnreachable(); }
33+
34+
private:
35+
llvm::Value* getUndef(const Type& type);
36+
37+
CodeGenerator& generator;
38+
llvm::IRBuilder<> ir;
39+
};
40+
41+
} // namespace slang
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//------------------------------------------------------------------------------
2+
//! @file StatementEmitter.h
3+
//! @brief Code emitter for statements
4+
//! @note Only included if slang is configured to use LLVM
5+
//
6+
// File is under the MIT license; see LICENSE for details
7+
//------------------------------------------------------------------------------
8+
#pragma once
9+
10+
#include <llvm/IR/BasicBlock.h>
11+
#include <llvm/IR/IRBuilder.h>
12+
13+
#include "slang/binding/Statements.h"
14+
15+
namespace slang {
16+
17+
class CodeGenerator;
18+
19+
class StatementEmitter {
20+
public:
21+
StatementEmitter(CodeGenerator& generator, llvm::BasicBlock* bb);
22+
23+
void emit(const Statement& stmt);
24+
25+
// TODO: remove once all statements are handled
26+
template<typename T>
27+
void visit(const T&) {}
28+
29+
void visit(const ExpressionStatement& stmt);
30+
31+
void visitInvalid(const Statement&) {}
32+
33+
private:
34+
CodeGenerator& generator;
35+
llvm::IRBuilder<> ir;
36+
};
37+
38+
} // namespace slang
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//------------------------------------------------------------------------------
2+
//! @file SystemCallEmitter.h
3+
//! @brief Code emitter for system calls (tasks and functions)
4+
//! @note Only included if slang is configured to use LLVM
5+
//
6+
// File is under the MIT license; see LICENSE for details
7+
//------------------------------------------------------------------------------
8+
#pragma once
9+
10+
#include <flat_hash_map.hpp>
11+
#include <llvm/IR/BasicBlock.h>
12+
#include <llvm/IR/IRBuilder.h>
13+
14+
#include "slang/util/Util.h"
15+
16+
namespace slang {
17+
18+
class CodeGenerator;
19+
class Expression;
20+
class Scope;
21+
class SystemSubroutine;
22+
23+
class SystemCallEmitter {
24+
public:
25+
private:
26+
};
27+
28+
} // namespace slang

source/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ add_library(slang
117117
)
118118

119119
if(SLANG_INCLUDE_LLVM)
120-
target_sources(slang PRIVATE codegen/CodeGenerator.cpp)
120+
target_sources(slang PRIVATE
121+
codegen/CodeGenerator.cpp
122+
codegen/ExpressionEmitter.cpp
123+
codegen/StatementEmitter.cpp
124+
codegen/SystemCallEmitter.cpp
125+
)
121126
endif()
122127

123128
add_dependencies(slang gen_version)

0 commit comments

Comments
 (0)