1 //===-- MSP430AsmPrinter.cpp - MSP430 LLVM assembly writer ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to the MSP430 assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
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/ADT/Statistic.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/FormattedStream.h"
41 #include "llvm/Support/Mangler.h"
42 #include "llvm/Support/ErrorHandling.h"
46 STATISTIC(EmittedInsts, "Number of machine instrs printed");
49 EnableMCInst("enable-msp430-mcinst-printer", cl::Hidden,
50 cl::desc("enable experimental mcinst gunk in the msp430 backend"));
53 class MSP430AsmPrinter : public AsmPrinter {
55 MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
56 const MCAsmInfo *MAI, bool V)
57 : AsmPrinter(O, TM, MAI, V) {}
59 virtual const char *getPassName() const {
60 return "MSP430 Assembly Printer";
63 void printMCInst(const MCInst *MI) {
64 MSP430InstPrinter(O, *MAI).printInstruction(MI);
66 void printOperand(const MachineInstr *MI, int OpNum,
67 const char* Modifier = 0);
68 void printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
69 printOperand(MI, OpNum);
71 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
72 const char* Modifier = 0);
73 void printCCOperand(const MachineInstr *MI, int OpNum);
74 void printMachineInstruction(const MachineInstr * MI);
75 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
77 const char *ExtraCode);
78 bool PrintAsmMemoryOperand(const MachineInstr *MI,
79 unsigned OpNo, unsigned AsmVariant,
80 const char *ExtraCode);
81 void printInstructionThroughMCStreamer(const MachineInstr *MI);
83 void PrintGlobalVariable(const GlobalVariable* GVar);
84 void emitFunctionHeader(const MachineFunction &MF);
85 bool runOnMachineFunction(MachineFunction &F);
87 void getAnalysisUsage(AnalysisUsage &AU) const {
88 AsmPrinter::getAnalysisUsage(AU);
92 } // end of anonymous namespace
94 void MSP430AsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
95 if (!GVar->hasInitializer())
96 return; // External global require no code
98 // Check to see if this is a special global used by LLVM, if so, emit it.
99 if (EmitSpecialLLVMGlobal(GVar))
102 const TargetData *TD = TM.getTargetData();
104 std::string name = Mang->getMangledName(GVar);
105 Constant *C = GVar->getInitializer();
106 unsigned Size = TD->getTypeAllocSize(C->getType());
107 unsigned Align = TD->getPreferredAlignmentLog(GVar);
109 printVisibility(name, GVar->getVisibility());
111 O << "\t.type\t" << name << ",@object\n";
113 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
116 if (C->isNullValue() && !GVar->hasSection() &&
117 !GVar->isThreadLocal() &&
118 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
120 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
122 if (GVar->hasLocalLinkage())
123 O << "\t.local\t" << name << '\n';
125 O << MAI->getCOMMDirective() << name << ',' << Size;
126 if (MAI->getCOMMDirectiveTakesAlignment())
127 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
130 O.PadToColumn(MAI->getCommentColumn());
131 O << MAI->getCommentString() << ' ';
132 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
138 switch (GVar->getLinkage()) {
139 case GlobalValue::CommonLinkage:
140 case GlobalValue::LinkOnceAnyLinkage:
141 case GlobalValue::LinkOnceODRLinkage:
142 case GlobalValue::WeakAnyLinkage:
143 case GlobalValue::WeakODRLinkage:
144 O << "\t.weak\t" << name << '\n';
146 case GlobalValue::DLLExportLinkage:
147 case GlobalValue::AppendingLinkage:
148 // FIXME: appending linkage variables should go into a section of
149 // their name or something. For now, just emit them as external.
150 case GlobalValue::ExternalLinkage:
151 // If external or appending, declare as a global symbol
152 O << "\t.globl " << name << '\n';
154 case GlobalValue::PrivateLinkage:
155 case GlobalValue::LinkerPrivateLinkage:
156 case GlobalValue::InternalLinkage:
159 assert(0 && "Unknown linkage type!");
162 // Use 16-bit alignment by default to simplify bunch of stuff
163 EmitAlignment(Align, GVar);
166 O.PadToColumn(MAI->getCommentColumn());
167 O << MAI->getCommentString() << ' ';
168 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
172 EmitGlobalConstant(C);
174 if (MAI->hasDotTypeDotSizeDirective())
175 O << "\t.size\t" << name << ", " << Size << '\n';
178 void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
179 const Function *F = MF.getFunction();
181 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
183 unsigned FnAlign = MF.getAlignment();
184 EmitAlignment(FnAlign, F);
186 switch (F->getLinkage()) {
187 default: llvm_unreachable("Unknown linkage type!");
188 case Function::InternalLinkage: // Symbols default to internal.
189 case Function::PrivateLinkage:
190 case Function::LinkerPrivateLinkage:
192 case Function::ExternalLinkage:
193 O << "\t.globl\t" << CurrentFnName << '\n';
195 case Function::LinkOnceAnyLinkage:
196 case Function::LinkOnceODRLinkage:
197 case Function::WeakAnyLinkage:
198 case Function::WeakODRLinkage:
199 O << "\t.weak\t" << CurrentFnName << '\n';
203 printVisibility(CurrentFnName, F->getVisibility());
205 O << "\t.type\t" << CurrentFnName << ",@function\n"
206 << CurrentFnName << ":\n";
209 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
210 SetupMachineFunction(MF);
213 // Print the 'header' of function
214 emitFunctionHeader(MF);
216 // Print out code for the function.
217 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
219 // Print a label for the basic block.
220 EmitBasicBlockStart(I);
222 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
224 // Print the assembly for the instruction.
225 printMachineInstruction(II);
228 if (MAI->hasDotTypeDotSizeDirective())
229 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
231 // We didn't modify anything
235 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
238 processDebugLoc(MI, true);
240 printInstructionThroughMCStreamer(MI);
246 processDebugLoc(MI, false);
249 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
250 const char* Modifier) {
251 const MachineOperand &MO = MI->getOperand(OpNum);
252 switch (MO.getType()) {
253 case MachineOperand::MO_Register:
254 O << MSP430InstPrinter::getRegisterName(MO.getReg());
256 case MachineOperand::MO_Immediate:
257 if (!Modifier || strcmp(Modifier, "nohash"))
261 case MachineOperand::MO_MachineBasicBlock:
262 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
264 case MachineOperand::MO_GlobalAddress: {
265 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
266 std::string Name = Mang->getMangledName(MO.getGlobal());
267 uint64_t Offset = MO.getOffset();
269 O << (isMemOp ? '&' : '#');
271 O << '(' << Offset << '+';
279 case MachineOperand::MO_ExternalSymbol: {
280 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
281 std::string Name(MAI->getGlobalPrefix());
282 Name += MO.getSymbolName();
284 O << (isMemOp ? '&' : '#') << Name;
289 llvm_unreachable("Not implemented yet!");
293 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
294 const char* Modifier) {
295 const MachineOperand &Base = MI->getOperand(OpNum);
296 const MachineOperand &Disp = MI->getOperand(OpNum+1);
298 // Print displacement first
300 printOperand(MI, OpNum+1, "mem");
305 printOperand(MI, OpNum+1, "nohash");
309 // Print register base field
312 printOperand(MI, OpNum);
317 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
318 unsigned CC = MI->getOperand(OpNum).getImm();
322 llvm_unreachable("Unsupported CC code");
324 case MSP430CC::COND_E:
327 case MSP430CC::COND_NE:
330 case MSP430CC::COND_HS:
333 case MSP430CC::COND_LO:
336 case MSP430CC::COND_GE:
339 case MSP430CC::COND_L:
345 /// PrintAsmOperand - Print out an operand for an inline asm expression.
347 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
349 const char *ExtraCode) {
350 // Does this asm operand have a single letter operand modifier?
351 if (ExtraCode && ExtraCode[0])
352 return true; // Unknown modifier.
354 printOperand(MI, OpNo);
358 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
359 unsigned OpNo, unsigned AsmVariant,
360 const char *ExtraCode) {
361 if (ExtraCode && ExtraCode[0]) {
362 return true; // Unknown modifier.
364 printSrcMemOperand(MI, OpNo);
368 //===----------------------------------------------------------------------===//
369 void MSP430AsmPrinter::printInstructionThroughMCStreamer(const MachineInstr *MI){
371 MSP430MCInstLower MCInstLowering(OutContext, *Mang, *this);
373 switch (MI->getOpcode()) {
374 case TargetInstrInfo::DBG_LABEL:
375 case TargetInstrInfo::EH_LABEL:
376 case TargetInstrInfo::GC_LABEL:
379 case TargetInstrInfo::KILL:
382 case TargetInstrInfo::INLINEASM:
385 case TargetInstrInfo::IMPLICIT_DEF:
386 printImplicitDef(MI);
392 MCInstLowering.Lower(MI, TmpInst);
394 printMCInst(&TmpInst);
397 static MCInstPrinter *createMSP430MCInstPrinter(const Target &T,
398 unsigned SyntaxVariant,
399 const MCAsmInfo &MAI,
401 if (SyntaxVariant == 0)
402 return new MSP430InstPrinter(O, MAI);
406 // Force static initialization.
407 extern "C" void LLVMInitializeMSP430AsmPrinter() {
408 RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
409 TargetRegistry::RegisterMCInstPrinter(TheMSP430Target,
410 createMSP430MCInstPrinter);