A Hugo incarnation of the blog.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

global_scope.hpp 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include <memory>
  3. #include <string>
  4. #include <vector>
  5. #include <llvm/IR/Function.h>
  6. #include "instruction.hpp"
  7. struct ast;
  8. using ast_ptr = std::unique_ptr<ast>;
  9. struct global_function {
  10. std::string name;
  11. std::vector<std::string> params;
  12. ast_ptr body;
  13. std::vector<instruction_ptr> instructions;
  14. llvm::Function* generated_function;
  15. global_function(std::string n, std::vector<std::string> ps, ast_ptr b)
  16. : name(std::move(n)), params(std::move(ps)), body(std::move(b)) {}
  17. void compile();
  18. void declare_llvm(llvm_context& ctx);
  19. void generate_llvm(llvm_context& ctx);
  20. };
  21. using global_function_ptr = std::unique_ptr<global_function>;
  22. struct global_constructor {
  23. std::string name;
  24. int8_t tag;
  25. size_t arity;
  26. global_constructor(std::string n, int8_t t, size_t a)
  27. : name(std::move(n)), tag(t), arity(a) {}
  28. void generate_llvm(llvm_context& ctx);
  29. };
  30. using global_constructor_ptr = std::unique_ptr<global_constructor>;
  31. struct global_scope {
  32. std::map<std::string, int> occurence_count;
  33. std::vector<global_function_ptr> functions;
  34. std::vector<global_constructor_ptr> constructors;
  35. global_function& add_function(std::string n, std::vector<std::string> ps, ast_ptr b);
  36. global_constructor& add_constructor(std::string n, int8_t t, size_t a);
  37. void compile();
  38. void generate_llvm(llvm_context& ctx);
  39. private:
  40. std::string mangle_name(const std::string& n);
  41. };