blob: 4efb5294fb8a6a1412f1d882812d19a041c0b1a8 [file] [log] [blame]
Chris Lattnera7e959d2009-09-20 07:28:26 +00001//===-- X86IntelInstPrinter.cpp - AT&T assembly instruction printing ------===//
Chris Lattnerd6153b42009-09-20 07:17:49 +00002//
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"
16#include "X86IntelInstPrinter.h"
17#include "llvm/MC/MCInst.h"
18#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/FormattedStream.h"
22#include "X86GenInstrNames.inc"
23using namespace llvm;
24
25// Include the auto-generated portion of the assembly writer.
26#define MachineInstr MCInst
27#define NO_ASM_WRITER_BOILERPLATE
Chris Lattnerd6153b42009-09-20 07:17:49 +000028#include "X86GenAsmWriter1.inc"
29#undef MachineInstr
30
31void X86IntelInstPrinter::printInst(const MCInst *MI) { printInstruction(MI); }
32
33void X86IntelInstPrinter::printSSECC(const MCInst *MI, unsigned Op) {
34 switch (MI->getOperand(Op).getImm()) {
35 default: llvm_unreachable("Invalid ssecc argument!");
36 case 0: O << "eq"; break;
37 case 1: O << "lt"; break;
38 case 2: O << "le"; break;
39 case 3: O << "unord"; break;
40 case 4: O << "neq"; break;
41 case 5: O << "nlt"; break;
42 case 6: O << "nle"; break;
43 case 7: O << "ord"; break;
44 }
45}
46
Chris Lattnerd6153b42009-09-20 07:17:49 +000047/// print_pcrel_imm - This is used to print an immediate value that ends up
Chris Lattnerf0544b62009-09-20 07:47:59 +000048/// being encoded as a pc-relative value.
Chris Lattnerd6153b42009-09-20 07:17:49 +000049void X86IntelInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo) {
50 const MCOperand &Op = MI->getOperand(OpNo);
51 if (Op.isImm())
52 O << Op.getImm();
53 else {
54 assert(Op.isExpr() && "unknown pcrel immediate operand");
Chris Lattner1e9a11b2010-01-18 00:37:40 +000055 O << *Op.getExpr();
Chris Lattnerd6153b42009-09-20 07:17:49 +000056 }
57}
58
59static void PrintRegName(raw_ostream &O, StringRef RegName) {
60 for (unsigned i = 0, e = RegName.size(); i != e; ++i)
61 O << (char)toupper(RegName[i]);
62}
63
64void X86IntelInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
65 const char *Modifier) {
66 assert(Modifier == 0 && "Modifiers should not be used");
67
68 const MCOperand &Op = MI->getOperand(OpNo);
69 if (Op.isReg()) {
70 PrintRegName(O, getRegisterName(Op.getReg()));
71 } else if (Op.isImm()) {
72 O << Op.getImm();
73 } else {
74 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattner1e9a11b2010-01-18 00:37:40 +000075 O << *Op.getExpr();
Chris Lattnerd6153b42009-09-20 07:17:49 +000076 }
77}
78
79void X86IntelInstPrinter::printLeaMemReference(const MCInst *MI, unsigned Op) {
80 const MCOperand &BaseReg = MI->getOperand(Op);
81 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
82 const MCOperand &IndexReg = MI->getOperand(Op+2);
83 const MCOperand &DispSpec = MI->getOperand(Op+3);
84
85 O << '[';
86
87 bool NeedPlus = false;
88 if (BaseReg.getReg()) {
89 printOperand(MI, Op);
90 NeedPlus = true;
91 }
92
93 if (IndexReg.getReg()) {
94 if (NeedPlus) O << " + ";
95 if (ScaleVal != 1)
96 O << ScaleVal << '*';
97 printOperand(MI, Op+2);
98 NeedPlus = true;
99 }
100
101
102 if (!DispSpec.isImm()) {
103 if (NeedPlus) O << " + ";
104 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattner1e9a11b2010-01-18 00:37:40 +0000105 O << *DispSpec.getExpr();
Chris Lattnerd6153b42009-09-20 07:17:49 +0000106 } else {
107 int64_t DispVal = DispSpec.getImm();
108 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
109 if (NeedPlus) {
110 if (DispVal > 0)
111 O << " + ";
112 else {
113 O << " - ";
114 DispVal = -DispVal;
115 }
116 }
117 O << DispVal;
118 }
119 }
120
121 O << ']';
122}
123
124void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op) {
125 // If this has a segment register, print it.
126 if (MI->getOperand(Op+4).getReg()) {
127 printOperand(MI, Op+4);
128 O << ':';
129 }
130 printLeaMemReference(MI, Op);
131}