blob: 8ec5b62192e901557fa1fe4a28d7226554d87244 [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"
Shantonu Sen558b79a2009-09-18 20:35:59 +000022#include "X86GenInstrNames.inc"
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 Lattnerc493fb22009-09-14 01:49:26 +000031void X86ATTInstPrinter::printInst(const MCInst *MI) { printInstruction(MI); }
32
Chris Lattnercae05cb2009-09-13 19:30:11 +000033void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op) {
Chris Lattnerc1243062009-06-20 07:03:18 +000034 switch (MI->getOperand(Op).getImm()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000035 default: llvm_unreachable("Invalid ssecc argument!");
Chris Lattnerf38c03af2009-06-20 00:49:26 +000036 case 0: O << "eq"; break;
37 case 1: O << "lt"; break;
38 case 2: O << "le"; break;
39 case 3: O << "unord"; break;
40 case 4: O << "neq"; break;
41 case 5: O << "nlt"; break;
42 case 6: O << "nle"; break;
43 case 7: O << "ord"; break;
Chris Lattnerd5fb7902009-06-19 23:59:57 +000044 }
45}
46
Chris Lattner7680e732009-06-20 19:34:09 +000047/// print_pcrel_imm - This is used to print an immediate value that ends up
48/// being encoded as a pc-relative value. These print slightly differently, for
49/// example, a $ is not emitted.
Chris Lattnercae05cb2009-09-13 19:30:11 +000050void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo) {
Chris Lattner7680e732009-06-20 19:34:09 +000051 const MCOperand &Op = MI->getOperand(OpNo);
Chris Lattner7680e732009-06-20 19:34:09 +000052 if (Op.isImm())
53 O << Op.getImm();
Chris Lattnerf92c95f2009-09-14 01:34:40 +000054 else {
55 assert(Op.isExpr() && "unknown pcrel immediate operand");
Chris Lattnerc493fb22009-09-14 01:49:26 +000056 Op.getExpr()->print(O, &MAI);
Chris Lattnerf92c95f2009-09-14 01:34:40 +000057 }
Chris Lattner7680e732009-06-20 19:34:09 +000058}
59
Chris Lattner172862a2009-10-19 19:51:42 +000060void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo) {
Chris Lattnerf38c03af2009-06-20 00:49:26 +000061
62 const MCOperand &Op = MI->getOperand(OpNo);
63 if (Op.isReg()) {
Chris Lattnerc510f4c2009-09-13 20:15:16 +000064 O << '%' << getRegisterName(Op.getReg());
Chris Lattnerf38c03af2009-06-20 00:49:26 +000065 } else if (Op.isImm()) {
Chris Lattner3de47b82009-09-09 00:40:31 +000066 O << '$' << Op.getImm();
Chris Lattnerf92c95f2009-09-14 01:34:40 +000067 } else {
68 assert(Op.isExpr() && "unknown operand kind in printOperand");
Daniel Dunbar61466c52009-08-14 03:42:12 +000069 O << '$';
Chris Lattnerc493fb22009-09-14 01:49:26 +000070 Op.getExpr()->print(O, &MAI);
Chris Lattnerf38c03af2009-06-20 00:49:26 +000071 }
Chris Lattnerd5fb7902009-06-19 23:59:57 +000072}
73
Chris Lattnercae05cb2009-09-13 19:30:11 +000074void X86ATTInstPrinter::printLeaMemReference(const MCInst *MI, unsigned Op) {
Chris Lattnerf38c03af2009-06-20 00:49:26 +000075 const MCOperand &BaseReg = MI->getOperand(Op);
76 const MCOperand &IndexReg = MI->getOperand(Op+2);
77 const MCOperand &DispSpec = MI->getOperand(Op+3);
78
Chris Lattnerf38c03af2009-06-20 00:49:26 +000079 if (DispSpec.isImm()) {
80 int64_t DispVal = DispSpec.getImm();
81 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
82 O << DispVal;
83 } else {
Chris Lattner3de47b82009-09-09 00:40:31 +000084 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattnerc493fb22009-09-14 01:49:26 +000085 DispSpec.getExpr()->print(O, &MAI);
Chris Lattnerf38c03af2009-06-20 00:49:26 +000086 }
87
88 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnerf38c03af2009-06-20 00:49:26 +000089 O << '(';
90 if (BaseReg.getReg())
Chris Lattnerdc479f62009-06-20 07:59:10 +000091 printOperand(MI, Op);
Chris Lattnerf38c03af2009-06-20 00:49:26 +000092
93 if (IndexReg.getReg()) {
94 O << ',';
Chris Lattnerdc479f62009-06-20 07:59:10 +000095 printOperand(MI, Op+2);
Chris Lattner7f8217f2009-06-20 08:13:12 +000096 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
97 if (ScaleVal != 1)
Chris Lattnerf38c03af2009-06-20 00:49:26 +000098 O << ',' << ScaleVal;
99 }
100 O << ')';
101 }
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000102}
103
Chris Lattnercae05cb2009-09-13 19:30:11 +0000104void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op) {
Chris Lattnerf92c95f2009-09-14 01:34:40 +0000105 // If this has a segment register, print it.
106 if (MI->getOperand(Op+4).getReg()) {
Chris Lattnerc1243062009-06-20 07:03:18 +0000107 printOperand(MI, Op+4);
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000108 O << ':';
109 }
Chris Lattnerc1243062009-06-20 07:03:18 +0000110 printLeaMemReference(MI, Op);
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000111}