Skip to content

Commit

Permalink
upb: tag upb_MiniTableField:offset as UPB_ONLYBITS()
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 592081461
  • Loading branch information
ericsalo authored and copybara-github committed Dec 19, 2023
1 parent 212067d commit 1f67b8d
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 46 deletions.
5 changes: 3 additions & 2 deletions upb/message/accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,14 @@ UPB_API_INLINE upb_Message* upb_Message_GetOrCreateMutableMessage(
const upb_MiniTableField* field, upb_Arena* arena) {
UPB_ASSERT(arena);
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Message);
upb_Message* sub_message = *UPB_PTR_AT(msg, field->offset, upb_Message*);
upb_Message* sub_message =
*UPB_PTR_AT(msg, field->UPB_ONLYBITS(offset), upb_Message*);
if (!sub_message) {
const upb_MiniTable* sub_mini_table = upb_MiniTableSub_Message(
mini_table->UPB_PRIVATE(subs)[field->UPB_PRIVATE(submsg_index)]);
UPB_ASSERT(sub_mini_table);
sub_message = _upb_Message_New(sub_mini_table, arena);
*UPB_PTR_AT(msg, field->offset, upb_Message*) = sub_message;
*UPB_PTR_AT(msg, field->UPB_ONLYBITS(offset), upb_Message*) = sub_message;
UPB_PRIVATE(_upb_Message_SetPresence)(msg, field);
}
return sub_message;
Expand Down
4 changes: 2 additions & 2 deletions upb/message/internal/accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ UPB_INLINE void UPB_PRIVATE(_upb_Message_SetOneofCase)(

UPB_INLINE void* _upb_MiniTableField_GetPtr(upb_Message* msg,
const upb_MiniTableField* field) {
return (char*)msg + field->offset;
return (char*)msg + field->UPB_ONLYBITS(offset);
}

UPB_INLINE const void* _upb_MiniTableField_GetConstPtr(
const upb_Message* msg, const upb_MiniTableField* field) {
return (char*)msg + field->offset;
return (char*)msg + field->UPB_ONLYBITS(offset);
}

UPB_INLINE void UPB_PRIVATE(_upb_Message_SetPresence)(
Expand Down
41 changes: 21 additions & 20 deletions upb/mini_descriptor/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ static void upb_MiniTable_SetField(upb_MtDecoder* d, uint8_t ch,
type -= kUpb_EncodedType_RepeatedBase;
field->UPB_PRIVATE(mode) = kUpb_FieldMode_Array;
field->UPB_PRIVATE(mode) |= pointer_rep << kUpb_FieldRep_Shift;
field->offset = kNoPresence;
field->UPB_PRIVATE(offset) = kNoPresence;
} else {
field->UPB_PRIVATE(mode) = kUpb_FieldMode_Scalar;
field->offset = kHasbitPresence;
field->UPB_PRIVATE(offset) = kHasbitPresence;
if (type == kUpb_EncodedType_Group || type == kUpb_EncodedType_Message) {
field->UPB_PRIVATE(mode) |= pointer_rep << kUpb_FieldRep_Shift;
} else if ((unsigned long)type >= sizeof(kUpb_EncodedToFieldRep)) {
Expand Down Expand Up @@ -225,7 +225,7 @@ static void upb_MtDecoder_ModifyField(upb_MtDecoder* d,
bool required = field_modifiers & kUpb_EncodedFieldModifier_IsRequired;

// Validate.
if ((singular || required) && field->offset != kHasbitPresence) {
if ((singular || required) && field->UPB_PRIVATE(offset) != kHasbitPresence) {
upb_MdDecoder_ErrorJmp(&d->base,
"Invalid modifier(s) for repeated field %" PRIu32,
upb_MiniTableField_Number(field));
Expand All @@ -236,9 +236,9 @@ static void upb_MtDecoder_ModifyField(upb_MtDecoder* d,
upb_MiniTableField_Number(field));
}

if (singular) field->offset = kNoPresence;
if (singular) field->UPB_PRIVATE(offset) = kNoPresence;
if (required) {
field->offset = kRequiredPresence;
field->UPB_PRIVATE(offset) = kRequiredPresence;
}
}

Expand Down Expand Up @@ -325,7 +325,7 @@ static const char* upb_MtDecoder_DecodeOneofField(upb_MtDecoder* d,
" to oneof, no such field number.",
field_num);
}
if (f->offset != kHasbitPresence) {
if (f->UPB_PRIVATE(offset) != kHasbitPresence) {
upb_MdDecoder_ErrorJmp(
&d->base,
"Cannot add repeated, required, or singular field %" PRIu32
Expand All @@ -340,7 +340,7 @@ static const char* upb_MtDecoder_DecodeOneofField(upb_MtDecoder* d,
item->rep = rep;
}
// Prepend this field to the linked list.
f->offset = item->field_index;
f->UPB_PRIVATE(offset) = item->field_index;
item->field_index = (f - d->fields) + kOneofBase;
return ptr;
}
Expand Down Expand Up @@ -517,7 +517,7 @@ static bool upb_MtDecoder_SortLayoutItems(upb_MtDecoder* d) {
int n = d->table->UPB_PRIVATE(field_count);
for (int i = 0; i < n; i++) {
upb_MiniTableField* f = &d->fields[i];
if (f->offset >= kOneofBase) continue;
if (f->UPB_PRIVATE(offset) >= kOneofBase) continue;
upb_LayoutItem item = {.field_index = i,
.rep = f->UPB_PRIVATE(mode) >> kUpb_FieldRep_Shift,
.type = kUpb_LayoutItemType_Field};
Expand Down Expand Up @@ -545,9 +545,9 @@ static void upb_MtDecoder_AssignHasbits(upb_MtDecoder* d) {
for (int i = 0; i < n; i++) {
upb_MiniTableField* field =
(upb_MiniTableField*)&ret->UPB_PRIVATE(fields)[i];
if (field->offset == kRequiredPresence) {
if (field->UPB_PRIVATE(offset) == kRequiredPresence) {
field->presence = ++last_hasbit;
} else if (field->offset == kNoPresence) {
} else if (field->UPB_PRIVATE(offset) == kNoPresence) {
field->presence = 0;
}
}
Expand All @@ -561,7 +561,7 @@ static void upb_MtDecoder_AssignHasbits(upb_MtDecoder* d) {
for (int i = 0; i < n; i++) {
upb_MiniTableField* field =
(upb_MiniTableField*)&ret->UPB_PRIVATE(fields)[i];
if (field->offset == kHasbitPresence) {
if (field->UPB_PRIVATE(offset) == kHasbitPresence) {
field->presence = ++last_hasbit;
}
}
Expand Down Expand Up @@ -599,9 +599,10 @@ static void upb_MtDecoder_AssignOffsets(upb_MtDecoder* d) {
upb_MiniTableField* f = &d->fields[item->field_index];
while (true) {
f->presence = ~item->offset;
if (f->offset == kUpb_LayoutItem_IndexSentinel) break;
UPB_ASSERT(f->offset - kOneofBase < d->table->UPB_PRIVATE(field_count));
f = &d->fields[f->offset - kOneofBase];
if (f->UPB_PRIVATE(offset) == kUpb_LayoutItem_IndexSentinel) break;
UPB_ASSERT(f->UPB_PRIVATE(offset) - kOneofBase <
d->table->UPB_PRIVATE(field_count));
f = &d->fields[f->UPB_PRIVATE(offset) - kOneofBase];
}
}

Expand All @@ -611,14 +612,14 @@ static void upb_MtDecoder_AssignOffsets(upb_MtDecoder* d) {
switch (item->type) {
case kUpb_LayoutItemType_OneofField:
while (true) {
uint16_t next_offset = f->offset;
f->offset = item->offset;
uint16_t next_offset = f->UPB_PRIVATE(offset);
f->UPB_PRIVATE(offset) = item->offset;
if (next_offset == kUpb_LayoutItem_IndexSentinel) break;
f = &d->fields[next_offset - kOneofBase];
}
break;
case kUpb_LayoutItemType_Field:
f->offset = item->offset;
f->UPB_PRIVATE(offset) = item->offset;
break;
default:
break;
Expand Down Expand Up @@ -689,8 +690,8 @@ static void upb_MtDecoder_ParseMap(upb_MtDecoder* d, const char* data,
// NOTE: sync with mini_table/message_internal.h.
const size_t kv_size = d->platform == kUpb_MiniTablePlatform_32Bit ? 8 : 16;
const size_t hasbit_size = 8;
d->fields[0].offset = hasbit_size;
d->fields[1].offset = hasbit_size + kv_size;
d->fields[0].UPB_PRIVATE(offset) = hasbit_size;
d->fields[1].UPB_PRIVATE(offset) = hasbit_size + kv_size;
d->table->UPB_PRIVATE(size) =
UPB_ALIGN_UP(hasbit_size + kv_size + kv_size, 8);

Expand Down Expand Up @@ -815,7 +816,7 @@ static const char* upb_MtDecoder_DoBuildMiniTableExtension(
upb_MiniTableField* f = &ext->UPB_PRIVATE(field);

f->UPB_PRIVATE(mode) |= kUpb_LabelFlags_IsExtension;
f->offset = 0;
f->UPB_PRIVATE(offset) = 0;
f->presence = 0;

if (extendee->UPB_PRIVATE(ext) & kUpb_ExtMode_IsMessageSet) {
Expand Down
19 changes: 10 additions & 9 deletions upb/mini_descriptor/internal/encode_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ TEST_P(MiniTableTest, AllScalarTypes) {
const upb_MiniTableField* f = &table->UPB_PRIVATE(fields)[i];
EXPECT_EQ(i + 1, upb_MiniTableField_Number(f));
EXPECT_TRUE(upb_MiniTableField_IsScalar(f));
EXPECT_TRUE(offsets.insert(f->offset).second);
EXPECT_TRUE(f->offset < table->UPB_PRIVATE(size));
EXPECT_TRUE(offsets.insert(f->UPB_PRIVATE(offset)).second);
EXPECT_TRUE(f->UPB_PRIVATE(offset) < table->UPB_PRIVATE(size));
}
EXPECT_EQ(0, table->UPB_PRIVATE(required_count));
}
Expand All @@ -96,8 +96,8 @@ TEST_P(MiniTableTest, AllRepeatedTypes) {
const upb_MiniTableField* f = &table->UPB_PRIVATE(fields)[i];
EXPECT_EQ(i + 1, upb_MiniTableField_Number(f));
EXPECT_TRUE(upb_MiniTableField_IsArray(f));
EXPECT_TRUE(offsets.insert(f->offset).second);
EXPECT_TRUE(f->offset < table->UPB_PRIVATE(size));
EXPECT_TRUE(offsets.insert(f->UPB_PRIVATE(offset)).second);
EXPECT_TRUE(f->UPB_PRIVATE(offset) < table->UPB_PRIVATE(size));
}
EXPECT_EQ(0, table->UPB_PRIVATE(required_count));
}
Expand Down Expand Up @@ -125,8 +125,8 @@ TEST_P(MiniTableTest, Skips) {
EXPECT_EQ(field_numbers[i], upb_MiniTableField_Number(f));
EXPECT_EQ(kUpb_FieldType_Float, upb_MiniTableField_Type(f));
EXPECT_TRUE(upb_MiniTableField_IsScalar(f));
EXPECT_TRUE(offsets.insert(f->offset).second);
EXPECT_TRUE(f->offset < table->UPB_PRIVATE(size));
EXPECT_TRUE(offsets.insert(f->UPB_PRIVATE(offset)).second);
EXPECT_TRUE(f->UPB_PRIVATE(offset) < table->UPB_PRIVATE(size));
}
EXPECT_EQ(0, table->UPB_PRIVATE(required_count));
}
Expand Down Expand Up @@ -155,13 +155,14 @@ TEST_P(MiniTableTest, AllScalarTypesOneof) {
EXPECT_EQ(i + 1, upb_MiniTableField_Number(f));
EXPECT_TRUE(upb_MiniTableField_IsScalar(f));
// For a oneof all fields have the same offset.
EXPECT_EQ(table->UPB_PRIVATE(fields)[0].offset, f->offset);
EXPECT_EQ(table->UPB_PRIVATE(fields)[0].UPB_PRIVATE(offset),
f->UPB_PRIVATE(offset));
// All presence fields should point to the same oneof case offset.
size_t case_ofs = _upb_MiniTableField_OneofOffset(f);
EXPECT_EQ(table->UPB_PRIVATE(fields)[0].presence, f->presence);
EXPECT_TRUE(f->offset < table->UPB_PRIVATE(size));
EXPECT_TRUE(f->UPB_PRIVATE(offset) < table->UPB_PRIVATE(size));
EXPECT_TRUE(case_ofs < table->UPB_PRIVATE(size));
EXPECT_TRUE(case_ofs != f->offset);
EXPECT_TRUE(case_ofs != f->UPB_PRIVATE(offset));
}
EXPECT_EQ(0, table->UPB_PRIVATE(required_count));
}
Expand Down
3 changes: 2 additions & 1 deletion upb/mini_table/compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ static upb_MiniTableEquals_Status upb_deep_check(const upb_MiniTable* src,
return false;
if (src_field->UPB_PRIVATE(mode) != dst_field->UPB_PRIVATE(mode))
return false;
if (src_field->offset != dst_field->offset) return false;
if (src_field->UPB_PRIVATE(offset) != dst_field->UPB_PRIVATE(offset))
return false;
if (src_field->presence != dst_field->presence) return false;
if (src_field->UPB_PRIVATE(submsg_index) !=
dst_field->UPB_PRIVATE(submsg_index))
Expand Down
7 changes: 6 additions & 1 deletion upb/mini_table/internal/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// LINT.IfChange(struct_definition)
struct upb_MiniTableField {
uint32_t UPB_ONLYBITS(number);
uint16_t offset;
uint16_t UPB_ONLYBITS(offset);
int16_t presence; // If >0, hasbit_index. If <0, ~oneof_index

// Indexes into `upb_MiniTable.subs`
Expand Down Expand Up @@ -174,6 +174,11 @@ UPB_PRIVATE(_upb_MiniTableField_Number)(const struct upb_MiniTableField* f) {
return f->UPB_ONLYBITS(number);
}

UPB_INLINE uint16_t
UPB_PRIVATE(_upb_MiniTableField_Offset)(const struct upb_MiniTableField* f) {
return f->UPB_ONLYBITS(offset);
}

UPB_INLINE size_t
_upb_MiniTableField_OneofOffset(const struct upb_MiniTableField* f) {
UPB_ASSERT(UPB_PRIVATE(_upb_MiniTableField_IsInOneof)(f));
Expand Down
10 changes: 5 additions & 5 deletions upb/wire/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ static const char* _upb_Decoder_DecodeToArray(upb_Decoder* d, const char* ptr,
const upb_MiniTableSub* subs,
const upb_MiniTableField* field,
wireval* val, int op) {
upb_Array** arrp = UPB_PTR_AT(msg, field->offset, void);
upb_Array** arrp = UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), void);
upb_Array* arr = *arrp;
void* mem;

Expand Down Expand Up @@ -608,8 +608,8 @@ upb_Map* _upb_Decoder_CreateMap(upb_Decoder* d, const upb_MiniTable* entry) {
const upb_MiniTableField* val_field = &entry->UPB_PRIVATE(fields)[1];
char key_size = kSizeInMap[key_field->UPB_PRIVATE(descriptortype)];
char val_size = kSizeInMap[val_field->UPB_PRIVATE(descriptortype)];
UPB_ASSERT(key_field->offset == offsetof(upb_MapEntryData, k));
UPB_ASSERT(val_field->offset == offsetof(upb_MapEntryData, v));
UPB_ASSERT(key_field->UPB_PRIVATE(offset) == offsetof(upb_MapEntryData, k));
UPB_ASSERT(val_field->UPB_PRIVATE(offset) == offsetof(upb_MapEntryData, v));
upb_Map* ret = _upb_Map_New(&d->arena, key_size, val_size);
if (!ret) _upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
return ret;
Expand All @@ -620,7 +620,7 @@ static const char* _upb_Decoder_DecodeToMap(upb_Decoder* d, const char* ptr,
const upb_MiniTableSub* subs,
const upb_MiniTableField* field,
wireval* val) {
upb_Map** map_p = UPB_PTR_AT(msg, field->offset, upb_Map*);
upb_Map** map_p = UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), upb_Map*);
upb_Map* map = *map_p;
upb_MapEntry ent;
UPB_ASSERT(upb_MiniTableField_Type(field) == kUpb_FieldType_Message);
Expand Down Expand Up @@ -683,7 +683,7 @@ static const char* _upb_Decoder_DecodeToSubMessage(
upb_Decoder* d, const char* ptr, upb_Message* msg,
const upb_MiniTableSub* subs, const upb_MiniTableField* field, wireval* val,
int op) {
void* mem = UPB_PTR_AT(msg, field->offset, void);
void* mem = UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), void);
int type = field->UPB_PRIVATE(descriptortype);

if (UPB_UNLIKELY(op == kUpb_DecodeOp_Enum) &&
Expand Down
9 changes: 5 additions & 4 deletions upb/wire/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ static void encode_scalar(upb_encstate* e, const void* _field_mem,
static void encode_array(upb_encstate* e, const upb_Message* msg,
const upb_MiniTableSub* subs,
const upb_MiniTableField* f) {
const upb_Array* arr = *UPB_PTR_AT(msg, f->offset, upb_Array*);
const upb_Array* arr = *UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), upb_Array*);
bool packed = upb_MiniTableField_IsPacked(f);
size_t pre_len = e->limit - e->ptr;

Expand Down Expand Up @@ -432,7 +432,7 @@ static void encode_mapentry(upb_encstate* e, uint32_t number,
static void encode_map(upb_encstate* e, const upb_Message* msg,
const upb_MiniTableSub* subs,
const upb_MiniTableField* f) {
const upb_Map* map = *UPB_PTR_AT(msg, f->offset, const upb_Map*);
const upb_Map* map = *UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), const upb_Map*);
const upb_MiniTable* layout =
upb_MiniTableSub_Message(subs[f->UPB_PRIVATE(submsg_index)]);
UPB_ASSERT(layout->UPB_PRIVATE(field_count) == 2);
Expand Down Expand Up @@ -467,7 +467,7 @@ static bool encode_shouldencode(upb_encstate* e, const upb_Message* msg,
const upb_MiniTableField* f) {
if (f->presence == 0) {
// Proto3 presence or map/array.
const void* mem = UPB_PTR_AT(msg, f->offset, void);
const void* mem = UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), void);
switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) {
case kUpb_FieldRep_1Byte: {
char ch;
Expand Down Expand Up @@ -512,7 +512,8 @@ static void encode_field(upb_encstate* e, const upb_Message* msg,
encode_map(e, msg, subs, field);
break;
case kUpb_FieldMode_Scalar:
encode_scalar(e, UPB_PTR_AT(msg, field->offset, void), subs, field);
encode_scalar(e, UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), void), subs,
field);
break;
default:
UPB_UNREACHABLE();
Expand Down
3 changes: 2 additions & 1 deletion upb_generator/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ std::string FieldInitializer(upb::FieldDefPtr field,
const upb_MiniTableField* field32) {
return absl::Substitute(
"{$0, $1, $2, $3, $4, $5}", upb_MiniTableField_Number(field64),
ArchDependentSize(field32->offset, field64->offset),
ArchDependentSize(field32->UPB_PRIVATE(offset),
field64->UPB_PRIVATE(offset)),
ArchDependentSize(field32->presence, field64->presence),
field64->UPB_PRIVATE(submsg_index) == kUpb_NoSub
? "kUpb_NoSub"
Expand Down
3 changes: 2 additions & 1 deletion upb_generator/protoc-gen-upb_minitable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ bool TryFillTableEntry(const DefPoolPair& pools, upb::FieldDefPtr field,
//
// - |presence| is either hasbit index or field number for oneofs.

uint64_t data = static_cast<uint64_t>(mt_f->offset) << 48 | expected_tag;
uint64_t data =
static_cast<uint64_t>(mt_f->UPB_PRIVATE(offset)) << 48 | expected_tag;

if (field.IsSequence()) {
// No hasbit/oneof-related fields.
Expand Down

0 comments on commit 1f67b8d

Please sign in to comment.