
This adds an llvm intrinsic for WebAssembly to test the type of a function. It is intended for adding a future clang builtin ` __builtin_wasm_test_function_pointer_signature` so we can test whether calling a function pointer will fail with function signature mismatch. Since the type of a function pointer is just `ptr` we can't figure out the expected type from that. The way I figured out to encode the type was by passing 0's of the appropriate type to the intrinsic. The first argument gives the expected type of the return type and the later values give the expected type of the arguments. So ```llvm @llvm.wasm.ref.test.func(ptr %func, float 0.000000e+00, double 0.000000e+00, i32 0) ``` tests if `%func` is of type `(double, i32) -> (i32)`. It will lower to: ```wat local.get $func table.get $__indirect_function_table ref.test (double, i32) -> (i32) ``` To indicate the function should be void, I somewhat arbitrarily picked `token poison`, so the following tests for `(i32) -> ()`: ```llvm @llvm.wasm.ref.test.func(ptr %func, token poison, i32 0) ``` To lower this intrinsic, we need some place to put the type information. With `encodeFunctionSignature()` we encode the signature information into an `APInt`. We decode it in `lowerEncodedFunctionSignature` in `WebAssemblyMCInstLower.cpp`.
49 lines
1.7 KiB
C++
49 lines
1.7 KiB
C++
//===-- WebAssemblyMCInstLower.h - Lower MachineInstr to MCInst -*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
///
|
|
/// \file
|
|
/// This file declares the class to lower WebAssembly MachineInstrs to
|
|
/// their corresponding MCInst records.
|
|
///
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYMCINSTLOWER_H
|
|
#define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYMCINSTLOWER_H
|
|
|
|
#include "llvm/BinaryFormat/Wasm.h"
|
|
#include "llvm/MC/MCInst.h"
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
namespace llvm {
|
|
class WebAssemblyAsmPrinter;
|
|
class MCContext;
|
|
class MCSymbol;
|
|
class MachineInstr;
|
|
class MachineOperand;
|
|
|
|
/// This class is used to lower an MachineInstr into an MCInst.
|
|
class LLVM_LIBRARY_VISIBILITY WebAssemblyMCInstLower {
|
|
MCContext &Ctx;
|
|
WebAssemblyAsmPrinter &Printer;
|
|
|
|
MCSymbol *GetGlobalAddressSymbol(const MachineOperand &MO) const;
|
|
MCSymbol *GetExternalSymbolSymbol(const MachineOperand &MO) const;
|
|
MCOperand lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const;
|
|
MCOperand lowerTypeIndexOperand(SmallVectorImpl<wasm::ValType> &&,
|
|
SmallVectorImpl<wasm::ValType> &&) const;
|
|
MCOperand lowerEncodedFunctionSignature(const APInt &Sig) const;
|
|
|
|
public:
|
|
WebAssemblyMCInstLower(MCContext &ctx, WebAssemblyAsmPrinter &printer)
|
|
: Ctx(ctx), Printer(printer) {}
|
|
void lower(const MachineInstr *MI, MCInst &OutMI) const;
|
|
};
|
|
} // end namespace llvm
|
|
|
|
#endif
|