blob: 263b35cf4d2bce86175e317bce0d8ad9c1917117 [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
Nate Begeman8cfa57b2005-12-06 06:18:55 +000014#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000015#include "llvm/CodeGen/AsmPrinter.h"
16#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000017#include "llvm/Module.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000018#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000019#include "llvm/Support/Mangler.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000020#include "llvm/Support/MathExtras.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000021#include "llvm/Target/TargetMachine.h"
Duraid Madina2e096c12005-12-28 06:29:02 +000022#include <iostream>
Chris Lattner66099132006-02-01 22:41:11 +000023#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000024using namespace llvm;
25
Chris Lattnered138932005-12-13 06:32:10 +000026AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm)
27: FunctionNumber(0), O(o), TM(tm),
28 CommentString("#"),
29 GlobalPrefix(""),
30 PrivateGlobalPrefix("."),
31 GlobalVarAddrPrefix(""),
32 GlobalVarAddrSuffix(""),
33 FunctionAddrPrefix(""),
34 FunctionAddrSuffix(""),
Chris Lattnerd6c65ea2006-02-08 23:41:56 +000035 InlineAsmStart("#APP\n"),
36 InlineAsmEnd("#NO_APP\n"),
Chris Lattnered138932005-12-13 06:32:10 +000037 ZeroDirective("\t.zero\t"),
38 AsciiDirective("\t.ascii\t"),
39 AscizDirective("\t.asciz\t"),
40 Data8bitsDirective("\t.byte\t"),
41 Data16bitsDirective("\t.short\t"),
42 Data32bitsDirective("\t.long\t"),
43 Data64bitsDirective("\t.quad\t"),
44 AlignDirective("\t.align\t"),
45 AlignmentIsInBytes(true),
46 SwitchToSectionDirective("\t.section\t"),
47 ConstantPoolSection("\t.section .rodata\n"),
48 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
49 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
50 LCOMMDirective(0),
51 COMMDirective("\t.comm\t"),
52 COMMDirectiveTakesAlignment(true),
53 HasDotTypeDotSizeDirective(true) {
54}
55
56
Chris Lattnerac28fbd2005-11-21 07:06:27 +000057/// SwitchSection - Switch to the specified section of the executable if we
58/// are not already in it!
59///
60void AsmPrinter::SwitchSection(const char *NewSection, const GlobalValue *GV) {
61 std::string NS;
62
63 if (GV && GV->hasSection())
Chris Lattner3b4fd322005-11-21 08:25:09 +000064 NS = SwitchToSectionDirective + GV->getSection();
Chris Lattnerac28fbd2005-11-21 07:06:27 +000065 else
Chris Lattner42a80fe2005-12-09 19:28:49 +000066 NS = std::string("\t")+NewSection;
Chris Lattnerac28fbd2005-11-21 07:06:27 +000067
68 if (CurrentSection != NS) {
69 CurrentSection = NS;
70 if (!CurrentSection.empty())
Chris Lattner42a80fe2005-12-09 19:28:49 +000071 O << CurrentSection << '\n';
Chris Lattnerac28fbd2005-11-21 07:06:27 +000072 }
73}
74
Chris Lattnera80ba712004-08-16 23:15:22 +000075bool AsmPrinter::doInitialization(Module &M) {
Chris Lattneraf2bf0a2004-08-17 06:06:19 +000076 Mang = new Mangler(M, GlobalPrefix);
Chris Lattner2c1b1592006-01-23 23:47:53 +000077
Chris Lattner3e2fa7a2006-01-24 04:16:34 +000078 if (!M.getModuleInlineAsm().empty())
79 O << CommentString << " Start of file scope inline assembly\n"
80 << M.getModuleInlineAsm()
81 << "\n" << CommentString << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +000082
Chris Lattnerac28fbd2005-11-21 07:06:27 +000083 SwitchSection("", 0); // Reset back to no section.
Jim Laskeyb3e789a2006-01-26 20:21:46 +000084
85 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
86 DebugInfo->AnalyzeModule(M);
87 }
88
Chris Lattnera80ba712004-08-16 23:15:22 +000089 return false;
90}
91
92bool AsmPrinter::doFinalization(Module &M) {
93 delete Mang; Mang = 0;
94 return false;
95}
96
Chris Lattner25045bd2005-11-21 07:51:36 +000097void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +000098 // What's my mangled name?
Chris Lattner450de392005-11-10 18:36:17 +000099 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner77bc2282005-11-21 08:13:27 +0000100 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +0000101}
102
Chris Lattner3b4fd322005-11-21 08:25:09 +0000103/// EmitConstantPool - Print to the current output stream assembly
104/// representations of the constants in the constant pool MCP. This is
105/// used to print out constants which have been "spilled to memory" by
106/// the code generator.
107///
108void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Evan Chengb8973bd2006-01-31 22:23:14 +0000109 const std::vector<std::pair<Constant*, unsigned> > &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000110 if (CP.empty()) return;
111 const TargetData &TD = TM.getTargetData();
112
113 SwitchSection(ConstantPoolSection, 0);
114 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
115 // FIXME: force doubles to be naturally aligned. We should handle this
116 // more correctly in the future.
Evan Chengb8973bd2006-01-31 22:23:14 +0000117 unsigned Alignment = CP[i].second;
118 if (Alignment == 0) {
119 Alignment = TD.getTypeAlignmentShift(CP[i].first->getType());
120 if (CP[i].first->getType() == Type::DoubleTy && Alignment < 3)
121 Alignment = 3;
122 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000123
124 EmitAlignment(Alignment);
125 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
Evan Chengb8973bd2006-01-31 22:23:14 +0000126 << ":\t\t\t\t\t" << CommentString << *CP[i].first << '\n';
127 EmitGlobalConstant(CP[i].first);
Chris Lattner3b4fd322005-11-21 08:25:09 +0000128 }
129}
130
Chris Lattnered138932005-12-13 06:32:10 +0000131/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
132/// special global used by LLVM. If so, emit it and return true, otherwise
133/// do nothing and return false.
134bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
135 assert(GV->hasInitializer() && GV->hasAppendingLinkage() &&
136 "Not a special LLVM global!");
137
138 if (GV->getName() == "llvm.used")
139 return true; // No need to emit this at all.
140
Chris Lattner5166b822006-01-12 19:17:23 +0000141 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattnered138932005-12-13 06:32:10 +0000142 SwitchSection(StaticCtorsSection, 0);
143 EmitAlignment(2, 0);
144 EmitXXStructorList(GV->getInitializer());
145 return true;
146 }
147
Chris Lattner5166b822006-01-12 19:17:23 +0000148 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattnered138932005-12-13 06:32:10 +0000149 SwitchSection(StaticDtorsSection, 0);
150 EmitAlignment(2, 0);
151 EmitXXStructorList(GV->getInitializer());
152 return true;
153 }
154
155 return false;
156}
157
158/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
159/// function pointers, ignoring the init priority.
160void AsmPrinter::EmitXXStructorList(Constant *List) {
161 // Should be an array of '{ int, void ()* }' structs. The first value is the
162 // init priority, which we ignore.
163 if (!isa<ConstantArray>(List)) return;
164 ConstantArray *InitList = cast<ConstantArray>(List);
165 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
166 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
167 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000168
169 if (CS->getOperand(1)->isNullValue())
170 return; // Found a null terminator, exit printing.
171 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000172 EmitGlobalConstant(CS->getOperand(1));
173 }
174}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000175
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000176/// getPreferredAlignmentLog - Return the preferred alignment of the
177/// specified global, returned in log form. This includes an explicitly
178/// requested alignment (if the global has one).
179unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
180 unsigned Alignment = TM.getTargetData().getTypeAlignmentShift(GV->getType());
181 if (GV->getAlignment() > (1U << Alignment))
182 Alignment = Log2_32(GV->getAlignment());
183
Chris Lattner519ea2a2006-02-05 01:46:49 +0000184 if (GV->hasInitializer()) {
185 // Always round up alignment of global doubles to 8 bytes.
186 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
187 Alignment = 3;
188 if (Alignment < 4) {
189 // If the global is not external, see if it is large. If so, give it a
190 // larger alignment.
191 if (TM.getTargetData().getTypeSize(GV->getType()->getElementType()) > 128)
192 Alignment = 4; // 16-byte alignment.
193 }
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000194 }
195 return Alignment;
196}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000197
Chris Lattner25045bd2005-11-21 07:51:36 +0000198// EmitAlignment - Emit an alignment directive to the specified power of two.
199void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnera1ab72d2005-11-14 19:00:06 +0000200 if (GV && GV->getAlignment())
201 NumBits = Log2_32(GV->getAlignment());
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000202 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattnerbfddc202004-08-17 19:14:29 +0000203 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
204 O << AlignDirective << NumBits << "\n";
205}
206
Chris Lattner25045bd2005-11-21 07:51:36 +0000207/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000208///
Chris Lattner25045bd2005-11-21 07:51:36 +0000209void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000210 if (NumZeros) {
211 if (ZeroDirective)
212 O << ZeroDirective << NumZeros << "\n";
213 else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000214 for (; NumZeros; --NumZeros)
215 O << Data8bitsDirective << "0\n";
216 }
217 }
218}
219
Chris Lattnera80ba712004-08-16 23:15:22 +0000220// Print out the specified constant, without a storage class. Only the
221// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000222void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000223 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000224 O << "0";
225 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
226 assert(CB == ConstantBool::True);
227 O << "1";
228 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
229 if (((CI->getValue() << 32) >> 32) == CI->getValue())
230 O << CI->getValue();
231 else
Duraid Madina45606572005-05-15 13:05:48 +0000232 O << (uint64_t)CI->getValue();
Chris Lattnera80ba712004-08-16 23:15:22 +0000233 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
234 O << CI->getValue();
Chris Lattner450de392005-11-10 18:36:17 +0000235 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000236 // This is a constant address for a global variable or function. Use the
237 // name of the variable or function as the address value, possibly
238 // decorating it with GlobalVarAddrPrefix/Suffix or
239 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattner450de392005-11-10 18:36:17 +0000240 if (isa<Function>(GV))
241 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina855a5192005-04-02 12:21:51 +0000242 else
Chris Lattner450de392005-11-10 18:36:17 +0000243 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina855a5192005-04-02 12:21:51 +0000244 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000245 const TargetData &TD = TM.getTargetData();
246 switch(CE->getOpcode()) {
247 case Instruction::GetElementPtr: {
248 // generate a symbolic expression for the byte address
249 const Constant *ptrVal = CE->getOperand(0);
250 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner27e19212005-02-14 21:40:26 +0000251 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
252 if (Offset)
253 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000254 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000255 if (Offset > 0)
256 O << ") + " << Offset;
257 else if (Offset < 0)
258 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000259 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000260 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000261 }
262 break;
263 }
264 case Instruction::Cast: {
265 // Support only non-converting or widening casts for now, that is, ones
266 // that do not involve a change in value. This assertion is really gross,
267 // and may not even be a complete check.
268 Constant *Op = CE->getOperand(0);
269 const Type *OpTy = Op->getType(), *Ty = CE->getType();
270
271 // Remember, kids, pointers can be losslessly converted back and forth
272 // into 32-bit or wider integers, regardless of signedness. :-P
273 assert(((isa<PointerType>(OpTy)
274 && (Ty == Type::LongTy || Ty == Type::ULongTy
275 || Ty == Type::IntTy || Ty == Type::UIntTy))
276 || (isa<PointerType>(Ty)
277 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
278 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
279 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
280 && OpTy->isLosslesslyConvertibleTo(Ty))))
281 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000282 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000283 break;
284 }
285 case Instruction::Add:
286 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000287 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattnera80ba712004-08-16 23:15:22 +0000288 O << ") + (";
Chris Lattner25045bd2005-11-21 07:51:36 +0000289 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000290 O << ")";
291 break;
292 default:
293 assert(0 && "Unsupported operator!");
294 }
295 } else {
296 assert(0 && "Unknown constant value!");
297 }
298}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000299
300/// toOctal - Convert the low order bits of X into an octal digit.
301///
302static inline char toOctal(int X) {
303 return (X&7)+'0';
304}
305
Chris Lattner2980cef2005-11-10 18:06:33 +0000306/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000307/// the predicate isString is true.
308///
Chris Lattner2980cef2005-11-10 18:06:33 +0000309static void printAsCString(std::ostream &O, const ConstantArray *CVA,
310 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000311 assert(CVA->isString() && "Array is not string compatible!");
312
313 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000314 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000315 unsigned char C =
Chris Lattnerdea18b62005-01-08 19:59:10 +0000316 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000317
318 if (C == '"') {
319 O << "\\\"";
320 } else if (C == '\\') {
321 O << "\\\\";
322 } else if (isprint(C)) {
323 O << C;
324 } else {
325 switch(C) {
326 case '\b': O << "\\b"; break;
327 case '\f': O << "\\f"; break;
328 case '\n': O << "\\n"; break;
329 case '\r': O << "\\r"; break;
330 case '\t': O << "\\t"; break;
331 default:
332 O << '\\';
333 O << toOctal(C >> 6);
334 O << toOctal(C >> 3);
335 O << toOctal(C >> 0);
336 break;
337 }
338 }
339 }
340 O << "\"";
341}
342
Chris Lattner25045bd2005-11-21 07:51:36 +0000343/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner1b7e2352004-08-17 06:36:49 +0000344///
Chris Lattner25045bd2005-11-21 07:51:36 +0000345void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000346 const TargetData &TD = TM.getTargetData();
347
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000348 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattner25045bd2005-11-21 07:51:36 +0000349 EmitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000350 return;
351 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
352 if (CVA->isString()) {
Chris Lattner2980cef2005-11-10 18:06:33 +0000353 unsigned NumElts = CVA->getNumOperands();
354 if (AscizDirective && NumElts &&
355 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
356 O << AscizDirective;
357 printAsCString(O, CVA, NumElts-1);
358 } else {
359 O << AsciiDirective;
360 printAsCString(O, CVA, NumElts);
361 }
Chris Lattner1b7e2352004-08-17 06:36:49 +0000362 O << "\n";
363 } else { // Not a string. Print the values in successive locations
364 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner25045bd2005-11-21 07:51:36 +0000365 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000366 }
367 return;
368 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
369 // Print the fields in successive locations. Pad to align if needed!
370 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000371 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000372 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
373 const Constant* field = CVS->getOperand(i);
374
375 // Check if padding is needed and insert one or more 0s.
Chris Lattnerdea18b62005-01-08 19:59:10 +0000376 uint64_t fieldSize = TD.getTypeSize(field->getType());
377 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner1b7e2352004-08-17 06:36:49 +0000378 : cvsLayout->MemberOffsets[i+1])
379 - cvsLayout->MemberOffsets[i]) - fieldSize;
380 sizeSoFar += fieldSize + padSize;
381
382 // Now print the actual field value
Chris Lattner25045bd2005-11-21 07:51:36 +0000383 EmitGlobalConstant(field);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000384
385 // Insert the field padding unless it's zero bytes...
Chris Lattner25045bd2005-11-21 07:51:36 +0000386 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000387 }
388 assert(sizeSoFar == cvsLayout->StructSize &&
389 "Layout of constant struct may be incorrect!");
390 return;
391 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
392 // FP Constants are printed as integer constants to avoid losing
393 // precision...
394 double Val = CFP->getValue();
395 if (CFP->getType() == Type::DoubleTy) {
Chris Lattnere85a5a92004-08-17 06:48:16 +0000396 if (Data64bitsDirective)
Jim Laskeycb6682f2005-08-17 19:34:49 +0000397 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattner7d057a32004-08-17 21:38:40 +0000398 << " double value: " << Val << "\n";
Chris Lattnere85a5a92004-08-17 06:48:16 +0000399 else if (TD.isBigEndian()) {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000400 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000401 << "\t" << CommentString << " double most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000402 << Val << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000403 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000404 << "\t" << CommentString << " double least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000405 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000406 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000407 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000408 << "\t" << CommentString << " double least significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000409 << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000410 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000411 << "\t" << CommentString << " double most significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000412 << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000413 }
414 return;
415 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000416 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattner0554fb62004-08-17 16:27:05 +0000417 << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000418 return;
419 }
420 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
421 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
422 uint64_t Val = CI->getRawValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000423
Chris Lattnere85a5a92004-08-17 06:48:16 +0000424 if (Data64bitsDirective)
425 O << Data64bitsDirective << Val << "\n";
426 else if (TD.isBigEndian()) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000427 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000428 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000429 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000430 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000431 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000432 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000433 } else {
434 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000435 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000436 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000437 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000438 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000439 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000440 }
441 return;
442 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000443 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
444 const PackedType *PTy = CP->getType();
445
446 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
447 EmitGlobalConstant(CP->getOperand(I));
448
449 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000450 }
451
452 const Type *type = CV->getType();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000453 switch (type->getTypeID()) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000454 case Type::BoolTyID:
Chris Lattner1b7e2352004-08-17 06:36:49 +0000455 case Type::UByteTyID: case Type::SByteTyID:
456 O << Data8bitsDirective;
457 break;
458 case Type::UShortTyID: case Type::ShortTyID:
459 O << Data16bitsDirective;
460 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000461 case Type::PointerTyID:
Andrew Lenharth571c9c32005-02-04 13:47:16 +0000462 if (TD.getPointerSize() == 8) {
463 O << Data64bitsDirective;
464 break;
465 }
466 //Fall through for pointer size == int size
Chris Lattner1b7e2352004-08-17 06:36:49 +0000467 case Type::UIntTyID: case Type::IntTyID:
468 O << Data32bitsDirective;
469 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000470 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner660538c2005-08-08 04:26:32 +0000471 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
472 O << Data64bitsDirective;
473 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000474 case Type::FloatTyID: case Type::DoubleTyID:
475 assert (0 && "Should have already output floating point constant.");
476 default:
477 assert (0 && "Can't handle printing this type of thing");
478 break;
479 }
Chris Lattner25045bd2005-11-21 07:51:36 +0000480 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000481 O << "\n";
482}
Chris Lattner0264d1a2006-01-27 02:10:10 +0000483
484/// printInlineAsm - This method formats and prints the specified machine
485/// instruction that is an inline asm.
486void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerd6c65ea2006-02-08 23:41:56 +0000487 O << InlineAsmStart;
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000488 unsigned NumOperands = MI->getNumOperands();
489
490 // Count the number of register definitions.
491 unsigned NumDefs = 0;
492 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
493 assert(NumDefs != NumOperands-1 && "No asm string?");
494
495 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +0000496
497 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000498 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +0000499
500 // The variant of the current asmprinter: FIXME: change.
501 int AsmPrinterVariant = 0;
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000502
Chris Lattner66099132006-02-01 22:41:11 +0000503 int CurVariant = -1; // The number of the {.|.|.} region we are in.
504 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +0000505
Chris Lattner66099132006-02-01 22:41:11 +0000506 while (*LastEmitted) {
507 switch (*LastEmitted) {
508 default: {
509 // Not a special case, emit the string section literally.
510 const char *LiteralEnd = LastEmitted+1;
511 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
512 *LiteralEnd != '}' && *LiteralEnd != '$')
513 ++LiteralEnd;
514 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
515 O.write(LastEmitted, LiteralEnd-LastEmitted);
516 LastEmitted = LiteralEnd;
517 break;
518 }
519 case '$': {
520 ++LastEmitted; // Consume '$' character.
521 if (*LastEmitted == '$') { // $$ -> $
522 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
523 O << '$';
524 ++LastEmitted; // Consume second '$' character.
525 break;
526 }
527
528 bool HasCurlyBraces = false;
529 if (*LastEmitted == '{') { // ${variable}
530 ++LastEmitted; // Consume '{' character.
531 HasCurlyBraces = true;
532 }
533
534 const char *IDStart = LastEmitted;
535 char *IDEnd;
536 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
537 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
538 std::cerr << "Bad $ operand number in inline asm string: '"
539 << AsmStr << "'\n";
540 exit(1);
541 }
542 LastEmitted = IDEnd;
543
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000544 char Modifier[2] = { 0, 0 };
545
Chris Lattner66099132006-02-01 22:41:11 +0000546 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000547 // If we have curly braces, check for a modifier character. This
548 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
549 if (*LastEmitted == ':') {
550 ++LastEmitted; // Consume ':' character.
551 if (*LastEmitted == 0) {
552 std::cerr << "Bad ${:} expression in inline asm string: '"
553 << AsmStr << "'\n";
554 exit(1);
555 }
556
557 Modifier[0] = *LastEmitted;
558 ++LastEmitted; // Consume modifier character.
559 }
560
Chris Lattner66099132006-02-01 22:41:11 +0000561 if (*LastEmitted != '}') {
562 std::cerr << "Bad ${} expression in inline asm string: '"
563 << AsmStr << "'\n";
564 exit(1);
565 }
566 ++LastEmitted; // Consume '}' character.
567 }
568
569 if ((unsigned)Val >= NumOperands-1) {
570 std::cerr << "Invalid $ operand number in inline asm string: '"
571 << AsmStr << "'\n";
572 exit(1);
573 }
574
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000575 char ExtraCode = 0; // FIXME:
576
Chris Lattner66099132006-02-01 22:41:11 +0000577 // Okay, we finally have an operand number. Ask the target to print this
578 // operand!
579 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
580 if (const_cast<AsmPrinter*>(this)->
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000581 PrintAsmOperand(MI, Val+1, AsmPrinterVariant,
582 Modifier[0] ? Modifier : 0)) {
Chris Lattner66099132006-02-01 22:41:11 +0000583 std::cerr << "Invalid operand found in inline asm: '"
584 << AsmStr << "'\n";
585 MI->dump();
586 exit(1);
587 }
588 break;
589 }
590 case '{':
591 ++LastEmitted; // Consume '{' character.
592 if (CurVariant != -1) {
593 std::cerr << "Nested variants found in inline asm string: '"
594 << AsmStr << "'\n";
595 exit(1);
596 }
597 CurVariant = 0; // We're in the first variant now.
598 break;
599 case '|':
600 ++LastEmitted; // consume '|' character.
601 if (CurVariant == -1) {
602 std::cerr << "Found '|' character outside of variant in inline asm "
603 << "string: '" << AsmStr << "'\n";
604 exit(1);
605 }
606 ++CurVariant; // We're in the next variant.
607 break;
608 case '}':
609 ++LastEmitted; // consume '}' character.
610 if (CurVariant == -1) {
611 std::cerr << "Found '}' character outside of variant in inline asm "
612 << "string: '" << AsmStr << "'\n";
613 exit(1);
614 }
615 CurVariant = -1;
616 break;
617 }
618 }
Chris Lattnerd6c65ea2006-02-08 23:41:56 +0000619 O << "\n" << InlineAsmEnd;
Chris Lattner66099132006-02-01 22:41:11 +0000620}
621
622/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
623/// instruction, using the specified assembler variant. Targets should
624/// overried this to format as appropriate.
625bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000626 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +0000627 // Target doesn't support this yet!
628 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +0000629}