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

Extend reader for bootstrap compiler #381

Merged
merged 12 commits into from
Dec 3, 2023
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 -O2 -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 ../../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="LLVM_LIB_DIR" value="D:/LLVM/build-release/lib" />
<env name="LLVM_INCLUDE_DIR" value="D:/LLVM/llvm/include" />
Expand Down
56 changes: 2 additions & 54 deletions media/test-project/test.spice
Original file line number Diff line number Diff line change
@@ -1,57 +1,5 @@
import "std/data/vector";
import "../../src-bootstrap/bindings/llvm/llvm" as llvm;
import "../../src-bootstrap/reader/reader";

f<int> main() {
//llvm::initializeAllTargets();
//llvm::initializeAllTargetInfos();
//llvm::initializeAllTargetMCs();
//llvm::initializeAllAsmPrinters();
llvm::initializeNativeTarget();
llvm::initializeNativeAsmPrinter();

heap string targetTriple = llvm::getDefaultTargetTriple();
string error;
llvm::Target target = llvm::getTargetFromTriple(targetTriple, &error);
llvm::TargetMachine targetMachine = target.createTargetMachine(targetTriple, "generic", "", llvm::LLVMCodeGenOptLevel::CodeGenLevelDefault, llvm::LLVMRelocMode::RelocDefault, llvm::LLVMCodeModel::CodeModelDefault);

llvm::LLVMContext context;
llvm::Module module = llvm::Module("test", context);
module.setDataLayout(targetMachine.createDataLayout());
module.setTargetTriple(targetTriple);
llvm::Builder builder = llvm::Builder(context);

llvm::Type returnType = builder.getInt32Ty();
Vector<llvm::Type> argTypes;
llvm::Type funcType = llvm::getFunctionType(returnType, argTypes);
llvm::Function func = llvm::Function(module, "main", funcType);
func.setLinkage(llvm::LLVMLinkage::ExternalLinkage);

llvm::BasicBlock entry = llvm::BasicBlock(context, "");
func.pushBack(entry);
builder.setInsertPoint(entry);

llvm::Value calcResult = builder.createAdd(builder.getInt32(1), builder.getInt32(2), "calcResult");

llvm::Value helloWorldStr = builder.createGlobalStringPtr("Hello, world!\n", "helloWorldStr");
Vector<llvm::Type> printfArgTypes;
printfArgTypes.pushBack(builder.getPtrTy());
printfArgTypes.pushBack(builder.getInt32Ty());
llvm::Type printfFuncType = llvm::getFunctionType(builder.getInt32Ty(), printfArgTypes, true);
llvm::Function printfFunc = module.getOrInsertFunction("printf", printfFuncType);

Vector<llvm::Value> printfArgs;
printfArgs.pushBack(helloWorldStr);
printfArgs.pushBack(calcResult);
builder.createCall(printfFunc, printfArgs);

builder.createRet(builder.getInt32(0));

assert !llvm::verifyFunction(func);
string output;
assert !llvm::verifyModule(module, &output);

printf("%s", module.print());

llvm::PassBuilderOptions passBuilderOptions;
//llvm::PassBuilder passBuilder = llvm::PassBuilder(module, passBuilderOptions);
Reader reader = Reader("./test.spice");
}
19 changes: 13 additions & 6 deletions media/test-project/test2.spice
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
public type Driveable interface {
public p drive(int);
public f<bool> isDriving();
import "std/type/result";
import "std/type/error";

type FilePtr alias byte*;
public type File struct {
FilePtr* filePtr
}

#[test]
f<bool> testDriveable() {
return true;
// Link external functions
ext f<FilePtr*> fopen(string, string);

public f<Result<File>> openFile(string path, string mode) {
FilePtr* fp = fopen(path, mode);
File file = File{fp};
return fp != nil<FilePtr*> ? ok(file) : err(file, Error("Failed to open file"));
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Std imports
import "std/type/any";

public type IAbstractAstVisitor interface {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "../CompilerPass";
import "../SourceFile";
import "SpiceVisitor";
import "../compiler-pass";
import "../source-file";
import "../ast/spice-visitor";

type ASTBuilder struct : ICompilerPass, SpiceVisitor {
CompilerPass compilerPass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import "std/type/Any";
import "std/data/Vector";

// Own imports
import "AbstractASTVisitor";
import "../util/CodeLoc";
import "../symbol/SymbolType";
import "../ast/abstract-ast-visitor";
import "../reader/code-loc";
import "../symbol/symbol-type";

/**
* Saves a constant value for an AST node to realize features like array-out-of-bounds checks
Expand Down
7 changes: 5 additions & 2 deletions src-bootstrap/bindings/llvm/llvm.spice
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#![core.compiler.warnings.ignore]

// Std imports
import "std/data/vector";
import "linker-flags";

#![core.compiler.warnings.ignore]
// Own imports
import "linker-flags";

// ===== External type definitions =====
type VoidPtr alias byte*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "global/GlobalResourceManager";
import "./global/global-resource-manager";

type ICompilerPass interface {
p changeToScope(Scope*, const ScopeType&);
Expand Down
22 changes: 11 additions & 11 deletions src-bootstrap/CliInterface.spice → src-bootstrap/driver.spice
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Imports
// Std imports
import "std/text/print";
import "std/os/cmd";
import "std/io/cli-parser";
Expand Down Expand Up @@ -37,20 +37,20 @@ public type CliOptions struct {
/**
* Helper class to setup the cli interface and command line parser
*/
public type CliInterface struct {
public type Driver struct {
CliParser cliParser
public CliOptions cliOptions
public bool shouldCompile = false
public bool shouldInstall = false
public bool shouldExecute = false
}

public p CliInterface.create() {
public p Driver.create() {
this.cliParser = CliParser("Spice", "Spice programming language");
this.cliParser.setFooter("(c) Marc Auberer 2021-2023");

// Add version flag
this.cliParser.setVersion("Spice version 0.14.3\nbuilt by: GitHub Actions\n\n(c) Marc Auberer 2021-2023");
this.cliParser.setVersion("Spice version 0.18.3\nbuilt by: GitHub Actions\n\n(c) Marc Auberer 2021-2023");

// Create sub-commands
this.addBuildSubcommand();
Expand All @@ -64,7 +64,7 @@ public p CliInterface.create() {
/**
* Initialize the cli options based on the input of the user
*/
public p CliInterface.enrich() {
public p Driver.enrich() {
// Propagate target information
if this.cliOptions.targetTriple.isEmpty() && this.cliOptions.targetArch.isEmpty() {
// ToDo: Extend
Expand All @@ -81,7 +81,7 @@ public p CliInterface.enrich() {
/**
* Add build subcommand to cli interface
*/
p CliInterface.addBuildSubcommand() {
p Driver.addBuildSubcommand() {
// Create sub-command itself
CliSubcommand& subCmd = this.cliParser.createSubcommand("build", "Builds your Spice program and emits an executable");
subCmd.addAlias("b");
Expand Down Expand Up @@ -112,7 +112,7 @@ p CliInterface.addBuildSubcommand() {
/**
* Add run subcommand to cli interface
*/
p CliInterface.addRunSubcommand() {
p Driver.addRunSubcommand() {
// Create sub-command itself
CliSubcommand& subCmd = this.cliParser.createSubcommand("run", "Builds your Spice program and runs it immediately");
subCmd.addAlias("r");
Expand All @@ -132,7 +132,7 @@ p CliInterface.addRunSubcommand() {
/**
* Add install subcommand to cli interface
*/
p CliInterface.addInstallSubcommand() {
p Driver.addInstallSubcommand() {
// Create sub-command itself
CliSubcommand& subCmd = this.cliParser.createSubcommand("install", "Builds your Spice program and installs it to a directory in the PATH variable");
subCmd.addAlias("i");
Expand All @@ -143,15 +143,15 @@ p CliInterface.addInstallSubcommand() {
/**
* Add uninstall subcommand to cli interface
*/
p CliInterface.addUninstallSubcommand() {
p Driver.addUninstallSubcommand() {
// Create sub-command itself
CliSubcommand& subCmd = this.cliParser.createSubcommand("uninstall", "Builds your Spice program and runs it immediately");
subCmd.addAlias("u");

addCompileSubcommandOptions(subCmd);
}

p CliInterface.addCompileSubcommandOptions(CliSubcommand& subCmd) {
p Driver.addCompileSubcommandOptions(CliSubcommand& subCmd) {
// --debug-output
CliOption<bool>& debugOutputFlag = subCmd.addFlag("--debug-output", this.cliOptions.printDebugOutput, "Enable debug output");
debugOutputFlag.addAlias("-d");
Expand Down Expand Up @@ -200,6 +200,6 @@ p CliInterface.addCompileSubcommandOptions(CliSubcommand& subCmd) {
* @param argv Argument vector
* @return Return code
*/
public f<int> CliInterface.parse(int argc, string[] argv) {
public f<int> Driver.parse(int argc, string[] argv) {
return this.cliParser.parse(argc, argv);
}
56 changes: 0 additions & 56 deletions src-bootstrap/exception/IRError.spice

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Imports
import "../util/CodeLoc" as cl;
// Own imports
import "../reader/code-loc";

public type CliErrorType enum {
INCOMPLETE_TARGET_TRIPLE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Imports
import "../util/CodeLoc" as cl;
import "../util/code-loc";

public type LexerErrorType enum {
TOKENIZING_FAILED
Expand All @@ -19,7 +19,7 @@ public type ParserError struct {
* @param errorType Type of the error
* @param message Error message suffix
*/
public p ParserError.ctor(const cl::CodeLoc* codeLoc, const LexerErrorType errorType, const string message) {
public p ParserError.ctor(const CodeLoc* codeLoc, const LexerErrorType errorType, const string message) {
this.errorMessage = "[Error|Lexer] " + codeLoc.toPrettyString() + ": " + this.getMessagePrefix(errorType) + ": " + message;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Imports
import "../util/CodeLoc" as cl;
import "../util/code-loc";

public type LinkerErrorType enum {
LINKER_NOT_FOUND,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Imports
import "../util/CodeLoc" as cl;
import "../util/code-loc";

public type ParserErrorType enum {
PARSING_FAILED,
Expand All @@ -21,7 +21,7 @@ public type ParserError struct {
* @param errorType Type of the error
* @param message Error message suffix
*/
public p ParserError.ctor(const cl::CodeLoc* codeLoc, const ParserErrorType errorType, const string message) {
public p ParserError.ctor(const CodeLoc* codeLoc, const ParserErrorType errorType, const string message) {
this.errorMessage = "[Error|Parser] " + codeLoc.toPrettyString() + ": " + this.getMessagePrefix(errorType) + ": " + message;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Imports
import "../ast/AstNodes" as ast;
import "../util/CodeLoc" as cl;
// Own imports
import "../ast/ast-nodes";
import "../reader/code-loc";

public type SemanticErrorType enum {
REFERENCED_UNDEFINED_FUNCTION,
Expand Down Expand Up @@ -74,7 +74,7 @@ public type SemanticError struct {
string message
}

public p SemanticError.ctor(const ast::AstNode* node, const SemanticErrorType errorType, const string message) {
public p SemanticError.ctor(const AstNode* node, const SemanticErrorType errorType, const string message) {
this.errorMessage = "[Error|Semantic] " + node.codeLoc.toPrettyString() + ":\n" + this.getMessagePrefix(errorType) +
": " + message + "\n\n" + node.errorMessage;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import "../exception/ErrorManager";
import "../global/RuntimeModuleManager";
import "../linker/ExternalLinkerInterface";
import "../util/CodeLoc";
import "../util/Timer";
import "../CliInterface";
// Own imports
import "../global/runtime-module-manager";
import "../linker/external-linker-interface";
import "../reader/code-loc";
import "../util/timer";
import "../driver";

// Constants
public const string MAIN_FILE_NAME = "root";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Imports
// Std imports

// Own imports

public type Generator struct {

Expand Down
2 changes: 0 additions & 2 deletions src-bootstrap/lexer/Lexer.spice

This file was deleted.

Loading