blob: f0efb6ce4a27f6711afebc4d92d18323930a9969 [file] [log] [blame]
Daniel Dunbar7760fbe2009-08-21 09:11:24 +00001//===- 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
Daniel Dunbarbd0b61d2010-03-10 20:58:29 +000013#include "llvm/ADT/DenseMap.h"
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +000014#include "llvm/ADT/SmallString.h"
Daniel Dunbar7760fbe2009-08-21 09:11:24 +000015#include "llvm/ADT/ilist.h"
16#include "llvm/ADT/ilist_node.h"
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +000017#include "llvm/Support/Casting.h"
Daniel Dunbarec3736d2010-02-13 09:28:54 +000018#include "llvm/MC/MCFixup.h"
Chandler Carruth1ec7df12009-10-26 01:35:46 +000019#include "llvm/System/DataTypes.h"
Daniel Dunbara0151d02009-08-24 11:56:58 +000020#include <vector> // FIXME: Shouldn't be needed.
Daniel Dunbar7760fbe2009-08-21 09:11:24 +000021
22namespace llvm {
23class raw_ostream;
24class MCAssembler;
Daniel Dunbar29efe7e2009-08-31 08:07:55 +000025class MCContext;
Daniel Dunbar4e5c1b62009-10-16 01:58:03 +000026class MCExpr;
Daniel Dunbar5e65c6f2010-02-11 21:29:29 +000027class MCFragment;
Daniel Dunbar7760fbe2009-08-21 09:11:24 +000028class MCSection;
29class MCSectionData;
Daniel Dunbar4e5c1b62009-10-16 01:58:03 +000030class MCSymbol;
Daniel Dunbard155a232010-03-11 01:34:27 +000031class TargetAsmBackend;
Daniel Dunbar7760fbe2009-08-21 09:11:24 +000032
Daniel Dunbar5e65c6f2010-02-11 21:29:29 +000033/// MCAsmFixup - Represent a fixed size region of bytes inside some fragment
34/// which needs to be rewritten. This region will either be rewritten by the
35/// assembler or cause a relocation entry to be generated.
36struct MCAsmFixup {
Daniel Dunbar5e65c6f2010-02-11 21:29:29 +000037 /// Offset - The offset inside the fragment which needs to be rewritten.
38 uint64_t Offset;
39
40 /// Value - The expression to eventually write into the fragment.
41 const MCExpr *Value;
42
Daniel Dunbarec3736d2010-02-13 09:28:54 +000043 /// Kind - The fixup kind.
44 MCFixupKind Kind;
Daniel Dunbar5e65c6f2010-02-11 21:29:29 +000045
46 /// FixedValue - The value to replace the fix up by.
47 //
48 // FIXME: This should not be here.
49 uint64_t FixedValue;
50
51public:
Daniel Dunbarec3736d2010-02-13 09:28:54 +000052 MCAsmFixup(uint64_t _Offset, const MCExpr &_Value, MCFixupKind _Kind)
53 : Offset(_Offset), Value(&_Value), Kind(_Kind), FixedValue(0) {}
Daniel Dunbar5e65c6f2010-02-11 21:29:29 +000054};
55
Daniel Dunbar7760fbe2009-08-21 09:11:24 +000056class MCFragment : public ilist_node<MCFragment> {
57 MCFragment(const MCFragment&); // DO NOT IMPLEMENT
58 void operator=(const MCFragment&); // DO NOT IMPLEMENT
59
60public:
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +000061 enum FragmentType {
62 FT_Data,
63 FT_Align,
64 FT_Fill,
Daniel Dunbardf98a142009-08-28 05:49:21 +000065 FT_Org,
66 FT_ZeroFill
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +000067 };
68
69private:
70 FragmentType Kind;
71
Daniel Dunbarb3730b12009-08-26 02:48:04 +000072 /// Parent - The data for the section this fragment is in.
73 MCSectionData *Parent;
74
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +000075 /// @name Assembler Backend Data
76 /// @{
77 //
78 // FIXME: This could all be kept private to the assembler implementation.
79
Daniel Dunbar1f022e22009-08-22 08:27:54 +000080 /// Offset - The offset of this fragment in its section. This is ~0 until
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +000081 /// initialized.
Daniel Dunbar1f022e22009-08-22 08:27:54 +000082 uint64_t Offset;
83
84 /// FileSize - The file size of this section. This is ~0 until initialized.
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +000085 uint64_t FileSize;
86
87 /// @}
88
89protected:
Daniel Dunbarb3730b12009-08-26 02:48:04 +000090 MCFragment(FragmentType _Kind, MCSectionData *_Parent = 0);
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +000091
92public:
93 // Only for sentinel.
94 MCFragment();
95 virtual ~MCFragment();
96
97 FragmentType getKind() const { return Kind; }
98
Daniel Dunbarb3730b12009-08-26 02:48:04 +000099 MCSectionData *getParent() const { return Parent; }
100 void setParent(MCSectionData *Value) { Parent = Value; }
101
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000102 // FIXME: This should be abstract, fix sentinel.
Daniel Dunbar3d153ff2009-08-21 23:07:38 +0000103 virtual uint64_t getMaxFileSize() const {
Daniel Dunbar197f5632009-08-22 10:13:24 +0000104 assert(0 && "Invalid getMaxFileSize call!");
Daniel Dunbar33bf7c22009-08-21 23:11:36 +0000105 return 0;
Douglas Gregor8cb41382009-12-19 07:05:23 +0000106 }
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000107
108 /// @name Assembler Backend Support
109 /// @{
110 //
111 // FIXME: This could all be kept private to the assembler implementation.
112
Daniel Dunbarb3730b12009-08-26 02:48:04 +0000113 uint64_t getAddress() const;
114
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000115 uint64_t getFileSize() const {
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000116 assert(FileSize != ~UINT64_C(0) && "File size not set!");
117 return FileSize;
118 }
119 void setFileSize(uint64_t Value) {
120 assert(Value <= getMaxFileSize() && "Invalid file size!");
121 FileSize = Value;
122 }
123
Daniel Dunbar1f022e22009-08-22 08:27:54 +0000124 uint64_t getOffset() const {
125 assert(Offset != ~UINT64_C(0) && "File offset not set!");
126 return Offset;
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000127 }
Daniel Dunbar1f022e22009-08-22 08:27:54 +0000128 void setOffset(uint64_t Value) { Offset = Value; }
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000129
130 /// @}
131
132 static bool classof(const MCFragment *O) { return true; }
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000133
134 virtual void dump();
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000135};
136
137class MCDataFragment : public MCFragment {
138 SmallString<32> Contents;
139
Daniel Dunbar8685fa02010-02-13 09:28:43 +0000140 /// Fixups - The list of fixups in this fragment.
141 std::vector<MCAsmFixup> Fixups;
142
143public:
144 typedef std::vector<MCAsmFixup>::const_iterator const_fixup_iterator;
145 typedef std::vector<MCAsmFixup>::iterator fixup_iterator;
146
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000147public:
148 MCDataFragment(MCSectionData *SD = 0) : MCFragment(FT_Data, SD) {}
149
150 /// @name Accessors
151 /// @{
152
Daniel Dunbar3d153ff2009-08-21 23:07:38 +0000153 uint64_t getMaxFileSize() const {
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000154 return Contents.size();
155 }
156
157 SmallString<32> &getContents() { return Contents; }
158 const SmallString<32> &getContents() const { return Contents; }
159
160 /// @}
161
Daniel Dunbar8685fa02010-02-13 09:28:43 +0000162 /// @name Fixup Access
163 /// @{
164
Daniel Dunbar03e06402010-03-12 21:00:38 +0000165 void addFixup(MCAsmFixup Fixup) {
166 // Enforce invariant that fixups are in offset order.
167 assert(Fixups.empty() || Fixup.Offset > Fixups.back().Offset &&
168 "Fixups must be added in order!");
169 Fixups.push_back(Fixup);
170 }
171
Daniel Dunbar8685fa02010-02-13 09:28:43 +0000172 std::vector<MCAsmFixup> &getFixups() { return Fixups; }
173 const std::vector<MCAsmFixup> &getFixups() const { return Fixups; }
174
175 fixup_iterator fixup_begin() { return Fixups.begin(); }
176 const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
177
178 fixup_iterator fixup_end() {return Fixups.end();}
179 const_fixup_iterator fixup_end() const {return Fixups.end();}
180
181 size_t fixup_size() const { return Fixups.size(); }
182
183 /// @}
184
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000185 static bool classof(const MCFragment *F) {
186 return F->getKind() == MCFragment::FT_Data;
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000187 }
188 static bool classof(const MCDataFragment *) { return true; }
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000189
190 virtual void dump();
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000191};
192
193class MCAlignFragment : public MCFragment {
194 /// Alignment - The alignment to ensure, in bytes.
195 unsigned Alignment;
196
197 /// Value - Value to use for filling padding bytes.
198 int64_t Value;
199
200 /// ValueSize - The size of the integer (in bytes) of \arg Value.
201 unsigned ValueSize;
202
203 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
204 /// cannot be satisfied in this width then this fragment is ignored.
205 unsigned MaxBytesToEmit;
206
Kevin Enderbyd767d4e2010-02-23 18:26:34 +0000207 /// EmitNops - true when aligning code and optimal nops to be used for filling
208 bool EmitNops;
209
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000210public:
211 MCAlignFragment(unsigned _Alignment, int64_t _Value, unsigned _ValueSize,
Kevin Enderbyd767d4e2010-02-23 18:26:34 +0000212 unsigned _MaxBytesToEmit, bool _EmitNops,
213 MCSectionData *SD = 0)
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000214 : MCFragment(FT_Align, SD), Alignment(_Alignment),
215 Value(_Value),ValueSize(_ValueSize),
Kevin Enderbyd767d4e2010-02-23 18:26:34 +0000216 MaxBytesToEmit(_MaxBytesToEmit), EmitNops(_EmitNops) {}
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000217
218 /// @name Accessors
219 /// @{
220
Daniel Dunbar3d153ff2009-08-21 23:07:38 +0000221 uint64_t getMaxFileSize() const {
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000222 return std::max(Alignment - 1, MaxBytesToEmit);
223 }
224
225 unsigned getAlignment() const { return Alignment; }
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000226
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000227 int64_t getValue() const { return Value; }
228
229 unsigned getValueSize() const { return ValueSize; }
230
231 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
232
Kevin Enderbyd767d4e2010-02-23 18:26:34 +0000233 unsigned getEmitNops() const { return EmitNops; }
234
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000235 /// @}
236
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000237 static bool classof(const MCFragment *F) {
238 return F->getKind() == MCFragment::FT_Align;
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000239 }
240 static bool classof(const MCAlignFragment *) { return true; }
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000241
242 virtual void dump();
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000243};
244
245class MCFillFragment : public MCFragment {
246 /// Value - Value to use for filling bytes.
Daniel Dunbarb591b732010-02-13 09:28:32 +0000247 int64_t Value;
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000248
249 /// ValueSize - The size (in bytes) of \arg Value to use when filling.
250 unsigned ValueSize;
251
252 /// Count - The number of copies of \arg Value to insert.
253 uint64_t Count;
254
255public:
Daniel Dunbarb591b732010-02-13 09:28:32 +0000256 MCFillFragment(int64_t _Value, unsigned _ValueSize, uint64_t _Count,
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000257 MCSectionData *SD = 0)
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000258 : MCFragment(FT_Fill, SD),
Daniel Dunbarb591b732010-02-13 09:28:32 +0000259 Value(_Value), ValueSize(_ValueSize), Count(_Count) {}
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000260
261 /// @name Accessors
262 /// @{
263
Daniel Dunbar3d153ff2009-08-21 23:07:38 +0000264 uint64_t getMaxFileSize() const {
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000265 return ValueSize * Count;
266 }
267
Daniel Dunbarb591b732010-02-13 09:28:32 +0000268 int64_t getValue() const { return Value; }
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000269
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000270 unsigned getValueSize() const { return ValueSize; }
271
272 uint64_t getCount() const { return Count; }
273
274 /// @}
275
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000276 static bool classof(const MCFragment *F) {
277 return F->getKind() == MCFragment::FT_Fill;
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000278 }
279 static bool classof(const MCFillFragment *) { return true; }
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000280
281 virtual void dump();
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000282};
283
284class MCOrgFragment : public MCFragment {
285 /// Offset - The offset this fragment should start at.
Daniel Dunbar4e5c1b62009-10-16 01:58:03 +0000286 const MCExpr *Offset;
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000287
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000288 /// Value - Value to use for filling bytes.
Daniel Dunbar3d153ff2009-08-21 23:07:38 +0000289 int8_t Value;
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000290
291public:
Daniel Dunbar4e5c1b62009-10-16 01:58:03 +0000292 MCOrgFragment(const MCExpr &_Offset, int8_t _Value, MCSectionData *SD = 0)
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000293 : MCFragment(FT_Org, SD),
Daniel Dunbar4e5c1b62009-10-16 01:58:03 +0000294 Offset(&_Offset), Value(_Value) {}
Daniel Dunbardf98a142009-08-28 05:49:21 +0000295
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000296 /// @name Accessors
297 /// @{
298
Daniel Dunbar3d153ff2009-08-21 23:07:38 +0000299 uint64_t getMaxFileSize() const {
300 // FIXME: This doesn't make much sense.
301 return ~UINT64_C(0);
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000302 }
303
Daniel Dunbar4e5c1b62009-10-16 01:58:03 +0000304 const MCExpr &getOffset() const { return *Offset; }
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000305
Daniel Dunbar3d153ff2009-08-21 23:07:38 +0000306 uint8_t getValue() const { return Value; }
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000307
308 /// @}
309
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000310 static bool classof(const MCFragment *F) {
311 return F->getKind() == MCFragment::FT_Org;
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000312 }
313 static bool classof(const MCOrgFragment *) { return true; }
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000314
315 virtual void dump();
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000316};
317
Daniel Dunbardf98a142009-08-28 05:49:21 +0000318/// MCZeroFillFragment - Represent data which has a fixed size and alignment,
319/// but requires no physical space in the object file.
320class MCZeroFillFragment : public MCFragment {
321 /// Size - The size of this fragment.
322 uint64_t Size;
323
324 /// Alignment - The alignment for this fragment.
325 unsigned Alignment;
326
327public:
328 MCZeroFillFragment(uint64_t _Size, unsigned _Alignment, MCSectionData *SD = 0)
329 : MCFragment(FT_ZeroFill, SD),
330 Size(_Size), Alignment(_Alignment) {}
331
332 /// @name Accessors
333 /// @{
334
335 uint64_t getMaxFileSize() const {
336 // FIXME: This also doesn't make much sense, this method is misnamed.
337 return ~UINT64_C(0);
338 }
339
340 uint64_t getSize() const { return Size; }
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000341
Daniel Dunbardf98a142009-08-28 05:49:21 +0000342 unsigned getAlignment() const { return Alignment; }
343
344 /// @}
345
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000346 static bool classof(const MCFragment *F) {
347 return F->getKind() == MCFragment::FT_ZeroFill;
Daniel Dunbardf98a142009-08-28 05:49:21 +0000348 }
349 static bool classof(const MCZeroFillFragment *) { return true; }
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000350
351 virtual void dump();
Daniel Dunbardf98a142009-08-28 05:49:21 +0000352};
353
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000354// FIXME: Should this be a separate class, or just merged into MCSection? Since
355// we anticipate the fast path being through an MCAssembler, the only reason to
356// keep it out is for API abstraction.
357class MCSectionData : public ilist_node<MCSectionData> {
358 MCSectionData(const MCSectionData&); // DO NOT IMPLEMENT
359 void operator=(const MCSectionData&); // DO NOT IMPLEMENT
360
361public:
362 typedef iplist<MCFragment> FragmentListType;
363
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000364 typedef FragmentListType::const_iterator const_iterator;
365 typedef FragmentListType::iterator iterator;
366
Daniel Dunbarbd91fed2010-02-11 21:29:46 +0000367 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
368 typedef FragmentListType::reverse_iterator reverse_iterator;
Daniel Dunbar8fa9da22009-08-26 13:58:10 +0000369
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000370private:
371 iplist<MCFragment> Fragments;
Daniel Dunbaradb77e42009-08-27 00:38:04 +0000372 const MCSection *Section;
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000373
374 /// Alignment - The maximum alignment seen in this section.
375 unsigned Alignment;
376
377 /// @name Assembler Backend Data
378 /// @{
379 //
380 // FIXME: This could all be kept private to the assembler implementation.
381
Daniel Dunbarb3730b12009-08-26 02:48:04 +0000382 /// Address - The computed address of this section. This is ~0 until
383 /// initialized.
384 uint64_t Address;
385
Daniel Dunbar0f56d412009-08-26 04:13:32 +0000386 /// Size - The content size of this section. This is ~0 until initialized.
387 uint64_t Size;
388
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000389 /// FileSize - The size of this section in the object file. This is ~0 until
390 /// initialized.
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000391 uint64_t FileSize;
392
Daniel Dunbar8253f502010-02-02 21:44:01 +0000393 /// HasInstructions - Whether this section has had instructions emitted into
394 /// it.
395 unsigned HasInstructions : 1;
396
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000397 /// @}
398
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000399public:
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000400 // Only for use as sentinel.
401 MCSectionData();
402 MCSectionData(const MCSection &Section, MCAssembler *A = 0);
403
Daniel Dunbaradb77e42009-08-27 00:38:04 +0000404 const MCSection &getSection() const { return *Section; }
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000405
406 unsigned getAlignment() const { return Alignment; }
407 void setAlignment(unsigned Value) { Alignment = Value; }
408
Daniel Dunbarc3c19922009-08-26 13:57:54 +0000409 /// @name Fragment Access
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000410 /// @{
411
412 const FragmentListType &getFragmentList() const { return Fragments; }
413 FragmentListType &getFragmentList() { return Fragments; }
414
415 iterator begin() { return Fragments.begin(); }
416 const_iterator begin() const { return Fragments.begin(); }
417
418 iterator end() { return Fragments.end(); }
419 const_iterator end() const { return Fragments.end(); }
420
Daniel Dunbarbd91fed2010-02-11 21:29:46 +0000421 reverse_iterator rbegin() { return Fragments.rbegin(); }
422 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
423
424 reverse_iterator rend() { return Fragments.rend(); }
425 const_reverse_iterator rend() const { return Fragments.rend(); }
426
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000427 size_t size() const { return Fragments.size(); }
428
Daniel Dunbar197f5632009-08-22 10:13:24 +0000429 bool empty() const { return Fragments.empty(); }
430
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000431 /// @}
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000432 /// @name Assembler Backend Support
433 /// @{
434 //
435 // FIXME: This could all be kept private to the assembler implementation.
436
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000437 uint64_t getAddress() const {
Daniel Dunbarb3730b12009-08-26 02:48:04 +0000438 assert(Address != ~UINT64_C(0) && "Address not set!");
439 return Address;
440 }
441 void setAddress(uint64_t Value) { Address = Value; }
442
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000443 uint64_t getSize() const {
Daniel Dunbar0f56d412009-08-26 04:13:32 +0000444 assert(Size != ~UINT64_C(0) && "File size not set!");
445 return Size;
446 }
447 void setSize(uint64_t Value) { Size = Value; }
448
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000449 uint64_t getFileSize() const {
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000450 assert(FileSize != ~UINT64_C(0) && "File size not set!");
451 return FileSize;
452 }
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000453 void setFileSize(uint64_t Value) { FileSize = Value; }
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000454
Daniel Dunbar8253f502010-02-02 21:44:01 +0000455 bool hasInstructions() const { return HasInstructions; }
456 void setHasInstructions(bool Value) { HasInstructions = Value; }
457
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000458 /// @}
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000459
460 void dump();
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000461};
462
Daniel Dunbar197f5632009-08-22 10:13:24 +0000463// FIXME: Same concerns as with SectionData.
464class MCSymbolData : public ilist_node<MCSymbolData> {
465public:
Daniel Dunbaref0f6372009-09-01 04:09:03 +0000466 const MCSymbol *Symbol;
Daniel Dunbar197f5632009-08-22 10:13:24 +0000467
468 /// Fragment - The fragment this symbol's value is relative to, if any.
469 MCFragment *Fragment;
470
471 /// Offset - The offset to apply to the fragment address to form this symbol's
472 /// value.
473 uint64_t Offset;
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000474
Daniel Dunbar6fae16d2009-08-22 11:41:10 +0000475 /// IsExternal - True if this symbol is visible outside this translation
476 /// unit.
477 unsigned IsExternal : 1;
Daniel Dunbar197f5632009-08-22 10:13:24 +0000478
Daniel Dunbar30b09422009-08-24 08:40:12 +0000479 /// IsPrivateExtern - True if this symbol is private extern.
480 unsigned IsPrivateExtern : 1;
481
Daniel Dunbar8388a8f2009-08-28 07:08:35 +0000482 /// CommonSize - The size of the symbol, if it is 'common', or 0.
483 //
484 // FIXME: Pack this in with other fields? We could put it in offset, since a
485 // common symbol can never get a definition.
486 uint64_t CommonSize;
487
488 /// CommonAlign - The alignment of the symbol, if it is 'common'.
489 //
490 // FIXME: Pack this in with other fields?
491 unsigned CommonAlign;
492
Daniel Dunbar30b09422009-08-24 08:40:12 +0000493 /// Flags - The Flags field is used by object file implementations to store
494 /// additional per symbol information which is not easily classified.
495 uint32_t Flags;
496
Daniel Dunbarc3c19922009-08-26 13:57:54 +0000497 /// Index - Index field, for use by the object file implementation.
498 uint64_t Index;
499
Daniel Dunbar197f5632009-08-22 10:13:24 +0000500public:
501 // Only for use as sentinel.
502 MCSymbolData();
Daniel Dunbar22ad5f82009-08-31 08:08:06 +0000503 MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment, uint64_t _Offset,
Daniel Dunbar197f5632009-08-22 10:13:24 +0000504 MCAssembler *A = 0);
505
506 /// @name Accessors
507 /// @{
508
Daniel Dunbaref0f6372009-09-01 04:09:03 +0000509 const MCSymbol &getSymbol() const { return *Symbol; }
Daniel Dunbar197f5632009-08-22 10:13:24 +0000510
511 MCFragment *getFragment() const { return Fragment; }
512 void setFragment(MCFragment *Value) { Fragment = Value; }
513
514 uint64_t getOffset() const { return Offset; }
515 void setOffset(uint64_t Value) { Offset = Value; }
516
Daniel Dunbarf97aee12010-03-11 18:22:51 +0000517 uint64_t getAddress() const {
518 assert(getFragment() && "Invalid getAddress() on undefined symbol!");
519 return getFragment()->getAddress() + getOffset();
520 }
521
Daniel Dunbar30b09422009-08-24 08:40:12 +0000522 /// @}
523 /// @name Symbol Attributes
524 /// @{
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000525
Daniel Dunbar30b09422009-08-24 08:40:12 +0000526 bool isExternal() const { return IsExternal; }
527 void setExternal(bool Value) { IsExternal = Value; }
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000528
Daniel Dunbar30b09422009-08-24 08:40:12 +0000529 bool isPrivateExtern() const { return IsPrivateExtern; }
530 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
Daniel Dunbar8388a8f2009-08-28 07:08:35 +0000531
532 /// isCommon - Is this a 'common' symbol.
533 bool isCommon() const { return CommonSize != 0; }
534
535 /// setCommon - Mark this symbol as being 'common'.
536 ///
537 /// \param Size - The size of the symbol.
538 /// \param Align - The alignment of the symbol.
539 void setCommon(uint64_t Size, unsigned Align) {
540 CommonSize = Size;
541 CommonAlign = Align;
542 }
543
544 /// getCommonSize - Return the size of a 'common' symbol.
545 uint64_t getCommonSize() const {
546 assert(isCommon() && "Not a 'common' symbol!");
547 return CommonSize;
548 }
549
550 /// getCommonAlignment - Return the alignment of a 'common' symbol.
551 unsigned getCommonAlignment() const {
552 assert(isCommon() && "Not a 'common' symbol!");
553 return CommonAlign;
554 }
555
Daniel Dunbar30b09422009-08-24 08:40:12 +0000556 /// getFlags - Get the (implementation defined) symbol flags.
557 uint32_t getFlags() const { return Flags; }
Daniel Dunbar6fae16d2009-08-22 11:41:10 +0000558
Daniel Dunbar30b09422009-08-24 08:40:12 +0000559 /// setFlags - Set the (implementation defined) symbol flags.
560 void setFlags(uint32_t Value) { Flags = Value; }
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000561
Daniel Dunbarc3c19922009-08-26 13:57:54 +0000562 /// getIndex - Get the (implementation defined) index.
563 uint64_t getIndex() const { return Index; }
564
565 /// setIndex - Set the (implementation defined) index.
566 void setIndex(uint64_t Value) { Index = Value; }
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000567
568 /// @}
569
570 void dump();
Daniel Dunbar197f5632009-08-22 10:13:24 +0000571};
572
Daniel Dunbara0151d02009-08-24 11:56:58 +0000573// FIXME: This really doesn't belong here. See comments below.
574struct IndirectSymbolData {
575 MCSymbol *Symbol;
576 MCSectionData *SectionData;
577};
578
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000579class MCAssembler {
580public:
581 typedef iplist<MCSectionData> SectionDataListType;
Daniel Dunbar197f5632009-08-22 10:13:24 +0000582 typedef iplist<MCSymbolData> SymbolDataListType;
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000583
584 typedef SectionDataListType::const_iterator const_iterator;
585 typedef SectionDataListType::iterator iterator;
586
Daniel Dunbar197f5632009-08-22 10:13:24 +0000587 typedef SymbolDataListType::const_iterator const_symbol_iterator;
588 typedef SymbolDataListType::iterator symbol_iterator;
589
Daniel Dunbara0151d02009-08-24 11:56:58 +0000590 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
591
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000592private:
593 MCAssembler(const MCAssembler&); // DO NOT IMPLEMENT
594 void operator=(const MCAssembler&); // DO NOT IMPLEMENT
595
Daniel Dunbar29efe7e2009-08-31 08:07:55 +0000596 MCContext &Context;
597
Daniel Dunbard155a232010-03-11 01:34:27 +0000598 TargetAsmBackend &Backend;
599
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000600 raw_ostream &OS;
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000601
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000602 iplist<MCSectionData> Sections;
603
Daniel Dunbar197f5632009-08-22 10:13:24 +0000604 iplist<MCSymbolData> Symbols;
605
Daniel Dunbarbd0b61d2010-03-10 20:58:29 +0000606 /// The map of sections to their associated assembler backend data.
607 //
608 // FIXME: Avoid this indirection?
609 DenseMap<const MCSection*, MCSectionData*> SectionMap;
610
611 /// The map of symbols to their associated assembler backend data.
612 //
613 // FIXME: Avoid this indirection?
614 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
615
Daniel Dunbara0151d02009-08-24 11:56:58 +0000616 std::vector<IndirectSymbolData> IndirectSymbols;
617
Daniel Dunbarf8c4bb72009-08-26 21:22:22 +0000618 unsigned SubsectionsViaSymbols : 1;
619
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000620private:
621 /// LayoutSection - Assign offsets and sizes to the fragments in the section
622 /// \arg SD, and update the section size. The section file offset should
623 /// already have been computed.
Daniel Dunbar1c215be2009-08-28 05:49:04 +0000624 void LayoutSection(MCSectionData &SD);
Daniel Dunbar8fcefbe2009-08-21 18:29:01 +0000625
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000626public:
627 /// Construct a new assembler instance.
628 ///
629 /// \arg OS - The stream to output to.
630 //
631 // FIXME: How are we going to parameterize this? Two obvious options are stay
632 // concrete and require clients to pass in a target like object. The other
633 // option is to make this abstract, and have targets provide concrete
634 // implementations as we do with AsmParser.
Daniel Dunbard155a232010-03-11 01:34:27 +0000635 MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend, raw_ostream &OS);
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000636 ~MCAssembler();
637
Daniel Dunbar29efe7e2009-08-31 08:07:55 +0000638 MCContext &getContext() const { return Context; }
639
Daniel Dunbarfe3eef52010-03-11 02:28:59 +0000640 TargetAsmBackend &getBackend() const { return Backend; }
641
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000642 /// Finish - Do final processing and write the object to the output stream.
643 void Finish();
644
Daniel Dunbarf8c4bb72009-08-26 21:22:22 +0000645 // FIXME: This does not belong here.
646 bool getSubsectionsViaSymbols() const {
647 return SubsectionsViaSymbols;
648 }
649 void setSubsectionsViaSymbols(bool Value) {
650 SubsectionsViaSymbols = Value;
651 }
652
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000653 /// @name Section List Access
654 /// @{
655
656 const SectionDataListType &getSectionList() const { return Sections; }
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000657 SectionDataListType &getSectionList() { return Sections; }
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000658
659 iterator begin() { return Sections.begin(); }
660 const_iterator begin() const { return Sections.begin(); }
661
662 iterator end() { return Sections.end(); }
663 const_iterator end() const { return Sections.end(); }
664
665 size_t size() const { return Sections.size(); }
666
667 /// @}
Daniel Dunbar197f5632009-08-22 10:13:24 +0000668 /// @name Symbol List Access
669 /// @{
670
671 const SymbolDataListType &getSymbolList() const { return Symbols; }
672 SymbolDataListType &getSymbolList() { return Symbols; }
673
674 symbol_iterator symbol_begin() { return Symbols.begin(); }
675 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
676
677 symbol_iterator symbol_end() { return Symbols.end(); }
678 const_symbol_iterator symbol_end() const { return Symbols.end(); }
679
680 size_t symbol_size() const { return Symbols.size(); }
681
682 /// @}
Daniel Dunbara0151d02009-08-24 11:56:58 +0000683 /// @name Indirect Symbol List Access
684 /// @{
685
686 // FIXME: This is a total hack, this should not be here. Once things are
687 // factored so that the streamer has direct access to the .o writer, it can
688 // disappear.
689 std::vector<IndirectSymbolData> &getIndirectSymbols() {
690 return IndirectSymbols;
691 }
692
693 indirect_symbol_iterator indirect_symbol_begin() {
694 return IndirectSymbols.begin();
695 }
696
697 indirect_symbol_iterator indirect_symbol_end() {
698 return IndirectSymbols.end();
699 }
700
701 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
702
703 /// @}
Daniel Dunbarbd0b61d2010-03-10 20:58:29 +0000704 /// @name Backend Data Access
705 /// @{
706
707 MCSectionData &getSectionData(const MCSection &Section) {
708 MCSectionData *&Entry = SectionMap[&Section];
709 assert(Entry && "Missing section data!");
710 return *Entry;
711 }
712
713 MCSectionData &getOrCreateSectionData(const MCSection &Section,
714 bool *Created = 0) {
715 MCSectionData *&Entry = SectionMap[&Section];
716
717 if (Created) *Created = !Entry;
718 if (!Entry)
719 Entry = new MCSectionData(Section, this);
720
721 return *Entry;
722 }
723
724 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
725 MCSymbolData *&Entry = SymbolMap[&Symbol];
726 assert(Entry && "Missing symbol data!");
727 return *Entry;
728 }
729
730 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
731 bool *Created = 0) {
732 MCSymbolData *&Entry = SymbolMap[&Symbol];
733
734 if (Created) *Created = !Entry;
735 if (!Entry)
736 Entry = new MCSymbolData(Symbol, 0, 0, this);
737
738 return *Entry;
739 }
740
741 /// @}
Daniel Dunbar6ad454a2010-02-13 09:28:03 +0000742
743 void dump();
Daniel Dunbar7760fbe2009-08-21 09:11:24 +0000744};
745
746} // end namespace llvm
747
748#endif