blob: 93f3dc1eff08a3a328c6f7513da87e5fe2ab804d [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 Lattnerfadc83c2009-06-19 00:47:59 +000017#include "llvm/MC/MCInst.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000019#include "llvm/MC/MCExpr.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000020#include "llvm/Support/ErrorHandling.h"
Chris Lattner5d672cf2010-02-10 00:10:18 +000021#include "llvm/Support/Format.h"
David Greene71847812009-07-14 20:18:05 +000022#include "llvm/Support/FormattedStream.h"
Shantonu Sen558b79a2009-09-18 20:35:59 +000023#include "X86GenInstrNames.inc"
Chris Lattnerfadc83c2009-06-19 00:47:59 +000024using namespace llvm;
25
Chris Lattnerd5fb7902009-06-19 23:59:57 +000026// Include the auto-generated portion of the assembly writer.
27#define MachineInstr MCInst
Chris Lattner0d7b0aa2010-02-11 22:57:32 +000028#define GET_INSTRUCTION_NAME
Chris Lattnerd5fb7902009-06-19 23:59:57 +000029#include "X86GenAsmWriter.inc"
30#undef MachineInstr
31
Chris Lattner35c33bd2010-04-04 04:47:45 +000032void X86ATTInstPrinter::printInst(const MCInst *MI) {
33 printInstruction(MI, O);
34}
Chris Lattner0d7b0aa2010-02-11 22:57:32 +000035StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const {
36 return getInstructionName(Opcode);
37}
38
Chris Lattnerc493fb22009-09-14 01:49:26 +000039
Chris Lattner35c33bd2010-04-04 04:47:45 +000040void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
41 raw_ostream &O) {
Chris Lattnerc1243062009-06-20 07:03:18 +000042 switch (MI->getOperand(Op).getImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +000043 default: assert(0 && "Invalid ssecc argument!");
Chris Lattnerf38c03af2009-06-20 00:49:26 +000044 case 0: O << "eq"; break;
45 case 1: O << "lt"; break;
46 case 2: O << "le"; break;
47 case 3: O << "unord"; break;
48 case 4: O << "neq"; break;
49 case 5: O << "nlt"; break;
50 case 6: O << "nle"; break;
51 case 7: O << "ord"; break;
Chris Lattnerd5fb7902009-06-19 23:59:57 +000052 }
53}
54
Chris Lattner7680e732009-06-20 19:34:09 +000055/// print_pcrel_imm - This is used to print an immediate value that ends up
Chris Lattnerffc05742009-12-22 00:44:05 +000056/// being encoded as a pc-relative value (e.g. for jumps and calls). These
57/// print slightly differently than normal immediates. For example, a $ is not
58/// emitted.
Chris Lattner35c33bd2010-04-04 04:47:45 +000059void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
60 raw_ostream &O) {
Chris Lattner7680e732009-06-20 19:34:09 +000061 const MCOperand &Op = MI->getOperand(OpNo);
Chris Lattner7680e732009-06-20 19:34:09 +000062 if (Op.isImm())
Chris Lattnerffc05742009-12-22 00:44:05 +000063 // Print this as a signed 32-bit value.
64 O << (int)Op.getImm();
Chris Lattnerf92c95f2009-09-14 01:34:40 +000065 else {
66 assert(Op.isExpr() && "unknown pcrel immediate operand");
Chris Lattner8cb9a3b2010-01-18 00:37:40 +000067 O << *Op.getExpr();
Chris Lattnerf92c95f2009-09-14 01:34:40 +000068 }
Chris Lattner7680e732009-06-20 19:34:09 +000069}
70
Chris Lattner35c33bd2010-04-04 04:47:45 +000071void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
72 raw_ostream &O) {
Chris Lattnerf38c03af2009-06-20 00:49:26 +000073 const MCOperand &Op = MI->getOperand(OpNo);
74 if (Op.isReg()) {
Chris Lattnerc510f4c2009-09-13 20:15:16 +000075 O << '%' << getRegisterName(Op.getReg());
Chris Lattnerf38c03af2009-06-20 00:49:26 +000076 } else if (Op.isImm()) {
Chris Lattner3de47b82009-09-09 00:40:31 +000077 O << '$' << Op.getImm();
Chris Lattner5d672cf2010-02-10 00:10:18 +000078
79 if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256))
Dan Gohman36b01cb2010-02-17 00:37:20 +000080 *CommentStream << format("imm = 0x%llX\n", (long long)Op.getImm());
Chris Lattner5d672cf2010-02-10 00:10:18 +000081
Chris Lattnerf92c95f2009-09-14 01:34:40 +000082 } else {
83 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattner8cb9a3b2010-01-18 00:37:40 +000084 O << '$' << *Op.getExpr();
Chris Lattnerf38c03af2009-06-20 00:49:26 +000085 }
Chris Lattnerd5fb7902009-06-19 23:59:57 +000086}
87
Chris Lattner35c33bd2010-04-04 04:47:45 +000088void X86ATTInstPrinter::printLeaMemReference(const MCInst *MI, unsigned Op,
89 raw_ostream &O) {
Chris Lattnerf38c03af2009-06-20 00:49:26 +000090 const MCOperand &BaseReg = MI->getOperand(Op);
91 const MCOperand &IndexReg = MI->getOperand(Op+2);
92 const MCOperand &DispSpec = MI->getOperand(Op+3);
93
Chris Lattnerf38c03af2009-06-20 00:49:26 +000094 if (DispSpec.isImm()) {
95 int64_t DispVal = DispSpec.getImm();
96 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
97 O << DispVal;
98 } else {
Chris Lattner3de47b82009-09-09 00:40:31 +000099 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattner8cb9a3b2010-01-18 00:37:40 +0000100 O << *DispSpec.getExpr();
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000101 }
102
103 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000104 O << '(';
105 if (BaseReg.getReg())
Chris Lattner35c33bd2010-04-04 04:47:45 +0000106 printOperand(MI, Op, O);
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000107
108 if (IndexReg.getReg()) {
109 O << ',';
Chris Lattner35c33bd2010-04-04 04:47:45 +0000110 printOperand(MI, Op+2, O);
Chris Lattner7f8217f2009-06-20 08:13:12 +0000111 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
112 if (ScaleVal != 1)
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000113 O << ',' << ScaleVal;
114 }
115 O << ')';
116 }
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000117}
118
Chris Lattner35c33bd2010-04-04 04:47:45 +0000119void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
120 raw_ostream &O) {
Chris Lattnerf92c95f2009-09-14 01:34:40 +0000121 // If this has a segment register, print it.
122 if (MI->getOperand(Op+4).getReg()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000123 printOperand(MI, Op+4, O);
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000124 O << ':';
125 }
Chris Lattner35c33bd2010-04-04 04:47:45 +0000126 printLeaMemReference(MI, Op, O);
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000127}