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.

llvm_context.hpp 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. #include <llvm/IR/DerivedTypes.h>
  3. #include <llvm/IR/Function.h>
  4. #include <llvm/IR/LLVMContext.h>
  5. #include <llvm/IR/IRBuilder.h>
  6. #include <llvm/IR/Module.h>
  7. #include <llvm/IR/Value.h>
  8. #include <map>
  9. class llvm_context {
  10. public:
  11. struct custom_function {
  12. llvm::Function* function;
  13. int32_t arity;
  14. };
  15. using custom_function_ptr = std::unique_ptr<custom_function>;
  16. private:
  17. llvm::LLVMContext ctx;
  18. llvm::IRBuilder<> builder;
  19. llvm::Module module;
  20. std::map<std::string, custom_function_ptr> custom_functions;
  21. std::map<std::string, llvm::Function*> functions;
  22. std::map<std::string, llvm::StructType*> struct_types;
  23. llvm::StructType* stack_type;
  24. llvm::StructType* gmachine_type;
  25. llvm::PointerType* stack_ptr_type;
  26. llvm::PointerType* gmachine_ptr_type;
  27. llvm::PointerType* node_ptr_type;
  28. llvm::IntegerType* tag_type;
  29. llvm::FunctionType* function_type;
  30. void create_types();
  31. void create_functions();
  32. public:
  33. llvm_context()
  34. : builder(ctx), module("bloglang", ctx) {
  35. create_types();
  36. create_functions();
  37. }
  38. llvm::IRBuilder<>& get_builder();
  39. llvm::Module& get_module();
  40. llvm::BasicBlock* create_basic_block(const std::string& name, llvm::Function* f);
  41. llvm::ConstantInt* create_i8(int8_t);
  42. llvm::ConstantInt* create_i32(int32_t);
  43. llvm::ConstantInt* create_size(size_t);
  44. llvm::Value* create_pop(llvm::Function*);
  45. llvm::Value* create_peek(llvm::Function*, llvm::Value*);
  46. void create_push(llvm::Function*, llvm::Value*);
  47. void create_popn(llvm::Function*, llvm::Value*);
  48. void create_update(llvm::Function*, llvm::Value*);
  49. void create_pack(llvm::Function*, llvm::Value*, llvm::Value*);
  50. void create_split(llvm::Function*, llvm::Value*);
  51. void create_slide(llvm::Function*, llvm::Value*);
  52. void create_alloc(llvm::Function*, llvm::Value*);
  53. llvm::Value* create_track(llvm::Function*, llvm::Value*);
  54. void create_unwind(llvm::Function*);
  55. llvm::Value* unwrap_gmachine_stack_ptr(llvm::Value*);
  56. llvm::Value* unwrap_num(llvm::Value*);
  57. llvm::Value* create_num(llvm::Function*, llvm::Value*);
  58. llvm::Value* unwrap_data_tag(llvm::Value*);
  59. llvm::Value* create_global(llvm::Function*, llvm::Value*, llvm::Value*);
  60. llvm::Value* create_app(llvm::Function*, llvm::Value*, llvm::Value*);
  61. llvm::Function* create_custom_function(const std::string& name, int32_t arity);
  62. custom_function& get_custom_function(const std::string& name);
  63. };