Addes statement raw function

This commit is contained in:
Rafał Grodziński
2025-07-12 22:20:32 +09:00
parent 48cc745e79
commit c7812ccf43
7 changed files with 96 additions and 3 deletions

2
.vscode/launch.json vendored
View File

@@ -6,7 +6,7 @@
"type": "lldb-dap",
"request": "launch",
"program": "${command:cmake.launchTargetPath}",
"args": ["-v", "${workspaceFolder}/samples/hello.brc"],
"args": ["-v", "${workspaceFolder}/samples/test.brc"],
"cwd": "${workspaceFolder}",
"internalConsoleOptions": "openOnSessionStart",
}

View File

@@ -1,5 +1,7 @@
@extern putchar fun: character sint32 -> sint32
// ./build/brb samples/test.brc -S -x86-asm-syntax=intel
/*
User type
name data<u8, 32>
@@ -18,8 +20,14 @@ i u32 <- 0, rep text[i] != 0:
// text data<u8> <- "Hello world!"
/*addStuff asm<+r, r>: num1 u32, num2 u32 -> u32
add $1, $0
;*/
main fun -> sint32
text data<sint32> <- "Hello string!\n"
//text data<sint32> <- "Hello string!\n"
abc sint32 <- 0
//addStuff()
ret 0
;

View File

@@ -14,6 +14,7 @@
#include "Parser/Expression/ExpressionBlock.h"
#include "Parser/Statement/StatementFunction.h"
#include "Parser/Statement/StatementRawFunction.h"
#include "Parser/Statement/StatementVariable.h"
#include "Parser/Statement/StatementAssignment.h"
#include "Parser/Statement/StatementReturn.h"
@@ -54,6 +55,9 @@ void ModuleBuilder::buildStatement(shared_ptr<Statement> statement) {
case StatementKind::FUNCTION:
buildFunctionDeclaration(dynamic_pointer_cast<StatementFunction>(statement));
break;
case StatementKind::RAW_FUNCTION:
buildRawFunction(dynamic_pointer_cast<StatementRawFunction>(statement));
break;
case StatementKind::VARIABLE:
buildVarDeclaration(dynamic_pointer_cast<StatementVariable>(statement));
break;
@@ -126,6 +130,29 @@ void ModuleBuilder::buildFunctionDeclaration(shared_ptr<StatementFunction> state
markError(0, 0, errorMessage);
}
void ModuleBuilder::buildRawFunction(shared_ptr<StatementRawFunction> statement) {
llvm::FunctionType *funType = llvm::FunctionType::get(llvm::Type::getVoidTy(*context), nullptr, false);
llvm::InlineAsm *rawFun = llvm::InlineAsm::get(funType, statement->getRawSource(), "", false, false, llvm::InlineAsm::AsmDialect::AD_Intel);
if (!setRawFun(statement->getName(), rawFun))
return;
/*int res;
int a = 42;
int b = 13;
vector<llvm::Type *> types;
types.push_back(typeSint32);
types.push_back(typeSint32);
llvm::FunctionType *asmType = llvm::FunctionType::get(typeSint32, types, false);
llvm::InlineAsm *asmm = llvm::InlineAsm::get(asmType, "add $0, $1", "+{ebx},i", false, false, llvm::InlineAsm::AsmDialect::AD_Intel);
vector<llvm::Value *>argValues;
argValues.push_back(llvm::ConstantInt::get(typeSint32, 5, true));
argValues.push_back(llvm::ConstantInt::get(typeSint32, 4, true));
llvm::Value *valu = builder->CreateCall(asmm, llvm::ArrayRef(argValues));*/
}
void ModuleBuilder::buildVarDeclaration(shared_ptr<StatementVariable> statement) {
if (statement->getValueType()->getKind() == ValueTypeKind::DATA) {
vector<llvm::Value *> values = valuesForExpression(statement->getExpression());
@@ -518,7 +545,7 @@ llvm::AllocaInst* ModuleBuilder::getAlloca(string name) {
bool ModuleBuilder::setFun(string name, llvm::Function *fun) {
if (scopes.top().funMap[name] != nullptr) {
markError(0, 0, format("Function \"{}\" already defined", name));
markError(0, 0, format("Function \"{}\" already defined in scope", name));
return false;
}
@@ -540,6 +567,30 @@ llvm::Function* ModuleBuilder::getFun(string name) {
return nullptr;
}
bool ModuleBuilder::setRawFun(string name, llvm::Value *rawFun) {
if (scopes.top().rawFunMap[name] != nullptr) {
markError(0, 0, format("Raw function \"{}\" already defined in scope", name));
return false;
}
scopes.top().rawFunMap[name] = rawFun;
return true;
}
llvm::Value *ModuleBuilder::getRawFun(string name) {
stack<Scope> scopes = this->scopes;
while (!scopes.empty()) {
llvm::Value *rawFun = scopes.top().rawFunMap[name];
if (rawFun != nullptr)
return rawFun;
scopes.pop();
}
markError(0, 0, format("Raw function \"{}\" not defined in scope", name));
return nullptr;
}
llvm::Type *ModuleBuilder::typeForValueType(shared_ptr<ValueType> valueType, int count) {
switch (valueType->getKind()) {
case ValueTypeKind::NONE:

View File

@@ -10,6 +10,7 @@
#include <llvm/IR/Constants.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/IR/Verifier.h>
#include <llvm/IR/InlineAsm.h>
class Error;
class ValueType;
@@ -26,6 +27,7 @@ enum class ExpressionBinaryOperation;
class Statement;
class StatementFunction;
class StatementRawFunction;
class StatementVariable;
class StatementAssignment;
class StatementReturn;
@@ -39,6 +41,7 @@ using namespace std;
typedef struct {
map<string, llvm::AllocaInst*> allocaMap;
map<string, llvm::Function*> funMap;
map<string, llvm::Value*> rawFunMap;
} Scope;
class ModuleBuilder {
@@ -61,6 +64,7 @@ private:
void buildStatement(shared_ptr<Statement> statement);
void buildFunctionDeclaration(shared_ptr<StatementFunction> statement);
void buildRawFunction(shared_ptr<StatementRawFunction> statement);
void buildVarDeclaration(shared_ptr<StatementVariable> statement);
void buildAssignment(shared_ptr<StatementAssignment> statement);
void buildBlock(shared_ptr<StatementBlock> statement);
@@ -88,6 +92,9 @@ private:
bool setFun(string name, llvm::Function *fun);
llvm::Function *getFun(string name);
bool setRawFun(string name, llvm::Value *rawFun);
llvm::Value *getRawFun(string name);
llvm::Type *typeForValueType(shared_ptr<ValueType> valueType, int count = 0);
void markError(int line, int column, string message);

View File

@@ -10,6 +10,7 @@ enum class StatementKind {
BLOCK,
RETURN,
FUNCTION,
RAW_FUNCTION,
VARIABLE,
ASSIGNMENT,
REPEAT,

View File

@@ -0,0 +1,12 @@
#include "StatementRawFunction.h"
StatementRawFunction::StatementRawFunction(string name, string rawSource):
Statement(StatementKind::RAW_FUNCTION), name(name), rawSource(rawSource) { }
string StatementRawFunction::getName() {
return name;
}
string StatementRawFunction::getRawSource() {
return rawSource;
}

View File

@@ -0,0 +1,14 @@
#include "Parser/Statement/Statement.h"
class Expression;
class StatementRawFunction: public Statement {
private:
string name;
string rawSource;
public:
StatementRawFunction(string name, string rawSource);
string getName();
string getRawSource();
};