Skip to content

RFC: Implement base numeric types and bool as a library #11526

Closed
@brendanzab

Description

@brendanzab

Here is a (possibly crazy) proposal to remove some magic from the compiler:

#[repr=1 ] pub enum bool { true, false }

#[repr=8 ] pub struct u8;
#[repr=16] pub struct u16;
#[repr=32] pub struct u32;
#[repr=64] pub struct u64;

#[cfg(target_pointer_size = "32")] #[repr=32] pub struct uintptr;
#[cfg(target_pointer_size = "64")] #[repr=64] pub struct uintptr;

#[repr=8 ] pub struct i8;
#[repr=16] pub struct i16;
#[repr=32] pub struct i32;
#[repr=64] pub struct i64;

#[cfg(target_pointer_size = "32")] #[repr=32] pub struct intptr;
#[cfg(target_pointer_size = "64")] #[repr=64] pub struct intptr;

#[repr=32] pub struct f32;
#[repr=64] pub struct f64;

macro_rules! impl_llvm_op(
    ($Binop:ident, $binop:ident, $Self:ident, $init:expr, $llvm_op:ident, $llvm_ty:ident) => (
        impl $Binop<$Self, $Self> For $Self {
            fn $binop(a: &$Self, b: &$Self) -> $Self {
                let (a, b) = (*a, *b);
                unsafe {
                    let ret = &mut $init;
                    llvm!(concat!("%ret = ", stringify!($llvm_op), " ",
                                  stringify!($llvm_ty), " %a, %b")
                        :"a" = a
                        :"b" = b
                        :"ret" = ret
                    )
                    *ret
                }
            }
        }
    )
)

impl_llvm_op!(Add, add, u8, 0, add, i8)
impl_llvm_op!(Add, add, u16, 0, add, i16)
impl_llvm_op!(Add, add, u32, 0, add, i32)
impl_llvm_op!(Add, add, u64, 0, add, i64)

#[cfg(target_pointer_size = "32")] impl_llvm_op!(Add, add, uintptr, 0, add, i32)
#[cfg(target_pointer_size = "64")] impl_llvm_op!(Add, add, uintptr, 0, add, i64)

impl_llvm_op!(Add, add, i8, 0, add, i8)
impl_llvm_op!(Add, add, i16, 0, add, i16)
impl_llvm_op!(Add, add, i32, 0, add, i32)
impl_llvm_op!(Add, add, i64, 0, add, i64)

#[cfg(target_pointer_size = "32")] impl_llvm_op!(Add, add, intptr, 0, add, i32)
#[cfg(target_pointer_size = "64")] impl_llvm_op!(Add, add, intptr, 0, add, i64)

impl_llvm_op!(Add, add, f32, 0.0, fadd, f32)
impl_llvm_op!(Add, add, f64, 0.0, fadd, f64)

This relies on many hypothetical language features including the llvm! macro, CTFE and possibly some way of implementing adding literal assignments, and so might be too ambitious for 1.0. But in the end it could make our language far more extensible and powerful. It might also help simplify our documentation issues with builtin types #10114 #11409

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions