blob: 11c6f231b9f29729edae3168a1c64cfd93499509 [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"
Chris Lattnera80ba712004-08-16 23:15:22 +000020#include "llvm/Support/Mangler.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000021#include "llvm/Support/MathExtras.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000022#include "llvm/Target/TargetMachine.h"
Duraid Madina2e096c12005-12-28 06:29:02 +000023#include <iostream>
Chris Lattner66099132006-02-01 22:41:11 +000024#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000025using namespace llvm;
26
Chris Lattnered138932005-12-13 06:32:10 +000027AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm)
28: FunctionNumber(0), O(o), TM(tm),
29 CommentString("#"),
30 GlobalPrefix(""),
31 PrivateGlobalPrefix("."),
32 GlobalVarAddrPrefix(""),
33 GlobalVarAddrSuffix(""),
34 FunctionAddrPrefix(""),
35 FunctionAddrSuffix(""),
Chris Lattnerd6c65ea2006-02-08 23:41:56 +000036 InlineAsmStart("#APP\n"),
37 InlineAsmEnd("#NO_APP\n"),
Chris Lattnered138932005-12-13 06:32:10 +000038 ZeroDirective("\t.zero\t"),
39 AsciiDirective("\t.ascii\t"),
40 AscizDirective("\t.asciz\t"),
41 Data8bitsDirective("\t.byte\t"),
42 Data16bitsDirective("\t.short\t"),
43 Data32bitsDirective("\t.long\t"),
44 Data64bitsDirective("\t.quad\t"),
45 AlignDirective("\t.align\t"),
46 AlignmentIsInBytes(true),
47 SwitchToSectionDirective("\t.section\t"),
48 ConstantPoolSection("\t.section .rodata\n"),
49 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
50 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
51 LCOMMDirective(0),
52 COMMDirective("\t.comm\t"),
53 COMMDirectiveTakesAlignment(true),
54 HasDotTypeDotSizeDirective(true) {
55}
56
57
Chris Lattnerac28fbd2005-11-21 07:06:27 +000058/// SwitchSection - Switch to the specified section of the executable if we
59/// are not already in it!
60///
61void AsmPrinter::SwitchSection(const char *NewSection, const GlobalValue *GV) {
62 std::string NS;
63
64 if (GV && GV->hasSection())
Chris Lattner3b4fd322005-11-21 08:25:09 +000065 NS = SwitchToSectionDirective + GV->getSection();
Chris Lattnerac28fbd2005-11-21 07:06:27 +000066 else
Chris Lattner42a80fe2005-12-09 19:28:49 +000067 NS = std::string("\t")+NewSection;
Chris Lattnerac28fbd2005-11-21 07:06:27 +000068
69 if (CurrentSection != NS) {
70 CurrentSection = NS;
71 if (!CurrentSection.empty())
Chris Lattner42a80fe2005-12-09 19:28:49 +000072 O << CurrentSection << '\n';
Chris Lattnerac28fbd2005-11-21 07:06:27 +000073 }
74}
75
Chris Lattnera80ba712004-08-16 23:15:22 +000076bool AsmPrinter::doInitialization(Module &M) {
Chris Lattneraf2bf0a2004-08-17 06:06:19 +000077 Mang = new Mangler(M, GlobalPrefix);
Chris Lattner2c1b1592006-01-23 23:47:53 +000078
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000079 if (!M.getModuleInlineAsm().empty())
80 O << CommentString << " Start of file scope inline assembly\n"
81 << M.getModuleInlineAsm()
82 << "\n" << CommentString << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +000083
Chris Lattnerac28fbd2005-11-21 07:06:27 +000084 SwitchSection("", 0); // Reset back to no section.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000085
86 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
87 DebugInfo->AnalyzeModule(M);
88 }
89
Chris Lattnera80ba712004-08-16 23:15:22 +000090 return false;
91}
92
93bool AsmPrinter::doFinalization(Module &M) {
94 delete Mang; Mang = 0;
95 return false;
96}
97
Chris Lattner25045bd2005-11-21 07:51:36 +000098void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +000099 // What's my mangled name?
Chris Lattner450de392005-11-10 18:36:17 +0000100 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner77bc2282005-11-21 08:13:27 +0000101 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +0000102}
103
Chris Lattner3b4fd322005-11-21 08:25:09 +0000104/// EmitConstantPool - Print to the current output stream assembly
105/// representations of the constants in the constant pool MCP. This is
106/// used to print out constants which have been "spilled to memory" by
107/// the code generator.
108///
109void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000110 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000111 if (CP.empty()) return;
112 const TargetData &TD = TM.getTargetData();
113
114 SwitchSection(ConstantPoolSection, 0);
Chris Lattner3029f922006-02-09 04:46:04 +0000115 EmitAlignment(MCP->getConstantPoolAlignment());
Chris Lattner3b4fd322005-11-21 08:25:09 +0000116 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner3b4fd322005-11-21 08:25:09 +0000117 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
Evan Cheng246ae0d2006-03-01 22:18:09 +0000118 << ":\t\t\t\t\t" << CommentString << " ";
119 WriteTypeSymbolic(O, CP[i].Val->getType(), 0) << '\n';
Chris Lattnerfa77d432006-02-09 04:22:52 +0000120 EmitGlobalConstant(CP[i].Val);
Chris Lattner3029f922006-02-09 04:46:04 +0000121 if (i != e-1) {
122 unsigned EntSize = TM.getTargetData().getTypeSize(CP[i].Val->getType());
123 unsigned ValEnd = CP[i].Offset + EntSize;
124 // Emit inter-object padding for alignment.
125 EmitZeros(CP[i+1].Offset-ValEnd);
126 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000127 }
128}
129
Chris Lattnered138932005-12-13 06:32:10 +0000130/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
131/// special global used by LLVM. If so, emit it and return true, otherwise
132/// do nothing and return false.
133bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey78098112006-03-07 22:00:35 +0000134 // Ignore debug and non-emitted data.
135 if (GV->getSection() == "llvm.metadata") return true;
136
137 if (!GV->hasAppendingLinkage()) return false;
138
139 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000140
141 if (GV->getName() == "llvm.used")
142 return true; // No need to emit this at all.
143
Chris Lattner5166b822006-01-12 19:17:23 +0000144 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattnered138932005-12-13 06:32:10 +0000145 SwitchSection(StaticCtorsSection, 0);
146 EmitAlignment(2, 0);
147 EmitXXStructorList(GV->getInitializer());
148 return true;
149 }
150
Chris Lattner5166b822006-01-12 19:17:23 +0000151 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattnered138932005-12-13 06:32:10 +0000152 SwitchSection(StaticDtorsSection, 0);
153 EmitAlignment(2, 0);
154 EmitXXStructorList(GV->getInitializer());
155 return true;
156 }
157
158 return false;
159}
160
161/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
162/// function pointers, ignoring the init priority.
163void AsmPrinter::EmitXXStructorList(Constant *List) {
164 // Should be an array of '{ int, void ()* }' structs. The first value is the
165 // init priority, which we ignore.
166 if (!isa<ConstantArray>(List)) return;
167 ConstantArray *InitList = cast<ConstantArray>(List);
168 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
169 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
170 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000171
172 if (CS->getOperand(1)->isNullValue())
173 return; // Found a null terminator, exit printing.
174 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000175 EmitGlobalConstant(CS->getOperand(1));
176 }
177}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000178
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000179/// getPreferredAlignmentLog - Return the preferred alignment of the
180/// specified global, returned in log form. This includes an explicitly
181/// requested alignment (if the global has one).
182unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
183 unsigned Alignment = TM.getTargetData().getTypeAlignmentShift(GV->getType());
184 if (GV->getAlignment() > (1U << Alignment))
185 Alignment = Log2_32(GV->getAlignment());
186
Chris Lattner519ea2a2006-02-05 01:46:49 +0000187 if (GV->hasInitializer()) {
188 // Always round up alignment of global doubles to 8 bytes.
189 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
190 Alignment = 3;
191 if (Alignment < 4) {
192 // If the global is not external, see if it is large. If so, give it a
193 // larger alignment.
194 if (TM.getTargetData().getTypeSize(GV->getType()->getElementType()) > 128)
195 Alignment = 4; // 16-byte alignment.
196 }
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000197 }
198 return Alignment;
199}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000200
Chris Lattner25045bd2005-11-21 07:51:36 +0000201// EmitAlignment - Emit an alignment directive to the specified power of two.
202void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnera1ab72d2005-11-14 19:00:06 +0000203 if (GV && GV->getAlignment())
204 NumBits = Log2_32(GV->getAlignment());
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000205 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattnerbfddc202004-08-17 19:14:29 +0000206 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
207 O << AlignDirective << NumBits << "\n";
208}
209
Chris Lattner25045bd2005-11-21 07:51:36 +0000210/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000211///
Chris Lattner25045bd2005-11-21 07:51:36 +0000212void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000213 if (NumZeros) {
214 if (ZeroDirective)
215 O << ZeroDirective << NumZeros << "\n";
216 else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000217 for (; NumZeros; --NumZeros)
218 O << Data8bitsDirective << "0\n";
219 }
220 }
221}
222
Chris Lattnera80ba712004-08-16 23:15:22 +0000223// Print out the specified constant, without a storage class. Only the
224// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000225void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000226 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000227 O << "0";
228 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
229 assert(CB == ConstantBool::True);
230 O << "1";
231 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
232 if (((CI->getValue() << 32) >> 32) == CI->getValue())
233 O << CI->getValue();
234 else
Duraid Madina45606572005-05-15 13:05:48 +0000235 O << (uint64_t)CI->getValue();
Chris Lattnera80ba712004-08-16 23:15:22 +0000236 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
237 O << CI->getValue();
Chris Lattner450de392005-11-10 18:36:17 +0000238 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000239 // This is a constant address for a global variable or function. Use the
240 // name of the variable or function as the address value, possibly
241 // decorating it with GlobalVarAddrPrefix/Suffix or
242 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattner450de392005-11-10 18:36:17 +0000243 if (isa<Function>(GV))
244 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina855a5192005-04-02 12:21:51 +0000245 else
Chris Lattner450de392005-11-10 18:36:17 +0000246 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina855a5192005-04-02 12:21:51 +0000247 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000248 const TargetData &TD = TM.getTargetData();
249 switch(CE->getOpcode()) {
250 case Instruction::GetElementPtr: {
251 // generate a symbolic expression for the byte address
252 const Constant *ptrVal = CE->getOperand(0);
253 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner27e19212005-02-14 21:40:26 +0000254 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
255 if (Offset)
256 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000257 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000258 if (Offset > 0)
259 O << ") + " << Offset;
260 else if (Offset < 0)
261 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000262 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000263 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000264 }
265 break;
266 }
267 case Instruction::Cast: {
268 // Support only non-converting or widening casts for now, that is, ones
269 // that do not involve a change in value. This assertion is really gross,
270 // and may not even be a complete check.
271 Constant *Op = CE->getOperand(0);
272 const Type *OpTy = Op->getType(), *Ty = CE->getType();
273
274 // Remember, kids, pointers can be losslessly converted back and forth
275 // into 32-bit or wider integers, regardless of signedness. :-P
276 assert(((isa<PointerType>(OpTy)
277 && (Ty == Type::LongTy || Ty == Type::ULongTy
278 || Ty == Type::IntTy || Ty == Type::UIntTy))
279 || (isa<PointerType>(Ty)
280 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
281 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
282 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
283 && OpTy->isLosslesslyConvertibleTo(Ty))))
284 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000285 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000286 break;
287 }
288 case Instruction::Add:
289 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000290 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattnera80ba712004-08-16 23:15:22 +0000291 O << ") + (";
Chris Lattner25045bd2005-11-21 07:51:36 +0000292 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000293 O << ")";
294 break;
295 default:
296 assert(0 && "Unsupported operator!");
297 }
298 } else {
299 assert(0 && "Unknown constant value!");
300 }
301}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000302
303/// toOctal - Convert the low order bits of X into an octal digit.
304///
305static inline char toOctal(int X) {
306 return (X&7)+'0';
307}
308
Chris Lattner2980cef2005-11-10 18:06:33 +0000309/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000310/// the predicate isString is true.
311///
Chris Lattner2980cef2005-11-10 18:06:33 +0000312static void printAsCString(std::ostream &O, const ConstantArray *CVA,
313 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000314 assert(CVA->isString() && "Array is not string compatible!");
315
316 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000317 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000318 unsigned char C =
Chris Lattnerdea18b62005-01-08 19:59:10 +0000319 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000320
321 if (C == '"') {
322 O << "\\\"";
323 } else if (C == '\\') {
324 O << "\\\\";
325 } else if (isprint(C)) {
326 O << C;
327 } else {
328 switch(C) {
329 case '\b': O << "\\b"; break;
330 case '\f': O << "\\f"; break;
331 case '\n': O << "\\n"; break;
332 case '\r': O << "\\r"; break;
333 case '\t': O << "\\t"; break;
334 default:
335 O << '\\';
336 O << toOctal(C >> 6);
337 O << toOctal(C >> 3);
338 O << toOctal(C >> 0);
339 break;
340 }
341 }
342 }
343 O << "\"";
344}
345
Chris Lattner25045bd2005-11-21 07:51:36 +0000346/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner1b7e2352004-08-17 06:36:49 +0000347///
Chris Lattner25045bd2005-11-21 07:51:36 +0000348void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000349 const TargetData &TD = TM.getTargetData();
350
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000351 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattner25045bd2005-11-21 07:51:36 +0000352 EmitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000353 return;
354 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
355 if (CVA->isString()) {
Chris Lattner2980cef2005-11-10 18:06:33 +0000356 unsigned NumElts = CVA->getNumOperands();
357 if (AscizDirective && NumElts &&
358 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
359 O << AscizDirective;
360 printAsCString(O, CVA, NumElts-1);
361 } else {
362 O << AsciiDirective;
363 printAsCString(O, CVA, NumElts);
364 }
Chris Lattner1b7e2352004-08-17 06:36:49 +0000365 O << "\n";
366 } else { // Not a string. Print the values in successive locations
367 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner25045bd2005-11-21 07:51:36 +0000368 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000369 }
370 return;
371 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
372 // Print the fields in successive locations. Pad to align if needed!
373 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000374 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000375 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
376 const Constant* field = CVS->getOperand(i);
377
378 // Check if padding is needed and insert one or more 0s.
Chris Lattnerdea18b62005-01-08 19:59:10 +0000379 uint64_t fieldSize = TD.getTypeSize(field->getType());
380 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner1b7e2352004-08-17 06:36:49 +0000381 : cvsLayout->MemberOffsets[i+1])
382 - cvsLayout->MemberOffsets[i]) - fieldSize;
383 sizeSoFar += fieldSize + padSize;
384
385 // Now print the actual field value
Chris Lattner25045bd2005-11-21 07:51:36 +0000386 EmitGlobalConstant(field);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000387
388 // Insert the field padding unless it's zero bytes...
Chris Lattner25045bd2005-11-21 07:51:36 +0000389 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000390 }
391 assert(sizeSoFar == cvsLayout->StructSize &&
392 "Layout of constant struct may be incorrect!");
393 return;
394 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
395 // FP Constants are printed as integer constants to avoid losing
396 // precision...
397 double Val = CFP->getValue();
398 if (CFP->getType() == Type::DoubleTy) {
Chris Lattnere85a5a92004-08-17 06:48:16 +0000399 if (Data64bitsDirective)
Jim Laskeycb6682f2005-08-17 19:34:49 +0000400 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattner7d057a32004-08-17 21:38:40 +0000401 << " double value: " << Val << "\n";
Chris Lattnere85a5a92004-08-17 06:48:16 +0000402 else if (TD.isBigEndian()) {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000403 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000404 << "\t" << CommentString << " double most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000405 << Val << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000406 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000407 << "\t" << CommentString << " double least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000408 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000409 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000410 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000411 << "\t" << CommentString << " double least significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000412 << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000413 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000414 << "\t" << CommentString << " double most significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000415 << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000416 }
417 return;
418 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000419 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattner0554fb62004-08-17 16:27:05 +0000420 << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000421 return;
422 }
423 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
424 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
425 uint64_t Val = CI->getRawValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000426
Chris Lattnere85a5a92004-08-17 06:48:16 +0000427 if (Data64bitsDirective)
428 O << Data64bitsDirective << Val << "\n";
429 else if (TD.isBigEndian()) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000430 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000431 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000432 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000433 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000434 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000435 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000436 } else {
437 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000438 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000439 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000440 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000441 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000442 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000443 }
444 return;
445 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000446 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
447 const PackedType *PTy = CP->getType();
448
449 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
450 EmitGlobalConstant(CP->getOperand(I));
451
452 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000453 }
454
455 const Type *type = CV->getType();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000456 switch (type->getTypeID()) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000457 case Type::BoolTyID:
Chris Lattner1b7e2352004-08-17 06:36:49 +0000458 case Type::UByteTyID: case Type::SByteTyID:
459 O << Data8bitsDirective;
460 break;
461 case Type::UShortTyID: case Type::ShortTyID:
462 O << Data16bitsDirective;
463 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000464 case Type::PointerTyID:
Andrew Lenharth571c9c32005-02-04 13:47:16 +0000465 if (TD.getPointerSize() == 8) {
466 O << Data64bitsDirective;
467 break;
468 }
469 //Fall through for pointer size == int size
Chris Lattner1b7e2352004-08-17 06:36:49 +0000470 case Type::UIntTyID: case Type::IntTyID:
471 O << Data32bitsDirective;
472 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000473 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner660538c2005-08-08 04:26:32 +0000474 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
475 O << Data64bitsDirective;
476 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000477 case Type::FloatTyID: case Type::DoubleTyID:
478 assert (0 && "Should have already output floating point constant.");
479 default:
480 assert (0 && "Can't handle printing this type of thing");
481 break;
482 }
Chris Lattner25045bd2005-11-21 07:51:36 +0000483 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000484 O << "\n";
485}
Chris Lattner0264d1a2006-01-27 02:10:10 +0000486
487/// printInlineAsm - This method formats and prints the specified machine
488/// instruction that is an inline asm.
489void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerd6c65ea2006-02-08 23:41:56 +0000490 O << InlineAsmStart;
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000491 unsigned NumOperands = MI->getNumOperands();
492
493 // Count the number of register definitions.
494 unsigned NumDefs = 0;
495 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
496 assert(NumDefs != NumOperands-1 && "No asm string?");
497
498 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +0000499
500 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000501 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +0000502
503 // The variant of the current asmprinter: FIXME: change.
504 int AsmPrinterVariant = 0;
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000505
Chris Lattner66099132006-02-01 22:41:11 +0000506 int CurVariant = -1; // The number of the {.|.|.} region we are in.
507 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +0000508
Chris Lattner66099132006-02-01 22:41:11 +0000509 while (*LastEmitted) {
510 switch (*LastEmitted) {
511 default: {
512 // Not a special case, emit the string section literally.
513 const char *LiteralEnd = LastEmitted+1;
514 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
515 *LiteralEnd != '}' && *LiteralEnd != '$')
516 ++LiteralEnd;
517 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
518 O.write(LastEmitted, LiteralEnd-LastEmitted);
519 LastEmitted = LiteralEnd;
520 break;
521 }
522 case '$': {
523 ++LastEmitted; // Consume '$' character.
524 if (*LastEmitted == '$') { // $$ -> $
525 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
526 O << '$';
527 ++LastEmitted; // Consume second '$' character.
528 break;
529 }
530
531 bool HasCurlyBraces = false;
532 if (*LastEmitted == '{') { // ${variable}
533 ++LastEmitted; // Consume '{' character.
534 HasCurlyBraces = true;
535 }
536
537 const char *IDStart = LastEmitted;
538 char *IDEnd;
539 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
540 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
541 std::cerr << "Bad $ operand number in inline asm string: '"
542 << AsmStr << "'\n";
543 exit(1);
544 }
545 LastEmitted = IDEnd;
546
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000547 char Modifier[2] = { 0, 0 };
548
Chris Lattner66099132006-02-01 22:41:11 +0000549 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000550 // If we have curly braces, check for a modifier character. This
551 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
552 if (*LastEmitted == ':') {
553 ++LastEmitted; // Consume ':' character.
554 if (*LastEmitted == 0) {
555 std::cerr << "Bad ${:} expression in inline asm string: '"
556 << AsmStr << "'\n";
557 exit(1);
558 }
559
560 Modifier[0] = *LastEmitted;
561 ++LastEmitted; // Consume modifier character.
562 }
563
Chris Lattner66099132006-02-01 22:41:11 +0000564 if (*LastEmitted != '}') {
565 std::cerr << "Bad ${} expression in inline asm string: '"
566 << AsmStr << "'\n";
567 exit(1);
568 }
569 ++LastEmitted; // Consume '}' character.
570 }
571
572 if ((unsigned)Val >= NumOperands-1) {
573 std::cerr << "Invalid $ operand number in inline asm string: '"
574 << AsmStr << "'\n";
575 exit(1);
576 }
577
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000578 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +0000579 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000580 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
581 unsigned OpNo = 1;
582
583 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000584 for (; Val; --Val) {
585 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
586 OpNo += (OpFlags >> 3) + 1;
587 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000588
Chris Lattnerdd260332006-02-24 20:21:58 +0000589 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000590 ++OpNo; // Skip over the ID number.
Chris Lattnerdd260332006-02-24 20:21:58 +0000591
592 bool Error;
593 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
594 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
595 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
596 Modifier[0] ? Modifier : 0);
597 } else {
598 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
599 Modifier[0] ? Modifier : 0);
600 }
601 if (Error) {
Chris Lattner66099132006-02-01 22:41:11 +0000602 std::cerr << "Invalid operand found in inline asm: '"
603 << AsmStr << "'\n";
604 MI->dump();
605 exit(1);
606 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000607 }
Chris Lattner66099132006-02-01 22:41:11 +0000608 break;
609 }
610 case '{':
611 ++LastEmitted; // Consume '{' character.
612 if (CurVariant != -1) {
613 std::cerr << "Nested variants found in inline asm string: '"
614 << AsmStr << "'\n";
615 exit(1);
616 }
617 CurVariant = 0; // We're in the first variant now.
618 break;
619 case '|':
620 ++LastEmitted; // consume '|' character.
621 if (CurVariant == -1) {
622 std::cerr << "Found '|' character outside of variant in inline asm "
623 << "string: '" << AsmStr << "'\n";
624 exit(1);
625 }
626 ++CurVariant; // We're in the next variant.
627 break;
628 case '}':
629 ++LastEmitted; // consume '}' character.
630 if (CurVariant == -1) {
631 std::cerr << "Found '}' character outside of variant in inline asm "
632 << "string: '" << AsmStr << "'\n";
633 exit(1);
634 }
635 CurVariant = -1;
636 break;
637 }
638 }
Chris Lattnerd6c65ea2006-02-08 23:41:56 +0000639 O << "\n" << InlineAsmEnd;
Chris Lattner66099132006-02-01 22:41:11 +0000640}
641
642/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
643/// instruction, using the specified assembler variant. Targets should
644/// overried this to format as appropriate.
645bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000646 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +0000647 // Target doesn't support this yet!
648 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +0000649}
Chris Lattnerdd260332006-02-24 20:21:58 +0000650
651bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
652 unsigned AsmVariant,
653 const char *ExtraCode) {
654 // Target doesn't support this yet!
655 return true;
656}