blob: bd12c775e899be24c44455aea4b035a211ebeb42 [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) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000109 const std::vector<MachineConstantPoolEntry> &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) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000115 EmitAlignment(CP[i].Alignment);
Chris Lattner3b4fd322005-11-21 08:25:09 +0000116 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
Chris Lattnerfa77d432006-02-09 04:22:52 +0000117 << ":\t\t\t\t\t" << CommentString << *CP[i].Val << '\n';
118 EmitGlobalConstant(CP[i].Val);
Chris Lattner3b4fd322005-11-21 08:25:09 +0000119 }
120}
121
Chris Lattnered138932005-12-13 06:32:10 +0000122/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
123/// special global used by LLVM. If so, emit it and return true, otherwise
124/// do nothing and return false.
125bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
126 assert(GV->hasInitializer() && GV->hasAppendingLinkage() &&
127 "Not a special LLVM global!");
128
129 if (GV->getName() == "llvm.used")
130 return true; // No need to emit this at all.
131
Chris Lattner5166b822006-01-12 19:17:23 +0000132 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattnered138932005-12-13 06:32:10 +0000133 SwitchSection(StaticCtorsSection, 0);
134 EmitAlignment(2, 0);
135 EmitXXStructorList(GV->getInitializer());
136 return true;
137 }
138
Chris Lattner5166b822006-01-12 19:17:23 +0000139 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattnered138932005-12-13 06:32:10 +0000140 SwitchSection(StaticDtorsSection, 0);
141 EmitAlignment(2, 0);
142 EmitXXStructorList(GV->getInitializer());
143 return true;
144 }
145
146 return false;
147}
148
149/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
150/// function pointers, ignoring the init priority.
151void AsmPrinter::EmitXXStructorList(Constant *List) {
152 // Should be an array of '{ int, void ()* }' structs. The first value is the
153 // init priority, which we ignore.
154 if (!isa<ConstantArray>(List)) return;
155 ConstantArray *InitList = cast<ConstantArray>(List);
156 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
157 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
158 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000159
160 if (CS->getOperand(1)->isNullValue())
161 return; // Found a null terminator, exit printing.
162 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000163 EmitGlobalConstant(CS->getOperand(1));
164 }
165}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000166
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000167/// getPreferredAlignmentLog - Return the preferred alignment of the
168/// specified global, returned in log form. This includes an explicitly
169/// requested alignment (if the global has one).
170unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
171 unsigned Alignment = TM.getTargetData().getTypeAlignmentShift(GV->getType());
172 if (GV->getAlignment() > (1U << Alignment))
173 Alignment = Log2_32(GV->getAlignment());
174
Chris Lattner519ea2a2006-02-05 01:46:49 +0000175 if (GV->hasInitializer()) {
176 // Always round up alignment of global doubles to 8 bytes.
177 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
178 Alignment = 3;
179 if (Alignment < 4) {
180 // If the global is not external, see if it is large. If so, give it a
181 // larger alignment.
182 if (TM.getTargetData().getTypeSize(GV->getType()->getElementType()) > 128)
183 Alignment = 4; // 16-byte alignment.
184 }
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000185 }
186 return Alignment;
187}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000188
Chris Lattner25045bd2005-11-21 07:51:36 +0000189// EmitAlignment - Emit an alignment directive to the specified power of two.
190void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnera1ab72d2005-11-14 19:00:06 +0000191 if (GV && GV->getAlignment())
192 NumBits = Log2_32(GV->getAlignment());
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000193 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattnerbfddc202004-08-17 19:14:29 +0000194 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
195 O << AlignDirective << NumBits << "\n";
196}
197
Chris Lattner25045bd2005-11-21 07:51:36 +0000198/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000199///
Chris Lattner25045bd2005-11-21 07:51:36 +0000200void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000201 if (NumZeros) {
202 if (ZeroDirective)
203 O << ZeroDirective << NumZeros << "\n";
204 else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000205 for (; NumZeros; --NumZeros)
206 O << Data8bitsDirective << "0\n";
207 }
208 }
209}
210
Chris Lattnera80ba712004-08-16 23:15:22 +0000211// Print out the specified constant, without a storage class. Only the
212// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000213void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000214 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000215 O << "0";
216 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
217 assert(CB == ConstantBool::True);
218 O << "1";
219 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
220 if (((CI->getValue() << 32) >> 32) == CI->getValue())
221 O << CI->getValue();
222 else
Duraid Madina45606572005-05-15 13:05:48 +0000223 O << (uint64_t)CI->getValue();
Chris Lattnera80ba712004-08-16 23:15:22 +0000224 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
225 O << CI->getValue();
Chris Lattner450de392005-11-10 18:36:17 +0000226 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000227 // This is a constant address for a global variable or function. Use the
228 // name of the variable or function as the address value, possibly
229 // decorating it with GlobalVarAddrPrefix/Suffix or
230 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattner450de392005-11-10 18:36:17 +0000231 if (isa<Function>(GV))
232 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina855a5192005-04-02 12:21:51 +0000233 else
Chris Lattner450de392005-11-10 18:36:17 +0000234 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina855a5192005-04-02 12:21:51 +0000235 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000236 const TargetData &TD = TM.getTargetData();
237 switch(CE->getOpcode()) {
238 case Instruction::GetElementPtr: {
239 // generate a symbolic expression for the byte address
240 const Constant *ptrVal = CE->getOperand(0);
241 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner27e19212005-02-14 21:40:26 +0000242 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
243 if (Offset)
244 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000245 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000246 if (Offset > 0)
247 O << ") + " << Offset;
248 else if (Offset < 0)
249 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000250 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000251 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000252 }
253 break;
254 }
255 case Instruction::Cast: {
256 // Support only non-converting or widening casts for now, that is, ones
257 // that do not involve a change in value. This assertion is really gross,
258 // and may not even be a complete check.
259 Constant *Op = CE->getOperand(0);
260 const Type *OpTy = Op->getType(), *Ty = CE->getType();
261
262 // Remember, kids, pointers can be losslessly converted back and forth
263 // into 32-bit or wider integers, regardless of signedness. :-P
264 assert(((isa<PointerType>(OpTy)
265 && (Ty == Type::LongTy || Ty == Type::ULongTy
266 || Ty == Type::IntTy || Ty == Type::UIntTy))
267 || (isa<PointerType>(Ty)
268 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
269 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
270 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
271 && OpTy->isLosslesslyConvertibleTo(Ty))))
272 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000273 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000274 break;
275 }
276 case Instruction::Add:
277 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000278 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattnera80ba712004-08-16 23:15:22 +0000279 O << ") + (";
Chris Lattner25045bd2005-11-21 07:51:36 +0000280 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000281 O << ")";
282 break;
283 default:
284 assert(0 && "Unsupported operator!");
285 }
286 } else {
287 assert(0 && "Unknown constant value!");
288 }
289}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000290
291/// toOctal - Convert the low order bits of X into an octal digit.
292///
293static inline char toOctal(int X) {
294 return (X&7)+'0';
295}
296
Chris Lattner2980cef2005-11-10 18:06:33 +0000297/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000298/// the predicate isString is true.
299///
Chris Lattner2980cef2005-11-10 18:06:33 +0000300static void printAsCString(std::ostream &O, const ConstantArray *CVA,
301 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000302 assert(CVA->isString() && "Array is not string compatible!");
303
304 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000305 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000306 unsigned char C =
Chris Lattnerdea18b62005-01-08 19:59:10 +0000307 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000308
309 if (C == '"') {
310 O << "\\\"";
311 } else if (C == '\\') {
312 O << "\\\\";
313 } else if (isprint(C)) {
314 O << C;
315 } else {
316 switch(C) {
317 case '\b': O << "\\b"; break;
318 case '\f': O << "\\f"; break;
319 case '\n': O << "\\n"; break;
320 case '\r': O << "\\r"; break;
321 case '\t': O << "\\t"; break;
322 default:
323 O << '\\';
324 O << toOctal(C >> 6);
325 O << toOctal(C >> 3);
326 O << toOctal(C >> 0);
327 break;
328 }
329 }
330 }
331 O << "\"";
332}
333
Chris Lattner25045bd2005-11-21 07:51:36 +0000334/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner1b7e2352004-08-17 06:36:49 +0000335///
Chris Lattner25045bd2005-11-21 07:51:36 +0000336void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000337 const TargetData &TD = TM.getTargetData();
338
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000339 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattner25045bd2005-11-21 07:51:36 +0000340 EmitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000341 return;
342 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
343 if (CVA->isString()) {
Chris Lattner2980cef2005-11-10 18:06:33 +0000344 unsigned NumElts = CVA->getNumOperands();
345 if (AscizDirective && NumElts &&
346 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
347 O << AscizDirective;
348 printAsCString(O, CVA, NumElts-1);
349 } else {
350 O << AsciiDirective;
351 printAsCString(O, CVA, NumElts);
352 }
Chris Lattner1b7e2352004-08-17 06:36:49 +0000353 O << "\n";
354 } else { // Not a string. Print the values in successive locations
355 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner25045bd2005-11-21 07:51:36 +0000356 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000357 }
358 return;
359 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
360 // Print the fields in successive locations. Pad to align if needed!
361 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000362 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000363 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
364 const Constant* field = CVS->getOperand(i);
365
366 // Check if padding is needed and insert one or more 0s.
Chris Lattnerdea18b62005-01-08 19:59:10 +0000367 uint64_t fieldSize = TD.getTypeSize(field->getType());
368 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner1b7e2352004-08-17 06:36:49 +0000369 : cvsLayout->MemberOffsets[i+1])
370 - cvsLayout->MemberOffsets[i]) - fieldSize;
371 sizeSoFar += fieldSize + padSize;
372
373 // Now print the actual field value
Chris Lattner25045bd2005-11-21 07:51:36 +0000374 EmitGlobalConstant(field);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000375
376 // Insert the field padding unless it's zero bytes...
Chris Lattner25045bd2005-11-21 07:51:36 +0000377 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000378 }
379 assert(sizeSoFar == cvsLayout->StructSize &&
380 "Layout of constant struct may be incorrect!");
381 return;
382 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
383 // FP Constants are printed as integer constants to avoid losing
384 // precision...
385 double Val = CFP->getValue();
386 if (CFP->getType() == Type::DoubleTy) {
Chris Lattnere85a5a92004-08-17 06:48:16 +0000387 if (Data64bitsDirective)
Jim Laskeycb6682f2005-08-17 19:34:49 +0000388 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattner7d057a32004-08-17 21:38:40 +0000389 << " double value: " << Val << "\n";
Chris Lattnere85a5a92004-08-17 06:48:16 +0000390 else if (TD.isBigEndian()) {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000391 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000392 << "\t" << CommentString << " double most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000393 << Val << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000394 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000395 << "\t" << CommentString << " double least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000396 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000397 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000398 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000399 << "\t" << CommentString << " double least significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000400 << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000401 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000402 << "\t" << CommentString << " double most significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000403 << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000404 }
405 return;
406 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000407 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattner0554fb62004-08-17 16:27:05 +0000408 << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000409 return;
410 }
411 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
412 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
413 uint64_t Val = CI->getRawValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000414
Chris Lattnere85a5a92004-08-17 06:48:16 +0000415 if (Data64bitsDirective)
416 O << Data64bitsDirective << Val << "\n";
417 else if (TD.isBigEndian()) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000418 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000419 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000420 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000421 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000422 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000423 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000424 } else {
425 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000426 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000427 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000428 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000429 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000430 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000431 }
432 return;
433 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000434 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
435 const PackedType *PTy = CP->getType();
436
437 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
438 EmitGlobalConstant(CP->getOperand(I));
439
440 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000441 }
442
443 const Type *type = CV->getType();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000444 switch (type->getTypeID()) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000445 case Type::BoolTyID:
Chris Lattner1b7e2352004-08-17 06:36:49 +0000446 case Type::UByteTyID: case Type::SByteTyID:
447 O << Data8bitsDirective;
448 break;
449 case Type::UShortTyID: case Type::ShortTyID:
450 O << Data16bitsDirective;
451 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000452 case Type::PointerTyID:
Andrew Lenharth571c9c32005-02-04 13:47:16 +0000453 if (TD.getPointerSize() == 8) {
454 O << Data64bitsDirective;
455 break;
456 }
457 //Fall through for pointer size == int size
Chris Lattner1b7e2352004-08-17 06:36:49 +0000458 case Type::UIntTyID: case Type::IntTyID:
459 O << Data32bitsDirective;
460 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000461 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner660538c2005-08-08 04:26:32 +0000462 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
463 O << Data64bitsDirective;
464 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000465 case Type::FloatTyID: case Type::DoubleTyID:
466 assert (0 && "Should have already output floating point constant.");
467 default:
468 assert (0 && "Can't handle printing this type of thing");
469 break;
470 }
Chris Lattner25045bd2005-11-21 07:51:36 +0000471 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000472 O << "\n";
473}
Chris Lattner0264d1a2006-01-27 02:10:10 +0000474
475/// printInlineAsm - This method formats and prints the specified machine
476/// instruction that is an inline asm.
477void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerd6c65ea2006-02-08 23:41:56 +0000478 O << InlineAsmStart;
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000479 unsigned NumOperands = MI->getNumOperands();
480
481 // Count the number of register definitions.
482 unsigned NumDefs = 0;
483 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
484 assert(NumDefs != NumOperands-1 && "No asm string?");
485
486 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +0000487
488 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000489 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +0000490
491 // The variant of the current asmprinter: FIXME: change.
492 int AsmPrinterVariant = 0;
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000493
Chris Lattner66099132006-02-01 22:41:11 +0000494 int CurVariant = -1; // The number of the {.|.|.} region we are in.
495 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +0000496
Chris Lattner66099132006-02-01 22:41:11 +0000497 while (*LastEmitted) {
498 switch (*LastEmitted) {
499 default: {
500 // Not a special case, emit the string section literally.
501 const char *LiteralEnd = LastEmitted+1;
502 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
503 *LiteralEnd != '}' && *LiteralEnd != '$')
504 ++LiteralEnd;
505 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
506 O.write(LastEmitted, LiteralEnd-LastEmitted);
507 LastEmitted = LiteralEnd;
508 break;
509 }
510 case '$': {
511 ++LastEmitted; // Consume '$' character.
512 if (*LastEmitted == '$') { // $$ -> $
513 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
514 O << '$';
515 ++LastEmitted; // Consume second '$' character.
516 break;
517 }
518
519 bool HasCurlyBraces = false;
520 if (*LastEmitted == '{') { // ${variable}
521 ++LastEmitted; // Consume '{' character.
522 HasCurlyBraces = true;
523 }
524
525 const char *IDStart = LastEmitted;
526 char *IDEnd;
527 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
528 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
529 std::cerr << "Bad $ operand number in inline asm string: '"
530 << AsmStr << "'\n";
531 exit(1);
532 }
533 LastEmitted = IDEnd;
534
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000535 char Modifier[2] = { 0, 0 };
536
Chris Lattner66099132006-02-01 22:41:11 +0000537 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000538 // If we have curly braces, check for a modifier character. This
539 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
540 if (*LastEmitted == ':') {
541 ++LastEmitted; // Consume ':' character.
542 if (*LastEmitted == 0) {
543 std::cerr << "Bad ${:} expression in inline asm string: '"
544 << AsmStr << "'\n";
545 exit(1);
546 }
547
548 Modifier[0] = *LastEmitted;
549 ++LastEmitted; // Consume modifier character.
550 }
551
Chris Lattner66099132006-02-01 22:41:11 +0000552 if (*LastEmitted != '}') {
553 std::cerr << "Bad ${} expression in inline asm string: '"
554 << AsmStr << "'\n";
555 exit(1);
556 }
557 ++LastEmitted; // Consume '}' character.
558 }
559
560 if ((unsigned)Val >= NumOperands-1) {
561 std::cerr << "Invalid $ operand number in inline asm string: '"
562 << AsmStr << "'\n";
563 exit(1);
564 }
565
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000566 char ExtraCode = 0; // FIXME:
567
Chris Lattner66099132006-02-01 22:41:11 +0000568 // Okay, we finally have an operand number. Ask the target to print this
569 // operand!
570 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
571 if (const_cast<AsmPrinter*>(this)->
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000572 PrintAsmOperand(MI, Val+1, AsmPrinterVariant,
573 Modifier[0] ? Modifier : 0)) {
Chris Lattner66099132006-02-01 22:41:11 +0000574 std::cerr << "Invalid operand found in inline asm: '"
575 << AsmStr << "'\n";
576 MI->dump();
577 exit(1);
578 }
579 break;
580 }
581 case '{':
582 ++LastEmitted; // Consume '{' character.
583 if (CurVariant != -1) {
584 std::cerr << "Nested variants found in inline asm string: '"
585 << AsmStr << "'\n";
586 exit(1);
587 }
588 CurVariant = 0; // We're in the first variant now.
589 break;
590 case '|':
591 ++LastEmitted; // consume '|' character.
592 if (CurVariant == -1) {
593 std::cerr << "Found '|' character outside of variant in inline asm "
594 << "string: '" << AsmStr << "'\n";
595 exit(1);
596 }
597 ++CurVariant; // We're in the next variant.
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 = -1;
607 break;
608 }
609 }
Chris Lattnerd6c65ea2006-02-08 23:41:56 +0000610 O << "\n" << InlineAsmEnd;
Chris Lattner66099132006-02-01 22:41:11 +0000611}
612
613/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
614/// instruction, using the specified assembler variant. Targets should
615/// overried this to format as appropriate.
616bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000617 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +0000618 // Target doesn't support this yet!
619 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +0000620}