Skip to contentSkip to conversation

Add support for decimal, octal and binary based cell values.

PATCH In review Jon Loeliger 9 participants 23 messages Feb 15, 2007 – Feb 16, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add support for decimal, octal and binary based cell values.

New syntax d#, b#, o# and h# allow for an explicit prefix on cell values to specify their base. Eg: <d# 123>

---

Beginnings of test cases in the next patch. Don't stress the yylloc too much. We'll eventually work to make that better and more general later.

 data.c       |   21 +++++++++++++++++++++
 dtc-lexer.l  |   21 +++++++++++++++------
 dtc-parser.y |   16 ++++++++++++++--
 dtc.h        |    1 +
 4 files changed, 51 insertions(+), 8 deletions(-)
data.c +21 -0
index 1907a1a..c6c2350 100644
@@ -19,6 +19,7 @@
  */
 
 #include "dtc.h"
+#include "dtc-parser.tab.h"
 
 void fixup_free(struct fixup *f)
 {
@@ -224,6 +225,26 @@ struct data data_merge(struct data d1, struct data d2)
 	return d;
 }
 
+/*
+ * Convert a string representation of a numberic cell
+ * in the given base into a cell.
+ */
+cell_t data_convert_cell(char *s, unsigned int base)
+{
+	cell_t c;
+	extern YYLTYPE yylloc;
+
+	c = strtoul(s, NULL, base);
+	if (errno == EINVAL || errno == ERANGE) {
+		fprintf(stderr,
+			"Line %d: Invalid cell value '%s'; %d assumed\n",
+			yylloc.first_line, s, c);
+	}
+
+	return c;
+}
+
+
 struct data data_append_cell(struct data d, cell_t word)
 {
 	cell_t beword = cpu_to_be32(word);
dtc-lexer.l +15 -6
index 6146bad..93f3268 100644
@@ -83,15 +83,24 @@ REFCHAR		({PROPCHAR}|{UNITCHAR}|[/@])
 			BEGIN(INITIAL);
 			return ';';
 		}
+<CELLDATA>[bodh]# {
+			yylloc.first_line = yylineno;
+			if (*yytext == 'b')
+				yylval.cbase = 2;
+			else if (*yytext == 'o')
+				yylval.cbase = 8;
+			else if (*yytext == 'd')
+				yylval.cbase = 10;
+			else
+				yylval.cbase = 16;
+			DPRINT("Base: %d\n", yylval.cbase);
+			return DT_BASE;
+		}
 
 <CELLDATA>[0-9a-fA-F]+	{
 			yylloc.first_line = yylineno;
-			if (yyleng > 2*sizeof(yylval.cval)) {
-				fprintf(stderr,
-					"Cell value %s too long\n", yytext);
-			}
-			yylval.cval = strtoul(yytext, NULL, 16);
-			DPRINT("Cell: %x\n", yylval.cval);
+			yylval.str = strdup(yytext);
+			DPRINT("Cell: '%s'\n", yylval.str);
 			return DT_CELL;
 		}
 
