Re: [PATCH V2] dtc: Basic integer expressions

2012-04-03 Thread David Gibson
On Tue, Mar 27, 2012 at 08:24:43PM -0600, Stephen Warren wrote:
> Written by David Gibson . Additions by me:
> * Ported to ToT dtc.
> * Renamed cell to integer throughout.
> * Implemented value range checks.
> * Allow L/UL/LL/ULL suffix on literals.
> * Enabled the commented test.
> 
> Signed-off-by: Stephen Warren 
> ---
> v2:
> * s/cell/integer/ throughout.
> * Allow signed-extended values to pass the overall cell range check.
> * Allow L/UL/LL/ULL suffix on literals. This is purely for compatibility
>   with C, and has no effect on dtc's processing.
> * Enabled the 3 disabled tests.
> 
> I'm not sure if the literal suffix handling is hacky or not...
> 
>  dtc-lexer.l |   11 +++-
>  dtc-parser.y|  131 
> ---
>  tests/Makefile.tests|3 +-
>  tests/integer-expressions.c |  117 ++
>  tests/run_tests.sh  |5 ++
>  5 files changed, 256 insertions(+), 11 deletions(-)
>  create mode 100644 tests/integer-expressions.c
> 
> diff --git a/dtc-lexer.l b/dtc-lexer.l
> index 73d190c..0dbbbd5 100644
> --- a/dtc-lexer.l
> +++ b/dtc-lexer.l
> @@ -110,7 +110,7 @@ static int pop_input_file(void);
>   return DT_LABEL;
>   }
>  
> -[0-9]+|0[xX][0-9a-fA-F]+  {
> +([0-9]+|0[xX][0-9a-fA-F]+)(L|UL|LL|ULL)? {

Should include 'U' here as well.

>   yylval.literal = xstrdup(yytext);
>   DPRINT("Literal: '%s'\n", yylval.literal);
>   return DT_LITERAL;
> @@ -164,6 +164,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]);
> diff --git a/dtc-parser.y b/dtc-parser.y
> index 348616b..19db90e 100644
> --- a/dtc-parser.y
> +++ b/dtc-parser.y
> @@ -56,10 +56,12 @@ static unsigned char eval_char_literal(const char *s);
>   struct node *node;
>   struct node *nodelist;
>   struct reserve_info *re;
> + uint64_t integer;
>  }
>  
>  %token DT_V1
>  %token DT_MEMRESERVE
> +%token DT_LSHIFT DT_RSHIFT DT_LE DT_GE DT_EQ DT_NE DT_AND DT_OR
>  %token DT_BITS
>  %token  DT_PROPNODENAME
>  %token  DT_LITERAL
> @@ -86,6 +88,21 @@ static unsigned char eval_char_literal(const char *s);
>  %type  subnode
>  %type  subnodes
>  
> +%type  integer_prim
> +%type  integer_unary
> +%type  integer_mul
> +%type  integer_add
> +%type  integer_shift
> +%type  integer_rela
> +%type  integer_eq
> +%type  integer_bitand
> +%type  integer_bitxor
> +%type  integer_bitor
> +%type  integer_and
> +%type  integer_or
> +%type  integer_trinary
> +%type  integer_expr
> +
>  %%
>  
>  sourcefile:
> @@ -267,17 +284,17 @@ arrayprefix:
>   $$.data = empty_data;
>   $$.bits = 32;
>   }
> - | arrayprefix DT_LITERAL
> - {
> - uint64_t val = eval_literal($2, 0, $1.bits);
> -
> - $$.data = data_append_integer($1.data, val, $1.bits);
> - }
> - | arrayprefix DT_CHAR_LITERAL
> + | arrayprefix integer_prim

Here is good, but you can also allow expressions in /memreserve/ lines.

>   {
> - uint64_t val = eval_char_literal($2);
> + if ($1.bits < 64) {
> + uint64_t mask = (1ULL << $1.bits) - 1;
> + if (($2 > mask) && (($2 | mask) != -1ULL))

Hrm.  That condition isn't the most instantly understandable.

> + print_error(
> + "integer value out of range "
> + "%016lx (%d bits)", $1.bits);
> + }
>  
> - $$.data = data_append_integer($1.data, val, $1.bits);
> + $$.data = data_append_integer($1.data, $2, $1.bits);
>   }
>   | arrayprefix DT_REF
>   {
> @@ -299,6 +316,95 @@ arrayprefix:
>   }
>   ;
>  
> +integer_prim:
> +   DT_LITERAL
> + {
> + $$ = eval_literal($1, 0, 64);
> + }
> + | DT_CHAR_LITERAL
> + {
> + $$ = eval_char_literal($1);
> + }
> + | '(' integer_expr ')'
> + {
> + $$ = $2;
> + }
> + ;
> +
> +integer_expr:
> + integer_trinary
> + ;
> +
> +integer_trinary:
> +   

