[RFC PATCH 32/77] dtc-parser: Introduce last_header_flags

Herve Codina posted 77 patches 3 weeks, 6 days ago
[RFC PATCH 32/77] dtc-parser: Introduce last_header_flags
Posted by Herve Codina 3 weeks, 6 days ago
The parser needs to get header flags value in different places.

It relies on the fact that the rule used to parse the dts file is always
  headers memreserves devicetree

With that only rule to parse the file, it uses '$<flags>-1' construct to
get the flags value.

With the future introduction of import symbols parsing, this rule will
change and the parser couldn't rely anymore on '$<flags>-1' to get flags
value. Indeed, import symbols parsing will add a new optional symbol in
this rule leading to two possible rules (with and without the new
symbol) to parse the source file.

Introduce the last_header_flags variable to explicitly keep track of
flags while also being agnostic of the rule structure and use this new
variable instead of '$<flags>-1'.

Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
 dtc-parser.y | 28 +++++++++-------------------
 1 file changed, 9 insertions(+), 19 deletions(-)

diff --git a/dtc-parser.y b/dtc-parser.y
index 4e46e9d..48c40e8 100644
--- a/dtc-parser.y
+++ b/dtc-parser.y
@@ -24,6 +24,8 @@ extern void yyerror(char const *s);
 extern struct dt_info *parser_output;
 extern bool treesource_error;
 