dtc-parser.y +14 -2
index bd725fe..992fdb1 100644
@@ -33,6 +33,7 @@ extern struct boot_info *the_boot_info;
 
 %union {
 	cell_t cval;
+	unsigned int cbase;
 	u8 byte;
 	char *str;
 	struct data data;
@@ -50,7 +51,8 @@ extern struct boot_info *the_boot_info;
 %token <addr> DT_ADDR
 %token <str> DT_PROPNAME
 %token <str> DT_NODENAME
-%token <cval> DT_CELL
+%token <cbase> DT_BASE
+%token <str> DT_CELL
 %token <byte> DT_BYTE
 %token <data> DT_STRING
 %token <str> DT_UNIT
@@ -61,6 +63,7 @@ extern struct boot_info *the_boot_info;
 %type <data> propdataprefix
 %type <re> memreserve
 %type <re> memreserves
+%type <cbase> opt_cell_base
 %type <data> celllist
 %type <data> bytestring
 %type <prop> propdef
@@ -133,7 +136,16 @@ propdataprefix:	propdata ',' { $$ = $1; }
 	|	/* empty */ { $$ = empty_data; }
 	;
 
-celllist:	celllist DT_CELL { $$ = data_append_cell($1, $2); }
+opt_cell_base:
+	  /* empty */
+		{ $$ = 16; }
+	| DT_BASE
+	;
+
+celllist:	celllist opt_cell_base DT_CELL {
+			$$ = data_append_cell($1,
+					      data_convert_cell($3, $2));
+		}
 	|	celllist DT_REF	{
 			$$ = data_append_cell(data_add_fixup($1, $2), -1);
 		}
dtc.h +1 -0
index 8d3964c..7ee96a5 100644
@@ -118,6 +118,7 @@ struct data data_copy_mem(char *mem, int len);
 struct data data_copy_escape_string(char *s, int len);
 struct data data_copy_file(FILE *f, size_t len);
 
+cell_t data_convert_cell(char *s, unsigned int base);
 struct data data_append_data(struct data d, void *p, int len);
 struct data data_merge(struct data d1, struct data d2);
 struct data data_append_cell(struct data d, cell_t word);
Signature · 1 line
1.5.0.rc3.ge4b0e
#1 lore raw
Quoted 7 lines · Jon Loeliger

New syntax d#, b#, o# and h# allow for an explicit prefix on cell values to specify their base. Eg: <d# 123>

---

What are people's thoughts on supporting '0x' and '0X' for hex?

- k

#2 lore raw
In reply to Kumar Gala

=20

Quoted 4 lines

-----Original Message-----
From: [email protected]=20 [mailto:[email protected]]=20 On Behalf Of Kumar Gala

[snip]

Quoted 8 lines
Quoted 6 lines

New syntax d#, b#, o# and h# allow for an explicit prefix on cell values to specify their base. Eg: <d# 123>

---

=20
What are people's thoughts on supporting '0x' and '0X' for hex?

Are you saying allow d#1234, h#5678, and 0x5678? Or use 0x instead of h#?

Stuart

#8 lore raw
In reply to Yoder Stuart-B08248
Quoted 5 lines · On Thu, Feb 15, 2007 at 10:45:54AM -0700, Yoder Stuart-B08248 wrote:

I like to see no space between the d#, h#, etc and the numeric value.

Does anyone see a good reason to keep whitespace there?

Does it really matter what color the bike shed is?

http://www.unixguide.net/freebsd/faq/16.19.shtml

(I've seen 0x<hex> <decimal> or 0d<decimal> and 0b<binary> been used sometimes and work well, but I really don't care if it's what the dtc uses or not.)

-Olof

#9 lore raw

=20

Quoted 9 lines

-----Original Message-----
From: Olof Johansson [mailto:[email protected]]=20
Sent: Thursday, February 15, 2007 12:00 PM
To: Yoder Stuart-B08248
Cc: Jon Loeliger; [email protected]
Subject: Re: [DTC PATCH] Add support for decimal, octal and=20 binary based cell values.
=20
On Thu, Feb 15, 2007 at 10:45:54AM -0700, Yoder Stuart-B08248 wrote:

Quoted 7 lines
Quoted 5 lines

=20
I like to see no space between the d#, h#, etc and the numeric value.
=20
Does anyone see a good reason to keep whitespace there?

=20
Does it really matter what color the bike shed is?

In this case I think it does matter (a little :)). If you represent a number as <#h5678> it's clear that the #h and 5678 are related.

If you represent it as <#h 5678> the relation between the two is not as clear-- of course once you read the=20 spec you figure it out.

It's not a huge deal, but while we're adding a feature to dtc why not make the syntax as clear as we can?

Stuart

#10 lore raw
Quoted 9 lines · Yoder Stuart-B08248 wrote:

In this case I think it does matter (a little :)). If you represent a number as <#h5678> it's clear that the #h and 5678 are related.

If you represent it as <#h 5678> the relation between
the two is not as clear-- of course once you read the spec you figure it out.

It's not a huge deal, but while we're adding a feature to dtc why not make the syntax as clear as we can?

It should be noted that the patch Jon posted should accept it either with or without the space.

-Scott

#11 lore raw
In reply to Yoder Stuart-B08248

