sammine-lang
Loading...
Searching...
No Matches
MLIRGenImpl.h
1#pragma once
2
3#include "ast/Ast.h"
4#include "ast/ASTProperties.h"
5#include "typecheck/Types.h"
6#include "util/Utilities.h"
7
8#include "mlir/IR/Builders.h"
9#include "mlir/IR/BuiltinOps.h"
10#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
11
12#include "llvm/ADT/StringRef.h"
13
14#include <map>
15#include <string>
16
17namespace sammine_lang {
18
19// Named constants for magic strings used across MLIR codegen
20static constexpr llvm::StringLiteral kClosureTypeName = "sammine.closure";
21static constexpr llvm::StringLiteral kStructTypePrefix = "sammine.struct.";
22static constexpr llvm::StringLiteral kWrapperPrefix = "__wrap_";
23static constexpr llvm::StringLiteral kPartialPrefix = "__partial_";
24static constexpr llvm::StringLiteral kStringPrefix = ".str.";
25static constexpr llvm::StringLiteral kMallocFunc = "malloc";
26static constexpr llvm::StringLiteral kFreeFunc = "free";
27static constexpr llvm::StringLiteral kExitFunc = "exit";
28
32class MLIRGenImpl {
33public:
34 MLIRGenImpl(mlir::MLIRContext &context, const std::string &moduleName,
35 const std::string &fileName, const std::string &sourceText,
36 const AST::ASTProperties &props)
37 : builder(&context), moduleName(moduleName), fileName(fileName),
38 diagnosticData(
39 sammine_util::Reporter::get_diagnostic_data(sourceText)),
40 props_(props) {}
41
42 mlir::ModuleOp generate(AST::ProgramAST *program);
43
44 // --- Member variables ---
45
46 mlir::ModuleOp theModule;
47 mlir::OpBuilder builder;
48 std::string moduleName;
49 std::string fileName;
50 sammine_util::Reporter::DiagnosticData diagnosticData;
51
53
54 int strCounter = 0;
55
56 std::map<std::string, mlir::LLVM::LLVMStructType> structTypes;
57 std::map<std::string, mlir::LLVM::LLVMStructType> enumTypes;
58
59 mlir::LLVM::LLVMStructType closureType;
60 std::map<std::string, std::string> closureWrappers;
61 int partialCounter = 0;
62
63 mlir::Value currentSretBuffer = nullptr;
64
65 const AST::ASTProperties &props_;
66
67 // --- Inline type helpers ---
68 mlir::LLVM::LLVMPointerType llvmPtrTy() {
69 return mlir::LLVM::LLVMPointerType::get(builder.getContext());
70 }
71 mlir::LLVM::LLVMVoidType llvmVoidTy() {
72 return mlir::LLVM::LLVMVoidType::get(builder.getContext());
73 }
74
75 // --- Location helpers ---
76 mlir::Location loc(AST::AstBase *ast);
77
78 // --- Runtime function declarations ---
79 void declareRuntimeFunctions();
80
81 // --- Type conversion ---
82 mlir::Type convertType(const Type &type);
83 bool isIntegerType(const Type &type);
84 bool isUnsignedIntegerType(const Type &type);
85 bool isFloatType(const Type &type);
86 bool isBoolType(const Type &type);
87 mlir::Type getEnumBackingMLIRType(const EnumType &et);
88
89 // --- Definition emission (MLIRGen.cpp) ---
90 void emitDefinition(AST::DefinitionAST *def);
91 mlir::FunctionType buildFuncType(AST::PrototypeAST *proto);
92 std::string mangleName(const sammine_util::QualifiedName &qn) const;
93
94 // --- Expression dispatcher + block/vardef (MLIRGen.cpp) ---
95 mlir::Value emitExpr(AST::ExprAST *ast);
96 mlir::Value emitBlock(AST::BlockAST *ast);
97 mlir::Value emitVarDef(AST::VarDefAST *ast);
98
99 // --- Functions/externs/closures/calls (MLIRGenFunction.cpp) ---
100 void forwardDeclareFunc(AST::PrototypeAST *proto);
101 void emitFunction(AST::FuncDefAST *ast);
102 void emitExtern(AST::ExternAST *ast);
103 mlir::Value buildClosure(mlir::Value codePtr, mlir::Value envPtr,
104 mlir::Location loc);
105 mlir::LLVM::LLVMFunctionType getClosureFuncType(const FunctionType &ft);
106 std::string getOrCreateClosureWrapper(const std::string &funcName,
107 const FunctionType &ft);
108 mlir::Value emitCallExpr(AST::CallExprAST *ast);
109 mlir::Value emitPartialApplication(AST::CallExprAST *ast,
110 const std::string &calleeName,
111 llvm::ArrayRef<mlir::Value> boundArgs);
112 mlir::Value emitIndirectCall(AST::CallExprAST *ast,
113 llvm::ArrayRef<mlir::Value> operands);
114 void emitFuncCallAndLLVMReturn(llvm::StringRef callee, const Type &retType,
115 mlir::ValueRange args, mlir::Location loc);
116 mlir::Value emitReturnExpr(AST::ReturnExprAST *ast);
117
118 // --- Expression emission (MLIRGenExpr.cpp) ---
119 mlir::Value emitNumberExpr(AST::NumberExprAST *ast);
120 mlir::Value emitBoolExpr(AST::BoolExprAST *ast);
121 mlir::Value emitCharExpr(AST::CharExprAST *ast);
122 mlir::Value emitUnitExpr(AST::UnitExprAST *ast);
123 mlir::Value emitVariableExpr(AST::VariableExprAST *ast);
124 mlir::Value emitBinaryExpr(AST::BinaryExprAST *ast);
125 mlir::Value emitUnaryNegExpr(AST::UnaryNegExprAST *ast);
126 mlir::Value emitIfExpr(AST::IfExprAST *ast);
127 mlir::Value emitWhileExpr(AST::WhileExprAST *ast);
128 mlir::Value emitStringExpr(AST::StringExprAST *ast);
129 mlir::Value emitArrayLiteralExpr(AST::ArrayLiteralExprAST *ast);
130 mlir::Value emitIndexExpr(AST::IndexExprAST *ast);
131 mlir::Value emitPtrArrayGEP(mlir::Value ptr, mlir::Value idx,
132 const ArrayType &arrType,
133 mlir::Location location);
134 mlir::Value emitPtrArrayLoad(mlir::Value ptr, mlir::Value idx,
135 const ArrayType &arrType,
136 mlir::Location location);
137 void emitPtrArrayStore(mlir::Value ptr, mlir::Value idx, mlir::Value val,
138 const ArrayType &arrType, mlir::Location location);
139 mlir::Value emitLenExpr(AST::LenExprAST *ast);
140 mlir::Value emitDerefExpr(AST::DerefExprAST *ast);
141 mlir::Value emitAddrOfExpr(AST::AddrOfExprAST *ast);
142 mlir::Value emitAllocExpr(AST::AllocExprAST *ast);
143 mlir::Value emitFreeExpr(AST::FreeExprAST *ast);
144 mlir::Value emitStructLiteralExpr(AST::StructLiteralExprAST *ast);
145 mlir::Value emitFieldAccessExpr(AST::FieldAccessExprAST *ast);
146 mlir::Value emitEnumConstructor(AST::CallExprAST *ast);
147 mlir::Value emitCaseExpr(AST::CaseExprAST *ast);
148 mlir::Value emitTupleLiteralExpr(AST::TupleLiteralExprAST *ast);
149 mlir::Value emitIntegerBackedCaseExpr(AST::CaseExprAST *ast,
150 mlir::Value scrutineeVal,
151 const EnumType &et);
152 void emitBoundsCheck(mlir::Value idx, size_t arrSize,
153 mlir::Location location);
154 mlir::Value emitArrayComparison(mlir::Value lhs, mlir::Value rhs,
155 const Type &arrType, TokenType tok,
156 mlir::Location location);
157
158 // --- Helpers (MLIRGen.cpp) ---
159 mlir::Value emitAllocaOne(mlir::Type elemType, mlir::Location loc);
160 int64_t getTypeSize(const Type &type);
161 mlir::Value getOrCreateGlobalString(llvm::StringRef name,
162 llvm::StringRef value,
163 mlir::Location location);
164};
165
166} // namespace sammine_lang
Defined the AST Node classes (ProgramAST, StructDefAST, FuncDefAST) and a visitor interface for trave...
Defines the core Type system for Sammine.
Holds classes and functionalities for dealing with Error handling, source locations caching & indexin...
Definition Types.h:68
Definition Types.h:97
Definition Types.h:43
Definition ASTProperties.h:40
Definition AstBase.h:341
An AST to simulate a { } code block.
Definition Ast.h:317
Definition Ast.h:485
Definition Ast.h:548
Definition Ast.h:798
Definition Ast.h:495
Definition Ast.h:302
A Function Definition that has the prototype and definition in terms of a block.
Definition Ast.h:291
Definition Ast.h:667
Definition Ast.h:326
Definition Ast.h:598
Definition Ast.h:707
Definition AstBase.h:239
Definition Ast.h:197
A prototype to present "func func_name(...) -> type;".
Definition Ast.h:236
Definition Ast.h:581
A variable definition: "var x = expression;" or "let (a, b) = expr;".
Definition Ast.h:420
Definition Types.h:139
Definition QualifiedName.h:23