change a ton of code to not implicitly use the "O" raw_ostream
[oota-llvm.git] / lib / Target / MSP430 / AsmPrinter / MSP430AsmPrinter.cpp
1 //===-- MSP430AsmPrinter.cpp - MSP430 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 the MSP430 assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "asm-printer"
16 #include "MSP430.h"
17 #include "MSP430InstrInfo.h"
18 #include "MSP430InstPrinter.h"
19 #include "MSP430MCAsmInfo.h"
20 #include "MSP430MCInstLower.h"
21 #include "MSP430TargetMachine.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Module.h"
25 #include "llvm/Assembly/Writer.h"
26 #include "llvm/CodeGen/AsmPrinter.h"
27 #include "llvm/CodeGen/DwarfWriter.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineFunctionPass.h"
30 #include "llvm/CodeGen/MachineConstantPool.h"
31 #include "llvm/CodeGen/MachineInstr.h"
32 #include "llvm/MC/MCInst.h"
33 #include "llvm/MC/MCStreamer.h"
34 #include "llvm/MC/MCSymbol.h"
35 #include "llvm/Target/Mangler.h"
36 #include "llvm/Target/TargetData.h"
37 #include "llvm/Target/TargetLoweringObjectFile.h"
38 #include "llvm/Target/TargetRegistry.h"
39 #include "llvm/Support/FormattedStream.h"
40 using namespace llvm;
41
42 namespace {
43   class MSP430AsmPrinter : public AsmPrinter {
44   public:
45     MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
46                      MCStreamer &Streamer)
47       : AsmPrinter(O, TM, Streamer) {}
48
49     virtual const char *getPassName() const {
50       return "MSP430 Assembly Printer";
51     }
52
53     void printMCInst(const MCInst *MI) {
54       MSP430InstPrinter(O, *MAI).printInstruction(MI, O);
55     }
56     void printOperand(const MachineInstr *MI, int OpNum,
57                       const char* Modifier = 0);
58     void printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
59       printOperand(MI, OpNum);
60     }
61     void printSrcMemOperand(const MachineInstr *MI, int OpNum,
62                             const char* Modifier = 0);
63     void printCCOperand(const MachineInstr *MI, int OpNum);
64     void printMachineInstruction(const MachineInstr * MI);
65     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
66                          unsigned AsmVariant,
67                          const char *ExtraCode);
68     bool PrintAsmMemoryOperand(const MachineInstr *MI,
69                                unsigned OpNo, unsigned AsmVariant,
70                                const char *ExtraCode);
71     void EmitInstruction(const MachineInstr *MI);
72
73     void getAnalysisUsage(AnalysisUsage &AU) const {
74       AsmPrinter::getAnalysisUsage(AU);
75       AU.setPreservesAll();
76     }
77   };
78 } // end of anonymous namespace
79
80
81 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
82                                     const char* Modifier) {
83   const MachineOperand &MO = MI->getOperand(OpNum);
84   switch (MO.getType()) {
85   default: assert(0 && "Not implemented yet!");
86   case MachineOperand::MO_Register:
87     O << MSP430InstPrinter::getRegisterName(MO.getReg());
88     return;
89   case MachineOperand::MO_Immediate:
90     if (!Modifier || strcmp(Modifier, "nohash"))
91       O << '#';
92     O << MO.getImm();
93     return;
94   case MachineOperand::MO_MachineBasicBlock:
95     O << *MO.getMBB()->getSymbol();
96     return;
97   case MachineOperand::MO_GlobalAddress: {
98     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
99     uint64_t Offset = MO.getOffset();
100
101     // If the global address expression is a part of displacement field with a
102     // register base, we should not emit any prefix symbol here, e.g.
103     //   mov.w &foo, r1
104     // vs
105     //   mov.w glb(r1), r2
106     // Otherwise (!) msp430-as will silently miscompile the output :(
107     if (!Modifier || strcmp(Modifier, "nohash"))
108       O << (isMemOp ? '&' : '#');
109     if (Offset)
110       O << '(' << Offset << '+';
111
112     O << *Mang->getSymbol(MO.getGlobal());
113
114     if (Offset)
115       O << ')';
116
117     return;
118   }
119   case MachineOperand::MO_ExternalSymbol: {
120     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
121     O << (isMemOp ? '&' : '#');
122     O << MAI->getGlobalPrefix() << MO.getSymbolName();
123     return;
124   }
125   }
126 }
127
128 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
129                                           const char* Modifier) {
130   const MachineOperand &Base = MI->getOperand(OpNum);
131   const MachineOperand &Disp = MI->getOperand(OpNum+1);
132
133   // Print displacement first
134
135   // Imm here is in fact global address - print extra modifier.
136   if (Disp.isImm() && !Base.getReg())
137     O << '&';
138   printOperand(MI, OpNum+1, "nohash");
139
140   // Print register base field
141   if (Base.getReg()) {
142     O << '(';
143     printOperand(MI, OpNum);
144     O << ')';
145   }
146 }
147
148 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
149   switch (MI->getOperand(OpNum).getImm()) {
150   default: assert(0 && "Unknown cond");
151   case MSP430CC::COND_E:  O << "eq"; break;
152   case MSP430CC::COND_NE: O << "ne"; break;
153   case MSP430CC::COND_HS: O << "hs"; break;
154   case MSP430CC::COND_LO: O << "lo"; break;
155   case MSP430CC::COND_GE: O << "ge"; break;
156   case MSP430CC::COND_L:  O << 'l';  break;
157   }
158 }
159
160 /// PrintAsmOperand - Print out an operand for an inline asm expression.
161 ///
162 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
163                                        unsigned AsmVariant,
164                                        const char *ExtraCode) {
165   // Does this asm operand have a single letter operand modifier?
166   if (ExtraCode && ExtraCode[0])
167     return true; // Unknown modifier.
168
169   printOperand(MI, OpNo);
170   return false;
171 }
172
173 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
174                                              unsigned OpNo, unsigned AsmVariant,
175                                              const char *ExtraCode) {
176   if (ExtraCode && ExtraCode[0]) {
177     return true; // Unknown modifier.
178   }
179   printSrcMemOperand(MI, OpNo);
180   return false;
181 }
182
183 //===----------------------------------------------------------------------===//
184 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
185   MSP430MCInstLower MCInstLowering(OutContext, *Mang, *this);
186
187   MCInst TmpInst;
188   MCInstLowering.Lower(MI, TmpInst);
189   OutStreamer.EmitInstruction(TmpInst);
190 }
191
192 static MCInstPrinter *createMSP430MCInstPrinter(const Target &T,
193                                                 unsigned SyntaxVariant,
194                                                 const MCAsmInfo &MAI,
195                                                 raw_ostream &O) {
196   if (SyntaxVariant == 0)
197     return new MSP430InstPrinter(O, MAI);
198   return 0;
199 }
200
201 // Force static initialization.
202 extern "C" void LLVMInitializeMSP430AsmPrinter() {
203   RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
204   TargetRegistry::RegisterMCInstPrinter(TheMSP430Target,
205                                         createMSP430MCInstPrinter);
206 }