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