| 1 | //===-- WebAssemblyTypeUtilities.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 "WebAssemblyTypeUtilities.h" |
| 15 | #include "llvm/ADT/StringSwitch.h" |
| 16 | |
| 17 | // Get register classes enum. |
| 18 | #define GET_REGINFO_ENUM |
| 19 | #include "WebAssemblyGenRegisterInfo.inc" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | MVT WebAssembly::parseMVT(StringRef Type) { |
| 24 | return StringSwitch<MVT>(Type) |
| 25 | .Case(S: "i32" , Value: MVT::i32) |
| 26 | .Case(S: "i64" , Value: MVT::i64) |
| 27 | .Case(S: "f32" , Value: MVT::f32) |
| 28 | .Case(S: "f64" , Value: MVT::f64) |
| 29 | .Case(S: "i64" , Value: MVT::i64) |
| 30 | .Case(S: "v16i8" , Value: MVT::v16i8) |
| 31 | .Case(S: "v8i16" , Value: MVT::v8i16) |
| 32 | .Case(S: "v4i32" , Value: MVT::v4i32) |
| 33 | .Case(S: "v2i64" , Value: MVT::v2i64) |
| 34 | .Case(S: "funcref" , Value: MVT::funcref) |
| 35 | .Case(S: "externref" , Value: MVT::externref) |
| 36 | .Case(S: "exnref" , Value: MVT::exnref) |
| 37 | .Default(Value: MVT::INVALID_SIMPLE_VALUE_TYPE); |
| 38 | } |
| 39 | |
| 40 | wasm::ValType WebAssembly::toValType(MVT Type) { |
| 41 | switch (Type.SimpleTy) { |
| 42 | case MVT::i32: |
| 43 | return wasm::ValType::I32; |
| 44 | case MVT::i64: |
| 45 | return wasm::ValType::I64; |
| 46 | case MVT::f32: |
| 47 | return wasm::ValType::F32; |
| 48 | case MVT::f64: |
| 49 | return wasm::ValType::F64; |
| 50 | case MVT::v16i8: |
| 51 | case MVT::v8i16: |
| 52 | case MVT::v4i32: |
| 53 | case MVT::v2i64: |
| 54 | case MVT::v8f16: |
| 55 | case MVT::v4f32: |
| 56 | case MVT::v2f64: |
| 57 | return wasm::ValType::V128; |
| 58 | case MVT::funcref: |
| 59 | return wasm::ValType::FUNCREF; |
| 60 | case MVT::externref: |
| 61 | return wasm::ValType::EXTERNREF; |
| 62 | case MVT::exnref: |
| 63 | return wasm::ValType::EXNREF; |
| 64 | default: |
| 65 | llvm_unreachable("unexpected type" ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void WebAssembly::wasmSymbolSetType(MCSymbolWasm *Sym, const Type *GlobalVT, |
| 70 | ArrayRef<MVT> VTs) { |
| 71 | assert(!Sym->getType()); |
| 72 | |
| 73 | // Tables are represented as Arrays in LLVM IR therefore |
| 74 | // they reach this point as aggregate Array types with an element type |
| 75 | // that is a reference type. |
| 76 | wasm::ValType ValTy; |
| 77 | bool IsTable = false; |
| 78 | if (WebAssembly::isWebAssemblyTableType(Ty: GlobalVT)) { |
| 79 | IsTable = true; |
| 80 | const Type *ElTy = GlobalVT->getArrayElementType(); |
| 81 | if (WebAssembly::isWebAssemblyExternrefType(Ty: ElTy)) |
| 82 | ValTy = wasm::ValType::EXTERNREF; |
| 83 | else if (WebAssembly::isWebAssemblyFuncrefType(Ty: ElTy)) |
| 84 | ValTy = wasm::ValType::FUNCREF; |
| 85 | else |
| 86 | report_fatal_error(reason: "unhandled reference type" ); |
| 87 | } else if (VTs.size() == 1) { |
| 88 | ValTy = WebAssembly::toValType(Type: VTs[0]); |
| 89 | } else |
| 90 | report_fatal_error(reason: "Aggregate globals not yet implemented" ); |
| 91 | |
| 92 | if (IsTable) { |
| 93 | Sym->setType(wasm::WASM_SYMBOL_TYPE_TABLE); |
| 94 | Sym->setTableType(VT: ValTy); |
| 95 | } else { |
| 96 | Sym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); |
| 97 | Sym->setGlobalType(wasm::WasmGlobalType{.Type: uint8_t(ValTy), /*Mutable=*/true}); |
| 98 | } |
| 99 | } |
| 100 | |