Skip to contentSkip to conversation

dtc expressions

DISCUSSION David Gibson 2 participants 5 messages Mar 14, 2012 – Mar 17, 2012 4 messages not in the archive

lore
Share & tools
lore
#0 lore raw

So, in order to actually make progress on the dtc expressions / macros / whatnot stuff, I really think I should dust off my integer (constant) expressions patch, and we can apply that.

While we still have disagreements on a bunch of stuff here, I think we're at least agreed on what integer expressions should look like. And even if we change approaches later (e.g. to your ifs and fors in dtc approach) the integer expression syntax won't change, only the implementation.

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
#2 lore raw
In reply to a message not in the archive
Quoted 9 lines · David Gibson

So, in order to actually make progress on the dtc expressions / macros / whatnot stuff, I really think I should dust off my integer (constant) expressions patch, and we can apply that.

While we still have disagreements on a bunch of stuff here, I think we're at least agreed on what integer expressions should look like. And even if we change approaches later (e.g. to your ifs and fors in dtc approach) the integer expression syntax won't change, only the implementation.

I'd love to see something like that. I've don't think I've actually looked through your patches for that yet; I briefly looked at Jon's since they're in a branch in the dtc repo, but I don't think I managed to track down the patches you sent for this.

#4 lore raw
In reply to a message not in the archive
Quoted 11 lines · Stephen Warren
Quoted 9 lines · David Gibson

So, in order to actually make progress on the dtc expressions / macros / whatnot stuff, I really think I should dust off my integer (constant) expressions patch, and we can apply that.

While we still have disagreements on a bunch of stuff here, I think we're at least agreed on what integer expressions should look like. And even if we change approaches later (e.g. to your ifs and fors in dtc approach) the integer expression syntax won't change, only the implementation.

Quoted 4 lines

I'd love to see something like that. I've don't think I've actually looked through your patches for that yet; I briefly looked at Jon's since they're in a branch in the dtc repo, but I don't think I managed to track down the patches you sent for this.

It was quite a while back that I posted. I've got a version below, but it's *very* bitrotted and won't even go close to applying at present. I'm meaning to update it, but it will take a little time.

