blob: dd100c6e19c150db7a8a4808e7458e0690e86643 [file] [log] [blame]
Chris Lattnera80ba712004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Nate Begeman8cfa57b2005-12-06 06:18:55 +000014#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000015#include "llvm/CodeGen/AsmPrinter.h"
16#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000017#include "llvm/Module.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000018#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000019#include "llvm/Support/Mangler.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000020#include "llvm/Support/MathExtras.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000021#include "llvm/Target/TargetMachine.h"
22using namespace llvm;
23
Chris Lattnerac28fbd2005-11-21 07:06:27 +000024/// SwitchSection - Switch to the specified section of the executable if we
25/// are not already in it!
26///
27void AsmPrinter::SwitchSection(const char *NewSection, const GlobalValue *GV) {
28 std::string NS;
29
30 if (GV && GV->hasSection())
Chris Lattner3b4fd322005-11-21 08:25:09 +000031 NS = SwitchToSectionDirective + GV->getSection();
Chris Lattnerac28fbd2005-11-21 07:06:27 +000032 else
Chris Lattner42a80fe2005-12-09 19:28:49 +000033 NS = std::string("\t")+NewSection;
Chris Lattnerac28fbd2005-11-21 07:06:27 +000034
35 if (CurrentSection != NS) {
36 CurrentSection = NS;
37 if (!CurrentSection.empty())
Chris Lattner42a80fe2005-12-09 19:28:49 +000038 O << CurrentSection << '\n';
Chris Lattnerac28fbd2005-11-21 07:06:27 +000039 }
40}
41
Chris Lattnera80ba712004-08-16 23:15:22 +000042bool AsmPrinter::doInitialization(Module &M) {
Chris Lattneraf2bf0a2004-08-17 06:06:19 +000043 Mang = new Mangler(M, GlobalPrefix);
Chris Lattnerac28fbd2005-11-21 07:06:27 +000044 SwitchSection("", 0); // Reset back to no section.
Chris Lattnera80ba712004-08-16 23:15:22 +000045 return false;
46}
47
48bool AsmPrinter::doFinalization(Module &M) {
49 delete Mang; Mang = 0;
50 return false;
51}
52
Chris Lattner25045bd2005-11-21 07:51:36 +000053void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +000054 // What's my mangled name?
Chris Lattner450de392005-11-10 18:36:17 +000055 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner77bc2282005-11-21 08:13:27 +000056 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +000057}
58
Chris Lattner3b4fd322005-11-21 08:25:09 +000059/// EmitConstantPool - Print to the current output stream assembly
60/// representations of the constants in the constant pool MCP. This is
61/// used to print out constants which have been "spilled to memory" by
62/// the code generator.
63///
64void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
65 const std::vector<Constant*> &CP = MCP->getConstants();
66 if (CP.empty()) return;
67 const TargetData &TD = TM.getTargetData();
68
69 SwitchSection(ConstantPoolSection, 0);
70 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
71 // FIXME: force doubles to be naturally aligned. We should handle this
72 // more correctly in the future.
73 unsigned Alignment = TD.getTypeAlignmentShift(CP[i]->getType());
74 if (CP[i]->getType() == Type::DoubleTy && Alignment < 3) Alignment = 3;
75
76 EmitAlignment(Alignment);
77 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
78 << ":\t\t\t\t\t" << CommentString << *CP[i] << '\n';
79 EmitGlobalConstant(CP[i]);
80 }
81}
82
83
84
Chris Lattner25045bd2005-11-21 07:51:36 +000085// EmitAlignment - Emit an alignment directive to the specified power of two.
86void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnera1ab72d2005-11-14 19:00:06 +000087 if (GV && GV->getAlignment())
88 NumBits = Log2_32(GV->getAlignment());
Chris Lattner2a21c6e2005-11-10 18:09:27 +000089 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattnerbfddc202004-08-17 19:14:29 +000090 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
91 O << AlignDirective << NumBits << "\n";
92}
93
Chris Lattner25045bd2005-11-21 07:51:36 +000094/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +000095///
Chris Lattner25045bd2005-11-21 07:51:36 +000096void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +000097 if (NumZeros) {
98 if (ZeroDirective)
99 O << ZeroDirective << NumZeros << "\n";
100 else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000101 for (; NumZeros; --NumZeros)
102 O << Data8bitsDirective << "0\n";
103 }
104 }
105}
106
Chris Lattnera80ba712004-08-16 23:15:22 +0000107// Print out the specified constant, without a storage class. Only the
108// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000109void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000110 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000111 O << "0";
112 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
113 assert(CB == ConstantBool::True);
114 O << "1";
115 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
116 if (((CI->getValue() << 32) >> 32) == CI->getValue())
117 O << CI->getValue();
118 else
Duraid Madina45606572005-05-15 13:05:48 +0000119 O << (uint64_t)CI->getValue();
Chris Lattnera80ba712004-08-16 23:15:22 +0000120 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
121 O << CI->getValue();
Chris Lattner450de392005-11-10 18:36:17 +0000122 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000123 // This is a constant address for a global variable or function. Use the
124 // name of the variable or function as the address value, possibly
125 // decorating it with GlobalVarAddrPrefix/Suffix or
126 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattner450de392005-11-10 18:36:17 +0000127 if (isa<Function>(GV))
128 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina855a5192005-04-02 12:21:51 +0000129 else
Chris Lattner450de392005-11-10 18:36:17 +0000130 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina855a5192005-04-02 12:21:51 +0000131 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000132 const TargetData &TD = TM.getTargetData();
133 switch(CE->getOpcode()) {
134 case Instruction::GetElementPtr: {
135 // generate a symbolic expression for the byte address
136 const Constant *ptrVal = CE->getOperand(0);
137 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner27e19212005-02-14 21:40:26 +0000138 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
139 if (Offset)
140 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000141 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000142 if (Offset > 0)
143 O << ") + " << Offset;
144 else if (Offset < 0)
145 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000146 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000147 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000148 }
149 break;
150 }
151 case Instruction::Cast: {
152 // Support only non-converting or widening casts for now, that is, ones
153 // that do not involve a change in value. This assertion is really gross,
154 // and may not even be a complete check.
155 Constant *Op = CE->getOperand(0);
156 const Type *OpTy = Op->getType(), *Ty = CE->getType();
157
158 // Remember, kids, pointers can be losslessly converted back and forth
159 // into 32-bit or wider integers, regardless of signedness. :-P
160 assert(((isa<PointerType>(OpTy)
161 && (Ty == Type::LongTy || Ty == Type::ULongTy
162 || Ty == Type::IntTy || Ty == Type::UIntTy))
163 || (isa<PointerType>(Ty)
164 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
165 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
166 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
167 && OpTy->isLosslesslyConvertibleTo(Ty))))
168 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000169 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000170 break;
171 }
172 case Instruction::Add:
173 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000174 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattnera80ba712004-08-16 23:15:22 +0000175 O << ") + (";
Chris Lattner25045bd2005-11-21 07:51:36 +0000176 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000177 O << ")";
178 break;
179 default:
180 assert(0 && "Unsupported operator!");
181 }
182 } else {
183 assert(0 && "Unknown constant value!");
184 }
185}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000186
187/// toOctal - Convert the low order bits of X into an octal digit.
188///
189static inline char toOctal(int X) {
190 return (X&7)+'0';
191}
192
Chris Lattner2980cef2005-11-10 18:06:33 +0000193/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000194/// the predicate isString is true.
195///
Chris Lattner2980cef2005-11-10 18:06:33 +0000196static void printAsCString(std::ostream &O, const ConstantArray *CVA,
197 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000198 assert(CVA->isString() && "Array is not string compatible!");
199
200 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000201 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000202 unsigned char C =
Chris Lattnerdea18b62005-01-08 19:59:10 +0000203 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000204
205 if (C == '"') {
206 O << "\\\"";
207 } else if (C == '\\') {
208 O << "\\\\";
209 } else if (isprint(C)) {
210 O << C;
211 } else {
212 switch(C) {
213 case '\b': O << "\\b"; break;
214 case '\f': O << "\\f"; break;
215 case '\n': O << "\\n"; break;
216 case '\r': O << "\\r"; break;
217 case '\t': O << "\\t"; break;
218 default:
219 O << '\\';
220 O << toOctal(C >> 6);
221 O << toOctal(C >> 3);
222 O << toOctal(C >> 0);
223 break;
224 }
225 }
226 }
227 O << "\"";
228}
229
Chris Lattner25045bd2005-11-21 07:51:36 +0000230/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner1b7e2352004-08-17 06:36:49 +0000231///
Chris Lattner25045bd2005-11-21 07:51:36 +0000232void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000233 const TargetData &TD = TM.getTargetData();
234
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000235 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattner25045bd2005-11-21 07:51:36 +0000236 EmitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000237 return;
238 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
239 if (CVA->isString()) {
Chris Lattner2980cef2005-11-10 18:06:33 +0000240 unsigned NumElts = CVA->getNumOperands();
241 if (AscizDirective && NumElts &&
242 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
243 O << AscizDirective;
244 printAsCString(O, CVA, NumElts-1);
245 } else {
246 O << AsciiDirective;
247 printAsCString(O, CVA, NumElts);
248 }
Chris Lattner1b7e2352004-08-17 06:36:49 +0000249 O << "\n";
250 } else { // Not a string. Print the values in successive locations
251 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner25045bd2005-11-21 07:51:36 +0000252 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000253 }
254 return;
255 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
256 // Print the fields in successive locations. Pad to align if needed!
257 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000258 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000259 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
260 const Constant* field = CVS->getOperand(i);
261
262 // Check if padding is needed and insert one or more 0s.
Chris Lattnerdea18b62005-01-08 19:59:10 +0000263 uint64_t fieldSize = TD.getTypeSize(field->getType());
264 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner1b7e2352004-08-17 06:36:49 +0000265 : cvsLayout->MemberOffsets[i+1])
266 - cvsLayout->MemberOffsets[i]) - fieldSize;
267 sizeSoFar += fieldSize + padSize;
268
269 // Now print the actual field value
Chris Lattner25045bd2005-11-21 07:51:36 +0000270 EmitGlobalConstant(field);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000271
272 // Insert the field padding unless it's zero bytes...
Chris Lattner25045bd2005-11-21 07:51:36 +0000273 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000274 }
275 assert(sizeSoFar == cvsLayout->StructSize &&
276 "Layout of constant struct may be incorrect!");
277 return;
278 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
279 // FP Constants are printed as integer constants to avoid losing
280 // precision...
281 double Val = CFP->getValue();
282 if (CFP->getType() == Type::DoubleTy) {
Chris Lattnere85a5a92004-08-17 06:48:16 +0000283 if (Data64bitsDirective)
Jim Laskeycb6682f2005-08-17 19:34:49 +0000284 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattner7d057a32004-08-17 21:38:40 +0000285 << " double value: " << Val << "\n";
Chris Lattnere85a5a92004-08-17 06:48:16 +0000286 else if (TD.isBigEndian()) {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000287 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000288 << "\t" << CommentString << " double most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000289 << Val << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000290 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000291 << "\t" << CommentString << " double least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000292 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000293 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000294 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000295 << "\t" << CommentString << " double least significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000296 << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000297 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000298 << "\t" << CommentString << " double most significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000299 << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000300 }
301 return;
302 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000303 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattner0554fb62004-08-17 16:27:05 +0000304 << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000305 return;
306 }
307 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
308 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
309 uint64_t Val = CI->getRawValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000310
Chris Lattnere85a5a92004-08-17 06:48:16 +0000311 if (Data64bitsDirective)
312 O << Data64bitsDirective << Val << "\n";
313 else if (TD.isBigEndian()) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000314 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000315 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000316 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000317 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000318 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000319 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000320 } else {
321 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000322 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000323 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000324 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000325 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000326 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000327 }
328 return;
329 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000330 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
331 const PackedType *PTy = CP->getType();
332
333 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
334 EmitGlobalConstant(CP->getOperand(I));
335
336 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000337 }
338
339 const Type *type = CV->getType();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000340 switch (type->getTypeID()) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000341 case Type::BoolTyID:
Chris Lattner1b7e2352004-08-17 06:36:49 +0000342 case Type::UByteTyID: case Type::SByteTyID:
343 O << Data8bitsDirective;
344 break;
345 case Type::UShortTyID: case Type::ShortTyID:
346 O << Data16bitsDirective;
347 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000348 case Type::PointerTyID:
Andrew Lenharth571c9c32005-02-04 13:47:16 +0000349 if (TD.getPointerSize() == 8) {
350 O << Data64bitsDirective;
351 break;
352 }
353 //Fall through for pointer size == int size
Chris Lattner1b7e2352004-08-17 06:36:49 +0000354 case Type::UIntTyID: case Type::IntTyID:
355 O << Data32bitsDirective;
356 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000357 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner660538c2005-08-08 04:26:32 +0000358 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
359 O << Data64bitsDirective;
360 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000361 case Type::FloatTyID: case Type::DoubleTyID:
362 assert (0 && "Should have already output floating point constant.");
363 default:
364 assert (0 && "Can't handle printing this type of thing");
365 break;
366 }
Chris Lattner25045bd2005-11-21 07:51:36 +0000367 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000368 O << "\n";
369}