Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Dec 31, 2022
1 parent e3e34cc commit c57d6a6
Show file tree
Hide file tree
Showing 22 changed files with 58 additions and 40 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,17 @@ jobs:
chmod +x setup-libs.sh
./setup-libs.sh
- name: Test
run: tree .

- name: Build Test target
env:
LLVM_DIR: /home/runner/work/spice/llvm/build/lib/cmake/llvm
run: |
echo "/usr/lib/ccache:/usr/local/opt/ccache/libexec" >> $GITHUB_PATH
mkdir ./bin
cd ./bin
cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DSPICE_BUILT_BY="ghactions" -DSPICE_LINK_STATIC=OFF -DSPICE_RUN_COVERAGE=ON -GNinja -Wattributes ..
cmake -DCMAKE_BUILD_TYPE=Debug -DSPICE_BUILT_BY="ghactions" -DSPICE_LINK_STATIC=OFF -DSPICE_RUN_COVERAGE=ON -GNinja -Wattributes ..
cmake --build . --target spicetest -j$(nproc)
- name: Run Test target
Expand Down
5 changes: 2 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ set(SOURCES
# Symbol table builder
symboltablebuilder/SymbolTableBuilder.cpp
symboltablebuilder/SymbolTableBuilder.h
symboltablebuilder/Scope.cpp
symboltablebuilder/Scope.h
symboltablebuilder/SymbolTable.cpp
symboltablebuilder/SymbolTable.h
symboltablebuilder/SymbolTableEntry.cpp
Expand Down Expand Up @@ -100,9 +102,6 @@ set(SOURCES
# Linker
linker/LinkerInterface.cpp
linker/LinkerInterface.h
# Scope management
scope/Scope.cpp
scope/Scope.h
# Model
model/Function.cpp
model/Function.h
Expand Down
2 changes: 1 addition & 1 deletion src/SourceFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <ast/ASTNodes.h>
#include <exception/AntlrThrowingErrorListener.h>
#include <global/RuntimeModuleManager.h>
#include <scope/Scope.h>
#include <symboltablebuilder/Scope.h>
#include <util/CompilerWarning.h>

#include <llvm/IR/IRBuilder.h>
Expand Down
2 changes: 1 addition & 1 deletion src/ast/ASTNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <ast/ParallelizableASTVisitor.h>
#include <model/Function.h>
#include <model/Struct.h>
#include <scope/Scope.h>
#include <symboltablebuilder/Scope.h>
#include <util/CodeLoc.h>

namespace spice::compiler {
Expand Down
4 changes: 1 addition & 3 deletions src/global/RuntimeModuleManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
#include "RuntimeModuleManager.h"

#include <SourceFile.h>
#include <scope/Scope.h>
#include <symboltablebuilder/Scope.h>
#include <util/FileUtil.h>

#include <ast/ASTNodes.h>

namespace spice::compiler {

SourceFile *RuntimeModuleManager::requestModule(SourceFile *sourceFile, const RuntimeModuleName &moduleName) {
Expand Down
6 changes: 3 additions & 3 deletions src/irgenerator/GenTopLevelDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ std::any IRGenerator::visitMainFctDef(const MainFctDefNode *node) {
}

// Create entry block
llvm::BasicBlock *bEntry = createBlock("entry");
llvm::BasicBlock *bEntry = createBlock();
switchToBlock(bEntry, fct);

// Reset alloca insert markers to this block
Expand Down Expand Up @@ -222,7 +222,7 @@ std::any IRGenerator::visitFctDef(const FctDefNode *node) {
diGenerator.generateFunctionDebugInfo(func, manifestation);

// Create entry block
llvm::BasicBlock *bEntry = createBlock("entry");
llvm::BasicBlock *bEntry = createBlock();
switchToBlock(bEntry, func);

// Reset alloca insert markers to this block
Expand Down Expand Up @@ -387,7 +387,7 @@ std::any IRGenerator::visitProcDef(const ProcDefNode *node) {
diGenerator.generateFunctionDebugInfo(proc, manifestation);

// Create entry block
llvm::BasicBlock *bEntry = createBlock("entry");
llvm::BasicBlock *bEntry = createBlock();
switchToBlock(bEntry, proc);

// Reset alloca insert markers to this block
Expand Down
3 changes: 1 addition & 2 deletions src/irgenerator/IRGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ llvm::Constant *IRGenerator::getDefaultValueForSymbolType(const SymbolType &symb
throw std::runtime_error("Internal compiler error: Cannot determine default value for symbol type"); // GCOV_EXCL_LINE
}

llvm::BasicBlock *IRGenerator::createBlock(const std::string &blockName) {
// Create block
llvm::BasicBlock *IRGenerator::createBlock(const std::string &blockName /*=""*/) {
return llvm::BasicBlock::Create(context, blockName);
}

Expand Down
2 changes: 1 addition & 1 deletion src/irgenerator/IRGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class IRGenerator : private CompilerPass, public ParallelizableASTVisitor {

private:
// Private methods
llvm::BasicBlock *createBlock(const std::string &blockName);
llvm::BasicBlock *createBlock(const std::string &blockName = "");
void switchToBlock(llvm::BasicBlock *block, llvm::Function *parentFct = nullptr);
void insertJump(llvm::BasicBlock *targetBlock);
void insertCondJump(llvm::Value *condition, llvm::BasicBlock *trueBlock, llvm::BasicBlock *falseBlock);
Expand Down
2 changes: 1 addition & 1 deletion src/irgenerator/OpRuleConversionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <ast/ASTNodes.h>
#include <irgenerator/IRGenerator.h>
#include <scope/Scope.h>
#include <symboltablebuilder/Scope.h>

namespace spice::compiler {

Expand Down
2 changes: 1 addition & 1 deletion src/model/Struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "Struct.h"

#include <scope/Scope.h>
#include <symboltablebuilder/Scope.h>

namespace spice::compiler {

Expand Down
6 changes: 3 additions & 3 deletions src/scope/Scope.cpp → src/symboltablebuilder/Scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Scope::~Scope() {
parent = nullptr;
codeLoc = nullptr;
// Notify all parents, that the scope is de-allocated now
for (Scope *parent : parents) {
if (parent)
for (auto &child : parent->children)
for (Scope *p : parents) {
if (p)
for (auto &child : p->children)
if (child.second == this)
child.second = nullptr;
}
Expand Down
2 changes: 1 addition & 1 deletion src/scope/Scope.h → src/symboltablebuilder/Scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Scope {
SourceFile *sourceFile;
std::unordered_map<std::string, Scope *> children;
const ScopeType type;
SymbolTable symbolTable = SymbolTable(parent == nullptr ? nullptr : &parent->symbolTable);
SymbolTable symbolTable = SymbolTable(parent == nullptr ? nullptr : &parent->symbolTable, this);
const CodeLoc *codeLoc = nullptr;

private:
Expand Down
2 changes: 1 addition & 1 deletion src/symboltablebuilder/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ SymbolTableEntry *SymbolTable::insert(const std::string &name, const SymbolSpeci
bool isGlobal = parent == nullptr;
size_t orderIndex = symbols.size();
// Insert into symbols map. The type is 'dyn', because concrete types are determined by the type checker later on
symbols.insert({name, SymbolTableEntry(name, SymbolType(TY_INVALID), this, specifiers, declNode, orderIndex, isGlobal)});
symbols.insert({name, SymbolTableEntry(name, SymbolType(TY_INVALID), scope, specifiers, declNode, orderIndex, isGlobal)});
return &symbols.at(name);
}

Expand Down
4 changes: 3 additions & 1 deletion src/symboltablebuilder/SymbolTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace spice::compiler {

// Forward declarations
class SymbolSpecifiers;
class Scope;
class SymbolType;
struct CodeLoc;

Expand All @@ -29,7 +30,7 @@ struct CodeLoc;
class SymbolTable {
public:
// Constructors
explicit SymbolTable(SymbolTable *parent) : parent(parent){};
SymbolTable(SymbolTable *parent, Scope *scope) : parent(parent), scope(scope){};

// Friend classes
friend class Scope;
Expand All @@ -49,6 +50,7 @@ class SymbolTable {

// Public members
SymbolTable *parent;
Scope *scope;
std::unordered_map<std::string, SymbolTableEntry> symbols;
std::unordered_map<std::string, Capture> captures;
bool capturingRequired = false;
Expand Down
18 changes: 13 additions & 5 deletions src/symboltablebuilder/SymbolTableEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <ast/ASTNodes.h>
#include <exception/SemanticError.h>
#include <symboltablebuilder/Scope.h>
#include <util/CodeLoc.h>

namespace spice::compiler {
Expand Down Expand Up @@ -92,13 +93,17 @@ llvm::Value *SymbolTableEntry::getAddress() const { return memAddress.empty() ?
*/
void SymbolTableEntry::updateAddress(llvm::Value *address) {
assert(address != nullptr);
if (!memAddress.empty())
memAddress.pop();
memAddress.push(address);
// Ensure that structs fields get no addresses assigned, as the addresses are meant for the struct instances
assert(scope->type != SCOPE_STRUCT && scope->type != SCOPE_INTERFACE);
if (memAddress.empty())
memAddress.push(address);
else
memAddress.top() = address;
}

/**
* Pushes an address onto the stack. Can be called when entering a nested function to ensure that a valid address is given
* Pushes an address onto the stack.
* Called when entering a nested function to ensure that a valid address is given, known to that function
*
* @param address Address of the symbol in memory
*/
Expand All @@ -110,7 +115,10 @@ void SymbolTableEntry::pushAddress(llvm::Value *address) {
/**
* Pop an address from the stack. Can be called when leaving a nested function
*/
void SymbolTableEntry::popAddress() { memAddress.pop(); }
void SymbolTableEntry::popAddress() {
assert(!memAddress.empty());
memAddress.pop();
}

/**
* Stringify the current symbol to a human-readable form. Used to dump whole symbol tables with their contents.
Expand Down
7 changes: 3 additions & 4 deletions src/symboltablebuilder/SymbolTableEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@ struct CodeLoc;
class SymbolTableEntry {
public:
// Constructors
SymbolTableEntry(std::string name, SymbolType type, SymbolTable *symbolTable, SymbolSpecifiers specifiers, ASTNode *declNode,
SymbolTableEntry(std::string name, SymbolType type, Scope *scope, SymbolSpecifiers specifiers, ASTNode *declNode,
size_t orderIndex, const bool global)
: name(std::move(name)), type(std::move(type)), symbolTable(symbolTable), specifiers(specifiers), declNode(declNode),
: name(std::move(name)), type(std::move(type)), scope(scope), specifiers(specifiers), declNode(declNode),
orderIndex(orderIndex), global(global){};

// Public methods
[[nodiscard]] const SymbolType &getType() const;
void updateType(const SymbolType &newType, bool overwriteExistingType);
void updateState(const LifecycleState &newState, ASTNode *node, bool force = false);
[[nodiscard]] const ASTNode *getDeclNode() const;
[[nodiscard]] const CodeLoc &getDeclCodeLoc() const;
[[nodiscard]] llvm::StructType *getStructLLVMType() const;
void setStructLLVMType(llvm::StructType *newStructType);
Expand All @@ -49,7 +48,7 @@ class SymbolTableEntry {

// Public members
const std::string name;
SymbolTable *symbolTable;
Scope *scope;
SymbolSpecifiers specifiers;
ASTNode *declNode;
const size_t orderIndex;
Expand Down
2 changes: 1 addition & 1 deletion src/symboltablebuilder/SymbolType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <exception/SemanticError.h>
#include <irgenerator/StdFunctionManager.h>
#include <model/Struct.h>
#include <scope/Scope.h>
#include <symboltablebuilder/Scope.h>
#include <symboltablebuilder/SymbolTableEntry.h>

namespace spice::compiler {
Expand Down
2 changes: 1 addition & 1 deletion src/typechecker/FunctionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "FunctionManager.h"

#include <ast/ASTNodes.h>
#include <scope/Scope.h>
#include <symboltablebuilder/Scope.h>
#include <symboltablebuilder/SymbolTableBuilder.h>
#include <typechecker/TypeChecker.h>

Expand Down
2 changes: 1 addition & 1 deletion src/typechecker/StructManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <ast/ASTNodes.h>
#include <exception/SemanticError.h>
#include <scope/Scope.h>
#include <symboltablebuilder/Scope.h>
#include <symboltablebuilder/SymbolTableBuilder.h>

namespace spice::compiler {
Expand Down
2 changes: 1 addition & 1 deletion src/typechecker/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ std::any TypeChecker::visitFunctionCall(FunctionCallNode *node) {
SymbolType returnType(TY_DYN);
SymbolType thisType(TY_DYN);
data.isMethodCall = firstFragmentEntry != nullptr && firstFragmentEntry->getType().isBaseType(TY_STRUCT) &&
firstFragmentEntry->symbolTable->parent;
firstFragmentEntry->scope->symbolTable.parent;
if (data.isMethodCall) {
// This is a method call
data.thisType = firstFragmentEntry->getType();
Expand Down
4 changes: 3 additions & 1 deletion test/TestRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
#include <global/GlobalResourceManager.h>
#include <util/FileUtil.h>

using namespace spice::compiler;

void execTestCase(const TestCase &testCase) {
// Check if test is disabled
if (TestUtil::isDisabled(testCase, skipNonGitHubTests))
GTEST_SKIP();

// Create fake cli options
std::string sourceFilePath = testCase.testPath + FileUtil::DIR_SEPARATOR + REF_NAME_SOURCE;
std::string sourceFilePath = testCase.testPath + spice::compiler::FileUtil::DIR_SEPARATOR + REF_NAME_SOURCE;
llvm::Triple targetTriple = llvm::Triple(llvm::sys::getDefaultTargetTriple());
CliOptions cliOptions = {/* mainSourceFile= */ sourceFilePath,
/* targetTriple= */ targetTriple.getTriple(),
Expand Down
14 changes: 11 additions & 3 deletions test/util/TestUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "util/CommonUtil.h"
#include "util/FileUtil.h"

using namespace spice::compiler;

#ifdef OS_UNIX
#include <cstring> // Required by builds on Linux
#endif
Expand All @@ -33,16 +35,22 @@ std::vector<TestCase> TestUtil::collectTestCases(const std::string &suiteName, b

// Convert them to test cases
for (const std::string &groupDirName : testGroupDirs) {
const std::string groupPath = suitePath + FileUtil::DIR_SEPARATOR + groupDirName;
std::stringstream groupPathBuilder;
groupPathBuilder << suitePath << FileUtil::DIR_SEPARATOR << groupDirName;
const std::string &groupPath = groupPathBuilder.str();
for (const std::string &caseDirName : TestUtil::getSubdirs(groupPath)) {
TestCase tc = {toCamelCase(groupDirName), toCamelCase(caseDirName), groupPath + FileUtil::DIR_SEPARATOR + caseDirName};
std::stringstream testPathBuilder;
testPathBuilder << groupPath << FileUtil::DIR_SEPARATOR << caseDirName;
TestCase tc = {toCamelCase(groupDirName), toCamelCase(caseDirName), testPathBuilder.str()};
testCases.push_back(tc);
}
}
} else {
// Collect test cases
for (const std::string &caseDirName : TestUtil::getSubdirs(suitePath)) {
TestCase tc = {toCamelCase(suiteName), toCamelCase(caseDirName), suitePath + FileUtil::DIR_SEPARATOR + caseDirName};
std::stringstream testPathBuilder;
testPathBuilder << suitePath << FileUtil::DIR_SEPARATOR << caseDirName;
TestCase tc = {toCamelCase(suiteName), toCamelCase(caseDirName), testPathBuilder.str()};
testCases.push_back(tc);
}
}
Expand Down

0 comments on commit c57d6a6

Please sign in to comment.