blob: 9a740692b9becd39f8443c1875692e949cc2c0ce [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"
Jim Laskeya6211dc2006-09-06 18:34:40 +000023#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000024#include "llvm/Target/TargetData.h"
Chris Lattner90438232006-10-06 22:50:56 +000025#include "llvm/Target/TargetLowering.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000026#include "llvm/Target/TargetMachine.h"
Duraid Madina26b037e2005-12-28 06:29:02 +000027#include <iostream>
Chris Lattneraa23fa92006-02-01 22:41:11 +000028#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000029using namespace llvm;
30
Jim Laskey261779b2006-09-07 22:06:40 +000031AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
32 const TargetAsmInfo *T)
Jim Laskeya6211dc2006-09-06 18:34:40 +000033: FunctionNumber(0), O(o), TM(tm), TAI(T)
34{}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000035
Chris Lattnerdc822412006-10-05 02:42:47 +000036std::string AsmPrinter::getSectionForFunction(const Function &F) const {
37 return TAI->getTextSection();
38}
39
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000040
Chris Lattner8488ba22006-05-09 04:59:56 +000041/// SwitchToTextSection - Switch to the specified text section of the executable
42/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000043///
Chris Lattner8488ba22006-05-09 04:59:56 +000044void AsmPrinter::SwitchToTextSection(const char *NewSection,
45 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000046 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000047 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000048 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000049 else
50 NS = NewSection;
51
52 // If we're already in this section, we're done.
53 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000054
Chris Lattner4ebc6a22006-05-09 05:33:48 +000055 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000056 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
57 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000058
Chris Lattner4ebc6a22006-05-09 05:33:48 +000059 CurrentSection = NS;
60
61 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000062 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattner2ea5c992005-11-21 07:06:27 +000063}
64
Nate Begeman78756502006-07-27 01:13:04 +000065/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-05-09 04:59:56 +000066/// if we are not already in it!
67///
68void AsmPrinter::SwitchToDataSection(const char *NewSection,
69 const GlobalValue *GV) {
70 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000071 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000072 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner6341df82006-05-09 05:23:12 +000073 else
74 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +000075
Chris Lattner6341df82006-05-09 05:23:12 +000076 // If we're already in this section, we're done.
77 if (CurrentSection == NS) return;
78
Chris Lattner4ebc6a22006-05-09 05:33:48 +000079 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000080 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
81 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner4ebc6a22006-05-09 05:33:48 +000082
83 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +000084
Chris Lattner4ebc6a22006-05-09 05:33:48 +000085 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000086 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner8488ba22006-05-09 04:59:56 +000087}
88
89
Chris Lattner6a8e0f52004-08-16 23:15:22 +000090bool AsmPrinter::doInitialization(Module &M) {
Jim Laskeya6211dc2006-09-06 18:34:40 +000091 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +000092
Chris Lattner00fcdfe2006-01-24 04:16:34 +000093 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000094 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +000095 << M.getModuleInlineAsm()
Jim Laskeya6211dc2006-09-06 18:34:40 +000096 << "\n" << TAI->getCommentString()
97 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +000098
Chris Lattner8488ba22006-05-09 04:59:56 +000099 SwitchToDataSection("", 0); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000100
101 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
102 DebugInfo->AnalyzeModule(M);
103 }
104
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000105 return false;
106}
107
108bool AsmPrinter::doFinalization(Module &M) {
109 delete Mang; Mang = 0;
110 return false;
111}
112
Chris Lattnerbb644e32005-11-21 07:51:36 +0000113void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000114 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000115 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000116 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000117}
118
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000119/// EmitConstantPool - Print to the current output stream assembly
120/// representations of the constants in the constant pool MCP. This is
121/// used to print out constants which have been "spilled to memory" by
122/// the code generator.
123///
124void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000125 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000126 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000127
128 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
129 // in special sections.
130 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
131 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
132 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
133 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000134 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000135 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000136 MachineConstantPoolEntry CPE = CP[i];
Evan Cheng2ad050f2006-09-14 07:35:00 +0000137 const Type *Ty = CPE.getType();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000138 if (TAI->getFourByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000139 TM.getTargetData()->getTypeSize(Ty) == 4)
140 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000141 else if (TAI->getEightByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000142 TM.getTargetData()->getTypeSize(Ty) == 8)
143 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000144 else if (TAI->getSixteenByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000145 TM.getTargetData()->getTypeSize(Ty) == 16)
146 SixteenByteCPs.push_back(std::make_pair(CPE, i));
147 else
148 OtherCPs.push_back(std::make_pair(CPE, i));
149 }
150
151 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000152 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
153 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
154 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
155 SixteenByteCPs);
156 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Chengec1d60b2006-06-29 00:26:09 +0000157}
158
159void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
160 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
161 if (CP.empty()) return;
162
163 SwitchToDataSection(Section, 0);
164 EmitAlignment(Alignment);
165 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000166 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
167 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Cheng2ad050f2006-09-14 07:35:00 +0000168 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
169 if (CP[i].first.isMachineConstantPoolEntry())
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000170 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000171 else
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000172 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000173 if (i != e-1) {
Evan Cheng2ad050f2006-09-14 07:35:00 +0000174 const Type *Ty = CP[i].first.getType();
Evan Chengec1d60b2006-06-29 00:26:09 +0000175 unsigned EntSize =
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000176 TM.getTargetData()->getTypeSize(Ty);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000177 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattnerf6190822006-02-09 04:46:04 +0000178 // Emit inter-object padding for alignment.
Evan Cheng2ad050f2006-09-14 07:35:00 +0000179 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattnerf6190822006-02-09 04:46:04 +0000180 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000181 }
182}
183
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000184/// EmitJumpTableInfo - Print assembly representations of the jump tables used
185/// by the current function to the current output stream.
186///
Chris Lattnera6a570e2006-10-05 03:01:21 +0000187void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
188 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000189 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
190 if (JT.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000191 const TargetData *TD = TM.getTargetData();
Nate Begemanefc312a2006-07-27 16:46:58 +0000192
193 // JTEntryDirective is a string to print sizeof(ptr) for non-PIC jump tables,
194 // and 32 bits for PIC since PIC jump table entries are differences, not
195 // pointers to blocks.
Andrew Lenharth783a4a92006-09-24 19:45:58 +0000196 // Use the architecture specific relocation directive, if it is set
197 const char *JTEntryDirective = TAI->getJumpTableDirective();
198 if (!JTEntryDirective)
199 JTEntryDirective = TAI->getData32bitsDirective();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000200
Nate Begeman78756502006-07-27 01:13:04 +0000201 // Pick the directive to use to print the jump table entries, and switch to
202 // the appropriate section.
203 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner90438232006-10-06 22:50:56 +0000204 TargetLowering *LoweringInfo = TM.getTargetLowering();
205 if (LoweringInfo && LoweringInfo->usesGlobalOffsetTable()) {
206 SwitchToDataSection(TAI->getJumpTableDataSection(), 0);
Andrew Lenhartha6bbf332006-10-11 04:29:42 +0000207 if (TD->getPointerSize() == 8 && !JTEntryDirective)
Chris Lattner90438232006-10-06 22:50:56 +0000208 JTEntryDirective = TAI->getData64bitsDirective();
209 } else {
210 // In PIC mode, we need to emit the jump table to the same section as the
211 // function body itself, otherwise the label differences won't make sense.
212 const Function *F = MF.getFunction();
213 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
214 }
Nate Begeman78756502006-07-27 01:13:04 +0000215 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000216 SwitchToDataSection(TAI->getJumpTableDataSection(), 0);
Nate Begeman78756502006-07-27 01:13:04 +0000217 if (TD->getPointerSize() == 8)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000218 JTEntryDirective = TAI->getData64bitsDirective();
Nate Begeman78756502006-07-27 01:13:04 +0000219 }
Owen Anderson20a631f2006-05-03 01:29:57 +0000220 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000221
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000222 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000223 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
224
225 // For PIC codegen, if possible we want to use the SetDirective to reduce
226 // the number of relocations the assembler will generate for the jump table.
227 // Set directives are all printed before the jump table itself.
228 std::set<MachineBasicBlock*> EmittedSets;
Jim Laskeya6211dc2006-09-06 18:34:40 +0000229 if (TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_)
Nate Begeman984c1a42006-08-12 21:29:52 +0000230 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
231 if (EmittedSets.insert(JTBBs[ii]).second)
232 printSetLabel(i, JTBBs[ii]);
233
Jim Laskeya6211dc2006-09-06 18:34:40 +0000234 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
235 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000236
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000237 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begemanefc312a2006-07-27 16:46:58 +0000238 O << JTEntryDirective << ' ';
Nate Begeman984c1a42006-08-12 21:29:52 +0000239 // If we have emitted set directives for the jump table entries, print
240 // them rather than the entries themselves. If we're emitting PIC, then
241 // emit the table entries as differences between two text section labels.
242 // If we're emitting non-PIC code, then emit the entries as direct
243 // references to the target basic blocks.
244 if (!EmittedSets.empty()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000245 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
246 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Nate Begeman984c1a42006-08-12 21:29:52 +0000247 } else if (TM.getRelocationModel() == Reloc::PIC_) {
248 printBasicBlockLabel(JTBBs[ii], false, false);
Andrew Lenharth783a4a92006-09-24 19:45:58 +0000249 //If the arch uses custom Jump Table directives, don't calc relative to JT
250 if (!TAI->getJumpTableDirective())
251 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
252 << getFunctionNumber() << '_' << i;
Nate Begeman984c1a42006-08-12 21:29:52 +0000253 } else {
254 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman78756502006-07-27 01:13:04 +0000255 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000256 O << '\n';
257 }
258 }
259}
260
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000261/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
262/// special global used by LLVM. If so, emit it and return true, otherwise
263/// do nothing and return false.
264bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey313570f2006-03-07 22:00:35 +0000265 // Ignore debug and non-emitted data.
266 if (GV->getSection() == "llvm.metadata") return true;
267
268 if (!GV->hasAppendingLinkage()) return false;
269
270 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000271
Chris Lattner66af3902006-09-26 03:38:18 +0000272 if (GV->getName() == "llvm.used") {
273 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
274 EmitLLVMUsedList(GV->getInitializer());
275 return true;
276 }
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000277
Chris Lattner3760e902006-01-12 19:17:23 +0000278 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000279 SwitchToDataSection(TAI->getStaticCtorsSection(), 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000280 EmitAlignment(2, 0);
281 EmitXXStructorList(GV->getInitializer());
282 return true;
283 }
284
Chris Lattner3760e902006-01-12 19:17:23 +0000285 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000286 SwitchToDataSection(TAI->getStaticDtorsSection(), 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000287 EmitAlignment(2, 0);
288 EmitXXStructorList(GV->getInitializer());
289 return true;
290 }
291
292 return false;
293}
294
Chris Lattner66af3902006-09-26 03:38:18 +0000295/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
296/// global in the specified llvm.used list as being used with this directive.
297void AsmPrinter::EmitLLVMUsedList(Constant *List) {
298 const char *Directive = TAI->getUsedDirective();
299
300 // Should be an array of 'sbyte*'.
301 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
302 if (InitList == 0) return;
303
304 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
305 O << Directive;
306 EmitConstantValueOnly(InitList->getOperand(i));
307 O << "\n";
308 }
309}
310
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000311/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
312/// function pointers, ignoring the init priority.
313void AsmPrinter::EmitXXStructorList(Constant *List) {
314 // Should be an array of '{ int, void ()* }' structs. The first value is the
315 // init priority, which we ignore.
316 if (!isa<ConstantArray>(List)) return;
317 ConstantArray *InitList = cast<ConstantArray>(List);
318 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
319 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
320 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000321
322 if (CS->getOperand(1)->isNullValue())
323 return; // Found a null terminator, exit printing.
324 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000325 EmitGlobalConstant(CS->getOperand(1));
326 }
327}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000328
Jim Laskey71262542006-10-17 13:41:07 +0000329/// getGlobalLinkName - Returns the asm/link name of of the specified
330/// global variable. Should be overridden by each target asm printer to
331/// generate the appropriate value.
Jim Laskeyd24b9132006-10-17 17:17:24 +0000332const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{
333 std::string LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000334 // Default action is to use a global symbol.
335 LinkName = TAI->getGlobalPrefix();
336 LinkName += GV->getName();
Jim Laskeyd24b9132006-10-17 17:17:24 +0000337 return LinkName;
Jim Laskey71262542006-10-17 13:41:07 +0000338}
339
Chris Lattnerbb644e32005-11-21 07:51:36 +0000340// EmitAlignment - Emit an alignment directive to the specified power of two.
341void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000342 if (GV && GV->getAlignment())
343 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000344 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000345 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
346 O << TAI->getAlignDirective() << NumBits << "\n";
Chris Lattner1d35c162004-08-17 19:14:29 +0000347}
348
Chris Lattnerbb644e32005-11-21 07:51:36 +0000349/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000350///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000351void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000352 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000353 if (TAI->getZeroDirective()) {
354 O << TAI->getZeroDirective() << NumZeros;
355 if (TAI->getZeroDirectiveSuffix())
356 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000357 O << "\n";
358 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000359 for (; NumZeros; --NumZeros)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000360 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000361 }
362 }
363}
364
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000365// Print out the specified constant, without a storage class. Only the
366// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000367void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000368 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000369 O << "0";
370 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
Chris Lattnereea983e2006-09-28 23:17:41 +0000371 assert(CB->getValue());
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000372 O << "1";
Reid Spencere0fc4df2006-10-20 07:07:24 +0000373 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
374 if (CI->getType()->isSigned()) {
375 if (((CI->getSExtValue() << 32) >> 32) == CI->getSExtValue())
376 O << CI->getSExtValue();
377 else
378 O << (uint64_t)CI->getSExtValue();
379 } else
380 O << CI->getZExtValue();
381 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000382 // This is a constant address for a global variable or function. Use the
383 // name of the variable or function as the address value, possibly
384 // decorating it with GlobalVarAddrPrefix/Suffix or
385 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000386 if (isa<Function>(GV)) {
387 O << TAI->getFunctionAddrPrefix()
388 << Mang->getValueName(GV)
389 << TAI->getFunctionAddrSuffix();
390 } else {
391 O << TAI->getGlobalVarAddrPrefix()
392 << Mang->getValueName(GV)
393 << TAI->getGlobalVarAddrSuffix();
394 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000395 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000396 const TargetData *TD = TM.getTargetData();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000397 switch(CE->getOpcode()) {
398 case Instruction::GetElementPtr: {
399 // generate a symbolic expression for the byte address
400 const Constant *ptrVal = CE->getOperand(0);
401 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Anderson20a631f2006-05-03 01:29:57 +0000402 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner145569b2005-02-14 21:40:26 +0000403 if (Offset)
404 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000405 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000406 if (Offset > 0)
407 O << ") + " << Offset;
408 else if (Offset < 0)
409 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000410 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000411 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000412 }
413 break;
414 }
415 case Instruction::Cast: {
Chris Lattner85492422006-07-29 01:57:19 +0000416 // Support only foldable casts to/from pointers that can be eliminated by
417 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000418 Constant *Op = CE->getOperand(0);
419 const Type *OpTy = Op->getType(), *Ty = CE->getType();
420
Chris Lattner85492422006-07-29 01:57:19 +0000421 // Handle casts to pointers by changing them into casts to the appropriate
422 // integer type. This promotes constant folding and simplifies this code.
423 if (isa<PointerType>(Ty)) {
424 const Type *IntPtrTy = TD->getIntPtrType();
425 Op = ConstantExpr::getCast(Op, IntPtrTy);
426 return EmitConstantValueOnly(Op);
427 }
428
429 // We know the dest type is not a pointer. Is the src value a pointer or
430 // integral?
431 if (isa<PointerType>(OpTy) || OpTy->isIntegral()) {
432 // We can emit the pointer value into this slot if the slot is an
433 // integer slot greater or equal to the size of the pointer.
434 if (Ty->isIntegral() && TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
435 return EmitConstantValueOnly(Op);
436 }
437
438 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000439 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000440 break;
441 }
442 case Instruction::Add:
443 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000444 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000445 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000446 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000447 O << ")";
448 break;
449 default:
450 assert(0 && "Unsupported operator!");
451 }
452 } else {
453 assert(0 && "Unknown constant value!");
454 }
455}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000456
457/// toOctal - Convert the low order bits of X into an octal digit.
458///
459static inline char toOctal(int X) {
460 return (X&7)+'0';
461}
462
Chris Lattner55a6d902005-11-10 18:06:33 +0000463/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000464/// the predicate isString is true.
465///
Chris Lattner55a6d902005-11-10 18:06:33 +0000466static void printAsCString(std::ostream &O, const ConstantArray *CVA,
467 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000468 assert(CVA->isString() && "Array is not string compatible!");
469
470 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000471 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000472 unsigned char C =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000473 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000474
475 if (C == '"') {
476 O << "\\\"";
477 } else if (C == '\\') {
478 O << "\\\\";
479 } else if (isprint(C)) {
480 O << C;
481 } else {
482 switch(C) {
483 case '\b': O << "\\b"; break;
484 case '\f': O << "\\f"; break;
485 case '\n': O << "\\n"; break;
486 case '\r': O << "\\r"; break;
487 case '\t': O << "\\t"; break;
488 default:
489 O << '\\';
490 O << toOctal(C >> 6);
491 O << toOctal(C >> 3);
492 O << toOctal(C >> 0);
493 break;
494 }
495 }
496 }
497 O << "\"";
498}
499
Jeff Cohen24a62a92006-05-02 01:16:28 +0000500/// EmitString - Emit a zero-byte-terminated string constant.
501///
502void AsmPrinter::EmitString(const ConstantArray *CVA) const {
503 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000504 if (TAI->getAscizDirective() && NumElts &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000505 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000506 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000507 printAsCString(O, CVA, NumElts-1);
508 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000509 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000510 printAsCString(O, CVA, NumElts);
511 }
512 O << "\n";
513}
514
Chris Lattnerbb644e32005-11-21 07:51:36 +0000515/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000516///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000517void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000518 const TargetData *TD = TM.getTargetData();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000519
Chris Lattner61753bf2004-10-16 18:19:26 +0000520 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000521 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000522 return;
523 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
524 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000525 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000526 } else { // Not a string. Print the values in successive locations
527 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000528 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000529 }
530 return;
531 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
532 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000533 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000534 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000535 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
536 const Constant* field = CVS->getOperand(i);
537
538 // Check if padding is needed and insert one or more 0s.
Owen Anderson20a631f2006-05-03 01:29:57 +0000539 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000540 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000541 : cvsLayout->MemberOffsets[i+1])
542 - cvsLayout->MemberOffsets[i]) - fieldSize;
543 sizeSoFar += fieldSize + padSize;
544
545 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000546 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000547
548 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000549 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000550 }
551 assert(sizeSoFar == cvsLayout->StructSize &&
552 "Layout of constant struct may be incorrect!");
553 return;
554 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
555 // FP Constants are printed as integer constants to avoid losing
556 // precision...
557 double Val = CFP->getValue();
558 if (CFP->getType() == Type::DoubleTy) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000559 if (TAI->getData64bitsDirective())
560 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
561 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000562 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000563 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
564 << "\t" << TAI->getCommentString()
565 << " double most significant word " << Val << "\n";
566 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
567 << "\t" << TAI->getCommentString()
568 << " double least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000569 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000570 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
571 << "\t" << TAI->getCommentString()
572 << " double least significant word " << Val << "\n";
573 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
574 << "\t" << TAI->getCommentString()
575 << " double most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000576 }
577 return;
578 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000579 O << TAI->getData32bitsDirective() << FloatToBits(Val)
580 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000581 return;
582 }
583 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
584 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000585 uint64_t Val = CI->getZExtValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000586
Jim Laskeya6211dc2006-09-06 18:34:40 +0000587 if (TAI->getData64bitsDirective())
588 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000589 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000590 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
591 << "\t" << TAI->getCommentString()
592 << " Double-word most significant word " << Val << "\n";
593 O << TAI->getData32bitsDirective() << unsigned(Val)
594 << "\t" << TAI->getCommentString()
595 << " Double-word least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000596 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000597 O << TAI->getData32bitsDirective() << unsigned(Val)
598 << "\t" << TAI->getCommentString()
599 << " Double-word least significant word " << Val << "\n";
600 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
601 << "\t" << TAI->getCommentString()
602 << " Double-word most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000603 }
604 return;
605 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000606 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
607 const PackedType *PTy = CP->getType();
608
609 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
610 EmitGlobalConstant(CP->getOperand(I));
611
612 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000613 }
614
615 const Type *type = CV->getType();
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000616 printDataDirective(type);
Chris Lattnerbb644e32005-11-21 07:51:36 +0000617 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000618 O << "\n";
619}
Chris Lattner061d9e22006-01-27 02:10:10 +0000620
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000621void
622AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
623 // Target doesn't support this yet!
624 abort();
625}
626
Chris Lattnera32814b2006-09-26 23:59:50 +0000627/// PrintSpecial - Print information related to the specified machine instr
628/// that is independent of the operand, and may be independent of the instr
629/// itself. This can be useful for portably encoding the comment character
630/// or other bits of target-specific knowledge into the asmstrings. The
631/// syntax used is ${:comment}. Targets can override this to add support
632/// for their own strange codes.
633void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattner30b47082006-09-27 00:06:07 +0000634 if (!strcmp(Code, "private")) {
635 O << TAI->getPrivateGlobalPrefix();
636 } else if (!strcmp(Code, "comment")) {
Chris Lattnera32814b2006-09-26 23:59:50 +0000637 O << TAI->getCommentString();
638 } else if (!strcmp(Code, "uid")) {
639 // Assign a unique ID to this machine instruction.
640 static const MachineInstr *LastMI = 0;
641 static unsigned Counter = 0U-1;
642 // If this is a new machine instruction, bump the counter.
643 if (LastMI != MI) { ++Counter; LastMI = MI; }
644 O << Counter;
645 } else {
646 std::cerr << "Unknown special formatter '" << Code
647 << "' for machine instr: " << *MI;
648 exit(1);
649 }
650}
651
652
Chris Lattner061d9e22006-01-27 02:10:10 +0000653/// printInlineAsm - This method formats and prints the specified machine
654/// instruction that is an inline asm.
655void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +0000656 unsigned NumOperands = MI->getNumOperands();
657
658 // Count the number of register definitions.
659 unsigned NumDefs = 0;
Chris Lattner45456d42006-09-05 20:02:51 +0000660 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
661 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +0000662 assert(NumDefs != NumOperands-1 && "No asm string?");
663
664 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000665
666 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000667 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000668
Chris Lattner3390cbe2006-07-28 00:17:20 +0000669 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
670 if (AsmStr[0] == 0) {
671 O << "\n"; // Tab already printed, avoid double indenting next instr.
672 return;
673 }
674
Jim Laskeya6211dc2006-09-06 18:34:40 +0000675 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +0000676
Chris Lattneraa23fa92006-02-01 22:41:11 +0000677 // The variant of the current asmprinter: FIXME: change.
678 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000679
Chris Lattneraa23fa92006-02-01 22:41:11 +0000680 int CurVariant = -1; // The number of the {.|.|.} region we are in.
681 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000682
Chris Lattneraa23fa92006-02-01 22:41:11 +0000683 while (*LastEmitted) {
684 switch (*LastEmitted) {
685 default: {
686 // Not a special case, emit the string section literally.
687 const char *LiteralEnd = LastEmitted+1;
688 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +0000689 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +0000690 ++LiteralEnd;
691 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
692 O.write(LastEmitted, LiteralEnd-LastEmitted);
693 LastEmitted = LiteralEnd;
694 break;
695 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000696 case '\n':
697 ++LastEmitted; // Consume newline character.
698 O << "\n\t"; // Indent code with newline.
699 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000700 case '$': {
701 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000702 bool Done = true;
703
704 // Handle escapes.
705 switch (*LastEmitted) {
706 default: Done = false; break;
707 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +0000708 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
709 O << '$';
710 ++LastEmitted; // Consume second '$' character.
711 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000712 case '(': // $( -> same as GCC's { character.
713 ++LastEmitted; // Consume '(' character.
714 if (CurVariant != -1) {
715 std::cerr << "Nested variants found in inline asm string: '"
716 << AsmStr << "'\n";
717 exit(1);
718 }
719 CurVariant = 0; // We're in the first variant now.
720 break;
721 case '|':
722 ++LastEmitted; // consume '|' character.
723 if (CurVariant == -1) {
724 std::cerr << "Found '|' character outside of variant in inline asm "
725 << "string: '" << AsmStr << "'\n";
726 exit(1);
727 }
728 ++CurVariant; // We're in the next variant.
729 break;
730 case ')': // $) -> same as GCC's } char.
731 ++LastEmitted; // consume ')' character.
732 if (CurVariant == -1) {
733 std::cerr << "Found '}' character outside of variant in inline asm "
734 << "string: '" << AsmStr << "'\n";
735 exit(1);
736 }
737 CurVariant = -1;
738 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000739 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000740 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000741
742 bool HasCurlyBraces = false;
743 if (*LastEmitted == '{') { // ${variable}
744 ++LastEmitted; // Consume '{' character.
745 HasCurlyBraces = true;
746 }
747
748 const char *IDStart = LastEmitted;
749 char *IDEnd;
750 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
751 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
752 std::cerr << "Bad $ operand number in inline asm string: '"
753 << AsmStr << "'\n";
754 exit(1);
755 }
756 LastEmitted = IDEnd;
757
Chris Lattner34f74c12006-02-06 22:17:23 +0000758 char Modifier[2] = { 0, 0 };
759
Chris Lattneraa23fa92006-02-01 22:41:11 +0000760 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000761 // If we have curly braces, check for a modifier character. This
762 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
763 if (*LastEmitted == ':') {
764 ++LastEmitted; // Consume ':' character.
765 if (*LastEmitted == 0) {
766 std::cerr << "Bad ${:} expression in inline asm string: '"
767 << AsmStr << "'\n";
768 exit(1);
769 }
770
771 Modifier[0] = *LastEmitted;
772 ++LastEmitted; // Consume modifier character.
773 }
774
Chris Lattneraa23fa92006-02-01 22:41:11 +0000775 if (*LastEmitted != '}') {
776 std::cerr << "Bad ${} expression in inline asm string: '"
777 << AsmStr << "'\n";
778 exit(1);
779 }
780 ++LastEmitted; // Consume '}' character.
781 }
782
783 if ((unsigned)Val >= NumOperands-1) {
784 std::cerr << "Invalid $ operand number in inline asm string: '"
785 << AsmStr << "'\n";
786 exit(1);
787 }
788
Chris Lattner571d9642006-02-23 19:21:04 +0000789 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +0000790 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +0000791 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
792 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000793
794 bool Error = false;
795
Chris Lattner571d9642006-02-23 19:21:04 +0000796 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +0000797 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000798 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner5af3fde2006-02-24 19:50:58 +0000799 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
800 OpNo += (OpFlags >> 3) + 1;
801 }
Chris Lattner1d08c652006-02-24 20:21:58 +0000802
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000803 if (OpNo >= MI->getNumOperands()) {
804 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +0000805 } else {
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000806 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
807 ++OpNo; // Skip over the ID number.
808
809 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
810 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
811 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
812 Modifier[0] ? Modifier : 0);
813 } else {
814 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
815 Modifier[0] ? Modifier : 0);
816 }
Chris Lattner1d08c652006-02-24 20:21:58 +0000817 }
818 if (Error) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000819 std::cerr << "Invalid operand found in inline asm: '"
820 << AsmStr << "'\n";
821 MI->dump();
822 exit(1);
823 }
Chris Lattner571d9642006-02-23 19:21:04 +0000824 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000825 break;
826 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000827 }
828 }
Jim Laskeya6211dc2006-09-06 18:34:40 +0000829 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000830}
831
832/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
833/// instruction, using the specified assembler variant. Targets should
834/// overried this to format as appropriate.
835bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000836 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000837 // Target doesn't support this yet!
838 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000839}
Chris Lattner1d08c652006-02-24 20:21:58 +0000840
841bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
842 unsigned AsmVariant,
843 const char *ExtraCode) {
844 // Target doesn't support this yet!
845 return true;
846}
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000847
848/// printBasicBlockLabel - This method prints the label for the specified
849/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +0000850void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
851 bool printColon,
852 bool printComment) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000853 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
Nate Begeman4971ba52006-05-02 17:36:46 +0000854 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +0000855 if (printColon)
856 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +0000857 if (printComment && MBB->getBasicBlock())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000858 O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000859}
Nate Begeman984c1a42006-08-12 21:29:52 +0000860
861/// printSetLabel - This method prints a set label for the specified
862/// MachineBasicBlock
863void AsmPrinter::printSetLabel(unsigned uid,
864 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000865 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +0000866 return;
867
Jim Laskeya6211dc2006-09-06 18:34:40 +0000868 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
869 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman984c1a42006-08-12 21:29:52 +0000870 printBasicBlockLabel(MBB, false, false);
Jim Laskeya6211dc2006-09-06 18:34:40 +0000871 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman984c1a42006-08-12 21:29:52 +0000872 << '_' << uid << '\n';
873}
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000874
875/// printDataDirective - This method prints the asm directive for the
876/// specified type.
877void AsmPrinter::printDataDirective(const Type *type) {
878 const TargetData *TD = TM.getTargetData();
879 switch (type->getTypeID()) {
880 case Type::BoolTyID:
881 case Type::UByteTyID: case Type::SByteTyID:
882 O << TAI->getData8bitsDirective();
883 break;
884 case Type::UShortTyID: case Type::ShortTyID:
885 O << TAI->getData16bitsDirective();
886 break;
887 case Type::PointerTyID:
888 if (TD->getPointerSize() == 8) {
889 assert(TAI->getData64bitsDirective() &&
890 "Target cannot handle 64-bit pointer exprs!");
891 O << TAI->getData64bitsDirective();
892 break;
893 }
894 //Fall through for pointer size == int size
895 case Type::UIntTyID: case Type::IntTyID:
896 O << TAI->getData32bitsDirective();
897 break;
898 case Type::ULongTyID: case Type::LongTyID:
899 assert(TAI->getData64bitsDirective() &&
900 "Target cannot handle 64-bit constant exprs!");
901 O << TAI->getData64bitsDirective();
902 break;
903 case Type::FloatTyID: case Type::DoubleTyID:
904 assert (0 && "Should have already output floating point constant.");
905 default:
906 assert (0 && "Can't handle printing this type of thing");
907 break;
908 }
909}