This commit is contained in:
Antoni Boucher 2023-01-08 11:42:00 -05:00
parent a4b74e3adf
commit 70659f7591
11 changed files with 53 additions and 108 deletions

View File

@ -177,7 +177,7 @@ To have the correct file paths in `gdb` instead of `/usr/src/debug/gcc/libstdc++
Maybe by calling the following at the beginning of gdb:
```
set substitute-path /usr/src/debug/gcc /home/bouanto/Ordinateur/Programmation/Projets/gcc-repo/gcc
set substitute-path /usr/src/debug/gcc /path/to/gcc-repo/gcc
```
TODO: but that's not what I remember I was doing.

View File

@ -1,4 +1,4 @@
#![feature(start, box_syntax, core_intrinsics, alloc_error_handler)]
#![feature(start, box_syntax, core_intrinsics, alloc_error_handler, lang_items)]
#![no_std]
extern crate alloc;
@ -26,6 +26,16 @@ fn alloc_error_handler(_: alloc::alloc::Layout) -> ! {
core::intrinsics::abort();
}
#[lang = "eh_personality"]
fn eh_personality() -> ! {
loop {}
}
#[no_mangle]
unsafe extern "C" fn _Unwind_Resume() {
core::intrinsics::unreachable();
}
#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
let world: Box<&str> = box "Hello World!\0";

View File

@ -352,7 +352,7 @@ fn codegen_inline_asm(&mut self, template: &[InlineAsmTemplatePiece], rust_opera
inputs.push(AsmInOperand {
constraint: "X".into(),
rust_idx,
val: get_fn(self.cx, instance, false).get_address(None),
val: get_fn(self.cx, instance).get_address(None),
});
}
@ -738,7 +738,7 @@ fn codegen_global_asm(&self, template: &[InlineAsmTemplatePiece], operands: &[Gl
}
GlobalAsmOperandRef::SymFn { instance } => {
let function = get_fn(self, instance, false);
let function = get_fn(self, instance);
self.add_used_function(function);
// TODO(@Amanieu): Additional mangling is needed on
// some targets to add a leading underscore (Mach-O)

View File

@ -91,12 +91,6 @@ fn module_codegen(tcx: TyCtxt<'_>, (cgu_name, supports_128bit_integers): (Symbol
context.add_command_line_option("-fexceptions");
context.add_driver_option("-fexceptions");
/*context.add_command_line_option("-fasynchronous-unwind-tables");
context.add_driver_option("-fasynchronous-unwind-tables");
context.add_command_line_option("-funwind-tables");
context.add_driver_option("-funwind-tables");*/
// TODO(antoyo): only set on x86 platforms.
context.add_command_line_option("-masm=intel");
// TODO(antoyo): only add the following cli argument if the feature is supported.
@ -156,8 +150,7 @@ fn module_codegen(tcx: TyCtxt<'_>, (cgu_name, supports_128bit_integers): (Symbol
context.set_keep_intermediates(true);
}
// TODO(bjorn3): Remove once unwinding is properly implemented
// TODO: remove.
// NOTE: The codegen generates unrechable blocks.
context.set_allow_unreachable_blocks(true);
{

View File

@ -457,7 +457,7 @@ fn invoke(&mut self, typ: Type<'gcc>, func: RValue<'gcc>, args: &[RValue<'gcc>],
let current_block = self.block.clone();
self.block = try_block;
let call = self.call(typ, func, args, None); // TODO: use funclet here?
let call = self.call(typ, func, args, None); // TODO(antoyo): use funclet here?
self.block = current_block;
let return_value = self.current_func()
@ -471,8 +471,6 @@ fn invoke(&mut self, typ: Type<'gcc>, func: RValue<'gcc>, args: &[RValue<'gcc>],
self.block.add_try_finally(None, try_block, catch);
}
else {
// FIXME: FIXME: FIXME: Seems like bad (_URC_NO_REASON) return code, perhaps because the cleanup pad was created properly.
println!("Try/catch in {:?}", self.current_func());
self.block.add_try_catch(None, try_block, catch);
}
@ -1197,26 +1195,16 @@ fn insert_value(&mut self, aggregate_value: RValue<'gcc>, value: RValue<'gcc>, i
}
fn set_personality_fn(&mut self, personality: RValue<'gcc>) {
let personality = self.rvalue_as_function(personality); // FIXME: why calling
//rvalue_as_function doesn't work?
//let personality = unsafe { std::mem::transmute(personality) };
let personality = self.rvalue_as_function(personality);
#[cfg(feature="master")]
self.current_func().set_personality_function(personality);
// FIXME: rustc manages to generate the symbol DW.ref.rust_eh_personality multiple times
// for the same asm file, which causes an assembler error.
}
fn cleanup_landing_pad(&mut self, _ty: Type<'gcc>, pers_fn: RValue<'gcc>) -> RValue<'gcc> {
self.set_personality_fn(pers_fn);
/*
* Matching GCC exception handling with LLVM:
*
* GCC LLVM
* CATCH_EXPR landing pad catch clause
* TRY_FINALLY_EXPR cleanup
*/
// NOTE: insert the current block in a variable so that a later call to invoke knows to
// generate a try/finally instead of a try/catch for this block.
self.cleanup_blocks.borrow_mut().insert(self.block);
let eh_pointer_builtin = self.cx.context.get_target_builtin_function("__builtin_eh_pointer");

View File

@ -1,11 +1,9 @@
#[cfg(feature="master")]
use gccjit::{FnAttribute, Visibility};
use gccjit::{FunctionType, RValue, Function};
use rustc_codegen_ssa::traits::BaseTypeMethods;
use gccjit::{FunctionType, Function};
use rustc_middle::ty::{self, Instance, TypeVisitable};
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt};
use crate::abi::FnAbiGccExt;
use crate::attributes;
use crate::context::CodegenCx;
@ -16,7 +14,7 @@
///
/// - `cx`: the crate context
/// - `instance`: the instance to be instantiated
pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>, dont_cache: bool) -> Function<'gcc> {
pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>) -> Function<'gcc> {
let tcx = cx.tcx();
assert!(!instance.substs.needs_infer());
@ -31,7 +29,9 @@ pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>,
let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
let func =
if let Some(func) = cx.get_declared_value(&sym) {
if let Some(_func) = cx.get_declared_value(&sym) {
// FIXME: we never reach this because get_declared_value only returns global variables
// and here we try to get a function.
unreachable!();
/*
// Create a fn pointer with the new signature.
@ -70,10 +70,7 @@ pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>,
}
else {
cx.linkage.set(FunctionType::Extern);
/*if sym == "rust_eh_personality" {
panic!();
}*/
let func = cx.declare_fn(&sym, &fn_abi, dont_cache);
let func = cx.declare_fn(&sym, &fn_abi);
attributes::from_fn_attrs(cx, func, instance);
@ -171,9 +168,7 @@ pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>,
func
};
//if !dont_cache {
cx.function_instances.borrow_mut().insert(instance, func);
//}
func
}

View File

@ -1,6 +1,6 @@
use std::cell::{Cell, RefCell};
use gccjit::{Block, CType, Context, Function, FunctionPtrType, FunctionType, LValue, RValue, Type, FnAttribute};
use gccjit::{Block, CType, Context, Function, FunctionPtrType, FunctionType, LValue, RValue, Type};
use rustc_codegen_ssa::base::wants_msvc_seh;
use rustc_codegen_ssa::traits::{
BackendTypes,
@ -259,8 +259,8 @@ pub fn new(context: &'gcc Context<'gcc>, codegen_unit: &'tcx CodegenUnit<'tcx>,
pub fn rvalue_as_function(&self, value: RValue<'gcc>) -> Function<'gcc> {
let function: Function<'gcc> = unsafe { std::mem::transmute(value) };
// FIXME: seems like self.functions get overwritten for rust_eh_personality.
/*debug_assert!(self.functions.borrow().values().find(|value| **value == function).is_some(),
"{:?} is not a function", function);*/
debug_assert!(self.functions.borrow().values().find(|value| **value == function).is_some(),
"{:?} is not a function", function);
function
}
@ -332,7 +332,7 @@ fn vtables(&self) -> &RefCell<FxHashMap<(Ty<'tcx>, Option<PolyExistentialTraitRe
}
fn get_fn(&self, instance: Instance<'tcx>) -> RValue<'gcc> {
let func = get_fn(self, instance, false);
let func = get_fn(self, instance);
*self.current_func.borrow_mut() = Some(func);
unsafe { std::mem::transmute(func) }
}
@ -345,7 +345,7 @@ fn get_fn_addr(&self, instance: Instance<'tcx>) -> RValue<'gcc> {
self.intrinsics.borrow()[func_name].clone()
}
else {
get_fn(self, instance, false)
get_fn(self, instance)
};
let ptr = func.get_address(None);
@ -386,8 +386,6 @@ fn eh_personality(&self) -> RValue<'gcc> {
let func =
match tcx.lang_items().eh_personality() {
Some(def_id) if !wants_msvc_seh(self.sess()) => {
// FIXME: this create an instance into self.functions and prevent the creating
// of the function defined in std.
let instance =
ty::Instance::resolve(
tcx,
@ -400,45 +398,19 @@ fn eh_personality(&self) -> RValue<'gcc> {
let symbol_name = tcx.symbol_name(instance).name;
let fn_abi = self.fn_abi_of_instance(instance, ty::List::empty());
self.linkage.set(FunctionType::Extern);
let func = self.declare_fn(symbol_name, &fn_abi, false);
//func.add_attribute(FnAttribute::Weak);
/*let block = func.new_block("eh_personality_block");
// NOTE: it seems this function is overwritten by the standard library, so just
// return a dummy value in this version.
let zero = self.context.new_rvalue_zero(self.type_u32());
block.end_with_return(None, zero);*/
//*self.current_func.borrow_mut() = Some(func);
let func = self.declare_fn(symbol_name, &fn_abi);
let func: RValue<'gcc> = unsafe { std::mem::transmute(func) };
func
/*self.get_fn(
ty::Instance::resolve(
tcx,
ty::ParamEnv::reveal_all(),
def_id,
tcx.intern_substs(&[]),
)
.unwrap().unwrap(),
)*/
},
_ => {
let name = if wants_msvc_seh(self.sess()) {
let name =
if wants_msvc_seh(self.sess()) {
"__CxxFrameHandler3"
} else {
}
else {
"rust_eh_personality"
};
let func = self.declare_func(name, self.type_i32(), &[], true);
//*self.current_func.borrow_mut() = Some(func);
// NOTE: this function is created multiple times and is overwritten by the
// standard library, so mark it as weak.
//func.add_attribute(FnAttribute::Weak);
//self.functions.borrow_mut().insert(name.to_string(), func);
/*let block = func.new_block("eh_personality_block");
// NOTE: it seems this function is overwritten by the standard library, so just
// return a dummy value in this version.
let zero = self.context.new_rvalue_zero(self.type_i32());
block.end_with_return(None, zero);*/
unsafe { std::mem::transmute(func) }
}
};

View File

@ -40,7 +40,7 @@ pub fn declare_global_with_linkage(&self, name: &str, ty: Type<'gcc>, linkage: G
pub fn declare_func(&self, name: &str, return_type: Type<'gcc>, params: &[Type<'gcc>], variadic: bool) -> Function<'gcc> {
self.linkage.set(FunctionType::Extern);
declare_raw_fn(self, name, () /*llvm::CCallConv*/, return_type, params, variadic, true)
declare_raw_fn(self, name, () /*llvm::CCallConv*/, return_type, params, variadic)
}
pub fn declare_global(&self, name: &str, ty: Type<'gcc>, global_kind: GlobalKind, is_tls: bool, link_section: Option<Symbol>) -> LValue<'gcc> {
@ -69,7 +69,7 @@ pub fn declare_cfn(&self, name: &str, _fn_type: Type<'gcc>) -> RValue<'gcc> {
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, false);
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);
@ -77,19 +77,9 @@ pub fn declare_cfn(&self, name: &str, _fn_type: Type<'gcc>) -> RValue<'gcc> {
unsafe { std::mem::transmute(func) }
}
pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>, dont_cache: bool) -> Function<'gcc> {
pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Function<'gcc> {
let (return_type, params, variadic, on_stack_param_indices) = fn_abi.gcc_type(self);
/*static mut COUNTER: i32 = 0;
if name.contains("personality") {
println!("{}: {}", name, skip_cache);
unsafe {
COUNTER += 1;
if COUNTER == 6 {
panic!("{}", name);
}
}
}*/
let func = declare_raw_fn(self, name, () /*fn_abi.llvm_cconv()*/, return_type, &params, variadic, dont_cache);
let func = declare_raw_fn(self, name, () /*fn_abi.llvm_cconv()*/, return_type, &params, variadic);
self.on_stack_function_params.borrow_mut().insert(func, on_stack_param_indices);
func
}
@ -108,7 +98,7 @@ pub fn get_declared_value(&self, name: &str) -> Option<RValue<'gcc>> {
///
/// If theres 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, dont_cache: bool) -> Function<'gcc> {
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> {
if name.starts_with("llvm.") {
let intrinsic = llvm::intrinsic(name, cx);
cx.intrinsics.borrow_mut().insert(name.to_string(), intrinsic);
@ -123,9 +113,7 @@ fn declare_raw_fn<'gcc>(cx: &CodegenCx<'gcc, '_>, name: &str, _callconv: () /*ll
.map(|(index, param)| cx.context.new_parameter(None, *param, &format!("param{}", index))) // TODO(antoyo): set name.
.collect();
let func = cx.context.new_function(None, cx.linkage.get(), return_type, &params, mangle_name(name), variadic);
//if !dont_cache {
cx.functions.borrow_mut().insert(name.to_string(), func);
//}
func
};

View File

@ -1118,9 +1118,7 @@ fn saturating_sub(&mut self, lhs: RValue<'gcc>, rhs: RValue<'gcc>, signed: bool,
}
fn try_intrinsic<'a, 'b, 'gcc, 'tcx>(bx: &'b mut Builder<'a, 'gcc, 'tcx>, try_func: RValue<'gcc>, data: RValue<'gcc>, catch_func: RValue<'gcc>, dest: RValue<'gcc>) {
// NOTE: the `|| true` here is to use the panic=abort strategy with panic=unwind too
if bx.sess().panic_strategy() == PanicStrategy::Abort {
// TODO(bjorn3): Properly implement unwinding and remove the `|| true` once this is done.
bx.call(bx.type_void(), try_func, &[data], None);
// Return 0 unconditionally from the intrinsic call;
// we can never unwind.
@ -1238,7 +1236,7 @@ fn get_rust_try_fn<'a, 'gcc, 'tcx>(cx: &'a CodegenCx<'gcc, 'tcx>, codegen: &mut
)));
// `unsafe fn(unsafe fn(*mut i8) -> (), *mut i8, unsafe fn(*mut i8, *mut i8) -> ()) -> i32`
let rust_fn_sig = ty::Binder::dummy(cx.tcx.mk_fn_sig(
[try_fn_ty, i8p, catch_fn_ty].into_iter(),
[try_fn_ty, i8p, catch_fn_ty].iter(),
&tcx.types.i32,
false,
rustc_hir::Unsafety::Unsafe,
@ -1256,7 +1254,7 @@ fn gen_fn<'a, 'gcc, 'tcx>(cx: &'a CodegenCx<'gcc, 'tcx>, name: &str, rust_fn_sig
let (typ, _, _, _) = fn_abi.gcc_type(cx);
// FIXME(eddyb) find a nicer way to do this.
cx.linkage.set(FunctionType::Internal);
let func = cx.declare_fn(name, fn_abi, false);
let func = cx.declare_fn(name, fn_abi);
let func_val = unsafe { std::mem::transmute(func) };
cx.set_frame_pointer_type(func_val);
cx.apply_target_cpu_attr(func_val);

View File

@ -35,7 +35,7 @@ fn predefine_fn(&self, instance: Instance<'tcx>, linkage: Linkage, visibility: V
let fn_abi = self.fn_abi_of_instance(instance, ty::List::empty());
self.linkage.set(base::linkage_to_gcc(linkage));
let decl = self.declare_fn(symbol_name, &fn_abi, false);
let decl = self.declare_fn(symbol_name, &fn_abi);
//let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
attributes::from_fn_attrs(self, decl, instance);

View File

@ -191,11 +191,11 @@ function std_tests() {
$RUN_WRAPPER ./target/out/std_example --target $TARGET_TRIPLE
echo "[AOT] subslice-patterns-const-eval"
$RUSTC example/subslice-patterns-const-eval.rs --crate-type bin -Cpanic=abort --target $TARGET_TRIPLE
$RUSTC example/subslice-patterns-const-eval.rs --crate-type bin --target $TARGET_TRIPLE
$RUN_WRAPPER ./target/out/subslice-patterns-const-eval
echo "[AOT] track-caller-attribute"
$RUSTC example/track-caller-attribute.rs --crate-type bin -Cpanic=abort --target $TARGET_TRIPLE
$RUSTC example/track-caller-attribute.rs --crate-type bin --target $TARGET_TRIPLE
$RUN_WRAPPER ./target/out/track-caller-attribute
echo "[BUILD] mod_bench"
@ -340,6 +340,7 @@ function test_rustc() {
rm -r src/test/ui/{abi*,extern/,panic-runtime/,panics/,unsized-locals/,proc-macro/,threads-sendsync/,thinlto/,borrowck/,chalkify/bugs/,test*,*lto*.rs,consts/const-float-bits-reject-conv.rs,consts/issue-miri-1910.rs} || true
rm src/test/ui/mir/mir_heavy_promoted.rs # this tests is oom-killed in the CI.
# TODO: re-enable panics tests.
for test in $(rg --files-with-matches "catch_unwind|should_panic|thread|lto" src/test/ui); do
rm $test
done
@ -348,7 +349,7 @@ function test_rustc() {
git checkout src/test/ui/type-alias-impl-trait/auxiliary/cross_crate_ice2.rs
git checkout src/test/ui/macros/rfc-2011-nicer-assert-messages/auxiliary/common.rs
RUSTC_ARGS="-Zpanic-abort-tests -Csymbol-mangling-version=v0 -Zcodegen-backend="$(pwd)"/../target/"$CHANNEL"/librustc_codegen_gcc."$dylib_ext" --sysroot "$(pwd)"/../build_sysroot/sysroot -Cpanic=abort"
RUSTC_ARGS="-Csymbol-mangling-version=v0 -Zcodegen-backend="$(pwd)"/../target/"$CHANNEL"/librustc_codegen_gcc."$dylib_ext" --sysroot "$(pwd)"/../build_sysroot/sysroot"
if [ $# -eq 0 ]; then
# No argument supplied to the function. Doing nothing.
@ -400,7 +401,7 @@ function all() {
mini_tests
build_sysroot
std_tests
asm_tests
#asm_tests
test_libcore
extended_sysroot_tests
test_rustc