diff options
author | Thomas Lively <tlively@google.com> | 2018-11-09 01:57:00 +0000 |
---|---|---|
committer | Thomas Lively <tlively@google.com> | 2018-11-09 01:57:00 +0000 |
commit | 2faf079494e90c1a941cf1bf51c8e7abb19bcef9 (patch) | |
tree | 7d5a9c9d963a109a9414f7a90c63b9d1c5633c2c /llvm/lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp | |
parent | 4ddd22581ee547c20222a4aba3a598803eb99a1e (diff) | |
download | bcm5719-llvm-2faf079494e90c1a941cf1bf51c8e7abb19bcef9.tar.gz bcm5719-llvm-2faf079494e90c1a941cf1bf51c8e7abb19bcef9.zip |
[WebAssembly] Read prefixed opcodes as ULEB128s
Summary: Depends on D54126.
Reviewers: aheejin, dschuff, aardappel
Subscribers: sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D54138
llvm-svn: 346465
Diffstat (limited to 'llvm/lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp')
-rw-r--r-- | llvm/lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/llvm/lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp b/llvm/lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp index 54403a56274..4ab290399ba 100644 --- a/llvm/lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp +++ b/llvm/lib/Target/WebAssembly/Disassembler/WebAssemblyDisassembler.cpp @@ -66,7 +66,7 @@ extern "C" void LLVMInitializeWebAssemblyDisassembler() { createWebAssemblyDisassembler); } -static int nextByte(ArrayRef<uint8_t> Bytes, uint64_t &Size) { +static uint8_t nextByte(ArrayRef<uint8_t> Bytes, uint64_t &Size) { if (Size >= Bytes.size()) return -1; auto V = Bytes[Size]; @@ -74,18 +74,26 @@ static int nextByte(ArrayRef<uint8_t> Bytes, uint64_t &Size) { return V; } -static bool parseLEBImmediate(MCInst &MI, uint64_t &Size, - ArrayRef<uint8_t> Bytes, bool Signed) { +static bool nextLEB(int64_t &Val, ArrayRef<uint8_t> Bytes, uint64_t &Size, + bool Signed = false) { unsigned N = 0; const char *Error = nullptr; - auto Val = Signed ? decodeSLEB128(Bytes.data() + Size, &N, - Bytes.data() + Bytes.size(), &Error) - : static_cast<int64_t>( - decodeULEB128(Bytes.data() + Size, &N, - Bytes.data() + Bytes.size(), &Error)); + Val = Signed ? decodeSLEB128(Bytes.data() + Size, &N, + Bytes.data() + Bytes.size(), &Error) + : static_cast<int64_t>(decodeULEB128(Bytes.data() + Size, &N, + Bytes.data() + Bytes.size(), + &Error)); if (Error) return false; Size += N; + return true; +} + +static bool parseLEBImmediate(MCInst &MI, uint64_t &Size, + ArrayRef<uint8_t> Bytes, bool Signed) { + int64_t Val; + if (!nextLEB(Val, Bytes, Size, Signed)) + return false; MI.addOperand(MCOperand::createImm(Val)); return true; } @@ -127,10 +135,12 @@ MCDisassembler::DecodeStatus WebAssemblyDisassembler::getInstruction( } if (!WasmInst) return MCDisassembler::Fail; - Opc = nextByte(Bytes, Size); - if (Opc < 0) + int64_t PrefixedOpc; + if (!nextLEB(PrefixedOpc, Bytes, Size)) + return MCDisassembler::Fail; + if (PrefixedOpc < 0 || PrefixedOpc >= WebAssemblyInstructionTableSize) return MCDisassembler::Fail; - WasmInst += Opc; + WasmInst += PrefixedOpc; } if (WasmInst->ET == ET_Unused) return MCDisassembler::Fail; |