mirror of
https://github.com/esphome/esphome.git
synced 2026-06-24 12:35:25 +00:00
Generated setup() is a single monolithic function whose stack frame scales super-linearly with config size. On a 5,943-line apollo build the frame reached 1,264 B at -Os; extrapolation onto larger configs (e.g. the 16k-line LVGL config in #15796) plausibly overflows the 8 KB loop task stack before safe_mode can increment its boot counter. Emit a ComponentMarker sentinel at the start of each component's to_code output, then have cpp_main_section wrap each component's block (and sub-splits of up to 50 statements within each block) in a noinline IIFE lambda. Each lambda's ENTRY frame is released on return, bounding peak stack to setup() frame + max chunk frame. Measured on apollo-r-pro-1-eth (esp32-s3, -Os): setup() frame 1264 B -> 160 B max chunk frame n/a -> 144 B peak setup stack 1264 B -> 304 B (-76%) total flash 792,471 B -> 791,995 B (-476 B) The brace-depth guard in _wrap_in_noinline_iifes ensures we never split between the RawStatement("{") / RawStatement("}") pair emitted by cg.with_local_variable() (currently only wifi), so scoped locals stay intact.
103 lines
2.0 KiB
Python
103 lines
2.0 KiB
Python
# Base file for all codegen-related imports
|
|
# All integrations should have a line in the import section like this
|
|
#
|
|
# >>> import esphome.codegen as cg
|
|
#
|
|
# Integrations should specifically *NOT* import directly from the
|
|
# other helper modules (cpp_generator etc) directly if they don't
|
|
# want to break suddenly due to a rename (this file will get backports for features).
|
|
|
|
# pylint: disable=unused-import
|
|
from esphome.cpp_generator import ( # noqa: F401
|
|
ArrayInitializer,
|
|
ComponentMarker,
|
|
Expression,
|
|
FlashStringLiteral,
|
|
LineComment,
|
|
LogStringLiteral,
|
|
MockObj,
|
|
MockObjClass,
|
|
Pvariable,
|
|
RawExpression,
|
|
RawStatement,
|
|
Statement,
|
|
StructInitializer,
|
|
TemplateArguments,
|
|
add,
|
|
add_build_flag,
|
|
add_build_unflag,
|
|
add_define,
|
|
add_global,
|
|
add_library,
|
|
add_platformio_option,
|
|
get_variable,
|
|
get_variable_with_full_id,
|
|
is_template,
|
|
new_Pvariable,
|
|
new_variable,
|
|
process_lambda,
|
|
progmem_array,
|
|
safe_exp,
|
|
set_cpp_standard,
|
|
statement,
|
|
static_const_array,
|
|
templatable,
|
|
variable,
|
|
with_local_variable,
|
|
)
|
|
from esphome.cpp_helpers import ( # noqa: F401
|
|
build_registry_entry,
|
|
build_registry_list,
|
|
extract_registry_entry_config,
|
|
gpio_pin_expression,
|
|
past_safe_mode,
|
|
register_component,
|
|
register_parented,
|
|
)
|
|
from esphome.cpp_types import ( # noqa: F401
|
|
NAN,
|
|
App,
|
|
Application,
|
|
Component,
|
|
ComponentPtr,
|
|
Controller,
|
|
EntityBase,
|
|
EntityCategory,
|
|
ESPTime,
|
|
FixedVector,
|
|
GPIOPin,
|
|
InternalGPIOPin,
|
|
JsonObject,
|
|
JsonObjectConst,
|
|
Parented,
|
|
PollingComponent,
|
|
StringRef,
|
|
arduino_json_ns,
|
|
bool_,
|
|
const_char_ptr,
|
|
double,
|
|
esphome_ns,
|
|
float_,
|
|
global_ns,
|
|
gpio_Flags,
|
|
int8,
|
|
int16,
|
|
int32,
|
|
int64,
|
|
int_,
|
|
nullptr,
|
|
optional,
|
|
size_t,
|
|
std_ns,
|
|
std_shared_ptr,
|
|
std_span,
|
|
std_string,
|
|
std_string_ref,
|
|
std_vector,
|
|
uint8,
|
|
uint16,
|
|
uint32,
|
|
uint64,
|
|
void,
|
|
)
|