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 O << (isMemOp ? '&' : '#');
103 O << '(' << Offset << '+';
105 O << *GetGlobalValueSymbol(MO.getGlobal());
112 case MachineOperand::MO_ExternalSymbol: {
113 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
114 O << (isMemOp ? '&' : '#');
115 O << MAI->getGlobalPrefix() << MO.getSymbolName();
121 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
122 const char* Modifier) {
123 const MachineOperand &Base = MI->getOperand(OpNum);
124 const MachineOperand &Disp = MI->getOperand(OpNum+1);
126 // Print displacement first
128 printOperand(MI, OpNum+1, "mem");
133 printOperand(MI, OpNum+1, "nohash");
137 // Print register base field
140 printOperand(MI, OpNum);
145 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
146 switch (MI->getOperand(OpNum).getImm()) {
147 default: assert(0 && "Unknown cond");
148 case MSP430CC::COND_E: O << "eq"; break;
149 case MSP430CC::COND_NE: O << "ne"; break;
150 case MSP430CC::COND_HS: O << "hs"; break;
151 case MSP430CC::COND_LO: O << "lo"; break;
152 case MSP430CC::COND_GE: O << "ge"; break;
153 case MSP430CC::COND_L: O << 'l'; break;
157 /// PrintAsmOperand - Print out an operand for an inline asm expression.
159 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
161 const char *ExtraCode) {
162 // Does this asm operand have a single letter operand modifier?
163 if (ExtraCode && ExtraCode[0])
164 return true; // Unknown modifier.
166 printOperand(MI, OpNo);
170 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
171 unsigned OpNo, unsigned AsmVariant,
172 const char *ExtraCode) {
173 if (ExtraCode && ExtraCode[0]) {
174 return true; // Unknown modifier.
176 printSrcMemOperand(MI, OpNo);
180 //===----------------------------------------------------------------------===//
181 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
182 MSP430MCInstLower MCInstLowering(OutContext, *Mang, *this);
185 MCInstLowering.Lower(MI, TmpInst);
186 printMCInst(&TmpInst);
190 static MCInstPrinter *createMSP430MCInstPrinter(const Target &T,
191 unsigned SyntaxVariant,
192 const MCAsmInfo &MAI,
194 if (SyntaxVariant == 0)
195 return new MSP430InstPrinter(O, MAI);
199 // Force static initialization.
200 extern "C" void LLVMInitializeMSP430AsmPrinter() {
201 RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
202 TargetRegistry::RegisterMCInstPrinter(TheMSP430Target,
203 createMSP430MCInstPrinter);