Skip to content

Commit d3096c2

Browse files
committed
Move llvm bindings to their own crate
1 parent 61e84a5 commit d3096c2

File tree

9 files changed

+209
-19
lines changed

9 files changed

+209
-19
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,5 @@ src/etc/dl
8686
.settings/
8787
/build
8888
i686-pc-mingw32/
89-
src/librustc/lib/llvmdeps.rs
89+
src/librustc_llvm/llvmdeps.rs
9090
*.pot

mk/crates.mk

+4-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ TARGET_CRATES := libc std green rustuv native flate arena glob term semver \
5353
uuid serialize sync getopts collections num test time rand \
5454
url log regex graphviz core rlibc alloc debug rustrt \
5555
unicode
56-
HOST_CRATES := syntax rustc rustdoc fourcc hexfloat regex_macros fmt_macros
56+
HOST_CRATES := syntax rustc rustdoc fourcc hexfloat regex_macros fmt_macros rustc_llvm
5757
CRATES := $(TARGET_CRATES) $(HOST_CRATES)
5858
TOOLS := compiletest rustdoc rustc
5959

@@ -70,8 +70,9 @@ DEPS_green := std native:context_switch
7070
DEPS_rustuv := std native:uv native:uv_support
7171
DEPS_native := std
7272
DEPS_syntax := std term serialize log fmt_macros debug
73-
DEPS_rustc := syntax native:rustllvm flate arena serialize getopts \
74-
time log graphviz debug
73+
DEPS_rustc := syntax flate arena serialize getopts \
74+
time log graphviz debug rustc_llvm
75+
DEPS_rustc_llvm := native:rustllvm libc std
7576
DEPS_rustdoc := rustc native:hoedown serialize getopts \
7677
test time debug
7778
DEPS_flate := std native:miniz

