diff options
author | John Hodge <tpg@ucc.asn.au> | 2018-07-01 11:47:16 +0800 |
---|---|---|
committer | John Hodge <tpg@ucc.asn.au> | 2018-07-01 11:47:16 +0800 |
commit | 5d16c2a8498d880f200f0602f63753b144b5cb6b (patch) | |
tree | 707cd9925c2c2629b9aa5e67b82d697cf3b55859 | |
parent | 87ec3ea65bc42214c4e78c991ec2033fc103bf63 (diff) | |
download | mrust-5d16c2a8498d880f200f0602f63753b144b5cb6b.tar.gz |
HIR/MIR - Truncate usize constants to 32-bits on 32-bit machines
-rw-r--r-- | src/hir_conv/constant_evaluation.cpp | 8 | ||||
-rw-r--r-- | src/mir/cleanup.cpp | 23 |
2 files changed, 30 insertions, 1 deletions
diff --git a/src/hir_conv/constant_evaluation.cpp b/src/hir_conv/constant_evaluation.cpp index 82d4ab0e..5a41e954 100644 --- a/src/hir_conv/constant_evaluation.cpp +++ b/src/hir_conv/constant_evaluation.cpp @@ -13,6 +13,7 @@ #include <mir/mir.hpp> #include <hir_typeck/common.hpp> // Monomorph #include <mir/helpers.hpp> +#include <trans/target.hpp> namespace { typedef ::std::vector< ::std::pair< ::std::string, ::HIR::Static> > t_new_values; @@ -1532,6 +1533,13 @@ namespace { case ::HIR::CoreType::I8: lit.as_Integer() &= (1ull<<8)-1; break; case ::HIR::CoreType::I16: lit.as_Integer() &= (1ull<<16)-1; break; case ::HIR::CoreType::I32: lit.as_Integer() &= (1ull<<32)-1; break; + + case ::HIR::CoreType::Usize: + case ::HIR::CoreType::Isize: + if( Target_GetCurSpec().m_arch.m_pointer_bits == 32 ) + lit.as_Integer() &= (1ull<<32)-1; + break; + default: break; } diff --git a/src/mir/cleanup.cpp b/src/mir/cleanup.cpp index 2d4b3468..78939f78 100644 --- a/src/mir/cleanup.cpp +++ b/src/mir/cleanup.cpp @@ -16,6 +16,7 @@ #include <mir/helpers.hpp> #include <mir/operations.hpp> #include <mir/visit_crate_mir.hpp> +#include <trans/target.hpp> struct MirMutator { @@ -911,6 +912,22 @@ void MIR_Cleanup_LValue(const ::MIR::TypeResolve& state, MirMutator& mutator, :: } } } +void MIR_Cleanup_Constant(const ::MIR::TypeResolve& state, MirMutator& mutator, ::MIR::Constant& p) +{ + if( auto* e = p.opt_Uint() ) + { + switch(e->t) + { + // HACK: Restrict Usize to 32-bits when needed + case ::HIR::CoreType::Usize: + if( Target_GetCurSpec().m_arch.m_pointer_bits == 32 ) + e->v &= 0xFFFFFFFF; + break; + default: + break; + } + } +} void MIR_Cleanup_Param(const ::MIR::TypeResolve& state, MirMutator& mutator, ::MIR::Param& p) { TU_MATCHA( (p), (e), @@ -918,7 +935,7 @@ void MIR_Cleanup_Param(const ::MIR::TypeResolve& state, MirMutator& mutator, ::M MIR_Cleanup_LValue(state, mutator, e); ), (Constant, - // NOTE: No cleanup + MIR_Cleanup_Constant(state, mutator, e); ) ) } @@ -973,6 +990,7 @@ void MIR_Cleanup(const StaticTraitResolve& resolve, const ::HIR::ItemPath& path, MIR_Cleanup_LValue(state, mutator, re); ), (Constant, + MIR_Cleanup_Constant(state, mutator, re); ), (SizedArray, MIR_Cleanup_Param(state, mutator, re.val); @@ -1057,6 +1075,9 @@ void MIR_Cleanup(const StaticTraitResolve& resolve, const ::HIR::ItemPath& path, { DEBUG("Replace constant " << ce.p << " with " << *lit_ptr); se.src = MIR_Cleanup_LiteralToRValue(state, mutator, *lit_ptr, mv$(ty), mv$(ce.p)); + if( auto* p = se.src.opt_Constant() ) { + MIR_Cleanup_Constant(state, mutator, *p); + } } else { |