summaryrefslogtreecommitdiffstats
path: root/llvm/lib/MC/MCInstPrinter.cpp
diff options
context:
space:
mode:
authorJonas Devlieghere <jonas@devlieghere.com>2019-09-06 01:13:32 +0000
committerJonas Devlieghere <jonas@devlieghere.com>2019-09-06 01:13:32 +0000
commitbee0f7ddd70120a05605682487bb34f0a074167b (patch)
treeb277b4268be349157c6a6b946c2acb368dc234c4 /llvm/lib/MC/MCInstPrinter.cpp
parent57fcb1d7fc01741a744eae8c8950ebbe308f8f44 (diff)
downloadbcm5719-llvm-bee0f7ddd70120a05605682487bb34f0a074167b.tar.gz
bcm5719-llvm-bee0f7ddd70120a05605682487bb34f0a074167b.zip
[MC] Fix undefined behavior in MCInstPrinter::formatHex
Passing INT64_MIN to MCInstPrinter::formatHex triggers undefined behavior because the negation of -9223372036854775808 cannot be represented in type 'int64_t' (aka 'long long'). This patch puts a workaround in place to just print the hex value directly. A possible alternative involves using a small helper functions that uses (implementation) defined conversions to achieve the desirable value: static int64_t helper(int64_t V) { auto U = static_cast<uint64_t>(V); return V < 0 ? -U : U; } The underlying problem is that MCInstPrinter::formatHex(int64_t) returns a format_object<int64_t> and should really return a format_object<uint64_t>. However, that's not possible because formatImm needs to be able to print both as decimal (where a signed is required) and hex (where we'd prefer to always have an unsigned). format_object<int64_t> formatImm(int64_t Value) const { return PrintImmHex ? formatHex(Value) : formatDec(Value); } Differential revision: https://reviews.llvm.org/D67236 llvm-svn: 371159
Diffstat (limited to 'llvm/lib/MC/MCInstPrinter.cpp')
-rw-r--r--llvm/lib/MC/MCInstPrinter.cpp25
1 files changed, 13 insertions, 12 deletions
diff --git a/llvm/lib/MC/MCInstPrinter.cpp b/llvm/lib/MC/MCInstPrinter.cpp
index c462dea3c8c..c5c06f323e6 100644
--- a/llvm/lib/MC/MCInstPrinter.cpp
+++ b/llvm/lib/MC/MCInstPrinter.cpp
@@ -83,24 +83,25 @@ format_object<int64_t> MCInstPrinter::formatDec(int64_t Value) const {
}
format_object<int64_t> MCInstPrinter::formatHex(int64_t Value) const {
- switch(PrintHexStyle) {
+ switch (PrintHexStyle) {
case HexStyle::C:
- if (Value < 0)
+ if (Value < 0) {
+ if (Value == std::numeric_limits<int64_t>::min())
+ return format<int64_t>("-0x8000000000000000", Value);
return format("-0x%" PRIx64, -Value);
- else
- return format("0x%" PRIx64, Value);
+ }
+ return format("0x%" PRIx64, Value);
case HexStyle::Asm:
if (Value < 0) {
- if (needsLeadingZero((uint64_t)(-Value)))
+ if (Value == std::numeric_limits<int64_t>::min())
+ return format<int64_t>("-8000000000000000h", Value);
+ if (needsLeadingZero(-(uint64_t)(Value)))
return format("-0%" PRIx64 "h", -Value);
- else
- return format("-%" PRIx64 "h", -Value);
- } else {
- if (needsLeadingZero((uint64_t)(Value)))
- return format("0%" PRIx64 "h", Value);
- else
- return format("%" PRIx64 "h", Value);
+ return format("-%" PRIx64 "h", -Value);
}
+ if (needsLeadingZero((uint64_t)(Value)))
+ return format("0%" PRIx64 "h", Value);
+ return format("%" PRIx64 "h", Value);
}
llvm_unreachable("unsupported print style");
}
OpenPOWER on IntegriCloud