blob: 48d4a20a83f55c6ef5442fc7815154edec15b5a4 [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
Evan Cheng932f0222006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng246ae0d2006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman8cfa57b2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman37efe672006-04-22 18:53:45 +000020#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000021#include "llvm/Support/Mangler.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000022#include "llvm/Support/MathExtras.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000023#include "llvm/Target/TargetMachine.h"
Duraid Madina2e096c12005-12-28 06:29:02 +000024#include <iostream>
Chris Lattner66099132006-02-01 22:41:11 +000025#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000026using namespace llvm;
27
Chris Lattnered138932005-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 Lattnerd6c65ea2006-02-08 23:41:56 +000037 InlineAsmStart("#APP\n"),
38 InlineAsmEnd("#NO_APP\n"),
Chris Lattnered138932005-12-13 06:32:10 +000039 ZeroDirective("\t.zero\t"),
40 AsciiDirective("\t.ascii\t"),
41 AscizDirective("\t.asciz\t"),
42 Data8bitsDirective("\t.byte\t"),
43 Data16bitsDirective("\t.short\t"),
44 Data32bitsDirective("\t.long\t"),
45 Data64bitsDirective("\t.quad\t"),
46 AlignDirective("\t.align\t"),
47 AlignmentIsInBytes(true),
48 SwitchToSectionDirective("\t.section\t"),
49 ConstantPoolSection("\t.section .rodata\n"),
Nate Begeman37efe672006-04-22 18:53:45 +000050 JumpTableSection("\t.section .rodata\n"),
Chris Lattnered138932005-12-13 06:32:10 +000051 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
52 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
53 LCOMMDirective(0),
54 COMMDirective("\t.comm\t"),
55 COMMDirectiveTakesAlignment(true),
56 HasDotTypeDotSizeDirective(true) {
57}
58
59
Chris Lattnerac28fbd2005-11-21 07:06:27 +000060/// SwitchSection - Switch to the specified section of the executable if we
61/// are not already in it!
62///
63void AsmPrinter::SwitchSection(const char *NewSection, const GlobalValue *GV) {
64 std::string NS;
65
66 if (GV && GV->hasSection())
Chris Lattner3b4fd322005-11-21 08:25:09 +000067 NS = SwitchToSectionDirective + GV->getSection();
Chris Lattnerac28fbd2005-11-21 07:06:27 +000068 else
Chris Lattner42a80fe2005-12-09 19:28:49 +000069 NS = std::string("\t")+NewSection;
Chris Lattnerac28fbd2005-11-21 07:06:27 +000070
71 if (CurrentSection != NS) {
72 CurrentSection = NS;
73 if (!CurrentSection.empty())
Chris Lattner42a80fe2005-12-09 19:28:49 +000074 O << CurrentSection << '\n';
Chris Lattnerac28fbd2005-11-21 07:06:27 +000075 }
76}
77
Chris Lattnera80ba712004-08-16 23:15:22 +000078bool AsmPrinter::doInitialization(Module &M) {
Chris Lattneraf2bf0a2004-08-17 06:06:19 +000079 Mang = new Mangler(M, GlobalPrefix);
Chris Lattner2c1b1592006-01-23 23:47:53 +000080
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000081 if (!M.getModuleInlineAsm().empty())
82 O << CommentString << " Start of file scope inline assembly\n"
83 << M.getModuleInlineAsm()
84 << "\n" << CommentString << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +000085
Chris Lattnerac28fbd2005-11-21 07:06:27 +000086 SwitchSection("", 0); // Reset back to no section.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000087
88 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
89 DebugInfo->AnalyzeModule(M);
90 }
91
Chris Lattnera80ba712004-08-16 23:15:22 +000092 return false;
93}
94
95bool AsmPrinter::doFinalization(Module &M) {
96 delete Mang; Mang = 0;
97 return false;
98}
99
Chris Lattner25045bd2005-11-21 07:51:36 +0000100void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000101 // What's my mangled name?
Chris Lattner450de392005-11-10 18:36:17 +0000102 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner77bc2282005-11-21 08:13:27 +0000103 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +0000104}
105
Chris Lattner3b4fd322005-11-21 08:25:09 +0000106/// EmitConstantPool - Print to the current output stream assembly
107/// representations of the constants in the constant pool MCP. This is
108/// used to print out constants which have been "spilled to memory" by
109/// the code generator.
110///
111void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000112 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000113 if (CP.empty()) return;
114 const TargetData &TD = TM.getTargetData();
115
116 SwitchSection(ConstantPoolSection, 0);
Chris Lattner3029f922006-02-09 04:46:04 +0000117 EmitAlignment(MCP->getConstantPoolAlignment());
Chris Lattner3b4fd322005-11-21 08:25:09 +0000118 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner3b4fd322005-11-21 08:25:09 +0000119 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
Evan Cheng246ae0d2006-03-01 22:18:09 +0000120 << ":\t\t\t\t\t" << CommentString << " ";
121 WriteTypeSymbolic(O, CP[i].Val->getType(), 0) << '\n';
Chris Lattnerfa77d432006-02-09 04:22:52 +0000122 EmitGlobalConstant(CP[i].Val);
Chris Lattner3029f922006-02-09 04:46:04 +0000123 if (i != e-1) {
124 unsigned EntSize = TM.getTargetData().getTypeSize(CP[i].Val->getType());
125 unsigned ValEnd = CP[i].Offset + EntSize;
126 // Emit inter-object padding for alignment.
127 EmitZeros(CP[i+1].Offset-ValEnd);
128 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000129 }
130}
131
Nate Begeman37efe672006-04-22 18:53:45 +0000132/// EmitJumpTableInfo - Print assembly representations of the jump tables used
133/// by the current function to the current output stream.
134///
135void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
136 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
137 if (JT.empty()) return;
138 const TargetData &TD = TM.getTargetData();
139
140 // FIXME: someday we need to handle PIC jump tables
141 assert((TM.getRelocationModel() == Reloc::Static ||
142 TM.getRelocationModel() == Reloc::DynamicNoPIC) &&
143 "Unhandled relocation model emitting jump table information!");
144
145 SwitchSection(JumpTableSection, 0);
146 EmitAlignment(Log2_32(TD.getPointerAlignment()));
147 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
148 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << '_' << i
149 << ":\n";
150 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
151 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
152 O << Data32bitsDirective << ' ';
153 printBasicBlockLabel(JTBBs[ii]);
154 O << '\n';
155 }
156 }
157}
158
Chris Lattnered138932005-12-13 06:32:10 +0000159/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
160/// special global used by LLVM. If so, emit it and return true, otherwise
161/// do nothing and return false.
162bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey78098112006-03-07 22:00:35 +0000163 // Ignore debug and non-emitted data.
164 if (GV->getSection() == "llvm.metadata") return true;
165
166 if (!GV->hasAppendingLinkage()) return false;
167
168 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000169
170 if (GV->getName() == "llvm.used")
171 return true; // No need to emit this at all.
172
Chris Lattner5166b822006-01-12 19:17:23 +0000173 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattnered138932005-12-13 06:32:10 +0000174 SwitchSection(StaticCtorsSection, 0);
175 EmitAlignment(2, 0);
176 EmitXXStructorList(GV->getInitializer());
177 return true;
178 }
179
Chris Lattner5166b822006-01-12 19:17:23 +0000180 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattnered138932005-12-13 06:32:10 +0000181 SwitchSection(StaticDtorsSection, 0);
182 EmitAlignment(2, 0);
183 EmitXXStructorList(GV->getInitializer());
184 return true;
185 }
186
187 return false;
188}
189
190/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
191/// function pointers, ignoring the init priority.
192void AsmPrinter::EmitXXStructorList(Constant *List) {
193 // Should be an array of '{ int, void ()* }' structs. The first value is the
194 // init priority, which we ignore.
195 if (!isa<ConstantArray>(List)) return;
196 ConstantArray *InitList = cast<ConstantArray>(List);
197 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
198 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
199 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000200
201 if (CS->getOperand(1)->isNullValue())
202 return; // Found a null terminator, exit printing.
203 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000204 EmitGlobalConstant(CS->getOperand(1));
205 }
206}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000207
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000208/// getPreferredAlignmentLog - Return the preferred alignment of the
209/// specified global, returned in log form. This includes an explicitly
210/// requested alignment (if the global has one).
211unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
212 unsigned Alignment = TM.getTargetData().getTypeAlignmentShift(GV->getType());
213 if (GV->getAlignment() > (1U << Alignment))
214 Alignment = Log2_32(GV->getAlignment());
215
Chris Lattner519ea2a2006-02-05 01:46:49 +0000216 if (GV->hasInitializer()) {
217 // Always round up alignment of global doubles to 8 bytes.
218 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
219 Alignment = 3;
220 if (Alignment < 4) {
221 // If the global is not external, see if it is large. If so, give it a
222 // larger alignment.
223 if (TM.getTargetData().getTypeSize(GV->getType()->getElementType()) > 128)
224 Alignment = 4; // 16-byte alignment.
225 }
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000226 }
227 return Alignment;
228}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000229
Chris Lattner25045bd2005-11-21 07:51:36 +0000230// EmitAlignment - Emit an alignment directive to the specified power of two.
231void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnera1ab72d2005-11-14 19:00:06 +0000232 if (GV && GV->getAlignment())
233 NumBits = Log2_32(GV->getAlignment());
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000234 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattnerbfddc202004-08-17 19:14:29 +0000235 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
236 O << AlignDirective << NumBits << "\n";
237}
238
Chris Lattner25045bd2005-11-21 07:51:36 +0000239/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000240///
Chris Lattner25045bd2005-11-21 07:51:36 +0000241void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000242 if (NumZeros) {
243 if (ZeroDirective)
244 O << ZeroDirective << NumZeros << "\n";
245 else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000246 for (; NumZeros; --NumZeros)
247 O << Data8bitsDirective << "0\n";
248 }
249 }
250}
251
Chris Lattnera80ba712004-08-16 23:15:22 +0000252// Print out the specified constant, without a storage class. Only the
253// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000254void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000255 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000256 O << "0";
257 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
258 assert(CB == ConstantBool::True);
259 O << "1";
260 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
261 if (((CI->getValue() << 32) >> 32) == CI->getValue())
262 O << CI->getValue();
263 else
Duraid Madina45606572005-05-15 13:05:48 +0000264 O << (uint64_t)CI->getValue();
Chris Lattnera80ba712004-08-16 23:15:22 +0000265 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
266 O << CI->getValue();
Chris Lattner450de392005-11-10 18:36:17 +0000267 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000268 // This is a constant address for a global variable or function. Use the
269 // name of the variable or function as the address value, possibly
270 // decorating it with GlobalVarAddrPrefix/Suffix or
271 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattner450de392005-11-10 18:36:17 +0000272 if (isa<Function>(GV))
273 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina855a5192005-04-02 12:21:51 +0000274 else
Chris Lattner450de392005-11-10 18:36:17 +0000275 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina855a5192005-04-02 12:21:51 +0000276 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000277 const TargetData &TD = TM.getTargetData();
278 switch(CE->getOpcode()) {
279 case Instruction::GetElementPtr: {
280 // generate a symbolic expression for the byte address
281 const Constant *ptrVal = CE->getOperand(0);
282 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner27e19212005-02-14 21:40:26 +0000283 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
284 if (Offset)
285 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000286 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000287 if (Offset > 0)
288 O << ") + " << Offset;
289 else if (Offset < 0)
290 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000291 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000292 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000293 }
294 break;
295 }
296 case Instruction::Cast: {
297 // Support only non-converting or widening casts for now, that is, ones
298 // that do not involve a change in value. This assertion is really gross,
299 // and may not even be a complete check.
300 Constant *Op = CE->getOperand(0);
301 const Type *OpTy = Op->getType(), *Ty = CE->getType();
302
303 // Remember, kids, pointers can be losslessly converted back and forth
304 // into 32-bit or wider integers, regardless of signedness. :-P
305 assert(((isa<PointerType>(OpTy)
306 && (Ty == Type::LongTy || Ty == Type::ULongTy
307 || Ty == Type::IntTy || Ty == Type::UIntTy))
308 || (isa<PointerType>(Ty)
309 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
310 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
311 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
312 && OpTy->isLosslesslyConvertibleTo(Ty))))
313 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000314 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000315 break;
316 }
317 case Instruction::Add:
318 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000319 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattnera80ba712004-08-16 23:15:22 +0000320 O << ") + (";
Chris Lattner25045bd2005-11-21 07:51:36 +0000321 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000322 O << ")";
323 break;
324 default:
325 assert(0 && "Unsupported operator!");
326 }
327 } else {
328 assert(0 && "Unknown constant value!");
329 }
330}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000331
332/// toOctal - Convert the low order bits of X into an octal digit.
333///
334static inline char toOctal(int X) {
335 return (X&7)+'0';
336}
337
Chris Lattner2980cef2005-11-10 18:06:33 +0000338/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000339/// the predicate isString is true.
340///
Chris Lattner2980cef2005-11-10 18:06:33 +0000341static void printAsCString(std::ostream &O, const ConstantArray *CVA,
342 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000343 assert(CVA->isString() && "Array is not string compatible!");
344
345 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000346 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000347 unsigned char C =
Chris Lattnerdea18b62005-01-08 19:59:10 +0000348 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000349
350 if (C == '"') {
351 O << "\\\"";
352 } else if (C == '\\') {
353 O << "\\\\";
354 } else if (isprint(C)) {
355 O << C;
356 } else {
357 switch(C) {
358 case '\b': O << "\\b"; break;
359 case '\f': O << "\\f"; break;
360 case '\n': O << "\\n"; break;
361 case '\r': O << "\\r"; break;
362 case '\t': O << "\\t"; break;
363 default:
364 O << '\\';
365 O << toOctal(C >> 6);
366 O << toOctal(C >> 3);
367 O << toOctal(C >> 0);
368 break;
369 }
370 }
371 }
372 O << "\"";
373}
374
Chris Lattner25045bd2005-11-21 07:51:36 +0000375/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner1b7e2352004-08-17 06:36:49 +0000376///
Chris Lattner25045bd2005-11-21 07:51:36 +0000377void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000378 const TargetData &TD = TM.getTargetData();
379
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000380 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattner25045bd2005-11-21 07:51:36 +0000381 EmitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000382 return;
383 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
384 if (CVA->isString()) {
Chris Lattner2980cef2005-11-10 18:06:33 +0000385 unsigned NumElts = CVA->getNumOperands();
386 if (AscizDirective && NumElts &&
387 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
388 O << AscizDirective;
389 printAsCString(O, CVA, NumElts-1);
390 } else {
391 O << AsciiDirective;
392 printAsCString(O, CVA, NumElts);
393 }
Chris Lattner1b7e2352004-08-17 06:36:49 +0000394 O << "\n";
395 } else { // Not a string. Print the values in successive locations
396 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner25045bd2005-11-21 07:51:36 +0000397 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000398 }
399 return;
400 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
401 // Print the fields in successive locations. Pad to align if needed!
402 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000403 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000404 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
405 const Constant* field = CVS->getOperand(i);
406
407 // Check if padding is needed and insert one or more 0s.
Chris Lattnerdea18b62005-01-08 19:59:10 +0000408 uint64_t fieldSize = TD.getTypeSize(field->getType());
409 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner1b7e2352004-08-17 06:36:49 +0000410 : cvsLayout->MemberOffsets[i+1])
411 - cvsLayout->MemberOffsets[i]) - fieldSize;
412 sizeSoFar += fieldSize + padSize;
413
414 // Now print the actual field value
Chris Lattner25045bd2005-11-21 07:51:36 +0000415 EmitGlobalConstant(field);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000416
417 // Insert the field padding unless it's zero bytes...
Chris Lattner25045bd2005-11-21 07:51:36 +0000418 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000419 }
420 assert(sizeSoFar == cvsLayout->StructSize &&
421 "Layout of constant struct may be incorrect!");
422 return;
423 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
424 // FP Constants are printed as integer constants to avoid losing
425 // precision...
426 double Val = CFP->getValue();
427 if (CFP->getType() == Type::DoubleTy) {
Chris Lattnere85a5a92004-08-17 06:48:16 +0000428 if (Data64bitsDirective)
Jim Laskeycb6682f2005-08-17 19:34:49 +0000429 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattner7d057a32004-08-17 21:38:40 +0000430 << " double value: " << Val << "\n";
Chris Lattnere85a5a92004-08-17 06:48:16 +0000431 else if (TD.isBigEndian()) {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000432 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000433 << "\t" << CommentString << " double most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000434 << Val << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000435 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000436 << "\t" << CommentString << " double least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000437 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000438 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000439 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000440 << "\t" << CommentString << " double least significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000441 << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000442 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000443 << "\t" << CommentString << " double most significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000444 << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000445 }
446 return;
447 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000448 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattner0554fb62004-08-17 16:27:05 +0000449 << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000450 return;
451 }
452 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
453 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
454 uint64_t Val = CI->getRawValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000455
Chris Lattnere85a5a92004-08-17 06:48:16 +0000456 if (Data64bitsDirective)
457 O << Data64bitsDirective << Val << "\n";
458 else if (TD.isBigEndian()) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000459 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000460 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000461 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000462 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000463 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000464 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000465 } else {
466 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000467 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000468 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000469 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000470 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000471 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000472 }
473 return;
474 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000475 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
476 const PackedType *PTy = CP->getType();
477
478 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
479 EmitGlobalConstant(CP->getOperand(I));
480
481 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000482 }
483
484 const Type *type = CV->getType();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000485 switch (type->getTypeID()) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000486 case Type::BoolTyID:
Chris Lattner1b7e2352004-08-17 06:36:49 +0000487 case Type::UByteTyID: case Type::SByteTyID:
488 O << Data8bitsDirective;
489 break;
490 case Type::UShortTyID: case Type::ShortTyID:
491 O << Data16bitsDirective;
492 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000493 case Type::PointerTyID:
Andrew Lenharth571c9c32005-02-04 13:47:16 +0000494 if (TD.getPointerSize() == 8) {
495 O << Data64bitsDirective;
496 break;
497 }
498 //Fall through for pointer size == int size
Chris Lattner1b7e2352004-08-17 06:36:49 +0000499 case Type::UIntTyID: case Type::IntTyID:
500 O << Data32bitsDirective;
501 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000502 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner660538c2005-08-08 04:26:32 +0000503 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
504 O << Data64bitsDirective;
505 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000506 case Type::FloatTyID: case Type::DoubleTyID:
507 assert (0 && "Should have already output floating point constant.");
508 default:
509 assert (0 && "Can't handle printing this type of thing");
510 break;
511 }
Chris Lattner25045bd2005-11-21 07:51:36 +0000512 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000513 O << "\n";
514}
Chris Lattner0264d1a2006-01-27 02:10:10 +0000515
516/// printInlineAsm - This method formats and prints the specified machine
517/// instruction that is an inline asm.
518void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerd6c65ea2006-02-08 23:41:56 +0000519 O << InlineAsmStart;
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000520 unsigned NumOperands = MI->getNumOperands();
521
522 // Count the number of register definitions.
523 unsigned NumDefs = 0;
524 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
525 assert(NumDefs != NumOperands-1 && "No asm string?");
526
527 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +0000528
529 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000530 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +0000531
532 // The variant of the current asmprinter: FIXME: change.
533 int AsmPrinterVariant = 0;
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000534
Chris Lattner66099132006-02-01 22:41:11 +0000535 int CurVariant = -1; // The number of the {.|.|.} region we are in.
536 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +0000537
Chris Lattner66099132006-02-01 22:41:11 +0000538 while (*LastEmitted) {
539 switch (*LastEmitted) {
540 default: {
541 // Not a special case, emit the string section literally.
542 const char *LiteralEnd = LastEmitted+1;
543 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
544 *LiteralEnd != '}' && *LiteralEnd != '$')
545 ++LiteralEnd;
546 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
547 O.write(LastEmitted, LiteralEnd-LastEmitted);
548 LastEmitted = LiteralEnd;
549 break;
550 }
551 case '$': {
552 ++LastEmitted; // Consume '$' character.
553 if (*LastEmitted == '$') { // $$ -> $
554 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
555 O << '$';
556 ++LastEmitted; // Consume second '$' character.
557 break;
558 }
559
560 bool HasCurlyBraces = false;
561 if (*LastEmitted == '{') { // ${variable}
562 ++LastEmitted; // Consume '{' character.
563 HasCurlyBraces = true;
564 }
565
566 const char *IDStart = LastEmitted;
567 char *IDEnd;
568 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
569 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
570 std::cerr << "Bad $ operand number in inline asm string: '"
571 << AsmStr << "'\n";
572 exit(1);
573 }
574 LastEmitted = IDEnd;
575
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000576 char Modifier[2] = { 0, 0 };
577
Chris Lattner66099132006-02-01 22:41:11 +0000578 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000579 // If we have curly braces, check for a modifier character. This
580 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
581 if (*LastEmitted == ':') {
582 ++LastEmitted; // Consume ':' character.
583 if (*LastEmitted == 0) {
584 std::cerr << "Bad ${:} expression in inline asm string: '"
585 << AsmStr << "'\n";
586 exit(1);
587 }
588
589 Modifier[0] = *LastEmitted;
590 ++LastEmitted; // Consume modifier character.
591 }
592
Chris Lattner66099132006-02-01 22:41:11 +0000593 if (*LastEmitted != '}') {
594 std::cerr << "Bad ${} expression in inline asm string: '"
595 << AsmStr << "'\n";
596 exit(1);
597 }
598 ++LastEmitted; // Consume '}' character.
599 }
600
601 if ((unsigned)Val >= NumOperands-1) {
602 std::cerr << "Invalid $ operand number in inline asm string: '"
603 << AsmStr << "'\n";
604 exit(1);
605 }
606
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000607 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +0000608 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000609 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
610 unsigned OpNo = 1;
611
612 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000613 for (; Val; --Val) {
614 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
615 OpNo += (OpFlags >> 3) + 1;
616 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000617
Chris Lattnerdd260332006-02-24 20:21:58 +0000618 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000619 ++OpNo; // Skip over the ID number.
Chris Lattnerdd260332006-02-24 20:21:58 +0000620
621 bool Error;
622 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
623 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
624 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
625 Modifier[0] ? Modifier : 0);
626 } else {
627 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
628 Modifier[0] ? Modifier : 0);
629 }
630 if (Error) {
Chris Lattner66099132006-02-01 22:41:11 +0000631 std::cerr << "Invalid operand found in inline asm: '"
632 << AsmStr << "'\n";
633 MI->dump();
634 exit(1);
635 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000636 }
Chris Lattner66099132006-02-01 22:41:11 +0000637 break;
638 }
639 case '{':
640 ++LastEmitted; // Consume '{' character.
641 if (CurVariant != -1) {
642 std::cerr << "Nested variants found in inline asm string: '"
643 << AsmStr << "'\n";
644 exit(1);
645 }
646 CurVariant = 0; // We're in the first variant now.
647 break;
648 case '|':
649 ++LastEmitted; // consume '|' character.
650 if (CurVariant == -1) {
651 std::cerr << "Found '|' character outside of variant in inline asm "
652 << "string: '" << AsmStr << "'\n";
653 exit(1);
654 }
655 ++CurVariant; // We're in the next variant.
656 break;
657 case '}':
658 ++LastEmitted; // consume '}' character.
659 if (CurVariant == -1) {
660 std::cerr << "Found '}' character outside of variant in inline asm "
661 << "string: '" << AsmStr << "'\n";
662 exit(1);
663 }
664 CurVariant = -1;
665 break;
666 }
667 }
Chris Lattnerd6c65ea2006-02-08 23:41:56 +0000668 O << "\n" << InlineAsmEnd;
Chris Lattner66099132006-02-01 22:41:11 +0000669}
670
671/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
672/// instruction, using the specified assembler variant. Targets should
673/// overried this to format as appropriate.
674bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000675 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +0000676 // Target doesn't support this yet!
677 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +0000678}
Chris Lattnerdd260332006-02-24 20:21:58 +0000679
680bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
681 unsigned AsmVariant,
682 const char *ExtraCode) {
683 // Target doesn't support this yet!
684 return true;
685}
Nate Begeman37efe672006-04-22 18:53:45 +0000686
687/// printBasicBlockLabel - This method prints the label for the specified
688/// MachineBasicBlock
689void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB) const {
690 O << PrivateGlobalPrefix << "LBB"
691 << Mang->getValueName(MBB->getParent()->getFunction())
692 << "_" << MBB->getNumber() << '\t' << CommentString
693 << MBB->getBasicBlock()->getName();
694}