From c7fad53ab2a6c99ba24a7b84401024cdb2b32c5b Mon Sep 17 00:00:00 2001 From: Yujia Qiao Date: Wed, 3 Nov 2021 21:24:14 +0800 Subject: [PATCH] hack. see all array type in funct proto as ptrs --- llvm.cc | 18 ++++++++++++++++-- llvm.h | 1 + parser.h | 4 ++-- type.h | 1 + visitor.cc | 10 +++++++++- 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/llvm.cc b/llvm.cc index 5d3b889..9096ee4 100644 --- a/llvm.cc +++ b/llvm.cc @@ -8,6 +8,16 @@ llvm::Type* llvmWrapper::getChar() { return llvm::Type::getInt8Ty(*ctx); } llvm::Type* llvmWrapper::getDouble() { return llvm::Type::getDoubleTy(*ctx); } llvm::Type* llvmWrapper::getType(Type type) { + auto baseType = getBaseType(type); + if (type.isArray) + return llvm::ArrayType::get(baseType, type.arraySize); + else if (type.isPointer) + return baseType->getPointerTo(); + else + return baseType; +} + +llvm::Type* llvmWrapper::getBaseType(Type type) { switch (type.base) { case Type::Base::INT: return getInt(); @@ -61,7 +71,11 @@ llvm::Value* llvmWrapper::implictConvert(llvm::Value* v, llvm::Type* t) { abortMsg("can't implict convert double into int"); else return builder->CreateIntCast(v, t, true, "toint"); - } else - abortMsg("unimplemented implict convert"); + } else if (t->isArrayTy()) { + return v; + } else if (t->isPointerTy()) { + return builder->CreateGEP(v, 0); + } + abortMsg("unimplemented implict convert"); return nullptr; } diff --git a/llvm.h b/llvm.h index 2309eec..f162ac6 100644 --- a/llvm.h +++ b/llvm.h @@ -27,6 +27,7 @@ struct llvmWrapper { llvm::Type* getChar(); llvm::Type* getDouble(); llvm::Type* getType(Type t); + llvm::Type* getBaseType(Type t); llvm::Value* convertToTruthy(llvm::Value*); llvm::Value* implictConvert(llvm::Value*, llvm::Type*); llvm::AllocaInst* createEntryBlockAlloca(llvm::Function* fun, diff --git a/parser.h b/parser.h index b64e620..ae37f29 100644 --- a/parser.h +++ b/parser.h @@ -29,8 +29,8 @@ class Parser { BlockStmt* forStmt(); // FOR '(' VAR_DECL EXPRESSION; EXPRESSION ')' WhileStmt* whileStmt(); // WHILE '(' EXPRESSION ')' STMT ReturnStmt* returnStmt(); // RETURN EXPR; - TypedVar typedVar(); // (VAR | INT | DOUBLE | CHAR) ID - VarDecl* varDecl(Type type, Token id); // TYPE ('['SIZE']')? IDENTIFIER + TypedVar typedVar(); // (VAR | INT | DOUBLE | CHAR) '*'? ID ('['SIZE']')? + VarDecl* varDecl(Type type, Token id); // TYPEDVAR // (EQUAL EXPRESSION)? ; FunDecl* funDecl(Type type, Token id); // TYPEDVAR '(' ARGS? ')' BLOCK? diff --git a/type.h b/type.h index 41c7375..182f56a 100644 --- a/type.h +++ b/type.h @@ -7,6 +7,7 @@ struct Type { enum class Base { VOID, INT, DOUBLE, CHAR, ARRAY, BOOL, FUNCTION } base; int arraySize; bool isArray; + bool isPointer; bool operator==(const Type& rhs) const { return base == rhs.base && arraySize == rhs.arraySize && isArray == rhs.isArray; diff --git a/visitor.cc b/visitor.cc index 593321e..06a9ed1 100644 --- a/visitor.cc +++ b/visitor.cc @@ -271,7 +271,11 @@ void CodeGenExprVisitor::visit(Call* expr) { auto protoArg = fun->getArg(i++); v.visit(a); auto val = v.getValue(); - val = l.implictConvert(val, protoArg->getType()); + if (protoArg->getType()->isPointerTy()) { + // FIXME: a hack. see all array type in function prototype as ptrs + } else { + val = l.implictConvert(val, protoArg->getType()); + } args.push_back(val); if (!args.back()) { value = nullptr; @@ -339,6 +343,10 @@ void CodeGenVisitor::visit(FunDecl* st) { std::vector args; for (auto [type, token] : st->args) { auto t = l.getType(type); + if (t->isArrayTy()) { + // FIXME: hack, see all array type in funct proto as ptrs + t = t->getArrayElementType()->getPointerTo(); + } args.push_back(t); }