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 "MSP430MCAsmInfo.h"
19 #include "MSP430TargetMachine.h"
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Module.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/DwarfWriter.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineConstantPool.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/Target/TargetData.h"
32 #include "llvm/Target/TargetLoweringObjectFile.h"
33 #include "llvm/Target/TargetRegistry.h"
34 #include "llvm/ADT/Statistic.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/FormattedStream.h"
37 #include "llvm/Support/Mangler.h"
38 #include "llvm/Support/ErrorHandling.h"
42 STATISTIC(EmittedInsts
, "Number of machine instrs printed");
45 class VISIBILITY_HIDDEN MSP430AsmPrinter
: public AsmPrinter
{
47 MSP430AsmPrinter(formatted_raw_ostream
&O
, TargetMachine
&TM
,
48 const MCAsmInfo
*MAI
, bool V
)
49 : AsmPrinter(O
, TM
, MAI
, V
) {}
51 virtual const char *getPassName() const {
52 return "MSP430 Assembly Printer";
55 void printOperand(const MachineInstr
*MI
, int OpNum
,
56 const char* Modifier
= 0);
57 void printSrcMemOperand(const MachineInstr
*MI
, int OpNum
,
58 const char* Modifier
= 0);
59 void printCCOperand(const MachineInstr
*MI
, int OpNum
);
60 void printInstruction(const MachineInstr
*MI
); // autogenerated.
61 void printMachineInstruction(const MachineInstr
* MI
);
62 bool PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
64 const char *ExtraCode
);
66 void emitFunctionHeader(const MachineFunction
&MF
);
67 bool runOnMachineFunction(MachineFunction
&F
);
69 virtual void PrintGlobalVariable(const GlobalVariable
*GV
) {
70 // FIXME: No support for global variables?
73 void getAnalysisUsage(AnalysisUsage
&AU
) const {
74 AsmPrinter::getAnalysisUsage(AU
);
78 } // end of anonymous namespace
80 #include "MSP430GenAsmWriter.inc"
83 void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction
&MF
) {
84 const Function
*F
= MF
.getFunction();
86 OutStreamer
.SwitchSection(getObjFileLowering().SectionForGlobal(F
, Mang
, TM
));
88 unsigned FnAlign
= MF
.getAlignment();
89 EmitAlignment(FnAlign
, F
);
91 switch (F
->getLinkage()) {
92 default: llvm_unreachable("Unknown linkage type!");
93 case Function::InternalLinkage
: // Symbols default to internal.
94 case Function::PrivateLinkage
:
95 case Function::LinkerPrivateLinkage
:
97 case Function::ExternalLinkage
:
98 O
<< "\t.globl\t" << CurrentFnName
<< '\n';
100 case Function::LinkOnceAnyLinkage
:
101 case Function::LinkOnceODRLinkage
:
102 case Function::WeakAnyLinkage
:
103 case Function::WeakODRLinkage
:
104 O
<< "\t.weak\t" << CurrentFnName
<< '\n';
108 printVisibility(CurrentFnName
, F
->getVisibility());
110 O
<< "\t.type\t" << CurrentFnName
<< ",@function\n"
111 << CurrentFnName
<< ":\n";
114 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction
&MF
) {
115 SetupMachineFunction(MF
);
118 // Print the 'header' of function
119 emitFunctionHeader(MF
);
121 // Print out code for the function.
122 for (MachineFunction::const_iterator I
= MF
.begin(), E
= MF
.end();
124 // Print a label for the basic block.
125 if (!VerboseAsm
&& (I
->pred_empty() || I
->isOnlyReachableByFallthrough())) {
126 // This is an entry block or a block that's only reachable via a
127 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
129 EmitBasicBlockStart(I
);
133 for (MachineBasicBlock::const_iterator II
= I
->begin(), E
= I
->end();
135 // Print the assembly for the instruction.
136 printMachineInstruction(II
);
139 if (MAI
->hasDotTypeDotSizeDirective())
140 O
<< "\t.size\t" << CurrentFnName
<< ", .-" << CurrentFnName
<< '\n';
142 // We didn't modify anything
146 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr
*MI
) {
149 processDebugLoc(MI
->getDebugLoc());
151 // Call the autogenerated instruction printer routines.
152 printInstruction(MI
);
154 if (VerboseAsm
&& !MI
->getDebugLoc().isUnknown())
159 void MSP430AsmPrinter::printOperand(const MachineInstr
*MI
, int OpNum
,
160 const char* Modifier
) {
161 const MachineOperand
&MO
= MI
->getOperand(OpNum
);
162 switch (MO
.getType()) {
163 case MachineOperand::MO_Register
:
164 assert (TargetRegisterInfo::isPhysicalRegister(MO
.getReg()) &&
165 "Virtual registers should be already mapped!");
166 O
<< TM
.getRegisterInfo()->get(MO
.getReg()).AsmName
;
168 case MachineOperand::MO_Immediate
:
169 if (!Modifier
|| strcmp(Modifier
, "nohash"))
173 case MachineOperand::MO_MachineBasicBlock
:
174 GetMBBSymbol(MO
.getMBB()->getNumber())->print(O
, MAI
);
176 case MachineOperand::MO_GlobalAddress
: {
177 bool isMemOp
= Modifier
&& !strcmp(Modifier
, "mem");
178 bool isCallOp
= Modifier
&& !strcmp(Modifier
, "call");
179 std::string Name
= Mang
->getMangledName(MO
.getGlobal());
180 assert(MO
.getOffset() == 0 && "No offsets allowed!");
191 case MachineOperand::MO_ExternalSymbol
: {
192 bool isCallOp
= Modifier
&& !strcmp(Modifier
, "call");
193 std::string
Name(MAI
->getGlobalPrefix());
194 Name
+= MO
.getSymbolName();
201 llvm_unreachable("Not implemented yet!");
205 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr
*MI
, int OpNum
,
206 const char* Modifier
) {
207 const MachineOperand
&Base
= MI
->getOperand(OpNum
);
208 const MachineOperand
&Disp
= MI
->getOperand(OpNum
+1);
211 printOperand(MI
, OpNum
, "mem");
212 else if (Disp
.isImm() && !Base
.getReg())
213 printOperand(MI
, OpNum
);
214 else if (Base
.getReg()) {
216 printOperand(MI
, OpNum
+ 1, "nohash");
218 printOperand(MI
, OpNum
);
222 printOperand(MI
, OpNum
);
225 llvm_unreachable("Unsupported memory operand");
228 void MSP430AsmPrinter::printCCOperand(const MachineInstr
*MI
, int OpNum
) {
229 unsigned CC
= MI
->getOperand(OpNum
).getImm();
233 llvm_unreachable("Unsupported CC code");
238 case MSP430::COND_NE
:
241 case MSP430::COND_HS
:
244 case MSP430::COND_LO
:
247 case MSP430::COND_GE
:
256 /// PrintAsmOperand - Print out an operand for an inline asm expression.
258 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
260 const char *ExtraCode
) {
261 // Does this asm operand have a single letter operand modifier?
262 if (ExtraCode
&& ExtraCode
[0])
263 return true; // Unknown modifier.
265 printOperand(MI
, OpNo
);
269 // Force static initialization.
270 extern "C" void LLVMInitializeMSP430AsmPrinter() {
271 RegisterAsmPrinter
<MSP430AsmPrinter
> X(TheMSP430Target
);