blob: 92a2d7ba8dc4529ded06fe1dbbc53c1eb6e14b5b [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);
Chris Lattner3029f922006-02-09 04:46:04 +0000114 EmitAlignment(MCP->getConstantPoolAlignment());
Chris Lattner3b4fd322005-11-21 08:25:09 +0000115 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
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 Lattner3029f922006-02-09 04:46:04 +0000119 if (i != e-1) {
120 unsigned EntSize = TM.getTargetData().getTypeSize(CP[i].Val->getType());
121 unsigned ValEnd = CP[i].Offset + EntSize;
122 // Emit inter-object padding for alignment.
123 EmitZeros(CP[i+1].Offset-ValEnd);
124 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000125 }
126}
127
Chris Lattnered138932005-12-13 06:32:10 +0000128/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
129/// special global used by LLVM. If so, emit it and return true, otherwise
130/// do nothing and return false.
131bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
132 assert(GV->hasInitializer() && GV->hasAppendingLinkage() &&
133 "Not a special LLVM global!");
134
135 if (GV->getName() == "llvm.used")
136 return true; // No need to emit this at all.
137
Chris Lattner5166b822006-01-12 19:17:23 +0000138 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattnered138932005-12-13 06:32:10 +0000139 SwitchSection(StaticCtorsSection, 0);
140 EmitAlignment(2, 0);
141 EmitXXStructorList(GV->getInitializer());
142 return true;
143 }
144
Chris Lattner5166b822006-01-12 19:17:23 +0000145 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattnered138932005-12-13 06:32:10 +0000146 SwitchSection(StaticDtorsSection, 0);
147 EmitAlignment(2, 0);
148 EmitXXStructorList(GV->getInitializer());
149 return true;
150 }
151
152 return false;
153}
154
155/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
156/// function pointers, ignoring the init priority.
157void AsmPrinter::EmitXXStructorList(Constant *List) {
158 // Should be an array of '{ int, void ()* }' structs. The first value is the
159 // init priority, which we ignore.
160 if (!isa<ConstantArray>(List)) return;
161 ConstantArray *InitList = cast<ConstantArray>(List);
162 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
163 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
164 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000165
166 if (CS->getOperand(1)->isNullValue())
167 return; // Found a null terminator, exit printing.
168 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000169 EmitGlobalConstant(CS->getOperand(1));
170 }
171}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000172
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000173/// getPreferredAlignmentLog - Return the preferred alignment of the
174/// specified global, returned in log form. This includes an explicitly
175/// requested alignment (if the global has one).
176unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
177 unsigned Alignment = TM.getTargetData().getTypeAlignmentShift(GV->getType());
178 if (GV->getAlignment() > (1U << Alignment))
179 Alignment = Log2_32(GV->getAlignment());
180
Chris Lattner519ea2a2006-02-05 01:46:49 +0000181 if (GV->hasInitializer()) {
182 // Always round up alignment of global doubles to 8 bytes.
183 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
184 Alignment = 3;
185 if (Alignment < 4) {
186 // If the global is not external, see if it is large. If so, give it a
187 // larger alignment.
188 if (TM.getTargetData().getTypeSize(GV->getType()->getElementType()) > 128)
189 Alignment = 4; // 16-byte alignment.
190 }
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000191 }
192 return Alignment;
193}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000194
Chris Lattner25045bd2005-11-21 07:51:36 +0000195// EmitAlignment - Emit an alignment directive to the specified power of two.
196void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnera1ab72d2005-11-14 19:00:06 +0000197 if (GV && GV->getAlignment())
198 NumBits = Log2_32(GV->getAlignment());
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000199 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattnerbfddc202004-08-17 19:14:29 +0000200 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
201 O << AlignDirective << NumBits << "\n";
202}
203
Chris Lattner25045bd2005-11-21 07:51:36 +0000204/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000205///
Chris Lattner25045bd2005-11-21 07:51:36 +0000206void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000207 if (NumZeros) {
208 if (ZeroDirective)
209 O << ZeroDirective << NumZeros << "\n";
210 else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000211 for (; NumZeros; --NumZeros)
212 O << Data8bitsDirective << "0\n";
213 }
214 }
215}
216
Chris Lattnera80ba712004-08-16 23:15:22 +0000217// Print out the specified constant, without a storage class. Only the
218// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000219void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000220 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000221 O << "0";
222 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
223 assert(CB == ConstantBool::True);
224 O << "1";
225 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
226 if (((CI->getValue() << 32) >> 32) == CI->getValue())
227 O << CI->getValue();
228 else
Duraid Madina45606572005-05-15 13:05:48 +0000229 O << (uint64_t)CI->getValue();
Chris Lattnera80ba712004-08-16 23:15:22 +0000230 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
231 O << CI->getValue();
Chris Lattner450de392005-11-10 18:36:17 +0000232 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000233 // This is a constant address for a global variable or function. Use the
234 // name of the variable or function as the address value, possibly
235 // decorating it with GlobalVarAddrPrefix/Suffix or
236 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattner450de392005-11-10 18:36:17 +0000237 if (isa<Function>(GV))
238 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina855a5192005-04-02 12:21:51 +0000239 else
Chris Lattner450de392005-11-10 18:36:17 +0000240 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina855a5192005-04-02 12:21:51 +0000241 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000242 const TargetData &TD = TM.getTargetData();
243 switch(CE->getOpcode()) {
244 case Instruction::GetElementPtr: {
245 // generate a symbolic expression for the byte address
246 const Constant *ptrVal = CE->getOperand(0);
247 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner27e19212005-02-14 21:40:26 +0000248 if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
249 if (Offset)
250 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000251 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000252 if (Offset > 0)
253 O << ") + " << Offset;
254 else if (Offset < 0)
255 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000256 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000257 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000258 }
259 break;
260 }
261 case Instruction::Cast: {
262 // Support only non-converting or widening casts for now, that is, ones
263 // that do not involve a change in value. This assertion is really gross,
264 // and may not even be a complete check.
265 Constant *Op = CE->getOperand(0);
266 const Type *OpTy = Op->getType(), *Ty = CE->getType();
267
268 // Remember, kids, pointers can be losslessly converted back and forth
269 // into 32-bit or wider integers, regardless of signedness. :-P
270 assert(((isa<PointerType>(OpTy)
271 && (Ty == Type::LongTy || Ty == Type::ULongTy
272 || Ty == Type::IntTy || Ty == Type::UIntTy))
273 || (isa<PointerType>(Ty)
274 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
275 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
276 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
277 && OpTy->isLosslesslyConvertibleTo(Ty))))
278 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000279 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000280 break;
281 }
282 case Instruction::Add:
283 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000284 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattnera80ba712004-08-16 23:15:22 +0000285 O << ") + (";
Chris Lattner25045bd2005-11-21 07:51:36 +0000286 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000287 O << ")";
288 break;
289 default:
290 assert(0 && "Unsupported operator!");
291 }
292 } else {
293 assert(0 && "Unknown constant value!");
294 }
295}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000296
297/// toOctal - Convert the low order bits of X into an octal digit.
298///
299static inline char toOctal(int X) {
300 return (X&7)+'0';
301}
302
Chris Lattner2980cef2005-11-10 18:06:33 +0000303/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000304/// the predicate isString is true.
305///
Chris Lattner2980cef2005-11-10 18:06:33 +0000306static void printAsCString(std::ostream &O, const ConstantArray *CVA,
307 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000308 assert(CVA->isString() && "Array is not string compatible!");
309
310 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000311 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000312 unsigned char C =
Chris Lattnerdea18b62005-01-08 19:59:10 +0000313 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000314
315 if (C == '"') {
316 O << "\\\"";
317 } else if (C == '\\') {
318 O << "\\\\";
319 } else if (isprint(C)) {
320 O << C;
321 } else {
322 switch(C) {
323 case '\b': O << "\\b"; break;
324 case '\f': O << "\\f"; break;
325 case '\n': O << "\\n"; break;
326 case '\r': O << "\\r"; break;
327 case '\t': O << "\\t"; break;
328 default:
329 O << '\\';
330 O << toOctal(C >> 6);
331 O << toOctal(C >> 3);
332 O << toOctal(C >> 0);
333 break;
334 }
335 }
336 }
337 O << "\"";
338}
339
Chris Lattner25045bd2005-11-21 07:51:36 +0000340/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner1b7e2352004-08-17 06:36:49 +0000341///
Chris Lattner25045bd2005-11-21 07:51:36 +0000342void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000343 const TargetData &TD = TM.getTargetData();
344
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000345 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Chris Lattner25045bd2005-11-21 07:51:36 +0000346 EmitZeros(TD.getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000347 return;
348 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
349 if (CVA->isString()) {
Chris Lattner2980cef2005-11-10 18:06:33 +0000350 unsigned NumElts = CVA->getNumOperands();
351 if (AscizDirective && NumElts &&
352 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
353 O << AscizDirective;
354 printAsCString(O, CVA, NumElts-1);
355 } else {
356 O << AsciiDirective;
357 printAsCString(O, CVA, NumElts);
358 }
Chris Lattner1b7e2352004-08-17 06:36:49 +0000359 O << "\n";
360 } else { // Not a string. Print the values in successive locations
361 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner25045bd2005-11-21 07:51:36 +0000362 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000363 }
364 return;
365 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
366 // Print the fields in successive locations. Pad to align if needed!
367 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000368 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000369 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
370 const Constant* field = CVS->getOperand(i);
371
372 // Check if padding is needed and insert one or more 0s.
Chris Lattnerdea18b62005-01-08 19:59:10 +0000373 uint64_t fieldSize = TD.getTypeSize(field->getType());
374 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner1b7e2352004-08-17 06:36:49 +0000375 : cvsLayout->MemberOffsets[i+1])
376 - cvsLayout->MemberOffsets[i]) - fieldSize;
377 sizeSoFar += fieldSize + padSize;
378
379 // Now print the actual field value
Chris Lattner25045bd2005-11-21 07:51:36 +0000380 EmitGlobalConstant(field);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000381
382 // Insert the field padding unless it's zero bytes...
Chris Lattner25045bd2005-11-21 07:51:36 +0000383 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000384 }
385 assert(sizeSoFar == cvsLayout->StructSize &&
386 "Layout of constant struct may be incorrect!");
387 return;
388 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
389 // FP Constants are printed as integer constants to avoid losing
390 // precision...
391 double Val = CFP->getValue();
392 if (CFP->getType() == Type::DoubleTy) {
Chris Lattnere85a5a92004-08-17 06:48:16 +0000393 if (Data64bitsDirective)
Jim Laskeycb6682f2005-08-17 19:34:49 +0000394 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattner7d057a32004-08-17 21:38:40 +0000395 << " double value: " << Val << "\n";
Chris Lattnere85a5a92004-08-17 06:48:16 +0000396 else if (TD.isBigEndian()) {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000397 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000398 << "\t" << CommentString << " double most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000399 << Val << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000400 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000401 << "\t" << CommentString << " double least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000402 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000403 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000404 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000405 << "\t" << CommentString << " double least significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000406 << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000407 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000408 << "\t" << CommentString << " double most significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000409 << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000410 }
411 return;
412 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000413 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattner0554fb62004-08-17 16:27:05 +0000414 << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000415 return;
416 }
417 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
418 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
419 uint64_t Val = CI->getRawValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000420
Chris Lattnere85a5a92004-08-17 06:48:16 +0000421 if (Data64bitsDirective)
422 O << Data64bitsDirective << Val << "\n";
423 else if (TD.isBigEndian()) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000424 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000425 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000426 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000427 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000428 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000429 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000430 } else {
431 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000432 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000433 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000434 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000435 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000436 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000437 }
438 return;
439 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000440 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
441 const PackedType *PTy = CP->getType();
442
443 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
444 EmitGlobalConstant(CP->getOperand(I));
445
446 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000447 }
448
449 const Type *type = CV->getType();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000450 switch (type->getTypeID()) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000451 case Type::BoolTyID:
Chris Lattner1b7e2352004-08-17 06:36:49 +0000452 case Type::UByteTyID: case Type::SByteTyID:
453 O << Data8bitsDirective;
454 break;
455 case Type::UShortTyID: case Type::ShortTyID:
456 O << Data16bitsDirective;
457 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000458 case Type::PointerTyID:
Andrew Lenharth571c9c32005-02-04 13:47:16 +0000459 if (TD.getPointerSize() == 8) {
460 O << Data64bitsDirective;
461 break;
462 }
463 //Fall through for pointer size == int size
Chris Lattner1b7e2352004-08-17 06:36:49 +0000464 case Type::UIntTyID: case Type::IntTyID:
465 O << Data32bitsDirective;
466 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000467 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner660538c2005-08-08 04:26:32 +0000468 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
469 O << Data64bitsDirective;
470 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000471 case Type::FloatTyID: case Type::DoubleTyID:
472 assert (0 && "Should have already output floating point constant.");
473 default:
474 assert (0 && "Can't handle printing this type of thing");
475 break;
476 }
Chris Lattner25045bd2005-11-21 07:51:36 +0000477 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000478 O << "\n";
479}
Chris Lattner0264d1a2006-01-27 02:10:10 +0000480
481/// printInlineAsm - This method formats and prints the specified machine
482/// instruction that is an inline asm.
483void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerd6c65ea2006-02-08 23:41:56 +0000484 O << InlineAsmStart;
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000485 unsigned NumOperands = MI->getNumOperands();
486
487 // Count the number of register definitions.
488 unsigned NumDefs = 0;
489 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
490 assert(NumDefs != NumOperands-1 && "No asm string?");
491
492 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +0000493
494 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000495 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +0000496
497 // The variant of the current asmprinter: FIXME: change.
498 int AsmPrinterVariant = 0;
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000499
Chris Lattner66099132006-02-01 22:41:11 +0000500 int CurVariant = -1; // The number of the {.|.|.} region we are in.
501 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +0000502
Chris Lattner66099132006-02-01 22:41:11 +0000503 while (*LastEmitted) {
504 switch (*LastEmitted) {
505 default: {
506 // Not a special case, emit the string section literally.
507 const char *LiteralEnd = LastEmitted+1;
508 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
509 *LiteralEnd != '}' && *LiteralEnd != '$')
510 ++LiteralEnd;
511 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
512 O.write(LastEmitted, LiteralEnd-LastEmitted);
513 LastEmitted = LiteralEnd;
514 break;
515 }
516 case '$': {
517 ++LastEmitted; // Consume '$' character.
518 if (*LastEmitted == '$') { // $$ -> $
519 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
520 O << '$';
521 ++LastEmitted; // Consume second '$' character.
522 break;
523 }
524
525 bool HasCurlyBraces = false;
526 if (*LastEmitted == '{') { // ${variable}
527 ++LastEmitted; // Consume '{' character.
528 HasCurlyBraces = true;
529 }
530
531 const char *IDStart = LastEmitted;
532 char *IDEnd;
533 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
534 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
535 std::cerr << "Bad $ operand number in inline asm string: '"
536 << AsmStr << "'\n";
537 exit(1);
538 }
539 LastEmitted = IDEnd;
540
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000541 char Modifier[2] = { 0, 0 };
542
Chris Lattner66099132006-02-01 22:41:11 +0000543 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000544 // If we have curly braces, check for a modifier character. This
545 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
546 if (*LastEmitted == ':') {
547 ++LastEmitted; // Consume ':' character.
548 if (*LastEmitted == 0) {
549 std::cerr << "Bad ${:} expression in inline asm string: '"
550 << AsmStr << "'\n";
551 exit(1);
552 }
553
554 Modifier[0] = *LastEmitted;
555 ++LastEmitted; // Consume modifier character.
556 }
557
Chris Lattner66099132006-02-01 22:41:11 +0000558 if (*LastEmitted != '}') {
559 std::cerr << "Bad ${} expression in inline asm string: '"
560 << AsmStr << "'\n";
561 exit(1);
562 }
563 ++LastEmitted; // Consume '}' character.
564 }
565
566 if ((unsigned)Val >= NumOperands-1) {
567 std::cerr << "Invalid $ operand number in inline asm string: '"
568 << AsmStr << "'\n";
569 exit(1);
570 }
571
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000572 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +0000573 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000574 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
575 unsigned OpNo = 1;
576
577 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000578 for (; Val; --Val) {
579 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
580 OpNo += (OpFlags >> 3) + 1;
581 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000582
Chris Lattnerdd260332006-02-24 20:21:58 +0000583 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000584 ++OpNo; // Skip over the ID number.
Chris Lattnerdd260332006-02-24 20:21:58 +0000585
586 bool Error;
587 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
588 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
589 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
590 Modifier[0] ? Modifier : 0);
591 } else {
592 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
593 Modifier[0] ? Modifier : 0);
594 }
595 if (Error) {
Chris Lattner66099132006-02-01 22:41:11 +0000596 std::cerr << "Invalid operand found in inline asm: '"
597 << AsmStr << "'\n";
598 MI->dump();
599 exit(1);
600 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000601 }
Chris Lattner66099132006-02-01 22:41:11 +0000602 break;
603 }
604 case '{':
605 ++LastEmitted; // Consume '{' character.
606 if (CurVariant != -1) {
607 std::cerr << "Nested variants found in inline asm string: '"
608 << AsmStr << "'\n";
609 exit(1);
610 }
611 CurVariant = 0; // We're in the first variant now.
612 break;
613 case '|':
614 ++LastEmitted; // consume '|' character.
615 if (CurVariant == -1) {
616 std::cerr << "Found '|' character outside of variant in inline asm "
617 << "string: '" << AsmStr << "'\n";
618 exit(1);
619 }
620 ++CurVariant; // We're in the next variant.
621 break;
622 case '}':
623 ++LastEmitted; // consume '}' character.
624 if (CurVariant == -1) {
625 std::cerr << "Found '}' character outside of variant in inline asm "
626 << "string: '" << AsmStr << "'\n";
627 exit(1);
628 }
629 CurVariant = -1;
630 break;
631 }
632 }
Chris Lattnerd6c65ea2006-02-08 23:41:56 +0000633 O << "\n" << InlineAsmEnd;
Chris Lattner66099132006-02-01 22:41:11 +0000634}
635
636/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
637/// instruction, using the specified assembler variant. Targets should
638/// overried this to format as appropriate.
639bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000640 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +0000641 // Target doesn't support this yet!
642 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +0000643}
Chris Lattnerdd260332006-02-24 20:21:58 +0000644
645bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
646 unsigned AsmVariant,
647 const char *ExtraCode) {
648 // Target doesn't support this yet!
649 return true;
650}