mk/llvm.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ $(foreach host,$(CFG_HOST), \
5757
$(foreach host,$(CFG_HOST), \
5858
$(eval LLVM_CONFIGS := $(LLVM_CONFIGS) $(LLVM_CONFIG_$(host))))
5959

60-
$(S)src/librustc/lib/llvmdeps.rs: \
60+
$(S)src/librustc_llvm/llvmdeps.rs: \
6161
$(LLVM_CONFIGS) \
6262
$(S)src/etc/mklldeps.py \
6363
$(MKFILE_DEPS)

mk/target.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ SNAPSHOT_RUSTC_POST_CLEANUP=$(HBIN0_H_$(CFG_BUILD))/rustc$(X_$(CFG_BUILD))
134134

135135
define TARGET_HOST_RULES
136136

137-
$$(TLIB$(1)_T_$(2)_H_$(3))/stamp.rustc: $(S)src/librustc/lib/llvmdeps.rs
137+
$$(TLIB$(1)_T_$(2)_H_$(3))/stamp.rustc_llvm: $(S)src/librustc_llvm/llvmdeps.rs
138138

139139
$$(TBIN$(1)_T_$(2)_H_$(3))/:
140140
mkdir -p $$@

src/librustc/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern crate flate;
4141
extern crate getopts;
4242
extern crate graphviz;
4343
extern crate libc;
44+
extern crate llvm = "rustc_llvm";
4445
extern crate serialize;
4546
extern crate time;
4647
#[phase(plugin, link)] extern crate log;
@@ -128,8 +129,7 @@ pub mod util {
128129
}
129130

130131
pub mod lib {
131-
pub mod llvm;
132-
pub mod llvmdeps;
132+
pub use llvm;
133133
}
134134

135135
__build_diagnostic_array!(DIAGNOSTICS)

src/librustc/lib/llvmdeps.rs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// WARNING: THIS IS A GENERATED FILE, DO NOT MODIFY
12+
// take a look at src/etc/mklldeps.py if you're interested
13+
14+
#[cfg(target_arch = "x86_64", target_os = "linux")]
15+
#[link(name = "LLVMInstrumentation", kind = "static")]
16+
#[link(name = "LLVMInterpreter", kind = "static")]
17+
#[link(name = "LLVMMCJIT", kind = "static")]
18+
#[link(name = "LLVMRuntimeDyld", kind = "static")]
19+
#[link(name = "LLVMJIT", kind = "static")]
20+
#[link(name = "LLVMExecutionEngine", kind = "static")]
21+
#[link(name = "LLVMAsmParser", kind = "static")]
22+
#[link(name = "LLVMLinker", kind = "static")]
23+
#[link(name = "LLVMBitWriter", kind = "static")]
24+
#[link(name = "LLVMipo", kind = "static")]
25+
#[link(name = "LLVMVectorize", kind = "static")]
26+
#[link(name = "LLVMMipsDisassembler", kind = "static")]
27+
#[link(name = "LLVMMipsCodeGen", kind = "static")]
28+
#[link(name = "LLVMMipsAsmParser", kind = "static")]
29+
#[link(name = "LLVMMipsDesc", kind = "static")]
30+
#[link(name = "LLVMMipsInfo", kind = "static")]
31+
#[link(name = "LLVMMipsAsmPrinter", kind = "static")]
32+
#[link(name = "LLVMARMDisassembler", kind = "static")]
33+
#[link(name = "LLVMARMCodeGen", kind = "static")]
34+
#[link(name = "LLVMARMAsmParser", kind = "static")]
35+
#[link(name = "LLVMARMDesc", kind = "static")]
36+
#[link(name = "LLVMARMInfo", kind = "static")]
37+
#[link(name = "LLVMARMAsmPrinter", kind = "static")]
38+
#[link(name = "LLVMX86Disassembler", kind = "static")]
39+
#[link(name = "LLVMX86AsmParser", kind = "static")]
40+
#[link(name = "LLVMX86CodeGen", kind = "static")]
41+
#[link(name = "LLVMSelectionDAG", kind = "static")]
42+
#[link(name = "LLVMAsmPrinter", kind = "static")]
43+
#[link(name = "LLVMMCParser", kind = "static")]
44+
#[link(name = "LLVMCodeGen", kind = "static")]
45+
#[link(name = "LLVMScalarOpts", kind = "static")]
46+
#[link(name = "LLVMInstCombine", kind = "static")]
47+
#[link(name = "LLVMTransformUtils", kind = "static")]
48+
#[link(name = "LLVMipa", kind = "static")]
49+
#[link(name = "LLVMAnalysis", kind = "static")]
50+
#[link(name = "LLVMTarget", kind = "static")]
51+
#[link(name = "LLVMX86Desc", kind = "static")]
52+
#[link(name = "LLVMX86Info", kind = "static")]
53+
#[link(name = "LLVMX86AsmPrinter", kind = "static")]
54+
#[link(name = "LLVMMC", kind = "static")]
55+
#[link(name = "LLVMObject", kind = "static")]
56+
#[link(name = "LLVMBitReader", kind = "static")]
57+
#[link(name = "LLVMCore", kind = "static")]
58+
#[link(name = "LLVMX86Utils", kind = "static")]
59+
#[link(name = "LLVMSupport", kind = "static")]
60+
#[link(name = "pthread")]
61+
#[link(name = "dl")]
62+
#[link(name = "m")]
63+
#[link(name = "stdc++")]
64+
extern {}

src/librustc/middle/trans/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use driver::config::NoDebugInfo;
1212
use driver::session::Session;
1313
use lib::llvm::{ContextRef, ModuleRef, ValueRef};
14-
use lib::llvm::{llvm, TargetData, TypeNames};
14+
use lib::llvm::{llvm, TargetData};
1515
use lib::llvm::mk_target_data;
1616
use metadata::common::LinkMeta;
1717
use middle::resolve;
@@ -21,7 +21,7 @@ use middle::trans::builder::Builder;
2121
use middle::trans::common::{ExternMap,tydesc_info,BuilderRef_res};
2222
use middle::trans::debuginfo;
2323
use middle::trans::monomorphize::MonoId;
24-
use middle::trans::type_::Type;
24+
use middle::trans::type_::{Type, TypeNames};
2525
use middle::ty;
2626
use util::sha2::Sha256;
2727
use util::nodemap::{NodeMap, NodeSet, DefIdMap};

src/librustc/middle/trans/type_.rs

+52-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#![allow(non_uppercase_pattern_statics)]
1212

13-
use lib::llvm::{llvm, TypeRef, Bool, False, True, TypeKind};
13+
use lib::llvm::{llvm, TypeRef, Bool, False, True, TypeKind, ValueRef};
1414
use lib::llvm::{Float, Double, X86_FP80, PPC_FP128, FP128};
1515

1616
use middle::trans::context::CrateContext;
@@ -20,8 +20,11 @@ use syntax::abi::{X86, X86_64, Arm, Mips, Mipsel};
2020

2121
use std::c_str::ToCStr;
2222
use std::mem;
23+
use std::cell::RefCell;
24+
use std::collections::HashMap;
25+
use std::str::raw::from_c_str;
2326

24-
use libc::{c_uint};
27+
use libc::{c_uint, c_void, free};
2528

2629
#[deriving(Clone, PartialEq, Show)]
2730
pub struct Type {
@@ -303,3 +306,50 @@ impl Type {
303306
}
304307
}
305308
}
309+
310+
/* Memory-managed object interface to type handles. */
311+
312+
pub struct TypeNames {
313+
named_types: RefCell<HashMap<String, TypeRef>>,
314+
}
315+
316+
impl TypeNames {
317+
pub fn new() -> TypeNames {
318+
TypeNames {
319+
named_types: RefCell::new(HashMap::new())
320+
}
321+
}
322+
323+
pub fn associate_type(&self, s: &str, t: &Type) {
324+
assert!(self.named_types.borrow_mut().insert(s.to_string(),
325+
t.to_ref()));
326+
}
327+
328+
pub fn find_type(&self, s: &str) -> Option<Type> {
329+
self.named_types.borrow().find_equiv(&s).map(|x| Type::from_ref(*x))
330+
}
331+
332+
pub fn type_to_str(&self, ty: Type) -> String {
333+
unsafe {
334+
let s = llvm::LLVMTypeToString(ty.to_ref());
335+
let ret = from_c_str(s);
336+
free(s as *mut c_void);
337+
ret.to_string()
338+
}
339+
}
340+
341+
pub fn types_to_str(&self, tys: &[Type]) -> String {
342+
let strs: Vec<String> = tys.iter().map(|t| self.type_to_str(*t)).collect();
343+
format!("[{}]", strs.connect(","))
344+
}
345+
346+
pub fn val_to_str(&self, val: ValueRef) -> String {
347+
unsafe {
348+
let s = llvm::LLVMValueToString(val);
349+
let ret = from_c_str(s);
350+
free(s as *mut c_void);
351+
ret.to_string()
352+
}
353+
}
354+
}
355+

