Implement PR3266 & PR5276, folding:
[oota-llvm.git] / include / llvm / MC / MCAssembler.h
1 //===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_MC_MCASSEMBLER_H
11 #define LLVM_MC_MCASSEMBLER_H
12
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/ilist.h"
15 #include "llvm/ADT/ilist_node.h"
16 #include "llvm/Support/Casting.h"
17 #include "llvm/Support/DataTypes.h"
18 #include <vector> // FIXME: Shouldn't be needed.
19
20 namespace llvm {
21 class raw_ostream;
22 class MCAssembler;
23 class MCContext;
24 class MCExpr;
25 class MCSection;
26 class MCSectionData;
27 class MCSymbol;
28
29 class MCFragment : public ilist_node<MCFragment> {
30   MCFragment(const MCFragment&);     // DO NOT IMPLEMENT
31   void operator=(const MCFragment&); // DO NOT IMPLEMENT
32
33 public:
34   enum FragmentType {
35     FT_Data,
36     FT_Align,
37     FT_Fill,
38     FT_Org,
39     FT_ZeroFill
40   };
41
42 private:
43   FragmentType Kind;
44
45   /// Parent - The data for the section this fragment is in.
46   MCSectionData *Parent;
47
48   /// @name Assembler Backend Data
49   /// @{
50   //
51   // FIXME: This could all be kept private to the assembler implementation.
52
53   /// Offset - The offset of this fragment in its section. This is ~0 until
54   /// initialized.
55   uint64_t Offset;
56
57   /// FileSize - The file size of this section. This is ~0 until initialized.
58   uint64_t FileSize;
59
60   /// @}
61
62 protected:
63   MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
64
65 public:
66   // Only for sentinel.
67   MCFragment();
68   virtual ~MCFragment();
69
70   FragmentType getKind() const { return Kind; }
71
72   MCSectionData *getParent() const { return Parent; }
73   void setParent(MCSectionData *Value) { Parent = Value; }
74
75   // FIXME: This should be abstract, fix sentinel.
76   virtual uint64_t getMaxFileSize() const {
77     assert(0 && "Invalid getMaxFileSize call!");
78     return 0;
79   };
80
81   /// @name Assembler Backend Support
82   /// @{
83   //
84   // FIXME: This could all be kept private to the assembler implementation.
85
86   uint64_t getAddress() const;
87
88   uint64_t getFileSize() const { 
89     assert(FileSize != ~UINT64_C(0) && "File size not set!");
90     return FileSize;
91   }
92   void setFileSize(uint64_t Value) {
93     assert(Value <= getMaxFileSize() && "Invalid file size!");
94     FileSize = Value;
95   }
96
97   uint64_t getOffset() const {
98     assert(Offset != ~UINT64_C(0) && "File offset not set!");
99     return Offset;
100   }
101   void setOffset(uint64_t Value) { Offset = Value; }
102
103   /// @}
104
105   static bool classof(const MCFragment *O) { return true; }
106 };
107
108 class MCDataFragment : public MCFragment {
109   SmallString<32> Contents;
110
111 public:
112   MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
113
114   /// @name Accessors
115   /// @{
116
117   uint64_t getMaxFileSize() const {
118     return Contents.size();
119   }
120
121   SmallString<32> &getContents() { return Contents; }
122   const SmallString<32> &getContents() const { return Contents; }
123
124   /// @}
125
126   static bool classof(const MCFragment *F) { 
127     return F->getKind() == MCFragment::FT_Data; 
128   }
129   static bool classof(const MCDataFragment *) { return true; }
130 };
131
132 class MCAlignFragment : public MCFragment {
133   /// Alignment - The alignment to ensure, in bytes.
134   unsigned Alignment;
135
136   /// Value - Value to use for filling padding bytes.
137   int64_t Value;
138
139   /// ValueSize - The size of the integer (in bytes) of \arg Value.
140   unsigned ValueSize;
141
142   /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
143   /// cannot be satisfied in this width then this fragment is ignored.
144   unsigned MaxBytesToEmit;
145
146 public:
147   MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
148                   unsigned _MaxBytesToEmit, MCSectionData *SD = 0)
149     : MCFragment(FT_Align, SD), Alignment(_Alignment),
150       Value(_Value),ValueSize(_ValueSize),
151       MaxBytesToEmit(_MaxBytesToEmit) {}
152
153   /// @name Accessors
154   /// @{
155
156   uint64_t getMaxFileSize() const {
157     return std::max(Alignment - 1, MaxBytesToEmit);
158   }
159
160   unsigned getAlignment() const { return Alignment; }
161   
162   int64_t getValue() const { return Value; }
163
164   unsigned getValueSize() const { return ValueSize; }
165
166   unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
167
168   /// @}
169
170   static bool classof(const MCFragment *F) { 
171     return F->getKind() == MCFragment::FT_Align; 
172   }
173   static bool classof(const MCAlignFragment *) { return true; }
174 };
175
176 class MCFillFragment : public MCFragment {
177   /// Value - Value to use for filling bytes.
178   const MCExpr *Value;
179
180   /// ValueSize - The size (in bytes) of \arg Value to use when filling.
181   unsigned ValueSize;
182
183   /// Count - The number of copies of \arg Value to insert.
184   uint64_t Count;
185
186 public:
187   MCFillFragment(const MCExpr &_Value, unsigned _ValueSize, uint64_t _Count,
188                  MCSectionData *SD = 0) 
189     : MCFragment(FT_Fill, SD),
190       Value(&_Value), ValueSize(_ValueSize), Count(_Count) {}
191
192   /// @name Accessors
193   /// @{
194
195   uint64_t getMaxFileSize() const {
196     return ValueSize * Count;
197   }
198
199   const MCExpr &getValue() const { return *Value; }
200   
201   unsigned getValueSize() const { return ValueSize; }
202
203   uint64_t getCount() const { return Count; }
204
205   /// @}
206
207   static bool classof(const MCFragment *F) { 
208     return F->getKind() == MCFragment::FT_Fill; 
209   }
210   static bool classof(const MCFillFragment *) { return true; }
211 };
212
213 class MCOrgFragment : public MCFragment {
214   /// Offset - The offset this fragment should start at.
215   const MCExpr *Offset;
216
217   /// Value - Value to use for filling bytes.  
218   int8_t Value;
219
220 public:
221   MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
222     : MCFragment(FT_Org, SD),
223       Offset(&_Offset), Value(_Value) {}
224
225   /// @name Accessors
226   /// @{
227
228   uint64_t getMaxFileSize() const {
229     // FIXME: This doesn't make much sense.
230     return ~UINT64_C(0);
231   }
232
233   const MCExpr &getOffset() const { return *Offset; }
234   
235   uint8_t getValue() const { return Value; }
236
237   /// @}
238
239   static bool classof(const MCFragment *F) { 
240     return F->getKind() == MCFragment::FT_Org; 
241   }
242   static bool classof(const MCOrgFragment *) { return true; }
243 };
244
245 /// MCZeroFillFragment - Represent data which has a fixed size and alignment,
246 /// but requires no physical space in the object file.
247 class MCZeroFillFragment : public MCFragment {
248   /// Size - The size of this fragment.
249   uint64_t Size;
250
251   /// Alignment - The alignment for this fragment.
252   unsigned Alignment;
253
254 public:
255   MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
256     : MCFragment(FT_ZeroFill, SD),
257       Size(_Size), Alignment(_Alignment) {}
258
259   /// @name Accessors
260   /// @{
261
262   uint64_t getMaxFileSize() const {
263     // FIXME: This also doesn't make much sense, this method is misnamed.
264     return ~UINT64_C(0);
265   }
266
267   uint64_t getSize() const { return Size; }
268   
269   unsigned getAlignment() const { return Alignment; }
270
271   /// @}
272
273   static bool classof(const MCFragment *F) { 
274     return F->getKind() == MCFragment::FT_ZeroFill; 
275   }
276   static bool classof(const MCZeroFillFragment *) { return true; }
277 };
278
279 // FIXME: Should this be a separate class, or just merged into MCSection? Since
280 // we anticipate the fast path being through an MCAssembler, the only reason to
281 // keep it out is for API abstraction.
282 class MCSectionData : public ilist_node<MCSectionData> {
283   MCSectionData(const MCSectionData&);  // DO NOT IMPLEMENT
284   void operator=(const MCSectionData&); // DO NOT IMPLEMENT
285
286 public:
287   /// Fixup - Represent a fixed size region of bytes inside some fragment which
288   /// needs to be rewritten. This region will either be rewritten by the
289   /// assembler or cause a relocation entry to be generated.
290   struct Fixup {
291     /// Fragment - The fragment containing the fixup.
292     MCFragment *Fragment;
293     
294     /// Offset - The offset inside the fragment which needs to be rewritten.
295     uint64_t Offset;
296
297     /// Value - The expression to eventually write into the fragment.
298     const MCExpr *Value;
299
300     /// Size - The fixup size.
301     unsigned Size;
302
303     /// FixedValue - The value to replace the fix up by.
304     //
305     // FIXME: This should not be here.
306     uint64_t FixedValue;
307
308   public:
309     Fixup(MCFragment &_Fragment, uint64_t _Offset, const MCExpr &_Value,
310           unsigned _Size) 
311       : Fragment(&_Fragment), Offset(_Offset), Value(&_Value), Size(_Size),
312         FixedValue(0) {}
313   };
314
315   typedef iplist<MCFragment> FragmentListType;
316
317   typedef FragmentListType::const_iterator const_iterator;
318   typedef FragmentListType::iterator iterator;
319
320   typedef std::vector<Fixup>::const_iterator const_fixup_iterator;
321   typedef std::vector<Fixup>::iterator fixup_iterator;
322
323 private:
324   iplist<MCFragment> Fragments;
325   const MCSection *Section;
326
327   /// Alignment - The maximum alignment seen in this section.
328   unsigned Alignment;
329
330   /// @name Assembler Backend Data
331   /// @{
332   //
333   // FIXME: This could all be kept private to the assembler implementation.
334
335   /// Address - The computed address of this section. This is ~0 until
336   /// initialized.
337   uint64_t Address;
338
339   /// Size - The content size of this section. This is ~0 until initialized.
340   uint64_t Size;
341
342   /// FileSize - The size of this section in the object file. This is ~0 until
343   /// initialized.
344   uint64_t FileSize;
345
346   /// LastFixupLookup - Cache for the last looked up fixup.
347   mutable unsigned LastFixupLookup;
348
349   /// Fixups - The list of fixups in this section.
350   std::vector<Fixup> Fixups;
351   
352   /// @}
353
354 public:    
355   // Only for use as sentinel.
356   MCSectionData();
357   MCSectionData(const MCSection &Section, MCAssembler *A = 0);
358
359   const MCSection &getSection() const { return *Section; }
360
361   unsigned getAlignment() const { return Alignment; }
362   void setAlignment(unsigned Value) { Alignment = Value; }
363
364   /// @name Fragment Access
365   /// @{
366
367   const FragmentListType &getFragmentList() const { return Fragments; }
368   FragmentListType &getFragmentList() { return Fragments; }
369
370   iterator begin() { return Fragments.begin(); }
371   const_iterator begin() const { return Fragments.begin(); }
372
373   iterator end() { return Fragments.end(); }
374   const_iterator end() const { return Fragments.end(); }
375
376   size_t size() const { return Fragments.size(); }
377
378   bool empty() const { return Fragments.empty(); }
379
380   /// @}
381   /// @name Fixup Access
382   /// @{
383
384   std::vector<Fixup> &getFixups() {
385     return Fixups;
386   }
387
388   fixup_iterator fixup_begin() {
389     return Fixups.begin();
390   }
391
392   fixup_iterator fixup_end() {
393     return Fixups.end();
394   }
395
396   size_t fixup_size() const { return Fixups.size(); }
397
398   /// @}
399   /// @name Assembler Backend Support
400   /// @{
401   //
402   // FIXME: This could all be kept private to the assembler implementation.
403
404   /// LookupFixup - Look up the fixup for the given \arg Fragment and \arg
405   /// Offset.
406   ///
407   /// If multiple fixups exist for the same fragment and offset it is undefined
408   /// which one is returned.
409   //
410   // FIXME: This isn't horribly slow in practice, but there are much nicer
411   // solutions to applying the fixups.
412   const Fixup *LookupFixup(const MCFragment *Fragment, uint64_t Offset) const;
413
414   uint64_t getAddress() const { 
415     assert(Address != ~UINT64_C(0) && "Address not set!");
416     return Address;
417   }
418   void setAddress(uint64_t Value) { Address = Value; }
419
420   uint64_t getSize() const { 
421     assert(Size != ~UINT64_C(0) && "File size not set!");
422     return Size;
423   }
424   void setSize(uint64_t Value) { Size = Value; }
425
426   uint64_t getFileSize() const { 
427     assert(FileSize != ~UINT64_C(0) && "File size not set!");
428     return FileSize;
429   }
430   void setFileSize(uint64_t Value) { FileSize = Value; }  
431
432   /// @}
433 };
434
435 // FIXME: Same concerns as with SectionData.
436 class MCSymbolData : public ilist_node<MCSymbolData> {
437 public:
438   const MCSymbol *Symbol;
439
440   /// Fragment - The fragment this symbol's value is relative to, if any.
441   MCFragment *Fragment;
442
443   /// Offset - The offset to apply to the fragment address to form this symbol's
444   /// value.
445   uint64_t Offset;
446     
447   /// IsExternal - True if this symbol is visible outside this translation
448   /// unit.
449   unsigned IsExternal : 1;
450
451   /// IsPrivateExtern - True if this symbol is private extern.
452   unsigned IsPrivateExtern : 1;
453
454   /// CommonSize - The size of the symbol, if it is 'common', or 0.
455   //
456   // FIXME: Pack this in with other fields? We could put it in offset, since a
457   // common symbol can never get a definition.
458   uint64_t CommonSize;
459
460   /// CommonAlign - The alignment of the symbol, if it is 'common'.
461   //
462   // FIXME: Pack this in with other fields?
463   unsigned CommonAlign;
464
465   /// Flags - The Flags field is used by object file implementations to store
466   /// additional per symbol information which is not easily classified.
467   uint32_t Flags;
468
469   /// Index - Index field, for use by the object file implementation.
470   uint64_t Index;
471
472 public:
473   // Only for use as sentinel.
474   MCSymbolData();
475   MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
476                MCAssembler *A = 0);
477
478   /// @name Accessors
479   /// @{
480
481   const MCSymbol &getSymbol() const { return *Symbol; }
482
483   MCFragment *getFragment() const { return Fragment; }
484   void setFragment(MCFragment *Value) { Fragment = Value; }
485
486   uint64_t getOffset() const { return Offset; }
487   void setOffset(uint64_t Value) { Offset = Value; }
488
489   /// @}
490   /// @name Symbol Attributes
491   /// @{
492   
493   bool isExternal() const { return IsExternal; }
494   void setExternal(bool Value) { IsExternal = Value; }
495   
496   bool isPrivateExtern() const { return IsPrivateExtern; }
497   void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
498
499   /// isCommon - Is this a 'common' symbol.
500   bool isCommon() const { return CommonSize != 0; }
501
502   /// setCommon - Mark this symbol as being 'common'.
503   ///
504   /// \param Size - The size of the symbol.
505   /// \param Align - The alignment of the symbol.
506   void setCommon(uint64_t Size, unsigned Align) {
507     CommonSize = Size;
508     CommonAlign = Align;
509   }
510
511   /// getCommonSize - Return the size of a 'common' symbol.
512   uint64_t getCommonSize() const {
513     assert(isCommon() && "Not a 'common' symbol!");
514     return CommonSize;
515   }
516
517   /// getCommonAlignment - Return the alignment of a 'common' symbol.
518   unsigned getCommonAlignment() const {
519     assert(isCommon() && "Not a 'common' symbol!");
520     return CommonAlign;
521   }
522
523   /// getFlags - Get the (implementation defined) symbol flags.
524   uint32_t getFlags() const { return Flags; }
525
526   /// setFlags - Set the (implementation defined) symbol flags.
527   void setFlags(uint32_t Value) { Flags = Value; }
528   
529   /// getIndex - Get the (implementation defined) index.
530   uint64_t getIndex() const { return Index; }
531
532   /// setIndex - Set the (implementation defined) index.
533   void setIndex(uint64_t Value) { Index = Value; }
534   
535   /// @}  
536 };
537
538 // FIXME: This really doesn't belong here. See comments below.
539 struct IndirectSymbolData {
540   MCSymbol *Symbol;
541   MCSectionData *SectionData;
542 };
543
544 class MCAssembler {
545 public:
546   typedef iplist<MCSectionData> SectionDataListType;
547   typedef iplist<MCSymbolData> SymbolDataListType;
548
549   typedef SectionDataListType::const_iterator const_iterator;
550   typedef SectionDataListType::iterator iterator;
551
552   typedef SymbolDataListType::const_iterator const_symbol_iterator;
553   typedef SymbolDataListType::iterator symbol_iterator;
554
555   typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
556
557 private:
558   MCAssembler(const MCAssembler&);    // DO NOT IMPLEMENT
559   void operator=(const MCAssembler&); // DO NOT IMPLEMENT
560
561   MCContext &Context;
562
563   raw_ostream &OS;
564   
565   iplist<MCSectionData> Sections;
566
567   iplist<MCSymbolData> Symbols;
568
569   std::vector<IndirectSymbolData> IndirectSymbols;
570
571   unsigned SubsectionsViaSymbols : 1;
572
573 private:
574   /// LayoutSection - Assign offsets and sizes to the fragments in the section
575   /// \arg SD, and update the section size. The section file offset should
576   /// already have been computed.
577   void LayoutSection(MCSectionData &SD);
578
579 public:
580   /// Construct a new assembler instance.
581   ///
582   /// \arg OS - The stream to output to.
583   //
584   // FIXME: How are we going to parameterize this? Two obvious options are stay
585   // concrete and require clients to pass in a target like object. The other
586   // option is to make this abstract, and have targets provide concrete
587   // implementations as we do with AsmParser.
588   MCAssembler(MCContext &_Context, raw_ostream &OS);
589   ~MCAssembler();
590
591   MCContext &getContext() const { return Context; }
592
593   /// Finish - Do final processing and write the object to the output stream.
594   void Finish();
595
596   // FIXME: This does not belong here.
597   bool getSubsectionsViaSymbols() const {
598     return SubsectionsViaSymbols;
599   }
600   void setSubsectionsViaSymbols(bool Value) {
601     SubsectionsViaSymbols = Value;
602   }
603
604   /// @name Section List Access
605   /// @{
606
607   const SectionDataListType &getSectionList() const { return Sections; }
608   SectionDataListType &getSectionList() { return Sections; }  
609
610   iterator begin() { return Sections.begin(); }
611   const_iterator begin() const { return Sections.begin(); }
612
613   iterator end() { return Sections.end(); }
614   const_iterator end() const { return Sections.end(); }
615
616   size_t size() const { return Sections.size(); }
617
618   /// @}
619   /// @name Symbol List Access
620   /// @{
621
622   const SymbolDataListType &getSymbolList() const { return Symbols; }
623   SymbolDataListType &getSymbolList() { return Symbols; }
624
625   symbol_iterator symbol_begin() { return Symbols.begin(); }
626   const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
627
628   symbol_iterator symbol_end() { return Symbols.end(); }
629   const_symbol_iterator symbol_end() const { return Symbols.end(); }
630
631   size_t symbol_size() const { return Symbols.size(); }
632
633   /// @}
634   /// @name Indirect Symbol List Access
635   /// @{
636
637   // FIXME: This is a total hack, this should not be here. Once things are
638   // factored so that the streamer has direct access to the .o writer, it can
639   // disappear.
640   std::vector<IndirectSymbolData> &getIndirectSymbols() {
641     return IndirectSymbols;
642   }
643
644   indirect_symbol_iterator indirect_symbol_begin() {
645     return IndirectSymbols.begin();
646   }
647
648   indirect_symbol_iterator indirect_symbol_end() {
649     return IndirectSymbols.end();
650   }
651
652   size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
653
654   /// @}
655 };
656
657 } // end namespace llvm
658
659 #endif