blob: 772fe1bf1930054b545f624babec3b764301f0c3 [file] [log] [blame]
Chris Lattnera08186a2009-06-19 00:47:59 +00001//===-- X86ATTInstPrinter.cpp - AT&T 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// This file includes code for rendering MCInst instances as AT&T-style
11// assembly.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "asm-printer"
Chris Lattner1cbd3de2009-09-13 19:30:11 +000016#include "X86ATTInstPrinter.h"
Chris Lattner7a05e6d2010-08-28 20:42:31 +000017#include "X86InstComments.h"
Evan Cheng3ddfbd32011-07-06 22:01:53 +000018#include "MCTargetDesc/X86MCTargetDesc.h"
Chris Lattnera08186a2009-06-19 00:47:59 +000019#include "llvm/MC/MCInst.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000021#include "llvm/MC/MCExpr.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Chris Lattner482bf692010-02-10 00:10:18 +000023#include "llvm/Support/Format.h"
David Greenea31f96c2009-07-14 20:18:05 +000024#include "llvm/Support/FormattedStream.h"
Bill Wendlingbc3f7902011-04-07 21:20:06 +000025#include <map>
Chris Lattnera08186a2009-06-19 00:47:59 +000026using namespace llvm;
27
Chris Lattner8d284c72009-06-19 23:59:57 +000028// Include the auto-generated portion of the assembly writer.
Chris Lattnerb1913c42010-02-11 22:57:32 +000029#define GET_INSTRUCTION_NAME
Bill Wendlingbc3f7902011-04-07 21:20:06 +000030#define PRINT_ALIAS_INSTR
Chris Lattner8d284c72009-06-19 23:59:57 +000031#include "X86GenAsmWriter.inc"
Bill Wendlingbc3f7902011-04-07 21:20:06 +000032
Evan Chengab37af92011-07-06 19:45:42 +000033X86ATTInstPrinter::X86ATTInstPrinter(const MCAsmInfo &MAI)
Bill Wendlingbc3f7902011-04-07 21:20:06 +000034 : MCInstPrinter(MAI) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +000035}
Chris Lattner8d284c72009-06-19 23:59:57 +000036
Rafael Espindolad6860522011-06-02 02:34:55 +000037void X86ATTInstPrinter::printRegName(raw_ostream &OS,
38 unsigned RegNo) const {
39 OS << '%' << getRegisterName(RegNo);
Rafael Espindola08600bc2011-05-30 20:20:15 +000040}
41
Owen Andersona0c3b972011-09-15 23:38:46 +000042void X86ATTInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
43 StringRef Annot) {
Eric Christopher2e3fbaa2011-04-18 21:28:11 +000044 // Try to print any aliases first.
45 if (!printAliasInstr(MI, OS))
Bill Wendling7e07d6f2011-04-14 01:11:51 +000046 printInstruction(MI, OS);
Chris Lattner7a05e6d2010-08-28 20:42:31 +000047
48 // If verbose assembly is enabled, we can print some informative comments.
Owen Andersond1814792011-09-15 18:36:29 +000049 if (CommentStream) {
Owen Anderson69fa8ff2011-09-21 00:25:23 +000050 printAnnotation(OS, Annot);
Chris Lattner7a05e6d2010-08-28 20:42:31 +000051 EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
Owen Andersond1814792011-09-15 18:36:29 +000052 }
Chris Lattner76c564b2010-04-04 04:47:45 +000053}
Bill Wendlingbc3f7902011-04-07 21:20:06 +000054
Chris Lattnerb1913c42010-02-11 22:57:32 +000055StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const {
56 return getInstructionName(Opcode);
57}
58
Chris Lattner76c564b2010-04-04 04:47:45 +000059void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
60 raw_ostream &O) {
Chris Lattnera0022a72009-06-20 07:03:18 +000061 switch (MI->getOperand(Op).getImm()) {
Craig Topper4ed72782012-02-05 05:38:58 +000062 default: llvm_unreachable("Invalid ssecc argument!");
Elena Demikhovsky1adc1d52012-02-08 08:37:26 +000063 case 0: O << "eq"; break;
64 case 1: O << "lt"; break;
65 case 2: O << "le"; break;
66 case 3: O << "unord"; break;
67 case 4: O << "neq"; break;
68 case 5: O << "nlt"; break;
69 case 6: O << "nle"; break;
70 case 7: O << "ord"; break;
71 case 8: O << "eq_uq"; break;
72 case 9: O << "nge"; break;
73 case 0xa: O << "ngt"; break;
74 case 0xb: O << "false"; break;
75 case 0xc: O << "neq_oq"; break;
76 case 0xd: O << "ge"; break;
77 case 0xe: O << "gt"; break;
78 case 0xf: O << "true"; break;
79 case 0x10: O << "eq_os"; break;
80 case 0x11: O << "lt_oq"; break;
81 case 0x12: O << "le_oq"; break;
82 case 0x13: O << "unord_s"; break;
83 case 0x14: O << "neq_us"; break;
84 case 0x15: O << "nlt_uq"; break;
85 case 0x16: O << "nle_uq"; break;
86 case 0x17: O << "ord_s"; break;
87 case 0x18: O << "eq_us"; break;
88 case 0x19: O << "nge_uq"; break;
89 case 0x1a: O << "ngt_uq"; break;
90 case 0x1b: O << "false_os"; break;
91 case 0x1c: O << "neq_os"; break;
92 case 0x1d: O << "ge_oq"; break;
93 case 0x1e: O << "gt_oq"; break;
94 case 0x1f: O << "true_us"; break;
Chris Lattner8d284c72009-06-19 23:59:57 +000095 }
96}
97
Chris Lattner9c211962009-06-20 19:34:09 +000098/// print_pcrel_imm - This is used to print an immediate value that ends up
Chris Lattner6211d7b2009-12-22 00:44:05 +000099/// being encoded as a pc-relative value (e.g. for jumps and calls). These
100/// print slightly differently than normal immediates. For example, a $ is not
101/// emitted.
Chris Lattner76c564b2010-04-04 04:47:45 +0000102void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
103 raw_ostream &O) {
Chris Lattner9c211962009-06-20 19:34:09 +0000104 const MCOperand &Op = MI->getOperand(OpNo);
Chris Lattner9c211962009-06-20 19:34:09 +0000105 if (Op.isImm())
Chris Lattner6211d7b2009-12-22 00:44:05 +0000106 // Print this as a signed 32-bit value.
107 O << (int)Op.getImm();
Chris Lattneraa398f52009-09-14 01:34:40 +0000108 else {
109 assert(Op.isExpr() && "unknown pcrel immediate operand");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000110 O << *Op.getExpr();
Chris Lattneraa398f52009-09-14 01:34:40 +0000111 }
Chris Lattner9c211962009-06-20 19:34:09 +0000112}
113
Chris Lattner76c564b2010-04-04 04:47:45 +0000114void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
115 raw_ostream &O) {
Chris Lattner46820152009-06-20 00:49:26 +0000116 const MCOperand &Op = MI->getOperand(OpNo);
117 if (Op.isReg()) {
Chris Lattner56950c62009-09-13 20:15:16 +0000118 O << '%' << getRegisterName(Op.getReg());
Chris Lattner46820152009-06-20 00:49:26 +0000119 } else if (Op.isImm()) {
Kevin Enderby5b03f722011-09-02 20:01:23 +0000120 // Print X86 immediates as signed values.
121 O << '$' << (int64_t)Op.getImm();
Chris Lattner482bf692010-02-10 00:10:18 +0000122
123 if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256))
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000124 *CommentStream << format("imm = 0x%" PRIX64 "\n", (uint64_t)Op.getImm());
Chris Lattner482bf692010-02-10 00:10:18 +0000125
Chris Lattneraa398f52009-09-14 01:34:40 +0000126 } else {
127 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000128 O << '$' << *Op.getExpr();
Chris Lattner46820152009-06-20 00:49:26 +0000129 }
Chris Lattner8d284c72009-06-19 23:59:57 +0000130}
131
Chris Lattnerf4693072010-07-08 23:46:44 +0000132void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
133 raw_ostream &O) {
Chris Lattner46820152009-06-20 00:49:26 +0000134 const MCOperand &BaseReg = MI->getOperand(Op);
135 const MCOperand &IndexReg = MI->getOperand(Op+2);
136 const MCOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattnerf4693072010-07-08 23:46:44 +0000137 const MCOperand &SegReg = MI->getOperand(Op+4);
138
139 // If this has a segment register, print it.
140 if (SegReg.getReg()) {
141 printOperand(MI, Op+4, O);
142 O << ':';
143 }
Chris Lattner46820152009-06-20 00:49:26 +0000144
Chris Lattner46820152009-06-20 00:49:26 +0000145 if (DispSpec.isImm()) {
146 int64_t DispVal = DispSpec.getImm();
147 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
148 O << DispVal;
149 } else {
Chris Lattner24083062009-09-09 00:40:31 +0000150 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000151 O << *DispSpec.getExpr();
Chris Lattner46820152009-06-20 00:49:26 +0000152 }
153
154 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner46820152009-06-20 00:49:26 +0000155 O << '(';
156 if (BaseReg.getReg())
Chris Lattner76c564b2010-04-04 04:47:45 +0000157 printOperand(MI, Op, O);
Chris Lattner46820152009-06-20 00:49:26 +0000158
159 if (IndexReg.getReg()) {
160 O << ',';
Chris Lattner76c564b2010-04-04 04:47:45 +0000161 printOperand(MI, Op+2, O);
Chris Lattnerb4b5c102009-06-20 08:13:12 +0000162 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
163 if (ScaleVal != 1)
Chris Lattner46820152009-06-20 00:49:26 +0000164 O << ',' << ScaleVal;
165 }
166 O << ')';
167 }
Chris Lattner8d284c72009-06-19 23:59:57 +0000168}