blob: 9e334c15df8438a77e1e8072ddc7b80cccb082a5 [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"
40#include <cctype>
41
42using namespace llvm;
43
44STATISTIC(EmittedInsts, "Number of machine instrs printed");
45
46namespace {
47 struct VISIBILITY_HIDDEN MipsAsmPrinter : public AsmPrinter {
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000048
49 const MipsSubtarget *Subtarget;
50
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000051 MipsAsmPrinter(std::ostream &O, MipsTargetMachine &TM,
52 const TargetAsmInfo *T):
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000053 AsmPrinter(O, TM, T) {
54 Subtarget = &TM.getSubtarget<MipsSubtarget>();
55 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000056
57 virtual const char *getPassName() const {
58 return "Mips Assembly Printer";
59 }
60
Anton Korobeynikovae408e62008-07-19 13:16:11 +000061 virtual std::string getSectionForFunction(const Function &F) const;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000062 void printOperand(const MachineInstr *MI, int opNum);
63 void printMemOperand(const MachineInstr *MI, int opNum,
64 const char *Modifier = 0);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000065 void printFCCOperand(const MachineInstr *MI, int opNum,
66 const char *Modifier = 0);
Anton Korobeynikovae408e62008-07-19 13:16:11 +000067 void printModuleLevelGV(const GlobalVariable* GVar);
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000068 unsigned int getSavedRegsBitmask(bool isFloat, MachineFunction &MF);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000069 void printHex32(unsigned int Value);
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000070
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000071 const char *emitCurrentABIString(void);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000072 void emitFunctionStart(MachineFunction &MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +000073 void emitFunctionEnd(MachineFunction &MF);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000074 void emitFrameDirective(MachineFunction &MF);
75 void emitMaskDirective(MachineFunction &MF);
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000076 void emitFMaskDirective(MachineFunction &MF);
Anton Korobeynikovae408e62008-07-19 13:16:11 +000077
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000078 bool printInstruction(const MachineInstr *MI); // autogenerated.
79 bool runOnMachineFunction(MachineFunction &F);
80 bool doInitialization(Module &M);
81 bool doFinalization(Module &M);
82 };
83} // end of anonymous namespace
84
85#include "MipsGenAsmWriter.inc"
86
87/// createMipsCodePrinterPass - Returns a pass that prints the MIPS
88/// assembly code for a MachineFunction to the given output stream,
89/// using the given target machine description. This should work
90/// regardless of whether the function is in SSA form.
91FunctionPass *llvm::createMipsCodePrinterPass(std::ostream &o,
92 MipsTargetMachine &tm)
93{
94 return new MipsAsmPrinter(o, tm, tm.getTargetAsmInfo());
95}
96
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000097//===----------------------------------------------------------------------===//
98//
99// Mips Asm Directives
100//
101// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
102// Describe the stack frame.
103//
104// -- Mask directives "(f)mask bitmask, offset"
105// Tells the assembler which registers are saved and where.
106// bitmask - contain a little endian bitset indicating which registers are
107// saved on function prologue (e.g. with a 0x80000000 mask, the
108// assembler knows the register 31 (RA) is saved at prologue.
109// offset - the position before stack pointer subtraction indicating where
110// the first saved register on prologue is located. (e.g. with a
111//
112// Consider the following function prologue:
113//
Bill Wendling6ef781f2008-02-27 06:33:05 +0000114// .frame $fp,48,$ra
115// .mask 0xc0000000,-8
116// addiu $sp, $sp, -48
117// sw $ra, 40($sp)
118// sw $fp, 36($sp)
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000119//
120// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
121// 30 (FP) are saved at prologue. As the save order on prologue is from
122// left to right, RA is saved first. A -8 offset means that after the
123// stack pointer subtration, the first register in the mask (RA) will be
124// saved at address 48-8=40.
125//
126//===----------------------------------------------------------------------===//
127
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000128//===----------------------------------------------------------------------===//
129// Mask directives
130//===----------------------------------------------------------------------===//
131
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000132/// Mask directive for GPR
133void MipsAsmPrinter::
134emitMaskDirective(MachineFunction &MF)
135{
136 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
137
138 int StackSize = MF.getFrameInfo()->getStackSize();
139 int Offset = (!MipsFI->getTopSavedRegOffset()) ? 0 :
140 (-(StackSize-MipsFI->getTopSavedRegOffset()));
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000141
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000142 #ifndef NDEBUG
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000143 DOUT << "--> emitMaskDirective" << '\n';
144 DOUT << "StackSize : " << StackSize << '\n';
145 DOUT << "getTopSavedReg : " << MipsFI->getTopSavedRegOffset() << '\n';
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000146 DOUT << "Offset : " << Offset << "\n\n";
147 #endif
148
149 unsigned int Bitmask = getSavedRegsBitmask(false, MF);
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000150 O << "\t.mask \t";
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000151 printHex32(Bitmask);
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000152 O << ',' << Offset << '\n';
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000153}
154
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +0000155/// TODO: Mask Directive for Floating Point
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000156void MipsAsmPrinter::
157emitFMaskDirective(MachineFunction &MF)
158{
159 unsigned int Bitmask = getSavedRegsBitmask(true, MF);
160
161 O << "\t.fmask\t";
162 printHex32(Bitmask);
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000163 O << ",0" << '\n';
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000164}
165
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000166// Create a bitmask with all callee saved registers for CPU
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +0000167// or Floating Point registers. For CPU registers consider RA,
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000168// GP and FP for saving if necessary.
169unsigned int MipsAsmPrinter::
170getSavedRegsBitmask(bool isFloat, MachineFunction &MF)
171{
Dan Gohman6f0d0242008-02-10 18:45:23 +0000172 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000173
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +0000174 // Floating Point Registers, TODO
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000175 if (isFloat)
176 return 0;
177
178 // CPU Registers
179 unsigned int Bitmask = 0;
180
181 MachineFrameInfo *MFI = MF.getFrameInfo();
182 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
183 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
184 Bitmask |= (1 << MipsRegisterInfo::getRegisterNumbering(CSI[i].getReg()));
185
186 if (RI.hasFP(MF))
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000187 Bitmask |= (1 << MipsRegisterInfo::
188 getRegisterNumbering(RI.getFrameRegister(MF)));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000189
190 if (MF.getFrameInfo()->hasCalls())
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000191 Bitmask |= (1 << MipsRegisterInfo::
192 getRegisterNumbering(RI.getRARegister()));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000193
194 return Bitmask;
195}
196
197// Print a 32 bit hex number with all numbers.
198void MipsAsmPrinter::
199printHex32(unsigned int Value)
200{
201 O << "0x" << std::hex;
202 for (int i = 7; i >= 0; i--)
203 O << std::hex << ( (Value & (0xF << (i*4))) >> (i*4) );
204 O << std::dec;
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000205}
206
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000207//===----------------------------------------------------------------------===//
208// Frame and Set directives
209//===----------------------------------------------------------------------===//
210
211/// Frame Directive
212void MipsAsmPrinter::
213emitFrameDirective(MachineFunction &MF)
214{
215 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
216
217 unsigned stackReg = RI.getFrameRegister(MF);
218 unsigned returnReg = RI.getRARegister();
219 unsigned stackSize = MF.getFrameInfo()->getStackSize();
220
221
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000222 O << "\t.frame\t" << '$' << LowercaseString(RI.get(stackReg).AsmName)
223 << ',' << stackSize << ','
224 << '$' << LowercaseString(RI.get(returnReg).AsmName)
225 << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000226}
227
228/// Emit Set directives.
229const char * MipsAsmPrinter::
230emitCurrentABIString(void)
231{
232 switch(Subtarget->getTargetABI()) {
233 case MipsSubtarget::O32: return "abi32";
234 case MipsSubtarget::O64: return "abiO64";
235 case MipsSubtarget::N32: return "abiN32";
236 case MipsSubtarget::N64: return "abi64";
237 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
238 default: break;
239 }
240
241 assert(0 && "Unknown Mips ABI");
242 return NULL;
243}
244
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000245// Substitute old hook with new one temporary
246std::string MipsAsmPrinter::getSectionForFunction(const Function &F) const {
247 return TAI->SectionForGlobal(&F);
248}
249
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000250/// Emit the directives used by GAS on the start of functions
251void MipsAsmPrinter::
252emitFunctionStart(MachineFunction &MF)
253{
254 // Print out the label for the function.
255 const Function *F = MF.getFunction();
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000256 SwitchToTextSection(TAI->SectionForGlobal(F).c_str());
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000257
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000258 // 2 bits aligned
259 EmitAlignment(2, F);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000260
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000261 O << "\t.globl\t" << CurrentFnName << '\n';
262 O << "\t.ent\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000263
264 if ((TAI->hasDotTypeDotSizeDirective()) && Subtarget->isLinux())
265 O << "\t.type\t" << CurrentFnName << ", @function\n";
266
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000267 O << CurrentFnName << ":\n";
268
269 emitFrameDirective(MF);
270 emitMaskDirective(MF);
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000271 emitFMaskDirective(MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000272
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000273 O << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000274}
275
276/// Emit the directives used by GAS on the end of functions
277void MipsAsmPrinter::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000278emitFunctionEnd(MachineFunction &MF)
279{
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000280 // There are instruction for this macros, but they must
281 // always be at the function end, and we can't emit and
282 // break with BB logic.
283 O << "\t.set\tmacro\n";
284 O << "\t.set\treorder\n";
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000285
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000286 O << "\t.end\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000287 if (TAI->hasDotTypeDotSizeDirective() && !Subtarget->isLinux())
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000288 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000289}
290
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000291/// runOnMachineFunction - This uses the printMachineInstruction()
292/// method to print assembly for each instruction.
293bool MipsAsmPrinter::
294runOnMachineFunction(MachineFunction &MF)
295{
296 SetupMachineFunction(MF);
297
298 // Print out constants referenced by the function
299 EmitConstantPool(MF.getConstantPool());
300
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000301 // Print out jump tables referenced by the function
302 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
303
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000304 O << "\n\n";
305
306 // What's my mangled name?
307 CurrentFnName = Mang->getValueName(MF.getFunction());
308
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000309 // Emit the function start directives
310 emitFunctionStart(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000311
312 // Print out code for the function.
313 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
314 I != E; ++I) {
315
316 // Print a label for the basic block.
317 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000318 printBasicBlockLabel(I, true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000319 O << '\n';
320 }
321
322 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
323 II != E; ++II) {
324 // Print the assembly for the instruction.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000325 printInstruction(II);
326 ++EmittedInsts;
327 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000328
329 // Each Basic Block is separated by a newline
330 O << '\n';
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000331 }
332
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000333 // Emit function end directives
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000334 emitFunctionEnd(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000335
336 // We didn't modify anything.
337 return false;
338}
339
340void MipsAsmPrinter::
341printOperand(const MachineInstr *MI, int opNum)
342{
343 const MachineOperand &MO = MI->getOperand(opNum);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000344 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000345 bool closeP = false;
346 bool isPIC = (TM.getRelocationModel() == Reloc::PIC_);
347 bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000348
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000349 // %hi and %lo used on mips gas to load global addresses on
350 // static code. %got is used to load global addresses when
351 // using PIC_. %call16 is used to load direct call targets
352 // on PIC_ and small code size. %call_lo and %call_hi load
353 // direct call targets on PIC_ and large code size.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000354 if (MI->getOpcode() == Mips::LUi && !MO.isRegister()
355 && !MO.isImmediate()) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000356 if ((isPIC) && (isCodeLarge))
357 O << "%call_hi(";
358 else
359 O << "%hi(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000360 closeP = true;
361 } else if ((MI->getOpcode() == Mips::ADDiu) && !MO.isRegister()
362 && !MO.isImmediate()) {
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000363 const MachineOperand &firstMO = MI->getOperand(opNum-1);
364 if (firstMO.getReg() == Mips::GP)
365 O << "%gp_rel(";
366 else
367 O << "%lo(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000368 closeP = true;
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000369 } else if ((isPIC) && (MI->getOpcode() == Mips::LW)
370 && (!MO.isRegister()) && (!MO.isImmediate())) {
371 const MachineOperand &firstMO = MI->getOperand(opNum-1);
372 const MachineOperand &lastMO = MI->getOperand(opNum+1);
373 if ((firstMO.isRegister()) && (lastMO.isRegister())) {
374 if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() == Mips::GP)
375 && (!isCodeLarge))
376 O << "%call16(";
377 else if ((firstMO.getReg() != Mips::T9) && (lastMO.getReg() == Mips::GP))
378 O << "%got(";
379 else if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() != Mips::GP)
380 && (isCodeLarge))
381 O << "%call_lo(";
382 closeP = true;
383 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000384 }
385
386 switch (MO.getType())
387 {
388 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000389 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000390 O << '$' << LowercaseString (RI.get(MO.getReg()).AsmName);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000391 else
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000392 O << '$' << MO.getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000393 break;
394
395 case MachineOperand::MO_Immediate:
396 if ((MI->getOpcode() == Mips::SLTiu) || (MI->getOpcode() == Mips::ORi) ||
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000397 (MI->getOpcode() == Mips::LUi) || (MI->getOpcode() == Mips::ANDi))
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000398 O << (unsigned short int)MO.getImm();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000399 else
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000400 O << (short int)MO.getImm();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000401 break;
402
403 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000404 printBasicBlockLabel(MO.getMBB());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000405 return;
406
407 case MachineOperand::MO_GlobalAddress:
408 O << Mang->getValueName(MO.getGlobal());
409 break;
410
411 case MachineOperand::MO_ExternalSymbol:
412 O << MO.getSymbolName();
413 break;
414
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000415 case MachineOperand::MO_JumpTableIndex:
416 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000417 << '_' << MO.getIndex();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000418 break;
419
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000420 // FIXME: Verify correct
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000421 case MachineOperand::MO_ConstantPoolIndex:
422 O << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000423 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000424 break;
425
426 default:
427 O << "<unknown operand type>"; abort (); break;
428 }
429
430 if (closeP) O << ")";
431}
432
433void MipsAsmPrinter::
434printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier)
435{
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000436 // when using stack locations for not load/store instructions
437 // print the same way as all normal 3 operand instructions.
438 if (Modifier && !strcmp(Modifier, "stackloc")) {
439 printOperand(MI, opNum+1);
440 O << ", ";
441 printOperand(MI, opNum);
442 return;
443 }
444
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000445 // Load/Store memory operands -- imm($reg)
446 // If PIC target the target is loaded as the
447 // pattern lw $25,%call16($28)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000448 printOperand(MI, opNum);
449 O << "(";
450 printOperand(MI, opNum+1);
451 O << ")";
452}
453
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000454void MipsAsmPrinter::
455printFCCOperand(const MachineInstr *MI, int opNum, const char *Modifier)
456{
457 const MachineOperand& MO = MI->getOperand(opNum);
458 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
459}
460
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000461bool MipsAsmPrinter::
462doInitialization(Module &M)
463{
464 Mang = new Mangler(M);
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000465
466 // Tell the assembler which ABI we are using
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000467 O << "\t.section .mdebug." << emitCurrentABIString() << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000468
469 // TODO: handle O64 ABI
470 if (Subtarget->isABI_EABI())
471 O << "\t.section .gcc_compiled_long" <<
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000472 (Subtarget->isGP32bit() ? "32" : "64") << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000473
474 // return to previous section
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000475 O << "\t.previous" << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000476
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000477 return false; // success
478}
479
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000480void MipsAsmPrinter::
481printModuleLevelGV(const GlobalVariable* GVar) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000482 const TargetData *TD = TM.getTargetData();
483
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000484 if (!GVar->hasInitializer())
485 return; // External global require no code
486
487 // Check to see if this is a special global used by LLVM, if so, emit it.
488 if (EmitSpecialLLVMGlobal(GVar))
489 return;
490
491 O << "\n\n";
492 std::string SectionName = TAI->SectionForGlobal(GVar);
493 std::string name = Mang->getValueName(GVar);
494 Constant *C = GVar->getInitializer();
495 const Type *CTy = C->getType();
496 unsigned Size = TD->getABITypeSize(CTy);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000497 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000498 bool printSizeAndType = true;
499
500 // A data structure or array is aligned in memory to the largest
501 // alignment boundary required by any data type inside it (this matches
502 // the Preferred Type Alignment). For integral types, the alignment is
503 // the type size.
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000504 unsigned Align;
505 if (CTy->getTypeID() == Type::IntegerTyID ||
506 CTy->getTypeID() == Type::VoidTyID) {
507 assert(!(Size & (Size-1)) && "Alignment is not a power of two!");
508 Align = Log2_32(Size);
509 } else
510 Align = TD->getPreferredTypeAlignmentShift(CTy);
511
512 // FIXME: ELF supports visibility
513
514 SwitchToDataSection(SectionName.c_str());
515
516 if (C->isNullValue() && !GVar->hasSection()) {
517 if (!GVar->isThreadLocal() &&
518 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
519 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
520
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000521 if (GVar->hasInternalLinkage())
522 O << "\t.local\t" << name << '\n';
523
524 O << TAI->getCOMMDirective() << name << ',' << Size;
525 if (TAI->getCOMMDirectiveTakesAlignment())
526 O << ',' << (1 << Align);
527
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000528 O << '\n';
529 return;
530 }
531 }
532 switch (GVar->getLinkage()) {
533 case GlobalValue::LinkOnceLinkage:
534 case GlobalValue::CommonLinkage:
535 case GlobalValue::WeakLinkage:
536 // FIXME: Verify correct for weak.
537 // Nonnull linkonce -> weak
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000538 O << "\t.weak " << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000539 break;
540 case GlobalValue::AppendingLinkage:
541 // FIXME: appending linkage variables should go into a section of their name
542 // or something. For now, just emit them as external.
543 case GlobalValue::ExternalLinkage:
544 // If external or appending, declare as a global symbol
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000545 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000546 // Fall Through
547 case GlobalValue::InternalLinkage:
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000548 if (CVA && CVA->isCString())
549 printSizeAndType = false;
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000550 break;
551 case GlobalValue::GhostLinkage:
552 cerr << "Should not have any unmaterialized functions!\n";
553 abort();
554 case GlobalValue::DLLImportLinkage:
555 cerr << "DLLImport linkage is not supported by this target!\n";
556 abort();
557 case GlobalValue::DLLExportLinkage:
558 cerr << "DLLExport linkage is not supported by this target!\n";
559 abort();
560 default:
561 assert(0 && "Unknown linkage type!");
562 }
563
564 if (Align)
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000565 O << "\t.align " << Align << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000566
567 if (TAI->hasDotTypeDotSizeDirective() && printSizeAndType) {
568 O << "\t.type " << name << ",@object\n";
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000569 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000570 }
571
572 O << name << ":\n";
573 EmitGlobalConstant(C);
574}
575
576bool MipsAsmPrinter::
577doFinalization(Module &M)
578{
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000579 // Print out module-level global variables here.
580 for (Module::const_global_iterator I = M.global_begin(),
581 E = M.global_end(); I != E; ++I)
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000582 printModuleLevelGV(I);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000583
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000584 O << '\n';
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000585
Dan Gohmanb8275a32007-07-25 19:33:14 +0000586 return AsmPrinter::doFinalization(M);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000587}