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/ErrorHandling.h"
44 STATISTIC(EmittedInsts, "Number of machine instrs printed");
47 EnableMCInst("enable-msp430-mcinst-printer", cl::Hidden,
48 cl::desc("enable experimental mcinst gunk in the msp430 backend"));
51 class MSP430AsmPrinter : public AsmPrinter {
53 MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
54 const MCAsmInfo *MAI, bool V)
55 : AsmPrinter(O, TM, MAI, V) {}
57 virtual const char *getPassName() const {
58 return "MSP430 Assembly Printer";
61 void printMCInst(const MCInst *MI) {
62 MSP430InstPrinter(O, *MAI).printInstruction(MI);
64 void printOperand(const MachineInstr *MI, int OpNum,
65 const char* Modifier = 0);
66 void printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
67 printOperand(MI, OpNum);
69 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
70 const char* Modifier = 0);
71 void printCCOperand(const MachineInstr *MI, int OpNum);
72 void printMachineInstruction(const MachineInstr * MI);
73 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
75 const char *ExtraCode);
76 bool PrintAsmMemoryOperand(const MachineInstr *MI,
77 unsigned OpNo, unsigned AsmVariant,
78 const char *ExtraCode);
79 void printInstructionThroughMCStreamer(const MachineInstr *MI);
81 void PrintGlobalVariable(const GlobalVariable* GVar);
82 void emitFunctionHeader(const MachineFunction &MF);
83 bool runOnMachineFunction(MachineFunction &F);
85 void getAnalysisUsage(AnalysisUsage &AU) const {
86 AsmPrinter::getAnalysisUsage(AU);
90 } // end of anonymous namespace
92 void MSP430AsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
93 if (!GVar->hasInitializer())
94 return; // External global require no code
96 // Check to see if this is a special global used by LLVM, if so, emit it.
97 if (EmitSpecialLLVMGlobal(GVar))
100 const TargetData *TD = TM.getTargetData();
102 MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
103 Constant *C = GVar->getInitializer();
104 unsigned Size = TD->getTypeAllocSize(C->getType());
105 unsigned Align = TD->getPreferredAlignmentLog(GVar);
107 printVisibility(GVarSym, GVar->getVisibility());
110 GVarSym->print(O, MAI);
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()) {
124 GVarSym->print(O, MAI);
128 O << MAI->getCOMMDirective();
129 GVarSym->print(O, MAI);
131 if (MAI->getCOMMDirectiveTakesAlignment())
132 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
135 O.PadToColumn(MAI->getCommentColumn());
136 O << MAI->getCommentString() << ' ';
137 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
143 switch (GVar->getLinkage()) {
144 case GlobalValue::CommonLinkage:
145 case GlobalValue::LinkOnceAnyLinkage:
146 case GlobalValue::LinkOnceODRLinkage:
147 case GlobalValue::WeakAnyLinkage:
148 case GlobalValue::WeakODRLinkage:
150 GVarSym->print(O, MAI);
153 case GlobalValue::DLLExportLinkage:
154 case GlobalValue::AppendingLinkage:
155 // FIXME: appending linkage variables should go into a section of
156 // their name or something. For now, just emit them as external.
157 case GlobalValue::ExternalLinkage:
158 // If external or appending, declare as a global symbol
160 GVarSym->print(O, MAI);
163 case GlobalValue::PrivateLinkage:
164 case GlobalValue::LinkerPrivateLinkage:
165 case GlobalValue::InternalLinkage:
168 assert(0 && "Unknown linkage type!");
171 // Use 16-bit alignment by default to simplify bunch of stuff
172 EmitAlignment(Align, GVar);
173 GVarSym->print(O, MAI);
176 O.PadToColumn(MAI->getCommentColumn());
177 O << MAI->getCommentString() << ' ';
178 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
182 EmitGlobalConstant(C);
184 if (MAI->hasDotTypeDotSizeDirective()) {
186 GVarSym->print(O, MAI);
187 O << ", " << Size << '\n';
191 void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
192 const Function *F = MF.getFunction();
194 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
196 unsigned FnAlign = MF.getAlignment();
197 EmitAlignment(FnAlign, F);
199 switch (F->getLinkage()) {
200 default: llvm_unreachable("Unknown linkage type!");
201 case Function::InternalLinkage: // Symbols default to internal.
202 case Function::PrivateLinkage:
203 case Function::LinkerPrivateLinkage:
205 case Function::ExternalLinkage:
207 CurrentFnSym->print(O, MAI);
210 case Function::LinkOnceAnyLinkage:
211 case Function::LinkOnceODRLinkage:
212 case Function::WeakAnyLinkage:
213 case Function::WeakODRLinkage:
215 CurrentFnSym->print(O, MAI);
220 printVisibility(CurrentFnSym, F->getVisibility());
223 CurrentFnSym->print(O, MAI);
225 CurrentFnSym->print(O, MAI);
229 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
230 SetupMachineFunction(MF);
233 // Print the 'header' of function
234 emitFunctionHeader(MF);
236 // Print out code for the function.
237 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
239 // Print a label for the basic block.
240 EmitBasicBlockStart(I);
242 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
244 // Print the assembly for the instruction.
245 printMachineInstruction(II);
248 if (MAI->hasDotTypeDotSizeDirective()) {
250 CurrentFnSym->print(O, MAI);
252 CurrentFnSym->print(O, MAI);
256 // We didn't modify anything
260 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
263 processDebugLoc(MI, true);
265 printInstructionThroughMCStreamer(MI);
271 processDebugLoc(MI, false);
274 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
275 const char* Modifier) {
276 const MachineOperand &MO = MI->getOperand(OpNum);
277 switch (MO.getType()) {
278 case MachineOperand::MO_Register:
279 O << MSP430InstPrinter::getRegisterName(MO.getReg());
281 case MachineOperand::MO_Immediate:
282 if (!Modifier || strcmp(Modifier, "nohash"))
286 case MachineOperand::MO_MachineBasicBlock:
287 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
289 case MachineOperand::MO_GlobalAddress: {
290 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
291 uint64_t Offset = MO.getOffset();
293 O << (isMemOp ? '&' : '#');
295 O << '(' << Offset << '+';
297 GetGlobalValueSymbol(MO.getGlobal())->print(O, MAI);
304 case MachineOperand::MO_ExternalSymbol: {
305 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
306 O << (isMemOp ? '&' : '#');
307 O << MAI->getGlobalPrefix() << MO.getSymbolName();
311 llvm_unreachable("Not implemented yet!");
315 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
316 const char* Modifier) {
317 const MachineOperand &Base = MI->getOperand(OpNum);
318 const MachineOperand &Disp = MI->getOperand(OpNum+1);
320 // Print displacement first
322 printOperand(MI, OpNum+1, "mem");
327 printOperand(MI, OpNum+1, "nohash");
331 // Print register base field
334 printOperand(MI, OpNum);
339 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
340 unsigned CC = MI->getOperand(OpNum).getImm();
344 llvm_unreachable("Unsupported CC code");
346 case MSP430CC::COND_E:
349 case MSP430CC::COND_NE:
352 case MSP430CC::COND_HS:
355 case MSP430CC::COND_LO:
358 case MSP430CC::COND_GE:
361 case MSP430CC::COND_L:
367 /// PrintAsmOperand - Print out an operand for an inline asm expression.
369 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
371 const char *ExtraCode) {
372 // Does this asm operand have a single letter operand modifier?
373 if (ExtraCode && ExtraCode[0])
374 return true; // Unknown modifier.
376 printOperand(MI, OpNo);
380 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
381 unsigned OpNo, unsigned AsmVariant,
382 const char *ExtraCode) {
383 if (ExtraCode && ExtraCode[0]) {
384 return true; // Unknown modifier.
386 printSrcMemOperand(MI, OpNo);
390 //===----------------------------------------------------------------------===//
391 void MSP430AsmPrinter::printInstructionThroughMCStreamer(const MachineInstr *MI){
393 MSP430MCInstLower MCInstLowering(OutContext, *Mang, *this);
395 switch (MI->getOpcode()) {
396 case TargetInstrInfo::DBG_LABEL:
397 case TargetInstrInfo::EH_LABEL:
398 case TargetInstrInfo::GC_LABEL:
401 case TargetInstrInfo::KILL:
404 case TargetInstrInfo::INLINEASM:
407 case TargetInstrInfo::IMPLICIT_DEF:
408 printImplicitDef(MI);
414 MCInstLowering.Lower(MI, TmpInst);
416 printMCInst(&TmpInst);
419 static MCInstPrinter *createMSP430MCInstPrinter(const Target &T,
420 unsigned SyntaxVariant,
421 const MCAsmInfo &MAI,
423 if (SyntaxVariant == 0)
424 return new MSP430InstPrinter(O, MAI);
428 // Force static initialization.
429 extern "C" void LLVMInitializeMSP430AsmPrinter() {
430 RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
431 TargetRegistry::RegisterMCInstPrinter(TheMSP430Target,
432 createMSP430MCInstPrinter);