Skip to content

Commit

Permalink
misc: refactored symbols.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra committed May 23, 2020
1 parent 94ce921 commit 61c12b2
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 253 deletions.
53 changes: 3 additions & 50 deletions crates/mun_codegen/src/code_gen.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use crate::code_gen::linker::LinkerError;
use crate::value::{CanInternalize, IrTypeContext, IrValueContext};
use crate::value::{IrTypeContext, IrValueContext};
use crate::IrDatabase;
use failure::Fail;
use hir::{FileId, RelativePathBuf};
use inkwell::targets::TargetData;
use inkwell::{
module::{Linkage, Module},
module::Module,
passes::{PassManager, PassManagerBuilder},
targets::{CodeModel, FileType, InitializationConfig, RelocMode, Target, TargetMachine},
types::StructType,
values::{BasicValue, GlobalValue, UnnamedAddress},
AddressSpace, OptimizationLevel,
OptimizationLevel,
};
use mun_target::spec;
use std::ffi::CString;
use std::io::{self, Write};
use std::{
path::{Path, PathBuf},
Expand Down Expand Up @@ -217,50 +214,6 @@ fn optimize_module(module: &Module, optimization_lvl: OptimizationLevel) {
module_pass_manager.run_on(module);
}

/// Intern a string by constructing a global value. Looks something like this:
/// ```c
/// const char[] GLOBAL_ = "str";
/// ```
pub(crate) fn intern_string(
context: &IrValueContext,
string: &str,
name: &str,
) -> inkwell::values::PointerValue {
CString::new(string)
.expect("could not convert IR string to CString")
.intern(name, context)
.into()
}

/// Construct a global from the specified value
pub(crate) fn gen_global(module: &Module, value: &dyn BasicValue, name: &str) -> GlobalValue {
let global = module.add_global(value.as_basic_value_enum().get_type(), None, name);
global.set_linkage(Linkage::Private);
global.set_constant(true);
global.set_unnamed_address(UnnamedAddress::Global);
global.set_initializer(value);
global
}

/// Generates a global array from the specified list of struct pointers
pub(crate) fn gen_struct_ptr_array(
context: &IrValueContext,
ir_type: StructType,
ptrs: &[inkwell::values::PointerValue],
name: &str,
) -> inkwell::values::PointerValue {
if ptrs.is_empty() {
ir_type
.ptr_type(AddressSpace::Generic)
.ptr_type(AddressSpace::Generic)
.const_null()
} else {
let ptr_array_ir = ir_type.ptr_type(AddressSpace::Generic).const_array(&ptrs);

gen_global(context.module, &ptr_array_ir, name).as_pointer_value()
}
}

/// Create an inkwell TargetData from the target in the database
pub(crate) fn target_data_query(db: &impl IrDatabase) -> Arc<TargetData> {
Arc::new(TargetData::create(&db.target().data_layout))
Expand Down
Loading

0 comments on commit 61c12b2

Please sign in to comment.