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.

main.cpp 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "ast.hpp"
  2. #include <iostream>
  3. #include "binop.hpp"
  4. #include "definition.hpp"
  5. #include "graph.hpp"
  6. #include "instruction.hpp"
  7. #include "llvm_context.hpp"
  8. #include "parser.hpp"
  9. #include "error.hpp"
  10. #include "type.hpp"
  11. #include "llvm/IR/LegacyPassManager.h"
  12. #include "llvm/IR/Verifier.h"
  13. #include "llvm/Support/TargetSelect.h"
  14. #include "llvm/Support/TargetRegistry.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. #include "llvm/Support/FileSystem.h"
  17. #include "llvm/Target/TargetOptions.h"
  18. #include "llvm/Target/TargetMachine.h"
  19. void yy::parser::error(const std::string& msg) {
  20. std::cout << "An error occured: " << msg << std::endl;
  21. }
  22. extern std::map<std::string, definition_data_ptr> defs_data;
  23. extern std::map<std::string, definition_defn_ptr> defs_defn;
  24. void typecheck_program(
  25. const std::map<std::string, definition_data_ptr>& defs_data,
  26. const std::map<std::string, definition_defn_ptr>& defs_defn,
  27. type_mgr& mgr, type_env_ptr& env) {
  28. type_ptr int_type = type_ptr(new type_base("Int"));
  29. env->bind_type("Int", int_type);
  30. type_ptr int_type_app = type_ptr(new type_app(int_type));
  31. type_ptr binop_type = type_ptr(new type_arr(
  32. int_type_app,
  33. type_ptr(new type_arr(int_type_app, int_type_app))));
  34. env->bind("+", binop_type);
  35. env->bind("-", binop_type);
  36. env->bind("*", binop_type);
  37. env->bind("/", binop_type);
  38. for(auto& def_data : defs_data) {
  39. def_data.second->insert_types(env);
  40. }
  41. for(auto& def_data : defs_data) {
  42. def_data.second->insert_constructors();
  43. }
  44. function_graph dependency_graph;
  45. for(auto& def_defn : defs_defn) {
  46. def_defn.second->find_free(mgr, env);
  47. dependency_graph.add_function(def_defn.second->name);
  48. for(auto& dependency : def_defn.second->free_variables) {
  49. if(defs_defn.find(dependency) == defs_defn.end())
  50. throw 0;
  51. dependency_graph.add_edge(def_defn.second->name, dependency);
  52. }
  53. }
  54. std::vector<group_ptr> groups = dependency_graph.compute_order();
  55. for(auto it = groups.rbegin(); it != groups.rend(); it++) {
  56. auto& group = *it;
  57. for(auto& def_defnn_name : group->members) {
  58. auto& def_defn = defs_defn.find(def_defnn_name)->second;
  59. def_defn->insert_types(mgr);
  60. }
  61. for(auto& def_defnn_name : group->members) {
  62. auto& def_defn = defs_defn.find(def_defnn_name)->second;
  63. def_defn->typecheck(mgr);
  64. }
  65. for(auto& def_defnn_name : group->members) {
  66. env->generalize(def_defnn_name, mgr);
  67. }
  68. }
  69. for(auto& pair : env->names) {
  70. std::cout << pair.first << ": ";
  71. pair.second->print(mgr, std::cout);
  72. std::cout << std::endl;
  73. }
  74. }
  75. void compile_program(const std::map<std::string, definition_defn_ptr>& defs_defn) {
  76. for(auto& def_defn : defs_defn) {
  77. def_defn.second->compile();
  78. for(auto& instruction : def_defn.second->instructions) {
  79. instruction->print(0, std::cout);
  80. }
  81. std::cout << std::endl;
  82. }
  83. }
  84. void gen_llvm_internal_op(llvm_context& ctx, binop op) {
  85. auto new_function = ctx.create_custom_function(op_action(op), 2);
  86. std::vector<instruction_ptr> instructions;
  87. instructions.push_back(instruction_ptr(new instruction_push(1)));
  88. instructions.push_back(instruction_ptr(new instruction_eval()));
  89. instructions.push_back(instruction_ptr(new instruction_push(1)));
  90. instructions.push_back(instruction_ptr(new instruction_eval()));
  91. instructions.push_back(instruction_ptr(new instruction_binop(op)));
  92. instructions.push_back(instruction_ptr(new instruction_update(2)));
  93. instructions.push_back(instruction_ptr(new instruction_pop(2)));
  94. ctx.builder.SetInsertPoint(&new_function->getEntryBlock());
  95. for(auto& instruction : instructions) {
  96. instruction->gen_llvm(ctx, new_function);
  97. }
  98. ctx.builder.CreateRetVoid();
  99. }
  100. void output_llvm(llvm_context& ctx, const std::string& filename) {
  101. std::string targetTriple = llvm::sys::getDefaultTargetTriple();
  102. llvm::InitializeNativeTarget();
  103. llvm::InitializeNativeTargetAsmParser();
  104. llvm::InitializeNativeTargetAsmPrinter();
  105. std::string error;
  106. const llvm::Target* target =
  107. llvm::TargetRegistry::lookupTarget(targetTriple, error);
  108. if (!target) {
  109. std::cerr << error << std::endl;
  110. } else {
  111. std::string cpu = "generic";
  112. std::string features = "";
  113. llvm::TargetOptions options;
  114. llvm::TargetMachine* targetMachine =
  115. target->createTargetMachine(targetTriple, cpu, features,
  116. options, llvm::Optional<llvm::Reloc::Model>());
  117. ctx.module.setDataLayout(targetMachine->createDataLayout());
  118. ctx.module.setTargetTriple(targetTriple);
  119. std::error_code ec;
  120. llvm::raw_fd_ostream file(filename, ec, llvm::sys::fs::F_None);
  121. if (ec) {
  122. throw 0;
  123. } else {
  124. llvm::CodeGenFileType type = llvm::CGFT_ObjectFile;
  125. llvm::legacy::PassManager pm;
  126. if (targetMachine->addPassesToEmitFile(pm, file, NULL, type)) {
  127. throw 0;
  128. } else {
  129. pm.run(ctx.module);
  130. file.close();
  131. }
  132. }
  133. }
  134. }
  135. void gen_llvm(
  136. const std::map<std::string, definition_data_ptr>& defs_data,
  137. const std::map<std::string, definition_defn_ptr>& defs_defn) {
  138. llvm_context ctx;
  139. gen_llvm_internal_op(ctx, PLUS);
  140. gen_llvm_internal_op(ctx, MINUS);
  141. gen_llvm_internal_op(ctx, TIMES);
  142. gen_llvm_internal_op(ctx, DIVIDE);
  143. for(auto& def_data : defs_data) {
  144. def_data.second->generate_llvm(ctx);
  145. }
  146. for(auto& def_defn : defs_defn) {
  147. def_defn.second->declare_llvm(ctx);
  148. }
  149. for(auto& def_defn : defs_defn) {
  150. def_defn.second->generate_llvm(ctx);
  151. }
  152. ctx.module.print(llvm::outs(), nullptr);
  153. output_llvm(ctx, "program.o");
  154. }
  155. int main() {
  156. yy::parser parser;
  157. type_mgr mgr;
  158. type_env_ptr env(new type_env);
  159. parser.parse();
  160. for(auto& def_defn : defs_defn) {
  161. std::cout << def_defn.second->name;
  162. for(auto& param : def_defn.second->params) std::cout << " " << param;
  163. std::cout << ":" << std::endl;
  164. def_defn.second->body->print(1, std::cout);
  165. }
  166. try {
  167. typecheck_program(defs_data, defs_defn, mgr, env);
  168. compile_program(defs_defn);
  169. gen_llvm(defs_data, defs_defn);
  170. } catch(unification_error& err) {
  171. std::cout << "failed to unify types: " << std::endl;
  172. std::cout << " (1) \033[34m";
  173. err.left->print(mgr, std::cout);
  174. std::cout << "\033[0m" << std::endl;
  175. std::cout << " (2) \033[32m";
  176. err.right->print(mgr, std::cout);
  177. std::cout << "\033[0m" << std::endl;
  178. } catch(type_error& err) {
  179. std::cout << "failed to type check program: " << err.description << std::endl;
  180. }
  181. }