Skip to content

Commit

Permalink
[mycpp/runtime] Fix mycpp header init after FIELD_MASK() move
Browse files Browse the repository at this point in the history
Use GC_CLASS_FIXED() and GC_CLASS_SCANNED(), instead of just GC_CLASS().

We also don't need Tag::Opaque because Tag::Scanned with
NUM_POINTERS(header_) == 0 should be just as efficient.
  • Loading branch information
Andy C committed Jan 2, 2023
1 parent e3342ed commit 78f7313
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
21 changes: 9 additions & 12 deletions mycpp/cppgen_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2170,10 +2170,7 @@ def visit_class_def(self, o: 'mypy.nodes.ClassDef') -> T:
# So we declare them in the right order
sorted_member_names = pointer_members + non_pointer_members

if len(pointer_members) == 0:
self.field_gc[o] = ('HeapTag::Opaque', None)
else:
self.field_gc[o] = ('HeapTag::Scanned', len(pointer_members))
self.field_gc[o] = ('HeapTag::Scanned', len(pointer_members))
else:
# Has inheritance

Expand Down Expand Up @@ -2250,17 +2247,17 @@ def visit_class_def(self, o: 'mypy.nodes.ClassDef') -> T:
obj_tag, obj_arg = self.field_gc[o]
if obj_tag == 'HeapTag::FixedSize':
obj_mask = obj_arg
obj_len = 'kNoObjLen' # don't need length
header_init = 'GC_CLASS_FIXED(header_, %s, sizeof(%s))' % (
obj_mask, o.name)
elif obj_tag == 'HeapTag::Scanned':
obj_mask = 'kZeroMask'
#obj_len = '%d * sizeof(void*) + sizeof(ObjHeader)' % obj_arg
obj_len = str(obj_arg) # number of pointers
elif obj_tag == 'HeapTag::Opaque':
obj_mask = 'kZeroMask'
obj_len = 'kNoObjLen'
num_pointers = obj_arg
header_init = 'GC_CLASS_SCANNED(header_, %s, sizeof(%s))' % (
num_pointers, o.name)
else:
raise AssertionError(o.name)

self.write('\n')
self.write(' : GC_CLASS(header_, %s, %s, %s)' % (obj_tag, obj_mask, obj_len))
self.write(' : %s' % header_init)

# Check for Base.__init__(self, ...) and move that to the initializer list.

Expand Down
11 changes: 8 additions & 3 deletions mycpp/gc_obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,19 @@ struct RawObject {
kIsHeader, TypeTag::OtherClass, kNoObjId, HeapTag::FixedSize, field_mask \
}

// Used by mycpp and frontend/flag_gen.py. TODO: Sort fields and use
// HeapTag::Scanned.
// Classes with no inheritance (e.g. used by mycpp)
#define GC_CLASS_SCANNED(header_, num_pointers, obj_len) \
header_ { \
kIsHeader, TypeTag::OtherClass, kNoObjId, HeapTag::Scanned, num_pointers \
}

// Used by frontend/flag_gen.py. TODO: Sort fields and use GC_CLASS_SCANNED
#define GC_CLASS(header_, heap_tag, field_mask, obj_len) \
header_ { \
kIsHeader, TypeTag::OtherClass, kNoObjId, heap_tag, field_mask \
}

// Used by ASDL. TODO: Sort fields and use HeapTag::Scanned
// Used by ASDL. TODO: Sort fields and use GC_CLASS_SCANNED
#define GC_ASDL_CLASS(header_, type_tag, field_mask, obj_len) \
header_ { \
kIsHeader, type_tag, kNoObjId, HeapTag::FixedSize, field_mask \
Expand Down

0 comments on commit 78f7313

Please sign in to comment.