So, like, the other day "Yoder Stuart-B08248" mumbled:

Quoted 3 lines

I like to see no space between the d#, h#, etc and the numeric value.

So, that's one of the really nice things about parsers. Just do this, then:

    d23 = <d#23>;		// Just like <d# 23>!

The real question, though, is this: MUST the space between 'd#' and '23' be removed so that it is all one token? I suppose I could repaint that shed if needed, but I don't think it is.

jdl

#12 lore raw

=20

Quoted 12 lines

-----Original Message-----
From: Jon Loeliger [mailto:[email protected]]=20
Sent: Thursday, February 15, 2007 12:05 PM
To: Yoder Stuart-B08248
Cc: [email protected]
Subject: Re: [DTC PATCH] Add support for decimal,octal and=20 binary based cell values.=20
=20
So, like, the other day "Yoder Stuart-B08248" mumbled:

Quoted 3 lines

=20
I like to see no space between the d#, h#, etc and the numeric value.

Quoted 8 lines

=20
So, that's one of the really nice things about parsers. Just do this, then:
=20

    d23 =3D <d#23>;		// Just like <d# 23>!

=20
The real question, though, is this: MUST the space between 'd#' and '23' be removed so that it is all one token?

As long as the space is not required, it's cool.

Stuart

#3 lore raw
In reply to Yoder Stuart-B08248
Quoted 10 lines · On Feb 15, 2007, at 11:49 AM, Yoder Stuart-B08248 wrote:
Quoted 6 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Kumar Gala

[snip]

Quoted 3 lines
Quoted 3 lines

New syntax d#, b#, o# and h# allow for an explicit prefix on cell values to specify their base. Eg: <d# 123>

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

---

What are people's thoughts on supporting '0x' and '0X' for hex?

Are you saying allow d#1234, h#5678, and 0x5678? Or use 0x instead of h#?

I'm saying in addition to supporting the d#, h# notation.

The reason I'm suggesting support '0x' is its pretty natural from C and I know there have been a number of times when I forget that all int constants in .dts are hex.

- k

#13 lore raw
In reply to Jon Loeliger
Quoted 8 lines · Jon Loeliger

So, like, the other day "Yoder Stuart-B08248" mumbled:

Quoted 3 lines

I like to see no space between the d#, h#, etc and the numeric value.

So, that's one of the really nice things about parsers. Just do this, then:

Quoted 6 lines
    d23 = <d#23>;		// Just like <d# 23>!

The real question, though, is this: MUST the space between 'd#' and '23' be removed so that it is all one token? I suppose I could repaint that shed if needed, but I don't think it is.

Also, the question is, will the d# affect only the next number or all the numbers until the next ">" ?

Ben.

#14 lore raw
Quoted 11 lines · Benjamin Herrenschmidt
Quoted 10 lines · Jon Loeliger

So, like, the other day "Yoder Stuart-B08248" mumbled:

Quoted 3 lines

I like to see no space between the d#, h#, etc and the numeric value.

So, that's one of the really nice things about parsers. Just do this, then:

    d23 = <d#23>;		// Just like <d# 23>!
Quoted 7 lines
Quoted 4 lines

The real question, though, is this: MUST the space between 'd#' and '23' be removed so that it is all one token? I suppose I could repaint that shed if needed, but I don't think it is.

Also, the question is, will the d# affect only the next number or all the numbers until the next ">" ?

Only the next.

Signature · 4 lines
David Gibson			| 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
#4 lore raw
In reply to Kumar Gala
Quoted 19 lines · Kumar Gala
Quoted 16 lines · On Feb 15, 2007, at 11:49 AM, Yoder Stuart-B08248 wrote:
Quoted 6 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Kumar Gala

[snip]

Quoted 8 lines
Quoted 6 lines

New syntax d#, b#, o# and h# allow for an explicit prefix on cell values to specify their base. Eg: <d# 123>

---

What are people's thoughts on supporting '0x' and '0X' for hex?

Quoted 8 lines
Quoted 2 lines

Are you saying allow d#1234, h#5678, and 0x5678? Or use 0x instead of h#?

I'm saying in addition to supporting the d#, h# notation.

