Skip to content

Commit 3293b34

Browse files
committed
Support sysv64 and ms ABIs
1 parent 4380568 commit 3293b34

File tree

5 files changed

+57
-10
lines changed

5 files changed

+57
-10
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

+42
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ 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+
use rustc_target::callconv::Conv;
1213

1314
use crate::builder::Builder;
1415
use crate::context::CodegenCx;
@@ -104,6 +105,7 @@ pub trait FnAbiGccExt<'gcc, 'tcx> {
104105
// TODO(antoyo): return a function pointer type instead?
105106
fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> FnAbiGcc<'gcc>;
106107
fn ptr_to_gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
108+
fn gcc_cconv(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Option<FnAttribute<'gcc>>;
107109
}
108110

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

src/context.rs

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

26+
use crate::abi::conv_to_fn_attribute;
2627
use crate::callee::get_fn;
2728
use crate::common::SignType;
2829

@@ -509,7 +510,8 @@ impl<'gcc, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
509510
fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
510511
let entry_name = self.sess().target.entry_name.as_ref();
511512
if !self.functions.borrow().contains_key(entry_name) {
512-
Some(self.declare_entry_fn(entry_name, fn_type, ()))
513+
let conv = conv_to_fn_attribute(self.sess().target.entry_abi, &self.sess().target.arch);
514+
Some(self.declare_entry_fn(entry_name, fn_type, conv))
513515
} else {
514516
// If the symbol already exists, it is an error: for example, the user wrote
515517
// #[no_mangle] extern "C" fn main(..) {..}

src/declare.rs

+7-4
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,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
9292
&self,
9393
name: &str,
9494
_fn_type: Type<'gcc>,
95-
callconv: (), /*llvm::CCallConv*/
95+
callconv: Option<FnAttribute<'gcc>>,
9696
) -> RValue<'gcc> {
9797
// TODO(antoyo): use the fn_type parameter.
9898
let const_string = self.context.new_type::<u8>().make_pointer().make_pointer();
@@ -126,7 +126,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
126126
let func = declare_raw_fn(
127127
self,
128128
name,
129-
(), /*fn_abi.llvm_cconv()*/
129+
fn_abi.gcc_cconv(self),
130130
return_type,
131131
&arguments_type,
132132
is_c_variadic,
@@ -162,7 +162,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
162162
fn declare_raw_fn<'gcc>(
163163
cx: &CodegenCx<'gcc, '_>,
164164
name: &str,
165-
_callconv: (), /*llvm::CallConv*/
165+
callconv: Option<FnAttribute<'gcc>>,
166166
return_type: Type<'gcc>,
167167
param_types: &[Type<'gcc>],
168168
variadic: bool,
@@ -192,6 +192,9 @@ fn declare_raw_fn<'gcc>(
192192
let name = &mangle_name(name);
193193
let func =
194194
cx.context.new_function(None, cx.linkage.get(), return_type, &params, name, variadic);
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")]

0 commit comments

Comments
 (0)