blob: cb4047988eb91563615fc16c21c41d6636fcf88f [file] [log] [blame]
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001//===-- MipsAsmPrinter.cpp - Mips LLVM assembly writer --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to GAS-format MIPS assembly language.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "mips-asm-printer"
16
17#include "Mips.h"
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000018#include "MipsSubtarget.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000019#include "MipsInstrInfo.h"
20#include "MipsTargetMachine.h"
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000021#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000022#include "llvm/Constants.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Module.h"
Devang Patele4c0c0f2009-06-25 00:47:42 +000025#include "llvm/MDNode.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000026#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000027#include "llvm/CodeGen/DwarfWriter.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000028#include "llvm/CodeGen/MachineFunctionPass.h"
29#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000031#include "llvm/CodeGen/MachineInstr.h"
32#include "llvm/Target/TargetAsmInfo.h"
33#include "llvm/Target/TargetData.h"
34#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000035#include "llvm/Target/TargetOptions.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036#include "llvm/Support/Mangler.h"
37#include "llvm/ADT/Statistic.h"
38#include "llvm/ADT/StringExtras.h"
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000039#include "llvm/Support/Debug.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000040#include "llvm/Support/CommandLine.h"
41#include "llvm/Support/MathExtras.h"
Owen Andersoncb371882008-08-21 00:14:44 +000042#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000043#include <cctype>
44
45using namespace llvm;
46
47STATISTIC(EmittedInsts, "Number of machine instrs printed");
48
49namespace {
Bill Wendling57f0db82009-02-24 08:30:20 +000050 class VISIBILITY_HIDDEN MipsAsmPrinter : public AsmPrinter {
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000051 const MipsSubtarget *Subtarget;
Bill Wendling57f0db82009-02-24 08:30:20 +000052 public:
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000053 explicit MipsAsmPrinter(raw_ostream &O, MipsTargetMachine &TM,
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000054 const TargetAsmInfo *T, bool V)
55 : AsmPrinter(O, TM, T, V) {
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000056 Subtarget = &TM.getSubtarget<MipsSubtarget>();
57 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000058
59 virtual const char *getPassName() const {
60 return "Mips Assembly Printer";
61 }
62
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +000063 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
64 unsigned AsmVariant, const char *ExtraCode);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000065 void printOperand(const MachineInstr *MI, int opNum);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +000066 void printUnsignedImm(const MachineInstr *MI, int opNum);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000067 void printMemOperand(const MachineInstr *MI, int opNum,
68 const char *Modifier = 0);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000069 void printFCCOperand(const MachineInstr *MI, int opNum,
70 const char *Modifier = 0);
Anton Korobeynikovae408e62008-07-19 13:16:11 +000071 void printModuleLevelGV(const GlobalVariable* GVar);
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +000072 void printSavedRegsBitmask(MachineFunction &MF);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000073 void printHex32(unsigned int Value);
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000074
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000075 const char *emitCurrentABIString(void);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000076 void emitFunctionStart(MachineFunction &MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +000077 void emitFunctionEnd(MachineFunction &MF);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000078 void emitFrameDirective(MachineFunction &MF);
Anton Korobeynikovae408e62008-07-19 13:16:11 +000079
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000080 bool printInstruction(const MachineInstr *MI); // autogenerated.
81 bool runOnMachineFunction(MachineFunction &F);
82 bool doInitialization(Module &M);
83 bool doFinalization(Module &M);
84 };
85} // end of anonymous namespace
86
87#include "MipsGenAsmWriter.inc"
88
89/// createMipsCodePrinterPass - Returns a pass that prints the MIPS
90/// assembly code for a MachineFunction to the given output stream,
91/// using the given target machine description. This should work
92/// regardless of whether the function is in SSA form.
Owen Andersoncb371882008-08-21 00:14:44 +000093FunctionPass *llvm::createMipsCodePrinterPass(raw_ostream &o,
Bill Wendling57f0db82009-02-24 08:30:20 +000094 MipsTargetMachine &tm,
Bill Wendling98a366d2009-04-29 23:29:43 +000095 bool verbose) {
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000096 return new MipsAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000097}
98
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000099//===----------------------------------------------------------------------===//
100//
101// Mips Asm Directives
102//
103// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
104// Describe the stack frame.
105//
106// -- Mask directives "(f)mask bitmask, offset"
107// Tells the assembler which registers are saved and where.
108// bitmask - contain a little endian bitset indicating which registers are
109// saved on function prologue (e.g. with a 0x80000000 mask, the
110// assembler knows the register 31 (RA) is saved at prologue.
111// offset - the position before stack pointer subtraction indicating where
112// the first saved register on prologue is located. (e.g. with a
113//
114// Consider the following function prologue:
115//
Bill Wendling6ef781f2008-02-27 06:33:05 +0000116// .frame $fp,48,$ra
117// .mask 0xc0000000,-8
118// addiu $sp, $sp, -48
119// sw $ra, 40($sp)
120// sw $fp, 36($sp)
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000121//
122// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
123// 30 (FP) are saved at prologue. As the save order on prologue is from
124// left to right, RA is saved first. A -8 offset means that after the
125// stack pointer subtration, the first register in the mask (RA) will be
126// saved at address 48-8=40.
127//
128//===----------------------------------------------------------------------===//
129
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000130//===----------------------------------------------------------------------===//
131// Mask directives
132//===----------------------------------------------------------------------===//
133
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000134// Create a bitmask with all callee saved registers for CPU or Floating Point
135// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000136void MipsAsmPrinter::
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000137printSavedRegsBitmask(MachineFunction &MF)
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000138{
Dan Gohman6f0d0242008-02-10 18:45:23 +0000139 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000140 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000141
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000142 // CPU and FPU Saved Registers Bitmasks
143 unsigned int CPUBitmask = 0;
144 unsigned int FPUBitmask = 0;
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000145
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000146 // Set the CPU and FPU Bitmasks
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000147 MachineFrameInfo *MFI = MF.getFrameInfo();
148 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000149 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
150 unsigned RegNum = MipsRegisterInfo::getRegisterNumbering(CSI[i].getReg());
151 if (CSI[i].getRegClass() == Mips::CPURegsRegisterClass)
152 CPUBitmask |= (1 << RegNum);
153 else
154 FPUBitmask |= (1 << RegNum);
155 }
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000156
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000157 // Return Address and Frame registers must also be set in CPUBitmask.
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000158 if (RI.hasFP(MF))
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000159 CPUBitmask |= (1 << MipsRegisterInfo::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000160 getRegisterNumbering(RI.getFrameRegister(MF)));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000161
162 if (MF.getFrameInfo()->hasCalls())
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000163 CPUBitmask |= (1 << MipsRegisterInfo::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000164 getRegisterNumbering(RI.getRARegister()));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000165
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000166 // Print CPUBitmask
167 O << "\t.mask \t"; printHex32(CPUBitmask); O << ','
168 << MipsFI->getCPUTopSavedRegOff() << '\n';
169
170 // Print FPUBitmask
171 O << "\t.fmask\t"; printHex32(FPUBitmask); O << ","
172 << MipsFI->getFPUTopSavedRegOff() << '\n';
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000173}
174
175// Print a 32 bit hex number with all numbers.
176void MipsAsmPrinter::
177printHex32(unsigned int Value)
178{
Owen Andersoncb371882008-08-21 00:14:44 +0000179 O << "0x";
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000180 for (int i = 7; i >= 0; i--)
Owen Andersoncb371882008-08-21 00:14:44 +0000181 O << utohexstr( (Value & (0xF << (i*4))) >> (i*4) );
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000182}
183
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000184//===----------------------------------------------------------------------===//
185// Frame and Set directives
186//===----------------------------------------------------------------------===//
187
188/// Frame Directive
189void MipsAsmPrinter::
190emitFrameDirective(MachineFunction &MF)
191{
192 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
193
194 unsigned stackReg = RI.getFrameRegister(MF);
195 unsigned returnReg = RI.getRARegister();
196 unsigned stackSize = MF.getFrameInfo()->getStackSize();
197
198
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000199 O << "\t.frame\t" << '$' << LowercaseString(RI.get(stackReg).AsmName)
200 << ',' << stackSize << ','
201 << '$' << LowercaseString(RI.get(returnReg).AsmName)
202 << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000203}
204
205/// Emit Set directives.
206const char * MipsAsmPrinter::
207emitCurrentABIString(void)
208{
209 switch(Subtarget->getTargetABI()) {
210 case MipsSubtarget::O32: return "abi32";
211 case MipsSubtarget::O64: return "abiO64";
212 case MipsSubtarget::N32: return "abiN32";
213 case MipsSubtarget::N64: return "abi64";
214 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
215 default: break;
216 }
217
218 assert(0 && "Unknown Mips ABI");
219 return NULL;
220}
221
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000222/// Emit the directives used by GAS on the start of functions
223void MipsAsmPrinter::
224emitFunctionStart(MachineFunction &MF)
225{
226 // Print out the label for the function.
227 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000228 SwitchToSection(TAI->SectionForGlobal(F));
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000229
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000230 // 2 bits aligned
Bill Wendling20c568f2009-06-30 22:38:32 +0000231 EmitAlignment(MF.getAlignment(), F);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000232
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000233 O << "\t.globl\t" << CurrentFnName << '\n';
234 O << "\t.ent\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000235
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000236 printVisibility(CurrentFnName, F->getVisibility());
237
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000238 if ((TAI->hasDotTypeDotSizeDirective()) && Subtarget->isLinux())
239 O << "\t.type\t" << CurrentFnName << ", @function\n";
240
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000241 O << CurrentFnName << ":\n";
242
243 emitFrameDirective(MF);
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000244 printSavedRegsBitmask(MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000245
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000246 O << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000247}
248
249/// Emit the directives used by GAS on the end of functions
250void MipsAsmPrinter::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000251emitFunctionEnd(MachineFunction &MF)
252{
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000253 // There are instruction for this macros, but they must
254 // always be at the function end, and we can't emit and
255 // break with BB logic.
256 O << "\t.set\tmacro\n";
257 O << "\t.set\treorder\n";
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000258
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000259 O << "\t.end\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000260 if (TAI->hasDotTypeDotSizeDirective() && !Subtarget->isLinux())
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000261 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000262}
263
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000264/// runOnMachineFunction - This uses the printMachineInstruction()
265/// method to print assembly for each instruction.
266bool MipsAsmPrinter::
267runOnMachineFunction(MachineFunction &MF)
268{
Bill Wendling57f0db82009-02-24 08:30:20 +0000269 this->MF = &MF;
270
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000271 SetupMachineFunction(MF);
272
273 // Print out constants referenced by the function
274 EmitConstantPool(MF.getConstantPool());
275
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000276 // Print out jump tables referenced by the function
277 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
278
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000279 O << "\n\n";
280
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000281 // Emit the function start directives
282 emitFunctionStart(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000283
284 // Print out code for the function.
285 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
286 I != E; ++I) {
287
288 // Print a label for the basic block.
289 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000290 printBasicBlockLabel(I, true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000291 O << '\n';
292 }
293
294 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
295 II != E; ++II) {
296 // Print the assembly for the instruction.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000297 printInstruction(II);
298 ++EmittedInsts;
299 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000300
301 // Each Basic Block is separated by a newline
302 O << '\n';
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000303 }
304
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000305 // Emit function end directives
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000306 emitFunctionEnd(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000307
308 // We didn't modify anything.
309 return false;
310}
311
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000312// Print out an operand for an inline asm expression.
313bool MipsAsmPrinter::
314PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
315 unsigned AsmVariant, const char *ExtraCode)
316{
317 // Does this asm operand have a single letter operand modifier?
318 if (ExtraCode && ExtraCode[0])
319 return true; // Unknown modifier.
320
321 printOperand(MI, OpNo);
322 return false;
323}
324
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000325void MipsAsmPrinter::
326printOperand(const MachineInstr *MI, int opNum)
327{
328 const MachineOperand &MO = MI->getOperand(opNum);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000329 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000330 bool closeP = false;
331 bool isPIC = (TM.getRelocationModel() == Reloc::PIC_);
332 bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000333
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000334 // %hi and %lo used on mips gas to load global addresses on
335 // static code. %got is used to load global addresses when
336 // using PIC_. %call16 is used to load direct call targets
337 // on PIC_ and small code size. %call_lo and %call_hi load
338 // direct call targets on PIC_ and large code size.
Dan Gohmand735b802008-10-03 15:45:36 +0000339 if (MI->getOpcode() == Mips::LUi && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000340 if ((isPIC) && (isCodeLarge))
341 O << "%call_hi(";
342 else
343 O << "%hi(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000344 closeP = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000345 } else if ((MI->getOpcode() == Mips::ADDiu) && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000346 const MachineOperand &firstMO = MI->getOperand(opNum-1);
347 if (firstMO.getReg() == Mips::GP)
348 O << "%gp_rel(";
349 else
350 O << "%lo(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000351 closeP = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000352 } else if ((isPIC) && (MI->getOpcode() == Mips::LW) &&
353 (!MO.isReg()) && (!MO.isImm())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000354 const MachineOperand &firstMO = MI->getOperand(opNum-1);
355 const MachineOperand &lastMO = MI->getOperand(opNum+1);
Dan Gohmand735b802008-10-03 15:45:36 +0000356 if ((firstMO.isReg()) && (lastMO.isReg())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000357 if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() == Mips::GP)
358 && (!isCodeLarge))
359 O << "%call16(";
360 else if ((firstMO.getReg() != Mips::T9) && (lastMO.getReg() == Mips::GP))
361 O << "%got(";
362 else if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() != Mips::GP)
363 && (isCodeLarge))
364 O << "%call_lo(";
365 closeP = true;
366 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000367 }
368
369 switch (MO.getType())
370 {
371 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000372 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000373 O << '$' << LowercaseString (RI.get(MO.getReg()).AsmName);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000374 else
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000375 O << '$' << MO.getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000376 break;
377
378 case MachineOperand::MO_Immediate:
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000379 O << (short int)MO.getImm();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000380 break;
381
382 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000383 printBasicBlockLabel(MO.getMBB());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000384 return;
385
386 case MachineOperand::MO_GlobalAddress:
Rafael Espindolabb46f522009-01-15 20:18:42 +0000387 {
388 const GlobalValue *GV = MO.getGlobal();
389 O << Mang->getValueName(GV);
390 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000391 break;
392
393 case MachineOperand::MO_ExternalSymbol:
394 O << MO.getSymbolName();
395 break;
396
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000397 case MachineOperand::MO_JumpTableIndex:
398 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000399 << '_' << MO.getIndex();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000400 break;
401
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000402 case MachineOperand::MO_ConstantPoolIndex:
403 O << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000404 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000405 break;
406
407 default:
408 O << "<unknown operand type>"; abort (); break;
409 }
410
411 if (closeP) O << ")";
412}
413
414void MipsAsmPrinter::
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000415printUnsignedImm(const MachineInstr *MI, int opNum)
416{
417 const MachineOperand &MO = MI->getOperand(opNum);
418 if (MO.getType() == MachineOperand::MO_Immediate)
419 O << (unsigned short int)MO.getImm();
420 else
421 printOperand(MI, opNum);
422}
423
424void MipsAsmPrinter::
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000425printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier)
426{
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000427 // when using stack locations for not load/store instructions
428 // print the same way as all normal 3 operand instructions.
429 if (Modifier && !strcmp(Modifier, "stackloc")) {
430 printOperand(MI, opNum+1);
431 O << ", ";
432 printOperand(MI, opNum);
433 return;
434 }
435
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000436 // Load/Store memory operands -- imm($reg)
437 // If PIC target the target is loaded as the
438 // pattern lw $25,%call16($28)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000439 printOperand(MI, opNum);
440 O << "(";
441 printOperand(MI, opNum+1);
442 O << ")";
443}
444
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000445void MipsAsmPrinter::
446printFCCOperand(const MachineInstr *MI, int opNum, const char *Modifier)
447{
448 const MachineOperand& MO = MI->getOperand(opNum);
449 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
450}
451
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000452bool MipsAsmPrinter::
453doInitialization(Module &M)
454{
Rafael Espindolabb46f522009-01-15 20:18:42 +0000455 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000456
457 // Tell the assembler which ABI we are using
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000458 O << "\t.section .mdebug." << emitCurrentABIString() << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000459
460 // TODO: handle O64 ABI
461 if (Subtarget->isABI_EABI())
462 O << "\t.section .gcc_compiled_long" <<
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000463 (Subtarget->isGP32bit() ? "32" : "64") << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000464
465 // return to previous section
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000466 O << "\t.previous" << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000467
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000468 return false; // success
469}
470
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000471void MipsAsmPrinter::
472printModuleLevelGV(const GlobalVariable* GVar) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000473 const TargetData *TD = TM.getTargetData();
474
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000475 if (!GVar->hasInitializer())
476 return; // External global require no code
477
478 // Check to see if this is a special global used by LLVM, if so, emit it.
479 if (EmitSpecialLLVMGlobal(GVar))
480 return;
481
482 O << "\n\n";
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000483 std::string name = Mang->getValueName(GVar);
484 Constant *C = GVar->getInitializer();
Devang Patel0f05d222009-06-26 02:26:12 +0000485 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patele4c0c0f2009-06-25 00:47:42 +0000486 return;
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000487 const Type *CTy = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000488 unsigned Size = TD->getTypeAllocSize(CTy);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000489 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000490 bool printSizeAndType = true;
491
492 // A data structure or array is aligned in memory to the largest
493 // alignment boundary required by any data type inside it (this matches
494 // the Preferred Type Alignment). For integral types, the alignment is
495 // the type size.
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000496 unsigned Align;
497 if (CTy->getTypeID() == Type::IntegerTyID ||
498 CTy->getTypeID() == Type::VoidTyID) {
499 assert(!(Size & (Size-1)) && "Alignment is not a power of two!");
500 Align = Log2_32(Size);
501 } else
502 Align = TD->getPreferredTypeAlignmentShift(CTy);
503
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000504 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000505
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000506 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000507
508 if (C->isNullValue() && !GVar->hasSection()) {
509 if (!GVar->isThreadLocal() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000510 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000511 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
512
Rafael Espindolabb46f522009-01-15 20:18:42 +0000513 if (GVar->hasLocalLinkage())
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000514 O << "\t.local\t" << name << '\n';
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000515
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000516 O << TAI->getCOMMDirective() << name << ',' << Size;
517 if (TAI->getCOMMDirectiveTakesAlignment())
518 O << ',' << (1 << Align);
519
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000520 O << '\n';
521 return;
522 }
523 }
524 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000525 case GlobalValue::LinkOnceAnyLinkage:
526 case GlobalValue::LinkOnceODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000527 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000528 case GlobalValue::WeakAnyLinkage:
529 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000530 // FIXME: Verify correct for weak.
531 // Nonnull linkonce -> weak
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000532 O << "\t.weak " << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000533 break;
534 case GlobalValue::AppendingLinkage:
535 // FIXME: appending linkage variables should go into a section of their name
536 // or something. For now, just emit them as external.
537 case GlobalValue::ExternalLinkage:
538 // If external or appending, declare as a global symbol
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000539 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000540 // Fall Through
Rafael Espindolabb46f522009-01-15 20:18:42 +0000541 case GlobalValue::PrivateLinkage:
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000542 case GlobalValue::InternalLinkage:
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000543 if (CVA && CVA->isCString())
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000544 printSizeAndType = false;
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000545 break;
546 case GlobalValue::GhostLinkage:
547 cerr << "Should not have any unmaterialized functions!\n";
548 abort();
549 case GlobalValue::DLLImportLinkage:
550 cerr << "DLLImport linkage is not supported by this target!\n";
551 abort();
552 case GlobalValue::DLLExportLinkage:
553 cerr << "DLLExport linkage is not supported by this target!\n";
554 abort();
555 default:
556 assert(0 && "Unknown linkage type!");
557 }
558
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000559 EmitAlignment(Align, GVar);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000560
561 if (TAI->hasDotTypeDotSizeDirective() && printSizeAndType) {
562 O << "\t.type " << name << ",@object\n";
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000563 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000564 }
565
566 O << name << ":\n";
567 EmitGlobalConstant(C);
568}
569
570bool MipsAsmPrinter::
571doFinalization(Module &M)
572{
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000573 // Print out module-level global variables here.
574 for (Module::const_global_iterator I = M.global_begin(),
575 E = M.global_end(); I != E; ++I)
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000576 printModuleLevelGV(I);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000577
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000578 O << '\n';
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000579
Dan Gohmanb8275a32007-07-25 19:33:14 +0000580 return AsmPrinter::doFinalization(M);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000581}
Douglas Gregor1555a232009-06-16 20:12:29 +0000582
Chris Lattnere8f10182009-06-16 22:38:04 +0000583namespace {
584 static struct Register {
585 Register() {
586 MipsTargetMachine::registerAsmPrinter(createMipsCodePrinterPass);
587 }
588 } Registrator;
589}
590
Bob Wilsona96751f2009-06-23 23:59:40 +0000591// Force static initialization.
592extern "C" void LLVMInitializeMipsAsmPrinter() { }