blob: 0587d9859c1a06e1922d51f891210144b42a0ab1 [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"
18#include "MipsInstrInfo.h"
19#include "MipsTargetMachine.h"
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000020#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000021#include "llvm/Constants.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/Module.h"
24#include "llvm/CodeGen/AsmPrinter.h"
25#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000028#include "llvm/CodeGen/MachineInstr.h"
29#include "llvm/Target/TargetAsmInfo.h"
30#include "llvm/Target/TargetData.h"
31#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000032#include "llvm/Target/TargetOptions.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/Support/Mangler.h"
34#include "llvm/ADT/Statistic.h"
35#include "llvm/ADT/StringExtras.h"
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000036#include "llvm/Support/Debug.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000037#include "llvm/Support/CommandLine.h"
38#include "llvm/Support/MathExtras.h"
39#include <cctype>
40
41using namespace llvm;
42
43STATISTIC(EmittedInsts, "Number of machine instrs printed");
44
45namespace {
46 struct VISIBILITY_HIDDEN MipsAsmPrinter : public AsmPrinter {
47 MipsAsmPrinter(std::ostream &O, MipsTargetMachine &TM,
48 const TargetAsmInfo *T):
49 AsmPrinter(O, TM, T) {}
50
51 virtual const char *getPassName() const {
52 return "Mips Assembly Printer";
53 }
54
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000055 enum SetDirectiveFlags {
56 REORDER, // enables instruction reordering.
57 NOREORDER, // disables instruction reordering.
58 MACRO, // enables GAS macros.
59 NOMACRO // disables GAS macros.
60 };
61
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);
65
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000066 unsigned int getSavedRegsBitmask(bool isFloat, MachineFunction &MF);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000067 void printHex32(unsigned int Value);
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000068
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000069 void emitFunctionStart(MachineFunction &MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +000070 void emitFunctionEnd(MachineFunction &MF);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000071 void emitFrameDirective(MachineFunction &MF);
72 void emitMaskDirective(MachineFunction &MF);
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000073 void emitFMaskDirective(MachineFunction &MF);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +000074 void emitSetDirective(SetDirectiveFlags Flag);
75
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000076 bool printInstruction(const MachineInstr *MI); // autogenerated.
77 bool runOnMachineFunction(MachineFunction &F);
78 bool doInitialization(Module &M);
79 bool doFinalization(Module &M);
80 };
81} // end of anonymous namespace
82
83#include "MipsGenAsmWriter.inc"
84
85/// createMipsCodePrinterPass - Returns a pass that prints the MIPS
86/// assembly code for a MachineFunction to the given output stream,
87/// using the given target machine description. This should work
88/// regardless of whether the function is in SSA form.
89FunctionPass *llvm::createMipsCodePrinterPass(std::ostream &o,
90 MipsTargetMachine &tm)
91{
92 return new MipsAsmPrinter(o, tm, tm.getTargetAsmInfo());
93}
94
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +000095//===----------------------------------------------------------------------===//
96//
97// Mips Asm Directives
98//
99// -- Frame directive "frame Stackpointer, Stacksize, RARegister"
100// Describe the stack frame.
101//
102// -- Mask directives "(f)mask bitmask, offset"
103// Tells the assembler which registers are saved and where.
104// bitmask - contain a little endian bitset indicating which registers are
105// saved on function prologue (e.g. with a 0x80000000 mask, the
106// assembler knows the register 31 (RA) is saved at prologue.
107// offset - the position before stack pointer subtraction indicating where
108// the first saved register on prologue is located. (e.g. with a
109//
110// Consider the following function prologue:
111//
Bill Wendling6ef781f2008-02-27 06:33:05 +0000112// .frame $fp,48,$ra
113// .mask 0xc0000000,-8
114// addiu $sp, $sp, -48
115// sw $ra, 40($sp)
116// sw $fp, 36($sp)
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000117//
118// With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
119// 30 (FP) are saved at prologue. As the save order on prologue is from
120// left to right, RA is saved first. A -8 offset means that after the
121// stack pointer subtration, the first register in the mask (RA) will be
122// saved at address 48-8=40.
123//
124//===----------------------------------------------------------------------===//
125
126/// Mask directive for GPR
127void MipsAsmPrinter::
128emitMaskDirective(MachineFunction &MF)
129{
130 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
131
132 int StackSize = MF.getFrameInfo()->getStackSize();
133 int Offset = (!MipsFI->getTopSavedRegOffset()) ? 0 :
134 (-(StackSize-MipsFI->getTopSavedRegOffset()));
135
136 #ifndef NDEBUG
137 DOUT << "--> emitMaskDirective" << "\n";
138 DOUT << "StackSize : " << StackSize << "\n";
139 DOUT << "getTopSavedReg : " << MipsFI->getTopSavedRegOffset() << "\n";
140 DOUT << "Offset : " << Offset << "\n\n";
141 #endif
142
143 unsigned int Bitmask = getSavedRegsBitmask(false, MF);
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000144 O << "\t.mask \t";
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000145 printHex32(Bitmask);
146 O << "," << Offset << "\n";
147}
148
149/// TODO: Mask Directive for Float Point
150void MipsAsmPrinter::
151emitFMaskDirective(MachineFunction &MF)
152{
153 unsigned int Bitmask = getSavedRegsBitmask(true, MF);
154
155 O << "\t.fmask\t";
156 printHex32(Bitmask);
157 O << ",0" << "\n";
158}
159
160/// Frame Directive
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000161void MipsAsmPrinter::
162emitFrameDirective(MachineFunction &MF)
163{
Dan Gohman6f0d0242008-02-10 18:45:23 +0000164 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000165
166 unsigned stackReg = RI.getFrameRegister(MF);
167 unsigned returnReg = RI.getRARegister();
168 unsigned stackSize = MF.getFrameInfo()->getStackSize();
169
170
Bill Wendling74ab84c2008-02-26 21:11:01 +0000171 O << "\t.frame\t" << "$" << LowercaseString(RI.get(stackReg).AsmName)
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000172 << "," << stackSize << ","
Bill Wendling74ab84c2008-02-26 21:11:01 +0000173 << "$" << LowercaseString(RI.get(returnReg).AsmName)
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000174 << "\n";
175}
176
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000177/// Emit Set directives.
178void MipsAsmPrinter::
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000179emitSetDirective(SetDirectiveFlags Flag)
180{
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000181 O << "\t.set\t";
182 switch(Flag) {
183 case REORDER: O << "reorder" << "\n"; break;
184 case NOREORDER: O << "noreorder" << "\n"; break;
185 case MACRO: O << "macro" << "\n"; break;
186 case NOMACRO: O << "nomacro" << "\n"; break;
187 default: break;
188 }
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000189}
190
191// Create a bitmask with all callee saved registers for CPU
192// or Float Point registers. For CPU registers consider RA,
193// GP and FP for saving if necessary.
194unsigned int MipsAsmPrinter::
195getSavedRegsBitmask(bool isFloat, MachineFunction &MF)
196{
Dan Gohman6f0d0242008-02-10 18:45:23 +0000197 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000198
199 // Float Point Registers, TODO
200 if (isFloat)
201 return 0;
202
203 // CPU Registers
204 unsigned int Bitmask = 0;
205
206 MachineFrameInfo *MFI = MF.getFrameInfo();
207 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
208 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
209 Bitmask |= (1 << MipsRegisterInfo::getRegisterNumbering(CSI[i].getReg()));
210
211 if (RI.hasFP(MF))
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000212 Bitmask |= (1 << MipsRegisterInfo::
213 getRegisterNumbering(RI.getFrameRegister(MF)));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000214
215 if (MF.getFrameInfo()->hasCalls())
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000216 Bitmask |= (1 << MipsRegisterInfo::
217 getRegisterNumbering(RI.getRARegister()));
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000218
219 return Bitmask;
220}
221
222// Print a 32 bit hex number with all numbers.
223void MipsAsmPrinter::
224printHex32(unsigned int Value)
225{
226 O << "0x" << std::hex;
227 for (int i = 7; i >= 0; i--)
228 O << std::hex << ( (Value & (0xF << (i*4))) >> (i*4) );
229 O << std::dec;
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000230}
231
232/// Emit the directives used by GAS on the start of functions
233void MipsAsmPrinter::
234emitFunctionStart(MachineFunction &MF)
235{
236 // Print out the label for the function.
237 const Function *F = MF.getFunction();
238 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
239
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000240 // 2 bits aligned
241 EmitAlignment(2, F);
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000242
243 O << "\t.globl\t" << CurrentFnName << "\n";
244 O << "\t.ent\t" << CurrentFnName << "\n";
245 O << "\t.type\t" << CurrentFnName << ", @function\n";
246 O << CurrentFnName << ":\n";
247
248 emitFrameDirective(MF);
249 emitMaskDirective(MF);
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000250 emitFMaskDirective(MF);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000251
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000252 if (TM.getRelocationModel() == Reloc::Static) {
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000253 emitSetDirective(NOREORDER);
254 emitSetDirective(NOMACRO);
255 }
256
Bruno Cardoso Lopes250a1712007-08-18 02:05:24 +0000257 O << "\n";
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000258}
259
260/// Emit the directives used by GAS on the end of functions
261void MipsAsmPrinter::
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000262emitFunctionEnd(MachineFunction &MF)
263{
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000264 if (TM.getRelocationModel() == Reloc::Static) {
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000265 emitSetDirective(MACRO);
266 emitSetDirective(REORDER);
267 }
268
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000269 O << "\t.end\t" << CurrentFnName << "\n";
270}
271
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000272/// runOnMachineFunction - This uses the printMachineInstruction()
273/// method to print assembly for each instruction.
274bool MipsAsmPrinter::
275runOnMachineFunction(MachineFunction &MF)
276{
277 SetupMachineFunction(MF);
278
279 // Print out constants referenced by the function
280 EmitConstantPool(MF.getConstantPool());
281
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000282 // Print out jump tables referenced by the function
283 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
284
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000285 O << "\n\n";
286
287 // What's my mangled name?
288 CurrentFnName = Mang->getValueName(MF.getFunction());
289
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000290 // Emit the function start directives
291 emitFunctionStart(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000292
293 // Print out code for the function.
294 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
295 I != E; ++I) {
296
297 // Print a label for the basic block.
298 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000299 printBasicBlockLabel(I, true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000300 O << '\n';
301 }
302
303 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
304 II != E; ++II) {
305 // Print the assembly for the instruction.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000306 printInstruction(II);
307 ++EmittedInsts;
308 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000309
310 // Each Basic Block is separated by a newline
311 O << '\n';
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000312 }
313
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000314 // Emit function end directives
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +0000315 emitFunctionEnd(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000316
317 // We didn't modify anything.
318 return false;
319}
320
321void MipsAsmPrinter::
322printOperand(const MachineInstr *MI, int opNum)
323{
324 const MachineOperand &MO = MI->getOperand(opNum);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000325 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000326 bool closeP = false;
327 bool isPIC = (TM.getRelocationModel() == Reloc::PIC_);
328 bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000329
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000330 // %hi and %lo used on mips gas to load global addresses on
331 // static code. %got is used to load global addresses when
332 // using PIC_. %call16 is used to load direct call targets
333 // on PIC_ and small code size. %call_lo and %call_hi load
334 // direct call targets on PIC_ and large code size.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000335 if (MI->getOpcode() == Mips::LUi && !MO.isRegister()
336 && !MO.isImmediate()) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000337 if ((isPIC) && (isCodeLarge))
338 O << "%call_hi(";
339 else
340 O << "%hi(";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000341 closeP = true;
342 } else if ((MI->getOpcode() == Mips::ADDiu) && !MO.isRegister()
343 && !MO.isImmediate()) {
344 O << "%lo(";
345 closeP = true;
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000346 } else if ((isPIC) && (MI->getOpcode() == Mips::LW)
347 && (!MO.isRegister()) && (!MO.isImmediate())) {
348 const MachineOperand &firstMO = MI->getOperand(opNum-1);
349 const MachineOperand &lastMO = MI->getOperand(opNum+1);
350 if ((firstMO.isRegister()) && (lastMO.isRegister())) {
351 if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() == Mips::GP)
352 && (!isCodeLarge))
353 O << "%call16(";
354 else if ((firstMO.getReg() != Mips::T9) && (lastMO.getReg() == Mips::GP))
355 O << "%got(";
356 else if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() != Mips::GP)
357 && (isCodeLarge))
358 O << "%call_lo(";
359 closeP = true;
360 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000361 }
362
363 switch (MO.getType())
364 {
365 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000366 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendling74ab84c2008-02-26 21:11:01 +0000367 O << "$" << LowercaseString (RI.get(MO.getReg()).AsmName);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000368 else
369 O << "$" << MO.getReg();
370 break;
371
372 case MachineOperand::MO_Immediate:
373 if ((MI->getOpcode() == Mips::SLTiu) || (MI->getOpcode() == Mips::ORi) ||
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000374 (MI->getOpcode() == Mips::LUi) || (MI->getOpcode() == Mips::ANDi))
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000375 O << (unsigned short int)MO.getImm();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000376 else
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000377 O << (short int)MO.getImm();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000378 break;
379
380 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000381 printBasicBlockLabel(MO.getMBB());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000382 return;
383
384 case MachineOperand::MO_GlobalAddress:
385 O << Mang->getValueName(MO.getGlobal());
386 break;
387
388 case MachineOperand::MO_ExternalSymbol:
389 O << MO.getSymbolName();
390 break;
391
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000392 case MachineOperand::MO_JumpTableIndex:
393 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000394 << '_' << MO.getIndex();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000395 break;
396
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000397 // FIXME: Verify correct
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000398 case MachineOperand::MO_ConstantPoolIndex:
399 O << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000400 << getFunctionNumber() << "_" << MO.getIndex();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000401 break;
402
403 default:
404 O << "<unknown operand type>"; abort (); break;
405 }
406
407 if (closeP) O << ")";
408}
409
410void MipsAsmPrinter::
411printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier)
412{
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000413 // when using stack locations for not load/store instructions
414 // print the same way as all normal 3 operand instructions.
415 if (Modifier && !strcmp(Modifier, "stackloc")) {
416 printOperand(MI, opNum+1);
417 O << ", ";
418 printOperand(MI, opNum);
419 return;
420 }
421
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000422 // Load/Store memory operands -- imm($reg)
423 // If PIC target the target is loaded as the
424 // pattern lw $25,%call16($28)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000425 printOperand(MI, opNum);
426 O << "(";
427 printOperand(MI, opNum+1);
428 O << ")";
429}
430
431bool MipsAsmPrinter::
432doInitialization(Module &M)
433{
434 Mang = new Mangler(M);
435 return false; // success
436}
437
438bool MipsAsmPrinter::
439doFinalization(Module &M)
440{
441 const TargetData *TD = TM.getTargetData();
442
443 // Print out module-level global variables here.
444 for (Module::const_global_iterator I = M.global_begin(),
445 E = M.global_end(); I != E; ++I)
446
447 // External global require no code
448 if (I->hasInitializer()) {
449
450 // Check to see if this is a special global
451 // used by LLVM, if so, emit it.
452 if (EmitSpecialLLVMGlobal(I))
453 continue;
454
455 O << "\n\n";
456 std::string name = Mang->getValueName(I);
457 Constant *C = I->getInitializer();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000458 unsigned Size = TD->getABITypeSize(C->getType());
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000459 unsigned Align = TD->getPreferredAlignmentLog(I);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000460
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000461 // Is this correct ?
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000462 if (C->isNullValue() && (I->hasLinkOnceLinkage() ||
Dale Johannesenaafce772008-05-14 20:12:51 +0000463 I->hasInternalLinkage() || I->hasWeakLinkage() ||
464 I->hasCommonLinkage()))
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000465 {
466 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000467
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000468 if (!NoZerosInBSS && TAI->getBSSSection())
469 SwitchToDataSection(TAI->getBSSSection(), I);
470 else
471 SwitchToDataSection(TAI->getDataSection(), I);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000472
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000473 if (I->hasInternalLinkage()) {
474 if (TAI->getLCOMMDirective())
475 O << TAI->getLCOMMDirective() << name << "," << Size;
476 else
477 O << "\t.local\t" << name << "\n";
478 } else {
479 O << TAI->getCOMMDirective() << name << "," << Size;
480 // The .comm alignment in bytes.
481 if (TAI->getCOMMDirectiveTakesAlignment())
482 O << "," << (1 << Align);
483 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000484
485 } else {
486
487 switch (I->getLinkage())
488 {
489 case GlobalValue::LinkOnceLinkage:
Dale Johannesenaafce772008-05-14 20:12:51 +0000490 case GlobalValue::CommonLinkage:
491 case GlobalValue::WeakLinkage:
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000492 // FIXME: Verify correct for weak.
493 // Nonnull linkonce -> weak
494 O << "\t.weak " << name << "\n";
495 SwitchToDataSection("", I);
496 O << "\t.section\t\".llvm.linkonce.d." << name
497 << "\",\"aw\",@progbits\n";
498 break;
499 case GlobalValue::AppendingLinkage:
500 // FIXME: appending linkage variables
501 // should go into a section of their name or
502 // something. For now, just emit them as external.
503 case GlobalValue::ExternalLinkage:
504 // If external or appending, declare as a global symbol
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000505 O << TAI->getGlobalDirective() << name << "\n";
506 // Fall Through
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000507 case GlobalValue::InternalLinkage:
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000508 // FIXME: special handling for ".ctors" & ".dtors" sections
509 if (I->hasSection() && (I->getSection() == ".ctors" ||
510 I->getSection() == ".dtors")) {
511 std::string SectionName = ".section " + I->getSection();
512 SectionName += ",\"aw\",%progbits";
513 SwitchToDataSection(SectionName.c_str());
514 } else {
515 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
516 SwitchToDataSection(TAI->getBSSSection(), I);
517 else if (!I->isConstant())
518 SwitchToDataSection(TAI->getDataSection(), I);
519 else {
520 // Read-only data.
521 if (TAI->getReadOnlySection())
522 SwitchToDataSection(TAI->getReadOnlySection(), I);
523 else
524 SwitchToDataSection(TAI->getDataSection(), I);
525 }
526 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000527 break;
528 case GlobalValue::GhostLinkage:
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000529 cerr << "Should not have any unmaterialized functions!\n";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000530 abort();
531 case GlobalValue::DLLImportLinkage:
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000532 cerr << "DLLImport linkage is not supported by this target!\n";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000533 abort();
534 case GlobalValue::DLLExportLinkage:
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000535 cerr << "DLLExport linkage is not supported by this target!\n";
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000536 abort();
537 default:
538 assert(0 && "Unknown linkage type!");
539 }
Bruno Cardoso Lopesdc0c04c2007-08-28 05:06:17 +0000540
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000541 O << "\t.align " << Align << "\n";
542 O << "\t.type " << name << ",@object\n";
543 O << "\t.size " << name << "," << Size << "\n";
544 O << name << ":\n";
545 EmitGlobalConstant(C);
546 }
547 }
548
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000549 O << "\n";
550
Dan Gohmanb8275a32007-07-25 19:33:14 +0000551 return AsmPrinter::doFinalization(M);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000552}