LLVM  3.7.0
MSP430AsmPrinter.cpp
Go to the documentation of this file.
1 //===-- 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 
15 #include "MSP430.h"
17 #include "MSP430InstrInfo.h"
18 #include "MSP430MCInstLower.h"
19 #include "MSP430TargetMachine.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"
35 using namespace llvm;
36 
37 #define DEBUG_TYPE "asm-printer"
38 
39 namespace {
40  class MSP430AsmPrinter : public AsmPrinter {
41  public:
42  MSP430AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
43  : AsmPrinter(TM, std::move(Streamer)) {}
44 
45  const char *getPassName() const override {
46  return "MSP430 Assembly Printer";
47  }
48 
49  void printOperand(const MachineInstr *MI, int OpNum,
50  raw_ostream &O, const char* Modifier = nullptr);
51  void printSrcMemOperand(const MachineInstr *MI, int OpNum,
52  raw_ostream &O);
53  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
54  unsigned AsmVariant, const char *ExtraCode,
55  raw_ostream &O) override;
56  bool PrintAsmMemoryOperand(const MachineInstr *MI,
57  unsigned OpNo, unsigned AsmVariant,
58  const char *ExtraCode, raw_ostream &O) override;
59  void EmitInstruction(const MachineInstr *MI) override;
60  };
61 } // end of anonymous namespace
62 
63 
64 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
65  raw_ostream &O, const char *Modifier) {
66  const MachineOperand &MO = MI->getOperand(OpNum);
67  switch (MO.getType()) {
68  default: llvm_unreachable("Not implemented yet!");
71  return;
73  if (!Modifier || strcmp(Modifier, "nohash"))
74  O << '#';
75  O << MO.getImm();
76  return;
78  MO.getMBB()->getSymbol()->print(O, MAI);
79  return;
81  bool isMemOp = Modifier && !strcmp(Modifier, "mem");
82  uint64_t Offset = MO.getOffset();
83 
84  // If the global address expression is a part of displacement field with a
85  // register base, we should not emit any prefix symbol here, e.g.
86  // mov.w &foo, r1
87  // vs
88  // mov.w glb(r1), r2
89  // Otherwise (!) msp430-as will silently miscompile the output :(
90  if (!Modifier || strcmp(Modifier, "nohash"))
91  O << (isMemOp ? '&' : '#');
92  if (Offset)
93  O << '(' << Offset << '+';
94 
95  getSymbol(MO.getGlobal())->print(O, MAI);
96 
97  if (Offset)
98  O << ')';
99 
100  return;
101  }
102  }
103 }
104 
105 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
106  raw_ostream &O) {
107  const MachineOperand &Base = MI->getOperand(OpNum);
108  const MachineOperand &Disp = MI->getOperand(OpNum+1);
109 
110  // Print displacement first
111 
112  // Imm here is in fact global address - print extra modifier.
113  if (Disp.isImm() && !Base.getReg())
114  O << '&';
115  printOperand(MI, OpNum+1, O, "nohash");
116 
117  // Print register base field
118  if (Base.getReg()) {
119  O << '(';
120  printOperand(MI, OpNum, O);
121  O << ')';
122  }
123 }
124 
125 /// PrintAsmOperand - Print out an operand for an inline asm expression.
126 ///
127 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
128  unsigned AsmVariant,
129  const char *ExtraCode, raw_ostream &O) {
130  // Does this asm operand have a single letter operand modifier?
131  if (ExtraCode && ExtraCode[0])
132  return true; // Unknown modifier.
133 
134  printOperand(MI, OpNo, O);
135  return false;
136 }
137 
138 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
139  unsigned OpNo, unsigned AsmVariant,
140  const char *ExtraCode,
141  raw_ostream &O) {
142  if (ExtraCode && ExtraCode[0]) {
143  return true; // Unknown modifier.
144  }
145  printSrcMemOperand(MI, OpNo, O);
146  return false;
147 }
148 
149 //===----------------------------------------------------------------------===//
150 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
151  MSP430MCInstLower MCInstLowering(OutContext, *this);
152 
153  MCInst TmpInst;
154  MCInstLowering.Lower(MI, TmpInst);
155  EmitToStreamer(*OutStreamer, TmpInst);
156 }
157 
158 // Force static initialization.
161 }
const GlobalValue * getGlobal() const
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:51
MachineBasicBlock * getMBB() const
static const char * getRegisterName(unsigned RegNo)
MachineBasicBlock reference.
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
static void printOperand(raw_ostream &OS, uint8_t Opcode, unsigned OperandIdx, uint64_t Operand, uint64_t CodeAlignmentFactor, int64_t DataAlignmentFactor)
Print Opcode's operand number OperandIdx which has value Operand.
MSP430MCInstLower - This class is used to lower an MachineInstr into an MCInst.
int64_t getImm() const
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
Address of a global value.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:273
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:66
Target TheMSP430Target
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
int64_t getOffset() const
Return the offset from the symbol in this operand.
void LLVMInitializeMSP430AsmPrinter()
MCSymbol * getSymbol() const
getSymbol - Return the MCSymbol for this basic block.
MachineOperand class - Representation of each machine instruction operand.
Module.h This file contains the declarations for the Module class.
Representation of each machine instruction.
Definition: MachineInstr.h:51
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
unsigned getReg() const
getReg - Returns the register number.
RegisterAsmPrinter - Helper template for registering a target specific assembly printer, for use in the target machine initialization function.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
Primary interface to the complete machine description for the target machine.