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.

instruction.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include "instruction.hpp"
  2. #include "llvm_context.hpp"
  3. #include <llvm/IR/BasicBlock.h>
  4. #include <llvm/IR/Function.h>
  5. using namespace llvm;
  6. static void print_indent(int n, std::ostream& to) {
  7. while(n--) to << " ";
  8. }
  9. void instruction_pushint::print(int indent, std::ostream& to) const {
  10. print_indent(indent, to);
  11. to << "PushInt(" << value << ")" << std::endl;
  12. }
  13. void instruction_pushint::gen_llvm(llvm_context& ctx, Function* f) const {
  14. ctx.create_push(f, ctx.create_num(f, ctx.create_i32(value)));
  15. }
  16. void instruction_pushglobal::print(int indent, std::ostream& to) const {
  17. print_indent(indent, to);
  18. to << "PushGlobal(" << name << ")" << std::endl;
  19. }
  20. void instruction_pushglobal::gen_llvm(llvm_context& ctx, Function* f) const {
  21. auto& global_f = ctx.get_custom_function(name);
  22. auto arity = ctx.create_i32(global_f.arity);
  23. ctx.create_push(f, ctx.create_global(f, global_f.function, arity));
  24. }
  25. void instruction_push::print(int indent, std::ostream& to) const {
  26. print_indent(indent, to);
  27. to << "Push(" << offset << ")" << std::endl;
  28. }
  29. void instruction_push::gen_llvm(llvm_context& ctx, Function* f) const {
  30. ctx.create_push(f, ctx.create_peek(f, ctx.create_size(offset)));
  31. }
  32. void instruction_pop::print(int indent, std::ostream& to) const {
  33. print_indent(indent, to);
  34. to << "Pop(" << count << ")" << std::endl;
  35. }
  36. void instruction_pop::gen_llvm(llvm_context& ctx, Function* f) const {
  37. ctx.create_popn(f, ctx.create_size(count));
  38. }
  39. void instruction_mkapp::print(int indent, std::ostream& to) const {
  40. print_indent(indent, to);
  41. to << "MkApp()" << std::endl;
  42. }
  43. void instruction_mkapp::gen_llvm(llvm_context& ctx, Function* f) const {
  44. auto left = ctx.create_pop(f);
  45. auto right = ctx.create_pop(f);
  46. ctx.create_push(f, ctx.create_app(f, left, right));
  47. }
  48. void instruction_update::print(int indent, std::ostream& to) const {
  49. print_indent(indent, to);
  50. to << "Update(" << offset << ")" << std::endl;
  51. }
  52. void instruction_update::gen_llvm(llvm_context& ctx, Function* f) const {
  53. ctx.create_update(f, ctx.create_size(offset));
  54. }
  55. void instruction_pack::print(int indent, std::ostream& to) const {
  56. print_indent(indent, to);
  57. to << "Pack(" << tag << ", " << size << ")" << std::endl;
  58. }
  59. void instruction_pack::gen_llvm(llvm_context& ctx, Function* f) const {
  60. ctx.create_pack(f, ctx.create_size(size), ctx.create_i8(tag));
  61. }
  62. void instruction_split::print(int indent, std::ostream& to) const {
  63. print_indent(indent, to);
  64. to << "Split()" << std::endl;
  65. }
  66. void instruction_split::gen_llvm(llvm_context& ctx, Function* f) const {
  67. ctx.create_split(f, ctx.create_size(size));
  68. }
  69. void instruction_jump::print(int indent, std::ostream& to) const {
  70. print_indent(indent, to);
  71. to << "Jump(" << std::endl;
  72. for(auto& instruction_set : branches) {
  73. for(auto& instruction : instruction_set) {
  74. instruction->print(indent + 2, to);
  75. }
  76. to << std::endl;
  77. }
  78. print_indent(indent, to);
  79. to << ")" << std::endl;
  80. }
  81. void instruction_jump::gen_llvm(llvm_context& ctx, Function* f) const {
  82. auto top_node = ctx.create_peek(f, ctx.create_size(0));
  83. auto tag = ctx.unwrap_data_tag(top_node);
  84. auto safety_block = ctx.create_basic_block("safety", f);
  85. auto switch_op = ctx.get_builder().CreateSwitch(tag, safety_block, tag_mappings.size());
  86. std::vector<BasicBlock*> blocks;
  87. for(auto& branch : branches) {
  88. auto branch_block = ctx.create_basic_block("branch", f);
  89. ctx.get_builder().SetInsertPoint(branch_block);
  90. for(auto& instruction : branch) {
  91. instruction->gen_llvm(ctx, f);
  92. }
  93. ctx.get_builder().CreateBr(safety_block);
  94. blocks.push_back(branch_block);
  95. }
  96. for(auto& mapping : tag_mappings) {
  97. switch_op->addCase(ctx.create_i8(mapping.first), blocks[mapping.second]);
  98. }
  99. ctx.get_builder().SetInsertPoint(safety_block);
  100. }
  101. void instruction_slide::print(int indent, std::ostream& to) const {
  102. print_indent(indent, to);
  103. to << "Slide(" << offset << ")" << std::endl;
  104. }
  105. void instruction_slide::gen_llvm(llvm_context& ctx, Function* f) const {
  106. ctx.create_slide(f, ctx.create_size(offset));
  107. }
  108. void instruction_binop::print(int indent, std::ostream& to) const {
  109. print_indent(indent, to);
  110. to << "BinOp(" << op_action(op) << ")" << std::endl;
  111. }
  112. void instruction_binop::gen_llvm(llvm_context& ctx, Function* f) const {
  113. auto left_int = ctx.unwrap_num(ctx.create_pop(f));
  114. auto right_int = ctx.unwrap_num(ctx.create_pop(f));
  115. llvm::Value* result;
  116. switch(op) {
  117. case PLUS: result = ctx.get_builder().CreateAdd(left_int, right_int); break;
  118. case MINUS: result = ctx.get_builder().CreateSub(left_int, right_int); break;
  119. case TIMES: result = ctx.get_builder().CreateMul(left_int, right_int); break;
  120. case DIVIDE: result = ctx.get_builder().CreateSDiv(left_int, right_int); break;
  121. }
  122. ctx.create_push(f, ctx.create_num(f, result));
  123. }
  124. void instruction_eval::print(int indent, std::ostream& to) const {
  125. print_indent(indent, to);
  126. to << "Eval()" << std::endl;
  127. }
  128. void instruction_eval::gen_llvm(llvm_context& ctx, Function* f) const {
  129. ctx.create_unwind(f);
  130. }
  131. void instruction_alloc::print(int indent, std::ostream& to) const {
  132. print_indent(indent, to);
  133. to << "Alloc(" << amount << ")" << std::endl;
  134. }
  135. void instruction_alloc::gen_llvm(llvm_context& ctx, Function* f) const {
  136. ctx.create_alloc(f, ctx.create_size(amount));
  137. }
  138. void instruction_unwind::print(int indent, std::ostream& to) const {
  139. print_indent(indent, to);
  140. to << "Unwind()" << std::endl;
  141. }
  142. void instruction_unwind::gen_llvm(llvm_context& ctx, Function* f) const {
  143. // Nothing
  144. }