blob: 6ff4442919e8e701633e891519d56f09c301a44d [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"
Bill Wendlingbc3f7902011-04-07 21:20:06 +000018#include "X86Subtarget.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"
Shantonu Sen7ba874b2009-09-18 20:35:59 +000025#include "X86GenInstrNames.inc"
Bill Wendlingbc3f7902011-04-07 21:20:06 +000026#include <map>
Chris Lattnera08186a2009-06-19 00:47:59 +000027using namespace llvm;
28
Chris Lattner8d284c72009-06-19 23:59:57 +000029// Include the auto-generated portion of the assembly writer.
Chris Lattnerb1913c42010-02-11 22:57:32 +000030#define GET_INSTRUCTION_NAME
Bill Wendlingbc3f7902011-04-07 21:20:06 +000031#define PRINT_ALIAS_INSTR
32#include "X86GenRegisterNames.inc"
Chris Lattner8d284c72009-06-19 23:59:57 +000033#include "X86GenAsmWriter.inc"
Bill Wendlingbc3f7902011-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 Lattner8d284c72009-06-19 23:59:57 +000043
Rafael Espindola08600bc2011-05-30 20:20:15 +000044StringRef X86ATTInstPrinter::getRegName(unsigned RegNo) const {
45 return getRegisterName(RegNo);
46}
47
Chris Lattner70129162010-04-04 05:04:31 +000048void X86ATTInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) {
Eric Christopher2e3fbaa2011-04-18 21:28:11 +000049 // Try to print any aliases first.
50 if (!printAliasInstr(MI, OS))
Bill Wendling7e07d6f2011-04-14 01:11:51 +000051 printInstruction(MI, OS);
Chris Lattner7a05e6d2010-08-28 20:42:31 +000052
53 // If verbose assembly is enabled, we can print some informative comments.
54 if (CommentStream)
55 EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
Chris Lattner76c564b2010-04-04 04:47:45 +000056}
Bill Wendlingbc3f7902011-04-07 21:20:06 +000057
Chris Lattnerb1913c42010-02-11 22:57:32 +000058StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const {
59 return getInstructionName(Opcode);
60}
61
Chris Lattner76c564b2010-04-04 04:47:45 +000062void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
63 raw_ostream &O) {
Chris Lattnera0022a72009-06-20 07:03:18 +000064 switch (MI->getOperand(Op).getImm()) {
Chris Lattner76c564b2010-04-04 04:47:45 +000065 default: assert(0 && "Invalid ssecc argument!");
Chris Lattner46820152009-06-20 00:49:26 +000066 case 0: O << "eq"; break;
67 case 1: O << "lt"; break;
68 case 2: O << "le"; break;
69 case 3: O << "unord"; break;
70 case 4: O << "neq"; break;
71 case 5: O << "nlt"; break;
72 case 6: O << "nle"; break;
73 case 7: O << "ord"; break;
Chris Lattner8d284c72009-06-19 23:59:57 +000074 }
75}
76
Chris Lattner9c211962009-06-20 19:34:09 +000077/// print_pcrel_imm - This is used to print an immediate value that ends up
Chris Lattner6211d7b2009-12-22 00:44:05 +000078/// being encoded as a pc-relative value (e.g. for jumps and calls). These
79/// print slightly differently than normal immediates. For example, a $ is not
80/// emitted.
Chris Lattner76c564b2010-04-04 04:47:45 +000081void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
82 raw_ostream &O) {
Chris Lattner9c211962009-06-20 19:34:09 +000083 const MCOperand &Op = MI->getOperand(OpNo);
Chris Lattner9c211962009-06-20 19:34:09 +000084 if (Op.isImm())
Chris Lattner6211d7b2009-12-22 00:44:05 +000085 // Print this as a signed 32-bit value.
86 O << (int)Op.getImm();
Chris Lattneraa398f52009-09-14 01:34:40 +000087 else {
88 assert(Op.isExpr() && "unknown pcrel immediate operand");
Chris Lattnerc8f77172010-01-18 00:37:40 +000089 O << *Op.getExpr();
Chris Lattneraa398f52009-09-14 01:34:40 +000090 }
Chris Lattner9c211962009-06-20 19:34:09 +000091}
92
Chris Lattner76c564b2010-04-04 04:47:45 +000093void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
94 raw_ostream &O) {
Chris Lattner46820152009-06-20 00:49:26 +000095 const MCOperand &Op = MI->getOperand(OpNo);
96 if (Op.isReg()) {
Chris Lattner56950c62009-09-13 20:15:16 +000097 O << '%' << getRegisterName(Op.getReg());
Chris Lattner46820152009-06-20 00:49:26 +000098 } else if (Op.isImm()) {
Chris Lattner24083062009-09-09 00:40:31 +000099 O << '$' << Op.getImm();
Chris Lattner482bf692010-02-10 00:10:18 +0000100
101 if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256))
Dan Gohman12995ba2010-02-17 00:37:20 +0000102 *CommentStream << format("imm = 0x%llX\n", (long long)Op.getImm());
Chris Lattner482bf692010-02-10 00:10:18 +0000103
Chris Lattneraa398f52009-09-14 01:34:40 +0000104 } else {
105 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000106 O << '$' << *Op.getExpr();
Chris Lattner46820152009-06-20 00:49:26 +0000107 }
Chris Lattner8d284c72009-06-19 23:59:57 +0000108}
109
Chris Lattnerf4693072010-07-08 23:46:44 +0000110void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
111 raw_ostream &O) {
Chris Lattner46820152009-06-20 00:49:26 +0000112 const MCOperand &BaseReg = MI->getOperand(Op);
113 const MCOperand &IndexReg = MI->getOperand(Op+2);
114 const MCOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattnerf4693072010-07-08 23:46:44 +0000115 const MCOperand &SegReg = MI->getOperand(Op+4);
116
117 // If this has a segment register, print it.
118 if (SegReg.getReg()) {
119 printOperand(MI, Op+4, O);
120 O << ':';
121 }
Chris Lattner46820152009-06-20 00:49:26 +0000122
Chris Lattner46820152009-06-20 00:49:26 +0000123 if (DispSpec.isImm()) {
124 int64_t DispVal = DispSpec.getImm();
125 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
126 O << DispVal;
127 } else {
Chris Lattner24083062009-09-09 00:40:31 +0000128 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000129 O << *DispSpec.getExpr();
Chris Lattner46820152009-06-20 00:49:26 +0000130 }
131
132 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner46820152009-06-20 00:49:26 +0000133 O << '(';
134 if (BaseReg.getReg())
Chris Lattner76c564b2010-04-04 04:47:45 +0000135 printOperand(MI, Op, O);
Chris Lattner46820152009-06-20 00:49:26 +0000136
137 if (IndexReg.getReg()) {
138 O << ',';
Chris Lattner76c564b2010-04-04 04:47:45 +0000139 printOperand(MI, Op+2, O);
Chris Lattnerb4b5c102009-06-20 08:13:12 +0000140 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
141 if (ScaleVal != 1)
Chris Lattner46820152009-06-20 00:49:26 +0000142 O << ',' << ScaleVal;
143 }
144 O << ')';
145 }
Chris Lattner8d284c72009-06-19 23:59:57 +0000146}