The reason I'm suggesting support '0x' is its pretty natural from C and I know there have been a number of times when I forget that all int constants in .dts are hex.

I'd prefer not to do this. I agree it would be nice in some ways, but I'm worried that if people see 0x all over the place, they'll assume that things without an 0x are decimal, which they can't be for compatibility.

Signature · 4 lines
David Gibson			| 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
#16 lore raw
In reply to Jon Loeliger
Quoted 5 lines · Jon Loeliger

New syntax d#, b#, o# and h# allow for an explicit prefix on cell values to specify their base. Eg: <d# 123>

[snip]

Quoted 27 lines

---

Beginnings of test cases in the next patch. Don't stress the yylloc too much. We'll eventually work to make that better and more general later.

 data.c       |   21 +++++++++++++++++++++
 dtc-lexer.l  |   21 +++++++++++++++------
 dtc-parser.y |   16 ++++++++++++++--
 dtc.h        |    1 +
 4 files changed, 51 insertions(+), 8 deletions(-)
data.c +1 -0
index 1907a1a..c6c2350 100644
@@ -19,6 +19,7 @@
  */
 
 #include "dtc.h"
+#include "dtc-parser.tab.h"
 
 void fixup_free(struct fixup *f)
 {

data.c struct data data_merge(struct data d1, struct data d2) · line 232

Quoted 8 lines
data.c +5 -0
@@ -224,6 +225,26 @@ struct data data_merge(struct data d1, struct data d2)
 	return d;
 }
 
+/*
+ * Convert a string representation of a numberic cell
+ * in the given base into a cell.
+ */
+cell_t data_convert_cell(char *s, unsigned int base)

I'd prefer a different name for this - and possibly it should go in a different file. The data_* prefix should be for functions that actually manipulate struct data objects.

data.c struct data data_merge(struct data d1, struct data d2) · line 242

