pipeline_script/compiler/
mod.rs1mod expr;
2mod stmt;
3mod r#type;
4
5use crate::context::Context;
6use crate::llvm::context::LLVMContext;
7use crate::llvm::global::Global;
8use crate::llvm::module::LLVMModule;
9use std::collections::HashMap;
10use std::rc::Rc;
11use std::sync::RwLock;
12
13use crate::ast::module::Module;
14use crate::context::key::ContextKey;
15use crate::context::value::ContextValue;
16use crate::llvm::types::LLVMType;
17use crate::llvm::value::fucntion::FunctionValue;
18use crate::llvm::value::LLVMValue;
19
20pub struct Compiler {
21 module: Module,
22 ctx: LLVMContext,
23 builtin_symbol: HashMap<String, LLVMValue>,
24 llvm_module: Rc<RwLock<LLVMModule>>,
25}
26
27impl Compiler {
28 pub fn new(module: Module) -> Self {
29 let llvm_ctx = LLVMContext::new();
30 let llvm_module = llvm_ctx.create_module(module.get_name());
31 Self {
32 ctx: llvm_ctx,
33 module,
34 llvm_module: Rc::new(RwLock::new(llvm_module)),
35 builtin_symbol: HashMap::new(),
36 }
37 }
38 pub fn register_builtin_symbol(&mut self, name: &str, ty: LLVMValue) {
39 self.builtin_symbol.insert(name.to_string(), ty);
40 }
41 pub fn register_llvm_function(&mut self, name: &str, ty: LLVMType, arg_names: Vec<String>) {
42 self.llvm_module
43 .write()
44 .unwrap()
45 .register_function(name, ty, arg_names);
46 }
47 pub fn compile(&mut self, ctx: &Context) -> Rc<RwLock<LLVMModule>> {
48 let ctx = Context::with_type_table(ctx, HashMap::new());
49 let ctx = Context::with_scope(&ctx);
51 let ctx = Context::with_value(
52 &ctx,
53 ContextKey::LLVMModule,
54 ContextValue::LLVMModule(self.llvm_module.clone()),
55 );
56 for (name, ty) in self.builtin_symbol.iter() {
58 ctx.set_symbol(name.clone(), ty.clone());
59 }
60 for (name, item) in self.module.get_structs() {
62 if !item.generics.is_empty() {
63 continue;
64 }
65 let fields = item.get_fields();
66 let mut field_index: HashMap<String, usize> = HashMap::new();
67 for (i, field) in fields.iter().enumerate() {
68 field_index.insert(field.name.clone(), i);
69 }
70 let t = item.get_type();
71 let mut t = self.get_type(&ctx, &t);
72 if t.is_function() {
73 t = Global::pointer_type(t);
74 }
75 self.llvm_module
76 .write()
77 .unwrap()
78 .register_struct(name, field_index, t);
79 }
80 for (name, item) in self.module.get_type_aliases().iter() {
82 let t = self.compile_type(item.get_type());
83 self.llvm_module
84 .write()
85 .unwrap()
86 .register_struct(name, HashMap::new(), t);
87 }
88 let builder = Global::create_builder();
89 let ctx = Context::with_builder(&ctx, builder);
90 let ctx = Context::with_default_expr(&ctx);
91 for (name, item) in self.module.get_functions().iter() {
93 if item.is_template {
94 continue;
95 }
96 let args = item.args();
97 let mut arg_types = vec![];
98 let mut is_var_arg = false;
99 for arg in args {
100 if arg.is_var_arg() {
101 is_var_arg = true;
102 continue;
103 }
104 let ty = arg.r#type().unwrap();
105 let t = self.get_type(&ctx, &ty);
106 if t.is_function() {
108 arg_types.push((
109 arg.name().to_string(),
110 Global::struct_type(
111 "Closure".into(),
112 vec![
113 ("ptr".to_string(), Global::pointer_type(t)),
114 ("env".to_string(), Global::pointer_type(Global::unit_type())),
115 ],
116 ),
117 ));
118 continue;
119 }
120 arg_types.push((arg.name().to_string(), t));
121 }
122 let t;
123 let return_type0 = item.return_type();
124 let return_type = self.get_type(&ctx, return_type0);
125 if is_var_arg || item.is_vararg() {
126 t = Global::function_type_with_var_arg(return_type.clone(), arg_types);
127 } else {
128 t = Global::function_type(return_type.clone(), arg_types);
129 }
130 let f = if item.is_extern {
131 self.llvm_module
132 .write()
133 .unwrap()
134 .register_extern_function(name, t)
135 } else {
136 let args = item.args();
137 let param_names: Vec<String> =
138 args.iter().map(|arg| arg.name()).collect::<Vec<_>>();
139 self.llvm_module
140 .write()
141 .unwrap()
142 .register_function(name, t, param_names)
143 };
144 let mut function_value = FunctionValue::new(
145 f.get_function_ref(),
146 name.clone(),
147 Box::new(return_type.get_undef()),
148 args.iter()
149 .map(|arg| {
150 let name = arg.name().clone();
151 let undef = self.compile_type(&arg.r#type().unwrap()).get_undef();
152
153 if arg.is_env() {
154 if let Some(default_expr) = arg.get_default() {
155 ctx.set_default_expr(name.clone(), Box::new(default_expr.clone()));
156 }
157 (name, undef)
158 } else if let Some(default_expr) = arg.get_default() {
159 (name, self.compile_expr(default_expr, &ctx))
160 } else {
161 (name, undef)
162 }
163 })
164 .collect(),
165 );
166 if item.is_vararg() || is_var_arg {
167 function_value.set_vararg();
168 }
169 ctx.set_symbol(name.clone(), function_value.into());
170 }
171
172 for (_, item) in self.module.get_functions().iter() {
174 if item.is_extern || item.is_template {
175 continue;
176 }
177 let ctx = self.prepare_function(&ctx, item);
178 for stmt in item.body() {
179 self.compile_stmt(stmt, &ctx);
180 }
181 let flag = ctx.get_flag("return").unwrap();
182 if !flag {
183 let builder = ctx.get_builder();
184 builder.build_return_void();
185 }
186 }
187 let main = self.llvm_module.write().unwrap().register_function(
189 "$Module.main",
190 Global::function_type(Global::unit_type(), vec![]),
191 vec![],
192 );
193 let block = self.module.get_global_block();
194
195 let entry = main.append_basic_block("entry");
196 let builder = ctx.get_builder();
197 builder.position_at_end(entry);
198 let function_value = FunctionValue::new(
199 main.get_function_ref(),
200 "$Module.main".into(),
201 Box::new(Global::unit_type().get_undef()),
202 vec![],
203 );
204 let ctx = Context::with_function(&ctx, function_value);
205 let ctx = Context::with_scope(&ctx);
206 let ctx = Context::with_flag(&ctx, "return", false);
207 for stmt in block.iter() {
208 self.compile_stmt(stmt, &ctx);
209 }
210 let flag = ctx.get_flag("return").unwrap();
211 if !flag {
212 builder.build_return_void();
213 }
214 self.llvm_module.clone()
215 }
216 pub fn prepare_function(
217 &self,
218 ctx: &Context,
219 function: &crate::ast::function::Function,
220 ) -> Context {
221 let function_value = ctx
222 .get_symbol(function.name())
223 .unwrap()
224 .as_function()
225 .unwrap();
226 let entry = function_value.append_basic_block("entry");
227 let builder = ctx.get_builder();
228 builder.position_at_end(entry);
229 let ctx = Context::with_function(&ctx, function_value.clone());
230 let ctx = Context::with_type(&ctx, "current_function".into(), function.get_type());
231 let ctx = Context::with_scope(&ctx);
232 let ctx = Context::with_flag(&ctx, "return", false);
233 for arg in function.args() {
235 let arg_name = arg.name();
236 let mut arg_value = function_value.get_param(arg_name.clone()).unwrap();
237 if let LLVMValue::Function(f) = &mut arg_value {
238 f.set_closure()
239 }
240 ctx.set_symbol(arg_name, arg_value);
241 }
242 ctx
243 }
244}