1 /*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "fields/scalar_field.h"
18
19 #include "fields/fixed_scalar_field.h"
20 #include "fields/size_field.h"
21 #include "util.h"
22
23 const std::string ScalarField::kFieldType = "ScalarField";
24
ScalarField(std::string name,int size,ParseLocation loc)25 ScalarField::ScalarField(std::string name, int size, ParseLocation loc)
26 : PacketField(name, loc), size_(size) {
27 if (size_ > 64 || size_ < 0) {
28 ERROR(this) << "Not implemented for size_ = " << size_;
29 }
30 }
31
GetFieldType() const32 const std::string& ScalarField::GetFieldType() const { return ScalarField::kFieldType; }
33
GetSize() const34 Size ScalarField::GetSize() const { return size_; }
35
GetDataType() const36 std::string ScalarField::GetDataType() const { return util::GetTypeForSize(size_); }
37
GetShiftBits(int i)38 int GetShiftBits(int i) {
39 int bits_past_byte_boundary = i % 8;
40 if (bits_past_byte_boundary == 0) {
41 return 0;
42 } else {
43 return 8 - bits_past_byte_boundary;
44 }
45 }
46
GenBounds(std::ostream & s,Size start_offset,Size end_offset,Size size) const47 int ScalarField::GenBounds(std::ostream& s, Size start_offset, Size end_offset, Size size) const {
48 int num_leading_bits = 0;
49
50 if (!start_offset.empty()) {
51 // Default to start if available.
52 num_leading_bits = start_offset.bits() % 8;
53 s << "auto " << GetName() << "_it = to_bound + (" << start_offset << ") / 8;";
54 } else if (!end_offset.empty()) {
55 num_leading_bits = GetShiftBits(end_offset.bits() + size.bits());
56 Size byte_offset = Size(num_leading_bits + size.bits()) + end_offset;
57 s << "auto " << GetName() << "_it = to_bound + (to_bound.NumBytesRemaining() - (" << byte_offset
58 << ") / 8);";
59 } else {
60 ERROR(this) << "Ambiguous offset for field.";
61 }
62 return num_leading_bits;
63 }
64
GenExtractor(std::ostream & s,int num_leading_bits,bool) const65 void ScalarField::GenExtractor(std::ostream& s, int num_leading_bits, bool) const {
66 Size size = GetSize();
67 // Extract the correct number of bytes. The return type could be different
68 // from the extract type if an earlier field causes the beginning of the
69 // current field to start in the middle of a byte.
70 std::string extract_type = util::GetTypeForSize(size.bits() + num_leading_bits);
71 s << "auto extracted_value = " << GetName() << "_it.extract<" << extract_type << ">();";
72
73 // Right shift the result to remove leading bits.
74 if (num_leading_bits != 0) {
75 s << "extracted_value >>= " << num_leading_bits << ";";
76 }
77 // Mask the result if necessary.
78 if (util::RoundSizeUp(size.bits()) != size.bits()) {
79 uint64_t mask = 0;
80 for (int i = 0; i < size.bits(); i++) {
81 mask <<= 1;
82 mask |= 1;
83 }
84 s << "extracted_value &= 0x" << std::hex << mask << std::dec << ";";
85 }
86 s << "*" << GetName() << "_ptr = static_cast<" << GetDataType() << ">(extracted_value);";
87 }
88
GetGetterFunctionName() const89 std::string ScalarField::GetGetterFunctionName() const {
90 std::stringstream ss;
91 ss << "Get" << util::UnderscoreToCamelCase(GetName());
92 return ss.str();
93 }
94
GenGetter(std::ostream & s,Size start_offset,Size end_offset) const95 void ScalarField::GenGetter(std::ostream& s, Size start_offset, Size end_offset) const {
96 s << GetDataType() << " " << GetGetterFunctionName() << "() const {";
97 s << "ASSERT(was_validated_);";
98 s << "auto to_bound = begin();";
99 int num_leading_bits = GenBounds(s, start_offset, end_offset, GetSize());
100 s << GetDataType() << " " << GetName() << "_value{};";
101 s << GetDataType() << "* " << GetName() << "_ptr = &" << GetName() << "_value;";
102 GenExtractor(s, num_leading_bits, false);
103 s << "return " << GetName() << "_value;";
104 s << "}";
105 }
106
GetBuilderParameterType() const107 std::string ScalarField::GetBuilderParameterType() const { return GetDataType(); }
108
HasParameterValidator() const109 bool ScalarField::HasParameterValidator() const {
110 return util::RoundSizeUp(GetSize().bits()) != GetSize().bits();
111 }
112
GenParameterValidator(std::ostream & s) const113 void ScalarField::GenParameterValidator(std::ostream& s) const {
114 s << "ASSERT(" << GetName() << " < (static_cast<uint64_t>(1) << " << GetSize().bits() << "));";
115 }
116
GenInserter(std::ostream & s) const117 void ScalarField::GenInserter(std::ostream& s) const {
118 if (GetSize().bits() == 8) {
119 s << "i.insert_byte(" << GetName() << "_);";
120 } else {
121 s << "insert(" << GetName() << "_, i," << GetSize().bits() << ");";
122 }
123 }
124
GenValidator(std::ostream &) const125 void ScalarField::GenValidator(std::ostream&) const {
126 // Do nothing
127 }
128
GenStringRepresentation(std::ostream & s,std::string accessor) const129 void ScalarField::GenStringRepresentation(std::ostream& s, std::string accessor) const {
130 // Generate a static_cast to uint64_t (largest supported field type) in order
131 // to force hexadecimal formatting; uint8_t fields will be rendered as escaped
132 // characters otherwise.
133 s << "static_cast<uint64_t>(" << accessor << ")";
134 }
135