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
56public:
57 llvm::StructType *FRAME_MAP_TYPE;
58 llvm::StructType *STACK_ENTRY_TYPE;
59 ShadowGarbageCollector(LLVMRes &resPtr)
60 : module(*resPtr.Module.get()), context(*resPtr.Context.get()),
61 builder(*resPtr.Builder) {
62 // INFO: Frame map
63 FRAME_MAP_TYPE = llvm::StructType::create(context, "frame_map_type");
64 auto *int64type =
65 llvm::Type::getInt64Ty(context); // 0 stands for generic address space
66 FRAME_MAP_TYPE->setBody(int64type);
67
68 // INFO: Stack entry
69 STACK_ENTRY_TYPE = llvm::StructType::create(context, "stack_entry_type");
70 llvm::PointerType *int64ptr =
71 llvm::PointerType::get(context, 0); // 0 stands for generic address space
72 llvm::ArrayType *root_array = llvm::ArrayType::get(int64ptr, 0);
73 // llvm::ArrayType *MetaArrayTy =
74 // llvm::ArrayType::get(int8ptr, MetaDataEntries.size());
75 // llvm::Constant *MetaArray =
76 // llvm::ConstantArray::get(MetaArrayTy, MetaDataEntries);
77 STACK_ENTRY_TYPE->setBody({int64ptr, int64ptr, root_array});
78 }
79
80 virtual std::string llvmStrategy() { return "shadow-stack"; }
81 llvm::GlobalVariable *getFrameMapForCallee(FuncDefAST *);
82 void applyStrategy(llvm::Function *f);
83 void setStackEntry(FuncDefAST *callee, llvm::Function *llvm_callee);
84 void relieveStackEntry();
85
87
88 void initGCFunc();
89};
90
91class RefCounter {
92 llvm::Module &module;
93 llvm::LLVMContext &context;
94 llvm::IRBuilder<> &builder;
95
96 inline static std::string REFCNT_MALLOC_WRAPPER_NAME =
97 "refcnt_malloc_wrapper";
98
99public:
100 inline static constexpr int REFCNT_SIZE = sizeof(int32_t);
101 void declare_refcnt_visitor(bool is_main_file,
102 llvm::StructType *stack_entry_type,
103 llvm::StructType *frame_map_type);
104 void declare_malloc_wrapper(bool is_main_file);
105 void decrease_refcnt(llvm::Value *);
106 void increase_refcnt(llvm::Value *);
107 RefCounter(LLVMRes &resPtr)
108 : module(*resPtr.Module.get()), context(*resPtr.Context.get()),
109 builder(*resPtr.Builder) {}
110};
111} // 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:40