Skip to content

Commit

Permalink
Format the code
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed Feb 28, 2024
1 parent 6ec5010 commit 12b58a6
Show file tree
Hide file tree
Showing 27 changed files with 3,732 additions and 2,639 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -49,6 +49,13 @@ jobs:
# `llvm-14-tools` is needed to install the `FileCheck` binary which is used for asm tests.
run: sudo apt-get install ninja-build ripgrep llvm-14-tools

- name: Install rustfmt
run: rustup component add rustfmt

# TODO: remove.
- name: Check formatting
run: cargo fmt -- --check

- name: Download artifact
run: curl -LO https://github.com/antoyo/gcc/releases/latest/download/${{ matrix.libgccjit_version.gcc }}

Expand Down Expand Up @@ -92,6 +99,9 @@ jobs:
run: |
./y.sh test --release --clean --build-sysroot ${{ matrix.commands }}
- name: Check formatting
run: cargo fmt -- --check

duplicates:
runs-on: ubuntu-latest
steps:
Expand Down
4 changes: 3 additions & 1 deletion .rustfmt.toml
@@ -1 +1,3 @@
ignore = ["/src", "/tests"]
version = "Two"
use_small_heuristics = "Max"
merge_derives = false
111 changes: 55 additions & 56 deletions src/abi.rs
Expand Up @@ -18,32 +18,28 @@ impl<'a, 'gcc, 'tcx> AbiBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
fn get_param(&mut self, index: usize) -> Self::Value {
let func = self.current_func();
let param = func.get_param(index as i32);
let on_stack =
if let Some(on_stack_param_indices) = self.on_stack_function_params.borrow().get(&func) {
on_stack_param_indices.contains(&index)
}
else {
false
};
if on_stack {
param.to_lvalue().get_address(None)
}
else {
param.to_rvalue()
}
let on_stack = if let Some(on_stack_param_indices) =
self.on_stack_function_params.borrow().get(&func)
{
on_stack_param_indices.contains(&index)
} else {
false
};
if on_stack { param.to_lvalue().get_address(None) } else { param.to_rvalue() }
}
}

impl GccType for CastTarget {
fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc> {
let rest_gcc_unit = self.rest.unit.gcc_type(cx);
let (rest_count, rem_bytes) =
if self.rest.unit.size.bytes() == 0 {
(0, 0)
}
else {
(self.rest.total.bytes() / self.rest.unit.size.bytes(), self.rest.total.bytes() % self.rest.unit.size.bytes())
};
let (rest_count, rem_bytes) = if self.rest.unit.size.bytes() == 0 {
(0, 0)
} else {
(
self.rest.total.bytes() / self.rest.unit.size.bytes(),
self.rest.total.bytes() % self.rest.unit.size.bytes(),
)
};

if self.prefix.iter().all(|x| x.is_none()) {
// Simplify to a single unit when there is no prefix and size <= unit size
Expand All @@ -61,9 +57,7 @@ impl GccType for CastTarget {
let mut args: Vec<_> = self
.prefix
.iter()
.flat_map(|option_reg| {
option_reg.map(|reg| reg.gcc_type(cx))
})
.flat_map(|option_reg| option_reg.map(|reg| reg.gcc_type(cx)))
.chain((0..rest_count).map(|_| rest_gcc_unit))
.collect();

Expand All @@ -86,12 +80,10 @@ impl GccType for Reg {
fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc> {
match self.kind {
RegKind::Integer => cx.type_ix(self.size.bits()),
RegKind::Float => {
match self.size.bits() {
32 => cx.type_f32(),
64 => cx.type_f64(),
_ => bug!("unsupported float: {:?}", self),
}
RegKind::Float => match self.size.bits() {
32 => cx.type_f32(),
64 => cx.type_f64(),
_ => bug!("unsupported float: {:?}", self),
},
RegKind::Vector => unimplemented!(), //cx.type_vector(cx.type_i8(), self.size.bytes()),
}
Expand Down Expand Up @@ -119,19 +111,18 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {

// This capacity calculation is approximate.
let mut argument_tys = Vec::with_capacity(
self.args.len() + if let PassMode::Indirect { .. } = self.ret.mode { 1 } else { 0 }
self.args.len() + if let PassMode::Indirect { .. } = self.ret.mode { 1 } else { 0 },
);

let return_type =
match self.ret.mode {
PassMode::Ignore => cx.type_void(),
PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_gcc_type(cx),
PassMode::Cast { ref cast, .. } => cast.gcc_type(cx),
PassMode::Indirect { .. } => {
argument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
cx.type_void()
}
};
let return_type = match self.ret.mode {
PassMode::Ignore => cx.type_void(),
PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_gcc_type(cx),
PassMode::Cast { ref cast, .. } => cast.gcc_type(cx),
PassMode::Indirect { .. } => {
argument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
cx.type_void()
}
};
#[cfg(feature = "master")]
let mut non_null_args = Vec::new();

Expand All @@ -149,17 +140,23 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
ty
};
#[cfg(not(feature = "master"))]
let apply_attrs = |ty: Type<'gcc>, _attrs: &ArgAttributes, _arg_index: usize| {
ty
};
let apply_attrs = |ty: Type<'gcc>, _attrs: &ArgAttributes, _arg_index: usize| ty;

for arg in self.args.iter() {
let arg_ty = match arg.mode {
PassMode::Ignore => continue,
PassMode::Pair(a, b) => {
let arg_pos = argument_tys.len();
argument_tys.push(apply_attrs(arg.layout.scalar_pair_element_gcc_type(cx, 0), &a, arg_pos));
argument_tys.push(apply_attrs(arg.layout.scalar_pair_element_gcc_type(cx, 1), &b, arg_pos + 1));
argument_tys.push(apply_attrs(
arg.layout.scalar_pair_element_gcc_type(cx, 0),
&a,
arg_pos,
));
argument_tys.push(apply_attrs(
arg.layout.scalar_pair_element_gcc_type(cx, 1),
&b,
arg_pos + 1,
));
continue;
}
PassMode::Cast { ref cast, pad_i32 } => {
Expand All @@ -174,14 +171,17 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
// This is a "byval" argument, so we don't apply the `restrict` attribute on it.
on_stack_param_indices.insert(argument_tys.len());
arg.memory_ty(cx)
},
PassMode::Direct(attrs) => apply_attrs(arg.layout.immediate_gcc_type(cx), &attrs, argument_tys.len()),
}
PassMode::Direct(attrs) => {
apply_attrs(arg.layout.immediate_gcc_type(cx), &attrs, argument_tys.len())
}
PassMode::Indirect { attrs, meta_attrs: None, on_stack: false } => {
apply_attrs(cx.type_ptr_to(arg.memory_ty(cx)), &attrs, argument_tys.len())
}
PassMode::Indirect { attrs, meta_attrs: Some(meta_attrs), on_stack } => {
assert!(!on_stack);
let ty = apply_attrs(cx.type_ptr_to(arg.memory_ty(cx)), &attrs, argument_tys.len());
let ty =
apply_attrs(cx.type_ptr_to(arg.memory_ty(cx)), &attrs, argument_tys.len());
apply_attrs(ty, &meta_attrs, argument_tys.len())
}
};
Expand All @@ -207,15 +207,14 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {

fn ptr_to_gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
// FIXME(antoyo): Should we do something with `FnAbiGcc::fn_attributes`?
let FnAbiGcc {
return_type,
arguments_type,
is_c_variadic,
let FnAbiGcc { return_type, arguments_type, is_c_variadic, on_stack_param_indices, .. } =
self.gcc_type(cx);
let pointer_type =
cx.context.new_function_pointer_type(None, return_type, &arguments_type, is_c_variadic);
cx.on_stack_params.borrow_mut().insert(
pointer_type.dyncast_function_ptr_type().expect("function ptr type"),
on_stack_param_indices,
..
} = self.gcc_type(cx);
let pointer_type = cx.context.new_function_pointer_type(None, return_type, &arguments_type, is_c_variadic);
cx.on_stack_params.borrow_mut().insert(pointer_type.dyncast_function_ptr_type().expect("function ptr type"), on_stack_param_indices);
);
pointer_type
}
}
56 changes: 39 additions & 17 deletions src/allocator.rs
@@ -1,4 +1,4 @@
#[cfg(feature="master")]
#[cfg(feature = "master")]
use gccjit::FnAttribute;
use gccjit::{Context, FunctionType, GlobalKind, ToRValue, Type};
use rustc_ast::expand::allocator::{
Expand All @@ -11,15 +11,20 @@ use rustc_session::config::OomStrategy;

use crate::GccContext;

pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_name: &str, kind: AllocatorKind, alloc_error_handler_kind: AllocatorKind) {
pub(crate) unsafe fn codegen(
tcx: TyCtxt<'_>,
mods: &mut GccContext,
_module_name: &str,
kind: AllocatorKind,
alloc_error_handler_kind: AllocatorKind,
) {
let context = &mods.context;
let usize =
match tcx.sess.target.pointer_width {
16 => context.new_type::<u16>(),
32 => context.new_type::<u32>(),
64 => context.new_type::<u64>(),
tws => bug!("Unsupported target word size for int: {}", tws),
};
let usize = match tcx.sess.target.pointer_width {
16 => context.new_type::<u16>(),
32 => context.new_type::<u32>(),
64 => context.new_type::<u64>(),
tws => bug!("Unsupported target word size for int: {}", tws),
};
let i8 = context.new_type::<i8>();
let i8p = i8.make_pointer();

Expand Down Expand Up @@ -85,24 +90,42 @@ fn create_wrapper_function(
) {
let void = context.new_type::<()>();

let args: Vec<_> = types.iter().enumerate()
let args: Vec<_> = types
.iter()
.enumerate()
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
.collect();
let func = context.new_function(None, FunctionType::Exported, output.unwrap_or(void), &args, from_name, false);
let func = context.new_function(
None,
FunctionType::Exported,
output.unwrap_or(void),
&args,
from_name,
false,
);

if tcx.sess.default_hidden_visibility() {
#[cfg(feature="master")]
#[cfg(feature = "master")]
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
}
if tcx.sess.must_emit_unwind_tables() {
// TODO(antoyo): emit unwind tables.
}

let args: Vec<_> = types.iter().enumerate()
let args: Vec<_> = types
.iter()
.enumerate()
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
.collect();
let callee = context.new_function(None, FunctionType::Extern, output.unwrap_or(void), &args, to_name, false);
#[cfg(feature="master")]
let callee = context.new_function(
None,
FunctionType::Extern,
output.unwrap_or(void),
&args,
to_name,
false,
);
#[cfg(feature = "master")]
callee.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));

let block = func.new_block("entry");
Expand All @@ -116,8 +139,7 @@ fn create_wrapper_function(
//llvm::LLVMSetTailCall(ret, True);
if output.is_some() {
block.end_with_return(None, ret);
}
else {
} else {
block.end_with_void_return(None);
}

Expand Down

0 comments on commit 12b58a6

Please sign in to comment.