blob: 8899c6b6e8af73d45fe5a71e7c7035ceed3af250 [file] [log] [blame]
Chris Lattnerd6153b42009-09-20 07:17:49 +00001//===-- X86IntelInstPrinter.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"
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
28#define X86IntelAsmPrinter X86IntelInstPrinter
29#include "X86GenAsmWriter1.inc"
30#undef MachineInstr
31
32void X86IntelInstPrinter::printInst(const MCInst *MI) { printInstruction(MI); }
33
34void X86IntelInstPrinter::printSSECC(const MCInst *MI, unsigned Op) {
35 switch (MI->getOperand(Op).getImm()) {
36 default: llvm_unreachable("Invalid ssecc argument!");
37 case 0: O << "eq"; break;
38 case 1: O << "lt"; break;
39 case 2: O << "le"; break;
40 case 3: O << "unord"; break;
41 case 4: O << "neq"; break;
42 case 5: O << "nlt"; break;
43 case 6: O << "nle"; break;
44 case 7: O << "ord"; break;
45 }
46}
47
48void X86IntelInstPrinter::printPICLabel(const MCInst *MI, unsigned Op) {
49 llvm_unreachable("This is only used for MOVPC32r,"
50 "should lower before instruction printing!");
51}
52
53/// print_pcrel_imm - This is used to print an immediate value that ends up
54/// being encoded as a pc-relative value. These print slightly differently, for
55/// example, a $ is not emitted.
56void X86IntelInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo) {
57 const MCOperand &Op = MI->getOperand(OpNo);
58 if (Op.isImm())
59 O << Op.getImm();
60 else {
61 assert(Op.isExpr() && "unknown pcrel immediate operand");
62 Op.getExpr()->print(O, &MAI);
63 }
64}
65
66static void PrintRegName(raw_ostream &O, StringRef RegName) {
67 for (unsigned i = 0, e = RegName.size(); i != e; ++i)
68 O << (char)toupper(RegName[i]);
69}
70
71void X86IntelInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
72 const char *Modifier) {
73 assert(Modifier == 0 && "Modifiers should not be used");
74
75 const MCOperand &Op = MI->getOperand(OpNo);
76 if (Op.isReg()) {
77 PrintRegName(O, getRegisterName(Op.getReg()));
78 } else if (Op.isImm()) {
79 O << Op.getImm();
80 } else {
81 assert(Op.isExpr() && "unknown operand kind in printOperand");
82 Op.getExpr()->print(O, &MAI);
83 }
84}
85
86void X86IntelInstPrinter::printLeaMemReference(const MCInst *MI, unsigned Op) {
87 const MCOperand &BaseReg = MI->getOperand(Op);
88 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
89 const MCOperand &IndexReg = MI->getOperand(Op+2);
90 const MCOperand &DispSpec = MI->getOperand(Op+3);
91
92 O << '[';
93
94 bool NeedPlus = false;
95 if (BaseReg.getReg()) {
96 printOperand(MI, Op);
97 NeedPlus = true;
98 }
99
100 if (IndexReg.getReg()) {
101 if (NeedPlus) O << " + ";
102 if (ScaleVal != 1)
103 O << ScaleVal << '*';
104 printOperand(MI, Op+2);
105 NeedPlus = true;
106 }
107
108
109 if (!DispSpec.isImm()) {
110 if (NeedPlus) O << " + ";
111 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
112 DispSpec.getExpr()->print(O, &MAI);
113 } else {
114 int64_t DispVal = DispSpec.getImm();
115 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
116 if (NeedPlus) {
117 if (DispVal > 0)
118 O << " + ";
119 else {
120 O << " - ";
121 DispVal = -DispVal;
122 }
123 }
124 O << DispVal;
125 }
126 }
127
128 O << ']';
129}
130
131void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op) {
132 // If this has a segment register, print it.
133 if (MI->getOperand(Op+4).getReg()) {
134 printOperand(MI, Op+4);
135 O << ':';
136 }
137 printLeaMemReference(MI, Op);
138}