duskphantom_middle/ir/module.rs
1// Copyright 2024 Duskphantom Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// SPDX-License-Identifier: Apache-2.0
16
17use super::*;
18
19/// one module is one file
20pub struct Module {
21 /// global variables in this module
22 pub global_variables: Vec<GlobalPtr>,
23
24 /// functions in this module.
25 /// Make sure that the first function is `main` function.
26 pub functions: Vec<FunPtr>,
27
28 pub mem_pool: ObjPtr<IRBuilder>,
29}
30
31impl Module {
32 pub fn new(mem_pool: ObjPtr<IRBuilder>) -> Self {
33 Self {
34 functions: Vec::new(),
35 mem_pool,
36 global_variables: Vec::new(),
37 }
38 }
39
40 pub fn gen_llvm_ir(&self) -> String {
41 let mut ir = String::new();
42 for global in self.global_variables.iter() {
43 ir.push_str(&global.as_ref().gen_llvm_ir());
44 }
45 for fun in &self.functions {
46 ir.push_str(&fun.gen_llvm_ir());
47 }
48 ir
49 }
50}