1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use std::collections::HashMap;
use std::ffi::{CStr, CString};
use std::sync::Arc;
use std::{env, fs, io, path, process, ptr};

use llvm_sys::core::*;
use llvm_sys::prelude::*;
use llvm_sys::target_machine::*;
use llvm_sys::LLVMLinkage;

use crate::codegen::analysis::AnalysedMod;
use crate::codegen::mod_gen::{gen_mod, GeneratedMod};
use crate::codegen::target_gen::TargetCtx;
use crate::context::LinkedLibrary;
use crate::libcstr;
use crate::mir;
use crate::SourceLoader;

#[derive(Copy, Clone, PartialEq)]
pub enum OutputType {
    None,
    LlvmIr,
    Assembly,
    Object,
    Executable,
}

#[derive(Copy, Clone, PartialEq)]
pub struct Options<'target> {
    target_triple: Option<&'target str>,
    output_type: OutputType,
    llvm_opt: bool,
}

impl<'target> Options<'target> {
    pub fn new() -> Options<'static> {
        Options {
            target_triple: None,
            output_type: OutputType::Executable,
            llvm_opt: true,
        }
    }

    pub fn with_target_triple(self, target_triple: Option<&'target str>) -> Options<'target> {
        Options {
            target_triple,
            ..self
        }
    }

    pub fn with_llvm_opt(self, llvm_opt: bool) -> Options<'target> {
        Options { llvm_opt, ..self }
    }

    pub fn with_output_type(self, output_type: OutputType) -> Options<'target> {
        Options {
            output_type,
            ..self
        }
    }

    pub fn output_type(&self) -> OutputType {
        self.output_type
    }
}

impl Default for Options<'static> {
    fn default() -> Options<'static> {
        Options::new()
    }
}

fn arret_main_llvm_type(tcx: &mut TargetCtx) -> LLVMTypeRef {
    unsafe {
        let llvm_arg_types = &mut [tcx.task_llvm_ptr_type()];

        LLVMFunctionType(
            LLVMVoidTypeInContext(tcx.llx),
            llvm_arg_types.as_mut_ptr(),
            llvm_arg_types.len() as u32,
            0,
        )
    }
}

fn c_main_llvm_type(tcx: &mut TargetCtx) -> LLVMTypeRef {
    unsafe {
        let llvm_argc_type = LLVMInt32TypeInContext(tcx.llx);
        let llvm_argv_type = LLVMPointerType(LLVMPointerType(LLVMInt8TypeInContext(tcx.llx), 0), 0);
        let llvm_ret_type = LLVMInt32TypeInContext(tcx.llx);

        let llvm_arg_types = &mut [llvm_argc_type, llvm_argv_type];

        LLVMFunctionType(
            llvm_ret_type,
            llvm_arg_types.as_mut_ptr(),
            llvm_arg_types.len() as u32,
            0,
        )
    }
}

fn program_to_module(
    tcx: &mut TargetCtx,
    program: &mir::BuiltProgram,
    debug_source_loader: Option<&SourceLoader>,
) -> LLVMModuleRef {
    unsafe {
        let analysed_mod = AnalysedMod::new(&program.private_funs, &program.main);

        // Build our Arret funs
        let GeneratedMod {
            llvm_module,
            llvm_entry_fun: llvm_arret_main,
            llvm_global_interned_names,
            llvm_classmap_classes,
        } = gen_mod(
            tcx,
            b"program\0",
            &analysed_mod,
            None,
            HashMap::new(),
            debug_source_loader,
        );

        LLVMSetLinkage(llvm_arret_main, LLVMLinkage::LLVMPrivateLinkage);

        // Declare our C main
        let builder = LLVMCreateBuilderInContext(tcx.llx);
        let c_main = LLVMAddFunction(llvm_module, libcstr!("main"), c_main_llvm_type(tcx));

        let bb = LLVMAppendBasicBlockInContext(tcx.llx, c_main, libcstr!("entry"));
        LLVMPositionBuilderAtEnd(builder, bb);

        let classmap_class_ptr_type = LLVMPointerType(tcx.classmap_class_llvm_type(), 0);

        // Declare arret_runtime_launch_task
        let launch_task_llvm_arg_types = &mut [
            LLVMTypeOf(llvm_global_interned_names),
            classmap_class_ptr_type,
            LLVMPointerType(arret_main_llvm_type(tcx), 0),
        ];

        let launch_task_llvm_type = LLVMFunctionType(
            LLVMVoidTypeInContext(tcx.llx),
            launch_task_llvm_arg_types.as_mut_ptr(),
            launch_task_llvm_arg_types.len() as u32,
            0,
        );

        // And launch the task from C main
        let launch_task_llvm_fun = LLVMAddFunction(
            llvm_module,
            libcstr!("arret_runtime_launch_task"),
            launch_task_llvm_type,
        );

        let launch_task_llvm_args = &mut [
            llvm_global_interned_names,
            llvm_classmap_classes,
            llvm_arret_main,
        ];

        LLVMBuildCall(
            builder,
            launch_task_llvm_fun,
            launch_task_llvm_args.as_mut_ptr(),
            launch_task_llvm_args.len() as u32,
            libcstr!(""),
        );

        LLVMBuildRet(builder, LLVMConstInt(LLVMInt32TypeInContext(tcx.llx), 0, 0));
        LLVMDisposeBuilder(builder);

        llvm_module
    }
}

