bstruct_functions.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. function tokenize ($str, &$out) {
  3. $out = array();
  4. while (strlen($str) > 0) {
  5. if (preg_match('/^\\/\\/.*/', $str, $matches)) {
  6. $str = substr($str, strlen($matches[0]));
  7. }
  8. else if (preg_match('/^\\s+/', $str, $matches)) {
  9. $str = substr($str, strlen($matches[0]));
  10. }
  11. else if (preg_match('/^include/', $str, $matches)) {
  12. $out[] = array('include', null);
  13. $str = substr($str, strlen($matches[0]));
  14. }
  15. else if (preg_match('/^structure/', $str, $matches)) {
  16. $out[] = array('structure', null);
  17. $str = substr($str, strlen($matches[0]));
  18. }
  19. else if (preg_match('/^size/', $str, $matches)) {
  20. $out[] = array('size', null);
  21. $str = substr($str, strlen($matches[0]));
  22. }
  23. else if (preg_match('/^align/', $str, $matches)) {
  24. $out[] = array('align', null);
  25. $str = substr($str, strlen($matches[0]));
  26. }
  27. else if (preg_match('/^{/', $str, $matches)) {
  28. $out[] = array('spar', null);
  29. $str = substr($str, strlen($matches[0]));
  30. }
  31. else if (preg_match('/^}/', $str, $matches)) {
  32. $out[] = array('epar', null);
  33. $str = substr($str, strlen($matches[0]));
  34. }
  35. else if (preg_match('/^\(/', $str, $matches)) {
  36. $out[] = array('srpar', null);
  37. $str = substr($str, strlen($matches[0]));
  38. }
  39. else if (preg_match('/^\)/', $str, $matches)) {
  40. $out[] = array('erpar', null);
  41. $str = substr($str, strlen($matches[0]));
  42. }
  43. else if (preg_match('/^\[/', $str, $matches)) {
  44. $out[] = array('sbracket', null);
  45. $str = substr($str, strlen($matches[0]));
  46. }
  47. else if (preg_match('/^\]/', $str, $matches)) {
  48. $out[] = array('ebracket', null);
  49. $str = substr($str, strlen($matches[0]));
  50. }
  51. else if (preg_match('/^;/', $str, $matches)) {
  52. $out[] = array('semicolon', null);
  53. $str = substr($str, strlen($matches[0]));
  54. }
  55. else if (preg_match('/^,/', $str, $matches)) {
  56. $out[] = array('comma', null);
  57. $str = substr($str, strlen($matches[0]));
  58. }
  59. else if (preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*/', $str, $matches)) {
  60. $out[] = array('name', $matches[0]);
  61. $str = substr($str, strlen($matches[0]));
  62. }
  63. else if (preg_match('/^"([^"]*)"/', $str, $matches)) {
  64. $out[] = array('string', $matches[1]);
  65. $str = substr($str, strlen($matches[0]));
  66. }
  67. else {
  68. return FALSE;
  69. }
  70. }
  71. return TRUE;
  72. }
  73. function fatal_error ($message)
  74. {
  75. fwrite(STDERR, "Fatal error: $message\n");
  76. ob_get_clean();
  77. exit(1);
  78. }
  79. function make_size ($entry)
  80. {
  81. switch ($entry["type"]["type"]) {
  82. case "sizealign":
  83. return "({$entry["type"]["size"]})";
  84. case "structure":
  85. return "o->{$entry["name"]}_params.len";
  86. default:
  87. assert(0);
  88. }
  89. }
  90. function make_align ($entry)
  91. {
  92. switch ($entry["type"]["type"]) {
  93. case "sizealign":
  94. return "({$entry["type"]["align"]})";
  95. case "structure":
  96. return "o->{$entry["name"]}_params.align";
  97. default:
  98. assert(0);
  99. }
  100. }
  101. function generate_header ($name, $directives, $structures)
  102. {
  103. ob_start();
  104. echo <<<EOD
  105. /*
  106. DO NOT EDIT THIS FILE!
  107. This file was automatically generated by the bstruct generator.
  108. */
  109. #include <stdint.h>
  110. #include <misc/balign.h>
  111. EOD;
  112. foreach ($directives as $directive) {
  113. if ($directive["type"] == "include") {
  114. echo <<<EOD
  115. #include "{$directive["file"]}"
  116. EOD;
  117. }
  118. }
  119. echo <<<EOD
  120. EOD;
  121. foreach ($structures as $struct) {
  122. if ($struct["parameters"] == "") {
  123. $add_parameters = "";
  124. } else {
  125. $add_parameters = ", {$struct["parameters"]}";
  126. }
  127. echo <<<EOD
  128. typedef struct {$struct["name"]}_struct {$struct["name"]};
  129. typedef struct {
  130. EOD;
  131. foreach ($struct["entries"] as $entry) {
  132. if ($entry["type"]["type"] == "structure") {
  133. echo <<<EOD
  134. {$entry["type"]["name"]}Params {$entry["name"]}_params;
  135. EOD;
  136. }
  137. echo <<<EOD
  138. int {$entry["name"]}_off;
  139. int {$entry["name"]}_size;
  140. #ifndef NDEBUG
  141. int {$entry["name"]}_count;
  142. #endif
  143. EOD;
  144. }
  145. echo <<<EOD
  146. int len;
  147. int align;
  148. } {$struct["name"]}Params;
  149. static void {$struct["name"]}Params_Init ({$struct["name"]}Params *o{$add_parameters})
  150. {
  151. int cur_size;
  152. int cur_align;
  153. int cur_count;
  154. o->len = 0;
  155. o->align = 1;
  156. EOD;
  157. // Calculate the alignment of the structure as the maximum of alignments of its entries.
  158. // This assumes the alignments are powers of two; in general we would need the least
  159. // common multiple of the alignments.
  160. $prev = NULL;
  161. foreach ($struct["entries"] as $entry) {
  162. if ($entry["type"]["type"] == "structure") {
  163. if ($entry["type"]["parameters"] == "") {
  164. $init_add_parameters = "";
  165. } else {
  166. $init_add_parameters = ", {$entry["type"]["parameters"]}";
  167. }
  168. echo <<<EOD
  169. {$entry["type"]["name"]}Params_Init(&o->{$entry["name"]}_params{$init_add_parameters});
  170. EOD;
  171. }
  172. $size = make_size($entry);
  173. $align = make_align($entry);
  174. echo <<<EOD
  175. cur_size = {$size};
  176. cur_align = {$align};
  177. cur_count = ({$entry["num"]});
  178. EOD;
  179. $off = "BALIGN_UP_N(o->len, cur_align)";
  180. $len = "(cur_count * cur_size)";
  181. echo <<<EOD
  182. o->{$entry["name"]}_off = {$off};
  183. o->{$entry["name"]}_size = cur_size;
  184. #ifndef NDEBUG
  185. o->{$entry["name"]}_count = cur_count;
  186. #endif
  187. o->len = o->{$entry["name"]}_off + {$len};
  188. o->align = (cur_align > o->align ? cur_align : o->align);
  189. EOD;
  190. }
  191. echo <<<EOD
  192. }
  193. EOD;
  194. $prev = NULL;
  195. foreach ($struct["entries"] as $entry) {
  196. echo <<<EOD
  197. static {$entry["type"]["ctype"]} * {$struct["name"]}_{$entry["name"]} ({$struct["name"]}Params *o, {$struct["name"]} *s)
  198. {
  199. return ({$entry["type"]["ctype"]} *)((uint8_t *)s + o->{$entry["name"]}_off);
  200. }
  201. static {$entry["type"]["ctype"]} * {$struct["name"]}_{$entry["name"]}_at ({$struct["name"]}Params *o, {$struct["name"]} *s, int i)
  202. {
  203. ASSERT(i >= 0)
  204. ASSERT(i < o->{$entry["name"]}_count)
  205. return ({$entry["type"]["ctype"]} *)((uint8_t *)s + o->{$entry["name"]}_off + i * o->{$entry["name"]}_size);
  206. }
  207. EOD;
  208. }
  209. echo <<<EOD
  210. EOD;
  211. }
  212. return ob_get_clean();
  213. }