• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- MCFragmentRef.h ----------------------------------------------------===//
2 //
3 //                     The MCLinker Project
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #ifndef MCLD_MCFRAGMENT_REFERENCE_H
10 #define MCLD_MCFRAGMENT_REFERENCE_H
11 #ifdef ENABLE_UNITTEST
12 #include <gtest.h>
13 #endif
14 #include <llvm/MC/MCAssembler.h>
15 #include <mcld/ADT/SizeTraits.h>
16 #include <mcld/ADT/TypeTraits.h>
17 
18 namespace mcld
19 {
20 
21 class Layout;
22 
23 /// compunteFragmentSize - compute the specific MCFragment size
24 uint64_t computeFragmentSize(const Layout& pLayout,
25                              const llvm::MCFragment& pFrag);
26 
27 /** \class MCFragmentRef
28  *  \brief MCFragmentRef is a reference of a MCFragment's contetnt.
29  *
30  */
31 class MCFragmentRef
32 {
33 public:
34   typedef uint64_t Offset; // FIXME: use SizeTraits<T>::Offset
35   typedef NonConstTraits<unsigned char>::pointer Address;
36   typedef ConstTraits<unsigned char>::pointer ConstAddress;
37 
38 public:
39   MCFragmentRef();
40   MCFragmentRef(llvm::MCFragment& pFrag, Offset pOffset = 0);
41   ~MCFragmentRef();
42 
43   // -----  modifiers  ----- //
44   MCFragmentRef& assign(const MCFragmentRef& pCopy);
45 
46   MCFragmentRef& assign(llvm::MCFragment& pFrag, Offset pOffset = 0);
47 
48   /// memcpy - copy memory
49   /// copy memory from the fragment to the pDesc.
50   /// @pDest - the destination address
51   /// @pNBytes - copies pNBytes from the fragment[offset()+pOffset]
52   /// @pOffset - additional offset.
53   ///            the start address offset from fragment[offset()]
54   void memcpy(void* pDest, size_t pNBytes, Offset pOffset = 0) const;
55 
56   // -----  observers  ----- //
frag()57   llvm::MCFragment* frag()
58   { return m_pFragment; }
59 
frag()60   const llvm::MCFragment* frag() const
61   { return m_pFragment; }
62 
offset()63   Offset offset() const
64   { return m_Offset; }
65 
66   // -----  dereference  ----- //
67   Address deref();
68 
69   ConstAddress deref() const;
70 
71   Address operator*()
72   { return deref(); }
73 
74   ConstAddress operator*() const
75   { return deref(); }
76 
77 private:
78   llvm::MCFragment* m_pFragment;
79   Offset m_Offset;
80 };
81 
82 } // namespace of mcld
83 
84 #endif
85 
86