Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact(rivetc.codegen): don't alloc tagged enum's struct values #55

Merged
merged 8 commits into from
Dec 16, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix
  • Loading branch information
StunxFS committed Dec 16, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 10d6b0b8c235adcbe017762167c98f1df99c3072
19 changes: 11 additions & 8 deletions rivetc/src/codegen/__init__.py
Original file line number Diff line number Diff line change
@@ -3021,7 +3021,7 @@ def gen_types(self):
ir.Field("_rc_", ir.UINT_T),
ir.Field("_idx_", ir.UINT_T),
ir.Field("obj", ir.Type(union_name))
], is_tagged_enum = True
]
)
)
elif ts.kind == TypeKind.Trait:
@@ -3154,10 +3154,11 @@ def sort_type_symbols(self, tss):
field_deps.append(dep)
elif ts.kind == TypeKind.Tuple:
for f in ts.info.types:
dep = cg_utils.mangle_symbol(f.symbol())
fsym = f.symbol()
dep = cg_utils.mangle_symbol(fsym)
if dep not in typ_names or dep in field_deps or isinstance(
f, type.Option
):
) or fsym.is_boxed():
continue
field_deps.append(dep)
elif ts.kind == TypeKind.Enum and ts.info.is_tagged:
@@ -3177,23 +3178,25 @@ def sort_type_symbols(self, tss):
continue
field_deps.append(dep)
for f in ts.fields:
dep = cg_utils.mangle_symbol(f.typ.symbol())
fsym = f.typ.symbol()
dep = cg_utils.mangle_symbol(fsym)
if dep not in typ_names or dep in field_deps or isinstance(
f.typ, type.Option
):
) or fsym.is_boxed():
continue
field_deps.append(dep)
elif ts.kind == TypeKind.Struct:
for base in ts.info.bases:
dep = cg_utils.mangle_symbol(base)
if dep not in typ_names or dep in field_deps:
if dep not in typ_names or dep in field_deps or base.is_boxed():
continue
field_deps.append(dep)
for f in ts.fields:
dep = cg_utils.mangle_symbol(f.typ.symbol())
fsym = f.typ.symbol()
dep = cg_utils.mangle_symbol(fsym)
if dep not in typ_names or dep in field_deps or isinstance(
f.typ, type.Option
):
) or fsym.is_boxed():
continue
field_deps.append(dep)
dg.add(ts.mangled_name, field_deps)
17 changes: 7 additions & 10 deletions rivetc/src/codegen/c.py
Original file line number Diff line number Diff line change
@@ -30,7 +30,6 @@ def __init__(self, comp):
self.comp = comp
self.typedefs = utils.Builder()
self.types = utils.Builder()
self.types2 = utils.Builder()
self.protos = utils.Builder()
self.globals = utils.Builder()
self.out = utils.Builder()
@@ -53,7 +52,6 @@ def gen(self, out_rir):
out.write(c_headers.RIVET_BREAKPOINT)
out.write(str(self.typedefs).strip() + "\n\n")
out.write(str(self.types).strip() + "\n\n")
out.write(str(self.types2).strip() + "\n\n")
out.write(str(self.protos).strip() + "\n\n")
out.write(str(self.globals).strip() + "\n\n")
out.write(str(self.out).strip())
@@ -102,18 +100,17 @@ def gen_types(self, types):
for s in types:
if isinstance(s, ir.Struct):
self.typedefs.writeln(f"typedef struct {s.name} {s.name};")
buf = self.types2 if s.is_tagged_enum else self.types
if not s.is_opaque:
buf.writeln(f"struct {s.name} {{")
self.types.writeln(f"struct {s.name} {{")
for i, f in enumerate(s.fields):
f_name = c_escape(f.name)
buf.write(" ")
buf.write(self.gen_type(f.typ, f_name))
self.types.write(" ")
self.types.write(self.gen_type(f.typ, f_name))
if not isinstance(f.typ, (ir.Array, ir.Function)):
buf.write(f" {f_name}")
buf.writeln(";")
buf.writeln("};")
buf.writeln()
self.types.write(f" {f_name}")
self.types.writeln(";")
self.types.writeln("};")
self.types.writeln()
else:
self.typedefs.writeln(f"typedef union {s.name} {s.name};")
self.types.writeln(f"union {s.name} {{")
3 changes: 1 addition & 2 deletions rivetc/src/codegen/ir.py
Original file line number Diff line number Diff line change
@@ -207,11 +207,10 @@ def __str__(self):
return str(sb)

class Struct:
def __init__(self, is_opaque, name, fields, is_tagged_enum=False):
def __init__(self, is_opaque, name, fields):
self.is_opaque = is_opaque
self.name = name
self.fields = fields
self.is_tagged_enum = is_tagged_enum

def __str__(self):
sb = utils.Builder()