JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 1 | //===-- WebAssemblyAsmPrinter.cpp - WebAssembly LLVM assembly writer ------===// |
| 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 This file contains a printer that converts from our internal |
| 12 | /// representation of machine-dependent LLVM code to the WebAssembly assembly |
| 13 | /// language. |
| 14 | /// |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "WebAssembly.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 18 | #include "InstPrinter/WebAssemblyInstPrinter.h" |
| 19 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
| 20 | #include "WebAssemblyMCInstLower.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 21 | #include "WebAssemblyMachineFunctionInfo.h" |
| 22 | #include "WebAssemblyRegisterInfo.h" |
| 23 | #include "WebAssemblySubtarget.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/Analysis.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/AsmPrinter.h" |
JF Bastien | 54be3b1 | 2015-08-25 23:19:49 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineConstantPool.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineInstr.h" |
| 30 | #include "llvm/IR/DataLayout.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCContext.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCStreamer.h" |
JF Bastien | b6091df | 2015-08-25 22:58:05 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCSymbol.h" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
| 35 | #include "llvm/Support/TargetRegistry.h" |
| 36 | #include "llvm/Support/raw_ostream.h" |
| 37 | |
| 38 | using namespace llvm; |
| 39 | |
| 40 | #define DEBUG_TYPE "asm-printer" |
| 41 | |
| 42 | namespace { |
| 43 | |
| 44 | class WebAssemblyAsmPrinter final : public AsmPrinter { |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 45 | const MachineRegisterInfo *MRI; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 46 | const WebAssemblyFunctionInfo *MFI; |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 47 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 48 | public: |
| 49 | WebAssemblyAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer) |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 50 | : AsmPrinter(TM, std::move(Streamer)), MRI(nullptr), MFI(nullptr) {} |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | const char *getPassName() const override { |
| 54 | return "WebAssembly Assembly Printer"; |
| 55 | } |
| 56 | |
| 57 | //===------------------------------------------------------------------===// |
| 58 | // MachineFunctionPass Implementation. |
| 59 | //===------------------------------------------------------------------===// |
| 60 | |
| 61 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 62 | AsmPrinter::getAnalysisUsage(AU); |
| 63 | } |
| 64 | |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 65 | bool runOnMachineFunction(MachineFunction &MF) override { |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 66 | MRI = &MF.getRegInfo(); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 67 | MFI = MF.getInfo<WebAssemblyFunctionInfo>(); |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 68 | return AsmPrinter::runOnMachineFunction(MF); |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | //===------------------------------------------------------------------===// |
| 72 | // AsmPrinter Implementation. |
| 73 | //===------------------------------------------------------------------===// |
| 74 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 75 | void EmitJumpTableInfo() override; |
JF Bastien | 54be3b1 | 2015-08-25 23:19:49 +0000 | [diff] [blame] | 76 | void EmitConstantPool() override; |
JF Bastien | b6091df | 2015-08-25 22:58:05 +0000 | [diff] [blame] | 77 | void EmitFunctionBodyStart() override; |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 78 | void EmitInstruction(const MachineInstr *MI) override; |
JF Bastien | 1a59c6b | 2015-10-21 02:23:09 +0000 | [diff] [blame] | 79 | void EmitEndOfAsmFile(Module &M) override; |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 80 | bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 81 | unsigned AsmVariant, const char *ExtraCode, |
| 82 | raw_ostream &OS) override; |
| 83 | bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, |
| 84 | unsigned AsmVariant, const char *ExtraCode, |
| 85 | raw_ostream &OS) override; |
Dan Gohman | 979840d | 2015-09-23 16:59:10 +0000 | [diff] [blame] | 86 | |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 87 | std::string getRegTypeName(unsigned RegNo) const; |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 88 | const char *toString(MVT VT) const; |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 89 | std::string regToString(const MachineOperand &MO); |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | } // end anonymous namespace |
| 93 | |
| 94 | //===----------------------------------------------------------------------===// |
JF Bastien | 45479f6 | 2015-08-26 22:09:54 +0000 | [diff] [blame] | 95 | // Helpers. |
| 96 | //===----------------------------------------------------------------------===// |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 97 | |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 98 | std::string WebAssemblyAsmPrinter::getRegTypeName(unsigned RegNo) const { |
| 99 | const TargetRegisterClass *TRC = MRI->getRegClass(RegNo); |
| 100 | for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64}) |
| 101 | if (TRC->hasType(T)) |
| 102 | return EVT(T).getEVTString(); |
| 103 | DEBUG(errs() << "Unknown type for register number: " << RegNo); |
| 104 | llvm_unreachable("Unknown register type"); |
| 105 | return "?"; |
| 106 | } |
| 107 | |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 108 | std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) { |
| 109 | unsigned RegNo = MO.getReg(); |
Dan Gohman | d962527 | 2015-11-20 03:13:31 +0000 | [diff] [blame^] | 110 | assert(TargetRegisterInfo::isVirtualRegister(RegNo) && |
| 111 | "Unlowered physical register encountered during assembly printing"); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 112 | assert(!MFI->isVRegStackified(RegNo)); |
Dan Gohman | 058fce5 | 2015-11-13 00:21:05 +0000 | [diff] [blame] | 113 | unsigned WAReg = MFI->getWAReg(RegNo); |
| 114 | assert(WAReg != WebAssemblyFunctionInfo::UnusedReg); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 115 | return '$' + utostr(WAReg); |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 118 | const char *WebAssemblyAsmPrinter::toString(MVT VT) const { |
| 119 | switch (VT.SimpleTy) { |
Dan Gohman | 979840d | 2015-09-23 16:59:10 +0000 | [diff] [blame] | 120 | default: |
| 121 | break; |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 122 | case MVT::f32: |
Dan Gohman | 979840d | 2015-09-23 16:59:10 +0000 | [diff] [blame] | 123 | return "f32"; |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 124 | case MVT::f64: |
Dan Gohman | 979840d | 2015-09-23 16:59:10 +0000 | [diff] [blame] | 125 | return "f64"; |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 126 | case MVT::i32: |
| 127 | return "i32"; |
| 128 | case MVT::i64: |
| 129 | return "i64"; |
JF Bastien | 73ff6af | 2015-08-31 22:24:11 +0000 | [diff] [blame] | 130 | } |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 131 | DEBUG(dbgs() << "Invalid type " << EVT(VT).getEVTString() << '\n'); |
JF Bastien | 73ff6af | 2015-08-31 22:24:11 +0000 | [diff] [blame] | 132 | llvm_unreachable("invalid type"); |
| 133 | return "<invalid>"; |
| 134 | } |
| 135 | |
JF Bastien | 45479f6 | 2015-08-26 22:09:54 +0000 | [diff] [blame] | 136 | //===----------------------------------------------------------------------===// |
| 137 | // WebAssemblyAsmPrinter Implementation. |
| 138 | //===----------------------------------------------------------------------===// |
| 139 | |
JF Bastien | 54be3b1 | 2015-08-25 23:19:49 +0000 | [diff] [blame] | 140 | void WebAssemblyAsmPrinter::EmitConstantPool() { |
| 141 | assert(MF->getConstantPool()->getConstants().empty() && |
| 142 | "WebAssembly disables constant pools"); |
| 143 | } |
| 144 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 145 | void WebAssemblyAsmPrinter::EmitJumpTableInfo() { |
| 146 | // Nothing to do; jump tables are incorporated into the instruction stream. |
| 147 | } |
| 148 | |
Derek Schuff | 46e3316 | 2015-11-16 21:12:41 +0000 | [diff] [blame] | 149 | static void ComputeLegalValueVTs(const Function &F, |
| 150 | const TargetMachine &TM, |
| 151 | Type *Ty, |
| 152 | SmallVectorImpl<MVT> &ValueVTs) { |
| 153 | const DataLayout& DL(F.getParent()->getDataLayout()); |
| 154 | const WebAssemblyTargetLowering &TLI = |
| 155 | *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering(); |
| 156 | SmallVector<EVT, 4> VTs; |
| 157 | ComputeValueVTs(TLI, DL, Ty, VTs); |
| 158 | |
| 159 | for (EVT VT : VTs) { |
| 160 | unsigned NumRegs = TLI.getNumRegisters(F.getContext(), VT); |
| 161 | MVT RegisterVT = TLI.getRegisterType(F.getContext(), VT); |
| 162 | for (unsigned i = 0; i != NumRegs; ++i) |
| 163 | ValueVTs.push_back(RegisterVT); |
| 164 | } |
| 165 | } |
| 166 | |
JF Bastien | b6091df | 2015-08-25 22:58:05 +0000 | [diff] [blame] | 167 | void WebAssemblyAsmPrinter::EmitFunctionBodyStart() { |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 168 | SmallString<128> Str; |
| 169 | raw_svector_ostream OS(Str); |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 170 | |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 171 | for (MVT VT : MFI->getParams()) |
| 172 | OS << "\t" ".param " << toString(VT) << '\n'; |
Derek Schuff | 46e3316 | 2015-11-16 21:12:41 +0000 | [diff] [blame] | 173 | |
| 174 | SmallVector<MVT, 4> ResultVTs; |
| 175 | const Function &F(*MF->getFunction()); |
| 176 | ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs); |
| 177 | // If the return type needs to be legalized it will get converted into |
| 178 | // passing a pointer. |
| 179 | if (ResultVTs.size() == 1) |
| 180 | OS << "\t" ".result " << toString(ResultVTs.front()) << '\n'; |
JF Bastien | b6091df | 2015-08-25 22:58:05 +0000 | [diff] [blame] | 181 | |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 182 | bool FirstWAReg = true; |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 183 | for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) { |
| 184 | unsigned VReg = TargetRegisterInfo::index2VirtReg(Idx); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 185 | unsigned WAReg = MFI->getWAReg(VReg); |
| 186 | // Don't declare unused registers. |
| 187 | if (WAReg == WebAssemblyFunctionInfo::UnusedReg) |
| 188 | continue; |
| 189 | // Don't redeclare parameters. |
| 190 | if (WAReg < MFI->getParams().size()) |
| 191 | continue; |
| 192 | // Don't declare stackified registers. |
| 193 | if (int(WAReg) < 0) |
| 194 | continue; |
| 195 | if (FirstWAReg) |
| 196 | OS << "\t" ".local "; |
| 197 | else |
| 198 | OS << ", "; |
| 199 | OS << getRegTypeName(VReg); |
| 200 | FirstWAReg = false; |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 201 | } |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 202 | if (!FirstWAReg) |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 203 | OS << '\n'; |
JF Bastien | 1d20a5e | 2015-10-16 00:53:49 +0000 | [diff] [blame] | 204 | |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 205 | // EmitRawText appends a newline, so strip off the last newline. |
| 206 | StringRef Text = OS.str(); |
| 207 | if (!Text.empty()) |
| 208 | OutStreamer->EmitRawText(Text.substr(0, Text.size() - 1)); |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 209 | AsmPrinter::EmitFunctionBodyStart(); |
JF Bastien | b6091df | 2015-08-25 22:58:05 +0000 | [diff] [blame] | 210 | } |
| 211 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 212 | void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) { |
JF Bastien | af111db | 2015-08-24 22:16:48 +0000 | [diff] [blame] | 213 | DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n'); |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 214 | |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 215 | switch (MI->getOpcode()) { |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 216 | case WebAssembly::ARGUMENT_I32: |
| 217 | case WebAssembly::ARGUMENT_I64: |
| 218 | case WebAssembly::ARGUMENT_F32: |
| 219 | case WebAssembly::ARGUMENT_F64: |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 220 | // These represent values which are live into the function entry, so there's |
| 221 | // no instruction to emit. |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 222 | break; |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 223 | default: { |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 224 | WebAssemblyMCInstLower MCInstLowering(OutContext, *this); |
| 225 | MCInst TmpInst; |
| 226 | MCInstLowering.Lower(MI, TmpInst); |
| 227 | EmitToStreamer(*OutStreamer, TmpInst); |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 228 | break; |
| 229 | } |
Dan Gohman | 4f52e00 | 2015-09-09 00:52:47 +0000 | [diff] [blame] | 230 | } |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 231 | } |
| 232 | |
JF Bastien | 1a59c6b | 2015-10-21 02:23:09 +0000 | [diff] [blame] | 233 | void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) { |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 234 | const DataLayout &DL = M.getDataLayout(); |
| 235 | |
JF Bastien | 5789a69 | 2015-10-30 16:41:21 +0000 | [diff] [blame] | 236 | SmallString<128> Str; |
| 237 | raw_svector_ostream OS(Str); |
JF Bastien | 1a59c6b | 2015-10-21 02:23:09 +0000 | [diff] [blame] | 238 | for (const Function &F : M) |
| 239 | if (F.isDeclarationForLinker()) { |
| 240 | assert(F.hasName() && "imported functions must have a name"); |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 241 | if (F.isIntrinsic()) |
JF Bastien | 5789a69 | 2015-10-30 16:41:21 +0000 | [diff] [blame] | 242 | continue; |
JF Bastien | 1a59c6b | 2015-10-21 02:23:09 +0000 | [diff] [blame] | 243 | if (Str.empty()) |
JF Bastien | 5789a69 | 2015-10-30 16:41:21 +0000 | [diff] [blame] | 244 | OS << "\t.imports\n"; |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 245 | |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 246 | MCSymbol *Sym = OutStreamer->getContext().getOrCreateSymbol(F.getName()); |
| 247 | OS << "\t.import " << *Sym << " \"\" " << *Sym; |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 248 | |
| 249 | const WebAssemblyTargetLowering &TLI = |
| 250 | *TM.getSubtarget<WebAssemblySubtarget>(F).getTargetLowering(); |
| 251 | |
| 252 | // If we need to legalize the return type, it'll get converted into |
| 253 | // passing a pointer. |
| 254 | bool SawParam = false; |
| 255 | SmallVector<MVT, 4> ResultVTs; |
Derek Schuff | 46e3316 | 2015-11-16 21:12:41 +0000 | [diff] [blame] | 256 | ComputeLegalValueVTs(F, TM, F.getReturnType(), ResultVTs); |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 257 | if (ResultVTs.size() > 1) { |
| 258 | ResultVTs.clear(); |
| 259 | OS << " (param " << toString(TLI.getPointerTy(DL)); |
| 260 | SawParam = true; |
| 261 | } |
| 262 | |
| 263 | for (const Argument &A : F.args()) { |
| 264 | SmallVector<MVT, 4> ParamVTs; |
Derek Schuff | 46e3316 | 2015-11-16 21:12:41 +0000 | [diff] [blame] | 265 | ComputeLegalValueVTs(F, TM, A.getType(), ParamVTs); |
| 266 | for (MVT VT : ParamVTs) { |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 267 | if (!SawParam) { |
| 268 | OS << " (param"; |
| 269 | SawParam = true; |
| 270 | } |
Derek Schuff | 46e3316 | 2015-11-16 21:12:41 +0000 | [diff] [blame] | 271 | OS << ' ' << toString(VT); |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | if (SawParam) |
| 275 | OS << ')'; |
| 276 | |
Derek Schuff | 46e3316 | 2015-11-16 21:12:41 +0000 | [diff] [blame] | 277 | for (MVT VT : ResultVTs) |
| 278 | OS << " (result " << toString(VT) << ')'; |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 279 | |
JF Bastien | 1a59c6b | 2015-10-21 02:23:09 +0000 | [diff] [blame] | 280 | OS << '\n'; |
JF Bastien | 5789a69 | 2015-10-30 16:41:21 +0000 | [diff] [blame] | 281 | } |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 282 | |
| 283 | StringRef Text = OS.str(); |
| 284 | if (!Text.empty()) |
| 285 | OutStreamer->EmitRawText(Text.substr(0, Text.size() - 1)); |
JF Bastien | 1a59c6b | 2015-10-21 02:23:09 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Dan Gohman | f19ed56 | 2015-11-13 01:42:29 +0000 | [diff] [blame] | 288 | bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI, |
| 289 | unsigned OpNo, unsigned AsmVariant, |
| 290 | const char *ExtraCode, |
| 291 | raw_ostream &OS) { |
| 292 | if (AsmVariant != 0) |
| 293 | report_fatal_error("There are no defined alternate asm variants"); |
| 294 | |
| 295 | if (!ExtraCode) { |
| 296 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 297 | if (MO.isImm()) |
| 298 | OS << MO.getImm(); |
| 299 | else |
| 300 | OS << regToString(MO); |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS); |
| 305 | } |
| 306 | |
| 307 | bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, |
| 308 | unsigned OpNo, |
| 309 | unsigned AsmVariant, |
| 310 | const char *ExtraCode, |
| 311 | raw_ostream &OS) { |
| 312 | if (AsmVariant != 0) |
| 313 | report_fatal_error("There are no defined alternate asm variants"); |
| 314 | |
| 315 | if (!ExtraCode) { |
| 316 | OS << regToString(MI->getOperand(OpNo)); |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS); |
| 321 | } |
| 322 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 323 | // Force static initialization. |
| 324 | extern "C" void LLVMInitializeWebAssemblyAsmPrinter() { |
| 325 | RegisterAsmPrinter<WebAssemblyAsmPrinter> X(TheWebAssemblyTarget32); |
| 326 | RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(TheWebAssemblyTarget64); |
| 327 | } |