blob: 76a1da49595f6638fbc8f7d8f3fb5067b3ee3fad [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
Chris Lattner70129162010-04-04 05:04:31 +000042void X86ATTInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) {
Eric Christopher2e3fbaa2011-04-18 21:28:11 +000043 // Try to print any aliases first.
44 if (!printAliasInstr(MI, OS))
Bill Wendling7e07d6f2011-04-14 01:11:51 +000045 printInstruction(MI, OS);
Chris Lattner7a05e6d2010-08-28 20:42:31 +000046
47 // If verbose assembly is enabled, we can print some informative comments.
Owen Andersond1814792011-09-15 18:36:29 +000048 if (CommentStream) {
49 printAnnotations(MI, *CommentStream);
Chris Lattner7a05e6d2010-08-28 20:42:31 +000050 EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
Owen Andersond1814792011-09-15 18:36:29 +000051 }
Chris Lattner76c564b2010-04-04 04:47:45 +000052}
Bill Wendlingbc3f7902011-04-07 21:20:06 +000053
Chris Lattnerb1913c42010-02-11 22:57:32 +000054StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const {
55 return getInstructionName(Opcode);
56}
57
Chris Lattner76c564b2010-04-04 04:47:45 +000058void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
59 raw_ostream &O) {
Chris Lattnera0022a72009-06-20 07:03:18 +000060 switch (MI->getOperand(Op).getImm()) {
Chris Lattner76c564b2010-04-04 04:47:45 +000061 default: assert(0 && "Invalid ssecc argument!");
Chris Lattner46820152009-06-20 00:49:26 +000062 case 0: O << "eq"; break;
63 case 1: O << "lt"; break;
64 case 2: O << "le"; break;
65 case 3: O << "unord"; break;
66 case 4: O << "neq"; break;
67 case 5: O << "nlt"; break;
68 case 6: O << "nle"; break;
69 case 7: O << "ord"; break;
Chris Lattner8d284c72009-06-19 23:59:57 +000070 }
71}
72
Chris Lattner9c211962009-06-20 19:34:09 +000073/// print_pcrel_imm - This is used to print an immediate value that ends up
Chris Lattner6211d7b2009-12-22 00:44:05 +000074/// being encoded as a pc-relative value (e.g. for jumps and calls). These
75/// print slightly differently than normal immediates. For example, a $ is not
76/// emitted.
Chris Lattner76c564b2010-04-04 04:47:45 +000077void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
78 raw_ostream &O) {
Chris Lattner9c211962009-06-20 19:34:09 +000079 const MCOperand &Op = MI->getOperand(OpNo);
Chris Lattner9c211962009-06-20 19:34:09 +000080 if (Op.isImm())
Chris Lattner6211d7b2009-12-22 00:44:05 +000081 // Print this as a signed 32-bit value.
82 O << (int)Op.getImm();
Chris Lattneraa398f52009-09-14 01:34:40 +000083 else {
84 assert(Op.isExpr() && "unknown pcrel immediate operand");
Chris Lattnerc8f77172010-01-18 00:37:40 +000085 O << *Op.getExpr();
Chris Lattneraa398f52009-09-14 01:34:40 +000086 }
Chris Lattner9c211962009-06-20 19:34:09 +000087}
88
Chris Lattner76c564b2010-04-04 04:47:45 +000089void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
90 raw_ostream &O) {
Chris Lattner46820152009-06-20 00:49:26 +000091 const MCOperand &Op = MI->getOperand(OpNo);
92 if (Op.isReg()) {
Chris Lattner56950c62009-09-13 20:15:16 +000093 O << '%' << getRegisterName(Op.getReg());
Chris Lattner46820152009-06-20 00:49:26 +000094 } else if (Op.isImm()) {
Kevin Enderby5b03f722011-09-02 20:01:23 +000095 // Print X86 immediates as signed values.
96 O << '$' << (int64_t)Op.getImm();
Chris Lattner482bf692010-02-10 00:10:18 +000097
98 if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256))
Dan Gohman12995ba2010-02-17 00:37:20 +000099 *CommentStream << format("imm = 0x%llX\n", (long long)Op.getImm());
Chris Lattner482bf692010-02-10 00:10:18 +0000100
Chris Lattneraa398f52009-09-14 01:34:40 +0000101 } else {
102 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000103 O << '$' << *Op.getExpr();
Chris Lattner46820152009-06-20 00:49:26 +0000104 }
Chris Lattner8d284c72009-06-19 23:59:57 +0000105}
106
Chris Lattnerf4693072010-07-08 23:46:44 +0000107void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
108 raw_ostream &O) {
Chris Lattner46820152009-06-20 00:49:26 +0000109 const MCOperand &BaseReg = MI->getOperand(Op);
110 const MCOperand &IndexReg = MI->getOperand(Op+2);
111 const MCOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattnerf4693072010-07-08 23:46:44 +0000112 const MCOperand &SegReg = MI->getOperand(Op+4);
113
114 // If this has a segment register, print it.
115 if (SegReg.getReg()) {
116 printOperand(MI, Op+4, O);
117 O << ':';
118 }
Chris Lattner46820152009-06-20 00:49:26 +0000119
Chris Lattner46820152009-06-20 00:49:26 +0000120 if (DispSpec.isImm()) {
121 int64_t DispVal = DispSpec.getImm();
122 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
123 O << DispVal;
124 } else {
Chris Lattner24083062009-09-09 00:40:31 +0000125 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000126 O << *DispSpec.getExpr();
Chris Lattner46820152009-06-20 00:49:26 +0000127 }
128
129 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner46820152009-06-20 00:49:26 +0000130 O << '(';
131 if (BaseReg.getReg())
Chris Lattner76c564b2010-04-04 04:47:45 +0000132 printOperand(MI, Op, O);
Chris Lattner46820152009-06-20 00:49:26 +0000133
134 if (IndexReg.getReg()) {
135 O << ',';
Chris Lattner76c564b2010-04-04 04:47:45 +0000136 printOperand(MI, Op+2, O);
Chris Lattnerb4b5c102009-06-20 08:13:12 +0000137 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
138 if (ScaleVal != 1)
Chris Lattner46820152009-06-20 00:49:26 +0000139 O << ',' << ScaleVal;
140 }
141 O << ')';
142 }
Chris Lattner8d284c72009-06-19 23:59:57 +0000143}