Skip to content

Commit

Permalink
Merge pull request #92 from Wodann/feature/fn-visibility
Browse files Browse the repository at this point in the history
feat: only create symbols for public functions
  • Loading branch information
Wodann authored Mar 2, 2020
2 parents 78cbcde + ad72958 commit 1ce4626
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 76 deletions.
32 changes: 20 additions & 12 deletions crates/mun_codegen/src/code_gen/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use inkwell::{
};
use std::collections::{HashMap, HashSet};

struct GlobalArrayValue(GlobalValue, usize);

/// Construct an IR `MunTypeInfo` struct value for the specified `TypeInfo`
fn type_info_ir<D: IrDatabase>(
db: &D,
Expand Down Expand Up @@ -258,8 +260,9 @@ fn gen_function_info_array<'a, D: IrDatabase>(
types: &AbiTypes,
global_type_info_lookup_table: &HashMap<TypeInfo, PointerValue>,
functions: impl Iterator<Item = (&'a hir::Function, &'a FunctionValue)>,
) -> GlobalValue {
) -> GlobalArrayValue {
let function_infos: Vec<StructValue> = functions
.filter(|(f, _)| f.visibility(db) == hir::Visibility::Public)
.map(|(f, value)| {
// Get the function from the cloned module and modify the linkage of the function.
let value = module
Expand All @@ -278,8 +281,13 @@ fn gen_function_info_array<'a, D: IrDatabase>(
])
})
.collect();
let num_functions = function_infos.len();
let function_infos = types.function_info_type.const_array(&function_infos);
gen_global(module, &function_infos, "fn.get_info.functions")

GlobalArrayValue(
gen_global(module, &function_infos, "fn.get_info.functions"),
num_functions,
)
}

/// Construct a global that holds a reference to all structs. e.g.:
Expand Down Expand Up @@ -473,22 +481,22 @@ pub(super) fn gen_reflection_ir(
types.iter().cloned(),
);

let GlobalArrayValue(function_info, num_functions) = gen_function_info_array(
db,
module,
&abi_types,
&global_type_info_lookup_table,
function_map.iter(),
);

