1 : // Copyright 2014 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 : // This file declares implementation classes to generate assembly code.
16 : // The API to the assembler is intentionally very close to the API exposed
17 : // by the V8 assembler (see src/ia32/assembler-ia32.* in V8 repository).
18 :
19 : #ifndef SYZYGY_ASSM_VALUE_BASE_H_
20 : #define SYZYGY_ASSM_VALUE_BASE_H_
21 :
22 : #include "syzygy/assm/register.h"
23 :
24 : namespace assm {
25 :
26 : // We use the same enum for value sizes.
27 : typedef RegisterSize ValueSize;
28 :
29 : // An instance of this class is an explicit value, which is either
30 : // an immediate or a displacement.
31 : template <class ReferenceType, class SubclassType>
32 : class ValueBase {
33 : public:
34 : ValueBase();
35 : ValueBase(uint32_t value, ValueSize size);
36 : ValueBase(uint32_t value, ValueSize size, ReferenceType imm_ref);
37 :
38 : // @name Accessors.
39 : // @{
40 E : uint32_t value() const { return value_; }
41 E : const ReferenceType& reference() const { return reference_; }
42 E : ValueSize size() const { return size_; }
43 : // @}
44 :
45 : // Comparison operator.
46 : bool operator==(const ValueBase& rhs) const;
47 :
48 : private:
49 : uint32_t value_;
50 : ReferenceType reference_;
51 : ValueSize size_;
52 : };
53 :
54 : template <class ReferenceType>
55 : class ImmediateBase
56 : : public ValueBase<ReferenceType, ImmediateBase<ReferenceType>> {
57 : public:
58 : typedef ValueBase<ReferenceType, ImmediateBase<ReferenceType>> Super;
59 :
60 E : ImmediateBase() {
61 E : }
62 E : ImmediateBase(uint32_t value, ValueSize size) : Super(value, size) {}
63 E : ImmediateBase(uint32_t value, ValueSize size, ReferenceType imm_ref)
64 E : : Super(value, size, imm_ref) {}
65 : };
66 :
67 : template <class ReferenceType>
68 : class DisplacementBase
69 : : public ValueBase<ReferenceType, DisplacementBase<ReferenceType>> {
70 : public:
71 : typedef ValueBase<ReferenceType, DisplacementBase<ReferenceType>> Super;
72 :
73 E : DisplacementBase() {
74 E : }
75 E : DisplacementBase(uint32_t value, ValueSize size) : Super(value, size) {}
76 E : DisplacementBase(uint32_t value, ValueSize size, ReferenceType imm_ref)
77 E : : Super(value, size, imm_ref) {}
78 : };
79 :
80 : template <class ReferenceType, class SubclassType>
81 : ValueBase<ReferenceType, SubclassType>::ValueBase()
82 E : : value_(0), reference_(), size_(kSizeNone) {
83 E : }
84 :
85 : template <class ReferenceType, class SubclassType>
86 : ValueBase<ReferenceType, SubclassType>::ValueBase(uint32_t value,
87 : ValueSize size)
88 E : : value_(value), reference_(), size_(size) {
89 E : }
90 :
91 : template <class ReferenceType, class SubclassType>
92 : ValueBase<ReferenceType, SubclassType>::ValueBase(uint32_t value,
93 : ValueSize size,
94 : ReferenceType value_ref)
95 E : : value_(value), reference_(value_ref), size_(size) {
96 : // We can't have a 16-bit value *and* a reference, as there are no
97 : // addressing modes that accept 16-bit input.
98 :
99 E : DCHECK(!details::IsValidReference(value_ref) || size != kSize16Bit);
100 E : }
101 :
102 : template <class ReferenceType, class SubclassType>
103 : bool ValueBase<ReferenceType, SubclassType>::operator==(
104 E : const ValueBase& rhs) const {
105 E : return value_ == rhs.value_ &&
106 : reference_ == rhs.reference_ &&
107 : size_ == rhs.size_;
108 E : }
109 :
110 : } // namespace assm
111 :
112 : #endif // SYZYGY_ASSM_VALUE_BASE_H_
|