blob: 6fc9bc13f93db503b896613fba9e761821d78670 [file] [log] [blame]
Andrew Lenharth01269522005-01-24 18:37:48 +00001//===-- AlphaAsmPrinter.cpp - Alpha LLVM assembly writer ------------------===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to GAS-format Alpha assembly language.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner95b2c7d2006-12-19 22:59:26 +000015#define DEBUG_TYPE "asm-printer"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000016#include "Alpha.h"
17#include "AlphaInstrInfo.h"
Andrew Lenharth120ab482005-09-29 22:54:56 +000018#include "AlphaTargetMachine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000019#include "llvm/Module.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000020#include "llvm/Type.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000021#include "llvm/Assembly/Writer.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000022#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000023#include "llvm/CodeGen/DwarfWriter.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000024#include "llvm/MC/MCStreamer.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000025#include "llvm/MC/MCAsmInfo.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000026#include "llvm/MC/MCSymbol.h"
Chris Lattnerd62f1b42010-03-12 21:19:23 +000027#include "llvm/Target/Mangler.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000029#include "llvm/Target/TargetMachine.h"
Daniel Dunbar51b198a2009-07-15 20:24:03 +000030#include "llvm/Target/TargetRegistry.h"
Torok Edwin804e0fe2009-07-08 19:04:27 +000031#include "llvm/Support/ErrorHandling.h"
David Greene71847812009-07-14 20:18:05 +000032#include "llvm/Support/FormattedStream.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000033using namespace llvm;
34
35namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000036 struct AlphaAsmPrinter : public AsmPrinter {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000037 /// Unique incrementer for label values for referencing Global values.
38 ///
Misha Brukman4633f1c2005-04-21 23:13:11 +000039
David Greene71847812009-07-14 20:18:05 +000040 explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattner11d53c12010-03-13 20:55:24 +000041 MCStreamer &Streamer)
42 : AsmPrinter(o, tm, Streamer) {}
Andrew Lenharth304d0f32005-01-22 23:41:55 +000043
Andrew Lenharth304d0f32005-01-22 23:41:55 +000044 virtual const char *getPassName() const {
45 return "Alpha Assembly Printer";
46 }
Chris Lattner35c33bd2010-04-04 04:47:45 +000047 void printInstruction(const MachineInstr *MI, raw_ostream &O);
Chris Lattnerd1ff72b2010-02-03 01:09:55 +000048 void EmitInstruction(const MachineInstr *MI) {
Chris Lattner35c33bd2010-04-04 04:47:45 +000049 printInstruction(MI, O);
Chris Lattner8e089a92010-02-10 00:36:00 +000050 OutStreamer.AddBlankLine();
Chris Lattnerd1ff72b2010-02-03 01:09:55 +000051 }
Chris Lattnerd95148f2009-09-13 20:19:22 +000052 static const char *getRegisterName(unsigned RegNo);
Chris Lattner05af2612009-09-13 20:08:00 +000053
Chris Lattner35c33bd2010-04-04 04:47:45 +000054 void printOp(const MachineOperand &MO, raw_ostream &O);
55 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
56 void printBaseOffsetPair(const MachineInstr *MI, int i, raw_ostream &O,
57 bool brackets=true);
Chris Lattnera34103f2010-01-28 06:22:43 +000058 virtual void EmitFunctionBodyStart();
59 virtual void EmitFunctionBodyEnd();
Bob Wilson812209a2009-09-30 22:06:26 +000060 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov79579c92008-08-07 09:53:57 +000061
Andrew Lenharth17255992006-06-21 13:37:27 +000062 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
63 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov79579c92008-08-07 09:53:57 +000064 bool PrintAsmMemoryOperand(const MachineInstr *MI,
Anton Korobeynikovbed29462007-04-16 18:10:23 +000065 unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +000066 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +000067 const char *ExtraCode);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000068 };
69} // end of anonymous namespace
70
Andrew Lenharth304d0f32005-01-22 23:41:55 +000071#include "AlphaGenAsmWriter.inc"
72
Chris Lattner35c33bd2010-04-04 04:47:45 +000073void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
74 raw_ostream &O) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000075 const MachineOperand &MO = MI->getOperand(opNum);
Chris Lattner2d90ac72006-05-04 18:05:43 +000076 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000077 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
78 "Not physreg??");
Chris Lattner762ccea2009-09-13 20:31:40 +000079 O << getRegisterName(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +000080 } else if (MO.isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +000081 O << MO.getImm();
82 assert(MO.getImm() < (1 << 30));
Andrew Lenharth304d0f32005-01-22 23:41:55 +000083 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +000084 printOp(MO, O);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000085 }
86}
87
88
Chris Lattner35c33bd2010-04-04 04:47:45 +000089void AlphaAsmPrinter::printOp(const MachineOperand &MO, raw_ostream &O) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000090 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +000091 case MachineOperand::MO_Register:
Chris Lattner762ccea2009-09-13 20:31:40 +000092 O << getRegisterName(MO.getReg());
Andrew Lenharth304d0f32005-01-22 23:41:55 +000093 return;
94
Chris Lattner63b3d712006-05-04 17:21:20 +000095 case MachineOperand::MO_Immediate:
Torok Edwinc23197a2009-07-14 16:55:14 +000096 llvm_unreachable("printOp() does not handle immediate values");
Andrew Lenharth304d0f32005-01-22 23:41:55 +000097 return;
98
Nate Begeman37efe672006-04-22 18:53:45 +000099 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000100 O << *MO.getMBB()->getSymbol();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000101 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000102
103 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000104 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000105 << MO.getIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000106 return;
107
108 case MachineOperand::MO_ExternalSymbol:
109 O << MO.getSymbolName();
110 return;
111
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000112 case MachineOperand::MO_GlobalAddress:
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000113 O << *Mang->getSymbol(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000114 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000115
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000116 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000117 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000118 << '_' << MO.getIndex();
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000119 return;
120
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000121 default:
122 O << "<unknown operand type: " << MO.getType() << ">";
123 return;
124 }
125}
126
Chris Lattnera34103f2010-01-28 06:22:43 +0000127/// EmitFunctionBodyStart - Targets can override this to emit stuff before
128/// the first basic block in the function.
129void AlphaAsmPrinter::EmitFunctionBodyStart() {
Chris Lattner10b318b2010-01-17 21:43:43 +0000130 O << "\t.ent " << *CurrentFnSym << "\n";
Chris Lattnera34103f2010-01-28 06:22:43 +0000131}
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000132
Chris Lattnera34103f2010-01-28 06:22:43 +0000133/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
134/// the last basic block in the function.
135void AlphaAsmPrinter::EmitFunctionBodyEnd() {
Chris Lattner10b318b2010-01-17 21:43:43 +0000136 O << "\t.end " << *CurrentFnSym << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000137}
138
Bob Wilson812209a2009-09-30 22:06:26 +0000139void AlphaAsmPrinter::EmitStartOfAsmFile(Module &M) {
140 if (TM.getSubtarget<AlphaSubtarget>().hasCT())
Andrew Lenharth3553d862007-01-24 21:09:16 +0000141 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000142 else
Andrew Lenharth3553d862007-01-24 21:09:16 +0000143 O << "\t.arch ev6\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000144 O << "\t.set noat\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000145}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000146
Andrew Lenharth17255992006-06-21 13:37:27 +0000147/// PrintAsmOperand - Print out an operand for an inline asm expression.
148///
149bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000150 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000151 const char *ExtraCode) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000152 printOperand(MI, OpNo, O);
Andrew Lenharth17255992006-06-21 13:37:27 +0000153 return false;
154}
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000155
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000156bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000157 unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000158 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000159 const char *ExtraCode) {
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000160 if (ExtraCode && ExtraCode[0])
161 return true; // Unknown modifier.
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000162 O << "0(";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000163 printOperand(MI, OpNo, O);
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000164 O << ")";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000165 return false;
166}
Douglas Gregor1555a232009-06-16 20:12:29 +0000167
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000168// Force static initialization.
169extern "C" void LLVMInitializeAlphaAsmPrinter() {
Daniel Dunbar0c795d62009-07-25 06:49:55 +0000170 RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000171}