Quoted 10 lines
data.c +10 -0
+{
+	cell_t c;
+	extern YYLTYPE yylloc;
+
+	c = strtoul(s, NULL, base);
+	if (errno == EINVAL || errno == ERANGE) {
+		fprintf(stderr,
+			"Line %d: Invalid cell value '%s'; %d assumed\n",
+			yylloc.first_line, s, c);
+	}

And I'd really prefer to keep the yyblah junk confined to the parser code. I think it would be better to move this function into dtc-parser.y

Otherwise looks pretty good.

Signature · 4 lines
David Gibson			| 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
#18 lore raw
In reply to Jon Loeliger
Quoted 2 lines · Jon Loeliger

New syntax d#, b#, o# and h# allow for an explicit prefix on cell values to specify their base. Eg: <d# 123>

Why do we need yet another syntax? Everyone else, including most silicon documentation, uses the standard "C" syntax 0x for hex, 0 for octal, and nothing for decimal. If really needed binary, we could us the '0b'. Of the few dts files I've done, anything to make this easier, rather than inventing something new, would be appreciated.

Thanks.

	-- Dan
#19 lore raw
Quoted 6 lines · Dan Malek
Quoted 2 lines · Jon Loeliger

New syntax d#, b#, o# and h# allow for an explicit prefix on cell values to specify their base. Eg: <d# 123>

Quoted 7 lines

Why do we need yet another syntax? Everyone else, including most silicon documentation, uses the standard "C" syntax 0x for hex, 0 for octal, and nothing for decimal. If really needed binary, we could us the '0b'. Of the few dts files I've done, anything to make this easier, rather than inventing something new, would be appreciated.

Yes, but hex-by-default is already implemented and out there. We can't change it without breaking compatibility horribly.

Signature · 4 lines
David Gibson			| 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
#15 lore raw
In reply to Benjamin Herrenschmidt

So, like, the other day Benjamin Herrenschmidt mumbled:

Quoted 3 lines

Also, the question is, will the d# affect only the next number or all the numbers until the next ">" ?

Just the next one.

jdl

#20 lore raw
In reply to David Gibson
Quoted 2 lines · David Gibson

Yes, but hex-by-default is already implemented and out there. We can't change it without breaking compatibility horribly.

Compatibility with what? The few dts files that exist that are still all changing frequently due to other tool modifications? I'd prefer to change them now, rather than propagate this new syntax.

Thanks.

	-- Dan
#17 lore raw
In reply to David Gibson

So, like, the other day David Gibson mumbled:

Quoted 4 lines

I'd prefer a different name for this - and possibly it should go in a different file. The data_* prefix should be for functions that actually manipulate struct data objects.

...

Quoted 3 lines

And I'd really prefer to keep the yyblah junk confined to the parser code. I think it would be better to move this function into dtc-parser.y

OK. Yeah, that's sort of what I was alluding to with the "don't stress the yylval gunk". But OK, in any event I'll move this function to the parser file or so.

Quoted 1 line

Otherwise looks pretty good.

Thanks.

jdl

#5 lore raw
In reply to David Gibson
Quoted 25 lines · David Gibson
Quoted 24 lines · Kumar Gala
Quoted 10 lines · On Feb 15, 2007, at 11:49 AM, Yoder Stuart-B08248 wrote:
Quoted 6 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Kumar Gala

[snip]

Quoted 3 lines
Quoted 3 lines

New syntax d#, b#, o# and h# allow for an explicit prefix on cell values to specify their base. Eg: <d# 123>

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

---

What are people's thoughts on supporting '0x' and '0X' for hex?

Are you saying allow d#1234, h#5678, and 0x5678? Or use 0x instead of h#?

I'm saying in addition to supporting the d#, h# notation.

Quoted 8 lines
Quoted 3 lines

The reason I'm suggesting support '0x' is its pretty natural from C and I know there have been a number of times when I forget that all int constants in .dts are hex.

I'd prefer not to do this. I agree it would be nice in some ways, but I'm worried that if people see 0x all over the place, they'll assume that things without an 0x are decimal, which they can't be for compatibility.

People don't know what to assume, I know I've made a number of errors when I forget that all numbers where hex.

I think we should change dtc to follow standard C conventions and do it now rather than later.

We've already introduced dtc version compatibilities issues. I agree with Dan that we should fix this now while there are a small handful of .dts in existence and we can provide a compat mode flag that treats non-prefixed numbers as hex instead of decimal.

(in theory with that we can use dtc to convert old .dts into new .dts)

- k

#21 lore raw
In reply to Dan Malek
Quoted 10 lines · Dan Malek
Quoted 2 lines · David Gibson

Yes, but hex-by-default is already implemented and out there. We can't change it without breaking compatibility horribly.

Compatibility with what? The few dts files that exist that are still all changing frequently due to other tool modifications? I'd prefer to change them now, rather than propagate this new syntax.

Nonetheless there are enough dts files out there - some of them within vendors where I can't see them - that I absolutely do not want to change something som fundamental in the format.

I have considered defining a "new-style" cell format, with delimiters different from < > that uses C style literals. But that idea's fairly ugly too.

Signature · 4 lines
David Gibson			| 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
#22 lore raw
In reply to Dan Malek
Quoted 4 lines

Why do we need yet another syntax? Everyone else, including most silicon documentation, uses the standard "C" syntax 0x for hex, 0 for octal, and nothing for decimal.

[Completely off-topic]

You're joking, right? Many docs use 1234h for hex and 1011b for binary; or $1234 for hex and %1011 for binary; or x'1234 and b'1011; or 1234_{(16)} and 1011_{(2)} (TeX notation -- base as decimal in a parenthesised subscript); etc. etc. etc.

Almost all computer languages use something more sane than the C-family stuff too (esp. the dreaded "0 is octal" thing is not exactly great).

Anyway, really off-topic :-)

Segher

#6 lore raw
In reply to Kumar Gala
Quoted 9 lines

I think we should change dtc to follow standard C conventions and do it now rather than later.

We've already introduced dtc version compatibilities issues. I agree with Dan that we should fix this now while there are a small handful of .dts in existence and we can provide a compat mode flag that treats non-prefixed numbers as hex instead of decimal.

(in theory with that we can use dtc to convert old .dts into new .dts)

May I suggest every DTS file start with a version statement? That way, you can change its format easily whenever you want. This would be version of the source language, not the same thing as version of the generated blob.

Segher