blob: f39c21fc8aa2b0c158530a590e57222ce1b974f8 [file] [log] [blame]
Nick Lewycky0aee1612010-10-02 01:08:17 +00001//===-- MSP430AsmPrinter.cpp - MSP430 LLVM assembly writer ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to the MSP430 assembly language.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "InstPrinter/MSP430InstPrinter.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "MSP430.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000017#include "MSP430InstrInfo.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000018#include "MSP430MCInstLower.h"
19#include "MSP430TargetMachine.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000020#include "llvm/CodeGen/AsmPrinter.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000023#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Constants.h"
26#include "llvm/IR/DerivedTypes.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000027#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Module.h"
Evan Cheng1705ab02011-07-14 23:50:31 +000029#include "llvm/MC/MCAsmInfo.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000030#include "llvm/MC/MCInst.h"
31#include "llvm/MC/MCStreamer.h"
32#include "llvm/MC/MCSymbol.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000033#include "llvm/Support/TargetRegistry.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000034#include "llvm/Support/raw_ostream.h"
35using namespace llvm;
36
Chandler Carruth84e68b22014-04-22 02:41:26 +000037#define DEBUG_TYPE "asm-printer"
38
Nick Lewycky0aee1612010-10-02 01:08:17 +000039namespace {
40 class MSP430AsmPrinter : public AsmPrinter {
41 public:
David Blaikie94598322015-01-18 20:29:04 +000042 MSP430AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
43 : AsmPrinter(TM, std::move(Streamer)) {}
Nick Lewycky0aee1612010-10-02 01:08:17 +000044
Mehdi Amini117296c2016-10-01 02:56:57 +000045 StringRef getPassName() const override { return "MSP430 Assembly Printer"; }
Nick Lewycky0aee1612010-10-02 01:08:17 +000046
47 void printOperand(const MachineInstr *MI, int OpNum,
Craig Topper062a2ba2014-04-25 05:30:21 +000048 raw_ostream &O, const char* Modifier = nullptr);
Nick Lewycky0aee1612010-10-02 01:08:17 +000049 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
50 raw_ostream &O);
51 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
52 unsigned AsmVariant, const char *ExtraCode,
Craig Topper6f9e59e2014-04-29 07:58:09 +000053 raw_ostream &O) override;
Nick Lewycky0aee1612010-10-02 01:08:17 +000054 bool PrintAsmMemoryOperand(const MachineInstr *MI,
55 unsigned OpNo, unsigned AsmVariant,
Craig Topper6f9e59e2014-04-29 07:58:09 +000056 const char *ExtraCode, raw_ostream &O) override;
57 void EmitInstruction(const MachineInstr *MI) override;
Nick Lewycky0aee1612010-10-02 01:08:17 +000058 };
59} // end of anonymous namespace
60
61
62void 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()) {
Craig Toppere55c5562012-02-07 02:50:20 +000066 default: llvm_unreachable("Not implemented yet!");
Nick Lewycky0aee1612010-10-02 01:08:17 +000067 case MachineOperand::MO_Register:
68 O << MSP430InstPrinter::getRegisterName(MO.getReg());
69 return;
70 case MachineOperand::MO_Immediate:
71 if (!Modifier || strcmp(Modifier, "nohash"))
72 O << '#';
73 O << MO.getImm();
74 return;
75 case MachineOperand::MO_MachineBasicBlock:
Matt Arsenault8b643552015-06-09 00:31:39 +000076 MO.getMBB()->getSymbol()->print(O, MAI);
Nick Lewycky0aee1612010-10-02 01:08:17 +000077 return;
78 case MachineOperand::MO_GlobalAddress: {
79 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
80 uint64_t Offset = MO.getOffset();
81
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.
84 // mov.w &foo, r1
85 // vs
86 // mov.w glb(r1), r2
87 // Otherwise (!) msp430-as will silently miscompile the output :(
88 if (!Modifier || strcmp(Modifier, "nohash"))
89 O << (isMemOp ? '&' : '#');
90 if (Offset)
91 O << '(' << Offset << '+';
92
Matt Arsenault8b643552015-06-09 00:31:39 +000093 getSymbol(MO.getGlobal())->print(O, MAI);
Nick Lewycky0aee1612010-10-02 01:08:17 +000094
95 if (Offset)
96 O << ')';
97
98 return;
99 }
Nick Lewycky0aee1612010-10-02 01:08:17 +0000100 }
101}
102
103void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
104 raw_ostream &O) {
105 const MachineOperand &Base = MI->getOperand(OpNum);
106 const MachineOperand &Disp = MI->getOperand(OpNum+1);
107
108 // Print displacement first
109
110 // Imm here is in fact global address - print extra modifier.
111 if (Disp.isImm() && !Base.getReg())
112 O << '&';
113 printOperand(MI, OpNum+1, O, "nohash");
114
115 // Print register base field
116 if (Base.getReg()) {
117 O << '(';
118 printOperand(MI, OpNum, O);
119 O << ')';
120 }
121}
122
123/// PrintAsmOperand - Print out an operand for an inline asm expression.
124///
125bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
126 unsigned AsmVariant,
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.
131
132 printOperand(MI, OpNo, O);
133 return false;
134}
135
136bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
137 unsigned OpNo, unsigned AsmVariant,
138 const char *ExtraCode,
139 raw_ostream &O) {
140 if (ExtraCode && ExtraCode[0]) {
141 return true; // Unknown modifier.
142 }
143 printSrcMemOperand(MI, OpNo, O);
144 return false;
145}
146
147//===----------------------------------------------------------------------===//
148void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
Benjamin Kramer628a39f2012-06-06 18:25:08 +0000149 MSP430MCInstLower MCInstLowering(OutContext, *this);
Nick Lewycky0aee1612010-10-02 01:08:17 +0000150
151 MCInst TmpInst;
152 MCInstLowering.Lower(MI, TmpInst);
Lang Hames9ff69c82015-04-24 19:11:51 +0000153 EmitToStreamer(*OutStreamer, TmpInst);
Nick Lewycky0aee1612010-10-02 01:08:17 +0000154}
155
Nick Lewycky0aee1612010-10-02 01:08:17 +0000156// Force static initialization.
157extern "C" void LLVMInitializeMSP430AsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000158 RegisterAsmPrinter<MSP430AsmPrinter> X(getTheMSP430Target());
Nick Lewycky0aee1612010-10-02 01:08:17 +0000159}