[WebAssembly] Reapply r252858, with svn add for the new file.
Switch to MC for instruction printing.
This encompasses several changes which are all interconnected:
- Use the MC framework for printing almost all instructions.
- AsmStrings are now live.
- This introduces an indirection between LLVM vregs and WebAssembly registers,
and a new pass, WebAssemblyRegNumbering, for computing a basic the mapping.
This addresses some basic issues with argument registers and unused registers.
- The way ARGUMENT instructions are handled no longer generates redundant
get_local+set_local for every argument.
This also changes the assembly syntax somewhat; most notably, MC's printing
does not use sigils on label names, so those are no longer present, and
push/pop now have a sigil to keep them unambiguous.
The usage of set_local/get_local/$push/$pop will continue to evolve
significantly. This patch is just one step of a larger change.
llvm-svn: 252910
diff --git a/llvm/lib/Target/WebAssembly/InstPrinter/WebAssemblyInstPrinter.cpp b/llvm/lib/Target/WebAssembly/InstPrinter/WebAssemblyInstPrinter.cpp
index bb10f20..cc77f56 100644
--- a/llvm/lib/Target/WebAssembly/InstPrinter/WebAssemblyInstPrinter.cpp
+++ b/llvm/lib/Target/WebAssembly/InstPrinter/WebAssemblyInstPrinter.cpp
@@ -36,28 +36,50 @@
void WebAssemblyInstPrinter::printRegName(raw_ostream &OS,
unsigned RegNo) const {
- if (TargetRegisterInfo::isPhysicalRegister(RegNo))
- OS << getRegisterName(RegNo);
- else
- OS << TargetRegisterInfo::virtReg2Index(RegNo);
+ // FIXME: Revisit whether we actually print the get_local explicitly.
+ OS << "(get_local " << RegNo << ")";
}
void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
StringRef Annot,
const MCSubtargetInfo &STI) {
printInstruction(MI, OS);
+
+ const MCInstrDesc &Desc = MII.get(MI->getOpcode());
+ if (Desc.isVariadic())
+ for (unsigned i = Desc.getNumOperands(), e = MI->getNumOperands(); i < e;
+ ++i) {
+ OS << ", ";
+ printOperand(MI, i, OS);
+ }
+
printAnnotation(OS, Annot);
unsigned NumDefs = MII.get(MI->getOpcode()).getNumDefs();
assert(NumDefs <= 1 &&
"Instructions with multiple result values not implemented");
- if (NumDefs != 0) {
+ // FIXME: Revisit whether we actually print the set_local explicitly.
+ if (NumDefs != 0)
OS << "\n"
- "\t" "set_local ";
- printRegName(OS, MI->getOperand(0).getReg());
- OS << ", pop";
- }
+ "\t" "set_local " << MI->getOperand(0).getReg() << ", $pop";
+}
+
+static std::string toString(const APFloat &FP) {
+ static const size_t BufBytes = 128;
+ char buf[BufBytes];
+ if (FP.isNaN())
+ assert((FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) ||
+ FP.bitwiseIsEqual(
+ APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) &&
+ "convertToHexString handles neither SNaN nor NaN payloads");
+ // Use C99's hexadecimal floating-point representation.
+ auto Written = FP.convertToHexString(
+ buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
+ (void)Written;
+ assert(Written != 0);
+ assert(Written < BufBytes);
+ return buf;
}
void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
@@ -65,13 +87,13 @@
const MCOperand &Op = MI->getOperand(OpNo);
if (Op.isReg()) {
if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
- O << "push";
+ O << "$push";
else
printRegName(O, Op.getReg());
} else if (Op.isImm())
- O << '#' << Op.getImm();
+ O << Op.getImm();
else if (Op.isFPImm())
- O << '#' << Op.getFPImm();
+ O << toString(APFloat(Op.getFPImm()));
else {
assert(Op.isExpr() && "unknown operand kind in printOperand");
Op.getExpr()->print(O, &MAI);