Re: [PATCH V2] dtc: Basic integer expressions

2012-04-03 Thread David Gibson
On Mon, Apr 02, 2012 at 10:16:27AM -0600, Stephen Warren wrote:
> On 03/31/2012 08:24 AM, Jon Loeliger wrote:
> >> Written by David Gibson . Additions by me:
> >> * Ported to ToT dtc.
> >> * Renamed cell to integer throughout.
> >> * Implemented value range checks.
> >> * Allow L/UL/LL/ULL suffix on literals.
> >> * Enabled the commented test.
> >>
> >> Signed-off-by: Stephen Warren 
> >> ---
> >> v2:
> >> * s/cell/integer/ throughout.
> >> * Allow signed-extended values to pass the overall cell range check.
> >> * Allow L/UL/LL/ULL suffix on literals. This is purely for compatibility
> >>   with C, and has no effect on dtc's processing.
> >> * Enabled the 3 disabled tests.
> >>
> >> I'm not sure if the literal suffix handling is hacky or not...
> > 
> > I get this too:
> > 
> >  CC tests/integer-expressions.o
> > cc1: warnings being treated as errors
> > tests/integer-expressions.c: In function 'main'
> > tests/integer-expressions.c:105: error: format  tests/integer-expressions.o
> > cc1: warnings being treated as errors
> > tests/integer-expressions.c:105: error: format '%ld' expects type 'long 
> > int', but argument 3 has type 'unsigned int'
> > make: *** [tests/integer-expressions.o] Error 1
> > 
> > $ gcc --version
> > gcc (Debian 4.4.5-8) 4.4.5
> 
> I assume this is a 32-bit system? I guess I can use  to
> solve this if needed.

The expression in question is based on sizeof() so has type size_t.
So %zd should do the trick.


-- 
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
___
devicetree-discuss mailing list
devicetree-discuss@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/devicetree-discuss


Re: [PATCH V2] dtc: Basic integer expressions

2012-04-02 Thread Stephen Warren
On 03/31/2012 08:24 AM, Jon Loeliger wrote:
>> Written by David Gibson . Additions by me:
>> * Ported to ToT dtc.
>> * Renamed cell to integer throughout.
>> * Implemented value range checks.
>> * Allow L/UL/LL/ULL suffix on literals.
>> * Enabled the commented test.
>>
>> Signed-off-by: Stephen Warren 
>> ---
>> v2:
>> * s/cell/integer/ throughout.
>> * Allow signed-extended values to pass the overall cell range check.
>> * Allow L/UL/LL/ULL suffix on literals. This is purely for compatibility
>>   with C, and has no effect on dtc's processing.
>> * Enabled the 3 disabled tests.
>>
>> I'm not sure if the literal suffix handling is hacky or not...
> 
> I get this too:
> 
>  CC tests/integer-expressions.o
> cc1: warnings being treated as errors
> tests/integer-expressions.c: In function 'main'
> tests/integer-expressions.c:105: error: format  tests/integer-expressions.o
> cc1: warnings being treated as errors
> tests/integer-expressions.c:105: error: format '%ld' expects type 'long int', 
> but argument 3 has type 'unsigned int'
> make: *** [tests/integer-expressions.o] Error 1
> 
> $ gcc --version
> gcc (Debian 4.4.5-8) 4.4.5

I assume this is a 32-bit system? I guess I can use  to
solve this if needed.
___
devicetree-discuss mailing list
devicetree-discuss@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/devicetree-discuss


Re: [PATCH V2] dtc: Basic integer expressions

2012-03-31 Thread Jon Loeliger
> Written by David Gibson . Additions by me:
> * Ported to ToT dtc.
> * Renamed cell to integer throughout.
> * Implemented value range checks.
> * Allow L/UL/LL/ULL suffix on literals.
> * Enabled the commented test.
> 
> Signed-off-by: Stephen Warren 
> ---
> v2:
> * s/cell/integer/ throughout.
> * Allow signed-extended values to pass the overall cell range check.
> * Allow L/UL/LL/ULL suffix on literals. This is purely for compatibility
>   with C, and has no effect on dtc's processing.
> * Enabled the 3 disabled tests.
> 
> I'm not sure if the literal suffix handling is hacky or not...

I get this too:

 CC tests/integer-expressions.o
cc1: warnings being treated as errors
tests/integer-expressions.c: In function 'main'
tests/integer-expressions.c:105: error: format  tests/integer-expressions.o
cc1: warnings being treated as errors
tests/integer-expressions.c:105: error: format '%ld' expects type 'long int', 
but argument 3 has type 'unsigned int'
make: *** [tests/integer-expressions.o] Error 1

$ gcc --version
gcc (Debian 4.4.5-8) 4.4.5

jdl
___
devicetree-discuss mailing list
devicetree-discuss@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/devicetree-discuss