fn target_triple_to_cc_args(target_triple: &str) -> Vec<&str> {
    // Try to use -m32 when possible for compatibility with GCC
    if (cfg!(target_arch = "x86_64") && target_triple.starts_with("i686-"))
        || (cfg!(target_arch = "aarch64") && target_triple.starts_with("arm"))
    {
        vec!["-m32"]
    } else {
        vec!["-target", target_triple]
    }
}

/// Generates code for the program with the given output type
///
/// `codegen::initialise_llvm()` must be called before this.
pub fn gen_program(
    options: Options<'_>,
    linked_libraries: &[Arc<LinkedLibrary>],
    program: &mir::BuiltProgram,
    output_file: &path::Path,
    debug_source_loader: Option<&SourceLoader>,
) {
    use crate::codegen::target_machine::create_target_machine;

    if env::var_os("ARRET_DUMP_MIR").is_some() {
        mir::print_program(&mut io::stdout().lock(), program, debug_source_loader).unwrap();
    }

    let Options {
        target_triple,
        output_type,
        llvm_opt,
    } = options;

    let llvm_output_path = if output_type == OutputType::Executable {
        // When outputting an executable this is an intermediate file that we pass to our linker
        output_file.with_extension("o")
    } else {
        // Otherwise this is the final destination
        output_file.to_owned()
    };

    let llvm_output_path_cstring = CString::new(llvm_output_path.to_str().unwrap()).unwrap();

    let target_machine = create_target_machine(
        target_triple,
        LLVMRelocMode::LLVMRelocDynamicNoPic,
        LLVMCodeModel::LLVMCodeModelDefault,
    );

    let mut tcx = TargetCtx::new(target_machine, llvm_opt);
    let module = program_to_module(&mut tcx, program, debug_source_loader);
    tcx.finish_module(module);

    unsafe {
        let mut error: *mut libc::c_char = ptr::null_mut();

        let llvm_code_gen_file_type = match output_type {
            OutputType::None => {
                LLVMDisposeTargetMachine(target_machine);
                return;
            }
            OutputType::LlvmIr => {
                if LLVMPrintModuleToFile(
                    module,
                    llvm_output_path_cstring.as_ptr() as *mut _,
                    &mut error as *mut _,
                ) != 0
                {
                    panic!(
                        "LLVMPrintModuleToFile: {}",
                        CStr::from_ptr(error).to_str().unwrap()
                    );
                }
                return;
            }
            OutputType::Assembly => LLVMCodeGenFileType::LLVMAssemblyFile,
            OutputType::Object | OutputType::Executable => LLVMCodeGenFileType::LLVMObjectFile,
        };

        if LLVMTargetMachineEmitToFile(
            target_machine,
            module,
            llvm_output_path_cstring.as_ptr() as *mut _,
            llvm_code_gen_file_type,
            &mut error as *mut _,
        ) != 0
        {
            panic!(
                "LLVMTargetMachineEmitToFile: {}",
                CStr::from_ptr(error).to_str().unwrap()
            );
        }
        LLVMDisposeTargetMachine(target_machine);
    }

    if output_type == OutputType::Executable {
        let target_args = match target_triple {
            Some(triple) => target_triple_to_cc_args(triple),
            None => vec![],
        };

        let status = process::Command::new("cc")
            .arg(llvm_output_path.clone())
            .args(target_args)
            .arg("-o")
            .arg(output_file)
            .args(linked_libraries.iter().map(|l| l.target_path()))
            .arg("-pthread")
            .arg("-ldl")
            .arg("-lm")
            .status()
            .unwrap();

        let _ = fs::remove_file(llvm_output_path);

        if !status.success() {
            panic!("Error invoking linker");
        }
    }
}