Skip to content

Commit 70659f7

Browse files
committed
Cleanup
1 parent a4b74e3 commit 70659f7

File tree

11 files changed

+53
-108
lines changed

11 files changed

+53
-108
lines changed

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ To have the correct file paths in `gdb` instead of `/usr/src/debug/gcc/libstdc++
177177
Maybe by calling the following at the beginning of gdb:
178178
179179
```
180-
set substitute-path /usr/src/debug/gcc /home/bouanto/Ordinateur/Programmation/Projets/gcc-repo/gcc
180+
set substitute-path /usr/src/debug/gcc /path/to/gcc-repo/gcc
181181
```
182182
183183
TODO: but that's not what I remember I was doing.

example/alloc_example.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(start, box_syntax, core_intrinsics, alloc_error_handler)]
1+
#![feature(start, box_syntax, core_intrinsics, alloc_error_handler, lang_items)]
22
#![no_std]
33

44
extern crate alloc;
@@ -26,6 +26,16 @@ fn alloc_error_handler(_: alloc::alloc::Layout) -> ! {
2626
core::intrinsics::abort();
2727
}
2828

29+
#[lang = "eh_personality"]
30+
fn eh_personality() -> ! {
31+
loop {}
32+
}
33+
34+
#[no_mangle]
35+
unsafe extern "C" fn _Unwind_Resume() {
36+
core::intrinsics::unreachable();
37+
}
38+
2939
#[start]
3040
fn main(_argc: isize, _argv: *const *const u8) -> isize {
3141
let world: Box<&str> = box "Hello World!\0";

src/asm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
352352
inputs.push(AsmInOperand {
353353
constraint: "X".into(),
354354
rust_idx,
355-
val: get_fn(self.cx, instance, false).get_address(None),
355+
val: get_fn(self.cx, instance).get_address(None),
356356
});
357357
}
358358

@@ -738,7 +738,7 @@ impl<'gcc, 'tcx> AsmMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
738738
}
739739

740740
GlobalAsmOperandRef::SymFn { instance } => {
741-
let function = get_fn(self, instance, false);
741+
let function = get_fn(self, instance);
742742
self.add_used_function(function);
743743
// TODO(@Amanieu): Additional mangling is needed on
744744
// some targets to add a leading underscore (Mach-O)

src/base.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,6 @@ pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol, supports_
9191
context.add_command_line_option("-fexceptions");
9292
context.add_driver_option("-fexceptions");
9393

94-
/*context.add_command_line_option("-fasynchronous-unwind-tables");
95-
context.add_driver_option("-fasynchronous-unwind-tables");
96-
97-
context.add_command_line_option("-funwind-tables");
98-
context.add_driver_option("-funwind-tables");*/
99-
10094
// TODO(antoyo): only set on x86 platforms.
10195
context.add_command_line_option("-masm=intel");
10296
// TODO(antoyo): only add the following cli argument if the feature is supported.
@@ -156,8 +150,7 @@ pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol, supports_
156150
context.set_keep_intermediates(true);
157151
}
158152

159-
// TODO(bjorn3): Remove once unwinding is properly implemented
160-
// TODO: remove.
153+
// NOTE: The codegen generates unrechable blocks.
161154
context.set_allow_unreachable_blocks(true);
162155

163156
{

src/builder.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
457457

458458
let current_block = self.block.clone();
459459
self.block = try_block;
460-
let call = self.call(typ, func, args, None); // TODO: use funclet here?
460+
let call = self.call(typ, func, args, None); // TODO(antoyo): use funclet here?
461461
self.block = current_block;
462462

463463
let return_value = self.current_func()
@@ -471,8 +471,6 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
471471
self.block.add_try_finally(None, try_block, catch);
472472
}
473473
else {
474-
// FIXME: FIXME: FIXME: Seems like bad (_URC_NO_REASON) return code, perhaps because the cleanup pad was created properly.
475-
println!("Try/catch in {:?}", self.current_func());
476474
self.block.add_try_catch(None, try_block, catch);
477475
}
478476

@@ -1197,26 +1195,16 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
11971195
}
11981196

11991197
fn set_personality_fn(&mut self, personality: RValue<'gcc>) {
1200-
let personality = self.rvalue_as_function(personality); // FIXME: why calling
1201-
//rvalue_as_function doesn't work?
1202-
//let personality = unsafe { std::mem::transmute(personality) };
1198+
let personality = self.rvalue_as_function(personality);
12031199
#[cfg(feature="master")]
12041200
self.current_func().set_personality_function(personality);
1205-
// FIXME: rustc manages to generate the symbol DW.ref.rust_eh_personality multiple times
1206-
// for the same asm file, which causes an assembler error.
12071201
}
12081202

12091203
fn cleanup_landing_pad(&mut self, _ty: Type<'gcc>, pers_fn: RValue<'gcc>) -> RValue<'gcc> {
12101204
self.set_personality_fn(pers_fn);
12111205

1212-
/*
1213-
* Matching GCC exception handling with LLVM:
1214-
*
1215-
* GCC LLVM
1216-
* CATCH_EXPR landing pad catch clause
1217-
* TRY_FINALLY_EXPR cleanup
1218-
*/
1219-
1206+
// NOTE: insert the current block in a variable so that a later call to invoke knows to
1207+
// generate a try/finally instead of a try/catch for this block.
12201208
self.cleanup_blocks.borrow_mut().insert(self.block);
12211209

12221210
let eh_pointer_builtin = self.cx.context.get_target_builtin_function("__builtin_eh_pointer");

src/callee.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#[cfg(feature="master")]
22
use gccjit::{FnAttribute, Visibility};
3-
use gccjit::{FunctionType, RValue, Function};
4-
use rustc_codegen_ssa::traits::BaseTypeMethods;
3+
use gccjit::{FunctionType, Function};
54
use rustc_middle::ty::{self, Instance, TypeVisitable};
65
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt};
76

8-
use crate::abi::FnAbiGccExt;
97
use crate::attributes;
108
use crate::context::CodegenCx;
119

@@ -16,7 +14,7 @@ use crate::context::CodegenCx;
1614
///
1715
/// - `cx`: the crate context
1816
/// - `instance`: the instance to be instantiated
19-
pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>, dont_cache: bool) -> Function<'gcc> {
17+
pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>) -> Function<'gcc> {
2018
let tcx = cx.tcx();
2119

2220
assert!(!instance.substs.needs_infer());
@@ -31,7 +29,9 @@ pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>,
3129
let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
3230

3331
let func =
34-
if let Some(func) = cx.get_declared_value(&sym) {
32+
if let Some(_func) = cx.get_declared_value(&sym) {
33+
// FIXME: we never reach this because get_declared_value only returns global variables
34+
// and here we try to get a function.
3535
unreachable!();
3636
/*
3737
// 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>,
7070
}
7171
else {
7272
cx.linkage.set(FunctionType::Extern);
73-
/*if sym == "rust_eh_personality" {
74-
panic!();
75-
}*/
76-
let func = cx.declare_fn(&sym, &fn_abi, dont_cache);
73+
let func = cx.declare_fn(&sym, &fn_abi);
7774

7875
attributes::from_fn_attrs(cx, func, instance);
7976

@@ -171,9 +168,7 @@ pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>,
171168
func
172169
};
173170

174-
//if !dont_cache {
175-
cx.function_instances.borrow_mut().insert(instance, func);
176-
//}
171+
cx.function_instances.borrow_mut().insert(instance, func);
177172

178173
func
179174
}

src/context.rs

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::cell::{Cell, RefCell};
22

3-
use gccjit::{Block, CType, Context, Function, FunctionPtrType, FunctionType, LValue, RValue, Type, FnAttribute};
3+
use gccjit::{Block, CType, Context, Function, FunctionPtrType, FunctionType, LValue, RValue, Type};
44
use rustc_codegen_ssa::base::wants_msvc_seh;
55
use rustc_codegen_ssa::traits::{
66
BackendTypes,
@@ -259,8 +259,8 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
259259
pub fn rvalue_as_function(&self, value: RValue<'gcc>) -> Function<'gcc> {
260260
let function: Function<'gcc> = unsafe { std::mem::transmute(value) };
261261
// FIXME: seems like self.functions get overwritten for rust_eh_personality.
262-
/*debug_assert!(self.functions.borrow().values().find(|value| **value == function).is_some(),
263-
"{:?} is not a function", function);*/
262+
debug_assert!(self.functions.borrow().values().find(|value| **value == function).is_some(),
263+
"{:?} is not a function", function);
264264
function
265265
}
266266

@@ -332,7 +332,7 @@ impl<'gcc, 'tcx> MiscMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
332332
}
333333

334334
fn get_fn(&self, instance: Instance<'tcx>) -> RValue<'gcc> {
335-
let func = get_fn(self, instance, false);
335+
let func = get_fn(self, instance);
336336
*self.current_func.borrow_mut() = Some(func);
337337
unsafe { std::mem::transmute(func) }
338338
}
@@ -345,7 +345,7 @@ impl<'gcc, 'tcx> MiscMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
345345
self.intrinsics.borrow()[func_name].clone()
346346
}
347347
else {
348-
get_fn(self, instance, false)
348+
get_fn(self, instance)
349349
};
350350
let ptr = func.get_address(None);
351351

@@ -386,8 +386,6 @@ impl<'gcc, 'tcx> MiscMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
386386
let func =
387387
match tcx.lang_items().eh_personality() {
388388
Some(def_id) if !wants_msvc_seh(self.sess()) => {
389-
// FIXME: this create an instance into self.functions and prevent the creating
390-
// of the function defined in std.
391389
let instance =
392390
ty::Instance::resolve(
393391
tcx,
@@ -400,45 +398,19 @@ impl<'gcc, 'tcx> MiscMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
400398
let symbol_name = tcx.symbol_name(instance).name;
401399
let fn_abi = self.fn_abi_of_instance(instance, ty::List::empty());
402400
self.linkage.set(FunctionType::Extern);
403-
let func = self.declare_fn(symbol_name, &fn_abi, false);
404-
//func.add_attribute(FnAttribute::Weak);
405-
406-
/*let block = func.new_block("eh_personality_block");
407-
// NOTE: it seems this function is overwritten by the standard library, so just
408-
// return a dummy value in this version.
409-
let zero = self.context.new_rvalue_zero(self.type_u32());
410-
block.end_with_return(None, zero);*/
411-
412-
//*self.current_func.borrow_mut() = Some(func);
401+
let func = self.declare_fn(symbol_name, &fn_abi);
413402
let func: RValue<'gcc> = unsafe { std::mem::transmute(func) };
414403
func
415-
/*self.get_fn(
416-
ty::Instance::resolve(
417-
tcx,
418-
ty::ParamEnv::reveal_all(),
419-
def_id,
420-
tcx.intern_substs(&[]),
421-
)
422-
.unwrap().unwrap(),
423-
)*/
424404
},
425405
_ => {
426-
let name = if wants_msvc_seh(self.sess()) {
427-
"__CxxFrameHandler3"
428-
} else {
429-
"rust_eh_personality"
430-
};
406+
let name =
407+
if wants_msvc_seh(self.sess()) {
408+
"__CxxFrameHandler3"
409+
}
410+
else {
411+
"rust_eh_personality"
412+
};
431413
let func = self.declare_func(name, self.type_i32(), &[], true);
432-
//*self.current_func.borrow_mut() = Some(func);
433-
// NOTE: this function is created multiple times and is overwritten by the
434-
// standard library, so mark it as weak.
435-
//func.add_attribute(FnAttribute::Weak);
436-
//self.functions.borrow_mut().insert(name.to_string(), func);
437-
/*let block = func.new_block("eh_personality_block");
438-
// NOTE: it seems this function is overwritten by the standard library, so just
439-
// return a dummy value in this version.
440-
let zero = self.context.new_rvalue_zero(self.type_i32());
441-
block.end_with_return(None, zero);*/
442414
unsafe { std::mem::transmute(func) }
443415
}
444416
};

src/declare.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
4040

4141
pub fn declare_func(&self, name: &str, return_type: Type<'gcc>, params: &[Type<'gcc>], variadic: bool) -> Function<'gcc> {
4242
self.linkage.set(FunctionType::Extern);
43-
declare_raw_fn(self, name, () /*llvm::CCallConv*/, return_type, params, variadic, true)
43+
declare_raw_fn(self, name, () /*llvm::CCallConv*/, return_type, params, variadic)
4444
}
4545

4646
pub fn declare_global(&self, name: &str, ty: Type<'gcc>, global_kind: GlobalKind, is_tls: bool, link_section: Option<Symbol>) -> LValue<'gcc> {
@@ -69,27 +69,17 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
6969
let return_type = self.type_i32();
7070
let variadic = false;
7171
self.linkage.set(FunctionType::Exported);
72-
let func = declare_raw_fn(self, name, () /*llvm::CCallConv*/, return_type, &[self.type_i32(), const_string], variadic, false);
72+
let func = declare_raw_fn(self, name, () /*llvm::CCallConv*/, return_type, &[self.type_i32(), const_string], variadic);
7373
// NOTE: it is needed to set the current_func here as well, because get_fn() is not called
7474
// for the main function.
7575
*self.current_func.borrow_mut() = Some(func);
7676
// FIXME(antoyo): this is a wrong cast. That requires changing the compiler API.
7777
unsafe { std::mem::transmute(func) }
7878
}
7979

80-
pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>, dont_cache: bool) -> Function<'gcc> {
80+
pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Function<'gcc> {
8181
let (return_type, params, variadic, on_stack_param_indices) = fn_abi.gcc_type(self);
82-
/*static mut COUNTER: i32 = 0;
83-
if name.contains("personality") {
84-
println!("{}: {}", name, skip_cache);
85-
unsafe {
86-
COUNTER += 1;
87-
if COUNTER == 6 {
88-
panic!("{}", name);
89-
}
90-
}
91-
}*/
92-
let func = declare_raw_fn(self, name, () /*fn_abi.llvm_cconv()*/, return_type, &params, variadic, dont_cache);
82+
let func = declare_raw_fn(self, name, () /*fn_abi.llvm_cconv()*/, return_type, &params, variadic);
9383
self.on_stack_function_params.borrow_mut().insert(func, on_stack_param_indices);
9484
func
9585
}
@@ -108,7 +98,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
10898
///
10999
/// If there’s a value with the same name already declared, the function will
110100
/// update the declaration and return existing Value instead.
111-
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> {
101+
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> {
112102
if name.starts_with("llvm.") {
113103
let intrinsic = llvm::intrinsic(name, cx);
114104
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
123113
.map(|(index, param)| cx.context.new_parameter(None, *param, &format!("param{}", index))) // TODO(antoyo): set name.
124114
.collect();
125115
let func = cx.context.new_function(None, cx.linkage.get(), return_type, &params, mangle_name(name), variadic);
126-
//if !dont_cache {
127-
cx.functions.borrow_mut().insert(name.to_string(), func);
128-
//}
116+
cx.functions.borrow_mut().insert(name.to_string(), func);
129117
func
130118
};
131119

src/intrinsic/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,9 +1118,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
11181118
}
11191119

11201120
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>) {
1121-
// NOTE: the `|| true` here is to use the panic=abort strategy with panic=unwind too
11221121
if bx.sess().panic_strategy() == PanicStrategy::Abort {
1123-
// TODO(bjorn3): Properly implement unwinding and remove the `|| true` once this is done.
11241122
bx.call(bx.type_void(), try_func, &[data], None);
11251123
// Return 0 unconditionally from the intrinsic call;
11261124
// we can never unwind.
@@ -1238,7 +1236,7 @@ fn get_rust_try_fn<'a, 'gcc, 'tcx>(cx: &'a CodegenCx<'gcc, 'tcx>, codegen: &mut
12381236
)));
12391237
// `unsafe fn(unsafe fn(*mut i8) -> (), *mut i8, unsafe fn(*mut i8, *mut i8) -> ()) -> i32`
12401238
let rust_fn_sig = ty::Binder::dummy(cx.tcx.mk_fn_sig(
1241-
[try_fn_ty, i8p, catch_fn_ty].into_iter(),
1239+
[try_fn_ty, i8p, catch_fn_ty].iter(),
12421240
&tcx.types.i32,
12431241
false,
12441242
rustc_hir::Unsafety::Unsafe,
@@ -1256,7 +1254,7 @@ fn gen_fn<'a, 'gcc, 'tcx>(cx: &'a CodegenCx<'gcc, 'tcx>, name: &str, rust_fn_sig
12561254
let (typ, _, _, _) = fn_abi.gcc_type(cx);
12571255
// FIXME(eddyb) find a nicer way to do this.
12581256
cx.linkage.set(FunctionType::Internal);
1259-
let func = cx.declare_fn(name, fn_abi, false);
1257+
let func = cx.declare_fn(name, fn_abi);
12601258
let func_val = unsafe { std::mem::transmute(func) };
12611259
cx.set_frame_pointer_type(func_val);
12621260
cx.apply_target_cpu_attr(func_val);

src/mono_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<'gcc, 'tcx> PreDefineMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
3535

3636
let fn_abi = self.fn_abi_of_instance(instance, ty::List::empty());
3737
self.linkage.set(base::linkage_to_gcc(linkage));
38-
let decl = self.declare_fn(symbol_name, &fn_abi, false);
38+
let decl = self.declare_fn(symbol_name, &fn_abi);
3939
//let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
4040

4141
attributes::from_fn_attrs(self, decl, instance);

0 commit comments

Comments
 (0)