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