2009-06-19 08:47:59 +08:00
|
|
|
//===-- X86ATTInstPrinter.cpp - AT&T assembly instruction printing --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file includes code for rendering MCInst instances as AT&T-style
|
|
|
|
// assembly.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-02-07 05:55:43 +08:00
|
|
|
#include "X86ATTInstPrinter.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "MCTargetDesc/X86BaseInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "X86InstComments.h"
|
2009-08-31 16:08:38 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/MC/MCInst.h"
|
2017-02-07 05:55:43 +08:00
|
|
|
#include "llvm/MC/MCInstrDesc.h"
|
2012-04-02 15:01:04 +08:00
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
2015-12-26 06:10:01 +08:00
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2017-02-07 05:55:43 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2009-07-09 02:01:40 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-02-10 08:10:18 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2017-02-07 05:55:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cinttypes>
|
|
|
|
#include <cstdint>
|
|
|
|
|
2009-06-19 08:47:59 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "asm-printer"
|
|
|
|
|
2009-06-20 07:59:57 +08:00
|
|
|
// Include the auto-generated portion of the assembly writer.
|
2011-04-08 05:20:06 +08:00
|
|
|
#define PRINT_ALIAS_INSTR
|
2009-06-20 07:59:57 +08:00
|
|
|
#include "X86GenAsmWriter.inc"
|
2011-04-08 05:20:06 +08:00
|
|
|
|
2015-03-28 12:25:41 +08:00
|
|
|
void X86ATTInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
|
|
|
|
OS << markup("<reg:") << '%' << getRegisterName(RegNo) << markup(">");
|
2011-05-31 04:20:15 +08:00
|
|
|
}
|
|
|
|
|
2011-09-16 07:38:46 +08:00
|
|
|
void X86ATTInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
|
2015-03-28 04:36:02 +08:00
|
|
|
StringRef Annot, const MCSubtargetInfo &STI) {
|
2012-09-26 13:13:44 +08:00
|
|
|
const MCInstrDesc &Desc = MII.get(MI->getOpcode());
|
|
|
|
uint64_t TSFlags = Desc.TSFlags;
|
|
|
|
|
2014-09-04 06:46:44 +08:00
|
|
|
// If verbose assembly is enabled, we can print some informative comments.
|
|
|
|
if (CommentStream)
|
|
|
|
HasCustomInstComment =
|
|
|
|
EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
|
|
|
|
|
2017-10-16 19:14:29 +08:00
|
|
|
unsigned Flags = MI->getFlags();
|
2012-09-26 13:13:44 +08:00
|
|
|
if (TSFlags & X86II::LOCK)
|
2015-05-27 02:35:10 +08:00
|
|
|
OS << "\tlock\t";
|
2017-10-16 19:14:29 +08:00
|
|
|
if (!(TSFlags & X86II::LOCK) && Flags & X86::IP_HAS_LOCK)
|
2017-11-03 23:25:13 +08:00
|
|
|
OS << "\tlock\t";
|
2017-10-16 19:14:29 +08:00
|
|
|
|
|
|
|
if (Flags & X86::IP_HAS_REPEAT_NE)
|
2017-11-03 23:25:13 +08:00
|
|
|
OS << "\trepne\t";
|
2017-10-16 19:14:29 +08:00
|
|
|
else if (Flags & X86::IP_HAS_REPEAT)
|
2017-11-03 23:25:13 +08:00
|
|
|
OS << "\trep\t";
|
2012-09-26 13:13:44 +08:00
|
|
|
|
2014-09-09 19:54:12 +08:00
|
|
|
// Output CALLpcrel32 as "callq" in 64-bit mode.
|
|
|
|
// In Intel annotation it's always emitted as "call".
|
|
|
|
//
|
|
|
|
// TODO: Probably this hack should be redesigned via InstAlias in
|
|
|
|
// InstrInfo.td as soon as Requires clause is supported properly
|
|
|
|
// for InstAlias.
|
|
|
|
if (MI->getOpcode() == X86::CALLpcrel32 &&
|
2015-05-26 18:47:10 +08:00
|
|
|
(STI.getFeatureBits()[X86::Mode64Bit])) {
|
2014-09-09 19:54:12 +08:00
|
|
|
OS << "\tcallq\t";
|
|
|
|
printPCRelImm(MI, 0, OS);
|
|
|
|
}
|
2017-01-18 16:07:51 +08:00
|
|
|
// data16 and data32 both have the same encoding of 0x66. While data32 is
|
|
|
|
// valid only in 16 bit systems, data16 is valid in the rest.
|
|
|
|
// There seems to be some lack of support of the Requires clause that causes
|
|
|
|
// 0x66 to be interpreted as "data16" by the asm printer.
|
|
|
|
// Thus we add an adjustment here in order to print the "right" instruction.
|
|
|
|
else if (MI->getOpcode() == X86::DATA16_PREFIX &&
|
|
|
|
(STI.getFeatureBits()[X86::Mode16Bit])) {
|
|
|
|
MCInst Data32MI(*MI);
|
|
|
|
Data32MI.setOpcode(X86::DATA32_PREFIX);
|
|
|
|
printInstruction(&Data32MI, OS);
|
|
|
|
}
|
2011-04-19 05:28:11 +08:00
|
|
|
// Try to print any aliases first.
|
2014-09-09 19:54:12 +08:00
|
|
|
else if (!printAliasInstr(MI, OS))
|
2011-04-14 09:11:51 +08:00
|
|
|
printInstruction(MI, OS);
|
2013-07-31 10:00:15 +08:00
|
|
|
|
2012-02-24 02:18:17 +08:00
|
|
|
// Next always print the annotation.
|
|
|
|
printAnnotation(OS, Annot);
|
2010-04-04 12:47:45 +08:00
|
|
|
}
|
2011-04-08 05:20:06 +08:00
|
|
|
|
2015-01-28 18:09:52 +08:00
|
|
|
void X86ATTInstPrinter::printSSEAVXCC(const MCInst *MI, unsigned Op,
|
|
|
|
raw_ostream &O) {
|
|
|
|
int64_t Imm = MI->getOperand(Op).getImm();
|
2012-10-09 13:26:13 +08:00
|
|
|
switch (Imm) {
|
2015-03-28 12:40:43 +08:00
|
|
|
default: llvm_unreachable("Invalid ssecc/avxcc argument!");
|
|
|
|
case 0: O << "eq"; break;
|
|
|
|
case 1: O << "lt"; break;
|
|
|
|
case 2: O << "le"; break;
|
|
|
|
case 3: O << "unord"; break;
|
|
|
|
case 4: O << "neq"; break;
|
|
|
|
case 5: O << "nlt"; break;
|
|
|
|
case 6: O << "nle"; break;
|
|
|
|
case 7: O << "ord"; break;
|
|
|
|
case 8: O << "eq_uq"; break;
|
|
|
|
case 9: O << "nge"; break;
|
|
|
|
case 0xa: O << "ngt"; break;
|
|
|
|
case 0xb: O << "false"; break;
|
|
|
|
case 0xc: O << "neq_oq"; break;
|
|
|
|
case 0xd: O << "ge"; break;
|
|
|
|
case 0xe: O << "gt"; break;
|
|
|
|
case 0xf: O << "true"; break;
|
|
|
|
case 0x10: O << "eq_os"; break;
|
|
|
|
case 0x11: O << "lt_oq"; break;
|
|
|
|
case 0x12: O << "le_oq"; break;
|
|
|
|
case 0x13: O << "unord_s"; break;
|
|
|
|
case 0x14: O << "neq_us"; break;
|
|
|
|
case 0x15: O << "nlt_uq"; break;
|
|
|
|
case 0x16: O << "nle_uq"; break;
|
|
|
|
case 0x17: O << "ord_s"; break;
|
|
|
|
case 0x18: O << "eq_us"; break;
|
|
|
|
case 0x19: O << "nge_uq"; break;
|
|
|
|
case 0x1a: O << "ngt_uq"; break;
|
|
|
|
case 0x1b: O << "false_os"; break;
|
|
|
|
case 0x1c: O << "neq_os"; break;
|
|
|
|
case 0x1d: O << "ge_oq"; break;
|
|
|
|
case 0x1e: O << "gt_oq"; break;
|
|
|
|
case 0x1f: O << "true_us"; break;
|
2009-06-20 07:59:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-13 15:42:25 +08:00
|
|
|
void X86ATTInstPrinter::printXOPCC(const MCInst *MI, unsigned Op,
|
2014-01-01 23:12:34 +08:00
|
|
|
raw_ostream &O) {
|
2015-02-13 15:42:25 +08:00
|
|
|
int64_t Imm = MI->getOperand(Op).getImm();
|
|
|
|
switch (Imm) {
|
2015-03-28 12:40:43 +08:00
|
|
|
default: llvm_unreachable("Invalid xopcc argument!");
|
|
|
|
case 0: O << "lt"; break;
|
|
|
|
case 1: O << "le"; break;
|
|
|
|
case 2: O << "gt"; break;
|
|
|
|
case 3: O << "ge"; break;
|
|
|
|
case 4: O << "eq"; break;
|
|
|
|
case 5: O << "neq"; break;
|
|
|
|
case 6: O << "false"; break;
|
|
|
|
case 7: O << "true"; break;
|
2015-02-13 15:42:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void X86ATTInstPrinter::printRoundingControl(const MCInst *MI, unsigned Op,
|
2015-03-28 12:40:43 +08:00
|
|
|
raw_ostream &O) {
|
2014-01-13 20:55:03 +08:00
|
|
|
int64_t Imm = MI->getOperand(Op).getImm() & 0x3;
|
2014-01-01 23:12:34 +08:00
|
|
|
switch (Imm) {
|
2015-03-28 12:40:43 +08:00
|
|
|
case 0: O << "{rn-sae}"; break;
|
|
|
|
case 1: O << "{rd-sae}"; break;
|
|
|
|
case 2: O << "{ru-sae}"; break;
|
|
|
|
case 3: O << "{rz-sae}"; break;
|
2014-01-01 23:12:34 +08:00
|
|
|
}
|
|
|
|
}
|
2017-02-07 05:55:43 +08:00
|
|
|
|
2012-09-11 06:50:57 +08:00
|
|
|
/// printPCRelImm - This is used to print an immediate value that ends up
|
2009-12-22 08:44:05 +08:00
|
|
|
/// being encoded as a pc-relative value (e.g. for jumps and calls). These
|
|
|
|
/// print slightly differently than normal immediates. For example, a $ is not
|
|
|
|
/// emitted.
|
2012-09-11 06:50:57 +08:00
|
|
|
void X86ATTInstPrinter::printPCRelImm(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) {
|
2009-06-21 03:34:09 +08:00
|
|
|
const MCOperand &Op = MI->getOperand(OpNo);
|
|
|
|
if (Op.isImm())
|
2012-12-06 02:13:19 +08:00
|
|
|
O << formatImm(Op.getImm());
|
2009-09-14 09:34:40 +08:00
|
|
|
else {
|
|
|
|
assert(Op.isExpr() && "unknown pcrel immediate operand");
|
2012-02-24 02:18:17 +08:00
|
|
|
// If a symbolic branch target was added as a constant expression then print
|
|
|
|
// that address in hex.
|
|
|
|
const MCConstantExpr *BranchTarget = dyn_cast<MCConstantExpr>(Op.getExpr());
|
|
|
|
int64_t Address;
|
2015-05-30 09:25:56 +08:00
|
|
|
if (BranchTarget && BranchTarget->evaluateAsAbsolute(Address)) {
|
2013-08-02 05:18:16 +08:00
|
|
|
O << formatHex((uint64_t)Address);
|
2014-12-04 13:20:33 +08:00
|
|
|
} else {
|
2012-02-24 02:18:17 +08:00
|
|
|
// Otherwise, just print the expression.
|
2015-06-09 08:31:39 +08:00
|
|
|
Op.getExpr()->print(O, &MAI);
|
2012-02-24 02:18:17 +08:00
|
|
|
}
|
2009-09-14 09:34:40 +08:00
|
|
|
}
|
2009-06-21 03:34:09 +08:00
|
|
|
}
|
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) {
|
2009-06-20 08:49:26 +08:00
|
|
|
const MCOperand &Op = MI->getOperand(OpNo);
|
|
|
|
if (Op.isReg()) {
|
2012-10-24 06:52:52 +08:00
|
|
|
printRegName(O, Op.getReg());
|
2009-06-20 08:49:26 +08:00
|
|
|
} else if (Op.isImm()) {
|
2016-05-28 22:58:37 +08:00
|
|
|
// Print immediates as signed values.
|
2016-05-19 01:23:38 +08:00
|
|
|
int64_t Imm = Op.getImm();
|
|
|
|
O << markup("<imm:") << '$' << formatImm(Imm) << markup(">");
|
2013-07-31 10:00:15 +08:00
|
|
|
|
2016-05-28 22:58:37 +08:00
|
|
|
// TODO: This should be in a helper function in the base class, so it can
|
|
|
|
// be used by other printers.
|
|
|
|
|
2014-09-04 06:46:44 +08:00
|
|
|
// If there are no instruction-specific comments, add a comment clarifying
|
|
|
|
// the hex value of the immediate operand when it isn't in the range
|
|
|
|
// [-256,255].
|
2016-05-28 22:58:37 +08:00
|
|
|
if (CommentStream && !HasCustomInstComment && (Imm > 255 || Imm < -256)) {
|
|
|
|
// Don't print unnecessary hex sign bits.
|
|
|
|
if (Imm == (int16_t)(Imm))
|
|
|
|
*CommentStream << format("imm = 0x%" PRIX16 "\n", (uint16_t)Imm);
|
|
|
|
else if (Imm == (int32_t)(Imm))
|
|
|
|
*CommentStream << format("imm = 0x%" PRIX32 "\n", (uint32_t)Imm);
|
|
|
|
else
|
|
|
|
*CommentStream << format("imm = 0x%" PRIX64 "\n", (uint64_t)Imm);
|
|
|
|
}
|
2009-09-14 09:34:40 +08:00
|
|
|
} else {
|
|
|
|
assert(Op.isExpr() && "unknown operand kind in printOperand");
|
2015-06-09 08:31:39 +08:00
|
|
|
O << markup("<imm:") << '$';
|
|
|
|
Op.getExpr()->print(O, &MAI);
|
|
|
|
O << markup(">");
|
2009-06-20 08:49:26 +08:00
|
|
|
}
|
2009-06-20 07:59:57 +08:00
|
|
|
}
|
|
|
|
|
2010-07-09 07:46:44 +08:00
|
|
|
void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
|
|
|
|
raw_ostream &O) {
|
2015-03-28 12:25:41 +08:00
|
|
|
const MCOperand &BaseReg = MI->getOperand(Op + X86::AddrBaseReg);
|
|
|
|
const MCOperand &IndexReg = MI->getOperand(Op + X86::AddrIndexReg);
|
|
|
|
const MCOperand &DispSpec = MI->getOperand(Op + X86::AddrDisp);
|
|
|
|
const MCOperand &SegReg = MI->getOperand(Op + X86::AddrSegmentReg);
|
2013-07-31 10:00:15 +08:00
|
|
|
|
2012-10-24 06:52:52 +08:00
|
|
|
O << markup("<mem:");
|
2012-10-23 06:31:46 +08:00
|
|
|
|
2010-07-09 07:46:44 +08:00
|
|
|
// If this has a segment register, print it.
|
|
|
|
if (SegReg.getReg()) {
|
2015-03-28 12:25:41 +08:00
|
|
|
printOperand(MI, Op + X86::AddrSegmentReg, O);
|
2010-07-09 07:46:44 +08:00
|
|
|
O << ':';
|
|
|
|
}
|
2013-07-31 10:00:15 +08:00
|
|
|
|
2009-06-20 08:49:26 +08:00
|
|
|
if (DispSpec.isImm()) {
|
|
|
|
int64_t DispVal = DispSpec.getImm();
|
|
|
|
if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
|
2012-12-06 02:13:19 +08:00
|
|
|
O << formatImm(DispVal);
|
2009-06-20 08:49:26 +08:00
|
|
|
} else {
|
2009-09-09 08:40:31 +08:00
|
|
|
assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
|
2015-06-09 08:31:39 +08:00
|
|
|
DispSpec.getExpr()->print(O, &MAI);
|
2009-06-20 08:49:26 +08:00
|
|
|
}
|
2013-07-31 10:00:15 +08:00
|
|
|
|
2009-06-20 08:49:26 +08:00
|
|
|
if (IndexReg.getReg() || BaseReg.getReg()) {
|
|
|
|
O << '(';
|
|
|
|
if (BaseReg.getReg())
|
2015-03-28 12:25:41 +08:00
|
|
|
printOperand(MI, Op + X86::AddrBaseReg, O);
|
2013-07-31 10:00:15 +08:00
|
|
|
|
2009-06-20 08:49:26 +08:00
|
|
|
if (IndexReg.getReg()) {
|
|
|
|
O << ',';
|
2015-03-28 12:25:41 +08:00
|
|
|
printOperand(MI, Op + X86::AddrIndexReg, O);
|
|
|
|
unsigned ScaleVal = MI->getOperand(Op + X86::AddrScaleAmt).getImm();
|
2012-10-23 06:31:46 +08:00
|
|
|
if (ScaleVal != 1) {
|
2015-03-28 12:25:41 +08:00
|
|
|
O << ',' << markup("<imm:") << ScaleVal // never printed in hex.
|
2013-07-31 10:00:15 +08:00
|
|
|
<< markup(">");
|
2012-10-23 06:31:46 +08:00
|
|
|
}
|
2009-06-20 08:49:26 +08:00
|
|
|
}
|
|
|
|
O << ')';
|
|
|
|
}
|
2012-10-23 06:31:46 +08:00
|
|
|
|
2012-10-24 06:52:52 +08:00
|
|
|
O << markup(">");
|
2009-06-20 07:59:57 +08:00
|
|
|
}
|
2013-08-26 06:23:38 +08:00
|
|
|
|
2014-01-22 23:08:08 +08:00
|
|
|
void X86ATTInstPrinter::printSrcIdx(const MCInst *MI, unsigned Op,
|
|
|
|
raw_ostream &O) {
|
2015-03-28 12:25:41 +08:00
|
|
|
const MCOperand &SegReg = MI->getOperand(Op + 1);
|
2014-01-22 23:08:08 +08:00
|
|
|
|
|
|
|
O << markup("<mem:");
|
|
|
|
|
|
|
|
// If this has a segment register, print it.
|
|
|
|
if (SegReg.getReg()) {
|
2015-03-28 12:25:41 +08:00
|
|
|
printOperand(MI, Op + 1, O);
|
2014-01-22 23:08:08 +08:00
|
|
|
O << ':';
|
|
|
|
}
|
|
|
|
|
|
|
|
O << "(";
|
|
|
|
printOperand(MI, Op, O);
|
|
|
|
O << ")";
|
|
|
|
|
|
|
|
O << markup(">");
|
|
|
|
}
|
|
|
|
|
2014-01-22 23:08:21 +08:00
|
|
|
void X86ATTInstPrinter::printDstIdx(const MCInst *MI, unsigned Op,
|
|
|
|
raw_ostream &O) {
|
|
|
|
O << markup("<mem:");
|
|
|
|
|
|
|
|
O << "%es:(";
|
|
|
|
printOperand(MI, Op, O);
|
|
|
|
O << ")";
|
|
|
|
|
|
|
|
O << markup(">");
|
|
|
|
}
|
|
|
|
|
2013-08-26 06:23:38 +08:00
|
|
|
void X86ATTInstPrinter::printMemOffset(const MCInst *MI, unsigned Op,
|
|
|
|
raw_ostream &O) {
|
|
|
|
const MCOperand &DispSpec = MI->getOperand(Op);
|
2015-03-28 12:25:41 +08:00
|
|
|
const MCOperand &SegReg = MI->getOperand(Op + 1);
|
2013-08-26 06:23:38 +08:00
|
|
|
|
|
|
|
O << markup("<mem:");
|
|
|
|
|
2014-01-16 15:36:58 +08:00
|
|
|
// If this has a segment register, print it.
|
|
|
|
if (SegReg.getReg()) {
|
2015-03-28 12:25:41 +08:00
|
|
|
printOperand(MI, Op + 1, O);
|
2014-01-16 15:36:58 +08:00
|
|
|
O << ':';
|
|
|
|
}
|
|
|
|
|
2013-08-26 06:23:38 +08:00
|
|
|
if (DispSpec.isImm()) {
|
|
|
|
O << formatImm(DispSpec.getImm());
|
|
|
|
} else {
|
|
|
|
assert(DispSpec.isExpr() && "non-immediate displacement?");
|
2015-06-09 08:31:39 +08:00
|
|
|
DispSpec.getExpr()->print(O, &MAI);
|
2013-08-26 06:23:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
O << markup(">");
|
|
|
|
}
|
2015-01-23 16:00:59 +08:00
|
|
|
|
|
|
|
void X86ATTInstPrinter::printU8Imm(const MCInst *MI, unsigned Op,
|
|
|
|
raw_ostream &O) {
|
2016-10-20 09:58:34 +08:00
|
|
|
if (MI->getOperand(Op).isExpr())
|
|
|
|
return printOperand(MI, Op, O);
|
|
|
|
|
2015-03-28 12:25:41 +08:00
|
|
|
O << markup("<imm:") << '$' << formatImm(MI->getOperand(Op).getImm() & 0xff)
|
2015-01-23 16:00:59 +08:00
|
|
|
<< markup(">");
|
|
|
|
}
|