blob: a21f49935893bf5b84df61fdd8e98550105683f8 [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"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineConstantPool.h"
23#include "llvm/CodeGen/MachineInstr.h"
24#include "llvm/Target/TargetMachine.h"
25#include "llvm/Support/Mangler.h"
Brian Gaeke74dfcf12004-09-02 02:37:43 +000026#include "llvm/ADT/Statistic.h"
27#include "llvm/ADT/StringExtras.h"
28#include "llvm/Support/CommandLine.h"
Jim Laskeyb8df7c22005-08-17 20:04:34 +000029#include "llvm/Support/MathExtras.h"
Brian Gaekea8b00ca2004-03-06 05:30:21 +000030#include <cctype>
Brian Gaeke4acfd032004-03-04 06:00:41 +000031using namespace llvm;
32
33namespace {
34 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
35
Chris Lattner994b7352005-12-16 06:34:17 +000036 struct SparcV8AsmPrinter : public MachineFunctionPass {
Brian Gaeke4acfd032004-03-04 06:00:41 +000037 /// Output stream on which we're printing assembly code.
38 ///
39 std::ostream &O;
40
41 /// Target machine description which we query for reg. names, data
42 /// layout, etc.
43 ///
44 TargetMachine &TM;
45
46 /// Name-mangler for global names.
47 ///
48 Mangler *Mang;
49
Chris Lattner994b7352005-12-16 06:34:17 +000050 SparcV8AsmPrinter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke4acfd032004-03-04 06:00:41 +000051
52 /// We name each basic block in a Function with a unique number, so
53 /// that we can consistently refer to them later. This is cleared
54 /// at the beginning of each call to runOnMachineFunction().
55 ///
56 typedef std::map<const Value *, unsigned> ValueMapTy;
57 ValueMapTy NumberForBB;
58
59 /// Cache of mangled name for current function. This is
60 /// recalculated at the beginning of each call to
61 /// runOnMachineFunction().
62 ///
63 std::string CurrentFnName;
64
65 virtual const char *getPassName() const {
66 return "SparcV8 Assembly Printer";
67 }
68
69 void emitConstantValueOnly(const Constant *CV);
70 void emitGlobalConstant(const Constant *CV);
71 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke446ae112004-06-15 19:52:59 +000072 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattner994b7352005-12-16 06:34:17 +000073 bool printInstruction(const MachineInstr *MI); // autogenerated.
Misha Brukmanb5f662f2005-04-21 23:30:14 +000074 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke4acfd032004-03-04 06:00:41 +000075 bool doInitialization(Module &M);
76 bool doFinalization(Module &M);
77 };
78} // end of anonymous namespace
79
Chris Lattner994b7352005-12-16 06:34:17 +000080#include "SparcV8GenAsmWriter.inc"
81
Brian Gaeke4acfd032004-03-04 06:00:41 +000082/// createSparcV8CodePrinterPass - Returns a pass that prints the SparcV8
83/// assembly code for a MachineFunction to the given output stream,
84/// using the given target machine description. This should work
85/// regardless of whether the function is in SSA form.
86///
87FunctionPass *llvm::createSparcV8CodePrinterPass (std::ostream &o,
88 TargetMachine &tm) {
Chris Lattner994b7352005-12-16 06:34:17 +000089 return new SparcV8AsmPrinter(o, tm);
Brian Gaeke4acfd032004-03-04 06:00:41 +000090}
91
92/// toOctal - Convert the low order bits of X into an octal digit.
93///
94static inline char toOctal(int X) {
95 return (X&7)+'0';
96}
97
98/// getAsCString - Return the specified array as a C compatible
99/// string, only if the predicate isStringCompatible is true.
100///
101static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
102 assert(CVA->isString() && "Array is not string compatible!");
103
104 O << "\"";
105 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
106 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
107
108 if (C == '"') {
109 O << "\\\"";
110 } else if (C == '\\') {
111 O << "\\\\";
112 } else if (isprint(C)) {
113 O << C;
114 } else {
115 switch(C) {
116 case '\b': O << "\\b"; break;
117 case '\f': O << "\\f"; break;
118 case '\n': O << "\\n"; break;
119 case '\r': O << "\\r"; break;
120 case '\t': O << "\\t"; break;
121 default:
122 O << '\\';
123 O << toOctal(C >> 6);
124 O << toOctal(C >> 3);
125 O << toOctal(C >> 0);
126 break;
127 }
128 }
129 }
130 O << "\"";
131}
132
133// Print out the specified constant, without a storage class. Only the
134// constants valid in constant expressions can occur here.
Chris Lattner994b7352005-12-16 06:34:17 +0000135void SparcV8AsmPrinter::emitConstantValueOnly(const Constant *CV) {
Brian Gaeke54799c22004-11-14 03:22:05 +0000136 if (CV->isNullValue() || isa<UndefValue> (CV))
Brian Gaeke4acfd032004-03-04 06:00:41 +0000137 O << "0";
138 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
139 assert(CB == ConstantBool::True);
140 O << "1";
141 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
142 if (((CI->getValue() << 32) >> 32) == CI->getValue())
143 O << CI->getValue();
144 else
145 O << (unsigned long long)CI->getValue();
146 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
147 O << CI->getValue();
Chris Lattner73302482004-07-18 07:26:17 +0000148 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Brian Gaeke4acfd032004-03-04 06:00:41 +0000149 // This is a constant address for a global variable or function. Use the
150 // name of the variable or function as the address value.
Chris Lattner73302482004-07-18 07:26:17 +0000151 O << Mang->getValueName(GV);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000152 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
153 const TargetData &TD = TM.getTargetData();
154 switch(CE->getOpcode()) {
155 case Instruction::GetElementPtr: {
156 // generate a symbolic expression for the byte address
157 const Constant *ptrVal = CE->getOperand(0);
158 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
159 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
160 O << "(";
161 emitConstantValueOnly(ptrVal);
162 O << ") + " << Offset;
163 } else {
164 emitConstantValueOnly(ptrVal);
165 }
166 break;
167 }
168 case Instruction::Cast: {
169 // Support only non-converting or widening casts for now, that is, ones
170 // that do not involve a change in value. This assertion is really gross,
171 // and may not even be a complete check.
172 Constant *Op = CE->getOperand(0);
173 const Type *OpTy = Op->getType(), *Ty = CE->getType();
174
175 // Pointers on ILP32 machines can be losslessly converted back and
176 // forth into 32-bit or wider integers, regardless of signedness.
177 assert(((isa<PointerType>(OpTy)
178 && (Ty == Type::LongTy || Ty == Type::ULongTy
179 || Ty == Type::IntTy || Ty == Type::UIntTy))
180 || (isa<PointerType>(Ty)
181 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
182 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
183 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
184 && OpTy->isLosslesslyConvertibleTo(Ty))))
185 && "FIXME: Don't yet support this kind of constant cast expr");
186 O << "(";
187 emitConstantValueOnly(Op);
188 O << ")";
189 break;
190 }
191 case Instruction::Add:
192 O << "(";
193 emitConstantValueOnly(CE->getOperand(0));
194 O << ") + (";
195 emitConstantValueOnly(CE->getOperand(1));
196 O << ")";
197 break;
198 default:
199 assert(0 && "Unsupported operator!");
200 }
201 } else {
202 assert(0 && "Unknown constant value!");
203 }
204}
205
206// Print a constant value or values, with the appropriate storage class as a
207// prefix.
Chris Lattner994b7352005-12-16 06:34:17 +0000208void SparcV8AsmPrinter::emitGlobalConstant(const Constant *CV) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000209 const TargetData &TD = TM.getTargetData();
210
Brian Gaeke9d2427c2004-06-18 08:59:16 +0000211 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000212 if (CVA->isString()) {
213 O << "\t.ascii\t";
214 printAsCString(O, CVA);
215 O << "\n";
216 } else { // Not a string. Print the values in successive locations
Chris Lattnercdf70122004-08-04 17:27:27 +0000217 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; i++)
218 emitGlobalConstant(CVA->getOperand(i));
Brian Gaeke4acfd032004-03-04 06:00:41 +0000219 }
220 return;
221 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
222 // Print the fields in successive locations. Pad to align if needed!
223 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Brian Gaeke4acfd032004-03-04 06:00:41 +0000224 unsigned sizeSoFar = 0;
Chris Lattnercdf70122004-08-04 17:27:27 +0000225 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; i++) {
226 const Constant* field = CVS->getOperand(i);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000227
228 // Check if padding is needed and insert one or more 0s.
229 unsigned fieldSize = TD.getTypeSize(field->getType());
Chris Lattnercdf70122004-08-04 17:27:27 +0000230 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Brian Gaeke4acfd032004-03-04 06:00:41 +0000231 : cvsLayout->MemberOffsets[i+1])
232 - cvsLayout->MemberOffsets[i]) - fieldSize;
233 sizeSoFar += fieldSize + padSize;
234
235 // Now print the actual field value
236 emitGlobalConstant(field);
237
238 // Insert the field padding unless it's zero bytes...
239 if (padSize)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000240 O << "\t.skip\t " << padSize << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000241 }
242 assert(sizeSoFar == cvsLayout->StructSize &&
243 "Layout of constant struct may be incorrect!");
244 return;
245 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
246 // FP Constants are printed as integer constants to avoid losing
247 // precision...
248 double Val = CFP->getValue();
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000249 switch (CFP->getType()->getTypeID()) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000250 default: assert(0 && "Unknown floating point type!");
251 case Type::FloatTyID: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000252 O << ".long\t" << FloatToBits(Val) << "\t! float " << Val << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000253 return;
254 }
255 case Type::DoubleTyID: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000256 O << ".word\t0x" << std::hex << (DoubleToBits(Val) >> 32) << std::dec << "\t! double " << Val << "\n";
257 O << ".word\t0x" << std::hex << (DoubleToBits(Val) & 0xffffffffUL) << std::dec << "\t! double " << Val << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000258 return;
259 }
260 }
Brian Gaeke54799c22004-11-14 03:22:05 +0000261 } else if (isa<UndefValue> (CV)) {
262 unsigned size = TD.getTypeSize (CV->getType ());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000263 O << "\t.skip\t " << size << "\n";
Brian Gaeke54799c22004-11-14 03:22:05 +0000264 return;
Brian Gaeke4dd043f2004-11-23 21:10:49 +0000265 } else if (isa<ConstantAggregateZero> (CV)) {
266 unsigned size = TD.getTypeSize (CV->getType ());
267 for (unsigned i = 0; i < size; ++i)
Misha Brukman27177f82005-04-22 18:06:01 +0000268 O << "\t.byte 0\n";
Brian Gaeke4dd043f2004-11-23 21:10:49 +0000269 return;
Brian Gaeke4acfd032004-03-04 06:00:41 +0000270 }
271
272 const Type *type = CV->getType();
273 O << "\t";
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000274 switch (type->getTypeID()) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000275 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
276 O << ".byte";
277 break;
278 case Type::UShortTyID: case Type::ShortTyID:
Brian Gaeke3bf960c2004-12-09 18:51:01 +0000279 O << ".half";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000280 break;
281 case Type::FloatTyID: case Type::PointerTyID:
282 case Type::UIntTyID: case Type::IntTyID:
Brian Gaeke3bf960c2004-12-09 18:51:01 +0000283 O << ".word";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000284 break;
285 case Type::DoubleTyID:
286 case Type::ULongTyID: case Type::LongTyID:
Brian Gaeke3bf960c2004-12-09 18:51:01 +0000287 O << ".xword";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000288 break;
289 default:
290 assert (0 && "Can't handle printing this type of thing");
291 break;
292 }
293 O << "\t";
294 emitConstantValueOnly(CV);
295 O << "\n";
296}
297
298/// printConstantPool - Print to the current output stream assembly
299/// representations of the constants in the constant pool MCP. This is
300/// used to print out constants which have been "spilled to memory" by
301/// the code generator.
302///
Chris Lattner994b7352005-12-16 06:34:17 +0000303void SparcV8AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000304 const std::vector<Constant*> &CP = MCP->getConstants();
305 const TargetData &TD = TM.getTargetData();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000306
Brian Gaeke4acfd032004-03-04 06:00:41 +0000307 if (CP.empty()) return;
308
309 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Brian Gaekeb27df442004-09-29 03:25:40 +0000310 O << "\t.section \".rodata\"\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000311 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
312 << "\n";
Brian Gaeke79db7402004-03-16 22:37:12 +0000313 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t!"
Brian Gaeke4acfd032004-03-04 06:00:41 +0000314 << *CP[i] << "\n";
315 emitGlobalConstant(CP[i]);
316 }
317}
318
319/// runOnMachineFunction - This uses the printMachineInstruction()
320/// method to print assembly for each instruction.
321///
Chris Lattner994b7352005-12-16 06:34:17 +0000322bool SparcV8AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000323 // BBNumber is used here so that a given Printer will never give two
324 // BBs the same name. (If you have a better way, please let me know!)
325 static unsigned BBNumber = 0;
326
327 O << "\n\n";
328 // What's my mangled name?
329 CurrentFnName = Mang->getValueName(MF.getFunction());
330
331 // Print out constants referenced by the function
332 printConstantPool(MF.getConstantPool());
333
334 // Print out labels for the function.
335 O << "\t.text\n";
336 O << "\t.align 16\n";
337 O << "\t.globl\t" << CurrentFnName << "\n";
Brian Gaeke54cc3c22004-03-16 22:52:04 +0000338 O << "\t.type\t" << CurrentFnName << ", #function\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000339 O << CurrentFnName << ":\n";
340
341 // Number each basic block so that we can consistently refer to them
342 // in PC-relative references.
343 NumberForBB.clear();
344 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
345 I != E; ++I) {
346 NumberForBB[I->getBasicBlock()] = BBNumber++;
347 }
348
349 // Print out code for the function.
350 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
351 I != E; ++I) {
352 // Print a label for the basic block.
Brian Gaeke09c13092004-06-17 19:39:23 +0000353 O << ".LBB" << Mang->getValueName(MF.getFunction ())
354 << "_" << I->getNumber () << ":\t! "
355 << I->getBasicBlock ()->getName () << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000356 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman27177f82005-04-22 18:06:01 +0000357 II != E; ++II) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000358 // Print the assembly for the instruction.
Chris Lattner0d8fcd32005-12-17 06:54:41 +0000359 O << "\t";
360 printInstruction(II);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000361 }
362 }
363
364 // We didn't modify anything.
365 return false;
366}
367
Chris Lattner994b7352005-12-16 06:34:17 +0000368void SparcV8AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000369 const MachineOperand &MO = MI->getOperand (opNum);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000370 const MRegisterInfo &RI = *TM.getRegisterInfo();
Brian Gaeke446ae112004-06-15 19:52:59 +0000371 bool CloseParen = false;
372 if (MI->getOpcode() == V8::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
373 O << "%hi(";
374 CloseParen = true;
Misha Brukmanf54ef972004-06-24 23:38:20 +0000375 } else if (MI->getOpcode() ==V8::ORri &&!MO.isRegister() &&!MO.isImmediate())
376 {
Brian Gaeke446ae112004-06-15 19:52:59 +0000377 O << "%lo(";
378 CloseParen = true;
379 }
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000380 switch (MO.getType()) {
381 case MachineOperand::MO_VirtualRegister:
382 if (Value *V = MO.getVRegValueOrNull()) {
383 O << "<" << V->getName() << ">";
Brian Gaeke446ae112004-06-15 19:52:59 +0000384 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000385 }
386 // FALLTHROUGH
387 case MachineOperand::MO_MachineRegister:
388 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaekea8b00ca2004-03-06 05:30:21 +0000389 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000390 else
391 O << "%reg" << MO.getReg();
Brian Gaeke446ae112004-06-15 19:52:59 +0000392 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000393
394 case MachineOperand::MO_SignExtendedImmed:
395 case MachineOperand::MO_UnextendedImmed:
396 O << (int)MO.getImmedValue();
Brian Gaeke446ae112004-06-15 19:52:59 +0000397 break;
Brian Gaeke09c13092004-06-17 19:39:23 +0000398 case MachineOperand::MO_MachineBasicBlock: {
399 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
400 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
401 << "_" << MBBOp->getNumber () << "\t! "
402 << MBBOp->getBasicBlock ()->getName ();
403 return;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000404 }
Brian Gaeke09c13092004-06-17 19:39:23 +0000405 case MachineOperand::MO_PCRelativeDisp:
406 std::cerr << "Shouldn't use addPCDisp() when building SparcV8 MachineInstrs";
407 abort ();
408 return;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000409 case MachineOperand::MO_GlobalAddress:
410 O << Mang->getValueName(MO.getGlobal());
Brian Gaeke446ae112004-06-15 19:52:59 +0000411 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000412 case MachineOperand::MO_ExternalSymbol:
413 O << MO.getSymbolName();
Brian Gaeke446ae112004-06-15 19:52:59 +0000414 break;
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000415 case MachineOperand::MO_ConstantPoolIndex:
416 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
417 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000418 default:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000419 O << "<unknown operand type>"; abort (); break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000420 }
Brian Gaeke446ae112004-06-15 19:52:59 +0000421 if (CloseParen) O << ")";
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000422}
423
Chris Lattner994b7352005-12-16 06:34:17 +0000424bool SparcV8AsmPrinter::doInitialization(Module &M) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000425 Mang = new Mangler(M);
426 return false; // success
427}
428
429// SwitchSection - Switch to the specified section of the executable if we are
430// not already in it!
431//
432static void SwitchSection(std::ostream &OS, std::string &CurSection,
433 const char *NewSection) {
434 if (CurSection != NewSection) {
435 CurSection = NewSection;
436 if (!CurSection.empty())
Brian Gaekeb27df442004-09-29 03:25:40 +0000437 OS << "\t.section \"" << NewSection << "\"\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000438 }
439}
440
Chris Lattner994b7352005-12-16 06:34:17 +0000441bool SparcV8AsmPrinter::doFinalization(Module &M) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000442 const TargetData &TD = TM.getTargetData();
443 std::string CurSection;
444
445 // Print out module-level global variables here.
Chris Lattnere4d5c442005-03-15 04:54:21 +0000446 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Brian Gaeke4acfd032004-03-04 06:00:41 +0000447 if (I->hasInitializer()) { // External global require no code
448 O << "\n\n";
449 std::string name = Mang->getValueName(I);
450 Constant *C = I->getInitializer();
451 unsigned Size = TD.getTypeSize(C->getType());
452 unsigned Align = TD.getTypeAlignment(C->getType());
453
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000454 if (C->isNullValue() &&
Brian Gaeke4acfd032004-03-04 06:00:41 +0000455 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
456 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
457 SwitchSection(O, CurSection, ".data");
458 if (I->hasInternalLinkage())
459 O << "\t.local " << name << "\n";
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000460
Brian Gaeke4acfd032004-03-04 06:00:41 +0000461 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
462 << "," << (unsigned)TD.getTypeAlignment(C->getType());
Brian Gaeke79db7402004-03-16 22:37:12 +0000463 O << "\t\t! ";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000464 WriteAsOperand(O, I, true, true, &M);
465 O << "\n";
466 } else {
467 switch (I->getLinkage()) {
468 case GlobalValue::LinkOnceLinkage:
469 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
470 // Nonnull linkonce -> weak
471 O << "\t.weak " << name << "\n";
472 SwitchSection(O, CurSection, "");
Brian Gaekeb27df442004-09-29 03:25:40 +0000473 O << "\t.section\t\".llvm.linkonce.d." << name << "\",\"aw\",@progbits\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000474 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000475
Brian Gaeke4acfd032004-03-04 06:00:41 +0000476 case GlobalValue::AppendingLinkage:
477 // FIXME: appending linkage variables should go into a section of
478 // their name or something. For now, just emit them as external.
479 case GlobalValue::ExternalLinkage:
480 // If external or appending, declare as a global symbol
481 O << "\t.globl " << name << "\n";
482 // FALL THROUGH
483 case GlobalValue::InternalLinkage:
484 if (C->isNullValue())
485 SwitchSection(O, CurSection, ".bss");
486 else
487 SwitchSection(O, CurSection, ".data");
488 break;
Misha Brukmanc11c44f2004-11-19 21:49:19 +0000489 case GlobalValue::GhostLinkage:
490 std::cerr << "Should not have any unmaterialized functions!\n";
491 abort();
Brian Gaeke4acfd032004-03-04 06:00:41 +0000492 }
493
494 O << "\t.align " << Align << "\n";
Brian Gaeke54cc3c22004-03-16 22:52:04 +0000495 O << "\t.type " << name << ",#object\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000496 O << "\t.size " << name << "," << Size << "\n";
Brian Gaeke79db7402004-03-16 22:37:12 +0000497 O << name << ":\t\t\t\t! ";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000498 WriteAsOperand(O, I, true, true, &M);
499 O << " = ";
500 WriteAsOperand(O, C, false, false, &M);
501 O << "\n";
502 emitGlobalConstant(C);
503 }
504 }
505
506 delete Mang;
507 return false; // success
508}