This commit is contained in:
Rafał Grodziński
2025-07-15 10:27:50 +09:00
parent 51115f5883
commit 9d991f46a2
2 changed files with 11 additions and 11 deletions

View File

@@ -53,7 +53,7 @@ shared_ptr<llvm::Module> ModuleBuilder::getModule() {
void ModuleBuilder::buildStatement(shared_ptr<Statement> statement) { void ModuleBuilder::buildStatement(shared_ptr<Statement> statement) {
switch (statement->getKind()) { switch (statement->getKind()) {
case StatementKind::FUNCTION: case StatementKind::FUNCTION:
buildFunctionDeclaration(dynamic_pointer_cast<StatementFunction>(statement)); buildFunction(dynamic_pointer_cast<StatementFunction>(statement));
break; break;
case StatementKind::RAW_FUNCTION: case StatementKind::RAW_FUNCTION:
buildRawFunction(dynamic_pointer_cast<StatementRawFunction>(statement)); buildRawFunction(dynamic_pointer_cast<StatementRawFunction>(statement));
@@ -84,15 +84,15 @@ void ModuleBuilder::buildStatement(shared_ptr<Statement> statement) {
} }
} }
void ModuleBuilder::buildFunctionDeclaration(shared_ptr<StatementFunction> statement) { void ModuleBuilder::buildFunction(shared_ptr<StatementFunction> statement) {
// get argument types // function types
vector<llvm::Type *> types; llvm::Type *returnType = typeForValueType(statement->getReturnValueType());
for (pair<string, shared_ptr<ValueType>> &arg : statement->getArguments()) { vector<llvm::Type *> argTypes;
types.push_back(typeForValueType(arg.second)); for (pair<string, shared_ptr<ValueType>> &arg : statement->getArguments())
} argTypes.push_back(typeForValueType(arg.second));
// build function declaration // build function declaration
llvm::FunctionType *funType = llvm::FunctionType::get(typeForValueType(statement->getReturnValueType()), types, false); llvm::FunctionType *funType = llvm::FunctionType::get(returnType, argTypes, false);
llvm::Function *fun = llvm::Function::Create(funType, llvm::GlobalValue::ExternalLinkage, statement->getName(), module.get()); llvm::Function *fun = llvm::Function::Create(funType, llvm::GlobalValue::ExternalLinkage, statement->getName(), module.get());
if (!setFun(statement->getName(), fun)) if (!setFun(statement->getName(), fun))
return; return;
@@ -107,7 +107,7 @@ void ModuleBuilder::buildFunctionDeclaration(shared_ptr<StatementFunction> state
int i=0; int i=0;
for (auto &arg : fun->args()) { for (auto &arg : fun->args()) {
string name = statement->getArguments()[i].first; string name = statement->getArguments()[i].first;
llvm::Type *type = types[i]; llvm::Type *type = argTypes[i];
arg.setName(name); arg.setName(name);
llvm::AllocaInst *alloca = builder->CreateAlloca(type, nullptr, name); llvm::AllocaInst *alloca = builder->CreateAlloca(type, nullptr, name);
@@ -137,7 +137,7 @@ void ModuleBuilder::buildRawFunction(shared_ptr<StatementRawFunction> statement)
for (pair<string, shared_ptr<ValueType>> &arg : statement->getArguments()) for (pair<string, shared_ptr<ValueType>> &arg : statement->getArguments())
argTypes.push_back(typeForValueType(arg.second)); argTypes.push_back(typeForValueType(arg.second));
// function declaration & body // build function declaration & body
llvm::FunctionType *funType = llvm::FunctionType::get(returnType, argTypes, false); llvm::FunctionType *funType = llvm::FunctionType::get(returnType, argTypes, false);
llvm::InlineAsm *rawFun = llvm::InlineAsm::get(funType, statement->getRawSource(), statement->getConstraints(), false, false, llvm::InlineAsm::AsmDialect::AD_Intel); llvm::InlineAsm *rawFun = llvm::InlineAsm::get(funType, statement->getRawSource(), statement->getConstraints(), false, false, llvm::InlineAsm::AsmDialect::AD_Intel);
if (!setRawFun(statement->getName(), rawFun)) if (!setRawFun(statement->getName(), rawFun))

View File

@@ -63,7 +63,7 @@ private:
stack<Scope> scopes; stack<Scope> scopes;
void buildStatement(shared_ptr<Statement> statement); void buildStatement(shared_ptr<Statement> statement);
void buildFunctionDeclaration(shared_ptr<StatementFunction> statement); void buildFunction(shared_ptr<StatementFunction> statement);
void buildRawFunction(shared_ptr<StatementRawFunction> statement); void buildRawFunction(shared_ptr<StatementRawFunction> statement);
void buildVarDeclaration(shared_ptr<StatementVariable> statement); void buildVarDeclaration(shared_ptr<StatementVariable> statement);
void buildAssignment(shared_ptr<StatementAssignment> statement); void buildAssignment(shared_ptr<StatementAssignment> statement);