devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: "Uwe Kleine-König" <u.kleine-koenig@baylibre.com>
Cc: devicetree-compiler@vger.kernel.org
Subject: Re: [PATCH 3/6] Improve type guessing when compiling to dts format
Date: Thu, 21 Aug 2025 14:24:50 +1000	[thread overview]
Message-ID: <aKafkjp1r5fUKYEF@zatzit> (raw)
In-Reply-To: <e3584b641b6c81199716d5ff745de821fe4446a3.1755692822.git.u.kleine-koenig@baylibre.com>

[-- Attachment #1: Type: text/plain, Size: 4849 bytes --]

On Wed, Aug 20, 2025 at 03:11:29PM +0200, Uwe Kleine-König wrote:
> In the presence of (non-type) markers guess the type of each chunk
> between markers individually instead of only once for the whole
> property.
> 
> Note that this only gets relevant with the next few commits that restore
> labels and phandles. Note further that this rework is necessary with
> these further changes, because phandle markers are currently not
> considered for type guessing and so a phandle at an offset that isn't a
> multiple of 4 triggers an assertion if the property was guessed to have
> type TYPE_UINT32.
> 
> Now that guess_value_type() is only called for data chunks without
> markers, the function can be simplified a bit.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>

> ---
>  treesource.c | 70 ++++++++++++++++++++++++++++++----------------------
>  1 file changed, 40 insertions(+), 30 deletions(-)
> 
> diff --git a/treesource.c b/treesource.c
> index 72d1cb5508b5..576495902f0d 100644
> --- a/treesource.c
> +++ b/treesource.c
> @@ -173,23 +173,20 @@ static struct marker **add_marker(struct marker **mi,
>  	return &nm->next;
>  }
>  
> -static void add_string_markers(struct property *prop)
> +static void add_string_markers(struct property *prop, unsigned int offset, int len)
>  {
> -	int l, len = prop->val.len;
> -	const char *p = prop->val.val;
> +	int l;
> +	const char *p = prop->val.val + offset;
>  	struct marker **mi = &prop->val.markers;
>  
>  	for (l = strlen(p) + 1; l < len; l += strlen(p + l) + 1)
> -		mi = add_marker(mi, TYPE_STRING, l, NULL);
> +		mi = add_marker(mi, TYPE_STRING, offset + l, NULL);
>  }
>  
> -static enum markertype guess_value_type(struct property *prop)
> +static enum markertype guess_value_type(struct property *prop, unsigned int offset, int len)
>  {
> -	int len = prop->val.len;
> -	const char *p = prop->val.val;
> -	struct marker *m = prop->val.markers;
> +	const char *p = prop->val.val + offset;
>  	int nnotstring = 0, nnul = 0;
> -	int nnotstringlbl = 0, nnotcelllbl = 0;
>  	int i;
>  
>  	for (i = 0; i < len; i++) {
> @@ -199,30 +196,49 @@ static enum markertype guess_value_type(struct property *prop)
>  			nnul++;
>  	}
>  
> -	for_each_marker_of_type(m, LABEL) {
> -		if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
> -			nnotstringlbl++;
> -		if ((m->offset % sizeof(cell_t)) != 0)
> -			nnotcelllbl++;
> -	}
> -
> -	if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul <= (len-nnul))
> -	    && (nnotstringlbl == 0)) {
> +	if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul <= len - nnul)) {
>  		if (nnul > 1)
> -			add_string_markers(prop);
> +			add_string_markers(prop, offset, len);
>  		return TYPE_STRING;
> -	} else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
> +	} else if ((len % sizeof(cell_t)) == 0) {
>  		return TYPE_UINT32;
>  	}
>  
>  	return TYPE_UINT8;
>  }
>  
> +static void guess_type_markers(struct property *prop)
> +{
> +	struct marker **m = &prop->val.markers;
> +	unsigned int offset = 0;
> +
> +	for (m = &prop->val.markers; *m; m = &((*m)->next)) {
> +		if (is_type_marker((*m)->type))
> +			/* assume the whole property is already marked */
> +			return;
> +
> +		if ((*m)->offset > offset) {
> +			m = add_marker(m, guess_value_type(prop, offset, (*m)->offset - offset),
> +				       offset, NULL);
> +
> +			offset = (*m)->offset;
> +		}
> +
> +		if ((*m)->type == REF_PHANDLE) {
> +			m = add_marker(m, TYPE_UINT32, offset, NULL);
> +			offset += 4;
> +		}
> +	}
> +
> +	if (offset < prop->val.len)
> +		add_marker(m, guess_value_type(prop, offset, prop->val.len - offset),
> +			   offset, NULL);
> +}
> +
>  static void write_propval(FILE *f, struct property *prop)
>  {
>  	size_t len = prop->val.len;
> -	struct marker *m = prop->val.markers;
> -	struct marker dummy_marker;
> +	struct marker *m;
>  	enum markertype emit_type = TYPE_NONE;
>  	char *srcstr;
>  
> @@ -241,14 +257,8 @@ static void write_propval(FILE *f, struct property *prop)
>  
>  	fprintf(f, " =");
>  
> -	if (!next_type_marker(m)) {
> -		/* data type information missing, need to guess */
> -		dummy_marker.type = guess_value_type(prop);
> -		dummy_marker.next = prop->val.markers;
> -		dummy_marker.offset = 0;
> -		dummy_marker.ref = NULL;
> -		m = &dummy_marker;
> -	}
> +	guess_type_markers(prop);
> +	m = prop->val.markers;
>  
>  	for_each_marker(m) {
>  		size_t chunk_len = (m->next ? m->next->offset : len) - m->offset;

-- 
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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2025-08-21  4:31 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-20 13:11 [PATCH 0/6] Restore phandles from binary representations Uwe Kleine-König
2025-08-20 13:11 ` [PATCH 1/6] Emit /plugin/ when compiling to .dts with DTSF_PLUGIN set Uwe Kleine-König
2025-08-21  4:24   ` David Gibson
2025-08-20 13:11 ` [PATCH 2/6] Set DTSF_PLUGIN if needed when compiling from dtb Uwe Kleine-König
2025-08-21  4:24   ` David Gibson
2025-08-20 13:11 ` [PATCH 3/6] Improve type guessing when compiling to dts format Uwe Kleine-König
2025-08-21  4:24   ` David Gibson [this message]
2025-08-20 13:11 ` [PATCH 4/6] Restore labels from __symbols__ node Uwe Kleine-König
2025-08-21  4:25   ` David Gibson
2025-08-20 13:11 ` [PATCH 5/6] Restore phandle references from __local_fixups__ node Uwe Kleine-König
2025-08-21  4:23   ` David Gibson
2025-08-20 13:11 ` [PATCH 6/6] Restore phandle references from __fixups__ node Uwe Kleine-König
2025-08-21  4:31   ` David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aKafkjp1r5fUKYEF@zatzit \
    --to=david@gibson.dropbear.id.au \
    --cc=devicetree-compiler@vger.kernel.org \
    --cc=u.kleine-koenig@baylibre.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).