blob: 132c9b0000b569adf72ef34c729d5380c8252a82 [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
90/// createMipsCodePrinterPass - Returns a pass that prints the MIPS
91/// assembly code for a MachineFunction to the given output stream,
92/// using the given target machine description. This should work
93/// regardless of whether the function is in SSA form.
David Greene71847812009-07-14 20:18:05 +000094FunctionPass *llvm::createMipsCodePrinterPass(formatted_raw_ostream &o,
Daniel Dunbar51b198a2009-07-15 20:24:03 +000095 TargetMachine &tm,
Bill Wendling98a366d2009-04-29 23:29:43 +000096 bool verbose) {
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000097 return new MipsAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000098}
99
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000100//===----------------------------------------------------------------------===//
101//
102// Mips Asm Directives
103//
104// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
105// Describe the stack frame.
106//
107// -- Mask directives "(f)mask bitmask, offset"
108// Tells the assembler which registers are saved and where.
109// bitmask - contain a little endian bitset indicating which registers are
110// saved on function prologue (e.g. with a 0x80000000 mask, the
111// assembler knows the register 31 (RA) is saved at prologue.
112// offset - the position before stack pointer subtraction indicating where
113// the first saved register on prologue is located. (e.g. with a
114//
115// Consider the following function prologue:
116//
Bill Wendling6ef781f2008-02-27 06:33:05 +0000117// .frame $fp,48,$ra
118// .mask 0xc0000000,-8
119// addiu $sp, $sp, -48
120// sw $ra, 40($sp)
121// sw $fp, 36($sp)
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000122//
123// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
124// 30 (FP) are saved at prologue. As the save order on prologue is from
125// left to right, RA is saved first. A -8 offset means that after the
126// stack pointer subtration, the first register in the mask (RA) will be
127// saved at address 48-8=40.
128//
129//===----------------------------------------------------------------------===//
130
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000131//===----------------------------------------------------------------------===//
132// Mask directives
133//===----------------------------------------------------------------------===//
134
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000135// Create a bitmask with all callee saved registers for CPU or Floating Point
136// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000137void MipsAsmPrinter::
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000138printSavedRegsBitmask(MachineFunction &MF)
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000139{
Dan Gohman6f0d0242008-02-10 18:45:23 +0000140 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000141 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000142
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000143 // CPU and FPU Saved Registers Bitmasks
144 unsigned int CPUBitmask = 0;
145 unsigned int FPUBitmask = 0;
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000146
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000147 // Set the CPU and FPU Bitmasks
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000148 MachineFrameInfo *MFI = MF.getFrameInfo();
149 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000150 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
151 unsigned RegNum = MipsRegisterInfo::getRegisterNumbering(CSI[i].getReg());
152 if (CSI[i].getRegClass() == Mips::CPURegsRegisterClass)
153 CPUBitmask |= (1 << RegNum);
154 else
155 FPUBitmask |= (1 << RegNum);
156 }
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000157
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000158 // Return Address and Frame registers must also be set in CPUBitmask.
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000159 if (RI.hasFP(MF))
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000160 CPUBitmask |= (1 << MipsRegisterInfo::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000161 getRegisterNumbering(RI.getFrameRegister(MF)));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000162
163 if (MF.getFrameInfo()->hasCalls())
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000164 CPUBitmask |= (1 << MipsRegisterInfo::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000165 getRegisterNumbering(RI.getRARegister()));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000166
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000167 // Print CPUBitmask
168 O << "\t.mask \t"; printHex32(CPUBitmask); O << ','
169 << MipsFI->getCPUTopSavedRegOff() << '\n';
170
171 // Print FPUBitmask
172 O << "\t.fmask\t"; printHex32(FPUBitmask); O << ","
173 << MipsFI->getFPUTopSavedRegOff() << '\n';
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000174}
175
176// Print a 32 bit hex number with all numbers.
177void MipsAsmPrinter::
178printHex32(unsigned int Value)
179{
Owen Andersoncb371882008-08-21 00:14:44 +0000180 O << "0x";
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000181 for (int i = 7; i >= 0; i--)
Owen Andersoncb371882008-08-21 00:14:44 +0000182 O << utohexstr( (Value & (0xF << (i*4))) >> (i*4) );
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000183}
184
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000185//===----------------------------------------------------------------------===//
186// Frame and Set directives
187//===----------------------------------------------------------------------===//
188
189/// Frame Directive
190void MipsAsmPrinter::
191emitFrameDirective(MachineFunction &MF)
192{
193 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
194
195 unsigned stackReg = RI.getFrameRegister(MF);
196 unsigned returnReg = RI.getRARegister();
197 unsigned stackSize = MF.getFrameInfo()->getStackSize();
198
199
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000200 O << "\t.frame\t" << '$' << LowercaseString(RI.get(stackReg).AsmName)
201 << ',' << stackSize << ','
202 << '$' << LowercaseString(RI.get(returnReg).AsmName)
203 << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000204}
205
206/// Emit Set directives.
207const char * MipsAsmPrinter::
208emitCurrentABIString(void)
209{
210 switch(Subtarget->getTargetABI()) {
211 case MipsSubtarget::O32: return "abi32";
212 case MipsSubtarget::O64: return "abiO64";
213 case MipsSubtarget::N32: return "abiN32";
214 case MipsSubtarget::N64: return "abi64";
215 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
216 default: break;
217 }
218
Torok Edwinc23197a2009-07-14 16:55:14 +0000219 llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000220 return NULL;
221}
222
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000223/// Emit the directives used by GAS on the start of functions
224void MipsAsmPrinter::
225emitFunctionStart(MachineFunction &MF)
226{
227 // Print out the label for the function.
228 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000229 SwitchToSection(TAI->SectionForGlobal(F));
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000230
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000231 // 2 bits aligned
Bill Wendling20c568f2009-06-30 22:38:32 +0000232 EmitAlignment(MF.getAlignment(), F);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000233
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000234 O << "\t.globl\t" << CurrentFnName << '\n';
235 O << "\t.ent\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000236
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000237 printVisibility(CurrentFnName, F->getVisibility());
238
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000239 if ((TAI->hasDotTypeDotSizeDirective()) && Subtarget->isLinux())
240 O << "\t.type\t" << CurrentFnName << ", @function\n";
241
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000242 O << CurrentFnName << ":\n";
243
244 emitFrameDirective(MF);
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000245 printSavedRegsBitmask(MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000246
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000247 O << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000248}
249
250/// Emit the directives used by GAS on the end of functions
251void MipsAsmPrinter::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000252emitFunctionEnd(MachineFunction &MF)
253{
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000254 // There are instruction for this macros, but they must
255 // always be at the function end, and we can't emit and
256 // break with BB logic.
257 O << "\t.set\tmacro\n";
258 O << "\t.set\treorder\n";
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000259
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000260 O << "\t.end\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000261 if (TAI->hasDotTypeDotSizeDirective() && !Subtarget->isLinux())
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000262 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000263}
264
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000265/// runOnMachineFunction - This uses the printMachineInstruction()
266/// method to print assembly for each instruction.
267bool MipsAsmPrinter::
268runOnMachineFunction(MachineFunction &MF)
269{
Bill Wendling57f0db82009-02-24 08:30:20 +0000270 this->MF = &MF;
271
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000272 SetupMachineFunction(MF);
273
274 // Print out constants referenced by the function
275 EmitConstantPool(MF.getConstantPool());
276
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000277 // Print out jump tables referenced by the function
278 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
279
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000280 O << "\n\n";
281
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000282 // Emit the function start directives
283 emitFunctionStart(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000284
285 // Print out code for the function.
286 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
287 I != E; ++I) {
288
289 // Print a label for the basic block.
290 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000291 printBasicBlockLabel(I, true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000292 O << '\n';
293 }
294
295 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
296 II != E; ++II) {
297 // Print the assembly for the instruction.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000298 printInstruction(II);
299 ++EmittedInsts;
300 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000301
302 // Each Basic Block is separated by a newline
303 O << '\n';
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000304 }
305
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000306 // Emit function end directives
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000307 emitFunctionEnd(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000308
309 // We didn't modify anything.
310 return false;
311}
312
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000313// Print out an operand for an inline asm expression.
314bool MipsAsmPrinter::
315PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
316 unsigned AsmVariant, const char *ExtraCode)
317{
318 // Does this asm operand have a single letter operand modifier?
319 if (ExtraCode && ExtraCode[0])
320 return true; // Unknown modifier.
321
322 printOperand(MI, OpNo);
323 return false;
324}
325
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000326void MipsAsmPrinter::
327printOperand(const MachineInstr *MI, int opNum)
328{
329 const MachineOperand &MO = MI->getOperand(opNum);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000330 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000331 bool closeP = false;
332 bool isPIC = (TM.getRelocationModel() == Reloc::PIC_);
333 bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000334
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000335 // %hi and %lo used on mips gas to load global addresses on
336 // static code. %got is used to load global addresses when
337 // using PIC_. %call16 is used to load direct call targets
338 // on PIC_ and small code size. %call_lo and %call_hi load
339 // direct call targets on PIC_ and large code size.
Dan Gohmand735b802008-10-03 15:45:36 +0000340 if (MI->getOpcode() == Mips::LUi && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000341 if ((isPIC) && (isCodeLarge))
342 O << "%call_hi(";
343 else
344 O << "%hi(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000345 closeP = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000346 } else if ((MI->getOpcode() == Mips::ADDiu) && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000347 const MachineOperand &firstMO = MI->getOperand(opNum-1);
348 if (firstMO.getReg() == Mips::GP)
349 O << "%gp_rel(";
350 else
351 O << "%lo(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000352 closeP = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000353 } else if ((isPIC) && (MI->getOpcode() == Mips::LW) &&
354 (!MO.isReg()) && (!MO.isImm())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000355 const MachineOperand &firstMO = MI->getOperand(opNum-1);
356 const MachineOperand &lastMO = MI->getOperand(opNum+1);
Dan Gohmand735b802008-10-03 15:45:36 +0000357 if ((firstMO.isReg()) && (lastMO.isReg())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000358 if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() == Mips::GP)
359 && (!isCodeLarge))
360 O << "%call16(";
361 else if ((firstMO.getReg() != Mips::T9) && (lastMO.getReg() == Mips::GP))
362 O << "%got(";
363 else if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() != Mips::GP)
364 && (isCodeLarge))
365 O << "%call_lo(";
366 closeP = true;
367 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000368 }
369
370 switch (MO.getType())
371 {
372 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000373 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000374 O << '$' << LowercaseString (RI.get(MO.getReg()).AsmName);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000375 else
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000376 O << '$' << MO.getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000377 break;
378
379 case MachineOperand::MO_Immediate:
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000380 O << (short int)MO.getImm();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000381 break;
382
383 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000384 printBasicBlockLabel(MO.getMBB());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000385 return;
386
387 case MachineOperand::MO_GlobalAddress:
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000388 O << Mang->getMangledName(MO.getGlobal());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000389 break;
390
391 case MachineOperand::MO_ExternalSymbol:
392 O << MO.getSymbolName();
393 break;
394
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000395 case MachineOperand::MO_JumpTableIndex:
396 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000397 << '_' << MO.getIndex();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000398 break;
399
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000400 case MachineOperand::MO_ConstantPoolIndex:
401 O << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000402 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000403 break;
404
405 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000406 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000407 }
408
409 if (closeP) O << ")";
410}
411
412void MipsAsmPrinter::
Chris Lattnerb34e3a92009-07-21 17:39:48 +0000413printUnsignedImm(const MachineInstr *MI, int opNum) {
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000414 const MachineOperand &MO = MI->getOperand(opNum);
415 if (MO.getType() == MachineOperand::MO_Immediate)
416 O << (unsigned short int)MO.getImm();
417 else
418 printOperand(MI, opNum);
419}
420
421void MipsAsmPrinter::
Chris Lattnerb34e3a92009-07-21 17:39:48 +0000422printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier) {
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000423 // when using stack locations for not load/store instructions
424 // print the same way as all normal 3 operand instructions.
425 if (Modifier && !strcmp(Modifier, "stackloc")) {
426 printOperand(MI, opNum+1);
427 O << ", ";
428 printOperand(MI, opNum);
429 return;
430 }
431
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000432 // Load/Store memory operands -- imm($reg)
433 // If PIC target the target is loaded as the
434 // pattern lw $25,%call16($28)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000435 printOperand(MI, opNum);
436 O << "(";
437 printOperand(MI, opNum+1);
438 O << ")";
439}
440
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000441void MipsAsmPrinter::
Chris Lattnerb34e3a92009-07-21 17:39:48 +0000442printFCCOperand(const MachineInstr *MI, int opNum, const char *Modifier) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000443 const MachineOperand& MO = MI->getOperand(opNum);
444 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
445}
446
Chris Lattnerb34e3a92009-07-21 17:39:48 +0000447bool MipsAsmPrinter::doInitialization(Module &M) {
448 // FIXME: Use SwitchToDataSection.
449
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000450 // Tell the assembler which ABI we are using
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000451 O << "\t.section .mdebug." << emitCurrentABIString() << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000452
453 // TODO: handle O64 ABI
454 if (Subtarget->isABI_EABI())
455 O << "\t.section .gcc_compiled_long" <<
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000456 (Subtarget->isGP32bit() ? "32" : "64") << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000457
458 // return to previous section
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000459 O << "\t.previous" << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000460
Chris Lattnerb34e3a92009-07-21 17:39:48 +0000461 return AsmPrinter::doInitialization(M);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000462}
463
Chris Lattner40bbebd2009-07-21 18:38:57 +0000464void MipsAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000465 const TargetData *TD = TM.getTargetData();
466
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000467 if (!GVar->hasInitializer())
468 return; // External global require no code
469
470 // Check to see if this is a special global used by LLVM, if so, emit it.
471 if (EmitSpecialLLVMGlobal(GVar))
472 return;
473
474 O << "\n\n";
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000475 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000476 Constant *C = GVar->getInitializer();
Devang Patel0f05d222009-06-26 02:26:12 +0000477 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patele4c0c0f2009-06-25 00:47:42 +0000478 return;
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000479 const Type *CTy = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000480 unsigned Size = TD->getTypeAllocSize(CTy);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000481 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000482 bool printSizeAndType = true;
483
484 // A data structure or array is aligned in memory to the largest
485 // alignment boundary required by any data type inside it (this matches
486 // the Preferred Type Alignment). For integral types, the alignment is
487 // the type size.
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000488 unsigned Align;
489 if (CTy->getTypeID() == Type::IntegerTyID ||
490 CTy->getTypeID() == Type::VoidTyID) {
491 assert(!(Size & (Size-1)) && "Alignment is not a power of two!");
492 Align = Log2_32(Size);
493 } else
494 Align = TD->getPreferredTypeAlignmentShift(CTy);
495
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000496 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000497
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000498 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000499
500 if (C->isNullValue() && !GVar->hasSection()) {
501 if (!GVar->isThreadLocal() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000502 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000503 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
504
Rafael Espindolabb46f522009-01-15 20:18:42 +0000505 if (GVar->hasLocalLinkage())
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000506 O << "\t.local\t" << name << '\n';
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000507
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000508 O << TAI->getCOMMDirective() << name << ',' << Size;
509 if (TAI->getCOMMDirectiveTakesAlignment())
510 O << ',' << (1 << Align);
511
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000512 O << '\n';
513 return;
514 }
515 }
516 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000517 case GlobalValue::LinkOnceAnyLinkage:
518 case GlobalValue::LinkOnceODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000519 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000520 case GlobalValue::WeakAnyLinkage:
521 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000522 // FIXME: Verify correct for weak.
523 // Nonnull linkonce -> weak
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000524 O << "\t.weak " << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000525 break;
526 case GlobalValue::AppendingLinkage:
527 // FIXME: appending linkage variables should go into a section of their name
528 // or something. For now, just emit them as external.
529 case GlobalValue::ExternalLinkage:
530 // If external or appending, declare as a global symbol
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000531 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000532 // Fall Through
Rafael Espindolabb46f522009-01-15 20:18:42 +0000533 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000534 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000535 case GlobalValue::InternalLinkage:
Owen Anderson1ca29d32009-07-13 21:27:19 +0000536 if (CVA && CVA->isCString())
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000537 printSizeAndType = false;
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000538 break;
539 case GlobalValue::GhostLinkage:
Torok Edwinc23197a2009-07-14 16:55:14 +0000540 llvm_unreachable("Should not have any unmaterialized functions!");
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000541 case GlobalValue::DLLImportLinkage:
Torok Edwinc23197a2009-07-14 16:55:14 +0000542 llvm_unreachable("DLLImport linkage is not supported by this target!");
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000543 case GlobalValue::DLLExportLinkage:
Torok Edwinc23197a2009-07-14 16:55:14 +0000544 llvm_unreachable("DLLExport linkage is not supported by this target!");
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000545 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000546 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000547 }
548
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000549 EmitAlignment(Align, GVar);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000550
551 if (TAI->hasDotTypeDotSizeDirective() && printSizeAndType) {
552 O << "\t.type " << name << ",@object\n";
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000553 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000554 }
555
556 O << name << ":\n";
557 EmitGlobalConstant(C);
558}
559
Douglas Gregor1555a232009-06-16 20:12:29 +0000560
Bob Wilsona96751f2009-06-23 23:59:40 +0000561// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000562extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000563 TargetRegistry::RegisterAsmPrinter(TheMipsTarget, createMipsCodePrinterPass);
564
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000565 TargetRegistry::RegisterAsmPrinter(TheMipselTarget,
566 createMipsCodePrinterPass);
567}