blob: e0fa83a78bf1c2aed644bb1604772626d0462ea7 [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"
16#include "llvm/MC/MCInst.h"
17#include "X86ATTAsmPrinter.h"
Chris Lattnerc1243062009-06-20 07:03:18 +000018#include "llvm/Target/TargetAsmInfo.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000019#include "llvm/Support/ErrorHandling.h"
Chris Lattnerd5fb7902009-06-19 23:59:57 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattnerfadc83c2009-06-19 00:47:59 +000021using namespace llvm;
22
Chris Lattnerd5fb7902009-06-19 23:59:57 +000023// Include the auto-generated portion of the assembly writer.
24#define MachineInstr MCInst
25#define NO_ASM_WRITER_BOILERPLATE
26#include "X86GenAsmWriter.inc"
27#undef MachineInstr
28
29void X86ATTAsmPrinter::printSSECC(const MCInst *MI, unsigned Op) {
Chris Lattnerc1243062009-06-20 07:03:18 +000030 switch (MI->getOperand(Op).getImm()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000031 default: llvm_unreachable("Invalid ssecc argument!");
Chris Lattnerf38c03af2009-06-20 00:49:26 +000032 case 0: O << "eq"; break;
33 case 1: O << "lt"; break;
34 case 2: O << "le"; break;
35 case 3: O << "unord"; break;
36 case 4: O << "neq"; break;
37 case 5: O << "nlt"; break;
38 case 6: O << "nle"; break;
39 case 7: O << "ord"; break;
Chris Lattnerd5fb7902009-06-19 23:59:57 +000040 }
41}
42
43
44void X86ATTAsmPrinter::printPICLabel(const MCInst *MI, unsigned Op) {
Torok Edwinc23197a2009-07-14 16:55:14 +000045 llvm_unreachable("This is only used for MOVPC32r,"
Torok Edwin481d15a2009-07-14 12:22:58 +000046 "should lower before asm printing!");
Chris Lattnerd5fb7902009-06-19 23:59:57 +000047}
48
49
Chris Lattner7680e732009-06-20 19:34:09 +000050/// print_pcrel_imm - This is used to print an immediate value that ends up
51/// being encoded as a pc-relative value. These print slightly differently, for
52/// example, a $ is not emitted.
53void X86ATTAsmPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo) {
54 const MCOperand &Op = MI->getOperand(OpNo);
55
56 if (Op.isImm())
57 O << Op.getImm();
58 else if (Op.isMBBLabel())
59 // FIXME: Keep in sync with printBasicBlockLabel. printBasicBlockLabel
60 // should eventually call into this code, not the other way around.
61 O << TAI->getPrivateGlobalPrefix() << "BB" << Op.getMBBLabelFunction()
62 << '_' << Op.getMBBLabelBlock();
63 else
Torok Edwinc23197a2009-07-14 16:55:14 +000064 llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattner7680e732009-06-20 19:34:09 +000065}
66
67
Chris Lattnerd5fb7902009-06-19 23:59:57 +000068void X86ATTAsmPrinter::printOperand(const MCInst *MI, unsigned OpNo,
Chris Lattner18c59872009-06-27 04:16:01 +000069 const char *Modifier) {
Chris Lattnerc1243062009-06-20 07:03:18 +000070 assert(Modifier == 0 && "Modifiers should not be used");
Chris Lattnerf38c03af2009-06-20 00:49:26 +000071
72 const MCOperand &Op = MI->getOperand(OpNo);
73 if (Op.isReg()) {
74 O << '%';
75 unsigned Reg = Op.getReg();
76#if 0
77 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
78 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
79 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
80 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
81 Reg = getX86SubSuperRegister(Reg, VT);
82 }
83#endif
84 O << TRI->getAsmName(Reg);
85 return;
86 } else if (Op.isImm()) {
Chris Lattner2f429e52009-06-21 01:48:49 +000087 //if (!Modifier || (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
Chris Lattnerf38c03af2009-06-20 00:49:26 +000088 O << '$';
89 O << Op.getImm();
90 return;
91 }
92
93 O << "<<UNKNOWN OPERAND KIND>>";
Chris Lattnerd5fb7902009-06-19 23:59:57 +000094}
95
Chris Lattnerc1243062009-06-20 07:03:18 +000096void X86ATTAsmPrinter::printLeaMemReference(const MCInst *MI, unsigned Op) {
Chris Lattnerc1243062009-06-20 07:03:18 +000097
Chris Lattnerf38c03af2009-06-20 00:49:26 +000098 const MCOperand &BaseReg = MI->getOperand(Op);
99 const MCOperand &IndexReg = MI->getOperand(Op+2);
100 const MCOperand &DispSpec = MI->getOperand(Op+3);
101
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000102 if (DispSpec.isImm()) {
103 int64_t DispVal = DispSpec.getImm();
104 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
105 O << DispVal;
106 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000107 llvm_unreachable("non-immediate displacement for LEA?");
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000108 //assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
109 // DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattner18c59872009-06-27 04:16:01 +0000110 //printOperand(MI, Op+3, "mem");
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000111 }
112
113 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000114 // There are cases where we can end up with ESP/RSP in the indexreg slot.
115 // If this happens, swap the base/index register to support assemblers that
116 // don't work when the index is *SP.
117 // FIXME: REMOVE THIS.
Chris Lattnerdc479f62009-06-20 07:59:10 +0000118 assert(IndexReg.getReg() != X86::ESP && IndexReg.getReg() != X86::RSP);
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000119
120 O << '(';
121 if (BaseReg.getReg())
Chris Lattnerdc479f62009-06-20 07:59:10 +0000122 printOperand(MI, Op);
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000123
124 if (IndexReg.getReg()) {
125 O << ',';
Chris Lattnerdc479f62009-06-20 07:59:10 +0000126 printOperand(MI, Op+2);
Chris Lattner7f8217f2009-06-20 08:13:12 +0000127 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
128 if (ScaleVal != 1)
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000129 O << ',' << ScaleVal;
130 }
131 O << ')';
132 }
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000133}
134
Chris Lattnerc1243062009-06-20 07:03:18 +0000135void X86ATTAsmPrinter::printMemReference(const MCInst *MI, unsigned Op) {
Chris Lattnerad48be02009-06-20 00:50:32 +0000136 const MCOperand &Segment = MI->getOperand(Op+4);
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000137 if (Segment.getReg()) {
Chris Lattnerc1243062009-06-20 07:03:18 +0000138 printOperand(MI, Op+4);
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000139 O << ':';
140 }
Chris Lattnerc1243062009-06-20 07:03:18 +0000141 printLeaMemReference(MI, Op);
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000142}