Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support VTable calls #358

Merged
merged 12 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ jobs:
uses: actions/cache@v3
with:
path: /home/runner/work/spice/llvm
key: llvm-17.0.2
key: llvm-17.0.4

- name: Setup LLVM
if: steps.cache-llvm.outputs.cache-hit != 'true'
run: |
cd ..
rm -rf llvm
git clone --depth 1 --branch llvmorg-17.0.2 https://github.com/llvm/llvm-project llvm
git clone --depth 1 --branch llvmorg-17.0.4 https://github.com/llvm/llvm-project llvm
mkdir ./llvm/build
cd ./llvm/build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_CXX_FLAGS_RELEASE="-O2" -DLLVM_ENABLE_RTTI=ON -GNinja ../llvm
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ jobs:
uses: actions/cache@v3
with:
path: /home/runner/work/spice/llvm
key: llvm-17.0.2
key: llvm-17.0.4

- name: Setup LLVM
if: steps.cache-llvm.outputs.cache-hit != 'true'
run: |
echo "/usr/lib/ccache:/usr/local/opt/ccache/libexec" >> $GITHUB_PATH
cd ..
git clone --depth 1 --branch llvmorg-17.0.2 https://github.com/llvm/llvm-project llvm
git clone --depth 1 --branch llvmorg-17.0.4 https://github.com/llvm/llvm-project llvm
mkdir ./llvm/build
cd ./llvm/build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_CXX_FLAGS_RELEASE="-O2" -DLLVM_ENABLE_RTTI=ON -GNinja ../llvm
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ jobs:
uses: actions/cache@v3
with:
path: /home/runner/work/spice/spice/llvm
key: llvm-17.0.2-linux-x64
key: llvm-17.0.4-linux-x64

- name: Setup LLVM
if: steps.cache-llvm.outputs.cache-hit != 'true'
run: |
git clone --depth 1 --branch llvmorg-17.0.2 https://github.com/llvm/llvm-project.git llvm
git clone --depth 1 --branch llvmorg-17.0.4 https://github.com/llvm/llvm-project.git llvm
mkdir ./llvm/build
cd ./llvm/build
cmake -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_CXX_FLAGS_RELEASE="-O2" -DLLVM_ENABLE_RTTI=ON -Wno-dev -Wattributes ../llvm
Expand Down Expand Up @@ -107,12 +107,12 @@ jobs:
uses: actions/cache@v3
with:
path: /home/runner/work/spice/spice/llvm
key: llvm-17.0.2-linux-aarch64
key: llvm-17.0.4-linux-aarch64

- name: Clone LLVM
if: steps.cache-llvm.outputs.cache-hit != 'true'
run: |
git clone --depth 1 --branch llvmorg-17.0.2 https://github.com/llvm/llvm-project.git llvm
git clone --depth 1 --branch llvmorg-17.0.4 https://github.com/llvm/llvm-project.git llvm
mkdir ./llvm/build

- name: Setup LLVM
Expand Down Expand Up @@ -185,12 +185,12 @@ jobs:
uses: actions/cache@v3
with:
path: D:/a/spice/spice/llvm
key: llvm-17.0.2-win-x64
key: llvm-17.0.4-win-x64

- name: Setup LLVM
if: steps.cache-llvm.outputs.cache-hit != 'true'
run: |
git clone --depth 1 --branch llvmorg-17.0.2 https://github.com/llvm/llvm-project.git llvm
git clone --depth 1 --branch llvmorg-17.0.4 https://github.com/llvm/llvm-project.git llvm
setx /M PATH "%PATH%;C:\mingw64\mingw64\bin"
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
echo "Adding MinGW to path done."
Expand Down
2 changes: 1 addition & 1 deletion .run/spice.run.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="spice" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="run -O0 -d -ir ../../media/test-project/test.spice" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spice" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spice">
<configuration default="false" name="spice" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="run -O0 -d -ir -g ../../media/test-project/test.spice" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spice" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spice">
<envs>
<env name="SPICE_STD_DIR" value="$PROJECT_DIR$/std" />
</envs>
Expand Down
2 changes: 1 addition & 1 deletion dev-setup.bat
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ echo done.