[PATCH V2] dtc: Basic integer expressions

2012-03-27 Thread Stephen Warren
Written by David Gibson . Additions by me:
* Ported to ToT dtc.
* Renamed cell to integer throughout.
* Implemented value range checks.
* Allow L/UL/LL/ULL suffix on literals.
* Enabled the commented test.

Signed-off-by: Stephen Warren 
---
v2:
* s/cell/integer/ throughout.
* Allow signed-extended values to pass the overall cell range check.
* Allow L/UL/LL/ULL suffix on literals. This is purely for compatibility
  with C, and has no effect on dtc's processing.
* Enabled the 3 disabled tests.

I'm not sure if the literal suffix handling is hacky or not...

 dtc-lexer.l |   11 +++-
 dtc-parser.y|  131 ---
 tests/Makefile.tests|3 +-
 tests/integer-expressions.c |  117 ++
 tests/run_tests.sh  |5 ++
 5 files changed, 256 insertions(+), 11 deletions(-)
 create mode 100644 tests/integer-expressions.c

diff --git a/dtc-lexer.l b/dtc-lexer.l
index 73d190c..0dbbbd5 100644
--- a/dtc-lexer.l
+++ b/dtc-lexer.l
@@ -110,7 +110,7 @@ static int pop_input_file(void);
return DT_LABEL;
}
 
-[0-9]+|0[xX][0-9a-fA-F]+  {
+([0-9]+|0[xX][0-9a-fA-F]+)(L|UL|LL|ULL)? {
yylval.literal = xstrdup(yytext);
DPRINT("Literal: '%s'\n", yylval.literal);
return DT_LITERAL;
@@ -164,6 +164,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]);
diff --git a/dtc-parser.y b/dtc-parser.y
index 348616b..19db90e 100644
--- a/dtc-parser.y
+++ b/dtc-parser.y
@@ -56,10 +56,12 @@ static unsigned char eval_char_literal(const char *s);
struct node *node;
struct node *nodelist;
struct reserve_info *re;
+   uint64_t integer;
 }
 
 %token DT_V1
 %token DT_MEMRESERVE
+%token DT_LSHIFT DT_RSHIFT DT_LE DT_GE DT_EQ DT_NE DT_AND DT_OR
 %token DT_BITS
 %token  DT_PROPNODENAME
 %token  DT_LITERAL
@@ -86,6 +88,21 @@ static unsigned char eval_char_literal(const char *s);
 %type  subnode
 %type  subnodes
 
+%type  integer_prim
+%type  integer_unary
+%type  integer_mul
+%type  integer_add
+%type  integer_shift
+%type  integer_rela
+%type  integer_eq
+%type  integer_bitand
+%type  integer_bitxor
+%type  integer_bitor
+%type  integer_and
+%type  integer_or
+%type  integer_trinary
+%type  integer_expr
+
 %%
 
 sourcefile:
@@ -267,17 +284,17 @@ arrayprefix:
$$.data = empty_data;
$$.bits = 32;
}
-   | arrayprefix DT_LITERAL
-   {
-   uint64_t val = eval_literal($2, 0, $1.bits);
-
-   $$.data = data_append_integer($1.data, val, $1.bits);
-   }
-   | arrayprefix DT_CHAR_LITERAL
+   | arrayprefix integer_prim
{
-   uint64_t val = eval_char_literal($2);
+   if ($1.bits < 64) {
+   uint64_t mask = (1ULL << $1.bits) - 1;
+   if (($2 > mask) && (($2 | mask) != -1ULL))
+   print_error(
+   "integer value out of range "
+   "%016lx (%d bits)", $1.bits);
+   }
 
-   $$.data = data_append_integer($1.data, val, $1.bits);
+   $$.data = data_append_integer($1.data, $2, $1.bits);
}
| arrayprefix DT_REF
{
@@ -299,6 +316,95 @@ arrayprefix:
}
;
 
+integer_prim:
+ DT_LITERAL
+   {
+   $$ = eval_literal($1, 0, 64);
+   }
+   | DT_CHAR_LITERAL
+   {
+   $$ = eval_char_literal($1);
+   }
+   | '(' integer_expr ')'
+   {
+   $$ = $2;
+   }
+   ;
+
+integer_expr:
+   integer_trinary
+   ;
+
+integer_trinary:
+ integer_or
+   | integer_or '?' integer_expr ':' integer_trinary { $$ = $1 ? $3 : $5 }
+   ;
+
+integer_or:
+ integer_and
+   | integer_or DT_OR integer_and { $$ = $1 || $3 };
+   ;
+
+integer_and:
+ integer_bitor
+   | integer_and DT_AND integer_bitor { $$ = $1 && $3 };
+   ;
+
+integer_bitor:
+ integer_bitxor
+   | integer_bitor '|' integer_bitxor { $$ =