blob: 3b648a761161222ecb2d7581497559fdeaeef81e [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 Lattner6a8e0f52004-08-16 23:15:22 +000054}
55
Chris Lattnerbb644e32005-11-21 07:51:36 +000056// EmitAlignment - Emit an alignment directive to the specified power of two.
57void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +000058 if (GV && GV->getAlignment())
59 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +000060 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner1d35c162004-08-17 19:14:29 +000061 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
62 O << AlignDirective << NumBits << "\n";
63}
64
Chris Lattnerbb644e32005-11-21 07:51:36 +000065/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +000066///
Chris Lattnerbb644e32005-11-21 07:51:36 +000067void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +000068 if (NumZeros) {
69 if (ZeroDirective)
70 O << ZeroDirective << NumZeros << "\n";
71 else {
Chris Lattnerea751992004-08-17 21:38:40 +000072 for (; NumZeros; --NumZeros)
73 O << Data8bitsDirective << "0\n";
74 }
75 }
76}
77
Chris Lattner6a8e0f52004-08-16 23:15:22 +000078// Print out the specified constant, without a storage class. Only the
79// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +000080void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +000081 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +000082 O << "0";
83 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
84 assert(CB == ConstantBool::True);
85 O << "1";
86 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
87 if (((CI->getValue() << 32) >> 32) == CI->getValue())
88 O << CI->getValue();
89 else
Duraid Madina73c4dba2005-05-15 13:05:48 +000090 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +000091 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
92 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000093 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +000094 // This is a constant address for a global variable or function. Use the
95 // name of the variable or function as the address value, possibly
96 // decorating it with GlobalVarAddrPrefix/Suffix or
97 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000098 if (isa<Function>(GV))
99 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000100 else
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000101 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000102 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000103 const TargetData &TD = TM.getTargetData();
104 switch(CE->getOpcode()) {
105 case Instruction::GetElementPtr: {
106 // generate a symbolic expression for the byte address
107 const Constant *ptrVal = CE->getOperand(0);
108 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner145569b2005-02-14 21:40:26 +0000109 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
110 if (Offset)
111 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000112 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000113 if (Offset > 0)
114 O << ") + " << Offset;
115 else if (Offset < 0)
116 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000117 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000118 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000119 }
120 break;
121 }
122 case Instruction::Cast: {
123 // Support only non-converting or widening casts for now, that is, ones
124 // that do not involve a change in value. This assertion is really gross,
125 // and may not even be a complete check.
126 Constant *Op = CE->getOperand(0);
127 const Type *OpTy = Op->getType(), *Ty = CE->getType();
128
129 // Remember, kids, pointers can be losslessly converted back and forth
130 // into 32-bit or wider integers, regardless of signedness. :-P
131 assert(((isa<PointerType>(OpTy)
132 && (Ty == Type::LongTy || Ty == Type::ULongTy
133 || Ty == Type::IntTy || Ty == Type::UIntTy))
134 || (isa<PointerType>(Ty)
135 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
136 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
137 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
138 && OpTy->isLosslesslyConvertibleTo(Ty))))
139 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000140 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000141 break;
142 }
143 case Instruction::Add:
144 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000145 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000146 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000147 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000148 O << ")";
149 break;
150 default:
151 assert(0 && "Unsupported operator!");
152 }
153 } else {
154 assert(0 && "Unknown constant value!");
155 }
156}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000157
158/// toOctal - Convert the low order bits of X into an octal digit.
159///
160static inline char toOctal(int X) {
161 return (X&7)+'0';
162}
163
Chris Lattner55a6d902005-11-10 18:06:33 +0000164/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000165/// the predicate isString is true.
166///
Chris Lattner55a6d902005-11-10 18:06:33 +0000167static void printAsCString(std::ostream &O, const ConstantArray *CVA,
168 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000169 assert(CVA->isString() && "Array is not string compatible!");
170
171 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000172 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000173 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000174 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000175
176 if (C == '"') {
177 O << "\\\"";
178 } else if (C == '\\') {
179 O << "\\\\";
180 } else if (isprint(C)) {
181 O << C;
182 } else {
183 switch(C) {
184 case '\b': O << "\\b"; break;
185 case '\f': O << "\\f"; break;
186 case '\n': O << "\\n"; break;
187 case '\r': O << "\\r"; break;
188 case '\t': O << "\\t"; break;
189 default:
190 O << '\\';
191 O << toOctal(C >> 6);
192 O << toOctal(C >> 3);
193 O << toOctal(C >> 0);
194 break;
195 }
196 }
197 }
198 O << "\"";
199}
200
Chris Lattnerbb644e32005-11-21 07:51:36 +0000201/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000202///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000203void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000204 const TargetData &TD = TM.getTargetData();
205
Chris Lattner61753bf2004-10-16 18:19:26 +0000206 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000207 EmitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000208 return;
209 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
210 if (CVA->isString()) {
Chris Lattner55a6d902005-11-10 18:06:33 +0000211 unsigned NumElts = CVA->getNumOperands();
212 if (AscizDirective && NumElts &&
213 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
214 O << AscizDirective;
215 printAsCString(O, CVA, NumElts-1);
216 } else {
217 O << AsciiDirective;
218 printAsCString(O, CVA, NumElts);
219 }
Chris Lattner8452a1f2004-08-17 06:36:49 +0000220 O << "\n";
221 } else { // Not a string. Print the values in successive locations
222 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000223 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000224 }
225 return;
226 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
227 // Print the fields in successive locations. Pad to align if needed!
228 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000229 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000230 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
231 const Constant* field = CVS->getOperand(i);
232
233 // Check if padding is needed and insert one or more 0s.
Chris Lattner0b955fd2005-01-08 19:59:10 +0000234 uint64_t fieldSize = TD.getTypeSize(field->getType());
235 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000236 : cvsLayout->MemberOffsets[i+1])
237 - cvsLayout->MemberOffsets[i]) - fieldSize;
238 sizeSoFar += fieldSize + padSize;
239
240 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000241 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000242
243 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000244 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000245 }
246 assert(sizeSoFar == cvsLayout->StructSize &&
247 "Layout of constant struct may be incorrect!");
248 return;
249 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
250 // FP Constants are printed as integer constants to avoid losing
251 // precision...
252 double Val = CFP->getValue();
253 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000254 if (Data64bitsDirective)
Jim Laskeyb74c6662005-08-17 19:34:49 +0000255 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattnerea751992004-08-17 21:38:40 +0000256 << " double value: " << Val << "\n";
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000257 else if (TD.isBigEndian()) {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000258 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000259 << "\t" << CommentString << " double most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000260 << Val << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000261 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000262 << "\t" << CommentString << " double least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000263 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000264 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000265 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000266 << "\t" << CommentString << " double least significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000267 << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000268 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000269 << "\t" << CommentString << " double most significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000270 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000271 }
272 return;
273 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000274 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattnerda6beac2004-08-17 16:27:05 +0000275 << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000276 return;
277 }
278 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
279 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
280 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000281
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000282 if (Data64bitsDirective)
283 O << Data64bitsDirective << Val << "\n";
284 else if (TD.isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000285 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000286 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000287 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000288 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000289 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000290 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000291 } else {
292 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000293 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000294 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000295 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000296 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000297 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000298 }
299 return;
300 }
301 }
302
303 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000304 switch (type->getTypeID()) {
Misha Brukman835702a2005-04-21 22:36:52 +0000305 case Type::BoolTyID:
Chris Lattner8452a1f2004-08-17 06:36:49 +0000306 case Type::UByteTyID: case Type::SByteTyID:
307 O << Data8bitsDirective;
308 break;
309 case Type::UShortTyID: case Type::ShortTyID:
310 O << Data16bitsDirective;
311 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000312 case Type::PointerTyID:
Andrew Lenharthc8770aa2005-02-04 13:47:16 +0000313 if (TD.getPointerSize() == 8) {
314 O << Data64bitsDirective;
315 break;
316 }
317 //Fall through for pointer size == int size
Chris Lattner8452a1f2004-08-17 06:36:49 +0000318 case Type::UIntTyID: case Type::IntTyID:
319 O << Data32bitsDirective;
320 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000321 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner88e2d2e2005-08-08 04:26:32 +0000322 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
323 O << Data64bitsDirective;
324 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000325 case Type::FloatTyID: case Type::DoubleTyID:
326 assert (0 && "Should have already output floating point constant.");
327 default:
328 assert (0 && "Can't handle printing this type of thing");
329 break;
330 }
Chris Lattnerbb644e32005-11-21 07:51:36 +0000331 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000332 O << "\n";
333}