emit instructions through the streamer.
[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/TargetData.h"
36 #include "llvm/Target/TargetLoweringObjectFile.h"
37 #include "llvm/Target/TargetRegistry.h"
38 #include "llvm/Support/FormattedStream.h"
39 using namespace llvm;
40
41 namespace {
42   class MSP430AsmPrinter : public AsmPrinter {
43   public:
44     MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
45                      MCContext &Ctx, MCStreamer &Streamer,
46                      const MCAsmInfo *MAI)
47       : AsmPrinter(O, TM, Ctx, Streamer, MAI) {}
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);
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(OutContext);
96     return;
97   case MachineOperand::MO_GlobalAddress: {
98     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
99     uint64_t Offset = MO.getOffset();
100
101     O << (isMemOp ? '&' : '#');
102     if (Offset)
103       O << '(' << Offset << '+';
104
105     O << *GetGlobalValueSymbol(MO.getGlobal());
106     
107     if (Offset)
108       O << ')';
109
110     return;
111   }
112   case MachineOperand::MO_ExternalSymbol: {
113     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
114     O << (isMemOp ? '&' : '#');
115     O << MAI->getGlobalPrefix() << MO.getSymbolName();
116     return;
117   }
118   }
119 }
120
121 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
122                                           const char* Modifier) {
123   const MachineOperand &Base = MI->getOperand(OpNum);
124   const MachineOperand &Disp = MI->getOperand(OpNum+1);
125
126   // Print displacement first
127   if (!Disp.isImm()) {
128     printOperand(MI, OpNum+1, "mem");
129   } else {
130     if (!Base.getReg())
131       O << '&';
132
133     printOperand(MI, OpNum+1, "nohash");
134   }
135
136
137   // Print register base field
138   if (Base.getReg()) {
139     O << '(';
140     printOperand(MI, OpNum);
141     O << ')';
142   }
143 }
144
145 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
146   switch (MI->getOperand(OpNum).getImm()) {
147   default: assert(0 && "Unknown cond");
148   case MSP430CC::COND_E:  O << "eq"; break;
149   case MSP430CC::COND_NE: O << "ne"; break;
150   case MSP430CC::COND_HS: O << "hs"; break;
151   case MSP430CC::COND_LO: O << "lo"; break;
152   case MSP430CC::COND_GE: O << "ge"; break;
153   case MSP430CC::COND_L:  O << 'l';  break;
154   }
155 }
156
157 /// PrintAsmOperand - Print out an operand for an inline asm expression.
158 ///
159 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
160                                        unsigned AsmVariant,
161                                        const char *ExtraCode) {
162   // Does this asm operand have a single letter operand modifier?
163   if (ExtraCode && ExtraCode[0])
164     return true; // Unknown modifier.
165
166   printOperand(MI, OpNo);
167   return false;
168 }
169
170 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
171                                              unsigned OpNo, unsigned AsmVariant,
172                                              const char *ExtraCode) {
173   if (ExtraCode && ExtraCode[0]) {
174     return true; // Unknown modifier.
175   }
176   printSrcMemOperand(MI, OpNo);
177   return false;
178 }
179
180 //===----------------------------------------------------------------------===//
181 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
182   MSP430MCInstLower MCInstLowering(OutContext, *Mang, *this);
183
184   MCInst TmpInst;
185   MCInstLowering.Lower(MI, TmpInst);
186   OutStreamer.EmitInstruction(TmpInst);
187 }
188
189 static MCInstPrinter *createMSP430MCInstPrinter(const Target &T,
190                                                 unsigned SyntaxVariant,
191                                                 const MCAsmInfo &MAI,
192                                                 raw_ostream &O) {
193   if (SyntaxVariant == 0)
194     return new MSP430InstPrinter(O, MAI);
195   return 0;
196 }
197
198 // Force static initialization.
199 extern "C" void LLVMInitializeMSP430AsmPrinter() {
200   RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
201   TargetRegistry::RegisterMCInstPrinter(TheMSP430Target,
202                                         createMSP430MCInstPrinter);
203 }