1 : // Copyright 2015 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Declares the assembler's label.
16 :
17 : #ifndef SYZYGY_ASSM_LABEL_BASE_H_
18 : #define SYZYGY_ASSM_LABEL_BASE_H_
19 :
20 : #include <vector>
21 :
22 : #include "base/basictypes.h"
23 : #include "base/macros.h"
24 :
25 : namespace assm {
26 :
27 : // Fwd.
28 : template <class ReferenceType>
29 : class AssemblerBase;
30 :
31 : // A label comes into existence unbound, and must be bound to the current
32 : // location at some point. Typical usage might go:
33 : //
34 : // Label success(&assm);
35 : // assm.cmp(...)
36 : // assm.j(kNotZero, &success);
37 : // ...
38 : // success.Bind(); // Binds the label to the assembler's current location.
39 : // ...
40 : template <class ReferenceType>
41 : class LabelBase {
42 : public:
43 : typedef AssemblerBase<ReferenceType> Assembler;
44 :
45 : explicit LabelBase(Assembler* assm);
46 : ~LabelBase();
47 :
48 : // Binds the label to the current assembly address.
49 : // @returns true on success.
50 : // @note binding can fail if the assembler's serializer doesn't support it,
51 : // or if any use of the label is out of range for a PC-relative reference
52 : // to the current address.
53 : bool Bind();
54 :
55 : private:
56 : friend class Assembler;
57 :
58 : struct LabelUsage {
59 : // The location of the use.
60 : uint32 location;
61 : // The size of the
62 : RegisterSize size;
63 : };
64 :
65 E : bool bound() const { return bound_; }
66 E : uint32 location() const { return location_; }
67 :
68 : // Interface for assembler to declare usage of unbound labels.
69 : void Use(uint32 location, RegisterSize size);
70 :
71 : // Revisits label usages and writes them with the correct value.
72 : bool Finalize();
73 :
74 : // The assembler this label belongs to.
75 : Assembler* assm_;
76 :
77 : // True iff the label is bound.
78 : bool bound_;
79 :
80 : // The location this label is bound to. Valid iff bound_ is true;
81 : uint32 location_;
82 :
83 : // Keeps track of where the unbound label has been used.
84 : std::vector<LabelUsage> uses_;
85 :
86 : DISALLOW_COPY_AND_ASSIGN(LabelBase);
87 : };
88 :
89 : template <class ReferenceType>
90 : LabelBase<ReferenceType>::LabelBase(Assembler* assm) :
91 E : assm_(assm), bound_(false), location_(0) {
92 E : DCHECK(assm != NULL);
93 E : }
94 :
95 : template <class ReferenceType>
96 E : LabelBase<ReferenceType>::~LabelBase() {
97 E : DCHECK_EQ(0U, uses_.size());
98 E : }
99 :
100 : template <class ReferenceType>
101 E : bool LabelBase<ReferenceType>::Bind() {
102 E : bound_ = true;
103 E : location_ = assm_->location();
104 :
105 E : return Finalize();
106 E : }
107 :
108 : template <class ReferenceType>
109 E : void LabelBase<ReferenceType>::Use(uint32 location, RegisterSize size) {
110 E : DCHECK(!bound_);
111 E : LabelUsage usage = { location, size };
112 E : uses_.push_back(usage);
113 E : }
114 :
115 : template <class ReferenceType>
116 E : bool LabelBase<ReferenceType>::Finalize() {
117 E : bool success = true;
118 E : for (auto usage: uses_) {
119 E : success = assm_->FinalizeLabel(usage.location, location_, usage.size);
120 E : if (!success)
121 E : break;
122 : }
123 E : uses_.clear();
124 :
125 E : return success;
126 E : }
127 :
128 : } // namespace assm
129 :
130 : #endif // SYZYGY_ASSM_LABEL_BASE_H_
|