blob: 68247d2f1a5b5403e3712b8e37667ddca005f84a [file] [log] [blame]
Chris Lattnerfadc83c2009-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 Lattnercae05cb2009-09-13 19:30:11 +000016#include "X86ATTInstPrinter.h"
Chris Lattner6aa928d2010-08-28 20:42:31 +000017#include "X86InstComments.h"
Bill Wendling44dcfd32011-04-07 21:20:06 +000018#include "X86Subtarget.h"
Chris Lattnerfadc83c2009-06-19 00:47:59 +000019#include "llvm/MC/MCInst.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000021#include "llvm/MC/MCExpr.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Chris Lattner5d672cf2010-02-10 00:10:18 +000023#include "llvm/Support/Format.h"
David Greene71847812009-07-14 20:18:05 +000024#include "llvm/Support/FormattedStream.h"
Shantonu Sen558b79a2009-09-18 20:35:59 +000025#include "X86GenInstrNames.inc"
Bill Wendling44dcfd32011-04-07 21:20:06 +000026#include <map>
Chris Lattnerfadc83c2009-06-19 00:47:59 +000027using namespace llvm;
28
Chris Lattnerd5fb7902009-06-19 23:59:57 +000029// Include the auto-generated portion of the assembly writer.
Chris Lattner0d7b0aa2010-02-11 22:57:32 +000030#define GET_INSTRUCTION_NAME
Bill Wendling44dcfd32011-04-07 21:20:06 +000031#define PRINT_ALIAS_INSTR
32#include "X86GenRegisterNames.inc"
Chris Lattnerd5fb7902009-06-19 23:59:57 +000033#include "X86GenAsmWriter.inc"
Bill Wendling44dcfd32011-04-07 21:20:06 +000034#undef PRINT_ALIAS_INSTR
35#undef GET_INSTRUCTION_NAME
36
37X86ATTInstPrinter::X86ATTInstPrinter(TargetMachine &TM, const MCAsmInfo &MAI)
38 : MCInstPrinter(MAI) {
39 // Initialize the set of available features.
40 setAvailableFeatures(ComputeAvailableFeatures(
41 &TM.getSubtarget<X86Subtarget>()));
42}
Chris Lattnerd5fb7902009-06-19 23:59:57 +000043
Rafael Espindolacde4ce42011-06-02 02:34:55 +000044void X86ATTInstPrinter::printRegName(raw_ostream &OS,
45 unsigned RegNo) const {
46 OS << '%' << getRegisterName(RegNo);
Rafael Espindola6e032942011-05-30 20:20:15 +000047}
48
Chris Lattnerd3740872010-04-04 05:04:31 +000049void X86ATTInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) {
Eric Christopher721ef662011-04-18 21:28:11 +000050 // Try to print any aliases first.
51 if (!printAliasInstr(MI, OS))
Bill Wendlingc6df9882011-04-14 01:11:51 +000052 printInstruction(MI, OS);
Chris Lattner6aa928d2010-08-28 20:42:31 +000053
54 // If verbose assembly is enabled, we can print some informative comments.
55 if (CommentStream)
56 EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
Chris Lattner35c33bd2010-04-04 04:47:45 +000057}
Bill Wendling44dcfd32011-04-07 21:20:06 +000058
Chris Lattner0d7b0aa2010-02-11 22:57:32 +000059StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const {
60 return getInstructionName(Opcode);
61}
62
Chris Lattner35c33bd2010-04-04 04:47:45 +000063void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
64 raw_ostream &O) {
Chris Lattnerc1243062009-06-20 07:03:18 +000065 switch (MI->getOperand(Op).getImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +000066 default: assert(0 && "Invalid ssecc argument!");
Chris Lattnerf38c03af2009-06-20 00:49:26 +000067 case 0: O << "eq"; break;
68 case 1: O << "lt"; break;
69 case 2: O << "le"; break;
70 case 3: O << "unord"; break;
71 case 4: O << "neq"; break;
72 case 5: O << "nlt"; break;
73 case 6: O << "nle"; break;
74 case 7: O << "ord"; break;
Chris Lattnerd5fb7902009-06-19 23:59:57 +000075 }
76}
77
Chris Lattner7680e732009-06-20 19:34:09 +000078/// print_pcrel_imm - This is used to print an immediate value that ends up
Chris Lattnerffc05742009-12-22 00:44:05 +000079/// being encoded as a pc-relative value (e.g. for jumps and calls). These
80/// print slightly differently than normal immediates. For example, a $ is not
81/// emitted.
Chris Lattner35c33bd2010-04-04 04:47:45 +000082void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
83 raw_ostream &O) {
Chris Lattner7680e732009-06-20 19:34:09 +000084 const MCOperand &Op = MI->getOperand(OpNo);
Chris Lattner7680e732009-06-20 19:34:09 +000085 if (Op.isImm())
Chris Lattnerffc05742009-12-22 00:44:05 +000086 // Print this as a signed 32-bit value.
87 O << (int)Op.getImm();
Chris Lattnerf92c95f2009-09-14 01:34:40 +000088 else {
89 assert(Op.isExpr() && "unknown pcrel immediate operand");
Chris Lattner8cb9a3b2010-01-18 00:37:40 +000090 O << *Op.getExpr();
Chris Lattnerf92c95f2009-09-14 01:34:40 +000091 }
Chris Lattner7680e732009-06-20 19:34:09 +000092}
93
Chris Lattner35c33bd2010-04-04 04:47:45 +000094void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
95 raw_ostream &O) {
Chris Lattnerf38c03af2009-06-20 00:49:26 +000096 const MCOperand &Op = MI->getOperand(OpNo);
97 if (Op.isReg()) {
Chris Lattnerc510f4c2009-09-13 20:15:16 +000098 O << '%' << getRegisterName(Op.getReg());
Chris Lattnerf38c03af2009-06-20 00:49:26 +000099 } else if (Op.isImm()) {
Chris Lattner3de47b82009-09-09 00:40:31 +0000100 O << '$' << Op.getImm();
Chris Lattner5d672cf2010-02-10 00:10:18 +0000101
102 if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256))
Dan Gohman36b01cb2010-02-17 00:37:20 +0000103 *CommentStream << format("imm = 0x%llX\n", (long long)Op.getImm());
Chris Lattner5d672cf2010-02-10 00:10:18 +0000104
Chris Lattnerf92c95f2009-09-14 01:34:40 +0000105 } else {
106 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattner8cb9a3b2010-01-18 00:37:40 +0000107 O << '$' << *Op.getExpr();
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000108 }
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000109}
110
Chris Lattner599b5312010-07-08 23:46:44 +0000111void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
112 raw_ostream &O) {
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000113 const MCOperand &BaseReg = MI->getOperand(Op);
114 const MCOperand &IndexReg = MI->getOperand(Op+2);
115 const MCOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattner599b5312010-07-08 23:46:44 +0000116 const MCOperand &SegReg = MI->getOperand(Op+4);
117
118 // If this has a segment register, print it.
119 if (SegReg.getReg()) {
120 printOperand(MI, Op+4, O);
121 O << ':';
122 }
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000123
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000124 if (DispSpec.isImm()) {
125 int64_t DispVal = DispSpec.getImm();
126 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
127 O << DispVal;
128 } else {
Chris Lattner3de47b82009-09-09 00:40:31 +0000129 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattner8cb9a3b2010-01-18 00:37:40 +0000130 O << *DispSpec.getExpr();
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000131 }
132
133 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000134 O << '(';
135 if (BaseReg.getReg())
Chris Lattner35c33bd2010-04-04 04:47:45 +0000136 printOperand(MI, Op, O);
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000137
138 if (IndexReg.getReg()) {
139 O << ',';
Chris Lattner35c33bd2010-04-04 04:47:45 +0000140 printOperand(MI, Op+2, O);
Chris Lattner7f8217f2009-06-20 08:13:12 +0000141 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
142 if (ScaleVal != 1)
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000143 O << ',' << ScaleVal;
144 }
145 O << ')';
146 }
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000147}