Skip to content

File tree

11 files changed

+348
-358
lines changed

11 files changed

+348
-358
lines changed
 

‎src/Cargo.lock

-3
Original file line numberDiff line numberDiff line change
@@ -2222,11 +2222,8 @@ dependencies = [
22222222
name = "rustc_llvm"
22232223
version = "0.0.0"
22242224
dependencies = [
2225-
"bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
22262225
"build_helper 0.1.0",
22272226
"cc 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)",
2228-
"libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
2229-
"rustc_cratesio_shim 0.0.0",
22302227
]
22312228

22322229
[[package]]

‎src/librustc_codegen_llvm/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#![feature(rustc_diagnostic_macros)]
3030
#![feature(slice_sort_by_cached_key)]
3131
#![feature(optin_builtin_traits)]
32+
#![feature(concat_idents)]
33+
#![feature(link_args)]
34+
#![feature(static_nobundle)]
3235

3336
use rustc::dep_graph::WorkProduct;
3437
use syntax_pos::symbol::Symbol;
@@ -46,7 +49,7 @@ extern crate rustc_target;
4649
#[macro_use] extern crate rustc_data_structures;
4750
extern crate rustc_demangle;
4851
extern crate rustc_incremental;
49-
extern crate rustc_llvm as llvm;
52+
extern crate rustc_llvm;
5053
extern crate rustc_platform_intrinsics as intrinsics;
5154
extern crate rustc_codegen_utils;
5255

@@ -110,6 +113,7 @@ mod debuginfo;
110113
mod declare;
111114
mod glue;
112115
mod intrinsic;
116+
pub mod llvm;
113117
mod llvm_util;
114118
mod metadata;
115119
mod meth;

‎src/librustc_llvm/archive_ro.rs ‎src/librustc_codegen_llvm/llvm/archive_ro.rs

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

1111
//! A wrapper around LLVM's archive (.a) code
1212
13-
use ArchiveRef;
13+
use super::ArchiveRef;
1414

1515
use std::ffi::CString;
1616
use std::marker;
@@ -26,11 +26,11 @@ unsafe impl Send for ArchiveRO {}
2626

2727
pub struct Iter<'a> {
2828
archive: &'a ArchiveRO,
29-
ptr: ::ArchiveIteratorRef,
29+
ptr: super::ArchiveIteratorRef,
3030
}
3131

3232
pub struct Child<'a> {
33-
ptr: ::ArchiveChildRef,
33+
ptr: super::ArchiveChildRef,
3434
_data: marker::PhantomData<&'a ArchiveRO>,
3535
}
3636

