Add support for casting.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::ir::{Expression, Primitive, Program, Statement, Value, ValueOrRef};
|
||||
use crate::eval::PrimitiveType;
|
||||
use crate::ir::{Expression, Primitive, Program, Statement, Type, Value, ValueOrRef};
|
||||
use crate::syntax::ConstantType;
|
||||
use cranelift_codegen::entity::EntityRef;
|
||||
use cranelift_codegen::ir::{
|
||||
@@ -307,6 +308,70 @@ impl Expression {
|
||||
Err(BackendError::VariableLookupFailure(name))
|
||||
}
|
||||
|
||||
Expression::Cast(_, target_type, expr) => {
|
||||
let (val, val_type) =
|
||||
expr.into_crane(builder, local_variables, global_variables)?;
|
||||
|
||||
match (val_type, &target_type) {
|
||||
(ConstantType::I8, Type::Primitive(PrimitiveType::I8)) => Ok((val, val_type)),
|
||||
(ConstantType::I8, Type::Primitive(PrimitiveType::I16)) => {
|
||||
Ok((builder.ins().sextend(types::I16, val), ConstantType::I16))
|
||||
}
|
||||
(ConstantType::I8, Type::Primitive(PrimitiveType::I32)) => {
|
||||
Ok((builder.ins().sextend(types::I32, val), ConstantType::I32))
|
||||
}
|
||||
(ConstantType::I8, Type::Primitive(PrimitiveType::I64)) => {
|
||||
Ok((builder.ins().sextend(types::I64, val), ConstantType::I64))
|
||||
}
|
||||
|
||||
(ConstantType::I16, Type::Primitive(PrimitiveType::I16)) => Ok((val, val_type)),
|
||||
(ConstantType::I16, Type::Primitive(PrimitiveType::I32)) => {
|
||||
Ok((builder.ins().sextend(types::I32, val), ConstantType::I32))
|
||||
}
|
||||
(ConstantType::I16, Type::Primitive(PrimitiveType::I64)) => {
|
||||
Ok((builder.ins().sextend(types::I64, val), ConstantType::I64))
|
||||
}
|
||||
|
||||
(ConstantType::I32, Type::Primitive(PrimitiveType::I32)) => Ok((val, val_type)),
|
||||
(ConstantType::I32, Type::Primitive(PrimitiveType::I64)) => {
|
||||
Ok((builder.ins().sextend(types::I64, val), ConstantType::I64))
|
||||
}
|
||||
|
||||
(ConstantType::I64, Type::Primitive(PrimitiveType::I64)) => Ok((val, val_type)),
|
||||
|
||||
(ConstantType::U8, Type::Primitive(PrimitiveType::U8)) => Ok((val, val_type)),
|
||||
(ConstantType::U8, Type::Primitive(PrimitiveType::U16)) => {
|
||||
Ok((builder.ins().uextend(types::I16, val), ConstantType::U16))
|
||||
}
|
||||
(ConstantType::U8, Type::Primitive(PrimitiveType::U32)) => {
|
||||
Ok((builder.ins().uextend(types::I32, val), ConstantType::U32))
|
||||
}
|
||||
(ConstantType::U8, Type::Primitive(PrimitiveType::U64)) => {
|
||||
Ok((builder.ins().uextend(types::I64, val), ConstantType::U64))
|
||||
}
|
||||
|
||||
(ConstantType::U16, Type::Primitive(PrimitiveType::U16)) => Ok((val, val_type)),
|
||||
(ConstantType::U16, Type::Primitive(PrimitiveType::U32)) => {
|
||||
Ok((builder.ins().uextend(types::I32, val), ConstantType::U32))
|
||||
}
|
||||
(ConstantType::U16, Type::Primitive(PrimitiveType::U64)) => {
|
||||
Ok((builder.ins().uextend(types::I64, val), ConstantType::U64))
|
||||
}
|
||||
|
||||
(ConstantType::U32, Type::Primitive(PrimitiveType::U32)) => Ok((val, val_type)),
|
||||
(ConstantType::U32, Type::Primitive(PrimitiveType::U64)) => {
|
||||
Ok((builder.ins().uextend(types::I64, val), ConstantType::U64))
|
||||
}
|
||||
|
||||
(ConstantType::U64, Type::Primitive(PrimitiveType::U64)) => Ok((val, val_type)),
|
||||
|
||||
_ => Err(BackendError::InvalidTypeCast {
|
||||
from: val_type.into(),
|
||||
to: target_type,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
Expression::Primitive(_, prim, mut vals) => {
|
||||
let mut values = vec![];
|
||||
let mut first_type = None;
|
||||
|
||||
Reference in New Issue
Block a user