blob: 734a545555f92df556dc3647e3de21b50bec9276 [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 Lattnera08186a2009-06-19 00:47:59 +000017#include "llvm/MC/MCInst.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000019#include "llvm/MC/MCExpr.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000020#include "llvm/Support/ErrorHandling.h"
Chris Lattner482bf692010-02-10 00:10:18 +000021#include "llvm/Support/Format.h"
David Greenea31f96c2009-07-14 20:18:05 +000022#include "llvm/Support/FormattedStream.h"
Shantonu Sen7ba874b2009-09-18 20:35:59 +000023#include "X86GenInstrNames.inc"
Chris Lattnera08186a2009-06-19 00:47:59 +000024using namespace llvm;
25
Chris Lattner8d284c72009-06-19 23:59:57 +000026// Include the auto-generated portion of the assembly writer.
27#define MachineInstr MCInst
Chris Lattnerb1913c42010-02-11 22:57:32 +000028#define GET_INSTRUCTION_NAME
Chris Lattner8d284c72009-06-19 23:59:57 +000029#include "X86GenAsmWriter.inc"
30#undef MachineInstr
31
Chris Lattner6d31b432009-09-14 01:49:26 +000032void X86ATTInstPrinter::printInst(const MCInst *MI) { printInstruction(MI); }
Chris Lattnerb1913c42010-02-11 22:57:32 +000033StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const {
34 return getInstructionName(Opcode);
35}
36
Chris Lattner6d31b432009-09-14 01:49:26 +000037
Chris Lattner1cbd3de2009-09-13 19:30:11 +000038void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op) {
Chris Lattnera0022a72009-06-20 07:03:18 +000039 switch (MI->getOperand(Op).getImm()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000040 default: llvm_unreachable("Invalid ssecc argument!");
Chris Lattner46820152009-06-20 00:49:26 +000041 case 0: O << "eq"; break;
42 case 1: O << "lt"; break;
43 case 2: O << "le"; break;
44 case 3: O << "unord"; break;
45 case 4: O << "neq"; break;
46 case 5: O << "nlt"; break;
47 case 6: O << "nle"; break;
48 case 7: O << "ord"; break;
Chris Lattner8d284c72009-06-19 23:59:57 +000049 }
50}
51
Chris Lattner9c211962009-06-20 19:34:09 +000052/// print_pcrel_imm - This is used to print an immediate value that ends up
Chris Lattner6211d7b2009-12-22 00:44:05 +000053/// being encoded as a pc-relative value (e.g. for jumps and calls). These
54/// print slightly differently than normal immediates. For example, a $ is not
55/// emitted.
Chris Lattner1cbd3de2009-09-13 19:30:11 +000056void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo) {
Chris Lattner9c211962009-06-20 19:34:09 +000057 const MCOperand &Op = MI->getOperand(OpNo);
Chris Lattner9c211962009-06-20 19:34:09 +000058 if (Op.isImm())
Chris Lattner6211d7b2009-12-22 00:44:05 +000059 // Print this as a signed 32-bit value.
60 O << (int)Op.getImm();
Chris Lattneraa398f52009-09-14 01:34:40 +000061 else {
62 assert(Op.isExpr() && "unknown pcrel immediate operand");
Chris Lattnerc8f77172010-01-18 00:37:40 +000063 O << *Op.getExpr();
Chris Lattneraa398f52009-09-14 01:34:40 +000064 }
Chris Lattner9c211962009-06-20 19:34:09 +000065}
66
Chris Lattner5db7b6a2009-10-19 19:51:42 +000067void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo) {
Chris Lattner46820152009-06-20 00:49:26 +000068
69 const MCOperand &Op = MI->getOperand(OpNo);
70 if (Op.isReg()) {
Chris Lattner56950c62009-09-13 20:15:16 +000071 O << '%' << getRegisterName(Op.getReg());
Chris Lattner46820152009-06-20 00:49:26 +000072 } else if (Op.isImm()) {
Chris Lattner24083062009-09-09 00:40:31 +000073 O << '$' << Op.getImm();
Chris Lattner482bf692010-02-10 00:10:18 +000074
75 if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256))
Dan Gohman12995ba2010-02-17 00:37:20 +000076 *CommentStream << format("imm = 0x%llX\n", (long long)Op.getImm());
Chris Lattner482bf692010-02-10 00:10:18 +000077
Chris Lattneraa398f52009-09-14 01:34:40 +000078 } else {
79 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattnerc8f77172010-01-18 00:37:40 +000080 O << '$' << *Op.getExpr();
Chris Lattner46820152009-06-20 00:49:26 +000081 }
Chris Lattner8d284c72009-06-19 23:59:57 +000082}
83
Chris Lattner1cbd3de2009-09-13 19:30:11 +000084void X86ATTInstPrinter::printLeaMemReference(const MCInst *MI, unsigned Op) {
Chris Lattner46820152009-06-20 00:49:26 +000085 const MCOperand &BaseReg = MI->getOperand(Op);
86 const MCOperand &IndexReg = MI->getOperand(Op+2);
87 const MCOperand &DispSpec = MI->getOperand(Op+3);
88
Chris Lattner46820152009-06-20 00:49:26 +000089 if (DispSpec.isImm()) {
90 int64_t DispVal = DispSpec.getImm();
91 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
92 O << DispVal;
93 } else {
Chris Lattner24083062009-09-09 00:40:31 +000094 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattnerc8f77172010-01-18 00:37:40 +000095 O << *DispSpec.getExpr();
Chris Lattner46820152009-06-20 00:49:26 +000096 }
97
98 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner46820152009-06-20 00:49:26 +000099 O << '(';
100 if (BaseReg.getReg())
Chris Lattner9fc4db52009-06-20 07:59:10 +0000101 printOperand(MI, Op);
Chris Lattner46820152009-06-20 00:49:26 +0000102
103 if (IndexReg.getReg()) {
104 O << ',';
Chris Lattner9fc4db52009-06-20 07:59:10 +0000105 printOperand(MI, Op+2);
Chris Lattnerb4b5c102009-06-20 08:13:12 +0000106 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
107 if (ScaleVal != 1)
Chris Lattner46820152009-06-20 00:49:26 +0000108 O << ',' << ScaleVal;
109 }
110 O << ')';
111 }
Chris Lattner8d284c72009-06-19 23:59:57 +0000112}
113
Chris Lattner1cbd3de2009-09-13 19:30:11 +0000114void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op) {
Chris Lattneraa398f52009-09-14 01:34:40 +0000115 // If this has a segment register, print it.
116 if (MI->getOperand(Op+4).getReg()) {
Chris Lattnera0022a72009-06-20 07:03:18 +0000117 printOperand(MI, Op+4);
Chris Lattner46820152009-06-20 00:49:26 +0000118 O << ':';
119 }
Chris Lattnera0022a72009-06-20 07:03:18 +0000120 printLeaMemReference(MI, Op);
Chris Lattner8d284c72009-06-19 23:59:57 +0000121}