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.5KB

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