blob: d989f60a5952831c5f5c099342276eb1d712fb93 [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"
Chris Lattnercae05cb2009-09-13 19:30:11 +000016#include "X86ATTInstPrinter.h"
Chris Lattnerfadc83c2009-06-19 00:47:59 +000017#include "llvm/MC/MCInst.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000019#include "llvm/MC/MCExpr.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000020#include "llvm/Support/ErrorHandling.h"
David Greene71847812009-07-14 20:18:05 +000021#include "llvm/Support/FormattedStream.h"
Chris Lattnercae05cb2009-09-13 19:30:11 +000022#include "llvm/Target/TargetRegisterInfo.h" // FIXME: REMOVE.
Chris Lattnerfadc83c2009-06-19 00:47:59 +000023using namespace llvm;
24
Chris Lattnerd5fb7902009-06-19 23:59:57 +000025// Include the auto-generated portion of the assembly writer.
26#define MachineInstr MCInst
27#define NO_ASM_WRITER_BOILERPLATE
28#include "X86GenAsmWriter.inc"
29#undef MachineInstr
30
Chris Lattnercae05cb2009-09-13 19:30:11 +000031void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op) {
Chris Lattnerc1243062009-06-20 07:03:18 +000032 switch (MI->getOperand(Op).getImm()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000033 default: llvm_unreachable("Invalid ssecc argument!");
Chris Lattnerf38c03af2009-06-20 00:49:26 +000034 case 0: O << "eq"; break;
35 case 1: O << "lt"; break;
36 case 2: O << "le"; break;
37 case 3: O << "unord"; break;
38 case 4: O << "neq"; break;
39 case 5: O << "nlt"; break;
40 case 6: O << "nle"; break;
41 case 7: O << "ord"; break;
Chris Lattnerd5fb7902009-06-19 23:59:57 +000042 }
43}
44
45
Chris Lattnercae05cb2009-09-13 19:30:11 +000046void X86ATTInstPrinter::printPICLabel(const MCInst *MI, unsigned Op) {
Torok Edwinc23197a2009-07-14 16:55:14 +000047 llvm_unreachable("This is only used for MOVPC32r,"
Chris Lattnercae05cb2009-09-13 19:30:11 +000048 "should lower before instruction printing!");
Chris Lattnerd5fb7902009-06-19 23:59:57 +000049}
50
51
Chris Lattner7680e732009-06-20 19:34:09 +000052/// print_pcrel_imm - This is used to print an immediate value that ends up
53/// being encoded as a pc-relative value. These print slightly differently, for
54/// example, a $ is not emitted.
Chris Lattnercae05cb2009-09-13 19:30:11 +000055void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo) {
Chris Lattner7680e732009-06-20 19:34:09 +000056 const MCOperand &Op = MI->getOperand(OpNo);
57
58 if (Op.isImm())
59 O << Op.getImm();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000060 else if (Op.isExpr())
Chris Lattner684c593d2009-09-03 05:46:51 +000061 Op.getExpr()->print(O, MAI);
Chris Lattner7680e732009-06-20 19:34:09 +000062 else
Torok Edwinc23197a2009-07-14 16:55:14 +000063 llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattner7680e732009-06-20 19:34:09 +000064}
65
66
Chris Lattnercae05cb2009-09-13 19:30:11 +000067void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
Chris Lattner18c59872009-06-27 04:16:01 +000068 const char *Modifier) {
Chris Lattnerc1243062009-06-20 07:03:18 +000069 assert(Modifier == 0 && "Modifiers should not be used");
Chris Lattnerf38c03af2009-06-20 00:49:26 +000070
71 const MCOperand &Op = MI->getOperand(OpNo);
72 if (Op.isReg()) {
Chris Lattnera1cb09e2009-09-13 19:44:38 +000073 O << '%' << TRI->getAsmName(Op.getReg());
Chris Lattnerf38c03af2009-06-20 00:49:26 +000074 return;
75 } else if (Op.isImm()) {
Chris Lattner3de47b82009-09-09 00:40:31 +000076 O << '$' << Op.getImm();
Chris Lattnerf38c03af2009-06-20 00:49:26 +000077 return;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000078 } else if (Op.isExpr()) {
Daniel Dunbar61466c52009-08-14 03:42:12 +000079 O << '$';
Chris Lattner684c593d2009-09-03 05:46:51 +000080 Op.getExpr()->print(O, MAI);
Daniel Dunbar61466c52009-08-14 03:42:12 +000081 return;
Chris Lattnerf38c03af2009-06-20 00:49:26 +000082 }
83
84 O << "<<UNKNOWN OPERAND KIND>>";
Chris Lattnerd5fb7902009-06-19 23:59:57 +000085}
86
Chris Lattnercae05cb2009-09-13 19:30:11 +000087void X86ATTInstPrinter::printLeaMemReference(const MCInst *MI, unsigned Op) {
Chris Lattnerf38c03af2009-06-20 00:49:26 +000088 const MCOperand &BaseReg = MI->getOperand(Op);
89 const MCOperand &IndexReg = MI->getOperand(Op+2);
90 const MCOperand &DispSpec = MI->getOperand(Op+3);
91
Chris Lattnerf38c03af2009-06-20 00:49:26 +000092 if (DispSpec.isImm()) {
93 int64_t DispVal = DispSpec.getImm();
94 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
95 O << DispVal;
96 } else {
Chris Lattner3de47b82009-09-09 00:40:31 +000097 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
98 DispSpec.getExpr()->print(O, MAI);
Chris Lattnerf38c03af2009-06-20 00:49:26 +000099 }
100
101 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000102 O << '(';
103 if (BaseReg.getReg())
Chris Lattnerdc479f62009-06-20 07:59:10 +0000104 printOperand(MI, Op);
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000105
106 if (IndexReg.getReg()) {
107 O << ',';
Chris Lattnerdc479f62009-06-20 07:59:10 +0000108 printOperand(MI, Op+2);
Chris Lattner7f8217f2009-06-20 08:13:12 +0000109 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
110 if (ScaleVal != 1)
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000111 O << ',' << ScaleVal;
112 }
113 O << ')';
114 }
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000115}
116
Chris Lattnercae05cb2009-09-13 19:30:11 +0000117void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op) {
Chris Lattnerad48be02009-06-20 00:50:32 +0000118 const MCOperand &Segment = MI->getOperand(Op+4);
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000119 if (Segment.getReg()) {
Chris Lattnerc1243062009-06-20 07:03:18 +0000120 printOperand(MI, Op+4);
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000121 O << ':';
122 }
Chris Lattnerc1243062009-06-20 07:03:18 +0000123 printLeaMemReference(MI, Op);
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000124}