xref: /freebsd/contrib/llvm-project/llvm/lib/BinaryFormat/Wasm.cpp (revision b51f459a2098622c31ed54f5c1bf0e03efce403b)
1 //===-- llvm/BinaryFormat/Wasm.cpp -------------------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/BinaryFormat/Wasm.h"
10 
11 std::string llvm::wasm::toString(wasm::WasmSymbolType Type) {
12   switch (Type) {
13   case wasm::WASM_SYMBOL_TYPE_FUNCTION:
14     return "WASM_SYMBOL_TYPE_FUNCTION";
15   case wasm::WASM_SYMBOL_TYPE_GLOBAL:
16     return "WASM_SYMBOL_TYPE_GLOBAL";
17   case wasm::WASM_SYMBOL_TYPE_DATA:
18     return "WASM_SYMBOL_TYPE_DATA";
19   case wasm::WASM_SYMBOL_TYPE_SECTION:
20     return "WASM_SYMBOL_TYPE_SECTION";
21   case wasm::WASM_SYMBOL_TYPE_EVENT:
22     return "WASM_SYMBOL_TYPE_EVENT";
23   }
24   llvm_unreachable("unknown symbol type");
25 }
26 
27 std::string llvm::wasm::relocTypetoString(uint32_t Type) {
28   switch (Type) {
29 #define WASM_RELOC(NAME, VALUE)                                                \
30   case VALUE:                                                                  \
31     return #NAME;
32 #include "llvm/BinaryFormat/WasmRelocs.def"
33 #undef WASM_RELOC
34   default:
35     llvm_unreachable("unknown reloc type");
36   }
37 }
38 
39 bool llvm::wasm::relocTypeHasAddend(uint32_t Type) {
40   switch (Type) {
41   case R_WASM_MEMORY_ADDR_LEB:
42   case R_WASM_MEMORY_ADDR_LEB64:
43   case R_WASM_MEMORY_ADDR_SLEB:
44   case R_WASM_MEMORY_ADDR_SLEB64:
45   case R_WASM_MEMORY_ADDR_REL_SLEB:
46   case R_WASM_MEMORY_ADDR_REL_SLEB64:
47   case R_WASM_MEMORY_ADDR_I32:
48   case R_WASM_MEMORY_ADDR_I64:
49   case R_WASM_FUNCTION_OFFSET_I32:
50   case R_WASM_SECTION_OFFSET_I32:
51     return true;
52   default:
53     return false;
54   }
55 }
56