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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 definition_group global_defs;
  23. void typecheck_program(
  24. definition_group& defs,
  25. type_mgr& mgr, type_env_ptr& env) {
  26. type_ptr int_type = type_ptr(new type_base("Int"));
  27. env->bind_type("Int", int_type);
  28. type_ptr int_type_app = type_ptr(new type_app(int_type));
  29. type_ptr binop_type = type_ptr(new type_arr(
  30. int_type_app,
  31. type_ptr(new type_arr(int_type_app, int_type_app))));
  32. env->bind("+", binop_type, visibility::global);
  33. env->bind("-", binop_type, visibility::global);
  34. env->bind("*", binop_type, visibility::global);
  35. env->bind("/", binop_type, visibility::global);
  36. std::set<std::string> free;
  37. defs.find_free(free);
  38. defs.typecheck(mgr, env);
  39. for(auto& pair : defs.env->names) {
  40. std::cout << pair.first << ": ";
  41. pair.second.type->print(mgr, std::cout);
  42. std::cout << std::endl;
  43. }
  44. }
  45. global_scope translate_program(definition_group& group) {
  46. global_scope scope;
  47. for(auto& data : group.defs_data) {
  48. data.second->into_globals(scope);
  49. }
  50. for(auto& defn : group.defs_defn) {
  51. auto& function = defn.second->into_global(scope);
  52. function.body->env->parent->set_mangled_name(defn.first, function.name);
  53. }
  54. return scope;
  55. }
  56. void gen_llvm_internal_op(llvm_context& ctx, binop op) {
  57. auto new_function = ctx.create_custom_function(op_action(op), 2);
  58. std::vector<instruction_ptr> instructions;
  59. instructions.push_back(instruction_ptr(new instruction_push(1)));
  60. instructions.push_back(instruction_ptr(new instruction_eval()));
  61. instructions.push_back(instruction_ptr(new instruction_push(1)));
  62. instructions.push_back(instruction_ptr(new instruction_eval()));
  63. instructions.push_back(instruction_ptr(new instruction_binop(op)));
  64. instructions.push_back(instruction_ptr(new instruction_update(2)));
  65. instructions.push_back(instruction_ptr(new instruction_pop(2)));
  66. ctx.builder.SetInsertPoint(&new_function->getEntryBlock());
  67. for(auto& instruction : instructions) {
  68. instruction->gen_llvm(ctx, new_function);
  69. }
  70. ctx.builder.CreateRetVoid();
  71. }
  72. void output_llvm(llvm_context& ctx, const std::string& filename) {
  73. std::string targetTriple = llvm::sys::getDefaultTargetTriple();
  74. llvm::InitializeNativeTarget();
  75. llvm::InitializeNativeTargetAsmParser();
  76. llvm::InitializeNativeTargetAsmPrinter();
  77. std::string error;
  78. const llvm::Target* target =
  79. llvm::TargetRegistry::lookupTarget(targetTriple, error);
  80. if (!target) {
  81. std::cerr << error << std::endl;
  82. } else {
  83. std::string cpu = "generic";
  84. std::string features = "";
  85. llvm::TargetOptions options;
  86. llvm::TargetMachine* targetMachine =
  87. target->createTargetMachine(targetTriple, cpu, features,
  88. options, llvm::Optional<llvm::Reloc::Model>());
  89. ctx.module.setDataLayout(targetMachine->createDataLayout());
  90. ctx.module.setTargetTriple(targetTriple);
  91. std::error_code ec;
  92. llvm::raw_fd_ostream file(filename, ec, llvm::sys::fs::F_None);
  93. if (ec) {
  94. throw 0;
  95. } else {
  96. llvm::CodeGenFileType type = llvm::CGFT_ObjectFile;
  97. llvm::legacy::PassManager pm;
  98. if (targetMachine->addPassesToEmitFile(pm, file, NULL, type)) {
  99. throw 0;
  100. } else {
  101. pm.run(ctx.module);
  102. file.close();
  103. }
  104. }
  105. }
  106. }
  107. void gen_llvm(global_scope& scope) {
  108. llvm_context ctx;
  109. gen_llvm_internal_op(ctx, PLUS);
  110. gen_llvm_internal_op(ctx, MINUS);
  111. gen_llvm_internal_op(ctx, TIMES);
  112. gen_llvm_internal_op(ctx, DIVIDE);
  113. scope.generate_llvm(ctx);
  114. ctx.module.print(llvm::outs(), nullptr);
  115. output_llvm(ctx, "program.o");
  116. }
  117. int main() {
  118. yy::parser parser;
  119. type_mgr mgr;
  120. type_env_ptr env(new type_env);
  121. parser.parse();
  122. for(auto& def_defn : global_defs.defs_defn) {
  123. std::cout << def_defn.second->name;
  124. for(auto& param : def_defn.second->params) std::cout << " " << param;
  125. std::cout << ":" << std::endl;
  126. def_defn.second->body->print(1, std::cout);
  127. }
  128. try {
  129. typecheck_program(global_defs, mgr, env);
  130. global_scope scope = translate_program(global_defs);
  131. scope.compile();
  132. gen_llvm(scope);
  133. } catch(unification_error& err) {
  134. std::cout << "failed to unify types: " << std::endl;
  135. std::cout << " (1) \033[34m";
  136. err.left->print(mgr, std::cout);
  137. std::cout << "\033[0m" << std::endl;
  138. std::cout << " (2) \033[32m";
  139. err.right->print(mgr, std::cout);
  140. std::cout << "\033[0m" << std::endl;
  141. } catch(type_error& err) {
  142. std::cout << "failed to type check program: " << err.description << std::endl;
  143. }
  144. }