blob: 5b44c9ca5d38c610e6d5423732768e2879e876a0 [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 Lattner8488ba22006-05-09 04:59:56 +000062/// SwitchToTextSection - Switch to the specified text section of the executable
63/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000064///
Chris Lattner8488ba22006-05-09 04:59:56 +000065void AsmPrinter::SwitchToTextSection(const char *NewSection,
66 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000067 std::string NS;
Jeff Cohen470f4312006-05-02 03:58:45 +000068
69 // Microsoft ML/MASM has a fundamentally different approach to handling
70 // sections.
71
72 if (MLSections) {
73 if (*NewSection == 0) {
74 // Simply end the current section, if any.
Jeff Cohence9b9fe2006-05-06 21:27:14 +000075 if (!CurrentSection.empty()) {
76 O << CurrentSection << "\tends\n\n";
77 CurrentSection.clear();
Jeff Cohen470f4312006-05-02 03:58:45 +000078 }
79 return;
80 }
81
Jeff Cohen470f4312006-05-02 03:58:45 +000082 if (GV && GV->hasSection())
83 NS = GV->getSection();
Jeff Cohen470f4312006-05-02 03:58:45 +000084 else
85 NS = "_text";
86
87 if (CurrentSection != NS) {
Jeff Cohence9b9fe2006-05-06 21:27:14 +000088 if (!CurrentSection.empty())
89 O << CurrentSection << "\tends\n\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000090 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +000091 O << CurrentSection << "\tsegment 'CODE'\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000092 }
93 } else {
94 if (GV && GV->hasSection())
95 NS = SwitchToSectionDirective + GV->getSection();
96 else
97 NS = std::string("\t")+NewSection;
98
99 if (CurrentSection != NS) {
100 CurrentSection = NS;
101 if (!CurrentSection.empty())
102 O << CurrentSection << '\n';
103 }
Chris Lattner2ea5c992005-11-21 07:06:27 +0000104 }
105}
106
Chris Lattner8488ba22006-05-09 04:59:56 +0000107/// SwitchToTextSection - Switch to the specified text section of the executable
108/// if we are not already in it!
109///
110void AsmPrinter::SwitchToDataSection(const char *NewSection,
111 const GlobalValue *GV) {
112 std::string NS;
113
114 // Microsoft ML/MASM has a fundamentally different approach to handling
115 // sections.
116
117 if (MLSections) {
118 if (*NewSection == 0) {
119 // Simply end the current section, if any.
120 if (!CurrentSection.empty()) {
121 O << CurrentSection << "\tends\n\n";
122 CurrentSection.clear();
123 }
124 return;
125 }
126
127 if (GV && GV->hasSection())
128 NS = GV->getSection();
129 else
130 NS = "_data";
131
132 if (CurrentSection != NS) {
133 if (!CurrentSection.empty())
134 O << CurrentSection << "\tends\n\n";
135 CurrentSection = NS;
136 O << CurrentSection << "\tsegment 'DATA'\n";
137 }
138 } else {
139 if (GV && GV->hasSection())
140 NS = SwitchToSectionDirective + GV->getSection();
141 else
142 NS = std::string("\t")+NewSection;
143
144 if (CurrentSection != NS) {
145 CurrentSection = NS;
146 if (!CurrentSection.empty())
147 O << CurrentSection << '\n';
148 }
149 }
150}
151
152
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000153bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner6d42cbd2004-08-17 06:06:19 +0000154 Mang = new Mangler(M, GlobalPrefix);
Chris Lattnere3a79262006-01-23 23:47:53 +0000155
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000156 if (!M.getModuleInlineAsm().empty())
157 O << CommentString << " Start of file scope inline assembly\n"
158 << M.getModuleInlineAsm()
159 << "\n" << CommentString << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000160
Chris Lattner8488ba22006-05-09 04:59:56 +0000161 SwitchToDataSection("", 0); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000162
163 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
164 DebugInfo->AnalyzeModule(M);
165 }
166
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000167 return false;
168}
169
170bool AsmPrinter::doFinalization(Module &M) {
171 delete Mang; Mang = 0;
172 return false;
173}
174
Chris Lattnerbb644e32005-11-21 07:51:36 +0000175void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000176 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000177 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000178 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000179}
180
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000181/// EmitConstantPool - Print to the current output stream assembly
182/// representations of the constants in the constant pool MCP. This is
183/// used to print out constants which have been "spilled to memory" by
184/// the code generator.
185///
186void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000187 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000188 if (CP.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000189 const TargetData *TD = TM.getTargetData();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000190
Chris Lattner8488ba22006-05-09 04:59:56 +0000191 SwitchToDataSection(ConstantPoolSection, 0);
Chris Lattnerf6190822006-02-09 04:46:04 +0000192 EmitAlignment(MCP->getConstantPoolAlignment());
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000193 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000194 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
Evan Cheng38d5e762006-03-01 22:18:09 +0000195 << ":\t\t\t\t\t" << CommentString << " ";
196 WriteTypeSymbolic(O, CP[i].Val->getType(), 0) << '\n';
Chris Lattnerba972642006-02-09 04:22:52 +0000197 EmitGlobalConstant(CP[i].Val);
Chris Lattnerf6190822006-02-09 04:46:04 +0000198 if (i != e-1) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000199 unsigned EntSize = TM.getTargetData()->getTypeSize(CP[i].Val->getType());
Chris Lattnerf6190822006-02-09 04:46:04 +0000200 unsigned ValEnd = CP[i].Offset + EntSize;
201 // Emit inter-object padding for alignment.
202 EmitZeros(CP[i+1].Offset-ValEnd);
203 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000204 }
205}
206
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000207/// EmitJumpTableInfo - Print assembly representations of the jump tables used
208/// by the current function to the current output stream.
209///
210void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
211 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
212 if (JT.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000213 const TargetData *TD = TM.getTargetData();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000214
215 // FIXME: someday we need to handle PIC jump tables
216 assert((TM.getRelocationModel() == Reloc::Static ||
217 TM.getRelocationModel() == Reloc::DynamicNoPIC) &&
218 "Unhandled relocation model emitting jump table information!");
219
Chris Lattner8488ba22006-05-09 04:59:56 +0000220 SwitchToDataSection(JumpTableSection, 0);
Owen Anderson20a631f2006-05-03 01:29:57 +0000221 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000222 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
223 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << '_' << i
224 << ":\n";
225 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
226 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
227 O << Data32bitsDirective << ' ';
228 printBasicBlockLabel(JTBBs[ii]);
229 O << '\n';
230 }
231 }
232}
233
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000234/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
235/// special global used by LLVM. If so, emit it and return true, otherwise
236/// do nothing and return false.
237bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey313570f2006-03-07 22:00:35 +0000238 // Ignore debug and non-emitted data.
239 if (GV->getSection() == "llvm.metadata") return true;
240
241 if (!GV->hasAppendingLinkage()) return false;
242
243 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000244
245 if (GV->getName() == "llvm.used")
246 return true; // No need to emit this at all.
247
Chris Lattner3760e902006-01-12 19:17:23 +0000248 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000249 SwitchToDataSection(StaticCtorsSection, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000250 EmitAlignment(2, 0);
251 EmitXXStructorList(GV->getInitializer());
252 return true;
253 }
254
Chris Lattner3760e902006-01-12 19:17:23 +0000255 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000256 SwitchToDataSection(StaticDtorsSection, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000257 EmitAlignment(2, 0);
258 EmitXXStructorList(GV->getInitializer());
259 return true;
260 }
261
262 return false;
263}
264
265/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
266/// function pointers, ignoring the init priority.
267void AsmPrinter::EmitXXStructorList(Constant *List) {
268 // Should be an array of '{ int, void ()* }' structs. The first value is the
269 // init priority, which we ignore.
270 if (!isa<ConstantArray>(List)) return;
271 ConstantArray *InitList = cast<ConstantArray>(List);
272 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
273 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
274 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000275
276 if (CS->getOperand(1)->isNullValue())
277 return; // Found a null terminator, exit printing.
278 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000279 EmitGlobalConstant(CS->getOperand(1));
280 }
281}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000282
Chris Lattnera9b25252006-02-05 01:29:18 +0000283/// getPreferredAlignmentLog - Return the preferred alignment of the
284/// specified global, returned in log form. This includes an explicitly
285/// requested alignment (if the global has one).
286unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Owen Anderson20a631f2006-05-03 01:29:57 +0000287 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(GV->getType());
Chris Lattnera9b25252006-02-05 01:29:18 +0000288 if (GV->getAlignment() > (1U << Alignment))
289 Alignment = Log2_32(GV->getAlignment());
290
Chris Lattnercbab2842006-02-05 01:46:49 +0000291 if (GV->hasInitializer()) {
292 // Always round up alignment of global doubles to 8 bytes.
293 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
294 Alignment = 3;
295 if (Alignment < 4) {
296 // If the global is not external, see if it is large. If so, give it a
297 // larger alignment.
Owen Anderson20a631f2006-05-03 01:29:57 +0000298 if (TM.getTargetData()->getTypeSize(GV->getType()->getElementType()) > 128)
Chris Lattnercbab2842006-02-05 01:46:49 +0000299 Alignment = 4; // 16-byte alignment.
300 }
Chris Lattnera9b25252006-02-05 01:29:18 +0000301 }
302 return Alignment;
303}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000304
Chris Lattnerbb644e32005-11-21 07:51:36 +0000305// EmitAlignment - Emit an alignment directive to the specified power of two.
306void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000307 if (GV && GV->getAlignment())
308 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000309 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner1d35c162004-08-17 19:14:29 +0000310 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
311 O << AlignDirective << NumBits << "\n";
312}
313
Chris Lattnerbb644e32005-11-21 07:51:36 +0000314/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000315///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000316void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000317 if (NumZeros) {
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000318 if (ZeroDirective) {
319 O << ZeroDirective << NumZeros;
320 if (ZeroDirectiveSuffix)
321 O << ZeroDirectiveSuffix;
322 O << "\n";
323 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000324 for (; NumZeros; --NumZeros)
325 O << Data8bitsDirective << "0\n";
326 }
327 }
328}
329
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000330// Print out the specified constant, without a storage class. Only the
331// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000332void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000333 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000334 O << "0";
335 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
336 assert(CB == ConstantBool::True);
337 O << "1";
338 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
339 if (((CI->getValue() << 32) >> 32) == CI->getValue())
340 O << CI->getValue();
341 else
Duraid Madina73c4dba2005-05-15 13:05:48 +0000342 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000343 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
344 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000345 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000346 // This is a constant address for a global variable or function. Use the
347 // name of the variable or function as the address value, possibly
348 // decorating it with GlobalVarAddrPrefix/Suffix or
349 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000350 if (isa<Function>(GV))
351 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000352 else
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000353 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000354 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000355 const TargetData *TD = TM.getTargetData();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000356 switch(CE->getOpcode()) {
357 case Instruction::GetElementPtr: {
358 // generate a symbolic expression for the byte address
359 const Constant *ptrVal = CE->getOperand(0);
360 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Anderson20a631f2006-05-03 01:29:57 +0000361 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner145569b2005-02-14 21:40:26 +0000362 if (Offset)
363 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000364 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000365 if (Offset > 0)
366 O << ") + " << Offset;
367 else if (Offset < 0)
368 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000369 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000370 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000371 }
372 break;
373 }
374 case Instruction::Cast: {
375 // Support only non-converting or widening casts for now, that is, ones
376 // that do not involve a change in value. This assertion is really gross,
377 // and may not even be a complete check.
378 Constant *Op = CE->getOperand(0);
379 const Type *OpTy = Op->getType(), *Ty = CE->getType();
380
381 // Remember, kids, pointers can be losslessly converted back and forth
382 // into 32-bit or wider integers, regardless of signedness. :-P
383 assert(((isa<PointerType>(OpTy)
384 && (Ty == Type::LongTy || Ty == Type::ULongTy
385 || Ty == Type::IntTy || Ty == Type::UIntTy))
386 || (isa<PointerType>(Ty)
387 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
388 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Owen Anderson20a631f2006-05-03 01:29:57 +0000389 || (((TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000390 && OpTy->isLosslesslyConvertibleTo(Ty))))
391 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000392 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000393 break;
394 }
395 case Instruction::Add:
396 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000397 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000398 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000399 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000400 O << ")";
401 break;
402 default:
403 assert(0 && "Unsupported operator!");
404 }
405 } else {
406 assert(0 && "Unknown constant value!");
407 }
408}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000409
410/// toOctal - Convert the low order bits of X into an octal digit.
411///
412static inline char toOctal(int X) {
413 return (X&7)+'0';
414}
415
Chris Lattner55a6d902005-11-10 18:06:33 +0000416/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000417/// the predicate isString is true.
418///
Chris Lattner55a6d902005-11-10 18:06:33 +0000419static void printAsCString(std::ostream &O, const ConstantArray *CVA,
420 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000421 assert(CVA->isString() && "Array is not string compatible!");
422
423 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000424 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000425 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000426 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000427
428 if (C == '"') {
429 O << "\\\"";
430 } else if (C == '\\') {
431 O << "\\\\";
432 } else if (isprint(C)) {
433 O << C;
434 } else {
435 switch(C) {
436 case '\b': O << "\\b"; break;
437 case '\f': O << "\\f"; break;
438 case '\n': O << "\\n"; break;
439 case '\r': O << "\\r"; break;
440 case '\t': O << "\\t"; break;
441 default:
442 O << '\\';
443 O << toOctal(C >> 6);
444 O << toOctal(C >> 3);
445 O << toOctal(C >> 0);
446 break;
447 }
448 }
449 }
450 O << "\"";
451}
452
Jeff Cohen24a62a92006-05-02 01:16:28 +0000453/// EmitString - Emit a zero-byte-terminated string constant.
454///
455void AsmPrinter::EmitString(const ConstantArray *CVA) const {
456 unsigned NumElts = CVA->getNumOperands();
457 if (AscizDirective && NumElts &&
458 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
459 O << AscizDirective;
460 printAsCString(O, CVA, NumElts-1);
461 } else {
462 O << AsciiDirective;
463 printAsCString(O, CVA, NumElts);
464 }
465 O << "\n";
466}
467
Chris Lattnerbb644e32005-11-21 07:51:36 +0000468/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000469///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000470void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000471 const TargetData *TD = TM.getTargetData();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000472
Chris Lattner61753bf2004-10-16 18:19:26 +0000473 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000474 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000475 return;
476 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
477 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000478 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000479 } else { // Not a string. Print the values in successive locations
480 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000481 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000482 }
483 return;
484 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
485 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000486 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000487 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000488 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
489 const Constant* field = CVS->getOperand(i);
490
491 // Check if padding is needed and insert one or more 0s.
Owen Anderson20a631f2006-05-03 01:29:57 +0000492 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000493 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000494 : cvsLayout->MemberOffsets[i+1])
495 - cvsLayout->MemberOffsets[i]) - fieldSize;
496 sizeSoFar += fieldSize + padSize;
497
498 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000499 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000500
501 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000502 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000503 }
504 assert(sizeSoFar == cvsLayout->StructSize &&
505 "Layout of constant struct may be incorrect!");
506 return;
507 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
508 // FP Constants are printed as integer constants to avoid losing
509 // precision...
510 double Val = CFP->getValue();
511 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000512 if (Data64bitsDirective)
Jim Laskeyb74c6662005-08-17 19:34:49 +0000513 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattnerea751992004-08-17 21:38:40 +0000514 << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000515 else if (TD->isBigEndian()) {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000516 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000517 << "\t" << CommentString << " double most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000518 << Val << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000519 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000520 << "\t" << CommentString << " double least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000521 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000522 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000523 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000524 << "\t" << CommentString << " double least significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000525 << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000526 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000527 << "\t" << CommentString << " double most significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000528 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000529 }
530 return;
531 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000532 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattnerda6beac2004-08-17 16:27:05 +0000533 << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000534 return;
535 }
536 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
537 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
538 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000539
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000540 if (Data64bitsDirective)
541 O << Data64bitsDirective << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000542 else if (TD->isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000543 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000544 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000545 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000546 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000547 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000548 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000549 } else {
550 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000551 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000552 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000553 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000554 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000555 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000556 }
557 return;
558 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000559 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
560 const PackedType *PTy = CP->getType();
561
562 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
563 EmitGlobalConstant(CP->getOperand(I));
564
565 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000566 }
567
568 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000569 switch (type->getTypeID()) {
Misha Brukman835702a2005-04-21 22:36:52 +0000570 case Type::BoolTyID:
Chris Lattner8452a1f2004-08-17 06:36:49 +0000571 case Type::UByteTyID: case Type::SByteTyID:
572 O << Data8bitsDirective;
573 break;
574 case Type::UShortTyID: case Type::ShortTyID:
575 O << Data16bitsDirective;
576 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000577 case Type::PointerTyID:
Owen Anderson20a631f2006-05-03 01:29:57 +0000578 if (TD->getPointerSize() == 8) {
Andrew Lenharthc8770aa2005-02-04 13:47:16 +0000579 O << Data64bitsDirective;
580 break;
581 }
582 //Fall through for pointer size == int size
Chris Lattner8452a1f2004-08-17 06:36:49 +0000583 case Type::UIntTyID: case Type::IntTyID:
584 O << Data32bitsDirective;
585 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000586 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner88e2d2e2005-08-08 04:26:32 +0000587 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
588 O << Data64bitsDirective;
589 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000590 case Type::FloatTyID: case Type::DoubleTyID:
591 assert (0 && "Should have already output floating point constant.");
592 default:
593 assert (0 && "Can't handle printing this type of thing");
594 break;
595 }
Chris Lattnerbb644e32005-11-21 07:51:36 +0000596 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000597 O << "\n";
598}
Chris Lattner061d9e22006-01-27 02:10:10 +0000599
600/// printInlineAsm - This method formats and prints the specified machine
601/// instruction that is an inline asm.
602void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnera633c3132006-05-05 21:47:05 +0000603 O << InlineAsmStart << "\n\t";
Chris Lattner57ecb562006-01-30 23:00:08 +0000604 unsigned NumOperands = MI->getNumOperands();
605
606 // Count the number of register definitions.
607 unsigned NumDefs = 0;
608 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
609 assert(NumDefs != NumOperands-1 && "No asm string?");
610
611 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000612
613 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000614 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000615
616 // The variant of the current asmprinter: FIXME: change.
617 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000618
Chris Lattneraa23fa92006-02-01 22:41:11 +0000619 int CurVariant = -1; // The number of the {.|.|.} region we are in.
620 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000621
Chris Lattneraa23fa92006-02-01 22:41:11 +0000622 while (*LastEmitted) {
623 switch (*LastEmitted) {
624 default: {
625 // Not a special case, emit the string section literally.
626 const char *LiteralEnd = LastEmitted+1;
627 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +0000628 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +0000629 ++LiteralEnd;
630 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
631 O.write(LastEmitted, LiteralEnd-LastEmitted);
632 LastEmitted = LiteralEnd;
633 break;
634 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000635 case '\n':
636 ++LastEmitted; // Consume newline character.
637 O << "\n\t"; // Indent code with newline.
638 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000639 case '$': {
640 ++LastEmitted; // Consume '$' character.
641 if (*LastEmitted == '$') { // $$ -> $
642 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
643 O << '$';
644 ++LastEmitted; // Consume second '$' character.
645 break;
646 }
647
648 bool HasCurlyBraces = false;
649 if (*LastEmitted == '{') { // ${variable}
650 ++LastEmitted; // Consume '{' character.
651 HasCurlyBraces = true;
652 }
653
654 const char *IDStart = LastEmitted;
655 char *IDEnd;
656 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
657 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
658 std::cerr << "Bad $ operand number in inline asm string: '"
659 << AsmStr << "'\n";
660 exit(1);
661 }
662 LastEmitted = IDEnd;
663
Chris Lattner34f74c12006-02-06 22:17:23 +0000664 char Modifier[2] = { 0, 0 };
665
Chris Lattneraa23fa92006-02-01 22:41:11 +0000666 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000667 // If we have curly braces, check for a modifier character. This
668 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
669 if (*LastEmitted == ':') {
670 ++LastEmitted; // Consume ':' character.
671 if (*LastEmitted == 0) {
672 std::cerr << "Bad ${:} expression in inline asm string: '"
673 << AsmStr << "'\n";
674 exit(1);
675 }
676
677 Modifier[0] = *LastEmitted;
678 ++LastEmitted; // Consume modifier character.
679 }
680
Chris Lattneraa23fa92006-02-01 22:41:11 +0000681 if (*LastEmitted != '}') {
682 std::cerr << "Bad ${} expression in inline asm string: '"
683 << AsmStr << "'\n";
684 exit(1);
685 }
686 ++LastEmitted; // Consume '}' character.
687 }
688
689 if ((unsigned)Val >= NumOperands-1) {
690 std::cerr << "Invalid $ operand number in inline asm string: '"
691 << AsmStr << "'\n";
692 exit(1);
693 }
694
Chris Lattner571d9642006-02-23 19:21:04 +0000695 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +0000696 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +0000697 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
698 unsigned OpNo = 1;
699
700 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +0000701 for (; Val; --Val) {
702 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
703 OpNo += (OpFlags >> 3) + 1;
704 }
Chris Lattner571d9642006-02-23 19:21:04 +0000705
Chris Lattner1d08c652006-02-24 20:21:58 +0000706 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
Chris Lattner571d9642006-02-23 19:21:04 +0000707 ++OpNo; // Skip over the ID number.
Chris Lattner1d08c652006-02-24 20:21:58 +0000708
709 bool Error;
710 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
711 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
712 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
713 Modifier[0] ? Modifier : 0);
714 } else {
715 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
716 Modifier[0] ? Modifier : 0);
717 }
718 if (Error) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000719 std::cerr << "Invalid operand found in inline asm: '"
720 << AsmStr << "'\n";
721 MI->dump();
722 exit(1);
723 }
Chris Lattner571d9642006-02-23 19:21:04 +0000724 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000725 break;
726 }
727 case '{':
728 ++LastEmitted; // Consume '{' character.
729 if (CurVariant != -1) {
730 std::cerr << "Nested variants found in inline asm string: '"
731 << AsmStr << "'\n";
732 exit(1);
733 }
734 CurVariant = 0; // We're in the first variant now.
735 break;
736 case '|':
737 ++LastEmitted; // consume '|' character.
738 if (CurVariant == -1) {
739 std::cerr << "Found '|' character outside of variant in inline asm "
740 << "string: '" << AsmStr << "'\n";
741 exit(1);
742 }
743 ++CurVariant; // We're in the next variant.
744 break;
745 case '}':
746 ++LastEmitted; // consume '}' character.
747 if (CurVariant == -1) {
748 std::cerr << "Found '}' character outside of variant in inline asm "
749 << "string: '" << AsmStr << "'\n";
750 exit(1);
751 }
752 CurVariant = -1;
753 break;
754 }
755 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000756 O << "\n\t" << InlineAsmEnd << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000757}
758
759/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
760/// instruction, using the specified assembler variant. Targets should
761/// overried this to format as appropriate.
762bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000763 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000764 // Target doesn't support this yet!
765 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000766}
Chris Lattner1d08c652006-02-24 20:21:58 +0000767
768bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
769 unsigned AsmVariant,
770 const char *ExtraCode) {
771 // Target doesn't support this yet!
772 return true;
773}
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000774
775/// printBasicBlockLabel - This method prints the label for the specified
776/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +0000777void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
778 bool printColon,
779 bool printComment) const {
Nate Begeman4971ba52006-05-02 17:36:46 +0000780 O << PrivateGlobalPrefix << "BB" << FunctionNumber << "_"
781 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +0000782 if (printColon)
783 O << ':';
784 if (printComment)
785 O << '\t' << CommentString << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000786}