sammine-lang
Loading...
Searching...
No Matches
Garbage.h
Go to the documentation of this file.
1
2#pragma once
3#include "llvm/IR/DerivedTypes.h"
4#include "llvm/IR/Function.h"
5#include "llvm/IR/IRBuilder.h"
6#include "llvm/IR/LLVMContext.h"
7#include "llvm/IR/Module.h"
8
9#include "ast/Ast.h"
10#include "ast/AstDecl.h"
11#include "codegen/LLVMRes.h"
12#include <cstdint>
13#include <llvm/Analysis/TargetLibraryInfo.h>
14#include <llvm/IR/GlobalVariable.h>
15#include <memory>
16#include <string>
17#include <string_view>
18
21//
22namespace sammine_lang::AST {
24public:
25 static int32_t calculate(BlockAST *ast);
26 static int32_t calculate(ExprAST *ast);
27 static int32_t calculate(IfExprAST *ast);
28 static int32_t calculate(VariableExprAST *ast);
29
30 // TODO: Tell Jasmine to re-check this
31 static int32_t calculate(CallExprAST *ast);
32
33 static int32_t calculate(ReturnExprAST *ast);
34 static int32_t calculate(BinaryExprAST *ast);
35 static int32_t calculate(BoolExprAST *ast);
36 static int32_t calculate(StringExprAST *ast);
37 static int32_t calculate(NumberExprAST *ast);
38 static int32_t calculate(UnitExprAST *ast);
39 static int32_t calculate(VarDefAST *ast);
40};
41class ShadowGarbageCollector {
42 [[maybe_unused]]
43 llvm::Module &module;
44 llvm::LLVMContext &context;
45 [[maybe_unused]]
46 llvm::IRBuilder<> &builder;
47 std::vector<llvm::Constant *> MetaDataEntries;
48
49 // TODO: work with this in create frame map, rename the void to frame map
50 // struct
51 // rename to getFrameMap
52 std::map<std::string, llvm::GlobalVariable *> fn_name_to_frame_map;
53 inline static std::string GLOBAL_ROOT_CHAIN = "global_root_chain";
54 inline static std::string FRAME_MAP = "frame_map";
55
56 llvm::StructType *FRAME_MAP_TYPE;
57 llvm::StructType *STACK_ENTRY_TYPE;
58
59public:
60 ShadowGarbageCollector(LLVMRes &resPtr)
61 : module(*resPtr.Module.get()), context(*resPtr.Context.get()),
62 builder(*resPtr.Builder) {
63 // INFO: Frame map
64 FRAME_MAP_TYPE = llvm::StructType::create(context, "frame_map_type");
65 auto *int64type =
66 llvm::Type::getInt64Ty(context); // 0 stands for generic address space
67 FRAME_MAP_TYPE->setBody(int64type);
68
69 // INFO: Stack entry
70 STACK_ENTRY_TYPE = llvm::StructType::create(context, "stack_entry_type");
71 llvm::PointerType *int64ptr =
72 llvm::PointerType::get(llvm::Type::getInt64Ty(context),
73 0); // 0 stands for generic address space
74 llvm::ArrayType *root_array = llvm::ArrayType::get(int64ptr, 0);
75 // llvm::ArrayType *MetaArrayTy =
76 // llvm::ArrayType::get(int8ptr, MetaDataEntries.size());
77 // llvm::Constant *MetaArray =
78 // llvm::ConstantArray::get(MetaArrayTy, MetaDataEntries);
79 STACK_ENTRY_TYPE->setBody({int64ptr, int64ptr, root_array});
80 }
81
82 virtual std::string llvmStrategy() { return "shadow-stack"; }
83 llvm::GlobalVariable *getFrameMapForCallee(FuncDefAST *);
84 void applyStrategy(llvm::Function *f);
85 void setStackEntry(FuncDefAST *callee, llvm::Function *llvm_callee);
86 void relieveStackEntry();
87
89
90 void initGCFunc();
91};
92
93class RefCounter {
94 llvm::Module &module;
95 llvm::LLVMContext &context;
96 llvm::IRBuilder<> &builder;
97
98 inline static constexpr int REFCNT_SIZE = sizeof(int32_t);
99
100 inline static std::string REFCNT_MALLOC_WRAPPER_NAME =
101 "refcnt_malloc_wrapper";
102
103public:
104 void declare_refcnt_visitor(bool is_main_file,
105 llvm::StructType *stack_entry_type,
106 llvm::StructType *frame_map_type);
107 void declare_malloc_wrapper(bool is_main_file);
108 void decrease_refcnt(llvm::Value *);
109 void increase_refcnt(llvm::Value *);
110 RefCounter(LLVMRes &resPtr)
111 : module(*resPtr.Module.get()), context(*resPtr.Context.get()),
112 builder(*resPtr.Builder) {}
113};
114} // namespace sammine_lang::AST
Holds declaration for all the AST Nodes.
Defined the AST Node classes (ProgramAST, RecordDefAST, FuncDefAST) and a visitor interface for trave...
Defined LLVMRes, which encapsulates the state of LLVM (Context, Modules, IRBuilder,...
An AST to simulate a { } code block.
Definition Ast.h:158
Definition Ast.h:301
Definition Ast.h:375
Definition Ast.h:150
Definition Ast.h:175
Definition Ast.h:432
void initGCFunc()
Definition Garbage.cpp:155
void setStackEntry(FuncDefAST *callee, llvm::Function *llvm_callee)
Definition Garbage.cpp:69
void initGlobalRootChain()
Definition Garbage.cpp:112
llvm::GlobalVariable * getFrameMapForCallee(FuncDefAST *)
Insert a FrameMap in the beginning of each function.
Definition Garbage.cpp:20
Definition Ast.h:406
A variable definition: "var x = expression;".
Definition Ast.h:231
Definition LLVMRes.h:39