From 57baf7ac7ba6839fe5c6ad54a92c03b15e977df6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 21 Dec 2025 17:20:01 -1000 Subject: [PATCH 1/2] [codegen] Add static storage class to global variables for size optimization --- esphome/components/lvgl/lvcode.py | 4 ++-- esphome/components/mapping/__init__.py | 2 +- esphome/cpp_generator.py | 15 ++++++++++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/esphome/components/lvgl/lvcode.py b/esphome/components/lvgl/lvcode.py index c11597131f1..44b9e598907 100644 --- a/esphome/components/lvgl/lvcode.py +++ b/esphome/components/lvgl/lvcode.py @@ -337,7 +337,7 @@ def lv_Pvariable(type, name) -> MockObj: """ if isinstance(name, str): name = ID(name, True, type) - decl = VariableDeclarationExpression(type, "*", name) + decl = VariableDeclarationExpression(type, "*", name, storage_class="static") CORE.add_global(decl) var = MockObj(name, "->") CORE.register_variable(name, var) @@ -353,7 +353,7 @@ def lv_variable(type, name) -> MockObj: """ if isinstance(name, str): name = ID(name, True, type) - decl = VariableDeclarationExpression(type, "", name) + decl = VariableDeclarationExpression(type, "", name, storage_class="static") CORE.add_global(decl) var = MockObj(name, ".") CORE.register_variable(name, var) diff --git a/esphome/components/mapping/__init__.py b/esphome/components/mapping/__init__.py index 94c7c10a82a..7f39b611ea3 100644 --- a/esphome/components/mapping/__init__.py +++ b/esphome/components/mapping/__init__.py @@ -133,7 +133,7 @@ async def to_code(config): value_type, ) var = MockObj(varid, ".") - decl = VariableDeclarationExpression(varid.type, "", varid) + decl = VariableDeclarationExpression(varid.type, "", varid, storage_class="static") add_global(decl) CORE.register_variable(varid, var) diff --git a/esphome/cpp_generator.py b/esphome/cpp_generator.py index 1a47b346b77..6f1923183ce 100644 --- a/esphome/cpp_generator.py +++ b/esphome/cpp_generator.py @@ -51,14 +51,19 @@ class AssignmentExpression(Expression): class VariableDeclarationExpression(Expression): - __slots__ = ("type", "modifier", "name") + __slots__ = ("type", "modifier", "name", "storage_class") - def __init__(self, type_, modifier, name): + def __init__( + self, type_: "MockObj", modifier: str, name: ID, storage_class: str = "" + ) -> None: self.type = type_ self.modifier = modifier self.name = name + self.storage_class = storage_class - def __str__(self): + def __str__(self) -> str: + if self.storage_class: + return f"{self.storage_class} {self.type} {self.modifier}{self.name}" return f"{self.type} {self.modifier}{self.name}" @@ -522,7 +527,7 @@ def new_variable(id_: ID, rhs: SafeExpType, type_: "MockObj" = None) -> "MockObj obj = MockObj(id_, ".") if type_ is not None: id_.type = type_ - decl = VariableDeclarationExpression(id_.type, "", id_) + decl = VariableDeclarationExpression(id_.type, "", id_, storage_class="static") CORE.add_global(decl) assignment = AssignmentExpression(None, "", id_, rhs) CORE.add(assignment) @@ -544,7 +549,7 @@ def Pvariable(id_: ID, rhs: SafeExpType, type_: "MockObj" = None) -> "MockObj": obj = MockObj(id_, "->") if type_ is not None: id_.type = type_ - decl = VariableDeclarationExpression(id_.type, "*", id_) + decl = VariableDeclarationExpression(id_.type, "*", id_, storage_class="static") CORE.add_global(decl) assignment = AssignmentExpression(None, None, id_, rhs) CORE.add(assignment) From ff808618da59b5c6776cfaac56c6b3e231d64c71 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 21 Dec 2025 17:27:49 -1000 Subject: [PATCH 2/2] better to be a kw --- esphome/components/lvgl/lvcode.py | 4 ++-- esphome/components/mapping/__init__.py | 2 +- esphome/cpp_generator.py | 15 +++++++-------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/esphome/components/lvgl/lvcode.py b/esphome/components/lvgl/lvcode.py index 44b9e598907..e2c70642a80 100644 --- a/esphome/components/lvgl/lvcode.py +++ b/esphome/components/lvgl/lvcode.py @@ -337,7 +337,7 @@ def lv_Pvariable(type, name) -> MockObj: """ if isinstance(name, str): name = ID(name, True, type) - decl = VariableDeclarationExpression(type, "*", name, storage_class="static") + decl = VariableDeclarationExpression(type, "*", name, static=True) CORE.add_global(decl) var = MockObj(name, "->") CORE.register_variable(name, var) @@ -353,7 +353,7 @@ def lv_variable(type, name) -> MockObj: """ if isinstance(name, str): name = ID(name, True, type) - decl = VariableDeclarationExpression(type, "", name, storage_class="static") + decl = VariableDeclarationExpression(type, "", name, static=True) CORE.add_global(decl) var = MockObj(name, ".") CORE.register_variable(name, var) diff --git a/esphome/components/mapping/__init__.py b/esphome/components/mapping/__init__.py index 7f39b611ea3..a36b414fd51 100644 --- a/esphome/components/mapping/__init__.py +++ b/esphome/components/mapping/__init__.py @@ -133,7 +133,7 @@ async def to_code(config): value_type, ) var = MockObj(varid, ".") - decl = VariableDeclarationExpression(varid.type, "", varid, storage_class="static") + decl = VariableDeclarationExpression(varid.type, "", varid, static=True) add_global(decl) CORE.register_variable(varid, var) diff --git a/esphome/cpp_generator.py b/esphome/cpp_generator.py index 6f1923183ce..0478e45f7b1 100644 --- a/esphome/cpp_generator.py +++ b/esphome/cpp_generator.py @@ -51,20 +51,19 @@ class AssignmentExpression(Expression): class VariableDeclarationExpression(Expression): - __slots__ = ("type", "modifier", "name", "storage_class") + __slots__ = ("type", "modifier", "name", "static") def __init__( - self, type_: "MockObj", modifier: str, name: ID, storage_class: str = "" + self, type_: "MockObj", modifier: str, name: ID, *, static: bool = False ) -> None: self.type = type_ self.modifier = modifier self.name = name - self.storage_class = storage_class + self.static = static def __str__(self) -> str: - if self.storage_class: - return f"{self.storage_class} {self.type} {self.modifier}{self.name}" - return f"{self.type} {self.modifier}{self.name}" + prefix = "static " if self.static else "" + return f"{prefix}{self.type} {self.modifier}{self.name}" class ExpressionList(Expression): @@ -527,7 +526,7 @@ def new_variable(id_: ID, rhs: SafeExpType, type_: "MockObj" = None) -> "MockObj obj = MockObj(id_, ".") if type_ is not None: id_.type = type_ - decl = VariableDeclarationExpression(id_.type, "", id_, storage_class="static") + decl = VariableDeclarationExpression(id_.type, "", id_, static=True) CORE.add_global(decl) assignment = AssignmentExpression(None, "", id_, rhs) CORE.add(assignment) @@ -549,7 +548,7 @@ def Pvariable(id_: ID, rhs: SafeExpType, type_: "MockObj" = None) -> "MockObj": obj = MockObj(id_, "->") if type_ is not None: id_.type = type_ - decl = VariableDeclarationExpression(id_.type, "*", id_, storage_class="static") + decl = VariableDeclarationExpression(id_.type, "*", id_, static=True) CORE.add_global(decl) assignment = AssignmentExpression(None, None, id_, rhs) CORE.add(assignment)