Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | None | 0 0
  1. #ifndef CODEGEN_H
  2. #define CODEGEN_H
  3.  
  4. #define __STDC_LIMIT_MACROS
  5. #define __STDC_CONSTANT_MACROS
  6. #include <llvm/Type.h>
  7. #include <llvm/Function.h>
  8.  
  9. #include <stack>
  10. #include <map>
  11. #include <string>
  12.  
  13. #include <tr1/memory>
  14.  
  15. #include <iostream>
  16.  
  17. class Program;
  18.  
  19.  
  20. class CodeScope;
  21. typedef std::tr1::shared_ptr<CodeScope> CodeScopePtr;
  22.  
  23.  
  24.  
  25. /**
  26.   code scope (variables etc.)
  27.   */
  28. class CodeScope
  29. {
  30. public:
  31.     CodeScope(llvm::BasicBlock *block, CodeScope *parent = NULL):
  32.         m_block(block),
  33.         m_parent(parent)
  34.     {}
  35.  
  36.     ~CodeScope()
  37.     {}
  38.  
  39.     /**
  40.       TODO: switch to lookup() calls only
  41.       check if scope contains declaration
  42.       \param name variable
  43.       */
  44.     inline bool contains(const std::string &name) const
  45.     {
  46.         if (m_decls.find(name) == m_decls.end())
  47.         {
  48.             // look in parent's
  49.             if (hasParent())
  50.                 return m_parent->contains(name);
  51.             else
  52.                 return false;
  53.         }
  54.         return true;
  55.     }
  56.  
  57.  
  58.  
  59.     inline llvm::BasicBlock *block() const
  60.     {
  61.         return m_block;
  62.     }
  63.  
  64.     /**
  65.       get variable type
  66.       \param name variable
  67.       */
  68.     inline llvm::Value *lookup(const std::string &name) const
  69.     {
  70.         if (m_decls.find(name) == m_decls.end())
  71.         {
  72.             if (hasParent())
  73.                 return m_parent->lookup(name);
  74.             else
  75.                 return NULL;
  76.  
  77.         }
  78.         return m_decls.at(name);
  79.     }
  80.  
  81.  
  82.     inline void addDeclaration(const std::string &name, llvm::Value *v)
  83.     {
  84.         m_decls[name] = v;
  85.     }
  86.  
  87.  
  88. private:
  89.     /// check if scope has parent
  90.     inline bool hasParent() const { return m_parent != NULL; }
  91.     /// declarations
  92.     std::map<std::string, llvm::Value *> m_decls;
  93.     /// associated block
  94.     llvm::BasicBlock *m_block;
  95.     /// parent scope
  96.     CodeScope *m_parent;
  97. };
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. class CodeGenerator
  108. {
  109. public:
  110.     CodeGenerator():
  111.         m_module(NULL),
  112.         m_mainFunction(NULL),
  113.         m_error(false)
  114.     {
  115.     }
  116.  
  117.     /// generate
  118.     bool generate(Program *program);
  119.  
  120.     /** add error to output
  121.       \param line number
  122.       \param msg error message
  123.       */
  124.     void error(const std::string &msg, unsigned int line = 0);
  125.  
  126.     /** add warning to output
  127.       \param line number
  128.       \param msg warning message
  129.       */
  130.     void warning(const std::string &msg, unsigned int line = 0);
  131.  
  132.  
  133.     /// run program
  134.     void run();
  135.  
  136.     /// get current local scope
  137.     inline CodeScopePtr local() const
  138.     {
  139.         return m_scopes.top();
  140.     }
  141.  
  142.     /// get current global scope
  143.     inline CodeScopePtr global() const
  144.     {
  145.         return m_global;
  146.     }
  147.  
  148.     inline void pushScope(CodeScope *scope)
  149.     {
  150.         m_scopes.push(CodeScopePtr(scope));
  151.     }
  152.  
  153.     inline void popScope()
  154.     {
  155.         m_scopes.pop();
  156.     }
  157.  
  158.  
  159.     inline llvm::Module *module() const
  160.     {
  161.         return m_module;
  162.     }
  163.  
  164.     inline llvm::Function *function() const
  165.     {
  166.         return m_mainFunction;
  167.     }
  168.  
  169.  
  170.  
  171.  
  172. private:
  173.     /// scope stack
  174.     std::stack<CodeScopePtr> m_scopes;
  175.     /// global scope
  176.     CodeScopePtr m_global;
  177.  
  178.     /// module
  179.     llvm::Module *m_module;
  180.  
  181.     /// main function
  182.     llvm::Function *m_mainFunction;
  183.  
  184.     /// error flag
  185.     bool m_error;
  186. };
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196. #endif // CODEGEN_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement