Add 'compiler/rustc_codegen_gcc/' from commit 'afae271d5d3719eeb92c18bc004bb6d1965a5f3f'
git-subtree-dir: compiler/rustc_codegen_gcc git-subtree-mainline:ae90dcf020git-subtree-split:afae271d5d
This commit is contained in:
286
compiler/rustc_codegen_gcc/src/abi.rs
Normal file
286
compiler/rustc_codegen_gcc/src/abi.rs
Normal file
@@ -0,0 +1,286 @@
|
||||
use gccjit::{ToRValue, Type};
|
||||
use rustc_codegen_ssa::traits::{AbiBuilderMethods, BaseTypeMethods};
|
||||
use rustc_middle::bug;
|
||||
use rustc_middle::ty::Ty;
|
||||
use rustc_target::abi::call::{CastTarget, FnAbi, PassMode, Reg, RegKind};
|
||||
|
||||
use crate::builder::Builder;
|
||||
use crate::context::CodegenCx;
|
||||
use crate::intrinsic::ArgAbiExt;
|
||||
use crate::type_of::LayoutGccExt;
|
||||
|
||||
impl<'a, 'gcc, 'tcx> AbiBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
|
||||
fn apply_attrs_callsite(&mut self, _fn_abi: &FnAbi<'tcx, Ty<'tcx>>, _callsite: Self::Value) {
|
||||
// TODO
|
||||
//fn_abi.apply_attrs_callsite(self, callsite)
|
||||
}
|
||||
|
||||
fn get_param(&self, index: usize) -> Self::Value {
|
||||
self.cx.current_func.borrow().expect("current func")
|
||||
.get_param(index as i32)
|
||||
.to_rvalue()
|
||||
}
|
||||
}
|
||||
|
||||
impl GccType for CastTarget {
|
||||
fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc> {
|
||||
let rest_gcc_unit = self.rest.unit.gcc_type(cx);
|
||||
let (rest_count, rem_bytes) =
|
||||
if self.rest.unit.size.bytes() == 0 {
|
||||
(0, 0)
|
||||
}
|
||||
else {
|
||||
(self.rest.total.bytes() / self.rest.unit.size.bytes(), self.rest.total.bytes() % self.rest.unit.size.bytes())
|
||||
};
|
||||
|
||||
if self.prefix.iter().all(|x| x.is_none()) {
|
||||
// Simplify to a single unit when there is no prefix and size <= unit size
|
||||
if self.rest.total <= self.rest.unit.size {
|
||||
return rest_gcc_unit;
|
||||
}
|
||||
|
||||
// Simplify to array when all chunks are the same size and type
|
||||
if rem_bytes == 0 {
|
||||
return cx.type_array(rest_gcc_unit, rest_count);
|
||||
}
|
||||
}
|
||||
|
||||
// Create list of fields in the main structure
|
||||
let mut args: Vec<_> = self
|
||||
.prefix
|
||||
.iter()
|
||||
.flat_map(|option_kind| {
|
||||
option_kind.map(|kind| Reg { kind, size: self.prefix_chunk_size }.gcc_type(cx))
|
||||
})
|
||||
.chain((0..rest_count).map(|_| rest_gcc_unit))
|
||||
.collect();
|
||||
|
||||
// Append final integer
|
||||
if rem_bytes != 0 {
|
||||
// Only integers can be really split further.
|
||||
assert_eq!(self.rest.unit.kind, RegKind::Integer);
|
||||
args.push(cx.type_ix(rem_bytes * 8));
|
||||
}
|
||||
|
||||
cx.type_struct(&args, false)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GccType {
|
||||
fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc>;
|
||||
}
|
||||
|
||||
impl GccType for Reg {
|
||||
fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc> {
|
||||
match self.kind {
|
||||
RegKind::Integer => cx.type_ix(self.size.bits()),
|
||||
RegKind::Float => {
|
||||
match self.size.bits() {
|
||||
32 => cx.type_f32(),
|
||||
64 => cx.type_f64(),
|
||||
_ => bug!("unsupported float: {:?}", self),
|
||||
}
|
||||
},
|
||||
RegKind::Vector => unimplemented!(), //cx.type_vector(cx.type_i8(), self.size.bytes()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FnAbiGccExt<'gcc, 'tcx> {
|
||||
// TODO: return a function pointer type instead?
|
||||
fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> (Type<'gcc>, Vec<Type<'gcc>>, bool);
|
||||
fn ptr_to_gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
|
||||
/*fn llvm_cconv(&self) -> llvm::CallConv;
|
||||
fn apply_attrs_llfn(&self, cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value);
|
||||
fn apply_attrs_callsite(&self, bx: &mut Builder<'a, 'll, 'tcx>, callsite: &'ll Value);*/
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
|
||||
fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> (Type<'gcc>, Vec<Type<'gcc>>, bool) {
|
||||
let args_capacity: usize = self.args.iter().map(|arg|
|
||||
if arg.pad.is_some() {
|
||||
1
|
||||
}
|
||||
else {
|
||||
0
|
||||
} +
|
||||
if let PassMode::Pair(_, _) = arg.mode {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
}
|
||||
).sum();
|
||||
let mut argument_tys = Vec::with_capacity(
|
||||
if let PassMode::Indirect { .. } = self.ret.mode {
|
||||
1
|
||||
}
|
||||
else {
|
||||
0
|
||||
} + args_capacity,
|
||||
);
|
||||
|
||||
let return_ty =
|
||||
match self.ret.mode {
|
||||
PassMode::Ignore => cx.type_void(),
|
||||
PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_gcc_type(cx),
|
||||
PassMode::Cast(cast) => cast.gcc_type(cx),
|
||||
PassMode::Indirect { .. } => {
|
||||
argument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
|
||||
cx.type_void()
|
||||
}
|
||||
};
|
||||
|
||||
for arg in &self.args {
|
||||
// add padding
|
||||
if let Some(ty) = arg.pad {
|
||||
argument_tys.push(ty.gcc_type(cx));
|
||||
}
|
||||
|
||||
let arg_ty = match arg.mode {
|
||||
PassMode::Ignore => continue,
|
||||
PassMode::Direct(_) => arg.layout.immediate_gcc_type(cx),
|
||||
PassMode::Pair(..) => {
|
||||
argument_tys.push(arg.layout.scalar_pair_element_gcc_type(cx, 0, true));
|
||||
argument_tys.push(arg.layout.scalar_pair_element_gcc_type(cx, 1, true));
|
||||
continue;
|
||||
}
|
||||
PassMode::Indirect { extra_attrs: Some(_), .. } => {
|
||||
/*let ptr_ty = cx.tcx.mk_mut_ptr(arg.layout.ty);
|
||||
let ptr_layout = cx.layout_of(ptr_ty);
|
||||
argument_tys.push(ptr_layout.scalar_pair_element_gcc_type(cx, 0, true));
|
||||
argument_tys.push(ptr_layout.scalar_pair_element_gcc_type(cx, 1, true));*/
|
||||
unimplemented!();
|
||||
//continue;
|
||||
}
|
||||
PassMode::Cast(cast) => cast.gcc_type(cx),
|
||||
PassMode::Indirect { extra_attrs: None, .. } => cx.type_ptr_to(arg.memory_ty(cx)),
|
||||
};
|
||||
argument_tys.push(arg_ty);
|
||||
}
|
||||
|
||||
(return_ty, argument_tys, self.c_variadic)
|
||||
}
|
||||
|
||||
fn ptr_to_gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
|
||||
let (return_type, params, variadic) = self.gcc_type(cx);
|
||||
let pointer_type = cx.context.new_function_pointer_type(None, return_type, ¶ms, variadic);
|
||||
pointer_type
|
||||
}
|
||||
|
||||
/*fn llvm_cconv(&self) -> llvm::CallConv {
|
||||
match self.conv {
|
||||
Conv::C | Conv::Rust => llvm::CCallConv,
|
||||
Conv::AmdGpuKernel => llvm::AmdGpuKernel,
|
||||
Conv::ArmAapcs => llvm::ArmAapcsCallConv,
|
||||
Conv::Msp430Intr => llvm::Msp430Intr,
|
||||
Conv::PtxKernel => llvm::PtxKernel,
|
||||
Conv::X86Fastcall => llvm::X86FastcallCallConv,
|
||||
Conv::X86Intr => llvm::X86_Intr,
|
||||
Conv::X86Stdcall => llvm::X86StdcallCallConv,
|
||||
Conv::X86ThisCall => llvm::X86_ThisCall,
|
||||
Conv::X86VectorCall => llvm::X86_VectorCall,
|
||||
Conv::X86_64SysV => llvm::X86_64_SysV,
|
||||
Conv::X86_64Win64 => llvm::X86_64_Win64,
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_attrs_llfn(&self, cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value) {
|
||||
// FIXME(eddyb) can this also be applied to callsites?
|
||||
if self.ret.layout.abi.is_uninhabited() {
|
||||
llvm::Attribute::NoReturn.apply_llfn(llvm::AttributePlace::Function, llfn);
|
||||
}
|
||||
|
||||
// FIXME(eddyb, wesleywiser): apply this to callsites as well?
|
||||
if !self.can_unwind {
|
||||
llvm::Attribute::NoUnwind.apply_llfn(llvm::AttributePlace::Function, llfn);
|
||||
}
|
||||
|
||||
let mut i = 0;
|
||||
let mut apply = |attrs: &ArgAttributes, ty: Option<&Type>| {
|
||||
attrs.apply_llfn(llvm::AttributePlace::Argument(i), llfn, ty);
|
||||
i += 1;
|
||||
};
|
||||
match self.ret.mode {
|
||||
PassMode::Direct(ref attrs) => {
|
||||
attrs.apply_llfn(llvm::AttributePlace::ReturnValue, llfn, None);
|
||||
}
|
||||
PassMode::Indirect(ref attrs, _) => apply(attrs, Some(self.ret.layout.gcc_type(cx))),
|
||||
_ => {}
|
||||
}
|
||||
for arg in &self.args {
|
||||
if arg.pad.is_some() {
|
||||
apply(&ArgAttributes::new(), None);
|
||||
}
|
||||
match arg.mode {
|
||||
PassMode::Ignore => {}
|
||||
PassMode::Direct(ref attrs) | PassMode::Indirect(ref attrs, None) => {
|
||||
apply(attrs, Some(arg.layout.gcc_type(cx)))
|
||||
}
|
||||
PassMode::Indirect(ref attrs, Some(ref extra_attrs)) => {
|
||||
apply(attrs, None);
|
||||
apply(extra_attrs, None);
|
||||
}
|
||||
PassMode::Pair(ref a, ref b) => {
|
||||
apply(a, None);
|
||||
apply(b, None);
|
||||
}
|
||||
PassMode::Cast(_) => apply(&ArgAttributes::new(), None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_attrs_callsite(&self, bx: &mut Builder<'a, 'll, 'tcx>, callsite: &'ll Value) {
|
||||
// FIXME(wesleywiser, eddyb): We should apply `nounwind` and `noreturn` as appropriate to this callsite.
|
||||
|
||||
let mut i = 0;
|
||||
let mut apply = |attrs: &ArgAttributes, ty: Option<&Type>| {
|
||||
attrs.apply_callsite(llvm::AttributePlace::Argument(i), callsite, ty);
|
||||
i += 1;
|
||||
};
|
||||
match self.ret.mode {
|
||||
PassMode::Direct(ref attrs) => {
|
||||
attrs.apply_callsite(llvm::AttributePlace::ReturnValue, callsite, None);
|
||||
}
|
||||
PassMode::Indirect(ref attrs, _) => apply(attrs, Some(self.ret.layout.gcc_type(bx))),
|
||||
_ => {}
|
||||
}
|
||||
if let abi::Abi::Scalar(ref scalar) = self.ret.layout.abi {
|
||||
// If the value is a boolean, the range is 0..2 and that ultimately
|
||||
// become 0..0 when the type becomes i1, which would be rejected
|
||||
// by the LLVM verifier.
|
||||
if let Int(..) = scalar.value {
|
||||
if !scalar.is_bool() {
|
||||
let range = scalar.valid_range_exclusive(bx);
|
||||
if range.start != range.end {
|
||||
bx.range_metadata(callsite, range);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for arg in &self.args {
|
||||
if arg.pad.is_some() {
|
||||
apply(&ArgAttributes::new(), None);
|
||||
}
|
||||
match arg.mode {
|
||||
PassMode::Ignore => {}
|
||||
PassMode::Direct(ref attrs) | PassMode::Indirect(ref attrs, None) => {
|
||||
apply(attrs, Some(arg.layout.gcc_type(bx)))
|
||||
}
|
||||
PassMode::Indirect(ref attrs, Some(ref extra_attrs)) => {
|
||||
apply(attrs, None);
|
||||
apply(extra_attrs, None);
|
||||
}
|
||||
PassMode::Pair(ref a, ref b) => {
|
||||
apply(a, None);
|
||||
apply(b, None);
|
||||
}
|
||||
PassMode::Cast(_) => apply(&ArgAttributes::new(), None),
|
||||
}
|
||||
}
|
||||
|
||||
let cconv = self.llvm_cconv();
|
||||
if cconv != llvm::CCallConv {
|
||||
llvm::SetInstructionCallConv(callsite, cconv);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
115
compiler/rustc_codegen_gcc/src/allocator.rs
Normal file
115
compiler/rustc_codegen_gcc/src/allocator.rs
Normal file
@@ -0,0 +1,115 @@
|
||||
//use crate::attributes;
|
||||
use gccjit::{FunctionType, ToRValue};
|
||||
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
|
||||
use rustc_middle::bug;
|
||||
use rustc_middle::ty::TyCtxt;
|
||||
use rustc_span::symbol::sym;
|
||||
|
||||
use crate::GccContext;
|
||||
|
||||
pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, kind: AllocatorKind, has_alloc_error_handler: bool) {
|
||||
let context = &mods.context;
|
||||
let usize =
|
||||
match tcx.sess.target.pointer_width {
|
||||
16 => context.new_type::<u16>(),
|
||||
32 => context.new_type::<u32>(),
|
||||
64 => context.new_type::<u64>(),
|
||||
tws => bug!("Unsupported target word size for int: {}", tws),
|
||||
};
|
||||
let i8 = context.new_type::<i8>();
|
||||
let i8p = i8.make_pointer();
|
||||
let void = context.new_type::<()>();
|
||||
|
||||
for method in ALLOCATOR_METHODS {
|
||||
let mut types = Vec::with_capacity(method.inputs.len());
|
||||
for ty in method.inputs.iter() {
|
||||
match *ty {
|
||||
AllocatorTy::Layout => {
|
||||
types.push(usize);
|
||||
types.push(usize);
|
||||
}
|
||||
AllocatorTy::Ptr => types.push(i8p),
|
||||
AllocatorTy::Usize => types.push(usize),
|
||||
|
||||
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
|
||||
}
|
||||
}
|
||||
let output = match method.output {
|
||||
AllocatorTy::ResultPtr => Some(i8p),
|
||||
AllocatorTy::Unit => None,
|
||||
|
||||
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
|
||||
panic!("invalid allocator output")
|
||||
}
|
||||
};
|
||||
let name = format!("__rust_{}", method.name);
|
||||
|
||||
let args: Vec<_> = types.iter().enumerate()
|
||||
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
|
||||
.collect();
|
||||
let func = context.new_function(None, FunctionType::Exported, output.unwrap_or(void), &args, name, false);
|
||||
|
||||
if tcx.sess.target.options.default_hidden_visibility {
|
||||
//llvm::LLVMRustSetVisibility(func, llvm::Visibility::Hidden);
|
||||
}
|
||||
if tcx.sess.must_emit_unwind_tables() {
|
||||
// TODO
|
||||
//attributes::emit_uwtable(func, true);
|
||||
}
|
||||
|
||||
let callee = kind.fn_name(method.name);
|
||||
let args: Vec<_> = types.iter().enumerate()
|
||||
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
|
||||
.collect();
|
||||
let callee = context.new_function(None, FunctionType::Extern, output.unwrap_or(void), &args, callee, false);
|
||||
//llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
|
||||
|
||||
let block = func.new_block("entry");
|
||||
|
||||
let args = args
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, _)| func.get_param(i as i32).to_rvalue())
|
||||
.collect::<Vec<_>>();
|
||||
let ret = context.new_call(None, callee, &args);
|
||||
//llvm::LLVMSetTailCall(ret, True);
|
||||
if output.is_some() {
|
||||
block.end_with_return(None, ret);
|
||||
}
|
||||
else {
|
||||
block.end_with_void_return(None);
|
||||
}
|
||||
}
|
||||
|
||||
let types = [usize, usize];
|
||||
let name = "__rust_alloc_error_handler".to_string();
|
||||
let args: Vec<_> = types.iter().enumerate()
|
||||
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
|
||||
.collect();
|
||||
let func = context.new_function(None, FunctionType::Exported, void, &args, name, false);
|
||||
|
||||
let kind =
|
||||
if has_alloc_error_handler {
|
||||
AllocatorKind::Global
|
||||
}
|
||||
else {
|
||||
AllocatorKind::Default
|
||||
};
|
||||
let callee = kind.fn_name(sym::oom);
|
||||
let args: Vec<_> = types.iter().enumerate()
|
||||
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
|
||||
.collect();
|
||||
let callee = context.new_function(None, FunctionType::Extern, void, &args, callee, false);
|
||||
//llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
|
||||
|
||||
let block = func.new_block("entry");
|
||||
|
||||
let args = args
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, _)| func.get_param(i as i32).to_rvalue())
|
||||
.collect::<Vec<_>>();
|
||||
let _ret = context.new_call(None, callee, &args);
|
||||
//llvm::LLVMSetTailCall(ret, True);
|
||||
block.end_with_void_return(None);
|
||||
}
|
||||
270
compiler/rustc_codegen_gcc/src/archive.rs
Normal file
270
compiler/rustc_codegen_gcc/src/archive.rs
Normal file
@@ -0,0 +1,270 @@
|
||||
use std::fs::File;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use rustc_session::Session;
|
||||
use rustc_codegen_ssa::back::archive::{find_library, ArchiveBuilder};
|
||||
use rustc_codegen_ssa::METADATA_FILENAME;
|
||||
use rustc_data_structures::temp_dir::MaybeTempDir;
|
||||
use rustc_middle::middle::cstore::DllImport;
|
||||
use rustc_span::symbol::Symbol;
|
||||
|
||||
struct ArchiveConfig<'a> {
|
||||
sess: &'a Session,
|
||||
dst: PathBuf,
|
||||
lib_search_paths: Vec<PathBuf>,
|
||||
use_native_ar: bool,
|
||||
use_gnu_style_archive: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ArchiveEntry {
|
||||
FromArchive {
|
||||
archive_index: usize,
|
||||
entry_index: usize,
|
||||
},
|
||||
File(PathBuf),
|
||||
}
|
||||
|
||||
pub struct ArArchiveBuilder<'a> {
|
||||
config: ArchiveConfig<'a>,
|
||||
src_archives: Vec<(PathBuf, ar::Archive<File>)>,
|
||||
// Don't use `HashMap` here, as the order is important. `rust.metadata.bin` must always be at
|
||||
// the end of an archive for linkers to not get confused.
|
||||
entries: Vec<(String, ArchiveEntry)>,
|
||||
}
|
||||
|
||||
impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
|
||||
fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> Self {
|
||||
use rustc_codegen_ssa::back::link::archive_search_paths;
|
||||
let config = ArchiveConfig {
|
||||
sess,
|
||||
dst: output.to_path_buf(),
|
||||
lib_search_paths: archive_search_paths(sess),
|
||||
use_native_ar: false,
|
||||
// FIXME test for linux and System V derivatives instead
|
||||
use_gnu_style_archive: sess.target.options.archive_format == "gnu",
|
||||
};
|
||||
|
||||
let (src_archives, entries) = if let Some(input) = input {
|
||||
let mut archive = ar::Archive::new(File::open(input).unwrap());
|
||||
let mut entries = Vec::new();
|
||||
|
||||
let mut i = 0;
|
||||
while let Some(entry) = archive.next_entry() {
|
||||
let entry = entry.unwrap();
|
||||
entries.push((
|
||||
String::from_utf8(entry.header().identifier().to_vec()).unwrap(),
|
||||
ArchiveEntry::FromArchive {
|
||||
archive_index: 0,
|
||||
entry_index: i,
|
||||
},
|
||||
));
|
||||
i += 1;
|
||||
}
|
||||
|
||||
(vec![(input.to_owned(), archive)], entries)
|
||||
} else {
|
||||
(vec![], Vec::new())
|
||||
};
|
||||
|
||||
ArArchiveBuilder {
|
||||
config,
|
||||
src_archives,
|
||||
entries,
|
||||
}
|
||||
}
|
||||
|
||||
fn src_files(&mut self) -> Vec<String> {
|
||||
self.entries.iter().map(|(name, _)| name.clone()).collect()
|
||||
}
|
||||
|
||||
fn remove_file(&mut self, name: &str) {
|
||||
let index = self
|
||||
.entries
|
||||
.iter()
|
||||
.position(|(entry_name, _)| entry_name == name)
|
||||
.expect("Tried to remove file not existing in src archive");
|
||||
self.entries.remove(index);
|
||||
}
|
||||
|
||||
fn add_file(&mut self, file: &Path) {
|
||||
self.entries.push((
|
||||
file.file_name().unwrap().to_str().unwrap().to_string(),
|
||||
ArchiveEntry::File(file.to_owned()),
|
||||
));
|
||||
}
|
||||
|
||||
fn add_native_library(&mut self, name: Symbol, verbatim: bool) {
|
||||
let location = find_library(name, verbatim, &self.config.lib_search_paths, self.config.sess);
|
||||
self.add_archive(location.clone(), |_| false)
|
||||
.unwrap_or_else(|e| {
|
||||
panic!(
|
||||
"failed to add native library {}: {}",
|
||||
location.to_string_lossy(),
|
||||
e
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
fn add_rlib(
|
||||
&mut self,
|
||||
rlib: &Path,
|
||||
name: &str,
|
||||
lto: bool,
|
||||
skip_objects: bool,
|
||||
) -> std::io::Result<()> {
|
||||
let obj_start = name.to_owned();
|
||||
|
||||
self.add_archive(rlib.to_owned(), move |fname: &str| {
|
||||
// Ignore metadata files, no matter the name.
|
||||
if fname == METADATA_FILENAME {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Don't include Rust objects if LTO is enabled
|
||||
if lto && fname.starts_with(&obj_start) && fname.ends_with(".o") {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Otherwise if this is *not* a rust object and we're skipping
|
||||
// objects then skip this file
|
||||
if skip_objects && (!fname.starts_with(&obj_start) || !fname.ends_with(".o")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// ok, don't skip this
|
||||
return false;
|
||||
})
|
||||
}
|
||||
|
||||
fn update_symbols(&mut self) {
|
||||
}
|
||||
|
||||
fn build(mut self) {
|
||||
use std::process::Command;
|
||||
|
||||
fn add_file_using_ar(archive: &Path, file: &Path) {
|
||||
Command::new("ar")
|
||||
.arg("r") // add or replace file
|
||||
.arg("-c") // silence created file message
|
||||
.arg(archive)
|
||||
.arg(&file)
|
||||
.status()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
enum BuilderKind<'a> {
|
||||
Bsd(ar::Builder<File>),
|
||||
Gnu(ar::GnuBuilder<File>),
|
||||
NativeAr(&'a Path),
|
||||
}
|
||||
|
||||
let mut builder = if self.config.use_native_ar {
|
||||
BuilderKind::NativeAr(&self.config.dst)
|
||||
} else if self.config.use_gnu_style_archive {
|
||||
BuilderKind::Gnu(ar::GnuBuilder::new(
|
||||
File::create(&self.config.dst).unwrap(),
|
||||
self.entries
|
||||
.iter()
|
||||
.map(|(name, _)| name.as_bytes().to_vec())
|
||||
.collect(),
|
||||
))
|
||||
} else {
|
||||
BuilderKind::Bsd(ar::Builder::new(File::create(&self.config.dst).unwrap()))
|
||||
};
|
||||
|
||||
// Add all files
|
||||
for (entry_name, entry) in self.entries.into_iter() {
|
||||
match entry {
|
||||
ArchiveEntry::FromArchive {
|
||||
archive_index,
|
||||
entry_index,
|
||||
} => {
|
||||
let (ref src_archive_path, ref mut src_archive) =
|
||||
self.src_archives[archive_index];
|
||||
let entry = src_archive.jump_to_entry(entry_index).unwrap();
|
||||
let header = entry.header().clone();
|
||||
|
||||
match builder {
|
||||
BuilderKind::Bsd(ref mut builder) => {
|
||||
builder.append(&header, entry).unwrap()
|
||||
}
|
||||
BuilderKind::Gnu(ref mut builder) => {
|
||||
builder.append(&header, entry).unwrap()
|
||||
}
|
||||
BuilderKind::NativeAr(archive_file) => {
|
||||
Command::new("ar")
|
||||
.arg("x")
|
||||
.arg(src_archive_path)
|
||||
.arg(&entry_name)
|
||||
.status()
|
||||
.unwrap();
|
||||
add_file_using_ar(archive_file, Path::new(&entry_name));
|
||||
std::fs::remove_file(entry_name).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
ArchiveEntry::File(file) =>
|
||||
match builder {
|
||||
BuilderKind::Bsd(ref mut builder) => {
|
||||
builder
|
||||
.append_file(entry_name.as_bytes(), &mut File::open(file).expect("file for bsd builder"))
|
||||
.unwrap()
|
||||
},
|
||||
BuilderKind::Gnu(ref mut builder) => {
|
||||
builder
|
||||
.append_file(entry_name.as_bytes(), &mut File::open(&file).expect(&format!("file {:?} for gnu builder", file)))
|
||||
.unwrap()
|
||||
},
|
||||
BuilderKind::NativeAr(archive_file) => add_file_using_ar(archive_file, &file),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Finalize archive
|
||||
std::mem::drop(builder);
|
||||
|
||||
// Run ranlib to be able to link the archive
|
||||
let status = std::process::Command::new("ranlib")
|
||||
.arg(self.config.dst)
|
||||
.status()
|
||||
.expect("Couldn't run ranlib");
|
||||
|
||||
if !status.success() {
|
||||
self.config.sess.fatal(&format!("Ranlib exited with code {:?}", status.code()));
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_dll_import_lib(&mut self, _lib_name: &str, _dll_imports: &[DllImport], _tmpdir: &MaybeTempDir) {
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ArArchiveBuilder<'a> {
|
||||
fn add_archive<F>(&mut self, archive_path: PathBuf, mut skip: F) -> std::io::Result<()>
|
||||
where
|
||||
F: FnMut(&str) -> bool + 'static,
|
||||
{
|
||||
let mut archive = ar::Archive::new(std::fs::File::open(&archive_path)?);
|
||||
let archive_index = self.src_archives.len();
|
||||
|
||||
let mut i = 0;
|
||||
while let Some(entry) = archive.next_entry() {
|
||||
let entry = entry.unwrap();
|
||||
let file_name = String::from_utf8(entry.header().identifier().to_vec()).unwrap();
|
||||
if !skip(&file_name) {
|
||||
self.entries.push((
|
||||
file_name,
|
||||
ArchiveEntry::FromArchive {
|
||||
archive_index,
|
||||
entry_index: i,
|
||||
},
|
||||
));
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
self.src_archives.push((archive_path, archive));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
632
compiler/rustc_codegen_gcc/src/asm.rs
Normal file
632
compiler/rustc_codegen_gcc/src/asm.rs
Normal file
@@ -0,0 +1,632 @@
|
||||
use gccjit::{RValue, ToRValue, Type};
|
||||
use rustc_ast::ast::{InlineAsmOptions, InlineAsmTemplatePiece};
|
||||
use rustc_codegen_ssa::mir::operand::OperandValue;
|
||||
use rustc_codegen_ssa::mir::place::PlaceRef;
|
||||
use rustc_codegen_ssa::traits::{AsmBuilderMethods, AsmMethods, BaseTypeMethods, BuilderMethods, GlobalAsmOperandRef, InlineAsmOperandRef};
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_hir::LlvmInlineAsmInner;
|
||||
use rustc_middle::bug;
|
||||
use rustc_span::Span;
|
||||
use rustc_target::asm::*;
|
||||
|
||||
use crate::builder::Builder;
|
||||
use crate::context::CodegenCx;
|
||||
use crate::type_of::LayoutGccExt;
|
||||
|
||||
impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
|
||||
fn codegen_llvm_inline_asm(&mut self, _ia: &LlvmInlineAsmInner, _outputs: Vec<PlaceRef<'tcx, RValue<'gcc>>>, mut _inputs: Vec<RValue<'gcc>>, _span: Span) -> bool {
|
||||
// TODO
|
||||
return true;
|
||||
|
||||
/*let mut ext_constraints = vec![];
|
||||
let mut output_types = vec![];
|
||||
|
||||
// Prepare the output operands
|
||||
let mut indirect_outputs = vec![];
|
||||
for (i, (out, &place)) in ia.outputs.iter().zip(&outputs).enumerate() {
|
||||
if out.is_rw {
|
||||
let operand = self.load_operand(place);
|
||||
if let OperandValue::Immediate(_) = operand.val {
|
||||
inputs.push(operand.immediate());
|
||||
}
|
||||
ext_constraints.push(i.to_string());
|
||||
}
|
||||
if out.is_indirect {
|
||||
let operand = self.load_operand(place);
|
||||
if let OperandValue::Immediate(_) = operand.val {
|
||||
indirect_outputs.push(operand.immediate());
|
||||
}
|
||||
} else {
|
||||
output_types.push(place.layout.gcc_type(self.cx()));
|
||||
}
|
||||
}
|
||||
if !indirect_outputs.is_empty() {
|
||||
indirect_outputs.extend_from_slice(&inputs);
|
||||
inputs = indirect_outputs;
|
||||
}
|
||||
|
||||
let clobbers = ia.clobbers.iter().map(|s| format!("~{{{}}}", &s));
|
||||
|
||||
// Default per-arch clobbers
|
||||
// Basically what clang does
|
||||
let arch_clobbers = match &self.sess().target.target.arch[..] {
|
||||
"x86" | "x86_64" => vec!["~{dirflag}", "~{fpsr}", "~{flags}"],
|
||||
"mips" | "mips64" => vec!["~{$1}"],
|
||||
_ => Vec::new(),
|
||||
};
|
||||
|
||||
let all_constraints = ia
|
||||
.outputs
|
||||
.iter()
|
||||
.map(|out| out.constraint.to_string())
|
||||
.chain(ia.inputs.iter().map(|s| s.to_string()))
|
||||
.chain(ext_constraints)
|
||||
.chain(clobbers)
|
||||
.chain(arch_clobbers.iter().map(|s| (*s).to_string()))
|
||||
.collect::<Vec<String>>()
|
||||
.join(",");
|
||||
|
||||
debug!("Asm Constraints: {}", &all_constraints);
|
||||
|
||||
// Depending on how many outputs we have, the return type is different
|
||||
let num_outputs = output_types.len();
|
||||
let output_type = match num_outputs {
|
||||
0 => self.type_void(),
|
||||
1 => output_types[0],
|
||||
_ => self.type_struct(&output_types, false),
|
||||
};
|
||||
|
||||
let asm = ia.asm.as_str();
|
||||
let r = inline_asm_call(
|
||||
self,
|
||||
&asm,
|
||||
&all_constraints,
|
||||
&inputs,
|
||||
output_type,
|
||||
ia.volatile,
|
||||
ia.alignstack,
|
||||
ia.dialect,
|
||||
);
|
||||
if r.is_none() {
|
||||
return false;
|
||||
}
|
||||
let r = r.unwrap();
|
||||
|
||||
// Again, based on how many outputs we have
|
||||
let outputs = ia.outputs.iter().zip(&outputs).filter(|&(ref o, _)| !o.is_indirect);
|
||||
for (i, (_, &place)) in outputs.enumerate() {
|
||||
let v = if num_outputs == 1 { r } else { self.extract_value(r, i as u64) };
|
||||
OperandValue::Immediate(v).store(self, place);
|
||||
}
|
||||
|
||||
// Store mark in a metadata node so we can map LLVM errors
|
||||
// back to source locations. See #17552.
|
||||
unsafe {
|
||||
let key = "srcloc";
|
||||
let kind = llvm::LLVMGetMDKindIDInContext(
|
||||
self.llcx,
|
||||
key.as_ptr() as *const c_char,
|
||||
key.len() as c_uint,
|
||||
);
|
||||
|
||||
let val: &'ll Value = self.const_i32(span.ctxt().outer_expn().as_u32() as i32);
|
||||
|
||||
llvm::LLVMSetMetadata(r, kind, llvm::LLVMMDNodeInContext(self.llcx, &val, 1));
|
||||
}
|
||||
|
||||
true*/
|
||||
}
|
||||
|
||||
fn codegen_inline_asm(&mut self, template: &[InlineAsmTemplatePiece], operands: &[InlineAsmOperandRef<'tcx, Self>], options: InlineAsmOptions, _span: &[Span]) {
|
||||
let asm_arch = self.tcx.sess.asm_arch.unwrap();
|
||||
|
||||
let intel_dialect =
|
||||
match asm_arch {
|
||||
InlineAsmArch::X86 | InlineAsmArch::X86_64 if !options.contains(InlineAsmOptions::ATT_SYNTAX) => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
// Collect the types of output operands
|
||||
// FIXME: we do this here instead of later because of a bug in libgccjit where creating the
|
||||
// variable after the extended asm expression causes a segfault:
|
||||
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100380
|
||||
let mut output_vars = FxHashMap::default();
|
||||
let mut operand_numbers = FxHashMap::default();
|
||||
let mut current_number = 0;
|
||||
for (idx, op) in operands.iter().enumerate() {
|
||||
match *op {
|
||||
InlineAsmOperandRef::Out { place, .. } => {
|
||||
let ty =
|
||||
match place {
|
||||
Some(place) => place.layout.gcc_type(self.cx, false),
|
||||
None => {
|
||||
// If the output is discarded, we don't really care what
|
||||
// type is used. We're just using this to tell GCC to
|
||||
// reserve the register.
|
||||
//dummy_output_type(self.cx, reg.reg_class())
|
||||
|
||||
// NOTE: if no output value, we should not create one (it will be a
|
||||
// clobber).
|
||||
continue;
|
||||
},
|
||||
};
|
||||
let var = self.current_func().new_local(None, ty, "output_register");
|
||||
operand_numbers.insert(idx, current_number);
|
||||
current_number += 1;
|
||||
output_vars.insert(idx, var);
|
||||
}
|
||||
InlineAsmOperandRef::InOut { out_place, .. } => {
|
||||
let ty =
|
||||
match out_place {
|
||||
Some(place) => place.layout.gcc_type(self.cx, false),
|
||||
None => {
|
||||
// If the output is discarded, we don't really care what
|
||||
// type is used. We're just using this to tell GCC to
|
||||
// reserve the register.
|
||||
//dummy_output_type(self.cx, reg.reg_class())
|
||||
|
||||
// NOTE: if no output value, we should not create one.
|
||||
continue;
|
||||
},
|
||||
};
|
||||
operand_numbers.insert(idx, current_number);
|
||||
current_number += 1;
|
||||
let var = self.current_func().new_local(None, ty, "output_register");
|
||||
output_vars.insert(idx, var);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// All output operands must come before the input operands, hence the 2 loops.
|
||||
for (idx, op) in operands.iter().enumerate() {
|
||||
match *op {
|
||||
InlineAsmOperandRef::In { .. } | InlineAsmOperandRef::InOut { .. } => {
|
||||
operand_numbers.insert(idx, current_number);
|
||||
current_number += 1;
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
// Build the template string
|
||||
let mut template_str = String::new();
|
||||
for piece in template {
|
||||
match *piece {
|
||||
InlineAsmTemplatePiece::String(ref string) => {
|
||||
if string.contains('%') {
|
||||
for c in string.chars() {
|
||||
if c == '%' {
|
||||
template_str.push_str("%%");
|
||||
}
|
||||
else {
|
||||
template_str.push(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
template_str.push_str(string)
|
||||
}
|
||||
}
|
||||
InlineAsmTemplatePiece::Placeholder { operand_idx, modifier, span: _ } => {
|
||||
match operands[operand_idx] {
|
||||
InlineAsmOperandRef::Out { reg, place: Some(_), .. } => {
|
||||
let modifier = modifier_to_gcc(asm_arch, reg.reg_class(), modifier);
|
||||
if let Some(modifier) = modifier {
|
||||
template_str.push_str(&format!("%{}{}", modifier, operand_numbers[&operand_idx]));
|
||||
} else {
|
||||
template_str.push_str(&format!("%{}", operand_numbers[&operand_idx]));
|
||||
}
|
||||
},
|
||||
InlineAsmOperandRef::Out { place: None, .. } => {
|
||||
unimplemented!("Out None");
|
||||
},
|
||||
InlineAsmOperandRef::In { reg, .. }
|
||||
| InlineAsmOperandRef::InOut { reg, .. } => {
|
||||
let modifier = modifier_to_gcc(asm_arch, reg.reg_class(), modifier);
|
||||
if let Some(modifier) = modifier {
|
||||
template_str.push_str(&format!("%{}{}", modifier, operand_numbers[&operand_idx]));
|
||||
} else {
|
||||
template_str.push_str(&format!("%{}", operand_numbers[&operand_idx]));
|
||||
}
|
||||
}
|
||||
InlineAsmOperandRef::Const { ref string } => {
|
||||
// Const operands get injected directly into the template
|
||||
template_str.push_str(string);
|
||||
}
|
||||
InlineAsmOperandRef::SymFn { .. }
|
||||
| InlineAsmOperandRef::SymStatic { .. } => {
|
||||
unimplemented!();
|
||||
// Only emit the raw symbol name
|
||||
//template_str.push_str(&format!("${{{}:c}}", op_idx[&operand_idx]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let block = self.llbb();
|
||||
let template_str =
|
||||
if intel_dialect {
|
||||
template_str
|
||||
}
|
||||
else {
|
||||
// FIXME: this might break the "m" memory constraint:
|
||||
// https://stackoverflow.com/a/9347957/389119
|
||||
// TODO: only set on x86 platforms.
|
||||
format!(".att_syntax noprefix\n\t{}\n\t.intel_syntax noprefix", template_str)
|
||||
};
|
||||
let extended_asm = block.add_extended_asm(None, &template_str);
|
||||
|
||||
// Collect the types of output operands
|
||||
let mut output_types = vec![];
|
||||
for (idx, op) in operands.iter().enumerate() {
|
||||
match *op {
|
||||
InlineAsmOperandRef::Out { reg, late, place } => {
|
||||
let ty =
|
||||
match place {
|
||||
Some(place) => place.layout.gcc_type(self.cx, false),
|
||||
None => {
|
||||
// If the output is discarded, we don't really care what
|
||||
// type is used. We're just using this to tell GCC to
|
||||
// reserve the register.
|
||||
dummy_output_type(self.cx, reg.reg_class())
|
||||
},
|
||||
};
|
||||
output_types.push(ty);
|
||||
//op_idx.insert(idx, constraints.len());
|
||||
let prefix = if late { "=" } else { "=&" };
|
||||
let constraint = format!("{}{}", prefix, reg_to_gcc(reg));
|
||||
|
||||
if place.is_some() {
|
||||
let var = output_vars[&idx];
|
||||
extended_asm.add_output_operand(None, &constraint, var);
|
||||
}
|
||||
else {
|
||||
// NOTE: reg.to_string() returns the register name with quotes around it so
|
||||
// remove them.
|
||||
extended_asm.add_clobber(reg.to_string().trim_matches('"'));
|
||||
}
|
||||
}
|
||||
InlineAsmOperandRef::InOut { reg, late, in_value, out_place } => {
|
||||
let ty =
|
||||
match out_place {
|
||||
Some(out_place) => out_place.layout.gcc_type(self.cx, false),
|
||||
None => dummy_output_type(self.cx, reg.reg_class())
|
||||
};
|
||||
output_types.push(ty);
|
||||
//op_idx.insert(idx, constraints.len());
|
||||
// TODO: prefix of "+" for reading and writing?
|
||||
let prefix = if late { "=" } else { "=&" };
|
||||
let constraint = format!("{}{}", prefix, reg_to_gcc(reg));
|
||||
|
||||
if out_place.is_some() {
|
||||
let var = output_vars[&idx];
|
||||
// TODO: also specify an output operand when out_place is none: that would
|
||||
// be the clobber but clobbers do not support general constraint like reg;
|
||||
// they only support named registers.
|
||||
// Not sure how we can do this. And the LLVM backend does not seem to add a
|
||||
// clobber.
|
||||
extended_asm.add_output_operand(None, &constraint, var);
|
||||
}
|
||||
|
||||
let constraint = reg_to_gcc(reg);
|
||||
extended_asm.add_input_operand(None, &constraint, in_value.immediate());
|
||||
}
|
||||
InlineAsmOperandRef::In { reg, value } => {
|
||||
let constraint = reg_to_gcc(reg);
|
||||
extended_asm.add_input_operand(None, &constraint, value.immediate());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/*if !options.contains(InlineAsmOptions::PRESERVES_FLAGS) {
|
||||
match asm_arch {
|
||||
InlineAsmArch::AArch64 | InlineAsmArch::Arm => {
|
||||
constraints.push("~{cc}".to_string());
|
||||
}
|
||||
InlineAsmArch::X86 | InlineAsmArch::X86_64 => {
|
||||
constraints.extend_from_slice(&[
|
||||
"~{dirflag}".to_string(),
|
||||
"~{fpsr}".to_string(),
|
||||
"~{flags}".to_string(),
|
||||
]);
|
||||
}
|
||||
InlineAsmArch::RiscV32 | InlineAsmArch::RiscV64 => {}
|
||||
}
|
||||
}
|
||||
if !options.contains(InlineAsmOptions::NOMEM) {
|
||||
// This is actually ignored by LLVM, but it's probably best to keep
|
||||
// it just in case. LLVM instead uses the ReadOnly/ReadNone
|
||||
// attributes on the call instruction to optimize.
|
||||
constraints.push("~{memory}".to_string());
|
||||
}
|
||||
let volatile = !options.contains(InlineAsmOptions::PURE);
|
||||
let alignstack = !options.contains(InlineAsmOptions::NOSTACK);
|
||||
let output_type = match &output_types[..] {
|
||||
[] => self.type_void(),
|
||||
[ty] => ty,
|
||||
tys => self.type_struct(&tys, false),
|
||||
};*/
|
||||
|
||||
/*let result = inline_asm_call(
|
||||
self,
|
||||
&template_str,
|
||||
&constraints.join(","),
|
||||
&inputs,
|
||||
output_type,
|
||||
volatile,
|
||||
alignstack,
|
||||
dialect,
|
||||
span,
|
||||
)
|
||||
.unwrap_or_else(|| span_bug!(span, "LLVM asm constraint validation failed"));
|
||||
|
||||
if options.contains(InlineAsmOptions::PURE) {
|
||||
if options.contains(InlineAsmOptions::NOMEM) {
|
||||
llvm::Attribute::ReadNone.apply_callsite(llvm::AttributePlace::Function, result);
|
||||
} else if options.contains(InlineAsmOptions::READONLY) {
|
||||
llvm::Attribute::ReadOnly.apply_callsite(llvm::AttributePlace::Function, result);
|
||||
}
|
||||
} else {
|
||||
if options.contains(InlineAsmOptions::NOMEM) {
|
||||
llvm::Attribute::InaccessibleMemOnly
|
||||
.apply_callsite(llvm::AttributePlace::Function, result);
|
||||
} else {
|
||||
// LLVM doesn't have an attribute to represent ReadOnly + SideEffect
|
||||
}
|
||||
}*/
|
||||
|
||||
// Write results to outputs
|
||||
for (idx, op) in operands.iter().enumerate() {
|
||||
if let InlineAsmOperandRef::Out { place: Some(place), .. }
|
||||
| InlineAsmOperandRef::InOut { out_place: Some(place), .. } = *op
|
||||
{
|
||||
OperandValue::Immediate(output_vars[&idx].to_rvalue()).store(self, place);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts a register class to a GCC constraint code.
|
||||
// TODO: return &'static str instead?
|
||||
fn reg_to_gcc(reg: InlineAsmRegOrRegClass) -> String {
|
||||
match reg {
|
||||
// For vector registers LLVM wants the register name to match the type size.
|
||||
InlineAsmRegOrRegClass::Reg(reg) => {
|
||||
// TODO: add support for vector register.
|
||||
let constraint =
|
||||
match reg.name() {
|
||||
"ax" => "a",
|
||||
"bx" => "b",
|
||||
"cx" => "c",
|
||||
"dx" => "d",
|
||||
"si" => "S",
|
||||
"di" => "D",
|
||||
// TODO: for registers like r11, we have to create a register variable: https://stackoverflow.com/a/31774784/389119
|
||||
// TODO: in this case though, it's a clobber, so it should work as r11.
|
||||
// Recent nightly supports clobber() syntax, so update to it. It does not seem
|
||||
// like it's implemented yet.
|
||||
name => name, // FIXME: probably wrong.
|
||||
};
|
||||
constraint.to_string()
|
||||
},
|
||||
InlineAsmRegOrRegClass::RegClass(reg) => match reg {
|
||||
InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::preg) => unimplemented!(),
|
||||
InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::reg) => unimplemented!(),
|
||||
InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg) => unimplemented!(),
|
||||
InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg_low16) => unimplemented!(),
|
||||
InlineAsmRegClass::Arm(ArmInlineAsmRegClass::reg) => unimplemented!(),
|
||||
InlineAsmRegClass::Arm(ArmInlineAsmRegClass::reg_thumb) => unimplemented!(),
|
||||
InlineAsmRegClass::Arm(ArmInlineAsmRegClass::sreg)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg_low16)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg_low8) => unimplemented!(),
|
||||
InlineAsmRegClass::Arm(ArmInlineAsmRegClass::sreg_low16)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg_low8)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg_low4) => unimplemented!(),
|
||||
InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg) => unimplemented!(),
|
||||
InlineAsmRegClass::Bpf(_) => unimplemented!(),
|
||||
InlineAsmRegClass::Hexagon(HexagonInlineAsmRegClass::reg) => unimplemented!(),
|
||||
InlineAsmRegClass::Mips(MipsInlineAsmRegClass::reg) => unimplemented!(),
|
||||
InlineAsmRegClass::Mips(MipsInlineAsmRegClass::freg) => unimplemented!(),
|
||||
InlineAsmRegClass::Nvptx(NvptxInlineAsmRegClass::reg16) => unimplemented!(),
|
||||
InlineAsmRegClass::Nvptx(NvptxInlineAsmRegClass::reg32) => unimplemented!(),
|
||||
InlineAsmRegClass::Nvptx(NvptxInlineAsmRegClass::reg64) => unimplemented!(),
|
||||
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::reg) => unimplemented!(),
|
||||
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::reg_nonzero) => unimplemented!(),
|
||||
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::freg) => unimplemented!(),
|
||||
InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::reg) => unimplemented!(),
|
||||
InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::freg) => unimplemented!(),
|
||||
InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::vreg) => unimplemented!(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::mmx_reg) => unimplemented!(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::reg) => "r",
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_abcd) => unimplemented!(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_byte) => unimplemented!(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::xmm_reg)
|
||||
| InlineAsmRegClass::X86(X86InlineAsmRegClass::ymm_reg) => unimplemented!(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::x87_reg) => unimplemented!(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::zmm_reg) => unimplemented!(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::kreg) => unimplemented!(),
|
||||
InlineAsmRegClass::Wasm(WasmInlineAsmRegClass::local) => unimplemented!(),
|
||||
InlineAsmRegClass::SpirV(SpirVInlineAsmRegClass::reg) => {
|
||||
bug!("GCC backend does not support SPIR-V")
|
||||
}
|
||||
InlineAsmRegClass::Err => unreachable!(),
|
||||
}
|
||||
.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Type to use for outputs that are discarded. It doesn't really matter what
|
||||
/// the type is, as long as it is valid for the constraint code.
|
||||
fn dummy_output_type<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, reg: InlineAsmRegClass) -> Type<'gcc> {
|
||||
match reg {
|
||||
InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::reg) => cx.type_i32(),
|
||||
InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::preg) => unimplemented!(),
|
||||
InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg)
|
||||
| InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg_low16) => {
|
||||
unimplemented!()
|
||||
}
|
||||
InlineAsmRegClass::Arm(ArmInlineAsmRegClass::reg)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::reg_thumb) => cx.type_i32(),
|
||||
InlineAsmRegClass::Arm(ArmInlineAsmRegClass::sreg)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::sreg_low16) => cx.type_f32(),
|
||||
InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg_low16)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg_low8) => cx.type_f64(),
|
||||
InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg_low8)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg_low4) => {
|
||||
unimplemented!()
|
||||
}
|
||||
InlineAsmRegClass::Bpf(_) => unimplemented!(),
|
||||
InlineAsmRegClass::Hexagon(HexagonInlineAsmRegClass::reg) => cx.type_i32(),
|
||||
InlineAsmRegClass::Mips(MipsInlineAsmRegClass::reg) => cx.type_i32(),
|
||||
InlineAsmRegClass::Mips(MipsInlineAsmRegClass::freg) => cx.type_f32(),
|
||||
InlineAsmRegClass::Nvptx(NvptxInlineAsmRegClass::reg16) => cx.type_i16(),
|
||||
InlineAsmRegClass::Nvptx(NvptxInlineAsmRegClass::reg32) => cx.type_i32(),
|
||||
InlineAsmRegClass::Nvptx(NvptxInlineAsmRegClass::reg64) => cx.type_i64(),
|
||||
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::reg) => cx.type_i32(),
|
||||
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::reg_nonzero) => cx.type_i32(),
|
||||
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::freg) => cx.type_f64(),
|
||||
InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::reg) => cx.type_i32(),
|
||||
InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::freg) => cx.type_f32(),
|
||||
InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::vreg) => cx.type_f32(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::reg)
|
||||
| InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_abcd) => cx.type_i32(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_byte) => cx.type_i8(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::mmx_reg) => unimplemented!(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::xmm_reg)
|
||||
| InlineAsmRegClass::X86(X86InlineAsmRegClass::ymm_reg)
|
||||
| InlineAsmRegClass::X86(X86InlineAsmRegClass::zmm_reg) => cx.type_f32(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::x87_reg) => unimplemented!(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::kreg) => cx.type_i16(),
|
||||
InlineAsmRegClass::Wasm(WasmInlineAsmRegClass::local) => cx.type_i32(),
|
||||
InlineAsmRegClass::SpirV(SpirVInlineAsmRegClass::reg) => {
|
||||
bug!("LLVM backend does not support SPIR-V")
|
||||
},
|
||||
InlineAsmRegClass::Err => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> AsmMethods for CodegenCx<'gcc, 'tcx> {
|
||||
fn codegen_global_asm(&self, template: &[InlineAsmTemplatePiece], operands: &[GlobalAsmOperandRef], options: InlineAsmOptions, _line_spans: &[Span]) {
|
||||
let asm_arch = self.tcx.sess.asm_arch.unwrap();
|
||||
|
||||
// Default to Intel syntax on x86
|
||||
let intel_syntax = matches!(asm_arch, InlineAsmArch::X86 | InlineAsmArch::X86_64)
|
||||
&& !options.contains(InlineAsmOptions::ATT_SYNTAX);
|
||||
|
||||
// Build the template string
|
||||
let mut template_str = String::new();
|
||||
for piece in template {
|
||||
match *piece {
|
||||
InlineAsmTemplatePiece::String(ref string) => {
|
||||
for line in string.lines() {
|
||||
// NOTE: gcc does not allow inline comment, so remove them.
|
||||
let line =
|
||||
if let Some(index) = line.rfind("//") {
|
||||
&line[..index]
|
||||
}
|
||||
else {
|
||||
line
|
||||
};
|
||||
template_str.push_str(line);
|
||||
template_str.push('\n');
|
||||
}
|
||||
},
|
||||
InlineAsmTemplatePiece::Placeholder { operand_idx, modifier: _, span: _ } => {
|
||||
match operands[operand_idx] {
|
||||
GlobalAsmOperandRef::Const { ref string } => {
|
||||
// Const operands get injected directly into the
|
||||
// template. Note that we don't need to escape $
|
||||
// here unlike normal inline assembly.
|
||||
template_str.push_str(string);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let template_str =
|
||||
if intel_syntax {
|
||||
format!("{}\n\t.intel_syntax noprefix", template_str)
|
||||
}
|
||||
else {
|
||||
format!(".att_syntax\n\t{}\n\t.intel_syntax noprefix", template_str)
|
||||
};
|
||||
// NOTE: seems like gcc will put the asm in the wrong section, so set it to .text manually.
|
||||
let template_str = format!(".pushsection .text\n{}\n.popsection", template_str);
|
||||
self.context.add_top_level_asm(None, &template_str);
|
||||
}
|
||||
}
|
||||
|
||||
fn modifier_to_gcc(arch: InlineAsmArch, reg: InlineAsmRegClass, modifier: Option<char>) -> Option<char> {
|
||||
match reg {
|
||||
InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::reg) => modifier,
|
||||
InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::preg) => modifier,
|
||||
InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg)
|
||||
| InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg_low16) => {
|
||||
unimplemented!()
|
||||
//if modifier == Some('v') { None } else { modifier }
|
||||
}
|
||||
InlineAsmRegClass::Arm(ArmInlineAsmRegClass::reg)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::reg_thumb) => unimplemented!(),
|
||||
InlineAsmRegClass::Arm(ArmInlineAsmRegClass::sreg)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::sreg_low16) => unimplemented!(),
|
||||
InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg_low16)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg_low8) => unimplemented!(),
|
||||
InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg_low8)
|
||||
| InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg_low4) => {
|
||||
unimplemented!()
|
||||
/*if modifier.is_none() {
|
||||
Some('q')
|
||||
} else {
|
||||
modifier
|
||||
}*/
|
||||
}
|
||||
InlineAsmRegClass::Bpf(_) => unimplemented!(),
|
||||
InlineAsmRegClass::Hexagon(_) => unimplemented!(),
|
||||
InlineAsmRegClass::Mips(_) => unimplemented!(),
|
||||
InlineAsmRegClass::Nvptx(_) => unimplemented!(),
|
||||
InlineAsmRegClass::PowerPC(_) => unimplemented!(),
|
||||
InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::reg)
|
||||
| InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::freg) => unimplemented!(),
|
||||
InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::vreg) => unimplemented!(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::reg)
|
||||
| InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_abcd) => match modifier {
|
||||
None if arch == InlineAsmArch::X86_64 => Some('q'),
|
||||
None => Some('k'),
|
||||
Some('l') => Some('b'),
|
||||
Some('h') => Some('h'),
|
||||
Some('x') => Some('w'),
|
||||
Some('e') => Some('k'),
|
||||
Some('r') => Some('q'),
|
||||
_ => unreachable!(),
|
||||
},
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::mmx_reg) => unimplemented!(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_byte) => unimplemented!(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::xmm_reg)
|
||||
| InlineAsmRegClass::X86(X86InlineAsmRegClass::ymm_reg)
|
||||
| InlineAsmRegClass::X86(X86InlineAsmRegClass::zmm_reg) => unimplemented!() /*match (reg, modifier) {
|
||||
(X86InlineAsmRegClass::xmm_reg, None) => Some('x'),
|
||||
(X86InlineAsmRegClass::ymm_reg, None) => Some('t'),
|
||||
(X86InlineAsmRegClass::zmm_reg, None) => Some('g'),
|
||||
(_, Some('x')) => Some('x'),
|
||||
(_, Some('y')) => Some('t'),
|
||||
(_, Some('z')) => Some('g'),
|
||||
_ => unreachable!(),
|
||||
}*/,
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::x87_reg) => unimplemented!(),
|
||||
InlineAsmRegClass::X86(X86InlineAsmRegClass::kreg) => unimplemented!(),
|
||||
InlineAsmRegClass::Wasm(WasmInlineAsmRegClass::local) => unimplemented!(),
|
||||
InlineAsmRegClass::SpirV(SpirVInlineAsmRegClass::reg) => {
|
||||
bug!("LLVM backend does not support SPIR-V")
|
||||
},
|
||||
InlineAsmRegClass::Err => unreachable!(),
|
||||
}
|
||||
}
|
||||
1
compiler/rustc_codegen_gcc/src/back/mod.rs
Normal file
1
compiler/rustc_codegen_gcc/src/back/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod write;
|
||||
234
compiler/rustc_codegen_gcc/src/back/write.rs
Normal file
234
compiler/rustc_codegen_gcc/src/back/write.rs
Normal file
@@ -0,0 +1,234 @@
|
||||
use std::fs;
|
||||
|
||||
use gccjit::OutputKind;
|
||||
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen};
|
||||
use rustc_codegen_ssa::back::write::{CodegenContext, EmitObj, ModuleConfig};
|
||||
use rustc_errors::Handler;
|
||||
use rustc_session::config::OutputType;
|
||||
use rustc_span::fatal_error::FatalError;
|
||||
use rustc_target::spec::SplitDebuginfo;
|
||||
|
||||
use crate::{GccCodegenBackend, GccContext};
|
||||
|
||||
pub(crate) unsafe fn codegen(cgcx: &CodegenContext<GccCodegenBackend>, _diag_handler: &Handler, module: ModuleCodegen<GccContext>, config: &ModuleConfig) -> Result<CompiledModule, FatalError> {
|
||||
let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_codegen", &module.name[..]);
|
||||
{
|
||||
let context = &module.module_llvm.context;
|
||||
|
||||
//let llcx = &*module.module_llvm.llcx;
|
||||
//let tm = &*module.module_llvm.tm;
|
||||
let module_name = module.name.clone();
|
||||
let module_name = Some(&module_name[..]);
|
||||
//let handlers = DiagnosticHandlers::new(cgcx, diag_handler, llcx);
|
||||
|
||||
/*if cgcx.msvc_imps_needed {
|
||||
create_msvc_imps(cgcx, llcx, llmod);
|
||||
}*/
|
||||
|
||||
// A codegen-specific pass manager is used to generate object
|
||||
// files for an GCC module.
|
||||
//
|
||||
// Apparently each of these pass managers is a one-shot kind of
|
||||
// thing, so we create a new one for each type of output. The
|
||||
// pass manager passed to the closure should be ensured to not
|
||||
// escape the closure itself, and the manager should only be
|
||||
// used once.
|
||||
/*unsafe fn with_codegen<'ll, F, R>(tm: &'ll llvm::TargetMachine, llmod: &'ll llvm::Module, no_builtins: bool, f: F) -> R
|
||||
where F: FnOnce(&'ll mut PassManager<'ll>) -> R,
|
||||
{
|
||||
let cpm = llvm::LLVMCreatePassManager();
|
||||
llvm::LLVMAddAnalysisPasses(tm, cpm);
|
||||
llvm::LLVMRustAddLibraryInfo(cpm, llmod, no_builtins);
|
||||
f(cpm)
|
||||
}*/
|
||||
|
||||
// Two things to note:
|
||||
// - If object files are just LLVM bitcode we write bitcode, copy it to
|
||||
// the .o file, and delete the bitcode if it wasn't otherwise
|
||||
// requested.
|
||||
// - If we don't have the integrated assembler then we need to emit
|
||||
// asm from LLVM and use `gcc` to create the object file.
|
||||
|
||||
let _bc_out = cgcx.output_filenames.temp_path(OutputType::Bitcode, module_name);
|
||||
let obj_out = cgcx.output_filenames.temp_path(OutputType::Object, module_name);
|
||||
|
||||
if config.bitcode_needed() {
|
||||
// TODO
|
||||
/*let _timer = cgcx
|
||||
.prof
|
||||
.generic_activity_with_arg("LLVM_module_codegen_make_bitcode", &module.name[..]);
|
||||
let thin = ThinBuffer::new(llmod);
|
||||
let data = thin.data();
|
||||
|
||||
if config.emit_bc || config.emit_obj == EmitObj::Bitcode {
|
||||
let _timer = cgcx.prof.generic_activity_with_arg(
|
||||
"LLVM_module_codegen_emit_bitcode",
|
||||
&module.name[..],
|
||||
);
|
||||
if let Err(e) = fs::write(&bc_out, data) {
|
||||
let msg = format!("failed to write bytecode to {}: {}", bc_out.display(), e);
|
||||
diag_handler.err(&msg);
|
||||
}
|
||||
}
|
||||
|
||||
if config.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full) {
|
||||
let _timer = cgcx.prof.generic_activity_with_arg(
|
||||
"LLVM_module_codegen_embed_bitcode",
|
||||
&module.name[..],
|
||||
);
|
||||
embed_bitcode(cgcx, llcx, llmod, Some(data));
|
||||
}
|
||||
|
||||
if config.emit_bc_compressed {
|
||||
let _timer = cgcx.prof.generic_activity_with_arg(
|
||||
"LLVM_module_codegen_emit_compressed_bitcode",
|
||||
&module.name[..],
|
||||
);
|
||||
let dst = bc_out.with_extension(RLIB_BYTECODE_EXTENSION);
|
||||
let data = bytecode::encode(&module.name, data);
|
||||
if let Err(e) = fs::write(&dst, data) {
|
||||
let msg = format!("failed to write bytecode to {}: {}", dst.display(), e);
|
||||
diag_handler.err(&msg);
|
||||
}
|
||||
}*/
|
||||
} /*else if config.emit_obj == EmitObj::ObjectCode(BitcodeSection::Marker) {
|
||||
unimplemented!();
|
||||
//embed_bitcode(cgcx, llcx, llmod, None);
|
||||
}*/
|
||||
|
||||
if config.emit_ir {
|
||||
unimplemented!();
|
||||
/*let _timer = cgcx
|
||||
.prof
|
||||
.generic_activity_with_arg("LLVM_module_codegen_emit_ir", &module.name[..]);
|
||||
let out = cgcx.output_filenames.temp_path(OutputType::LlvmAssembly, module_name);
|
||||
let out_c = path_to_c_string(&out);
|
||||
|
||||
extern "C" fn demangle_callback(
|
||||
input_ptr: *const c_char,
|
||||
input_len: size_t,
|
||||
output_ptr: *mut c_char,
|
||||
output_len: size_t,
|
||||
) -> size_t {
|
||||
let input =
|
||||
unsafe { slice::from_raw_parts(input_ptr as *const u8, input_len as usize) };
|
||||
|
||||
let input = match str::from_utf8(input) {
|
||||
Ok(s) => s,
|
||||
Err(_) => return 0,
|
||||
};
|
||||
|
||||
let output = unsafe {
|
||||
slice::from_raw_parts_mut(output_ptr as *mut u8, output_len as usize)
|
||||
};
|
||||
let mut cursor = io::Cursor::new(output);
|
||||
|
||||
let demangled = match rustc_demangle::try_demangle(input) {
|
||||
Ok(d) => d,
|
||||
Err(_) => return 0,
|
||||
};
|
||||
|
||||
if write!(cursor, "{:#}", demangled).is_err() {
|
||||
// Possible only if provided buffer is not big enough
|
||||
return 0;
|
||||
}
|
||||
|
||||
cursor.position() as size_t
|
||||
}
|
||||
|
||||
let result = llvm::LLVMRustPrintModule(llmod, out_c.as_ptr(), demangle_callback);
|
||||
result.into_result().map_err(|()| {
|
||||
let msg = format!("failed to write LLVM IR to {}", out.display());
|
||||
llvm_err(diag_handler, &msg)
|
||||
})?;*/
|
||||
}
|
||||
|
||||
if config.emit_asm {
|
||||
let _timer = cgcx
|
||||
.prof
|
||||
.generic_activity_with_arg("LLVM_module_codegen_emit_asm", &module.name[..]);
|
||||
let path = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);
|
||||
context.compile_to_file(OutputKind::Assembler, path.to_str().expect("path to str"));
|
||||
|
||||
/*with_codegen(tm, llmod, config.no_builtins, |cpm| {
|
||||
write_output_file(diag_handler, tm, cpm, llmod, &path, llvm::FileType::AssemblyFile)
|
||||
})?;*/
|
||||
}
|
||||
|
||||
match config.emit_obj {
|
||||
EmitObj::ObjectCode(_) => {
|
||||
let _timer = cgcx
|
||||
.prof
|
||||
.generic_activity_with_arg("LLVM_module_codegen_emit_obj", &module.name[..]);
|
||||
//with_codegen(tm, llmod, config.no_builtins, |cpm| {
|
||||
//println!("1: {}", module.name);
|
||||
match &*module.name {
|
||||
"std_example.7rcbfp3g-cgu.15" => {
|
||||
println!("Dumping reproducer {}", module.name);
|
||||
let _ = fs::create_dir("/tmp/reproducers");
|
||||
// FIXME: segfault in dump_reproducer_to_file() might be caused by
|
||||
// transmuting an rvalue to an lvalue.
|
||||
// Segfault is actually in gcc::jit::reproducer::get_identifier_as_lvalue
|
||||
context.dump_reproducer_to_file(&format!("/tmp/reproducers/{}.c", module.name));
|
||||
println!("Dumped reproducer {}", module.name);
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
/*let _ = fs::create_dir("/tmp/dumps");
|
||||
context.dump_to_file(&format!("/tmp/dumps/{}.c", module.name), true);
|
||||
println!("Dumped {}", module.name);*/
|
||||
//println!("Compile module {}", module.name);
|
||||
context.compile_to_file(OutputKind::ObjectFile, obj_out.to_str().expect("path to str"));
|
||||
//})?;
|
||||
}
|
||||
|
||||
EmitObj::Bitcode => {
|
||||
//unimplemented!();
|
||||
/*debug!("copying bitcode {:?} to obj {:?}", bc_out, obj_out);
|
||||
if let Err(e) = link_or_copy(&bc_out, &obj_out) {
|
||||
diag_handler.err(&format!("failed to copy bitcode to object file: {}", e));
|
||||
}
|
||||
|
||||
if !config.emit_bc {
|
||||
debug!("removing_bitcode {:?}", bc_out);
|
||||
if let Err(e) = fs::remove_file(&bc_out) {
|
||||
diag_handler.err(&format!("failed to remove bitcode: {}", e));
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
EmitObj::None => {}
|
||||
}
|
||||
|
||||
//drop(handlers);
|
||||
}
|
||||
|
||||
Ok(module.into_compiled_module(
|
||||
config.emit_obj != EmitObj::None,
|
||||
cgcx.target_can_use_split_dwarf && cgcx.split_debuginfo == SplitDebuginfo::Unpacked,
|
||||
config.emit_bc,
|
||||
&cgcx.output_filenames,
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn link(_cgcx: &CodegenContext<GccCodegenBackend>, _diag_handler: &Handler, mut _modules: Vec<ModuleCodegen<GccContext>>) -> Result<ModuleCodegen<GccContext>, FatalError> {
|
||||
unimplemented!();
|
||||
/*use super::lto::{Linker, ModuleBuffer};
|
||||
// Sort the modules by name to ensure to ensure deterministic behavior.
|
||||
modules.sort_by(|a, b| a.name.cmp(&b.name));
|
||||
let (first, elements) =
|
||||
modules.split_first().expect("Bug! modules must contain at least one module.");
|
||||
|
||||
let mut linker = Linker::new(first.module_llvm.llmod());
|
||||
for module in elements {
|
||||
let _timer =
|
||||
cgcx.prof.generic_activity_with_arg("LLVM_link_module", format!("{:?}", module.name));
|
||||
let buffer = ModuleBuffer::new(module.module_llvm.llmod());
|
||||
linker.add(&buffer.data()).map_err(|()| {
|
||||
let msg = format!("failed to serialize module {:?}", module.name);
|
||||
llvm_err(&diag_handler, &msg)
|
||||
})?;
|
||||
}
|
||||
drop(linker);
|
||||
Ok(modules.remove(0))*/
|
||||
}
|
||||
173
compiler/rustc_codegen_gcc/src/base.rs
Normal file
173
compiler/rustc_codegen_gcc/src/base.rs
Normal file
@@ -0,0 +1,173 @@
|
||||
use std::env;
|
||||
use std::sync::Once;
|
||||
use std::time::Instant;
|
||||
|
||||
use gccjit::{
|
||||
Context,
|
||||
FunctionType,
|
||||
GlobalKind,
|
||||
};
|
||||
use rustc_hir::def_id::LOCAL_CRATE;
|
||||
use rustc_middle::dep_graph;
|
||||
use rustc_middle::middle::cstore::EncodedMetadata;
|
||||
use rustc_middle::middle::exported_symbols;
|
||||
use rustc_middle::ty::TyCtxt;
|
||||
use rustc_middle::mir::mono::Linkage;
|
||||
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind};
|
||||
use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
|
||||
use rustc_codegen_ssa::mono_item::MonoItemExt;
|
||||
use rustc_codegen_ssa::traits::DebugInfoMethods;
|
||||
use rustc_session::config::DebugInfo;
|
||||
use rustc_span::Symbol;
|
||||
|
||||
use crate::{GccContext, create_function_calling_initializers};
|
||||
use crate::builder::Builder;
|
||||
use crate::context::CodegenCx;
|
||||
|
||||
pub fn global_linkage_to_gcc(linkage: Linkage) -> GlobalKind {
|
||||
match linkage {
|
||||
Linkage::External => GlobalKind::Imported,
|
||||
Linkage::AvailableExternally => GlobalKind::Imported,
|
||||
Linkage::LinkOnceAny => unimplemented!(),
|
||||
Linkage::LinkOnceODR => unimplemented!(),
|
||||
Linkage::WeakAny => unimplemented!(),
|
||||
Linkage::WeakODR => unimplemented!(),
|
||||
Linkage::Appending => unimplemented!(),
|
||||
Linkage::Internal => GlobalKind::Internal,
|
||||
Linkage::Private => GlobalKind::Internal,
|
||||
Linkage::ExternalWeak => GlobalKind::Imported, // TODO: should be weak linkage.
|
||||
Linkage::Common => unimplemented!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn linkage_to_gcc(linkage: Linkage) -> FunctionType {
|
||||
match linkage {
|
||||
Linkage::External => FunctionType::Exported,
|
||||
Linkage::AvailableExternally => FunctionType::Extern,
|
||||
Linkage::LinkOnceAny => unimplemented!(),
|
||||
Linkage::LinkOnceODR => unimplemented!(),
|
||||
Linkage::WeakAny => FunctionType::Exported, // FIXME: should be similar to linkonce.
|
||||
Linkage::WeakODR => unimplemented!(),
|
||||
Linkage::Appending => unimplemented!(),
|
||||
Linkage::Internal => FunctionType::Internal,
|
||||
Linkage::Private => FunctionType::Internal,
|
||||
Linkage::ExternalWeak => unimplemented!(),
|
||||
Linkage::Common => unimplemented!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (ModuleCodegen<GccContext>, u64) {
|
||||
let prof_timer = tcx.prof.generic_activity("codegen_module");
|
||||
let start_time = Instant::now();
|
||||
|
||||
let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
|
||||
let (module, _) = tcx.dep_graph.with_task(dep_node, tcx, cgu_name, module_codegen, dep_graph::hash_result);
|
||||
let time_to_codegen = start_time.elapsed();
|
||||
drop(prof_timer);
|
||||
|
||||
// We assume that the cost to run GCC on a CGU is proportional to
|
||||
// the time we needed for codegenning it.
|
||||
let cost = time_to_codegen.as_secs() * 1_000_000_000 + time_to_codegen.subsec_nanos() as u64;
|
||||
|
||||
fn module_codegen(tcx: TyCtxt<'_>, cgu_name: Symbol) -> ModuleCodegen<GccContext> {
|
||||
let cgu = tcx.codegen_unit(cgu_name);
|
||||
// Instantiate monomorphizations without filling out definitions yet...
|
||||
//let llvm_module = ModuleLlvm::new(tcx, &cgu_name.as_str());
|
||||
let context = Context::default();
|
||||
// TODO: only set on x86 platforms.
|
||||
context.add_command_line_option("-masm=intel");
|
||||
for arg in &tcx.sess.opts.cg.llvm_args {
|
||||
context.add_command_line_option(arg);
|
||||
}
|
||||
context.add_command_line_option("-fno-semantic-interposition");
|
||||
//context.set_dump_code_on_compile(true);
|
||||
if env::var("CG_GCCJIT_DUMP_GIMPLE").as_deref() == Ok("1") {
|
||||
context.set_dump_initial_gimple(true);
|
||||
}
|
||||
context.set_debug_info(true);
|
||||
//context.set_dump_everything(true);
|
||||
//context.set_keep_intermediates(true);
|
||||
|
||||
{
|
||||
let cx = CodegenCx::new(&context, cgu, tcx);
|
||||
|
||||
static START: Once = Once::new();
|
||||
START.call_once(|| {
|
||||
let initializer_name = format!("__gccGlobalCrateInit{}", tcx.crate_name(LOCAL_CRATE));
|
||||
let func = context.new_function(None, FunctionType::Exported, context.new_type::<()>(), &[], initializer_name, false);
|
||||
let block = func.new_block("initial");
|
||||
create_function_calling_initializers(tcx, &context, block);
|
||||
block.end_with_void_return(None);
|
||||
});
|
||||
|
||||
//println!("module_codegen: {:?} {:?}", cgu_name, &cx.context as *const _);
|
||||
let mono_items = cgu.items_in_deterministic_order(tcx);
|
||||
for &(mono_item, (linkage, visibility)) in &mono_items {
|
||||
mono_item.predefine::<Builder<'_, '_, '_>>(&cx, linkage, visibility);
|
||||
}
|
||||
|
||||
// ... and now that we have everything pre-defined, fill out those definitions.
|
||||
for &(mono_item, _) in &mono_items {
|
||||
mono_item.define::<Builder<'_, '_, '_>>(&cx);
|
||||
}
|
||||
|
||||
// If this codegen unit contains the main function, also create the
|
||||
// wrapper here
|
||||
maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx);
|
||||
|
||||
// Finalize debuginfo
|
||||
if cx.sess().opts.debuginfo != DebugInfo::None {
|
||||
cx.debuginfo_finalize();
|
||||
}
|
||||
|
||||
cx.global_init_block.end_with_void_return(None);
|
||||
}
|
||||
|
||||
ModuleCodegen {
|
||||
name: cgu_name.to_string(),
|
||||
module_llvm: GccContext {
|
||||
context
|
||||
},
|
||||
kind: ModuleKind::Regular,
|
||||
}
|
||||
}
|
||||
|
||||
(module, cost)
|
||||
}
|
||||
|
||||
pub fn write_compressed_metadata<'tcx>(tcx: TyCtxt<'tcx>, metadata: &EncodedMetadata, gcc_module: &mut GccContext) {
|
||||
use snap::write::FrameEncoder;
|
||||
use std::io::Write;
|
||||
|
||||
// Historical note:
|
||||
//
|
||||
// When using link.exe it was seen that the section name `.note.rustc`
|
||||
// was getting shortened to `.note.ru`, and according to the PE and COFF
|
||||
// specification:
|
||||
//
|
||||
// > Executable images do not use a string table and do not support
|
||||
// > section names longer than 8 characters
|
||||
//
|
||||
// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
|
||||
//
|
||||
// As a result, we choose a slightly shorter name! As to why
|
||||
// `.note.rustc` works on MinGW, see
|
||||
// https://github.com/llvm/llvm-project/blob/llvmorg-12.0.0/lld/COFF/Writer.cpp#L1190-L1197
|
||||
let section_name = if tcx.sess.target.is_like_osx { "__DATA,.rustc" } else { ".rustc" };
|
||||
|
||||
let context = &gcc_module.context;
|
||||
let mut compressed = rustc_metadata::METADATA_HEADER.to_vec();
|
||||
FrameEncoder::new(&mut compressed).write_all(&metadata.raw_data).unwrap();
|
||||
|
||||
let name = exported_symbols::metadata_symbol_name(tcx);
|
||||
let typ = context.new_array_type(None, context.new_type::<u8>(), compressed.len() as i32);
|
||||
let global = context.new_global(None, GlobalKind::Exported, typ, name);
|
||||
global.global_set_initializer(&compressed);
|
||||
global.set_link_section(section_name);
|
||||
|
||||
// Also generate a .section directive to force no
|
||||
// flags, at least for ELF outputs, so that the
|
||||
// metadata doesn't get loaded into memory.
|
||||
let directive = format!(".section {}", section_name);
|
||||
context.add_top_level_asm(None, &directive);
|
||||
}
|
||||
1812
compiler/rustc_codegen_gcc/src/builder.rs
Normal file
1812
compiler/rustc_codegen_gcc/src/builder.rs
Normal file
File diff suppressed because it is too large
Load Diff
99
compiler/rustc_codegen_gcc/src/callee.rs
Normal file
99
compiler/rustc_codegen_gcc/src/callee.rs
Normal file
@@ -0,0 +1,99 @@
|
||||
use gccjit::{FunctionType, RValue};
|
||||
use rustc_codegen_ssa::traits::BaseTypeMethods;
|
||||
use rustc_middle::ty::{Instance, TypeFoldable};
|
||||
use rustc_middle::ty::layout::{FnAbiExt, HasTyCtxt};
|
||||
use rustc_target::abi::call::FnAbi;
|
||||
|
||||
use crate::abi::FnAbiGccExt;
|
||||
use crate::context::CodegenCx;
|
||||
|
||||
/// Codegens a reference to a fn/method item, monomorphizing and
|
||||
/// inlining as it goes.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// - `cx`: the crate context
|
||||
/// - `instance`: the instance to be instantiated
|
||||
pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>) -> RValue<'gcc> {
|
||||
let tcx = cx.tcx();
|
||||
|
||||
//debug!("get_fn(instance={:?})", instance);
|
||||
|
||||
assert!(!instance.substs.needs_infer());
|
||||
assert!(!instance.substs.has_escaping_bound_vars());
|
||||
assert!(!instance.substs.has_param_types_or_consts());
|
||||
|
||||
if let Some(&func) = cx.instances.borrow().get(&instance) {
|
||||
return func;
|
||||
}
|
||||
|
||||
let sym = tcx.symbol_name(instance).name;
|
||||
//debug!("get_fn({:?}: {:?}) => {}", instance, instance.monomorphic_ty(cx.tcx()), sym);
|
||||
|
||||
let fn_abi = FnAbi::of_instance(cx, instance, &[]);
|
||||
|
||||
// TODO
|
||||
let func =
|
||||
if let Some(func) = cx.get_declared_value(&sym) {
|
||||
// Create a fn pointer with the new signature.
|
||||
let ptrty = fn_abi.ptr_to_gcc_type(cx);
|
||||
|
||||
// This is subtle and surprising, but sometimes we have to bitcast
|
||||
// the resulting fn pointer. The reason has to do with external
|
||||
// functions. If you have two crates that both bind the same C
|
||||
// library, they may not use precisely the same types: for
|
||||
// example, they will probably each declare their own structs,
|
||||
// which are distinct types from LLVM's point of view (nominal
|
||||
// types).
|
||||
//
|
||||
// Now, if those two crates are linked into an application, and
|
||||
// they contain inlined code, you can wind up with a situation
|
||||
// where both of those functions wind up being loaded into this
|
||||
// application simultaneously. In that case, the same function
|
||||
// (from LLVM's point of view) requires two types. But of course
|
||||
// LLVM won't allow one function to have two types.
|
||||
//
|
||||
// What we currently do, therefore, is declare the function with
|
||||
// one of the two types (whichever happens to come first) and then
|
||||
// bitcast as needed when the function is referenced to make sure
|
||||
// it has the type we expect.
|
||||
//
|
||||
// This can occur on either a crate-local or crate-external
|
||||
// reference. It also occurs when testing libcore and in some
|
||||
// other weird situations. Annoying.
|
||||
if cx.val_ty(func) != ptrty {
|
||||
//debug!("get_fn: casting {:?} to {:?}", func, ptrty);
|
||||
// TODO
|
||||
//cx.const_ptrcast(func, ptrty)
|
||||
func
|
||||
}
|
||||
else {
|
||||
//debug!("get_fn: not casting pointer!");
|
||||
func
|
||||
}
|
||||
}
|
||||
else {
|
||||
cx.linkage.set(FunctionType::Extern);
|
||||
let func = cx.declare_fn(&sym, &fn_abi);
|
||||
//cx.linkage.set(FunctionType::Internal);
|
||||
//debug!("get_fn: not casting pointer!");
|
||||
|
||||
// TODO
|
||||
//attributes::from_fn_attrs(cx, func, instance);
|
||||
|
||||
//let instance_def_id = instance.def_id();
|
||||
|
||||
// TODO
|
||||
/*if cx.use_dll_storage_attrs && tcx.is_dllimport_foreign_item(instance_def_id) {
|
||||
unsafe {
|
||||
llvm::LLVMSetDLLStorageClass(func, llvm::DLLStorageClass::DllImport);
|
||||
}
|
||||
}*/
|
||||
|
||||
func
|
||||
};
|
||||
|
||||
cx.instances.borrow_mut().insert(instance, func);
|
||||
|
||||
func
|
||||
}
|
||||
448
compiler/rustc_codegen_gcc/src/common.rs
Normal file
448
compiler/rustc_codegen_gcc/src/common.rs
Normal file
@@ -0,0 +1,448 @@
|
||||
use std::convert::TryFrom;
|
||||
use std::convert::TryInto;
|
||||
|
||||
use gccjit::{Block, CType, RValue, Type, ToRValue};
|
||||
use rustc_codegen_ssa::mir::place::PlaceRef;
|
||||
use rustc_codegen_ssa::traits::{
|
||||
BaseTypeMethods,
|
||||
ConstMethods,
|
||||
DerivedTypeMethods,
|
||||
MiscMethods,
|
||||
StaticMethods,
|
||||
};
|
||||
use rustc_middle::bug;
|
||||
use rustc_middle::mir::Mutability;
|
||||
use rustc_middle::ty::{layout::TyAndLayout, ScalarInt};
|
||||
use rustc_mir::interpret::{Allocation, GlobalAlloc, Scalar};
|
||||
use rustc_span::Symbol;
|
||||
use rustc_target::abi::{self, HasDataLayout, LayoutOf, Pointer, Size};
|
||||
|
||||
use crate::consts::const_alloc_to_gcc;
|
||||
use crate::context::CodegenCx;
|
||||
use crate::type_of::LayoutGccExt;
|
||||
|
||||
impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
|
||||
pub fn const_bytes(&self, bytes: &[u8]) -> RValue<'gcc> {
|
||||
bytes_in_context(self, bytes)
|
||||
}
|
||||
|
||||
fn const_cstr(&self, symbol: Symbol, _null_terminated: bool) -> RValue<'gcc> {
|
||||
// TODO: handle null_terminated.
|
||||
if let Some(&value) = self.const_cstr_cache.borrow().get(&symbol) {
|
||||
return value.to_rvalue();
|
||||
}
|
||||
|
||||
let global = self.global_string(&*symbol.as_str());
|
||||
|
||||
self.const_cstr_cache.borrow_mut().insert(symbol, global.dereference(None));
|
||||
global
|
||||
}
|
||||
|
||||
fn global_string(&self, string: &str) -> RValue<'gcc> {
|
||||
// TODO: handle non-null-terminated strings.
|
||||
let string = self.context.new_string_literal(&*string);
|
||||
let sym = self.generate_local_symbol_name("str");
|
||||
// NOTE: TLS is always off for a string litteral.
|
||||
// NOTE: string litterals do not have a link section.
|
||||
let global = self.define_global(&sym, self.val_ty(string), false, None)
|
||||
.unwrap_or_else(|| bug!("symbol `{}` is already defined", sym));
|
||||
self.global_init_block.add_assignment(None, global.dereference(None), string);
|
||||
global.to_rvalue()
|
||||
//llvm::LLVMRustSetLinkage(global, llvm::Linkage::InternalLinkage);
|
||||
}
|
||||
|
||||
pub fn inttoptr(&self, block: Block<'gcc>, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
|
||||
let func = block.get_function();
|
||||
let local = func.new_local(None, value.get_type(), "intLocal");
|
||||
block.add_assignment(None, local, value);
|
||||
let value_address = local.get_address(None);
|
||||
|
||||
let ptr = self.context.new_cast(None, value_address, dest_ty.make_pointer());
|
||||
ptr.dereference(None).to_rvalue()
|
||||
}
|
||||
|
||||
pub fn ptrtoint(&self, block: Block<'gcc>, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
|
||||
// TODO: when libgccjit allow casting from pointer to int, remove this.
|
||||
let func = block.get_function();
|
||||
let local = func.new_local(None, value.get_type(), "ptrLocal");
|
||||
block.add_assignment(None, local, value);
|
||||
let ptr_address = local.get_address(None);
|
||||
|
||||
let ptr = self.context.new_cast(None, ptr_address, dest_ty.make_pointer());
|
||||
ptr.dereference(None).to_rvalue()
|
||||
}
|
||||
|
||||
/*pub fn const_vector(&self, elements: &[RValue<'gcc>]) -> RValue<'gcc> {
|
||||
self.context.new_rvalue_from_vector(None, elements[0].get_type(), elements)
|
||||
}*/
|
||||
}
|
||||
|
||||
pub fn bytes_in_context<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, bytes: &[u8]) -> RValue<'gcc> {
|
||||
let context = &cx.context;
|
||||
let typ = context.new_array_type(None, context.new_type::<u8>(), bytes.len() as i32);
|
||||
let global = cx.declare_unnamed_global(typ);
|
||||
global.global_set_initializer(bytes);
|
||||
global.to_rvalue()
|
||||
}
|
||||
|
||||
pub fn type_is_pointer<'gcc>(typ: Type<'gcc>) -> bool {
|
||||
typ.get_pointee().is_some()
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
|
||||
fn const_null(&self, typ: Type<'gcc>) -> RValue<'gcc> {
|
||||
if type_is_pointer(typ) {
|
||||
self.context.new_null(typ)
|
||||
}
|
||||
else {
|
||||
self.const_int(typ, 0)
|
||||
}
|
||||
}
|
||||
|
||||
fn const_undef(&self, typ: Type<'gcc>) -> RValue<'gcc> {
|
||||
let local = self.current_func.borrow().expect("func")
|
||||
.new_local(None, typ, "undefined");
|
||||
if typ.is_struct().is_some() {
|
||||
// NOTE: hack to workaround a limitation of the rustc API: see comment on
|
||||
// CodegenCx.structs_as_pointer
|
||||
let pointer = local.get_address(None);
|
||||
self.structs_as_pointer.borrow_mut().insert(pointer);
|
||||
pointer
|
||||
}
|
||||
else {
|
||||
local.to_rvalue()
|
||||
}
|
||||
}
|
||||
|
||||
fn const_int(&self, typ: Type<'gcc>, int: i64) -> RValue<'gcc> {
|
||||
self.context.new_rvalue_from_long(typ, i64::try_from(int).expect("i64::try_from"))
|
||||
}
|
||||
|
||||
fn const_uint(&self, typ: Type<'gcc>, int: u64) -> RValue<'gcc> {
|
||||
self.context.new_rvalue_from_long(typ, u64::try_from(int).expect("u64::try_from") as i64)
|
||||
}
|
||||
|
||||
fn const_uint_big(&self, typ: Type<'gcc>, num: u128) -> RValue<'gcc> {
|
||||
let num64: Result<i64, _> = num.try_into();
|
||||
if let Ok(num) = num64 {
|
||||
// FIXME: workaround for a bug where libgccjit is expecting a constant.
|
||||
// The operations >> 64 and | low are making the normal case a non-constant.
|
||||
return self.context.new_rvalue_from_long(typ, num as i64);
|
||||
}
|
||||
|
||||
if num >> 64 != 0 {
|
||||
// FIXME: use a new function new_rvalue_from_unsigned_long()?
|
||||
let low = self.context.new_rvalue_from_long(self.u64_type, num as u64 as i64);
|
||||
let high = self.context.new_rvalue_from_long(typ, (num >> 64) as u64 as i64);
|
||||
|
||||
let sixty_four = self.context.new_rvalue_from_long(typ, 64);
|
||||
(high << sixty_four) | self.context.new_cast(None, low, typ)
|
||||
}
|
||||
else if typ.is_i128(self) {
|
||||
let num = self.context.new_rvalue_from_long(self.u64_type, num as u64 as i64);
|
||||
self.context.new_cast(None, num, typ)
|
||||
}
|
||||
else {
|
||||
self.context.new_rvalue_from_long(typ, num as u64 as i64)
|
||||
}
|
||||
}
|
||||
|
||||
fn const_bool(&self, val: bool) -> RValue<'gcc> {
|
||||
self.const_uint(self.type_i1(), val as u64)
|
||||
}
|
||||
|
||||
fn const_i32(&self, i: i32) -> RValue<'gcc> {
|
||||
self.const_int(self.type_i32(), i as i64)
|
||||
}
|
||||
|
||||
fn const_u32(&self, i: u32) -> RValue<'gcc> {
|
||||
self.const_uint(self.type_u32(), i as u64)
|
||||
}
|
||||
|
||||
fn const_u64(&self, i: u64) -> RValue<'gcc> {
|
||||
self.const_uint(self.type_u64(), i)
|
||||
}
|
||||
|
||||
fn const_usize(&self, i: u64) -> RValue<'gcc> {
|
||||
let bit_size = self.data_layout().pointer_size.bits();
|
||||
if bit_size < 64 {
|
||||
// make sure it doesn't overflow
|
||||
assert!(i < (1 << bit_size));
|
||||
}
|
||||
|
||||
self.const_uint(self.usize_type, i)
|
||||
}
|
||||
|
||||
fn const_u8(&self, _i: u8) -> RValue<'gcc> {
|
||||
unimplemented!();
|
||||
//self.const_uint(self.type_i8(), i as u64)
|
||||
}
|
||||
|
||||
fn const_real(&self, _t: Type<'gcc>, _val: f64) -> RValue<'gcc> {
|
||||
unimplemented!();
|
||||
//unsafe { llvm::LLVMConstReal(t, val) }
|
||||
}
|
||||
|
||||
fn const_str(&self, s: Symbol) -> (RValue<'gcc>, RValue<'gcc>) {
|
||||
let len = s.as_str().len();
|
||||
let cs = self.const_ptrcast(self.const_cstr(s, false),
|
||||
self.type_ptr_to(self.layout_of(self.tcx.types.str_).gcc_type(self, true)),
|
||||
);
|
||||
(cs, self.const_usize(len as u64))
|
||||
}
|
||||
|
||||
fn const_struct(&self, values: &[RValue<'gcc>], packed: bool) -> RValue<'gcc> {
|
||||
let fields: Vec<_> = values.iter()
|
||||
.map(|value| value.get_type())
|
||||
.collect();
|
||||
// TODO: cache the type? It's anonymous, so probably not.
|
||||
let name = fields.iter().map(|typ| format!("{:?}", typ)).collect::<Vec<_>>().join("_");
|
||||
let typ = self.type_struct(&fields, packed);
|
||||
let structure = self.global_init_func.new_local(None, typ, &name);
|
||||
let struct_type = typ.is_struct().expect("struct type");
|
||||
for (index, value) in values.iter().enumerate() {
|
||||
let field = struct_type.get_field(index as i32);
|
||||
let field_lvalue = structure.access_field(None, field);
|
||||
self.global_init_block.add_assignment(None, field_lvalue, *value);
|
||||
}
|
||||
self.lvalue_to_rvalue(structure)
|
||||
}
|
||||
|
||||
fn const_to_opt_uint(&self, _v: RValue<'gcc>) -> Option<u64> {
|
||||
// TODO
|
||||
None
|
||||
//try_as_const_integral(v).map(|v| unsafe { llvm::LLVMConstIntGetZExtValue(v) })
|
||||
}
|
||||
|
||||
fn const_to_opt_u128(&self, _v: RValue<'gcc>, _sign_ext: bool) -> Option<u128> {
|
||||
// TODO
|
||||
None
|
||||
/*try_as_const_integral(v).and_then(|v| unsafe {
|
||||
let (mut lo, mut hi) = (0u64, 0u64);
|
||||
let success = llvm::LLVMRustConstInt128Get(v, sign_ext, &mut hi, &mut lo);
|
||||
success.then_some(hi_lo_to_u128(lo, hi))
|
||||
})*/
|
||||
}
|
||||
|
||||
fn scalar_to_backend(&self, cv: Scalar, layout: &abi::Scalar, ty: Type<'gcc>) -> RValue<'gcc> {
|
||||
let bitsize = if layout.is_bool() { 1 } else { layout.value.size(self).bits() };
|
||||
match cv {
|
||||
Scalar::Int(ScalarInt::ZST) => {
|
||||
assert_eq!(0, layout.value.size(self).bytes());
|
||||
self.const_undef(self.type_ix(0))
|
||||
}
|
||||
Scalar::Int(int) => {
|
||||
let data = int.assert_bits(layout.value.size(self));
|
||||
|
||||
// FIXME: there's some issues with using the u128 code that follows, so hard-code
|
||||
// the paths for floating-point values.
|
||||
if ty == self.float_type {
|
||||
return self.context.new_rvalue_from_double(ty, f32::from_bits(data as u32) as f64);
|
||||
}
|
||||
else if ty == self.double_type {
|
||||
return self.context.new_rvalue_from_double(ty, f64::from_bits(data as u64));
|
||||
}
|
||||
|
||||
let value = self.const_uint_big(self.type_ix(bitsize), data);
|
||||
if layout.value == Pointer {
|
||||
self.inttoptr(self.current_block.borrow().expect("block"), value, ty)
|
||||
} else {
|
||||
self.const_bitcast(value, ty)
|
||||
}
|
||||
}
|
||||
Scalar::Ptr(ptr, _size) => {
|
||||
let (alloc_id, offset) = ptr.into_parts();
|
||||
let base_addr =
|
||||
match self.tcx.global_alloc(alloc_id) {
|
||||
GlobalAlloc::Memory(alloc) => {
|
||||
let init = const_alloc_to_gcc(self, alloc);
|
||||
let value =
|
||||
match alloc.mutability {
|
||||
Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
|
||||
_ => self.static_addr_of(init, alloc.align, None),
|
||||
};
|
||||
if !self.sess().fewer_names() {
|
||||
// TODO
|
||||
//llvm::set_value_name(value, format!("{:?}", ptr.alloc_id).as_bytes());
|
||||
}
|
||||
value
|
||||
},
|
||||
GlobalAlloc::Function(fn_instance) => {
|
||||
self.get_fn_addr(fn_instance)
|
||||
},
|
||||
GlobalAlloc::Static(def_id) => {
|
||||
assert!(self.tcx.is_static(def_id));
|
||||
self.get_static(def_id)
|
||||
},
|
||||
};
|
||||
let ptr_type = base_addr.get_type();
|
||||
let base_addr = self.const_bitcast(base_addr, self.usize_type);
|
||||
let offset = self.context.new_rvalue_from_long(self.usize_type, offset.bytes() as i64);
|
||||
let ptr = self.const_bitcast(base_addr + offset, ptr_type);
|
||||
let value = ptr.dereference(None);
|
||||
if layout.value != Pointer {
|
||||
self.const_bitcast(value.to_rvalue(), ty)
|
||||
}
|
||||
else {
|
||||
self.const_bitcast(value.get_address(None), ty)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn const_data_from_alloc(&self, alloc: &Allocation) -> Self::Value {
|
||||
const_alloc_to_gcc(self, alloc)
|
||||
}
|
||||
|
||||
fn from_const_alloc(&self, layout: TyAndLayout<'tcx>, alloc: &Allocation, offset: Size) -> PlaceRef<'tcx, RValue<'gcc>> {
|
||||
assert_eq!(alloc.align, layout.align.abi);
|
||||
let ty = self.type_ptr_to(layout.gcc_type(self, true));
|
||||
let value =
|
||||
if layout.size == Size::ZERO {
|
||||
let value = self.const_usize(alloc.align.bytes());
|
||||
self.context.new_cast(None, value, ty)
|
||||
}
|
||||
else {
|
||||
let init = const_alloc_to_gcc(self, alloc);
|
||||
let base_addr = self.static_addr_of(init, alloc.align, None);
|
||||
|
||||
let array = self.const_bitcast(base_addr, self.type_i8p());
|
||||
let value = self.context.new_array_access(None, array, self.const_usize(offset.bytes())).get_address(None);
|
||||
self.const_bitcast(value, ty)
|
||||
};
|
||||
PlaceRef::new_sized(value, layout)
|
||||
}
|
||||
|
||||
fn const_ptrcast(&self, val: RValue<'gcc>, ty: Type<'gcc>) -> RValue<'gcc> {
|
||||
self.context.new_cast(None, val, ty)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SignType<'gcc, 'tcx> {
|
||||
fn is_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
fn is_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
fn to_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> SignType<'gcc, 'tcx> for Type<'gcc> {
|
||||
fn is_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.is_i8(cx) || self.is_i16(cx) || self.is_i32(cx) || self.is_i64(cx) || self.is_i128(cx)
|
||||
}
|
||||
|
||||
fn is_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.is_u8(cx) || self.is_u16(cx) || self.is_u32(cx) || self.is_u64(cx) || self.is_u128(cx)
|
||||
}
|
||||
|
||||
fn to_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
|
||||
if self.is_u8(cx) {
|
||||
cx.i8_type
|
||||
}
|
||||
else if self.is_u16(cx) {
|
||||
cx.i16_type
|
||||
}
|
||||
else if self.is_u32(cx) {
|
||||
cx.i32_type
|
||||
}
|
||||
else if self.is_u64(cx) {
|
||||
cx.i64_type
|
||||
}
|
||||
else if self.is_u128(cx) {
|
||||
cx.i128_type
|
||||
}
|
||||
else {
|
||||
self.clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TypeReflection<'gcc, 'tcx> {
|
||||
fn is_uchar(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
fn is_ushort(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
fn is_uint(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
fn is_ulong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
fn is_ulonglong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
|
||||
fn is_i8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
fn is_u8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
fn is_i16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
fn is_u16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
fn is_i32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
fn is_u32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
fn is_i64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
fn is_u64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
fn is_i128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
fn is_u128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
|
||||
fn is_f32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
fn is_f64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> TypeReflection<'gcc, 'tcx> for Type<'gcc> {
|
||||
fn is_uchar(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.u8_type
|
||||
}
|
||||
|
||||
fn is_ushort(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.u16_type
|
||||
}
|
||||
|
||||
fn is_uint(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.uint_type
|
||||
}
|
||||
|
||||
fn is_ulong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.ulong_type
|
||||
}
|
||||
|
||||
fn is_ulonglong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.ulonglong_type
|
||||
}
|
||||
|
||||
fn is_i8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.i8_type
|
||||
}
|
||||
|
||||
fn is_u8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.u8_type
|
||||
}
|
||||
|
||||
fn is_i16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.i16_type
|
||||
}
|
||||
|
||||
fn is_u16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.u16_type
|
||||
}
|
||||
|
||||
fn is_i32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.i32_type
|
||||
}
|
||||
|
||||
fn is_u32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.u32_type
|
||||
}
|
||||
|
||||
fn is_i64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.i64_type
|
||||
}
|
||||
|
||||
fn is_u64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.u64_type
|
||||
}
|
||||
|
||||
fn is_i128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.context.new_c_type(CType::Int128t)
|
||||
}
|
||||
|
||||
fn is_u128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.context.new_c_type(CType::UInt128t)
|
||||
}
|
||||
|
||||
fn is_f32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.context.new_type::<f32>()
|
||||
}
|
||||
|
||||
fn is_f64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
|
||||
self.unqualified() == cx.context.new_type::<f64>()
|
||||
}
|
||||
}
|
||||
527
compiler/rustc_codegen_gcc/src/consts.rs
Normal file
527
compiler/rustc_codegen_gcc/src/consts.rs
Normal file
@@ -0,0 +1,527 @@
|
||||
use gccjit::{RValue, Type};
|
||||
use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods, DerivedTypeMethods, StaticMethods};
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::Node;
|
||||
use rustc_middle::{bug, span_bug};
|
||||
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
|
||||
use rustc_middle::mir::mono::MonoItem;
|
||||
use rustc_middle::ty::{self, Instance, Ty};
|
||||
use rustc_mir::interpret::{self, Allocation, ErrorHandled, Scalar as InterpScalar, read_target_uint};
|
||||
use rustc_span::Span;
|
||||
use rustc_span::def_id::DefId;
|
||||
use rustc_target::abi::{self, Align, HasDataLayout, LayoutOf, Primitive, Size};
|
||||
|
||||
use crate::base;
|
||||
use crate::context::CodegenCx;
|
||||
use crate::mangled_std_symbols::{ARGC, ARGV, ARGV_INIT_ARRAY};
|
||||
use crate::type_of::LayoutGccExt;
|
||||
|
||||
impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
|
||||
pub fn const_bitcast(&self, value: RValue<'gcc>, typ: Type<'gcc>) -> RValue<'gcc> {
|
||||
if value.get_type() == self.bool_type.make_pointer() {
|
||||
if let Some(pointee) = typ.get_pointee() {
|
||||
if pointee.is_vector().is_some() {
|
||||
panic!()
|
||||
}
|
||||
}
|
||||
}
|
||||
self.context.new_bitcast(None, value, typ)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> StaticMethods for CodegenCx<'gcc, 'tcx> {
|
||||
fn static_addr_of(&self, cv: RValue<'gcc>, align: Align, kind: Option<&str>) -> RValue<'gcc> {
|
||||
if let Some(global_value) = self.const_globals.borrow().get(&cv) {
|
||||
// TODO
|
||||
/*unsafe {
|
||||
// Upgrade the alignment in cases where the same constant is used with different
|
||||
// alignment requirements
|
||||
let llalign = align.bytes() as u32;
|
||||
if llalign > llvm::LLVMGetAlignment(gv) {
|
||||
llvm::LLVMSetAlignment(gv, llalign);
|
||||
}
|
||||
}*/
|
||||
return *global_value;
|
||||
}
|
||||
let global_value = self.static_addr_of_mut(cv, align, kind);
|
||||
// TODO
|
||||
/*unsafe {
|
||||
llvm::LLVMSetGlobalConstant(global_value, True);
|
||||
}*/
|
||||
self.const_globals.borrow_mut().insert(cv, global_value);
|
||||
global_value
|
||||
}
|
||||
|
||||
fn codegen_static(&self, def_id: DefId, is_mutable: bool) {
|
||||
let attrs = self.tcx.codegen_fn_attrs(def_id);
|
||||
|
||||
let instance = Instance::mono(self.tcx, def_id);
|
||||
let name = &*self.tcx.symbol_name(instance).name;
|
||||
|
||||
let (value, alloc) =
|
||||
match codegen_static_initializer(&self, def_id) {
|
||||
Ok(value) => value,
|
||||
// Error has already been reported
|
||||
Err(_) => return,
|
||||
};
|
||||
|
||||
let is_tls = attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
|
||||
let global = self.get_static(def_id);
|
||||
|
||||
// boolean SSA values are i1, but they have to be stored in i8 slots,
|
||||
// otherwise some LLVM optimization passes don't work as expected
|
||||
let val_llty = self.val_ty(value);
|
||||
let value =
|
||||
if val_llty == self.type_i1() {
|
||||
//val_llty = self.type_i8();
|
||||
unimplemented!();
|
||||
//llvm::LLVMConstZExt(value, val_llty)
|
||||
}
|
||||
else {
|
||||
value
|
||||
};
|
||||
|
||||
let instance = Instance::mono(self.tcx, def_id);
|
||||
let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
|
||||
let gcc_type = self.layout_of(ty).gcc_type(self, true);
|
||||
|
||||
let global =
|
||||
if val_llty == gcc_type {
|
||||
global
|
||||
}
|
||||
else {
|
||||
// If we created the global with the wrong type,
|
||||
// correct the type.
|
||||
/*let name = llvm::get_value_name(global).to_vec();
|
||||
llvm::set_value_name(global, b"");
|
||||
|
||||
let linkage = llvm::LLVMRustGetLinkage(global);
|
||||
let visibility = llvm::LLVMRustGetVisibility(global);*/
|
||||
|
||||
let new_global = self.get_or_insert_global(&name, val_llty, is_tls, attrs.link_section);
|
||||
|
||||
/*llvm::LLVMRustSetLinkage(new_global, linkage);
|
||||
llvm::LLVMRustSetVisibility(new_global, visibility);*/
|
||||
|
||||
// To avoid breaking any invariants, we leave around the old
|
||||
// global for the moment; we'll replace all references to it
|
||||
// with the new global later. (See base::codegen_backend.)
|
||||
//self.statics_to_rauw.borrow_mut().push((global, new_global));
|
||||
new_global
|
||||
};
|
||||
// TODO
|
||||
//set_global_alignment(&self, global, self.align_of(ty));
|
||||
//llvm::LLVMSetInitializer(global, value);
|
||||
let value = self.rvalue_as_lvalue(value);
|
||||
let value = value.get_address(None);
|
||||
let dest_typ = global.get_type();
|
||||
let value = self.context.new_cast(None, value, dest_typ);
|
||||
|
||||
// NOTE: do not init the variables related to argc/argv because it seems we cannot
|
||||
// overwrite those variables.
|
||||
// FIXME: correctly support global variable initialization.
|
||||
let skip_init = [
|
||||
ARGV_INIT_ARRAY,
|
||||
ARGC,
|
||||
ARGV,
|
||||
];
|
||||
if !skip_init.iter().any(|symbol_name| name.starts_with(symbol_name)) {
|
||||
// TODO: switch to set_initializer when libgccjit supports that.
|
||||
let memcpy = self.context.get_builtin_function("memcpy");
|
||||
let dst = self.context.new_cast(None, global, self.type_i8p());
|
||||
let src = self.context.new_cast(None, value, self.type_ptr_to(self.type_void()));
|
||||
let size = self.context.new_rvalue_from_long(self.sizet_type, alloc.size().bytes() as i64);
|
||||
self.global_init_block.add_eval(None, self.context.new_call(None, memcpy, &[dst, src, size]));
|
||||
}
|
||||
|
||||
// As an optimization, all shared statics which do not have interior
|
||||
// mutability are placed into read-only memory.
|
||||
if !is_mutable {
|
||||
if self.type_is_freeze(ty) {
|
||||
// TODO
|
||||
//llvm::LLVMSetGlobalConstant(global, llvm::True);
|
||||
}
|
||||
}
|
||||
|
||||
//debuginfo::create_global_var_metadata(&self, def_id, global);
|
||||
|
||||
if attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
|
||||
// Do not allow LLVM to change the alignment of a TLS on macOS.
|
||||
//
|
||||
// By default a global's alignment can be freely increased.
|
||||
// This allows LLVM to generate more performant instructions
|
||||
// e.g., using load-aligned into a SIMD register.
|
||||
//
|
||||
// However, on macOS 10.10 or below, the dynamic linker does not
|
||||
// respect any alignment given on the TLS (radar 24221680).
|
||||
// This will violate the alignment assumption, and causing segfault at runtime.
|
||||
//
|
||||
// This bug is very easy to trigger. In `println!` and `panic!`,
|
||||
// the `LOCAL_STDOUT`/`LOCAL_STDERR` handles are stored in a TLS,
|
||||
// which the values would be `mem::replace`d on initialization.
|
||||
// The implementation of `mem::replace` will use SIMD
|
||||
// whenever the size is 32 bytes or higher. LLVM notices SIMD is used
|
||||
// and tries to align `LOCAL_STDOUT`/`LOCAL_STDERR` to a 32-byte boundary,
|
||||
// which macOS's dyld disregarded and causing crashes
|
||||
// (see issues #51794, #51758, #50867, #48866 and #44056).
|
||||
//
|
||||
// To workaround the bug, we trick LLVM into not increasing
|
||||
// the global's alignment by explicitly assigning a section to it
|
||||
// (equivalent to automatically generating a `#[link_section]` attribute).
|
||||
// See the comment in the `GlobalValue::canIncreaseAlignment()` function
|
||||
// of `lib/IR/Globals.cpp` for why this works.
|
||||
//
|
||||
// When the alignment is not increased, the optimized `mem::replace`
|
||||
// will use load-unaligned instructions instead, and thus avoiding the crash.
|
||||
//
|
||||
// We could remove this hack whenever we decide to drop macOS 10.10 support.
|
||||
if self.tcx.sess.target.options.is_like_osx {
|
||||
// The `inspect` method is okay here because we checked relocations, and
|
||||
// because we are doing this access to inspect the final interpreter state
|
||||
// (not as part of the interpreter execution).
|
||||
//
|
||||
// FIXME: This check requires that the (arbitrary) value of undefined bytes
|
||||
// happens to be zero. Instead, we should only check the value of defined bytes
|
||||
// and set all undefined bytes to zero if this allocation is headed for the
|
||||
// BSS.
|
||||
/*let all_bytes_are_zero = alloc.relocations().is_empty()
|
||||
&& alloc
|
||||
.inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len())
|
||||
.iter()
|
||||
.all(|&byte| byte == 0);
|
||||
|
||||
let sect_name = if all_bytes_are_zero {
|
||||
CStr::from_bytes_with_nul_unchecked(b"__DATA,__thread_bss\0")
|
||||
} else {
|
||||
CStr::from_bytes_with_nul_unchecked(b"__DATA,__thread_data\0")
|
||||
};*/
|
||||
unimplemented!();
|
||||
//llvm::LLVMSetSection(global, sect_name.as_ptr());
|
||||
}
|
||||
}
|
||||
|
||||
// Wasm statics with custom link sections get special treatment as they
|
||||
// go into custom sections of the wasm executable.
|
||||
if self.tcx.sess.opts.target_triple.triple().starts_with("wasm32") {
|
||||
if let Some(_section) = attrs.link_section {
|
||||
unimplemented!();
|
||||
/*let section = llvm::LLVMMDStringInContext(
|
||||
self.llcx,
|
||||
section.as_str().as_ptr().cast(),
|
||||
section.as_str().len() as c_uint,
|
||||
);
|
||||
assert!(alloc.relocations().is_empty());
|
||||
|
||||
// The `inspect` method is okay here because we checked relocations, and
|
||||
// because we are doing this access to inspect the final interpreter state (not
|
||||
// as part of the interpreter execution).
|
||||
let bytes =
|
||||
alloc.inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len());
|
||||
let alloc = llvm::LLVMMDStringInContext(
|
||||
self.llcx,
|
||||
bytes.as_ptr().cast(),
|
||||
bytes.len() as c_uint,
|
||||
);
|
||||
let data = [section, alloc];
|
||||
let meta = llvm::LLVMMDNodeInContext(self.llcx, data.as_ptr(), 2);
|
||||
llvm::LLVMAddNamedMetadataOperand(
|
||||
self.llmod,
|
||||
"wasm.custom_sections\0".as_ptr().cast(),
|
||||
meta,
|
||||
);*/
|
||||
}
|
||||
} else {
|
||||
// TODO
|
||||
//base::set_link_section(global, &attrs);
|
||||
}
|
||||
|
||||
if attrs.flags.contains(CodegenFnAttrFlags::USED) {
|
||||
self.add_used_global(global);
|
||||
}
|
||||
}
|
||||
|
||||
/// Add a global value to a list to be stored in the `llvm.used` variable, an array of i8*.
|
||||
fn add_used_global(&self, _global: RValue<'gcc>) {
|
||||
// TODO
|
||||
//let cast = self.context.new_cast(None, global, self.type_i8p());
|
||||
//self.used_statics.borrow_mut().push(cast);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
|
||||
pub fn static_addr_of_mut(&self, cv: RValue<'gcc>, align: Align, kind: Option<&str>) -> RValue<'gcc> {
|
||||
let (name, gv) =
|
||||
match kind {
|
||||
Some(kind) if !self.tcx.sess.fewer_names() => {
|
||||
let name = self.generate_local_symbol_name(kind);
|
||||
// TODO: check if it's okay that TLS is off here.
|
||||
// TODO: check if it's okay that link_section is None here.
|
||||
// TODO: set alignment here as well.
|
||||
let gv = self.define_global(&name[..], self.val_ty(cv), false, None).unwrap_or_else(|| {
|
||||
bug!("symbol `{}` is already defined", name);
|
||||
});
|
||||
//llvm::LLVMRustSetLinkage(gv, llvm::Linkage::PrivateLinkage);
|
||||
(name, gv)
|
||||
}
|
||||
_ => {
|
||||
let index = self.global_gen_sym_counter.get();
|
||||
let name = format!("global_{}_{}", index, self.codegen_unit.name());
|
||||
let typ = self.val_ty(cv).get_aligned(align.bytes());
|
||||
let global = self.define_private_global(typ);
|
||||
(name, global)
|
||||
},
|
||||
};
|
||||
// FIXME: I think the name coming from generate_local_symbol_name() above cannot be used
|
||||
// globally.
|
||||
// NOTE: global seems to only be global in a module. So save the name instead of the value
|
||||
// to import it later.
|
||||
self.global_names.borrow_mut().insert(cv, name);
|
||||
self.global_init_block.add_assignment(None, gv.dereference(None), cv);
|
||||
//llvm::SetUnnamedAddress(gv, llvm::UnnamedAddr::Global);
|
||||
gv
|
||||
}
|
||||
|
||||
pub fn get_static(&self, def_id: DefId) -> RValue<'gcc> {
|
||||
let instance = Instance::mono(self.tcx, def_id);
|
||||
let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
|
||||
if let Some(&global) = self.instances.borrow().get(&instance) {
|
||||
/*let attrs = self.tcx.codegen_fn_attrs(def_id);
|
||||
let name = &*self.tcx.symbol_name(instance).name;
|
||||
let name =
|
||||
if let Some(linkage) = attrs.linkage {
|
||||
// This is to match what happens in check_and_apply_linkage.
|
||||
Cow::from(format!("_rust_extern_with_linkage_{}", name))
|
||||
}
|
||||
else {
|
||||
Cow::from(name)
|
||||
};
|
||||
let global = self.context.new_global(None, GlobalKind::Imported, global.get_type(), &name)
|
||||
.get_address(None);
|
||||
self.global_names.borrow_mut().insert(global, name.to_string());*/
|
||||
return global;
|
||||
}
|
||||
|
||||
let defined_in_current_codegen_unit =
|
||||
self.codegen_unit.items().contains_key(&MonoItem::Static(def_id));
|
||||
assert!(
|
||||
!defined_in_current_codegen_unit,
|
||||
"consts::get_static() should always hit the cache for \
|
||||
statics defined in the same CGU, but did not for `{:?}`",
|
||||
def_id
|
||||
);
|
||||
|
||||
let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
|
||||
let sym = self.tcx.symbol_name(instance).name;
|
||||
|
||||
//debug!("get_static: sym={} instance={:?}", sym, instance);
|
||||
|
||||
let global =
|
||||
if let Some(def_id) = def_id.as_local() {
|
||||
let id = self.tcx.hir().local_def_id_to_hir_id(def_id);
|
||||
let llty = self.layout_of(ty).gcc_type(self, true);
|
||||
// FIXME: refactor this to work without accessing the HIR
|
||||
let global = match self.tcx.hir().get(id) {
|
||||
Node::Item(&hir::Item { span, kind: hir::ItemKind::Static(..), .. }) => {
|
||||
if let Some(global) = self.get_declared_value(&sym) {
|
||||
if self.val_ty(global) != self.type_ptr_to(llty) {
|
||||
span_bug!(span, "Conflicting types for static");
|
||||
}
|
||||
}
|
||||
|
||||
let is_tls = fn_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
|
||||
let global = self.declare_global(&sym, llty, is_tls, fn_attrs.link_section);
|
||||
|
||||
if !self.tcx.is_reachable_non_generic(def_id) {
|
||||
/*unsafe {
|
||||
llvm::LLVMRustSetVisibility(global, llvm::Visibility::Hidden);
|
||||
}*/
|
||||
}
|
||||
|
||||
global
|
||||
}
|
||||
|
||||
Node::ForeignItem(&hir::ForeignItem {
|
||||
span,
|
||||
kind: hir::ForeignItemKind::Static(..),
|
||||
..
|
||||
}) => {
|
||||
let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
|
||||
check_and_apply_linkage(&self, &fn_attrs, ty, sym, span)
|
||||
}
|
||||
|
||||
item => bug!("get_static: expected static, found {:?}", item),
|
||||
};
|
||||
|
||||
//debug!("get_static: sym={} attrs={:?}", sym, attrs);
|
||||
|
||||
global
|
||||
}
|
||||
else {
|
||||
// FIXME(nagisa): perhaps the map of externs could be offloaded to llvm somehow?
|
||||
//debug!("get_static: sym={} item_attr={:?}", sym, self.tcx.item_attrs(def_id));
|
||||
|
||||
let attrs = self.tcx.codegen_fn_attrs(def_id);
|
||||
let span = self.tcx.def_span(def_id);
|
||||
let global = check_and_apply_linkage(&self, &attrs, ty, sym, span);
|
||||
|
||||
let needs_dll_storage_attr = false; /*self.use_dll_storage_attrs && !self.tcx.is_foreign_item(def_id) &&
|
||||
// ThinLTO can't handle this workaround in all cases, so we don't
|
||||
// emit the attrs. Instead we make them unnecessary by disallowing
|
||||
// dynamic linking when linker plugin based LTO is enabled.
|
||||
!self.tcx.sess.opts.cg.linker_plugin_lto.enabled();*/
|
||||
|
||||
// If this assertion triggers, there's something wrong with commandline
|
||||
// argument validation.
|
||||
debug_assert!(
|
||||
!(self.tcx.sess.opts.cg.linker_plugin_lto.enabled()
|
||||
&& self.tcx.sess.target.options.is_like_msvc
|
||||
&& self.tcx.sess.opts.cg.prefer_dynamic)
|
||||
);
|
||||
|
||||
if needs_dll_storage_attr {
|
||||
// This item is external but not foreign, i.e., it originates from an external Rust
|
||||
// crate. Since we don't know whether this crate will be linked dynamically or
|
||||
// statically in the final application, we always mark such symbols as 'dllimport'.
|
||||
// If final linkage happens to be static, we rely on compiler-emitted __imp_ stubs
|
||||
// to make things work.
|
||||
//
|
||||
// However, in some scenarios we defer emission of statics to downstream
|
||||
// crates, so there are cases where a static with an upstream DefId
|
||||
// is actually present in the current crate. We can find out via the
|
||||
// is_codegened_item query.
|
||||
if !self.tcx.is_codegened_item(def_id) {
|
||||
unimplemented!();
|
||||
/*unsafe {
|
||||
llvm::LLVMSetDLLStorageClass(global, llvm::DLLStorageClass::DllImport);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
global
|
||||
};
|
||||
|
||||
/*if self.use_dll_storage_attrs && self.tcx.is_dllimport_foreign_item(def_id) {
|
||||
// For foreign (native) libs we know the exact storage type to use.
|
||||
unsafe {
|
||||
llvm::LLVMSetDLLStorageClass(global, llvm::DLLStorageClass::DllImport);
|
||||
}
|
||||
}*/
|
||||
|
||||
self.instances.borrow_mut().insert(instance, global);
|
||||
global
|
||||
}
|
||||
}
|
||||
|
||||
pub fn const_alloc_to_gcc<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, alloc: &Allocation) -> RValue<'gcc> {
|
||||
let mut llvals = Vec::with_capacity(alloc.relocations().len() + 1);
|
||||
let dl = cx.data_layout();
|
||||
let pointer_size = dl.pointer_size.bytes() as usize;
|
||||
|
||||
let mut next_offset = 0;
|
||||
for &(offset, alloc_id) in alloc.relocations().iter() {
|
||||
let offset = offset.bytes();
|
||||
assert_eq!(offset as usize as u64, offset);
|
||||
let offset = offset as usize;
|
||||
if offset > next_offset {
|
||||
// This `inspect` is okay since we have checked that it is not within a relocation, it
|
||||
// is within the bounds of the allocation, and it doesn't affect interpreter execution
|
||||
// (we inspect the result after interpreter execution). Any undef byte is replaced with
|
||||
// some arbitrary byte value.
|
||||
//
|
||||
// FIXME: relay undef bytes to codegen as undef const bytes
|
||||
let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(next_offset..offset);
|
||||
llvals.push(cx.const_bytes(bytes));
|
||||
}
|
||||
let ptr_offset =
|
||||
read_target_uint( dl.endian,
|
||||
// This `inspect` is okay since it is within the bounds of the allocation, it doesn't
|
||||
// affect interpreter execution (we inspect the result after interpreter execution),
|
||||
// and we properly interpret the relocation as a relocation pointer offset.
|
||||
alloc.inspect_with_uninit_and_ptr_outside_interpreter(offset..(offset + pointer_size)),
|
||||
)
|
||||
.expect("const_alloc_to_llvm: could not read relocation pointer")
|
||||
as u64;
|
||||
llvals.push(cx.scalar_to_backend(
|
||||
InterpScalar::from_pointer(
|
||||
interpret::Pointer::new(alloc_id, Size::from_bytes(ptr_offset)),
|
||||
&cx.tcx,
|
||||
),
|
||||
&abi::Scalar { value: Primitive::Pointer, valid_range: 0..=!0 },
|
||||
cx.type_i8p(),
|
||||
));
|
||||
next_offset = offset + pointer_size;
|
||||
}
|
||||
if alloc.len() >= next_offset {
|
||||
let range = next_offset..alloc.len();
|
||||
// This `inspect` is okay since we have check that it is after all relocations, it is
|
||||
// within the bounds of the allocation, and it doesn't affect interpreter execution (we
|
||||
// inspect the result after interpreter execution). Any undef byte is replaced with some
|
||||
// arbitrary byte value.
|
||||
//
|
||||
// FIXME: relay undef bytes to codegen as undef const bytes
|
||||
let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(range);
|
||||
llvals.push(cx.const_bytes(bytes));
|
||||
}
|
||||
|
||||
cx.const_struct(&llvals, true)
|
||||
}
|
||||
|
||||
pub fn codegen_static_initializer<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, def_id: DefId) -> Result<(RValue<'gcc>, &'tcx Allocation), ErrorHandled> {
|
||||
let alloc = cx.tcx.eval_static_initializer(def_id)?;
|
||||
Ok((const_alloc_to_gcc(cx, alloc), alloc))
|
||||
}
|
||||
|
||||
fn check_and_apply_linkage<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, attrs: &CodegenFnAttrs, ty: Ty<'tcx>, sym: &str, span: Span) -> RValue<'gcc> {
|
||||
let is_tls = attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
|
||||
let llty = cx.layout_of(ty).gcc_type(cx, true);
|
||||
if let Some(linkage) = attrs.linkage {
|
||||
//debug!("get_static: sym={} linkage={:?}", sym, linkage);
|
||||
|
||||
// If this is a static with a linkage specified, then we need to handle
|
||||
// it a little specially. The typesystem prevents things like &T and
|
||||
// extern "C" fn() from being non-null, so we can't just declare a
|
||||
// static and call it a day. Some linkages (like weak) will make it such
|
||||
// that the static actually has a null value.
|
||||
let llty2 =
|
||||
if let ty::RawPtr(ref mt) = ty.kind() {
|
||||
cx.layout_of(mt.ty).gcc_type(cx, true)
|
||||
}
|
||||
else {
|
||||
cx.sess().span_fatal(
|
||||
span,
|
||||
"must have type `*const T` or `*mut T` due to `#[linkage]` attribute",
|
||||
)
|
||||
};
|
||||
// Declare a symbol `foo` with the desired linkage.
|
||||
let global1 = cx.declare_global_with_linkage(&sym, llty2, base::global_linkage_to_gcc(linkage));
|
||||
|
||||
// Declare an internal global `extern_with_linkage_foo` which
|
||||
// is initialized with the address of `foo`. If `foo` is
|
||||
// discarded during linking (for example, if `foo` has weak
|
||||
// linkage and there are no definitions), then
|
||||
// `extern_with_linkage_foo` will instead be initialized to
|
||||
// zero.
|
||||
let mut real_name = "_rust_extern_with_linkage_".to_string();
|
||||
real_name.push_str(&sym);
|
||||
let global2 =
|
||||
cx.define_global(&real_name, llty, is_tls, attrs.link_section).unwrap_or_else(|| {
|
||||
cx.sess().span_fatal(span, &format!("symbol `{}` is already defined", &sym))
|
||||
});
|
||||
//llvm::LLVMRustSetLinkage(global2, llvm::Linkage::InternalLinkage);
|
||||
let lvalue = global2.dereference(None);
|
||||
cx.global_init_block.add_assignment(None, lvalue, global1);
|
||||
//llvm::LLVMSetInitializer(global2, global1);
|
||||
global2
|
||||
}
|
||||
else {
|
||||
// Generate an external declaration.
|
||||
// FIXME(nagisa): investigate whether it can be changed into define_global
|
||||
|
||||
// Thread-local statics in some other crate need to *always* be linked
|
||||
// against in a thread-local fashion, so we need to be sure to apply the
|
||||
// thread-local attribute locally if it was present remotely. If we
|
||||
// don't do this then linker errors can be generated where the linker
|
||||
// complains that one object files has a thread local version of the
|
||||
// symbol and another one doesn't.
|
||||
cx.declare_global(&sym, llty, is_tls, attrs.link_section)
|
||||
}
|
||||
}
|
||||
491
compiler/rustc_codegen_gcc/src/context.rs
Normal file
491
compiler/rustc_codegen_gcc/src/context.rs
Normal file
@@ -0,0 +1,491 @@
|
||||
use std::cell::{Cell, RefCell};
|
||||
|
||||
use gccjit::{
|
||||
Block,
|
||||
Context,
|
||||
CType,
|
||||
Function,
|
||||
FunctionType,
|
||||
LValue,
|
||||
RValue,
|
||||
Struct,
|
||||
Type,
|
||||
};
|
||||
use rustc_codegen_ssa::base::wants_msvc_seh;
|
||||
use rustc_codegen_ssa::traits::{
|
||||
BackendTypes,
|
||||
BaseTypeMethods,
|
||||
MiscMethods,
|
||||
};
|
||||
use rustc_data_structures::base_n;
|
||||
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
||||
use rustc_middle::bug;
|
||||
use rustc_middle::mir::mono::CodegenUnit;
|
||||
use rustc_middle::ty::{self, Instance, ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt};
|
||||
use rustc_middle::ty::layout::{HasParamEnv, HasTyCtxt, LayoutError, TyAndLayout};
|
||||
use rustc_session::Session;
|
||||
use rustc_span::{Span, Symbol, DUMMY_SP};
|
||||
use rustc_target::abi::{HasDataLayout, LayoutOf, PointeeInfo, Size, TargetDataLayout, VariantIdx};
|
||||
use rustc_target::spec::{HasTargetSpec, Target, TlsModel};
|
||||
|
||||
use crate::callee::get_fn;
|
||||
use crate::declare::mangle_name;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FuncSig<'gcc> {
|
||||
pub params: Vec<Type<'gcc>>,
|
||||
pub return_type: Type<'gcc>,
|
||||
}
|
||||
|
||||
pub struct CodegenCx<'gcc, 'tcx> {
|
||||
pub check_overflow: bool,
|
||||
pub codegen_unit: &'tcx CodegenUnit<'tcx>,
|
||||
pub context: &'gcc Context<'gcc>,
|
||||
|
||||
// TODO: First set it to a dummy block to avoid using Option?
|
||||
pub current_block: RefCell<Option<Block<'gcc>>>,
|
||||
pub current_func: RefCell<Option<Function<'gcc>>>,
|
||||
pub normal_function_addresses: RefCell<FxHashSet<RValue<'gcc>>>,
|
||||
|
||||
/// The function where globals are initialized.
|
||||
pub global_init_func: Function<'gcc>,
|
||||
pub global_init_block: Block<'gcc>,
|
||||
|
||||
pub functions: RefCell<FxHashMap<String, Function<'gcc>>>,
|
||||
|
||||
pub tls_model: gccjit::TlsModel,
|
||||
|
||||
pub bool_type: Type<'gcc>,
|
||||
pub i8_type: Type<'gcc>,
|
||||
pub i16_type: Type<'gcc>,
|
||||
pub i32_type: Type<'gcc>,
|
||||
pub i64_type: Type<'gcc>,
|
||||
pub i128_type: Type<'gcc>,
|
||||
pub isize_type: Type<'gcc>,
|
||||
|
||||
pub u8_type: Type<'gcc>,
|
||||
pub u16_type: Type<'gcc>,
|
||||
pub u32_type: Type<'gcc>,
|
||||
pub u64_type: Type<'gcc>,
|
||||
pub u128_type: Type<'gcc>,
|
||||
pub usize_type: Type<'gcc>,
|
||||
|
||||
pub int_type: Type<'gcc>,
|
||||
pub uint_type: Type<'gcc>,
|
||||
pub long_type: Type<'gcc>,
|
||||
pub ulong_type: Type<'gcc>,
|
||||
pub ulonglong_type: Type<'gcc>,
|
||||
pub sizet_type: Type<'gcc>,
|
||||
|
||||
pub float_type: Type<'gcc>,
|
||||
pub double_type: Type<'gcc>,
|
||||
|
||||
pub linkage: Cell<FunctionType>,
|
||||
pub scalar_types: RefCell<FxHashMap<Ty<'tcx>, Type<'gcc>>>,
|
||||
pub types: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), Type<'gcc>>>,
|
||||
pub tcx: TyCtxt<'tcx>,
|
||||
|
||||
pub struct_types: RefCell<FxHashMap<Vec<Type<'gcc>>, Type<'gcc>>>,
|
||||
|
||||
pub types_with_fields_to_set: RefCell<FxHashMap<Type<'gcc>, (Struct<'gcc>, TyAndLayout<'tcx>)>>,
|
||||
|
||||
/// Cache instances of monomorphic and polymorphic items
|
||||
pub instances: RefCell<FxHashMap<Instance<'tcx>, RValue<'gcc>>>,
|
||||
/// Cache generated vtables
|
||||
pub vtables: RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), RValue<'gcc>>>,
|
||||
|
||||
/// Cache of emitted const globals (value -> global)
|
||||
pub const_globals: RefCell<FxHashMap<RValue<'gcc>, RValue<'gcc>>>,
|
||||
|
||||
pub init_argv_var: RefCell<String>,
|
||||
pub argv_initialized: Cell<bool>,
|
||||
|
||||
/// Cache of constant strings,
|
||||
pub const_cstr_cache: RefCell<FxHashMap<Symbol, LValue<'gcc>>>,
|
||||
|
||||
/// Cache of globals.
|
||||
pub globals: RefCell<FxHashMap<String, RValue<'gcc>>>,
|
||||
// TODO: remove global_names.
|
||||
pub global_names: RefCell<FxHashMap<RValue<'gcc>, String>>,
|
||||
|
||||
/// A counter that is used for generating local symbol names
|
||||
local_gen_sym_counter: Cell<usize>,
|
||||
pub global_gen_sym_counter: Cell<usize>,
|
||||
|
||||
eh_personality: Cell<Option<RValue<'gcc>>>,
|
||||
|
||||
pub pointee_infos: RefCell<FxHashMap<(Ty<'tcx>, Size), Option<PointeeInfo>>>,
|
||||
|
||||
/// NOTE: a hack is used because the rustc API is not suitable to libgccjit and as such,
|
||||
/// `const_undef()` returns struct as pointer so that they can later be assigned a value.
|
||||
/// As such, this set remembers which of these pointers were returned by this function so that
|
||||
/// they can be derefered later.
|
||||
/// FIXME: fix the rustc API to avoid having this hack.
|
||||
pub structs_as_pointer: RefCell<FxHashSet<RValue<'gcc>>>,
|
||||
|
||||
/// Store the pointer of different types for safety.
|
||||
/// When casting the values back to their original types, check that they are indeed that type
|
||||
/// with these sets.
|
||||
/// FIXME: remove when the API supports more types.
|
||||
#[cfg(debug_assertions)]
|
||||
lvalues: RefCell<FxHashSet<LValue<'gcc>>>,
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
|
||||
pub fn new(context: &'gcc Context<'gcc>, codegen_unit: &'tcx CodegenUnit<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
|
||||
let check_overflow = tcx.sess.overflow_checks();
|
||||
// TODO: fix this mess. libgccjit seems to return random type when using new_int_type().
|
||||
//let isize_type = context.new_int_type((tcx.data_layout.pointer_size.bits() / 8) as i32, true);
|
||||
let isize_type = context.new_c_type(CType::LongLong);
|
||||
//let usize_type = context.new_int_type((tcx.data_layout.pointer_size.bits() / 8) as i32, false);
|
||||
let usize_type = context.new_c_type(CType::ULongLong);
|
||||
let bool_type = context.new_type::<bool>();
|
||||
let i8_type = context.new_type::<i8>();
|
||||
let i16_type = context.new_type::<i16>();
|
||||
let i32_type = context.new_type::<i32>();
|
||||
let i64_type = context.new_c_type(CType::LongLong);
|
||||
let i128_type = context.new_c_type(CType::Int128t).get_aligned(8); // TODO: should this be hard-coded?
|
||||
let u8_type = context.new_type::<u8>();
|
||||
let u16_type = context.new_type::<u16>();
|
||||
let u32_type = context.new_type::<u32>();
|
||||
let u64_type = context.new_c_type(CType::ULongLong);
|
||||
let u128_type = context.new_c_type(CType::UInt128t).get_aligned(8); // TODO: should this be hard-coded?
|
||||
|
||||
let tls_model = to_gcc_tls_mode(tcx.sess.tls_model());
|
||||
|
||||
let float_type = context.new_type::<f32>();
|
||||
let double_type = context.new_type::<f64>();
|
||||
|
||||
let int_type = context.new_c_type(CType::Int);
|
||||
let uint_type = context.new_c_type(CType::UInt);
|
||||
let long_type = context.new_c_type(CType::Long);
|
||||
let ulong_type = context.new_c_type(CType::ULong);
|
||||
let ulonglong_type = context.new_c_type(CType::ULongLong);
|
||||
let sizet_type = context.new_c_type(CType::SizeT);
|
||||
|
||||
assert_eq!(isize_type, i64_type);
|
||||
assert_eq!(usize_type, u64_type);
|
||||
|
||||
let mut functions = FxHashMap::default();
|
||||
let builtins = [
|
||||
"__builtin_unreachable", "abort", "__builtin_expect", "__builtin_add_overflow", "__builtin_mul_overflow",
|
||||
"__builtin_saddll_overflow", /*"__builtin_sadd_overflow",*/ "__builtin_smulll_overflow", /*"__builtin_smul_overflow",*/
|
||||
"__builtin_ssubll_overflow", /*"__builtin_ssub_overflow",*/ "__builtin_sub_overflow", "__builtin_uaddll_overflow",
|
||||
"__builtin_uadd_overflow", "__builtin_umulll_overflow", "__builtin_umul_overflow", "__builtin_usubll_overflow",
|
||||
"__builtin_usub_overflow", "sqrtf", "sqrt", "__builtin_powif", "__builtin_powi", "sinf", "sin", "cosf", "cos",
|
||||
"powf", "pow", "expf", "exp", "exp2f", "exp2", "logf", "log", "log10f", "log10", "log2f", "log2", "fmaf",
|
||||
"fma", "fabsf", "fabs", "fminf", "fmin", "fmaxf", "fmax", "copysignf", "copysign", "floorf", "floor", "ceilf",
|
||||
"ceil", "truncf", "trunc", "rintf", "rint", "nearbyintf", "nearbyint", "roundf", "round",
|
||||
"__builtin_expect_with_probability",
|
||||
];
|
||||
|
||||
for builtin in builtins.iter() {
|
||||
functions.insert(builtin.to_string(), context.get_builtin_function(builtin));
|
||||
}
|
||||
|
||||
let global_init_func = context.new_function(None, FunctionType::Exported, context.new_type::<()>(), &[],
|
||||
&format!("__gccGlobalInit{}", unit_name(&codegen_unit)), false);
|
||||
let global_init_block = global_init_func.new_block("initial");
|
||||
|
||||
Self {
|
||||
check_overflow,
|
||||
codegen_unit,
|
||||
context,
|
||||
current_block: RefCell::new(None),
|
||||
current_func: RefCell::new(None),
|
||||
normal_function_addresses: Default::default(),
|
||||
functions: RefCell::new(functions),
|
||||
global_init_func,
|
||||
global_init_block,
|
||||
|
||||
tls_model,
|
||||
|
||||
bool_type,
|
||||
i8_type,
|
||||
i16_type,
|
||||
i32_type,
|
||||
i64_type,
|
||||
i128_type,
|
||||
isize_type,
|
||||
usize_type,
|
||||
u8_type,
|
||||
u16_type,
|
||||
u32_type,
|
||||
u64_type,
|
||||
u128_type,
|
||||
int_type,
|
||||
uint_type,
|
||||
long_type,
|
||||
ulong_type,
|
||||
ulonglong_type,
|
||||
sizet_type,
|
||||
|
||||
float_type,
|
||||
double_type,
|
||||
|
||||
linkage: Cell::new(FunctionType::Internal),
|
||||
#[cfg(debug_assertions)]
|
||||
lvalues: Default::default(),
|
||||
instances: Default::default(),
|
||||
vtables: Default::default(),
|
||||
const_globals: Default::default(),
|
||||
init_argv_var: RefCell::new(String::new()),
|
||||
argv_initialized: Cell::new(false),
|
||||
const_cstr_cache: Default::default(),
|
||||
global_names: Default::default(),
|
||||
globals: Default::default(),
|
||||
scalar_types: Default::default(),
|
||||
types: Default::default(),
|
||||
tcx,
|
||||
struct_types: Default::default(),
|
||||
types_with_fields_to_set: Default::default(),
|
||||
local_gen_sym_counter: Cell::new(0),
|
||||
global_gen_sym_counter: Cell::new(0),
|
||||
eh_personality: Cell::new(None),
|
||||
pointee_infos: Default::default(),
|
||||
structs_as_pointer: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn lvalue_to_rvalue(&self, value: LValue<'gcc>) -> RValue<'gcc> {
|
||||
#[cfg(debug_assertions)]
|
||||
self.lvalues.borrow_mut().insert(value);
|
||||
unsafe { std::mem::transmute(value) }
|
||||
}
|
||||
|
||||
pub fn rvalue_as_function(&self, value: RValue<'gcc>) -> Function<'gcc> {
|
||||
let function: Function<'gcc> = unsafe { std::mem::transmute(value) };
|
||||
debug_assert!(self.functions.borrow().values().find(|value| **value == function).is_some(),
|
||||
"{:?} ({:?}) is not a function", value, value.get_type());
|
||||
function
|
||||
}
|
||||
|
||||
pub fn rvalue_as_lvalue(&self, value: RValue<'gcc>) -> LValue<'gcc> {
|
||||
let lvalue: LValue<'gcc> = unsafe { std::mem::transmute(value) };
|
||||
//debug_assert!(self.lvalues.borrow().contains(&lvalue), "{:?} is not an lvalue", value);
|
||||
lvalue
|
||||
}
|
||||
|
||||
pub fn sess(&self) -> &Session {
|
||||
&self.tcx.sess
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> BackendTypes for CodegenCx<'gcc, 'tcx> {
|
||||
type Value = RValue<'gcc>;
|
||||
type Function = RValue<'gcc>;
|
||||
|
||||
type BasicBlock = Block<'gcc>;
|
||||
type Type = Type<'gcc>;
|
||||
type Funclet = (); // TODO
|
||||
|
||||
type DIScope = (); // TODO
|
||||
type DILocation = (); // TODO
|
||||
type DIVariable = (); // TODO
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> MiscMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
|
||||
fn vtables(&self) -> &RefCell<FxHashMap<(Ty<'tcx>, Option<PolyExistentialTraitRef<'tcx>>), RValue<'gcc>>> {
|
||||
&self.vtables
|
||||
}
|
||||
|
||||
fn get_fn(&self, instance: Instance<'tcx>) -> RValue<'gcc> {
|
||||
let func = get_fn(self, instance);
|
||||
*self.current_func.borrow_mut() = Some(self.rvalue_as_function(func));
|
||||
func
|
||||
}
|
||||
|
||||
fn get_fn_addr(&self, instance: Instance<'tcx>) -> RValue<'gcc> {
|
||||
//let symbol = self.tcx.symbol_name(instance).name;
|
||||
|
||||
let func = get_fn(self, instance);
|
||||
let func = self.rvalue_as_function(func);
|
||||
let ptr = func.get_address(None);
|
||||
|
||||
// TODO: don't do this twice: i.e. in declare_fn and here.
|
||||
//let fn_abi = FnAbi::of_instance(self, instance, &[]);
|
||||
//let (return_type, params, _) = fn_abi.gcc_type(self);
|
||||
// FIXME: the rustc API seems to call get_fn_addr() when not needed (e.g. for FFI).
|
||||
//let pointer_type = ptr.get_type();
|
||||
|
||||
self.normal_function_addresses.borrow_mut().insert(ptr);
|
||||
|
||||
ptr
|
||||
}
|
||||
|
||||
fn eh_personality(&self) -> RValue<'gcc> {
|
||||
// The exception handling personality function.
|
||||
//
|
||||
// If our compilation unit has the `eh_personality` lang item somewhere
|
||||
// within it, then we just need to codegen that. Otherwise, we're
|
||||
// building an rlib which will depend on some upstream implementation of
|
||||
// this function, so we just codegen a generic reference to it. We don't
|
||||
// specify any of the types for the function, we just make it a symbol
|
||||
// that LLVM can later use.
|
||||
//
|
||||
// Note that MSVC is a little special here in that we don't use the
|
||||
// `eh_personality` lang item at all. Currently LLVM has support for
|
||||
// both Dwarf and SEH unwind mechanisms for MSVC targets and uses the
|
||||
// *name of the personality function* to decide what kind of unwind side
|
||||
// tables/landing pads to emit. It looks like Dwarf is used by default,
|
||||
// injecting a dependency on the `_Unwind_Resume` symbol for resuming
|
||||
// an "exception", but for MSVC we want to force SEH. This means that we
|
||||
// can't actually have the personality function be our standard
|
||||
// `rust_eh_personality` function, but rather we wired it up to the
|
||||
// CRT's custom personality function, which forces LLVM to consider
|
||||
// landing pads as "landing pads for SEH".
|
||||
if let Some(llpersonality) = self.eh_personality.get() {
|
||||
return llpersonality;
|
||||
}
|
||||
let tcx = self.tcx;
|
||||
let llfn = match tcx.lang_items().eh_personality() {
|
||||
Some(def_id) if !wants_msvc_seh(self.sess()) => self.get_fn_addr(
|
||||
ty::Instance::resolve(
|
||||
tcx,
|
||||
ty::ParamEnv::reveal_all(),
|
||||
def_id,
|
||||
tcx.intern_substs(&[]),
|
||||
)
|
||||
.unwrap().unwrap(),
|
||||
),
|
||||
_ => {
|
||||
let name = if wants_msvc_seh(self.sess()) {
|
||||
"__CxxFrameHandler3"
|
||||
} else {
|
||||
"rust_eh_personality"
|
||||
};
|
||||
self.declare_func(name, self.type_i32(), &[], true)
|
||||
}
|
||||
};
|
||||
//attributes::apply_target_cpu_attr(self, llfn);
|
||||
self.eh_personality.set(Some(llfn));
|
||||
llfn
|
||||
}
|
||||
|
||||
fn sess(&self) -> &Session {
|
||||
&self.tcx.sess
|
||||
}
|
||||
|
||||
fn check_overflow(&self) -> bool {
|
||||
self.check_overflow
|
||||
}
|
||||
|
||||
fn codegen_unit(&self) -> &'tcx CodegenUnit<'tcx> {
|
||||
self.codegen_unit
|
||||
}
|
||||
|
||||
fn used_statics(&self) -> &RefCell<Vec<RValue<'gcc>>> {
|
||||
unimplemented!();
|
||||
//&self.used_statics
|
||||
}
|
||||
|
||||
fn set_frame_pointer_type(&self, _llfn: RValue<'gcc>) {
|
||||
// TODO
|
||||
//attributes::set_frame_pointer_type(self, llfn)
|
||||
}
|
||||
|
||||
fn apply_target_cpu_attr(&self, _llfn: RValue<'gcc>) {
|
||||
// TODO
|
||||
//attributes::apply_target_cpu_attr(self, llfn)
|
||||
}
|
||||
|
||||
fn create_used_variable(&self) {
|
||||
unimplemented!();
|
||||
/*let name = const_cstr!("llvm.used");
|
||||
let section = const_cstr!("llvm.metadata");
|
||||
let array =
|
||||
self.const_array(&self.type_ptr_to(self.type_i8()), &*self.used_statics.borrow());
|
||||
|
||||
unsafe {
|
||||
let g = llvm::LLVMAddGlobal(self.llmod, self.val_ty(array), name.as_ptr());
|
||||
llvm::LLVMSetInitializer(g, array);
|
||||
llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
|
||||
llvm::LLVMSetSection(g, section.as_ptr());
|
||||
}*/
|
||||
}
|
||||
|
||||
fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
|
||||
if self.get_declared_value("main").is_none() {
|
||||
Some(self.declare_cfn("main", fn_type))
|
||||
}
|
||||
else {
|
||||
// If the symbol already exists, it is an error: for example, the user wrote
|
||||
// #[no_mangle] extern "C" fn main(..) {..}
|
||||
// instead of #[start]
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> HasTyCtxt<'tcx> for CodegenCx<'gcc, 'tcx> {
|
||||
fn tcx(&self) -> TyCtxt<'tcx> {
|
||||
self.tcx
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> HasDataLayout for CodegenCx<'gcc, 'tcx> {
|
||||
fn data_layout(&self) -> &TargetDataLayout {
|
||||
&self.tcx.data_layout
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> HasTargetSpec for CodegenCx<'gcc, 'tcx> {
|
||||
fn target_spec(&self) -> &Target {
|
||||
&self.tcx.sess.target
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> LayoutOf for CodegenCx<'gcc, 'tcx> {
|
||||
type Ty = Ty<'tcx>;
|
||||
type TyAndLayout = TyAndLayout<'tcx>;
|
||||
|
||||
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
|
||||
self.spanned_layout_of(ty, DUMMY_SP)
|
||||
}
|
||||
|
||||
fn spanned_layout_of(&self, ty: Ty<'tcx>, span: Span) -> Self::TyAndLayout {
|
||||
self.tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap_or_else(|e| {
|
||||
if let LayoutError::SizeOverflow(_) = e {
|
||||
self.sess().span_fatal(span, &e.to_string())
|
||||
} else {
|
||||
bug!("failed to get layout for `{}`: {}", ty, e)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, 'gcc> HasParamEnv<'tcx> for CodegenCx<'gcc, 'tcx> {
|
||||
fn param_env(&self) -> ParamEnv<'tcx> {
|
||||
ParamEnv::reveal_all()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
|
||||
/// Generates a new symbol name with the given prefix. This symbol name must
|
||||
/// only be used for definitions with `internal` or `private` linkage.
|
||||
pub fn generate_local_symbol_name(&self, prefix: &str) -> String {
|
||||
let idx = self.local_gen_sym_counter.get();
|
||||
self.local_gen_sym_counter.set(idx + 1);
|
||||
// Include a '.' character, so there can be no accidental conflicts with
|
||||
// user defined names
|
||||
let mut name = String::with_capacity(prefix.len() + 6);
|
||||
name.push_str(prefix);
|
||||
name.push_str(".");
|
||||
base_n::push_str(idx as u128, base_n::ALPHANUMERIC_ONLY, &mut name);
|
||||
name
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unit_name<'tcx>(codegen_unit: &CodegenUnit<'tcx>) -> String {
|
||||
let name = &codegen_unit.name().to_string();
|
||||
mangle_name(&name.replace('-', "_"))
|
||||
}
|
||||
|
||||
fn to_gcc_tls_mode(tls_model: TlsModel) -> gccjit::TlsModel {
|
||||
match tls_model {
|
||||
TlsModel::GeneralDynamic => gccjit::TlsModel::GlobalDynamic,
|
||||
TlsModel::LocalDynamic => gccjit::TlsModel::LocalDynamic,
|
||||
TlsModel::InitialExec => gccjit::TlsModel::InitialExec,
|
||||
TlsModel::LocalExec => gccjit::TlsModel::LocalExec,
|
||||
}
|
||||
}
|
||||
140
compiler/rustc_codegen_gcc/src/coverageinfo.rs
Normal file
140
compiler/rustc_codegen_gcc/src/coverageinfo.rs
Normal file
@@ -0,0 +1,140 @@
|
||||
use gccjit::RValue;
|
||||
use rustc_codegen_ssa::traits::{CoverageInfoBuilderMethods, CoverageInfoMethods};
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_middle::mir::coverage::{
|
||||
CodeRegion,
|
||||
CounterValueReference,
|
||||
ExpressionOperandId,
|
||||
InjectedExpressionId,
|
||||
Op,
|
||||
};
|
||||
use rustc_middle::ty::Instance;
|
||||
|
||||
use crate::builder::Builder;
|
||||
use crate::context::CodegenCx;
|
||||
|
||||
impl<'a, 'gcc, 'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
|
||||
fn set_function_source_hash(
|
||||
&mut self,
|
||||
_instance: Instance<'tcx>,
|
||||
_function_source_hash: u64,
|
||||
) -> bool {
|
||||
unimplemented!();
|
||||
/*if let Some(coverage_context) = self.coverage_context() {
|
||||
debug!(
|
||||
"ensuring function source hash is set for instance={:?}; function_source_hash={}",
|
||||
instance, function_source_hash,
|
||||
);
|
||||
let mut coverage_map = coverage_context.function_coverage_map.borrow_mut();
|
||||
coverage_map
|
||||
.entry(instance)
|
||||
.or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
|
||||
.set_function_source_hash(function_source_hash);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}*/
|
||||
}
|
||||
|
||||
fn add_coverage_counter(&mut self, _instance: Instance<'tcx>, _id: CounterValueReference, _region: CodeRegion) -> bool {
|
||||
/*if let Some(coverage_context) = self.coverage_context() {
|
||||
debug!(
|
||||
"adding counter to coverage_regions: instance={:?}, function_source_hash={}, id={:?}, \
|
||||
at {:?}",
|
||||
instance, function_source_hash, id, region,
|
||||
);
|
||||
let mut coverage_regions = coverage_context.function_coverage_map.borrow_mut();
|
||||
coverage_regions
|
||||
.entry(instance)
|
||||
.or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
|
||||
.add_counter(function_source_hash, id, region);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}*/
|
||||
// TODO
|
||||
false
|
||||
}
|
||||
|
||||
fn add_coverage_counter_expression(&mut self, _instance: Instance<'tcx>, _id: InjectedExpressionId, _lhs: ExpressionOperandId, _op: Op, _rhs: ExpressionOperandId, _region: Option<CodeRegion>) -> bool {
|
||||
/*if let Some(coverage_context) = self.coverage_context() {
|
||||
debug!(
|
||||
"adding counter expression to coverage_regions: instance={:?}, id={:?}, {:?} {:?} {:?}, \
|
||||
at {:?}",
|
||||
instance, id, lhs, op, rhs, region,
|
||||
);
|
||||
let mut coverage_regions = coverage_context.function_coverage_map.borrow_mut();
|
||||
coverage_regions
|
||||
.entry(instance)
|
||||
.or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
|
||||
.add_counter_expression(id, lhs, op, rhs, region);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}*/
|
||||
// TODO
|
||||
false
|
||||
}
|
||||
|
||||
fn add_coverage_unreachable(&mut self, _instance: Instance<'tcx>, _region: CodeRegion) -> bool {
|
||||
/*if let Some(coverage_context) = self.coverage_context() {
|
||||
debug!(
|
||||
"adding unreachable code to coverage_regions: instance={:?}, at {:?}",
|
||||
instance, region,
|
||||
);
|
||||
let mut coverage_regions = coverage_context.function_coverage_map.borrow_mut();
|
||||
coverage_regions
|
||||
.entry(instance)
|
||||
.or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
|
||||
.add_unreachable_region(region);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}*/
|
||||
// TODO
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> CoverageInfoMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
|
||||
fn coverageinfo_finalize(&self) {
|
||||
// TODO
|
||||
//mapgen::finalize(self)
|
||||
}
|
||||
|
||||
fn get_pgo_func_name_var(&self, _instance: Instance<'tcx>) -> RValue<'gcc> {
|
||||
unimplemented!();
|
||||
/*if let Some(coverage_context) = self.coverage_context() {
|
||||
debug!("getting pgo_func_name_var for instance={:?}", instance);
|
||||
let mut pgo_func_name_var_map = coverage_context.pgo_func_name_var_map.borrow_mut();
|
||||
pgo_func_name_var_map
|
||||
.entry(instance)
|
||||
.or_insert_with(|| create_pgo_func_name_var(self, instance))
|
||||
} else {
|
||||
bug!("Could not get the `coverage_context`");
|
||||
}*/
|
||||
}
|
||||
|
||||
/// Functions with MIR-based coverage are normally codegenned _only_ if
|
||||
/// called. LLVM coverage tools typically expect every function to be
|
||||
/// defined (even if unused), with at least one call to LLVM intrinsic
|
||||
/// `instrprof.increment`.
|
||||
///
|
||||
/// Codegen a small function that will never be called, with one counter
|
||||
/// that will never be incremented.
|
||||
///
|
||||
/// For used/called functions, the coverageinfo was already added to the
|
||||
/// `function_coverage_map` (keyed by function `Instance`) during codegen.
|
||||
/// But in this case, since the unused function was _not_ previously
|
||||
/// codegenned, collect the coverage `CodeRegion`s from the MIR and add
|
||||
/// them. The first `CodeRegion` is used to add a single counter, with the
|
||||
/// same counter ID used in the injected `instrprof.increment` intrinsic
|
||||
/// call. Since the function is never called, all other `CodeRegion`s can be
|
||||
/// added as `unreachable_region`s.
|
||||
fn define_unused_fn(&self, _def_id: DefId) {
|
||||
unimplemented!();
|
||||
/*let instance = declare_unused_fn(self, &def_id);
|
||||
codegen_unused_fn_and_counter(self, instance);
|
||||
add_unused_function_coverage(self, instance, def_id);*/
|
||||
}
|
||||
}
|
||||
407
compiler/rustc_codegen_gcc/src/debuginfo.rs
Normal file
407
compiler/rustc_codegen_gcc/src/debuginfo.rs
Normal file
@@ -0,0 +1,407 @@
|
||||
use gccjit::{FunctionType, RValue};
|
||||
use rustc_codegen_ssa::mir::debuginfo::{FunctionDebugContext, VariableKind};
|
||||
use rustc_codegen_ssa::traits::{BuilderMethods, DebugInfoBuilderMethods, DebugInfoMethods};
|
||||
use rustc_middle::middle::cstore::CrateDepKind;
|
||||
use rustc_middle::mir;
|
||||
use rustc_middle::ty::{Instance, Ty};
|
||||
use rustc_span::{SourceFile, Span, Symbol};
|
||||
use rustc_span::def_id::LOCAL_CRATE;
|
||||
use rustc_target::abi::Size;
|
||||
use rustc_target::abi::call::FnAbi;
|
||||
|
||||
use crate::builder::Builder;
|
||||
use crate::context::CodegenCx;
|
||||
|
||||
impl<'a, 'gcc, 'tcx> DebugInfoBuilderMethods for Builder<'a, 'gcc, 'tcx> {
|
||||
// FIXME(eddyb) find a common convention for all of the debuginfo-related
|
||||
// names (choose between `dbg`, `debug`, `debuginfo`, `debug_info` etc.).
|
||||
fn dbg_var_addr(&mut self, _dbg_var: Self::DIVariable, _scope_metadata: Self::DIScope, _variable_alloca: Self::Value, _direct_offset: Size, _indirect_offsets: &[Size]) {
|
||||
unimplemented!();
|
||||
/*let cx = self.cx();
|
||||
|
||||
// Convert the direct and indirect offsets to address ops.
|
||||
// FIXME(eddyb) use `const`s instead of getting the values via FFI,
|
||||
// the values should match the ones in the DWARF standard anyway.
|
||||
let op_deref = || unsafe { llvm::LLVMRustDIBuilderCreateOpDeref() };
|
||||
let op_plus_uconst = || unsafe { llvm::LLVMRustDIBuilderCreateOpPlusUconst() };
|
||||
let mut addr_ops = SmallVec::<[_; 8]>::new();
|
||||
|
||||
if direct_offset.bytes() > 0 {
|
||||
addr_ops.push(op_plus_uconst());
|
||||
addr_ops.push(direct_offset.bytes() as i64);
|
||||
}
|
||||
for &offset in indirect_offsets {
|
||||
addr_ops.push(op_deref());
|
||||
if offset.bytes() > 0 {
|
||||
addr_ops.push(op_plus_uconst());
|
||||
addr_ops.push(offset.bytes() as i64);
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME(eddyb) maybe this information could be extracted from `dbg_var`,
|
||||
// to avoid having to pass it down in both places?
|
||||
// NB: `var` doesn't seem to know about the column, so that's a limitation.
|
||||
let dbg_loc = cx.create_debug_loc(scope_metadata, span);
|
||||
unsafe {
|
||||
// FIXME(eddyb) replace `llvm.dbg.declare` with `llvm.dbg.addr`.
|
||||
llvm::LLVMRustDIBuilderInsertDeclareAtEnd(
|
||||
DIB(cx),
|
||||
variable_alloca,
|
||||
dbg_var,
|
||||
addr_ops.as_ptr(),
|
||||
addr_ops.len() as c_uint,
|
||||
dbg_loc,
|
||||
self.llbb(),
|
||||
);
|
||||
}*/
|
||||
}
|
||||
|
||||
/*fn set_source_location(&mut self, scope: Self::DIScope, span: Span) {
|
||||
unimplemented!();
|
||||
/*debug!("set_source_location: {}", self.sess().source_map().span_to_string(span));
|
||||
|
||||
let dbg_loc = self.cx().create_debug_loc(scope, span);
|
||||
|
||||
unsafe {
|
||||
llvm::LLVMSetCurrentDebugLocation(self.llbuilder, dbg_loc);
|
||||
}*/
|
||||
}*/
|
||||
|
||||
fn insert_reference_to_gdb_debug_scripts_section_global(&mut self) {
|
||||
// TODO: replace with gcc_jit_context_new_global_with_initializer() if it's added:
|
||||
// https://gcc.gnu.org/pipermail/jit/2020q3/001225.html
|
||||
//
|
||||
// Call the function to initialize global values here.
|
||||
// We assume this is only called for the main function.
|
||||
use std::iter;
|
||||
|
||||
for crate_num in self.cx.tcx.crates(()).iter().copied().chain(iter::once(LOCAL_CRATE)) {
|
||||
// FIXME: better way to find if a crate is of proc-macro type?
|
||||
if crate_num == LOCAL_CRATE || self.cx.tcx.dep_kind(crate_num) != CrateDepKind::MacrosOnly {
|
||||
// NOTE: proc-macro crates are not included in the executable, so don't call their
|
||||
// initialization routine.
|
||||
let initializer_name = format!("__gccGlobalCrateInit{}", self.cx.tcx.crate_name(crate_num));
|
||||
let codegen_init_func = self.context.new_function(None, FunctionType::Extern, self.context.new_type::<()>(), &[],
|
||||
initializer_name, false);
|
||||
self.llbb().add_eval(None, self.context.new_call(None, codegen_init_func, &[]));
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
//gdb::insert_reference_to_gdb_debug_scripts_section_global(self)
|
||||
}
|
||||
|
||||
fn set_var_name(&mut self, _value: RValue<'gcc>, _name: &str) {
|
||||
unimplemented!();
|
||||
// Avoid wasting time if LLVM value names aren't even enabled.
|
||||
/*if self.sess().fewer_names() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Only function parameters and instructions are local to a function,
|
||||
// don't change the name of anything else (e.g. globals).
|
||||
let param_or_inst = unsafe {
|
||||
llvm::LLVMIsAArgument(value).is_some() || llvm::LLVMIsAInstruction(value).is_some()
|
||||
};
|
||||
if !param_or_inst {
|
||||
return;
|
||||
}
|
||||
|
||||
// Avoid replacing the name if it already exists.
|
||||
// While we could combine the names somehow, it'd
|
||||
// get noisy quick, and the usefulness is dubious.
|
||||
if llvm::get_value_name(value).is_empty() {
|
||||
llvm::set_value_name(value, name.as_bytes());
|
||||
}*/
|
||||
}
|
||||
|
||||
fn set_dbg_loc(&mut self, _dbg_loc: Self::DILocation) {
|
||||
unimplemented!();
|
||||
/*unsafe {
|
||||
let dbg_loc_as_llval = llvm::LLVMRustMetadataAsValue(self.cx().llcx, dbg_loc);
|
||||
llvm::LLVMSetCurrentDebugLocation(self.llbuilder, dbg_loc_as_llval);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> DebugInfoMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
|
||||
fn create_vtable_metadata(&self, _ty: Ty<'tcx>, _vtable: Self::Value) {
|
||||
//metadata::create_vtable_metadata(self, ty, vtable)
|
||||
}
|
||||
|
||||
fn create_function_debug_context(&self, _instance: Instance<'tcx>, _fn_abi: &FnAbi<'tcx, Ty<'tcx>>, _llfn: RValue<'gcc>, _mir: &mir::Body<'tcx>) -> Option<FunctionDebugContext<Self::DIScope, Self::DILocation>> {
|
||||
// TODO
|
||||
None
|
||||
}
|
||||
|
||||
fn extend_scope_to_file(&self, _scope_metadata: Self::DIScope, _file: &SourceFile) -> Self::DIScope {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn debuginfo_finalize(&self) {
|
||||
//unimplemented!();
|
||||
}
|
||||
|
||||
fn create_dbg_var(&self, _variable_name: Symbol, _variable_type: Ty<'tcx>, _scope_metadata: Self::DIScope, _variable_kind: VariableKind, _span: Span) -> Self::DIVariable {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn dbg_scope_fn(&self, _instance: Instance<'tcx>, _fn_abi: &FnAbi<'tcx, Ty<'tcx>>, _maybe_definition_llfn: Option<RValue<'gcc>>) -> Self::DIScope {
|
||||
unimplemented!();
|
||||
/*let def_id = instance.def_id();
|
||||
let containing_scope = get_containing_scope(self, instance);
|
||||
let span = self.tcx.def_span(def_id);
|
||||
let loc = self.lookup_debug_loc(span.lo());
|
||||
let file_metadata = file_metadata(self, &loc.file);
|
||||
|
||||
let function_type_metadata = unsafe {
|
||||
let fn_signature = get_function_signature(self, fn_abi);
|
||||
llvm::LLVMRustDIBuilderCreateSubroutineType(DIB(self), fn_signature)
|
||||
};
|
||||
|
||||
// Find the enclosing function, in case this is a closure.
|
||||
let def_key = self.tcx().def_key(def_id);
|
||||
let mut name = def_key.disambiguated_data.data.to_string();
|
||||
|
||||
let enclosing_fn_def_id = self.tcx().closure_base_def_id(def_id);
|
||||
|
||||
// Get_template_parameters() will append a `<...>` clause to the function
|
||||
// name if necessary.
|
||||
let generics = self.tcx().generics_of(enclosing_fn_def_id);
|
||||
let substs = instance.substs.truncate_to(self.tcx(), generics);
|
||||
let template_parameters = get_template_parameters(self, &generics, substs, &mut name);
|
||||
|
||||
let linkage_name = &mangled_name_of_instance(self, instance).name;
|
||||
// Omit the linkage_name if it is the same as subprogram name.
|
||||
let linkage_name = if &name == linkage_name { "" } else { linkage_name };
|
||||
|
||||
// FIXME(eddyb) does this need to be separate from `loc.line` for some reason?
|
||||
let scope_line = loc.line;
|
||||
|
||||
let mut flags = DIFlags::FlagPrototyped;
|
||||
|
||||
if fn_abi.ret.layout.abi.is_uninhabited() {
|
||||
flags |= DIFlags::FlagNoReturn;
|
||||
}
|
||||
|
||||
let mut spflags = DISPFlags::SPFlagDefinition;
|
||||
if is_node_local_to_unit(self, def_id) {
|
||||
spflags |= DISPFlags::SPFlagLocalToUnit;
|
||||
}
|
||||
if self.sess().opts.optimize != config::OptLevel::No {
|
||||
spflags |= DISPFlags::SPFlagOptimized;
|
||||
}
|
||||
if let Some((id, _)) = self.tcx.entry_fn(LOCAL_CRATE) {
|
||||
if id.to_def_id() == def_id {
|
||||
spflags |= DISPFlags::SPFlagMainSubprogram;
|
||||
}
|
||||
}
|
||||
|
||||
unsafe {
|
||||
return llvm::LLVMRustDIBuilderCreateFunction(
|
||||
DIB(self),
|
||||
containing_scope,
|
||||
name.as_ptr().cast(),
|
||||
name.len(),
|
||||
linkage_name.as_ptr().cast(),
|
||||
linkage_name.len(),
|
||||
file_metadata,
|
||||
loc.line.unwrap_or(UNKNOWN_LINE_NUMBER),
|
||||
function_type_metadata,
|
||||
scope_line.unwrap_or(UNKNOWN_LINE_NUMBER),
|
||||
flags,
|
||||
spflags,
|
||||
maybe_definition_llfn,
|
||||
template_parameters,
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
fn get_function_signature<'ll, 'tcx>(
|
||||
cx: &CodegenCx<'ll, 'tcx>,
|
||||
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
|
||||
) -> &'ll DIArray {
|
||||
if cx.sess().opts.debuginfo == DebugInfo::Limited {
|
||||
return create_DIArray(DIB(cx), &[]);
|
||||
}
|
||||
|
||||
let mut signature = Vec::with_capacity(fn_abi.args.len() + 1);
|
||||
|
||||
// Return type -- llvm::DIBuilder wants this at index 0
|
||||
signature.push(if fn_abi.ret.is_ignore() {
|
||||
None
|
||||
} else {
|
||||
Some(type_metadata(cx, fn_abi.ret.layout.ty, rustc_span::DUMMY_SP))
|
||||
});
|
||||
|
||||
// Arguments types
|
||||
if cx.sess().target.options.is_like_msvc {
|
||||
// FIXME(#42800):
|
||||
// There is a bug in MSDIA that leads to a crash when it encounters
|
||||
// a fixed-size array of `u8` or something zero-sized in a
|
||||
// function-type (see #40477).
|
||||
// As a workaround, we replace those fixed-size arrays with a
|
||||
// pointer-type. So a function `fn foo(a: u8, b: [u8; 4])` would
|
||||
// appear as `fn foo(a: u8, b: *const u8)` in debuginfo,
|
||||
// and a function `fn bar(x: [(); 7])` as `fn bar(x: *const ())`.
|
||||
// This transformed type is wrong, but these function types are
|
||||
// already inaccurate due to ABI adjustments (see #42800).
|
||||
signature.extend(fn_abi.args.iter().map(|arg| {
|
||||
let t = arg.layout.ty;
|
||||
let t = match t.kind() {
|
||||
ty::Array(ct, _)
|
||||
if (*ct == cx.tcx.types.u8) || cx.layout_of(ct).is_zst() =>
|
||||
{
|
||||
cx.tcx.mk_imm_ptr(ct)
|
||||
}
|
||||
_ => t,
|
||||
};
|
||||
Some(type_metadata(cx, t, rustc_span::DUMMY_SP))
|
||||
}));
|
||||
} else {
|
||||
signature.extend(
|
||||
fn_abi
|
||||
.args
|
||||
.iter()
|
||||
.map(|arg| Some(type_metadata(cx, arg.layout.ty, rustc_span::DUMMY_SP))),
|
||||
);
|
||||
}
|
||||
|
||||
create_DIArray(DIB(cx), &signature[..])
|
||||
}
|
||||
|
||||
fn get_template_parameters<'ll, 'tcx>(
|
||||
cx: &CodegenCx<'ll, 'tcx>,
|
||||
generics: &ty::Generics,
|
||||
substs: SubstsRef<'tcx>,
|
||||
name_to_append_suffix_to: &mut String,
|
||||
) -> &'ll DIArray {
|
||||
if substs.types().next().is_none() {
|
||||
return create_DIArray(DIB(cx), &[]);
|
||||
}
|
||||
|
||||
name_to_append_suffix_to.push('<');
|
||||
for (i, actual_type) in substs.types().enumerate() {
|
||||
if i != 0 {
|
||||
name_to_append_suffix_to.push(',');
|
||||
}
|
||||
|
||||
let actual_type =
|
||||
cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), actual_type);
|
||||
// Add actual type name to <...> clause of function name
|
||||
let actual_type_name = compute_debuginfo_type_name(cx.tcx(), actual_type, true);
|
||||
name_to_append_suffix_to.push_str(&actual_type_name[..]);
|
||||
}
|
||||
name_to_append_suffix_to.push('>');
|
||||
|
||||
// Again, only create type information if full debuginfo is enabled
|
||||
let template_params: Vec<_> = if cx.sess().opts.debuginfo == DebugInfo::Full {
|
||||
let names = get_parameter_names(cx, generics);
|
||||
substs
|
||||
.iter()
|
||||
.zip(names)
|
||||
.filter_map(|(kind, name)| {
|
||||
if let GenericArgKind::Type(ty) = kind.unpack() {
|
||||
let actual_type =
|
||||
cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), ty);
|
||||
let actual_type_metadata =
|
||||
type_metadata(cx, actual_type, rustc_span::DUMMY_SP);
|
||||
let name = name.as_str();
|
||||
Some(unsafe {
|
||||
Some(llvm::LLVMRustDIBuilderCreateTemplateTypeParameter(
|
||||
DIB(cx),
|
||||
None,
|
||||
name.as_ptr().cast(),
|
||||
name.len(),
|
||||
actual_type_metadata,
|
||||
))
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
|
||||
create_DIArray(DIB(cx), &template_params[..])
|
||||
}
|
||||
|
||||
fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
|
||||
let mut names = generics
|
||||
.parent
|
||||
.map_or(vec![], |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)));
|
||||
names.extend(generics.params.iter().map(|param| param.name));
|
||||
names
|
||||
}
|
||||
|
||||
fn get_containing_scope<'ll, 'tcx>(
|
||||
cx: &CodegenCx<'ll, 'tcx>,
|
||||
instance: Instance<'tcx>,
|
||||
) -> &'ll DIScope {
|
||||
// First, let's see if this is a method within an inherent impl. Because
|
||||
// if yes, we want to make the result subroutine DIE a child of the
|
||||
// subroutine's self-type.
|
||||
let self_type = cx.tcx.impl_of_method(instance.def_id()).and_then(|impl_def_id| {
|
||||
// If the method does *not* belong to a trait, proceed
|
||||
if cx.tcx.trait_id_of_impl(impl_def_id).is_none() {
|
||||
let impl_self_ty = cx.tcx.subst_and_normalize_erasing_regions(
|
||||
instance.substs,
|
||||
ty::ParamEnv::reveal_all(),
|
||||
&cx.tcx.type_of(impl_def_id),
|
||||
);
|
||||
|
||||
// Only "class" methods are generally understood by LLVM,
|
||||
// so avoid methods on other types (e.g., `<*mut T>::null`).
|
||||
match impl_self_ty.kind() {
|
||||
ty::Adt(def, ..) if !def.is_box() => {
|
||||
// Again, only create type information if full debuginfo is enabled
|
||||
if cx.sess().opts.debuginfo == DebugInfo::Full
|
||||
&& !impl_self_ty.needs_subst()
|
||||
{
|
||||
Some(type_metadata(cx, impl_self_ty, rustc_span::DUMMY_SP))
|
||||
} else {
|
||||
Some(namespace::item_namespace(cx, def.did))
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
} else {
|
||||
// For trait method impls we still use the "parallel namespace"
|
||||
// strategy
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
self_type.unwrap_or_else(|| {
|
||||
namespace::item_namespace(
|
||||
cx,
|
||||
DefId {
|
||||
krate: instance.def_id().krate,
|
||||
index: cx
|
||||
.tcx
|
||||
.def_key(instance.def_id())
|
||||
.parent
|
||||
.expect("get_containing_scope: missing parent?"),
|
||||
},
|
||||
)
|
||||
})
|
||||
}*/
|
||||
}
|
||||
|
||||
fn dbg_loc(&self, _scope: Self::DIScope, _inlined_at: Option<Self::DILocation>, _span: Span) -> Self::DILocation {
|
||||
unimplemented!();
|
||||
/*let DebugLoc { line, col, .. } = self.lookup_debug_loc(span.lo());
|
||||
|
||||
unsafe {
|
||||
llvm::LLVMRustDIBuilderCreateDebugLocation(
|
||||
utils::debug_context(self).llcontext,
|
||||
line.unwrap_or(UNKNOWN_LINE_NUMBER),
|
||||
col.unwrap_or(UNKNOWN_COLUMN_NUMBER),
|
||||
scope,
|
||||
inlined_at,
|
||||
)
|
||||
}*/
|
||||
}
|
||||
}
|
||||
220
compiler/rustc_codegen_gcc/src/declare.rs
Normal file
220
compiler/rustc_codegen_gcc/src/declare.rs
Normal file
@@ -0,0 +1,220 @@
|
||||
use gccjit::{Function, FunctionType, GlobalKind, LValue, RValue, Type};
|
||||
use rustc_codegen_ssa::traits::BaseTypeMethods;
|
||||
use rustc_middle::ty::Ty;
|
||||
use rustc_span::Symbol;
|
||||
use rustc_target::abi::call::FnAbi;
|
||||
|
||||
use crate::abi::FnAbiGccExt;
|
||||
use crate::context::{CodegenCx, unit_name};
|
||||
use crate::intrinsic::llvm;
|
||||
use crate::mangled_std_symbols::{ARGV_INIT_ARRAY, ARGV_INIT_WRAPPER};
|
||||
|
||||
impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
|
||||
pub fn get_or_insert_global(&self, name: &str, ty: Type<'gcc>, is_tls: bool, link_section: Option<Symbol>) -> RValue<'gcc> {
|
||||
if self.globals.borrow().contains_key(name) {
|
||||
let typ = self.globals.borrow().get(name).expect("global").get_type();
|
||||
let global = self.context.new_global(None, GlobalKind::Imported, typ, name);
|
||||
if is_tls {
|
||||
global.set_tls_model(self.tls_model);
|
||||
}
|
||||
if let Some(link_section) = link_section {
|
||||
global.set_link_section(&link_section.as_str());
|
||||
}
|
||||
global.get_address(None)
|
||||
}
|
||||
else {
|
||||
self.declare_global(name, ty, is_tls, link_section)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn declare_unnamed_global(&self, ty: Type<'gcc>) -> LValue<'gcc> {
|
||||
let index = self.global_gen_sym_counter.get();
|
||||
self.global_gen_sym_counter.set(index + 1);
|
||||
let name = format!("global_{}_{}", index, unit_name(&self.codegen_unit));
|
||||
self.context.new_global(None, GlobalKind::Exported, ty, &name)
|
||||
}
|
||||
|
||||
pub fn declare_global_with_linkage(&self, name: &str, ty: Type<'gcc>, linkage: GlobalKind) -> RValue<'gcc> {
|
||||
//debug!("declare_global_with_linkage(name={:?})", name);
|
||||
let global = self.context.new_global(None, linkage, ty, name)
|
||||
.get_address(None);
|
||||
self.globals.borrow_mut().insert(name.to_string(), global);
|
||||
// NOTE: global seems to only be global in a module. So save the name instead of the value
|
||||
// to import it later.
|
||||
self.global_names.borrow_mut().insert(global, name.to_string());
|
||||
global
|
||||
}
|
||||
|
||||
pub fn declare_func(&self, name: &str, return_type: Type<'gcc>, params: &[Type<'gcc>], variadic: bool) -> RValue<'gcc> {
|
||||
self.linkage.set(FunctionType::Exported);
|
||||
let func = declare_raw_fn(self, name, () /*llvm::CCallConv*/, return_type, params, variadic);
|
||||
// FIXME: this is a wrong cast. That requires changing the compiler API.
|
||||
unsafe { std::mem::transmute(func) }
|
||||
}
|
||||
|
||||
pub fn declare_global(&self, name: &str, ty: Type<'gcc>, is_tls: bool, link_section: Option<Symbol>) -> RValue<'gcc> {
|
||||
//debug!("declare_global(name={:?})", name);
|
||||
// FIXME: correctly support global variable initialization.
|
||||
if name.starts_with(ARGV_INIT_ARRAY) {
|
||||
// NOTE: hack to avoid having to update the names in mangled_std_symbols: we save the
|
||||
// name of the variable now to actually declare it later.
|
||||
*self.init_argv_var.borrow_mut() = name.to_string();
|
||||
|
||||
let global = self.context.new_global(None, GlobalKind::Imported, ty, name);
|
||||
if let Some(link_section) = link_section {
|
||||
global.set_link_section(&link_section.as_str());
|
||||
}
|
||||
return global.get_address(None);
|
||||
}
|
||||
let global = self.context.new_global(None, GlobalKind::Exported, ty, name);
|
||||
if is_tls {
|
||||
global.set_tls_model(self.tls_model);
|
||||
}
|
||||
if let Some(link_section) = link_section {
|
||||
global.set_link_section(&link_section.as_str());
|
||||
}
|
||||
let global = global.get_address(None);
|
||||
self.globals.borrow_mut().insert(name.to_string(), global);
|
||||
// NOTE: global seems to only be global in a module. So save the name instead of the value
|
||||
// to import it later.
|
||||
self.global_names.borrow_mut().insert(global, name.to_string());
|
||||
global
|
||||
}
|
||||
|
||||
pub fn declare_cfn(&self, name: &str, _fn_type: Type<'gcc>) -> RValue<'gcc> {
|
||||
// TODO: use the fn_type parameter.
|
||||
let const_string = self.context.new_type::<u8>().make_pointer().make_pointer();
|
||||
let return_type = self.type_i32();
|
||||
let variadic = false;
|
||||
self.linkage.set(FunctionType::Exported);
|
||||
let func = declare_raw_fn(self, name, () /*llvm::CCallConv*/, return_type, &[self.type_i32(), const_string], variadic);
|
||||
// NOTE: it is needed to set the current_func here as well, because get_fn() is not called
|
||||
// for the main function.
|
||||
*self.current_func.borrow_mut() = Some(func);
|
||||
// FIXME: this is a wrong cast. That requires changing the compiler API.
|
||||
unsafe { std::mem::transmute(func) }
|
||||
}
|
||||
|
||||
pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> RValue<'gcc> {
|
||||
// NOTE: hack to avoid having to update the names in mangled_std_symbols: we found the name
|
||||
// of the variable earlier, so we declare it now.
|
||||
// Since we don't correctly support initializers yet, we initialize this variable manually
|
||||
// for now.
|
||||
if name.starts_with(ARGV_INIT_WRAPPER) && !self.argv_initialized.get() {
|
||||
let global_name = &*self.init_argv_var.borrow();
|
||||
let return_type = self.type_void();
|
||||
let params = [
|
||||
self.context.new_parameter(None, self.int_type, "argc"),
|
||||
self.context.new_parameter(None, self.u8_type.make_pointer().make_pointer(), "argv"),
|
||||
self.context.new_parameter(None, self.u8_type.make_pointer().make_pointer(), "envp"),
|
||||
];
|
||||
let function = self.context.new_function(None, FunctionType::Extern, return_type, ¶ms, name, false);
|
||||
let initializer = function.get_address(None);
|
||||
|
||||
let param_types = [
|
||||
self.int_type,
|
||||
self.u8_type.make_pointer().make_pointer(),
|
||||
self.u8_type.make_pointer().make_pointer(),
|
||||
];
|
||||
let ty = self.context.new_function_pointer_type(None, return_type, ¶m_types, false);
|
||||
|
||||
let global = self.context.new_global(None, GlobalKind::Exported, ty, global_name);
|
||||
global.set_link_section(".init_array.00099");
|
||||
global.global_set_initializer_value(initializer);
|
||||
let global = global.get_address(None);
|
||||
self.globals.borrow_mut().insert(global_name.to_string(), global);
|
||||
// NOTE: global seems to only be global in a module. So save the name instead of the value
|
||||
// to import it later.
|
||||
self.global_names.borrow_mut().insert(global, global_name.to_string());
|
||||
self.argv_initialized.set(true);
|
||||
}
|
||||
//debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
|
||||
let (return_type, params, variadic) = fn_abi.gcc_type(self);
|
||||
let func = declare_raw_fn(self, name, () /*fn_abi.llvm_cconv()*/, return_type, ¶ms, variadic);
|
||||
//fn_abi.apply_attrs_llfn(self, func);
|
||||
// FIXME: this is a wrong cast. That requires changing the compiler API.
|
||||
unsafe { std::mem::transmute(func) }
|
||||
}
|
||||
|
||||
pub fn define_global(&self, name: &str, ty: Type<'gcc>, is_tls: bool, link_section: Option<Symbol>) -> Option<RValue<'gcc>> {
|
||||
Some(self.get_or_insert_global(name, ty, is_tls, link_section))
|
||||
}
|
||||
|
||||
pub fn define_private_global(&self, ty: Type<'gcc>) -> RValue<'gcc> {
|
||||
let global = self.declare_unnamed_global(ty);
|
||||
global.get_address(None)
|
||||
}
|
||||
|
||||
pub fn get_declared_value(&self, name: &str) -> Option<RValue<'gcc>> {
|
||||
//debug!("get_declared_value(name={:?})", name);
|
||||
// TODO: use a different field than globals, because this seems to return a function?
|
||||
self.globals.borrow().get(name).cloned()
|
||||
}
|
||||
|
||||
/*fn get_defined_value(&self, name: &str) -> Option<RValue<'gcc>> {
|
||||
// TODO: gcc does not allow global initialization.
|
||||
None
|
||||
/*self.get_declared_value(name).and_then(|val| {
|
||||
let declaration = unsafe { llvm::LLVMIsDeclaration(val) != 0 };
|
||||
if !declaration { Some(val) } else { None }
|
||||
})*/
|
||||
}*/
|
||||
}
|
||||
|
||||
/// Declare a function.
|
||||
///
|
||||
/// If there’s a value with the same name already declared, the function will
|
||||
/// update the declaration and return existing Value instead.
|
||||
fn declare_raw_fn<'gcc>(cx: &CodegenCx<'gcc, '_>, name: &str, _callconv: () /*llvm::CallConv*/, return_type: Type<'gcc>, param_types: &[Type<'gcc>], variadic: bool) -> Function<'gcc> {
|
||||
//debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
|
||||
/*let llfn = unsafe {
|
||||
llvm::LLVMRustGetOrInsertFunction(cx.llmod, name.as_ptr().cast(), name.len(), ty)
|
||||
};*/
|
||||
|
||||
if name.starts_with("llvm.") {
|
||||
return llvm::intrinsic(name, cx);
|
||||
}
|
||||
let func =
|
||||
if cx.functions.borrow().contains_key(name) {
|
||||
*cx.functions.borrow().get(name).expect("function")
|
||||
}
|
||||
else {
|
||||
let params: Vec<_> = param_types.into_iter().enumerate()
|
||||
.map(|(index, param)| cx.context.new_parameter(None, *param, &format!("param{}", index))) // TODO: set name.
|
||||
.collect();
|
||||
let func = cx.context.new_function(None, cx.linkage.get(), return_type, ¶ms, mangle_name(name), variadic);
|
||||
cx.functions.borrow_mut().insert(name.to_string(), func);
|
||||
func
|
||||
};
|
||||
|
||||
//llvm::SetFunctionCallConv(llfn, callconv); // TODO
|
||||
// Function addresses in Rust are never significant, allowing functions to
|
||||
// be merged.
|
||||
//llvm::SetUnnamedAddress(llfn, llvm::UnnamedAddr::Global); // TODO
|
||||
|
||||
/*if cx.tcx.sess.opts.cg.no_redzone.unwrap_or(cx.tcx.sess.target.target.options.disable_redzone) {
|
||||
llvm::Attribute::NoRedZone.apply_llfn(Function, llfn);
|
||||
}*/
|
||||
|
||||
//attributes::default_optimisation_attrs(cx.tcx.sess, llfn);
|
||||
//attributes::non_lazy_bind(cx.sess(), llfn);
|
||||
|
||||
// FIXME: invalid cast.
|
||||
// TODO: is this line useful?
|
||||
//cx.globals.borrow_mut().insert(name.to_string(), unsafe { std::mem::transmute(func) });
|
||||
func
|
||||
}
|
||||
|
||||
// FIXME: this is a hack because libgccjit currently only supports alpha, num and _.
|
||||
// Unsupported characters: `$` and `.`.
|
||||
pub fn mangle_name(name: &str) -> String {
|
||||
name.replace(|char: char| {
|
||||
if !char.is_alphanumeric() && char != '_' {
|
||||
debug_assert!("$.".contains(char), "Unsupported char in function name: {}", char);
|
||||
true
|
||||
}
|
||||
else {
|
||||
false
|
||||
}
|
||||
}, "_")
|
||||
}
|
||||
26
compiler/rustc_codegen_gcc/src/intrinsic/llvm.rs
Normal file
26
compiler/rustc_codegen_gcc/src/intrinsic/llvm.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use gccjit::Function;
|
||||
|
||||
use crate::context::CodegenCx;
|
||||
|
||||
pub fn intrinsic<'gcc, 'tcx>(name: &str, cx: &CodegenCx<'gcc, 'tcx>) -> Function<'gcc> {
|
||||
let _gcc_name =
|
||||
match name {
|
||||
"llvm.x86.xgetbv" => {
|
||||
let gcc_name = "__builtin_trap";
|
||||
let func = cx.context.get_builtin_function(gcc_name);
|
||||
cx.functions.borrow_mut().insert(gcc_name.to_string(), func);
|
||||
return func;
|
||||
},
|
||||
// TODO: this doc specifies the equivalent GCC builtins: http://huonw.github.io/llvmint/llvmint/x86/index.html
|
||||
"llvm.x86.sse2.cmp.pd" => "__builtin_ia32_cmppd",
|
||||
"llvm.x86.sse2.movmsk.pd" => "__builtin_ia32_movmskpd",
|
||||
"llvm.x86.sse2.pmovmskb.128" => "__builtin_ia32_pmovmskb128",
|
||||
_ => unimplemented!("unsupported LLVM intrinsic {}", name)
|
||||
};
|
||||
|
||||
println!("Get target builtin");
|
||||
unimplemented!();
|
||||
/*let func = cx.context.get_target_builtin_function(gcc_name);
|
||||
cx.functions.borrow_mut().insert(gcc_name.to_string(), func);
|
||||
func*/
|
||||
}
|
||||
1286
compiler/rustc_codegen_gcc/src/intrinsic/mod.rs
Normal file
1286
compiler/rustc_codegen_gcc/src/intrinsic/mod.rs
Normal file
File diff suppressed because it is too large
Load Diff
1001
compiler/rustc_codegen_gcc/src/intrinsic/simd.rs
Normal file
1001
compiler/rustc_codegen_gcc/src/intrinsic/simd.rs
Normal file
File diff suppressed because it is too large
Load Diff
342
compiler/rustc_codegen_gcc/src/lib.rs
Normal file
342
compiler/rustc_codegen_gcc/src/lib.rs
Normal file
@@ -0,0 +1,342 @@
|
||||
/*
|
||||
* TODO: support #[inline] attributes.
|
||||
* TODO: support LTO.
|
||||
*
|
||||
* TODO: remove the local gccjit LD_LIBRARY_PATH in config.sh.
|
||||
* TODO: remove the object dependency.
|
||||
* TODO: remove the patches.
|
||||
*/
|
||||
|
||||
#![feature(rustc_private, decl_macro, associated_type_bounds, never_type, trusted_len)]
|
||||
#![allow(broken_intra_doc_links)]
|
||||
#![recursion_limit="256"]
|
||||
#![warn(rust_2018_idioms)]
|
||||
#![warn(unused_lifetimes)]
|
||||
|
||||
/*extern crate flate2;
|
||||
extern crate libc;*/
|
||||
extern crate rustc_ast;
|
||||
extern crate rustc_codegen_ssa;
|
||||
extern crate rustc_data_structures;
|
||||
extern crate rustc_errors;
|
||||
//extern crate rustc_fs_util;
|
||||
extern crate rustc_hir;
|
||||
extern crate rustc_metadata;
|
||||
extern crate rustc_middle;
|
||||
extern crate rustc_mir;
|
||||
extern crate rustc_session;
|
||||
extern crate rustc_span;
|
||||
extern crate rustc_symbol_mangling;
|
||||
extern crate rustc_target;
|
||||
extern crate snap;
|
||||
|
||||
// This prevents duplicating functions and statics that are already part of the host rustc process.
|
||||
#[allow(unused_extern_crates)]
|
||||
extern crate rustc_driver;
|
||||
|
||||
mod abi;
|
||||
mod allocator;
|
||||
mod archive;
|
||||
mod asm;
|
||||
mod back;
|
||||
mod base;
|
||||
mod builder;
|
||||
mod callee;
|
||||
mod common;
|
||||
mod consts;
|
||||
mod context;
|
||||
mod coverageinfo;
|
||||
mod debuginfo;
|
||||
mod declare;
|
||||
mod intrinsic;
|
||||
mod mangled_std_symbols;
|
||||
mod mono_item;
|
||||
mod type_;
|
||||
mod type_of;
|
||||
mod va_arg;
|
||||
|
||||
use std::any::Any;
|
||||
use std::sync::Arc;
|
||||
|
||||
use gccjit::{Block, Context, FunctionType, OptimizationLevel};
|
||||
use rustc_ast::expand::allocator::AllocatorKind;
|
||||
use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen};
|
||||
use rustc_codegen_ssa::base::codegen_crate;
|
||||
use rustc_codegen_ssa::back::write::{CodegenContext, FatLTOInput, ModuleConfig, TargetMachineFactoryFn};
|
||||
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
|
||||
use rustc_codegen_ssa::target_features::supported_target_features;
|
||||
use rustc_codegen_ssa::traits::{CodegenBackend, ExtraBackendMethods, ModuleBufferMethods, ThinBufferMethods, WriteBackendMethods};
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_errors::{ErrorReported, Handler};
|
||||
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
|
||||
use rustc_middle::middle::cstore::EncodedMetadata;
|
||||
use rustc_middle::ty::TyCtxt;
|
||||
use rustc_session::config::{CrateType, Lto, OptLevel, OutputFilenames};
|
||||
use rustc_session::Session;
|
||||
use rustc_span::Symbol;
|
||||
use rustc_span::fatal_error::FatalError;
|
||||
|
||||
use crate::context::unit_name;
|
||||
|
||||
pub struct PrintOnPanic<F: Fn() -> String>(pub F);
|
||||
|
||||
impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
|
||||
fn drop(&mut self) {
|
||||
if ::std::thread::panicking() {
|
||||
println!("{}", (self.0)());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct GccCodegenBackend;
|
||||
|
||||
impl CodegenBackend for GccCodegenBackend {
|
||||
fn init(&self, sess: &Session) {
|
||||
if sess.lto() != Lto::No {
|
||||
sess.warn("LTO is not supported. You may get a linker error.");
|
||||
}
|
||||
}
|
||||
|
||||
fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>, metadata: EncodedMetadata, need_metadata_module: bool) -> Box<dyn Any> {
|
||||
let target_cpu = target_cpu(tcx.sess);
|
||||
let res = codegen_crate(self.clone(), tcx, target_cpu.to_string(), metadata, need_metadata_module);
|
||||
|
||||
rustc_symbol_mangling::test::report_symbol_names(tcx);
|
||||
|
||||
Box::new(res)
|
||||
}
|
||||
|
||||
fn join_codegen(&self, ongoing_codegen: Box<dyn Any>, sess: &Session) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorReported> {
|
||||
let (codegen_results, work_products) = ongoing_codegen
|
||||
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<GccCodegenBackend>>()
|
||||
.expect("Expected GccCodegenBackend's OngoingCodegen, found Box<Any>")
|
||||
.join(sess);
|
||||
|
||||
Ok((codegen_results, work_products))
|
||||
}
|
||||
|
||||
fn link(&self, sess: &Session, mut codegen_results: CodegenResults, outputs: &OutputFilenames) -> Result<(), ErrorReported> {
|
||||
use rustc_codegen_ssa::back::link::link_binary;
|
||||
if let Some(symbols) = codegen_results.crate_info.exported_symbols.get_mut(&CrateType::Dylib) {
|
||||
// TODO: remove when global initializer work without calling a function at runtime.
|
||||
// HACK: since this codegen add some symbols (e.g. __gccGlobalCrateInit) and the UI
|
||||
// tests load libstd.so as a dynamic library, and rustc use a version-script to specify
|
||||
// the symbols visibility, we add * to export all symbols.
|
||||
// It seems other symbols from libstd/libcore are causing some issues here as well.
|
||||
symbols.push("*".to_string());
|
||||
}
|
||||
|
||||
link_binary::<crate::archive::ArArchiveBuilder<'_>>(
|
||||
sess,
|
||||
&codegen_results,
|
||||
outputs,
|
||||
)
|
||||
}
|
||||
|
||||
fn target_features(&self, sess: &Session) -> Vec<Symbol> {
|
||||
target_features(sess)
|
||||
}
|
||||
}
|
||||
|
||||
impl ExtraBackendMethods for GccCodegenBackend {
|
||||
fn new_metadata<'tcx>(&self, _tcx: TyCtxt<'tcx>, _mod_name: &str) -> Self::Module {
|
||||
GccContext {
|
||||
context: Context::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn write_compressed_metadata<'tcx>(&self, tcx: TyCtxt<'tcx>, metadata: &EncodedMetadata, gcc_module: &mut Self::Module) {
|
||||
base::write_compressed_metadata(tcx, metadata, gcc_module)
|
||||
}
|
||||
|
||||
fn codegen_allocator<'tcx>(&self, tcx: TyCtxt<'tcx>, mods: &mut Self::Module, kind: AllocatorKind, has_alloc_error_handler: bool) {
|
||||
unsafe { allocator::codegen(tcx, mods, kind, has_alloc_error_handler) }
|
||||
}
|
||||
|
||||
fn compile_codegen_unit<'tcx>(&self, tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (ModuleCodegen<Self::Module>, u64) {
|
||||
base::compile_codegen_unit(tcx, cgu_name)
|
||||
}
|
||||
|
||||
fn target_machine_factory(&self, _sess: &Session, _opt_level: OptLevel) -> TargetMachineFactoryFn<Self> {
|
||||
// TODO: set opt level.
|
||||
Arc::new(|_| {
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
fn target_cpu<'b>(&self, _sess: &'b Session) -> &'b str {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn tune_cpu<'b>(&self, _sess: &'b Session) -> Option<&'b str> {
|
||||
None
|
||||
// TODO
|
||||
//llvm_util::tune_cpu(sess)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ModuleBuffer;
|
||||
|
||||
impl ModuleBufferMethods for ModuleBuffer {
|
||||
fn data(&self) -> &[u8] {
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ThinBuffer;
|
||||
|
||||
impl ThinBufferMethods for ThinBuffer {
|
||||
fn data(&self) -> &[u8] {
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GccContext {
|
||||
context: Context<'static>,
|
||||
}
|
||||
|
||||
unsafe impl Send for GccContext {}
|
||||
// FIXME: that shouldn't be Sync. Parallel compilation is currently disabled with "-Zno-parallel-llvm". Try to disable it here.
|
||||
unsafe impl Sync for GccContext {}
|
||||
|
||||
impl WriteBackendMethods for GccCodegenBackend {
|
||||
type Module = GccContext;
|
||||
type TargetMachine = ();
|
||||
type ModuleBuffer = ModuleBuffer;
|
||||
type Context = ();
|
||||
type ThinData = ();
|
||||
type ThinBuffer = ThinBuffer;
|
||||
|
||||
fn run_fat_lto(_cgcx: &CodegenContext<Self>, mut modules: Vec<FatLTOInput<Self>>, _cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>) -> Result<LtoModuleCodegen<Self>, FatalError> {
|
||||
// TODO: implement LTO by sending -flto to libgccjit and adding the appropriate gcc linker plugins.
|
||||
// NOTE: implemented elsewhere.
|
||||
let module =
|
||||
match modules.remove(0) {
|
||||
FatLTOInput::InMemory(module) => module,
|
||||
FatLTOInput::Serialized { .. } => {
|
||||
unimplemented!();
|
||||
/*info!("pushing serialized module {:?}", name);
|
||||
let buffer = SerializedModule::Local(buffer);
|
||||
serialized_modules.push((buffer, CString::new(name).unwrap()));*/
|
||||
}
|
||||
};
|
||||
Ok(LtoModuleCodegen::Fat { module: Some(module), _serialized_bitcode: vec![] })
|
||||
}
|
||||
|
||||
fn run_thin_lto(_cgcx: &CodegenContext<Self>, _modules: Vec<(String, Self::ThinBuffer)>, _cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn print_pass_timings(&self) {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
unsafe fn optimize(_cgcx: &CodegenContext<Self>, _diag_handler: &Handler, module: &ModuleCodegen<Self::Module>, config: &ModuleConfig) -> Result<(), FatalError> {
|
||||
//if cgcx.lto == Lto::Fat {
|
||||
//module.module_llvm.context.add_driver_option("-flto");
|
||||
//}
|
||||
module.module_llvm.context.set_optimization_level(to_gcc_opt_level(config.opt_level));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
unsafe fn optimize_thin(_cgcx: &CodegenContext<Self>, _thin: &mut ThinModule<Self>) -> Result<ModuleCodegen<Self::Module>, FatalError> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
unsafe fn codegen(cgcx: &CodegenContext<Self>, diag_handler: &Handler, module: ModuleCodegen<Self::Module>, config: &ModuleConfig) -> Result<CompiledModule, FatalError> {
|
||||
back::write::codegen(cgcx, diag_handler, module, config)
|
||||
}
|
||||
|
||||
fn prepare_thin(_module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer) {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn serialize_module(_module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer) {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn run_lto_pass_manager(_cgcx: &CodegenContext<Self>, _module: &ModuleCodegen<Self::Module>, _config: &ModuleConfig, _thin: bool) -> Result<(), FatalError> {
|
||||
// TODO
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_link(cgcx: &CodegenContext<Self>, diag_handler: &Handler, modules: Vec<ModuleCodegen<Self::Module>>) -> Result<ModuleCodegen<Self::Module>, FatalError> {
|
||||
back::write::link(cgcx, diag_handler, modules)
|
||||
}
|
||||
}
|
||||
|
||||
/*fn target_triple(sess: &Session) -> target_lexicon::Triple {
|
||||
sess.target.llvm_target.parse().unwrap()
|
||||
}*/
|
||||
|
||||
/// This is the entrypoint for a hot plugged rustc_codegen_gccjit
|
||||
#[no_mangle]
|
||||
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
|
||||
Box::new(GccCodegenBackend)
|
||||
}
|
||||
|
||||
fn to_gcc_opt_level(optlevel: Option<OptLevel>) -> OptimizationLevel {
|
||||
match optlevel {
|
||||
None => OptimizationLevel::None,
|
||||
Some(level) => {
|
||||
match level {
|
||||
OptLevel::No => OptimizationLevel::None,
|
||||
OptLevel::Less => OptimizationLevel::Limited,
|
||||
OptLevel::Default => OptimizationLevel::Standard,
|
||||
OptLevel::Aggressive => OptimizationLevel::Aggressive,
|
||||
OptLevel::Size | OptLevel::SizeMin => OptimizationLevel::Limited,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn create_function_calling_initializers<'gcc, 'tcx>(tcx: TyCtxt<'tcx>, context: &Context<'gcc>, block: Block<'gcc>) {
|
||||
let codegen_units = tcx.collect_and_partition_mono_items(()).1;
|
||||
for codegen_unit in codegen_units {
|
||||
let codegen_init_func = context.new_function(None, FunctionType::Extern, context.new_type::<()>(), &[],
|
||||
&format!("__gccGlobalInit{}", unit_name(&codegen_unit)), false);
|
||||
block.add_eval(None, context.new_call(None, codegen_init_func, &[]));
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_native(name: &str) -> &str {
|
||||
if name != "native" {
|
||||
return name;
|
||||
}
|
||||
|
||||
unimplemented!();
|
||||
/*unsafe {
|
||||
let mut len = 0;
|
||||
let ptr = llvm::LLVMRustGetHostCPUName(&mut len);
|
||||
str::from_utf8(slice::from_raw_parts(ptr as *const u8, len)).unwrap()
|
||||
}*/
|
||||
}
|
||||
|
||||
pub fn target_cpu(sess: &Session) -> &str {
|
||||
let name = sess.opts.cg.target_cpu.as_ref().unwrap_or(&sess.target.cpu);
|
||||
handle_native(name)
|
||||
}
|
||||
|
||||
pub fn target_features(sess: &Session) -> Vec<Symbol> {
|
||||
supported_target_features(sess)
|
||||
.iter()
|
||||
.filter_map(
|
||||
|&(feature, gate)| {
|
||||
if sess.is_nightly_build() || gate.is_none() { Some(feature) } else { None }
|
||||
},
|
||||
)
|
||||
.filter(|_feature| {
|
||||
/*if feature.starts_with("sse") {
|
||||
return true;
|
||||
}*/
|
||||
// TODO: implement a way to get enabled feature in libgccjit.
|
||||
//println!("Feature: {}", feature);
|
||||
/*let llvm_feature = to_llvm_feature(sess, feature);
|
||||
let cstr = CString::new(llvm_feature).unwrap();
|
||||
unsafe { llvm::LLVMRustHasFeature(target_machine, cstr.as_ptr()) }*/
|
||||
false
|
||||
})
|
||||
.map(|feature| Symbol::intern(feature))
|
||||
.collect()
|
||||
}
|
||||
4
compiler/rustc_codegen_gcc/src/mangled_std_symbols.rs
Normal file
4
compiler/rustc_codegen_gcc/src/mangled_std_symbols.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
pub const ARGV_INIT_ARRAY: &str = "_ZN3std3sys4unix4args3imp15ARGV_INIT_ARRAY";
|
||||
pub const ARGV_INIT_WRAPPER: &str = "_ZN3std3sys4unix4args3imp15ARGV_INIT_ARRAY12init_wrapper";
|
||||
pub const ARGC: &str = "_ZN3std3sys4unix4args3imp4ARGC";
|
||||
pub const ARGV: &str = "_ZN3std3sys4unix4args3imp4ARGV";
|
||||
59
compiler/rustc_codegen_gcc/src/mono_item.rs
Normal file
59
compiler/rustc_codegen_gcc/src/mono_item.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use rustc_codegen_ssa::traits::PreDefineMethods;
|
||||
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
||||
use rustc_middle::mir::mono::{Linkage, Visibility};
|
||||
use rustc_middle::ty::{self, Instance, TypeFoldable};
|
||||
use rustc_middle::ty::layout::FnAbiExt;
|
||||
use rustc_span::def_id::DefId;
|
||||
use rustc_target::abi::LayoutOf;
|
||||
use rustc_target::abi::call::FnAbi;
|
||||
|
||||
use crate::base;
|
||||
use crate::context::CodegenCx;
|
||||
use crate::type_of::LayoutGccExt;
|
||||
|
||||
impl<'gcc, 'tcx> PreDefineMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
|
||||
fn predefine_static(&self, def_id: DefId, _linkage: Linkage, _visibility: Visibility, symbol_name: &str) {
|
||||
let attrs = self.tcx.codegen_fn_attrs(def_id);
|
||||
let instance = Instance::mono(self.tcx, def_id);
|
||||
let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
|
||||
let gcc_type = self.layout_of(ty).gcc_type(self, true);
|
||||
|
||||
let is_tls = attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL);
|
||||
let global = self.define_global(symbol_name, gcc_type, is_tls, attrs.link_section).unwrap_or_else(|| {
|
||||
self.sess().span_fatal(
|
||||
self.tcx.def_span(def_id),
|
||||
&format!("symbol `{}` is already defined", symbol_name),
|
||||
)
|
||||
});
|
||||
|
||||
// TODO
|
||||
/*unsafe {
|
||||
llvm::LLVMRustSetLinkage(global, base::linkage_to_llvm(linkage));
|
||||
llvm::LLVMRustSetVisibility(global, base::visibility_to_llvm(visibility));
|
||||
}*/
|
||||
|
||||
self.instances.borrow_mut().insert(instance, global);
|
||||
}
|
||||
|
||||
fn predefine_fn(&self, instance: Instance<'tcx>, linkage: Linkage, _visibility: Visibility, symbol_name: &str) {
|
||||
assert!(!instance.substs.needs_infer() && !instance.substs.has_param_types_or_consts());
|
||||
|
||||
let fn_abi = FnAbi::of_instance(self, instance, &[]);
|
||||
self.linkage.set(base::linkage_to_gcc(linkage));
|
||||
let _decl = self.declare_fn(symbol_name, &fn_abi);
|
||||
//let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
|
||||
|
||||
// TODO: call set_link_section() to allow initializing argc/argv.
|
||||
//base::set_link_section(decl, &attrs);
|
||||
/*if linkage == Linkage::LinkOnceODR || linkage == Linkage::WeakODR {
|
||||
llvm::SetUniqueComdat(self.llmod, decl);
|
||||
}*/
|
||||
|
||||
//debug!("predefine_fn: instance = {:?}", instance);
|
||||
|
||||
// TODO: use inline attribute from there in linkage.set() above:
|
||||
//attributes::from_fn_attrs(self, decl, instance);
|
||||
|
||||
//self.instances.borrow_mut().insert(instance, decl);
|
||||
}
|
||||
}
|
||||
355
compiler/rustc_codegen_gcc/src/type_.rs
Normal file
355
compiler/rustc_codegen_gcc/src/type_.rs
Normal file
@@ -0,0 +1,355 @@
|
||||
use std::convert::TryInto;
|
||||
|
||||
use gccjit::{RValue, Struct, Type};
|
||||
use rustc_codegen_ssa::traits::{BaseTypeMethods, DerivedTypeMethods};
|
||||
use rustc_codegen_ssa::common::TypeKind;
|
||||
use rustc_middle::bug;
|
||||
use rustc_middle::ty::layout::TyAndLayout;
|
||||
use rustc_target::abi::{AddressSpace, Align, Integer, Size};
|
||||
|
||||
use crate::common::TypeReflection;
|
||||
use crate::context::CodegenCx;
|
||||
use crate::type_of::LayoutGccExt;
|
||||
|
||||
impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
|
||||
pub fn type_ix(&self, num_bits: u64) -> Type<'gcc> {
|
||||
// gcc only supports 1, 2, 4 or 8-byte integers.
|
||||
let bytes = (num_bits / 8).next_power_of_two() as i32;
|
||||
match bytes {
|
||||
1 => self.i8_type,
|
||||
2 => self.i16_type,
|
||||
4 => self.i32_type,
|
||||
8 => self.i64_type,
|
||||
16 => self.i128_type,
|
||||
_ => panic!("unexpected num_bits: {}", num_bits),
|
||||
}
|
||||
/*
|
||||
let bytes = (num_bits / 8).next_power_of_two() as i32;
|
||||
println!("num_bits: {}, bytes: {}", num_bits, bytes);
|
||||
self.context.new_int_type(bytes, true) // TODO: check if it is indeed a signed integer.
|
||||
*/
|
||||
}
|
||||
|
||||
/*pub fn type_bool(&self) -> Type<'gcc> {
|
||||
self.bool_type
|
||||
}*/
|
||||
|
||||
pub fn type_void(&self) -> Type<'gcc> {
|
||||
self.context.new_type::<()>()
|
||||
}
|
||||
|
||||
pub fn type_size_t(&self) -> Type<'gcc> {
|
||||
self.context.new_type::<usize>()
|
||||
}
|
||||
|
||||
pub fn type_u8(&self) -> Type<'gcc> {
|
||||
self.u8_type
|
||||
}
|
||||
|
||||
pub fn type_u16(&self) -> Type<'gcc> {
|
||||
self.u16_type
|
||||
}
|
||||
|
||||
pub fn type_u32(&self) -> Type<'gcc> {
|
||||
self.u32_type
|
||||
}
|
||||
|
||||
pub fn type_u64(&self) -> Type<'gcc> {
|
||||
self.u64_type
|
||||
}
|
||||
|
||||
pub fn type_u128(&self) -> Type<'gcc> {
|
||||
self.u128_type
|
||||
}
|
||||
|
||||
pub fn type_pointee_for_align(&self, align: Align) -> Type<'gcc> {
|
||||
// FIXME(eddyb) We could find a better approximation if ity.align < align.
|
||||
let ity = Integer::approximate_align(self, align);
|
||||
self.type_from_integer(ity)
|
||||
}
|
||||
|
||||
/*pub fn type_int_from_ty(&self, t: ty::IntTy) -> Type<'gcc> {
|
||||
match t {
|
||||
ty::IntTy::Isize => self.type_isize(),
|
||||
ty::IntTy::I8 => self.type_i8(),
|
||||
ty::IntTy::I16 => self.type_i16(),
|
||||
ty::IntTy::I32 => self.type_i32(),
|
||||
ty::IntTy::I64 => self.type_i64(),
|
||||
ty::IntTy::I128 => self.type_i128(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn type_uint_from_ty(&self, t: ty::UintTy) -> Type<'gcc> {
|
||||
match t {
|
||||
ty::UintTy::Usize => self.type_isize(),
|
||||
ty::UintTy::U8 => self.type_i8(),
|
||||
ty::UintTy::U16 => self.type_i16(),
|
||||
ty::UintTy::U32 => self.type_i32(),
|
||||
ty::UintTy::U64 => self.type_i64(),
|
||||
ty::UintTy::U128 => self.type_i128(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn type_float_from_ty(&self, t: ty::FloatTy) -> Type<'gcc> {
|
||||
match t {
|
||||
ty::FloatTy::F32 => self.type_f32(),
|
||||
ty::FloatTy::F64 => self.type_f64(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn type_vector(&self, ty: Type<'gcc>, len: u64) -> Type<'gcc> {
|
||||
self.context.new_vector_type(ty, len)
|
||||
}*/
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
|
||||
fn type_i1(&self) -> Type<'gcc> {
|
||||
self.bool_type
|
||||
}
|
||||
|
||||
fn type_i8(&self) -> Type<'gcc> {
|
||||
self.i8_type
|
||||
}
|
||||
|
||||
fn type_i16(&self) -> Type<'gcc> {
|
||||
self.i16_type
|
||||
}
|
||||
|
||||
fn type_i32(&self) -> Type<'gcc> {
|
||||
self.i32_type
|
||||
}
|
||||
|
||||
fn type_i64(&self) -> Type<'gcc> {
|
||||
self.i64_type
|
||||
}
|
||||
|
||||
fn type_i128(&self) -> Type<'gcc> {
|
||||
self.i128_type
|
||||
}
|
||||
|
||||
fn type_isize(&self) -> Type<'gcc> {
|
||||
self.isize_type
|
||||
}
|
||||
|
||||
fn type_f32(&self) -> Type<'gcc> {
|
||||
self.context.new_type::<f32>()
|
||||
}
|
||||
|
||||
fn type_f64(&self) -> Type<'gcc> {
|
||||
self.context.new_type::<f64>()
|
||||
}
|
||||
|
||||
fn type_func(&self, params: &[Type<'gcc>], return_type: Type<'gcc>) -> Type<'gcc> {
|
||||
self.context.new_function_pointer_type(None, return_type, params, false)
|
||||
}
|
||||
|
||||
fn type_struct(&self, fields: &[Type<'gcc>], _packed: bool) -> Type<'gcc> {
|
||||
let types = fields.to_vec();
|
||||
if let Some(typ) = self.struct_types.borrow().get(fields) {
|
||||
return typ.clone();
|
||||
}
|
||||
let fields: Vec<_> = fields.iter().enumerate()
|
||||
.map(|(index, field)| self.context.new_field(None, *field, &format!("field{}_TODO", index)))
|
||||
.collect();
|
||||
// TODO: use packed.
|
||||
//let name = types.iter().map(|typ| format!("{:?}", typ)).collect::<Vec<_>>().join("_");
|
||||
//let typ = self.context.new_struct_type(None, format!("struct{}", name), &fields).as_type();
|
||||
let typ = self.context.new_struct_type(None, "struct", &fields).as_type();
|
||||
self.struct_types.borrow_mut().insert(types, typ);
|
||||
typ
|
||||
}
|
||||
|
||||
fn type_kind(&self, typ: Type<'gcc>) -> TypeKind {
|
||||
if typ.is_integral() {
|
||||
TypeKind::Integer
|
||||
}
|
||||
else if typ.is_vector().is_some() {
|
||||
TypeKind::Vector
|
||||
}
|
||||
else {
|
||||
// TODO
|
||||
TypeKind::Void
|
||||
}
|
||||
}
|
||||
|
||||
fn type_ptr_to(&self, ty: Type<'gcc>) -> Type<'gcc> {
|
||||
// TODO
|
||||
/*assert_ne!(self.type_kind(ty), TypeKind::Function,
|
||||
"don't call ptr_to on function types, use ptr_to_gcc_type on FnAbi instead"
|
||||
);*/
|
||||
ty.make_pointer()
|
||||
}
|
||||
|
||||
fn type_ptr_to_ext(&self, ty: Type<'gcc>, _address_space: AddressSpace) -> Type<'gcc> {
|
||||
// TODO: use address_space
|
||||
ty.make_pointer()
|
||||
}
|
||||
|
||||
fn element_type(&self, ty: Type<'gcc>) -> Type<'gcc> {
|
||||
if let Some(typ) = ty.is_array() {
|
||||
typ
|
||||
}
|
||||
else if let Some(vector_type) = ty.is_vector() {
|
||||
vector_type.get_element_type()
|
||||
}
|
||||
else if let Some(typ) = ty.get_pointee() {
|
||||
typ
|
||||
}
|
||||
else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
fn vector_length(&self, _ty: Type<'gcc>) -> usize {
|
||||
unimplemented!();
|
||||
//unsafe { llvm::LLVMGetVectorSize(ty) as usize }
|
||||
}
|
||||
|
||||
fn float_width(&self, typ: Type<'gcc>) -> usize {
|
||||
let f32 = self.context.new_type::<f32>();
|
||||
let f64 = self.context.new_type::<f64>();
|
||||
if typ == f32 {
|
||||
32
|
||||
}
|
||||
else if typ == f64 {
|
||||
64
|
||||
}
|
||||
else {
|
||||
panic!("Cannot get width of float type {:?}", typ);
|
||||
}
|
||||
// TODO: support other sizes.
|
||||
/*match self.type_kind(ty) {
|
||||
TypeKind::Float => 32,
|
||||
TypeKind::Double => 64,
|
||||
TypeKind::X86_FP80 => 80,
|
||||
TypeKind::FP128 | TypeKind::PPC_FP128 => 128,
|
||||
_ => bug!("llvm_float_width called on a non-float type"),
|
||||
}*/
|
||||
}
|
||||
|
||||
fn int_width(&self, typ: Type<'gcc>) -> u64 {
|
||||
if typ.is_i8(self) || typ.is_u8(self) {
|
||||
8
|
||||
}
|
||||
else if typ.is_i16(self) || typ.is_u16(self) {
|
||||
16
|
||||
}
|
||||
else if typ.is_i32(self) || typ.is_u32(self) {
|
||||
32
|
||||
}
|
||||
else if typ.is_i64(self) || typ.is_u64(self) {
|
||||
64
|
||||
}
|
||||
else if typ.is_i128(self) || typ.is_u128(self) {
|
||||
128
|
||||
}
|
||||
else {
|
||||
panic!("Cannot get width of int type {:?}", typ);
|
||||
}
|
||||
}
|
||||
|
||||
fn val_ty(&self, value: RValue<'gcc>) -> Type<'gcc> {
|
||||
value.get_type()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
|
||||
pub fn type_padding_filler(&self, size: Size, align: Align) -> Type<'gcc> {
|
||||
let unit = Integer::approximate_align(self, align);
|
||||
let size = size.bytes();
|
||||
let unit_size = unit.size().bytes();
|
||||
assert_eq!(size % unit_size, 0);
|
||||
self.type_array(self.type_from_integer(unit), size / unit_size)
|
||||
}
|
||||
|
||||
pub fn set_struct_body(&self, typ: Struct<'gcc>, fields: &[Type<'gcc>], _packed: bool) {
|
||||
// TODO: use packed.
|
||||
let fields: Vec<_> = fields.iter().enumerate()
|
||||
.map(|(index, field)| self.context.new_field(None, *field, &format!("field_{}", index)))
|
||||
.collect();
|
||||
typ.set_fields(None, &fields);
|
||||
}
|
||||
|
||||
/*fn type_struct(&self, fields: &[Type<'gcc>], packed: bool) -> Type<'gcc> {
|
||||
// TODO: use packed.
|
||||
let fields: Vec<_> = fields.iter().enumerate()
|
||||
.map(|(index, field)| self.context.new_field(None, *field, &format!("field_{}", index)))
|
||||
.collect();
|
||||
return self.context.new_struct_type(None, "unnamedStruct", &fields).as_type();
|
||||
}*/
|
||||
|
||||
pub fn type_named_struct(&self, name: &str) -> Struct<'gcc> {
|
||||
self.context.new_opaque_struct_type(None, name)
|
||||
}
|
||||
|
||||
pub fn type_array(&self, ty: Type<'gcc>, mut len: u64) -> Type<'gcc> {
|
||||
if let Some(struct_type) = ty.is_struct() {
|
||||
if struct_type.get_field_count() == 0 {
|
||||
// NOTE: since gccjit only supports i32 for the array size and libcore's tests uses a
|
||||
// size of usize::MAX in test_binary_search, we workaround this by setting the size to
|
||||
// zero for ZSTs.
|
||||
// FIXME: fix gccjit API.
|
||||
len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
let len: i32 = len.try_into().expect("array len");
|
||||
|
||||
self.context.new_array_type(None, ty, len)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn struct_fields<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLayout<'tcx>) -> (Vec<Type<'gcc>>, bool) {
|
||||
//debug!("struct_fields: {:#?}", layout);
|
||||
let field_count = layout.fields.count();
|
||||
|
||||
let mut packed = false;
|
||||
let mut offset = Size::ZERO;
|
||||
let mut prev_effective_align = layout.align.abi;
|
||||
let mut result: Vec<_> = Vec::with_capacity(1 + field_count * 2);
|
||||
for i in layout.fields.index_by_increasing_offset() {
|
||||
let target_offset = layout.fields.offset(i as usize);
|
||||
let field = layout.field(cx, i);
|
||||
let effective_field_align =
|
||||
layout.align.abi.min(field.align.abi).restrict_for_offset(target_offset);
|
||||
packed |= effective_field_align < field.align.abi;
|
||||
|
||||
/*debug!(
|
||||
"struct_fields: {}: {:?} offset: {:?} target_offset: {:?} \
|
||||
effective_field_align: {}",
|
||||
i,
|
||||
field,
|
||||
offset,
|
||||
target_offset,
|
||||
effective_field_align.bytes()
|
||||
);*/
|
||||
assert!(target_offset >= offset);
|
||||
let padding = target_offset - offset;
|
||||
let padding_align = prev_effective_align.min(effective_field_align);
|
||||
assert_eq!(offset.align_to(padding_align) + padding, target_offset);
|
||||
result.push(cx.type_padding_filler(padding, padding_align));
|
||||
//debug!(" padding before: {:?}", padding);
|
||||
|
||||
result.push(field.gcc_type(cx, !field.ty.is_any_ptr())); // FIXME: might need to check if the type is inside another, like Box<Type>.
|
||||
offset = target_offset + field.size;
|
||||
prev_effective_align = effective_field_align;
|
||||
}
|
||||
if !layout.is_unsized() && field_count > 0 {
|
||||
if offset > layout.size {
|
||||
bug!("layout: {:#?} stride: {:?} offset: {:?}", layout, layout.size, offset);
|
||||
}
|
||||
let padding = layout.size - offset;
|
||||
let padding_align = prev_effective_align;
|
||||
assert_eq!(offset.align_to(padding_align) + padding, layout.size);
|
||||
/*debug!(
|
||||
"struct_fields: pad_bytes: {:?} offset: {:?} stride: {:?}",
|
||||
padding, offset, layout.size
|
||||
);*/
|
||||
result.push(cx.type_padding_filler(padding, padding_align));
|
||||
assert_eq!(result.len(), 1 + field_count * 2);
|
||||
} else {
|
||||
//debug!("struct_fields: offset: {:?} stride: {:?}", offset, layout.size);
|
||||
}
|
||||
|
||||
(result, packed)
|
||||
}
|
||||
366
compiler/rustc_codegen_gcc/src/type_of.rs
Normal file
366
compiler/rustc_codegen_gcc/src/type_of.rs
Normal file
@@ -0,0 +1,366 @@
|
||||
use std::fmt::Write;
|
||||
|
||||
use gccjit::{Struct, Type};
|
||||
use crate::rustc_codegen_ssa::traits::{BaseTypeMethods, DerivedTypeMethods, LayoutTypeMethods};
|
||||
use rustc_middle::bug;
|
||||
use rustc_middle::ty::{self, Ty, TypeFoldable};
|
||||
use rustc_middle::ty::layout::{FnAbiExt, TyAndLayout};
|
||||
use rustc_middle::ty::print::with_no_trimmed_paths;
|
||||
use rustc_target::abi::{self, Abi, F32, F64, FieldsShape, Int, Integer, LayoutOf, Pointer, PointeeInfo, Size, TyAndLayoutMethods, Variants};
|
||||
use rustc_target::abi::call::{CastTarget, FnAbi, Reg};
|
||||
|
||||
use crate::abi::{FnAbiGccExt, GccType};
|
||||
use crate::context::CodegenCx;
|
||||
use crate::type_::struct_fields;
|
||||
|
||||
impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
|
||||
fn type_from_unsigned_integer(&self, i: Integer) -> Type<'gcc> {
|
||||
use Integer::*;
|
||||
match i {
|
||||
I8 => self.type_u8(),
|
||||
I16 => self.type_u16(),
|
||||
I32 => self.type_u32(),
|
||||
I64 => self.type_u64(),
|
||||
I128 => self.type_u128(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn uncached_gcc_type<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLayout<'tcx>, defer: &mut Option<(Struct<'gcc>, TyAndLayout<'tcx>)>) -> Type<'gcc> {
|
||||
match layout.abi {
|
||||
Abi::Scalar(_) => bug!("handled elsewhere"),
|
||||
Abi::Vector { ref element, count } => {
|
||||
let element = layout.scalar_gcc_type_at(cx, element, Size::ZERO);
|
||||
return cx.context.new_vector_type(element, count);
|
||||
},
|
||||
Abi::ScalarPair(..) => {
|
||||
return cx.type_struct(
|
||||
&[
|
||||
layout.scalar_pair_element_gcc_type(cx, 0, false),
|
||||
layout.scalar_pair_element_gcc_type(cx, 1, false),
|
||||
],
|
||||
false,
|
||||
);
|
||||
}
|
||||
Abi::Uninhabited | Abi::Aggregate { .. } => {}
|
||||
}
|
||||
|
||||
let name = match layout.ty.kind() {
|
||||
// FIXME(eddyb) producing readable type names for trait objects can result
|
||||
// in problematically distinct types due to HRTB and subtyping (see #47638).
|
||||
// ty::Dynamic(..) |
|
||||
ty::Adt(..) | ty::Closure(..) | ty::Foreign(..) | ty::Generator(..) | ty::Str
|
||||
if !cx.sess().fewer_names() =>
|
||||
{
|
||||
let mut name = with_no_trimmed_paths(|| layout.ty.to_string());
|
||||
if let (&ty::Adt(def, _), &Variants::Single { index }) =
|
||||
(layout.ty.kind(), &layout.variants)
|
||||
{
|
||||
if def.is_enum() && !def.variants.is_empty() {
|
||||
write!(&mut name, "::{}", def.variants[index].ident).unwrap();
|
||||
}
|
||||
}
|
||||
if let (&ty::Generator(_, _, _), &Variants::Single { index }) =
|
||||
(layout.ty.kind(), &layout.variants)
|
||||
{
|
||||
write!(&mut name, "::{}", ty::GeneratorSubsts::variant_name(index)).unwrap();
|
||||
}
|
||||
Some(name)
|
||||
}
|
||||
ty::Adt(..) => {
|
||||
// If `Some` is returned then a named struct is created in LLVM. Name collisions are
|
||||
// avoided by LLVM (with increasing suffixes). If rustc doesn't generate names then that
|
||||
// can improve perf.
|
||||
// FIXME: I don't think that's true for libgccjit.
|
||||
Some(String::new())
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
|
||||
match layout.fields {
|
||||
FieldsShape::Primitive | FieldsShape::Union(_) => {
|
||||
let fill = cx.type_padding_filler(layout.size, layout.align.abi);
|
||||
let packed = false;
|
||||
match name {
|
||||
None => cx.type_struct(&[fill], packed),
|
||||
Some(ref name) => {
|
||||
let gcc_type = cx.type_named_struct(name);
|
||||
cx.set_struct_body(gcc_type, &[fill], packed);
|
||||
gcc_type.as_type()
|
||||
},
|
||||
}
|
||||
}
|
||||
FieldsShape::Array { count, .. } => cx.type_array(layout.field(cx, 0).gcc_type(cx, true), count),
|
||||
FieldsShape::Arbitrary { .. } =>
|
||||
match name {
|
||||
None => {
|
||||
let (gcc_fields, packed) = struct_fields(cx, layout);
|
||||
cx.type_struct(&gcc_fields, packed)
|
||||
},
|
||||
Some(ref name) => {
|
||||
let gcc_type = cx.type_named_struct(name);
|
||||
*defer = Some((gcc_type, layout));
|
||||
gcc_type.as_type()
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub trait LayoutGccExt<'tcx> {
|
||||
fn is_gcc_immediate(&self) -> bool;
|
||||
fn is_gcc_scalar_pair(&self) -> bool;
|
||||
fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, set_fields: bool) -> Type<'gcc>;
|
||||
fn immediate_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
|
||||
fn scalar_gcc_type_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, scalar: &abi::Scalar, offset: Size) -> Type<'gcc>;
|
||||
fn scalar_pair_element_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, index: usize, immediate: bool) -> Type<'gcc>;
|
||||
fn gcc_field_index(&self, index: usize) -> u64;
|
||||
fn pointee_info_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, offset: Size) -> Option<PointeeInfo>;
|
||||
}
|
||||
|
||||
impl<'tcx> LayoutGccExt<'tcx> for TyAndLayout<'tcx> {
|
||||
fn is_gcc_immediate(&self) -> bool {
|
||||
match self.abi {
|
||||
Abi::Scalar(_) | Abi::Vector { .. } => true,
|
||||
Abi::ScalarPair(..) => false,
|
||||
Abi::Uninhabited | Abi::Aggregate { .. } => self.is_zst(),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_gcc_scalar_pair(&self) -> bool {
|
||||
match self.abi {
|
||||
Abi::ScalarPair(..) => true,
|
||||
Abi::Uninhabited | Abi::Scalar(_) | Abi::Vector { .. } | Abi::Aggregate { .. } => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the GCC type corresponding to a Rust type, i.e., `rustc_middle::ty::Ty`.
|
||||
/// The pointee type of the pointer in `PlaceRef` is always this type.
|
||||
/// For sized types, it is also the right LLVM type for an `alloca`
|
||||
/// containing a value of that type, and most immediates (except `bool`).
|
||||
/// Unsized types, however, are represented by a "minimal unit", e.g.
|
||||
/// `[T]` becomes `T`, while `str` and `Trait` turn into `i8` - this
|
||||
/// is useful for indexing slices, as `&[T]`'s data pointer is `T*`.
|
||||
/// If the type is an unsized struct, the regular layout is generated,
|
||||
/// with the inner-most trailing unsized field using the "minimal unit"
|
||||
/// of that field's type - this is useful for taking the address of
|
||||
/// that field and ensuring the struct has the right alignment.
|
||||
fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, set_fields: bool) -> Type<'gcc> {
|
||||
if let Abi::Scalar(ref scalar) = self.abi {
|
||||
// Use a different cache for scalars because pointers to DSTs
|
||||
// can be either fat or thin (data pointers of fat pointers).
|
||||
if let Some(&ty) = cx.scalar_types.borrow().get(&self.ty) {
|
||||
return ty;
|
||||
}
|
||||
let ty =
|
||||
match *self.ty.kind() {
|
||||
ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {
|
||||
cx.type_ptr_to(cx.layout_of(ty).gcc_type(cx, set_fields))
|
||||
}
|
||||
ty::Adt(def, _) if def.is_box() => {
|
||||
cx.type_ptr_to(cx.layout_of(self.ty.boxed_ty()).gcc_type(cx, true))
|
||||
}
|
||||
ty::FnPtr(sig) => cx.fn_ptr_backend_type(&FnAbi::of_fn_ptr(cx, sig, &[])),
|
||||
_ => self.scalar_gcc_type_at(cx, scalar, Size::ZERO),
|
||||
};
|
||||
cx.scalar_types.borrow_mut().insert(self.ty, ty);
|
||||
return ty;
|
||||
}
|
||||
|
||||
// Check the cache.
|
||||
let variant_index =
|
||||
match self.variants {
|
||||
Variants::Single { index } => Some(index),
|
||||
_ => None,
|
||||
};
|
||||
let cached_type = cx.types.borrow().get(&(self.ty, variant_index)).cloned();
|
||||
if let Some(ty) = cached_type {
|
||||
let type_to_set_fields = cx.types_with_fields_to_set.borrow_mut().remove(&ty);
|
||||
if let Some((struct_type, layout)) = type_to_set_fields {
|
||||
// Since we might be trying to generate a type containing another type which is not
|
||||
// completely generated yet, we deferred setting the fields until now.
|
||||
let (fields, packed) = struct_fields(cx, layout);
|
||||
cx.set_struct_body(struct_type, &fields, packed);
|
||||
}
|
||||
return ty;
|
||||
}
|
||||
|
||||
//debug!("gcc_type({:#?})", self);
|
||||
|
||||
assert!(!self.ty.has_escaping_bound_vars(), "{:?} has escaping bound vars", self.ty);
|
||||
|
||||
// Make sure lifetimes are erased, to avoid generating distinct LLVM
|
||||
// types for Rust types that only differ in the choice of lifetimes.
|
||||
let normal_ty = cx.tcx.erase_regions(self.ty);
|
||||
|
||||
let mut defer = None;
|
||||
let ty =
|
||||
if self.ty != normal_ty {
|
||||
let mut layout = cx.layout_of(normal_ty);
|
||||
if let Some(v) = variant_index {
|
||||
layout = layout.for_variant(cx, v);
|
||||
}
|
||||
layout.gcc_type(cx, true)
|
||||
}
|
||||
else {
|
||||
uncached_gcc_type(cx, *self, &mut defer)
|
||||
};
|
||||
//debug!("--> mapped {:#?} to ty={:?}", self, ty);
|
||||
|
||||
cx.types.borrow_mut().insert((self.ty, variant_index), ty);
|
||||
|
||||
if let Some((ty, layout)) = defer {
|
||||
//TODO: do we still need this conditions and the set_fields parameter?
|
||||
//if set_fields {
|
||||
let (fields, packed) = struct_fields(cx, layout);
|
||||
cx.set_struct_body(ty, &fields, packed);
|
||||
/*}
|
||||
else {
|
||||
// Since we might be trying to generate a type containing another type which is not
|
||||
// completely generated yet, we don't set the fields right now, but we save the
|
||||
// type to set the fields later.
|
||||
cx.types_with_fields_to_set.borrow_mut().insert(ty.as_type(), (ty, layout));
|
||||
}*/
|
||||
}
|
||||
|
||||
ty
|
||||
}
|
||||
|
||||
fn immediate_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
|
||||
if let Abi::Scalar(ref scalar) = self.abi {
|
||||
if scalar.is_bool() {
|
||||
return cx.type_i1();
|
||||
}
|
||||
}
|
||||
self.gcc_type(cx, true)
|
||||
}
|
||||
|
||||
fn scalar_gcc_type_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, scalar: &abi::Scalar, offset: Size) -> Type<'gcc> {
|
||||
match scalar.value {
|
||||
Int(i, true) => cx.type_from_integer(i),
|
||||
Int(i, false) => cx.type_from_unsigned_integer(i),
|
||||
F32 => cx.type_f32(),
|
||||
F64 => cx.type_f64(),
|
||||
Pointer => {
|
||||
// If we know the alignment, pick something better than i8.
|
||||
let pointee =
|
||||
if let Some(pointee) = self.pointee_info_at(cx, offset) {
|
||||
cx.type_pointee_for_align(pointee.align)
|
||||
}
|
||||
else {
|
||||
cx.type_i8()
|
||||
};
|
||||
cx.type_ptr_to(pointee)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn scalar_pair_element_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, index: usize, immediate: bool) -> Type<'gcc> {
|
||||
// TODO: remove llvm hack:
|
||||
// HACK(eddyb) special-case fat pointers until LLVM removes
|
||||
// pointee types, to avoid bitcasting every `OperandRef::deref`.
|
||||
match self.ty.kind() {
|
||||
ty::Ref(..) | ty::RawPtr(_) => {
|
||||
return self.field(cx, index).gcc_type(cx, true);
|
||||
}
|
||||
ty::Adt(def, _) if def.is_box() => {
|
||||
let ptr_ty = cx.tcx.mk_mut_ptr(self.ty.boxed_ty());
|
||||
return cx.layout_of(ptr_ty).scalar_pair_element_gcc_type(cx, index, immediate);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let (a, b) = match self.abi {
|
||||
Abi::ScalarPair(ref a, ref b) => (a, b),
|
||||
_ => bug!("TyAndLayout::scalar_pair_element_llty({:?}): not applicable", self),
|
||||
};
|
||||
let scalar = [a, b][index];
|
||||
|
||||
// Make sure to return the same type `immediate_gcc_type` would when
|
||||
// dealing with an immediate pair. This means that `(bool, bool)` is
|
||||
// effectively represented as `{i8, i8}` in memory and two `i1`s as an
|
||||
// immediate, just like `bool` is typically `i8` in memory and only `i1`
|
||||
// when immediate. We need to load/store `bool` as `i8` to avoid
|
||||
// crippling LLVM optimizations or triggering other LLVM bugs with `i1`.
|
||||
// TODO: this bugs certainly don't happen in this case since the bool type is used instead of i1.
|
||||
if /*immediate &&*/ scalar.is_bool() {
|
||||
return cx.type_i1();
|
||||
}
|
||||
|
||||
let offset =
|
||||
if index == 0 {
|
||||
Size::ZERO
|
||||
}
|
||||
else {
|
||||
a.value.size(cx).align_to(b.value.align(cx).abi)
|
||||
};
|
||||
self.scalar_gcc_type_at(cx, scalar, offset)
|
||||
}
|
||||
|
||||
fn gcc_field_index(&self, index: usize) -> u64 {
|
||||
match self.abi {
|
||||
Abi::Scalar(_) | Abi::ScalarPair(..) => {
|
||||
bug!("TyAndLayout::gcc_field_index({:?}): not applicable", self)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
match self.fields {
|
||||
FieldsShape::Primitive | FieldsShape::Union(_) => {
|
||||
bug!("TyAndLayout::gcc_field_index({:?}): not applicable", self)
|
||||
}
|
||||
|
||||
FieldsShape::Array { .. } => index as u64,
|
||||
|
||||
FieldsShape::Arbitrary { .. } => 1 + (self.fields.memory_index(index) as u64) * 2,
|
||||
}
|
||||
}
|
||||
|
||||
fn pointee_info_at<'a>(&self, cx: &CodegenCx<'a, 'tcx>, offset: Size) -> Option<PointeeInfo> {
|
||||
if let Some(&pointee) = cx.pointee_infos.borrow().get(&(self.ty, offset)) {
|
||||
return pointee;
|
||||
}
|
||||
|
||||
let result = Ty::pointee_info_at(*self, cx, offset);
|
||||
|
||||
cx.pointee_infos.borrow_mut().insert((self.ty, offset), result);
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gcc, 'tcx> LayoutTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
|
||||
fn backend_type(&self, layout: TyAndLayout<'tcx>) -> Type<'gcc> {
|
||||
layout.gcc_type(self, true)
|
||||
}
|
||||
|
||||
fn immediate_backend_type(&self, layout: TyAndLayout<'tcx>) -> Type<'gcc> {
|
||||
layout.immediate_gcc_type(self)
|
||||
}
|
||||
|
||||
fn is_backend_immediate(&self, layout: TyAndLayout<'tcx>) -> bool {
|
||||
layout.is_gcc_immediate()
|
||||
}
|
||||
|
||||
fn is_backend_scalar_pair(&self, layout: TyAndLayout<'tcx>) -> bool {
|
||||
layout.is_gcc_scalar_pair()
|
||||
}
|
||||
|
||||
fn backend_field_index(&self, layout: TyAndLayout<'tcx>, index: usize) -> u64 {
|
||||
layout.gcc_field_index(index)
|
||||
}
|
||||
|
||||
fn scalar_pair_element_backend_type(&self, layout: TyAndLayout<'tcx>, index: usize, immediate: bool) -> Type<'gcc> {
|
||||
layout.scalar_pair_element_gcc_type(self, index, immediate)
|
||||
}
|
||||
|
||||
fn cast_backend_type(&self, ty: &CastTarget) -> Type<'gcc> {
|
||||
ty.gcc_type(self)
|
||||
}
|
||||
|
||||
fn fn_ptr_backend_type(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Type<'gcc> {
|
||||
fn_abi.ptr_to_gcc_type(self)
|
||||
}
|
||||
|
||||
fn reg_backend_type(&self, _ty: &Reg) -> Type<'gcc> {
|
||||
unimplemented!();
|
||||
//ty.gcc_type(self)
|
||||
}
|
||||
}
|
||||
179
compiler/rustc_codegen_gcc/src/va_arg.rs
Normal file
179
compiler/rustc_codegen_gcc/src/va_arg.rs
Normal file
@@ -0,0 +1,179 @@
|
||||
/*use gccjit::{RValue, ToRValue, Type};
|
||||
use rustc_codegen_ssa::mir::operand::OperandRef;
|
||||
use rustc_codegen_ssa::{
|
||||
common::IntPredicate,
|
||||
traits::{BaseTypeMethods, BuilderMethods, ConstMethods, DerivedTypeMethods},
|
||||
};
|
||||
use rustc_middle::ty::layout::HasTyCtxt;
|
||||
use rustc_middle::ty::Ty;
|
||||
use rustc_target::abi::{Align, Endian, HasDataLayout, LayoutOf, Size};
|
||||
|
||||
use crate::builder::Builder;
|
||||
use crate::type_of::LayoutGccExt;
|
||||
|
||||
fn round_pointer_up_to_alignment<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>, addr: RValue<'gcc>, align: Align, ptr_ty: Type<'gcc>) -> RValue<'gcc> {
|
||||
let mut ptr_as_int = bx.ptrtoint(addr, bx.cx().type_isize());
|
||||
ptr_as_int = bx.add(ptr_as_int, bx.cx().const_i32(align.bytes() as i32 - 1));
|
||||
ptr_as_int = bx.and(ptr_as_int, bx.cx().const_i32(-(align.bytes() as i32)));
|
||||
bx.inttoptr(ptr_as_int, ptr_ty)
|
||||
}
|
||||
|
||||
fn emit_direct_ptr_va_arg<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>, list: OperandRef<'tcx, RValue<'gcc>>, llty: Type<'gcc>, size: Size, align: Align, slot_size: Align, allow_higher_align: bool) -> (RValue<'gcc>, Align) {
|
||||
let va_list_ptr_ty = bx.cx().type_ptr_to(bx.cx.type_i8p());
|
||||
let va_list_addr =
|
||||
if list.layout.gcc_type(bx.cx, true) != va_list_ptr_ty {
|
||||
bx.bitcast(list.immediate(), va_list_ptr_ty)
|
||||
}
|
||||
else {
|
||||
list.immediate()
|
||||
};
|
||||
|
||||
let ptr = bx.load(va_list_addr, bx.tcx().data_layout.pointer_align.abi);
|
||||
|
||||
let (addr, addr_align) = if allow_higher_align && align > slot_size {
|
||||
(round_pointer_up_to_alignment(bx, ptr, align, bx.cx().type_i8p()), align)
|
||||
} else {
|
||||
(ptr, slot_size)
|
||||
};
|
||||
|
||||
let aligned_size = size.align_to(slot_size).bytes() as i32;
|
||||
let full_direct_size = bx.cx().const_i32(aligned_size);
|
||||
let next = bx.inbounds_gep(addr, &[full_direct_size]);
|
||||
bx.store(next, va_list_addr, bx.tcx().data_layout.pointer_align.abi);
|
||||
|
||||
if size.bytes() < slot_size.bytes() && bx.tcx().sess.target.endian == Endian::Big {
|
||||
let adjusted_size = bx.cx().const_i32((slot_size.bytes() - size.bytes()) as i32);
|
||||
let adjusted = bx.inbounds_gep(addr, &[adjusted_size]);
|
||||
(bx.bitcast(adjusted, bx.cx().type_ptr_to(llty)), addr_align)
|
||||
} else {
|
||||
(bx.bitcast(addr, bx.cx().type_ptr_to(llty)), addr_align)
|
||||
}
|
||||
}
|
||||
|
||||
fn emit_ptr_va_arg<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>, list: OperandRef<'tcx, RValue<'gcc>>, target_ty: Ty<'tcx>, indirect: bool, slot_size: Align, allow_higher_align: bool) -> RValue<'gcc> {
|
||||
let layout = bx.cx.layout_of(target_ty);
|
||||
let (llty, size, align) =
|
||||
if indirect {
|
||||
(
|
||||
bx.cx.layout_of(bx.cx.tcx.mk_imm_ptr(target_ty)).gcc_type(bx.cx, true),
|
||||
bx.cx.data_layout().pointer_size,
|
||||
bx.cx.data_layout().pointer_align,
|
||||
)
|
||||
}
|
||||
else {
|
||||
(layout.gcc_type(bx.cx, true), layout.size, layout.align)
|
||||
};
|
||||
let (addr, addr_align) = emit_direct_ptr_va_arg(bx, list, llty, size, align.abi, slot_size, allow_higher_align);
|
||||
if indirect {
|
||||
let tmp_ret = bx.load(addr, addr_align);
|
||||
bx.load(tmp_ret, align.abi)
|
||||
}
|
||||
else {
|
||||
bx.load(addr, addr_align)
|
||||
}
|
||||
}
|
||||
|
||||
fn emit_aapcs_va_arg<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>, list: OperandRef<'tcx, RValue<'gcc>>, target_ty: Ty<'tcx>) -> RValue<'gcc> {
|
||||
// Implementation of the AAPCS64 calling convention for va_args see
|
||||
// https://github.com/ARM-software/abi-aa/blob/master/aapcs64/aapcs64.rst
|
||||
let va_list_addr = list.immediate();
|
||||
let layout = bx.cx.layout_of(target_ty);
|
||||
let gcc_type = layout.immediate_gcc_type(bx);
|
||||
|
||||
let function = bx.llbb().get_function();
|
||||
let variable = function.new_local(None, gcc_type, "va_arg");
|
||||
|
||||
let mut maybe_reg = bx.build_sibling_block("va_arg.maybe_reg");
|
||||
let mut in_reg = bx.build_sibling_block("va_arg.in_reg");
|
||||
let mut on_stack = bx.build_sibling_block("va_arg.on_stack");
|
||||
let end = bx.build_sibling_block("va_arg.end");
|
||||
let zero = bx.const_i32(0);
|
||||
let offset_align = Align::from_bytes(4).unwrap();
|
||||
assert!(bx.tcx().sess.target.endian == Endian::Little);
|
||||
|
||||
let gr_type = target_ty.is_any_ptr() || target_ty.is_integral();
|
||||
let (reg_off, reg_top_index, slot_size) = if gr_type {
|
||||
let gr_offs = bx.struct_gep(va_list_addr, 7);
|
||||
let nreg = (layout.size.bytes() + 7) / 8;
|
||||
(gr_offs, 3, nreg * 8)
|
||||
} else {
|
||||
let vr_off = bx.struct_gep(va_list_addr, 9);
|
||||
let nreg = (layout.size.bytes() + 15) / 16;
|
||||
(vr_off, 5, nreg * 16)
|
||||
};
|
||||
|
||||
// if the offset >= 0 then the value will be on the stack
|
||||
let mut reg_off_v = bx.load(reg_off, offset_align);
|
||||
let use_stack = bx.icmp(IntPredicate::IntSGE, reg_off_v, zero);
|
||||
bx.cond_br(use_stack, on_stack.llbb(), maybe_reg.llbb());
|
||||
|
||||
// The value at this point might be in a register, but there is a chance that
|
||||
// it could be on the stack so we have to update the offset and then check
|
||||
// the offset again.
|
||||
|
||||
if gr_type && layout.align.abi.bytes() > 8 {
|
||||
reg_off_v = maybe_reg.add(reg_off_v, bx.const_i32(15));
|
||||
reg_off_v = maybe_reg.and(reg_off_v, bx.const_i32(-16));
|
||||
}
|
||||
let new_reg_off_v = maybe_reg.add(reg_off_v, bx.const_i32(slot_size as i32));
|
||||
|
||||
maybe_reg.store(new_reg_off_v, reg_off, offset_align);
|
||||
|
||||
// Check to see if we have overflowed the registers as a result of this.
|
||||
// If we have then we need to use the stack for this value
|
||||
let use_stack = maybe_reg.icmp(IntPredicate::IntSGT, new_reg_off_v, zero);
|
||||
maybe_reg.cond_br(use_stack, on_stack.llbb(), in_reg.llbb());
|
||||
|
||||
let top = in_reg.struct_gep(va_list_addr, reg_top_index);
|
||||
let top = in_reg.load(top, bx.tcx().data_layout.pointer_align.abi);
|
||||
|
||||
// reg_value = *(@top + reg_off_v);
|
||||
let top = in_reg.gep(top, &[reg_off_v]);
|
||||
let top = in_reg.bitcast(top, bx.cx.type_ptr_to(layout.gcc_type(bx, true)));
|
||||
let reg_value = in_reg.load(top, layout.align.abi);
|
||||
in_reg.assign(variable, reg_value);
|
||||
in_reg.br(end.llbb());
|
||||
|
||||
// On Stack block
|
||||
let stack_value =
|
||||
emit_ptr_va_arg(&mut on_stack, list, target_ty, false, Align::from_bytes(8).unwrap(), true);
|
||||
on_stack.assign(variable, stack_value);
|
||||
on_stack.br(end.llbb());
|
||||
|
||||
*bx = end;
|
||||
variable.to_rvalue()
|
||||
}
|
||||
|
||||
pub(super) fn emit_va_arg<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>, addr: OperandRef<'tcx, RValue<'gcc>>, target_ty: Ty<'tcx>) -> RValue<'gcc> {
|
||||
// Determine the va_arg implementation to use. The LLVM va_arg instruction
|
||||
// is lacking in some instances, so we should only use it as a fallback.
|
||||
let target = &bx.cx.tcx.sess.target;
|
||||
let arch = &bx.cx.tcx.sess.target.arch;
|
||||
match &**arch {
|
||||
// Windows x86
|
||||
"x86" if target.options.is_like_windows => {
|
||||
emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(4).unwrap(), false)
|
||||
}
|
||||
// Generic x86
|
||||
"x86" => emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(4).unwrap(), true),
|
||||
// Windows AArch64
|
||||
"aarch64" if target.options.is_like_windows => {
|
||||
emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(8).unwrap(), false)
|
||||
}
|
||||
// macOS / iOS AArch64
|
||||
"aarch64" if target.options.is_like_osx => {
|
||||
emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(8).unwrap(), true)
|
||||
}
|
||||
"aarch64" => emit_aapcs_va_arg(bx, addr, target_ty),
|
||||
// Windows x86_64
|
||||
"x86_64" if target.options.is_like_windows => {
|
||||
let target_ty_size = bx.cx.size_of(target_ty).bytes();
|
||||
let indirect: bool = target_ty_size > 8 || !target_ty_size.is_power_of_two();
|
||||
emit_ptr_va_arg(bx, addr, target_ty, indirect, Align::from_bytes(8).unwrap(), false)
|
||||
}
|
||||
// For all other architecture/OS combinations fall back to using
|
||||
// the LLVM va_arg instruction.
|
||||
// https://llvm.org/docs/LangRef.html#va-arg-instruction
|
||||
_ => bx.va_arg(addr.immediate(), bx.cx.layout_of(target_ty).gcc_type(bx.cx, true)),
|
||||
}
|
||||
}*/
|
||||
Reference in New Issue
Block a user