blob: 0e9802d5c1fc58db146f26455018216dd7b7c4a7 [file] [log] [blame]
Chris Lattner6a8e0f52004-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
14#include "llvm/CodeGen/AsmPrinter.h"
15#include "llvm/Constants.h"
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000016#include "llvm/Module.h"
Chris Lattnerf2991ce2005-11-21 08:25:09 +000017#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000018#include "llvm/Support/Mangler.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000019#include "llvm/Support/MathExtras.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000020#include "llvm/Target/TargetMachine.h"
21using namespace llvm;
22
Chris Lattner2ea5c992005-11-21 07:06:27 +000023/// SwitchSection - Switch to the specified section of the executable if we
24/// are not already in it!
25///
26void AsmPrinter::SwitchSection(const char *NewSection, const GlobalValue *GV) {
27 std::string NS;
28
29 if (GV && GV->hasSection())
Chris Lattnerf2991ce2005-11-21 08:25:09 +000030 NS = SwitchToSectionDirective + GV->getSection();
Chris Lattner2ea5c992005-11-21 07:06:27 +000031 else
32 NS = NewSection;
33
34 if (CurrentSection != NS) {
35 CurrentSection = NS;
36 if (!CurrentSection.empty())
37 O << "\t" << CurrentSection << "\n";
38 }
39}
40
Chris Lattner6a8e0f52004-08-16 23:15:22 +000041bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner6d42cbd2004-08-17 06:06:19 +000042 Mang = new Mangler(M, GlobalPrefix);
Chris Lattner2ea5c992005-11-21 07:06:27 +000043 SwitchSection("", 0); // Reset back to no section.
Chris Lattner6a8e0f52004-08-16 23:15:22 +000044 return false;
45}
46
47bool AsmPrinter::doFinalization(Module &M) {
48 delete Mang; Mang = 0;
49 return false;
50}
51
Chris Lattnerbb644e32005-11-21 07:51:36 +000052void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +000053 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000054 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +000055 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +000056}
57
Chris Lattnerf2991ce2005-11-21 08:25:09 +000058/// EmitConstantPool - Print to the current output stream assembly
59/// representations of the constants in the constant pool MCP. This is
60/// used to print out constants which have been "spilled to memory" by
61/// the code generator.
62///
63void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
64 const std::vector<Constant*> &CP = MCP->getConstants();
65 if (CP.empty()) return;
66 const TargetData &TD = TM.getTargetData();
67
68 SwitchSection(ConstantPoolSection, 0);
69 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
70 // FIXME: force doubles to be naturally aligned. We should handle this
71 // more correctly in the future.
72 unsigned Alignment = TD.getTypeAlignmentShift(CP[i]->getType());
73 if (CP[i]->getType() == Type::DoubleTy && Alignment < 3) Alignment = 3;
74
75 EmitAlignment(Alignment);
76 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
77 << ":\t\t\t\t\t" << CommentString << *CP[i] << '\n';
78 EmitGlobalConstant(CP[i]);
79 }
80}
81
82
83
Chris Lattnerbb644e32005-11-21 07:51:36 +000084// EmitAlignment - Emit an alignment directive to the specified power of two.
85void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +000086 if (GV && GV->getAlignment())
87 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +000088 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner1d35c162004-08-17 19:14:29 +000089 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
90 O << AlignDirective << NumBits << "\n";
91}
92
Chris Lattnerbb644e32005-11-21 07:51:36 +000093/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +000094///
Chris Lattnerbb644e32005-11-21 07:51:36 +000095void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +000096 if (NumZeros) {
97 if (ZeroDirective)
98 O << ZeroDirective << NumZeros << "\n";
99 else {
Chris Lattnerea751992004-08-17 21:38:40 +0000100 for (; NumZeros; --NumZeros)
101 O << Data8bitsDirective << "0\n";
102 }
103 }
104}
105
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000106// Print out the specified constant, without a storage class. Only the
107// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000108void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000109 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000110 O << "0";
111 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
112 assert(CB == ConstantBool::True);
113 O << "1";
114 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
115 if (((CI->getValue() << 32) >> 32) == CI->getValue())
116 O << CI->getValue();
117 else
Duraid Madina73c4dba2005-05-15 13:05:48 +0000118 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000119 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
120 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000121 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000122 // This is a constant address for a global variable or function. Use the
123 // name of the variable or function as the address value, possibly
124 // decorating it with GlobalVarAddrPrefix/Suffix or
125 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000126 if (isa<Function>(GV))
127 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000128 else
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000129 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000130 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000131 const TargetData &TD = TM.getTargetData();
132 switch(CE->getOpcode()) {
133 case Instruction::GetElementPtr: {
134 // generate a symbolic expression for the byte address
135 const Constant *ptrVal = CE->getOperand(0);
136 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner145569b2005-02-14 21:40:26 +0000137 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
138 if (Offset)
139 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000140 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000141 if (Offset > 0)
142 O << ") + " << Offset;
143 else if (Offset < 0)
144 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000145 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000146 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000147 }
148 break;
149 }
150 case Instruction::Cast: {
151 // Support only non-converting or widening casts for now, that is, ones
152 // that do not involve a change in value. This assertion is really gross,
153 // and may not even be a complete check.
154 Constant *Op = CE->getOperand(0);
155 const Type *OpTy = Op->getType(), *Ty = CE->getType();
156
157 // Remember, kids, pointers can be losslessly converted back and forth
158 // into 32-bit or wider integers, regardless of signedness. :-P
159 assert(((isa<PointerType>(OpTy)
160 && (Ty == Type::LongTy || Ty == Type::ULongTy
161 || Ty == Type::IntTy || Ty == Type::UIntTy))
162 || (isa<PointerType>(Ty)
163 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
164 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
165 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
166 && OpTy->isLosslesslyConvertibleTo(Ty))))
167 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000168 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000169 break;
170 }
171 case Instruction::Add:
172 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000173 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000174 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000175 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000176 O << ")";
177 break;
178 default:
179 assert(0 && "Unsupported operator!");
180 }
181 } else {
182 assert(0 && "Unknown constant value!");
183 }
184}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000185
186/// toOctal - Convert the low order bits of X into an octal digit.
187///
188static inline char toOctal(int X) {
189 return (X&7)+'0';
190}
191
Chris Lattner55a6d902005-11-10 18:06:33 +0000192/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000193/// the predicate isString is true.
194///
Chris Lattner55a6d902005-11-10 18:06:33 +0000195static void printAsCString(std::ostream &O, const ConstantArray *CVA,
196 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000197 assert(CVA->isString() && "Array is not string compatible!");
198
199 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000200 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000201 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000202 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000203
204 if (C == '"') {
205 O << "\\\"";
206 } else if (C == '\\') {
207 O << "\\\\";
208 } else if (isprint(C)) {
209 O << C;
210 } else {
211 switch(C) {
212 case '\b': O << "\\b"; break;
213 case '\f': O << "\\f"; break;
214 case '\n': O << "\\n"; break;
215 case '\r': O << "\\r"; break;
216 case '\t': O << "\\t"; break;
217 default:
218 O << '\\';
219 O << toOctal(C >> 6);
220 O << toOctal(C >> 3);
221 O << toOctal(C >> 0);
222 break;
223 }
224 }
225 }
226 O << "\"";
227}
228
Chris Lattnerbb644e32005-11-21 07:51:36 +0000229/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000230///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000231void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000232 const TargetData &TD = TM.getTargetData();
233
Chris Lattner61753bf2004-10-16 18:19:26 +0000234 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000235 EmitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000236 return;
237 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
238 if (CVA->isString()) {
Chris Lattner55a6d902005-11-10 18:06:33 +0000239 unsigned NumElts = CVA->getNumOperands();
240 if (AscizDirective && NumElts &&
241 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
242 O << AscizDirective;
243 printAsCString(O, CVA, NumElts-1);
244 } else {
245 O << AsciiDirective;
246 printAsCString(O, CVA, NumElts);
247 }
Chris Lattner8452a1f2004-08-17 06:36:49 +0000248 O << "\n";
249 } else { // Not a string. Print the values in successive locations
250 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000251 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000252 }
253 return;
254 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
255 // Print the fields in successive locations. Pad to align if needed!
256 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000257 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000258 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
259 const Constant* field = CVS->getOperand(i);
260
261 // Check if padding is needed and insert one or more 0s.
Chris Lattner0b955fd2005-01-08 19:59:10 +0000262 uint64_t fieldSize = TD.getTypeSize(field->getType());
263 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000264 : cvsLayout->MemberOffsets[i+1])
265 - cvsLayout->MemberOffsets[i]) - fieldSize;
266 sizeSoFar += fieldSize + padSize;
267
268 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000269 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000270
271 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000272 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000273 }
274 assert(sizeSoFar == cvsLayout->StructSize &&
275 "Layout of constant struct may be incorrect!");
276 return;
277 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
278 // FP Constants are printed as integer constants to avoid losing
279 // precision...
280 double Val = CFP->getValue();
281 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000282 if (Data64bitsDirective)
Jim Laskeyb74c6662005-08-17 19:34:49 +0000283 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattnerea751992004-08-17 21:38:40 +0000284 << " double value: " << Val << "\n";
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000285 else if (TD.isBigEndian()) {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000286 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000287 << "\t" << CommentString << " double most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000288 << Val << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000289 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000290 << "\t" << CommentString << " double least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000291 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000292 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000293 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000294 << "\t" << CommentString << " double least significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000295 << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000296 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000297 << "\t" << CommentString << " double most significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000298 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000299 }
300 return;
301 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000302 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattnerda6beac2004-08-17 16:27:05 +0000303 << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000304 return;
305 }
306 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
307 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
308 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000309
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000310 if (Data64bitsDirective)
311 O << Data64bitsDirective << Val << "\n";
312 else if (TD.isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000313 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000314 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000315 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000316 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000317 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000318 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000319 } else {
320 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000321 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000322 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000323 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000324 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000325 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000326 }
327 return;
328 }
329 }
330
331 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000332 switch (type->getTypeID()) {
Misha Brukman835702a2005-04-21 22:36:52 +0000333 case Type::BoolTyID:
Chris Lattner8452a1f2004-08-17 06:36:49 +0000334 case Type::UByteTyID: case Type::SByteTyID:
335 O << Data8bitsDirective;
336 break;
337 case Type::UShortTyID: case Type::ShortTyID:
338 O << Data16bitsDirective;
339 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000340 case Type::PointerTyID:
Andrew Lenharthc8770aa2005-02-04 13:47:16 +0000341 if (TD.getPointerSize() == 8) {
342 O << Data64bitsDirective;
343 break;
344 }
345 //Fall through for pointer size == int size
Chris Lattner8452a1f2004-08-17 06:36:49 +0000346 case Type::UIntTyID: case Type::IntTyID:
347 O << Data32bitsDirective;
348 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000349 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner88e2d2e2005-08-08 04:26:32 +0000350 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
351 O << Data64bitsDirective;
352 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000353 case Type::FloatTyID: case Type::DoubleTyID:
354 assert (0 && "Should have already output floating point constant.");
355 default:
356 assert (0 && "Can't handle printing this type of thing");
357 break;
358 }
Chris Lattnerbb644e32005-11-21 07:51:36 +0000359 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000360 O << "\n";
361}