Skip to content

Commit b08a0c6

Browse files
authoredFeb 13, 2025··
Merge pull request #629 from rust-lang/feature/non-default-abis
Support sysv64 and ms ABIs
2 parents 7f05920 + 4f59a68 commit b08a0c6

File tree

6 files changed

+74
-19
lines changed

6 files changed

+74
-19
lines changed
 

‎Cargo.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ master = ["gccjit/master"]
2222
default = ["master"]
2323

2424
[dependencies]
25-
gccjit = "2.4"
25+
gccjit = "2.5"
2626
#gccjit = { git = "https://github.com/rust-lang/gccjit.rs" }
2727

2828
# Local copy.

‎src/abi.rs

+46
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use rustc_middle::ty::layout::LayoutOf;
99
#[cfg(feature = "master")]
1010
use rustc_session::config;
1111
use rustc_target::abi::call::{ArgAttributes, CastTarget, FnAbi, PassMode, Reg, RegKind};
12+
#[cfg(feature = "master")]
13+
use rustc_target::callconv::Conv;
1214

1315
use crate::builder::Builder;
1416
use crate::context::CodegenCx;
@@ -104,6 +106,8 @@ pub trait FnAbiGccExt<'gcc, 'tcx> {
104106
// TODO(antoyo): return a function pointer type instead?
105107
fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> FnAbiGcc<'gcc>;
106108
fn ptr_to_gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
109+
#[cfg(feature = "master")]
110+
fn gcc_cconv(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Option<FnAttribute<'gcc>>;
107111
}
108112

109113
impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
@@ -226,4 +230,46 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
226230
);
227231
pointer_type
228232
}
233+
234+
#[cfg(feature = "master")]
235+
fn gcc_cconv(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Option<FnAttribute<'gcc>> {
236+
conv_to_fn_attribute(self.conv, &cx.tcx.sess.target.arch)
237+
}
238+
}
239+
240+
#[cfg(feature = "master")]
241+
pub fn conv_to_fn_attribute<'gcc>(conv: Conv, _arch: &str) -> Option<FnAttribute<'gcc>> {
242+
// TODO: handle the calling conventions returning None.
243+
let attribute = match conv {
244+
Conv::C
245+
| Conv::Rust
246+
| Conv::CCmseNonSecureCall
247+
| Conv::CCmseNonSecureEntry
248+
| Conv::RiscvInterrupt { .. } => return None,
249+
Conv::Cold => return None,
250+
Conv::PreserveMost => return None,
251+
Conv::PreserveAll => return None,
252+
/*Conv::GpuKernel => {
253+
if arch == "amdgpu" {
254+
return None
255+
} else if arch == "nvptx64" {
256+
return None
257+
} else {
258+
panic!("Architecture {arch} does not support GpuKernel calling convention");
259+
}
260+
}*/
261+
Conv::AvrInterrupt => return None,
262+
Conv::AvrNonBlockingInterrupt => return None,
263+
Conv::ArmAapcs => return None,
264+
Conv::Msp430Intr => return None,
265+
Conv::PtxKernel => return None,
266+
Conv::X86Fastcall => return None,
267+
Conv::X86Intr => return None,
268+
Conv::X86Stdcall => return None,
269+
Conv::X86ThisCall => return None,
270+
Conv::X86VectorCall => return None,
271+
Conv::X86_64SysV => FnAttribute::SysvAbi,
272+
Conv::X86_64Win64 => FnAttribute::MsAbi,
273+
};
274+
Some(attribute)
229275
}

‎src/context.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use rustc_target::spec::{
2323
HasTargetSpec, HasWasmCAbiOpt, HasX86AbiOpt, Target, TlsModel, WasmCAbi, X86Abi,
2424
};
2525

26+
#[cfg(feature = "master")]
27+
use crate::abi::conv_to_fn_attribute;
2628
use crate::callee::get_fn;
2729
use crate::common::SignType;
2830

