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

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