| 1 | //===- WebAssemblyMCTypeUtilities.cpp - WebAssembly Type Utility Functions-===// |
| 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 | /// \file |
| 10 | /// This file implements several utility functions for WebAssembly type parsing. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "WebAssemblyMCTypeUtilities.h" |
| 15 | #include "WebAssemblyMCTargetDesc.h" |
| 16 | #include "llvm/ADT/StringSwitch.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | std::optional<wasm::ValType> WebAssembly::parseType(StringRef Type) { |
| 21 | return llvm::StringSwitch<std::optional<wasm::ValType>>{Type} |
| 22 | .Case(S: "i32" , Value: wasm::ValType::I32) |
| 23 | .Case(S: "i64" , Value: wasm::ValType::I64) |
| 24 | .Case(S: "f32" , Value: wasm::ValType::F32) |
| 25 | .Case(S: "f64" , Value: wasm::ValType::F64) |
| 26 | .Cases(CaseStrings: {"v128" , "i8x16" , "i16x8" , "i32x4" , "i64x2" , "f32x4" , "f64x2" }, |
| 27 | Value: wasm::ValType::V128) |
| 28 | .Case(S: "funcref" , Value: wasm::ValType::FUNCREF) |
| 29 | .Case(S: "externref" , Value: wasm::ValType::EXTERNREF) |
| 30 | .Case(S: "exnref" , Value: wasm::ValType::EXNREF) |
| 31 | .Default(Value: std::nullopt); |
| 32 | } |
| 33 | |
| 34 | WebAssembly::BlockType WebAssembly::parseBlockType(StringRef Type) { |
| 35 | // Multivalue block types are handled separately in parseSignature |
| 36 | return StringSwitch<WebAssembly::BlockType>(Type) |
| 37 | .Case(S: "i32" , Value: WebAssembly::BlockType::I32) |
| 38 | .Case(S: "i64" , Value: WebAssembly::BlockType::I64) |
| 39 | .Case(S: "f32" , Value: WebAssembly::BlockType::F32) |
| 40 | .Case(S: "f64" , Value: WebAssembly::BlockType::F64) |
| 41 | .Case(S: "v128" , Value: WebAssembly::BlockType::V128) |
| 42 | .Case(S: "funcref" , Value: WebAssembly::BlockType::Funcref) |
| 43 | .Case(S: "externref" , Value: WebAssembly::BlockType::Externref) |
| 44 | .Case(S: "exnref" , Value: WebAssembly::BlockType::Exnref) |
| 45 | .Case(S: "void" , Value: WebAssembly::BlockType::Void) |
| 46 | .Default(Value: WebAssembly::BlockType::Invalid); |
| 47 | } |
| 48 | |
| 49 | // We have various enums representing a subset of these types, use this |
| 50 | // function to convert any of them to text. |
| 51 | const char *WebAssembly::anyTypeToString(unsigned Type) { |
| 52 | switch (Type) { |
| 53 | case wasm::WASM_TYPE_I32: |
| 54 | return "i32" ; |
| 55 | case wasm::WASM_TYPE_I64: |
| 56 | return "i64" ; |
| 57 | case wasm::WASM_TYPE_F32: |
| 58 | return "f32" ; |
| 59 | case wasm::WASM_TYPE_F64: |
| 60 | return "f64" ; |
| 61 | case wasm::WASM_TYPE_V128: |
| 62 | return "v128" ; |
| 63 | case wasm::WASM_TYPE_FUNCREF: |
| 64 | return "funcref" ; |
| 65 | case wasm::WASM_TYPE_EXTERNREF: |
| 66 | return "externref" ; |
| 67 | case wasm::WASM_TYPE_EXNREF: |
| 68 | return "exnref" ; |
| 69 | case wasm::WASM_TYPE_FUNC: |
| 70 | return "func" ; |
| 71 | case wasm::WASM_TYPE_NORESULT: |
| 72 | return "void" ; |
| 73 | default: |
| 74 | return "invalid_type" ; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | const char *WebAssembly::typeToString(wasm::ValType Type) { |
| 79 | return anyTypeToString(Type: static_cast<unsigned>(Type)); |
| 80 | } |
| 81 | |
| 82 | std::string WebAssembly::typeListToString(ArrayRef<wasm::ValType> List) { |
| 83 | std::string S; |
| 84 | for (const auto &Type : List) { |
| 85 | if (&Type != &List[0]) |
| 86 | S += ", " ; |
| 87 | S += WebAssembly::typeToString(Type); |
| 88 | } |
| 89 | return S; |
| 90 | } |
| 91 | |
| 92 | std::string WebAssembly::signatureToString(const wasm::WasmSignature *Sig) { |
| 93 | std::string S("(" ); |
| 94 | S += typeListToString(List: Sig->Params); |
| 95 | S += ") -> (" ; |
| 96 | S += typeListToString(List: Sig->Returns); |
| 97 | S += ")" ; |
| 98 | return S; |
| 99 | } |
| 100 | |
| 101 | wasm::ValType WebAssembly::regClassToValType(unsigned RC) { |
| 102 | switch (RC) { |
| 103 | case WebAssembly::I32RegClassID: |
| 104 | return wasm::ValType::I32; |
| 105 | case WebAssembly::I64RegClassID: |
| 106 | return wasm::ValType::I64; |
| 107 | case WebAssembly::F32RegClassID: |
| 108 | return wasm::ValType::F32; |
| 109 | case WebAssembly::F64RegClassID: |
| 110 | return wasm::ValType::F64; |
| 111 | case WebAssembly::V128RegClassID: |
| 112 | return wasm::ValType::V128; |
| 113 | case WebAssembly::FUNCREFRegClassID: |
| 114 | return wasm::ValType::FUNCREF; |
| 115 | case WebAssembly::EXTERNREFRegClassID: |
| 116 | return wasm::ValType::EXTERNREF; |
| 117 | case WebAssembly::EXNREFRegClassID: |
| 118 | return wasm::ValType::EXNREF; |
| 119 | default: |
| 120 | llvm_unreachable("unexpected type" ); |
| 121 | } |
| 122 | } |
| 123 | |