treesource.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #include "dtc.h"
  21. #include "srcpos.h"
  22. extern FILE *yyin;
  23. extern int yyparse(void);
  24. extern YYLTYPE yylloc;
  25. struct dt_info *parser_output;
  26. bool treesource_error;
  27. struct dt_info *dt_from_source(const char *fname)
  28. {
  29. parser_output = NULL;
  30. treesource_error = false;
  31. srcfile_push(fname);
  32. yyin = current_srcfile->f;
  33. yylloc.file = current_srcfile;
  34. if (yyparse() != 0)
  35. die("Unable to parse input tree\n");
  36. if (treesource_error)
  37. die("Syntax error parsing input tree\n");
  38. return parser_output;
  39. }
  40. static void write_prefix(FILE *f, int level)
  41. {
  42. int i;
  43. for (i = 0; i < level; i++)
  44. fputc('\t', f);
  45. }
  46. static bool isstring(char c)
  47. {
  48. return (isprint((unsigned char)c)
  49. || (c == '\0')
  50. || strchr("\a\b\t\n\v\f\r", c));
  51. }
  52. static void write_propval_string(FILE *f, const char *s, size_t len)
  53. {
  54. const char *end = s + len - 1;
  55. assert(*end == '\0');
  56. fprintf(f, "\"");
  57. while (s < end) {
  58. char c = *s++;
  59. switch (c) {
  60. case '\a':
  61. fprintf(f, "\\a");
  62. break;
  63. case '\b':
  64. fprintf(f, "\\b");
  65. break;
  66. case '\t':
  67. fprintf(f, "\\t");
  68. break;
  69. case '\n':
  70. fprintf(f, "\\n");
  71. break;
  72. case '\v':
  73. fprintf(f, "\\v");
  74. break;
  75. case '\f':
  76. fprintf(f, "\\f");
  77. break;
  78. case '\r':
  79. fprintf(f, "\\r");
  80. break;
  81. case '\\':
  82. fprintf(f, "\\\\");
  83. break;
  84. case '\"':
  85. fprintf(f, "\\\"");
  86. break;
  87. case '\0':
  88. fprintf(f, "\\0");
  89. break;
  90. default:
  91. if (isprint((unsigned char)c))
  92. fprintf(f, "%c", c);
  93. else
  94. fprintf(f, "\\x%02"PRIx8, c);
  95. }
  96. }
  97. fprintf(f, "\"");
  98. }
  99. static void write_propval_int(FILE *f, const char *p, size_t len, size_t width)
  100. {
  101. const char *end = p + len;
  102. assert(len % width == 0);
  103. for (; p < end; p += width) {
  104. switch (width) {
  105. case 1:
  106. fprintf(f, " %02"PRIx8, *(const uint8_t*)p);
  107. break;
  108. case 2:
  109. fprintf(f, " 0x%02"PRIx16, fdt16_to_cpu(*(const fdt16_t*)p));
  110. break;
  111. case 4:
  112. fprintf(f, " 0x%02"PRIx32, fdt32_to_cpu(*(const fdt32_t*)p));
  113. break;
  114. case 8:
  115. fprintf(f, " 0x%02"PRIx64, fdt64_to_cpu(*(const fdt64_t*)p));
  116. break;
  117. }
  118. }
  119. }
  120. static bool has_data_type_information(struct marker *m)
  121. {
  122. return m->type >= TYPE_UINT8;
  123. }
  124. static struct marker *next_type_marker(struct marker *m)
  125. {
  126. while (m && !has_data_type_information(m))
  127. m = m->next;
  128. return m;
  129. }
  130. size_t type_marker_length(struct marker *m)
  131. {
  132. struct marker *next = next_type_marker(m->next);
  133. if (next)
  134. return next->offset - m->offset;
  135. return 0;
  136. }
  137. static const char *delim_start[] = {
  138. [TYPE_UINT8] = "[",
  139. [TYPE_UINT16] = "/bits/ 16 <",
  140. [TYPE_UINT32] = "<",
  141. [TYPE_UINT64] = "/bits/ 64 <",
  142. [TYPE_STRING] = "",
  143. };
  144. static const char *delim_end[] = {
  145. [TYPE_UINT8] = " ]",
  146. [TYPE_UINT16] = " >",
  147. [TYPE_UINT32] = " >",
  148. [TYPE_UINT64] = " >",
  149. [TYPE_STRING] = "",
  150. };
  151. static enum markertype guess_value_type(struct property *prop)
  152. {
  153. int len = prop->val.len;
  154. const char *p = prop->val.val;
  155. struct marker *m = prop->val.markers;
  156. int nnotstring = 0, nnul = 0;
  157. int nnotstringlbl = 0, nnotcelllbl = 0;
  158. int i;
  159. for (i = 0; i < len; i++) {
  160. if (! isstring(p[i]))
  161. nnotstring++;
  162. if (p[i] == '\0')
  163. nnul++;
  164. }
  165. for_each_marker_of_type(m, LABEL) {
  166. if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
  167. nnotstringlbl++;
  168. if ((m->offset % sizeof(cell_t)) != 0)
  169. nnotcelllbl++;
  170. }
  171. if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
  172. && (nnotstringlbl == 0)) {
  173. return TYPE_STRING;
  174. } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
  175. return TYPE_UINT32;
  176. }
  177. return TYPE_UINT8;
  178. }
  179. static void write_propval(FILE *f, struct property *prop)
  180. {
  181. size_t len = prop->val.len;
  182. struct marker *m = prop->val.markers;
  183. struct marker dummy_marker;
  184. enum markertype emit_type = TYPE_NONE;
  185. if (len == 0) {
  186. fprintf(f, ";\n");
  187. return;
  188. }
  189. fprintf(f, " = ");
  190. if (!next_type_marker(m)) {
  191. /* data type information missing, need to guess */
  192. dummy_marker.type = guess_value_type(prop);
  193. dummy_marker.next = prop->val.markers;
  194. dummy_marker.offset = 0;
  195. dummy_marker.ref = NULL;
  196. m = &dummy_marker;
  197. }
  198. struct marker *m_label = prop->val.markers;
  199. for_each_marker(m) {
  200. size_t chunk_len;
  201. const char *p = &prop->val.val[m->offset];
  202. if (!has_data_type_information(m))
  203. continue;
  204. chunk_len = type_marker_length(m);
  205. if (!chunk_len)
  206. chunk_len = len - m->offset;
  207. if (emit_type != TYPE_NONE)
  208. fprintf(f, "%s, ", delim_end[emit_type]);
  209. emit_type = m->type;
  210. for_each_marker_of_type(m_label, LABEL) {
  211. if (m_label->offset > m->offset)
  212. break;
  213. fprintf(f, "%s: ", m_label->ref);
  214. }
  215. fprintf(f, "%s", delim_start[emit_type]);
  216. if (chunk_len <= 0)
  217. continue;
  218. switch(emit_type) {
  219. case TYPE_UINT16:
  220. write_propval_int(f, p, chunk_len, 2);
  221. break;
  222. case TYPE_UINT32:
  223. write_propval_int(f, p, chunk_len, 4);
  224. break;
  225. case TYPE_UINT64:
  226. write_propval_int(f, p, chunk_len, 8);
  227. break;
  228. case TYPE_STRING:
  229. write_propval_string(f, p, chunk_len);
  230. break;
  231. default:
  232. write_propval_int(f, p, chunk_len, 1);
  233. }
  234. }
  235. /* Wrap up any labels at the end of the value */
  236. for_each_marker_of_type(m_label, LABEL) {
  237. assert (m_label->offset == len);
  238. fprintf(f, " %s:", m_label->ref);
  239. }
  240. fprintf(f, "%s;\n", delim_end[emit_type] ? : "");
  241. }
  242. static void write_tree_source_node(FILE *f, struct node *tree, int level)
  243. {
  244. struct property *prop;
  245. struct node *child;
  246. struct label *l;
  247. write_prefix(f, level);
  248. for_each_label(tree->labels, l)
  249. fprintf(f, "%s: ", l->label);
  250. if (tree->name && (*tree->name))
  251. fprintf(f, "%s {\n", tree->name);
  252. else
  253. fprintf(f, "/ {\n");
  254. for_each_property(tree, prop) {
  255. write_prefix(f, level+1);
  256. for_each_label(prop->labels, l)
  257. fprintf(f, "%s: ", l->label);
  258. fprintf(f, "%s", prop->name);
  259. write_propval(f, prop);
  260. }
  261. for_each_child(tree, child) {
  262. fprintf(f, "\n");
  263. write_tree_source_node(f, child, level+1);
  264. }
  265. write_prefix(f, level);
  266. fprintf(f, "};\n");
  267. }
  268. void dt_to_source(FILE *f, struct dt_info *dti)
  269. {
  270. struct reserve_info *re;
  271. fprintf(f, "/dts-v1/;\n\n");
  272. for (re = dti->reservelist; re; re = re->next) {
  273. struct label *l;
  274. for_each_label(re->labels, l)
  275. fprintf(f, "%s: ", l->label);
  276. fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
  277. (unsigned long long)re->address,
  278. (unsigned long long)re->size);
  279. }
  280. write_tree_source_node(f, dti->dt, 0);
  281. }