blob: 86c3f4bb959e190d0e338bca0e7a7109bd72a2c3 [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 Lattner6a8e0f52004-08-16 23:15:22 +000017#include "llvm/Support/Mangler.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000018#include "llvm/Support/MathExtras.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000019#include "llvm/Target/TargetMachine.h"
20using namespace llvm;
21
Chris Lattner2ea5c992005-11-21 07:06:27 +000022/// SwitchSection - Switch to the specified section of the executable if we
23/// are not already in it!
24///
25void AsmPrinter::SwitchSection(const char *NewSection, const GlobalValue *GV) {
26 std::string NS;
27
28 if (GV && GV->hasSection())
29 NS = ".section " + GV->getSection();
30 else
31 NS = NewSection;
32
33 if (CurrentSection != NS) {
34 CurrentSection = NS;
35 if (!CurrentSection.empty())
36 O << "\t" << CurrentSection << "\n";
37 }
38}
39
Chris Lattner6a8e0f52004-08-16 23:15:22 +000040bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner6d42cbd2004-08-17 06:06:19 +000041 Mang = new Mangler(M, GlobalPrefix);
Chris Lattner2ea5c992005-11-21 07:06:27 +000042 SwitchSection("", 0); // Reset back to no section.
Chris Lattner6a8e0f52004-08-16 23:15:22 +000043 return false;
44}
45
46bool AsmPrinter::doFinalization(Module &M) {
47 delete Mang; Mang = 0;
48 return false;
49}
50
Chris Lattnerbb644e32005-11-21 07:51:36 +000051void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +000052 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000053 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +000054 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +000055}
56
Chris Lattnerbb644e32005-11-21 07:51:36 +000057// EmitAlignment - Emit an alignment directive to the specified power of two.
58void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +000059 if (GV && GV->getAlignment())
60 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +000061 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner1d35c162004-08-17 19:14:29 +000062 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
63 O << AlignDirective << NumBits << "\n";
64}
65
Chris Lattnerbb644e32005-11-21 07:51:36 +000066/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +000067///
Chris Lattnerbb644e32005-11-21 07:51:36 +000068void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +000069 if (NumZeros) {
70 if (ZeroDirective)
71 O << ZeroDirective << NumZeros << "\n";
72 else {
Chris Lattnerea751992004-08-17 21:38:40 +000073 for (; NumZeros; --NumZeros)
74 O << Data8bitsDirective << "0\n";
75 }
76 }
77}
78
Chris Lattner6a8e0f52004-08-16 23:15:22 +000079// Print out the specified constant, without a storage class. Only the
80// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +000081void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +000082 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +000083 O << "0";
84 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
85 assert(CB == ConstantBool::True);
86 O << "1";
87 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
88 if (((CI->getValue() << 32) >> 32) == CI->getValue())
89 O << CI->getValue();
90 else
Duraid Madina73c4dba2005-05-15 13:05:48 +000091 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +000092 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
93 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000094 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +000095 // This is a constant address for a global variable or function. Use the
96 // name of the variable or function as the address value, possibly
97 // decorating it with GlobalVarAddrPrefix/Suffix or
98 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000099 if (isa<Function>(GV))
100 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000101 else
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000102 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000103 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000104 const TargetData &TD = TM.getTargetData();
105 switch(CE->getOpcode()) {
106 case Instruction::GetElementPtr: {
107 // generate a symbolic expression for the byte address
108 const Constant *ptrVal = CE->getOperand(0);
109 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner145569b2005-02-14 21:40:26 +0000110 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
111 if (Offset)
112 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000113 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000114 if (Offset > 0)
115 O << ") + " << Offset;
116 else if (Offset < 0)
117 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000118 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000119 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000120 }
121 break;
122 }
123 case Instruction::Cast: {
124 // Support only non-converting or widening casts for now, that is, ones
125 // that do not involve a change in value. This assertion is really gross,
126 // and may not even be a complete check.
127 Constant *Op = CE->getOperand(0);
128 const Type *OpTy = Op->getType(), *Ty = CE->getType();
129
130 // Remember, kids, pointers can be losslessly converted back and forth
131 // into 32-bit or wider integers, regardless of signedness. :-P
132 assert(((isa<PointerType>(OpTy)
133 && (Ty == Type::LongTy || Ty == Type::ULongTy
134 || Ty == Type::IntTy || Ty == Type::UIntTy))
135 || (isa<PointerType>(Ty)
136 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
137 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
138 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
139 && OpTy->isLosslesslyConvertibleTo(Ty))))
140 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000141 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000142 break;
143 }
144 case Instruction::Add:
145 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000146 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000147 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000148 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000149 O << ")";
150 break;
151 default:
152 assert(0 && "Unsupported operator!");
153 }
154 } else {
155 assert(0 && "Unknown constant value!");
156 }
157}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000158
159/// toOctal - Convert the low order bits of X into an octal digit.
160///
161static inline char toOctal(int X) {
162 return (X&7)+'0';
163}
164
Chris Lattner55a6d902005-11-10 18:06:33 +0000165/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000166/// the predicate isString is true.
167///
Chris Lattner55a6d902005-11-10 18:06:33 +0000168static void printAsCString(std::ostream &O, const ConstantArray *CVA,
169 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000170 assert(CVA->isString() && "Array is not string compatible!");
171
172 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000173 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000174 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000175 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000176
177 if (C == '"') {
178 O << "\\\"";
179 } else if (C == '\\') {
180 O << "\\\\";
181 } else if (isprint(C)) {
182 O << C;
183 } else {
184 switch(C) {
185 case '\b': O << "\\b"; break;
186 case '\f': O << "\\f"; break;
187 case '\n': O << "\\n"; break;
188 case '\r': O << "\\r"; break;
189 case '\t': O << "\\t"; break;
190 default:
191 O << '\\';
192 O << toOctal(C >> 6);
193 O << toOctal(C >> 3);
194 O << toOctal(C >> 0);
195 break;
196 }
197 }
198 }
199 O << "\"";
200}
201
Chris Lattnerbb644e32005-11-21 07:51:36 +0000202/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000203///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000204void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000205 const TargetData &TD = TM.getTargetData();
206
Chris Lattner61753bf2004-10-16 18:19:26 +0000207 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000208 EmitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000209 return;
210 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
211 if (CVA->isString()) {
Chris Lattner55a6d902005-11-10 18:06:33 +0000212 unsigned NumElts = CVA->getNumOperands();
213 if (AscizDirective && NumElts &&
214 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
215 O << AscizDirective;
216 printAsCString(O, CVA, NumElts-1);
217 } else {
218 O << AsciiDirective;
219 printAsCString(O, CVA, NumElts);
220 }
Chris Lattner8452a1f2004-08-17 06:36:49 +0000221 O << "\n";
222 } else { // Not a string. Print the values in successive locations
223 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000224 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000225 }
226 return;
227 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
228 // Print the fields in successive locations. Pad to align if needed!
229 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000230 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000231 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
232 const Constant* field = CVS->getOperand(i);
233
234 // Check if padding is needed and insert one or more 0s.
Chris Lattner0b955fd2005-01-08 19:59:10 +0000235 uint64_t fieldSize = TD.getTypeSize(field->getType());
236 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000237 : cvsLayout->MemberOffsets[i+1])
238 - cvsLayout->MemberOffsets[i]) - fieldSize;
239 sizeSoFar += fieldSize + padSize;
240
241 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000242 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000243
244 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000245 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000246 }
247 assert(sizeSoFar == cvsLayout->StructSize &&
248 "Layout of constant struct may be incorrect!");
249 return;
250 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
251 // FP Constants are printed as integer constants to avoid losing
252 // precision...
253 double Val = CFP->getValue();
254 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000255 if (Data64bitsDirective)
Jim Laskeyb74c6662005-08-17 19:34:49 +0000256 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattnerea751992004-08-17 21:38:40 +0000257 << " double value: " << Val << "\n";
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000258 else if (TD.isBigEndian()) {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000259 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000260 << "\t" << CommentString << " double most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000261 << Val << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000262 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000263 << "\t" << CommentString << " double least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000264 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000265 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000266 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000267 << "\t" << CommentString << " double least significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000268 << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000269 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000270 << "\t" << CommentString << " double most significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000271 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000272 }
273 return;
274 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000275 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattnerda6beac2004-08-17 16:27:05 +0000276 << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000277 return;
278 }
279 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
280 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
281 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000282
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000283 if (Data64bitsDirective)
284 O << Data64bitsDirective << Val << "\n";
285 else if (TD.isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000286 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000287 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000288 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000289 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000290 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000291 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000292 } else {
293 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000294 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000295 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000296 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000297 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000298 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000299 }
300 return;
301 }
302 }
303
304 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000305 switch (type->getTypeID()) {
Misha Brukman835702a2005-04-21 22:36:52 +0000306 case Type::BoolTyID:
Chris Lattner8452a1f2004-08-17 06:36:49 +0000307 case Type::UByteTyID: case Type::SByteTyID:
308 O << Data8bitsDirective;
309 break;
310 case Type::UShortTyID: case Type::ShortTyID:
311 O << Data16bitsDirective;
312 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000313 case Type::PointerTyID:
Andrew Lenharthc8770aa2005-02-04 13:47:16 +0000314 if (TD.getPointerSize() == 8) {
315 O << Data64bitsDirective;
316 break;
317 }
318 //Fall through for pointer size == int size
Chris Lattner8452a1f2004-08-17 06:36:49 +0000319 case Type::UIntTyID: case Type::IntTyID:
320 O << Data32bitsDirective;
321 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000322 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner88e2d2e2005-08-08 04:26:32 +0000323 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
324 O << Data64bitsDirective;
325 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000326 case Type::FloatTyID: case Type::DoubleTyID:
327 assert (0 && "Should have already output floating point constant.");
328 default:
329 assert (0 && "Can't handle printing this type of thing");
330 break;
331 }
Chris Lattnerbb644e32005-11-21 07:51:36 +0000332 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000333 O << "\n";
334}