105ac43feSDan Gohman //=- WebAssemblyMCCodeEmitter.cpp - Convert WebAssembly code to machine code -//
205ac43feSDan Gohman //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
605ac43feSDan Gohman //
705ac43feSDan Gohman //===----------------------------------------------------------------------===//
805ac43feSDan Gohman ///
905ac43feSDan Gohman /// \file
105f8f34e4SAdrian Prantl /// This file implements the WebAssemblyMCCodeEmitter class.
1105ac43feSDan Gohman ///
1205ac43feSDan Gohman //===----------------------------------------------------------------------===//
1305ac43feSDan Gohman
14d934cb88SDan Gohman #include "MCTargetDesc/WebAssemblyFixupKinds.h"
156bda14b3SChandler Carruth #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
161a427287SDan Gohman #include "llvm/ADT/STLExtras.h"
1705ac43feSDan Gohman #include "llvm/ADT/Statistic.h"
1805ac43feSDan Gohman #include "llvm/MC/MCCodeEmitter.h"
1905ac43feSDan Gohman #include "llvm/MC/MCFixup.h"
2005ac43feSDan Gohman #include "llvm/MC/MCInst.h"
2105ac43feSDan Gohman #include "llvm/MC/MCInstrInfo.h"
2205ac43feSDan Gohman #include "llvm/MC/MCRegisterInfo.h"
2305ac43feSDan Gohman #include "llvm/MC/MCSubtargetInfo.h"
2405ac43feSDan Gohman #include "llvm/MC/MCSymbol.h"
25685c5e83SSam Clegg #include "llvm/Support/Debug.h"
268f4bd1fdSReid Kleckner #include "llvm/Support/EndianStream.h"
274fc4e42dSDan Gohman #include "llvm/Support/LEB128.h"
2805ac43feSDan Gohman #include "llvm/Support/raw_ostream.h"
29685c5e83SSam Clegg
3005ac43feSDan Gohman using namespace llvm;
3105ac43feSDan Gohman
3205ac43feSDan Gohman #define DEBUG_TYPE "mccodeemitter"
3305ac43feSDan Gohman
341a427287SDan Gohman STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
351a427287SDan Gohman STATISTIC(MCNumFixups, "Number of MC fixups created.");
361a427287SDan Gohman
3705ac43feSDan Gohman namespace {
3805ac43feSDan Gohman class WebAssemblyMCCodeEmitter final : public MCCodeEmitter {
391a427287SDan Gohman const MCInstrInfo &MCII;
4005ac43feSDan Gohman
411a427287SDan Gohman // Implementation generated by tablegen.
4205ac43feSDan Gohman uint64_t getBinaryCodeForInstr(const MCInst &MI,
4305ac43feSDan Gohman SmallVectorImpl<MCFixup> &Fixups,
4405ac43feSDan Gohman const MCSubtargetInfo &STI) const;
4505ac43feSDan Gohman
4605ac43feSDan Gohman void encodeInstruction(const MCInst &MI, raw_ostream &OS,
4705ac43feSDan Gohman SmallVectorImpl<MCFixup> &Fixups,
4805ac43feSDan Gohman const MCSubtargetInfo &STI) const override;
491a427287SDan Gohman
501a427287SDan Gohman public:
WebAssemblyMCCodeEmitter(const MCInstrInfo & MCII)5118c56a07SHeejin Ahn WebAssemblyMCCodeEmitter(const MCInstrInfo &MCII) : MCII(MCII) {}
5205ac43feSDan Gohman };
5305ac43feSDan Gohman } // end anonymous namespace
5405ac43feSDan Gohman
createWebAssemblyMCCodeEmitter(const MCInstrInfo & MCII)559d24fb7fSSam Clegg MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII) {
569d24fb7fSSam Clegg return new WebAssemblyMCCodeEmitter(MCII);
5705ac43feSDan Gohman }
5805ac43feSDan Gohman
encodeInstruction(const MCInst & MI,raw_ostream & OS,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const5905ac43feSDan Gohman void WebAssemblyMCCodeEmitter::encodeInstruction(
6005ac43feSDan Gohman const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
6105ac43feSDan Gohman const MCSubtargetInfo &STI) const {
624fc4e42dSDan Gohman uint64_t Start = OS.tell();
634fc4e42dSDan Gohman
644fc4e42dSDan Gohman uint64_t Binary = getBinaryCodeForInstr(MI, Fixups, STI);
6531e94455SThomas Lively if (Binary < (1 << 8)) {
663acb187dSDan Gohman OS << uint8_t(Binary);
6731e94455SThomas Lively } else if (Binary < (1 << 16)) {
68299d214aSThomas Lively OS << uint8_t(Binary >> 8);
69299d214aSThomas Lively encodeULEB128(uint8_t(Binary), OS);
7031e94455SThomas Lively } else if (Binary < (1 << 24)) {
7131e94455SThomas Lively OS << uint8_t(Binary >> 16);
7231e94455SThomas Lively encodeULEB128(uint16_t(Binary), OS);
7331e94455SThomas Lively } else {
7431e94455SThomas Lively llvm_unreachable("Very large (prefix + 3 byte) opcodes not supported");
75cdd48b8aSDan Gohman }
764fc4e42dSDan Gohman
77d934cb88SDan Gohman // For br_table instructions, encode the size of the table. In the MCInst,
788a9cb242SWouter van Oortmerssen // there's an index operand (if not a stack instruction), one operand for
798a9cb242SWouter van Oortmerssen // each table entry, and the default operand.
808a9cb242SWouter van Oortmerssen if (MI.getOpcode() == WebAssembly::BR_TABLE_I32_S ||
818a9cb242SWouter van Oortmerssen MI.getOpcode() == WebAssembly::BR_TABLE_I64_S)
828a9cb242SWouter van Oortmerssen encodeULEB128(MI.getNumOperands() - 1, OS);
83d934cb88SDan Gohman if (MI.getOpcode() == WebAssembly::BR_TABLE_I32 ||
84d934cb88SDan Gohman MI.getOpcode() == WebAssembly::BR_TABLE_I64)
85d934cb88SDan Gohman encodeULEB128(MI.getNumOperands() - 2, OS);
86d934cb88SDan Gohman
871a427287SDan Gohman const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
8818c56a07SHeejin Ahn for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
8918c56a07SHeejin Ahn const MCOperand &MO = MI.getOperand(I);
901a427287SDan Gohman if (MO.isReg()) {
914fc4e42dSDan Gohman /* nothing to encode */
92da419bdbSHeejin Ahn
931a427287SDan Gohman } else if (MO.isImm()) {
9418c56a07SHeejin Ahn if (I < Desc.getNumOperands()) {
9518c56a07SHeejin Ahn const MCOperandInfo &Info = Desc.OpInfo[I];
96d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Encoding immediate: type="
97d34e60caSNicola Zaghen << int(Info.OperandType) << "\n");
9822442924SThomas Lively switch (Info.OperandType) {
9922442924SThomas Lively case WebAssembly::OPERAND_I32IMM:
1004fc4e42dSDan Gohman encodeSLEB128(int32_t(MO.getImm()), OS);
10122442924SThomas Lively break;
10222442924SThomas Lively case WebAssembly::OPERAND_OFFSET32:
103685c5e83SSam Clegg encodeULEB128(uint32_t(MO.getImm()), OS);
10422442924SThomas Lively break;
10522442924SThomas Lively case WebAssembly::OPERAND_I64IMM:
1064fc4e42dSDan Gohman encodeSLEB128(int64_t(MO.getImm()), OS);
10722442924SThomas Lively break;
10822442924SThomas Lively case WebAssembly::OPERAND_SIGNATURE:
10922442924SThomas Lively case WebAssembly::OPERAND_VEC_I8IMM:
11022442924SThomas Lively support::endian::write<uint8_t>(OS, MO.getImm(), support::little);
11122442924SThomas Lively break;
11222442924SThomas Lively case WebAssembly::OPERAND_VEC_I16IMM:
11322442924SThomas Lively support::endian::write<uint16_t>(OS, MO.getImm(), support::little);
11422442924SThomas Lively break;
11522442924SThomas Lively case WebAssembly::OPERAND_VEC_I32IMM:
11622442924SThomas Lively support::endian::write<uint32_t>(OS, MO.getImm(), support::little);
11722442924SThomas Lively break;
11822442924SThomas Lively case WebAssembly::OPERAND_VEC_I64IMM:
11922442924SThomas Lively support::endian::write<uint64_t>(OS, MO.getImm(), support::little);
12022442924SThomas Lively break;
12122442924SThomas Lively case WebAssembly::OPERAND_GLOBAL:
12222442924SThomas Lively llvm_unreachable("wasm globals should only be accessed symbolicly");
12322442924SThomas Lively default:
1244fc4e42dSDan Gohman encodeULEB128(uint64_t(MO.getImm()), OS);
1254fc4e42dSDan Gohman }
1263acb187dSDan Gohman } else {
1273acb187dSDan Gohman encodeULEB128(uint64_t(MO.getImm()), OS);
1283acb187dSDan Gohman }
129da419bdbSHeejin Ahn
130698c6b0aSDan Gohman } else if (MO.isSFPImm()) {
131698c6b0aSDan Gohman uint32_t F = MO.getSFPImm();
132698c6b0aSDan Gohman support::endian::write<uint32_t>(OS, F, support::little);
133698c6b0aSDan Gohman } else if (MO.isDFPImm()) {
134698c6b0aSDan Gohman uint64_t D = MO.getDFPImm();
135698c6b0aSDan Gohman support::endian::write<uint64_t>(OS, D, support::little);
1361a427287SDan Gohman } else if (MO.isExpr()) {
13718c56a07SHeejin Ahn const MCOperandInfo &Info = Desc.OpInfo[I];
138d934cb88SDan Gohman llvm::MCFixupKind FixupKind;
13966a99e41SSam Clegg size_t PaddedSize = 5;
140da419bdbSHeejin Ahn switch (Info.OperandType) {
141da419bdbSHeejin Ahn case WebAssembly::OPERAND_I32IMM:
142a5e175c6SSam Clegg FixupKind = MCFixupKind(WebAssembly::fixup_sleb128_i32);
143da419bdbSHeejin Ahn break;
144da419bdbSHeejin Ahn case WebAssembly::OPERAND_I64IMM:
145a5e175c6SSam Clegg FixupKind = MCFixupKind(WebAssembly::fixup_sleb128_i64);
146d934cb88SDan Gohman PaddedSize = 10;
147da419bdbSHeejin Ahn break;
148da419bdbSHeejin Ahn case WebAssembly::OPERAND_FUNCTION32:
14969e2797eSPaulo Matos case WebAssembly::OPERAND_TABLE:
150da419bdbSHeejin Ahn case WebAssembly::OPERAND_OFFSET32:
1512cb27072SThomas Lively case WebAssembly::OPERAND_SIGNATURE:
152da419bdbSHeejin Ahn case WebAssembly::OPERAND_TYPEINDEX:
153da419bdbSHeejin Ahn case WebAssembly::OPERAND_GLOBAL:
154*1d891d44SHeejin Ahn case WebAssembly::OPERAND_TAG:
155a5e175c6SSam Clegg FixupKind = MCFixupKind(WebAssembly::fixup_uleb128_i32);
156da419bdbSHeejin Ahn break;
157d9e0bbd1SWouter van Oortmerssen case WebAssembly::OPERAND_OFFSET64:
158d9e0bbd1SWouter van Oortmerssen FixupKind = MCFixupKind(WebAssembly::fixup_uleb128_i64);
1593b29376eSWouter van Oortmerssen PaddedSize = 10;
160d9e0bbd1SWouter van Oortmerssen break;
161da419bdbSHeejin Ahn default:
162d934cb88SDan Gohman llvm_unreachable("unexpected symbolic operand kind");
163d934cb88SDan Gohman }
164f208f631SHeejin Ahn Fixups.push_back(MCFixup::create(OS.tell() - Start, MO.getExpr(),
165d934cb88SDan Gohman FixupKind, MI.getLoc()));
1661a427287SDan Gohman ++MCNumFixups;
16766a99e41SSam Clegg encodeULEB128(0, OS, PaddedSize);
1681a427287SDan Gohman } else {
1691a427287SDan Gohman llvm_unreachable("unexpected operand kind");
1701a427287SDan Gohman }
17105ac43feSDan Gohman }
17205ac43feSDan Gohman
1731a427287SDan Gohman ++MCNumEmitted; // Keep track of the # of mi's emitted.
17405ac43feSDan Gohman }
17505ac43feSDan Gohman
17605ac43feSDan Gohman #include "WebAssemblyGenMCCodeEmitter.inc"
177