Skip to content

Commit

Permalink
hack. see all array type in funct proto as ptrs
Browse files Browse the repository at this point in the history
  • Loading branch information
rapiz1 committed Nov 3, 2021
1 parent 523c62c commit c7fad53
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
18 changes: 16 additions & 2 deletions llvm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
1 change: 1 addition & 0 deletions type.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -339,6 +343,10 @@ void CodeGenVisitor::visit(FunDecl* st) {
std::vector<llvm::Type*> 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);
}

Expand Down

0 comments on commit c7fad53

Please sign in to comment.