Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.48 KB | None | 0 0
  1. #include <codegen.h>
  2.  
  3. #include "syntaxtree.h"
  4.  
  5. // grammar files
  6. #include "grammar.hpp"
  7.  
  8.  
  9. #include <llvm/Module.h>
  10. #include <llvm/Function.h>
  11. #include <llvm/Type.h>
  12. #include <llvm/DerivedTypes.h>
  13. #include <llvm/LLVMContext.h>
  14. #include <llvm/PassManager.h>
  15. #include <llvm/Instructions.h>
  16. #include <llvm/CallingConv.h>
  17. #include <llvm/Bitcode/ReaderWriter.h>
  18. #include <llvm/Analysis/Verifier.h>
  19. #include <llvm/Assembly/PrintModulePass.h>
  20. #include <llvm/Support/IRBuilder.h>
  21. #include <llvm/Target/TargetSelect.h>
  22. #include <llvm/ExecutionEngine/GenericValue.h>
  23. #include <llvm/ExecutionEngine/JIT.h>
  24. #include <llvm/Support/raw_ostream.h>
  25.  
  26.  
  27.  
  28.  
  29. #include <iostream>
  30.  
  31.  
  32.  
  33.  
  34.  
  35. using namespace llvm;
  36.  
  37.  
  38.  
  39. bool CodeGenerator::generate(Program *program)
  40. {
  41.     m_module = new Module(program->name(), getGlobalContext());
  42.  
  43.     std::vector<const Type *> args;
  44.     FunctionType *mainType = FunctionType::get(Type::getVoidTy(getGlobalContext()), args, false);
  45.     m_mainFunction = Function::Create(mainType, GlobalValue::InternalLinkage, program->name(), m_module);
  46.  
  47.     BasicBlock *mainBlock = BasicBlock::Create(getGlobalContext(), "main", m_mainFunction);
  48.     pushScope(new CodeScope(mainBlock));
  49.     program->codeGen(*this);
  50.     ReturnInst::Create(getGlobalContext(), mainBlock);
  51.     popScope();
  52.  
  53. //    if (m_error)
  54. //        return false;
  55.  
  56.     PassManager pm;
  57.     pm.add(createPrintModulePass(&outs()));
  58.  
  59.     return pm.run(*m_module);
  60. }
  61.  
  62.  
  63.  
  64. void CodeGenerator::error(const std::string &msg, unsigned int line)
  65. {
  66.     m_error = true;
  67.     std::cerr << "ERROR:" << msg << std::endl;
  68. }
  69.  
  70.  
  71. void CodeGenerator::warning(const std::string &msg, unsigned int line)
  72. {
  73.     std::cerr << "WARNING:" << msg << std::endl;
  74. }
  75.  
  76.  
  77.  
  78. void CodeGenerator::run()
  79. {
  80.     InitializeNativeTarget();
  81.     ExecutionEngine *ee = ExecutionEngine::create(m_module, false);
  82.     if (ee == NULL)
  83.     {
  84.         std::cerr << "can't create execution engine" << std::endl;
  85.         return;
  86.     }
  87.  
  88.     std::vector<GenericValue> noargs;
  89.     GenericValue v = ee->runFunction(m_mainFunction, noargs);
  90. }
  91.  
  92.  
  93.  
  94.  
  95. const Type *typeById(Identifier *tp)
  96. {
  97.     if (tp->name() == "integer")
  98.         return Type::getInt32Ty(getGlobalContext());
  99.     else if (tp->name() == "float")
  100.         return Type::getFloatTy(getGlobalContext());
  101.  
  102.     return Type::getVoidTy(getGlobalContext());
  103. }
  104.  
  105.  
  106. //llvm::Value *Node::codeGen(CodeGenerator &gen)
  107. //{
  108. //    std::cout << "generating Node" << std::endl;
  109. //    return NULL;
  110. //}
  111.  
  112.  
  113.  
  114. Value *Program::codeGen(CodeGenerator &gen)
  115. {
  116.     std::cout << "generating Program" << std::endl;
  117.     return m_body->codeGen(gen);
  118. }
  119.  
  120.  
  121. llvm::Value *Block::codeGen(CodeGenerator &gen)
  122. {
  123.     std::cout << "generating block" << std::endl;
  124.  
  125.     for (VariableList::iterator it = m_decls->begin(); it != m_decls->end(); ++it)
  126.     {
  127.         Value *val = (*it)->codeGen(gen);
  128.         gen.local()->addDeclaration((*it)->name(), val);
  129.     }
  130.  
  131.     Value *last = NULL;
  132.  
  133.     for (StatementList::iterator it = m_stmts->begin(); it != m_stmts->end(); ++it)
  134.         last = (*it)->codeGen(gen);
  135.  
  136.     return last;
  137. }
  138.  
  139. Value *Variable::codeGen(CodeGenerator &gen)
  140. {
  141.     std::cout << "generating variable: " << m_var->name() << ": " << m_type->name() << std::endl;
  142.  
  143.     if (gen.local()->contains(m_var->name()))
  144.     {
  145.         gen.error("variable with name:" + m_var->name() + " is already declared");
  146.         return NULL;
  147.     }
  148.  
  149.     // variable declaration
  150.     return new AllocaInst(typeById(m_type.get()), m_var->name(), gen.local()->block());
  151. }
  152.  
  153.  
  154. Value *Integer::codeGen(CodeGenerator &gen)
  155. {
  156.     return ConstantInt::get(Type::getInt32Ty(getGlobalContext()), m_value);
  157. }
  158.  
  159.  
  160. Value *Float::codeGen(CodeGenerator &gen)
  161. {
  162.     return ConstantFP::get(Type::getFloatTy(getGlobalContext()), m_value);
  163. }
  164.  
  165.  
  166. Value *String::codeGen(CodeGenerator &gen)
  167. {
  168.     return ConstantArray::get(getGlobalContext(), m_value);
  169. }
  170.  
  171.  
  172. Value *Identifier::codeGen(CodeGenerator &gen)
  173. {
  174.     if (!gen.local()->contains(m_name))
  175.     {
  176.         gen.error("undeclared variable:" + m_name);
  177.         return NULL;
  178.     }
  179.  
  180.     return new LoadInst(gen.local()->lookup(m_name), "", gen.local()->block());
  181. }
  182.  
  183.  
  184. Value *AssignStatement::codeGen(CodeGenerator &gen)
  185. {
  186.     std::cout << "generating assignment:" << m_left->name() << ":" << std::endl;
  187.  
  188.     if (!gen.local()->contains(m_left->name()))
  189.     {
  190.         gen.error("undeclared variable:" + m_left->name());
  191.         return NULL;
  192.     }
  193.  
  194.     Value *variable = gen.local()->lookup(m_left->name());
  195.     Value *value = m_right->codeGen(gen);
  196.  
  197.     return new StoreInst(value, variable, gen.local()->block());
  198. }
  199.  
  200.  
  201. Value *IfStatement::codeGen(CodeGenerator &gen)
  202. {
  203.     gen.error("IF statement isn't implemented");
  204.  
  205.  
  206.  
  207.  
  208.  
  209.     BasicBlock *blockTrue = BasicBlock::Create(getGlobalContext(), "true", gen.function());
  210.     BasicBlock *blockFalse = BasicBlock::Create(getGlobalContext(), "false");
  211.     BasicBlock *blockAfter = BasicBlock::Create(getGlobalContext(), "after");
  212.  
  213.     gen.pushScope(new CodeScope(blockTrue, gen.local().get()));
  214.     m_ifTrue->codeGen(gen);
  215.     gen.popScope();
  216.  
  217.  
  218.     gen.function()->getBasicBlockList().push_back(blockFalse);
  219.     gen.function()->getBasicBlockList().push_back(blockAfter);
  220.  
  221.  
  222.  
  223.     gen.pushScope(new CodeScope(blockFalse, gen.local().get()));
  224.     m_ifFalse->codeGen(gen);
  225.     gen.popScope();
  226.  
  227.  
  228.     return BranchInst::Create(blockTrue, blockFalse,
  229.                               m_cond->codeGen(gen),
  230.                               gen.local()->block());
  231. }
  232.  
  233.  
  234. Value *CompoundStatement::codeGen(CodeGenerator &gen)
  235. {
  236.     gen.warning("compound statement isn't implemented");
  237.  
  238.     Value *last = NULL;
  239.     for (StatementList::iterator it = m_stmts->begin(); it != m_stmts->end(); ++it)
  240.         last = (*it)->codeGen(gen);
  241.  
  242.     return last;
  243. }
  244.  
  245. Value *UnaryExpression::codeGen(CodeGenerator &gen)
  246. {
  247.     gen.error("unary expression isn't implemented");
  248.  
  249.     return NULL;
  250. }
  251.  
  252.  
  253.  
  254. Value *BinaryExpression::codeGen(CodeGenerator &gen)
  255. {
  256.     gen.warning("binary expression isn't implemented");
  257.  
  258.     Instruction::BinaryOps op;
  259.  
  260.     switch (m_op)
  261.     {
  262.     /* add */
  263.     // +
  264.     case PLUS:
  265.         op = Instruction::Add;
  266.         break;
  267.     // -
  268.     case MINUS:
  269.         op = Instruction::Sub;
  270.         break;
  271.     // or
  272.     case OR:
  273.         op = Instruction::Or;
  274.         break;
  275.  
  276.     /* multiplication */
  277.     case MULT:
  278.         op = Instruction::Mul;
  279.         break;
  280.     case DIVIDE:
  281.         op = Instruction::SDiv;
  282.         break;
  283.  
  284.     case MOD:
  285.     case DIV:
  286.     case AND:
  287.         op = Instruction::And;
  288.         break;
  289.  
  290.     case NOTSYM:
  291.  
  292.     default:
  293.         gen.error("unknown operator");
  294.         return NULL;
  295.     }
  296.  
  297.  
  298.     return BinaryOperator::Create(op, m_left->codeGen(gen), m_right->codeGen(gen),
  299.                                   "", gen.local()->block());
  300. }
  301.  
  302.  
  303.  
  304. Value *CompareExpression::codeGen(CodeGenerator &gen)
  305. {
  306.     gen.warning("comparing expression isn't implemented");
  307.  
  308.  
  309.     Instruction::OtherOps op;
  310.  
  311.     // checking the types etc.
  312.     op = CmpInst::ICmp;
  313.     //
  314.  
  315.     CmpInst::Predicate pr;
  316.  
  317.     switch (m_op)
  318.     {
  319.     /* comparison */
  320.  
  321.     // ==
  322.     case EQ:
  323.         if (op == CmpInst::ICmp)
  324.             pr = CmpInst::ICMP_EQ;
  325.         else if (op == CmpInst::FCmp)
  326.             pr = CmpInst::FCMP_OEQ;
  327.         break;
  328.     // <>
  329.     case NE:
  330.         if (op == CmpInst::ICmp)
  331.             pr = CmpInst::ICMP_NE;
  332.         else if (op == CmpInst::FCmp)
  333.             pr = CmpInst::FCMP_ONE;
  334.         break;
  335.     // >
  336.     case GT:
  337.         if (op == CmpInst::ICmp)
  338.             pr = CmpInst::ICMP_SGT;
  339.         else if (op == CmpInst::FCmp)
  340.             pr = CmpInst::FCMP_OGT;
  341.         break;
  342.     // >=
  343.     case GE:
  344.         if (op == CmpInst::ICmp)
  345.             pr = CmpInst::ICMP_SGE;
  346.         else if (op == CmpInst::FCmp)
  347.             pr = CmpInst::FCMP_OGE;
  348.         break;
  349.     // <
  350.     case LT:
  351.         if (op == CmpInst::ICmp)
  352.             pr = CmpInst::ICMP_SLT;
  353.         else if (op == CmpInst::FCmp)
  354.             pr = CmpInst::FCMP_OLT;
  355.         break;
  356.  
  357.     // <=
  358.     case LE:
  359.         if (op == CmpInst::ICmp)
  360.             pr = CmpInst::ICMP_SLE;
  361.         else if (op == CmpInst::FCmp)
  362.             pr = CmpInst::FCMP_OLE;
  363.         break;
  364.  
  365.     default:
  366.         gen.error("unknown compare operator");
  367.         return NULL;
  368.  
  369.     }
  370.  
  371.     return CmpInst::Create(op, pr,
  372.                            m_left->codeGen(gen), m_right->codeGen(gen),
  373.                            "", gen.local()->block());
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement