Skip to content

Commit

Permalink
xdrgen: Add generator code for XDR width macros
Browse files Browse the repository at this point in the history
Introduce logic in the code generators to emit maxsize (XDR
width) definitions. In C, these are pre-processor macros.

Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
  • Loading branch information
chucklever committed Nov 11, 2024
1 parent ce5a75d commit e9e1e7e
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 6 deletions.
4 changes: 4 additions & 0 deletions tools/net/sunrpc/xdrgen/generators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,7 @@ def emit_definition(self, node: _XdrAst) -> None:
def emit_encoder(self, node: _XdrAst) -> None:
"""Emit one encoder function for this XDR type"""
raise NotImplementedError("Encoder generation not supported")

def emit_maxsize(self, node: _XdrAst) -> None:
"""Emit one maxsize macro for this XDR type"""
raise NotImplementedError("Maxsize macro generation not supported")
13 changes: 12 additions & 1 deletion tools/net/sunrpc/xdrgen/generators/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""Generate code to handle XDR enum types"""

from generators import SourceGenerator, create_jinja2_environment
from xdr_ast import _XdrEnum, public_apis, big_endian
from xdr_ast import _XdrEnum, public_apis, big_endian, get_header_name


class XdrEnumGenerator(SourceGenerator):
Expand Down Expand Up @@ -51,3 +51,14 @@ def emit_encoder(self, node: _XdrEnum) -> None:
else:
template = self.environment.get_template("encoder/enum.j2")
print(template.render(name=node.name))

def emit_maxsize(self, node: _XdrEnum) -> None:
"""Emit one maxsize macro for an XDR enum type"""
macro_name = get_header_name().upper() + "_" + node.name + "_sz"
template = self.environment.get_template("maxsize/enum.j2")
print(
template.render(
macro=macro_name,
width=" + ".join(node.symbolic_width()),
)
)
18 changes: 17 additions & 1 deletion tools/net/sunrpc/xdrgen/generators/pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from xdr_ast import _XdrFixedLengthOpaque, _XdrVariableLengthOpaque
from xdr_ast import _XdrFixedLengthArray, _XdrVariableLengthArray
from xdr_ast import _XdrOptionalData, _XdrPointer, _XdrDeclaration
from xdr_ast import public_apis
from xdr_ast import public_apis, get_header_name


def emit_pointer_declaration(environment: Environment, node: _XdrPointer) -> None:
Expand Down Expand Up @@ -247,6 +247,18 @@ def emit_pointer_encoder(environment: Environment, node: _XdrPointer) -> None:
print(template.render())


def emit_pointer_maxsize(environment: Environment, node: _XdrPointer) -> None:
"""Emit one maxsize macro for an XDR pointer type"""
macro_name = get_header_name().upper() + "_" + node.name + "_sz"
template = get_jinja2_template(environment, "maxsize", "pointer")
print(
template.render(
macro=macro_name,
width=" + ".join(node.symbolic_width()),
)
)


class XdrPointerGenerator(SourceGenerator):
"""Generate source code for XDR pointer"""

Expand All @@ -270,3 +282,7 @@ def emit_decoder(self, node: _XdrPointer) -> None:
def emit_encoder(self, node: _XdrPointer) -> None:
"""Emit one encoder function for an XDR pointer type"""
emit_pointer_encoder(self.environment, node)

def emit_maxsize(self, node: _XdrPointer) -> None:
"""Emit one maxsize macro for an XDR pointer type"""
emit_pointer_maxsize(self.environment, node)
18 changes: 17 additions & 1 deletion tools/net/sunrpc/xdrgen/generators/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from xdr_ast import _XdrFixedLengthOpaque, _XdrVariableLengthOpaque
from xdr_ast import _XdrFixedLengthArray, _XdrVariableLengthArray
from xdr_ast import _XdrOptionalData, _XdrStruct, _XdrDeclaration
from xdr_ast import public_apis
from xdr_ast import public_apis, get_header_name


def emit_struct_declaration(environment: Environment, node: _XdrStruct) -> None:
Expand Down Expand Up @@ -247,6 +247,18 @@ def emit_struct_encoder(environment: Environment, node: _XdrStruct) -> None:
print(template.render())


def emit_struct_maxsize(environment: Environment, node: _XdrStruct) -> None:
"""Emit one maxsize macro for an XDR struct type"""
macro_name = get_header_name().upper() + "_" + node.name + "_sz"
template = get_jinja2_template(environment, "maxsize", "struct")
print(
template.render(
macro=macro_name,
width=" + ".join(node.symbolic_width()),
)
)


class XdrStructGenerator(SourceGenerator):
"""Generate source code for XDR structs"""

Expand All @@ -270,3 +282,7 @@ def emit_decoder(self, node: _XdrStruct) -> None:
def emit_encoder(self, node: _XdrStruct) -> None:
"""Emit one encoder function for an XDR struct type"""
emit_struct_encoder(self.environment, node)

def emit_maxsize(self, node: _XdrStruct) -> None:
"""Emit one maxsize macro for an XDR struct type"""
emit_struct_maxsize(self.environment, node)
18 changes: 17 additions & 1 deletion tools/net/sunrpc/xdrgen/generators/typedef.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from xdr_ast import _XdrFixedLengthOpaque, _XdrVariableLengthOpaque
from xdr_ast import _XdrFixedLengthArray, _XdrVariableLengthArray
from xdr_ast import _XdrOptionalData, _XdrVoid, _XdrDeclaration
from xdr_ast import public_apis
from xdr_ast import public_apis, get_header_name


def emit_typedef_declaration(environment: Environment, node: _XdrDeclaration) -> None:
Expand Down Expand Up @@ -230,6 +230,18 @@ def emit_typedef_encoder(environment: Environment, node: _XdrDeclaration) -> Non
raise NotImplementedError("typedef: type not recognized")


def emit_typedef_maxsize(environment: Environment, node: _XdrDeclaration) -> None:
"""Emit a maxsize macro for an XDR typedef"""
macro_name = get_header_name().upper() + "_" + node.name + "_sz"
template = get_jinja2_template(environment, "maxsize", node.template)
print(
template.render(
macro=macro_name,
width=" + ".join(node.symbolic_width()),
)
)


class XdrTypedefGenerator(SourceGenerator):
"""Generate source code for XDR typedefs"""

Expand All @@ -253,3 +265,7 @@ def emit_decoder(self, node: _XdrTypedef) -> None:
def emit_encoder(self, node: _XdrTypedef) -> None:
"""Emit one encoder function for an XDR typedef"""
emit_typedef_encoder(self.environment, node.declaration)

def emit_maxsize(self, node: _XdrTypedef) -> None:
"""Emit one maxsize macro for an XDR typedef"""
emit_typedef_maxsize(self.environment, node.declaration)
20 changes: 18 additions & 2 deletions tools/net/sunrpc/xdrgen/generators/union.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from generators import SourceGenerator
from generators import create_jinja2_environment, get_jinja2_template

from xdr_ast import _XdrBasic, _XdrUnion, _XdrVoid, big_endian
from xdr_ast import _XdrDeclaration, _XdrCaseSpec, public_apis
from xdr_ast import _XdrBasic, _XdrUnion, _XdrVoid, get_header_name
from xdr_ast import _XdrDeclaration, _XdrCaseSpec, public_apis, big_endian


def emit_union_declaration(environment: Environment, node: _XdrUnion) -> None:
Expand Down Expand Up @@ -234,6 +234,18 @@ def emit_union_encoder(environment, node: _XdrUnion) -> None:
print(template.render())


def emit_union_maxsize(environment: Environment, node: _XdrUnion) -> None:
"""Emit one maxsize macro for an XDR union type"""
macro_name = get_header_name().upper() + "_" + node.name + "_sz"
template = get_jinja2_template(environment, "maxsize", "union")
print(
template.render(
macro=macro_name,
width=" + ".join(node.symbolic_width()),
)
)


class XdrUnionGenerator(SourceGenerator):
"""Generate source code for XDR unions"""

Expand All @@ -257,3 +269,7 @@ def emit_decoder(self, node: _XdrUnion) -> None:
def emit_encoder(self, node: _XdrUnion) -> None:
"""Emit one encoder function for an XDR union"""
emit_union_encoder(self.environment, node)

def emit_maxsize(self, node: _XdrUnion) -> None:
"""Emit one maxsize macro for an XDR union"""
emit_union_maxsize(self.environment, node)
2 changes: 2 additions & 0 deletions tools/net/sunrpc/xdrgen/templates/C/enum/maxsize/enum.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} ({{ width }})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} \
({{ width }})
3 changes: 3 additions & 0 deletions tools/net/sunrpc/xdrgen/templates/C/struct/maxsize/struct.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} \
({{ width }})
3 changes: 3 additions & 0 deletions tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/basic.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} \
({{ width }})
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} ({{ width }})
2 changes: 2 additions & 0 deletions tools/net/sunrpc/xdrgen/templates/C/typedef/maxsize/string.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} ({{ width }})
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} ({{ width }})
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} ({{ width }})
3 changes: 3 additions & 0 deletions tools/net/sunrpc/xdrgen/templates/C/union/maxsize/union.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} \
({{ width }})

0 comments on commit e9e1e7e

Please sign in to comment.