// Construct the module info struct
let module_info = abi_types.module_info_type.const_named_struct(&[
intern_string(module, "").into(),
gen_function_info_array(
db,
module,
&abi_types,
&global_type_info_lookup_table,
function_map.iter(),
)
.as_pointer_value()
.into(),
function_info.as_pointer_value().into(),
module
.get_context()
.i32_type()
.const_int(function_map.len() as u64, false)
.const_int(num_functions as u64, false)
.into(),
types.into(),
module
Expand Down
38 changes: 18 additions & 20 deletions crates/mun_lld/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ lazy_static! {
pb.push(binary_name);

let ver = llvm_version(&pb)
.expect(&format!("Failed to execute {:?}", &pb));
.unwrap_or_else(|_| panic!("Failed to execute {:?}", &pb));
if is_compatible_llvm(&ver) {
return pb;
} else {
Expand Down Expand Up @@ -108,7 +108,7 @@ fn locate_system_llvm_config() -> Option<&'static str> {
/// Check whether the given version of LLVM is blacklisted,
/// returning `Some(reason)` if it is.
fn is_blacklisted_llvm(llvm_version: &Version) -> Option<&'static str> {
static BLACKLIST: &'static [(u64, u64, u64, &'static str)] = &[];
static BLACKLIST: &[(u64, u64, u64, &str)] = &[];

let blacklist_var = format!(
"LLVM_SYS_{}_IGNORE_BLACKLIST",
Expand All @@ -131,9 +131,9 @@ fn is_blacklisted_llvm(llvm_version: &Version) -> Option<&'static str> {

for &(major, minor, patch, reason) in BLACKLIST.iter() {
let bad_version = Version {
major: major,
minor: minor,
patch: patch,
major,
minor,
patch,
pre: vec![],
build: vec![],
};
Expand Down Expand Up @@ -241,16 +241,14 @@ fn get_system_libcpp() -> Option<&'static str> {
if cfg!(target_env = "msvc") {
// MSVC doesn't need an explicit one.
None
} else if cfg!(target_os = "macos") {
} else if cfg!(target_os = "macos") || cfg!(target_os = "freebsd") {
// On OS X 10.9 and later, LLVM's libc++ is the default. On earlier
// releases GCC's libstdc++ is default. Unfortunately we can't
// reasonably detect which one we need (on older ones libc++ is
// available and can be selected with -stdlib=lib++), so assume the
// latest, at the cost of breaking the build on older OS releases
// when LLVM was built against libstdc++.
Some("c++")
} else if cfg!(target_os = "freebsd") {
Some("c++")
} else {
// Otherwise assume GCC's libstdc++.
// This assumption is probably wrong on some platforms, but would need
Expand Down Expand Up @@ -357,7 +355,7 @@ fn main() {
))
.is_some();
if cfg!(target_env = "msvc") && (use_debug_msvcrt || is_llvm_debug()) {
println!("cargo:rustc-link-lib={}", "msvcrtd");
println!("cargo:rustc-link-lib=msvcrtd");
}

// Link libffi if the user requested this workaround.
Expand All @@ -368,19 +366,19 @@ fn main() {
))
.is_some();
if force_ffi {
println!("cargo:rustc-link-lib=dylib={}", "ffi");
println!("cargo:rustc-link-lib=dylib=ffi");
}

println!("cargo:rustc-link-lib=static={}", "lldCOFF");
println!("cargo:rustc-link-lib=static={}", "lldCommon");
println!("cargo:rustc-link-lib=static={}", "lldCore");
println!("cargo:rustc-link-lib=static={}", "lldDriver");
println!("cargo:rustc-link-lib=static={}", "lldELF");
println!("cargo:rustc-link-lib=static={}", "lldMachO");
println!("cargo:rustc-link-lib=static={}", "lldMinGW");
println!("cargo:rustc-link-lib=static={}", "lldReaderWriter");
println!("cargo:rustc-link-lib=static={}", "lldWasm");
println!("cargo:rustc-link-lib=static={}", "lldYAML");
println!("cargo:rustc-link-lib=static=lldCOFF");
println!("cargo:rustc-link-lib=static=lldCommon");
println!("cargo:rustc-link-lib=static=lldCore");
println!("cargo:rustc-link-lib=static=lldDriver");
println!("cargo:rustc-link-lib=static=lldELF");
println!("cargo:rustc-link-lib=static=lldMachO");
println!("cargo:rustc-link-lib=static=lldMinGW");
println!("cargo:rustc-link-lib=static=lldReaderWriter");
println!("cargo:rustc-link-lib=static=lldWasm");
println!("cargo:rustc-link-lib=static=lldYAML");

if cfg!(not(target_os = "windows")) {
println!("cargo:rustc-link-lib=dylib=ffi");
Expand Down
70 changes: 35 additions & 35 deletions crates/mun_runtime/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ macro_rules! assert_invoke_eq {
fn compile_and_run() {
let mut driver = TestDriver::new(
r"
fn main() {}
pub fn main() {}
",
);
assert_invoke_eq!((), (), driver, "main");
Expand All @@ -88,7 +88,7 @@ fn compile_and_run() {
fn return_value() {
let mut driver = TestDriver::new(
r"
fn main():int { 3 }
pub fn main():int { 3 }
",
);
assert_invoke_eq!(i64, 3, driver, "main");
Expand All @@ -98,7 +98,7 @@ fn return_value() {
fn arguments() {
let mut driver = TestDriver::new(
r"
fn main(a:int, b:int):int { a+b }
pub fn main(a:int, b:int):int { a+b }
",
);
let a: i64 = 52;
Expand All @@ -110,8 +110,8 @@ fn arguments() {
fn dispatch_table() {
let mut driver = TestDriver::new(
r"
fn add(a:int, b:int):int { a+b }
fn main(a:int, b:int):int { add(a,b) }
pub fn add(a:int, b:int):int { a+b }
pub fn main(a:int, b:int):int { add(a,b) }
",
);

Expand All @@ -128,18 +128,18 @@ fn dispatch_table() {
fn booleans() {
let mut driver = TestDriver::new(
r#"
fn equal(a:int, b:int):bool { a==b }
fn equalf(a:float, b:float):bool { a==b }
fn not_equal(a:int, b:int):bool { a!=b }
fn not_equalf(a:float, b:float):bool { a!=b }
fn less(a:int, b:int):bool { a<b }
fn lessf(a:float, b:float):bool { a<b }
fn greater(a:int, b:int):bool { a>b }
fn greaterf(a:float, b:float):bool { a>b }
fn less_equal(a:int, b:int):bool { a<=b }
fn less_equalf(a:float, b:float):bool { a<=b }
fn greater_equal(a:int, b:int):bool { a>=b }
fn greater_equalf(a:float, b:float):bool { a>=b }
pub fn equal(a:int, b:int):bool { a==b }
pub fn equalf(a:float, b:float):bool { a==b }
pub fn not_equal(a:int, b:int):bool { a!=b }
pub fn not_equalf(a:float, b:float):bool { a!=b }
pub fn less(a:int, b:int):bool { a<b }
pub fn lessf(a:float, b:float):bool { a<b }
pub fn greater(a:int, b:int):bool { a>b }
pub fn greaterf(a:float, b:float):bool { a>b }
pub fn less_equal(a:int, b:int):bool { a<=b }
pub fn less_equalf(a:float, b:float):bool { a<=b }
pub fn greater_equal(a:int, b:int):bool { a>=b }
pub fn greater_equalf(a:float, b:float):bool { a>=b }
"#,
);
assert_invoke_eq!(bool, false, driver, "equal", 52i64, 764i64);
Expand Down Expand Up @@ -172,7 +172,7 @@ fn booleans() {
fn fibonacci() {
let mut driver = TestDriver::new(
r#"
fn fibonacci(n:int):int {
pub fn fibonacci(n:int):int {
if n <= 1 {
n
} else {
Expand All @@ -191,7 +191,7 @@ fn fibonacci() {
fn fibonacci_loop() {
let mut driver = TestDriver::new(
r#"
fn fibonacci(n:int):int {
pub fn fibonacci(n:int):int {
let a = 0;
let b = 1;
let i = 1;
Expand All @@ -218,7 +218,7 @@ fn fibonacci_loop() {
fn fibonacci_loop_break() {
let mut driver = TestDriver::new(
r#"
fn fibonacci(n:int):int {
pub fn fibonacci(n:int):int {
let a = 0;
let b = 1;
let i = 1;
Expand All @@ -245,7 +245,7 @@ fn fibonacci_loop_break() {
fn fibonacci_while() {
let mut driver = TestDriver::new(
r#"
fn fibonacci(n:int):int {
pub fn fibonacci(n:int):int {
let a = 0;
let b = 1;
let i = 1;
Expand All @@ -270,11 +270,11 @@ fn fibonacci_while() {
fn true_is_true() {
let mut driver = TestDriver::new(
r#"
fn test_true():bool {
pub fn test_true():bool {
true
}
fn test_false():bool {
pub fn test_false():bool {
false
}
"#,
Expand All @@ -287,13 +287,13 @@ fn true_is_true() {
fn hotreloadable() {
let mut driver = TestDriver::new(
r"
fn main():int { 5 }
pub fn main():int { 5 }
",
);
assert_invoke_eq!(i64, 5, driver, "main");
driver.update(
r"
fn main():int { 10 }
pub fn main():int { 10 }
",
);
assert_invoke_eq!(i64, 10, driver, "main");
Expand All @@ -310,7 +310,7 @@ fn compiler_valid_utf8() {
a: int,
}
fn foo(n:Foo):bool { false }
pub fn foo(n:Foo):bool { false }
"#,
);

Expand Down Expand Up @@ -355,7 +355,7 @@ fn fields() {
let mut driver = TestDriver::new(
r#"
struct(gc) Foo { a:int, b:int };
fn main(foo:int):bool {
pub fn main(foo:int):bool {
let a = Foo { a: foo, b: foo };
a.a += a.b;
let result = a;
Expand All @@ -373,7 +373,7 @@ fn field_crash() {
r#"
struct(gc) Foo { a: int };
fn main(c:int):int {
pub fn main(c:int):int {
let b = Foo { a: c + 5 }
b.a
}
Expand All @@ -389,16 +389,16 @@ fn marshal_struct() {
struct(gc) Foo { a: int, b: bool, c: float, };
struct Bar(Foo);
fn foo_new(a: int, b: bool, c: float): Foo {
pub fn foo_new(a: int, b: bool, c: float): Foo {
Foo { a, b, c, }
}
fn bar_new(foo: Foo): Bar {
pub fn bar_new(foo: Foo): Bar {
Bar(foo)
}
fn foo_a(foo: Foo):int { foo.a }
fn foo_b(foo: Foo):bool { foo.b }
fn foo_c(foo: Foo):float { foo.c }
pub fn foo_a(foo: Foo):int { foo.a }
pub fn foo_b(foo: Foo):bool { foo.b }
pub fn foo_c(foo: Foo):float { foo.c }
"#,
);

Expand Down Expand Up @@ -473,7 +473,7 @@ fn hotreload_struct_decl() {
m: float,
}
fn args(): Args {
pub fn args(): Args {
Args { n: 3, foo: Bar { m: 1.0 }, }
}
"#,
Expand All @@ -489,7 +489,7 @@ fn hotreload_struct_decl() {
m: int,
}
fn args(): Args {
pub fn args(): Args {
Args { n: 3, foo: Bar { m: 1 }, }
}
"#,
Expand Down
Loading

0 comments on commit 1ce4626

Please sign in to comment.