blob: 9aca4e25ac055b57159060e6120566f4e42eaced [file] [log] [blame]
Chris Lattner6a8e0f52004-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 Cheng993e9cf2006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng38d5e762006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman41b1cdc2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattnerf2991ce2005-11-21 08:25:09 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000020#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000021#include "llvm/Support/Mangler.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000022#include "llvm/Support/MathExtras.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000023#include "llvm/Target/TargetMachine.h"
Duraid Madina26b037e2005-12-28 06:29:02 +000024#include <iostream>
Chris Lattneraa23fa92006-02-01 22:41:11 +000025#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000026using namespace llvm;
27
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000028AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm)
29: FunctionNumber(0), O(o), TM(tm),
30 CommentString("#"),
31 GlobalPrefix(""),
32 PrivateGlobalPrefix("."),
33 GlobalVarAddrPrefix(""),
34 GlobalVarAddrSuffix(""),
35 FunctionAddrPrefix(""),
36 FunctionAddrSuffix(""),
Chris Lattnera633c3132006-05-05 21:47:05 +000037 InlineAsmStart("#APP"),
38 InlineAsmEnd("#NO_APP"),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000039 ZeroDirective("\t.zero\t"),
Jeff Cohenf34ddb12006-05-02 03:46:13 +000040 ZeroDirectiveSuffix(0),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000041 AsciiDirective("\t.ascii\t"),
42 AscizDirective("\t.asciz\t"),
43 Data8bitsDirective("\t.byte\t"),
44 Data16bitsDirective("\t.short\t"),
45 Data32bitsDirective("\t.long\t"),
46 Data64bitsDirective("\t.quad\t"),
47 AlignDirective("\t.align\t"),
48 AlignmentIsInBytes(true),
49 SwitchToSectionDirective("\t.section\t"),
Jeff Cohen470f4312006-05-02 03:58:45 +000050 MLSections(false),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000051 ConstantPoolSection("\t.section .rodata\n"),
Nate Begeman4ca2ea52006-04-22 18:53:45 +000052 JumpTableSection("\t.section .rodata\n"),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000053 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
54 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
55 LCOMMDirective(0),
56 COMMDirective("\t.comm\t"),
57 COMMDirectiveTakesAlignment(true),
58 HasDotTypeDotSizeDirective(true) {
59}
60
61
Chris Lattner2ea5c992005-11-21 07:06:27 +000062/// SwitchSection - Switch to the specified section of the executable if we
63/// are not already in it!
64///
65void AsmPrinter::SwitchSection(const char *NewSection, const GlobalValue *GV) {
66 std::string NS;
Jeff Cohen470f4312006-05-02 03:58:45 +000067
68 // Microsoft ML/MASM has a fundamentally different approach to handling
69 // sections.
70
71 if (MLSections) {
72 if (*NewSection == 0) {
73 // Simply end the current section, if any.
Jeff Cohence9b9fe2006-05-06 21:27:14 +000074 if (!CurrentSection.empty()) {
75 O << CurrentSection << "\tends\n\n";
76 CurrentSection.clear();
Jeff Cohen470f4312006-05-02 03:58:45 +000077 }
78 return;
79 }
80
81 bool isData = strcmp(NewSection , ".data") == 0;
82
83 if (GV && GV->hasSection())
84 NS = GV->getSection();
85 else if (isData)
86 NS = "_data";
87 else
88 NS = "_text";
89
90 if (CurrentSection != NS) {
Jeff Cohence9b9fe2006-05-06 21:27:14 +000091 if (!CurrentSection.empty())
92 O << CurrentSection << "\tends\n\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000093 CurrentSection = NS;
94 O << CurrentSection << (isData ? "\tsegment 'DATA'\n"
95 : "\tsegment 'CODE'\n");
96 }
97 } else {
98 if (GV && GV->hasSection())
99 NS = SwitchToSectionDirective + GV->getSection();
100 else
101 NS = std::string("\t")+NewSection;
102
103 if (CurrentSection != NS) {
104 CurrentSection = NS;
105 if (!CurrentSection.empty())
106 O << CurrentSection << '\n';
107 }
Chris Lattner2ea5c992005-11-21 07:06:27 +0000108 }
109}
110
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000111bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner6d42cbd2004-08-17 06:06:19 +0000112 Mang = new Mangler(M, GlobalPrefix);
Chris Lattnere3a79262006-01-23 23:47:53 +0000113
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000114 if (!M.getModuleInlineAsm().empty())
115 O << CommentString << " Start of file scope inline assembly\n"
116 << M.getModuleInlineAsm()
117 << "\n" << CommentString << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000118
Chris Lattner2ea5c992005-11-21 07:06:27 +0000119 SwitchSection("", 0); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000120
121 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
122 DebugInfo->AnalyzeModule(M);
123 }
124
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000125 return false;
126}
127
128bool AsmPrinter::doFinalization(Module &M) {
129 delete Mang; Mang = 0;
130 return false;
131}
132
Chris Lattnerbb644e32005-11-21 07:51:36 +0000133void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000134 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000135 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000136 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000137}
138
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000139/// EmitConstantPool - Print to the current output stream assembly
140/// representations of the constants in the constant pool MCP. This is
141/// used to print out constants which have been "spilled to memory" by
142/// the code generator.
143///
144void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000145 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000146 if (CP.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000147 const TargetData *TD = TM.getTargetData();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000148
149 SwitchSection(ConstantPoolSection, 0);
Chris Lattnerf6190822006-02-09 04:46:04 +0000150 EmitAlignment(MCP->getConstantPoolAlignment());
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000151 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000152 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
Evan Cheng38d5e762006-03-01 22:18:09 +0000153 << ":\t\t\t\t\t" << CommentString << " ";
154 WriteTypeSymbolic(O, CP[i].Val->getType(), 0) << '\n';
Chris Lattnerba972642006-02-09 04:22:52 +0000155 EmitGlobalConstant(CP[i].Val);
Chris Lattnerf6190822006-02-09 04:46:04 +0000156 if (i != e-1) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000157 unsigned EntSize = TM.getTargetData()->getTypeSize(CP[i].Val->getType());
Chris Lattnerf6190822006-02-09 04:46:04 +0000158 unsigned ValEnd = CP[i].Offset + EntSize;
159 // Emit inter-object padding for alignment.
160 EmitZeros(CP[i+1].Offset-ValEnd);
161 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000162 }
163}
164
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000165/// EmitJumpTableInfo - Print assembly representations of the jump tables used
166/// by the current function to the current output stream.
167///
168void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
169 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
170 if (JT.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000171 const TargetData *TD = TM.getTargetData();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000172
173 // FIXME: someday we need to handle PIC jump tables
174 assert((TM.getRelocationModel() == Reloc::Static ||
175 TM.getRelocationModel() == Reloc::DynamicNoPIC) &&
176 "Unhandled relocation model emitting jump table information!");
177
178 SwitchSection(JumpTableSection, 0);
Owen Anderson20a631f2006-05-03 01:29:57 +0000179 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000180 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
181 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << '_' << i
182 << ":\n";
183 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
184 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
185 O << Data32bitsDirective << ' ';
186 printBasicBlockLabel(JTBBs[ii]);
187 O << '\n';
188 }
189 }
190}
191
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000192/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
193/// special global used by LLVM. If so, emit it and return true, otherwise
194/// do nothing and return false.
195bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey313570f2006-03-07 22:00:35 +0000196 // Ignore debug and non-emitted data.
197 if (GV->getSection() == "llvm.metadata") return true;
198
199 if (!GV->hasAppendingLinkage()) return false;
200
201 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000202
203 if (GV->getName() == "llvm.used")
204 return true; // No need to emit this at all.
205
Chris Lattner3760e902006-01-12 19:17:23 +0000206 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000207 SwitchSection(StaticCtorsSection, 0);
208 EmitAlignment(2, 0);
209 EmitXXStructorList(GV->getInitializer());
210 return true;
211 }
212
Chris Lattner3760e902006-01-12 19:17:23 +0000213 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000214 SwitchSection(StaticDtorsSection, 0);
215 EmitAlignment(2, 0);
216 EmitXXStructorList(GV->getInitializer());
217 return true;
218 }
219
220 return false;
221}
222
223/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
224/// function pointers, ignoring the init priority.
225void AsmPrinter::EmitXXStructorList(Constant *List) {
226 // Should be an array of '{ int, void ()* }' structs. The first value is the
227 // init priority, which we ignore.
228 if (!isa<ConstantArray>(List)) return;
229 ConstantArray *InitList = cast<ConstantArray>(List);
230 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
231 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
232 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000233
234 if (CS->getOperand(1)->isNullValue())
235 return; // Found a null terminator, exit printing.
236 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000237 EmitGlobalConstant(CS->getOperand(1));
238 }
239}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000240
Chris Lattnera9b25252006-02-05 01:29:18 +0000241/// getPreferredAlignmentLog - Return the preferred alignment of the
242/// specified global, returned in log form. This includes an explicitly
243/// requested alignment (if the global has one).
244unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Owen Anderson20a631f2006-05-03 01:29:57 +0000245 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(GV->getType());
Chris Lattnera9b25252006-02-05 01:29:18 +0000246 if (GV->getAlignment() > (1U << Alignment))
247 Alignment = Log2_32(GV->getAlignment());
248
Chris Lattnercbab2842006-02-05 01:46:49 +0000249 if (GV->hasInitializer()) {
250 // Always round up alignment of global doubles to 8 bytes.
251 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
252 Alignment = 3;
253 if (Alignment < 4) {
254 // If the global is not external, see if it is large. If so, give it a
255 // larger alignment.
Owen Anderson20a631f2006-05-03 01:29:57 +0000256 if (TM.getTargetData()->getTypeSize(GV->getType()->getElementType()) > 128)
Chris Lattnercbab2842006-02-05 01:46:49 +0000257 Alignment = 4; // 16-byte alignment.
258 }
Chris Lattnera9b25252006-02-05 01:29:18 +0000259 }
260 return Alignment;
261}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000262
Chris Lattnerbb644e32005-11-21 07:51:36 +0000263// EmitAlignment - Emit an alignment directive to the specified power of two.
264void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000265 if (GV && GV->getAlignment())
266 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000267 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner1d35c162004-08-17 19:14:29 +0000268 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
269 O << AlignDirective << NumBits << "\n";
270}
271
Chris Lattnerbb644e32005-11-21 07:51:36 +0000272/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000273///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000274void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000275 if (NumZeros) {
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000276 if (ZeroDirective) {
277 O << ZeroDirective << NumZeros;
278 if (ZeroDirectiveSuffix)
279 O << ZeroDirectiveSuffix;
280 O << "\n";
281 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000282 for (; NumZeros; --NumZeros)
283 O << Data8bitsDirective << "0\n";
284 }
285 }
286}
287
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000288// Print out the specified constant, without a storage class. Only the
289// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000290void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000291 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000292 O << "0";
293 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
294 assert(CB == ConstantBool::True);
295 O << "1";
296 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
297 if (((CI->getValue() << 32) >> 32) == CI->getValue())
298 O << CI->getValue();
299 else
Duraid Madina73c4dba2005-05-15 13:05:48 +0000300 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000301 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
302 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000303 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000304 // This is a constant address for a global variable or function. Use the
305 // name of the variable or function as the address value, possibly
306 // decorating it with GlobalVarAddrPrefix/Suffix or
307 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000308 if (isa<Function>(GV))
309 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000310 else
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000311 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000312 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000313 const TargetData *TD = TM.getTargetData();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000314 switch(CE->getOpcode()) {
315 case Instruction::GetElementPtr: {
316 // generate a symbolic expression for the byte address
317 const Constant *ptrVal = CE->getOperand(0);
318 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Anderson20a631f2006-05-03 01:29:57 +0000319 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner145569b2005-02-14 21:40:26 +0000320 if (Offset)
321 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000322 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000323 if (Offset > 0)
324 O << ") + " << Offset;
325 else if (Offset < 0)
326 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000327 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000328 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000329 }
330 break;
331 }
332 case Instruction::Cast: {
333 // Support only non-converting or widening casts for now, that is, ones
334 // that do not involve a change in value. This assertion is really gross,
335 // and may not even be a complete check.
336 Constant *Op = CE->getOperand(0);
337 const Type *OpTy = Op->getType(), *Ty = CE->getType();
338
339 // Remember, kids, pointers can be losslessly converted back and forth
340 // into 32-bit or wider integers, regardless of signedness. :-P
341 assert(((isa<PointerType>(OpTy)
342 && (Ty == Type::LongTy || Ty == Type::ULongTy
343 || Ty == Type::IntTy || Ty == Type::UIntTy))
344 || (isa<PointerType>(Ty)
345 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
346 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Owen Anderson20a631f2006-05-03 01:29:57 +0000347 || (((TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000348 && OpTy->isLosslesslyConvertibleTo(Ty))))
349 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000350 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000351 break;
352 }
353 case Instruction::Add:
354 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000355 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000356 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000357 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000358 O << ")";
359 break;
360 default:
361 assert(0 && "Unsupported operator!");
362 }
363 } else {
364 assert(0 && "Unknown constant value!");
365 }
366}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000367
368/// toOctal - Convert the low order bits of X into an octal digit.
369///
370static inline char toOctal(int X) {
371 return (X&7)+'0';
372}
373
Chris Lattner55a6d902005-11-10 18:06:33 +0000374/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000375/// the predicate isString is true.
376///
Chris Lattner55a6d902005-11-10 18:06:33 +0000377static void printAsCString(std::ostream &O, const ConstantArray *CVA,
378 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000379 assert(CVA->isString() && "Array is not string compatible!");
380
381 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000382 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000383 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000384 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000385
386 if (C == '"') {
387 O << "\\\"";
388 } else if (C == '\\') {
389 O << "\\\\";
390 } else if (isprint(C)) {
391 O << C;
392 } else {
393 switch(C) {
394 case '\b': O << "\\b"; break;
395 case '\f': O << "\\f"; break;
396 case '\n': O << "\\n"; break;
397 case '\r': O << "\\r"; break;
398 case '\t': O << "\\t"; break;
399 default:
400 O << '\\';
401 O << toOctal(C >> 6);
402 O << toOctal(C >> 3);
403 O << toOctal(C >> 0);
404 break;
405 }
406 }
407 }
408 O << "\"";
409}
410
Jeff Cohen24a62a92006-05-02 01:16:28 +0000411/// EmitString - Emit a zero-byte-terminated string constant.
412///
413void AsmPrinter::EmitString(const ConstantArray *CVA) const {
414 unsigned NumElts = CVA->getNumOperands();
415 if (AscizDirective && NumElts &&
416 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
417 O << AscizDirective;
418 printAsCString(O, CVA, NumElts-1);
419 } else {
420 O << AsciiDirective;
421 printAsCString(O, CVA, NumElts);
422 }
423 O << "\n";
424}
425
Chris Lattnerbb644e32005-11-21 07:51:36 +0000426/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000427///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000428void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000429 const TargetData *TD = TM.getTargetData();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000430
Chris Lattner61753bf2004-10-16 18:19:26 +0000431 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000432 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000433 return;
434 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
435 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000436 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000437 } else { // Not a string. Print the values in successive locations
438 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000439 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000440 }
441 return;
442 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
443 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000444 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000445 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000446 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
447 const Constant* field = CVS->getOperand(i);
448
449 // Check if padding is needed and insert one or more 0s.
Owen Anderson20a631f2006-05-03 01:29:57 +0000450 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000451 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000452 : cvsLayout->MemberOffsets[i+1])
453 - cvsLayout->MemberOffsets[i]) - fieldSize;
454 sizeSoFar += fieldSize + padSize;
455
456 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000457 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000458
459 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000460 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000461 }
462 assert(sizeSoFar == cvsLayout->StructSize &&
463 "Layout of constant struct may be incorrect!");
464 return;
465 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
466 // FP Constants are printed as integer constants to avoid losing
467 // precision...
468 double Val = CFP->getValue();
469 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000470 if (Data64bitsDirective)
Jim Laskeyb74c6662005-08-17 19:34:49 +0000471 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattnerea751992004-08-17 21:38:40 +0000472 << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000473 else if (TD->isBigEndian()) {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000474 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000475 << "\t" << CommentString << " double most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000476 << Val << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000477 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000478 << "\t" << CommentString << " double least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000479 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000480 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000481 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000482 << "\t" << CommentString << " double least significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000483 << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000484 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000485 << "\t" << CommentString << " double most significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000486 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000487 }
488 return;
489 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000490 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattnerda6beac2004-08-17 16:27:05 +0000491 << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000492 return;
493 }
494 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
495 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
496 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000497
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000498 if (Data64bitsDirective)
499 O << Data64bitsDirective << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000500 else if (TD->isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000501 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000502 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000503 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000504 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000505 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000506 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000507 } else {
508 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000509 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000510 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000511 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000512 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000513 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000514 }
515 return;
516 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000517 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
518 const PackedType *PTy = CP->getType();
519
520 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
521 EmitGlobalConstant(CP->getOperand(I));
522
523 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000524 }
525
526 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000527 switch (type->getTypeID()) {
Misha Brukman835702a2005-04-21 22:36:52 +0000528 case Type::BoolTyID:
Chris Lattner8452a1f2004-08-17 06:36:49 +0000529 case Type::UByteTyID: case Type::SByteTyID:
530 O << Data8bitsDirective;
531 break;
532 case Type::UShortTyID: case Type::ShortTyID:
533 O << Data16bitsDirective;
534 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000535 case Type::PointerTyID:
Owen Anderson20a631f2006-05-03 01:29:57 +0000536 if (TD->getPointerSize() == 8) {
Andrew Lenharthc8770aa2005-02-04 13:47:16 +0000537 O << Data64bitsDirective;
538 break;
539 }
540 //Fall through for pointer size == int size
Chris Lattner8452a1f2004-08-17 06:36:49 +0000541 case Type::UIntTyID: case Type::IntTyID:
542 O << Data32bitsDirective;
543 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000544 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner88e2d2e2005-08-08 04:26:32 +0000545 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
546 O << Data64bitsDirective;
547 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000548 case Type::FloatTyID: case Type::DoubleTyID:
549 assert (0 && "Should have already output floating point constant.");
550 default:
551 assert (0 && "Can't handle printing this type of thing");
552 break;
553 }
Chris Lattnerbb644e32005-11-21 07:51:36 +0000554 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000555 O << "\n";
556}
Chris Lattner061d9e22006-01-27 02:10:10 +0000557
558/// printInlineAsm - This method formats and prints the specified machine
559/// instruction that is an inline asm.
560void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnera633c3132006-05-05 21:47:05 +0000561 O << InlineAsmStart << "\n\t";
Chris Lattner57ecb562006-01-30 23:00:08 +0000562 unsigned NumOperands = MI->getNumOperands();
563
564 // Count the number of register definitions.
565 unsigned NumDefs = 0;
566 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
567 assert(NumDefs != NumOperands-1 && "No asm string?");
568
569 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000570
571 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000572 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000573
574 // The variant of the current asmprinter: FIXME: change.
575 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000576
Chris Lattneraa23fa92006-02-01 22:41:11 +0000577 int CurVariant = -1; // The number of the {.|.|.} region we are in.
578 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000579
Chris Lattneraa23fa92006-02-01 22:41:11 +0000580 while (*LastEmitted) {
581 switch (*LastEmitted) {
582 default: {
583 // Not a special case, emit the string section literally.
584 const char *LiteralEnd = LastEmitted+1;
585 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +0000586 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +0000587 ++LiteralEnd;
588 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
589 O.write(LastEmitted, LiteralEnd-LastEmitted);
590 LastEmitted = LiteralEnd;
591 break;
592 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000593 case '\n':
594 ++LastEmitted; // Consume newline character.
595 O << "\n\t"; // Indent code with newline.
596 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000597 case '$': {
598 ++LastEmitted; // Consume '$' character.
599 if (*LastEmitted == '$') { // $$ -> $
600 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
601 O << '$';
602 ++LastEmitted; // Consume second '$' character.
603 break;
604 }
605
606 bool HasCurlyBraces = false;
607 if (*LastEmitted == '{') { // ${variable}
608 ++LastEmitted; // Consume '{' character.
609 HasCurlyBraces = true;
610 }
611
612 const char *IDStart = LastEmitted;
613 char *IDEnd;
614 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
615 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
616 std::cerr << "Bad $ operand number in inline asm string: '"
617 << AsmStr << "'\n";
618 exit(1);
619 }
620 LastEmitted = IDEnd;
621
Chris Lattner34f74c12006-02-06 22:17:23 +0000622 char Modifier[2] = { 0, 0 };
623
Chris Lattneraa23fa92006-02-01 22:41:11 +0000624 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000625 // If we have curly braces, check for a modifier character. This
626 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
627 if (*LastEmitted == ':') {
628 ++LastEmitted; // Consume ':' character.
629 if (*LastEmitted == 0) {
630 std::cerr << "Bad ${:} expression in inline asm string: '"
631 << AsmStr << "'\n";
632 exit(1);
633 }
634
635 Modifier[0] = *LastEmitted;
636 ++LastEmitted; // Consume modifier character.
637 }
638
Chris Lattneraa23fa92006-02-01 22:41:11 +0000639 if (*LastEmitted != '}') {
640 std::cerr << "Bad ${} expression in inline asm string: '"
641 << AsmStr << "'\n";
642 exit(1);
643 }
644 ++LastEmitted; // Consume '}' character.
645 }
646
647 if ((unsigned)Val >= NumOperands-1) {
648 std::cerr << "Invalid $ operand number in inline asm string: '"
649 << AsmStr << "'\n";
650 exit(1);
651 }
652
Chris Lattner571d9642006-02-23 19:21:04 +0000653 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +0000654 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +0000655 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
656 unsigned OpNo = 1;
657
658 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +0000659 for (; Val; --Val) {
660 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
661 OpNo += (OpFlags >> 3) + 1;
662 }
Chris Lattner571d9642006-02-23 19:21:04 +0000663
Chris Lattner1d08c652006-02-24 20:21:58 +0000664 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
Chris Lattner571d9642006-02-23 19:21:04 +0000665 ++OpNo; // Skip over the ID number.
Chris Lattner1d08c652006-02-24 20:21:58 +0000666
667 bool Error;
668 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
669 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
670 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
671 Modifier[0] ? Modifier : 0);
672 } else {
673 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
674 Modifier[0] ? Modifier : 0);
675 }
676 if (Error) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000677 std::cerr << "Invalid operand found in inline asm: '"
678 << AsmStr << "'\n";
679 MI->dump();
680 exit(1);
681 }
Chris Lattner571d9642006-02-23 19:21:04 +0000682 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000683 break;
684 }
685 case '{':
686 ++LastEmitted; // Consume '{' character.
687 if (CurVariant != -1) {
688 std::cerr << "Nested variants found in inline asm string: '"
689 << AsmStr << "'\n";
690 exit(1);
691 }
692 CurVariant = 0; // We're in the first variant now.
693 break;
694 case '|':
695 ++LastEmitted; // consume '|' character.
696 if (CurVariant == -1) {
697 std::cerr << "Found '|' character outside of variant in inline asm "
698 << "string: '" << AsmStr << "'\n";
699 exit(1);
700 }
701 ++CurVariant; // We're in the next variant.
702 break;
703 case '}':
704 ++LastEmitted; // consume '}' character.
705 if (CurVariant == -1) {
706 std::cerr << "Found '}' character outside of variant in inline asm "
707 << "string: '" << AsmStr << "'\n";
708 exit(1);
709 }
710 CurVariant = -1;
711 break;
712 }
713 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000714 O << "\n\t" << InlineAsmEnd << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000715}
716
717/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
718/// instruction, using the specified assembler variant. Targets should
719/// overried this to format as appropriate.
720bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000721 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000722 // Target doesn't support this yet!
723 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000724}
Chris Lattner1d08c652006-02-24 20:21:58 +0000725
726bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
727 unsigned AsmVariant,
728 const char *ExtraCode) {
729 // Target doesn't support this yet!
730 return true;
731}
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000732
733/// printBasicBlockLabel - This method prints the label for the specified
734/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +0000735void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
736 bool printColon,
737 bool printComment) const {
Nate Begeman4971ba52006-05-02 17:36:46 +0000738 O << PrivateGlobalPrefix << "BB" << FunctionNumber << "_"
739 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +0000740 if (printColon)
741 O << ':';
742 if (printComment)
743 O << '\t' << CommentString << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000744}