@@ -509,7 +511,11 @@ impl<'gcc, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
509511
fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
510512
let entry_name = self.sess().target.entry_name.as_ref();
511513
if !self.functions.borrow().contains_key(entry_name) {
512-
Some(self.declare_entry_fn(entry_name, fn_type, ()))
514+
#[cfg(feature = "master")]
515+
let conv = conv_to_fn_attribute(self.sess().target.entry_abi, &self.sess().target.arch);
516+
#[cfg(not(feature = "master"))]
517+
let conv = None;
518+
Some(self.declare_entry_fn(entry_name, fn_type, conv))
513519
} else {
514520
// If the symbol already exists, it is an error: for example, the user wrote
515521
// #[no_mangle] extern "C" fn main(..) {..}

‎src/declare.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
5858
variadic: bool,
5959
) -> Function<'gcc> {
6060
self.linkage.set(FunctionType::Extern);
61-
declare_raw_fn(self, name, () /*llvm::CCallConv*/, return_type, params, variadic)
61+
declare_raw_fn(self, name, None, return_type, params, variadic)
6262
}
6363

6464
pub fn declare_global(
@@ -92,7 +92,8 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
9292
&self,
9393
name: &str,
9494
_fn_type: Type<'gcc>,
95-
callconv: (), /*llvm::CCallConv*/
95+
#[cfg(feature = "master")] callconv: Option<FnAttribute<'gcc>>,
96+
#[cfg(not(feature = "master"))] callconv: Option<()>,
9697
) -> RValue<'gcc> {
9798
// TODO(antoyo): use the fn_type parameter.
9899
let const_string = self.context.new_type::<u8>().make_pointer().make_pointer();
@@ -123,14 +124,11 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
123124
#[cfg(feature = "master")]
124125
fn_attributes,
125126
} = fn_abi.gcc_type(self);
126-
let func = declare_raw_fn(
127-
self,
128-
name,
129-
(), /*fn_abi.llvm_cconv()*/
130-
return_type,
131-
&arguments_type,
132-
is_c_variadic,
133-
);
127+
#[cfg(feature = "master")]
128+
let conv = fn_abi.gcc_cconv(self);
129+
#[cfg(not(feature = "master"))]
130+
let conv = None;
131+
let func = declare_raw_fn(self, name, conv, return_type, &arguments_type, is_c_variadic);
134132
self.on_stack_function_params.borrow_mut().insert(func, on_stack_param_indices);
135133
#[cfg(feature = "master")]
136134
for fn_attr in fn_attributes {
@@ -162,7 +160,8 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
162160
fn declare_raw_fn<'gcc>(
163161
cx: &CodegenCx<'gcc, '_>,
164162
name: &str,
165-
_callconv: (), /*llvm::CallConv*/
163+
#[cfg(feature = "master")] callconv: Option<FnAttribute<'gcc>>,
164+
#[cfg(not(feature = "master"))] _callconv: Option<()>,
166165
return_type: Type<'gcc>,
167166
param_types: &[Type<'gcc>],
168167
variadic: bool,
@@ -192,6 +191,10 @@ fn declare_raw_fn<'gcc>(
192191
let name = &mangle_name(name);
193192
let func =
194193
cx.context.new_function(None, cx.linkage.get(), return_type, &params, name, variadic);
194+
#[cfg(feature = "master")]
195+
if let Some(attribute) = callconv {
196+
func.add_attribute(attribute);
197+
}
195198
cx.functions.borrow_mut().insert(name.to_string(), func);
196199

197200
#[cfg(feature = "master")]

‎src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ impl CodegenBackend for GccCodegenBackend {
187187
crate::DEFAULT_LOCALE_RESOURCE
188188
}
189189

190-
fn init(&self, sess: &Session) {
190+
fn init(&self, _sess: &Session) {
191191
#[cfg(feature = "master")]
192192
{
193-
let target_cpu = target_cpu(sess);
193+
let target_cpu = target_cpu(_sess);
194194

195195
// Get the second TargetInfo with the correct CPU features by setting the arch.
196196
let context = Context::default();

0 commit comments

Comments
 (0)
Please sign in to comment.