03a49b27349c924135f8b151bacdce47bbd66cf8
[oota-llvm.git] / lib / Target / Alpha / AsmPrinter / AlphaAsmPrinter.cpp
1 //===-- AlphaAsmPrinter.cpp - Alpha LLVM assembly writer ------------------===//
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 contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to GAS-format Alpha assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "asm-printer"
16 #include "Alpha.h"
17 #include "AlphaInstrInfo.h"
18 #include "AlphaTargetMachine.h"
19 #include "llvm/Module.h"
20 #include "llvm/Type.h"
21 #include "llvm/Assembly/Writer.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/CodeGen/DwarfWriter.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Target/TargetLoweringObjectFile.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetRegistry.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/FormattedStream.h"
32 using namespace llvm;
33
34 namespace {
35   struct AlphaAsmPrinter : public AsmPrinter {
36     /// Unique incrementer for label values for referencing Global values.
37     ///
38
39     explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
40                              MCContext &Ctx, MCStreamer &Streamer,
41                              const MCAsmInfo *T)
42       : AsmPrinter(o, tm, Ctx, Streamer, T) {}
43
44     virtual const char *getPassName() const {
45       return "Alpha Assembly Printer";
46     }
47     void printInstruction(const MachineInstr *MI);
48     void EmitInstruction(const MachineInstr *MI) {
49       printInstruction(MI);
50       O << '\n';
51     }
52     static const char *getRegisterName(unsigned RegNo);
53
54     void printOp(const MachineOperand &MO, bool IsCallOp = false);
55     void printOperand(const MachineInstr *MI, int opNum);
56     void printBaseOffsetPair(const MachineInstr *MI, int i, bool brackets=true);
57     virtual void EmitFunctionBodyStart();
58     virtual void EmitFunctionBodyEnd(); 
59     void EmitStartOfAsmFile(Module &M);
60
61     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
62                          unsigned AsmVariant, const char *ExtraCode);
63     bool PrintAsmMemoryOperand(const MachineInstr *MI,
64                                unsigned OpNo,
65                                unsigned AsmVariant,
66                                const char *ExtraCode);
67   };
68 } // end of anonymous namespace
69
70 #include "AlphaGenAsmWriter.inc"
71
72 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
73 {
74   const MachineOperand &MO = MI->getOperand(opNum);
75   if (MO.getType() == MachineOperand::MO_Register) {
76     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
77            "Not physreg??");
78     O << getRegisterName(MO.getReg());
79   } else if (MO.isImm()) {
80     O << MO.getImm();
81     assert(MO.getImm() < (1 << 30));
82   } else {
83     printOp(MO);
84   }
85 }
86
87
88 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
89   switch (MO.getType()) {
90   case MachineOperand::MO_Register:
91     O << getRegisterName(MO.getReg());
92     return;
93
94   case MachineOperand::MO_Immediate:
95     llvm_unreachable("printOp() does not handle immediate values");
96     return;
97
98   case MachineOperand::MO_MachineBasicBlock:
99     O << *MO.getMBB()->getSymbol(OutContext);
100     return;
101
102   case MachineOperand::MO_ConstantPoolIndex:
103     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
104       << MO.getIndex();
105     return;
106
107   case MachineOperand::MO_ExternalSymbol:
108     O << MO.getSymbolName();
109     return;
110
111   case MachineOperand::MO_GlobalAddress:
112     O << *GetGlobalValueSymbol(MO.getGlobal());
113     return;
114
115   case MachineOperand::MO_JumpTableIndex:
116     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
117       << '_' << MO.getIndex();
118     return;
119
120   default:
121     O << "<unknown operand type: " << MO.getType() << ">";
122     return;
123   }
124 }
125
126 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
127 /// the first basic block in the function.
128 void AlphaAsmPrinter::EmitFunctionBodyStart() {
129   O << "\t.ent " << *CurrentFnSym << "\n";
130 }
131
132 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
133 /// the last basic block in the function.
134 void AlphaAsmPrinter::EmitFunctionBodyEnd() {
135   O << "\t.end " << *CurrentFnSym << "\n";
136 }
137
138 void AlphaAsmPrinter::EmitStartOfAsmFile(Module &M) {
139   if (TM.getSubtarget<AlphaSubtarget>().hasCT())
140     O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
141   else
142     O << "\t.arch ev6\n";
143   O << "\t.set noat\n";
144 }
145
146 /// PrintAsmOperand - Print out an operand for an inline asm expression.
147 ///
148 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
149                                       unsigned AsmVariant,
150                                       const char *ExtraCode) {
151   printOperand(MI, OpNo);
152   return false;
153 }
154
155 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
156                                             unsigned OpNo,
157                                             unsigned AsmVariant,
158                                             const char *ExtraCode) {
159   if (ExtraCode && ExtraCode[0])
160     return true; // Unknown modifier.
161   O << "0(";
162   printOperand(MI, OpNo);
163   O << ")";
164   return false;
165 }
166
167 // Force static initialization.
168 extern "C" void LLVMInitializeAlphaAsmPrinter() { 
169   RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);
170 }