blob: f6d8dda16a006a7b080cdbbd6e14fa667d7ad109 [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"
26#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000029#include "llvm/CodeGen/MachineInstr.h"
30#include "llvm/Target/TargetAsmInfo.h"
31#include "llvm/Target/TargetData.h"
32#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000033#include "llvm/Target/TargetOptions.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034#include "llvm/Support/Mangler.h"
35#include "llvm/ADT/Statistic.h"
36#include "llvm/ADT/StringExtras.h"
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000037#include "llvm/Support/Debug.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000038#include "llvm/Support/CommandLine.h"
39#include "llvm/Support/MathExtras.h"
Owen Andersoncb371882008-08-21 00:14:44 +000040#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000041#include <cctype>
42
43using namespace llvm;
44
45STATISTIC(EmittedInsts, "Number of machine instrs printed");
46
47namespace {
48 struct VISIBILITY_HIDDEN MipsAsmPrinter : public AsmPrinter {
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000049
50 const MipsSubtarget *Subtarget;
51
Owen Andersoncb371882008-08-21 00:14:44 +000052 MipsAsmPrinter(raw_ostream &O, MipsTargetMachine &TM,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000053 const TargetAsmInfo *T):
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000054 AsmPrinter(O, TM, T) {
55 Subtarget = &TM.getSubtarget<MipsSubtarget>();
56 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000057
58 virtual const char *getPassName() const {
59 return "Mips Assembly Printer";
60 }
61
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +000062 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
63 unsigned AsmVariant, const char *ExtraCode);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000064 void printOperand(const MachineInstr *MI, int opNum);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +000065 void printUnsignedImm(const MachineInstr *MI, int opNum);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000066 void printMemOperand(const MachineInstr *MI, int opNum,
67 const char *Modifier = 0);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000068 void printFCCOperand(const MachineInstr *MI, int opNum,
69 const char *Modifier = 0);
Anton Korobeynikovae408e62008-07-19 13:16:11 +000070 void printModuleLevelGV(const GlobalVariable* GVar);
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +000071 void printSavedRegsBitmask(MachineFunction &MF);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000072 void printHex32(unsigned int Value);
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000073
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000074 const char *emitCurrentABIString(void);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000075 void emitFunctionStart(MachineFunction &MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +000076 void emitFunctionEnd(MachineFunction &MF);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000077 void emitFrameDirective(MachineFunction &MF);
Anton Korobeynikovae408e62008-07-19 13:16:11 +000078
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000079 bool printInstruction(const MachineInstr *MI); // autogenerated.
80 bool runOnMachineFunction(MachineFunction &F);
81 bool doInitialization(Module &M);
82 bool doFinalization(Module &M);
83 };
84} // end of anonymous namespace
85
86#include "MipsGenAsmWriter.inc"
87
88/// createMipsCodePrinterPass - Returns a pass that prints the MIPS
89/// assembly code for a MachineFunction to the given output stream,
90/// using the given target machine description. This should work
91/// regardless of whether the function is in SSA form.
Owen Andersoncb371882008-08-21 00:14:44 +000092FunctionPass *llvm::createMipsCodePrinterPass(raw_ostream &o,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000093 MipsTargetMachine &tm)
94{
95 return new MipsAsmPrinter(o, tm, tm.getTargetAsmInfo());
96}
97
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000098//===----------------------------------------------------------------------===//
99//
100// Mips Asm Directives
101//
102// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
103// Describe the stack frame.
104//
105// -- Mask directives "(f)mask bitmask, offset"
106// Tells the assembler which registers are saved and where.
107// bitmask - contain a little endian bitset indicating which registers are
108// saved on function prologue (e.g. with a 0x80000000 mask, the
109// assembler knows the register 31 (RA) is saved at prologue.
110// offset - the position before stack pointer subtraction indicating where
111// the first saved register on prologue is located. (e.g. with a
112//
113// Consider the following function prologue:
114//
Bill Wendling6ef781f2008-02-27 06:33:05 +0000115// .frame $fp,48,$ra
116// .mask 0xc0000000,-8
117// addiu $sp, $sp, -48
118// sw $ra, 40($sp)
119// sw $fp, 36($sp)
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000120//
121// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
122// 30 (FP) are saved at prologue. As the save order on prologue is from
123// left to right, RA is saved first. A -8 offset means that after the
124// stack pointer subtration, the first register in the mask (RA) will be
125// saved at address 48-8=40.
126//
127//===----------------------------------------------------------------------===//
128
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000129//===----------------------------------------------------------------------===//
130// Mask directives
131//===----------------------------------------------------------------------===//
132
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000133// Create a bitmask with all callee saved registers for CPU or Floating Point
134// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000135void MipsAsmPrinter::
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000136printSavedRegsBitmask(MachineFunction &MF)
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000137{
Dan Gohman6f0d0242008-02-10 18:45:23 +0000138 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000139 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000140
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000141 // CPU and FPU Saved Registers Bitmasks
142 unsigned int CPUBitmask = 0;
143 unsigned int FPUBitmask = 0;
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000144
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000145 // Set the CPU and FPU Bitmasks
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000146 MachineFrameInfo *MFI = MF.getFrameInfo();
147 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000148 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
149 unsigned RegNum = MipsRegisterInfo::getRegisterNumbering(CSI[i].getReg());
150 if (CSI[i].getRegClass() == Mips::CPURegsRegisterClass)
151 CPUBitmask |= (1 << RegNum);
152 else
153 FPUBitmask |= (1 << RegNum);
154 }
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000155
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000156 // Return Address and Frame registers must also be set in CPUBitmask.
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000157 if (RI.hasFP(MF))
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000158 CPUBitmask |= (1 << MipsRegisterInfo::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000159 getRegisterNumbering(RI.getFrameRegister(MF)));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000160
161 if (MF.getFrameInfo()->hasCalls())
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000162 CPUBitmask |= (1 << MipsRegisterInfo::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000163 getRegisterNumbering(RI.getRARegister()));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000164
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000165 // Print CPUBitmask
166 O << "\t.mask \t"; printHex32(CPUBitmask); O << ','
167 << MipsFI->getCPUTopSavedRegOff() << '\n';
168
169 // Print FPUBitmask
170 O << "\t.fmask\t"; printHex32(FPUBitmask); O << ","
171 << MipsFI->getFPUTopSavedRegOff() << '\n';
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000172}
173
174// Print a 32 bit hex number with all numbers.
175void MipsAsmPrinter::
176printHex32(unsigned int Value)
177{
Owen Andersoncb371882008-08-21 00:14:44 +0000178 O << "0x";
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000179 for (int i = 7; i >= 0; i--)
Owen Andersoncb371882008-08-21 00:14:44 +0000180 O << utohexstr( (Value & (0xF << (i*4))) >> (i*4) );
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000181}
182
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000183//===----------------------------------------------------------------------===//
184// Frame and Set directives
185//===----------------------------------------------------------------------===//
186
187/// Frame Directive
188void MipsAsmPrinter::
189emitFrameDirective(MachineFunction &MF)
190{
191 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
192
193 unsigned stackReg = RI.getFrameRegister(MF);
194 unsigned returnReg = RI.getRARegister();
195 unsigned stackSize = MF.getFrameInfo()->getStackSize();
196
197
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000198 O << "\t.frame\t" << '$' << LowercaseString(RI.get(stackReg).AsmName)
199 << ',' << stackSize << ','
200 << '$' << LowercaseString(RI.get(returnReg).AsmName)
201 << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000202}
203
204/// Emit Set directives.
205const char * MipsAsmPrinter::
206emitCurrentABIString(void)
207{
208 switch(Subtarget->getTargetABI()) {
209 case MipsSubtarget::O32: return "abi32";
210 case MipsSubtarget::O64: return "abiO64";
211 case MipsSubtarget::N32: return "abiN32";
212 case MipsSubtarget::N64: return "abi64";
213 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
214 default: break;
215 }
216
217 assert(0 && "Unknown Mips ABI");
218 return NULL;
219}
220
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000221/// Emit the directives used by GAS on the start of functions
222void MipsAsmPrinter::
223emitFunctionStart(MachineFunction &MF)
224{
225 // Print out the label for the function.
226 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000227 SwitchToSection(TAI->SectionForGlobal(F));
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000228
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000229 // 2 bits aligned
230 EmitAlignment(2, F);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000231
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000232 O << "\t.globl\t" << CurrentFnName << '\n';
233 O << "\t.ent\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000234
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000235 printVisibility(CurrentFnName, F->getVisibility());
236
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000237 if ((TAI->hasDotTypeDotSizeDirective()) && Subtarget->isLinux())
238 O << "\t.type\t" << CurrentFnName << ", @function\n";
239
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000240 O << CurrentFnName << ":\n";
241
242 emitFrameDirective(MF);
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000243 printSavedRegsBitmask(MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000244
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000245 O << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000246}
247
248/// Emit the directives used by GAS on the end of functions
249void MipsAsmPrinter::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000250emitFunctionEnd(MachineFunction &MF)
251{
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000252 // There are instruction for this macros, but they must
253 // always be at the function end, and we can't emit and
254 // break with BB logic.
255 O << "\t.set\tmacro\n";
256 O << "\t.set\treorder\n";
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000257
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000258 O << "\t.end\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000259 if (TAI->hasDotTypeDotSizeDirective() && !Subtarget->isLinux())
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000260 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000261}
262
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000263/// runOnMachineFunction - This uses the printMachineInstruction()
264/// method to print assembly for each instruction.
265bool MipsAsmPrinter::
266runOnMachineFunction(MachineFunction &MF)
267{
268 SetupMachineFunction(MF);
269
270 // Print out constants referenced by the function
271 EmitConstantPool(MF.getConstantPool());
272
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000273 // Print out jump tables referenced by the function
274 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
275
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000276 O << "\n\n";
277
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000278 // Emit the function start directives
279 emitFunctionStart(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000280
281 // Print out code for the function.
282 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
283 I != E; ++I) {
284
285 // Print a label for the basic block.
286 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000287 printBasicBlockLabel(I, true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000288 O << '\n';
289 }
290
291 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
292 II != E; ++II) {
293 // Print the assembly for the instruction.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000294 printInstruction(II);
295 ++EmittedInsts;
296 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000297
298 // Each Basic Block is separated by a newline
299 O << '\n';
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000300 }
301
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000302 // Emit function end directives
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000303 emitFunctionEnd(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000304
305 // We didn't modify anything.
306 return false;
307}
308
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000309// Print out an operand for an inline asm expression.
310bool MipsAsmPrinter::
311PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
312 unsigned AsmVariant, const char *ExtraCode)
313{
314 // Does this asm operand have a single letter operand modifier?
315 if (ExtraCode && ExtraCode[0])
316 return true; // Unknown modifier.
317
318 printOperand(MI, OpNo);
319 return false;
320}
321
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000322void MipsAsmPrinter::
323printOperand(const MachineInstr *MI, int opNum)
324{
325 const MachineOperand &MO = MI->getOperand(opNum);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000326 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000327 bool closeP = false;
328 bool isPIC = (TM.getRelocationModel() == Reloc::PIC_);
329 bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000330
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000331 // %hi and %lo used on mips gas to load global addresses on
332 // static code. %got is used to load global addresses when
333 // using PIC_. %call16 is used to load direct call targets
334 // on PIC_ and small code size. %call_lo and %call_hi load
335 // direct call targets on PIC_ and large code size.
Dan Gohmand735b802008-10-03 15:45:36 +0000336 if (MI->getOpcode() == Mips::LUi && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000337 if ((isPIC) && (isCodeLarge))
338 O << "%call_hi(";
339 else
340 O << "%hi(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000341 closeP = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000342 } else if ((MI->getOpcode() == Mips::ADDiu) && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000343 const MachineOperand &firstMO = MI->getOperand(opNum-1);
344 if (firstMO.getReg() == Mips::GP)
345 O << "%gp_rel(";
346 else
347 O << "%lo(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000348 closeP = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000349 } else if ((isPIC) && (MI->getOpcode() == Mips::LW) &&
350 (!MO.isReg()) && (!MO.isImm())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000351 const MachineOperand &firstMO = MI->getOperand(opNum-1);
352 const MachineOperand &lastMO = MI->getOperand(opNum+1);
Dan Gohmand735b802008-10-03 15:45:36 +0000353 if ((firstMO.isReg()) && (lastMO.isReg())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000354 if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() == Mips::GP)
355 && (!isCodeLarge))
356 O << "%call16(";
357 else if ((firstMO.getReg() != Mips::T9) && (lastMO.getReg() == Mips::GP))
358 O << "%got(";
359 else if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() != Mips::GP)
360 && (isCodeLarge))
361 O << "%call_lo(";
362 closeP = true;
363 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000364 }
365
366 switch (MO.getType())
367 {
368 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000369 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000370 O << '$' << LowercaseString (RI.get(MO.getReg()).AsmName);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000371 else
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000372 O << '$' << MO.getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000373 break;
374
375 case MachineOperand::MO_Immediate:
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000376 O << (short int)MO.getImm();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000377 break;
378
379 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000380 printBasicBlockLabel(MO.getMBB());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000381 return;
382
383 case MachineOperand::MO_GlobalAddress:
Rafael Espindolabb46f522009-01-15 20:18:42 +0000384 {
385 const GlobalValue *GV = MO.getGlobal();
386 O << Mang->getValueName(GV);
387 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000388 break;
389
390 case MachineOperand::MO_ExternalSymbol:
391 O << MO.getSymbolName();
392 break;
393
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000394 case MachineOperand::MO_JumpTableIndex:
395 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000396 << '_' << MO.getIndex();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000397 break;
398
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000399 case MachineOperand::MO_ConstantPoolIndex:
400 O << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000401 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000402 break;
403
404 default:
405 O << "<unknown operand type>"; abort (); break;
406 }
407
408 if (closeP) O << ")";
409}
410
411void MipsAsmPrinter::
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000412printUnsignedImm(const MachineInstr *MI, int opNum)
413{
414 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::
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000422printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier)
423{
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000424 // when using stack locations for not load/store instructions
425 // print the same way as all normal 3 operand instructions.
426 if (Modifier && !strcmp(Modifier, "stackloc")) {
427 printOperand(MI, opNum+1);
428 O << ", ";
429 printOperand(MI, opNum);
430 return;
431 }
432
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000433 // Load/Store memory operands -- imm($reg)
434 // If PIC target the target is loaded as the
435 // pattern lw $25,%call16($28)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000436 printOperand(MI, opNum);
437 O << "(";
438 printOperand(MI, opNum+1);
439 O << ")";
440}
441
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000442void MipsAsmPrinter::
443printFCCOperand(const MachineInstr *MI, int opNum, const char *Modifier)
444{
445 const MachineOperand& MO = MI->getOperand(opNum);
446 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
447}
448
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000449bool MipsAsmPrinter::
450doInitialization(Module &M)
451{
Rafael Espindolabb46f522009-01-15 20:18:42 +0000452 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000453
454 // Tell the assembler which ABI we are using
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000455 O << "\t.section .mdebug." << emitCurrentABIString() << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000456
457 // TODO: handle O64 ABI
458 if (Subtarget->isABI_EABI())
459 O << "\t.section .gcc_compiled_long" <<
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000460 (Subtarget->isGP32bit() ? "32" : "64") << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000461
462 // return to previous section
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000463 O << "\t.previous" << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000464
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000465 return false; // success
466}
467
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000468void MipsAsmPrinter::
469printModuleLevelGV(const GlobalVariable* GVar) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000470 const TargetData *TD = TM.getTargetData();
471
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000472 if (!GVar->hasInitializer())
473 return; // External global require no code
474
475 // Check to see if this is a special global used by LLVM, if so, emit it.
476 if (EmitSpecialLLVMGlobal(GVar))
477 return;
478
479 O << "\n\n";
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000480 std::string name = Mang->getValueName(GVar);
481 Constant *C = GVar->getInitializer();
482 const Type *CTy = C->getType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000483 unsigned Size = TD->getTypePaddedSize(CTy);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000484 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000485 bool printSizeAndType = true;
486
487 // A data structure or array is aligned in memory to the largest
488 // alignment boundary required by any data type inside it (this matches
489 // the Preferred Type Alignment). For integral types, the alignment is
490 // the type size.
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000491 unsigned Align;
492 if (CTy->getTypeID() == Type::IntegerTyID ||
493 CTy->getTypeID() == Type::VoidTyID) {
494 assert(!(Size & (Size-1)) && "Alignment is not a power of two!");
495 Align = Log2_32(Size);
496 } else
497 Align = TD->getPreferredTypeAlignmentShift(CTy);
498
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000499 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000500
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000501 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000502
503 if (C->isNullValue() && !GVar->hasSection()) {
504 if (!GVar->isThreadLocal() &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000505 (GVar->hasLocalLinkage() || GVar->mayBeOverridden())) {
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000506 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
507
Rafael Espindolabb46f522009-01-15 20:18:42 +0000508 if (GVar->hasLocalLinkage())
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000509 O << "\t.local\t" << name << '\n';
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000510
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000511 O << TAI->getCOMMDirective() << name << ',' << Size;
512 if (TAI->getCOMMDirectiveTakesAlignment())
513 O << ',' << (1 << Align);
514
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000515 O << '\n';
516 return;
517 }
518 }
519 switch (GVar->getLinkage()) {
520 case GlobalValue::LinkOnceLinkage:
521 case GlobalValue::CommonLinkage:
522 case GlobalValue::WeakLinkage:
523 // FIXME: Verify correct for weak.
524 // Nonnull linkonce -> weak
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000525 O << "\t.weak " << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000526 break;
527 case GlobalValue::AppendingLinkage:
528 // FIXME: appending linkage variables should go into a section of their name
529 // or something. For now, just emit them as external.
530 case GlobalValue::ExternalLinkage:
531 // If external or appending, declare as a global symbol
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000532 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000533 // Fall Through
Rafael Espindolabb46f522009-01-15 20:18:42 +0000534 case GlobalValue::PrivateLinkage:
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000535 case GlobalValue::InternalLinkage:
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +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:
540 cerr << "Should not have any unmaterialized functions!\n";
541 abort();
542 case GlobalValue::DLLImportLinkage:
543 cerr << "DLLImport linkage is not supported by this target!\n";
544 abort();
545 case GlobalValue::DLLExportLinkage:
546 cerr << "DLLExport linkage is not supported by this target!\n";
547 abort();
548 default:
549 assert(0 && "Unknown linkage type!");
550 }
551
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000552 EmitAlignment(Align, GVar);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000553
554 if (TAI->hasDotTypeDotSizeDirective() && printSizeAndType) {
555 O << "\t.type " << name << ",@object\n";
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000556 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000557 }
558
559 O << name << ":\n";
560 EmitGlobalConstant(C);
561}
562
563bool MipsAsmPrinter::
564doFinalization(Module &M)
565{
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000566 // Print out module-level global variables here.
567 for (Module::const_global_iterator I = M.global_begin(),
568 E = M.global_end(); I != E; ++I)
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000569 printModuleLevelGV(I);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000570
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000571 O << '\n';
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000572
Dan Gohmanb8275a32007-07-25 19:33:14 +0000573 return AsmPrinter::doFinalization(M);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000574}