blob: fd2ae51b975e8303d39dedfe94b17bc42ec8b99a [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 {
Bill Wendling57f0db82009-02-24 08:30:20 +000049 class VISIBILITY_HIDDEN MipsAsmPrinter : public AsmPrinter {
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000050 const MipsSubtarget *Subtarget;
Bill Wendling57f0db82009-02-24 08:30:20 +000051 public:
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000052 explicit MipsAsmPrinter(raw_ostream &O, MipsTargetMachine &TM,
Bill Wendling98a366d2009-04-29 23:29:43 +000053 const TargetAsmInfo *T, CodeGenOpt::Level OL,
54 bool V)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000055 : AsmPrinter(O, TM, T, OL, V) {
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000056 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,
Bill Wendling57f0db82009-02-24 08:30:20 +000094 MipsTargetMachine &tm,
Bill Wendling98a366d2009-04-29 23:29:43 +000095 CodeGenOpt::Level OptLevel,
96 bool verbose) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000097 return new MipsAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000098}
99
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000100//===----------------------------------------------------------------------===//
101//
102// Mips Asm Directives
103//
104// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
105// Describe the stack frame.
106//
107// -- Mask directives "(f)mask bitmask, offset"
108// Tells the assembler which registers are saved and where.
109// bitmask - contain a little endian bitset indicating which registers are
110// saved on function prologue (e.g. with a 0x80000000 mask, the
111// assembler knows the register 31 (RA) is saved at prologue.
112// offset - the position before stack pointer subtraction indicating where
113// the first saved register on prologue is located. (e.g. with a
114//
115// Consider the following function prologue:
116//
Bill Wendling6ef781f2008-02-27 06:33:05 +0000117// .frame $fp,48,$ra
118// .mask 0xc0000000,-8
119// addiu $sp, $sp, -48
120// sw $ra, 40($sp)
121// sw $fp, 36($sp)
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000122//
123// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
124// 30 (FP) are saved at prologue. As the save order on prologue is from
125// left to right, RA is saved first. A -8 offset means that after the
126// stack pointer subtration, the first register in the mask (RA) will be
127// saved at address 48-8=40.
128//
129//===----------------------------------------------------------------------===//
130
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000131//===----------------------------------------------------------------------===//
132// Mask directives
133//===----------------------------------------------------------------------===//
134
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000135// Create a bitmask with all callee saved registers for CPU or Floating Point
136// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000137void MipsAsmPrinter::
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000138printSavedRegsBitmask(MachineFunction &MF)
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000139{
Dan Gohman6f0d0242008-02-10 18:45:23 +0000140 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000141 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000142
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000143 // CPU and FPU Saved Registers Bitmasks
144 unsigned int CPUBitmask = 0;
145 unsigned int FPUBitmask = 0;
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000146
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000147 // Set the CPU and FPU Bitmasks
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000148 MachineFrameInfo *MFI = MF.getFrameInfo();
149 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000150 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
151 unsigned RegNum = MipsRegisterInfo::getRegisterNumbering(CSI[i].getReg());
152 if (CSI[i].getRegClass() == Mips::CPURegsRegisterClass)
153 CPUBitmask |= (1 << RegNum);
154 else
155 FPUBitmask |= (1 << RegNum);
156 }
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000157
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000158 // Return Address and Frame registers must also be set in CPUBitmask.
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000159 if (RI.hasFP(MF))
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000160 CPUBitmask |= (1 << MipsRegisterInfo::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000161 getRegisterNumbering(RI.getFrameRegister(MF)));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000162
163 if (MF.getFrameInfo()->hasCalls())
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000164 CPUBitmask |= (1 << MipsRegisterInfo::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000165 getRegisterNumbering(RI.getRARegister()));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000166
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000167 // Print CPUBitmask
168 O << "\t.mask \t"; printHex32(CPUBitmask); O << ','
169 << MipsFI->getCPUTopSavedRegOff() << '\n';
170
171 // Print FPUBitmask
172 O << "\t.fmask\t"; printHex32(FPUBitmask); O << ","
173 << MipsFI->getFPUTopSavedRegOff() << '\n';
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000174}
175
176// Print a 32 bit hex number with all numbers.
177void MipsAsmPrinter::
178printHex32(unsigned int Value)
179{
Owen Andersoncb371882008-08-21 00:14:44 +0000180 O << "0x";
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000181 for (int i = 7; i >= 0; i--)
Owen Andersoncb371882008-08-21 00:14:44 +0000182 O << utohexstr( (Value & (0xF << (i*4))) >> (i*4) );
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000183}
184
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000185//===----------------------------------------------------------------------===//
186// Frame and Set directives
187//===----------------------------------------------------------------------===//
188
189/// Frame Directive
190void MipsAsmPrinter::
191emitFrameDirective(MachineFunction &MF)
192{
193 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
194
195 unsigned stackReg = RI.getFrameRegister(MF);
196 unsigned returnReg = RI.getRARegister();
197 unsigned stackSize = MF.getFrameInfo()->getStackSize();
198
199
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000200 O << "\t.frame\t" << '$' << LowercaseString(RI.get(stackReg).AsmName)
201 << ',' << stackSize << ','
202 << '$' << LowercaseString(RI.get(returnReg).AsmName)
203 << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000204}
205
206/// Emit Set directives.
207const char * MipsAsmPrinter::
208emitCurrentABIString(void)
209{
210 switch(Subtarget->getTargetABI()) {
211 case MipsSubtarget::O32: return "abi32";
212 case MipsSubtarget::O64: return "abiO64";
213 case MipsSubtarget::N32: return "abiN32";
214 case MipsSubtarget::N64: return "abi64";
215 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
216 default: break;
217 }
218
219 assert(0 && "Unknown Mips ABI");
220 return NULL;
221}
222
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000223/// Emit the directives used by GAS on the start of functions
224void MipsAsmPrinter::
225emitFunctionStart(MachineFunction &MF)
226{
227 // Print out the label for the function.
228 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000229 SwitchToSection(TAI->SectionForGlobal(F));
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000230
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000231 // 2 bits aligned
232 EmitAlignment(2, F);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000233
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000234 O << "\t.globl\t" << CurrentFnName << '\n';
235 O << "\t.ent\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000236
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000237 printVisibility(CurrentFnName, F->getVisibility());
238
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000239 if ((TAI->hasDotTypeDotSizeDirective()) && Subtarget->isLinux())
240 O << "\t.type\t" << CurrentFnName << ", @function\n";
241
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000242 O << CurrentFnName << ":\n";
243
244 emitFrameDirective(MF);
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000245 printSavedRegsBitmask(MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000246
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000247 O << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000248}
249
250/// Emit the directives used by GAS on the end of functions
251void MipsAsmPrinter::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000252emitFunctionEnd(MachineFunction &MF)
253{
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000254 // There are instruction for this macros, but they must
255 // always be at the function end, and we can't emit and
256 // break with BB logic.
257 O << "\t.set\tmacro\n";
258 O << "\t.set\treorder\n";
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000259
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000260 O << "\t.end\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000261 if (TAI->hasDotTypeDotSizeDirective() && !Subtarget->isLinux())
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000262 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000263}
264
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000265/// runOnMachineFunction - This uses the printMachineInstruction()
266/// method to print assembly for each instruction.
267bool MipsAsmPrinter::
268runOnMachineFunction(MachineFunction &MF)
269{
Bill Wendling57f0db82009-02-24 08:30:20 +0000270 this->MF = &MF;
271
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000272 SetupMachineFunction(MF);
273
274 // Print out constants referenced by the function
275 EmitConstantPool(MF.getConstantPool());
276
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000277 // Print out jump tables referenced by the function
278 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
279
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000280 O << "\n\n";
281
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000282 // Emit the function start directives
283 emitFunctionStart(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000284
285 // Print out code for the function.
286 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
287 I != E; ++I) {
288
289 // Print a label for the basic block.
290 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000291 printBasicBlockLabel(I, true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000292 O << '\n';
293 }
294
295 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
296 II != E; ++II) {
297 // Print the assembly for the instruction.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000298 printInstruction(II);
299 ++EmittedInsts;
300 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000301
302 // Each Basic Block is separated by a newline
303 O << '\n';
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000304 }
305
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000306 // Emit function end directives
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000307 emitFunctionEnd(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000308
309 // We didn't modify anything.
310 return false;
311}
312
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000313// Print out an operand for an inline asm expression.
314bool MipsAsmPrinter::
315PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
316 unsigned AsmVariant, const char *ExtraCode)
317{
318 // Does this asm operand have a single letter operand modifier?
319 if (ExtraCode && ExtraCode[0])
320 return true; // Unknown modifier.
321
322 printOperand(MI, OpNo);
323 return false;
324}
325
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000326void MipsAsmPrinter::
327printOperand(const MachineInstr *MI, int opNum)
328{
329 const MachineOperand &MO = MI->getOperand(opNum);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000330 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000331 bool closeP = false;
332 bool isPIC = (TM.getRelocationModel() == Reloc::PIC_);
333 bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000334
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000335 // %hi and %lo used on mips gas to load global addresses on
336 // static code. %got is used to load global addresses when
337 // using PIC_. %call16 is used to load direct call targets
338 // on PIC_ and small code size. %call_lo and %call_hi load
339 // direct call targets on PIC_ and large code size.
Dan Gohmand735b802008-10-03 15:45:36 +0000340 if (MI->getOpcode() == Mips::LUi && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000341 if ((isPIC) && (isCodeLarge))
342 O << "%call_hi(";
343 else
344 O << "%hi(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000345 closeP = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000346 } else if ((MI->getOpcode() == Mips::ADDiu) && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000347 const MachineOperand &firstMO = MI->getOperand(opNum-1);
348 if (firstMO.getReg() == Mips::GP)
349 O << "%gp_rel(";
350 else
351 O << "%lo(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000352 closeP = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000353 } else if ((isPIC) && (MI->getOpcode() == Mips::LW) &&
354 (!MO.isReg()) && (!MO.isImm())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000355 const MachineOperand &firstMO = MI->getOperand(opNum-1);
356 const MachineOperand &lastMO = MI->getOperand(opNum+1);
Dan Gohmand735b802008-10-03 15:45:36 +0000357 if ((firstMO.isReg()) && (lastMO.isReg())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000358 if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() == Mips::GP)
359 && (!isCodeLarge))
360 O << "%call16(";
361 else if ((firstMO.getReg() != Mips::T9) && (lastMO.getReg() == Mips::GP))
362 O << "%got(";
363 else if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() != Mips::GP)
364 && (isCodeLarge))
365 O << "%call_lo(";
366 closeP = true;
367 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000368 }
369
370 switch (MO.getType())
371 {
372 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000373 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000374 O << '$' << LowercaseString (RI.get(MO.getReg()).AsmName);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000375 else
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000376 O << '$' << MO.getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000377 break;
378
379 case MachineOperand::MO_Immediate:
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000380 O << (short int)MO.getImm();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000381 break;
382
383 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000384 printBasicBlockLabel(MO.getMBB());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000385 return;
386
387 case MachineOperand::MO_GlobalAddress:
Rafael Espindolabb46f522009-01-15 20:18:42 +0000388 {
389 const GlobalValue *GV = MO.getGlobal();
390 O << Mang->getValueName(GV);
391 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000392 break;
393
394 case MachineOperand::MO_ExternalSymbol:
395 O << MO.getSymbolName();
396 break;
397
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000398 case MachineOperand::MO_JumpTableIndex:
399 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000400 << '_' << MO.getIndex();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000401 break;
402
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000403 case MachineOperand::MO_ConstantPoolIndex:
404 O << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000405 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000406 break;
407
408 default:
409 O << "<unknown operand type>"; abort (); break;
410 }
411
412 if (closeP) O << ")";
413}
414
415void MipsAsmPrinter::
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000416printUnsignedImm(const MachineInstr *MI, int opNum)
417{
418 const MachineOperand &MO = MI->getOperand(opNum);
419 if (MO.getType() == MachineOperand::MO_Immediate)
420 O << (unsigned short int)MO.getImm();
421 else
422 printOperand(MI, opNum);
423}
424
425void MipsAsmPrinter::
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000426printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier)
427{
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000428 // when using stack locations for not load/store instructions
429 // print the same way as all normal 3 operand instructions.
430 if (Modifier && !strcmp(Modifier, "stackloc")) {
431 printOperand(MI, opNum+1);
432 O << ", ";
433 printOperand(MI, opNum);
434 return;
435 }
436
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000437 // Load/Store memory operands -- imm($reg)
438 // If PIC target the target is loaded as the
439 // pattern lw $25,%call16($28)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000440 printOperand(MI, opNum);
441 O << "(";
442 printOperand(MI, opNum+1);
443 O << ")";
444}
445
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000446void MipsAsmPrinter::
447printFCCOperand(const MachineInstr *MI, int opNum, const char *Modifier)
448{
449 const MachineOperand& MO = MI->getOperand(opNum);
450 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
451}
452
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000453bool MipsAsmPrinter::
454doInitialization(Module &M)
455{
Rafael Espindolabb46f522009-01-15 20:18:42 +0000456 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000457
458 // Tell the assembler which ABI we are using
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000459 O << "\t.section .mdebug." << emitCurrentABIString() << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000460
461 // TODO: handle O64 ABI
462 if (Subtarget->isABI_EABI())
463 O << "\t.section .gcc_compiled_long" <<
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000464 (Subtarget->isGP32bit() ? "32" : "64") << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000465
466 // return to previous section
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000467 O << "\t.previous" << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000468
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000469 return false; // success
470}
471
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000472void MipsAsmPrinter::
473printModuleLevelGV(const GlobalVariable* GVar) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000474 const TargetData *TD = TM.getTargetData();
475
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000476 if (!GVar->hasInitializer())
477 return; // External global require no code
478
479 // Check to see if this is a special global used by LLVM, if so, emit it.
480 if (EmitSpecialLLVMGlobal(GVar))
481 return;
482
483 O << "\n\n";
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000484 std::string name = Mang->getValueName(GVar);
485 Constant *C = GVar->getInitializer();
486 const Type *CTy = C->getType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000487 unsigned Size = TD->getTypePaddedSize(CTy);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000488 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000489 bool printSizeAndType = true;
490
491 // A data structure or array is aligned in memory to the largest
492 // alignment boundary required by any data type inside it (this matches
493 // the Preferred Type Alignment). For integral types, the alignment is
494 // the type size.
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000495 unsigned Align;
496 if (CTy->getTypeID() == Type::IntegerTyID ||
497 CTy->getTypeID() == Type::VoidTyID) {
498 assert(!(Size & (Size-1)) && "Alignment is not a power of two!");
499 Align = Log2_32(Size);
500 } else
501 Align = TD->getPreferredTypeAlignmentShift(CTy);
502
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000503 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000504
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000505 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000506
507 if (C->isNullValue() && !GVar->hasSection()) {
508 if (!GVar->isThreadLocal() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000509 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000510 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
511
Rafael Espindolabb46f522009-01-15 20:18:42 +0000512 if (GVar->hasLocalLinkage())
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000513 O << "\t.local\t" << name << '\n';
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000514
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000515 O << TAI->getCOMMDirective() << name << ',' << Size;
516 if (TAI->getCOMMDirectiveTakesAlignment())
517 O << ',' << (1 << Align);
518
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000519 O << '\n';
520 return;
521 }
522 }
523 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000524 case GlobalValue::LinkOnceAnyLinkage:
525 case GlobalValue::LinkOnceODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000526 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000527 case GlobalValue::WeakAnyLinkage:
528 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000529 // FIXME: Verify correct for weak.
530 // Nonnull linkonce -> weak
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000531 O << "\t.weak " << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000532 break;
533 case GlobalValue::AppendingLinkage:
534 // FIXME: appending linkage variables should go into a section of their name
535 // or something. For now, just emit them as external.
536 case GlobalValue::ExternalLinkage:
537 // If external or appending, declare as a global symbol
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000538 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000539 // Fall Through
Rafael Espindolabb46f522009-01-15 20:18:42 +0000540 case GlobalValue::PrivateLinkage:
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000541 case GlobalValue::InternalLinkage:
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000542 if (CVA && CVA->isCString())
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000543 printSizeAndType = false;
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000544 break;
545 case GlobalValue::GhostLinkage:
546 cerr << "Should not have any unmaterialized functions!\n";
547 abort();
548 case GlobalValue::DLLImportLinkage:
549 cerr << "DLLImport linkage is not supported by this target!\n";
550 abort();
551 case GlobalValue::DLLExportLinkage:
552 cerr << "DLLExport linkage is not supported by this target!\n";
553 abort();
554 default:
555 assert(0 && "Unknown linkage type!");
556 }
557
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000558 EmitAlignment(Align, GVar);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000559
560 if (TAI->hasDotTypeDotSizeDirective() && printSizeAndType) {
561 O << "\t.type " << name << ",@object\n";
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000562 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000563 }
564
565 O << name << ":\n";
566 EmitGlobalConstant(C);
567}
568
569bool MipsAsmPrinter::
570doFinalization(Module &M)
571{
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000572 // Print out module-level global variables here.
573 for (Module::const_global_iterator I = M.global_begin(),
574 E = M.global_end(); I != E; ++I)
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000575 printModuleLevelGV(I);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000576
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000577 O << '\n';
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000578
Dan Gohmanb8275a32007-07-25 19:33:14 +0000579 return AsmPrinter::doFinalization(M);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000580}