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 "MSP430MCInstLower.h"
20 #include "MSP430TargetMachine.h"
21 #include "InstPrinter/MSP430InstPrinter.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/MachineModuleInfo.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineConstantPool.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/MC/MCInst.h"
32 #include "llvm/MC/MCStreamer.h"
33 #include "llvm/MC/MCSymbol.h"
34 #include "llvm/Target/Mangler.h"
35 #include "llvm/Target/TargetData.h"
36 #include "llvm/Target/TargetLoweringObjectFile.h"
37 #include "llvm/Target/TargetRegistry.h"
38 #include "llvm/Support/raw_ostream.h"
42 class MSP430AsmPrinter : public AsmPrinter {
44 MSP430AsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
45 : AsmPrinter(TM, Streamer) {}
47 virtual const char *getPassName() const {
48 return "MSP430 Assembly Printer";
51 void printOperand(const MachineInstr *MI, int OpNum,
52 raw_ostream &O, const char* Modifier = 0);
53 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
55 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
56 unsigned AsmVariant, const char *ExtraCode,
58 bool PrintAsmMemoryOperand(const MachineInstr *MI,
59 unsigned OpNo, unsigned AsmVariant,
60 const char *ExtraCode, raw_ostream &O);
61 void EmitInstruction(const MachineInstr *MI);
63 } // end of anonymous namespace
66 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
67 raw_ostream &O, const char *Modifier) {
68 const MachineOperand &MO = MI->getOperand(OpNum);
69 switch (MO.getType()) {
70 default: assert(0 && "Not implemented yet!");
71 case MachineOperand::MO_Register:
72 O << MSP430InstPrinter::getRegisterName(MO.getReg());
74 case MachineOperand::MO_Immediate:
75 if (!Modifier || strcmp(Modifier, "nohash"))
79 case MachineOperand::MO_MachineBasicBlock:
80 O << *MO.getMBB()->getSymbol();
82 case MachineOperand::MO_GlobalAddress: {
83 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
84 uint64_t Offset = MO.getOffset();
86 // If the global address expression is a part of displacement field with a
87 // register base, we should not emit any prefix symbol here, e.g.
91 // Otherwise (!) msp430-as will silently miscompile the output :(
92 if (!Modifier || strcmp(Modifier, "nohash"))
93 O << (isMemOp ? '&' : '#');
95 O << '(' << Offset << '+';
97 O << *Mang->getSymbol(MO.getGlobal());
104 case MachineOperand::MO_ExternalSymbol: {
105 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
106 O << (isMemOp ? '&' : '#');
107 O << MAI->getGlobalPrefix() << MO.getSymbolName();
113 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
115 const MachineOperand &Base = MI->getOperand(OpNum);
116 const MachineOperand &Disp = MI->getOperand(OpNum+1);
118 // Print displacement first
120 // Imm here is in fact global address - print extra modifier.
121 if (Disp.isImm() && !Base.getReg())
123 printOperand(MI, OpNum+1, O, "nohash");
125 // Print register base field
128 printOperand(MI, OpNum, O);
133 /// PrintAsmOperand - Print out an operand for an inline asm expression.
135 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
137 const char *ExtraCode, raw_ostream &O) {
138 // Does this asm operand have a single letter operand modifier?
139 if (ExtraCode && ExtraCode[0])
140 return true; // Unknown modifier.
142 printOperand(MI, OpNo, O);
146 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
147 unsigned OpNo, unsigned AsmVariant,
148 const char *ExtraCode,
150 if (ExtraCode && ExtraCode[0]) {
151 return true; // Unknown modifier.
153 printSrcMemOperand(MI, OpNo, O);
157 //===----------------------------------------------------------------------===//
158 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
159 MSP430MCInstLower MCInstLowering(OutContext, *Mang, *this);
162 MCInstLowering.Lower(MI, TmpInst);
163 OutStreamer.EmitInstruction(TmpInst);
166 static MCInstPrinter *createMSP430MCInstPrinter(const Target &T,
167 unsigned SyntaxVariant,
168 const MCAsmInfo &MAI) {
169 if (SyntaxVariant == 0)
170 return new MSP430InstPrinter(MAI);
174 // Force static initialization.
175 extern "C" void LLVMInitializeMSP430AsmPrinter() {
176 RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
177 TargetRegistry::RegisterMCInstPrinter(TheMSP430Target,
178 createMSP430MCInstPrinter);