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/Mangler.h"
36 #include "llvm/Target/TargetData.h"
37 #include "llvm/Target/TargetLoweringObjectFile.h"
38 #include "llvm/Target/TargetRegistry.h"
39 #include "llvm/Support/FormattedStream.h"
43 class MSP430AsmPrinter : public AsmPrinter {
45 MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
46 MCContext &Ctx, MCStreamer &Streamer,
48 : AsmPrinter(O, TM, Ctx, Streamer, MAI) {}
50 virtual const char *getPassName() const {
51 return "MSP430 Assembly Printer";
54 void printMCInst(const MCInst *MI) {
55 MSP430InstPrinter(O, *MAI).printInstruction(MI);
57 void printOperand(const MachineInstr *MI, int OpNum,
58 const char* Modifier = 0);
59 void printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
60 printOperand(MI, OpNum);
62 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
63 const char* Modifier = 0);
64 void printCCOperand(const MachineInstr *MI, int OpNum);
65 void printMachineInstruction(const MachineInstr * MI);
66 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
68 const char *ExtraCode);
69 bool PrintAsmMemoryOperand(const MachineInstr *MI,
70 unsigned OpNo, unsigned AsmVariant,
71 const char *ExtraCode);
72 void EmitInstruction(const MachineInstr *MI);
74 void getAnalysisUsage(AnalysisUsage &AU) const {
75 AsmPrinter::getAnalysisUsage(AU);
79 } // end of anonymous namespace
82 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
83 const char* Modifier) {
84 const MachineOperand &MO = MI->getOperand(OpNum);
85 switch (MO.getType()) {
86 default: assert(0 && "Not implemented yet!");
87 case MachineOperand::MO_Register:
88 O << MSP430InstPrinter::getRegisterName(MO.getReg());
90 case MachineOperand::MO_Immediate:
91 if (!Modifier || strcmp(Modifier, "nohash"))
95 case MachineOperand::MO_MachineBasicBlock:
96 O << *MO.getMBB()->getSymbol(OutContext);
98 case MachineOperand::MO_GlobalAddress: {
99 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
100 uint64_t Offset = MO.getOffset();
102 // If the global address expression is a part of displacement field with a
103 // register base, we should not emit any prefix symbol here, e.g.
107 // Otherwise (!) msp430-as will silently miscompile the output :(
108 if (!Modifier || strcmp(Modifier, "nohash"))
109 O << (isMemOp ? '&' : '#');
111 O << '(' << Offset << '+';
113 O << *Mang->getSymbol(MO.getGlobal());
120 case MachineOperand::MO_ExternalSymbol: {
121 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
122 O << (isMemOp ? '&' : '#');
123 O << MAI->getGlobalPrefix() << MO.getSymbolName();
129 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
130 const char* Modifier) {
131 const MachineOperand &Base = MI->getOperand(OpNum);
132 const MachineOperand &Disp = MI->getOperand(OpNum+1);
134 // Print displacement first
136 // Imm here is in fact global address - print extra modifier.
137 if (Disp.isImm() && !Base.getReg())
139 printOperand(MI, OpNum+1, "nohash");
141 // Print register base field
144 printOperand(MI, OpNum);
149 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
150 switch (MI->getOperand(OpNum).getImm()) {
151 default: assert(0 && "Unknown cond");
152 case MSP430CC::COND_E: O << "eq"; break;
153 case MSP430CC::COND_NE: O << "ne"; break;
154 case MSP430CC::COND_HS: O << "hs"; break;
155 case MSP430CC::COND_LO: O << "lo"; break;
156 case MSP430CC::COND_GE: O << "ge"; break;
157 case MSP430CC::COND_L: O << 'l'; break;
161 /// PrintAsmOperand - Print out an operand for an inline asm expression.
163 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
165 const char *ExtraCode) {
166 // Does this asm operand have a single letter operand modifier?
167 if (ExtraCode && ExtraCode[0])
168 return true; // Unknown modifier.
170 printOperand(MI, OpNo);
174 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
175 unsigned OpNo, unsigned AsmVariant,
176 const char *ExtraCode) {
177 if (ExtraCode && ExtraCode[0]) {
178 return true; // Unknown modifier.
180 printSrcMemOperand(MI, OpNo);
184 //===----------------------------------------------------------------------===//
185 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
186 MSP430MCInstLower MCInstLowering(OutContext, *Mang, *this);
189 MCInstLowering.Lower(MI, TmpInst);
190 OutStreamer.EmitInstruction(TmpInst);
193 static MCInstPrinter *createMSP430MCInstPrinter(const Target &T,
194 unsigned SyntaxVariant,
195 const MCAsmInfo &MAI,
197 if (SyntaxVariant == 0)
198 return new MSP430InstPrinter(O, MAI);
202 // Force static initialization.
203 extern "C" void LLVMInitializeMSP430AsmPrinter() {
204 RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
205 TargetRegistry::RegisterMCInstPrinter(TheMSP430Target,
206 createMSP430MCInstPrinter);