aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--patio-to-cranelift/src/codegen/values.rs31
1 files changed, 27 insertions, 4 deletions
diff --git a/patio-to-cranelift/src/codegen/values.rs b/patio-to-cranelift/src/codegen/values.rs
index 11983d3c..32f1c70a 100644
--- a/patio-to-cranelift/src/codegen/values.rs
+++ b/patio-to-cranelift/src/codegen/values.rs
@@ -40,9 +40,22 @@ fn write_data_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());
+ let value = eval_int_value(int_value, target_triple);
+
+ match target_triple.pointer_width() {
+ Ok(target_lexicon::PointerWidth::U16) => {
+ panic!("unsupported pointer width: 16 bits")
+ }
+ Ok(target_lexicon::PointerWidth::U32) => {
+ let value_at_width: i32 = value;
+ bytes.extend_from_slice(&value_at_width.to_le_bytes());
+ }
+ Ok(target_lexicon::PointerWidth::U64) => {
+ let value_at_width: i64 = value.into();
+ bytes.extend_from_slice(&value_at_width.to_le_bytes());
+ }
+ Err(()) => panic!("pointer width unknown"),
+ }
}
PatioDataValue::Int32(int_value) => {
@@ -137,7 +150,7 @@ mod data_value_to_bytes_test {
}
#[test]
- fn given_pointer_is_at_least_32_bits_wide_then_int_is_written_with_pointer_width() {
+ fn given_pointer_is_64_bits_wide_then_int_is_written_with_pointer_width() {
let data_value = PatioDataValue::Int(PatioIntValue::Literal { value: 0x12345678 });
let bytes = data_value_to_bytes(data_value);
@@ -146,6 +159,16 @@ mod data_value_to_bytes_test {
}
#[test]
+ fn given_pointer_is_32_bits_wide_then_int_is_written_with_pointer_width() {
+ let target_triple = target_lexicon::Triple::from_str("i686-unknown-linux-gnu").unwrap();
+ let data_value = PatioDataValue::Int(PatioIntValue::Literal { value: 0x12345678 });
+
+ let bytes = super::data_value_to_bytes(&data_value, &target_triple);
+
+ assert_eq!(bytes.as_ref(), &[0x78, 0x56, 0x34, 0x12]);
+ }
+
+ #[test]
fn int_32_is_written_as_four_bytes() {
let data_value = PatioDataValue::Int32(PatioIntValue::Literal { value: 0x12345678 });