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 #include "InstPrinter/MSP430InstPrinter.h"
17 #include "MSP430InstrInfo.h"
18 #include "MSP430MCInstLower.h"
19 #include "MSP430TargetMachine.h"
20 #include "llvm/CodeGen/AsmPrinter.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Mangler.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/MC/MCInst.h"
31 #include "llvm/MC/MCStreamer.h"
32 #include "llvm/MC/MCSymbol.h"
33 #include "llvm/Support/TargetRegistry.h"
34 #include "llvm/Support/raw_ostream.h"
37 #define DEBUG_TYPE "asm-printer"
40 class MSP430AsmPrinter : public AsmPrinter {
42 MSP430AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
43 : AsmPrinter(TM, std::move(Streamer)) {}
45 StringRef getPassName() const override { return "MSP430 Assembly Printer"; }
47 void printOperand(const MachineInstr *MI, int OpNum,
48 raw_ostream &O, const char* Modifier = nullptr);
49 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
51 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
52 unsigned AsmVariant, const char *ExtraCode,
53 raw_ostream &O) override;
54 bool PrintAsmMemoryOperand(const MachineInstr *MI,
55 unsigned OpNo, unsigned AsmVariant,
56 const char *ExtraCode, raw_ostream &O) override;
57 void EmitInstruction(const MachineInstr *MI) override;
59 } // end of anonymous namespace
62 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
63 raw_ostream &O, const char *Modifier) {
64 const MachineOperand &MO = MI->getOperand(OpNum);
65 switch (MO.getType()) {
66 default: llvm_unreachable("Not implemented yet!");
67 case MachineOperand::MO_Register:
68 O << MSP430InstPrinter::getRegisterName(MO.getReg());
70 case MachineOperand::MO_Immediate:
71 if (!Modifier || strcmp(Modifier, "nohash"))
75 case MachineOperand::MO_MachineBasicBlock:
76 MO.getMBB()->getSymbol()->print(O, MAI);
78 case MachineOperand::MO_GlobalAddress: {
79 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
80 uint64_t Offset = MO.getOffset();
82 // If the global address expression is a part of displacement field with a
83 // register base, we should not emit any prefix symbol here, e.g.
87 // Otherwise (!) msp430-as will silently miscompile the output :(
88 if (!Modifier || strcmp(Modifier, "nohash"))
89 O << (isMemOp ? '&' : '#');
91 O << '(' << Offset << '+';
93 getSymbol(MO.getGlobal())->print(O, MAI);
103 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
105 const MachineOperand &Base = MI->getOperand(OpNum);
106 const MachineOperand &Disp = MI->getOperand(OpNum+1);
108 // Print displacement first
110 // Imm here is in fact global address - print extra modifier.
111 if (Disp.isImm() && !Base.getReg())
113 printOperand(MI, OpNum+1, O, "nohash");
115 // Print register base field
118 printOperand(MI, OpNum, O);
123 /// PrintAsmOperand - Print out an operand for an inline asm expression.
125 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
127 const char *ExtraCode, raw_ostream &O) {
128 // Does this asm operand have a single letter operand modifier?
129 if (ExtraCode && ExtraCode[0])
130 return true; // Unknown modifier.
132 printOperand(MI, OpNo, O);
136 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
137 unsigned OpNo, unsigned AsmVariant,
138 const char *ExtraCode,
140 if (ExtraCode && ExtraCode[0]) {
141 return true; // Unknown modifier.
143 printSrcMemOperand(MI, OpNo, O);
147 //===----------------------------------------------------------------------===//
148 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
149 MSP430MCInstLower MCInstLowering(OutContext, *this);
152 MCInstLowering.Lower(MI, TmpInst);
153 EmitToStreamer(*OutStreamer, TmpInst);
156 // Force static initialization.
157 extern "C" void LLVMInitializeMSP430AsmPrinter() {
158 RegisterAsmPrinter<MSP430AsmPrinter> X(getTheMSP430Target());