Skip to content

Commit

Permalink
scripts/dtc: Add support for floating-point literals
Browse files Browse the repository at this point in the history
Signed-off-by: Asahi Lina <lina@asahilina.net>
  • Loading branch information
asahilina authored and marcan committed Mar 28, 2023
1 parent 5ee7556 commit a636217
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
27 changes: 27 additions & 0 deletions scripts/dtc/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,33 @@ struct data data_append_integer(struct data d, uint64_t value, int bits)
}
}

struct data data_append_float(struct data d, double value, int bits)
{
float f32;
uint32_t u32;
double f64;
uint64_t u64;
fdt32_t value_32;
fdt64_t value_64;

switch (bits) {
case 32:
f32 = value;
memcpy(&u32, &f32, sizeof(u32));
value_32 = cpu_to_fdt32(u32);
return data_append_data(d, &value_32, 4);

case 64:
f64 = value;
memcpy(&u64, &f64, sizeof(u64));
value_64 = cpu_to_fdt64(u64);
return data_append_data(d, &value_64, 8);

default:
die("Invalid literal size (%d)\n", bits);
}
}

struct data data_append_re(struct data d, uint64_t address, uint64_t size)
{
struct fdt_reserve_entry re;
Expand Down
22 changes: 22 additions & 0 deletions scripts/dtc/dtc-lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,28 @@ static void PRINTF(1, 2) lexical_error(const char *fmt, ...);
return DT_LABEL;
}

<V1>[-+]?(([0-9]+\.[0-9]*)|([0-9]*\.[0-9]+))(e[-+]?[0-9]+)?f? {
char *e;
DPRINT("Floating-point Literal: '%s'\n", yytext);

errno = 0;
yylval.floating = strtod(yytext, &e);

if (*e && (*e != 'f' || e[1])) {
lexical_error("Bad floating-point literal '%s'",
yytext);
}

if (errno == ERANGE)
lexical_error("Floating-point literal '%s' out of range",
yytext);
else
/* ERANGE is the only strtod error triggerable
* by strings matching the pattern */
assert(errno == 0);
return DT_FP_LITERAL;
}

<V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
char *e;
DPRINT("Integer Literal: '%s'\n", yytext);
Expand Down
16 changes: 16 additions & 0 deletions scripts/dtc/dtc-parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ static bool is_ref_relative(const char *ref)
struct node *nodelist;
struct reserve_info *re;
uint64_t integer;
double floating;
unsigned int flags;
}

Expand All @@ -61,6 +62,7 @@ static bool is_ref_relative(const char *ref)
%token DT_OMIT_NO_REF
%token <propnodename> DT_PROPNODENAME
%token <integer> DT_LITERAL
%token <floating> DT_FP_LITERAL
%token <integer> DT_CHAR_LITERAL
%token <byte> DT_BYTE
%token <data> DT_STRING
Expand All @@ -86,6 +88,7 @@ static bool is_ref_relative(const char *ref)
%type <node> subnode
%type <nodelist> subnodes

%type <floating> floating_prim
%type <integer> integer_prim
%type <integer> integer_unary
%type <integer> integer_mul
Expand Down Expand Up @@ -392,6 +395,15 @@ arrayprefix:
$$.data = data_add_marker(empty_data, TYPE_UINT32, NULL);
$$.bits = 32;
}
| arrayprefix floating_prim
{
if ($1.bits < 32) {
ERROR(&@2, "Floating-point values must be"
" 32-bit or 64-bit");
}

$$.data = data_append_float($1.data, $2, $1.bits);
}
| arrayprefix integer_prim
{
if ($1.bits < 64) {
Expand Down Expand Up @@ -436,6 +448,10 @@ arrayprefix:
}
;

floating_prim:
DT_FP_LITERAL
;

integer_prim:
DT_LITERAL
| DT_CHAR_LITERAL
Expand Down
1 change: 1 addition & 0 deletions scripts/dtc/dtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ struct data data_insert_at_marker(struct data d, struct marker *m,
struct data data_merge(struct data d1, struct data d2);
struct data data_append_cell(struct data d, cell_t word);
struct data data_append_integer(struct data d, uint64_t word, int bits);
struct data data_append_float(struct data d, double value, int bits);
struct data data_append_re(struct data d, uint64_t address, uint64_t size);
struct data data_append_addr(struct data d, uint64_t addr);
struct data data_append_byte(struct data d, uint8_t byte);
Expand Down

0 comments on commit a636217

Please sign in to comment.