blob: 9841d708d86f7c78b09b5b29de11468853c0b0a8 [file] [log] [blame]
Chris Lattnerb4fa7192009-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 Lattner621c44d2009-08-22 20:48:53 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar6e966212009-08-31 08:08:38 +000019#include "llvm/MC/MCExpr.h"
Edwin Török3cb88482009-07-08 18:01:40 +000020#include "llvm/Support/ErrorHandling.h"
David Greene302008d2009-07-14 20:18:05 +000021#include "llvm/Support/FormattedStream.h"
Chris Lattnerb4fa7192009-06-19 00:47:59 +000022using namespace llvm;
23
Chris Lattnere67184e2009-06-19 23:59:57 +000024// Include the auto-generated portion of the assembly writer.
25#define MachineInstr MCInst
26#define NO_ASM_WRITER_BOILERPLATE
27#include "X86GenAsmWriter.inc"
28#undef MachineInstr
29
30void X86ATTAsmPrinter::printSSECC(const MCInst *MI, unsigned Op) {
Chris Lattnerf5da5902009-06-20 07:03:18 +000031 switch (MI->getOperand(Op).getImm()) {
Edwin Törökbd448e32009-07-14 16:55:14 +000032 default: llvm_unreachable("Invalid ssecc argument!");
Chris Lattner4ebc52f2009-06-20 00:49:26 +000033 case 0: O << "eq"; break;
34 case 1: O << "lt"; break;
35 case 2: O << "le"; break;
36 case 3: O << "unord"; break;
37 case 4: O << "neq"; break;
38 case 5: O << "nlt"; break;
39 case 6: O << "nle"; break;
40 case 7: O << "ord"; break;
Chris Lattnere67184e2009-06-19 23:59:57 +000041 }
42}
43
44
45void X86ATTAsmPrinter::printPICLabel(const MCInst *MI, unsigned Op) {
Edwin Törökbd448e32009-07-14 16:55:14 +000046 llvm_unreachable("This is only used for MOVPC32r,"
Edwin Törökb2de05e2009-07-14 12:22:58 +000047 "should lower before asm printing!");
Chris Lattnere67184e2009-06-19 23:59:57 +000048}
49
50
Chris Lattner357a0ca2009-06-20 19:34:09 +000051/// print_pcrel_imm - This is used to print an immediate value that ends up
52/// being encoded as a pc-relative value. These print slightly differently, for
53/// example, a $ is not emitted.
54void X86ATTAsmPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo) {
55 const MCOperand &Op = MI->getOperand(OpNo);
56
57 if (Op.isImm())
58 O << Op.getImm();
Daniel Dunbar6e966212009-08-31 08:08:38 +000059 else if (Op.isExpr())
Chris Lattner0fe3a1e2009-09-03 05:46:51 +000060 Op.getExpr()->print(O, MAI);
Chris Lattner357a0ca2009-06-20 19:34:09 +000061 else if (Op.isMBBLabel())
62 // FIXME: Keep in sync with printBasicBlockLabel. printBasicBlockLabel
63 // should eventually call into this code, not the other way around.
Chris Lattnera5ef4d32009-08-22 21:43:10 +000064 O << MAI->getPrivateGlobalPrefix() << "BB" << Op.getMBBLabelFunction()
Chris Lattner357a0ca2009-06-20 19:34:09 +000065 << '_' << Op.getMBBLabelBlock();
66 else
Edwin Törökbd448e32009-07-14 16:55:14 +000067 llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattner357a0ca2009-06-20 19:34:09 +000068}
69
70
Chris Lattnere67184e2009-06-19 23:59:57 +000071void X86ATTAsmPrinter::printOperand(const MCInst *MI, unsigned OpNo,
Chris Lattnerdc6fc472009-06-27 04:16:01 +000072 const char *Modifier) {
Chris Lattnerf5da5902009-06-20 07:03:18 +000073 assert(Modifier == 0 && "Modifiers should not be used");
Chris Lattner4ebc52f2009-06-20 00:49:26 +000074
75 const MCOperand &Op = MI->getOperand(OpNo);
76 if (Op.isReg()) {
77 O << '%';
78 unsigned Reg = Op.getReg();
Chris Lattner4ebc52f2009-06-20 00:49:26 +000079 O << TRI->getAsmName(Reg);
80 return;
81 } else if (Op.isImm()) {
Chris Lattner78c95d82009-09-09 00:40:31 +000082 O << '$' << Op.getImm();
Chris Lattner4ebc52f2009-06-20 00:49:26 +000083 return;
Daniel Dunbar6e966212009-08-31 08:08:38 +000084 } else if (Op.isExpr()) {
Daniel Dunbarc7360422009-08-14 03:42:12 +000085 O << '$';
Chris Lattner0fe3a1e2009-09-03 05:46:51 +000086 Op.getExpr()->print(O, MAI);
Daniel Dunbarc7360422009-08-14 03:42:12 +000087 return;
Chris Lattner4ebc52f2009-06-20 00:49:26 +000088 }
89
90 O << "<<UNKNOWN OPERAND KIND>>";
Chris Lattnere67184e2009-06-19 23:59:57 +000091}
92
Chris Lattnerf5da5902009-06-20 07:03:18 +000093void X86ATTAsmPrinter::printLeaMemReference(const MCInst *MI, unsigned Op) {
Chris Lattner4ebc52f2009-06-20 00:49:26 +000094 const MCOperand &BaseReg = MI->getOperand(Op);
95 const MCOperand &IndexReg = MI->getOperand(Op+2);
96 const MCOperand &DispSpec = MI->getOperand(Op+3);
97
Chris Lattner4ebc52f2009-06-20 00:49:26 +000098 if (DispSpec.isImm()) {
99 int64_t DispVal = DispSpec.getImm();
100 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
101 O << DispVal;
102 } else {
Chris Lattner78c95d82009-09-09 00:40:31 +0000103 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
104 DispSpec.getExpr()->print(O, MAI);
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000105 }
106
107 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner6b9f7742009-06-20 07:59:10 +0000108 assert(IndexReg.getReg() != X86::ESP && IndexReg.getReg() != X86::RSP);
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000109
110 O << '(';
111 if (BaseReg.getReg())
Chris Lattner6b9f7742009-06-20 07:59:10 +0000112 printOperand(MI, Op);
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000113
114 if (IndexReg.getReg()) {
115 O << ',';
Chris Lattner6b9f7742009-06-20 07:59:10 +0000116 printOperand(MI, Op+2);
Chris Lattner29754362009-06-20 08:13:12 +0000117 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
118 if (ScaleVal != 1)
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000119 O << ',' << ScaleVal;
120 }
121 O << ')';
122 }
Chris Lattnere67184e2009-06-19 23:59:57 +0000123}
124
Chris Lattnerf5da5902009-06-20 07:03:18 +0000125void X86ATTAsmPrinter::printMemReference(const MCInst *MI, unsigned Op) {
Chris Lattnerea223d82009-06-20 00:50:32 +0000126 const MCOperand &Segment = MI->getOperand(Op+4);
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000127 if (Segment.getReg()) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000128 printOperand(MI, Op+4);
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000129 O << ':';
130 }
Chris Lattnerf5da5902009-06-20 07:03:18 +0000131 printLeaMemReference(MI, Op);
Chris Lattnere67184e2009-06-19 23:59:57 +0000132}