blob: 96a481fc8461577cebb9f3d5bf9367ae64b3070b [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 Lattner6a8e0f52004-08-16 23:15:22 +000025#include "llvm/Target/TargetMachine.h"
Duraid Madina26b037e2005-12-28 06:29:02 +000026#include <iostream>
Chris Lattneraa23fa92006-02-01 22:41:11 +000027#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000028using namespace llvm;
29
Jim Laskey261779b2006-09-07 22:06:40 +000030AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
31 const TargetAsmInfo *T)
Jim Laskeya6211dc2006-09-06 18:34:40 +000032: FunctionNumber(0), O(o), TM(tm), TAI(T)
33{}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000034
Chris Lattnerdc822412006-10-05 02:42:47 +000035std::string AsmPrinter::getSectionForFunction(const Function &F) const {
36 return TAI->getTextSection();
37}
38
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000039
Chris Lattner8488ba22006-05-09 04:59:56 +000040/// SwitchToTextSection - Switch to the specified text section of the executable
41/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000042///
Chris Lattner8488ba22006-05-09 04:59:56 +000043void AsmPrinter::SwitchToTextSection(const char *NewSection,
44 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000045 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000046 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000047 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000048 else
49 NS = NewSection;
50
51 // If we're already in this section, we're done.
52 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000053
Chris Lattner4ebc6a22006-05-09 05:33:48 +000054 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000055 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
56 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000057
Chris Lattner4ebc6a22006-05-09 05:33:48 +000058 CurrentSection = NS;
59
60 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000061 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattner2ea5c992005-11-21 07:06:27 +000062}
63
Nate Begeman78756502006-07-27 01:13:04 +000064/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-05-09 04:59:56 +000065/// if we are not already in it!
66///
67void AsmPrinter::SwitchToDataSection(const char *NewSection,
68 const GlobalValue *GV) {
69 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000070 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000071 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner6341df82006-05-09 05:23:12 +000072 else
73 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +000074
Chris Lattner6341df82006-05-09 05:23:12 +000075 // If we're already in this section, we're done.
76 if (CurrentSection == NS) return;
77
Chris Lattner4ebc6a22006-05-09 05:33:48 +000078 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000079 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
80 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner4ebc6a22006-05-09 05:33:48 +000081
82 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +000083
Chris Lattner4ebc6a22006-05-09 05:33:48 +000084 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000085 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner8488ba22006-05-09 04:59:56 +000086}
87
88
Chris Lattner6a8e0f52004-08-16 23:15:22 +000089bool AsmPrinter::doInitialization(Module &M) {
Jim Laskeya6211dc2006-09-06 18:34:40 +000090 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +000091
Chris Lattner00fcdfe2006-01-24 04:16:34 +000092 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000093 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +000094 << M.getModuleInlineAsm()
Jim Laskeya6211dc2006-09-06 18:34:40 +000095 << "\n" << TAI->getCommentString()
96 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +000097
Chris Lattner8488ba22006-05-09 04:59:56 +000098 SwitchToDataSection("", 0); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +000099
100 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
101 DebugInfo->AnalyzeModule(M);
102 }
103
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000104 return false;
105}
106
107bool AsmPrinter::doFinalization(Module &M) {
108 delete Mang; Mang = 0;
109 return false;
110}
111
Chris Lattnerbb644e32005-11-21 07:51:36 +0000112void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000113 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000114 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000115 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000116}
117
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000118/// EmitConstantPool - Print to the current output stream assembly
119/// representations of the constants in the constant pool MCP. This is
120/// used to print out constants which have been "spilled to memory" by
121/// the code generator.
122///
123void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000124 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000125 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000126
127 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
128 // in special sections.
129 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
130 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
131 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
132 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000133 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000134 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000135 MachineConstantPoolEntry CPE = CP[i];
Evan Cheng2ad050f2006-09-14 07:35:00 +0000136 const Type *Ty = CPE.getType();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000137 if (TAI->getFourByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000138 TM.getTargetData()->getTypeSize(Ty) == 4)
139 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000140 else if (TAI->getEightByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000141 TM.getTargetData()->getTypeSize(Ty) == 8)
142 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000143 else if (TAI->getSixteenByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000144 TM.getTargetData()->getTypeSize(Ty) == 16)
145 SixteenByteCPs.push_back(std::make_pair(CPE, i));
146 else
147 OtherCPs.push_back(std::make_pair(CPE, i));
148 }
149
150 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000151 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
152 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
153 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
154 SixteenByteCPs);
155 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Chengec1d60b2006-06-29 00:26:09 +0000156}
157
158void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
159 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
160 if (CP.empty()) return;
161
162 SwitchToDataSection(Section, 0);
163 EmitAlignment(Alignment);
164 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000165 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
166 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Cheng2ad050f2006-09-14 07:35:00 +0000167 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
168 if (CP[i].first.isMachineConstantPoolEntry())
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000169 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000170 else
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000171 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000172 if (i != e-1) {
Evan Cheng2ad050f2006-09-14 07:35:00 +0000173 const Type *Ty = CP[i].first.getType();
Evan Chengec1d60b2006-06-29 00:26:09 +0000174 unsigned EntSize =
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000175 TM.getTargetData()->getTypeSize(Ty);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000176 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattnerf6190822006-02-09 04:46:04 +0000177 // Emit inter-object padding for alignment.
Evan Cheng2ad050f2006-09-14 07:35:00 +0000178 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattnerf6190822006-02-09 04:46:04 +0000179 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000180 }
181}
182
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000183/// EmitJumpTableInfo - Print assembly representations of the jump tables used
184/// by the current function to the current output stream.
185///
Chris Lattnera6a570e2006-10-05 03:01:21 +0000186void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
187 MachineFunction &MF) {
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000188 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
189 if (JT.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000190 const TargetData *TD = TM.getTargetData();
Nate Begemanefc312a2006-07-27 16:46:58 +0000191
192 // JTEntryDirective is a string to print sizeof(ptr) for non-PIC jump tables,
193 // and 32 bits for PIC since PIC jump table entries are differences, not
194 // pointers to blocks.
Andrew Lenharth783a4a92006-09-24 19:45:58 +0000195 // Use the architecture specific relocation directive, if it is set
196 const char *JTEntryDirective = TAI->getJumpTableDirective();
197 if (!JTEntryDirective)
198 JTEntryDirective = TAI->getData32bitsDirective();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000199
Nate Begeman78756502006-07-27 01:13:04 +0000200 // Pick the directive to use to print the jump table entries, and switch to
201 // the appropriate section.
202 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner66c16252006-10-05 03:13:28 +0000203 // In PIC mode, we need to emit the jump table to the same section as the
204 // function body itself, otherwise the label differences won't make sense.
205 const Function *F = MF.getFunction();
206 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Nate Begeman78756502006-07-27 01:13:04 +0000207 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000208 SwitchToDataSection(TAI->getJumpTableDataSection(), 0);
Nate Begeman78756502006-07-27 01:13:04 +0000209 if (TD->getPointerSize() == 8)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000210 JTEntryDirective = TAI->getData64bitsDirective();
Nate Begeman78756502006-07-27 01:13:04 +0000211 }
Owen Anderson20a631f2006-05-03 01:29:57 +0000212 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000213
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000214 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000215 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
216
217 // For PIC codegen, if possible we want to use the SetDirective to reduce
218 // the number of relocations the assembler will generate for the jump table.
219 // Set directives are all printed before the jump table itself.
220 std::set<MachineBasicBlock*> EmittedSets;
Jim Laskeya6211dc2006-09-06 18:34:40 +0000221 if (TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_)
Nate Begeman984c1a42006-08-12 21:29:52 +0000222 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
223 if (EmittedSets.insert(JTBBs[ii]).second)
224 printSetLabel(i, JTBBs[ii]);
225
Jim Laskeya6211dc2006-09-06 18:34:40 +0000226 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
227 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000228
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000229 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begemanefc312a2006-07-27 16:46:58 +0000230 O << JTEntryDirective << ' ';
Nate Begeman984c1a42006-08-12 21:29:52 +0000231 // If we have emitted set directives for the jump table entries, print
232 // them rather than the entries themselves. If we're emitting PIC, then
233 // emit the table entries as differences between two text section labels.
234 // If we're emitting non-PIC code, then emit the entries as direct
235 // references to the target basic blocks.
236 if (!EmittedSets.empty()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000237 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
238 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Nate Begeman984c1a42006-08-12 21:29:52 +0000239 } else if (TM.getRelocationModel() == Reloc::PIC_) {
240 printBasicBlockLabel(JTBBs[ii], false, false);
Andrew Lenharth783a4a92006-09-24 19:45:58 +0000241 //If the arch uses custom Jump Table directives, don't calc relative to JT
242 if (!TAI->getJumpTableDirective())
243 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
244 << getFunctionNumber() << '_' << i;
Nate Begeman984c1a42006-08-12 21:29:52 +0000245 } else {
246 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman78756502006-07-27 01:13:04 +0000247 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000248 O << '\n';
249 }
250 }
251}
252
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000253/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
254/// special global used by LLVM. If so, emit it and return true, otherwise
255/// do nothing and return false.
256bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey313570f2006-03-07 22:00:35 +0000257 // Ignore debug and non-emitted data.
258 if (GV->getSection() == "llvm.metadata") return true;
259
260 if (!GV->hasAppendingLinkage()) return false;
261
262 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000263
Chris Lattner66af3902006-09-26 03:38:18 +0000264 if (GV->getName() == "llvm.used") {
265 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
266 EmitLLVMUsedList(GV->getInitializer());
267 return true;
268 }
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000269
Chris Lattner3760e902006-01-12 19:17:23 +0000270 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000271 SwitchToDataSection(TAI->getStaticCtorsSection(), 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000272 EmitAlignment(2, 0);
273 EmitXXStructorList(GV->getInitializer());
274 return true;
275 }
276
Chris Lattner3760e902006-01-12 19:17:23 +0000277 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000278 SwitchToDataSection(TAI->getStaticDtorsSection(), 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000279 EmitAlignment(2, 0);
280 EmitXXStructorList(GV->getInitializer());
281 return true;
282 }
283
284 return false;
285}
286
Chris Lattner66af3902006-09-26 03:38:18 +0000287/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
288/// global in the specified llvm.used list as being used with this directive.
289void AsmPrinter::EmitLLVMUsedList(Constant *List) {
290 const char *Directive = TAI->getUsedDirective();
291
292 // Should be an array of 'sbyte*'.
293 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
294 if (InitList == 0) return;
295
296 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
297 O << Directive;
298 EmitConstantValueOnly(InitList->getOperand(i));
299 O << "\n";
300 }
301}
302
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000303/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
304/// function pointers, ignoring the init priority.
305void AsmPrinter::EmitXXStructorList(Constant *List) {
306 // Should be an array of '{ int, void ()* }' structs. The first value is the
307 // init priority, which we ignore.
308 if (!isa<ConstantArray>(List)) return;
309 ConstantArray *InitList = cast<ConstantArray>(List);
310 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
311 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
312 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000313
314 if (CS->getOperand(1)->isNullValue())
315 return; // Found a null terminator, exit printing.
316 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000317 EmitGlobalConstant(CS->getOperand(1));
318 }
319}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000320
Chris Lattnera9b25252006-02-05 01:29:18 +0000321/// getPreferredAlignmentLog - Return the preferred alignment of the
322/// specified global, returned in log form. This includes an explicitly
323/// requested alignment (if the global has one).
324unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Jim Laskeydce07562006-06-15 13:10:58 +0000325 const Type *ElemType = GV->getType()->getElementType();
326 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(ElemType);
Chris Lattnera9b25252006-02-05 01:29:18 +0000327 if (GV->getAlignment() > (1U << Alignment))
328 Alignment = Log2_32(GV->getAlignment());
329
Chris Lattnercbab2842006-02-05 01:46:49 +0000330 if (GV->hasInitializer()) {
Jim Laskey3519b872006-06-15 19:37:14 +0000331 // Always round up alignment of global doubles to 8 bytes.
332 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
333 Alignment = 3;
Chris Lattnercbab2842006-02-05 01:46:49 +0000334 if (Alignment < 4) {
335 // If the global is not external, see if it is large. If so, give it a
336 // larger alignment.
Jim Laskeydce07562006-06-15 13:10:58 +0000337 if (TM.getTargetData()->getTypeSize(ElemType) > 128)
Chris Lattnercbab2842006-02-05 01:46:49 +0000338 Alignment = 4; // 16-byte alignment.
339 }
Chris Lattnera9b25252006-02-05 01:29:18 +0000340 }
341 return Alignment;
342}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000343
Chris Lattnerbb644e32005-11-21 07:51:36 +0000344// EmitAlignment - Emit an alignment directive to the specified power of two.
345void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000346 if (GV && GV->getAlignment())
347 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000348 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000349 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
350 O << TAI->getAlignDirective() << NumBits << "\n";
Chris Lattner1d35c162004-08-17 19:14:29 +0000351}
352
Chris Lattnerbb644e32005-11-21 07:51:36 +0000353/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000354///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000355void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000356 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000357 if (TAI->getZeroDirective()) {
358 O << TAI->getZeroDirective() << NumZeros;
359 if (TAI->getZeroDirectiveSuffix())
360 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000361 O << "\n";
362 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000363 for (; NumZeros; --NumZeros)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000364 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000365 }
366 }
367}
368
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000369// Print out the specified constant, without a storage class. Only the
370// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000371void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000372 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000373 O << "0";
374 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
Chris Lattnereea983e2006-09-28 23:17:41 +0000375 assert(CB->getValue());
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000376 O << "1";
377 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
378 if (((CI->getValue() << 32) >> 32) == CI->getValue())
379 O << CI->getValue();
380 else
Duraid Madina73c4dba2005-05-15 13:05:48 +0000381 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000382 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
383 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000384 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000385 // This is a constant address for a global variable or function. Use the
386 // name of the variable or function as the address value, possibly
387 // decorating it with GlobalVarAddrPrefix/Suffix or
388 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000389 if (isa<Function>(GV)) {
390 O << TAI->getFunctionAddrPrefix()
391 << Mang->getValueName(GV)
392 << TAI->getFunctionAddrSuffix();
393 } else {
394 O << TAI->getGlobalVarAddrPrefix()
395 << Mang->getValueName(GV)
396 << TAI->getGlobalVarAddrSuffix();
397 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000398 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000399 const TargetData *TD = TM.getTargetData();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000400 switch(CE->getOpcode()) {
401 case Instruction::GetElementPtr: {
402 // generate a symbolic expression for the byte address
403 const Constant *ptrVal = CE->getOperand(0);
404 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Anderson20a631f2006-05-03 01:29:57 +0000405 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner145569b2005-02-14 21:40:26 +0000406 if (Offset)
407 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000408 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000409 if (Offset > 0)
410 O << ") + " << Offset;
411 else if (Offset < 0)
412 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000413 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000414 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000415 }
416 break;
417 }
418 case Instruction::Cast: {
Chris Lattner85492422006-07-29 01:57:19 +0000419 // Support only foldable casts to/from pointers that can be eliminated by
420 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000421 Constant *Op = CE->getOperand(0);
422 const Type *OpTy = Op->getType(), *Ty = CE->getType();
423
Chris Lattner85492422006-07-29 01:57:19 +0000424 // Handle casts to pointers by changing them into casts to the appropriate
425 // integer type. This promotes constant folding and simplifies this code.
426 if (isa<PointerType>(Ty)) {
427 const Type *IntPtrTy = TD->getIntPtrType();
428 Op = ConstantExpr::getCast(Op, IntPtrTy);
429 return EmitConstantValueOnly(Op);
430 }
431
432 // We know the dest type is not a pointer. Is the src value a pointer or
433 // integral?
434 if (isa<PointerType>(OpTy) || OpTy->isIntegral()) {
435 // We can emit the pointer value into this slot if the slot is an
436 // integer slot greater or equal to the size of the pointer.
437 if (Ty->isIntegral() && TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
438 return EmitConstantValueOnly(Op);
439 }
440
441 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000442 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000443 break;
444 }
445 case Instruction::Add:
446 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000447 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000448 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000449 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000450 O << ")";
451 break;
452 default:
453 assert(0 && "Unsupported operator!");
454 }
455 } else {
456 assert(0 && "Unknown constant value!");
457 }
458}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000459
460/// toOctal - Convert the low order bits of X into an octal digit.
461///
462static inline char toOctal(int X) {
463 return (X&7)+'0';
464}
465
Chris Lattner55a6d902005-11-10 18:06:33 +0000466/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000467/// the predicate isString is true.
468///
Chris Lattner55a6d902005-11-10 18:06:33 +0000469static void printAsCString(std::ostream &O, const ConstantArray *CVA,
470 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000471 assert(CVA->isString() && "Array is not string compatible!");
472
473 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000474 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000475 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000476 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000477
478 if (C == '"') {
479 O << "\\\"";
480 } else if (C == '\\') {
481 O << "\\\\";
482 } else if (isprint(C)) {
483 O << C;
484 } else {
485 switch(C) {
486 case '\b': O << "\\b"; break;
487 case '\f': O << "\\f"; break;
488 case '\n': O << "\\n"; break;
489 case '\r': O << "\\r"; break;
490 case '\t': O << "\\t"; break;
491 default:
492 O << '\\';
493 O << toOctal(C >> 6);
494 O << toOctal(C >> 3);
495 O << toOctal(C >> 0);
496 break;
497 }
498 }
499 }
500 O << "\"";
501}
502
Jeff Cohen24a62a92006-05-02 01:16:28 +0000503/// EmitString - Emit a zero-byte-terminated string constant.
504///
505void AsmPrinter::EmitString(const ConstantArray *CVA) const {
506 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000507 if (TAI->getAscizDirective() && NumElts &&
Jeff Cohen24a62a92006-05-02 01:16:28 +0000508 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000509 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000510 printAsCString(O, CVA, NumElts-1);
511 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000512 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000513 printAsCString(O, CVA, NumElts);
514 }
515 O << "\n";
516}
517
Chris Lattnerbb644e32005-11-21 07:51:36 +0000518/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000519///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000520void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000521 const TargetData *TD = TM.getTargetData();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000522
Chris Lattner61753bf2004-10-16 18:19:26 +0000523 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000524 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000525 return;
526 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
527 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000528 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000529 } else { // Not a string. Print the values in successive locations
530 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000531 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000532 }
533 return;
534 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
535 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000536 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000537 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000538 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
539 const Constant* field = CVS->getOperand(i);
540
541 // Check if padding is needed and insert one or more 0s.
Owen Anderson20a631f2006-05-03 01:29:57 +0000542 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000543 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000544 : cvsLayout->MemberOffsets[i+1])
545 - cvsLayout->MemberOffsets[i]) - fieldSize;
546 sizeSoFar += fieldSize + padSize;
547
548 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000549 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000550
551 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000552 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000553 }
554 assert(sizeSoFar == cvsLayout->StructSize &&
555 "Layout of constant struct may be incorrect!");
556 return;
557 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
558 // FP Constants are printed as integer constants to avoid losing
559 // precision...
560 double Val = CFP->getValue();
561 if (CFP->getType() == Type::DoubleTy) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000562 if (TAI->getData64bitsDirective())
563 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
564 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000565 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000566 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
567 << "\t" << TAI->getCommentString()
568 << " double most significant word " << Val << "\n";
569 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
570 << "\t" << TAI->getCommentString()
571 << " double least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000572 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000573 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
574 << "\t" << TAI->getCommentString()
575 << " double least significant word " << Val << "\n";
576 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
577 << "\t" << TAI->getCommentString()
578 << " double most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000579 }
580 return;
581 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000582 O << TAI->getData32bitsDirective() << FloatToBits(Val)
583 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000584 return;
585 }
586 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
587 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
588 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000589
Jim Laskeya6211dc2006-09-06 18:34:40 +0000590 if (TAI->getData64bitsDirective())
591 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000592 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000593 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
594 << "\t" << TAI->getCommentString()
595 << " Double-word most significant word " << Val << "\n";
596 O << TAI->getData32bitsDirective() << unsigned(Val)
597 << "\t" << TAI->getCommentString()
598 << " Double-word least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000599 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000600 O << TAI->getData32bitsDirective() << unsigned(Val)
601 << "\t" << TAI->getCommentString()
602 << " Double-word least significant word " << Val << "\n";
603 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
604 << "\t" << TAI->getCommentString()
605 << " Double-word most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000606 }
607 return;
608 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000609 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
610 const PackedType *PTy = CP->getType();
611
612 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
613 EmitGlobalConstant(CP->getOperand(I));
614
615 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000616 }
617
618 const Type *type = CV->getType();
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000619 printDataDirective(type);
Chris Lattnerbb644e32005-11-21 07:51:36 +0000620 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000621 O << "\n";
622}
Chris Lattner061d9e22006-01-27 02:10:10 +0000623
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000624void
625AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
626 // Target doesn't support this yet!
627 abort();
628}
629
Chris Lattnera32814b2006-09-26 23:59:50 +0000630/// PrintSpecial - Print information related to the specified machine instr
631/// that is independent of the operand, and may be independent of the instr
632/// itself. This can be useful for portably encoding the comment character
633/// or other bits of target-specific knowledge into the asmstrings. The
634/// syntax used is ${:comment}. Targets can override this to add support
635/// for their own strange codes.
636void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattner30b47082006-09-27 00:06:07 +0000637 if (!strcmp(Code, "private")) {
638 O << TAI->getPrivateGlobalPrefix();
639 } else if (!strcmp(Code, "comment")) {
Chris Lattnera32814b2006-09-26 23:59:50 +0000640 O << TAI->getCommentString();
641 } else if (!strcmp(Code, "uid")) {
642 // Assign a unique ID to this machine instruction.
643 static const MachineInstr *LastMI = 0;
644 static unsigned Counter = 0U-1;
645 // If this is a new machine instruction, bump the counter.
646 if (LastMI != MI) { ++Counter; LastMI = MI; }
647 O << Counter;
648 } else {
649 std::cerr << "Unknown special formatter '" << Code
650 << "' for machine instr: " << *MI;
651 exit(1);
652 }
653}
654
655
Chris Lattner061d9e22006-01-27 02:10:10 +0000656/// printInlineAsm - This method formats and prints the specified machine
657/// instruction that is an inline asm.
658void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +0000659 unsigned NumOperands = MI->getNumOperands();
660
661 // Count the number of register definitions.
662 unsigned NumDefs = 0;
Chris Lattner45456d42006-09-05 20:02:51 +0000663 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
664 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +0000665 assert(NumDefs != NumOperands-1 && "No asm string?");
666
667 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000668
669 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000670 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000671
Chris Lattner3390cbe2006-07-28 00:17:20 +0000672 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
673 if (AsmStr[0] == 0) {
674 O << "\n"; // Tab already printed, avoid double indenting next instr.
675 return;
676 }
677
Jim Laskeya6211dc2006-09-06 18:34:40 +0000678 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +0000679
Chris Lattneraa23fa92006-02-01 22:41:11 +0000680 // The variant of the current asmprinter: FIXME: change.
681 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000682
Chris Lattneraa23fa92006-02-01 22:41:11 +0000683 int CurVariant = -1; // The number of the {.|.|.} region we are in.
684 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000685
Chris Lattneraa23fa92006-02-01 22:41:11 +0000686 while (*LastEmitted) {
687 switch (*LastEmitted) {
688 default: {
689 // Not a special case, emit the string section literally.
690 const char *LiteralEnd = LastEmitted+1;
691 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +0000692 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +0000693 ++LiteralEnd;
694 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
695 O.write(LastEmitted, LiteralEnd-LastEmitted);
696 LastEmitted = LiteralEnd;
697 break;
698 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000699 case '\n':
700 ++LastEmitted; // Consume newline character.
701 O << "\n\t"; // Indent code with newline.
702 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000703 case '$': {
704 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000705 bool Done = true;
706
707 // Handle escapes.
708 switch (*LastEmitted) {
709 default: Done = false; break;
710 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +0000711 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
712 O << '$';
713 ++LastEmitted; // Consume second '$' character.
714 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000715 case '(': // $( -> same as GCC's { character.
716 ++LastEmitted; // Consume '(' character.
717 if (CurVariant != -1) {
718 std::cerr << "Nested variants found in inline asm string: '"
719 << AsmStr << "'\n";
720 exit(1);
721 }
722 CurVariant = 0; // We're in the first variant now.
723 break;
724 case '|':
725 ++LastEmitted; // consume '|' character.
726 if (CurVariant == -1) {
727 std::cerr << "Found '|' character outside of variant in inline asm "
728 << "string: '" << AsmStr << "'\n";
729 exit(1);
730 }
731 ++CurVariant; // We're in the next variant.
732 break;
733 case ')': // $) -> same as GCC's } char.
734 ++LastEmitted; // consume ')' character.
735 if (CurVariant == -1) {
736 std::cerr << "Found '}' character outside of variant in inline asm "
737 << "string: '" << AsmStr << "'\n";
738 exit(1);
739 }
740 CurVariant = -1;
741 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000742 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000743 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000744
745 bool HasCurlyBraces = false;
746 if (*LastEmitted == '{') { // ${variable}
747 ++LastEmitted; // Consume '{' character.
748 HasCurlyBraces = true;
749 }
750
751 const char *IDStart = LastEmitted;
752 char *IDEnd;
753 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
754 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
755 std::cerr << "Bad $ operand number in inline asm string: '"
756 << AsmStr << "'\n";
757 exit(1);
758 }
759 LastEmitted = IDEnd;
760
Chris Lattner34f74c12006-02-06 22:17:23 +0000761 char Modifier[2] = { 0, 0 };
762
Chris Lattneraa23fa92006-02-01 22:41:11 +0000763 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000764 // If we have curly braces, check for a modifier character. This
765 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
766 if (*LastEmitted == ':') {
767 ++LastEmitted; // Consume ':' character.
768 if (*LastEmitted == 0) {
769 std::cerr << "Bad ${:} expression in inline asm string: '"
770 << AsmStr << "'\n";
771 exit(1);
772 }
773
774 Modifier[0] = *LastEmitted;
775 ++LastEmitted; // Consume modifier character.
776 }
777
Chris Lattneraa23fa92006-02-01 22:41:11 +0000778 if (*LastEmitted != '}') {
779 std::cerr << "Bad ${} expression in inline asm string: '"
780 << AsmStr << "'\n";
781 exit(1);
782 }
783 ++LastEmitted; // Consume '}' character.
784 }
785
786 if ((unsigned)Val >= NumOperands-1) {
787 std::cerr << "Invalid $ operand number in inline asm string: '"
788 << AsmStr << "'\n";
789 exit(1);
790 }
791
Chris Lattner571d9642006-02-23 19:21:04 +0000792 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +0000793 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +0000794 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
795 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000796
797 bool Error = false;
798
Chris Lattner571d9642006-02-23 19:21:04 +0000799 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +0000800 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000801 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner5af3fde2006-02-24 19:50:58 +0000802 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
803 OpNo += (OpFlags >> 3) + 1;
804 }
Chris Lattner1d08c652006-02-24 20:21:58 +0000805
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000806 if (OpNo >= MI->getNumOperands()) {
807 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +0000808 } else {
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000809 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
810 ++OpNo; // Skip over the ID number.
811
812 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
813 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
814 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
815 Modifier[0] ? Modifier : 0);
816 } else {
817 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
818 Modifier[0] ? Modifier : 0);
819 }
Chris Lattner1d08c652006-02-24 20:21:58 +0000820 }
821 if (Error) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000822 std::cerr << "Invalid operand found in inline asm: '"
823 << AsmStr << "'\n";
824 MI->dump();
825 exit(1);
826 }
Chris Lattner571d9642006-02-23 19:21:04 +0000827 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000828 break;
829 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000830 }
831 }
Jim Laskeya6211dc2006-09-06 18:34:40 +0000832 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000833}
834
835/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
836/// instruction, using the specified assembler variant. Targets should
837/// overried this to format as appropriate.
838bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000839 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000840 // Target doesn't support this yet!
841 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000842}
Chris Lattner1d08c652006-02-24 20:21:58 +0000843
844bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
845 unsigned AsmVariant,
846 const char *ExtraCode) {
847 // Target doesn't support this yet!
848 return true;
849}
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000850
851/// printBasicBlockLabel - This method prints the label for the specified
852/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +0000853void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
854 bool printColon,
855 bool printComment) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000856 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
Nate Begeman4971ba52006-05-02 17:36:46 +0000857 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +0000858 if (printColon)
859 O << ':';
Chris Lattner8b1a59a2006-10-05 21:40:14 +0000860 if (printComment && MBB->getBasicBlock())
Jim Laskeya6211dc2006-09-06 18:34:40 +0000861 O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000862}
Nate Begeman984c1a42006-08-12 21:29:52 +0000863
864/// printSetLabel - This method prints a set label for the specified
865/// MachineBasicBlock
866void AsmPrinter::printSetLabel(unsigned uid,
867 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000868 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +0000869 return;
870
Jim Laskeya6211dc2006-09-06 18:34:40 +0000871 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
872 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman984c1a42006-08-12 21:29:52 +0000873 printBasicBlockLabel(MBB, false, false);
Jim Laskeya6211dc2006-09-06 18:34:40 +0000874 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman984c1a42006-08-12 21:29:52 +0000875 << '_' << uid << '\n';
876}
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000877
878/// printDataDirective - This method prints the asm directive for the
879/// specified type.
880void AsmPrinter::printDataDirective(const Type *type) {
881 const TargetData *TD = TM.getTargetData();
882 switch (type->getTypeID()) {
883 case Type::BoolTyID:
884 case Type::UByteTyID: case Type::SByteTyID:
885 O << TAI->getData8bitsDirective();
886 break;
887 case Type::UShortTyID: case Type::ShortTyID:
888 O << TAI->getData16bitsDirective();
889 break;
890 case Type::PointerTyID:
891 if (TD->getPointerSize() == 8) {
892 assert(TAI->getData64bitsDirective() &&
893 "Target cannot handle 64-bit pointer exprs!");
894 O << TAI->getData64bitsDirective();
895 break;
896 }
897 //Fall through for pointer size == int size
898 case Type::UIntTyID: case Type::IntTyID:
899 O << TAI->getData32bitsDirective();
900 break;
901 case Type::ULongTyID: case Type::LongTyID:
902 assert(TAI->getData64bitsDirective() &&
903 "Target cannot handle 64-bit constant exprs!");
904 O << TAI->getData64bitsDirective();
905 break;
906 case Type::FloatTyID: case Type::DoubleTyID:
907 assert (0 && "Should have already output floating point constant.");
908 default:
909 assert (0 && "Can't handle printing this type of thing");
910 break;
911 }
912}