dtc-parser.y +83 -3
Index: dtc/dtc-parser.y
===================================================================
@@ -54,6 +54,7 @@ static unsigned long long eval_literal(c
 
 %token DT_V1
 %token DT_MEMRESERVE
+%token DT_LSHIFT DT_RSHIFT DT_LE DT_GE DT_EQ DT_NE DT_AND DT_OR
 %token <propnodename> DT_PROPNODENAME
 %token <literal> DT_LITERAL
 %token <cbase> DT_BASE
@@ -69,7 +70,6 @@ static unsigned long long eval_literal(c
 %type <re> memreserves
 %type <addr> addr
 %type <data> celllist
-%type <cell> cellval
 %type <data> bytestring
 %type <prop> propdef
 %type <proplist> proplist
@@ -80,6 +80,21 @@ static unsigned long long eval_literal(c
 %type <nodelist> subnodes
 %type <labelref> label
 
+%type <cell> cell_prim
+%type <cell> cell_unary
+%type <cell> cell_mul
+%type <cell> cell_add
+%type <cell> cell_shift
+%type <cell> cell_rela
+%type <cell> cell_eq
+%type <cell> cell_bitand
+%type <cell> cell_bitxor
+%type <cell> cell_bitor
+%type <cell> cell_and
+%type <cell> cell_or
+%type <cell> cell_trinary
+%type <cell> cell_expr
+
 %%
 
 sourcefile:
@@ -221,7 +236,7 @@ celllist:
 		{
 			$$ = empty_data;
 		}
-	| celllist cellval
+	| celllist cell_prim
 		{
 			$$ = data_append_cell($1, $2);
 		}
@@ -236,11 +251,76 @@ celllist:
 		}
 	;
 
-cellval:
+cell_prim:
 	  DT_LITERAL
 		{
 			$$ = eval_literal($1, 0, 32);
 		}
+	| '(' cell_expr ')'
+		{
+			$$ = $2;
+		}
+	;
+
+cell_expr:	cell_trinary
+	;
+
+cell_trinary:	cell_or
+	| cell_or '?' cell_expr ':' cell_trinary { $$ = $1 ? $3 : $5 }
+	;
+
+cell_or:	cell_and
+	|	cell_or DT_OR cell_and { $$ = $1 || $3 };
+
+cell_and:	cell_bitor
+	|	cell_and DT_AND cell_bitor { $$ = $1 && $3 };
+	;
+
+cell_bitor:	cell_bitxor
+	|	cell_bitor '|' cell_bitxor { $$ = $1 | $3 };
+	;
+
+cell_bitxor:	cell_bitand
+	|	cell_bitxor '^' cell_bitand { $$ = $1 ^ $3 };
+	;
+
+cell_bitand:	cell_eq
+	|	cell_bitand '&' cell_eq { $$ = $1 & $3 };
+	;
+
+cell_eq:	cell_rela
+	|	cell_eq DT_EQ cell_rela { $$ = $1 == $3; }
+	|	cell_eq DT_NE cell_rela { $$ = $1 != $3; }
+	;
+
+
+cell_rela:	cell_shift
+	|	cell_rela '<' cell_shift { $$ = $1 < $3; }
+	|	cell_rela '>' cell_shift { $$ = $1 > $3; }
+	|	cell_rela DT_LE cell_shift { $$ = $1 <= $3; }
+	|	cell_rela DT_GE cell_shift { $$ = $1 >= $3; }
+	;
+
+cell_shift:	cell_shift DT_LSHIFT cell_add { $$ = $1 << $3; }
+	|	cell_shift DT_RSHIFT cell_add { $$ = $1 >> $3; }
+	|	cell_add
+	;
+
+cell_add:	cell_add '+' cell_mul { $$ = $1 + $3; }
+	|	cell_add '-' cell_mul { $$ = $1 - $3; }
+	|	cell_mul
+	;
+
+cell_mul:	cell_mul '*' cell_unary { $$ = $1 * $3; }
+	|	cell_mul '/' cell_unary { $$ = $1 / $3; }
+	|	cell_mul '%' cell_unary { $$ = $1 % $3; }
+	|	cell_unary
+	;
+
+cell_unary:	cell_prim
+	|	'-' cell_unary { $$ = -$2; }
+	|	'~' cell_unary { $$ = ~$2; }
+	|	'!' cell_unary { $$ = !$2; }
 	;
 
 bytestring:
tests/run_tests.sh +5 -0
Index: dtc/tests/run_tests.sh
===================================================================
@@ -298,6 +298,11 @@ dtc_tests () {
     run_dtc_test -I dtb -O dts -o stdin_odts_test_tree1.dtb.test.dts - < test_tree1.dtb
     run_wrap_test cmp stdin_odts_test_tree1.dtb.test.dts odts_test_tree1.dtb.test.dts
 
+    # Check integer expresisons
+    run_test cell-expressions -g cell-expressions.test.dts
+    run_dtc_test -I dts -O dtb -o cell-expressions.test.dtb cell-expressions.test.dts
+    run_test cell-expressions cell-expressions.test.dtb
+
     # Check for graceful failure in some error conditions
     run_sh_test dtc-fatal.sh -I dts -O dtb nosuchfile.dts
     run_sh_test dtc-fatal.sh -I dtb -O dtb nosuchfile.dtb
dtc-lexer.l +9 -0
Index: dtc/dtc-lexer.l
===================================================================
@@ -149,6 +149,15 @@ static int pop_input_file(void);
 <*>{COMMENT}+	/* eat C-style comments */
 <*>{LINECOMMENT}+ /* eat C++-style comments */
 
+<*>"<<"		{ return DT_LSHIFT; };
+<*>">>"		{ return DT_RSHIFT; };
+<*>"<="		{ return DT_LE; };
+<*>">="		{ return DT_GE; };
+<*>"=="		{ return DT_EQ; };
+<*>"!="		{ return DT_NE; };
+<*>"&&"		{ return DT_AND; };
+<*>"||"		{ return DT_OR; };
+
 <*>.		{
 			DPRINT("Char: %c (\\x%02x)\n", yytext[0],
 				(unsigned)yytext[0]);
tests/Makefile.tests +1 -0
Index: dtc/tests/Makefile.tests
===================================================================
@@ -11,6 +11,7 @@ LIB_TESTS_L = get_mem_rsv \
 	move_and_save mangle-layout nopulate \
 	open_pack rw_tree1 set_name setprop del_property del_node \
 	string_escapes references path-references boot-cpuid incbin \
+	cell-expressions \
 	dtbs_equal_ordered \
 	add_subnode_with_nops path_offset_aliases
 LIB_TESTS = $(LIB_TESTS_L:%=$(TESTS_PREFIX)%)
tests/cell-expressions.c new file +115 -0

115 lines not on this page · raw message

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
#6 lore raw
In reply to a message not in the archive
Quoted 12 lines · David Gibson
Quoted 11 lines · Stephen Warren
Quoted 9 lines · David Gibson

So, in order to actually make progress on the dtc expressions / macros / whatnot stuff, I really think I should dust off my integer (constant) expressions patch, and we can apply that.

While we still have disagreements on a bunch of stuff here, I think we're at least agreed on what integer expressions should look like. And even if we change approaches later (e.g. to your ifs and fors in dtc approach) the integer expression syntax won't change, only the implementation.

Quoted 8 lines
Quoted 4 lines

I'd love to see something like that. I've don't think I've actually looked through your patches for that yet; I briefly looked at Jon's since they're in a branch in the dtc repo, but I don't think I managed to track down the patches you sent for this.

It was quite a while back that I posted. I've got a version below, but it's *very* bitrotted and won't even go close to applying at present. I'm meaning to update it, but it will take a little time.

That looks like a good set of integer expressions.

Do you think it'd be easy to agree on a simple "#define foo (expr)"-like syntax on top of this? I think that'd cover a lot of the use-cases people are looking for right now, which are more about creating/naming/using constants rather than building whole nodes or properties in some programmatic way.

Quoted 1 line

Index: dtc/dtc-lexer.l

...

Quoted 8 lines

+<*>"<<" { return DT_LSHIFT; };
+<*>">>" { return DT_RSHIFT; };
+<*>"<=" { return DT_LE; };
+<*>">=" { return DT_GE; };
+<*>"==" { return DT_EQ; };
+<*>"!=" { return DT_NE; };
+<*>"&&" { return DT_AND; };
+<*>"||" { return DT_OR; };

For my education, why aren't all the operators in the lexer file? When I first looked at this file, it seemed like a bunch were missing because they're only represented in the parser, as literal strings.

#8 lore raw
In reply to a message not in the archive
Quoted 18 lines · Stephen Warren
Quoted 17 lines · David Gibson
Quoted 15 lines · Stephen Warren
Quoted 9 lines · David Gibson

So, in order to actually make progress on the dtc expressions / macros / whatnot stuff, I really think I should dust off my integer (constant) expressions patch, and we can apply that.

While we still have disagreements on a bunch of stuff here, I think we're at least agreed on what integer expressions should look like. And even if we change approaches later (e.g. to your ifs and fors in dtc approach) the integer expression syntax won't change, only the implementation.

I'd love to see something like that. I've don't think I've actually looked through your patches for that yet; I briefly looked at Jon's since they're in a branch in the dtc repo, but I don't think I managed to track down the patches you sent for this.

Quoted 5 lines
Quoted 3 lines

It was quite a while back that I posted. I've got a version below, but it's *very* bitrotted and won't even go close to applying at present. I'm meaning to update it, but it will take a little time.

That looks like a good set of integer expressions.

Unsurprising - I basically copied the grammar for them from a grammar for C.

Quoted 2 lines

Do you think it'd be easy to agree on a simple "#define foo (expr)"-like syntax on top of this?

I'm not sure.

Quoted 4 lines

I think that'd cover a lot of the use-cases
people are looking for right now, which are more about creating/naming/using constants rather than building whole nodes or properties in some programmatic way.

Right, that's why I'm suggesting this as a starting point. I know there are people manually using cpp or other preprocessors on dts files already, and with this change alone, that becomes a lot more useful.

In terms of allowing a #define of our own, I see two simple options:
1) Just use cpp, and hack around the # issue, either by
invoking it with options to only recognize it in column 0, or by writing our own cpp implementation which definitely only recognizes in column 0.
2) Write our own little preprocessor that does defines
basically the same way as cpp, but calls it !define or /define/ or something.

Quoted 11 lines
Quoted 1 line

Index: dtc/dtc-lexer.l

...

Quoted 8 lines

+<*>"<<" { return DT_LSHIFT; };
+<*>">>" { return DT_RSHIFT; };
+<*>"<=" { return DT_LE; };
+<*>">=" { return DT_GE; };
+<*>"==" { return DT_EQ; };
+<*>"!=" { return DT_NE; };
+<*>"&&" { return DT_AND; };
+<*>"||" { return DT_OR; };

Quoted 3 lines

For my education, why aren't all the operators in the lexer file? When I first looked at this file, it seemed like a bunch were missing because they're only represented in the parser, as literal strings.

lex/yacc conventions. If you look you'll see the last rule in the lex file is <*>. and will pass through single characters as tokens to bison with their token code same as the character code. Using quoted characters directly in bison rules assumes that behaviour. The upshot of all that is that we only need to explicitly declare tokens for multi-character operators.

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