Added args and return type

This commit is contained in:
Rafał Grodziński
2025-07-15 10:23:32 +09:00
parent 5616036c17
commit 51115f5883
4 changed files with 30 additions and 24 deletions

View File

@@ -131,29 +131,17 @@ void ModuleBuilder::buildFunctionDeclaration(shared_ptr<StatementFunction> state
}
void ModuleBuilder::buildRawFunction(shared_ptr<StatementRawFunction> statement) {
// get argument types
vector<llvm::Type *> types;
// function types
llvm::Type *returnType = typeForValueType(statement->getReturnValueType());
vector<llvm::Type *> argTypes;
for (pair<string, shared_ptr<ValueType>> &arg : statement->getArguments())
argTypes.push_back(typeForValueType(arg.second));
llvm::FunctionType *funType = llvm::FunctionType::get(llvm::Type::getVoidTy(*context), types, false);
// function declaration & body
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);
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) {

View File

@@ -241,6 +241,8 @@ shared_ptr<Statement> Parser::matchStatementRawFunction() {
string name;
string constraints;
vector<pair<string, shared_ptr<ValueType>>> arguments;
shared_ptr<ValueType> returnType = ValueType::NONE;
string rawSource;
// name
@@ -260,6 +262,10 @@ shared_ptr<Statement> Parser::matchStatementRawFunction() {
markError({TokenKind::GREATER}, {});
}
// arguments
// return type
// consume new line
if (!tryMatchingTokenKinds({TokenKind::NEW_LINE}, true, true)) {
markError(TokenKind::NEW_LINE, {});
@@ -281,7 +287,7 @@ shared_ptr<Statement> Parser::matchStatementRawFunction() {
return nullptr;
}
return make_shared<StatementRawFunction>(name, constraints, rawSource);
return make_shared<StatementRawFunction>(name, constraints, arguments, returnType, rawSource);
}
shared_ptr<Statement> Parser::matchStatementBlock(vector<TokenKind> terminalTokenKinds) {

View File

@@ -1,7 +1,7 @@
#include "StatementRawFunction.h"
StatementRawFunction::StatementRawFunction(string name, string constraints, string rawSource):
Statement(StatementKind::RAW_FUNCTION), name(name), constraints(constraints), rawSource(rawSource) { }
StatementRawFunction::StatementRawFunction(string name, string constraints, vector<pair<string, shared_ptr<ValueType>>> arguments, shared_ptr<ValueType> returnValueType, string rawSource):
Statement(StatementKind::RAW_FUNCTION), name(name), constraints(constraints), arguments(arguments), returnValueType(returnValueType), rawSource(rawSource) { }
string StatementRawFunction::getName() {
return name;
@@ -11,6 +11,14 @@ string StatementRawFunction::getConstraints() {
return constraints;
}
vector<pair<string, shared_ptr<ValueType>>> StatementRawFunction::getArguments() {
return arguments;
}
shared_ptr<ValueType> StatementRawFunction::getReturnValueType() {
return returnValueType;
}
string StatementRawFunction::getRawSource() {
return rawSource;
}

View File

@@ -1,16 +1,20 @@
#include "Parser/Statement/Statement.h"
class Expression;
class ValueType;
class StatementRawFunction: public Statement {
private:
string name;
string constraints;
vector<pair<string, shared_ptr<ValueType>>> arguments;
shared_ptr<ValueType> returnValueType;
string rawSource;
public:
StatementRawFunction(string name, string constraints, string rawSource);
StatementRawFunction(string name, string constraints, vector<pair<string, shared_ptr<ValueType>>> arguments, shared_ptr<ValueType> returnValueType, string rawSource);
string getName();
string getConstraints();
vector<pair<string, shared_ptr<ValueType>>> getArguments();
shared_ptr<ValueType> getReturnValueType();
string getRawSource();
};