2015-06-30 07:51:55 +08:00
|
|
|
//=- WebAssemblyInstPrinter.cpp - WebAssembly assembly instruction printing -=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief Print MCInst instructions to wasm format.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InstPrinter/WebAssemblyInstPrinter.h"
|
2015-11-30 06:32:02 +08:00
|
|
|
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
|
2015-06-30 07:51:55 +08:00
|
|
|
#include "WebAssembly.h"
|
2015-11-13 08:21:05 +08:00
|
|
|
#include "WebAssemblyMachineFunctionInfo.h"
|
[WebAssembly] Make CFG stackification independent of basic-block labels.
This patch changes the way labels are referenced. Instead of referencing the
basic-block label name (eg. .LBB0_0), instructions now just have an immediate
which indicates the depth in the control-flow stack to find a label to jump to.
This makes them much closer to what we expect to have in the binary encoding,
and avoids the problem of basic-block label names not being explicit in the
binary encoding.
Also, it terminates blocks and loops with end_block and end_loop instructions,
rather than basic-block label names, for similar reasons.
This will also fix problems where two constructs appear to have the same label,
because we no longer explicitly use labels, so consumers that need labels will
presumably create their own labels, and presumably they won't reuse labels
when they do.
This patch does make the code a little more awkward to read; as a partial
mitigation, this patch also introduces comments showing where the labels are,
and comments on each branch showing where it's branching to.
llvm-svn: 257505
2016-01-13 03:14:46 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2015-06-30 07:51:55 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
|
|
|
#include "llvm/MC/MCSymbol.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2015-11-06 03:28:16 +08:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2015-06-30 07:51:55 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "asm-printer"
|
|
|
|
|
2015-07-23 05:28:15 +08:00
|
|
|
#include "WebAssemblyGenAsmWriter.inc"
|
|
|
|
|
2015-06-30 07:51:55 +08:00
|
|
|
WebAssemblyInstPrinter::WebAssemblyInstPrinter(const MCAsmInfo &MAI,
|
|
|
|
const MCInstrInfo &MII,
|
|
|
|
const MCRegisterInfo &MRI)
|
[WebAssembly] Make CFG stackification independent of basic-block labels.
This patch changes the way labels are referenced. Instead of referencing the
basic-block label name (eg. .LBB0_0), instructions now just have an immediate
which indicates the depth in the control-flow stack to find a label to jump to.
This makes them much closer to what we expect to have in the binary encoding,
and avoids the problem of basic-block label names not being explicit in the
binary encoding.
Also, it terminates blocks and loops with end_block and end_loop instructions,
rather than basic-block label names, for similar reasons.
This will also fix problems where two constructs appear to have the same label,
because we no longer explicitly use labels, so consumers that need labels will
presumably create their own labels, and presumably they won't reuse labels
when they do.
This patch does make the code a little more awkward to read; as a partial
mitigation, this patch also introduces comments showing where the labels are,
and comments on each branch showing where it's branching to.
llvm-svn: 257505
2016-01-13 03:14:46 +08:00
|
|
|
: MCInstPrinter(MAI, MII, MRI), ControlFlowCounter(0) {}
|
2015-06-30 07:51:55 +08:00
|
|
|
|
|
|
|
void WebAssemblyInstPrinter::printRegName(raw_ostream &OS,
|
|
|
|
unsigned RegNo) const {
|
2015-11-13 08:21:05 +08:00
|
|
|
assert(RegNo != WebAssemblyFunctionInfo::UnusedReg);
|
2015-11-19 00:12:01 +08:00
|
|
|
// Note that there's an implicit get_local/set_local here!
|
|
|
|
OS << "$" << RegNo;
|
2015-06-30 07:51:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
|
|
|
|
StringRef Annot,
|
2015-11-30 06:32:02 +08:00
|
|
|
const MCSubtargetInfo & /*STI*/) {
|
2015-12-22 00:50:41 +08:00
|
|
|
// Print the instruction (this uses the AsmStrings from the .td files).
|
2015-07-23 05:28:15 +08:00
|
|
|
printInstruction(MI, OS);
|
2015-11-13 01:04:33 +08:00
|
|
|
|
2015-12-22 00:50:41 +08:00
|
|
|
// Print any additional variadic operands.
|
2015-11-13 01:04:33 +08:00
|
|
|
const MCInstrDesc &Desc = MII.get(MI->getOpcode());
|
|
|
|
if (Desc.isVariadic())
|
2015-12-22 00:50:41 +08:00
|
|
|
for (auto i = Desc.getNumOperands(), e = MI->getNumOperands(); i < e; ++i) {
|
2016-10-26 00:55:52 +08:00
|
|
|
// FIXME: For CALL_INDIRECT_VOID, don't print a leading comma, because
|
|
|
|
// we have an extra flags operand which is not currently printed, for
|
|
|
|
// compatiblity reasons.
|
|
|
|
if (i != 0 &&
|
|
|
|
(MI->getOpcode() != WebAssembly::CALL_INDIRECT_VOID ||
|
|
|
|
i != Desc.getNumOperands()))
|
2015-11-24 00:50:18 +08:00
|
|
|
OS << ", ";
|
2015-11-13 01:04:33 +08:00
|
|
|
printOperand(MI, i, OS);
|
|
|
|
}
|
|
|
|
|
2015-12-22 00:50:41 +08:00
|
|
|
// Print any added annotation.
|
2015-07-23 05:28:15 +08:00
|
|
|
printAnnotation(OS, Annot);
|
[WebAssembly] Make CFG stackification independent of basic-block labels.
This patch changes the way labels are referenced. Instead of referencing the
basic-block label name (eg. .LBB0_0), instructions now just have an immediate
which indicates the depth in the control-flow stack to find a label to jump to.
This makes them much closer to what we expect to have in the binary encoding,
and avoids the problem of basic-block label names not being explicit in the
binary encoding.
Also, it terminates blocks and loops with end_block and end_loop instructions,
rather than basic-block label names, for similar reasons.
This will also fix problems where two constructs appear to have the same label,
because we no longer explicitly use labels, so consumers that need labels will
presumably create their own labels, and presumably they won't reuse labels
when they do.
This patch does make the code a little more awkward to read; as a partial
mitigation, this patch also introduces comments showing where the labels are,
and comments on each branch showing where it's branching to.
llvm-svn: 257505
2016-01-13 03:14:46 +08:00
|
|
|
|
|
|
|
if (CommentStream) {
|
|
|
|
// Observe any effects on the control flow stack, for use in annotating
|
|
|
|
// control flow label references.
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case WebAssembly::LOOP: {
|
2016-10-07 06:10:23 +08:00
|
|
|
printAnnotation(OS, "label" + utostr(ControlFlowCounter) + ':');
|
|
|
|
ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, true));
|
[WebAssembly] Make CFG stackification independent of basic-block labels.
This patch changes the way labels are referenced. Instead of referencing the
basic-block label name (eg. .LBB0_0), instructions now just have an immediate
which indicates the depth in the control-flow stack to find a label to jump to.
This makes them much closer to what we expect to have in the binary encoding,
and avoids the problem of basic-block label names not being explicit in the
binary encoding.
Also, it terminates blocks and loops with end_block and end_loop instructions,
rather than basic-block label names, for similar reasons.
This will also fix problems where two constructs appear to have the same label,
because we no longer explicitly use labels, so consumers that need labels will
presumably create their own labels, and presumably they won't reuse labels
when they do.
This patch does make the code a little more awkward to read; as a partial
mitigation, this patch also introduces comments showing where the labels are,
and comments on each branch showing where it's branching to.
llvm-svn: 257505
2016-01-13 03:14:46 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WebAssembly::BLOCK:
|
|
|
|
ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
|
|
|
|
break;
|
|
|
|
case WebAssembly::END_LOOP:
|
|
|
|
ControlFlowStack.pop_back();
|
|
|
|
break;
|
|
|
|
case WebAssembly::END_BLOCK:
|
|
|
|
printAnnotation(
|
|
|
|
OS, "label" + utostr(ControlFlowStack.pop_back_val().first) + ':');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Annotate any control flow label references.
|
|
|
|
unsigned NumFixedOperands = Desc.NumOperands;
|
|
|
|
SmallSet<uint64_t, 8> Printed;
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i < e; ++i) {
|
|
|
|
const MCOperandInfo &Info = Desc.OpInfo[i];
|
|
|
|
if (!(i < NumFixedOperands
|
|
|
|
? (Info.OperandType == WebAssembly::OPERAND_BASIC_BLOCK)
|
|
|
|
: (Desc.TSFlags & WebAssemblyII::VariableOpImmediateIsLabel)))
|
|
|
|
continue;
|
|
|
|
uint64_t Depth = MI->getOperand(i).getImm();
|
|
|
|
if (!Printed.insert(Depth).second)
|
|
|
|
continue;
|
|
|
|
const auto &Pair = ControlFlowStack.rbegin()[Depth];
|
|
|
|
printAnnotation(OS, utostr(Depth) + ": " + (Pair.second ? "up" : "down") +
|
|
|
|
" to label" + utostr(Pair.first));
|
|
|
|
}
|
|
|
|
}
|
2015-11-13 01:04:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string toString(const APFloat &FP) {
|
2016-02-16 23:14:23 +08:00
|
|
|
// Print NaNs with custom payloads specially.
|
|
|
|
if (FP.isNaN() &&
|
|
|
|
!FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics())) &&
|
|
|
|
!FP.bitwiseIsEqual(APFloat::getQNaN(FP.getSemantics(), /*Negative=*/true))) {
|
|
|
|
APInt AI = FP.bitcastToAPInt();
|
|
|
|
return
|
|
|
|
std::string(AI.isNegative() ? "-" : "") + "nan:0x" +
|
|
|
|
utohexstr(AI.getZExtValue() &
|
|
|
|
(AI.getBitWidth() == 32 ? INT64_C(0x007fffff) :
|
|
|
|
INT64_C(0x000fffffffffffff)),
|
|
|
|
/*LowerCase=*/true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use C99's hexadecimal floating-point representation.
|
2015-11-13 01:04:33 +08:00
|
|
|
static const size_t BufBytes = 128;
|
|
|
|
char buf[BufBytes];
|
|
|
|
auto Written = FP.convertToHexString(
|
|
|
|
buf, /*hexDigits=*/0, /*upperCase=*/false, APFloat::rmNearestTiesToEven);
|
|
|
|
(void)Written;
|
|
|
|
assert(Written != 0);
|
|
|
|
assert(Written < BufBytes);
|
|
|
|
return buf;
|
2015-06-30 07:51:55 +08:00
|
|
|
}
|
2015-08-25 06:16:48 +08:00
|
|
|
|
|
|
|
void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) {
|
|
|
|
const MCOperand &Op = MI->getOperand(OpNo);
|
2015-11-06 03:28:16 +08:00
|
|
|
if (Op.isReg()) {
|
2016-01-12 09:45:12 +08:00
|
|
|
assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
|
|
|
|
MII.get(MI->getOpcode()).TSFlags == 0) &&
|
|
|
|
"WebAssembly variable_ops register ops don't use TSFlags");
|
2015-11-19 00:12:01 +08:00
|
|
|
unsigned WAReg = Op.getReg();
|
|
|
|
if (int(WAReg) >= 0)
|
|
|
|
printRegName(O, WAReg);
|
|
|
|
else if (OpNo >= MII.get(MI->getOpcode()).getNumDefs())
|
2016-05-21 08:21:56 +08:00
|
|
|
O << "$pop" << WebAssemblyFunctionInfo::getWARegStackId(WAReg);
|
2015-11-19 00:12:01 +08:00
|
|
|
else if (WAReg != WebAssemblyFunctionInfo::UnusedReg)
|
2016-05-21 08:21:56 +08:00
|
|
|
O << "$push" << WebAssemblyFunctionInfo::getWARegStackId(WAReg);
|
2015-11-19 00:12:01 +08:00
|
|
|
else
|
2016-05-18 07:19:03 +08:00
|
|
|
O << "$drop";
|
2015-11-24 05:55:57 +08:00
|
|
|
// Add a '=' suffix if this is a def.
|
|
|
|
if (OpNo < MII.get(MI->getOpcode()).getNumDefs())
|
|
|
|
O << '=';
|
2015-11-24 00:50:18 +08:00
|
|
|
} else if (Op.isImm()) {
|
2016-10-25 03:49:43 +08:00
|
|
|
const MCInstrDesc &Desc = MII.get(MI->getOpcode());
|
|
|
|
assert((OpNo < Desc.getNumOperands() ||
|
|
|
|
(Desc.TSFlags & WebAssemblyII::VariableOpIsImmediate)) &&
|
2016-01-12 09:45:12 +08:00
|
|
|
"WebAssemblyII::VariableOpIsImmediate should be set for "
|
|
|
|
"variable_ops immediate ops");
|
2016-10-25 17:08:50 +08:00
|
|
|
(void)Desc;
|
2016-10-25 07:27:49 +08:00
|
|
|
// TODO: (MII.get(MI->getOpcode()).TSFlags &
|
|
|
|
// WebAssemblyII::VariableOpImmediateIsLabel)
|
|
|
|
// can tell us whether this is an immediate referencing a label in the
|
|
|
|
// control flow stack, and it may be nice to pretty-print.
|
|
|
|
O << Op.getImm();
|
2016-01-12 09:45:12 +08:00
|
|
|
} else if (Op.isFPImm()) {
|
2016-02-16 23:14:23 +08:00
|
|
|
const MCInstrDesc &Desc = MII.get(MI->getOpcode());
|
|
|
|
assert(OpNo < Desc.getNumOperands() &&
|
|
|
|
"Unexpected floating-point immediate as a non-fixed operand");
|
|
|
|
assert(Desc.TSFlags == 0 &&
|
2016-01-12 09:45:12 +08:00
|
|
|
"WebAssembly variable_ops floating point ops don't use TSFlags");
|
2016-02-16 23:14:23 +08:00
|
|
|
const MCOperandInfo &Info = Desc.OpInfo[OpNo];
|
2016-10-04 05:31:31 +08:00
|
|
|
if (Info.OperandType == WebAssembly::OPERAND_F32IMM) {
|
2016-02-16 23:14:23 +08:00
|
|
|
// TODO: MC converts all floating point immediate operands to double.
|
|
|
|
// This is fine for numeric values, but may cause NaNs to change bits.
|
|
|
|
O << toString(APFloat(float(Op.getFPImm())));
|
|
|
|
} else {
|
2016-10-04 05:31:31 +08:00
|
|
|
assert(Info.OperandType == WebAssembly::OPERAND_F64IMM);
|
2016-02-16 23:14:23 +08:00
|
|
|
O << toString(APFloat(Op.getFPImm()));
|
|
|
|
}
|
2016-01-12 09:45:12 +08:00
|
|
|
} else {
|
|
|
|
assert((OpNo < MII.get(MI->getOpcode()).getNumOperands() ||
|
|
|
|
(MII.get(MI->getOpcode()).TSFlags &
|
[WebAssembly] Make CFG stackification independent of basic-block labels.
This patch changes the way labels are referenced. Instead of referencing the
basic-block label name (eg. .LBB0_0), instructions now just have an immediate
which indicates the depth in the control-flow stack to find a label to jump to.
This makes them much closer to what we expect to have in the binary encoding,
and avoids the problem of basic-block label names not being explicit in the
binary encoding.
Also, it terminates blocks and loops with end_block and end_loop instructions,
rather than basic-block label names, for similar reasons.
This will also fix problems where two constructs appear to have the same label,
because we no longer explicitly use labels, so consumers that need labels will
presumably create their own labels, and presumably they won't reuse labels
when they do.
This patch does make the code a little more awkward to read; as a partial
mitigation, this patch also introduces comments showing where the labels are,
and comments on each branch showing where it's branching to.
llvm-svn: 257505
2016-01-13 03:14:46 +08:00
|
|
|
WebAssemblyII::VariableOpIsImmediate)) &&
|
2016-01-12 09:45:12 +08:00
|
|
|
"WebAssemblyII::VariableOpIsImmediate should be set for "
|
|
|
|
"variable_ops expr ops");
|
2015-08-25 06:16:48 +08:00
|
|
|
assert(Op.isExpr() && "unknown operand kind in printOperand");
|
|
|
|
Op.getExpr()->print(O, &MAI);
|
|
|
|
}
|
|
|
|
}
|
2015-12-07 03:42:29 +08:00
|
|
|
|
2016-01-26 11:39:31 +08:00
|
|
|
void
|
|
|
|
WebAssemblyInstPrinter::printWebAssemblyP2AlignOperand(const MCInst *MI,
|
|
|
|
unsigned OpNo,
|
|
|
|
raw_ostream &O) {
|
|
|
|
int64_t Imm = MI->getOperand(OpNo).getImm();
|
|
|
|
if (Imm == WebAssembly::GetDefaultP2Align(MI->getOpcode()))
|
|
|
|
return;
|
|
|
|
O << ":p2align=" << Imm;
|
|
|
|
}
|
|
|
|
|
2016-10-07 06:29:32 +08:00
|
|
|
void
|
|
|
|
WebAssemblyInstPrinter::printWebAssemblySignatureOperand(const MCInst *MI,
|
|
|
|
unsigned OpNo,
|
|
|
|
raw_ostream &O) {
|
|
|
|
int64_t Imm = MI->getOperand(OpNo).getImm();
|
2016-10-25 03:49:43 +08:00
|
|
|
switch (WebAssembly::ExprType(Imm)) {
|
|
|
|
case WebAssembly::ExprType::Void: break;
|
|
|
|
case WebAssembly::ExprType::I32: O << "i32"; break;
|
|
|
|
case WebAssembly::ExprType::I64: O << "i64"; break;
|
|
|
|
case WebAssembly::ExprType::F32: O << "f32"; break;
|
|
|
|
case WebAssembly::ExprType::F64: O << "f64"; break;
|
|
|
|
case WebAssembly::ExprType::I8x16: O << "i8x16"; break;
|
|
|
|
case WebAssembly::ExprType::I16x8: O << "i16x8"; break;
|
|
|
|
case WebAssembly::ExprType::I32x4: O << "i32x4"; break;
|
|
|
|
case WebAssembly::ExprType::F32x4: O << "f32x4"; break;
|
2016-10-25 07:27:49 +08:00
|
|
|
case WebAssembly::ExprType::B8x16: O << "b8x16"; break;
|
|
|
|
case WebAssembly::ExprType::B16x8: O << "b16x8"; break;
|
|
|
|
case WebAssembly::ExprType::B32x4: O << "b32x4"; break;
|
2016-10-07 06:29:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 03:42:29 +08:00
|
|
|
const char *llvm::WebAssembly::TypeToString(MVT Ty) {
|
|
|
|
switch (Ty.SimpleTy) {
|
|
|
|
case MVT::i32:
|
|
|
|
return "i32";
|
|
|
|
case MVT::i64:
|
|
|
|
return "i64";
|
|
|
|
case MVT::f32:
|
|
|
|
return "f32";
|
|
|
|
case MVT::f64:
|
|
|
|
return "f64";
|
2016-08-03 07:16:09 +08:00
|
|
|
case MVT::v16i8:
|
|
|
|
case MVT::v8i16:
|
|
|
|
case MVT::v4i32:
|
|
|
|
case MVT::v4f32:
|
|
|
|
return "v128";
|
2015-12-07 03:42:29 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unsupported type");
|
|
|
|
}
|
|
|
|
}
|