123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- #include "CodegenVisitor.h"
- #include "CodeGeneration.h"
- #include "Type.h"
- #include "Builtin.h"
- #include "ErrorReporting.h"
- #include <llvm/IR/Constants.h>
- #include <llvm/IR/IRBuilder.h>
- using namespace qlow;
- llvm::Value* ExpressionCodegenVisitor::visit(sem::LocalVariableExpression& lve, llvm::IRBuilder<>& builder)
- {
- assert(lve.var->allocaInst != nullptr);
- if (llvm::dyn_cast<llvm::AllocaInst>(lve.var->allocaInst)) {
- llvm::Value* val = builder.CreateLoad(lve.var->allocaInst);
- return val;
- }
- else {
- return lve.var->allocaInst;
- }
- }
- llvm::Value* ExpressionCodegenVisitor::visit(sem::UnaryOperation& unop, llvm::IRBuilder<>& builder)
- {
- using llvm::Value;
- auto value = unop.arg->accept(*this, builder);
- auto& type = unop.arg->type;
-
- if (type == sem::NO_TYPE)
- throw "invalid type to negate";
-
- }
- llvm::Value* ExpressionCodegenVisitor::visit(sem::BinaryOperation& binop, llvm::IRBuilder<>& builder)
- {
- using llvm::Value;
- using sem::Type;
- auto left = binop.left->accept(*this, builder);
- auto right = binop.right->accept(*this, builder);
-
- auto& leftType = binop.left->type;
- auto& rightType = binop.right->type;
-
- sem::Method* operation = binop.operationMethod;
-
- if (operation != nullptr) {
- if (sem::NativeMethod* nm = dynamic_cast<sem::NativeMethod*>(operation); nm) {
- return nm->generateCode(builder, {left, right});
- }
- else
- throw "only native operations supported at the moment";
- }
- else {
- throw "internal error: operation method null";
- }
-
- if (left == nullptr) {
- printf("WOW: %s\n", binop.left->toString().c_str());
- }
-
- Value* implicitelyCastedRight = right;
-
-
-
-
-
-
- }
- llvm::Value* ExpressionCodegenVisitor::visit(sem::CastExpression& cast, llvm::IRBuilder<>& builder)
- {
-
- }
- llvm::Value* ExpressionCodegenVisitor::visit(sem::NewArrayExpression& naexpr, llvm::IRBuilder<>& builder)
- {
- using llvm::Value;
-
- }
- llvm::Value* ExpressionCodegenVisitor::visit(sem::MethodCallExpression& call, llvm::IRBuilder<>& builder)
- {
-
- }
- llvm::Value* ExpressionCodegenVisitor::visit(sem::FieldAccessExpression& access, llvm::IRBuilder<>& builder)
- {
-
- }
- llvm::Value* ExpressionCodegenVisitor::visit(sem::AddressExpression& node, llvm::IRBuilder<>& builder)
- {
- using llvm::Value;
- Value* lvalue = node.target->accept(fg.lvalueVisitor, builder);
-
-
- if (auto* allocaInst = llvm::dyn_cast<llvm::AllocaInst>(lvalue)) {
- return lvalue;
- }
- else {
- return lvalue;
- }
- }
- llvm::Value* ExpressionCodegenVisitor::visit(sem::IntConst& node, llvm::IRBuilder<>& builder)
- {
- return llvm::ConstantInt::get(builder.getContext(),
- llvm::APInt(32, std::to_string(node.value), 10));
- }
- llvm::Value* ExpressionCodegenVisitor::visit(sem::ThisExpression& thisExpr, llvm::IRBuilder<>& builder)
- {
- return thisExpr.allocaInst;
- }
- llvm::Value* LValueVisitor::visit(sem::Expression& e, llvm::IRBuilder<>& builder)
- {
- throw "cannot construct lvalue from expression";
- }
- llvm::Value* LValueVisitor::visit(sem::LocalVariableExpression& lve, llvm::IRBuilder<>& builder)
- {
- assert(lve.var->allocaInst != nullptr);
-
- if (llvm::dyn_cast<llvm::AllocaInst>(lve.var->allocaInst)) {
- return lve.var->allocaInst;
-
-
- }
- else if (llvm::dyn_cast<llvm::PointerType> (lve.var->allocaInst->getType())) {
- return lve.var->allocaInst;
- }
- else {
- throw "unable to find alloca instance of local variable";
- }
- }
- llvm::Value* LValueVisitor::visit(sem::FieldAccessExpression& access, llvm::IRBuilder<>& builder)
- {
-
-
- using llvm::Value;
- using llvm::Type;
-
-
- }
- llvm::Value* StatementVisitor::visit(sem::DoEndBlock& assignment,
- qlow::gen::FunctionGenerator& fg)
- {
- for (auto& statement : assignment.statements) {
- statement->accept(*this, fg);
- }
- return nullptr;
- }
- llvm::Value* StatementVisitor::visit(sem::IfElseBlock& ifElseBlock,
- qlow::gen::FunctionGenerator& fg)
- {
- using llvm::Value;
- using llvm::BasicBlock;
-
- llvm::IRBuilder<> builder(fg.getContext());
- builder.SetInsertPoint(fg.getCurrentBlock());
- auto condition = ifElseBlock.condition->accept(fg.expressionVisitor, builder);
-
- llvm::Function* function = fg.getCurrentBlock()->getParent();
-
- BasicBlock* thenB = BasicBlock::Create(fg.getContext(), "then", function);
- BasicBlock* elseB = BasicBlock::Create(fg.getContext(), "else", function);
- BasicBlock* merge = BasicBlock::Create(fg.getContext(), "merge", function);
-
- Value* boolCond = builder.CreateIntCast(condition, llvm::Type::getInt1Ty(fg.getContext()), false);
-
- builder.CreateCondBr(boolCond, thenB, elseB);
-
- fg.pushBlock(thenB);
- ifElseBlock.ifBlock->accept(*this, fg);
- builder.SetInsertPoint(thenB);
- if (!thenB->getTerminator())
- builder.CreateBr(merge);
- fg.popBlock();
- fg.pushBlock(elseB);
- ifElseBlock.elseBlock->accept(*this, fg);
- builder.SetInsertPoint(elseB);
- if (!elseB->getTerminator())
- builder.CreateBr(merge);
- fg.popBlock();
- fg.popBlock();
- fg.pushBlock(merge);
- }
- llvm::Value* StatementVisitor::visit(sem::WhileBlock& whileBlock,
- qlow::gen::FunctionGenerator& fg)
- {
- using llvm::Value;
- using llvm::BasicBlock;
-
- llvm::IRBuilder<> builder(fg.getContext());
- builder.SetInsertPoint(fg.getCurrentBlock());
-
- llvm::Function* function = fg.getCurrentBlock()->getParent();
-
- BasicBlock* startloop = BasicBlock::Create(fg.getContext(), "startloop", function);
- BasicBlock* body = BasicBlock::Create(fg.getContext(), "loopbody", function);
- BasicBlock* merge = BasicBlock::Create(fg.getContext(), "merge", function);
-
-
-
- builder.CreateBr(startloop);
- fg.pushBlock(startloop);
- builder.SetInsertPoint(startloop);
- auto condition = whileBlock.condition->accept(fg.expressionVisitor, builder);
- Value* boolCond = builder.CreateIntCast(condition, llvm::Type::getInt1Ty(fg.getContext()), false);
- builder.CreateCondBr(condition, body, merge);
- fg.popBlock();
-
- fg.pushBlock(body);
- whileBlock.body->accept(*this, fg);
- builder.SetInsertPoint(body);
- builder.CreateBr(startloop);
- fg.popBlock();
- fg.pushBlock(merge);
- return nullptr;
- }
- llvm::Value* StatementVisitor::visit(sem::AssignmentStatement& assignment,
- qlow::gen::FunctionGenerator& fg)
- {
- Logger& logger = Logger::getInstance();
- llvm::IRBuilder<> builder(fg.getContext());
- builder.SetInsertPoint(fg.getCurrentBlock());
-
- auto val = assignment.value->accept(fg.expressionVisitor, builder);
- auto target = assignment.target->accept(fg.lvalueVisitor, builder);
-
- return builder.CreateStore(val, target);
-
- if (auto* targetVar =
- dynamic_cast<sem::LocalVariableExpression*>(assignment.target.get()); targetVar) {
- logger.debug() << "assigning to LocalVariableExpression" << std::endl;
- builder.CreateStore(val, targetVar->var->allocaInst);
- }
- else if (auto* targetVar =
- dynamic_cast<sem::FieldAccessExpression*>(assignment.target.get()); targetVar) {
-
- logger.debug() << "assigning to FieldAccessExpression" << std::endl;
- if (targetVar->target) {
- llvm::Value* target = targetVar->target->accept(fg.expressionVisitor, builder);
-
-
- auto elementPtr = llvm::ConstantPointerNull::get(val->getType()->getPointerTo());
-
- builder.CreateStore(val, elementPtr);
- }
- else {
- throw "field access without target";
- }
- }
- else {
- logger.debug() << "assigning to instance of " << assignment.target->toString() << std::endl;
- throw "only local variables are assignable at the moment";
- }
-
- return nullptr;
-
- }
- llvm::Value* StatementVisitor::visit(sem::ReturnStatement& returnStatement,
- qlow::gen::FunctionGenerator& fg)
- {
- llvm::IRBuilder<> builder(fg.getContext());
- builder.SetInsertPoint(fg.getCurrentBlock());
- auto val = returnStatement.value->accept(fg.expressionVisitor, builder);
- builder.CreateRet(val);
- return val;
- }
- llvm::Value* StatementVisitor::visit(sem::FeatureCallStatement& fc, gen::FunctionGenerator& fg)
- {
- llvm::Module* module = fg.getModule();
- llvm::IRBuilder<> builder(fg.getContext());
- builder.SetInsertPoint(fg.getCurrentBlock());
-
-
- return fc.expr->accept(fg.expressionVisitor, builder);
-
-
-
- }
- llvm::Value* CastGenerator::generateCast(llvm::Value* toCast,
- llvm::IRBuilder<>& b)
- {
-
- }
|