+unsigned int last_header_flags;
+
 static bool is_ref_relative(const char *ref)
 {
 	return ref[0] != '/' && strchr(&ref[1], '/');
@@ -122,14 +124,17 @@ header:
 	  DT_V1 ';'
 		{
 			$$ = DTSF_V1;
+			last_header_flags = $$;
 		}
 	| DT_V1 ';' DT_PLUGIN ';'
 		{
 			$$ = DTSF_V1 | DTSF_PLUGIN;
+			last_header_flags = $$;
 		}
 	| DT_V1 ';' DT_ADDON ';'
 		{
 			$$ = DTSF_V1 | DTSF_ADDON;
+			last_header_flags = $$;
 		}
 	;
 
@@ -179,12 +184,7 @@ devicetree:
 		}
 	| dt_ref nodedef
 		{
-			/*
-			 * We rely on the rule being always:
-			 *   versioninfo plugindecl memreserves devicetree
-			 * so $-1 is what we want (plugindecl)
-			 */
-			if (!($<flags>-1 & DTSF_PLUGIN))
+			if (!(last_header_flags & DTSF_PLUGIN))
 				ERROR(&@2, "Label or path %s not found", $1);
 			else if (is_ref_relative($1))
 				ERROR(&@2, "Label-relative reference %s not supported in plugin", $1);
@@ -197,7 +197,7 @@ devicetree:
 		{
 			struct node *target = get_node_by_ref($1, $3);
 
-			if (($<flags>-1 & DTSF_PLUGIN) && is_ref_relative($3))
+			if ((last_header_flags & DTSF_PLUGIN) && is_ref_relative($3))
 				ERROR(&@2, "Label-relative reference %s not supported in plugin", $3);
 
 			if (target) {
@@ -209,12 +209,7 @@ devicetree:
 		}
 	| devicetree DT_PATH_REF nodedef
 		{
-			/*
-			 * We rely on the rule being always:
-			 *   versioninfo plugindecl memreserves devicetree
-			 * so $-1 is what we want (plugindecl)
-			 */
-			if ($<flags>-1 & DTSF_PLUGIN) {
+			if (last_header_flags & DTSF_PLUGIN) {
 				if (is_ref_relative($2))
 					ERROR(&@2, "Label-relative reference %s not supported in plugin", $2);
 				add_orphan_node($1, $3, $2);
@@ -235,12 +230,7 @@ devicetree:
 			if (target) {
 				merge_nodes(target, $3);
 			} else {
-				/*
-				 * We rely on the rule being always:
-				 *   versioninfo plugindecl memreserves devicetree
-				 * so $-1 is what we want (plugindecl)
-				 */
-				if ($<flags>-1 & DTSF_PLUGIN)
+				if (last_header_flags & DTSF_PLUGIN)
 					add_orphan_node($1, $3, $2);
 				else
 					ERROR(&@2, "Label or path %s not found", $2);
-- 
2.52.0
Re: [RFC PATCH 32/77] dtc-parser: Introduce last_header_flags
Posted by David Gibson 3 weeks, 3 days ago
On Mon, Jan 12, 2026 at 03:19:22PM +0100, Herve Codina wrote:
> The parser needs to get header flags value in different places.
> 
> It relies on the fact that the rule used to parse the dts file is always
>   headers memreserves devicetree
> 
> With that only rule to parse the file, it uses '$<flags>-1' construct to
> get the flags value.
> 
> With the future introduction of import symbols parsing, this rule will
> change and the parser couldn't rely anymore on '$<flags>-1' to get flags
> value. Indeed, import symbols parsing will add a new optional symbol in
> this rule leading to two possible rules (with and without the new
> symbol) to parse the source file.
> 
> Introduce the last_header_flags variable to explicitly keep track of
> flags while also being agnostic of the rule structure and use this new
> variable instead of '$<flags>-1'.

I'm not sure this approach is safe: I'm not sure bison guarantees that
semantic rules will always be executed in the same order, so using
global variables is risky.

> 
> Signed-off-by: Herve Codina <herve.codina@bootlin.com>
> ---
>  dtc-parser.y | 28 +++++++++-------------------
>  1 file changed, 9 insertions(+), 19 deletions(-)
> 
> diff --git a/dtc-parser.y b/dtc-parser.y
> index 4e46e9d..48c40e8 100644
> --- a/dtc-parser.y
> +++ b/dtc-parser.y
> @@ -24,6 +24,8 @@ extern void yyerror(char const *s);
>  extern struct dt_info *parser_output;
>  extern bool treesource_error;
>  
> +unsigned int last_header_flags;
> +
>  static bool is_ref_relative(const char *ref)
>  {
>  	return ref[0] != '/' && strchr(&ref[1], '/');
> @@ -122,14 +124,17 @@ header:
>  	  DT_V1 ';'
>  		{
>  			$$ = DTSF_V1;
> +			last_header_flags = $$;
>  		}
>  	| DT_V1 ';' DT_PLUGIN ';'
>  		{
>  			$$ = DTSF_V1 | DTSF_PLUGIN;
> +			last_header_flags = $$;
>  		}
>  	| DT_V1 ';' DT_ADDON ';'
>  		{
>  			$$ = DTSF_V1 | DTSF_ADDON;
> +			last_header_flags = $$;
>  		}
>  	;
>  
> @@ -179,12 +184,7 @@ devicetree:
>  		}
>  	| dt_ref nodedef
>  		{
> -			/*
> -			 * We rely on the rule being always:
> -			 *   versioninfo plugindecl memreserves devicetree
> -			 * so $-1 is what we want (plugindecl)
> -			 */
> -			if (!($<flags>-1 & DTSF_PLUGIN))
> +			if (!(last_header_flags & DTSF_PLUGIN))
>  				ERROR(&@2, "Label or path %s not found", $1);
>  			else if (is_ref_relative($1))
>  				ERROR(&@2, "Label-relative reference %s not supported in plugin", $1);
> @@ -197,7 +197,7 @@ devicetree:
>  		{
>  			struct node *target = get_node_by_ref($1, $3);
>  
> -			if (($<flags>-1 & DTSF_PLUGIN) && is_ref_relative($3))
> +			if ((last_header_flags & DTSF_PLUGIN) && is_ref_relative($3))
>  				ERROR(&@2, "Label-relative reference %s not supported in plugin", $3);
>  
>  			if (target) {
> @@ -209,12 +209,7 @@ devicetree:
>  		}
>  	| devicetree DT_PATH_REF nodedef
>  		{
> -			/*
> -			 * We rely on the rule being always:
> -			 *   versioninfo plugindecl memreserves devicetree
> -			 * so $-1 is what we want (plugindecl)
> -			 */
> -			if ($<flags>-1 & DTSF_PLUGIN) {
> +			if (last_header_flags & DTSF_PLUGIN) {
>  				if (is_ref_relative($2))
>  					ERROR(&@2, "Label-relative reference %s not supported in plugin", $2);
>  				add_orphan_node($1, $3, $2);
> @@ -235,12 +230,7 @@ devicetree:
>  			if (target) {
>  				merge_nodes(target, $3);
>  			} else {
> -				/*
> -				 * We rely on the rule being always:
> -				 *   versioninfo plugindecl memreserves devicetree
> -				 * so $-1 is what we want (plugindecl)
> -				 */
> -				if ($<flags>-1 & DTSF_PLUGIN)
> +				if (last_header_flags & DTSF_PLUGIN)
>  					add_orphan_node($1, $3, $2);
>  				else
>  					ERROR(&@2, "Label or path %s not found", $2);
> -- 
> 2.52.0
> 
> 

-- 
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson
Re: [RFC PATCH 32/77] dtc-parser: Introduce last_header_flags
Posted by Herve Codina 2 weeks, 6 days ago
On Thu, 15 Jan 2026 17:31:24 +1100
David Gibson <david@gibson.dropbear.id.au> wrote:

> On Mon, Jan 12, 2026 at 03:19:22PM +0100, Herve Codina wrote:
> > The parser needs to get header flags value in different places.
> > 
> > It relies on the fact that the rule used to parse the dts file is always
> >   headers memreserves devicetree
> > 
> > With that only rule to parse the file, it uses '$<flags>-1' construct to
> > get the flags value.
> > 
> > With the future introduction of import symbols parsing, this rule will
> > change and the parser couldn't rely anymore on '$<flags>-1' to get flags
> > value. Indeed, import symbols parsing will add a new optional symbol in
> > this rule leading to two possible rules (with and without the new
> > symbol) to parse the source file.
> > 
> > Introduce the last_header_flags variable to explicitly keep track of
> > flags while also being agnostic of the rule structure and use this new
> > variable instead of '$<flags>-1'.  
> 
> I'm not sure this approach is safe: I'm not sure bison guarantees that
> semantic rules will always be executed in the same order, so using
> global variables is risky.

if rules were not executed in the same order '$<flags>-1' construct would
not work.

The problem is not the order. I don't think the order will change. The
problem is related to the number of items on stack.

With import symbols, that the stack will be:
  header memreserved devicetree
or
  header memreserved importsyms devicetree

Using '$-1' will no more be possible. Indeed, '$-1' from the devicetree
rule will reference 'header' in one case and 'memreserved' in the other
case.

Without a global variable, I don't know how to reference 'header' (or flags
value) in all cases.

Any better ideas are welcome.

Best regards,
Hervé
Re: [RFC PATCH 32/77] dtc-parser: Introduce last_header_flags
Posted by David Gibson 2 weeks, 4 days ago
On Mon, Jan 19, 2026 at 03:11:45PM +0100, Herve Codina wrote:
> On Thu, 15 Jan 2026 17:31:24 +1100
> David Gibson <david@gibson.dropbear.id.au> wrote:
> 
> > On Mon, Jan 12, 2026 at 03:19:22PM +0100, Herve Codina wrote:
> > > The parser needs to get header flags value in different places.
> > > 
> > > It relies on the fact that the rule used to parse the dts file is always
> > >   headers memreserves devicetree
> > > 
> > > With that only rule to parse the file, it uses '$<flags>-1' construct to
> > > get the flags value.
> > > 
> > > With the future introduction of import symbols parsing, this rule will
> > > change and the parser couldn't rely anymore on '$<flags>-1' to get flags
> > > value. Indeed, import symbols parsing will add a new optional symbol in
> > > this rule leading to two possible rules (with and without the new
> > > symbol) to parse the source file.
> > > 
> > > Introduce the last_header_flags variable to explicitly keep track of
> > > flags while also being agnostic of the rule structure and use this new
> > > variable instead of '$<flags>-1'.  
> > 
> > I'm not sure this approach is safe: I'm not sure bison guarantees that
> > semantic rules will always be executed in the same order, so using
> > global variables is risky.
> 
> if rules were not executed in the same order '$<flags>-1' construct would
> not work.

Uhh.. I'm pretty sure $<flags>-1 only depends on the parse tree
structure, not the order in which the actual semantic rule code
fragments run.

> The problem is not the order. I don't think the order will change. The
> problem is related to the number of items on stack.
> 
> With import symbols, that the stack will be:
>   header memreserved devicetree
> or
>   header memreserved importsyms devicetree
> 
> Using '$-1' will no more be possible. Indeed, '$-1' from the devicetree
> rule will reference 'header' in one case and 'memreserved' in the other
> case.
> 
> Without a global variable, I don't know how to reference 'header' (or flags
> value) in all cases.
> 
> Any better ideas are welcome.

Ok, I'll try to look into it and see what I can come up with.

-- 
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson