LLVM API Documentation
00001 //===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output --------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "llvm/MC/MCStreamer.h" 00011 #include "llvm/CodeGen/AsmPrinter.h" 00012 #include "llvm/MC/MCContext.h" 00013 #include "llvm/MC/MCInst.h" 00014 #include "llvm/MC/MCSectionMachO.h" 00015 #include "llvm/MC/MCSymbol.h" 00016 #include "llvm/MC/MCValue.h" 00017 #include "llvm/Target/TargetAsmInfo.h" 00018 #include "llvm/Support/ErrorHandling.h" 00019 #include "llvm/Support/MathExtras.h" 00020 #include "llvm/Support/raw_ostream.h" 00021 using namespace llvm; 00022 00023 namespace { 00024 00025 class MCAsmStreamer : public MCStreamer { 00026 raw_ostream &OS; 00027 const TargetAsmInfo &TAI; 00028 AsmPrinter *Printer; 00029 public: 00030 MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const TargetAsmInfo &tai, 00031 AsmPrinter *_AsmPrinter) 00032 : MCStreamer(Context), OS(_OS), TAI(tai), Printer(_AsmPrinter) {} 00033 ~MCAsmStreamer() {} 00034 00035 /// @name MCStreamer Interface 00036 /// @{ 00037 00038 virtual void SwitchSection(const MCSection *Section); 00039 00040 virtual void EmitLabel(MCSymbol *Symbol); 00041 00042 virtual void EmitAssemblerFlag(AssemblerFlag Flag); 00043 00044 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value, 00045 bool MakeAbsolute = false); 00046 00047 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute); 00048 00049 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue); 00050 00051 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value); 00052 00053 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, 00054 unsigned Pow2Alignment, bool IsLocal); 00055 00056 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL, 00057 unsigned Size = 0, unsigned Pow2Alignment = 0); 00058 00059 virtual void EmitBytes(const StringRef &Data); 00060 00061 virtual void EmitValue(const MCValue &Value, unsigned Size); 00062 00063 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, 00064 unsigned ValueSize = 1, 00065 unsigned MaxBytesToEmit = 0); 00066 00067 virtual void EmitValueToOffset(const MCValue &Offset, 00068 unsigned char Value = 0); 00069 00070 virtual void EmitInstruction(const MCInst &Inst); 00071 00072 virtual void Finish(); 00073 00074 /// @} 00075 }; 00076 00077 } // end anonymous namespace. 00078 00079 /// Allow printing symbols directly to a raw_ostream with proper quoting. 00080 static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) { 00081 S->print(os); 00082 return os; 00083 } 00084 00085 /// Allow printing values directly to a raw_ostream. 00086 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) { 00087 Value.print(os); 00088 return os; 00089 } 00090 00091 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) { 00092 assert(Bytes && "Invalid size!"); 00093 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8)); 00094 } 00095 00096 static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) { 00097 return MCValue::get(Value.getSymA(), Value.getSymB(), 00098 truncateToSize(Value.getConstant(), Bytes)); 00099 } 00100 00101 void MCAsmStreamer::SwitchSection(const MCSection *Section) { 00102 assert(Section && "Cannot switch to a null section!"); 00103 if (Section != CurSection) { 00104 CurSection = Section; 00105 Section->PrintSwitchToSection(TAI, OS); 00106 } 00107 } 00108 00109 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) { 00110 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!"); 00111 assert(CurSection && "Cannot emit before setting section!"); 00112 assert(!getContext().GetSymbolValue(Symbol) && 00113 "Cannot emit symbol which was directly assigned to!"); 00114 00115 OS << Symbol << ":\n"; 00116 Symbol->setSection(CurSection); 00117 Symbol->setExternal(false); 00118 } 00119 00120 void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) { 00121 switch (Flag) { 00122 default: assert(0 && "Invalid flag!"); 00123 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break; 00124 } 00125 OS << '\n'; 00126 } 00127 00128 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value, 00129 bool MakeAbsolute) { 00130 assert(!Symbol->getSection() && "Cannot assign to a label!"); 00131 00132 if (MakeAbsolute) { 00133 OS << ".set " << Symbol << ", " << Value << '\n'; 00134 00135 // HACK: If the value isn't already absolute, set the symbol value to 00136 // itself, we want to use the .set absolute value, not the actual 00137 // expression. 00138 if (!Value.isAbsolute()) 00139 getContext().SetSymbolValue(Symbol, MCValue::get(Symbol)); 00140 else 00141 getContext().SetSymbolValue(Symbol, Value); 00142 } else { 00143 OS << Symbol << " = " << Value << '\n'; 00144 getContext().SetSymbolValue(Symbol, Value); 00145 } 00146 } 00147 00148 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol, 00149 SymbolAttr Attribute) { 00150 switch (Attribute) { 00151 case Global: OS << ".globl"; break; 00152 case Hidden: OS << ".hidden"; break; 00153 case IndirectSymbol: OS << ".indirect_symbol"; break; 00154 case Internal: OS << ".internal"; break; 00155 case LazyReference: OS << ".lazy_reference"; break; 00156 case NoDeadStrip: OS << ".no_dead_strip"; break; 00157 case PrivateExtern: OS << ".private_extern"; break; 00158 case Protected: OS << ".protected"; break; 00159 case Reference: OS << ".reference"; break; 00160 case Weak: OS << ".weak"; break; 00161 case WeakDefinition: OS << ".weak_definition"; break; 00162 case WeakReference: OS << ".weak_reference"; break; 00163 } 00164 00165 OS << ' ' << Symbol << '\n'; 00166 } 00167 00168 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { 00169 OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n'; 00170 } 00171 00172 void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) { 00173 OS << ".lsym" << ' ' << Symbol << ',' << Value << '\n'; 00174 } 00175 00176 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size, 00177 unsigned Pow2Alignment, bool IsLocal) { 00178 if (IsLocal) 00179 OS << ".lcomm"; 00180 else 00181 OS << ".comm"; 00182 OS << ' ' << Symbol << ',' << Size; 00183 if (Pow2Alignment != 0) 00184 OS << ',' << Pow2Alignment; 00185 OS << '\n'; 00186 } 00187 00188 void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol, 00189 unsigned Size, unsigned Pow2Alignment) { 00190 // Note: a .zerofill directive does not switch sections. 00191 OS << ".zerofill "; 00192 00193 // This is a mach-o specific directive. 00194 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section); 00195 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName(); 00196 00197 if (Symbol != NULL) { 00198 OS << ',' << Symbol << ',' << Size; 00199 if (Pow2Alignment != 0) 00200 OS << ',' << Pow2Alignment; 00201 } 00202 OS << '\n'; 00203 } 00204 00205 void MCAsmStreamer::EmitBytes(const StringRef &Data) { 00206 assert(CurSection && "Cannot emit contents before setting section!"); 00207 for (unsigned i = 0, e = Data.size(); i != e; ++i) 00208 OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n'; 00209 } 00210 00211 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) { 00212 assert(CurSection && "Cannot emit contents before setting section!"); 00213 // Need target hooks to know how to print this. 00214 switch (Size) { 00215 default: 00216 llvm_unreachable("Invalid size for machine code value!"); 00217 case 1: OS << ".byte"; break; 00218 case 2: OS << ".short"; break; 00219 case 4: OS << ".long"; break; 00220 case 8: OS << ".quad"; break; 00221 } 00222 00223 OS << ' ' << truncateToSize(Value, Size) << '\n'; 00224 } 00225 00226 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value, 00227 unsigned ValueSize, 00228 unsigned MaxBytesToEmit) { 00229 // Some assemblers don't support non-power of two alignments, so we always 00230 // emit alignments as a power of two if possible. 00231 if (isPowerOf2_32(ByteAlignment)) { 00232 switch (ValueSize) { 00233 default: llvm_unreachable("Invalid size for machine code value!"); 00234 case 1: OS << TAI.getAlignDirective(); break; 00235 // FIXME: use TAI for this! 00236 case 2: OS << ".p2alignw "; break; 00237 case 4: OS << ".p2alignl "; break; 00238 case 8: llvm_unreachable("Unsupported alignment size!"); 00239 } 00240 00241 if (TAI.getAlignmentIsInBytes()) 00242 OS << ByteAlignment; 00243 else 00244 OS << Log2_32(ByteAlignment); 00245 00246 if (Value || MaxBytesToEmit) { 00247 OS << ", " << truncateToSize(Value, ValueSize); 00248 00249 if (MaxBytesToEmit) 00250 OS << ", " << MaxBytesToEmit; 00251 } 00252 OS << '\n'; 00253 return; 00254 } 00255 00256 // Non-power of two alignment. This is not widely supported by assemblers. 00257 // FIXME: Parameterize this based on TAI. 00258 switch (ValueSize) { 00259 default: llvm_unreachable("Invalid size for machine code value!"); 00260 case 1: OS << ".balign"; break; 00261 case 2: OS << ".balignw"; break; 00262 case 4: OS << ".balignl"; break; 00263 case 8: llvm_unreachable("Unsupported alignment size!"); 00264 } 00265 00266 OS << ' ' << ByteAlignment; 00267 OS << ", " << truncateToSize(Value, ValueSize); 00268 if (MaxBytesToEmit) 00269 OS << ", " << MaxBytesToEmit; 00270 OS << '\n'; 00271 } 00272 00273 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset, 00274 unsigned char Value) { 00275 // FIXME: Verify that Offset is associated with the current section. 00276 OS << ".org " << Offset << ", " << (unsigned) Value << '\n'; 00277 } 00278 00279 static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) { 00280 if (Op.isReg()) 00281 return OS << "reg:" << Op.getReg(); 00282 if (Op.isImm()) 00283 return OS << "imm:" << Op.getImm(); 00284 if (Op.isMBBLabel()) 00285 return OS << "mbblabel:(" 00286 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock(); 00287 assert(Op.isMCValue() && "Invalid operand!"); 00288 return OS << "val:" << Op.getMCValue(); 00289 } 00290 00291 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) { 00292 assert(CurSection && "Cannot emit contents before setting section!"); 00293 00294 // If we have an AsmPrinter, use that to print. 00295 if (Printer) { 00296 Printer->printMCInst(&Inst); 00297 return; 00298 } 00299 00300 // Otherwise fall back to a structural printing for now. Eventually we should 00301 // always have access to the target specific printer. 00302 OS << "MCInst(" 00303 << "opcode=" << Inst.getOpcode() << ", " 00304 << "operands=["; 00305 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) { 00306 if (i) 00307 OS << ", "; 00308 OS << Inst.getOperand(i); 00309 } 00310 OS << "])\n"; 00311 } 00312 00313 void MCAsmStreamer::Finish() { 00314 OS.flush(); 00315 } 00316 00317 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS, 00318 const TargetAsmInfo &TAI, AsmPrinter *AP) { 00319 return new MCAsmStreamer(Context, OS, TAI, AP); 00320 }