blob: aad02327c64cedd62b7705dc716770400f3bfaa2 [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
Evan Cheng993e9cf2006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng38d5e762006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman41b1cdc2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattnerf2991ce2005-11-21 08:25:09 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000020#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000021#include "llvm/Support/Mangler.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000022#include "llvm/Support/MathExtras.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000023#include "llvm/Target/TargetMachine.h"
Duraid Madina26b037e2005-12-28 06:29:02 +000024#include <iostream>
Chris Lattneraa23fa92006-02-01 22:41:11 +000025#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000026using namespace llvm;
27
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000028AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm)
29: FunctionNumber(0), O(o), TM(tm),
30 CommentString("#"),
31 GlobalPrefix(""),
32 PrivateGlobalPrefix("."),
33 GlobalVarAddrPrefix(""),
34 GlobalVarAddrSuffix(""),
35 FunctionAddrPrefix(""),
36 FunctionAddrSuffix(""),
Chris Lattner25f55ae2006-05-01 04:11:03 +000037 InlineAsmStart("#APP\n\t"),
38 InlineAsmEnd("\t#NO_APP\n"),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000039 ZeroDirective("\t.zero\t"),
Jeff Cohenf34ddb12006-05-02 03:46:13 +000040 ZeroDirectiveSuffix(0),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000041 AsciiDirective("\t.ascii\t"),
42 AscizDirective("\t.asciz\t"),
43 Data8bitsDirective("\t.byte\t"),
44 Data16bitsDirective("\t.short\t"),
45 Data32bitsDirective("\t.long\t"),
46 Data64bitsDirective("\t.quad\t"),
47 AlignDirective("\t.align\t"),
48 AlignmentIsInBytes(true),
49 SwitchToSectionDirective("\t.section\t"),
50 ConstantPoolSection("\t.section .rodata\n"),
Nate Begeman4ca2ea52006-04-22 18:53:45 +000051 JumpTableSection("\t.section .rodata\n"),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000052 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
53 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
54 LCOMMDirective(0),
55 COMMDirective("\t.comm\t"),
56 COMMDirectiveTakesAlignment(true),
57 HasDotTypeDotSizeDirective(true) {
58}
59
60
Chris Lattner2ea5c992005-11-21 07:06:27 +000061/// SwitchSection - Switch to the specified section of the executable if we
62/// are not already in it!
63///
64void AsmPrinter::SwitchSection(const char *NewSection, const GlobalValue *GV) {
65 std::string NS;
66
67 if (GV && GV->hasSection())
Chris Lattnerf2991ce2005-11-21 08:25:09 +000068 NS = SwitchToSectionDirective + GV->getSection();
Chris Lattner2ea5c992005-11-21 07:06:27 +000069 else
Chris Lattnera6f835f2005-12-09 19:28:49 +000070 NS = std::string("\t")+NewSection;
Chris Lattner2ea5c992005-11-21 07:06:27 +000071
72 if (CurrentSection != NS) {
73 CurrentSection = NS;
74 if (!CurrentSection.empty())
Chris Lattnera6f835f2005-12-09 19:28:49 +000075 O << CurrentSection << '\n';
Chris Lattner2ea5c992005-11-21 07:06:27 +000076 }
77}
78
Chris Lattner6a8e0f52004-08-16 23:15:22 +000079bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner6d42cbd2004-08-17 06:06:19 +000080 Mang = new Mangler(M, GlobalPrefix);
Chris Lattnere3a79262006-01-23 23:47:53 +000081
Chris Lattner00fcdfe2006-01-24 04:16:34 +000082 if (!M.getModuleInlineAsm().empty())
83 O << CommentString << " Start of file scope inline assembly\n"
84 << M.getModuleInlineAsm()
85 << "\n" << CommentString << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +000086
Chris Lattner2ea5c992005-11-21 07:06:27 +000087 SwitchSection("", 0); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +000088
89 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
90 DebugInfo->AnalyzeModule(M);
91 }
92
Chris Lattner6a8e0f52004-08-16 23:15:22 +000093 return false;
94}
95
96bool AsmPrinter::doFinalization(Module &M) {
97 delete Mang; Mang = 0;
98 return false;
99}
100
Chris Lattnerbb644e32005-11-21 07:51:36 +0000101void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000102 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000103 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000104 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000105}
106
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000107/// EmitConstantPool - Print to the current output stream assembly
108/// representations of the constants in the constant pool MCP. This is
109/// used to print out constants which have been "spilled to memory" by
110/// the code generator.
111///
112void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000113 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000114 if (CP.empty()) return;
115 const TargetData &TD = TM.getTargetData();
116
117 SwitchSection(ConstantPoolSection, 0);
Chris Lattnerf6190822006-02-09 04:46:04 +0000118 EmitAlignment(MCP->getConstantPoolAlignment());
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000119 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000120 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
Evan Cheng38d5e762006-03-01 22:18:09 +0000121 << ":\t\t\t\t\t" << CommentString << " ";
122 WriteTypeSymbolic(O, CP[i].Val->getType(), 0) << '\n';
Chris Lattnerba972642006-02-09 04:22:52 +0000123 EmitGlobalConstant(CP[i].Val);
Chris Lattnerf6190822006-02-09 04:46:04 +0000124 if (i != e-1) {
125 unsigned EntSize = TM.getTargetData().getTypeSize(CP[i].Val->getType());
126 unsigned ValEnd = CP[i].Offset + EntSize;
127 // Emit inter-object padding for alignment.
128 EmitZeros(CP[i+1].Offset-ValEnd);
129 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000130 }
131}
132
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000133/// EmitJumpTableInfo - Print assembly representations of the jump tables used
134/// by the current function to the current output stream.
135///
136void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
137 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
138 if (JT.empty()) return;
139 const TargetData &TD = TM.getTargetData();
140
141 // FIXME: someday we need to handle PIC jump tables
142 assert((TM.getRelocationModel() == Reloc::Static ||
143 TM.getRelocationModel() == Reloc::DynamicNoPIC) &&
144 "Unhandled relocation model emitting jump table information!");
145
146 SwitchSection(JumpTableSection, 0);
147 EmitAlignment(Log2_32(TD.getPointerAlignment()));
148 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
149 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << '_' << i
150 << ":\n";
151 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
152 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
153 O << Data32bitsDirective << ' ';
154 printBasicBlockLabel(JTBBs[ii]);
155 O << '\n';
156 }
157 }
158}
159
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000160/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
161/// special global used by LLVM. If so, emit it and return true, otherwise
162/// do nothing and return false.
163bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey313570f2006-03-07 22:00:35 +0000164 // Ignore debug and non-emitted data.
165 if (GV->getSection() == "llvm.metadata") return true;
166
167 if (!GV->hasAppendingLinkage()) return false;
168
169 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000170
171 if (GV->getName() == "llvm.used")
172 return true; // No need to emit this at all.
173
Chris Lattner3760e902006-01-12 19:17:23 +0000174 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000175 SwitchSection(StaticCtorsSection, 0);
176 EmitAlignment(2, 0);
177 EmitXXStructorList(GV->getInitializer());
178 return true;
179 }
180
Chris Lattner3760e902006-01-12 19:17:23 +0000181 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000182 SwitchSection(StaticDtorsSection, 0);
183 EmitAlignment(2, 0);
184 EmitXXStructorList(GV->getInitializer());
185 return true;
186 }
187
188 return false;
189}
190
191/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
192/// function pointers, ignoring the init priority.
193void AsmPrinter::EmitXXStructorList(Constant *List) {
194 // Should be an array of '{ int, void ()* }' structs. The first value is the
195 // init priority, which we ignore.
196 if (!isa<ConstantArray>(List)) return;
197 ConstantArray *InitList = cast<ConstantArray>(List);
198 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
199 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
200 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000201
202 if (CS->getOperand(1)->isNullValue())
203 return; // Found a null terminator, exit printing.
204 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000205 EmitGlobalConstant(CS->getOperand(1));
206 }
207}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000208
Chris Lattnera9b25252006-02-05 01:29:18 +0000209/// getPreferredAlignmentLog - Return the preferred alignment of the
210/// specified global, returned in log form. This includes an explicitly
211/// requested alignment (if the global has one).
212unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
213 unsigned Alignment = TM.getTargetData().getTypeAlignmentShift(GV->getType());
214 if (GV->getAlignment() > (1U << Alignment))
215 Alignment = Log2_32(GV->getAlignment());
216
Chris Lattnercbab2842006-02-05 01:46:49 +0000217 if (GV->hasInitializer()) {
218 // Always round up alignment of global doubles to 8 bytes.
219 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
220 Alignment = 3;
221 if (Alignment < 4) {
222 // If the global is not external, see if it is large. If so, give it a
223 // larger alignment.
224 if (TM.getTargetData().getTypeSize(GV->getType()->getElementType()) > 128)
225 Alignment = 4; // 16-byte alignment.
226 }
Chris Lattnera9b25252006-02-05 01:29:18 +0000227 }
228 return Alignment;
229}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000230
Chris Lattnerbb644e32005-11-21 07:51:36 +0000231// EmitAlignment - Emit an alignment directive to the specified power of two.
232void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000233 if (GV && GV->getAlignment())
234 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000235 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner1d35c162004-08-17 19:14:29 +0000236 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
237 O << AlignDirective << NumBits << "\n";
238}
239
Chris Lattnerbb644e32005-11-21 07:51:36 +0000240/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000241///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000242void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000243 if (NumZeros) {
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000244 if (ZeroDirective) {
245 O << ZeroDirective << NumZeros;
246 if (ZeroDirectiveSuffix)
247 O << ZeroDirectiveSuffix;
248 O << "\n";
249 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000250 for (; NumZeros; --NumZeros)
251 O << Data8bitsDirective << "0\n";
252 }
253 }
254}
255
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000256// Print out the specified constant, without a storage class. Only the
257// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000258void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000259 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000260 O << "0";
261 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
262 assert(CB == ConstantBool::True);
263 O << "1";
264 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
265 if (((CI->getValue() << 32) >> 32) == CI->getValue())
266 O << CI->getValue();
267 else
Duraid Madina73c4dba2005-05-15 13:05:48 +0000268 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000269 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
270 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000271 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000272 // This is a constant address for a global variable or function. Use the
273 // name of the variable or function as the address value, possibly
274 // decorating it with GlobalVarAddrPrefix/Suffix or
275 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000276 if (isa<Function>(GV))
277 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000278 else
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000279 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000280 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000281 const TargetData &TD = TM.getTargetData();
282 switch(CE->getOpcode()) {
283 case Instruction::GetElementPtr: {
284 // generate a symbolic expression for the byte address
285 const Constant *ptrVal = CE->getOperand(0);
286 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner145569b2005-02-14 21:40:26 +0000287 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
288 if (Offset)
289 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000290 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000291 if (Offset > 0)
292 O << ") + " << Offset;
293 else if (Offset < 0)
294 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000295 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000296 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000297 }
298 break;
299 }
300 case Instruction::Cast: {
301 // Support only non-converting or widening casts for now, that is, ones
302 // that do not involve a change in value. This assertion is really gross,
303 // and may not even be a complete check.
304 Constant *Op = CE->getOperand(0);
305 const Type *OpTy = Op->getType(), *Ty = CE->getType();
306
307 // Remember, kids, pointers can be losslessly converted back and forth
308 // into 32-bit or wider integers, regardless of signedness. :-P
309 assert(((isa<PointerType>(OpTy)
310 && (Ty == Type::LongTy || Ty == Type::ULongTy
311 || Ty == Type::IntTy || Ty == Type::UIntTy))
312 || (isa<PointerType>(Ty)
313 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
314 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
315 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
316 && OpTy->isLosslesslyConvertibleTo(Ty))))
317 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000318 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000319 break;
320 }
321 case Instruction::Add:
322 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000323 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000324 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000325 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000326 O << ")";
327 break;
328 default:
329 assert(0 && "Unsupported operator!");
330 }
331 } else {
332 assert(0 && "Unknown constant value!");
333 }
334}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000335
336/// toOctal - Convert the low order bits of X into an octal digit.
337///
338static inline char toOctal(int X) {
339 return (X&7)+'0';
340}
341
Chris Lattner55a6d902005-11-10 18:06:33 +0000342/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000343/// the predicate isString is true.
344///
Chris Lattner55a6d902005-11-10 18:06:33 +0000345static void printAsCString(std::ostream &O, const ConstantArray *CVA,
346 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000347 assert(CVA->isString() && "Array is not string compatible!");
348
349 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000350 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000351 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000352 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000353
354 if (C == '"') {
355 O << "\\\"";
356 } else if (C == '\\') {
357 O << "\\\\";
358 } else if (isprint(C)) {
359 O << C;
360 } else {
361 switch(C) {
362 case '\b': O << "\\b"; break;
363 case '\f': O << "\\f"; break;
364 case '\n': O << "\\n"; break;
365 case '\r': O << "\\r"; break;
366 case '\t': O << "\\t"; break;
367 default:
368 O << '\\';
369 O << toOctal(C >> 6);
370 O << toOctal(C >> 3);
371 O << toOctal(C >> 0);
372 break;
373 }
374 }
375 }
376 O << "\"";
377}
378
Jeff Cohen24a62a92006-05-02 01:16:28 +0000379/// EmitString - Emit a zero-byte-terminated string constant.
380///
381void AsmPrinter::EmitString(const ConstantArray *CVA) const {
382 unsigned NumElts = CVA->getNumOperands();
383 if (AscizDirective && NumElts &&
384 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
385 O << AscizDirective;
386 printAsCString(O, CVA, NumElts-1);
387 } else {
388 O << AsciiDirective;
389 printAsCString(O, CVA, NumElts);
390 }
391 O << "\n";
392}
393
Chris Lattnerbb644e32005-11-21 07:51:36 +0000394/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000395///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000396void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000397 const TargetData &TD = TM.getTargetData();
398
Chris Lattner61753bf2004-10-16 18:19:26 +0000399 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000400 EmitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000401 return;
402 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
403 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000404 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000405 } else { // Not a string. Print the values in successive locations
406 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000407 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000408 }
409 return;
410 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
411 // Print the fields in successive locations. Pad to align if needed!
412 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000413 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000414 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
415 const Constant* field = CVS->getOperand(i);
416
417 // Check if padding is needed and insert one or more 0s.
Chris Lattner0b955fd2005-01-08 19:59:10 +0000418 uint64_t fieldSize = TD.getTypeSize(field->getType());
419 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000420 : cvsLayout->MemberOffsets[i+1])
421 - cvsLayout->MemberOffsets[i]) - fieldSize;
422 sizeSoFar += fieldSize + padSize;
423
424 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000425 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000426
427 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000428 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000429 }
430 assert(sizeSoFar == cvsLayout->StructSize &&
431 "Layout of constant struct may be incorrect!");
432 return;
433 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
434 // FP Constants are printed as integer constants to avoid losing
435 // precision...
436 double Val = CFP->getValue();
437 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000438 if (Data64bitsDirective)
Jim Laskeyb74c6662005-08-17 19:34:49 +0000439 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattnerea751992004-08-17 21:38:40 +0000440 << " double value: " << Val << "\n";
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000441 else if (TD.isBigEndian()) {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000442 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000443 << "\t" << CommentString << " double most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000444 << Val << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000445 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000446 << "\t" << CommentString << " double least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000447 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000448 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000449 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000450 << "\t" << CommentString << " double least significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000451 << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000452 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000453 << "\t" << CommentString << " double most significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000454 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000455 }
456 return;
457 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000458 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattnerda6beac2004-08-17 16:27:05 +0000459 << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000460 return;
461 }
462 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
463 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
464 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000465
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000466 if (Data64bitsDirective)
467 O << Data64bitsDirective << Val << "\n";
468 else if (TD.isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000469 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000470 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000471 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000472 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000473 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000474 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000475 } else {
476 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000477 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000478 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000479 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000480 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000481 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000482 }
483 return;
484 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000485 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
486 const PackedType *PTy = CP->getType();
487
488 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
489 EmitGlobalConstant(CP->getOperand(I));
490
491 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000492 }
493
494 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000495 switch (type->getTypeID()) {
Misha Brukman835702a2005-04-21 22:36:52 +0000496 case Type::BoolTyID:
Chris Lattner8452a1f2004-08-17 06:36:49 +0000497 case Type::UByteTyID: case Type::SByteTyID:
498 O << Data8bitsDirective;
499 break;
500 case Type::UShortTyID: case Type::ShortTyID:
501 O << Data16bitsDirective;
502 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000503 case Type::PointerTyID:
Andrew Lenharthc8770aa2005-02-04 13:47:16 +0000504 if (TD.getPointerSize() == 8) {
505 O << Data64bitsDirective;
506 break;
507 }
508 //Fall through for pointer size == int size
Chris Lattner8452a1f2004-08-17 06:36:49 +0000509 case Type::UIntTyID: case Type::IntTyID:
510 O << Data32bitsDirective;
511 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000512 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner88e2d2e2005-08-08 04:26:32 +0000513 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
514 O << Data64bitsDirective;
515 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000516 case Type::FloatTyID: case Type::DoubleTyID:
517 assert (0 && "Should have already output floating point constant.");
518 default:
519 assert (0 && "Can't handle printing this type of thing");
520 break;
521 }
Chris Lattnerbb644e32005-11-21 07:51:36 +0000522 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000523 O << "\n";
524}
Chris Lattner061d9e22006-01-27 02:10:10 +0000525
526/// printInlineAsm - This method formats and prints the specified machine
527/// instruction that is an inline asm.
528void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnered87dcd2006-02-08 23:41:56 +0000529 O << InlineAsmStart;
Chris Lattner57ecb562006-01-30 23:00:08 +0000530 unsigned NumOperands = MI->getNumOperands();
531
532 // Count the number of register definitions.
533 unsigned NumDefs = 0;
534 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
535 assert(NumDefs != NumOperands-1 && "No asm string?");
536
537 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000538
539 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000540 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000541
542 // The variant of the current asmprinter: FIXME: change.
543 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000544
Chris Lattneraa23fa92006-02-01 22:41:11 +0000545 int CurVariant = -1; // The number of the {.|.|.} region we are in.
546 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000547
Chris Lattneraa23fa92006-02-01 22:41:11 +0000548 while (*LastEmitted) {
549 switch (*LastEmitted) {
550 default: {
551 // Not a special case, emit the string section literally.
552 const char *LiteralEnd = LastEmitted+1;
553 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
554 *LiteralEnd != '}' && *LiteralEnd != '$')
555 ++LiteralEnd;
556 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
557 O.write(LastEmitted, LiteralEnd-LastEmitted);
558 LastEmitted = LiteralEnd;
559 break;
560 }
561 case '$': {
562 ++LastEmitted; // Consume '$' character.
563 if (*LastEmitted == '$') { // $$ -> $
564 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
565 O << '$';
566 ++LastEmitted; // Consume second '$' character.
567 break;
568 }
569
570 bool HasCurlyBraces = false;
571 if (*LastEmitted == '{') { // ${variable}
572 ++LastEmitted; // Consume '{' character.
573 HasCurlyBraces = true;
574 }
575
576 const char *IDStart = LastEmitted;
577 char *IDEnd;
578 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
579 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
580 std::cerr << "Bad $ operand number in inline asm string: '"
581 << AsmStr << "'\n";
582 exit(1);
583 }
584 LastEmitted = IDEnd;
585
Chris Lattner34f74c12006-02-06 22:17:23 +0000586 char Modifier[2] = { 0, 0 };
587
Chris Lattneraa23fa92006-02-01 22:41:11 +0000588 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000589 // If we have curly braces, check for a modifier character. This
590 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
591 if (*LastEmitted == ':') {
592 ++LastEmitted; // Consume ':' character.
593 if (*LastEmitted == 0) {
594 std::cerr << "Bad ${:} expression in inline asm string: '"
595 << AsmStr << "'\n";
596 exit(1);
597 }
598
599 Modifier[0] = *LastEmitted;
600 ++LastEmitted; // Consume modifier character.
601 }
602
Chris Lattneraa23fa92006-02-01 22:41:11 +0000603 if (*LastEmitted != '}') {
604 std::cerr << "Bad ${} expression in inline asm string: '"
605 << AsmStr << "'\n";
606 exit(1);
607 }
608 ++LastEmitted; // Consume '}' character.
609 }
610
611 if ((unsigned)Val >= NumOperands-1) {
612 std::cerr << "Invalid $ operand number in inline asm string: '"
613 << AsmStr << "'\n";
614 exit(1);
615 }
616
Chris Lattner571d9642006-02-23 19:21:04 +0000617 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +0000618 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +0000619 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
620 unsigned OpNo = 1;
621
622 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +0000623 for (; Val; --Val) {
624 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
625 OpNo += (OpFlags >> 3) + 1;
626 }
Chris Lattner571d9642006-02-23 19:21:04 +0000627
Chris Lattner1d08c652006-02-24 20:21:58 +0000628 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
Chris Lattner571d9642006-02-23 19:21:04 +0000629 ++OpNo; // Skip over the ID number.
Chris Lattner1d08c652006-02-24 20:21:58 +0000630
631 bool Error;
632 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
633 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
634 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
635 Modifier[0] ? Modifier : 0);
636 } else {
637 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
638 Modifier[0] ? Modifier : 0);
639 }
640 if (Error) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000641 std::cerr << "Invalid operand found in inline asm: '"
642 << AsmStr << "'\n";
643 MI->dump();
644 exit(1);
645 }
Chris Lattner571d9642006-02-23 19:21:04 +0000646 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000647 break;
648 }
649 case '{':
650 ++LastEmitted; // Consume '{' character.
651 if (CurVariant != -1) {
652 std::cerr << "Nested variants found in inline asm string: '"
653 << AsmStr << "'\n";
654 exit(1);
655 }
656 CurVariant = 0; // We're in the first variant now.
657 break;
658 case '|':
659 ++LastEmitted; // consume '|' character.
660 if (CurVariant == -1) {
661 std::cerr << "Found '|' character outside of variant in inline asm "
662 << "string: '" << AsmStr << "'\n";
663 exit(1);
664 }
665 ++CurVariant; // We're in the next variant.
666 break;
667 case '}':
668 ++LastEmitted; // consume '}' character.
669 if (CurVariant == -1) {
670 std::cerr << "Found '}' character outside of variant in inline asm "
671 << "string: '" << AsmStr << "'\n";
672 exit(1);
673 }
674 CurVariant = -1;
675 break;
676 }
677 }
Chris Lattnered87dcd2006-02-08 23:41:56 +0000678 O << "\n" << InlineAsmEnd;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000679}
680
681/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
682/// instruction, using the specified assembler variant. Targets should
683/// overried this to format as appropriate.
684bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000685 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000686 // Target doesn't support this yet!
687 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000688}
Chris Lattner1d08c652006-02-24 20:21:58 +0000689
690bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
691 unsigned AsmVariant,
692 const char *ExtraCode) {
693 // Target doesn't support this yet!
694 return true;
695}
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000696
697/// printBasicBlockLabel - This method prints the label for the specified
698/// MachineBasicBlock
699void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB) const {
700 O << PrivateGlobalPrefix << "LBB"
701 << Mang->getValueName(MBB->getParent()->getFunction())
702 << "_" << MBB->getNumber() << '\t' << CommentString
703 << MBB->getBasicBlock()->getName();
704}