David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. |
| 3 | * |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation; either version 2 of the |
| 8 | * License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 18 | * USA |
| 19 | */ |
| 20 | |
David Gibson | 86dbcbd | 2005-10-19 16:00:31 +1000 | [diff] [blame] | 21 | %glr-parser |
| 22 | %locations |
| 23 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 24 | %{ |
| 25 | #include "dtc.h" |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 26 | #include "srcpos.h" |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 27 | |
Jon Loeliger | 3948849 | 2007-02-16 09:33:54 -0600 | [diff] [blame] | 28 | int yylex(void); |
Jon Loeliger | 3948849 | 2007-02-16 09:33:54 -0600 | [diff] [blame] | 29 | cell_t cell_from_string(char *s, unsigned int base); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 30 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 31 | extern struct boot_info *the_boot_info; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 32 | |
| 33 | %} |
| 34 | |
| 35 | %union { |
| 36 | cell_t cval; |
Jon Loeliger | af0278a | 2007-02-15 10:59:27 -0600 | [diff] [blame] | 37 | unsigned int cbase; |
David Gibson | 03a9b9d | 2005-07-11 16:49:52 +1000 | [diff] [blame] | 38 | u8 byte; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 39 | char *str; |
| 40 | struct data data; |
| 41 | struct property *prop; |
| 42 | struct property *proplist; |
| 43 | struct node *node; |
| 44 | struct node *nodelist; |
| 45 | int datalen; |
| 46 | int hexlen; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 47 | u64 addr; |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 48 | struct reserve_info *re; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 49 | } |
| 50 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 51 | %token DT_MEMRESERVE |
| 52 | %token <addr> DT_ADDR |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 53 | %token <str> DT_PROPNAME |
| 54 | %token <str> DT_NODENAME |
Jon Loeliger | af0278a | 2007-02-15 10:59:27 -0600 | [diff] [blame] | 55 | %token <cbase> DT_BASE |
| 56 | %token <str> DT_CELL |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 57 | %token <byte> DT_BYTE |
| 58 | %token <data> DT_STRING |
| 59 | %token <str> DT_UNIT |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 60 | %token <str> DT_LABEL |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 61 | %token <str> DT_REF |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 62 | |
| 63 | %type <data> propdata |
David Gibson | 32da475 | 2007-02-07 16:37:50 +1100 | [diff] [blame] | 64 | %type <data> propdataprefix |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 65 | %type <re> memreserve |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 66 | %type <re> memreserves |
Jon Loeliger | af0278a | 2007-02-15 10:59:27 -0600 | [diff] [blame] | 67 | %type <cbase> opt_cell_base |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 68 | %type <data> celllist |
| 69 | %type <data> bytestring |
| 70 | %type <prop> propdef |
| 71 | %type <proplist> proplist |
| 72 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 73 | %type <node> devicetree |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 74 | %type <node> nodedef |
| 75 | %type <node> subnode |
| 76 | %type <nodelist> subnodes |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 77 | %type <str> label |
| 78 | %type <str> nodename |
| 79 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 80 | %% |
| 81 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 82 | sourcefile: memreserves devicetree { |
| 83 | the_boot_info = build_boot_info($1, $2); |
| 84 | } |
| 85 | ; |
| 86 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 87 | memreserves: memreserve memreserves { |
| 88 | $$ = chain_reserve_entry($1, $2); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 89 | } |
| 90 | | /* empty */ { |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 91 | $$ = NULL; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 92 | } |
| 93 | ; |
| 94 | |
Milton Miller | d429033 | 2007-07-07 01:18:49 -0500 | [diff] [blame] | 95 | memreserve: label DT_MEMRESERVE DT_ADDR DT_ADDR ';' { |
| 96 | $$ = build_reserve_entry($3, $4, $1); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 97 | } |
Milton Miller | d429033 | 2007-07-07 01:18:49 -0500 | [diff] [blame] | 98 | | label DT_MEMRESERVE DT_ADDR '-' DT_ADDR ';' { |
| 99 | $$ = build_reserve_entry($3, $5 - $3 + 1, $1); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 100 | } |
| 101 | ; |
| 102 | |
| 103 | devicetree: '/' nodedef { |
| 104 | $$ = name_node($2, "", NULL); |
| 105 | } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 106 | ; |
| 107 | |
| 108 | nodedef: '{' proplist subnodes '}' ';' { |
| 109 | $$ = build_node($2, $3); |
| 110 | } |
| 111 | ; |
| 112 | |
| 113 | proplist: propdef proplist { |
| 114 | $$ = chain_property($1, $2); |
| 115 | } |
| 116 | | /* empty */ { |
| 117 | $$ = NULL; |
| 118 | } |
| 119 | ; |
| 120 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 121 | propdef: label DT_PROPNAME '=' propdata ';' { |
| 122 | $$ = build_property($2, $4, $1); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 123 | } |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 124 | | label DT_PROPNAME ';' { |
David Gibson | 9ad4587 | 2005-06-17 14:32:32 +1000 | [diff] [blame] | 125 | $$ = build_property($2, empty_data, $1); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 126 | } |
| 127 | ; |
| 128 | |
David Gibson | 32da475 | 2007-02-07 16:37:50 +1100 | [diff] [blame] | 129 | propdata: propdataprefix DT_STRING { $$ = data_merge($1, $2); } |
| 130 | | propdataprefix '<' celllist '>' { |
| 131 | $$ = data_merge(data_append_align($1, sizeof(cell_t)), $3); |
| 132 | } |
| 133 | | propdataprefix '[' bytestring ']' { $$ = data_merge($1, $3); } |
Milton Miller | 6a99b13 | 2007-07-07 01:18:51 -0500 | [diff] [blame^] | 134 | | propdata DT_LABEL { $$ = data_add_label($1, $2); } |
David Gibson | 32da475 | 2007-02-07 16:37:50 +1100 | [diff] [blame] | 135 | ; |
| 136 | |
| 137 | propdataprefix: propdata ',' { $$ = $1; } |
Milton Miller | 6a99b13 | 2007-07-07 01:18:51 -0500 | [diff] [blame^] | 138 | | propdataprefix DT_LABEL { $$ = data_add_label($1, $2); } |
David Gibson | 32da475 | 2007-02-07 16:37:50 +1100 | [diff] [blame] | 139 | | /* empty */ { $$ = empty_data; } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 140 | ; |
| 141 | |
Jon Loeliger | af0278a | 2007-02-15 10:59:27 -0600 | [diff] [blame] | 142 | opt_cell_base: |
| 143 | /* empty */ |
| 144 | { $$ = 16; } |
| 145 | | DT_BASE |
| 146 | ; |
| 147 | |
| 148 | celllist: celllist opt_cell_base DT_CELL { |
| 149 | $$ = data_append_cell($1, |
Jon Loeliger | 3948849 | 2007-02-16 09:33:54 -0600 | [diff] [blame] | 150 | cell_from_string($3, $2)); |
Jon Loeliger | af0278a | 2007-02-15 10:59:27 -0600 | [diff] [blame] | 151 | } |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 152 | | celllist DT_REF { |
| 153 | $$ = data_append_cell(data_add_fixup($1, $2), -1); |
| 154 | } |
Milton Miller | 6a99b13 | 2007-07-07 01:18:51 -0500 | [diff] [blame^] | 155 | | celllist DT_LABEL { $$ = data_add_label($1, $2); } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 156 | | /* empty */ { $$ = empty_data; } |
| 157 | ; |
| 158 | |
| 159 | bytestring: bytestring DT_BYTE { $$ = data_append_byte($1, $2); } |
Milton Miller | 6a99b13 | 2007-07-07 01:18:51 -0500 | [diff] [blame^] | 160 | | bytestring DT_LABEL { $$ = data_add_label($1, $2); } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 161 | | /* empty */ { $$ = empty_data; } |
| 162 | ; |
| 163 | |
| 164 | subnodes: subnode subnodes { |
| 165 | $$ = chain_node($1, $2); |
| 166 | } |
| 167 | | /* empty */ { $$ = NULL; } |
| 168 | ; |
| 169 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 170 | subnode: label nodename nodedef { $$ = name_node($3, $2, $1); } |
| 171 | ; |
| 172 | |
| 173 | nodename: DT_NODENAME { $$ = $1; } |
| 174 | | DT_PROPNAME { $$ = $1; } |
| 175 | ; |
| 176 | |
| 177 | label: DT_LABEL { $$ = $1; } |
| 178 | | /* empty */ { $$ = NULL; } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 179 | ; |
| 180 | |
| 181 | %% |
| 182 | |
| 183 | void yyerror (char const *s) |
| 184 | { |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 185 | const char *fname = srcpos_filename_for_num(yylloc.filenum); |
| 186 | |
| 187 | if (strcmp(fname, "-") == 0) |
| 188 | fname = "stdin"; |
| 189 | |
| 190 | fprintf(stderr, "%s:%d %s\n", |
| 191 | fname, yylloc.first_line, s); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 192 | } |
Jon Loeliger | 3948849 | 2007-02-16 09:33:54 -0600 | [diff] [blame] | 193 | |
| 194 | |
| 195 | /* |
| 196 | * Convert a string representation of a numeric cell |
| 197 | * in the given base into a cell. |
| 198 | * |
Milton Miller | 85ab5cc | 2007-07-07 01:18:48 -0500 | [diff] [blame] | 199 | * FIXME: should these specification errors be fatal instead? |
Jon Loeliger | 3948849 | 2007-02-16 09:33:54 -0600 | [diff] [blame] | 200 | */ |
| 201 | |
| 202 | cell_t cell_from_string(char *s, unsigned int base) |
| 203 | { |
| 204 | cell_t c; |
Milton Miller | 85ab5cc | 2007-07-07 01:18:48 -0500 | [diff] [blame] | 205 | char *e; |
Jon Loeliger | 3948849 | 2007-02-16 09:33:54 -0600 | [diff] [blame] | 206 | |
Milton Miller | 85ab5cc | 2007-07-07 01:18:48 -0500 | [diff] [blame] | 207 | c = strtoul(s, &e, base); |
| 208 | if (*e) { |
| 209 | fprintf(stderr, |
| 210 | "Line %d: Invalid cell value '%s' : " |
| 211 | "%c is not a base %d digit; %d assumed\n", |
| 212 | yylloc.first_line, s, *e, base, c); |
| 213 | } |
| 214 | |
Jon Loeliger | 3948849 | 2007-02-16 09:33:54 -0600 | [diff] [blame] | 215 | if (errno == EINVAL || errno == ERANGE) { |
| 216 | fprintf(stderr, |
| 217 | "Line %d: Invalid cell value '%s'; %d assumed\n", |
| 218 | yylloc.first_line, s, c); |
Milton Miller | 85ab5cc | 2007-07-07 01:18:48 -0500 | [diff] [blame] | 219 | errno = 0; |
Jon Loeliger | 3948849 | 2007-02-16 09:33:54 -0600 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | return c; |
| 223 | } |