Home | History | Annotate | Line # | Download | only in Utils
      1 //===-- WebAssemblyTypeUtilities - WebAssembly Type Utilities---*- 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 /// \file
     10 /// This file contains the declaration of the WebAssembly-specific type parsing
     11 /// utility functions.
     12 ///
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_UTILS_WEBASSEMBLYTYPEUTILITIES_H
     16 #define LLVM_LIB_TARGET_WEBASSEMBLY_UTILS_WEBASSEMBLYTYPEUTILITIES_H
     17 
     18 #include "llvm/ADT/Optional.h"
     19 #include "llvm/BinaryFormat/Wasm.h"
     20 #include "llvm/Support/MachineValueType.h"
     21 
     22 namespace llvm {
     23 namespace WebAssembly {
     24 
     25 /// Used as immediate MachineOperands for block signatures
     26 enum class BlockType : unsigned {
     27   Invalid = 0x00,
     28   Void = 0x40,
     29   I32 = unsigned(wasm::ValType::I32),
     30   I64 = unsigned(wasm::ValType::I64),
     31   F32 = unsigned(wasm::ValType::F32),
     32   F64 = unsigned(wasm::ValType::F64),
     33   V128 = unsigned(wasm::ValType::V128),
     34   Externref = unsigned(wasm::ValType::EXTERNREF),
     35   Funcref = unsigned(wasm::ValType::FUNCREF),
     36   // Multivalue blocks (and other non-void blocks) are only emitted when the
     37   // blocks will never be exited and are at the ends of functions (see
     38   // WebAssemblyCFGStackify::fixEndsAtEndOfFunction). They also are never made
     39   // to pop values off the stack, so the exact multivalue signature can always
     40   // be inferred from the return type of the parent function in MCInstLower.
     41   Multivalue = 0xffff,
     42 };
     43 
     44 /// Used as immediate MachineOperands for heap types, e.g. for ref.null.
     45 enum class HeapType : unsigned {
     46   Invalid = 0x00,
     47   Externref = unsigned(wasm::ValType::EXTERNREF),
     48   Funcref = unsigned(wasm::ValType::FUNCREF),
     49 };
     50 
     51 // Convert StringRef to ValType / HealType / BlockType
     52 
     53 Optional<wasm::ValType> parseType(StringRef Type);
     54 HeapType parseHeapType(StringRef Type);
     55 BlockType parseBlockType(StringRef Type);
     56 MVT parseMVT(StringRef Type);
     57 
     58 // Convert ValType or a list/signature of ValTypes to a string.
     59 
     60 // Convert an unsinged integer, which can be among wasm::ValType enum, to its
     61 // type name string. If the input is not within wasm::ValType, returns
     62 // "invalid_type".
     63 const char *anyTypeToString(unsigned Type);
     64 const char *typeToString(wasm::ValType Type);
     65 // Convert a list of ValTypes into a string in the format of
     66 // "type0, type1, ... typeN"
     67 std::string typeListToString(ArrayRef<wasm::ValType> List);
     68 // Convert a wasm signature into a string in the format of
     69 // "(params) -> (results)", where params and results are a string of ValType
     70 // lists.
     71 std::string signatureToString(const wasm::WasmSignature *Sig);
     72 
     73 // Convert a MVT into its corresponding wasm ValType.
     74 wasm::ValType toValType(MVT Type);
     75 
     76 } // end namespace WebAssembly
     77 } // end namespace llvm
     78 
     79 #endif
     80