blob: fb338ec2cbb0222d42a328ee34bbb1fe9da07c6d [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- MipsAsmPrinter.cpp - Mips LLVM assembly writer --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Lopes29c15352008-07-14 14:42:54 +000018#include "MipsSubtarget.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "MipsInstrInfo.h"
20#include "MipsTargetMachine.h"
21#include "MipsMachineFunction.h"
22#include "llvm/Constants.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Module.h"
Devang Patelf667ab42009-06-25 00:47:42 +000025#include "llvm/MDNode.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000027#include "llvm/CodeGen/DwarfWriter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/CodeGen/MachineFunctionPass.h"
29#include "llvm/CodeGen/MachineConstantPool.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#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 Lopesea377302007-11-12 19:49:57 +000035#include "llvm/Target/TargetOptions.h"
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000036#include "llvm/Target/TargetRegistry.h"
Edwin Török2b331342009-07-08 19:04:27 +000037#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Support/Mangler.h"
39#include "llvm/ADT/Statistic.h"
40#include "llvm/ADT/StringExtras.h"
41#include "llvm/Support/Debug.h"
42#include "llvm/Support/CommandLine.h"
David Greene302008d2009-07-14 20:18:05 +000043#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044#include "llvm/Support/MathExtras.h"
45#include <cctype>
46
47using namespace llvm;
48
49STATISTIC(EmittedInsts, "Number of machine instrs printed");
50
51namespace {
Bill Wendling4f405312009-02-24 08:30:20 +000052 class VISIBILITY_HIDDEN MipsAsmPrinter : public AsmPrinter {
Bruno Cardoso Lopes29c15352008-07-14 14:42:54 +000053 const MipsSubtarget *Subtarget;
Bill Wendling4f405312009-02-24 08:30:20 +000054 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000055 explicit MipsAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000056 const TargetAsmInfo *T, bool V)
57 : AsmPrinter(O, TM, T, V) {
Bruno Cardoso Lopes29c15352008-07-14 14:42:54 +000058 Subtarget = &TM.getSubtarget<MipsSubtarget>();
59 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060
61 virtual const char *getPassName() const {
62 return "Mips Assembly Printer";
63 }
64
Bruno Cardoso Lopes4f0bb3c2008-08-02 19:42:36 +000065 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
66 unsigned AsmVariant, const char *ExtraCode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 void printOperand(const MachineInstr *MI, int opNum);
Bruno Cardoso Lopes60f3e052008-08-13 07:13:40 +000068 void printUnsignedImm(const MachineInstr *MI, int opNum);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 void printMemOperand(const MachineInstr *MI, int opNum,
70 const char *Modifier = 0);
Bruno Cardoso Lopes4fb1f542008-07-05 19:05:21 +000071 void printFCCOperand(const MachineInstr *MI, int opNum,
72 const char *Modifier = 0);
asldedcd842008-07-19 13:16:11 +000073 void printModuleLevelGV(const GlobalVariable* GVar);
Bruno Cardoso Lopesf046f872008-08-06 06:14:43 +000074 void printSavedRegsBitmask(MachineFunction &MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 void printHex32(unsigned int Value);
Bruno Cardoso Lopes7c19d332007-08-28 05:06:17 +000076
Bruno Cardoso Lopes29c15352008-07-14 14:42:54 +000077 const char *emitCurrentABIString(void);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 void emitFunctionStart(MachineFunction &MF);
Bruno Cardoso Lopes5f8e1112007-10-09 03:01:19 +000079 void emitFunctionEnd(MachineFunction &MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 void emitFrameDirective(MachineFunction &MF);
asldedcd842008-07-19 13:16:11 +000081
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 bool printInstruction(const MachineInstr *MI); // autogenerated.
83 bool runOnMachineFunction(MachineFunction &F);
84 bool doInitialization(Module &M);
85 bool doFinalization(Module &M);
86 };
87} // end of anonymous namespace
88
89#include "MipsGenAsmWriter.inc"
90
91/// createMipsCodePrinterPass - Returns a pass that prints the MIPS
92/// assembly code for a MachineFunction to the given output stream,
93/// using the given target machine description. This should work
94/// regardless of whether the function is in SSA form.
David Greene302008d2009-07-14 20:18:05 +000095FunctionPass *llvm::createMipsCodePrinterPass(formatted_raw_ostream &o,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000096 TargetMachine &tm,
Bill Wendling5ed22ac2009-04-29 23:29:43 +000097 bool verbose) {
Daniel Dunbarb10d2222009-07-01 01:48:54 +000098 return new MipsAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099}
100
Bruno Cardoso Lopes7c19d332007-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 Wendling6c02cd22008-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 Lopes7c19d332007-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 Lopes29c15352008-07-14 14:42:54 +0000132//===----------------------------------------------------------------------===//
133// Mask directives
134//===----------------------------------------------------------------------===//
135
Bruno Cardoso Lopesf046f872008-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 Lopes7c19d332007-08-28 05:06:17 +0000138void MipsAsmPrinter::
Bruno Cardoso Lopesf046f872008-08-06 06:14:43 +0000139printSavedRegsBitmask(MachineFunction &MF)
Bruno Cardoso Lopes7c19d332007-08-28 05:06:17 +0000140{
Dan Gohman1e57df32008-02-10 18:45:23 +0000141 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesf046f872008-08-06 06:14:43 +0000142 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes7c19d332007-08-28 05:06:17 +0000143
Bruno Cardoso Lopesf046f872008-08-06 06:14:43 +0000144 // CPU and FPU Saved Registers Bitmasks
145 unsigned int CPUBitmask = 0;
146 unsigned int FPUBitmask = 0;
Bruno Cardoso Lopes7c19d332007-08-28 05:06:17 +0000147
Bruno Cardoso Lopesf046f872008-08-06 06:14:43 +0000148 // Set the CPU and FPU Bitmasks
Bruno Cardoso Lopes7c19d332007-08-28 05:06:17 +0000149 MachineFrameInfo *MFI = MF.getFrameInfo();
150 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Bruno Cardoso Lopesf046f872008-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 Lopes7c19d332007-08-28 05:06:17 +0000158
Bruno Cardoso Lopesf046f872008-08-06 06:14:43 +0000159 // Return Address and Frame registers must also be set in CPUBitmask.
Bruno Cardoso Lopes7c19d332007-08-28 05:06:17 +0000160 if (RI.hasFP(MF))
Bruno Cardoso Lopesf046f872008-08-06 06:14:43 +0000161 CPUBitmask |= (1 << MipsRegisterInfo::
Bruno Cardoso Lopes5f8e1112007-10-09 03:01:19 +0000162 getRegisterNumbering(RI.getFrameRegister(MF)));
Bruno Cardoso Lopes7c19d332007-08-28 05:06:17 +0000163
164 if (MF.getFrameInfo()->hasCalls())
Bruno Cardoso Lopesf046f872008-08-06 06:14:43 +0000165 CPUBitmask |= (1 << MipsRegisterInfo::
Bruno Cardoso Lopes5f8e1112007-10-09 03:01:19 +0000166 getRegisterNumbering(RI.getRARegister()));
Bruno Cardoso Lopes7c19d332007-08-28 05:06:17 +0000167
Bruno Cardoso Lopesf046f872008-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 Lopes7c19d332007-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 Anderson847b99b2008-08-21 00:14:44 +0000181 O << "0x";
Bruno Cardoso Lopes7c19d332007-08-28 05:06:17 +0000182 for (int i = 7; i >= 0; i--)
Owen Anderson847b99b2008-08-21 00:14:44 +0000183 O << utohexstr( (Value & (0xF << (i*4))) >> (i*4) );
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184}
185
Bruno Cardoso Lopes29c15352008-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
aslcdb49b92008-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 Lopes29c15352008-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
Edwin Törökbd448e32009-07-14 16:55:14 +0000220 llvm_unreachable("Unknown Mips ABI");
Bruno Cardoso Lopes29c15352008-07-14 14:42:54 +0000221 return NULL;
222}
223
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Korobeynikov1a9edae2008-09-24 22:14:23 +0000230 SwitchToSection(TAI->SectionForGlobal(F));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231
Bruno Cardoso Lopesea377302007-11-12 19:49:57 +0000232 // 2 bits aligned
Bill Wendling25a8ae32009-06-30 22:38:32 +0000233 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234
aslcdb49b92008-07-19 13:16:32 +0000235 O << "\t.globl\t" << CurrentFnName << '\n';
236 O << "\t.ent\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes29c15352008-07-14 14:42:54 +0000237
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000238 printVisibility(CurrentFnName, F->getVisibility());
239
Bruno Cardoso Lopes29c15352008-07-14 14:42:54 +0000240 if ((TAI->hasDotTypeDotSizeDirective()) && Subtarget->isLinux())
241 O << "\t.type\t" << CurrentFnName << ", @function\n";
242
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 O << CurrentFnName << ":\n";
244
245 emitFrameDirective(MF);
Bruno Cardoso Lopesf046f872008-08-06 06:14:43 +0000246 printSavedRegsBitmask(MF);
Bruno Cardoso Lopes5f8e1112007-10-09 03:01:19 +0000247
aslcdb49b92008-07-19 13:16:32 +0000248 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249}
250
251/// Emit the directives used by GAS on the end of functions
252void MipsAsmPrinter::
Bruno Cardoso Lopes5f8e1112007-10-09 03:01:19 +0000253emitFunctionEnd(MachineFunction &MF)
254{
Bruno Cardoso Lopes29c15352008-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 Lopes5f8e1112007-10-09 03:01:19 +0000260
aslcdb49b92008-07-19 13:16:32 +0000261 O << "\t.end\t" << CurrentFnName << '\n';
Bruno Cardoso Lopes29c15352008-07-14 14:42:54 +0000262 if (TAI->hasDotTypeDotSizeDirective() && !Subtarget->isLinux())
aslcdb49b92008-07-19 13:16:32 +0000263 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264}
265
266/// runOnMachineFunction - This uses the printMachineInstruction()
267/// method to print assembly for each instruction.
268bool MipsAsmPrinter::
269runOnMachineFunction(MachineFunction &MF)
270{
Bill Wendling4f405312009-02-24 08:30:20 +0000271 this->MF = &MF;
272
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 SetupMachineFunction(MF);
274
275 // Print out constants referenced by the function
276 EmitConstantPool(MF.getConstantPool());
277
Bruno Cardoso Lopesea377302007-11-12 19:49:57 +0000278 // Print out jump tables referenced by the function
279 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
280
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 O << "\n\n";
282
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 // Emit the function start directives
284 emitFunctionStart(MF);
285
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 Cheng45c1edb2008-02-28 00:43:03 +0000292 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 printInstruction(II);
300 ++EmittedInsts;
301 }
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000302
303 // Each Basic Block is separated by a newline
304 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 }
306
307 // Emit function end directives
Bruno Cardoso Lopes5f8e1112007-10-09 03:01:19 +0000308 emitFunctionEnd(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309
310 // We didn't modify anything.
311 return false;
312}
313
Bruno Cardoso Lopes4f0bb3c2008-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327void MipsAsmPrinter::
328printOperand(const MachineInstr *MI, int opNum)
329{
330 const MachineOperand &MO = MI->getOperand(opNum);
Dan Gohman1e57df32008-02-10 18:45:23 +0000331 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000332 bool closeP = false;
333 bool isPIC = (TM.getRelocationModel() == Reloc::PIC_);
334 bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335
Bruno Cardoso Lopes218d5822007-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 Gohmanb9f4fa72008-10-03 15:45:36 +0000341 if (MI->getOpcode() == Mips::LUi && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000342 if ((isPIC) && (isCodeLarge))
343 O << "%call_hi(";
344 else
345 O << "%hi(";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346 closeP = true;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000347 } else if ((MI->getOpcode() == Mips::ADDiu) && !MO.isReg() && !MO.isImm()) {
Bruno Cardoso Lopes12355a82008-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(";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 closeP = true;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000354 } else if ((isPIC) && (MI->getOpcode() == Mips::LW) &&
355 (!MO.isReg()) && (!MO.isImm())) {
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000356 const MachineOperand &firstMO = MI->getOperand(opNum-1);
357 const MachineOperand &lastMO = MI->getOperand(opNum+1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000358 if ((firstMO.isReg()) && (lastMO.isReg())) {
Bruno Cardoso Lopes218d5822007-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 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 }
370
371 switch (MO.getType())
372 {
373 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000374 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
aslcdb49b92008-07-19 13:16:32 +0000375 O << '$' << LowercaseString (RI.get(MO.getReg()).AsmName);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 else
aslcdb49b92008-07-19 13:16:32 +0000377 O << '$' << MO.getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 break;
379
380 case MachineOperand::MO_Immediate:
Bruno Cardoso Lopes60f3e052008-08-13 07:13:40 +0000381 O << (short int)MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382 break;
383
384 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000385 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 return;
387
388 case MachineOperand::MO_GlobalAddress:
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000389 O << Mang->getMangledName(MO.getGlobal());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 break;
391
392 case MachineOperand::MO_ExternalSymbol:
393 O << MO.getSymbolName();
394 break;
395
Bruno Cardoso Lopesea377302007-11-12 19:49:57 +0000396 case MachineOperand::MO_JumpTableIndex:
397 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000398 << '_' << MO.getIndex();
Bruno Cardoso Lopesea377302007-11-12 19:49:57 +0000399 break;
400
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 case MachineOperand::MO_ConstantPoolIndex:
402 O << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner6017d482007-12-30 23:10:15 +0000403 << getFunctionNumber() << "_" << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 break;
405
406 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000407 llvm_unreachable("<unknown operand type>");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408 }
409
410 if (closeP) O << ")";
411}
412
413void MipsAsmPrinter::
Chris Lattner8b91d2d2009-07-21 17:39:48 +0000414printUnsignedImm(const MachineInstr *MI, int opNum) {
Bruno Cardoso Lopes60f3e052008-08-13 07:13:40 +0000415 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::
Chris Lattner8b91d2d2009-07-21 17:39:48 +0000423printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier) {
Bruno Cardoso Lopes96433662007-09-24 20:15:11 +0000424 // when using stack locations for not load/store instructions
425 // print the same way as all normal 3 operand instructions.
426 if (Modifier && !strcmp(Modifier, "stackloc")) {
427 printOperand(MI, opNum+1);
428 O << ", ";
429 printOperand(MI, opNum);
430 return;
431 }
432
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000433 // Load/Store memory operands -- imm($reg)
434 // If PIC target the target is loaded as the
435 // pattern lw $25,%call16($28)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 printOperand(MI, opNum);
437 O << "(";
438 printOperand(MI, opNum+1);
439 O << ")";
440}
441
Bruno Cardoso Lopes4fb1f542008-07-05 19:05:21 +0000442void MipsAsmPrinter::
Chris Lattner8b91d2d2009-07-21 17:39:48 +0000443printFCCOperand(const MachineInstr *MI, int opNum, const char *Modifier) {
Bruno Cardoso Lopes4fb1f542008-07-05 19:05:21 +0000444 const MachineOperand& MO = MI->getOperand(opNum);
445 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
446}
447
Chris Lattner8b91d2d2009-07-21 17:39:48 +0000448bool MipsAsmPrinter::doInitialization(Module &M) {
449 // FIXME: Use SwitchToDataSection.
450
Bruno Cardoso Lopes29c15352008-07-14 14:42:54 +0000451 // Tell the assembler which ABI we are using
aslcdb49b92008-07-19 13:16:32 +0000452 O << "\t.section .mdebug." << emitCurrentABIString() << '\n';
Bruno Cardoso Lopes29c15352008-07-14 14:42:54 +0000453
454 // TODO: handle O64 ABI
455 if (Subtarget->isABI_EABI())
456 O << "\t.section .gcc_compiled_long" <<
aslcdb49b92008-07-19 13:16:32 +0000457 (Subtarget->isGP32bit() ? "32" : "64") << '\n';
Bruno Cardoso Lopes29c15352008-07-14 14:42:54 +0000458
459 // return to previous section
aslcdb49b92008-07-19 13:16:32 +0000460 O << "\t.previous" << '\n';
Bruno Cardoso Lopes29c15352008-07-14 14:42:54 +0000461
Chris Lattner8b91d2d2009-07-21 17:39:48 +0000462 return AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463}
464
asldedcd842008-07-19 13:16:11 +0000465void MipsAsmPrinter::
Chris Lattner8b91d2d2009-07-21 17:39:48 +0000466printModuleLevelGV(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 const TargetData *TD = TM.getTargetData();
468
asldedcd842008-07-19 13:16:11 +0000469 if (!GVar->hasInitializer())
470 return; // External global require no code
471
472 // Check to see if this is a special global used by LLVM, if so, emit it.
473 if (EmitSpecialLLVMGlobal(GVar))
474 return;
475
476 O << "\n\n";
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000477 std::string name = Mang->getMangledName(GVar);
asldedcd842008-07-19 13:16:11 +0000478 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000479 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000480 return;
asldedcd842008-07-19 13:16:11 +0000481 const Type *CTy = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000482 unsigned Size = TD->getTypeAllocSize(CTy);
Bruno Cardoso Lopes12355a82008-07-21 18:52:34 +0000483 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
asldedcd842008-07-19 13:16:11 +0000484 bool printSizeAndType = true;
485
486 // A data structure or array is aligned in memory to the largest
487 // alignment boundary required by any data type inside it (this matches
488 // the Preferred Type Alignment). For integral types, the alignment is
489 // the type size.
asldedcd842008-07-19 13:16:11 +0000490 unsigned Align;
491 if (CTy->getTypeID() == Type::IntegerTyID ||
492 CTy->getTypeID() == Type::VoidTyID) {
493 assert(!(Size & (Size-1)) && "Alignment is not a power of two!");
494 Align = Log2_32(Size);
495 } else
496 Align = TD->getPreferredTypeAlignmentShift(CTy);
497
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000498 printVisibility(name, GVar->getVisibility());
asldedcd842008-07-19 13:16:11 +0000499
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000500 SwitchToSection(TAI->SectionForGlobal(GVar));
asldedcd842008-07-19 13:16:11 +0000501
502 if (C->isNullValue() && !GVar->hasSection()) {
503 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000504 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
asldedcd842008-07-19 13:16:11 +0000505 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
506
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000507 if (GVar->hasLocalLinkage())
Bruno Cardoso Lopes8642dfd2008-07-28 19:11:24 +0000508 O << "\t.local\t" << name << '\n';
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000509
Bruno Cardoso Lopes8642dfd2008-07-28 19:11:24 +0000510 O << TAI->getCOMMDirective() << name << ',' << Size;
511 if (TAI->getCOMMDirectiveTakesAlignment())
512 O << ',' << (1 << Align);
513
asldedcd842008-07-19 13:16:11 +0000514 O << '\n';
515 return;
516 }
517 }
518 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000519 case GlobalValue::LinkOnceAnyLinkage:
520 case GlobalValue::LinkOnceODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000521 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000522 case GlobalValue::WeakAnyLinkage:
523 case GlobalValue::WeakODRLinkage:
asldedcd842008-07-19 13:16:11 +0000524 // FIXME: Verify correct for weak.
525 // Nonnull linkonce -> weak
aslcdb49b92008-07-19 13:16:32 +0000526 O << "\t.weak " << name << '\n';
asldedcd842008-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
aslcdb49b92008-07-19 13:16:32 +0000533 O << TAI->getGlobalDirective() << name << '\n';
asldedcd842008-07-19 13:16:11 +0000534 // Fall Through
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000535 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000536 case GlobalValue::LinkerPrivateLinkage:
asldedcd842008-07-19 13:16:11 +0000537 case GlobalValue::InternalLinkage:
Owen Anderson8d9397a2009-07-13 21:27:19 +0000538 if (CVA && CVA->isCString())
Anton Korobeynikovb59b2292008-08-07 09:53:38 +0000539 printSizeAndType = false;
asldedcd842008-07-19 13:16:11 +0000540 break;
541 case GlobalValue::GhostLinkage:
Edwin Törökbd448e32009-07-14 16:55:14 +0000542 llvm_unreachable("Should not have any unmaterialized functions!");
asldedcd842008-07-19 13:16:11 +0000543 case GlobalValue::DLLImportLinkage:
Edwin Törökbd448e32009-07-14 16:55:14 +0000544 llvm_unreachable("DLLImport linkage is not supported by this target!");
asldedcd842008-07-19 13:16:11 +0000545 case GlobalValue::DLLExportLinkage:
Edwin Törökbd448e32009-07-14 16:55:14 +0000546 llvm_unreachable("DLLExport linkage is not supported by this target!");
asldedcd842008-07-19 13:16:11 +0000547 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000548 llvm_unreachable("Unknown linkage type!");
asldedcd842008-07-19 13:16:11 +0000549 }
550
Anton Korobeynikovb59b2292008-08-07 09:53:38 +0000551 EmitAlignment(Align, GVar);
asldedcd842008-07-19 13:16:11 +0000552
553 if (TAI->hasDotTypeDotSizeDirective() && printSizeAndType) {
554 O << "\t.type " << name << ",@object\n";
aslcdb49b92008-07-19 13:16:32 +0000555 O << "\t.size " << name << ',' << Size << '\n';
asldedcd842008-07-19 13:16:11 +0000556 }
557
558 O << name << ":\n";
559 EmitGlobalConstant(C);
560}
561
562bool MipsAsmPrinter::
563doFinalization(Module &M)
564{
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 // Print out module-level global variables here.
566 for (Module::const_global_iterator I = M.global_begin(),
567 E = M.global_end(); I != E; ++I)
asldedcd842008-07-19 13:16:11 +0000568 printModuleLevelGV(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569
aslcdb49b92008-07-19 13:16:32 +0000570 O << '\n';
Bruno Cardoso Lopesdb260942008-06-04 01:45:25 +0000571
Dan Gohman4a558a32007-07-25 19:33:14 +0000572 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000574
Bob Wilsonebbc1c42009-06-23 23:59:40 +0000575// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000576extern "C" void LLVMInitializeMipsAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000577 TargetRegistry::RegisterAsmPrinter(TheMipsTarget, createMipsCodePrinterPass);
578
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000579 TargetRegistry::RegisterAsmPrinter(TheMipselTarget,
580 createMipsCodePrinterPass);
581}