blob: 431630b146543439ed66f05b616da69e159291cd [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"
Devang Patele4c0c0f2009-06-25 00:47:42 +000025#include "llvm/MDNode.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000026#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000027#include "llvm/CodeGen/DwarfWriter.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000028#include "llvm/CodeGen/MachineFunctionPass.h"
29#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000031#include "llvm/CodeGen/MachineInstr.h"
32#include "llvm/Target/TargetAsmInfo.h"
33#include "llvm/Target/TargetData.h"
34#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000035#include "llvm/Target/TargetOptions.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036#include "llvm/Support/Mangler.h"
37#include "llvm/ADT/Statistic.h"
38#include "llvm/ADT/StringExtras.h"
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000039#include "llvm/Support/Debug.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000040#include "llvm/Support/CommandLine.h"
41#include "llvm/Support/MathExtras.h"
Owen Andersoncb371882008-08-21 00:14:44 +000042#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000043#include <cctype>
44
45using namespace llvm;
46
47STATISTIC(EmittedInsts, "Number of machine instrs printed");
48
49namespace {
Bill Wendling57f0db82009-02-24 08:30:20 +000050 class VISIBILITY_HIDDEN MipsAsmPrinter : public AsmPrinter {
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000051 const MipsSubtarget *Subtarget;
Bill Wendling57f0db82009-02-24 08:30:20 +000052 public:
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000053 explicit MipsAsmPrinter(raw_ostream &O, MipsTargetMachine &TM,
Bill Wendling98a366d2009-04-29 23:29:43 +000054 const TargetAsmInfo *T, CodeGenOpt::Level OL,
55 bool V)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000056 : AsmPrinter(O, TM, T, OL, V) {
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000057 Subtarget = &TM.getSubtarget<MipsSubtarget>();
58 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000059
60 virtual const char *getPassName() const {
61 return "Mips Assembly Printer";
62 }
63
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +000064 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
65 unsigned AsmVariant, const char *ExtraCode);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000066 void printOperand(const MachineInstr *MI, int opNum);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +000067 void printUnsignedImm(const MachineInstr *MI, int opNum);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000068 void printMemOperand(const MachineInstr *MI, int opNum,
69 const char *Modifier = 0);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000070 void printFCCOperand(const MachineInstr *MI, int opNum,
71 const char *Modifier = 0);
Anton Korobeynikovae408e62008-07-19 13:16:11 +000072 void printModuleLevelGV(const GlobalVariable* GVar);
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +000073 void printSavedRegsBitmask(MachineFunction &MF);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000074 void printHex32(unsigned int Value);
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000075
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000076 const char *emitCurrentABIString(void);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000077 void emitFunctionStart(MachineFunction &MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +000078 void emitFunctionEnd(MachineFunction &MF);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000079 void emitFrameDirective(MachineFunction &MF);
Anton Korobeynikovae408e62008-07-19 13:16:11 +000080
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000081 bool printInstruction(const MachineInstr *MI); // autogenerated.
82 bool runOnMachineFunction(MachineFunction &F);
83 bool doInitialization(Module &M);
84 bool doFinalization(Module &M);
85 };
86} // end of anonymous namespace
87
88#include "MipsGenAsmWriter.inc"
89
90/// createMipsCodePrinterPass - Returns a pass that prints the MIPS
91/// assembly code for a MachineFunction to the given output stream,
92/// using the given target machine description. This should work
93/// regardless of whether the function is in SSA form.
Owen Andersoncb371882008-08-21 00:14:44 +000094FunctionPass *llvm::createMipsCodePrinterPass(raw_ostream &o,
Bill Wendling57f0db82009-02-24 08:30:20 +000095 MipsTargetMachine &tm,
Bill Wendling98a366d2009-04-29 23:29:43 +000096 CodeGenOpt::Level OptLevel,
97 bool verbose) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000098 return new MipsAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000099}
100
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000101//===----------------------------------------------------------------------===//
102//
103// Mips Asm Directives
104//
105// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
106// Describe the stack frame.
107//
108// -- Mask directives "(f)mask bitmask, offset"
109// Tells the assembler which registers are saved and where.
110// bitmask - contain a little endian bitset indicating which registers are
111// saved on function prologue (e.g. with a 0x80000000 mask, the
112// assembler knows the register 31 (RA) is saved at prologue.
113// offset - the position before stack pointer subtraction indicating where
114// the first saved register on prologue is located. (e.g. with a
115//
116// Consider the following function prologue:
117//
Bill Wendling6ef781f2008-02-27 06:33:05 +0000118// .frame $fp,48,$ra
119// .mask 0xc0000000,-8
120// addiu $sp, $sp, -48
121// sw $ra, 40($sp)
122// sw $fp, 36($sp)
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000123//
124// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
125// 30 (FP) are saved at prologue. As the save order on prologue is from
126// left to right, RA is saved first. A -8 offset means that after the
127// stack pointer subtration, the first register in the mask (RA) will be
128// saved at address 48-8=40.
129//
130//===----------------------------------------------------------------------===//
131
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000132//===----------------------------------------------------------------------===//
133// Mask directives
134//===----------------------------------------------------------------------===//
135
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000136// Create a bitmask with all callee saved registers for CPU or Floating Point
137// registers. For CPU registers consider RA, GP and FP for saving if necessary.
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000138void MipsAsmPrinter::
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000139printSavedRegsBitmask(MachineFunction &MF)
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000140{
Dan Gohman6f0d0242008-02-10 18:45:23 +0000141 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000142 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000143
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000144 // CPU and FPU Saved Registers Bitmasks
145 unsigned int CPUBitmask = 0;
146 unsigned int FPUBitmask = 0;
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000147
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000148 // Set the CPU and FPU Bitmasks
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000149 MachineFrameInfo *MFI = MF.getFrameInfo();
150 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000151 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
152 unsigned RegNum = MipsRegisterInfo::getRegisterNumbering(CSI[i].getReg());
153 if (CSI[i].getRegClass() == Mips::CPURegsRegisterClass)
154 CPUBitmask |= (1 << RegNum);
155 else
156 FPUBitmask |= (1 << RegNum);
157 }
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000158
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000159 // Return Address and Frame registers must also be set in CPUBitmask.
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000160 if (RI.hasFP(MF))
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000161 CPUBitmask |= (1 << MipsRegisterInfo::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000162 getRegisterNumbering(RI.getFrameRegister(MF)));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000163
164 if (MF.getFrameInfo()->hasCalls())
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000165 CPUBitmask |= (1 << MipsRegisterInfo::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000166 getRegisterNumbering(RI.getRARegister()));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000167
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000168 // Print CPUBitmask
169 O << "\t.mask \t"; printHex32(CPUBitmask); O << ','
170 << MipsFI->getCPUTopSavedRegOff() << '\n';
171
172 // Print FPUBitmask
173 O << "\t.fmask\t"; printHex32(FPUBitmask); O << ","
174 << MipsFI->getFPUTopSavedRegOff() << '\n';
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000175}
176
177// Print a 32 bit hex number with all numbers.
178void MipsAsmPrinter::
179printHex32(unsigned int Value)
180{
Owen Andersoncb371882008-08-21 00:14:44 +0000181 O << "0x";
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000182 for (int i = 7; i >= 0; i--)
Owen Andersoncb371882008-08-21 00:14:44 +0000183 O << utohexstr( (Value & (0xF << (i*4))) >> (i*4) );
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000184}
185
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000186//===----------------------------------------------------------------------===//
187// Frame and Set directives
188//===----------------------------------------------------------------------===//
189
190/// Frame Directive
191void MipsAsmPrinter::
192emitFrameDirective(MachineFunction &MF)
193{
194 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
195
196 unsigned stackReg = RI.getFrameRegister(MF);
197 unsigned returnReg = RI.getRARegister();
198 unsigned stackSize = MF.getFrameInfo()->getStackSize();
199
200
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000201 O << "\t.frame\t" << '$' << LowercaseString(RI.get(stackReg).AsmName)
202 << ',' << stackSize << ','
203 << '$' << LowercaseString(RI.get(returnReg).AsmName)
204 << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000205}
206
207/// Emit Set directives.
208const char * MipsAsmPrinter::
209emitCurrentABIString(void)
210{
211 switch(Subtarget->getTargetABI()) {
212 case MipsSubtarget::O32: return "abi32";
213 case MipsSubtarget::O64: return "abiO64";
214 case MipsSubtarget::N32: return "abiN32";
215 case MipsSubtarget::N64: return "abi64";
216 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64
217 default: break;
218 }
219
220 assert(0 && "Unknown Mips ABI");
221 return NULL;
222}
223
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000224/// Emit the directives used by GAS on the start of functions
225void MipsAsmPrinter::
226emitFunctionStart(MachineFunction &MF)
227{
228 // Print out the label for the function.
229 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000230 SwitchToSection(TAI->SectionForGlobal(F));
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000231
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000232 // 2 bits aligned
233 EmitAlignment(2, F);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000234
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000235 O << "\t.globl\t" << CurrentFnName << '\n';
236 O << "\t.ent\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000237
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000238 printVisibility(CurrentFnName, F->getVisibility());
239
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000240 if ((TAI->hasDotTypeDotSizeDirective()) && Subtarget->isLinux())
241 O << "\t.type\t" << CurrentFnName << ", @function\n";
242
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000243 O << CurrentFnName << ":\n";
244
245 emitFrameDirective(MF);
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +0000246 printSavedRegsBitmask(MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000247
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000248 O << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000249}
250
251/// Emit the directives used by GAS on the end of functions
252void MipsAsmPrinter::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000253emitFunctionEnd(MachineFunction &MF)
254{
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000255 // There are instruction for this macros, but they must
256 // always be at the function end, and we can't emit and
257 // break with BB logic.
258 O << "\t.set\tmacro\n";
259 O << "\t.set\treorder\n";
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000260
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000261 O << "\t.end\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000262 if (TAI->hasDotTypeDotSizeDirective() && !Subtarget->isLinux())
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000263 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000264}
265
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000266/// runOnMachineFunction - This uses the printMachineInstruction()
267/// method to print assembly for each instruction.
268bool MipsAsmPrinter::
269runOnMachineFunction(MachineFunction &MF)
270{
Bill Wendling57f0db82009-02-24 08:30:20 +0000271 this->MF = &MF;
272
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000273 SetupMachineFunction(MF);
274
275 // Print out constants referenced by the function
276 EmitConstantPool(MF.getConstantPool());
277
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000278 // Print out jump tables referenced by the function
279 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
280
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000281 O << "\n\n";
282
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000283 // Emit the function start directives
284 emitFunctionStart(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000285
286 // Print out code for the function.
287 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
288 I != E; ++I) {
289
290 // Print a label for the basic block.
291 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000292 printBasicBlockLabel(I, true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000293 O << '\n';
294 }
295
296 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
297 II != E; ++II) {
298 // Print the assembly for the instruction.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000299 printInstruction(II);
300 ++EmittedInsts;
301 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000302
303 // Each Basic Block is separated by a newline
304 O << '\n';
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000305 }
306
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000307 // Emit function end directives
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000308 emitFunctionEnd(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000309
310 // We didn't modify anything.
311 return false;
312}
313
Bruno Cardoso Lopes91ef8492008-08-02 19:42:36 +0000314// Print out an operand for an inline asm expression.
315bool MipsAsmPrinter::
316PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
317 unsigned AsmVariant, const char *ExtraCode)
318{
319 // Does this asm operand have a single letter operand modifier?
320 if (ExtraCode && ExtraCode[0])
321 return true; // Unknown modifier.
322
323 printOperand(MI, OpNo);
324 return false;
325}
326
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000327void MipsAsmPrinter::
328printOperand(const MachineInstr *MI, int opNum)
329{
330 const MachineOperand &MO = MI->getOperand(opNum);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000331 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000332 bool closeP = false;
333 bool isPIC = (TM.getRelocationModel() == Reloc::PIC_);
334 bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000335
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000336 // %hi and %lo used on mips gas to load global addresses on
337 // static code. %got is used to load global addresses when
338 // using PIC_. %call16 is used to load direct call targets
339 // on PIC_ and small code size. %call_lo and %call_hi load
340 // direct call targets on PIC_ and large code size.
Dan Gohmand735b802008-10-03 15:45:36 +0000341 if (MI->getOpcode() == Mips::LUi && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000342 if ((isPIC) && (isCodeLarge))
343 O << "%call_hi(";
344 else
345 O << "%hi(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000346 closeP = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000347 } else if ((MI->getOpcode() == Mips::ADDiu) && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000348 const MachineOperand &firstMO = MI->getOperand(opNum-1);
349 if (firstMO.getReg() == Mips::GP)
350 O << "%gp_rel(";
351 else
352 O << "%lo(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000353 closeP = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000354 } else if ((isPIC) && (MI->getOpcode() == Mips::LW) &&
355 (!MO.isReg()) && (!MO.isImm())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000356 const MachineOperand &firstMO = MI->getOperand(opNum-1);
357 const MachineOperand &lastMO = MI->getOperand(opNum+1);
Dan Gohmand735b802008-10-03 15:45:36 +0000358 if ((firstMO.isReg()) && (lastMO.isReg())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000359 if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() == Mips::GP)
360 && (!isCodeLarge))
361 O << "%call16(";
362 else if ((firstMO.getReg() != Mips::T9) && (lastMO.getReg() == Mips::GP))
363 O << "%got(";
364 else if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() != Mips::GP)
365 && (isCodeLarge))
366 O << "%call_lo(";
367 closeP = true;
368 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000369 }
370
371 switch (MO.getType())
372 {
373 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000374 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000375 O << '$' << LowercaseString (RI.get(MO.getReg()).AsmName);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000376 else
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000377 O << '$' << MO.getReg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000378 break;
379
380 case MachineOperand::MO_Immediate:
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000381 O << (short int)MO.getImm();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000382 break;
383
384 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000385 printBasicBlockLabel(MO.getMBB());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000386 return;
387
388 case MachineOperand::MO_GlobalAddress:
Rafael Espindolabb46f522009-01-15 20:18:42 +0000389 {
390 const GlobalValue *GV = MO.getGlobal();
391 O << Mang->getValueName(GV);
392 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000393 break;
394
395 case MachineOperand::MO_ExternalSymbol:
396 O << MO.getSymbolName();
397 break;
398
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000399 case MachineOperand::MO_JumpTableIndex:
400 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000401 << '_' << MO.getIndex();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000402 break;
403
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000404 case MachineOperand::MO_ConstantPoolIndex:
405 O << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000406 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000407 break;
408
409 default:
410 O << "<unknown operand type>"; abort (); break;
411 }
412
413 if (closeP) O << ")";
414}
415
416void MipsAsmPrinter::
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000417printUnsignedImm(const MachineInstr *MI, int opNum)
418{
419 const MachineOperand &MO = MI->getOperand(opNum);
420 if (MO.getType() == MachineOperand::MO_Immediate)
421 O << (unsigned short int)MO.getImm();
422 else
423 printOperand(MI, opNum);
424}
425
426void MipsAsmPrinter::
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000427printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier)
428{
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000429 // when using stack locations for not load/store instructions
430 // print the same way as all normal 3 operand instructions.
431 if (Modifier && !strcmp(Modifier, "stackloc")) {
432 printOperand(MI, opNum+1);
433 O << ", ";
434 printOperand(MI, opNum);
435 return;
436 }
437
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000438 // Load/Store memory operands -- imm($reg)
439 // If PIC target the target is loaded as the
440 // pattern lw $25,%call16($28)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000441 printOperand(MI, opNum);
442 O << "(";
443 printOperand(MI, opNum+1);
444 O << ")";
445}
446
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000447void MipsAsmPrinter::
448printFCCOperand(const MachineInstr *MI, int opNum, const char *Modifier)
449{
450 const MachineOperand& MO = MI->getOperand(opNum);
451 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
452}
453
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000454bool MipsAsmPrinter::
455doInitialization(Module &M)
456{
Rafael Espindolabb46f522009-01-15 20:18:42 +0000457 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000458
459 // Tell the assembler which ABI we are using
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000460 O << "\t.section .mdebug." << emitCurrentABIString() << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000461
462 // TODO: handle O64 ABI
463 if (Subtarget->isABI_EABI())
464 O << "\t.section .gcc_compiled_long" <<
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000465 (Subtarget->isGP32bit() ? "32" : "64") << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000466
467 // return to previous section
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000468 O << "\t.previous" << '\n';
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +0000469
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000470 return false; // success
471}
472
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000473void MipsAsmPrinter::
474printModuleLevelGV(const GlobalVariable* GVar) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000475 const TargetData *TD = TM.getTargetData();
476
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000477 if (!GVar->hasInitializer())
478 return; // External global require no code
479
480 // Check to see if this is a special global used by LLVM, if so, emit it.
481 if (EmitSpecialLLVMGlobal(GVar))
482 return;
483
484 O << "\n\n";
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000485 std::string name = Mang->getValueName(GVar);
486 Constant *C = GVar->getInitializer();
Devang Patel0f05d222009-06-26 02:26:12 +0000487 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patele4c0c0f2009-06-25 00:47:42 +0000488 return;
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000489 const Type *CTy = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000490 unsigned Size = TD->getTypeAllocSize(CTy);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000491 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000492 bool printSizeAndType = true;
493
494 // A data structure or array is aligned in memory to the largest
495 // alignment boundary required by any data type inside it (this matches
496 // the Preferred Type Alignment). For integral types, the alignment is
497 // the type size.
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000498 unsigned Align;
499 if (CTy->getTypeID() == Type::IntegerTyID ||
500 CTy->getTypeID() == Type::VoidTyID) {
501 assert(!(Size & (Size-1)) && "Alignment is not a power of two!");
502 Align = Log2_32(Size);
503 } else
504 Align = TD->getPreferredTypeAlignmentShift(CTy);
505
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000506 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000507
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000508 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000509
510 if (C->isNullValue() && !GVar->hasSection()) {
511 if (!GVar->isThreadLocal() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000512 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000513 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
514
Rafael Espindolabb46f522009-01-15 20:18:42 +0000515 if (GVar->hasLocalLinkage())
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000516 O << "\t.local\t" << name << '\n';
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000517
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000518 O << TAI->getCOMMDirective() << name << ',' << Size;
519 if (TAI->getCOMMDirectiveTakesAlignment())
520 O << ',' << (1 << Align);
521
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000522 O << '\n';
523 return;
524 }
525 }
526 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000527 case GlobalValue::LinkOnceAnyLinkage:
528 case GlobalValue::LinkOnceODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000529 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000530 case GlobalValue::WeakAnyLinkage:
531 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000532 // FIXME: Verify correct for weak.
533 // Nonnull linkonce -> weak
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000534 O << "\t.weak " << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000535 break;
536 case GlobalValue::AppendingLinkage:
537 // FIXME: appending linkage variables should go into a section of their name
538 // or something. For now, just emit them as external.
539 case GlobalValue::ExternalLinkage:
540 // If external or appending, declare as a global symbol
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000541 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000542 // Fall Through
Rafael Espindolabb46f522009-01-15 20:18:42 +0000543 case GlobalValue::PrivateLinkage:
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000544 case GlobalValue::InternalLinkage:
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000545 if (CVA && CVA->isCString())
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000546 printSizeAndType = false;
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000547 break;
548 case GlobalValue::GhostLinkage:
549 cerr << "Should not have any unmaterialized functions!\n";
550 abort();
551 case GlobalValue::DLLImportLinkage:
552 cerr << "DLLImport linkage is not supported by this target!\n";
553 abort();
554 case GlobalValue::DLLExportLinkage:
555 cerr << "DLLExport linkage is not supported by this target!\n";
556 abort();
557 default:
558 assert(0 && "Unknown linkage type!");
559 }
560
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000561 EmitAlignment(Align, GVar);
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000562
563 if (TAI->hasDotTypeDotSizeDirective() && printSizeAndType) {
564 O << "\t.type " << name << ",@object\n";
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000565 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000566 }
567
568 O << name << ":\n";
569 EmitGlobalConstant(C);
570}
571
572bool MipsAsmPrinter::
573doFinalization(Module &M)
574{
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000575 // Print out module-level global variables here.
576 for (Module::const_global_iterator I = M.global_begin(),
577 E = M.global_end(); I != E; ++I)
Anton Korobeynikovae408e62008-07-19 13:16:11 +0000578 printModuleLevelGV(I);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000579
Anton Korobeynikov055a76b2008-07-19 13:16:32 +0000580 O << '\n';
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000581
Dan Gohmanb8275a32007-07-25 19:33:14 +0000582 return AsmPrinter::doFinalization(M);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000583}
Douglas Gregor1555a232009-06-16 20:12:29 +0000584
Chris Lattnere8f10182009-06-16 22:38:04 +0000585namespace {
586 static struct Register {
587 Register() {
588 MipsTargetMachine::registerAsmPrinter(createMipsCodePrinterPass);
589 }
590 } Registrator;
591}
592
Bob Wilsona96751f2009-06-23 23:59:40 +0000593// Force static initialization.
594extern "C" void LLVMInitializeMipsAsmPrinter() { }