mv compiler to compiler/
This commit is contained in:
105
compiler/rustc_codegen_llvm/src/llvm/archive_ro.rs
Normal file
105
compiler/rustc_codegen_llvm/src/llvm/archive_ro.rs
Normal file
@@ -0,0 +1,105 @@
|
||||
//! A wrapper around LLVM's archive (.a) code
|
||||
|
||||
use rustc_fs_util::path_to_c_string;
|
||||
use std::path::Path;
|
||||
use std::slice;
|
||||
use std::str;
|
||||
|
||||
pub struct ArchiveRO {
|
||||
pub raw: &'static mut super::Archive,
|
||||
}
|
||||
|
||||
unsafe impl Send for ArchiveRO {}
|
||||
|
||||
pub struct Iter<'a> {
|
||||
raw: &'a mut super::ArchiveIterator<'a>,
|
||||
}
|
||||
|
||||
pub struct Child<'a> {
|
||||
pub raw: &'a mut super::ArchiveChild<'a>,
|
||||
}
|
||||
|
||||
impl ArchiveRO {
|
||||
/// Opens a static archive for read-only purposes. This is more optimized
|
||||
/// than the `open` method because it uses LLVM's internal `Archive` class
|
||||
/// rather than shelling out to `ar` for everything.
|
||||
///
|
||||
/// If this archive is used with a mutable method, then an error will be
|
||||
/// raised.
|
||||
pub fn open(dst: &Path) -> Result<ArchiveRO, String> {
|
||||
unsafe {
|
||||
let s = path_to_c_string(dst);
|
||||
let ar = super::LLVMRustOpenArchive(s.as_ptr()).ok_or_else(|| {
|
||||
super::last_error().unwrap_or_else(|| "failed to open archive".to_owned())
|
||||
})?;
|
||||
Ok(ArchiveRO { raw: ar })
|
||||
}
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> Iter<'_> {
|
||||
unsafe { Iter { raw: super::LLVMRustArchiveIteratorNew(self.raw) } }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ArchiveRO {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
super::LLVMRustDestroyArchive(&mut *(self.raw as *mut _));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for Iter<'a> {
|
||||
type Item = Result<Child<'a>, String>;
|
||||
|
||||
fn next(&mut self) -> Option<Result<Child<'a>, String>> {
|
||||
unsafe {
|
||||
match super::LLVMRustArchiveIteratorNext(self.raw) {
|
||||
Some(raw) => Some(Ok(Child { raw })),
|
||||
None => super::last_error().map(Err),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for Iter<'a> {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
super::LLVMRustArchiveIteratorFree(&mut *(self.raw as *mut _));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Child<'a> {
|
||||
pub fn name(&self) -> Option<&'a str> {
|
||||
unsafe {
|
||||
let mut name_len = 0;
|
||||
let name_ptr = super::LLVMRustArchiveChildName(self.raw, &mut name_len);
|
||||
if name_ptr.is_null() {
|
||||
None
|
||||
} else {
|
||||
let name = slice::from_raw_parts(name_ptr as *const u8, name_len as usize);
|
||||
str::from_utf8(name).ok().map(|s| s.trim())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn data(&self) -> &'a [u8] {
|
||||
unsafe {
|
||||
let mut data_len = 0;
|
||||
let data_ptr = super::LLVMRustArchiveChildData(self.raw, &mut data_len);
|
||||
if data_ptr.is_null() {
|
||||
panic!("failed to read data from archive child");
|
||||
}
|
||||
slice::from_raw_parts(data_ptr as *const u8, data_len as usize)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for Child<'a> {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
super::LLVMRustArchiveChildFree(&mut *(self.raw as *mut _));
|
||||
}
|
||||
}
|
||||
}
|
||||
166
compiler/rustc_codegen_llvm/src/llvm/diagnostic.rs
Normal file
166
compiler/rustc_codegen_llvm/src/llvm/diagnostic.rs
Normal file
@@ -0,0 +1,166 @@
|
||||
//! LLVM diagnostic reports.
|
||||
|
||||
pub use self::Diagnostic::*;
|
||||
pub use self::OptimizationDiagnosticKind::*;
|
||||
|
||||
use crate::value::Value;
|
||||
use libc::c_uint;
|
||||
|
||||
use super::{DiagnosticInfo, Twine};
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum OptimizationDiagnosticKind {
|
||||
OptimizationRemark,
|
||||
OptimizationMissed,
|
||||
OptimizationAnalysis,
|
||||
OptimizationAnalysisFPCommute,
|
||||
OptimizationAnalysisAliasing,
|
||||
OptimizationFailure,
|
||||
OptimizationRemarkOther,
|
||||
}
|
||||
|
||||
impl OptimizationDiagnosticKind {
|
||||
pub fn describe(self) -> &'static str {
|
||||
match self {
|
||||
OptimizationRemark | OptimizationRemarkOther => "remark",
|
||||
OptimizationMissed => "missed",
|
||||
OptimizationAnalysis => "analysis",
|
||||
OptimizationAnalysisFPCommute => "floating-point",
|
||||
OptimizationAnalysisAliasing => "aliasing",
|
||||
OptimizationFailure => "failure",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct OptimizationDiagnostic<'ll> {
|
||||
pub kind: OptimizationDiagnosticKind,
|
||||
pub pass_name: String,
|
||||
pub function: &'ll Value,
|
||||
pub line: c_uint,
|
||||
pub column: c_uint,
|
||||
pub filename: String,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl OptimizationDiagnostic<'ll> {
|
||||
unsafe fn unpack(kind: OptimizationDiagnosticKind, di: &'ll DiagnosticInfo) -> Self {
|
||||
let mut function = None;
|
||||
let mut line = 0;
|
||||
let mut column = 0;
|
||||
|
||||
let mut message = None;
|
||||
let mut filename = None;
|
||||
let pass_name = super::build_string(|pass_name| {
|
||||
message = super::build_string(|message| {
|
||||
filename = super::build_string(|filename| {
|
||||
super::LLVMRustUnpackOptimizationDiagnostic(
|
||||
di,
|
||||
pass_name,
|
||||
&mut function,
|
||||
&mut line,
|
||||
&mut column,
|
||||
filename,
|
||||
message,
|
||||
)
|
||||
})
|
||||
.ok()
|
||||
})
|
||||
.ok()
|
||||
})
|
||||
.ok();
|
||||
|
||||
let mut filename = filename.unwrap_or_default();
|
||||
if filename.is_empty() {
|
||||
filename.push_str("<unknown file>");
|
||||
}
|
||||
|
||||
OptimizationDiagnostic {
|
||||
kind,
|
||||
pass_name: pass_name.expect("got a non-UTF8 pass name from LLVM"),
|
||||
function: function.unwrap(),
|
||||
line,
|
||||
column,
|
||||
filename,
|
||||
message: message.expect("got a non-UTF8 OptimizationDiagnostic message from LLVM"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct InlineAsmDiagnostic<'ll> {
|
||||
pub level: super::DiagnosticLevel,
|
||||
pub cookie: c_uint,
|
||||
pub message: &'ll Twine,
|
||||
pub instruction: Option<&'ll Value>,
|
||||
}
|
||||
|
||||
impl InlineAsmDiagnostic<'ll> {
|
||||
unsafe fn unpack(di: &'ll DiagnosticInfo) -> Self {
|
||||
let mut cookie = 0;
|
||||
let mut message = None;
|
||||
let mut instruction = None;
|
||||
let mut level = super::DiagnosticLevel::Error;
|
||||
|
||||
super::LLVMRustUnpackInlineAsmDiagnostic(
|
||||
di,
|
||||
&mut level,
|
||||
&mut cookie,
|
||||
&mut message,
|
||||
&mut instruction,
|
||||
);
|
||||
|
||||
InlineAsmDiagnostic { level, cookie, message: message.unwrap(), instruction }
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Diagnostic<'ll> {
|
||||
Optimization(OptimizationDiagnostic<'ll>),
|
||||
InlineAsm(InlineAsmDiagnostic<'ll>),
|
||||
PGO(&'ll DiagnosticInfo),
|
||||
Linker(&'ll DiagnosticInfo),
|
||||
|
||||
/// LLVM has other types that we do not wrap here.
|
||||
UnknownDiagnostic(&'ll DiagnosticInfo),
|
||||
}
|
||||
|
||||
impl Diagnostic<'ll> {
|
||||
pub unsafe fn unpack(di: &'ll DiagnosticInfo) -> Self {
|
||||
use super::DiagnosticKind as Dk;
|
||||
let kind = super::LLVMRustGetDiagInfoKind(di);
|
||||
|
||||
match kind {
|
||||
Dk::InlineAsm => InlineAsm(InlineAsmDiagnostic::unpack(di)),
|
||||
|
||||
Dk::OptimizationRemark => {
|
||||
Optimization(OptimizationDiagnostic::unpack(OptimizationRemark, di))
|
||||
}
|
||||
Dk::OptimizationRemarkOther => {
|
||||
Optimization(OptimizationDiagnostic::unpack(OptimizationRemarkOther, di))
|
||||
}
|
||||
Dk::OptimizationRemarkMissed => {
|
||||
Optimization(OptimizationDiagnostic::unpack(OptimizationMissed, di))
|
||||
}
|
||||
|
||||
Dk::OptimizationRemarkAnalysis => {
|
||||
Optimization(OptimizationDiagnostic::unpack(OptimizationAnalysis, di))
|
||||
}
|
||||
|
||||
Dk::OptimizationRemarkAnalysisFPCommute => {
|
||||
Optimization(OptimizationDiagnostic::unpack(OptimizationAnalysisFPCommute, di))
|
||||
}
|
||||
|
||||
Dk::OptimizationRemarkAnalysisAliasing => {
|
||||
Optimization(OptimizationDiagnostic::unpack(OptimizationAnalysisAliasing, di))
|
||||
}
|
||||
|
||||
Dk::OptimizationFailure => {
|
||||
Optimization(OptimizationDiagnostic::unpack(OptimizationFailure, di))
|
||||
}
|
||||
|
||||
Dk::PGOProfile => PGO(di),
|
||||
Dk::Linker => Linker(di),
|
||||
|
||||
_ => UnknownDiagnostic(di),
|
||||
}
|
||||
}
|
||||
}
|
||||
2365
compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Normal file
2365
compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Normal file
File diff suppressed because it is too large
Load Diff
310
compiler/rustc_codegen_llvm/src/llvm/mod.rs
Normal file
310
compiler/rustc_codegen_llvm/src/llvm/mod.rs
Normal file
@@ -0,0 +1,310 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
pub use self::AtomicRmwBinOp::*;
|
||||
pub use self::CallConv::*;
|
||||
pub use self::CodeGenOptSize::*;
|
||||
pub use self::IntPredicate::*;
|
||||
pub use self::Linkage::*;
|
||||
pub use self::MetadataType::*;
|
||||
pub use self::RealPredicate::*;
|
||||
|
||||
use libc::c_uint;
|
||||
use rustc_data_structures::small_c_str::SmallCStr;
|
||||
use rustc_llvm::RustString;
|
||||
use std::cell::RefCell;
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::str::FromStr;
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
pub mod archive_ro;
|
||||
pub mod diagnostic;
|
||||
mod ffi;
|
||||
|
||||
pub use self::ffi::*;
|
||||
|
||||
impl LLVMRustResult {
|
||||
pub fn into_result(self) -> Result<(), ()> {
|
||||
match self {
|
||||
LLVMRustResult::Success => Ok(()),
|
||||
LLVMRustResult::Failure => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn AddFunctionAttrStringValue(llfn: &'a Value, idx: AttributePlace, attr: &CStr, value: &CStr) {
|
||||
unsafe {
|
||||
LLVMRustAddFunctionAttrStringValue(llfn, idx.as_uint(), attr.as_ptr(), value.as_ptr())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum AttributePlace {
|
||||
ReturnValue,
|
||||
Argument(u32),
|
||||
Function,
|
||||
}
|
||||
|
||||
impl AttributePlace {
|
||||
pub fn as_uint(self) -> c_uint {
|
||||
match self {
|
||||
AttributePlace::ReturnValue => 0,
|
||||
AttributePlace::Argument(i) => 1 + i,
|
||||
AttributePlace::Function => !0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
#[repr(C)]
|
||||
pub enum CodeGenOptSize {
|
||||
CodeGenOptSizeNone = 0,
|
||||
CodeGenOptSizeDefault = 1,
|
||||
CodeGenOptSizeAggressive = 2,
|
||||
}
|
||||
|
||||
impl FromStr for ArchiveKind {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s {
|
||||
"gnu" => Ok(ArchiveKind::K_GNU),
|
||||
"bsd" => Ok(ArchiveKind::K_BSD),
|
||||
"darwin" => Ok(ArchiveKind::K_DARWIN),
|
||||
"coff" => Ok(ArchiveKind::K_COFF),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn SetInstructionCallConv(instr: &'a Value, cc: CallConv) {
|
||||
unsafe {
|
||||
LLVMSetInstructionCallConv(instr, cc as c_uint);
|
||||
}
|
||||
}
|
||||
pub fn SetFunctionCallConv(fn_: &'a Value, cc: CallConv) {
|
||||
unsafe {
|
||||
LLVMSetFunctionCallConv(fn_, cc as c_uint);
|
||||
}
|
||||
}
|
||||
|
||||
// Externally visible symbols that might appear in multiple codegen units need to appear in
|
||||
// their own comdat section so that the duplicates can be discarded at link time. This can for
|
||||
// example happen for generics when using multiple codegen units. This function simply uses the
|
||||
// value's name as the comdat value to make sure that it is in a 1-to-1 relationship to the
|
||||
// function.
|
||||
// For more details on COMDAT sections see e.g., http://www.airs.com/blog/archives/52
|
||||
pub fn SetUniqueComdat(llmod: &Module, val: &'a Value) {
|
||||
unsafe {
|
||||
let name = get_value_name(val);
|
||||
LLVMRustSetComdat(llmod, val, name.as_ptr().cast(), name.len());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn UnsetComdat(val: &'a Value) {
|
||||
unsafe {
|
||||
LLVMRustUnsetComdat(val);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn SetUnnamedAddress(global: &'a Value, unnamed: UnnamedAddr) {
|
||||
unsafe {
|
||||
LLVMSetUnnamedAddress(global, unnamed);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_thread_local(global: &'a Value, is_thread_local: bool) {
|
||||
unsafe {
|
||||
LLVMSetThreadLocal(global, is_thread_local as Bool);
|
||||
}
|
||||
}
|
||||
pub fn set_thread_local_mode(global: &'a Value, mode: ThreadLocalMode) {
|
||||
unsafe {
|
||||
LLVMSetThreadLocalMode(global, mode);
|
||||
}
|
||||
}
|
||||
|
||||
impl Attribute {
|
||||
pub fn apply_llfn(&self, idx: AttributePlace, llfn: &Value) {
|
||||
unsafe { LLVMRustAddFunctionAttribute(llfn, idx.as_uint(), *self) }
|
||||
}
|
||||
|
||||
pub fn apply_callsite(&self, idx: AttributePlace, callsite: &Value) {
|
||||
unsafe { LLVMRustAddCallSiteAttribute(callsite, idx.as_uint(), *self) }
|
||||
}
|
||||
|
||||
pub fn unapply_llfn(&self, idx: AttributePlace, llfn: &Value) {
|
||||
unsafe { LLVMRustRemoveFunctionAttributes(llfn, idx.as_uint(), *self) }
|
||||
}
|
||||
|
||||
pub fn toggle_llfn(&self, idx: AttributePlace, llfn: &Value, set: bool) {
|
||||
if set {
|
||||
self.apply_llfn(idx, llfn);
|
||||
} else {
|
||||
self.unapply_llfn(idx, llfn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Memory-managed interface to object files.
|
||||
|
||||
pub struct ObjectFile {
|
||||
pub llof: &'static mut ffi::ObjectFile,
|
||||
}
|
||||
|
||||
unsafe impl Send for ObjectFile {}
|
||||
|
||||
impl ObjectFile {
|
||||
// This will take ownership of llmb
|
||||
pub fn new(llmb: &'static mut MemoryBuffer) -> Option<ObjectFile> {
|
||||
unsafe {
|
||||
let llof = LLVMCreateObjectFile(llmb)?;
|
||||
Some(ObjectFile { llof })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ObjectFile {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
LLVMDisposeObjectFile(&mut *(self.llof as *mut _));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Memory-managed interface to section iterators.
|
||||
|
||||
pub struct SectionIter<'a> {
|
||||
pub llsi: &'a mut SectionIterator<'a>,
|
||||
}
|
||||
|
||||
impl Drop for SectionIter<'a> {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
LLVMDisposeSectionIterator(&mut *(self.llsi as *mut _));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mk_section_iter(llof: &ffi::ObjectFile) -> SectionIter<'_> {
|
||||
unsafe { SectionIter { llsi: LLVMGetSections(llof) } }
|
||||
}
|
||||
|
||||
pub fn set_section(llglobal: &Value, section_name: &str) {
|
||||
let section_name_cstr = CString::new(section_name).expect("unexpected CString error");
|
||||
unsafe {
|
||||
LLVMSetSection(llglobal, section_name_cstr.as_ptr());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_global<'a>(llmod: &'a Module, ty: &'a Type, name: &str) -> &'a Value {
|
||||
let name_cstr = CString::new(name).expect("unexpected CString error");
|
||||
unsafe { LLVMAddGlobal(llmod, ty, name_cstr.as_ptr()) }
|
||||
}
|
||||
|
||||
pub fn set_initializer(llglobal: &Value, constant_val: &Value) {
|
||||
unsafe {
|
||||
LLVMSetInitializer(llglobal, constant_val);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_global_constant(llglobal: &Value, is_constant: bool) {
|
||||
unsafe {
|
||||
LLVMSetGlobalConstant(llglobal, if is_constant { ffi::True } else { ffi::False });
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_linkage(llglobal: &Value, linkage: Linkage) {
|
||||
unsafe {
|
||||
LLVMRustSetLinkage(llglobal, linkage);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_alignment(llglobal: &Value, bytes: usize) {
|
||||
unsafe {
|
||||
ffi::LLVMSetAlignment(llglobal, bytes as c_uint);
|
||||
}
|
||||
}
|
||||
|
||||
/// Safe wrapper around `LLVMGetParam`, because segfaults are no fun.
|
||||
pub fn get_param(llfn: &Value, index: c_uint) -> &Value {
|
||||
unsafe {
|
||||
assert!(
|
||||
index < LLVMCountParams(llfn),
|
||||
"out of bounds argument access: {} out of {} arguments",
|
||||
index,
|
||||
LLVMCountParams(llfn)
|
||||
);
|
||||
LLVMGetParam(llfn, index)
|
||||
}
|
||||
}
|
||||
|
||||
/// Safe wrapper for `LLVMGetValueName2` into a byte slice
|
||||
pub fn get_value_name(value: &Value) -> &[u8] {
|
||||
unsafe {
|
||||
let mut len = 0;
|
||||
let data = LLVMGetValueName2(value, &mut len);
|
||||
std::slice::from_raw_parts(data.cast(), len)
|
||||
}
|
||||
}
|
||||
|
||||
/// Safe wrapper for `LLVMSetValueName2` from a byte slice
|
||||
pub fn set_value_name(value: &Value, name: &[u8]) {
|
||||
unsafe {
|
||||
let data = name.as_ptr().cast();
|
||||
LLVMSetValueName2(value, data, name.len());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
|
||||
let sr = RustString { bytes: RefCell::new(Vec::new()) };
|
||||
f(&sr);
|
||||
String::from_utf8(sr.bytes.into_inner())
|
||||
}
|
||||
|
||||
pub fn build_byte_buffer(f: impl FnOnce(&RustString)) -> Vec<u8> {
|
||||
let sr = RustString { bytes: RefCell::new(Vec::new()) };
|
||||
f(&sr);
|
||||
sr.bytes.into_inner()
|
||||
}
|
||||
|
||||
pub fn twine_to_string(tr: &Twine) -> String {
|
||||
unsafe {
|
||||
build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn last_error() -> Option<String> {
|
||||
unsafe {
|
||||
let cstr = LLVMRustGetLastError();
|
||||
if cstr.is_null() {
|
||||
None
|
||||
} else {
|
||||
let err = CStr::from_ptr(cstr).to_bytes();
|
||||
let err = String::from_utf8_lossy(err).to_string();
|
||||
libc::free(cstr as *mut _);
|
||||
Some(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct OperandBundleDef<'a> {
|
||||
pub raw: &'a mut ffi::OperandBundleDef<'a>,
|
||||
}
|
||||
|
||||
impl OperandBundleDef<'a> {
|
||||
pub fn new(name: &str, vals: &[&'a Value]) -> Self {
|
||||
let name = SmallCStr::new(name);
|
||||
let def = unsafe {
|
||||
LLVMRustBuildOperandBundleDef(name.as_ptr(), vals.as_ptr(), vals.len() as c_uint)
|
||||
};
|
||||
OperandBundleDef { raw: def }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for OperandBundleDef<'a> {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
LLVMRustFreeOperandBundleDef(&mut *(self.raw as *mut _));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user