mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 15:21:12 +00:00
generalize all number intrinsics
This commit is contained in:
parent
9fc832d8ef
commit
ab34c2a55e
2 changed files with 182 additions and 218 deletions
|
@ -5,9 +5,10 @@ pub const OBJ_PATH: &str = env!(
|
||||||
"Env var BUILTINS_O not found. Is there a problem with the build script?"
|
"Env var BUILTINS_O not found. Is there a problem with the build script?"
|
||||||
);
|
);
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct IntrinsicName {
|
pub struct IntrinsicName {
|
||||||
pub options: [ &'static str; 12 ],
|
pub base: &'static str,
|
||||||
|
pub options: [&'static str; 13],
|
||||||
pub is_float: bool,
|
pub is_float: bool,
|
||||||
pub is_int: bool,
|
pub is_int: bool,
|
||||||
}
|
}
|
||||||
|
@ -15,15 +16,16 @@ pub struct IntrinsicName {
|
||||||
impl IntrinsicName {
|
impl IntrinsicName {
|
||||||
pub const fn default() -> Self {
|
pub const fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
options: [ ""; 12 ],
|
base: "",
|
||||||
|
options: [""; 13],
|
||||||
is_float: false,
|
is_float: false,
|
||||||
is_int: false,
|
is_int: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn intrinsics<'a>(&'a self) -> impl Iterator<Item = &&'a str> {
|
pub fn intrinsics<'a>(&'a self) -> impl Iterator<Item = &&'a str> {
|
||||||
let start_index = if self.is_float { 0} else { 3 };
|
let start_index = if self.is_float { 0 } else { 3 };
|
||||||
let end_index = if self.is_int { 12 } else { 3 };
|
let end_index = if self.is_int { 13 } else { 3 };
|
||||||
|
|
||||||
let slice = &self.options[start_index..end_index];
|
let slice = &self.options[start_index..end_index];
|
||||||
|
|
||||||
|
@ -31,7 +33,6 @@ impl IntrinsicName {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum DecWidth {
|
pub enum DecWidth {
|
||||||
Dec,
|
Dec,
|
||||||
|
@ -58,7 +59,6 @@ pub enum IntWidth {
|
||||||
I128,
|
I128,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl Index<FloatWidth> for IntrinsicName {
|
impl Index<FloatWidth> for IntrinsicName {
|
||||||
type Output = str;
|
type Output = str;
|
||||||
|
|
||||||
|
@ -76,23 +76,20 @@ impl Index<IntWidth> for IntrinsicName {
|
||||||
|
|
||||||
fn index(&self, index: IntWidth) -> &Self::Output {
|
fn index(&self, index: IntWidth) -> &Self::Output {
|
||||||
match index {
|
match index {
|
||||||
IntWidth::U8 => self.options[3],
|
IntWidth::U8 => self.options[3],
|
||||||
IntWidth::U16 => self.options[4],
|
IntWidth::U16 => self.options[4],
|
||||||
IntWidth::U32 => self.options[5],
|
IntWidth::U32 => self.options[5],
|
||||||
IntWidth::U64 => self.options[6],
|
IntWidth::U64 => self.options[6],
|
||||||
IntWidth::U128 => self.options[7],
|
IntWidth::U128 => self.options[7],
|
||||||
IntWidth::I8 => self.options[8],
|
IntWidth::I8 => self.options[8],
|
||||||
IntWidth::I16 => self.options[9],
|
IntWidth::I16 => self.options[9],
|
||||||
IntWidth::I32 => self.options[10],
|
IntWidth::I32 => self.options[10],
|
||||||
IntWidth::I64 => self.options[11],
|
IntWidth::I64 => self.options[11],
|
||||||
IntWidth::I128 => self.options[12],
|
IntWidth::I128 => self.options[12],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub const NUM_ASIN: &str = "roc_builtins.num.asin";
|
pub const NUM_ASIN: &str = "roc_builtins.num.asin";
|
||||||
pub const NUM_ACOS: &str = "roc_builtins.num.acos";
|
pub const NUM_ACOS: &str = "roc_builtins.num.acos";
|
||||||
pub const NUM_ATAN: &str = "roc_builtins.num.atan";
|
pub const NUM_ATAN: &str = "roc_builtins.num.atan";
|
||||||
|
|
|
@ -48,7 +48,7 @@ use inkwell::{AddressSpace, IntPredicate};
|
||||||
use morphic_lib::{
|
use morphic_lib::{
|
||||||
CalleeSpecVar, FuncName, FuncSpec, FuncSpecSolutions, ModSolutions, UpdateMode, UpdateModeVar,
|
CalleeSpecVar, FuncName, FuncSpec, FuncSpecSolutions, ModSolutions, UpdateMode, UpdateModeVar,
|
||||||
};
|
};
|
||||||
use roc_builtins::bitcode::{self, IntrinsicName, IntWidth, FloatWidth, DecWidth};
|
use roc_builtins::bitcode::{self, DecWidth, FloatWidth, IntWidth, IntrinsicName};
|
||||||
use roc_collections::all::{ImMap, MutMap, MutSet};
|
use roc_collections::all::{ImMap, MutMap, MutSet};
|
||||||
use roc_module::low_level::LowLevel;
|
use roc_module::low_level::LowLevel;
|
||||||
use roc_module::symbol::{Interns, ModuleId, Symbol};
|
use roc_module::symbol::{Interns, ModuleId, Symbol};
|
||||||
|
@ -430,6 +430,58 @@ pub fn module_from_builtins<'ctx>(
|
||||||
module
|
module
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_float_intrinsic<'ctx, F>(
|
||||||
|
ctx: &'ctx Context,
|
||||||
|
module: &Module<'ctx>,
|
||||||
|
name: &IntrinsicName,
|
||||||
|
construct_type: F,
|
||||||
|
) where
|
||||||
|
F: Fn(inkwell::types::FloatType<'ctx>) -> inkwell::types::FunctionType<'ctx>,
|
||||||
|
{
|
||||||
|
macro_rules! check {
|
||||||
|
($width:expr, $typ:expr) => {
|
||||||
|
let full_name = &name[$width];
|
||||||
|
|
||||||
|
if let Some(_) = module.get_function(full_name) {
|
||||||
|
// zig defined this function already
|
||||||
|
} else {
|
||||||
|
add_intrinsic(module, full_name, construct_type($typ));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
check!(FloatWidth::F32, ctx.f32_type());
|
||||||
|
check!(FloatWidth::F64, ctx.f64_type());
|
||||||
|
// check!(IntWidth::F128, ctx.i128_type());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_int_intrinsic<'ctx, F>(
|
||||||
|
ctx: &'ctx Context,
|
||||||
|
module: &Module<'ctx>,
|
||||||
|
name: &IntrinsicName,
|
||||||
|
construct_type: F,
|
||||||
|
) where
|
||||||
|
F: Fn(inkwell::types::IntType<'ctx>) -> inkwell::types::FunctionType<'ctx>,
|
||||||
|
{
|
||||||
|
macro_rules! check {
|
||||||
|
($width:expr, $typ:expr) => {
|
||||||
|
let full_name = &name[$width];
|
||||||
|
|
||||||
|
if let Some(_) = module.get_function(full_name) {
|
||||||
|
// zig defined this function already
|
||||||
|
} else {
|
||||||
|
add_intrinsic(module, full_name, construct_type($typ));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
check!(IntWidth::I8, ctx.i8_type());
|
||||||
|
check!(IntWidth::I16, ctx.i16_type());
|
||||||
|
check!(IntWidth::I32, ctx.i32_type());
|
||||||
|
check!(IntWidth::I64, ctx.i64_type());
|
||||||
|
check!(IntWidth::I128, ctx.i128_type());
|
||||||
|
}
|
||||||
|
|
||||||
fn add_intrinsics<'ctx>(ctx: &'ctx Context, module: &Module<'ctx>) {
|
fn add_intrinsics<'ctx>(ctx: &'ctx Context, module: &Module<'ctx>) {
|
||||||
// List of all supported LLVM intrinsics:
|
// List of all supported LLVM intrinsics:
|
||||||
//
|
//
|
||||||
|
@ -438,7 +490,6 @@ fn add_intrinsics<'ctx>(ctx: &'ctx Context, module: &Module<'ctx>) {
|
||||||
let i1_type = ctx.bool_type();
|
let i1_type = ctx.bool_type();
|
||||||
let i8_type = ctx.i8_type();
|
let i8_type = ctx.i8_type();
|
||||||
let i8_ptr_type = i8_type.ptr_type(AddressSpace::Generic);
|
let i8_ptr_type = i8_type.ptr_type(AddressSpace::Generic);
|
||||||
let i16_type = ctx.i16_type();
|
|
||||||
let i32_type = ctx.i32_type();
|
let i32_type = ctx.i32_type();
|
||||||
let i64_type = ctx.i64_type();
|
let i64_type = ctx.i64_type();
|
||||||
let void_type = ctx.void_type();
|
let void_type = ctx.void_type();
|
||||||
|
@ -475,103 +526,40 @@ fn add_intrinsics<'ctx>(ctx: &'ctx Context, module: &Module<'ctx>) {
|
||||||
|
|
||||||
add_intrinsic(module, LLVM_STACK_SAVE, i8_ptr_type.fn_type(&[], false));
|
add_intrinsic(module, LLVM_STACK_SAVE, i8_ptr_type.fn_type(&[], false));
|
||||||
|
|
||||||
add_intrinsic(
|
|
||||||
module,
|
|
||||||
LLVM_LOG_F64,
|
|
||||||
f64_type.fn_type(&[f64_type.into()], false),
|
|
||||||
);
|
|
||||||
|
|
||||||
add_intrinsic(
|
add_intrinsic(
|
||||||
module,
|
module,
|
||||||
LLVM_LROUND_I64_F64,
|
LLVM_LROUND_I64_F64,
|
||||||
i64_type.fn_type(&[f64_type.into()], false),
|
i64_type.fn_type(&[f64_type.into()], false),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
add_float_intrinsic(ctx, module, &LLVM_LOG, |t| t.fn_type(&[t.into()], false));
|
||||||
|
add_float_intrinsic(ctx, module, &LLVM_POW, |t| {
|
||||||
|
t.fn_type(&[t.into(), t.into()], false)
|
||||||
|
});
|
||||||
|
add_float_intrinsic(ctx, module, &LLVM_FABS, |t| t.fn_type(&[t.into()], false));
|
||||||
|
add_float_intrinsic(ctx, module, &LLVM_SIN, |t| t.fn_type(&[t.into()], false));
|
||||||
|
add_float_intrinsic(ctx, module, &LLVM_COS, |t| t.fn_type(&[t.into()], false));
|
||||||
|
add_float_intrinsic(ctx, module, &LLVM_CEILING, |t| {
|
||||||
|
t.fn_type(&[t.into()], false)
|
||||||
|
});
|
||||||
|
add_float_intrinsic(ctx, module, &LLVM_FLOOR, |t| t.fn_type(&[t.into()], false));
|
||||||
|
|
||||||
for name in LLVM_POW.intrinsics() {
|
add_int_intrinsic(ctx, module, &LLVM_SADD_WITH_OVERFLOW, |t| {
|
||||||
add_intrinsic(
|
let fields = [t.into(), i1_type.into()];
|
||||||
module,
|
|
||||||
name,
|
|
||||||
f64_type.fn_type(&[f64_type.into(), f64_type.into()], false),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
for width in ["f32", "f64"] {
|
|
||||||
let name = format!("{}.{}", LLVM_FABS, width);
|
|
||||||
add_intrinsic(module, &name, f64_type.fn_type(&[f64_type.into()], false));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
add_intrinsic(
|
|
||||||
module,
|
|
||||||
LLVM_SIN_F64,
|
|
||||||
f64_type.fn_type(&[f64_type.into()], false),
|
|
||||||
);
|
|
||||||
|
|
||||||
add_intrinsic(
|
|
||||||
module,
|
|
||||||
LLVM_COS_F64,
|
|
||||||
f64_type.fn_type(&[f64_type.into()], false),
|
|
||||||
);
|
|
||||||
|
|
||||||
add_intrinsic(
|
|
||||||
module,
|
|
||||||
LLVM_CEILING_F64,
|
|
||||||
f64_type.fn_type(&[f64_type.into()], false),
|
|
||||||
);
|
|
||||||
|
|
||||||
add_intrinsic(
|
|
||||||
module,
|
|
||||||
LLVM_FLOOR_F64,
|
|
||||||
f64_type.fn_type(&[f64_type.into()], false),
|
|
||||||
);
|
|
||||||
|
|
||||||
add_intrinsic(module, LLVM_SADD_WITH_OVERFLOW_I8, {
|
|
||||||
let fields = [i8_type.into(), i1_type.into()];
|
|
||||||
ctx.struct_type(&fields, false)
|
ctx.struct_type(&fields, false)
|
||||||
.fn_type(&[i8_type.into(), i8_type.into()], false)
|
.fn_type(&[t.into(), t.into()], false)
|
||||||
});
|
});
|
||||||
|
|
||||||
add_intrinsic(module, LLVM_SADD_WITH_OVERFLOW_I16, {
|
add_int_intrinsic(ctx, module, &LLVM_SSUB_WITH_OVERFLOW, |t| {
|
||||||
let fields = [i16_type.into(), i1_type.into()];
|
let fields = [t.into(), i1_type.into()];
|
||||||
ctx.struct_type(&fields, false)
|
ctx.struct_type(&fields, false)
|
||||||
.fn_type(&[i16_type.into(), i16_type.into()], false)
|
.fn_type(&[t.into(), t.into()], false)
|
||||||
});
|
});
|
||||||
|
|
||||||
add_intrinsic(module, LLVM_SADD_WITH_OVERFLOW_I32, {
|
add_int_intrinsic(ctx, module, &LLVM_SMUL_WITH_OVERFLOW, |t| {
|
||||||
let fields = [i32_type.into(), i1_type.into()];
|
let fields = [t.into(), i1_type.into()];
|
||||||
ctx.struct_type(&fields, false)
|
ctx.struct_type(&fields, false)
|
||||||
.fn_type(&[i32_type.into(), i32_type.into()], false)
|
.fn_type(&[t.into(), t.into()], false)
|
||||||
});
|
|
||||||
|
|
||||||
add_intrinsic(module, LLVM_SADD_WITH_OVERFLOW_I64, {
|
|
||||||
let fields = [i64_type.into(), i1_type.into()];
|
|
||||||
ctx.struct_type(&fields, false)
|
|
||||||
.fn_type(&[i64_type.into(), i64_type.into()], false)
|
|
||||||
});
|
|
||||||
|
|
||||||
add_intrinsic(module, LLVM_SSUB_WITH_OVERFLOW_I8, {
|
|
||||||
let fields = [i8_type.into(), i1_type.into()];
|
|
||||||
ctx.struct_type(&fields, false)
|
|
||||||
.fn_type(&[i8_type.into(), i8_type.into()], false)
|
|
||||||
});
|
|
||||||
|
|
||||||
add_intrinsic(module, LLVM_SSUB_WITH_OVERFLOW_I16, {
|
|
||||||
let fields = [i16_type.into(), i1_type.into()];
|
|
||||||
ctx.struct_type(&fields, false)
|
|
||||||
.fn_type(&[i16_type.into(), i16_type.into()], false)
|
|
||||||
});
|
|
||||||
|
|
||||||
add_intrinsic(module, LLVM_SSUB_WITH_OVERFLOW_I32, {
|
|
||||||
let fields = [i32_type.into(), i1_type.into()];
|
|
||||||
ctx.struct_type(&fields, false)
|
|
||||||
.fn_type(&[i32_type.into(), i32_type.into()], false)
|
|
||||||
});
|
|
||||||
|
|
||||||
add_intrinsic(module, LLVM_SSUB_WITH_OVERFLOW_I64, {
|
|
||||||
let fields = [i64_type.into(), i1_type.into()];
|
|
||||||
ctx.struct_type(&fields, false)
|
|
||||||
.fn_type(&[i64_type.into(), i64_type.into()], false)
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -585,57 +573,58 @@ macro_rules! define_float_intrinsic {
|
||||||
|
|
||||||
macro_rules! define_int_intrinsic {
|
macro_rules! define_int_intrinsic {
|
||||||
($name:literal, $output:expr) => {
|
($name:literal, $output:expr) => {
|
||||||
$output.options[3] = concat!($name, ".i8");
|
$output.options[3] = concat!($name, ".i8");
|
||||||
$output.options[4] = concat!($name, ".i16");
|
$output.options[4] = concat!($name, ".i16");
|
||||||
$output.options[5] = concat!($name, ".i32");
|
$output.options[5] = concat!($name, ".i32");
|
||||||
$output.options[6] = concat!($name, ".i64");
|
$output.options[6] = concat!($name, ".i64");
|
||||||
$output.options[7] = concat!($name, ".i128");
|
$output.options[7] = concat!($name, ".i128");
|
||||||
$output.options[8] = concat!($name, ".i8");
|
$output.options[8] = concat!($name, ".i8");
|
||||||
$output.options[9] = concat!($name, ".i16");
|
$output.options[9] = concat!($name, ".i16");
|
||||||
$output.options[10] = concat!($name, ".i32");
|
$output.options[10] = concat!($name, ".i32");
|
||||||
$output.options[11] = concat!($name, ".i64");
|
$output.options[11] = concat!($name, ".i64");
|
||||||
$output.options[12] = concat!($name, ".i128");
|
$output.options[12] = concat!($name, ".i128");
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! float_intrinsic {
|
macro_rules! float_intrinsic {
|
||||||
($name:literal) => { {
|
($name:literal) => {{
|
||||||
let mut output = IntrinsicName::default();
|
let mut output = IntrinsicName::default();
|
||||||
|
|
||||||
define_float_intrinsic!($name, output);
|
define_float_intrinsic!($name, output);
|
||||||
|
|
||||||
output.is_float = true;
|
output.is_float = true;
|
||||||
|
output.base = $name;
|
||||||
|
|
||||||
output
|
output
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! int_intrinsic {
|
macro_rules! int_intrinsic {
|
||||||
($name:literal) => { {
|
($name:literal) => {{
|
||||||
let mut output = IntrinsicName::default();
|
let mut output = IntrinsicName::default();
|
||||||
|
|
||||||
define_int_intrinsic!($name, output);
|
define_int_intrinsic!($name, output);
|
||||||
|
|
||||||
output.is_int = true;
|
output.is_int = true;
|
||||||
|
output.base = $name;
|
||||||
|
|
||||||
output
|
output
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub const LLVM_FABS: IntrinsicName = float_intrinsic!("llvm.fabs");
|
const LLVM_POW: IntrinsicName = float_intrinsic!("llvm.pow");
|
||||||
static LLVM_POW: IntrinsicName = float_intrinsic!("llvm.pow");
|
const LLVM_FABS: IntrinsicName = float_intrinsic!("llvm.fabs");
|
||||||
|
static LLVM_SQRT: IntrinsicName = float_intrinsic!("llvm.sqrt");
|
||||||
|
static LLVM_LOG: IntrinsicName = float_intrinsic!("llvm.log");
|
||||||
|
|
||||||
|
static LLVM_SIN: IntrinsicName = float_intrinsic!("llvm.sin");
|
||||||
|
static LLVM_COS: IntrinsicName = float_intrinsic!("llvm.cos");
|
||||||
|
static LLVM_CEILING: IntrinsicName = float_intrinsic!("llvm.ceil");
|
||||||
|
static LLVM_FLOOR: IntrinsicName = float_intrinsic!("llvm.floor");
|
||||||
|
|
||||||
static LLVM_MEMSET_I64: &str = "llvm.memset.p0i8.i64";
|
static LLVM_MEMSET_I64: &str = "llvm.memset.p0i8.i64";
|
||||||
static LLVM_MEMSET_I32: &str = "llvm.memset.p0i8.i32";
|
static LLVM_MEMSET_I32: &str = "llvm.memset.p0i8.i32";
|
||||||
static LLVM_SQRT_F64: &str = "llvm.sqrt.f64";
|
|
||||||
static LLVM_LOG_F64: &str = "llvm.log.f64";
|
|
||||||
static LLVM_LROUND_I64_F64: &str = "llvm.lround.i64.f64";
|
static LLVM_LROUND_I64_F64: &str = "llvm.lround.i64.f64";
|
||||||
static LLVM_FABS: &str = "llvm.fabs";
|
|
||||||
static LLVM_SIN_F64: &str = "llvm.sin.f64";
|
|
||||||
static LLVM_COS_F64: &str = "llvm.cos.f64";
|
|
||||||
static LLVM_CEILING_F64: &str = "llvm.ceil.f64";
|
|
||||||
static LLVM_FLOOR_F64: &str = "llvm.floor.f64";
|
|
||||||
|
|
||||||
// static LLVM_FRAME_ADDRESS: &str = "llvm.frameaddress";
|
// static LLVM_FRAME_ADDRESS: &str = "llvm.frameaddress";
|
||||||
static LLVM_FRAME_ADDRESS: &str = "llvm.frameaddress.p0i8";
|
static LLVM_FRAME_ADDRESS: &str = "llvm.frameaddress.p0i8";
|
||||||
|
@ -644,19 +633,9 @@ static LLVM_STACK_SAVE: &str = "llvm.stacksave";
|
||||||
static LLVM_SETJMP: &str = "llvm.eh.sjlj.setjmp";
|
static LLVM_SETJMP: &str = "llvm.eh.sjlj.setjmp";
|
||||||
pub static LLVM_LONGJMP: &str = "llvm.eh.sjlj.longjmp";
|
pub static LLVM_LONGJMP: &str = "llvm.eh.sjlj.longjmp";
|
||||||
|
|
||||||
pub static LLVM_SADD_WITH_OVERFLOW_I8: &str = "llvm.sadd.with.overflow.i8";
|
const LLVM_SADD_WITH_OVERFLOW: IntrinsicName = int_intrinsic!("llvm.sadd.with.overflow");
|
||||||
pub static LLVM_SADD_WITH_OVERFLOW_I16: &str = "llvm.sadd.with.overflow.i16";
|
const LLVM_SSUB_WITH_OVERFLOW: IntrinsicName = int_intrinsic!("llvm.ssub.with.overflow");
|
||||||
pub static LLVM_SADD_WITH_OVERFLOW_I32: &str = "llvm.sadd.with.overflow.i32";
|
const LLVM_SMUL_WITH_OVERFLOW: IntrinsicName = int_intrinsic!("llvm.smul.with.overflow");
|
||||||
pub static LLVM_SADD_WITH_OVERFLOW_I64: &str = "llvm.sadd.with.overflow.i64";
|
|
||||||
pub static LLVM_SADD_WITH_OVERFLOW_I128: &str = "llvm.sadd.with.overflow.i128";
|
|
||||||
|
|
||||||
pub static LLVM_SSUB_WITH_OVERFLOW_I8: &str = "llvm.ssub.with.overflow.i8";
|
|
||||||
pub static LLVM_SSUB_WITH_OVERFLOW_I16: &str = "llvm.ssub.with.overflow.i16";
|
|
||||||
pub static LLVM_SSUB_WITH_OVERFLOW_I32: &str = "llvm.ssub.with.overflow.i32";
|
|
||||||
pub static LLVM_SSUB_WITH_OVERFLOW_I64: &str = "llvm.ssub.with.overflow.i64";
|
|
||||||
pub static LLVM_SSUB_WITH_OVERFLOW_I128: &str = "llvm.ssub.with.overflow.i128";
|
|
||||||
|
|
||||||
pub static LLVM_SMUL_WITH_OVERFLOW_I64: &str = "llvm.smul.with.overflow.i64";
|
|
||||||
|
|
||||||
fn add_intrinsic<'ctx>(
|
fn add_intrinsic<'ctx>(
|
||||||
module: &Module<'ctx>,
|
module: &Module<'ctx>,
|
||||||
|
@ -5956,28 +5935,28 @@ fn throw_on_overflow<'a, 'ctx, 'env>(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn intwidth_from_builtin(builtin: Builtin<'_>, ptr_bytes: u32) -> IntWidth {
|
pub fn intwidth_from_builtin(builtin: Builtin<'_>, ptr_bytes: u32) -> IntWidth {
|
||||||
use IntWidth::*;
|
use IntWidth::*;
|
||||||
|
|
||||||
match builtin {
|
match builtin {
|
||||||
Builtin::Int128 => I128,
|
Builtin::Int128 => I128,
|
||||||
Builtin::Int64 => I64,
|
Builtin::Int64 => I64,
|
||||||
Builtin::Int32 => I32,
|
Builtin::Int32 => I32,
|
||||||
Builtin::Int16 => I16,
|
Builtin::Int16 => I16,
|
||||||
Builtin::Int8 => I8,
|
Builtin::Int8 => I8,
|
||||||
Builtin::Usize => match ptr_bytes {
|
Builtin::Usize => match ptr_bytes {
|
||||||
4 => I32,
|
4 => I32,
|
||||||
8 => I64,
|
8 => I64,
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
},
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn intwidth_from_layout(layout: Layout<'_>, ptr_bytes: u32) -> IntWidth {
|
fn intwidth_from_layout(layout: Layout<'_>, ptr_bytes: u32) -> IntWidth {
|
||||||
match layout {
|
match layout {
|
||||||
Layout::Builtin(builtin) => intwidth_from_builtin(builtin, ptr_bytes),
|
Layout::Builtin(builtin) => intwidth_from_builtin(builtin, ptr_bytes),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_int_binop<'a, 'ctx, 'env>(
|
fn build_int_binop<'a, 'ctx, 'env>(
|
||||||
|
@ -5998,60 +5977,50 @@ fn build_int_binop<'a, 'ctx, 'env>(
|
||||||
|
|
||||||
match op {
|
match op {
|
||||||
NumAdd => {
|
NumAdd => {
|
||||||
let intrinsic = match lhs_layout {
|
|
||||||
Layout::Builtin(Builtin::Int8) => LLVM_SADD_WITH_OVERFLOW_I8,
|
|
||||||
Layout::Builtin(Builtin::Int16) => LLVM_SADD_WITH_OVERFLOW_I16,
|
|
||||||
Layout::Builtin(Builtin::Int32) => LLVM_SADD_WITH_OVERFLOW_I32,
|
|
||||||
Layout::Builtin(Builtin::Int64) => LLVM_SADD_WITH_OVERFLOW_I64,
|
|
||||||
Layout::Builtin(Builtin::Int128) => LLVM_SADD_WITH_OVERFLOW_I128,
|
|
||||||
Layout::Builtin(Builtin::Usize) => match env.ptr_bytes {
|
|
||||||
4 => LLVM_SADD_WITH_OVERFLOW_I32,
|
|
||||||
8 => LLVM_SADD_WITH_OVERFLOW_I64,
|
|
||||||
other => panic!("invalid ptr_bytes {}", other),
|
|
||||||
},
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let result = env
|
let result = env
|
||||||
.call_intrinsic(intrinsic, &[lhs.into(), rhs.into()])
|
.call_intrinsic(
|
||||||
|
&LLVM_SADD_WITH_OVERFLOW[int_width],
|
||||||
|
&[lhs.into(), rhs.into()],
|
||||||
|
)
|
||||||
.into_struct_value();
|
.into_struct_value();
|
||||||
|
|
||||||
throw_on_overflow(env, parent, result, "integer addition overflowed!")
|
throw_on_overflow(env, parent, result, "integer addition overflowed!")
|
||||||
}
|
}
|
||||||
NumAddWrap => bd.build_int_add(lhs, rhs, "add_int_wrap").into(),
|
NumAddWrap => bd.build_int_add(lhs, rhs, "add_int_wrap").into(),
|
||||||
NumAddChecked => env.call_intrinsic(LLVM_SADD_WITH_OVERFLOW_I64, &[lhs.into(), rhs.into()]),
|
NumAddChecked => env.call_intrinsic(
|
||||||
|
&LLVM_SADD_WITH_OVERFLOW[int_width],
|
||||||
|
&[lhs.into(), rhs.into()],
|
||||||
|
),
|
||||||
NumSub => {
|
NumSub => {
|
||||||
let intrinsic = match lhs_layout {
|
|
||||||
Layout::Builtin(Builtin::Int8) => LLVM_SSUB_WITH_OVERFLOW_I8,
|
|
||||||
Layout::Builtin(Builtin::Int16) => LLVM_SSUB_WITH_OVERFLOW_I16,
|
|
||||||
Layout::Builtin(Builtin::Int32) => LLVM_SSUB_WITH_OVERFLOW_I32,
|
|
||||||
Layout::Builtin(Builtin::Int64) => LLVM_SSUB_WITH_OVERFLOW_I64,
|
|
||||||
Layout::Builtin(Builtin::Int128) => LLVM_SSUB_WITH_OVERFLOW_I128,
|
|
||||||
Layout::Builtin(Builtin::Usize) => match env.ptr_bytes {
|
|
||||||
4 => LLVM_SSUB_WITH_OVERFLOW_I32,
|
|
||||||
8 => LLVM_SSUB_WITH_OVERFLOW_I64,
|
|
||||||
other => panic!("invalid ptr_bytes {}", other),
|
|
||||||
},
|
|
||||||
_ => unreachable!("invalid layout {:?}", lhs_layout),
|
|
||||||
};
|
|
||||||
|
|
||||||
let result = env
|
let result = env
|
||||||
.call_intrinsic(intrinsic, &[lhs.into(), rhs.into()])
|
.call_intrinsic(
|
||||||
|
&LLVM_SSUB_WITH_OVERFLOW[int_width],
|
||||||
|
&[lhs.into(), rhs.into()],
|
||||||
|
)
|
||||||
.into_struct_value();
|
.into_struct_value();
|
||||||
|
|
||||||
throw_on_overflow(env, parent, result, "integer subtraction overflowed!")
|
throw_on_overflow(env, parent, result, "integer subtraction overflowed!")
|
||||||
}
|
}
|
||||||
NumSubWrap => bd.build_int_sub(lhs, rhs, "sub_int").into(),
|
NumSubWrap => bd.build_int_sub(lhs, rhs, "sub_int").into(),
|
||||||
NumSubChecked => env.call_intrinsic(LLVM_SSUB_WITH_OVERFLOW_I64, &[lhs.into(), rhs.into()]),
|
NumSubChecked => env.call_intrinsic(
|
||||||
|
&LLVM_SSUB_WITH_OVERFLOW[int_width],
|
||||||
|
&[lhs.into(), rhs.into()],
|
||||||
|
),
|
||||||
NumMul => {
|
NumMul => {
|
||||||
let result = env
|
let result = env
|
||||||
.call_intrinsic(LLVM_SMUL_WITH_OVERFLOW_I64, &[lhs.into(), rhs.into()])
|
.call_intrinsic(
|
||||||
|
&LLVM_SMUL_WITH_OVERFLOW[int_width],
|
||||||
|
&[lhs.into(), rhs.into()],
|
||||||
|
)
|
||||||
.into_struct_value();
|
.into_struct_value();
|
||||||
|
|
||||||
throw_on_overflow(env, parent, result, "integer multiplication overflowed!")
|
throw_on_overflow(env, parent, result, "integer multiplication overflowed!")
|
||||||
}
|
}
|
||||||
NumMulWrap => bd.build_int_mul(lhs, rhs, "mul_int").into(),
|
NumMulWrap => bd.build_int_mul(lhs, rhs, "mul_int").into(),
|
||||||
NumMulChecked => env.call_intrinsic(LLVM_SMUL_WITH_OVERFLOW_I64, &[lhs.into(), rhs.into()]),
|
NumMulChecked => env.call_intrinsic(
|
||||||
|
&LLVM_SMUL_WITH_OVERFLOW[int_width],
|
||||||
|
&[lhs.into(), rhs.into()],
|
||||||
|
),
|
||||||
NumGt => bd.build_int_compare(SGT, lhs, rhs, "int_gt").into(),
|
NumGt => bd.build_int_compare(SGT, lhs, rhs, "int_gt").into(),
|
||||||
NumGte => bd.build_int_compare(SGE, lhs, rhs, "int_gte").into(),
|
NumGte => bd.build_int_compare(SGE, lhs, rhs, "int_gte").into(),
|
||||||
NumLt => bd.build_int_compare(SLT, lhs, rhs, "int_lt").into(),
|
NumLt => bd.build_int_compare(SLT, lhs, rhs, "int_lt").into(),
|
||||||
|
@ -6627,7 +6596,6 @@ fn int_abs_with_overflow<'a, 'ctx, 'env>(
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn build_float_unary_op<'a, 'ctx, 'env>(
|
fn build_float_unary_op<'a, 'ctx, 'env>(
|
||||||
env: &Env<'a, 'ctx, 'env>,
|
env: &Env<'a, 'ctx, 'env>,
|
||||||
arg: FloatValue<'ctx>,
|
arg: FloatValue<'ctx>,
|
||||||
|
@ -6641,19 +6609,19 @@ fn build_float_unary_op<'a, 'ctx, 'env>(
|
||||||
// TODO: Handle different sized floats
|
// TODO: Handle different sized floats
|
||||||
match op {
|
match op {
|
||||||
NumNeg => bd.build_float_neg(arg, "negate_float").into(),
|
NumNeg => bd.build_float_neg(arg, "negate_float").into(),
|
||||||
NumAbs => call_float_intrinsic(env, LLVM_FABS, &[arg.into()], float_width),
|
NumAbs => env.call_intrinsic(&LLVM_FABS[float_width], &[arg.into()]),
|
||||||
NumSqrtUnchecked => env.call_intrinsic(LLVM_SQRT_F64, &[arg.into()]),
|
NumSqrtUnchecked => env.call_intrinsic(&LLVM_SQRT[float_width], &[arg.into()]),
|
||||||
NumLogUnchecked => env.call_intrinsic(LLVM_LOG_F64, &[arg.into()]),
|
NumLogUnchecked => env.call_intrinsic(&LLVM_LOG[float_width], &[arg.into()]),
|
||||||
NumToFloat => arg.into(), /* Converting from Float to Float is a no-op */
|
NumToFloat => arg.into(), /* Converting from Float to Float is a no-op */
|
||||||
NumCeiling => env.builder.build_cast(
|
NumCeiling => env.builder.build_cast(
|
||||||
InstructionOpcode::FPToSI,
|
InstructionOpcode::FPToSI,
|
||||||
env.call_intrinsic(LLVM_CEILING_F64, &[arg.into()]),
|
env.call_intrinsic(&LLVM_CEILING[float_width], &[arg.into()]),
|
||||||
env.context.i64_type(),
|
env.context.i64_type(),
|
||||||
"num_ceiling",
|
"num_ceiling",
|
||||||
),
|
),
|
||||||
NumFloor => env.builder.build_cast(
|
NumFloor => env.builder.build_cast(
|
||||||
InstructionOpcode::FPToSI,
|
InstructionOpcode::FPToSI,
|
||||||
env.call_intrinsic(LLVM_FLOOR_F64, &[arg.into()]),
|
env.call_intrinsic(&LLVM_FLOOR[float_width], &[arg.into()]),
|
||||||
env.context.i64_type(),
|
env.context.i64_type(),
|
||||||
"num_floor",
|
"num_floor",
|
||||||
),
|
),
|
||||||
|
@ -6664,8 +6632,8 @@ fn build_float_unary_op<'a, 'ctx, 'env>(
|
||||||
NumRound => call_bitcode_float_fn(env, bitcode::NUM_ROUND, &[arg.into()], float_width),
|
NumRound => call_bitcode_float_fn(env, bitcode::NUM_ROUND, &[arg.into()], float_width),
|
||||||
|
|
||||||
// trigonometry
|
// trigonometry
|
||||||
NumSin => env.call_intrinsic(LLVM_SIN_F64, &[arg.into()]),
|
NumSin => env.call_intrinsic(&LLVM_SIN[float_width], &[arg.into()]),
|
||||||
NumCos => env.call_intrinsic(LLVM_COS_F64, &[arg.into()]),
|
NumCos => env.call_intrinsic(&LLVM_COS[float_width], &[arg.into()]),
|
||||||
|
|
||||||
NumAtan => call_bitcode_float_fn(env, bitcode::NUM_ATAN, &[arg.into()], float_width),
|
NumAtan => call_bitcode_float_fn(env, bitcode::NUM_ATAN, &[arg.into()], float_width),
|
||||||
NumAcos => call_bitcode_float_fn(env, bitcode::NUM_ACOS, &[arg.into()], float_width),
|
NumAcos => call_bitcode_float_fn(env, bitcode::NUM_ACOS, &[arg.into()], float_width),
|
||||||
|
@ -6725,7 +6693,6 @@ pub fn call_float_intrinsic<'a, 'ctx, 'env>(
|
||||||
env.call_intrinsic(&format!("{}.{}", fn_name, suffix), args)
|
env.call_intrinsic(&format!("{}.{}", fn_name, suffix), args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn define_global_str_literal_ptr<'a, 'ctx, 'env>(
|
fn define_global_str_literal_ptr<'a, 'ctx, 'env>(
|
||||||
env: &Env<'a, 'ctx, 'env>,
|
env: &Env<'a, 'ctx, 'env>,
|
||||||
message: &str,
|
message: &str,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue