use crate::abi::{self, Abi, Align, FieldsShape, Size};
use crate::abi::{HasDataLayout, LayoutOf, TyAndLayout, TyAndLayoutMethods};
use crate::spec::{self, HasTargetSpec};
mod aarch64;
mod amdgpu;
mod arm;
mod avr;
mod hexagon;
mod mips;
mod mips64;
mod msp430;
mod nvptx;
mod nvptx64;
mod powerpc;
mod powerpc64;
mod riscv;
mod s390x;
mod sparc;
mod sparc64;
mod wasm32;
mod wasm32_bindgen_compat;
mod x86;
mod x86_64;
mod x86_win64;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum PassMode {
    
    Ignore,
    
    Direct(ArgAttributes),
    
    Pair(ArgAttributes, ArgAttributes),
    
    
    Cast(CastTarget),
    
    
    
    
    
    
    Indirect { attrs: ArgAttributes, extra_attrs: Option<ArgAttributes>, on_stack: bool },
}
pub use attr_impl::ArgAttribute;
#[allow(non_upper_case_globals)]
#[allow(unused)]
mod attr_impl {
    
    bitflags::bitflags! {
        #[derive(Default)]
        pub struct ArgAttribute: u16 {
            const NoAlias   = 1 << 1;
            const NoCapture = 1 << 2;
            const NonNull   = 1 << 3;
            const ReadOnly  = 1 << 4;
            const InReg     = 1 << 8;
        }
    }
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum ArgExtension {
    None,
    Zext,
    Sext,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct ArgAttributes {
    pub regular: ArgAttribute,
    pub arg_ext: ArgExtension,
    
    
    pub pointee_size: Size,
    pub pointee_align: Option<Align>,
}
impl ArgAttributes {
    pub fn new() -> Self {
        ArgAttributes {
            regular: ArgAttribute::default(),
            arg_ext: ArgExtension::None,
            pointee_size: Size::ZERO,
            pointee_align: None,
        }
    }
    pub fn ext(&mut self, ext: ArgExtension) -> &mut Self {
        assert!(self.arg_ext == ArgExtension::None || self.arg_ext == ext);
        self.arg_ext = ext;
        self
    }
    pub fn set(&mut self, attr: ArgAttribute) -> &mut Self {
        self.regular |= attr;
        self
    }
    pub fn contains(&self, attr: ArgAttribute) -> bool {
        self.regular.contains(attr)
    }
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum RegKind {
    Integer,
    Float,
    Vector,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Reg {
    pub kind: RegKind,
    pub size: Size,
}
macro_rules! reg_ctor {
    ($name:ident, $kind:ident, $bits:expr) => {
        pub fn $name() -> Reg {
            Reg { kind: RegKind::$kind, size: Size::from_bits($bits) }
        }
    };
}
impl Reg {
    reg_ctor!(i8, Integer, 8);
    reg_ctor!(i16, Integer, 16);
    reg_ctor!(i32, Integer, 32);
    reg_ctor!(i64, Integer, 64);
    reg_ctor!(i128, Integer, 128);
    reg_ctor!(f32, Float, 32);
    reg_ctor!(f64, Float, 64);
}
impl Reg {
    pub fn align<C: HasDataLayout>(&self, cx: &C) -> Align {
        let dl = cx.data_layout();
        match self.kind {
            RegKind::Integer => match self.size.bits() {
                1 => dl.i1_align.abi,
                2..=8 => dl.i8_align.abi,
                9..=16 => dl.i16_align.abi,
                17..=32 => dl.i32_align.abi,
                33..=64 => dl.i64_align.abi,
                65..=128 => dl.i128_align.abi,
                _ => panic!("unsupported integer: {:?}", self),
            },
            RegKind::Float => match self.size.bits() {
                32 => dl.f32_align.abi,
                64 => dl.f64_align.abi,
                _ => panic!("unsupported float: {:?}", self),
            },
            RegKind::Vector => dl.vector_align(self.size).abi,
        }
    }
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Uniform {
    pub unit: Reg,
    
    
    
    
    
    
    pub total: Size,
}
impl From<Reg> for Uniform {
    fn from(unit: Reg) -> Uniform {
        Uniform { unit, total: unit.size }
    }
}
impl Uniform {
    pub fn align<C: HasDataLayout>(&self, cx: &C) -> Align {
        self.unit.align(cx)
    }
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct CastTarget {
    pub prefix: [Option<RegKind>; 8],
    pub prefix_chunk_size: Size,
    pub rest: Uniform,
}
impl From<Reg> for CastTarget {
    fn from(unit: Reg) -> CastTarget {
        CastTarget::from(Uniform::from(unit))
    }
}
impl From<Uniform> for CastTarget {
    fn from(uniform: Uniform) -> CastTarget {
        CastTarget { prefix: [None; 8], prefix_chunk_size: Size::ZERO, rest: uniform }
    }
}
impl CastTarget {
    pub fn pair(a: Reg, b: Reg) -> CastTarget {
        CastTarget {
            prefix: [Some(a.kind), None, None, None, None, None, None, None],
            prefix_chunk_size: a.size,
            rest: Uniform::from(b),
        }
    }
    pub fn size<C: HasDataLayout>(&self, cx: &C) -> Size {
        (self.prefix_chunk_size * self.prefix.iter().filter(|x| x.is_some()).count() as u64)
            .align_to(self.rest.align(cx))
            + self.rest.total
    }
    pub fn align<C: HasDataLayout>(&self, cx: &C) -> Align {
        self.prefix
            .iter()
            .filter_map(|x| x.map(|kind| Reg { kind, size: self.prefix_chunk_size }.align(cx)))
            .fold(cx.data_layout().aggregate_align.abi.max(self.rest.align(cx)), |acc, align| {
                acc.max(align)
            })
    }
}
#[derive(Copy, Clone, Debug)]
pub enum HomogeneousAggregate {
    
    
    Homogeneous(Reg),
    
    NoData,
}
#[derive(Copy, Clone, Debug)]
pub struct Heterogeneous;
impl HomogeneousAggregate {
    
    
    pub fn unit(self) -> Option<Reg> {
        match self {
            HomogeneousAggregate::Homogeneous(reg) => Some(reg),
            HomogeneousAggregate::NoData => None,
        }
    }
    
    
    
    fn merge(self, other: HomogeneousAggregate) -> Result<HomogeneousAggregate, Heterogeneous> {
        match (self, other) {
            (x, HomogeneousAggregate::NoData) | (HomogeneousAggregate::NoData, x) => Ok(x),
            (HomogeneousAggregate::Homogeneous(a), HomogeneousAggregate::Homogeneous(b)) => {
                if a != b {
                    return Err(Heterogeneous);
                }
                Ok(self)
            }
        }
    }
}
impl<'a, Ty> TyAndLayout<'a, Ty> {
    fn is_aggregate(&self) -> bool {
        match self.abi {
            Abi::Uninhabited | Abi::Scalar(_) | Abi::Vector { .. } => false,
            Abi::ScalarPair(..) | Abi::Aggregate { .. } => true,
        }
    }
    
    
    
    
    
    
    
    
    
    
    pub fn homogeneous_aggregate<C>(&self, cx: &C) -> Result<HomogeneousAggregate, Heterogeneous>
    where
        Ty: TyAndLayoutMethods<'a, C> + Copy,
        C: LayoutOf<Ty = Ty, TyAndLayout = Self>,
    {
        match self.abi {
            Abi::Uninhabited => Err(Heterogeneous),
            
            Abi::Scalar(ref scalar) => {
                let kind = match scalar.value {
                    abi::Int(..) | abi::Pointer => RegKind::Integer,
                    abi::F32 | abi::F64 => RegKind::Float,
                };
                Ok(HomogeneousAggregate::Homogeneous(Reg { kind, size: self.size }))
            }
            Abi::Vector { .. } => {
                assert!(!self.is_zst());
                Ok(HomogeneousAggregate::Homogeneous(Reg {
                    kind: RegKind::Vector,
                    size: self.size,
                }))
            }
            Abi::ScalarPair(..) | Abi::Aggregate { .. } => {
                
                
                let from_fields_at =
                    |layout: Self,
                     start: Size|
                     -> Result<(HomogeneousAggregate, Size), Heterogeneous> {
                        let is_union = match layout.fields {
                            FieldsShape::Primitive => {
                                unreachable!("aggregates can't have `FieldsShape::Primitive`")
                            }
                            FieldsShape::Array { count, .. } => {
                                assert_eq!(start, Size::ZERO);
                                let result = if count > 0 {
                                    layout.field(cx, 0).homogeneous_aggregate(cx)?
                                } else {
                                    HomogeneousAggregate::NoData
                                };
                                return Ok((result, layout.size));
                            }
                            FieldsShape::Union(_) => true,
                            FieldsShape::Arbitrary { .. } => false,
                        };
                        let mut result = HomogeneousAggregate::NoData;
                        let mut total = start;
                        for i in 0..layout.fields.count() {
                            if !is_union && total != layout.fields.offset(i) {
                                return Err(Heterogeneous);
                            }
                            let field = layout.field(cx, i);
                            result = result.merge(field.homogeneous_aggregate(cx)?)?;
                            
                            let size = field.size;
                            if is_union {
                                total = total.max(size);
                            } else {
                                total += size;
                            }
                        }
                        Ok((result, total))
                    };
                let (mut result, mut total) = from_fields_at(*self, Size::ZERO)?;
                match &self.variants {
                    abi::Variants::Single { .. } => {}
                    abi::Variants::Multiple { variants, .. } => {
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        let variant_start = total;
                        for variant_idx in variants.indices() {
                            let (variant_result, variant_total) =
                                from_fields_at(self.for_variant(cx, variant_idx), variant_start)?;
                            result = result.merge(variant_result)?;
                            total = total.max(variant_total);
                        }
                    }
                }
                
                if total != self.size {
                    Err(Heterogeneous)
                } else {
                    match result {
                        HomogeneousAggregate::Homogeneous(_) => {
                            assert_ne!(total, Size::ZERO);
                        }
                        HomogeneousAggregate::NoData => {
                            assert_eq!(total, Size::ZERO);
                        }
                    }
                    Ok(result)
                }
            }
        }
    }
}
#[derive(Debug)]
pub struct ArgAbi<'a, Ty> {
    pub layout: TyAndLayout<'a, Ty>,
    
    pub pad: Option<Reg>,
    pub mode: PassMode,
}
impl<'a, Ty> ArgAbi<'a, Ty> {
    pub fn new(layout: TyAndLayout<'a, Ty>) -> Self {
        ArgAbi { layout, pad: None, mode: PassMode::Direct(ArgAttributes::new()) }
    }
    pub fn make_indirect(&mut self) {
        assert_eq!(self.mode, PassMode::Direct(ArgAttributes::new()));
        
        let mut attrs = ArgAttributes::new();
        
        
        
        attrs.set(ArgAttribute::NoAlias).set(ArgAttribute::NoCapture).set(ArgAttribute::NonNull);
        attrs.pointee_size = self.layout.size;
        
        
        
        let extra_attrs = self.layout.is_unsized().then_some(ArgAttributes::new());
        self.mode = PassMode::Indirect { attrs, extra_attrs, on_stack: false };
    }
    pub fn make_indirect_byval(&mut self) {
        self.make_indirect();
        match self.mode {
            PassMode::Indirect { attrs: _, extra_attrs: _, ref mut on_stack } => {
                *on_stack = true;
            }
            _ => unreachable!(),
        }
    }
    pub fn extend_integer_width_to(&mut self, bits: u64) {
        
        if let Abi::Scalar(ref scalar) = self.layout.abi {
            if let abi::Int(i, signed) = scalar.value {
                if i.size().bits() < bits {
                    if let PassMode::Direct(ref mut attrs) = self.mode {
                        if signed {
                            attrs.ext(ArgExtension::Sext)
                        } else {
                            attrs.ext(ArgExtension::Zext)
                        };
                    }
                }
            }
        }
    }
    pub fn cast_to<T: Into<CastTarget>>(&mut self, target: T) {
        assert_eq!(self.mode, PassMode::Direct(ArgAttributes::new()));
        self.mode = PassMode::Cast(target.into());
    }
    pub fn pad_with(&mut self, reg: Reg) {
        self.pad = Some(reg);
    }
    pub fn is_indirect(&self) -> bool {
        matches!(self.mode, PassMode::Indirect {..})
    }
    pub fn is_sized_indirect(&self) -> bool {
        matches!(self.mode, PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ })
    }
    pub fn is_unsized_indirect(&self) -> bool {
        matches!(self.mode, PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ })
    }
    pub fn is_ignore(&self) -> bool {
        matches!(self.mode, PassMode::Ignore)
    }
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Conv {
    
    
    C,
    Rust,
    
    ArmAapcs,
    Msp430Intr,
    PtxKernel,
    X86Fastcall,
    X86Intr,
    X86Stdcall,
    X86ThisCall,
    X86VectorCall,
    X86_64SysV,
    X86_64Win64,
    AmdGpuKernel,
    AvrInterrupt,
    AvrNonBlockingInterrupt,
}
#[derive(Debug)]
pub struct FnAbi<'a, Ty> {
    
    pub args: Vec<ArgAbi<'a, Ty>>,
    
    pub ret: ArgAbi<'a, Ty>,
    pub c_variadic: bool,
    
    
    
    
    pub fixed_count: usize,
    pub conv: Conv,
    pub can_unwind: bool,
}
impl<'a, Ty> FnAbi<'a, Ty> {
    pub fn adjust_for_cabi<C>(&mut self, cx: &C, abi: spec::abi::Abi) -> Result<(), String>
    where
        Ty: TyAndLayoutMethods<'a, C> + Copy,
        C: LayoutOf<Ty = Ty, TyAndLayout = TyAndLayout<'a, Ty>> + HasDataLayout + HasTargetSpec,
    {
        match &cx.target_spec().arch[..] {
            "x86" => {
                let flavor = if abi == spec::abi::Abi::Fastcall {
                    x86::Flavor::Fastcall
                } else {
                    x86::Flavor::General
                };
                x86::compute_abi_info(cx, self, flavor);
            }
            "x86_64" => {
                if abi == spec::abi::Abi::SysV64 {
                    x86_64::compute_abi_info(cx, self);
                } else if abi == spec::abi::Abi::Win64 || cx.target_spec().is_like_windows {
                    x86_win64::compute_abi_info(self);
                } else {
                    x86_64::compute_abi_info(cx, self);
                }
            }
            "aarch64" => aarch64::compute_abi_info(cx, self),
            "amdgpu" => amdgpu::compute_abi_info(cx, self),
            "arm" => arm::compute_abi_info(cx, self),
            "avr" => avr::compute_abi_info(self),
            "mips" => mips::compute_abi_info(cx, self),
            "mips64" => mips64::compute_abi_info(cx, self),
            "powerpc" => powerpc::compute_abi_info(self),
            "powerpc64" => powerpc64::compute_abi_info(cx, self),
            "s390x" => s390x::compute_abi_info(cx, self),
            "msp430" => msp430::compute_abi_info(self),
            "sparc" => sparc::compute_abi_info(cx, self),
            "sparc64" => sparc64::compute_abi_info(cx, self),
            "nvptx" => nvptx::compute_abi_info(self),
            "nvptx64" => nvptx64::compute_abi_info(self),
            "hexagon" => hexagon::compute_abi_info(self),
            "riscv32" | "riscv64" => riscv::compute_abi_info(cx, self),
            "wasm32" => match cx.target_spec().os.as_str() {
                "emscripten" | "wasi" => wasm32::compute_abi_info(cx, self),
                _ => wasm32_bindgen_compat::compute_abi_info(self),
            },
            "asmjs" => wasm32::compute_abi_info(cx, self),
            a => return Err(format!("unrecognized arch \"{}\" in target specification", a)),
        }
        Ok(())
    }
}