blob: 3048602e12ba2548bf01a43e9cf6ce77f9d3c557 [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"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000023#include "llvm/Target/TargetData.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000024#include "llvm/Target/TargetMachine.h"
Duraid Madina26b037e2005-12-28 06:29:02 +000025#include <iostream>
Chris Lattneraa23fa92006-02-01 22:41:11 +000026#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000027using namespace llvm;
28
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000029AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm)
30: FunctionNumber(0), O(o), TM(tm),
31 CommentString("#"),
32 GlobalPrefix(""),
33 PrivateGlobalPrefix("."),
34 GlobalVarAddrPrefix(""),
35 GlobalVarAddrSuffix(""),
36 FunctionAddrPrefix(""),
37 FunctionAddrSuffix(""),
Chris Lattnera633c3132006-05-05 21:47:05 +000038 InlineAsmStart("#APP"),
39 InlineAsmEnd("#NO_APP"),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000040 ZeroDirective("\t.zero\t"),
Jeff Cohenf34ddb12006-05-02 03:46:13 +000041 ZeroDirectiveSuffix(0),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000042 AsciiDirective("\t.ascii\t"),
43 AscizDirective("\t.asciz\t"),
44 Data8bitsDirective("\t.byte\t"),
45 Data16bitsDirective("\t.short\t"),
46 Data32bitsDirective("\t.long\t"),
47 Data64bitsDirective("\t.quad\t"),
48 AlignDirective("\t.align\t"),
49 AlignmentIsInBytes(true),
50 SwitchToSectionDirective("\t.section\t"),
Chris Lattner4ebc6a22006-05-09 05:33:48 +000051 TextSectionStartSuffix(""),
52 DataSectionStartSuffix(""),
53 SectionEndDirectiveSuffix(0),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000054 ConstantPoolSection("\t.section .rodata\n"),
Nate Begeman4ca2ea52006-04-22 18:53:45 +000055 JumpTableSection("\t.section .rodata\n"),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000056 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
57 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
58 LCOMMDirective(0),
59 COMMDirective("\t.comm\t"),
60 COMMDirectiveTakesAlignment(true),
61 HasDotTypeDotSizeDirective(true) {
62}
63
64
Chris Lattner8488ba22006-05-09 04:59:56 +000065/// SwitchToTextSection - Switch to the specified text section of the executable
66/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000067///
Chris Lattner8488ba22006-05-09 04:59:56 +000068void AsmPrinter::SwitchToTextSection(const char *NewSection,
69 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000070 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000071 if (GV && GV->hasSection())
Chris Lattnerf8017922006-05-09 16:41:59 +000072 NS = SwitchToSectionDirective + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000073 else
74 NS = NewSection;
75
76 // If we're already in this section, we're done.
77 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000078
Chris Lattner4ebc6a22006-05-09 05:33:48 +000079 // Close the current section, if applicable.
80 if (SectionEndDirectiveSuffix && !CurrentSection.empty())
81 O << CurrentSection << SectionEndDirectiveSuffix << "\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000082
Chris Lattner4ebc6a22006-05-09 05:33:48 +000083 CurrentSection = NS;
84
85 if (!CurrentSection.empty())
86 O << CurrentSection << TextSectionStartSuffix << '\n';
Chris Lattner2ea5c992005-11-21 07:06:27 +000087}
88
Chris Lattner8488ba22006-05-09 04:59:56 +000089/// SwitchToTextSection - Switch to the specified text section of the executable
90/// if we are not already in it!
91///
92void AsmPrinter::SwitchToDataSection(const char *NewSection,
93 const GlobalValue *GV) {
94 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000095 if (GV && GV->hasSection())
96 NS = SwitchToSectionDirective + GV->getSection();
97 else
98 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +000099
Chris Lattner6341df82006-05-09 05:23:12 +0000100 // If we're already in this section, we're done.
101 if (CurrentSection == NS) return;
102
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000103 // Close the current section, if applicable.
104 if (SectionEndDirectiveSuffix && !CurrentSection.empty())
105 O << CurrentSection << SectionEndDirectiveSuffix << "\n";
106
107 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +0000108
Chris Lattner4ebc6a22006-05-09 05:33:48 +0000109 if (!CurrentSection.empty())
110 O << CurrentSection << DataSectionStartSuffix << '\n';
Chris Lattner8488ba22006-05-09 04:59:56 +0000111}
112
113
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000114bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner6d42cbd2004-08-17 06:06:19 +0000115 Mang = new Mangler(M, GlobalPrefix);
Chris Lattnere3a79262006-01-23 23:47:53 +0000116
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000117 if (!M.getModuleInlineAsm().empty())
118 O << CommentString << " Start of file scope inline assembly\n"
119 << M.getModuleInlineAsm()
120 << "\n" << CommentString << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000121
Chris Lattner8488ba22006-05-09 04:59:56 +0000122 SwitchToDataSection("", 0); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000123
124 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
125 DebugInfo->AnalyzeModule(M);
126 }
127
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000128 return false;
129}
130
131bool AsmPrinter::doFinalization(Module &M) {
132 delete Mang; Mang = 0;
133 return false;
134}
135
Chris Lattnerbb644e32005-11-21 07:51:36 +0000136void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000137 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000138 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000139 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000140}
141
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000142/// EmitConstantPool - Print to the current output stream assembly
143/// representations of the constants in the constant pool MCP. This is
144/// used to print out constants which have been "spilled to memory" by
145/// the code generator.
146///
147void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000148 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000149 if (CP.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000150 const TargetData *TD = TM.getTargetData();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000151
Chris Lattner8488ba22006-05-09 04:59:56 +0000152 SwitchToDataSection(ConstantPoolSection, 0);
Chris Lattnerf6190822006-02-09 04:46:04 +0000153 EmitAlignment(MCP->getConstantPoolAlignment());
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000154 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000155 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
Evan Cheng38d5e762006-03-01 22:18:09 +0000156 << ":\t\t\t\t\t" << CommentString << " ";
157 WriteTypeSymbolic(O, CP[i].Val->getType(), 0) << '\n';
Chris Lattnerba972642006-02-09 04:22:52 +0000158 EmitGlobalConstant(CP[i].Val);
Chris Lattnerf6190822006-02-09 04:46:04 +0000159 if (i != e-1) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000160 unsigned EntSize = TM.getTargetData()->getTypeSize(CP[i].Val->getType());
Chris Lattnerf6190822006-02-09 04:46:04 +0000161 unsigned ValEnd = CP[i].Offset + EntSize;
162 // Emit inter-object padding for alignment.
163 EmitZeros(CP[i+1].Offset-ValEnd);
164 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000165 }
166}
167
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000168/// EmitJumpTableInfo - Print assembly representations of the jump tables used
169/// by the current function to the current output stream.
170///
171void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
172 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
173 if (JT.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000174 const TargetData *TD = TM.getTargetData();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000175
176 // FIXME: someday we need to handle PIC jump tables
177 assert((TM.getRelocationModel() == Reloc::Static ||
178 TM.getRelocationModel() == Reloc::DynamicNoPIC) &&
179 "Unhandled relocation model emitting jump table information!");
180
Chris Lattner8488ba22006-05-09 04:59:56 +0000181 SwitchToDataSection(JumpTableSection, 0);
Owen Anderson20a631f2006-05-03 01:29:57 +0000182 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000183 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
184 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << '_' << i
185 << ":\n";
186 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
187 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
188 O << Data32bitsDirective << ' ';
189 printBasicBlockLabel(JTBBs[ii]);
190 O << '\n';
191 }
192 }
193}
194
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000195/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
196/// special global used by LLVM. If so, emit it and return true, otherwise
197/// do nothing and return false.
198bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey313570f2006-03-07 22:00:35 +0000199 // Ignore debug and non-emitted data.
200 if (GV->getSection() == "llvm.metadata") return true;
201
202 if (!GV->hasAppendingLinkage()) return false;
203
204 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000205
206 if (GV->getName() == "llvm.used")
207 return true; // No need to emit this at all.
208
Chris Lattner3760e902006-01-12 19:17:23 +0000209 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000210 SwitchToDataSection(StaticCtorsSection, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000211 EmitAlignment(2, 0);
212 EmitXXStructorList(GV->getInitializer());
213 return true;
214 }
215
Chris Lattner3760e902006-01-12 19:17:23 +0000216 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000217 SwitchToDataSection(StaticDtorsSection, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000218 EmitAlignment(2, 0);
219 EmitXXStructorList(GV->getInitializer());
220 return true;
221 }
222
223 return false;
224}
225
226/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
227/// function pointers, ignoring the init priority.
228void AsmPrinter::EmitXXStructorList(Constant *List) {
229 // Should be an array of '{ int, void ()* }' structs. The first value is the
230 // init priority, which we ignore.
231 if (!isa<ConstantArray>(List)) return;
232 ConstantArray *InitList = cast<ConstantArray>(List);
233 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
234 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
235 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000236
237 if (CS->getOperand(1)->isNullValue())
238 return; // Found a null terminator, exit printing.
239 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000240 EmitGlobalConstant(CS->getOperand(1));
241 }
242}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000243
Chris Lattnera9b25252006-02-05 01:29:18 +0000244/// getPreferredAlignmentLog - Return the preferred alignment of the
245/// specified global, returned in log form. This includes an explicitly
246/// requested alignment (if the global has one).
247unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Owen Anderson20a631f2006-05-03 01:29:57 +0000248 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(GV->getType());
Chris Lattnera9b25252006-02-05 01:29:18 +0000249 if (GV->getAlignment() > (1U << Alignment))
250 Alignment = Log2_32(GV->getAlignment());
251
Chris Lattnercbab2842006-02-05 01:46:49 +0000252 if (GV->hasInitializer()) {
253 // Always round up alignment of global doubles to 8 bytes.
254 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
255 Alignment = 3;
256 if (Alignment < 4) {
257 // If the global is not external, see if it is large. If so, give it a
258 // larger alignment.
Owen Anderson20a631f2006-05-03 01:29:57 +0000259 if (TM.getTargetData()->getTypeSize(GV->getType()->getElementType()) > 128)
Chris Lattnercbab2842006-02-05 01:46:49 +0000260 Alignment = 4; // 16-byte alignment.
261 }
Chris Lattnera9b25252006-02-05 01:29:18 +0000262 }
263 return Alignment;
264}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000265
Chris Lattnerbb644e32005-11-21 07:51:36 +0000266// EmitAlignment - Emit an alignment directive to the specified power of two.
267void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000268 if (GV && GV->getAlignment())
269 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000270 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner1d35c162004-08-17 19:14:29 +0000271 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
272 O << AlignDirective << NumBits << "\n";
273}
274
Chris Lattnerbb644e32005-11-21 07:51:36 +0000275/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000276///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000277void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000278 if (NumZeros) {
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000279 if (ZeroDirective) {
280 O << ZeroDirective << NumZeros;
281 if (ZeroDirectiveSuffix)
282 O << ZeroDirectiveSuffix;
283 O << "\n";
284 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000285 for (; NumZeros; --NumZeros)
286 O << Data8bitsDirective << "0\n";
287 }
288 }
289}
290
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000291// Print out the specified constant, without a storage class. Only the
292// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000293void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000294 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000295 O << "0";
296 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
297 assert(CB == ConstantBool::True);
298 O << "1";
299 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
300 if (((CI->getValue() << 32) >> 32) == CI->getValue())
301 O << CI->getValue();
302 else
Duraid Madina73c4dba2005-05-15 13:05:48 +0000303 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000304 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
305 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000306 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000307 // This is a constant address for a global variable or function. Use the
308 // name of the variable or function as the address value, possibly
309 // decorating it with GlobalVarAddrPrefix/Suffix or
310 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000311 if (isa<Function>(GV))
312 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000313 else
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000314 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000315 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000316 const TargetData *TD = TM.getTargetData();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000317 switch(CE->getOpcode()) {
318 case Instruction::GetElementPtr: {
319 // generate a symbolic expression for the byte address
320 const Constant *ptrVal = CE->getOperand(0);
321 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Anderson20a631f2006-05-03 01:29:57 +0000322 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner145569b2005-02-14 21:40:26 +0000323 if (Offset)
324 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000325 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000326 if (Offset > 0)
327 O << ") + " << Offset;
328 else if (Offset < 0)
329 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000330 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000331 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000332 }
333 break;
334 }
335 case Instruction::Cast: {
336 // Support only non-converting or widening casts for now, that is, ones
337 // that do not involve a change in value. This assertion is really gross,
338 // and may not even be a complete check.
339 Constant *Op = CE->getOperand(0);
340 const Type *OpTy = Op->getType(), *Ty = CE->getType();
341
342 // Remember, kids, pointers can be losslessly converted back and forth
343 // into 32-bit or wider integers, regardless of signedness. :-P
344 assert(((isa<PointerType>(OpTy)
345 && (Ty == Type::LongTy || Ty == Type::ULongTy
346 || Ty == Type::IntTy || Ty == Type::UIntTy))
347 || (isa<PointerType>(Ty)
348 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
349 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Owen Anderson20a631f2006-05-03 01:29:57 +0000350 || (((TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000351 && OpTy->isLosslesslyConvertibleTo(Ty))))
352 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000353 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000354 break;
355 }
356 case Instruction::Add:
357 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000358 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000359 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000360 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000361 O << ")";
362 break;
363 default:
364 assert(0 && "Unsupported operator!");
365 }
366 } else {
367 assert(0 && "Unknown constant value!");
368 }
369}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000370
371/// toOctal - Convert the low order bits of X into an octal digit.
372///
373static inline char toOctal(int X) {
374 return (X&7)+'0';
375}
376
Chris Lattner55a6d902005-11-10 18:06:33 +0000377/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000378/// the predicate isString is true.
379///
Chris Lattner55a6d902005-11-10 18:06:33 +0000380static void printAsCString(std::ostream &O, const ConstantArray *CVA,
381 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000382 assert(CVA->isString() && "Array is not string compatible!");
383
384 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000385 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000386 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000387 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000388
389 if (C == '"') {
390 O << "\\\"";
391 } else if (C == '\\') {
392 O << "\\\\";
393 } else if (isprint(C)) {
394 O << C;
395 } else {
396 switch(C) {
397 case '\b': O << "\\b"; break;
398 case '\f': O << "\\f"; break;
399 case '\n': O << "\\n"; break;
400 case '\r': O << "\\r"; break;
401 case '\t': O << "\\t"; break;
402 default:
403 O << '\\';
404 O << toOctal(C >> 6);
405 O << toOctal(C >> 3);
406 O << toOctal(C >> 0);
407 break;
408 }
409 }
410 }
411 O << "\"";
412}
413
Jeff Cohen24a62a92006-05-02 01:16:28 +0000414/// EmitString - Emit a zero-byte-terminated string constant.
415///
416void AsmPrinter::EmitString(const ConstantArray *CVA) const {
417 unsigned NumElts = CVA->getNumOperands();
418 if (AscizDirective && NumElts &&
419 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
420 O << AscizDirective;
421 printAsCString(O, CVA, NumElts-1);
422 } else {
423 O << AsciiDirective;
424 printAsCString(O, CVA, NumElts);
425 }
426 O << "\n";
427}
428
Chris Lattnerbb644e32005-11-21 07:51:36 +0000429/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000430///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000431void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000432 const TargetData *TD = TM.getTargetData();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000433
Chris Lattner61753bf2004-10-16 18:19:26 +0000434 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000435 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000436 return;
437 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
438 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000439 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000440 } else { // Not a string. Print the values in successive locations
441 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000442 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000443 }
444 return;
445 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
446 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000447 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000448 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000449 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
450 const Constant* field = CVS->getOperand(i);
451
452 // Check if padding is needed and insert one or more 0s.
Owen Anderson20a631f2006-05-03 01:29:57 +0000453 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000454 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000455 : cvsLayout->MemberOffsets[i+1])
456 - cvsLayout->MemberOffsets[i]) - fieldSize;
457 sizeSoFar += fieldSize + padSize;
458
459 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000460 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000461
462 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000463 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000464 }
465 assert(sizeSoFar == cvsLayout->StructSize &&
466 "Layout of constant struct may be incorrect!");
467 return;
468 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
469 // FP Constants are printed as integer constants to avoid losing
470 // precision...
471 double Val = CFP->getValue();
472 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000473 if (Data64bitsDirective)
Jim Laskeyb74c6662005-08-17 19:34:49 +0000474 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattnerea751992004-08-17 21:38:40 +0000475 << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000476 else if (TD->isBigEndian()) {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000477 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000478 << "\t" << CommentString << " double most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000479 << Val << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000480 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000481 << "\t" << CommentString << " double least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000482 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000483 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000484 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000485 << "\t" << CommentString << " double least significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000486 << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000487 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000488 << "\t" << CommentString << " double most significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000489 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000490 }
491 return;
492 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000493 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattnerda6beac2004-08-17 16:27:05 +0000494 << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000495 return;
496 }
497 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
498 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
499 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000500
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000501 if (Data64bitsDirective)
502 O << Data64bitsDirective << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000503 else if (TD->isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000504 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000505 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000506 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000507 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000508 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000509 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000510 } else {
511 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000512 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000513 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000514 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000515 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000516 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000517 }
518 return;
519 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000520 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
521 const PackedType *PTy = CP->getType();
522
523 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
524 EmitGlobalConstant(CP->getOperand(I));
525
526 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000527 }
528
529 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000530 switch (type->getTypeID()) {
Misha Brukman835702a2005-04-21 22:36:52 +0000531 case Type::BoolTyID:
Chris Lattner8452a1f2004-08-17 06:36:49 +0000532 case Type::UByteTyID: case Type::SByteTyID:
533 O << Data8bitsDirective;
534 break;
535 case Type::UShortTyID: case Type::ShortTyID:
536 O << Data16bitsDirective;
537 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000538 case Type::PointerTyID:
Owen Anderson20a631f2006-05-03 01:29:57 +0000539 if (TD->getPointerSize() == 8) {
Andrew Lenharthc8770aa2005-02-04 13:47:16 +0000540 O << Data64bitsDirective;
541 break;
542 }
543 //Fall through for pointer size == int size
Chris Lattner8452a1f2004-08-17 06:36:49 +0000544 case Type::UIntTyID: case Type::IntTyID:
545 O << Data32bitsDirective;
546 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000547 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner88e2d2e2005-08-08 04:26:32 +0000548 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
549 O << Data64bitsDirective;
550 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000551 case Type::FloatTyID: case Type::DoubleTyID:
552 assert (0 && "Should have already output floating point constant.");
553 default:
554 assert (0 && "Can't handle printing this type of thing");
555 break;
556 }
Chris Lattnerbb644e32005-11-21 07:51:36 +0000557 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000558 O << "\n";
559}
Chris Lattner061d9e22006-01-27 02:10:10 +0000560
561/// printInlineAsm - This method formats and prints the specified machine
562/// instruction that is an inline asm.
563void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnera633c3132006-05-05 21:47:05 +0000564 O << InlineAsmStart << "\n\t";
Chris Lattner57ecb562006-01-30 23:00:08 +0000565 unsigned NumOperands = MI->getNumOperands();
566
567 // Count the number of register definitions.
568 unsigned NumDefs = 0;
569 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
570 assert(NumDefs != NumOperands-1 && "No asm string?");
571
572 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000573
574 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000575 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000576
577 // The variant of the current asmprinter: FIXME: change.
578 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000579
Chris Lattneraa23fa92006-02-01 22:41:11 +0000580 int CurVariant = -1; // The number of the {.|.|.} region we are in.
581 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000582
Chris Lattneraa23fa92006-02-01 22:41:11 +0000583 while (*LastEmitted) {
584 switch (*LastEmitted) {
585 default: {
586 // Not a special case, emit the string section literally.
587 const char *LiteralEnd = LastEmitted+1;
588 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +0000589 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +0000590 ++LiteralEnd;
591 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
592 O.write(LastEmitted, LiteralEnd-LastEmitted);
593 LastEmitted = LiteralEnd;
594 break;
595 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000596 case '\n':
597 ++LastEmitted; // Consume newline character.
598 O << "\n\t"; // Indent code with newline.
599 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000600 case '$': {
601 ++LastEmitted; // Consume '$' character.
602 if (*LastEmitted == '$') { // $$ -> $
603 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
604 O << '$';
605 ++LastEmitted; // Consume second '$' character.
606 break;
607 }
608
609 bool HasCurlyBraces = false;
610 if (*LastEmitted == '{') { // ${variable}
611 ++LastEmitted; // Consume '{' character.
612 HasCurlyBraces = true;
613 }
614
615 const char *IDStart = LastEmitted;
616 char *IDEnd;
617 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
618 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
619 std::cerr << "Bad $ operand number in inline asm string: '"
620 << AsmStr << "'\n";
621 exit(1);
622 }
623 LastEmitted = IDEnd;
624
Chris Lattner34f74c12006-02-06 22:17:23 +0000625 char Modifier[2] = { 0, 0 };
626
Chris Lattneraa23fa92006-02-01 22:41:11 +0000627 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000628 // If we have curly braces, check for a modifier character. This
629 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
630 if (*LastEmitted == ':') {
631 ++LastEmitted; // Consume ':' character.
632 if (*LastEmitted == 0) {
633 std::cerr << "Bad ${:} expression in inline asm string: '"
634 << AsmStr << "'\n";
635 exit(1);
636 }
637
638 Modifier[0] = *LastEmitted;
639 ++LastEmitted; // Consume modifier character.
640 }
641
Chris Lattneraa23fa92006-02-01 22:41:11 +0000642 if (*LastEmitted != '}') {
643 std::cerr << "Bad ${} expression in inline asm string: '"
644 << AsmStr << "'\n";
645 exit(1);
646 }
647 ++LastEmitted; // Consume '}' character.
648 }
649
650 if ((unsigned)Val >= NumOperands-1) {
651 std::cerr << "Invalid $ operand number in inline asm string: '"
652 << AsmStr << "'\n";
653 exit(1);
654 }
655
Chris Lattner571d9642006-02-23 19:21:04 +0000656 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +0000657 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +0000658 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
659 unsigned OpNo = 1;
660
661 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +0000662 for (; Val; --Val) {
663 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
664 OpNo += (OpFlags >> 3) + 1;
665 }
Chris Lattner571d9642006-02-23 19:21:04 +0000666
Chris Lattner1d08c652006-02-24 20:21:58 +0000667 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
Chris Lattner571d9642006-02-23 19:21:04 +0000668 ++OpNo; // Skip over the ID number.
Chris Lattner1d08c652006-02-24 20:21:58 +0000669
670 bool Error;
671 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
672 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
673 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
674 Modifier[0] ? Modifier : 0);
675 } else {
676 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
677 Modifier[0] ? Modifier : 0);
678 }
679 if (Error) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000680 std::cerr << "Invalid operand found in inline asm: '"
681 << AsmStr << "'\n";
682 MI->dump();
683 exit(1);
684 }
Chris Lattner571d9642006-02-23 19:21:04 +0000685 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000686 break;
687 }
688 case '{':
689 ++LastEmitted; // Consume '{' character.
690 if (CurVariant != -1) {
691 std::cerr << "Nested variants found in inline asm string: '"
692 << AsmStr << "'\n";
693 exit(1);
694 }
695 CurVariant = 0; // We're in the first variant now.
696 break;
697 case '|':
698 ++LastEmitted; // consume '|' character.
699 if (CurVariant == -1) {
700 std::cerr << "Found '|' character outside of variant in inline asm "
701 << "string: '" << AsmStr << "'\n";
702 exit(1);
703 }
704 ++CurVariant; // We're in the next variant.
705 break;
706 case '}':
707 ++LastEmitted; // consume '}' character.
708 if (CurVariant == -1) {
709 std::cerr << "Found '}' character outside of variant in inline asm "
710 << "string: '" << AsmStr << "'\n";
711 exit(1);
712 }
713 CurVariant = -1;
714 break;
715 }
716 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000717 O << "\n\t" << InlineAsmEnd << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000718}
719
720/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
721/// instruction, using the specified assembler variant. Targets should
722/// overried this to format as appropriate.
723bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000724 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000725 // Target doesn't support this yet!
726 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000727}
Chris Lattner1d08c652006-02-24 20:21:58 +0000728
729bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
730 unsigned AsmVariant,
731 const char *ExtraCode) {
732 // Target doesn't support this yet!
733 return true;
734}
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000735
736/// printBasicBlockLabel - This method prints the label for the specified
737/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +0000738void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
739 bool printColon,
740 bool printComment) const {
Nate Begeman4971ba52006-05-02 17:36:46 +0000741 O << PrivateGlobalPrefix << "BB" << FunctionNumber << "_"
742 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +0000743 if (printColon)
744 O << ':';
745 if (printComment)
746 O << '\t' << CommentString << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000747}