Skip to content

Commit 655fc2f

Browse files
kettenisjannau
authored andcommitted
scripts/dtc: Add support for floating-point literals
Signed-off-by: Asahi Lina <[email protected]>
1 parent a079050 commit 655fc2f

4 files changed

Lines changed: 66 additions & 0 deletions

File tree

scripts/dtc/data.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,33 @@ struct data data_append_integer(struct data d, uint64_t value, int bits)
197197
}
198198
}
199199

200+
struct data data_append_float(struct data d, double value, int bits)
201+
{
202+
float f32;
203+
uint32_t u32;
204+
double f64;
205+
uint64_t u64;
206+
fdt32_t value_32;
207+
fdt64_t value_64;
208+
209+
switch (bits) {
210+
case 32:
211+
f32 = value;
212+
memcpy(&u32, &f32, sizeof(u32));
213+
value_32 = cpu_to_fdt32(u32);
214+
return data_append_data(d, &value_32, 4);
215+
216+
case 64:
217+
f64 = value;
218+
memcpy(&u64, &f64, sizeof(u64));
219+
value_64 = cpu_to_fdt64(u64);
220+
return data_append_data(d, &value_64, 8);
221+
222+
default:
223+
die("Invalid literal size (%d)\n", bits);
224+
}
225+
}
226+
200227
struct data data_append_re(struct data d, uint64_t address, uint64_t size)
201228
{
202229
struct fdt_reserve_entry re;

scripts/dtc/dtc-lexer.l

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,28 @@ static void PRINTF(1, 2) lexical_error(const char *fmt, ...);
166166
return DT_LABEL;
167167
}
168168

169+
<V1>[-+]?(([0-9]+\.[0-9]*)|([0-9]*\.[0-9]+))(e[-+]?[0-9]+)?f? {
170+
char *e;
171+
DPRINT("Floating-point Literal: '%s'\n", yytext);
172+
173+
errno = 0;
174+
yylval.floating = strtod(yytext, &e);
175+
176+
if (*e && (*e != 'f' || e[1])) {
177+
lexical_error("Bad floating-point literal '%s'",
178+
yytext);
179+
}
180+
181+
if (errno == ERANGE)
182+
lexical_error("Floating-point literal '%s' out of range",
183+
yytext);
184+
else
185+
/* ERANGE is the only strtod error triggerable
186+
* by strings matching the pattern */
187+
assert(errno == 0);
188+
return DT_FP_LITERAL;
189+
}
190+
169191
<V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
170192
char *e;
171193
DPRINT("Integer Literal: '%s'\n", yytext);

scripts/dtc/dtc-parser.y

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ static bool is_ref_relative(const char *ref)
5959
struct node *nodelist;
6060
struct reserve_info *re;
6161
uint64_t integer;
62+
double floating;
6263
unsigned int flags;
6364
}
6465

@@ -72,6 +73,7 @@ static bool is_ref_relative(const char *ref)
7273
%token DT_OMIT_NO_REF
7374
%token <propnodename> DT_PROPNODENAME
7475
%token <integer> DT_LITERAL
76+
%token <floating> DT_FP_LITERAL
7577
%token <integer> DT_CHAR_LITERAL
7678
%token <byte> DT_BYTE
7779
%token <data> DT_STRING
@@ -95,6 +97,7 @@ static bool is_ref_relative(const char *ref)
9597
%type <node> subnode
9698
%type <nodelist> subnodes
9799

100+
%type <floating> floating_prim
98101
%type <integer> integer_prim
99102
%type <integer> integer_unary
100103
%type <integer> integer_mul
@@ -371,6 +374,15 @@ arrayprefix:
371374
$$.data = empty_data;
372375
$$.bits = 32;
373376
}
377+
| arrayprefix floating_prim
378+
{
379+
if ($1.bits < 32) {
380+
ERROR(&@2, "Floating-point values must be"
381+
" 32-bit or 64-bit");
382+
}
383+
384+
$$.data = data_append_float($1.data, $2, $1.bits);
385+
}
374386
| arrayprefix integer_prim
375387
{
376388
if ($1.bits < 64) {
@@ -410,6 +422,10 @@ arrayprefix:
410422
}
411423
;
412424

425+
floating_prim:
426+
DT_FP_LITERAL
427+
;
428+
413429
integer_prim:
414430
DT_LITERAL
415431
| DT_CHAR_LITERAL

scripts/dtc/dtc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ struct data data_insert_at_marker(struct data d, struct marker *m,
113113
struct data data_merge(struct data d1, struct data d2);
114114
struct data data_append_cell(struct data d, cell_t word);
115115
struct data data_append_integer(struct data d, uint64_t word, int bits);
116+
struct data data_append_float(struct data d, double value, int bits);
116117
struct data data_append_re(struct data d, uint64_t address, uint64_t size);
117118
struct data data_append_addr(struct data d, uint64_t addr);
118119
struct data data_append_byte(struct data d, uint8_t byte);

0 commit comments

Comments
 (0)