blob: 9eda5e222cf5cc89dd57e06ca24b5aeb26389173 [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"
Daniel Dunbar51b198a2009-07-15 20:24:03 +000036#include "llvm/Target/TargetRegistry.h"
Torok Edwin804e0fe2009-07-08 19:04:27 +000037#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000038#include "llvm/Support/Mangler.h"
39#include "llvm/ADT/Statistic.h"
40#include "llvm/ADT/StringExtras.h"
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000041#include "llvm/Support/Debug.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000042#include "llvm/Support/CommandLine.h"
David Greene71847812009-07-14 20:18:05 +000043#include "llvm/Support/FormattedStream.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000044#include "llvm/Support/MathExtras.h"
45#include <cctype>
46
47using namespace llvm;
48
49STATISTIC(EmittedInsts, "Number of machine instrs printed");
50
51namespace {
Bill Wendling57f0db82009-02-24 08:30:20 +000052 class VISIBILITY_HIDDEN MipsAsmPrinter : public AsmPrinter {
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000053 const MipsSubtarget *Subtarget;
Bill Wendling57f0db82009-02-24 08:30:20 +000054 public:
Daniel Dunbar51b198a2009-07-15 20:24:03 +000055 explicit MipsAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000056 const TargetAsmInfo *T, bool V)
57 : AsmPrinter(O, TM, T, V) {
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000058 Subtarget = &TM.getSubtarget<MipsSubtarget>();
59 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000060
61 virtual const char *getPassName() const {
62 return "Mips Assembly Printer";
63 }
64
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +000065 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
66 unsigned AsmVariant, const char *ExtraCode);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000067 void printOperand(const MachineInstr *MI, int opNum);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +000068 void printUnsignedImm(const MachineInstr *MI, int opNum);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000069 void printMemOperand(const MachineInstr *MI, int opNum,
70 const char *Modifier = 0);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000071 void printFCCOperand(const MachineInstr *MI, int opNum,
72 const char *Modifier = 0);
Chris Lattner40bbebd2009-07-21 18:38:57 +000073 void PrintGlobalVariable(const GlobalVariable *GVar);
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +000074 void printSavedRegsBitmask(MachineFunction &MF);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000075 void printHex32(unsigned int Value);
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000076
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000077 const char *emitCurrentABIString(void);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000078 void emitFunctionStart(MachineFunction &MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +000079 void emitFunctionEnd(MachineFunction &MF);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000080 void emitFrameDirective(MachineFunction &MF);
Anton Korobeynikovae408e62008-07-19 13:16:11 +000081
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000082 bool printInstruction(const MachineInstr *MI); // autogenerated.
83 bool runOnMachineFunction(MachineFunction &F);
84 bool doInitialization(Module &M);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000085 };
86} // end of anonymous namespace
87
88#include "MipsGenAsmWriter.inc"
89
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000090//===----------------------------------------------------------------------===//
91//
92// Mips Asm Directives
93//
94// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
95// Describe the stack frame.
96//
97// -- Mask directives "(f)mask bitmask, offset"
98// Tells the assembler which registers are saved and where.
99// bitmask - contain a little endian bitset indicating which registers are
100// saved on function prologue (e.g. with a 0x80000000 mask, the
101// assembler knows the register 31 (RA) is saved at prologue.
102// offset - the position before stack pointer subtraction indicating where
103// the first saved register on prologue is located. (e.g. with a
104//
105// Consider the following function prologue:
106//
Bill Wendling6ef781f2008-02-27 06:33:05 +0000107// .frame $fp,48,$ra
108// .mask 0xc0000000,-8
109// addiu $sp, $sp, -48
110// sw $ra, 40($sp)
111// sw $fp, 36($sp)
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000112//
113// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
114// 30 (FP) are saved at prologue. As the save order on prologue is from
115// left to right, RA is saved first. A -8 offset means that after the
116// stack pointer subtration, the first register in the mask (RA) will be
117// saved at address 48-8=40.
118//
119//===----------------------------------------------------------------------===//
120
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000121//===----------------------------------------------------------------------===//
122// Mask directives
123//===----------------------------------------------------------------------===//
124
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000125// Create a bitmask with all callee saved registers for CPU or Floating Point
126// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000127void MipsAsmPrinter::
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000128printSavedRegsBitmask(MachineFunction &MF)
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000129{
Dan Gohman6f0d0242008-02-10 18:45:23 +0000130 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000131 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000132
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000133 // CPU and FPU Saved Registers Bitmasks
134 unsigned int CPUBitmask = 0;
135 unsigned int FPUBitmask = 0;
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000136
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000137 // Set the CPU and FPU Bitmasks
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000138 MachineFrameInfo *MFI = MF.getFrameInfo();
139 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000140 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
141 unsigned RegNum = MipsRegisterInfo::getRegisterNumbering(CSI[i].getReg());
142 if (CSI[i].getRegClass() == Mips::CPURegsRegisterClass)
143 CPUBitmask |= (1 << RegNum);
144 else
145 FPUBitmask |= (1 << RegNum);
146 }
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000147
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000148 // Return Address and Frame registers must also be set in CPUBitmask.
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000149 if (RI.hasFP(MF))
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000150 CPUBitmask |= (1 << MipsRegisterInfo::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000151 getRegisterNumbering(RI.getFrameRegister(MF)));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000152
153 if (MF.getFrameInfo()->hasCalls())
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000154 CPUBitmask |= (1 << MipsRegisterInfo::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000155 getRegisterNumbering(RI.getRARegister()));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000156
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000157 // Print CPUBitmask
158 O << "\t.mask \t"; printHex32(CPUBitmask); O << ','
159 << MipsFI->getCPUTopSavedRegOff() << '\n';
160
161 // Print FPUBitmask
162 O << "\t.fmask\t"; printHex32(FPUBitmask); O << ","
163 << MipsFI->getFPUTopSavedRegOff() << '\n';
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000164}
165
166// Print a 32 bit hex number with all numbers.
167void MipsAsmPrinter::
168printHex32(unsigned int Value)
169{
Owen Andersoncb371882008-08-21 00:14:44 +0000170 O << "0x";
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000171 for (int i = 7; i >= 0; i--)
Owen Andersoncb371882008-08-21 00:14:44 +0000172 O << utohexstr( (Value & (0xF << (i*4))) >> (i*4) );
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000173}
174
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000175//===----------------------------------------------------------------------===//
176// Frame and Set directives
177//===----------------------------------------------------------------------===//
178
179/// Frame Directive
180void MipsAsmPrinter::
181emitFrameDirective(MachineFunction &MF)
182{
183 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
184
185 unsigned stackReg = RI.getFrameRegister(MF);
186 unsigned returnReg = RI.getRARegister();
187 unsigned stackSize = MF.getFrameInfo()->getStackSize();
188
189
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000190 O << "\t.frame\t" << '$' << LowercaseString(RI.get(stackReg).AsmName)
191 << ',' << stackSize << ','
192 << '$' << LowercaseString(RI.get(returnReg).AsmName)
193 << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000194}
195
196/// Emit Set directives.
197const char * MipsAsmPrinter::
198emitCurrentABIString(void)
199{
200 switch(Subtarget->getTargetABI()) {
201 case MipsSubtarget::O32: return "abi32";
202 case MipsSubtarget::O64: return "abiO64";
203 case MipsSubtarget::N32: return "abiN32";
204 case MipsSubtarget::N64: return "abi64";
205 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
206 default: break;
207 }
208
Torok Edwinc23197a2009-07-14 16:55:14 +0000209 llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000210 return NULL;
211}
212
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000213/// Emit the directives used by GAS on the start of functions
214void MipsAsmPrinter::
215emitFunctionStart(MachineFunction &MF)
216{
217 // Print out the label for the function.
218 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000219 SwitchToSection(TAI->SectionForGlobal(F));
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000220
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000221 // 2 bits aligned
Bill Wendling20c568f2009-06-30 22:38:32 +0000222 EmitAlignment(MF.getAlignment(), F);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000223
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000224 O << "\t.globl\t" << CurrentFnName << '\n';
225 O << "\t.ent\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000226
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000227 printVisibility(CurrentFnName, F->getVisibility());
228
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000229 if ((TAI->hasDotTypeDotSizeDirective()) && Subtarget->isLinux())
230 O << "\t.type\t" << CurrentFnName << ", @function\n";
231
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000232 O << CurrentFnName << ":\n";
233
234 emitFrameDirective(MF);
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000235 printSavedRegsBitmask(MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000236
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000237 O << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000238}
239
240/// Emit the directives used by GAS on the end of functions
241void MipsAsmPrinter::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000242emitFunctionEnd(MachineFunction &MF)
243{
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000244 // There are instruction for this macros, but they must
245 // always be at the function end, and we can't emit and
246 // break with BB logic.
247 O << "\t.set\tmacro\n";
248 O << "\t.set\treorder\n";
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000249
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000250 O << "\t.end\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000251 if (TAI->hasDotTypeDotSizeDirective() && !Subtarget->isLinux())
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000252 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000253}
254
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000255/// runOnMachineFunction - This uses the printMachineInstruction()
256/// method to print assembly for each instruction.
257bool MipsAsmPrinter::
258runOnMachineFunction(MachineFunction &MF)
259{
Bill Wendling57f0db82009-02-24 08:30:20 +0000260 this->MF = &MF;
261
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000262 SetupMachineFunction(MF);
263
264 // Print out constants referenced by the function
265 EmitConstantPool(MF.getConstantPool());
266
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000267 // Print out jump tables referenced by the function
268 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
269
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000270 O << "\n\n";
271
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000272 // Emit the function start directives
273 emitFunctionStart(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000274
275 // Print out code for the function.
276 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
277 I != E; ++I) {
278
279 // Print a label for the basic block.
280 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000281 printBasicBlockLabel(I, true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000282 O << '\n';
283 }
284
285 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
286 II != E; ++II) {
287 // Print the assembly for the instruction.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000288 printInstruction(II);
289 ++EmittedInsts;
290 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000291
292 // Each Basic Block is separated by a newline
293 O << '\n';
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000294 }
295
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000296 // Emit function end directives
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000297 emitFunctionEnd(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000298
299 // We didn't modify anything.
300 return false;
301}
302
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000303// Print out an operand for an inline asm expression.
304bool MipsAsmPrinter::
305PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
306 unsigned AsmVariant, const char *ExtraCode)
307{
308 // Does this asm operand have a single letter operand modifier?
309 if (ExtraCode && ExtraCode[0])
310 return true; // Unknown modifier.
311
312 printOperand(MI, OpNo);
313 return false;
314}
315
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000316void MipsAsmPrinter::
317printOperand(const MachineInstr *MI, int opNum)
318{
319 const MachineOperand &MO = MI->getOperand(opNum);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000320 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000321 bool closeP = false;
322 bool isPIC = (TM.getRelocationModel() == Reloc::PIC_);
323 bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000324
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000325 // %hi and %lo used on mips gas to load global addresses on
326 // static code. %got is used to load global addresses when
327 // using PIC_. %call16 is used to load direct call targets
328 // on PIC_ and small code size. %call_lo and %call_hi load
329 // direct call targets on PIC_ and large code size.
Dan Gohmand735b802008-10-03 15:45:36 +0000330 if (MI->getOpcode() == Mips::LUi && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000331 if ((isPIC) && (isCodeLarge))
332 O << "%call_hi(";
333 else
334 O << "%hi(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000335 closeP = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000336 } else if ((MI->getOpcode() == Mips::ADDiu) && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000337 const MachineOperand &firstMO = MI->getOperand(opNum-1);
338 if (firstMO.getReg() == Mips::GP)
339 O << "%gp_rel(";
340 else
341 O << "%lo(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000342 closeP = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000343 } else if ((isPIC) && (MI->getOpcode() == Mips::LW) &&
344 (!MO.isReg()) && (!MO.isImm())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000345 const MachineOperand &firstMO = MI->getOperand(opNum-1);
346 const MachineOperand &lastMO = MI->getOperand(opNum+1);
Dan Gohmand735b802008-10-03 15:45:36 +0000347 if ((firstMO.isReg()) && (lastMO.isReg())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000348 if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() == Mips::GP)
349 && (!isCodeLarge))
350 O << "%call16(";
351 else if ((firstMO.getReg() != Mips::T9) && (lastMO.getReg() == Mips::GP))
352 O << "%got(";
353 else if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() != Mips::GP)
354 && (isCodeLarge))
355 O << "%call_lo(";
356 closeP = true;
357 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000358 }
359
360 switch (MO.getType())
361 {
362 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000363 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000364 O << '$' << LowercaseString (RI.get(MO.getReg()).AsmName);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000365 else
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000366 O << '$' << MO.getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000367 break;
368
369 case MachineOperand::MO_Immediate:
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000370 O << (short int)MO.getImm();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000371 break;
372
373 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000374 printBasicBlockLabel(MO.getMBB());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000375 return;
376
377 case MachineOperand::MO_GlobalAddress:
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000378 O << Mang->getMangledName(MO.getGlobal());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000379 break;
380
381 case MachineOperand::MO_ExternalSymbol:
382 O << MO.getSymbolName();
383 break;
384
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000385 case MachineOperand::MO_JumpTableIndex:
386 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000387 << '_' << MO.getIndex();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000388 break;
389
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000390 case MachineOperand::MO_ConstantPoolIndex:
391 O << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000392 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000393 break;
394
395 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000396 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000397 }
398
399 if (closeP) O << ")";
400}
401
402void MipsAsmPrinter::
Chris Lattnerb34e3a92009-07-21 17:39:48 +0000403printUnsignedImm(const MachineInstr *MI, int opNum) {
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000404 const MachineOperand &MO = MI->getOperand(opNum);
405 if (MO.getType() == MachineOperand::MO_Immediate)
406 O << (unsigned short int)MO.getImm();
407 else
408 printOperand(MI, opNum);
409}
410
411void MipsAsmPrinter::
Chris Lattnerb34e3a92009-07-21 17:39:48 +0000412printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier) {
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000413 // when using stack locations for not load/store instructions
414 // print the same way as all normal 3 operand instructions.
415 if (Modifier && !strcmp(Modifier, "stackloc")) {
416 printOperand(MI, opNum+1);
417 O << ", ";
418 printOperand(MI, opNum);
419 return;
420 }
421
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000422 // Load/Store memory operands -- imm($reg)
423 // If PIC target the target is loaded as the
424 // pattern lw $25,%call16($28)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000425 printOperand(MI, opNum);
426 O << "(";
427 printOperand(MI, opNum+1);
428 O << ")";
429}
430
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000431void MipsAsmPrinter::
Chris Lattnerb34e3a92009-07-21 17:39:48 +0000432printFCCOperand(const MachineInstr *MI, int opNum, const char *Modifier) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000433 const MachineOperand& MO = MI->getOperand(opNum);
434 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
435}
436
Chris Lattnerb34e3a92009-07-21 17:39:48 +0000437bool MipsAsmPrinter::doInitialization(Module &M) {
438 // FIXME: Use SwitchToDataSection.
439
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000440 // Tell the assembler which ABI we are using
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000441 O << "\t.section .mdebug." << emitCurrentABIString() << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000442
443 // TODO: handle O64 ABI
444 if (Subtarget->isABI_EABI())
445 O << "\t.section .gcc_compiled_long" <<
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000446 (Subtarget->isGP32bit() ? "32" : "64") << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000447
448 // return to previous section
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000449 O << "\t.previous" << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000450
Chris Lattnerb34e3a92009-07-21 17:39:48 +0000451 return AsmPrinter::doInitialization(M);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000452}
453
Chris Lattner40bbebd2009-07-21 18:38:57 +0000454void MipsAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000455 const TargetData *TD = TM.getTargetData();
456
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000457 if (!GVar->hasInitializer())
458 return; // External global require no code
459
460 // Check to see if this is a special global used by LLVM, if so, emit it.
461 if (EmitSpecialLLVMGlobal(GVar))
462 return;
463
464 O << "\n\n";
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000465 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000466 Constant *C = GVar->getInitializer();
Devang Patel0f05d222009-06-26 02:26:12 +0000467 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patele4c0c0f2009-06-25 00:47:42 +0000468 return;
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000469 const Type *CTy = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000470 unsigned Size = TD->getTypeAllocSize(CTy);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000471 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000472 bool printSizeAndType = true;
473
474 // A data structure or array is aligned in memory to the largest
475 // alignment boundary required by any data type inside it (this matches
476 // the Preferred Type Alignment). For integral types, the alignment is
477 // the type size.
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000478 unsigned Align;
479 if (CTy->getTypeID() == Type::IntegerTyID ||
480 CTy->getTypeID() == Type::VoidTyID) {
481 assert(!(Size & (Size-1)) && "Alignment is not a power of two!");
482 Align = Log2_32(Size);
483 } else
484 Align = TD->getPreferredTypeAlignmentShift(CTy);
485
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000486 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000487
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000488 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000489
490 if (C->isNullValue() && !GVar->hasSection()) {
491 if (!GVar->isThreadLocal() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000492 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000493 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
494
Rafael Espindolabb46f522009-01-15 20:18:42 +0000495 if (GVar->hasLocalLinkage())
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000496 O << "\t.local\t" << name << '\n';
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000497
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000498 O << TAI->getCOMMDirective() << name << ',' << Size;
499 if (TAI->getCOMMDirectiveTakesAlignment())
500 O << ',' << (1 << Align);
501
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000502 O << '\n';
503 return;
504 }
505 }
506 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000507 case GlobalValue::LinkOnceAnyLinkage:
508 case GlobalValue::LinkOnceODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000509 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000510 case GlobalValue::WeakAnyLinkage:
511 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000512 // FIXME: Verify correct for weak.
513 // Nonnull linkonce -> weak
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000514 O << "\t.weak " << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000515 break;
516 case GlobalValue::AppendingLinkage:
517 // FIXME: appending linkage variables should go into a section of their name
518 // or something. For now, just emit them as external.
519 case GlobalValue::ExternalLinkage:
520 // If external or appending, declare as a global symbol
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000521 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000522 // Fall Through
Rafael Espindolabb46f522009-01-15 20:18:42 +0000523 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000524 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000525 case GlobalValue::InternalLinkage:
Owen Anderson1ca29d32009-07-13 21:27:19 +0000526 if (CVA && CVA->isCString())
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000527 printSizeAndType = false;
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000528 break;
529 case GlobalValue::GhostLinkage:
Torok Edwinc23197a2009-07-14 16:55:14 +0000530 llvm_unreachable("Should not have any unmaterialized functions!");
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000531 case GlobalValue::DLLImportLinkage:
Torok Edwinc23197a2009-07-14 16:55:14 +0000532 llvm_unreachable("DLLImport linkage is not supported by this target!");
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000533 case GlobalValue::DLLExportLinkage:
Torok Edwinc23197a2009-07-14 16:55:14 +0000534 llvm_unreachable("DLLExport linkage is not supported by this target!");
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000535 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000536 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000537 }
538
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000539 EmitAlignment(Align, GVar);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000540
541 if (TAI->hasDotTypeDotSizeDirective() && printSizeAndType) {
542 O << "\t.type " << name << ",@object\n";
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000543 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000544 }
545
546 O << name << ":\n";
547 EmitGlobalConstant(C);
548}
549
Douglas Gregor1555a232009-06-16 20:12:29 +0000550
Bob Wilsona96751f2009-06-23 23:59:40 +0000551// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000552extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbar0c795d62009-07-25 06:49:55 +0000553 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
554 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000555}