@@ -44,9 +44,9 @@ impl ArchiveRO {
4444
pub fn open(dst: &Path) -> Result<ArchiveRO, String> {
4545
return unsafe {
4646
let s = path2cstr(dst);
47-
let ar = ::LLVMRustOpenArchive(s.as_ptr());
47+
let ar = super::LLVMRustOpenArchive(s.as_ptr());
4848
if ar.is_null() {
49-
Err(::last_error().unwrap_or("failed to open archive".to_string()))
49+
Err(super::last_error().unwrap_or("failed to open archive".to_string()))
5050
} else {
5151
Ok(ArchiveRO { ptr: ar })
5252
}
@@ -72,7 +72,7 @@ impl ArchiveRO {
7272
pub fn iter(&self) -> Iter {
7373
unsafe {
7474
Iter {
75-
ptr: ::LLVMRustArchiveIteratorNew(self.ptr),
75+
ptr: super::LLVMRustArchiveIteratorNew(self.ptr),
7676
archive: self,
7777
}
7878
}
@@ -82,7 +82,7 @@ impl ArchiveRO {
8282
impl Drop for ArchiveRO {
8383
fn drop(&mut self) {
8484
unsafe {
85-
::LLVMRustDestroyArchive(self.ptr);
85+
super::LLVMRustDestroyArchive(self.ptr);
8686
}
8787
}
8888
}
@@ -91,9 +91,9 @@ impl<'a> Iterator for Iter<'a> {
9191
type Item = Result<Child<'a>, String>;
9292

9393
fn next(&mut self) -> Option<Result<Child<'a>, String>> {
94-
let ptr = unsafe { ::LLVMRustArchiveIteratorNext(self.ptr) };
94+
let ptr = unsafe { super::LLVMRustArchiveIteratorNext(self.ptr) };
9595
if ptr.is_null() {
96-
::last_error().map(Err)
96+
super::last_error().map(Err)
9797
} else {
9898
Some(Ok(Child {
9999
ptr,
@@ -106,7 +106,7 @@ impl<'a> Iterator for Iter<'a> {
106106
impl<'a> Drop for Iter<'a> {
107107
fn drop(&mut self) {
108108
unsafe {
109-
::LLVMRustArchiveIteratorFree(self.ptr);
109+
super::LLVMRustArchiveIteratorFree(self.ptr);
110110
}
111111
}
112112
}
@@ -115,7 +115,7 @@ impl<'a> Child<'a> {
115115
pub fn name(&self) -> Option<&'a str> {
116116
unsafe {
117117
let mut name_len = 0;
118-
let name_ptr = ::LLVMRustArchiveChildName(self.ptr, &mut name_len);
118+
let name_ptr = super::LLVMRustArchiveChildName(self.ptr, &mut name_len);
119119
if name_ptr.is_null() {
120120
None
121121
} else {
@@ -128,23 +128,23 @@ impl<'a> Child<'a> {
128128
pub fn data(&self) -> &'a [u8] {
129129
unsafe {
130130
let mut data_len = 0;
131-
let data_ptr = ::LLVMRustArchiveChildData(self.ptr, &mut data_len);
131+
let data_ptr = super::LLVMRustArchiveChildData(self.ptr, &mut data_len);
132132
if data_ptr.is_null() {
133133
panic!("failed to read data from archive child");
134134
}
135135
slice::from_raw_parts(data_ptr as *const u8, data_len as usize)
136136
}
137137
}
138138

139-
pub fn raw(&self) -> ::ArchiveChildRef {
139+
pub fn raw(&self) -> super::ArchiveChildRef {
140140
self.ptr
141141
}
142142
}
143143

144144
impl<'a> Drop for Child<'a> {
145145
fn drop(&mut self) {
146146
unsafe {
147-
::LLVMRustArchiveChildFree(self.ptr);
147+
super::LLVMRustArchiveChildFree(self.ptr);
148148
}
149149
}
150150
}

‎src/librustc_llvm/diagnostic.rs ‎src/librustc_codegen_llvm/llvm/diagnostic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub use self::Diagnostic::*;
1616
use libc::c_uint;
1717
use std::ptr;
1818

19-
use {DiagnosticInfoRef, TwineRef, ValueRef};
19+
use super::{DiagnosticInfoRef, TwineRef, ValueRef};
2020

2121
#[derive(Copy, Clone)]
2222
pub enum OptimizationDiagnosticKind {

‎src/librustc_llvm/ffi.rs ‎src/librustc_codegen_llvm/llvm/ffi.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414
// This method was changed in this LLVM patch:
1515
// https://reviews.llvm.org/D26769
1616

17-
use debuginfo::{DIBuilderRef, DIDescriptor, DIFile, DILexicalBlock, DISubprogram, DIType,
18-
DIBasicType, DIDerivedType, DICompositeType, DIScope, DIVariable,
19-
DIGlobalVariable, DIArray, DISubrange, DITemplateTypeParameter, DIEnumerator,
20-
DINameSpace, DIFlags};
17+
use super::debuginfo::{
18+
DIBuilderRef, DIDescriptor, DIFile, DILexicalBlock, DISubprogram, DIType,
19+
DIBasicType, DIDerivedType, DICompositeType, DIScope, DIVariable,
20+
DIGlobalVariable, DIArray, DISubrange, DITemplateTypeParameter, DIEnumerator,
21+
DINameSpace, DIFlags,
22+
};
2123

2224
use libc::{c_uint, c_int, size_t, c_char};
2325
use libc::{c_longlong, c_ulonglong, c_void};
2426

25-
use RustStringRef;
27+
use super::RustStringRef;
2628

2729
pub type Opcode = u32;
2830
pub type Bool = c_uint;
@@ -512,13 +514,6 @@ pub mod debuginfo {
512514

513515
pub enum ModuleBuffer {}
514516

515-
// This annotation is primarily needed for MSVC where attributes like
516-
// dllimport/dllexport are applied and need to be correct for everything to
517-
// link successfully. The #[link] annotation here says "these symbols are
518-
// included statically" which means that they're all exported with dllexport
519-
// and from the rustc_llvm dynamic library. Otherwise the rustc_codegen_llvm dynamic
520-
// library would not be able to access these symbols.
521-
#[link(name = "rustllvm", kind = "static")]
522517
extern "C" {
523518
// Create and destroy contexts.
524519
pub fn LLVMRustContextCreate(shouldDiscardNames: bool) -> ContextRef;

‎src/librustc_codegen_llvm/llvm/mod.rs

+315
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
// Copyright 2012-2015 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+
#![allow(non_upper_case_globals)]
12+
#![allow(non_camel_case_types)]
13+
#![allow(non_snake_case)]
14+
#![allow(dead_code)]
15+
#![deny(bare_trait_objects)]
16+
17+
pub use self::IntPredicate::*;
18+
pub use self::RealPredicate::*;
19+
pub use self::TypeKind::*;
20+
pub use self::AtomicRmwBinOp::*;
21+
pub use self::MetadataType::*;
22+
pub use self::CodeGenOptSize::*;
23+
pub use self::CallConv::*;
24+
pub use self::Linkage::*;
25+
26+
use std::str::FromStr;
27+
use std::slice;
28+
use std::ffi::{CString, CStr};
29+
use std::cell::RefCell;
30+
use libc::{self, c_uint, c_char, size_t};
31+
32+
pub mod archive_ro;
33+
pub mod diagnostic;
34+
mod ffi;
35+
36+
pub use self::ffi::*;
37+
38+
impl LLVMRustResult {
39+
pub fn into_result(self) -> Result<(), ()> {
40+
match self {
41+
LLVMRustResult::Success => Ok(()),
42+
LLVMRustResult::Failure => Err(()),
43+
}
44+
}
45+
}
46+
47+
pub fn AddFunctionAttrStringValue(llfn: ValueRef,
48+
idx: AttributePlace,
49+
attr: &CStr,
50+
value: &CStr) {
51+
unsafe {
52+
LLVMRustAddFunctionAttrStringValue(llfn,
53+
idx.as_uint(),
54+
attr.as_ptr(),
55+
value.as_ptr())
56+
}
57+
}
58+
59+
#[derive(Copy, Clone)]
60+
pub enum AttributePlace {
61+
ReturnValue,
62+
Argument(u32),
63+
Function,
64+
}
65+
66+
impl AttributePlace {
67+
pub fn as_uint(self) -> c_uint {
68+
match self {
69+
AttributePlace::ReturnValue => 0,
70+
AttributePlace::Argument(i) => 1 + i,
71+
AttributePlace::Function => !0,
72+
}
73+
}
74+
}
75+
76+
#[derive(Copy, Clone, PartialEq)]
77+
#[repr(C)]
78+
pub enum CodeGenOptSize {
79+
CodeGenOptSizeNone = 0,
80+
CodeGenOptSizeDefault = 1,
81+
CodeGenOptSizeAggressive = 2,
82+
}
83+
84+
impl FromStr for ArchiveKind {
85+
type Err = ();
86+
87+
fn from_str(s: &str) -> Result<Self, Self::Err> {
88+
match s {
89+
"gnu" => Ok(ArchiveKind::K_GNU),
90+
"bsd" => Ok(ArchiveKind::K_BSD),
91+
"coff" => Ok(ArchiveKind::K_COFF),
92+
_ => Err(()),
93+
}
94+
}
95+
}
96+
97+
#[allow(missing_copy_implementations)]
98+
pub enum RustString_opaque {}
99+
type RustStringRef = *mut RustString_opaque;
100+
type RustStringRepr = *mut RefCell<Vec<u8>>;
101+
102+
/// Appending to a Rust string -- used by RawRustStringOstream.
103+
#[no_mangle]
104+
pub unsafe extern "C" fn LLVMRustStringWriteImpl(sr: RustStringRef,
105+
ptr: *const c_char,
106+
size: size_t) {
107+
let slice = slice::from_raw_parts(ptr as *const u8, size as usize);
108+
109+
let sr = sr as RustStringRepr;
110+
(*sr).borrow_mut().extend_from_slice(slice);
111+
}
112+
113+
pub fn SetInstructionCallConv(instr: ValueRef, cc: CallConv) {
114+
unsafe {
115+
LLVMSetInstructionCallConv(instr, cc as c_uint);
116+
}
117+
}
118+
pub fn SetFunctionCallConv(fn_: ValueRef, cc: CallConv) {
119+
unsafe {
120+
LLVMSetFunctionCallConv(fn_, cc as c_uint);
121+
}
122+
}
123+
124+
// Externally visible symbols that might appear in multiple codegen units need to appear in
125+
// their own comdat section so that the duplicates can be discarded at link time. This can for
126+
// example happen for generics when using multiple codegen units. This function simply uses the
127+
// value's name as the comdat value to make sure that it is in a 1-to-1 relationship to the
128+
// function.
129+
// For more details on COMDAT sections see e.g. http://www.airs.com/blog/archives/52
130+
pub fn SetUniqueComdat(llmod: ModuleRef, val: ValueRef) {
131+
unsafe {
132+
LLVMRustSetComdat(llmod, val, LLVMGetValueName(val));
133+
}
134+
}
135+
136+
pub fn UnsetComdat(val: ValueRef) {
137+
unsafe {
138+
LLVMRustUnsetComdat(val);
139+
}
140+
}
141+
142+
pub fn SetUnnamedAddr(global: ValueRef, unnamed: bool) {
143+
unsafe {
144+
LLVMSetUnnamedAddr(global, unnamed as Bool);
145+
}
146+
}
147+
148+
pub fn set_thread_local(global: ValueRef, is_thread_local: bool) {
149+
unsafe {
150+
LLVMSetThreadLocal(global, is_thread_local as Bool);
151+
}
152+
}
153+
pub fn set_thread_local_mode(global: ValueRef, mode: ThreadLocalMode) {
154+
unsafe {
155+
LLVMSetThreadLocalMode(global, mode);
156+
}
157+
}
158+
159+
impl Attribute {
160+
pub fn apply_llfn(&self, idx: AttributePlace, llfn: ValueRef) {
161+
unsafe { LLVMRustAddFunctionAttribute(llfn, idx.as_uint(), *self) }
162+
}
163+
164+
pub fn apply_callsite(&self, idx: AttributePlace, callsite: ValueRef) {
165+
unsafe { LLVMRustAddCallSiteAttribute(callsite, idx.as_uint(), *self) }
166+
}
167+
168+
pub fn unapply_llfn(&self, idx: AttributePlace, llfn: ValueRef) {
169+
unsafe { LLVMRustRemoveFunctionAttributes(llfn, idx.as_uint(), *self) }
170+
}
171+
172+
pub fn toggle_llfn(&self, idx: AttributePlace, llfn: ValueRef, set: bool) {
173+
if set {
174+
self.apply_llfn(idx, llfn);
175+
} else {
176+
self.unapply_llfn(idx, llfn);
177+
}
178+
}
179+
}
180+
181+
// Memory-managed interface to target data.
182+
183+
struct TargetData {
184+
lltd: TargetDataRef,
185+
}
186+
187+
impl Drop for TargetData {
188+
fn drop(&mut self) {
189+
unsafe {
190+
LLVMDisposeTargetData(self.lltd);
191+
}
192+
}
193+
}
194+
195+
fn mk_target_data(string_rep: &str) -> TargetData {
196+
let string_rep = CString::new(string_rep).unwrap();
197+
TargetData { lltd: unsafe { LLVMCreateTargetData(string_rep.as_ptr()) } }
198+
}
199+
200+
// Memory-managed interface to object files.
201+
202+
pub struct ObjectFile {
203+
pub llof: ObjectFileRef,
204+
}
205+
206+
unsafe impl Send for ObjectFile {}
207+
208+
impl ObjectFile {
209+
// This will take ownership of llmb
210+
pub fn new(llmb: MemoryBufferRef) -> Option<ObjectFile> {
211+
unsafe {
212+
let llof = LLVMCreateObjectFile(llmb);
213+
if llof as isize == 0 {
214+
// LLVMCreateObjectFile took ownership of llmb
215+
return None;
216+
}
217+
218+
Some(ObjectFile { llof: llof })
219+
}
220+
}
221+
}
222+
223+
impl Drop for ObjectFile {
224+
fn drop(&mut self) {
225+
unsafe {
226+
LLVMDisposeObjectFile(self.llof);
227+
}
228+
}
229+
}
230+
231+
// Memory-managed interface to section iterators.
232+
233+
pub struct SectionIter {
234+
pub llsi: SectionIteratorRef,
235+
}
236+
237+
impl Drop for SectionIter {
238+
fn drop(&mut self) {
239+
unsafe {
240+
LLVMDisposeSectionIterator(self.llsi);
241+
}
242+
}
243+
}
244+
245+
pub fn mk_section_iter(llof: ObjectFileRef) -> SectionIter {
246+
unsafe { SectionIter { llsi: LLVMGetSections(llof) } }
247+
}
248+
249+
/// Safe wrapper around `LLVMGetParam`, because segfaults are no fun.
250+
pub fn get_param(llfn: ValueRef, index: c_uint) -> ValueRef {
251+
unsafe {
252+
assert!(index < LLVMCountParams(llfn),
253+
"out of bounds argument access: {} out of {} arguments", index, LLVMCountParams(llfn));
254+
LLVMGetParam(llfn, index)
255+
}
256+
}
257+
258+
fn get_params(llfn: ValueRef) -> Vec<ValueRef> {
259+
unsafe {
260+
let num_params = LLVMCountParams(llfn);
261+
(0..num_params).map(|idx| LLVMGetParam(llfn, idx)).collect()
262+
}
263+
}
264+
265+
pub fn build_string<F>(f: F) -> Option<String>
266+
where F: FnOnce(RustStringRef)
267+
{
268+
let mut buf = RefCell::new(Vec::new());
269+
f(&mut buf as RustStringRepr as RustStringRef);
270+
String::from_utf8(buf.into_inner()).ok()
271+
}
272+
273+
pub unsafe fn twine_to_string(tr: TwineRef) -> String {
274+
build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
275+
}
276+
277+
pub fn last_error() -> Option<String> {
278+
unsafe {
279+
let cstr = LLVMRustGetLastError();
280+
if cstr.is_null() {
281+
None
282+
} else {
283+
let err = CStr::from_ptr(cstr).to_bytes();
284+
let err = String::from_utf8_lossy(err).to_string();
285+
libc::free(cstr as *mut _);
286+
Some(err)
287+
}
288+
}
289+
}
290+
291+
pub struct OperandBundleDef {
292+
inner: OperandBundleDefRef,
293+
}
294+
295+
impl OperandBundleDef {
296+
pub fn new(name: &str, vals: &[ValueRef]) -> OperandBundleDef {
297+
let name = CString::new(name).unwrap();
298+
let def = unsafe {
299+
LLVMRustBuildOperandBundleDef(name.as_ptr(), vals.as_ptr(), vals.len() as c_uint)
300+
};
301+
OperandBundleDef { inner: def }
302+
}
303+
304+
pub fn raw(&self) -> OperandBundleDefRef {
305+
self.inner
306+
}
307+
}
308+
309+
impl Drop for OperandBundleDef {
310+
fn drop(&mut self) {
311+
unsafe {
312+
LLVMRustFreeOperandBundleDef(self.inner);
313+
}
314+
}
315+
}

‎src/librustc_codegen_llvm/llvm_util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ unsafe fn configure_llvm(sess: &Session) {
7373

7474
llvm::LLVMInitializePasses();
7575

76-
llvm::initialize_available_targets();
76+
::rustc_llvm::initialize_available_targets();
7777

7878
llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int,
7979
llvm_args.as_ptr());

‎src/librustc_llvm/Cargo.toml

-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ path = "lib.rs"
1212
static-libstdcpp = []
1313
emscripten = []
1414

15-
[dependencies]
16-
bitflags = "1.0"
17-
libc = "0.2"
18-
rustc_cratesio_shim = { path = "../librustc_cratesio_shim" }
19-
2015
[build-dependencies]
2116
build_helper = { path = "../build_helper" }
2217
cc = "1.0.1"

‎src/librustc_llvm/lib.rs

+3-315
Original file line numberDiff line numberDiff line change
@@ -8,290 +8,18 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![allow(non_upper_case_globals)]
12-
#![allow(non_camel_case_types)]
13-
#![allow(non_snake_case)]
14-
#![allow(dead_code)]
15-
1611
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
1712
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
1813
html_root_url = "https://doc.rust-lang.org/nightly/")]
1914

20-
#![feature(box_syntax)]
21-
#![feature(concat_idents)]
22-
#![feature(libc)]
23-
#![feature(link_args)]
24-
#![feature(static_nobundle)]
25-
2615
// See librustc_cratesio_shim/Cargo.toml for a comment explaining this.
2716
#[allow(unused_extern_crates)]
2817
extern crate rustc_cratesio_shim;
2918

30-
#[macro_use]
31-
extern crate bitflags;
32-
extern crate libc;
33-
34-
pub use self::IntPredicate::*;
35-
pub use self::RealPredicate::*;
36-
pub use self::TypeKind::*;
37-
pub use self::AtomicRmwBinOp::*;
38-
pub use self::MetadataType::*;
39-
pub use self::CodeGenOptSize::*;
40-
pub use self::CallConv::*;
41-
pub use self::Linkage::*;
42-
43-
use std::str::FromStr;
44-
use std::slice;
45-
use std::ffi::{CString, CStr};
46-
use std::cell::RefCell;
47-
use libc::{c_uint, c_char, size_t};
48-
49-
pub mod archive_ro;
50-
pub mod diagnostic;
51-
mod ffi;
52-
53-
pub use ffi::*;
54-
55-
impl LLVMRustResult {
56-
pub fn into_result(self) -> Result<(), ()> {
57-
match self {
58-
LLVMRustResult::Success => Ok(()),
59-
LLVMRustResult::Failure => Err(()),
60-
}
61-
}
62-
}
63-
64-
pub fn AddFunctionAttrStringValue(llfn: ValueRef,
65-
idx: AttributePlace,
66-
attr: &CStr,
67-
value: &CStr) {
68-
unsafe {
69-
LLVMRustAddFunctionAttrStringValue(llfn,
70-
idx.as_uint(),
71-
attr.as_ptr(),
72-
value.as_ptr())
73-
}
74-
}
75-
76-
#[derive(Copy, Clone)]
77-
pub enum AttributePlace {
78-
ReturnValue,
79-
Argument(u32),
80-
Function,
81-
}
82-
83-
impl AttributePlace {
84-
pub fn as_uint(self) -> c_uint {
85-
match self {
86-
AttributePlace::ReturnValue => 0,
87-
AttributePlace::Argument(i) => 1 + i,
88-
AttributePlace::Function => !0,
89-
}
90-
}
91-
}
92-
93-
#[derive(Copy, Clone, PartialEq)]
94-
#[repr(C)]
95-
pub enum CodeGenOptSize {
96-
CodeGenOptSizeNone = 0,
97-
CodeGenOptSizeDefault = 1,
98-
CodeGenOptSizeAggressive = 2,
99-
}
100-
101-
impl FromStr for ArchiveKind {
102-
type Err = ();
103-
104-
fn from_str(s: &str) -> Result<Self, Self::Err> {
105-
match s {
106-
"gnu" => Ok(ArchiveKind::K_GNU),
107-
"bsd" => Ok(ArchiveKind::K_BSD),
108-
"coff" => Ok(ArchiveKind::K_COFF),
109-
_ => Err(()),
110-
}
111-
}
112-
}
113-
114-
#[allow(missing_copy_implementations)]
115-
pub enum RustString_opaque {}
116-
type RustStringRef = *mut RustString_opaque;
117-
type RustStringRepr = *mut RefCell<Vec<u8>>;
118-
119-
/// Appending to a Rust string -- used by RawRustStringOstream.
120-
#[no_mangle]
121-
pub unsafe extern "C" fn LLVMRustStringWriteImpl(sr: RustStringRef,
122-
ptr: *const c_char,
123-
size: size_t) {
124-
let slice = slice::from_raw_parts(ptr as *const u8, size as usize);
125-
126-
let sr = sr as RustStringRepr;
127-
(*sr).borrow_mut().extend_from_slice(slice);
128-
}
129-
130-
pub fn SetInstructionCallConv(instr: ValueRef, cc: CallConv) {
131-
unsafe {
132-
LLVMSetInstructionCallConv(instr, cc as c_uint);
133-
}
134-
}
135-
pub fn SetFunctionCallConv(fn_: ValueRef, cc: CallConv) {
136-
unsafe {
137-
LLVMSetFunctionCallConv(fn_, cc as c_uint);
138-
}
139-
}
140-
141-
// Externally visible symbols that might appear in multiple codegen units need to appear in
142-
// their own comdat section so that the duplicates can be discarded at link time. This can for
143-
// example happen for generics when using multiple codegen units. This function simply uses the
144-
// value's name as the comdat value to make sure that it is in a 1-to-1 relationship to the
145-
// function.
146-
// For more details on COMDAT sections see e.g. http://www.airs.com/blog/archives/52
147-
pub fn SetUniqueComdat(llmod: ModuleRef, val: ValueRef) {
148-
unsafe {
149-
LLVMRustSetComdat(llmod, val, LLVMGetValueName(val));
150-
}
151-
}
152-
153-
pub fn UnsetComdat(val: ValueRef) {
154-
unsafe {
155-
LLVMRustUnsetComdat(val);
156-
}
157-
}
158-
159-
pub fn SetUnnamedAddr(global: ValueRef, unnamed: bool) {
160-
unsafe {
161-
LLVMSetUnnamedAddr(global, unnamed as Bool);
162-
}
163-
}
164-
165-
pub fn set_thread_local(global: ValueRef, is_thread_local: bool) {
166-
unsafe {
167-
LLVMSetThreadLocal(global, is_thread_local as Bool);
168-
}
169-
}
170-
pub fn set_thread_local_mode(global: ValueRef, mode: ThreadLocalMode) {
171-
unsafe {
172-
LLVMSetThreadLocalMode(global, mode);
173-
}
174-
}
175-
176-
impl Attribute {
177-
pub fn apply_llfn(&self, idx: AttributePlace, llfn: ValueRef) {
178-
unsafe { LLVMRustAddFunctionAttribute(llfn, idx.as_uint(), *self) }
179-
}
180-
181-
pub fn apply_callsite(&self, idx: AttributePlace, callsite: ValueRef) {
182-
unsafe { LLVMRustAddCallSiteAttribute(callsite, idx.as_uint(), *self) }
183-
}
184-
185-
pub fn unapply_llfn(&self, idx: AttributePlace, llfn: ValueRef) {
186-
unsafe { LLVMRustRemoveFunctionAttributes(llfn, idx.as_uint(), *self) }
187-
}
188-
189-
pub fn toggle_llfn(&self, idx: AttributePlace, llfn: ValueRef, set: bool) {
190-
if set {
191-
self.apply_llfn(idx, llfn);
192-
} else {
193-
self.unapply_llfn(idx, llfn);
194-
}
195-
}
196-
}
197-
198-
// Memory-managed interface to target data.
199-
200-
struct TargetData {
201-
lltd: TargetDataRef,
202-
}
203-
204-
impl Drop for TargetData {
205-
fn drop(&mut self) {
206-
unsafe {
207-
LLVMDisposeTargetData(self.lltd);
208-
}
209-
}
210-
}
211-
212-
fn mk_target_data(string_rep: &str) -> TargetData {
213-
let string_rep = CString::new(string_rep).unwrap();
214-
TargetData { lltd: unsafe { LLVMCreateTargetData(string_rep.as_ptr()) } }
215-
}
216-
217-
// Memory-managed interface to object files.
218-
219-
pub struct ObjectFile {
220-
pub llof: ObjectFileRef,
221-
}
222-
223-
unsafe impl Send for ObjectFile {}
224-
225-
impl ObjectFile {
226-
// This will take ownership of llmb
227-
pub fn new(llmb: MemoryBufferRef) -> Option<ObjectFile> {
228-
unsafe {
229-
let llof = LLVMCreateObjectFile(llmb);
230-
if llof as isize == 0 {
231-
// LLVMCreateObjectFile took ownership of llmb
232-
return None;
233-
}
234-
235-
Some(ObjectFile { llof: llof })
236-
}
237-
}
238-
}
239-
240-
impl Drop for ObjectFile {
241-
fn drop(&mut self) {
242-
unsafe {
243-
LLVMDisposeObjectFile(self.llof);
244-
}
245-
}
246-
}
247-
248-
// Memory-managed interface to section iterators.
249-
250-
pub struct SectionIter {
251-
pub llsi: SectionIteratorRef,
252-
}
253-
254-
impl Drop for SectionIter {
255-
fn drop(&mut self) {
256-
unsafe {
257-
LLVMDisposeSectionIterator(self.llsi);
258-
}
259-
}
260-
}
261-
262-
pub fn mk_section_iter(llof: ObjectFileRef) -> SectionIter {
263-
unsafe { SectionIter { llsi: LLVMGetSections(llof) } }
264-
}
265-
266-
/// Safe wrapper around `LLVMGetParam`, because segfaults are no fun.
267-
pub fn get_param(llfn: ValueRef, index: c_uint) -> ValueRef {
268-
unsafe {
269-
assert!(index < LLVMCountParams(llfn),
270-
"out of bounds argument access: {} out of {} arguments", index, LLVMCountParams(llfn));
271-
LLVMGetParam(llfn, index)
272-
}
273-
}
274-
275-
fn get_params(llfn: ValueRef) -> Vec<ValueRef> {
276-
unsafe {
277-
let num_params = LLVMCountParams(llfn);
278-
279-
(0..num_params).map(|idx| LLVMGetParam(llfn, idx)).collect()
280-
}
281-
}
282-
283-
pub fn build_string<F>(f: F) -> Option<String>
284-
where F: FnOnce(RustStringRef)
285-
{
286-
let mut buf = RefCell::new(Vec::new());
287-
f(&mut buf as RustStringRepr as RustStringRef);
288-
String::from_utf8(buf.into_inner()).ok()
289-
}
290-
291-
pub unsafe fn twine_to_string(tr: TwineRef) -> String {
292-
build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
293-
}
19+
// NOTE: This crate only exists to allow linking on mingw targets.
29420

21+
/// Initialize targets enabled by the build script via `cfg(llvm_component = "...")`.
22+
/// NB: this function can't be moved to `rustc_codegen_llvm` because of the `cfg`s.
29523
pub fn initialize_available_targets() {
29624
macro_rules! init_target(
29725
($cfg:meta, $($method:ident),*) => { {
@@ -383,43 +111,3 @@ pub fn initialize_available_targets() {
383111
LLVMInitializeWebAssemblyTargetMC,
384112
LLVMInitializeWebAssemblyAsmPrinter);
385113
}
386-
387-
pub fn last_error() -> Option<String> {
388-
unsafe {
389-
let cstr = LLVMRustGetLastError();
390-
if cstr.is_null() {
391-
None
392-
} else {
393-
let err = CStr::from_ptr(cstr).to_bytes();
394-
let err = String::from_utf8_lossy(err).to_string();
395-
libc::free(cstr as *mut _);
396-
Some(err)
397-
}
398-
}
399-
}
400-
401-
pub struct OperandBundleDef {
402-
inner: OperandBundleDefRef,
403-
}
404-
405-
impl OperandBundleDef {
406-
pub fn new(name: &str, vals: &[ValueRef]) -> OperandBundleDef {
407-
let name = CString::new(name).unwrap();
408-
let def = unsafe {
409-
LLVMRustBuildOperandBundleDef(name.as_ptr(), vals.as_ptr(), vals.len() as c_uint)
410-
};
411-
OperandBundleDef { inner: def }
412-
}
413-
414-
pub fn raw(&self) -> OperandBundleDefRef {
415-
self.inner
416-
}
417-
}
418-
419-
impl Drop for OperandBundleDef {
420-
fn drop(&mut self) {
421-
unsafe {
422-
LLVMRustFreeOperandBundleDef(self.inner);
423-
}
424-
}
425-
}

‎src/librustc_target/lib.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
//! is really just odds-and-ends relating to code gen and linking.
1616
//! This crate mostly exists to make rustc smaller, so we might put
1717
//! more 'stuff' here in the future. It does not have a dependency on
18-
//! rustc_llvm.
19-
//!
20-
//! FIXME: Split this into two crates: one that has deps on syntax, and
21-
//! one that doesn't; the one that doesn't might get decent parallel
22-
//! build speedups.
18+
//! LLVM.
2319
2420
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
2521
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",

‎src/rustllvm/ArchiveWrapper.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ LLVMRustArchiveChildName(LLVMRustArchiveChildConstRef Child, size_t *Size) {
148148
#if LLVM_VERSION_GE(4, 0)
149149
Expected<StringRef> NameOrErr = Child->getName();
150150
if (!NameOrErr) {
151-
// rustc_llvm currently doesn't use this error string, but it might be
151+
// rustc_codegen_llvm currently doesn't use this error string, but it might be
152152
// useful in the future, and in the mean time this tells LLVM that the
153153
// error was not ignored and that it shouldn't abort the process.
154154
LLVMRustSetLastError(toString(NameOrErr.takeError()).c_str());

0 commit comments

Comments
 (0)
Please sign in to comment.