blob: 94cd71a760919e96ed29d03dc8262d81b3404a1b [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"
33#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000034#include "llvm/Target/TargetOptions.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000035#include "llvm/Support/Mangler.h"
36#include "llvm/ADT/Statistic.h"
37#include "llvm/ADT/StringExtras.h"
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000038#include "llvm/Support/Debug.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000039#include "llvm/Support/CommandLine.h"
40#include "llvm/Support/MathExtras.h"
Owen Andersoncb371882008-08-21 00:14:44 +000041#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000042#include <cctype>
43
44using namespace llvm;
45
46STATISTIC(EmittedInsts, "Number of machine instrs printed");
47
48namespace {
49 struct VISIBILITY_HIDDEN MipsAsmPrinter : public AsmPrinter {
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000050
51 const MipsSubtarget *Subtarget;
52
Owen Andersoncb371882008-08-21 00:14:44 +000053 MipsAsmPrinter(raw_ostream &O, MipsTargetMachine &TM,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000054 const TargetAsmInfo *T):
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000055 AsmPrinter(O, TM, T) {
56 Subtarget = &TM.getSubtarget<MipsSubtarget>();
57 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000058
59 virtual const char *getPassName() const {
60 return "Mips Assembly Printer";
61 }
62
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +000063 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
64 unsigned AsmVariant, const char *ExtraCode);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000065 void printOperand(const MachineInstr *MI, int opNum);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +000066 void printUnsignedImm(const MachineInstr *MI, int opNum);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000067 void printMemOperand(const MachineInstr *MI, int opNum,
68 const char *Modifier = 0);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000069 void printFCCOperand(const MachineInstr *MI, int opNum,
70 const char *Modifier = 0);
Anton Korobeynikovae408e62008-07-19 13:16:11 +000071 void printModuleLevelGV(const GlobalVariable* GVar);
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +000072 void printSavedRegsBitmask(MachineFunction &MF);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000073 void printHex32(unsigned int Value);
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000074
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000075 const char *emitCurrentABIString(void);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000076 void emitFunctionStart(MachineFunction &MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +000077 void emitFunctionEnd(MachineFunction &MF);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000078 void emitFrameDirective(MachineFunction &MF);
Anton Korobeynikovae408e62008-07-19 13:16:11 +000079
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000080 bool printInstruction(const MachineInstr *MI); // autogenerated.
81 bool runOnMachineFunction(MachineFunction &F);
82 bool doInitialization(Module &M);
83 bool doFinalization(Module &M);
84 };
85} // end of anonymous namespace
86
87#include "MipsGenAsmWriter.inc"
88
89/// createMipsCodePrinterPass - Returns a pass that prints the MIPS
90/// assembly code for a MachineFunction to the given output stream,
91/// using the given target machine description. This should work
92/// regardless of whether the function is in SSA form.
Owen Andersoncb371882008-08-21 00:14:44 +000093FunctionPass *llvm::createMipsCodePrinterPass(raw_ostream &o,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000094 MipsTargetMachine &tm)
95{
96 return new MipsAsmPrinter(o, tm, tm.getTargetAsmInfo());
97}
98
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000099//===----------------------------------------------------------------------===//
100//
101// Mips Asm Directives
102//
103// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
104// Describe the stack frame.
105//
106// -- Mask directives "(f)mask bitmask, offset"
107// Tells the assembler which registers are saved and where.
108// bitmask - contain a little endian bitset indicating which registers are
109// saved on function prologue (e.g. with a 0x80000000 mask, the
110// assembler knows the register 31 (RA) is saved at prologue.
111// offset - the position before stack pointer subtraction indicating where
112// the first saved register on prologue is located. (e.g. with a
113//
114// Consider the following function prologue:
115//
Bill Wendling6ef781f2008-02-27 06:33:05 +0000116// .frame $fp,48,$ra
117// .mask 0xc0000000,-8
118// addiu $sp, $sp, -48
119// sw $ra, 40($sp)
120// sw $fp, 36($sp)
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000121//
122// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
123// 30 (FP) are saved at prologue. As the save order on prologue is from
124// left to right, RA is saved first. A -8 offset means that after the
125// stack pointer subtration, the first register in the mask (RA) will be
126// saved at address 48-8=40.
127//
128//===----------------------------------------------------------------------===//
129
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000130//===----------------------------------------------------------------------===//
131// Mask directives
132//===----------------------------------------------------------------------===//
133
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000134// Create a bitmask with all callee saved registers for CPU or Floating Point
135// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000136void MipsAsmPrinter::
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000137printSavedRegsBitmask(MachineFunction &MF)
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000138{
Dan Gohman6f0d0242008-02-10 18:45:23 +0000139 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000140 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000141
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000142 // CPU and FPU Saved Registers Bitmasks
143 unsigned int CPUBitmask = 0;
144 unsigned int FPUBitmask = 0;
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000145
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000146 // Set the CPU and FPU Bitmasks
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000147 MachineFrameInfo *MFI = MF.getFrameInfo();
148 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000149 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
150 unsigned RegNum = MipsRegisterInfo::getRegisterNumbering(CSI[i].getReg());
151 if (CSI[i].getRegClass() == Mips::CPURegsRegisterClass)
152 CPUBitmask |= (1 << RegNum);
153 else
154 FPUBitmask |= (1 << RegNum);
155 }
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000156
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000157 // Return Address and Frame registers must also be set in CPUBitmask.
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000158 if (RI.hasFP(MF))
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000159 CPUBitmask |= (1 << MipsRegisterInfo::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000160 getRegisterNumbering(RI.getFrameRegister(MF)));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000161
162 if (MF.getFrameInfo()->hasCalls())
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000163 CPUBitmask |= (1 << MipsRegisterInfo::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000164 getRegisterNumbering(RI.getRARegister()));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000165
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000166 // Print CPUBitmask
167 O << "\t.mask \t"; printHex32(CPUBitmask); O << ','
168 << MipsFI->getCPUTopSavedRegOff() << '\n';
169
170 // Print FPUBitmask
171 O << "\t.fmask\t"; printHex32(FPUBitmask); O << ","
172 << MipsFI->getFPUTopSavedRegOff() << '\n';
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000173}
174
175// Print a 32 bit hex number with all numbers.
176void MipsAsmPrinter::
177printHex32(unsigned int Value)
178{
Owen Andersoncb371882008-08-21 00:14:44 +0000179 O << "0x";
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000180 for (int i = 7; i >= 0; i--)
Owen Andersoncb371882008-08-21 00:14:44 +0000181 O << utohexstr( (Value & (0xF << (i*4))) >> (i*4) );
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000182}
183
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000184//===----------------------------------------------------------------------===//
185// Frame and Set directives
186//===----------------------------------------------------------------------===//
187
188/// Frame Directive
189void MipsAsmPrinter::
190emitFrameDirective(MachineFunction &MF)
191{
192 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
193
194 unsigned stackReg = RI.getFrameRegister(MF);
195 unsigned returnReg = RI.getRARegister();
196 unsigned stackSize = MF.getFrameInfo()->getStackSize();
197
198
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000199 O << "\t.frame\t" << '$' << LowercaseString(RI.get(stackReg).AsmName)
200 << ',' << stackSize << ','
201 << '$' << LowercaseString(RI.get(returnReg).AsmName)
202 << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000203}
204
205/// Emit Set directives.
206const char * MipsAsmPrinter::
207emitCurrentABIString(void)
208{
209 switch(Subtarget->getTargetABI()) {
210 case MipsSubtarget::O32: return "abi32";
211 case MipsSubtarget::O64: return "abiO64";
212 case MipsSubtarget::N32: return "abiN32";
213 case MipsSubtarget::N64: return "abi64";
214 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
215 default: break;
216 }
217
218 assert(0 && "Unknown Mips ABI");
219 return NULL;
220}
221
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000222/// Emit the directives used by GAS on the start of functions
223void MipsAsmPrinter::
224emitFunctionStart(MachineFunction &MF)
225{
226 // Print out the label for the function.
227 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000228 SwitchToSection(TAI->SectionForGlobal(F));
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000229
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000230 // 2 bits aligned
231 EmitAlignment(2, F);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000232
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000233 O << "\t.globl\t" << CurrentFnName << '\n';
234 O << "\t.ent\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000235
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000236 printVisibility(CurrentFnName, F->getVisibility());
237
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000238 if ((TAI->hasDotTypeDotSizeDirective()) && Subtarget->isLinux())
239 O << "\t.type\t" << CurrentFnName << ", @function\n";
240
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000241 O << CurrentFnName << ":\n";
242
243 emitFrameDirective(MF);
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000244 printSavedRegsBitmask(MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000245
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000246 O << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000247}
248
249/// Emit the directives used by GAS on the end of functions
250void MipsAsmPrinter::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000251emitFunctionEnd(MachineFunction &MF)
252{
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000253 // There are instruction for this macros, but they must
254 // always be at the function end, and we can't emit and
255 // break with BB logic.
256 O << "\t.set\tmacro\n";
257 O << "\t.set\treorder\n";
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000258
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000259 O << "\t.end\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000260 if (TAI->hasDotTypeDotSizeDirective() && !Subtarget->isLinux())
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000261 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000262}
263
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000264/// runOnMachineFunction - This uses the printMachineInstruction()
265/// method to print assembly for each instruction.
266bool MipsAsmPrinter::
267runOnMachineFunction(MachineFunction &MF)
268{
269 SetupMachineFunction(MF);
270
271 // Print out constants referenced by the function
272 EmitConstantPool(MF.getConstantPool());
273
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000274 // Print out jump tables referenced by the function
275 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
276
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000277 O << "\n\n";
278
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000279 // Emit the function start directives
280 emitFunctionStart(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000281
282 // Print out code for the function.
283 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
284 I != E; ++I) {
285
286 // Print a label for the basic block.
287 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000288 printBasicBlockLabel(I, true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000289 O << '\n';
290 }
291
292 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
293 II != E; ++II) {
294 // Print the assembly for the instruction.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000295 printInstruction(II);
296 ++EmittedInsts;
297 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000298
299 // Each Basic Block is separated by a newline
300 O << '\n';
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000301 }
302
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000303 // Emit function end directives
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000304 emitFunctionEnd(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000305
306 // We didn't modify anything.
307 return false;
308}
309
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000310// Print out an operand for an inline asm expression.
311bool MipsAsmPrinter::
312PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
313 unsigned AsmVariant, const char *ExtraCode)
314{
315 // Does this asm operand have a single letter operand modifier?
316 if (ExtraCode && ExtraCode[0])
317 return true; // Unknown modifier.
318
319 printOperand(MI, OpNo);
320 return false;
321}
322
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000323void MipsAsmPrinter::
324printOperand(const MachineInstr *MI, int opNum)
325{
326 const MachineOperand &MO = MI->getOperand(opNum);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000327 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000328 bool closeP = false;
329 bool isPIC = (TM.getRelocationModel() == Reloc::PIC_);
330 bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000331
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000332 // %hi and %lo used on mips gas to load global addresses on
333 // static code. %got is used to load global addresses when
334 // using PIC_. %call16 is used to load direct call targets
335 // on PIC_ and small code size. %call_lo and %call_hi load
336 // direct call targets on PIC_ and large code size.
Dan Gohmand735b802008-10-03 15:45:36 +0000337 if (MI->getOpcode() == Mips::LUi && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000338 if ((isPIC) && (isCodeLarge))
339 O << "%call_hi(";
340 else
341 O << "%hi(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000342 closeP = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000343 } else if ((MI->getOpcode() == Mips::ADDiu) && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000344 const MachineOperand &firstMO = MI->getOperand(opNum-1);
345 if (firstMO.getReg() == Mips::GP)
346 O << "%gp_rel(";
347 else
348 O << "%lo(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000349 closeP = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000350 } else if ((isPIC) && (MI->getOpcode() == Mips::LW) &&
351 (!MO.isReg()) && (!MO.isImm())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000352 const MachineOperand &firstMO = MI->getOperand(opNum-1);
353 const MachineOperand &lastMO = MI->getOperand(opNum+1);
Dan Gohmand735b802008-10-03 15:45:36 +0000354 if ((firstMO.isReg()) && (lastMO.isReg())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000355 if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() == Mips::GP)
356 && (!isCodeLarge))
357 O << "%call16(";
358 else if ((firstMO.getReg() != Mips::T9) && (lastMO.getReg() == Mips::GP))
359 O << "%got(";
360 else if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() != Mips::GP)
361 && (isCodeLarge))
362 O << "%call_lo(";
363 closeP = true;
364 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000365 }
366
367 switch (MO.getType())
368 {
369 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000370 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000371 O << '$' << LowercaseString (RI.get(MO.getReg()).AsmName);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000372 else
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000373 O << '$' << MO.getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000374 break;
375
376 case MachineOperand::MO_Immediate:
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000377 O << (short int)MO.getImm();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000378 break;
379
380 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000381 printBasicBlockLabel(MO.getMBB());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000382 return;
383
384 case MachineOperand::MO_GlobalAddress:
Rafael Espindolabb46f522009-01-15 20:18:42 +0000385 {
386 const GlobalValue *GV = MO.getGlobal();
387 O << Mang->getValueName(GV);
388 }
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:
406 O << "<unknown operand type>"; abort (); break;
407 }
408
409 if (closeP) O << ")";
410}
411
412void MipsAsmPrinter::
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000413printUnsignedImm(const MachineInstr *MI, int opNum)
414{
415 const MachineOperand &MO = MI->getOperand(opNum);
416 if (MO.getType() == MachineOperand::MO_Immediate)
417 O << (unsigned short int)MO.getImm();
418 else
419 printOperand(MI, opNum);
420}
421
422void MipsAsmPrinter::
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000423printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier)
424{
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000425 // when using stack locations for not load/store instructions
426 // print the same way as all normal 3 operand instructions.
427 if (Modifier && !strcmp(Modifier, "stackloc")) {
428 printOperand(MI, opNum+1);
429 O << ", ";
430 printOperand(MI, opNum);
431 return;
432 }
433
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000434 // Load/Store memory operands -- imm($reg)
435 // If PIC target the target is loaded as the
436 // pattern lw $25,%call16($28)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000437 printOperand(MI, opNum);
438 O << "(";
439 printOperand(MI, opNum+1);
440 O << ")";
441}
442
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000443void MipsAsmPrinter::
444printFCCOperand(const MachineInstr *MI, int opNum, const char *Modifier)
445{
446 const MachineOperand& MO = MI->getOperand(opNum);
447 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
448}
449
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000450bool MipsAsmPrinter::
451doInitialization(Module &M)
452{
Rafael Espindolabb46f522009-01-15 20:18:42 +0000453 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000454
455 // Tell the assembler which ABI we are using
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000456 O << "\t.section .mdebug." << emitCurrentABIString() << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000457
458 // TODO: handle O64 ABI
459 if (Subtarget->isABI_EABI())
460 O << "\t.section .gcc_compiled_long" <<
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000461 (Subtarget->isGP32bit() ? "32" : "64") << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000462
463 // return to previous section
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000464 O << "\t.previous" << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000465
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000466 return false; // success
467}
468
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000469void MipsAsmPrinter::
470printModuleLevelGV(const GlobalVariable* GVar) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000471 const TargetData *TD = TM.getTargetData();
472
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000473 if (!GVar->hasInitializer())
474 return; // External global require no code
475
476 // Check to see if this is a special global used by LLVM, if so, emit it.
477 if (EmitSpecialLLVMGlobal(GVar))
478 return;
479
480 O << "\n\n";
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000481 std::string name = Mang->getValueName(GVar);
482 Constant *C = GVar->getInitializer();
483 const Type *CTy = C->getType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000484 unsigned Size = TD->getTypePaddedSize(CTy);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000485 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000486 bool printSizeAndType = true;
487
488 // A data structure or array is aligned in memory to the largest
489 // alignment boundary required by any data type inside it (this matches
490 // the Preferred Type Alignment). For integral types, the alignment is
491 // the type size.
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000492 unsigned Align;
493 if (CTy->getTypeID() == Type::IntegerTyID ||
494 CTy->getTypeID() == Type::VoidTyID) {
495 assert(!(Size & (Size-1)) && "Alignment is not a power of two!");
496 Align = Log2_32(Size);
497 } else
498 Align = TD->getPreferredTypeAlignmentShift(CTy);
499
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000500 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000501
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000502 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000503
504 if (C->isNullValue() && !GVar->hasSection()) {
505 if (!GVar->isThreadLocal() &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000506 (GVar->hasLocalLinkage() || GVar->mayBeOverridden())) {
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000507 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
508
Rafael Espindolabb46f522009-01-15 20:18:42 +0000509 if (GVar->hasLocalLinkage())
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000510 O << "\t.local\t" << name << '\n';
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000511
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000512 O << TAI->getCOMMDirective() << name << ',' << Size;
513 if (TAI->getCOMMDirectiveTakesAlignment())
514 O << ',' << (1 << Align);
515
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000516 O << '\n';
517 return;
518 }
519 }
520 switch (GVar->getLinkage()) {
521 case GlobalValue::LinkOnceLinkage:
522 case GlobalValue::CommonLinkage:
523 case GlobalValue::WeakLinkage:
524 // FIXME: Verify correct for weak.
525 // Nonnull linkonce -> weak
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000526 O << "\t.weak " << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000527 break;
528 case GlobalValue::AppendingLinkage:
529 // FIXME: appending linkage variables should go into a section of their name
530 // or something. For now, just emit them as external.
531 case GlobalValue::ExternalLinkage:
532 // If external or appending, declare as a global symbol
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000533 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000534 // Fall Through
Rafael Espindolabb46f522009-01-15 20:18:42 +0000535 case GlobalValue::PrivateLinkage:
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000536 case GlobalValue::InternalLinkage:
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000537 if (CVA && CVA->isCString())
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000538 printSizeAndType = false;
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000539 break;
540 case GlobalValue::GhostLinkage:
541 cerr << "Should not have any unmaterialized functions!\n";
542 abort();
543 case GlobalValue::DLLImportLinkage:
544 cerr << "DLLImport linkage is not supported by this target!\n";
545 abort();
546 case GlobalValue::DLLExportLinkage:
547 cerr << "DLLExport linkage is not supported by this target!\n";
548 abort();
549 default:
550 assert(0 && "Unknown linkage type!");
551 }
552
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000553 EmitAlignment(Align, GVar);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000554
555 if (TAI->hasDotTypeDotSizeDirective() && printSizeAndType) {
556 O << "\t.type " << name << ",@object\n";
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000557 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000558 }
559
560 O << name << ":\n";
561 EmitGlobalConstant(C);
562}
563
564bool MipsAsmPrinter::
565doFinalization(Module &M)
566{
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000567 // Print out module-level global variables here.
568 for (Module::const_global_iterator I = M.global_begin(),
569 E = M.global_end(); I != E; ++I)
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000570 printModuleLevelGV(I);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000571
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000572 O << '\n';
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000573
Dan Gohmanb8275a32007-07-25 19:33:14 +0000574 return AsmPrinter::doFinalization(M);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000575}