REM - Clone LLVM
echo [Step 2] Cloning LLVM (Could take a while) ...
git clone --depth 1 --branch llvmorg-17.0.2 https://github.com/llvm/llvm-project llvm
git clone --depth 1 --branch llvmorg-17.0.4 https://github.com/llvm/llvm-project llvm
echo done.

REM - Build LLVM
Expand Down
2 changes: 1 addition & 1 deletion dev-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ colored_echo "done."

# Clone LLVM
colored_echo "[Step 2] Cloning LLVM (Could take a while) ... "
git clone --depth 1 --branch llvmorg-17.0.2 https://github.com/llvm/llvm-project llvm
git clone --depth 1 --branch llvmorg-17.0.4 https://github.com/llvm/llvm-project llvm
colored_echo "done."

# Build LLVM
Expand Down
9 changes: 7 additions & 2 deletions docs/docs/language/threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ f<int> main() {
Thread thread = Thread(p() {
// Do something
});
thread.run();
}
```

Expand All @@ -26,17 +27,19 @@ f<int> main() {
Thread thread1 = Thread(p() {
// Do something
});
thread1.run();
Thread thread2 = Thread(p() {
// Do something
});
thread2.run();
// Do something
thread1.join();
thread2.join();
}
```

To get the ID of a thread, use the `getId()` method. To get the ID of the current thread (i.e. within the thread routine), you can
call the static `getThreadId()` function:
To get the ID of a thread, use the `getId()` method. This only works, if `run()` was already called on the thread object.
To get the ID of the current thread (i.e. within the thread routine), you can call the static `getThreadId()` function:

