blob: a25fb2e0e31f68bb9cf91bbb5d55acc55d064a6a [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 Lattnerfadc83c2009-06-19 00:47:59 +000022using namespace llvm;
23
Chris Lattnerd5fb7902009-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
Chris Lattnercae05cb2009-09-13 19:30:11 +000030void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op) {
Chris Lattnerc1243062009-06-20 07:03:18 +000031 switch (MI->getOperand(Op).getImm()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000032 default: llvm_unreachable("Invalid ssecc argument!");
Chris Lattnerf38c03af2009-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 Lattnerd5fb7902009-06-19 23:59:57 +000041 }
42}
43
44
Chris Lattnercae05cb2009-09-13 19:30:11 +000045void X86ATTInstPrinter::printPICLabel(const MCInst *MI, unsigned Op) {
Torok Edwinc23197a2009-07-14 16:55:14 +000046 llvm_unreachable("This is only used for MOVPC32r,"
Chris Lattnercae05cb2009-09-13 19:30:11 +000047 "should lower before instruction printing!");
Chris Lattnerd5fb7902009-06-19 23:59:57 +000048}
49
50
Chris Lattner7680e732009-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.
Chris Lattnercae05cb2009-09-13 19:30:11 +000054void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo) {
Chris Lattner7680e732009-06-20 19:34:09 +000055 const MCOperand &Op = MI->getOperand(OpNo);
56
57 if (Op.isImm())
58 O << Op.getImm();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000059 else if (Op.isExpr())
Chris Lattner684c593d2009-09-03 05:46:51 +000060 Op.getExpr()->print(O, MAI);
Chris Lattner7680e732009-06-20 19:34:09 +000061 else
Torok Edwinc23197a2009-07-14 16:55:14 +000062 llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattner7680e732009-06-20 19:34:09 +000063}
64
65
Chris Lattnercae05cb2009-09-13 19:30:11 +000066void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
Chris Lattner18c59872009-06-27 04:16:01 +000067 const char *Modifier) {
Chris Lattnerc1243062009-06-20 07:03:18 +000068 assert(Modifier == 0 && "Modifiers should not be used");
Chris Lattnerf38c03af2009-06-20 00:49:26 +000069
70 const MCOperand &Op = MI->getOperand(OpNo);
71 if (Op.isReg()) {
Chris Lattnerc510f4c2009-09-13 20:15:16 +000072 O << '%' << getRegisterName(Op.getReg());
Chris Lattnerf38c03af2009-06-20 00:49:26 +000073 return;
74 } else if (Op.isImm()) {
Chris Lattner3de47b82009-09-09 00:40:31 +000075 O << '$' << Op.getImm();
Chris Lattnerf38c03af2009-06-20 00:49:26 +000076 return;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000077 } else if (Op.isExpr()) {
Daniel Dunbar61466c52009-08-14 03:42:12 +000078 O << '$';
Chris Lattner684c593d2009-09-03 05:46:51 +000079 Op.getExpr()->print(O, MAI);
Daniel Dunbar61466c52009-08-14 03:42:12 +000080 return;
Chris Lattnerf38c03af2009-06-20 00:49:26 +000081 }
82
83 O << "<<UNKNOWN OPERAND KIND>>";
Chris Lattnerd5fb7902009-06-19 23:59:57 +000084}
85
Chris Lattnercae05cb2009-09-13 19:30:11 +000086void X86ATTInstPrinter::printLeaMemReference(const MCInst *MI, unsigned Op) {
Chris Lattnerf38c03af2009-06-20 00:49:26 +000087 const MCOperand &BaseReg = MI->getOperand(Op);
88 const MCOperand &IndexReg = MI->getOperand(Op+2);
89 const MCOperand &DispSpec = MI->getOperand(Op+3);
90
Chris Lattnerf38c03af2009-06-20 00:49:26 +000091 if (DispSpec.isImm()) {
92 int64_t DispVal = DispSpec.getImm();
93 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
94 O << DispVal;
95 } else {
Chris Lattner3de47b82009-09-09 00:40:31 +000096 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
97 DispSpec.getExpr()->print(O, MAI);
Chris Lattnerf38c03af2009-06-20 00:49:26 +000098 }
99
100 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000101 O << '(';
102 if (BaseReg.getReg())
Chris Lattnerdc479f62009-06-20 07:59:10 +0000103 printOperand(MI, Op);
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000104
105 if (IndexReg.getReg()) {
106 O << ',';
Chris Lattnerdc479f62009-06-20 07:59:10 +0000107 printOperand(MI, Op+2);
Chris Lattner7f8217f2009-06-20 08:13:12 +0000108 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
109 if (ScaleVal != 1)
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000110 O << ',' << ScaleVal;
111 }
112 O << ')';
113 }
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000114}
115
Chris Lattnercae05cb2009-09-13 19:30:11 +0000116void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op) {
Chris Lattnerad48be02009-06-20 00:50:32 +0000117 const MCOperand &Segment = MI->getOperand(Op+4);
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000118 if (Segment.getReg()) {
Chris Lattnerc1243062009-06-20 07:03:18 +0000119 printOperand(MI, Op+4);
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000120 O << ':';
121 }
Chris Lattnerc1243062009-06-20 07:03:18 +0000122 printLeaMemReference(MI, Op);
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000123}