This adds the basic assembly generation support for the final EH proposal, which was newly adopted in Sep 2023 and advanced into Phase 4 in Jul 2024: https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md This adds support for the generation of new `try_table` and `throw_ref` instruction in .s asesmbly format. This does NOT yet include - Block annotation comment generation for .s format - .o object file generation - .s assembly parsing - Type checking (AsmTypeCheck) - Disassembler - Fixing unwind mismatches in CFGStackify These will be added as follow-up PRs. --- The format for `TRY_TABLE`, both for `MachineInstr` and `MCInst`, is as follows: ``` TRY_TABLE type number_of_catches catch_clauses* ``` where `catch_clause` is ``` catch_opcode tag+ destination ``` `catch_opcode` should be one of 0/1/2/3, which denotes `CATCH`/`CATCH_REF`/`CATCH_ALL`/`CATCH_ALL_REF` respectively. (See `BinaryFormat/Wasm.h`) `tag` exists when the catch is one of `CATCH` or `CATCH_REF`. The MIR format is printed as just the list of raw operands. The (stack-based) assembly instruction supports pretty-printing, including printing `catch` clauses by name, in InstPrinter. In addition to the new instructions `TRY_TABLE` and `THROW_REF`, this adds four pseudo instructions: `CATCH`, `CATCH_REF`, `CATCH_ALL`, and `CATCH_ALL_REF`. These are pseudo instructions to simulate block return values of `catch`, `catch_ref`, `catch_all`, `catch_all_ref` clauses in `try_table` respectively, given that we don't support block return values except for one case (`fixEndsAtEndOfFunction` in CFGStackify). These will be omitted when we lower the instructions to `MCInst` at the end. LateEHPrepare now will have one more stage to covert `CATCH`/`CATCH_ALL`s to `CATCH_REF`/`CATCH_ALL_REF`s when there is a `RETHROW` to rethrow its exception. The pass also converts `RETHROW`s into `THROW_REF`. Note that we still use `RETHROW` as an interim pseudo instruction until we convert them to `THROW_REF` in LateEHPrepare. CFGStackify has a new `placeTryTableMarker` function, which places `try_table`/`end_try_table` markers with a necessary `catch` clause and also `block`/`end_block` markers for the destination of the `catch` clause. In MCInstLower, now we need to support one more case for the multivalue block signature (`catch_ref`'s destination's `(i32, exnref)` return type). InstPrinter has a new routine to print the `catch_list` type, which is used to print `try_table` instructions. The new test, `exception.ll`'s source is the same as `exception-legacy.ll`, with the FileCheck expectations changed. One difference is the commands in this file have `-wasm-enable-exnref` to test the new format, and don't have `-wasm-disable-explicit-locals -wasm-keep-registers`, because the new custom InstPrinter routine to print `catch_list` only works for the stack-based instructions (`_S`), and we can't use `-wasm-keep-registers` for them. As in `exception-legacy.ll`, the FileCheck lines for the new tests do not contain the whole program; they mostly contain only the control flow instructions for readability.
61 lines
2.3 KiB
C++
61 lines
2.3 KiB
C++
// WebAssemblyInstPrinter.h - Print wasm MCInst to assembly syntax -*- 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 class prints an WebAssembly MCInst to wasm file syntax.
|
|
///
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_INSTPRINTER_WEBASSEMBLYINSTPRINTER_H
|
|
#define LLVM_LIB_TARGET_WEBASSEMBLY_INSTPRINTER_WEBASSEMBLYINSTPRINTER_H
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
#include "llvm/BinaryFormat/Wasm.h"
|
|
#include "llvm/CodeGenTypes/MachineValueType.h"
|
|
#include "llvm/MC/MCInstPrinter.h"
|
|
|
|
namespace llvm {
|
|
|
|
class MCSubtargetInfo;
|
|
|
|
class WebAssemblyInstPrinter final : public MCInstPrinter {
|
|
uint64_t ControlFlowCounter = 0;
|
|
SmallVector<std::pair<uint64_t, bool>, 4> ControlFlowStack;
|
|
SmallVector<uint64_t, 4> TryStack;
|
|
|
|
enum EHInstKind { TRY, CATCH_LEGACY, CATCH_ALL_LEGACY };
|
|
SmallVector<EHInstKind, 4> EHInstStack;
|
|
|
|
public:
|
|
WebAssemblyInstPrinter(const MCAsmInfo &MAI, const MCInstrInfo &MII,
|
|
const MCRegisterInfo &MRI);
|
|
|
|
void printRegName(raw_ostream &OS, MCRegister Reg) const override;
|
|
void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
|
|
const MCSubtargetInfo &STI, raw_ostream &OS) override;
|
|
|
|
// Used by tblegen code.
|
|
void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
|
|
bool IsVariadicDef = false);
|
|
void printBrList(const MCInst *MI, unsigned OpNo, raw_ostream &O);
|
|
void printWebAssemblyP2AlignOperand(const MCInst *MI, unsigned OpNo,
|
|
raw_ostream &O);
|
|
void printWebAssemblySignatureOperand(const MCInst *MI, unsigned OpNo,
|
|
raw_ostream &O);
|
|
void printCatchList(const MCInst *MI, unsigned OpNo, raw_ostream &O);
|
|
|
|
// Autogenerated by tblgen.
|
|
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
|
|
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
|
|
static const char *getRegisterName(MCRegister Reg);
|
|
};
|
|
|
|
} // end namespace llvm
|
|
|
|
#endif
|