spec_syntax.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /**
  2. * Copyright (C) 2011 Anders Sundman <anders@4zm.org>
  3. *
  4. * This file is part of mfterm.
  5. *
  6. * mfterm is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * mfterm is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with mfterm. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <stddef.h>
  23. #include "util.h"
  24. #include "spec_syntax.h"
  25. // Forward declarations
  26. int sp_parse();
  27. extern FILE* sp_in;
  28. // Parse the input file and set up type_root and instance_root.
  29. // Return 0 on success.
  30. int spec_import(FILE* input) {
  31. // Parse the input to build a type hierarchy
  32. sp_in = input;
  33. if (sp_parse()) {
  34. printf("Syntax Error: Specification not imported\n");
  35. goto error;
  36. }
  37. // Check for missing definitions
  38. type_t* partial = tt_contains_partial_types();
  39. if (partial) {
  40. printf("Error: Incomplete declaration '%s' in specification.\n",
  41. partial->composite_extras->name);
  42. goto error;
  43. }
  44. // Make sure we have a root type defined
  45. if (type_root == NULL) {
  46. printf("Error: No root type '.' found in specification.\n");
  47. goto error;
  48. }
  49. // Create the instance tree
  50. instance_root = make_instance_tree(type_root);
  51. printf("Specification sucessfully imported.\n");
  52. return 0;
  53. error:
  54. clear_instance_tree();
  55. tt_clear();
  56. return 1;
  57. }
  58. // The primitive Byte type
  59. type_t byte_type = {
  60. .type_category = BYTE_TYPE,
  61. .composite_extras = NULL,
  62. };
  63. // The primitive Bit type
  64. type_t bit_type = {
  65. .type_category = BIT_TYPE,
  66. .composite_extras = NULL,
  67. };
  68. // Allocate and return a composite type instance. The type will assume
  69. // ownership of the heap allocated name.
  70. type_t* make_composite_type(char* name) {
  71. type_t* t = malloc(sizeof(type_t));
  72. t->type_category = COMPOSITE_TYPE;
  73. t->composite_extras = (composite_type_extras_t*)
  74. malloc(sizeof(composite_type_extras_t));
  75. if (name)
  76. t->composite_extras->name = name;
  77. else
  78. t->composite_extras->name = NULL; // Anonymous type
  79. t->composite_extras->fields = NULL;
  80. return t;
  81. }
  82. // Free a composite type. This function will also free it's fields.
  83. void free_composite_type(type_t* t) {
  84. // Free the fields
  85. field_list_t* iter = t->composite_extras->fields;
  86. while (iter) {
  87. field_list_t* tmp = iter;
  88. iter = iter->next_;
  89. free_field(tmp->field);
  90. free(tmp);
  91. }
  92. // Free the type data
  93. free(t->composite_extras->name);
  94. free(t->composite_extras);
  95. free(t);
  96. }
  97. // Allocate a new field with the given parameters. Anonymous '-'
  98. // filler fields use NULL as name. The field will assume ownership of
  99. // the heap allocated name.
  100. field_t* make_field(char* name, type_t* type, size_t length) {
  101. field_t* f = (field_t*) malloc(sizeof(field_t));
  102. f->name = name; // NULL for fillers
  103. f->type = type;
  104. f->length = length;
  105. return f;
  106. }
  107. // Free the memory used by a field.
  108. void free_field(field_t* field) {
  109. if (field == NULL)
  110. return;
  111. free(field->name);
  112. free(field);
  113. }
  114. // Add a field to an existing list of fields or, if the field_list
  115. // parameter is NULL, create a new field list. The order of fields is
  116. // significant and this function will append the field to the end of
  117. // the field_list.
  118. field_list_t* append_field(field_list_t* field_list, field_t* field) {
  119. // Create the list node
  120. field_list_t* flist = (field_list_t*) malloc(sizeof(field_list_t));
  121. flist->field = field;
  122. flist->next_ = NULL;
  123. // Create the list or append to the end
  124. if (field_list != NULL) {
  125. field_list_t* it = field_list;
  126. while(it->next_)
  127. it = it->next_;
  128. it->next_ = flist;
  129. }
  130. else {
  131. field_list = flist; // Won't effect the inarg
  132. }
  133. // Return the start of the list
  134. return field_list;
  135. }
  136. // Search the field list for a field with the given name
  137. field_t* get_field(field_list_t* field_list, const char* name) {
  138. if (name == NULL || field_list == NULL)
  139. return NULL;
  140. field_list_t* it = field_list;
  141. while(it) {
  142. field_t* f = it->field;
  143. if (f && f->name && strcmp(f->name, name) == 0)
  144. return f;
  145. it = it->next_;
  146. }
  147. // Not found
  148. return NULL;
  149. }
  150. // The global instance of the type table. If there isn't any, the
  151. // variable will be NULL. All the type table operations (tt_) operate
  152. // on this global variable.
  153. type_table_t* type_table = NULL;
  154. // The root type of the type hierarchy
  155. type_t* type_root = NULL;
  156. // Internal functions for allocating and freeing type table list nodes.
  157. type_table_t* tt_make_node_(type_t* t);
  158. void tt_free_node_(type_table_t* tt);
  159. // Clear the type table - freeing the memory used by the table and by
  160. // all the types.
  161. void tt_clear() {
  162. // Reset the root
  163. type_root = NULL;
  164. // Free all types and the table itself
  165. type_table_t* it = type_table;
  166. while(it) {
  167. type_table_t* next = it->next_;
  168. free_composite_type(it->type);
  169. tt_free_node_(it);
  170. it = next;
  171. };
  172. type_table = NULL;
  173. }
  174. // Add a type to the type table.
  175. type_t* tt_add_type(type_t* t) {
  176. if (type_table == NULL) {
  177. type_table = tt_make_node_(t);
  178. }
  179. else {
  180. type_table_t* it = type_table;
  181. while(it->next_)
  182. it = it->next_;
  183. it->next_ = tt_make_node_(t);
  184. }
  185. return t;
  186. }
  187. // Search the type table for a type with the given name. The first
  188. // type found will be returned. If no type is found, NULL is returned.
  189. type_t* tt_get_type(const char* type_name) {
  190. if (type_name == NULL)
  191. return NULL;
  192. type_table_t* it = type_table;
  193. while(it) {
  194. // Anonymous types (name == NULL) will never match
  195. if (it->type->composite_extras->name &&
  196. strcmp(type_name, it->type->composite_extras->name) == 0)
  197. return it->type; // Type was found!
  198. it = it->next_;
  199. }
  200. // Not found
  201. return NULL;
  202. }
  203. // Check if there are any partially declared types in the type
  204. // table. Return a pointer to the first incomplete type or NULL if
  205. // none exists.
  206. type_t* tt_contains_partial_types() {
  207. type_table_t* it = type_table;
  208. while(it) {
  209. if (it->type->composite_extras->decl_status == PARTIAL_DECL)
  210. return it->type;
  211. it = it->next_;
  212. }
  213. return NULL;
  214. }
  215. // Allocate a new list entry for the type table
  216. type_table_t* tt_make_node_(type_t* t) {
  217. type_table_t* tt = malloc(sizeof(type_table_t));
  218. tt->type = t;
  219. tt->next_ = NULL;
  220. return tt;
  221. }
  222. // Free a type table list entry (this won't free the type)
  223. void tt_free_node_(type_table_t* tt) {
  224. free(tt);
  225. }
  226. // The global variable representing the root instance; it is an
  227. // instanciation of the '.' type.
  228. instance_t* instance_root = NULL;
  229. // Forward decls of internal functions used during creation and
  230. // destruction of the instance tree.
  231. void clear_instance_tree_(instance_t* root);
  232. void make_instance_(instance_t* root,
  233. size_t* obytes, size_t* obits,
  234. size_t* sbytes, size_t* sbits);
  235. instance_t* make_byte_instance_(field_t* field, size_t* obytes, size_t* obits);
  236. instance_t* make_bit_instance_(field_t* field, size_t* obytes, size_t* obits);
  237. instance_t* make_composite_instance_(field_t* field,
  238. size_t* obytes, size_t* obits);
  239. instance_list_t* append_instance_(instance_list_t** end_ptr,
  240. instance_t* new_field);
  241. // Create an instance tree matching the type tree starting at
  242. // type_root. The global instance tree is constructed with type_root '.'.
  243. instance_t* make_instance_tree(type_t* type_root) {
  244. instance_t* root = malloc(sizeof(instance_t));
  245. root->offset_bytes = 0;
  246. root->offset_bits = 0;
  247. root->size_bytes = 0;
  248. root->size_bits = 0;
  249. root->field = make_field(strdup("."), type_root, 1);
  250. root->fields = NULL;
  251. size_t obytes = 0;
  252. size_t obits = 0;
  253. make_instance_(root, &obytes, &obits,
  254. &(root->size_bytes), &(root->size_bits));
  255. return root;
  256. }
  257. // Clear the global instance tree. Free it and set instance_root NULL
  258. void clear_instance_tree() {
  259. if (instance_root == NULL)
  260. return;
  261. clear_instance_tree_(instance_root);
  262. instance_root = NULL;
  263. }
  264. /* Get the child instance with a given name. Only look to children, */
  265. /* not grand children. If no child with the given name exists, return */
  266. /* null. */
  267. instance_t* get_instance_child(instance_t* inst, const char* name) {
  268. if (name == NULL)
  269. return NULL;
  270. return get_instance_child_n(inst, name, strlen(name));
  271. }
  272. /**
  273. * Like get_instance_child(inst, name), but name does not have to be
  274. * null terminated. Instead the length of the name string is given by
  275. * the last argument.
  276. */
  277. instance_t* get_instance_child_n(instance_t* inst,
  278. const char* name,
  279. size_t nlen) {
  280. if (inst == NULL || name == NULL || nlen == 0)
  281. return NULL;
  282. instance_list_t* iter = inst->fields;
  283. while(iter) {
  284. field_t* f = iter->instance->field;
  285. // Make sure the field has a name (isn't anaonymous) before comparing.
  286. if (f && f->name &&
  287. strlen(f->name) == nlen &&
  288. strncmp(f->name, name, nlen) == 0)
  289. return iter->instance;
  290. iter = iter->next_;
  291. }
  292. return NULL;
  293. }
  294. /**
  295. * Parse a specification path of the form '.fu.bar.baz' and return the
  296. * instance pointed to by baz. In case the path doesn't point to a
  297. * loaded instance, return NULL. */
  298. instance_t* parse_spec_path(const char* path) {
  299. const char* lastpath;
  300. instance_t* inst;
  301. // Parse the path up to the last instance
  302. if (parse_partial_spec_path(path, &lastpath, &inst) != 0)
  303. return NULL;
  304. // Find and return the leaf node
  305. return get_instance_child(inst, lastpath);
  306. }
  307. /**
  308. * Parse the path to produce a parent section and an instance that
  309. * points to the head of the parent.
  310. *
  311. * The format is .fu.bar.ba(z). Where .fu.bar.ba is the path, fu, bar
  312. * and baz are nested fields. The function should return parent_end
  313. * pointing into path to the point after the last '.', i.e. to the 'b'
  314. * in the last 'ba'. parent_inst will point to bar.
  315. *
  316. * The function returns 0 on success.
  317. */
  318. int parse_partial_spec_path(const char* path,
  319. const char** parent_end,
  320. instance_t** parent_inst) {
  321. // Check input. Paths start with '.'
  322. if (path == NULL || path[0] != '.')
  323. return 1;
  324. instance_t* inst = instance_root;
  325. const char* remaining_path = path + 1;
  326. char* tok_end;
  327. while((tok_end = strchr(remaining_path, '.')) != NULL) {
  328. // There is still a part of the path before the last '.'
  329. ptrdiff_t tok_name = tok_end - remaining_path;
  330. if (tok_name <= 0)
  331. return 1;
  332. inst = get_instance_child_n(inst, remaining_path, (size_t)tok_name);
  333. // Exit early (error in ancestor path)
  334. if (inst == NULL)
  335. return 1;
  336. // Remove the token and the '.' from the start of remaining string
  337. remaining_path = tok_end + 1;
  338. }
  339. // Set the output arguments
  340. if (parent_end != NULL)
  341. *parent_end = remaining_path;
  342. if (parent_inst != NULL)
  343. *parent_inst = inst;
  344. return 0;
  345. }
  346. // Count the number of fields in the instance
  347. int instance_fields_count(instance_t* inst) {
  348. if (inst == NULL)
  349. return 0;
  350. int count = 0;
  351. instance_list_t* it = inst->fields;
  352. while (it) {
  353. ++count;
  354. it = it->next_;
  355. }
  356. return count;
  357. }
  358. void clear_instance_tree_(instance_t* root) {
  359. instance_list_t* iter = root->fields;
  360. while (iter) {
  361. instance_list_t* tmp = iter->next_;
  362. clear_instance_tree_(iter->instance);
  363. free(iter);
  364. iter = tmp;
  365. }
  366. free(root);
  367. }
  368. void make_instance_(instance_t* root,
  369. size_t* obytes, size_t* obits,
  370. size_t* sbytes, size_t* sbits) {
  371. // Pointers to the matching type field and instance field beign processed.
  372. field_list_t* tf_iter = root->field->type->composite_extras->fields;
  373. instance_list_t* if_iter = root->fields;
  374. // Iterate over all the type fields and add matching instance fields.
  375. while(tf_iter) {
  376. field_t* f = tf_iter->field;
  377. // Create the child sub-tree or instance.
  378. instance_t* child;
  379. // Byte
  380. if (f->type == &byte_type) {
  381. child = make_byte_instance_(f, obytes, obits);
  382. }
  383. // Bit
  384. else if (f->type == &bit_type) {
  385. child = make_bit_instance_(f, obytes, obits);
  386. }
  387. // Composite type
  388. else {
  389. child = make_composite_instance_(f, obytes, obits);
  390. child->size_bytes = 0;
  391. child->size_bits = 0;
  392. make_instance_(child, obytes, obits,
  393. &(child->size_bytes), &(child->size_bits));
  394. // Update size with bits % 8
  395. child->size_bytes =
  396. child->size_bytes * f->length +
  397. (child->size_bits * f->length) / 8;
  398. child->size_bits = (child->size_bits * f->length) % 8;
  399. }
  400. // Add the new instance or instance tree, to the root instance
  401. // field list.
  402. if_iter = if_iter == NULL ?
  403. append_instance_(&(root->fields), child) :
  404. append_instance_(&(if_iter->next_), child);
  405. // Note that we are changing the value of the input parameters here
  406. // this is part of the 'return value' of the function. Wrap the
  407. // bit size mod 8.
  408. *sbytes = *sbytes + child->size_bytes + (*sbits + child->size_bits) / 8;
  409. *sbits = (*sbits + child->size_bits) % 8;
  410. tf_iter = tf_iter->next_;
  411. }
  412. }
  413. instance_t* make_byte_instance_(field_t* field, size_t* obytes, size_t* obits) {
  414. instance_t* i = malloc(sizeof(instance_t));
  415. i->field = field;
  416. i->fields = NULL;
  417. i->offset_bytes = *obytes;
  418. i->offset_bits = *obits;
  419. i->size_bytes = field->length;
  420. i->size_bits = 0;
  421. *obytes += i->size_bytes;
  422. // *obits += 0;
  423. return i;
  424. }
  425. instance_t* make_bit_instance_(field_t* field, size_t* obytes, size_t* obits) {
  426. instance_t* i = malloc(sizeof(instance_t));
  427. i->field = field;
  428. i->fields = NULL;
  429. i->offset_bytes = *obytes;
  430. i->offset_bits = *obits;
  431. // Bits % 8, overflow in bytes field
  432. i->size_bytes = field->length / 8;
  433. i->size_bits = field->length % 8;
  434. *obytes += i->size_bytes + (*obits + i->size_bits) / 8;
  435. *obits = (*obits + i->size_bits) % 8;
  436. return i;
  437. }
  438. instance_t* make_composite_instance_(field_t* field,
  439. size_t* obytes, size_t* obits) {
  440. instance_t* i = malloc(sizeof(instance_t));
  441. i->field = field;
  442. i->fields = NULL;
  443. i->offset_bytes = *obytes;
  444. i->offset_bits = *obits;
  445. // The size will be updated later, after recusion into all child fields.
  446. i->size_bytes = 0;
  447. i->size_bits = 0;
  448. return i;
  449. }
  450. instance_list_t* append_instance_(instance_list_t** end_ptr,
  451. instance_t* new_field) {
  452. instance_list_t* il = (instance_list_t*) malloc(sizeof(instance_list_t));
  453. il->instance = new_field;
  454. il->next_ = NULL;
  455. *end_ptr = il;
  456. return il;
  457. }
  458. // Forward declaration
  459. void print_instance_tree_(instance_t* i, int indent);
  460. void print_instance_(instance_t* i);
  461. void print_instance_tree() {
  462. if (instance_root == NULL) {
  463. printf("No specification loaded.\n");
  464. return;
  465. }
  466. printf("[%zu, %zu] [%zu, %zu] -- . (root)\n",
  467. instance_root->offset_bytes,
  468. instance_root->offset_bits,
  469. instance_root->size_bytes,
  470. instance_root->size_bits);
  471. print_instance_tree_(instance_root, 1);
  472. }
  473. void print_instance_tree_(instance_t* root, int indent) {
  474. // For each field of the root instance
  475. instance_list_t* il = root->fields;
  476. while(il) {
  477. // Indent
  478. int count = (indent - 1) * 2;
  479. while(count--)
  480. printf(" ");
  481. printf("+- ");
  482. // Print instance field
  483. instance_t* inst = il->instance;
  484. print_instance_(inst);
  485. if (inst->field->type->composite_extras != NULL)
  486. print_instance_tree_(inst, indent + 1);
  487. il = il->next_;
  488. }
  489. }
  490. void print_instance_(instance_t* i) {
  491. if (i->field->type == &byte_type) {
  492. printf("[%zu, %zu] [%zu, %zu] -- Byte[%zu] %s\n",
  493. i->offset_bytes,
  494. i->offset_bits,
  495. i->size_bytes,
  496. i->size_bits,
  497. i->field->length,
  498. i->field->name);
  499. }
  500. else if (i->field->type == &bit_type) {
  501. printf("[%zu, %zu] [%zu, %zu] -- Bit[%zu] %s\n",
  502. i->offset_bytes,
  503. i->offset_bits,
  504. i->size_bytes,
  505. i->size_bits,
  506. i->field->length,
  507. i->field->name);
  508. }
  509. else {
  510. printf("[%zu, %zu] [%zu, %zu] -- %s[%zu] %s\n",
  511. i->offset_bytes,
  512. i->offset_bits,
  513. i->size_bytes,
  514. i->size_bits,
  515. i->field->type->composite_extras->name,
  516. i->field->length,
  517. i->field->name);
  518. }
  519. }