blob: 8c275015758a5d4196e87395117de4b9dd5276de [file] [log] [blame]
Brian Gaeke4acfd032004-03-04 06:00:41 +00001//===-- SparcV8AsmPrinter.cpp - SparcV8 LLVM assembly writer --------------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Brian Gaeke4acfd032004-03-04 06:00:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Brian Gaeke4acfd032004-03-04 06:00:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to GAS-format Sparc V8 assembly language.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SparcV8.h"
16#include "SparcV8InstrInfo.h"
17#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Module.h"
20#include "llvm/Assembly/Writer.h"
Chris Lattner1dbed162005-12-17 07:04:29 +000021#include "llvm/CodeGen/AsmPrinter.h"
Brian Gaeke4acfd032004-03-04 06:00:41 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineConstantPool.h"
24#include "llvm/CodeGen/MachineInstr.h"
25#include "llvm/Target/TargetMachine.h"
26#include "llvm/Support/Mangler.h"
Brian Gaeke74dfcf12004-09-02 02:37:43 +000027#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/StringExtras.h"
29#include "llvm/Support/CommandLine.h"
Jim Laskeyb8df7c22005-08-17 20:04:34 +000030#include "llvm/Support/MathExtras.h"
Brian Gaekea8b00ca2004-03-06 05:30:21 +000031#include <cctype>
Brian Gaeke4acfd032004-03-04 06:00:41 +000032using namespace llvm;
33
34namespace {
35 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
36
Chris Lattner1dbed162005-12-17 07:04:29 +000037 struct SparcV8AsmPrinter : public AsmPrinter {
Chris Lattnerb5e9eb62005-12-17 07:11:43 +000038 SparcV8AsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) {
39 Data16bitsDirective = "\t.half\t";
40 Data32bitsDirective = "\t.word\t";
41 Data64bitsDirective = "\t.xword\t";
42 }
Brian Gaeke4acfd032004-03-04 06:00:41 +000043
44 /// We name each basic block in a Function with a unique number, so
45 /// that we can consistently refer to them later. This is cleared
46 /// at the beginning of each call to runOnMachineFunction().
47 ///
48 typedef std::map<const Value *, unsigned> ValueMapTy;
49 ValueMapTy NumberForBB;
50
Brian Gaeke4acfd032004-03-04 06:00:41 +000051 virtual const char *getPassName() const {
52 return "SparcV8 Assembly Printer";
53 }
54
55 void emitConstantValueOnly(const Constant *CV);
56 void emitGlobalConstant(const Constant *CV);
Brian Gaeke446ae112004-06-15 19:52:59 +000057 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattner994b7352005-12-16 06:34:17 +000058 bool printInstruction(const MachineInstr *MI); // autogenerated.
Misha Brukmanb5f662f2005-04-21 23:30:14 +000059 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke4acfd032004-03-04 06:00:41 +000060 bool doInitialization(Module &M);
61 bool doFinalization(Module &M);
62 };
63} // end of anonymous namespace
64
Chris Lattner994b7352005-12-16 06:34:17 +000065#include "SparcV8GenAsmWriter.inc"
66
Brian Gaeke4acfd032004-03-04 06:00:41 +000067/// createSparcV8CodePrinterPass - Returns a pass that prints the SparcV8
68/// assembly code for a MachineFunction to the given output stream,
69/// using the given target machine description. This should work
70/// regardless of whether the function is in SSA form.
71///
72FunctionPass *llvm::createSparcV8CodePrinterPass (std::ostream &o,
73 TargetMachine &tm) {
Chris Lattner994b7352005-12-16 06:34:17 +000074 return new SparcV8AsmPrinter(o, tm);
Brian Gaeke4acfd032004-03-04 06:00:41 +000075}
76
77/// toOctal - Convert the low order bits of X into an octal digit.
78///
79static inline char toOctal(int X) {
80 return (X&7)+'0';
81}
82
83/// getAsCString - Return the specified array as a C compatible
84/// string, only if the predicate isStringCompatible is true.
85///
86static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
87 assert(CVA->isString() && "Array is not string compatible!");
88
89 O << "\"";
90 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
91 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
92
93 if (C == '"') {
94 O << "\\\"";
95 } else if (C == '\\') {
96 O << "\\\\";
97 } else if (isprint(C)) {
98 O << C;
99 } else {
100 switch(C) {
101 case '\b': O << "\\b"; break;
102 case '\f': O << "\\f"; break;
103 case '\n': O << "\\n"; break;
104 case '\r': O << "\\r"; break;
105 case '\t': O << "\\t"; break;
106 default:
107 O << '\\';
108 O << toOctal(C >> 6);
109 O << toOctal(C >> 3);
110 O << toOctal(C >> 0);
111 break;
112 }
113 }
114 }
115 O << "\"";
116}
117
118// Print out the specified constant, without a storage class. Only the
119// constants valid in constant expressions can occur here.
Chris Lattner994b7352005-12-16 06:34:17 +0000120void SparcV8AsmPrinter::emitConstantValueOnly(const Constant *CV) {
Brian Gaeke54799c22004-11-14 03:22:05 +0000121 if (CV->isNullValue() || isa<UndefValue> (CV))
Brian Gaeke4acfd032004-03-04 06:00:41 +0000122 O << "0";
123 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
124 assert(CB == ConstantBool::True);
125 O << "1";
126 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
127 if (((CI->getValue() << 32) >> 32) == CI->getValue())
128 O << CI->getValue();
129 else
130 O << (unsigned long long)CI->getValue();
131 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
132 O << CI->getValue();
Chris Lattner73302482004-07-18 07:26:17 +0000133 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Brian Gaeke4acfd032004-03-04 06:00:41 +0000134 // This is a constant address for a global variable or function. Use the
135 // name of the variable or function as the address value.
Chris Lattner73302482004-07-18 07:26:17 +0000136 O << Mang->getValueName(GV);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000137 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
138 const TargetData &TD = TM.getTargetData();
139 switch(CE->getOpcode()) {
140 case Instruction::GetElementPtr: {
141 // generate a symbolic expression for the byte address
142 const Constant *ptrVal = CE->getOperand(0);
143 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
144 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
145 O << "(";
146 emitConstantValueOnly(ptrVal);
147 O << ") + " << Offset;
148 } else {
149 emitConstantValueOnly(ptrVal);
150 }
151 break;
152 }
153 case Instruction::Cast: {
154 // Support only non-converting or widening casts for now, that is, ones
155 // that do not involve a change in value. This assertion is really gross,
156 // and may not even be a complete check.
157 Constant *Op = CE->getOperand(0);
158 const Type *OpTy = Op->getType(), *Ty = CE->getType();
159
160 // Pointers on ILP32 machines can be losslessly converted back and
161 // forth into 32-bit or wider integers, regardless of signedness.
162 assert(((isa<PointerType>(OpTy)
163 && (Ty == Type::LongTy || Ty == Type::ULongTy
164 || Ty == Type::IntTy || Ty == Type::UIntTy))
165 || (isa<PointerType>(Ty)
166 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
167 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
168 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
169 && OpTy->isLosslesslyConvertibleTo(Ty))))
170 && "FIXME: Don't yet support this kind of constant cast expr");
171 O << "(";
172 emitConstantValueOnly(Op);
173 O << ")";
174 break;
175 }
176 case Instruction::Add:
177 O << "(";
178 emitConstantValueOnly(CE->getOperand(0));
179 O << ") + (";
180 emitConstantValueOnly(CE->getOperand(1));
181 O << ")";
182 break;
183 default:
184 assert(0 && "Unsupported operator!");
185 }
186 } else {
187 assert(0 && "Unknown constant value!");
188 }
189}
190
191// Print a constant value or values, with the appropriate storage class as a
192// prefix.
Chris Lattner994b7352005-12-16 06:34:17 +0000193void SparcV8AsmPrinter::emitGlobalConstant(const Constant *CV) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000194 const TargetData &TD = TM.getTargetData();
195
Brian Gaeke9d2427c2004-06-18 08:59:16 +0000196 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000197 if (CVA->isString()) {
198 O << "\t.ascii\t";
199 printAsCString(O, CVA);
200 O << "\n";
201 } else { // Not a string. Print the values in successive locations
Chris Lattnercdf70122004-08-04 17:27:27 +0000202 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; i++)
203 emitGlobalConstant(CVA->getOperand(i));
Brian Gaeke4acfd032004-03-04 06:00:41 +0000204 }
205 return;
206 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
207 // Print the fields in successive locations. Pad to align if needed!
208 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Brian Gaeke4acfd032004-03-04 06:00:41 +0000209 unsigned sizeSoFar = 0;
Chris Lattnercdf70122004-08-04 17:27:27 +0000210 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; i++) {
211 const Constant* field = CVS->getOperand(i);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000212
213 // Check if padding is needed and insert one or more 0s.
214 unsigned fieldSize = TD.getTypeSize(field->getType());
Chris Lattnercdf70122004-08-04 17:27:27 +0000215 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Brian Gaeke4acfd032004-03-04 06:00:41 +0000216 : cvsLayout->MemberOffsets[i+1])
217 - cvsLayout->MemberOffsets[i]) - fieldSize;
218 sizeSoFar += fieldSize + padSize;
219
220 // Now print the actual field value
221 emitGlobalConstant(field);
222
223 // Insert the field padding unless it's zero bytes...
224 if (padSize)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000225 O << "\t.skip\t " << padSize << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000226 }
227 assert(sizeSoFar == cvsLayout->StructSize &&
228 "Layout of constant struct may be incorrect!");
229 return;
230 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
231 // FP Constants are printed as integer constants to avoid losing
232 // precision...
233 double Val = CFP->getValue();
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000234 switch (CFP->getType()->getTypeID()) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000235 default: assert(0 && "Unknown floating point type!");
236 case Type::FloatTyID: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000237 O << ".long\t" << FloatToBits(Val) << "\t! float " << Val << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000238 return;
239 }
240 case Type::DoubleTyID: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000241 O << ".word\t0x" << std::hex << (DoubleToBits(Val) >> 32) << std::dec << "\t! double " << Val << "\n";
242 O << ".word\t0x" << std::hex << (DoubleToBits(Val) & 0xffffffffUL) << std::dec << "\t! double " << Val << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000243 return;
244 }
245 }
Brian Gaeke54799c22004-11-14 03:22:05 +0000246 } else if (isa<UndefValue> (CV)) {
247 unsigned size = TD.getTypeSize (CV->getType ());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000248 O << "\t.skip\t " << size << "\n";
Brian Gaeke54799c22004-11-14 03:22:05 +0000249 return;
Brian Gaeke4dd043f2004-11-23 21:10:49 +0000250 } else if (isa<ConstantAggregateZero> (CV)) {
251 unsigned size = TD.getTypeSize (CV->getType ());
252 for (unsigned i = 0; i < size; ++i)
Misha Brukman27177f82005-04-22 18:06:01 +0000253 O << "\t.byte 0\n";
Brian Gaeke4dd043f2004-11-23 21:10:49 +0000254 return;
Brian Gaeke4acfd032004-03-04 06:00:41 +0000255 }
256
257 const Type *type = CV->getType();
258 O << "\t";
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000259 switch (type->getTypeID()) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000260 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
261 O << ".byte";
262 break;
263 case Type::UShortTyID: case Type::ShortTyID:
Brian Gaeke3bf960c2004-12-09 18:51:01 +0000264 O << ".half";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000265 break;
266 case Type::FloatTyID: case Type::PointerTyID:
267 case Type::UIntTyID: case Type::IntTyID:
Brian Gaeke3bf960c2004-12-09 18:51:01 +0000268 O << ".word";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000269 break;
270 case Type::DoubleTyID:
271 case Type::ULongTyID: case Type::LongTyID:
Brian Gaeke3bf960c2004-12-09 18:51:01 +0000272 O << ".xword";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000273 break;
274 default:
275 assert (0 && "Can't handle printing this type of thing");
276 break;
277 }
278 O << "\t";
279 emitConstantValueOnly(CV);
280 O << "\n";
281}
282
Brian Gaeke4acfd032004-03-04 06:00:41 +0000283/// runOnMachineFunction - This uses the printMachineInstruction()
284/// method to print assembly for each instruction.
285///
Chris Lattner994b7352005-12-16 06:34:17 +0000286bool SparcV8AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner1dbed162005-12-17 07:04:29 +0000287 SetupMachineFunction(MF);
288
Chris Lattnerb5e9eb62005-12-17 07:11:43 +0000289 // Print out constants referenced by the function
290 EmitConstantPool(MF.getConstantPool());
291
Brian Gaeke4acfd032004-03-04 06:00:41 +0000292 // BBNumber is used here so that a given Printer will never give two
293 // BBs the same name. (If you have a better way, please let me know!)
294 static unsigned BBNumber = 0;
295
296 O << "\n\n";
297 // What's my mangled name?
298 CurrentFnName = Mang->getValueName(MF.getFunction());
299
Brian Gaeke4acfd032004-03-04 06:00:41 +0000300 // Print out labels for the function.
301 O << "\t.text\n";
302 O << "\t.align 16\n";
303 O << "\t.globl\t" << CurrentFnName << "\n";
Brian Gaeke54cc3c22004-03-16 22:52:04 +0000304 O << "\t.type\t" << CurrentFnName << ", #function\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000305 O << CurrentFnName << ":\n";
306
307 // Number each basic block so that we can consistently refer to them
308 // in PC-relative references.
309 NumberForBB.clear();
310 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
311 I != E; ++I) {
312 NumberForBB[I->getBasicBlock()] = BBNumber++;
313 }
314
315 // Print out code for the function.
316 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
317 I != E; ++I) {
318 // Print a label for the basic block.
Brian Gaeke09c13092004-06-17 19:39:23 +0000319 O << ".LBB" << Mang->getValueName(MF.getFunction ())
320 << "_" << I->getNumber () << ":\t! "
321 << I->getBasicBlock ()->getName () << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000322 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman27177f82005-04-22 18:06:01 +0000323 II != E; ++II) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000324 // Print the assembly for the instruction.
Chris Lattner0d8fcd32005-12-17 06:54:41 +0000325 O << "\t";
326 printInstruction(II);
Chris Lattner1dbed162005-12-17 07:04:29 +0000327 ++EmittedInsts;
Brian Gaeke4acfd032004-03-04 06:00:41 +0000328 }
329 }
330
331 // We didn't modify anything.
332 return false;
333}
334
Chris Lattner994b7352005-12-16 06:34:17 +0000335void SparcV8AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000336 const MachineOperand &MO = MI->getOperand (opNum);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000337 const MRegisterInfo &RI = *TM.getRegisterInfo();
Brian Gaeke446ae112004-06-15 19:52:59 +0000338 bool CloseParen = false;
339 if (MI->getOpcode() == V8::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
340 O << "%hi(";
341 CloseParen = true;
Misha Brukmanf54ef972004-06-24 23:38:20 +0000342 } else if (MI->getOpcode() ==V8::ORri &&!MO.isRegister() &&!MO.isImmediate())
343 {
Brian Gaeke446ae112004-06-15 19:52:59 +0000344 O << "%lo(";
345 CloseParen = true;
346 }
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000347 switch (MO.getType()) {
348 case MachineOperand::MO_VirtualRegister:
349 if (Value *V = MO.getVRegValueOrNull()) {
350 O << "<" << V->getName() << ">";
Brian Gaeke446ae112004-06-15 19:52:59 +0000351 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000352 }
353 // FALLTHROUGH
354 case MachineOperand::MO_MachineRegister:
355 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaekea8b00ca2004-03-06 05:30:21 +0000356 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000357 else
358 O << "%reg" << MO.getReg();
Brian Gaeke446ae112004-06-15 19:52:59 +0000359 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000360
361 case MachineOperand::MO_SignExtendedImmed:
362 case MachineOperand::MO_UnextendedImmed:
363 O << (int)MO.getImmedValue();
Brian Gaeke446ae112004-06-15 19:52:59 +0000364 break;
Brian Gaeke09c13092004-06-17 19:39:23 +0000365 case MachineOperand::MO_MachineBasicBlock: {
366 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
367 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
368 << "_" << MBBOp->getNumber () << "\t! "
369 << MBBOp->getBasicBlock ()->getName ();
370 return;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000371 }
Brian Gaeke09c13092004-06-17 19:39:23 +0000372 case MachineOperand::MO_PCRelativeDisp:
373 std::cerr << "Shouldn't use addPCDisp() when building SparcV8 MachineInstrs";
374 abort ();
375 return;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000376 case MachineOperand::MO_GlobalAddress:
377 O << Mang->getValueName(MO.getGlobal());
Brian Gaeke446ae112004-06-15 19:52:59 +0000378 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000379 case MachineOperand::MO_ExternalSymbol:
380 O << MO.getSymbolName();
Brian Gaeke446ae112004-06-15 19:52:59 +0000381 break;
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000382 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnerb5e9eb62005-12-17 07:11:43 +0000383 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
384 << MO.getConstantPoolIndex();
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000385 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000386 default:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000387 O << "<unknown operand type>"; abort (); break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000388 }
Brian Gaeke446ae112004-06-15 19:52:59 +0000389 if (CloseParen) O << ")";
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000390}
391
Chris Lattner994b7352005-12-16 06:34:17 +0000392bool SparcV8AsmPrinter::doInitialization(Module &M) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000393 Mang = new Mangler(M);
394 return false; // success
395}
396
Chris Lattner994b7352005-12-16 06:34:17 +0000397bool SparcV8AsmPrinter::doFinalization(Module &M) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000398 const TargetData &TD = TM.getTargetData();
Brian Gaeke4acfd032004-03-04 06:00:41 +0000399
400 // Print out module-level global variables here.
Chris Lattnere4d5c442005-03-15 04:54:21 +0000401 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Brian Gaeke4acfd032004-03-04 06:00:41 +0000402 if (I->hasInitializer()) { // External global require no code
403 O << "\n\n";
404 std::string name = Mang->getValueName(I);
405 Constant *C = I->getInitializer();
406 unsigned Size = TD.getTypeSize(C->getType());
407 unsigned Align = TD.getTypeAlignment(C->getType());
408
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000409 if (C->isNullValue() &&
Brian Gaeke4acfd032004-03-04 06:00:41 +0000410 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
411 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner1dbed162005-12-17 07:04:29 +0000412 SwitchSection(".data", I);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000413 if (I->hasInternalLinkage())
414 O << "\t.local " << name << "\n";
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000415
Brian Gaeke4acfd032004-03-04 06:00:41 +0000416 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
417 << "," << (unsigned)TD.getTypeAlignment(C->getType());
Brian Gaeke79db7402004-03-16 22:37:12 +0000418 O << "\t\t! ";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000419 WriteAsOperand(O, I, true, true, &M);
420 O << "\n";
421 } else {
422 switch (I->getLinkage()) {
423 case GlobalValue::LinkOnceLinkage:
424 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
425 // Nonnull linkonce -> weak
426 O << "\t.weak " << name << "\n";
Chris Lattner1dbed162005-12-17 07:04:29 +0000427 SwitchSection("", I);
428 O << "\t.section\t\".llvm.linkonce.d." << name
429 << "\",\"aw\",@progbits\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000430 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000431
Brian Gaeke4acfd032004-03-04 06:00:41 +0000432 case GlobalValue::AppendingLinkage:
433 // FIXME: appending linkage variables should go into a section of
434 // their name or something. For now, just emit them as external.
435 case GlobalValue::ExternalLinkage:
436 // If external or appending, declare as a global symbol
437 O << "\t.globl " << name << "\n";
438 // FALL THROUGH
439 case GlobalValue::InternalLinkage:
440 if (C->isNullValue())
Chris Lattner1dbed162005-12-17 07:04:29 +0000441 SwitchSection(".bss", I);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000442 else
Chris Lattner1dbed162005-12-17 07:04:29 +0000443 SwitchSection(".data", I);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000444 break;
Misha Brukmanc11c44f2004-11-19 21:49:19 +0000445 case GlobalValue::GhostLinkage:
446 std::cerr << "Should not have any unmaterialized functions!\n";
447 abort();
Brian Gaeke4acfd032004-03-04 06:00:41 +0000448 }
449
450 O << "\t.align " << Align << "\n";
Brian Gaeke54cc3c22004-03-16 22:52:04 +0000451 O << "\t.type " << name << ",#object\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000452 O << "\t.size " << name << "," << Size << "\n";
Brian Gaeke79db7402004-03-16 22:37:12 +0000453 O << name << ":\t\t\t\t! ";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000454 WriteAsOperand(O, I, true, true, &M);
455 O << " = ";
456 WriteAsOperand(O, C, false, false, &M);
457 O << "\n";
458 emitGlobalConstant(C);
459 }
460 }
461
Chris Lattner1dbed162005-12-17 07:04:29 +0000462 AsmPrinter::doFinalization(M);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000463 return false; // success
464}