blob: bb10f20fd6d069814d2fdfeaee58ed611306fbbf [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//=- WebAssemblyInstPrinter.cpp - WebAssembly assembly instruction printing -=//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief Print MCInst instructions to wasm format.
12///
13//===----------------------------------------------------------------------===//
14
15#include "InstPrinter/WebAssemblyInstPrinter.h"
16#include "WebAssembly.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCInst.h"
19#include "llvm/MC/MCInstrInfo.h"
20#include "llvm/MC/MCSubtargetInfo.h"
21#include "llvm/MC/MCSymbol.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/FormattedStream.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000024#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000025#include <cctype>
26using namespace llvm;
27
28#define DEBUG_TYPE "asm-printer"
29
JF Bastienb9073fb2015-07-22 21:28:15 +000030#include "WebAssemblyGenAsmWriter.inc"
31
Dan Gohman10e730a2015-06-29 23:51:55 +000032WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI,
33 const MCInstrInfo &MII,
34 const MCRegisterInfo &MRI)
35 : MCInstPrinter(MAI, MII, MRI) {}
36
37void WebAssemblyInstPrinter::printRegName(raw_ostream &OS,
38 unsigned RegNo) const {
Dan Gohmane9361d52015-11-05 19:28:16 +000039 if (TargetRegisterInfo::isPhysicalRegister(RegNo))
40 OS << getRegisterName(RegNo);
41 else
42 OS << TargetRegisterInfo::virtReg2Index(RegNo);
Dan Gohman10e730a2015-06-29 23:51:55 +000043}
44
45void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
46 StringRef Annot,
47 const MCSubtargetInfo &STI) {
JF Bastienb9073fb2015-07-22 21:28:15 +000048 printInstruction(MI, OS);
49 printAnnotation(OS, Annot);
Dan Gohmane9361d52015-11-05 19:28:16 +000050
51 unsigned NumDefs = MII.get(MI->getOpcode()).getNumDefs();
52 assert(NumDefs <= 1 &&
53 "Instructions with multiple result values not implemented");
54
55 if (NumDefs != 0) {
56 OS << "\n"
57 "\t" "set_local ";
58 printRegName(OS, MI->getOperand(0).getReg());
59 OS << ", pop";
60 }
Dan Gohman10e730a2015-06-29 23:51:55 +000061}
JF Bastienaf111db2015-08-24 22:16:48 +000062
63void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
64 raw_ostream &O) {
65 const MCOperand &Op = MI->getOperand(OpNo);
Dan Gohmane9361d52015-11-05 19:28:16 +000066 if (Op.isReg()) {
67 if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
68 O << "push";
69 else
70 printRegName(O, Op.getReg());
71 } else if (Op.isImm())
JF Bastienaf111db2015-08-24 22:16:48 +000072 O << '#' << Op.getImm();
Dan Gohmane9361d52015-11-05 19:28:16 +000073 else if (Op.isFPImm())
74 O << '#' << Op.getFPImm();
JF Bastienaf111db2015-08-24 22:16:48 +000075 else {
76 assert(Op.isExpr() && "unknown operand kind in printOperand");
77 Op.getExpr()->print(O, &MAI);
78 }
79}