1
Fork 0

Merge commit 'e228f0c16e' into libgccjit-codegen

This commit is contained in:
Antoni Boucher 2021-08-15 08:29:07 -04:00
commit 3d5d4e324d
27 changed files with 265 additions and 3053 deletions

View file

@ -35,7 +35,6 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
}
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);
@ -48,13 +47,12 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
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.
// FIXME(antoyo): 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.
// FIXME(antoyo): 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.
@ -82,7 +80,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
}
pub fn declare_cfn(&self, name: &str, _fn_type: Type<'gcc>) -> RValue<'gcc> {
// TODO: use the fn_type parameter.
// TODO(antoyo): 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;
@ -91,7 +89,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
// 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.
// FIXME(antoyo): this is a wrong cast. That requires changing the compiler API.
unsafe { std::mem::transmute(func) }
}
@ -128,11 +126,9 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
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, &params, variadic);
//fn_abi.apply_attrs_llfn(self, func);
// FIXME: this is a wrong cast. That requires changing the compiler API.
// FIXME(antoyo): this is a wrong cast. That requires changing the compiler API.
unsafe { std::mem::transmute(func) }
}
@ -146,19 +142,9 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
}
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?
// TODO(antoyo): 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.
@ -166,11 +152,6 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
/// 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) -> 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);
}
@ -180,32 +161,24 @@ fn declare_raw_fn<'gcc>(cx: &CodegenCx<'gcc, '_>, name: &str, _callconv: () /*ll
}
else {
let params: Vec<_> = param_types.into_iter().enumerate()
.map(|(index, param)| cx.context.new_parameter(None, *param, &format!("param{}", index))) // TODO: set name.
.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);
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
// TODO(antoyo): set function calling convention.
// TODO(antoyo): set unnamed address.
// TODO(antoyo): set no red zone function attribute.
// TODO(antoyo): set attributes for optimisation.
// TODO(antoyo): set attributes for non lazy bind.
/*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) });
// FIXME(antoyo): invalid cast.
func
}
// FIXME: this is a hack because libgccjit currently only supports alpha, num and _.
// FIXME(antoyo): 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| {