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/Support/FormattedStream.h"
42 class MSP430AsmPrinter : public AsmPrinter {
44 MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
45 MCContext &Ctx, MCStreamer &Streamer,
47 : AsmPrinter(O, TM, Ctx, Streamer, MAI) {}
49 virtual const char *getPassName() const {
50 return "MSP430 Assembly Printer";
53 void printMCInst(const MCInst *MI) {
54 MSP430InstPrinter(O, *MAI).printInstruction(MI);
56 void printOperand(const MachineInstr *MI, int OpNum,
57 const char* Modifier = 0);
58 void printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
59 printOperand(MI, OpNum);
61 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
62 const char* Modifier = 0);
63 void printCCOperand(const MachineInstr *MI, int OpNum);
64 void printMachineInstruction(const MachineInstr * MI);
65 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
67 const char *ExtraCode);
68 bool PrintAsmMemoryOperand(const MachineInstr *MI,
69 unsigned OpNo, unsigned AsmVariant,
70 const char *ExtraCode);
71 void EmitInstruction(const MachineInstr *MI);
73 void getAnalysisUsage(AnalysisUsage &AU) const {
74 AsmPrinter::getAnalysisUsage(AU);
78 } // end of anonymous namespace
81 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
82 const char* Modifier) {
83 const MachineOperand &MO = MI->getOperand(OpNum);
84 switch (MO.getType()) {
85 default: assert(0 && "Not implemented yet!");
86 case MachineOperand::MO_Register:
87 O << MSP430InstPrinter::getRegisterName(MO.getReg());
89 case MachineOperand::MO_Immediate:
90 if (!Modifier || strcmp(Modifier, "nohash"))
94 case MachineOperand::MO_MachineBasicBlock:
95 O << *MO.getMBB()->getSymbol(OutContext);
97 case MachineOperand::MO_GlobalAddress: {
98 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
99 uint64_t Offset = MO.getOffset();
101 // If the global address expression is a part of displacement field with a
102 // register base, we should not emit any prefix symbol here, e.g.
106 // Otherwise (!) msp430-as will silently miscompile the output :(
107 if (!Modifier || strcmp(Modifier, "nohash"))
108 O << (isMemOp ? '&' : '#');
110 O << '(' << Offset << '+';
112 O << *GetGlobalValueSymbol(MO.getGlobal());
119 case MachineOperand::MO_ExternalSymbol: {
120 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
121 O << (isMemOp ? '&' : '#');
122 O << MAI->getGlobalPrefix() << MO.getSymbolName();
128 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
129 const char* Modifier) {
130 const MachineOperand &Base = MI->getOperand(OpNum);
131 const MachineOperand &Disp = MI->getOperand(OpNum+1);
133 // Print displacement first
135 // Imm here is in fact global address - print extra modifier.
136 if (Disp.isImm() && !Base.getReg())
138 printOperand(MI, OpNum+1, "nohash");
140 // Print register base field
143 printOperand(MI, OpNum);
148 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
149 switch (MI->getOperand(OpNum).getImm()) {
150 default: assert(0 && "Unknown cond");
151 case MSP430CC::COND_E: O << "eq"; break;
152 case MSP430CC::COND_NE: O << "ne"; break;
153 case MSP430CC::COND_HS: O << "hs"; break;
154 case MSP430CC::COND_LO: O << "lo"; break;
155 case MSP430CC::COND_GE: O << "ge"; break;
156 case MSP430CC::COND_L: O << 'l'; break;
160 /// PrintAsmOperand - Print out an operand for an inline asm expression.
162 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
164 const char *ExtraCode) {
165 // Does this asm operand have a single letter operand modifier?
166 if (ExtraCode && ExtraCode[0])
167 return true; // Unknown modifier.
169 printOperand(MI, OpNo);
173 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
174 unsigned OpNo, unsigned AsmVariant,
175 const char *ExtraCode) {
176 if (ExtraCode && ExtraCode[0]) {
177 return true; // Unknown modifier.
179 printSrcMemOperand(MI, OpNo);
183 //===----------------------------------------------------------------------===//
184 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
185 MSP430MCInstLower MCInstLowering(OutContext, *Mang, *this);
188 MCInstLowering.Lower(MI, TmpInst);
189 OutStreamer.EmitInstruction(TmpInst);
192 static MCInstPrinter *createMSP430MCInstPrinter(const Target &T,
193 unsigned SyntaxVariant,
194 const MCAsmInfo &MAI,
196 if (SyntaxVariant == 0)
197 return new MSP430InstPrinter(O, MAI);
201 // Force static initialization.
202 extern "C" void LLVMInitializeMSP430AsmPrinter() {
203 RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
204 TargetRegistry::RegisterMCInstPrinter(TheMSP430Target,
205 createMSP430MCInstPrinter);