```spice
import "std/os/thread";
Expand All @@ -48,6 +51,8 @@ f<int> main() {
Thread thread2 = Thread(p() {
// Do something
});
thread2.run();
thread1.run();
printf("Thread 1 ID: %d\n", thread1.getId());
printf("Thread 2 ID: %d\n", thread2.getId());
}
Expand Down
64 changes: 62 additions & 2 deletions media/test-project/test.spice
Original file line number Diff line number Diff line change
@@ -1,4 +1,64 @@
type Iterable interface {
import "std/io/cli-parser";

type CliOptions struct {
bool sayHi = false
}

p callback(bool& value) {
printf("Callback called with value %d\n", value);
}

f<int> main(int argc, string[] argv) {
CliParser parser = CliParser("Test Program", "This is a simple test program");
parser.setVersion("v0.1.0");
parser.setFooter("Copyright (c) Marc Auberer 2021-2023");

CliOptions options;
parser.addFlag("--hi", options.sayHi, "Say hi to the user");
parser.addFlag("--callback", callback, "Call a callback function");
parser.addFlag("-cb", p(bool& value) {
printf("CB called with value %d\n", value);
}, "Call a callback function");

parser.parse(argc, argv);

// Print hi if requested
if options.sayHi {
printf("Hi!\n");
}
}

/*import "std/io/cli-parser";

type CliOptions struct {
bool sayHi = false
}

p callback(bool& value) {
printf("Callback called with value %d\n", value);
}

f<int> main(int argc, string[] argv) {
CliParser parser = CliParser("Test Program", "This is a simple test program");
parser.setVersion("v0.1.0");
parser.setFooter("Copyright (c) Marc Auberer 2021-2023");

CliOptions options;
parser.addFlag("--hi", options.sayHi, "Say hi to the user");
parser.addFlag("--callback", callback, "Call a callback function");
parser.addFlag("-cb", p(bool& value) {
printf("CB called with value %d\n", value);
}, "Call a callback function");

parser.parse(argc, argv);

// Print hi if requested
if options.sayHi {
printf("Hi!\n");
}
}*/

/*type Iterable interface {
p print();
}

Expand All @@ -14,4 +74,4 @@ f<int> main() {
A a = A{5};
Iterable* i = &a;
i.print();
}
}*/
3 changes: 2 additions & 1 deletion src/ast/ASTNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class FctDefBaseNode : public ASTNode {
[[nodiscard]] std::string getSymbolTableEntryName() const { return Function::getSymbolTableEntryName(name->name, codeLoc); }
std::vector<Function *> *getFctManifestations(const std::string &_) override { return &manifestations; }
[[nodiscard]] bool isFctOrProcDef() const override { return true; }
[[nodiscard]] bool returnsOnAllControlPaths(bool *overrideUnreachable) const override;
bool returnsOnAllControlPaths(bool *overrideUnreachable) const override;

// Public members
FctNameNode *name;
Expand Down Expand Up @@ -1728,6 +1728,7 @@ class FctCallNode : public ASTNode {
// Methods
[[nodiscard]] bool isOrdinaryCall() const { return callType == TYPE_ORDINARY; }
[[nodiscard]] bool isMethodCall() const { return callType == TYPE_METHOD; }
[[nodiscard]] bool isVirtualMethodCall() const { return isMethodCall() && thisType.isBaseType(TY_INTERFACE); }
[[nodiscard]] bool isCtorCall() const { return callType == TYPE_CTOR; }
[[nodiscard]] bool isFctPtrCall() const { return callType == TYPE_FCT_PTR; }
};
Expand Down
38 changes: 33 additions & 5 deletions src/irgenerator/DebugInfoGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ llvm::DIType *DebugInfoGenerator::getDITypeForSymbolType(const ASTNode *node, co
assert(spiceStruct != nullptr);

// Check if we already know the DI type
if (spiceStruct->structDIType != nullptr) {
baseDiType = spiceStruct->structDIType;
if (spiceStruct->diType != nullptr) {
baseDiType = spiceStruct->diType;
break;
}

Expand All @@ -333,14 +333,17 @@ llvm::DIType *DebugInfoGenerator::getDITypeForSymbolType(const ASTNode *node, co
llvm::DICompositeType *structDiType = diBuilder->createStructType(
diFile, spiceStruct->name, diFile, lineNo, structLayout->getSizeInBits(), alignInBits,
llvm::DINode::FlagTypePassByValue | llvm::DINode::FlagNonTrivial, nullptr, {}, 0, nullptr, mangledName);
spiceStruct->structDIType = structDiType;
baseDiType = spiceStruct->diType = structDiType;

// Collect DI types for fields
std::vector<llvm::Metadata *> fieldTypes;
for (size_t i = 0; i < spiceStruct->fieldTypes.size(); i++) {
for (size_t i = 0; i < spiceStruct->scope->getFieldCount(); i++) {
// Get field entry
SymbolTableEntry *fieldEntry = spiceStruct->scope->symbolTable.lookupStrictByIndex(i);
assert(fieldEntry != nullptr && fieldEntry->isField());
if (fieldEntry->isImplicitField)
continue;

const SymbolType fieldType = fieldEntry->getType();
const size_t fieldLineNo = fieldEntry->declNode->codeLoc.line;
const size_t offsetInBits = structLayout->getElementOffsetInBits(i);
Expand All @@ -354,7 +357,32 @@ llvm::DIType *DebugInfoGenerator::getDITypeForSymbolType(const ASTNode *node, co
}

structDiType->replaceElements(llvm::MDTuple::get(irGenerator->context, fieldTypes));
baseDiType = structDiType;
break;
}
case TY_INTERFACE: {
Interface *spiceInterface = symbolType.getInterface(node);
assert(spiceInterface != nullptr);

// Check if we already know the DI type
if (spiceInterface->diType != nullptr) {
baseDiType = spiceInterface->diType;
break;
}

// Retrieve information about the interface
const size_t lineNo = spiceInterface->getDeclCodeLoc().line;
llvm::Type *interfaceType = spiceInterface->entry->getType().toLLVMType(irGenerator->context, irGenerator->currentScope);
assert(interfaceType != nullptr);
const llvm::StructLayout *structLayout =
irGenerator->module->getDataLayout().getStructLayout(static_cast<llvm::StructType *>(interfaceType));
const uint32_t alignInBits = irGenerator->module->getDataLayout().getABITypeAlign(interfaceType).value();

// Create interface type
const std::string mangledName = NameMangling::mangleInterface(*spiceInterface);
llvm::DICompositeType *interfaceDiType = diBuilder->createStructType(
diFile, spiceInterface->name, diFile, lineNo, structLayout->getSizeInBits(), alignInBits,
llvm::DINode::FlagTypePassByValue | llvm::DINode::FlagNonTrivial, nullptr, {}, 0, nullptr, mangledName);
baseDiType = spiceInterface->diType = interfaceDiType;
break;
}
case TY_FUNCTION: // fall-through
Expand Down
Loading