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.cpp 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "global_scope.hpp"
  2. #include "ast.hpp"
  3. void global_function::compile() {
  4. env_ptr new_env = env_ptr(new env_offset(0, nullptr));
  5. for(auto it = params.rbegin(); it != params.rend(); it++) {
  6. new_env = env_ptr(new env_var(*it, new_env));
  7. }
  8. body->compile(new_env, instructions);
  9. instructions.push_back(instruction_ptr(new instruction_update(params.size())));
  10. instructions.push_back(instruction_ptr(new instruction_pop(params.size())));
  11. }
  12. void global_function::declare_llvm(llvm_context& ctx) {
  13. generated_function = ctx.create_custom_function(name, params.size());
  14. }
  15. void global_function::generate_llvm(llvm_context& ctx) {
  16. ctx.get_builder().SetInsertPoint(&generated_function->getEntryBlock());
  17. for(auto& instruction : instructions) {
  18. instruction->gen_llvm(ctx, generated_function);
  19. }
  20. ctx.get_builder().CreateRetVoid();
  21. }
  22. void global_constructor::generate_llvm(llvm_context& ctx) {
  23. auto new_function =
  24. ctx.create_custom_function(name, arity);
  25. std::vector<instruction_ptr> instructions;
  26. instructions.push_back(instruction_ptr(new instruction_pack(tag, arity)));
  27. instructions.push_back(instruction_ptr(new instruction_update(0)));
  28. ctx.get_builder().SetInsertPoint(&new_function->getEntryBlock());
  29. for (auto& instruction : instructions) {
  30. instruction->gen_llvm(ctx, new_function);
  31. }
  32. ctx.get_builder().CreateRetVoid();
  33. }
  34. global_function& global_scope::add_function(
  35. const std::string& n,
  36. std::vector<std::string> ps,
  37. ast_ptr b) {
  38. auto name = mng->new_mangled_name(n);
  39. global_function* new_function =
  40. new global_function(std::move(name), std::move(ps), std::move(b));
  41. functions.push_back(global_function_ptr(new_function));
  42. return *new_function;
  43. }
  44. global_constructor& global_scope::add_constructor(
  45. const std::string& n,
  46. int8_t t,
  47. size_t a) {
  48. auto name = mng->new_mangled_name(n);
  49. global_constructor* new_constructor = new global_constructor(name, t, a);
  50. constructors.push_back(global_constructor_ptr(new_constructor));
  51. return *new_constructor;
  52. }
  53. void global_scope::compile() {
  54. for(auto& function : functions) {
  55. function->compile();
  56. }
  57. }
  58. void global_scope::generate_llvm(llvm_context& ctx) {
  59. for(auto& constructor : constructors) {
  60. constructor->generate_llvm(ctx);
  61. }
  62. for(auto& function : functions) {
  63. function->declare_llvm(ctx);
  64. }
  65. for(auto& function : functions) {
  66. function->generate_llvm(ctx);
  67. }
  68. }