bstruct_functions.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 <limits.h>
  111. #include <misc/balign.h>
  112. #include <misc/debug.h>
  113. EOD;
  114. foreach ($directives as $directive) {
  115. if ($directive["type"] == "include") {
  116. echo <<<EOD
  117. #include "{$directive["file"]}"
  118. EOD;
  119. }
  120. }
  121. echo <<<EOD
  122. EOD;
  123. foreach ($structures as $struct) {
  124. if ($struct["parameters"] == "") {
  125. $add_parameters = "";
  126. } else {
  127. $add_parameters = ", {$struct["parameters"]}";
  128. }
  129. echo <<<EOD
  130. typedef struct {$struct["name"]}_struct {$struct["name"]};
  131. typedef struct {
  132. EOD;
  133. foreach ($struct["entries"] as $entry) {
  134. if ($entry["type"]["type"] == "structure") {
  135. echo <<<EOD
  136. {$entry["type"]["name"]}Params {$entry["name"]}_params;
  137. EOD;
  138. }
  139. echo <<<EOD
  140. size_t {$entry["name"]}_off;
  141. size_t {$entry["name"]}_size;
  142. #ifndef NDEBUG
  143. size_t {$entry["name"]}_count;
  144. #endif
  145. EOD;
  146. }
  147. echo <<<EOD
  148. size_t len;
  149. size_t align;
  150. } {$struct["name"]}Params;
  151. static int {$struct["name"]}Params_Init ({$struct["name"]}Params *o{$add_parameters}) WARN_UNUSED;
  152. static int {$struct["name"]}Params_Init ({$struct["name"]}Params *o{$add_parameters})
  153. {
  154. size_t cur_size;
  155. size_t cur_align;
  156. size_t cur_count;
  157. o->len = 0;
  158. o->align = 1;
  159. EOD;
  160. // Calculate the alignment of the structure as the maximum of alignments of its entries.
  161. // This assumes the alignments are powers of two; in general we would need the least
  162. // common multiple of the alignments.
  163. $prev = NULL;
  164. foreach ($struct["entries"] as $entry) {
  165. if ($entry["type"]["type"] == "structure") {
  166. if ($entry["type"]["parameters"] == "") {
  167. $init_add_parameters = "";
  168. } else {
  169. $init_add_parameters = ", {$entry["type"]["parameters"]}";
  170. }
  171. echo <<<EOD
  172. if (!{$entry["type"]["name"]}Params_Init(&o->{$entry["name"]}_params{$init_add_parameters})) {
  173. return 0;
  174. }
  175. EOD;
  176. }
  177. $size = make_size($entry);
  178. $align = make_align($entry);
  179. echo <<<EOD
  180. if ({$size} > SIZE_MAX) {
  181. return 0;
  182. }
  183. cur_size = {$size};
  184. if ({$align} > SIZE_MAX) {
  185. return 0;
  186. }
  187. cur_align = {$align};
  188. if ({$entry["num"]} > SIZE_MAX) {
  189. return 0;
  190. }
  191. cur_count = ({$entry["num"]});
  192. EOD;
  193. $off = "balign_up(o->len, cur_align)";
  194. echo <<<EOD
  195. if (balign_up_overflows(o->len, cur_align)) {
  196. return 0;
  197. }
  198. o->{$entry["name"]}_off = {$off};
  199. o->{$entry["name"]}_size = cur_size;
  200. #ifndef NDEBUG
  201. o->{$entry["name"]}_count = cur_count;
  202. #endif
  203. if (cur_count > SIZE_MAX / cur_size) {
  204. return 0;
  205. }
  206. if (o->{$entry["name"]}_off > SIZE_MAX - cur_count * cur_size) {
  207. return 0;
  208. }
  209. o->len = o->{$entry["name"]}_off + cur_count * cur_size;
  210. o->align = (cur_align > o->align ? cur_align : o->align);
  211. EOD;
  212. }
  213. echo <<<EOD
  214. return 1;
  215. }
  216. EOD;
  217. $prev = NULL;
  218. foreach ($struct["entries"] as $entry) {
  219. echo <<<EOD
  220. static {$entry["type"]["ctype"]} * {$struct["name"]}_{$entry["name"]} ({$struct["name"]}Params *o, {$struct["name"]} *s)
  221. {
  222. return ({$entry["type"]["ctype"]} *)((uint8_t *)s + o->{$entry["name"]}_off);
  223. }
  224. static {$entry["type"]["ctype"]} * {$struct["name"]}_{$entry["name"]}_at ({$struct["name"]}Params *o, {$struct["name"]} *s, size_t i)
  225. {
  226. ASSERT(i >= 0)
  227. ASSERT(i < o->{$entry["name"]}_count)
  228. return ({$entry["type"]["ctype"]} *)((uint8_t *)s + o->{$entry["name"]}_off + i * o->{$entry["name"]}_size);
  229. }
  230. EOD;
  231. }
  232. echo <<<EOD
  233. EOD;
  234. }
  235. return ob_get_clean();
  236. }