aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-04-05 22:17:06 +0100
committerMichael Williamson <mike@zwobble.org>2026-04-05 22:17:06 +0100
commitfbd61014d3fcf520359a2b9906cdaacc801c2d5c (patch)
tree7116872a408249b5ac4caf7d3e1798d9858fa7b7
parentb6e159ded1db01f2162cc22457e9e46c4b8643a7 (diff)
Add tests for different int values
-rw-r--r--patio-to-cranelift/src/codegen/values.rs39
1 files changed, 38 insertions, 1 deletions
diff --git a/patio-to-cranelift/src/codegen/values.rs b/patio-to-cranelift/src/codegen/values.rs
index 39f8d40c..ac2c5682 100644
--- a/patio-to-cranelift/src/codegen/values.rs
+++ b/patio-to-cranelift/src/codegen/values.rs
@@ -34,11 +34,13 @@ fn write_data_value(
PatioDataValue::Byte(value) => bytes.push(*value),
PatioDataValue::DataPtr(_) => todo!(),
+
PatioDataValue::Int(int_value) => {
// TODO: handle 32-bit int
let value: i64 = eval_int_value(int_value, target_triple).into();
bytes.extend_from_slice(&value.to_le_bytes());
}
+
PatioDataValue::Int32(int_value) => {
let value: i32 = eval_int_value(int_value, target_triple);
bytes.extend_from_slice(&value.to_le_bytes());
@@ -73,7 +75,10 @@ pub(crate) fn eval_int_value(value: &PatioIntValue, target_triple: &target_lexic
mod data_value_to_bytes_test {
use std::str::FromStr;
- use crate::patio::values::{PatioDataValue, PatioIntValue};
+ use crate::patio::{
+ types::PatioDataLayout,
+ values::{PatioDataValue, PatioIntValue},
+ };
fn data_value_to_bytes(data_value: PatioDataValue) -> Box<[u8]> {
super::data_value_to_bytes(
@@ -126,4 +131,36 @@ mod data_value_to_bytes_test {
assert_eq!(bytes.as_ref(), &[0x78, 0x56, 0x34, 0x12]);
}
+
+ #[test]
+ fn int_value_literal_is_used_verbatim() {
+ let data_value = PatioDataValue::Int32(PatioIntValue::Literal { value: 0x12345678 });
+
+ let bytes = data_value_to_bytes(data_value);
+
+ assert_eq!(bytes.as_ref(), &[0x78, 0x56, 0x34, 0x12]);
+ }
+
+ #[test]
+ fn int_can_have_size_of_layout() {
+ let data_value = PatioDataValue::Int32(PatioIntValue::ByteSizeOf {
+ layout: PatioDataLayout::Int32,
+ });
+
+ let bytes = data_value_to_bytes(data_value);
+
+ assert_eq!(bytes.as_ref(), &[4, 0, 0, 0]);
+ }
+
+ #[test]
+ fn int_can_have_byte_offset() {
+ let data_value = PatioDataValue::Int32(PatioIntValue::ByteOffsetOf {
+ layout: PatioDataLayout::array(PatioDataLayout::Int32, 7),
+ element_offsets: vec![5],
+ });
+
+ let bytes = data_value_to_bytes(data_value);
+
+ assert_eq!(bytes.as_ref(), &[20, 0, 0, 0]);
+ }
}