blob: bcdbe008d6c0a815875aba41954be04f91112e9b [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"
25#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000026#include "llvm/CodeGen/DwarfWriter.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000027#include "llvm/CodeGen/MachineFunctionPass.h"
28#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000030#include "llvm/CodeGen/MachineInstr.h"
31#include "llvm/Target/TargetAsmInfo.h"
32#include "llvm/Target/TargetData.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000033#include "llvm/Target/TargetLoweringObjectFile.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034#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
Chris Lattner41aefdc2009-08-08 01:32:19 +000082 void printInstruction(const MachineInstr *MI); // autogenerated.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000083 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
Chris Lattnere53a6002009-07-29 05:09:30 +0000180void MipsAsmPrinter::emitFrameDirective(MachineFunction &MF) {
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000181 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
182
183 unsigned stackReg = RI.getFrameRegister(MF);
184 unsigned returnReg = RI.getRARegister();
185 unsigned stackSize = MF.getFrameInfo()->getStackSize();
186
187
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000188 O << "\t.frame\t" << '$' << LowercaseString(RI.get(stackReg).AsmName)
189 << ',' << stackSize << ','
190 << '$' << LowercaseString(RI.get(returnReg).AsmName)
191 << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000192}
193
194/// Emit Set directives.
Chris Lattnere53a6002009-07-29 05:09:30 +0000195const char *MipsAsmPrinter::emitCurrentABIString() {
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000196 switch(Subtarget->getTargetABI()) {
197 case MipsSubtarget::O32: return "abi32";
198 case MipsSubtarget::O64: return "abiO64";
199 case MipsSubtarget::N32: return "abiN32";
200 case MipsSubtarget::N64: return "abi64";
201 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
202 default: break;
203 }
204
Torok Edwinc23197a2009-07-14 16:55:14 +0000205 llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000206 return NULL;
207}
208
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000209/// Emit the directives used by GAS on the start of functions
Chris Lattnerf0144122009-07-28 03:13:23 +0000210void MipsAsmPrinter::emitFunctionStart(MachineFunction &MF) {
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000211 // Print out the label for the function.
212 const Function *F = MF.getFunction();
Chris Lattnere53a6002009-07-29 05:09:30 +0000213 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000214
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000215 // 2 bits aligned
Bill Wendling20c568f2009-06-30 22:38:32 +0000216 EmitAlignment(MF.getAlignment(), F);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000217
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000218 O << "\t.globl\t" << CurrentFnName << '\n';
219 O << "\t.ent\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000220
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000221 printVisibility(CurrentFnName, F->getVisibility());
222
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000223 if ((TAI->hasDotTypeDotSizeDirective()) && Subtarget->isLinux())
224 O << "\t.type\t" << CurrentFnName << ", @function\n";
225
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000226 O << CurrentFnName << ":\n";
227
228 emitFrameDirective(MF);
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000229 printSavedRegsBitmask(MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000230
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000231 O << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000232}
233
234/// Emit the directives used by GAS on the end of functions
Chris Lattnere53a6002009-07-29 05:09:30 +0000235void MipsAsmPrinter::emitFunctionEnd(MachineFunction &MF) {
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000236 // There are instruction for this macros, but they must
237 // always be at the function end, and we can't emit and
238 // break with BB logic.
239 O << "\t.set\tmacro\n";
240 O << "\t.set\treorder\n";
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000241
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000242 O << "\t.end\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000243 if (TAI->hasDotTypeDotSizeDirective() && !Subtarget->isLinux())
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000244 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000245}
246
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000247/// runOnMachineFunction - This uses the printMachineInstruction()
248/// method to print assembly for each instruction.
Chris Lattnere53a6002009-07-29 05:09:30 +0000249bool MipsAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000250 this->MF = &MF;
251
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000252 SetupMachineFunction(MF);
253
254 // Print out constants referenced by the function
255 EmitConstantPool(MF.getConstantPool());
256
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000257 // Print out jump tables referenced by the function
258 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
259
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000260 O << "\n\n";
261
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000262 // Emit the function start directives
263 emitFunctionStart(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000264
265 // Print out code for the function.
266 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
267 I != E; ++I) {
268
269 // Print a label for the basic block.
270 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000271 printBasicBlockLabel(I, true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000272 O << '\n';
273 }
274
275 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
276 II != E; ++II) {
277 // Print the assembly for the instruction.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000278 printInstruction(II);
279 ++EmittedInsts;
280 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000281
282 // Each Basic Block is separated by a newline
283 O << '\n';
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000284 }
285
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000286 // Emit function end directives
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000287 emitFunctionEnd(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000288
289 // We didn't modify anything.
290 return false;
291}
292
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000293// Print out an operand for an inline asm expression.
Chris Lattnere53a6002009-07-29 05:09:30 +0000294bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
295 unsigned AsmVariant,const char *ExtraCode){
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000296 // Does this asm operand have a single letter operand modifier?
297 if (ExtraCode && ExtraCode[0])
298 return true; // Unknown modifier.
299
300 printOperand(MI, OpNo);
301 return false;
302}
303
Chris Lattnere53a6002009-07-29 05:09:30 +0000304void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000305 const MachineOperand &MO = MI->getOperand(opNum);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000306 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000307 bool closeP = false;
308 bool isPIC = (TM.getRelocationModel() == Reloc::PIC_);
309 bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000310
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000311 // %hi and %lo used on mips gas to load global addresses on
312 // static code. %got is used to load global addresses when
313 // using PIC_. %call16 is used to load direct call targets
314 // on PIC_ and small code size. %call_lo and %call_hi load
315 // direct call targets on PIC_ and large code size.
Dan Gohmand735b802008-10-03 15:45:36 +0000316 if (MI->getOpcode() == Mips::LUi && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000317 if ((isPIC) && (isCodeLarge))
318 O << "%call_hi(";
319 else
320 O << "%hi(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000321 closeP = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000322 } else if ((MI->getOpcode() == Mips::ADDiu) && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000323 const MachineOperand &firstMO = MI->getOperand(opNum-1);
324 if (firstMO.getReg() == Mips::GP)
325 O << "%gp_rel(";
326 else
327 O << "%lo(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000328 closeP = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000329 } else if ((isPIC) && (MI->getOpcode() == Mips::LW) &&
330 (!MO.isReg()) && (!MO.isImm())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000331 const MachineOperand &firstMO = MI->getOperand(opNum-1);
332 const MachineOperand &lastMO = MI->getOperand(opNum+1);
Dan Gohmand735b802008-10-03 15:45:36 +0000333 if ((firstMO.isReg()) && (lastMO.isReg())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000334 if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() == Mips::GP)
335 && (!isCodeLarge))
336 O << "%call16(";
337 else if ((firstMO.getReg() != Mips::T9) && (lastMO.getReg() == Mips::GP))
338 O << "%got(";
339 else if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() != Mips::GP)
340 && (isCodeLarge))
341 O << "%call_lo(";
342 closeP = true;
343 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000344 }
345
346 switch (MO.getType())
347 {
348 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000349 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000350 O << '$' << LowercaseString (RI.get(MO.getReg()).AsmName);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000351 else
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000352 O << '$' << MO.getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000353 break;
354
355 case MachineOperand::MO_Immediate:
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000356 O << (short int)MO.getImm();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000357 break;
358
359 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000360 printBasicBlockLabel(MO.getMBB());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000361 return;
362
363 case MachineOperand::MO_GlobalAddress:
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000364 O << Mang->getMangledName(MO.getGlobal());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000365 break;
366
367 case MachineOperand::MO_ExternalSymbol:
368 O << MO.getSymbolName();
369 break;
370
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000371 case MachineOperand::MO_JumpTableIndex:
372 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000373 << '_' << MO.getIndex();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000374 break;
375
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000376 case MachineOperand::MO_ConstantPoolIndex:
377 O << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000378 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000379 break;
380
381 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000382 llvm_unreachable("<unknown operand type>");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000383 }
384
385 if (closeP) O << ")";
386}
387
Chris Lattnere53a6002009-07-29 05:09:30 +0000388void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum) {
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000389 const MachineOperand &MO = MI->getOperand(opNum);
390 if (MO.getType() == MachineOperand::MO_Immediate)
391 O << (unsigned short int)MO.getImm();
392 else
393 printOperand(MI, opNum);
394}
395
396void MipsAsmPrinter::
Chris Lattnerb34e3a92009-07-21 17:39:48 +0000397printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier) {
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000398 // when using stack locations for not load/store instructions
399 // print the same way as all normal 3 operand instructions.
400 if (Modifier && !strcmp(Modifier, "stackloc")) {
401 printOperand(MI, opNum+1);
402 O << ", ";
403 printOperand(MI, opNum);
404 return;
405 }
406
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000407 // Load/Store memory operands -- imm($reg)
408 // If PIC target the target is loaded as the
409 // pattern lw $25,%call16($28)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000410 printOperand(MI, opNum);
411 O << "(";
412 printOperand(MI, opNum+1);
413 O << ")";
414}
415
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000416void MipsAsmPrinter::
Chris Lattnerb34e3a92009-07-21 17:39:48 +0000417printFCCOperand(const MachineInstr *MI, int opNum, const char *Modifier) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000418 const MachineOperand& MO = MI->getOperand(opNum);
419 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
420}
421
Chris Lattnerb34e3a92009-07-21 17:39:48 +0000422bool MipsAsmPrinter::doInitialization(Module &M) {
Chris Lattnerc8bfb7a2009-08-03 21:57:00 +0000423 // FIXME: Use SwitchToSection.
Chris Lattnerb34e3a92009-07-21 17:39:48 +0000424
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000425 // Tell the assembler which ABI we are using
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000426 O << "\t.section .mdebug." << emitCurrentABIString() << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000427
428 // TODO: handle O64 ABI
429 if (Subtarget->isABI_EABI())
430 O << "\t.section .gcc_compiled_long" <<
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000431 (Subtarget->isGP32bit() ? "32" : "64") << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000432
433 // return to previous section
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000434 O << "\t.previous" << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000435
Chris Lattnerb34e3a92009-07-21 17:39:48 +0000436 return AsmPrinter::doInitialization(M);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000437}
438
Chris Lattner40bbebd2009-07-21 18:38:57 +0000439void MipsAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000440 const TargetData *TD = TM.getTargetData();
441
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000442 if (!GVar->hasInitializer())
443 return; // External global require no code
444
445 // Check to see if this is a special global used by LLVM, if so, emit it.
446 if (EmitSpecialLLVMGlobal(GVar))
447 return;
448
449 O << "\n\n";
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000450 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000451 Constant *C = GVar->getInitializer();
452 const Type *CTy = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000453 unsigned Size = TD->getTypeAllocSize(CTy);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000454 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000455 bool printSizeAndType = true;
456
457 // A data structure or array is aligned in memory to the largest
458 // alignment boundary required by any data type inside it (this matches
459 // the Preferred Type Alignment). For integral types, the alignment is
460 // the type size.
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000461 unsigned Align;
462 if (CTy->getTypeID() == Type::IntegerTyID ||
463 CTy->getTypeID() == Type::VoidTyID) {
464 assert(!(Size & (Size-1)) && "Alignment is not a power of two!");
465 Align = Log2_32(Size);
466 } else
467 Align = TD->getPreferredTypeAlignmentShift(CTy);
468
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000469 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000470
Chris Lattnere53a6002009-07-29 05:09:30 +0000471 SwitchToSection(getObjFileLowering().SectionForGlobal(GVar, Mang, TM));
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000472
473 if (C->isNullValue() && !GVar->hasSection()) {
474 if (!GVar->isThreadLocal() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000475 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000476 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
477
Rafael Espindolabb46f522009-01-15 20:18:42 +0000478 if (GVar->hasLocalLinkage())
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000479 O << "\t.local\t" << name << '\n';
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000480
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000481 O << TAI->getCOMMDirective() << name << ',' << Size;
482 if (TAI->getCOMMDirectiveTakesAlignment())
483 O << ',' << (1 << Align);
484
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000485 O << '\n';
486 return;
487 }
488 }
489 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000490 case GlobalValue::LinkOnceAnyLinkage:
491 case GlobalValue::LinkOnceODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000492 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000493 case GlobalValue::WeakAnyLinkage:
494 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000495 // FIXME: Verify correct for weak.
496 // Nonnull linkonce -> weak
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000497 O << "\t.weak " << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000498 break;
499 case GlobalValue::AppendingLinkage:
500 // FIXME: appending linkage variables should go into a section of their name
501 // or something. For now, just emit them as external.
502 case GlobalValue::ExternalLinkage:
503 // If external or appending, declare as a global symbol
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000504 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000505 // Fall Through
Rafael Espindolabb46f522009-01-15 20:18:42 +0000506 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000507 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000508 case GlobalValue::InternalLinkage:
Owen Anderson1ca29d32009-07-13 21:27:19 +0000509 if (CVA && CVA->isCString())
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000510 printSizeAndType = false;
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000511 break;
512 case GlobalValue::GhostLinkage:
Torok Edwinc23197a2009-07-14 16:55:14 +0000513 llvm_unreachable("Should not have any unmaterialized functions!");
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000514 case GlobalValue::DLLImportLinkage:
Torok Edwinc23197a2009-07-14 16:55:14 +0000515 llvm_unreachable("DLLImport linkage is not supported by this target!");
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000516 case GlobalValue::DLLExportLinkage:
Torok Edwinc23197a2009-07-14 16:55:14 +0000517 llvm_unreachable("DLLExport linkage is not supported by this target!");
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000518 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000519 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000520 }
521
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000522 EmitAlignment(Align, GVar);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000523
524 if (TAI->hasDotTypeDotSizeDirective() && printSizeAndType) {
525 O << "\t.type " << name << ",@object\n";
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000526 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000527 }
528
529 O << name << ":\n";
530 EmitGlobalConstant(C);
531}
532
Douglas Gregor1555a232009-06-16 20:12:29 +0000533
Bob Wilsona96751f2009-06-23 23:59:40 +0000534// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000535extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbar0c795d62009-07-25 06:49:55 +0000536 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget);
537 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000538}