Merge branch 'master' of git.tuxzone.org:woc2013
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
#ifndef TYPE_H
|
#ifndef TYPE_H
|
||||||
#define TYPE_H
|
#define TYPE_H
|
||||||
|
|
||||||
|
#include "llvm/Type.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
enum class Type { BOOL, INT, STRING, VOID };
|
enum class Type { BOOL, INT, STRING, VOID };
|
||||||
|
|
||||||
std::string typeToString(Type type);
|
std::string typeToString(Type type);
|
||||||
|
llvm::Type* typeToLLVMType(Type type);
|
||||||
|
|
||||||
#endif // TYPE_H
|
#endif // TYPE_H
|
||||||
|
|||||||
@@ -88,6 +88,38 @@ void CodeGenVisitor::visit(FunctionCallExpression* e) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void CodeGenVisitor::visit(FunctionDefinition* e) {
|
void CodeGenVisitor::visit(FunctionDefinition* e) {
|
||||||
|
std::vector<llvm::Type*> argTypes;
|
||||||
|
auto params = e->getParams()->getParameters();
|
||||||
|
auto iter = params.begin();
|
||||||
|
auto end = params.end();
|
||||||
|
for (; iter != end; ++iter) {
|
||||||
|
Parameter& p = (*iter);
|
||||||
|
argTypes.push_back(typeToLLVMType(p.first));
|
||||||
|
}
|
||||||
|
|
||||||
|
llvm::FunctionType* ft = llvm::FunctionType::get(typeToLLVMType(e->getType()), argTypes, false);
|
||||||
|
llvm::Function *f = llvm::Function::Create(ft, llvm::Function::ExternalLinkage, e->getName(), module_);
|
||||||
|
|
||||||
|
// If f conflicted, there was already something named 'Name'. If it has a body,
|
||||||
|
// don't allow redefinition or reextern.
|
||||||
|
if (f->getName() != e->getName()) {
|
||||||
|
f->eraseFromParent();
|
||||||
|
f = module_->getFunction(e->getName());
|
||||||
|
|
||||||
|
// If f already has a body, reject this.
|
||||||
|
if (!f->empty()) {
|
||||||
|
throw "redefinition of function";
|
||||||
|
}
|
||||||
|
|
||||||
|
// If f took a different number of args, reject.
|
||||||
|
if (f->arg_size() != e->getParams()->getParameters().size()) {
|
||||||
|
throw "redefinition of function with different # of arguments";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO set arg names
|
||||||
|
|
||||||
|
value_ = f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CodeGenVisitor::visit(IfStatement* e) {
|
void CodeGenVisitor::visit(IfStatement* e) {
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#include "AST/Type.h"
|
#include "AST/Type.h"
|
||||||
|
#include "llvm/DerivedTypes.h"
|
||||||
|
#include "llvm/LLVMContext.h"
|
||||||
|
|
||||||
std::string typeToString(Type type) {
|
std::string typeToString(Type type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
@@ -12,5 +14,21 @@ std::string typeToString(Type type) {
|
|||||||
return "void";
|
return "void";
|
||||||
default:
|
default:
|
||||||
return "ERROR";
|
return "ERROR";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
llvm::Type* typeToLLVMType(Type type) {
|
||||||
|
switch (type) {
|
||||||
|
case Type::BOOL:
|
||||||
|
return llvm::Type::getInt1Ty(llvm::getGlobalContext());
|
||||||
|
case Type::INT:
|
||||||
|
return llvm::Type::getInt32Ty(llvm::getGlobalContext());
|
||||||
|
case Type::STRING:
|
||||||
|
return llvm::Type::getInt8PtrTy(llvm::getGlobalContext());
|
||||||
|
case Type::VOID:
|
||||||
|
return llvm::Type::getVoidTy(llvm::getGlobalContext());
|
||||||
|
default:
|
||||||
|
// TODO error
|
||||||
|
return llvm::Type::getVoidTy(llvm::getGlobalContext());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user