CodeGeneration.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifndef QLOW_CODE_GENERATION_H
  2. #define QLOW_CODE_GENERATION_H
  3. #include "Semantic.h"
  4. #include "Builtin.h"
  5. #include "CodegenVisitor.h"
  6. #include <stack>
  7. #include <llvm/IR/Module.h>
  8. namespace qlow
  9. {
  10. namespace gen
  11. {
  12. std::unique_ptr<llvm::Module> generateModule(const sem::GlobalScope& objects);
  13. llvm::Function* generateFunction (llvm::Module* module, sem::Method* method);
  14. void generateObjectFile(const std::string& name, std::unique_ptr<llvm::Module> module, int optLevel);
  15. class FunctionGenerator;
  16. }
  17. }
  18. class qlow::gen::FunctionGenerator
  19. {
  20. const sem::Method& method;
  21. llvm::Module* module;
  22. llvm::AttributeSet& attributes;
  23. std::stack<llvm::BasicBlock*> basicBlocks;
  24. public:
  25. StatementVisitor statementVisitor;
  26. ExpressionCodegenVisitor expressionVisitor;
  27. LValueVisitor lvalueVisitor;
  28. inline FunctionGenerator(const sem::Method& m, llvm::Module* module,
  29. llvm::AttributeSet& attributes) :
  30. method{ m },
  31. module{ module },
  32. attributes{ attributes },
  33. expressionVisitor{ *this }
  34. {
  35. }
  36. llvm::Function* generate(void);
  37. inline llvm::Module* getModule(void) const { return module; }
  38. inline llvm::LLVMContext& getContext(void) const { return module->getContext(); }
  39. inline llvm::BasicBlock* getCurrentBlock(void) const { return basicBlocks.top(); }
  40. inline void pushBlock(llvm::BasicBlock* bb) { basicBlocks.push(bb); }
  41. inline llvm::BasicBlock* popBlock(void) { auto* bb = basicBlocks.top(); basicBlocks.pop(); return bb; }
  42. };
  43. #endif // QLOW_CODE_GENERATION_H