src/librustc/lib/llvm.rs src/librustc_llvm/lib.rs

+82-7
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,24 @@
1313
#![allow(non_snake_case_functions)]
1414
#![allow(dead_code)]
1515

16-
use std::c_str::ToCStr;
17-
use std::cell::RefCell;
18-
use std::collections::HashMap;
19-
use libc::{c_uint, c_ushort, c_void, free, uint64_t};
20-
use std::str::raw::from_c_str;
16+
#![crate_id = "rustc_llvm#0.11.0"]
17+
#![crate_name = "rustc_llvm"]
18+
#![experimental]
19+
#![license = "MIT/ASL2"]
20+
#![crate_type = "dylib"]
21+
#![crate_type = "rlib"]
22+
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
23+
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
24+
html_root_url = "http://doc.rust-lang.org/")]
25+
26+
#![feature(globs)]
27+
#![feature(link_args)]
28+
#![allow(unused_attribute)] // NOTE: remove after stage0
29+
30+
extern crate libc;
2131

22-
use middle::trans::type_::Type;
32+
use std::c_str::ToCStr;
33+
use libc::{c_uint, c_ushort, uint64_t, c_int, size_t};
2334

2435
pub type Opcode = u32;
2536
pub type Bool = c_uint;
@@ -338,6 +349,10 @@ pub mod llvm {
338349
use libc::{c_char, c_int, c_longlong, c_ushort, c_uint, c_ulonglong,
339350
size_t, uint64_t};
340351

352+
pub unsafe fn LLVMInitializeX86AsmPrinter() {
353+
LLVMInitializeX86AsmPrinter_()
354+
}
355+
341356
// Link to our native llvm bindings (things that we need to use the C++ api
342357
// for) and because llvm is written in C++ we need to link against libstdc++
343358
//
@@ -1747,7 +1762,8 @@ pub mod llvm {
17471762
pub fn LLVMInitializeX86TargetInfo();
17481763
pub fn LLVMInitializeX86Target();
17491764
pub fn LLVMInitializeX86TargetMC();
1750-
pub fn LLVMInitializeX86AsmPrinter();
1765+
#[link_name = "LLVMInitializeX86AsmPrinter"]
1766+
pub fn LLVMInitializeX86AsmPrinter_();
17511767
pub fn LLVMInitializeX86AsmParser();
17521768
pub fn LLVMInitializeARMTargetInfo();
17531769
pub fn LLVMInitializeARMTarget();
@@ -1861,6 +1877,7 @@ pub fn SetFunctionAttribute(fn_: ValueRef, attr: Attribute) {
18611877
llvm::LLVMAddFunctionAttribute(fn_, FunctionIndex as c_uint, attr as uint64_t)
18621878
}
18631879
}
1880+
18641881
/* Memory-managed object interface to type handles. */
18651882

18661883
pub struct TypeNames {
@@ -1981,3 +1998,61 @@ pub fn mk_section_iter(llof: ObjectFileRef) -> SectionIter {
19811998
}
19821999
}
19832000
}
2001+
2002+
// FIXME #15460 - create a public function that actually calls our
2003+
// static LLVM symbols. Otherwise the linker will just throw llvm
2004+
// away. We're just calling lots of stuff until we transitively get
2005+
// all of LLVM. This is worse than anything.
2006+
pub unsafe fn static_link_hack_this_sucks() {
2007+
llvm::LLVMInitializePasses();
2008+
2009+
llvm::LLVMInitializeX86TargetInfo();
2010+
llvm::LLVMInitializeX86Target();
2011+
llvm::LLVMInitializeX86TargetMC();
2012+
llvm::LLVMInitializeX86AsmPrinter();
2013+
llvm::LLVMInitializeX86AsmParser();
2014+
2015+
llvm::LLVMInitializeARMTargetInfo();
2016+
llvm::LLVMInitializeARMTarget();
2017+
llvm::LLVMInitializeARMTargetMC();
2018+
llvm::LLVMInitializeARMAsmPrinter();
2019+
llvm::LLVMInitializeARMAsmParser();
2020+
2021+
llvm::LLVMInitializeMipsTargetInfo();
2022+
llvm::LLVMInitializeMipsTarget();
2023+
llvm::LLVMInitializeMipsTargetMC();
2024+
llvm::LLVMInitializeMipsAsmPrinter();
2025+
llvm::LLVMInitializeMipsAsmParser();
2026+
2027+
llvm::LLVMRustSetLLVMOptions(0 as c_int,
2028+
0 as *const _);
2029+
2030+
llvm::LLVMPassManagerBuilderPopulateModulePassManager(0 as *mut _, 0 as *mut _);
2031+
llvm::LLVMPassManagerBuilderPopulateLTOPassManager(0 as *mut _, 0 as *mut _, False, False);
2032+
llvm::LLVMPassManagerBuilderPopulateFunctionPassManager(0 as *mut _, 0 as *mut _);
2033+
llvm::LLVMPassManagerBuilderSetOptLevel(0 as *mut _, 0 as c_uint);
2034+
llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(0 as *mut _, 0 as c_uint);
2035+
llvm::LLVMWriteBitcodeToFile(0 as *mut _, 0 as *const _);
2036+
llvm::LLVMPassManagerBuilderCreate();
2037+
llvm::LLVMPassManagerBuilderDispose(0 as *mut _);
2038+
2039+
llvm::LLVMRustLinkInExternalBitcode(0 as *mut _, 0 as *const _, 0 as size_t);
2040+
2041+
LLVMLinkInJIT();
2042+
LLVMLinkInMCJIT();
2043+
LLVMLinkInInterpreter();
2044+
2045+
extern {
2046+
fn LLVMLinkInJIT();
2047+
fn LLVMLinkInMCJIT();
2048+
fn LLVMLinkInInterpreter();
2049+
}
2050+
}
2051+
2052+
// The module containing the native LLVM dependencies, generated by the build system
2053+
// Note that this must come after the rustllvm extern declaration so that
2054+
// parts of LLVM that rustllvm depends on aren't thrown away by the linker.
2055+
// Works to the above fix for #15460 to ensure LLVM dependencies that
2056+
// are only used by rustllvm don't get stripped by the linker.
2057+
mod llvmdeps;
2058+

0 commit comments

Comments
 (0)