blob: c74b97af308d274bad1a640ec45076a9589d4480 [file] [log] [blame]
Chris Lattnera08186a2009-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 Lattner1cbd3de2009-09-13 19:30:11 +000016#include "X86ATTInstPrinter.h"
Chris Lattnera08186a2009-06-19 00:47:59 +000017#include "llvm/MC/MCInst.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000019#include "llvm/MC/MCExpr.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000020#include "llvm/Support/ErrorHandling.h"
David Greenea31f96c2009-07-14 20:18:05 +000021#include "llvm/Support/FormattedStream.h"
Shantonu Sen7ba874b2009-09-18 20:35:59 +000022#include "X86GenInstrNames.inc"
Chris Lattnera08186a2009-06-19 00:47:59 +000023using namespace llvm;
24
Chris Lattner8d284c72009-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 Lattner6d31b432009-09-14 01:49:26 +000031void X86ATTInstPrinter::printInst(const MCInst *MI) { printInstruction(MI); }
32
Chris Lattner1cbd3de2009-09-13 19:30:11 +000033void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op) {
Chris Lattnera0022a72009-06-20 07:03:18 +000034 switch (MI->getOperand(Op).getImm()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000035 default: llvm_unreachable("Invalid ssecc argument!");
Chris Lattner46820152009-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 Lattner8d284c72009-06-19 23:59:57 +000044 }
45}
46
Chris Lattner9c211962009-06-20 19:34:09 +000047/// print_pcrel_imm - This is used to print an immediate value that ends up
Chris Lattner6211d7b2009-12-22 00:44:05 +000048/// being encoded as a pc-relative value (e.g. for jumps and calls). These
49/// print slightly differently than normal immediates. For example, a $ is not
50/// emitted.
Chris Lattner1cbd3de2009-09-13 19:30:11 +000051void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo) {
Chris Lattner9c211962009-06-20 19:34:09 +000052 const MCOperand &Op = MI->getOperand(OpNo);
Chris Lattner9c211962009-06-20 19:34:09 +000053 if (Op.isImm())
Chris Lattner6211d7b2009-12-22 00:44:05 +000054 // Print this as a signed 32-bit value.
55 O << (int)Op.getImm();
Chris Lattneraa398f52009-09-14 01:34:40 +000056 else {
57 assert(Op.isExpr() && "unknown pcrel immediate operand");
Chris Lattner6d31b432009-09-14 01:49:26 +000058 Op.getExpr()->print(O, &MAI);
Chris Lattneraa398f52009-09-14 01:34:40 +000059 }
Chris Lattner9c211962009-06-20 19:34:09 +000060}
61
Chris Lattner5db7b6a2009-10-19 19:51:42 +000062void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo) {
Chris Lattner46820152009-06-20 00:49:26 +000063
64 const MCOperand &Op = MI->getOperand(OpNo);
65 if (Op.isReg()) {
Chris Lattner56950c62009-09-13 20:15:16 +000066 O << '%' << getRegisterName(Op.getReg());
Chris Lattner46820152009-06-20 00:49:26 +000067 } else if (Op.isImm()) {
Chris Lattner24083062009-09-09 00:40:31 +000068 O << '$' << Op.getImm();
Chris Lattneraa398f52009-09-14 01:34:40 +000069 } else {
70 assert(Op.isExpr() && "unknown operand kind in printOperand");
Daniel Dunbarad8740a2009-08-14 03:42:12 +000071 O << '$';
Chris Lattner6d31b432009-09-14 01:49:26 +000072 Op.getExpr()->print(O, &MAI);
Chris Lattner46820152009-06-20 00:49:26 +000073 }
Chris Lattner8d284c72009-06-19 23:59:57 +000074}
75
Chris Lattner1cbd3de2009-09-13 19:30:11 +000076void X86ATTInstPrinter::printLeaMemReference(const MCInst *MI, unsigned Op) {
Chris Lattner46820152009-06-20 00:49:26 +000077 const MCOperand &BaseReg = MI->getOperand(Op);
78 const MCOperand &IndexReg = MI->getOperand(Op+2);
79 const MCOperand &DispSpec = MI->getOperand(Op+3);
80
Chris Lattner46820152009-06-20 00:49:26 +000081 if (DispSpec.isImm()) {
82 int64_t DispVal = DispSpec.getImm();
83 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
84 O << DispVal;
85 } else {
Chris Lattner24083062009-09-09 00:40:31 +000086 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattner6d31b432009-09-14 01:49:26 +000087 DispSpec.getExpr()->print(O, &MAI);
Chris Lattner46820152009-06-20 00:49:26 +000088 }
89
90 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner46820152009-06-20 00:49:26 +000091 O << '(';
92 if (BaseReg.getReg())
Chris Lattner9fc4db52009-06-20 07:59:10 +000093 printOperand(MI, Op);
Chris Lattner46820152009-06-20 00:49:26 +000094
95 if (IndexReg.getReg()) {
96 O << ',';
Chris Lattner9fc4db52009-06-20 07:59:10 +000097 printOperand(MI, Op+2);
Chris Lattnerb4b5c102009-06-20 08:13:12 +000098 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
99 if (ScaleVal != 1)
Chris Lattner46820152009-06-20 00:49:26 +0000100 O << ',' << ScaleVal;
101 }
102 O << ')';
103 }
Chris Lattner8d284c72009-06-19 23:59:57 +0000104}
105
Chris Lattner1cbd3de2009-09-13 19:30:11 +0000106void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op) {
Chris Lattneraa398f52009-09-14 01:34:40 +0000107 // If this has a segment register, print it.
108 if (MI->getOperand(Op+4).getReg()) {
Chris Lattnera0022a72009-06-20 07:03:18 +0000109 printOperand(MI, Op+4);
Chris Lattner46820152009-06-20 00:49:26 +0000110 O << ':';
111 }
Chris Lattnera0022a72009-06-20 07:03:18 +0000112 printLeaMemReference(MI, Op);
Chris Lattner8d284c72009-06-19 23:59:57 +0000113}