arch_templ.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* tripl/arch/arch_templ.c
  2. *
  3. * This file is part of tripl.
  4. *
  5. * tripl is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * tripl 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with tripl. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <sys/types.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <klist.h>
  24. #include <tripl/tripl.h>
  25. #include <tripl/limits.h>
  26. #include <tripl/arch.h>
  27. char *psave;
  28. struct klist_node names_list = KLIST_NODE_INIT(&names_list);
  29. extern char *line_eof;
  30. __arch_templ static int new_var(char *p, struct arch *);
  31. __arch_templ static int set_name(char *p, struct arch *);
  32. __arch_templ static int math_set_name(char *p, struct arch *);
  33. __arch_templ static int declare_object(char *p, struct arch *);
  34. __arch_templ int cstyle_declare(char *p, struct arch *);
  35. __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
  36. psave = p;
  37. if (unlikely(p == line_eof))
  38. return 0;
  39. switch (*p) {
  40. case '$':
  41. /* '$ asdf = 0;' --- asdf = 0
  42. * '$ asdf 0;' --- asdf = 0
  43. * '$ asdf;' --- asdf = 0
  44. * */
  45. p++;
  46. _skip_spaces(p);
  47. new_var(p, arch_fn);
  48. break;
  49. case '!':
  50. p++;
  51. _skip_spaces(p);
  52. if (likely(!arch_fn->arch_dump))
  53. exit(generic_error("unsupported dumping in current mode", psave, lno));
  54. arch_fn->arch_dump(getattrmask(p, dump_attrtab));
  55. break;
  56. case '<':
  57. declare_object(p, arch_fn);
  58. break;
  59. default:
  60. if (unlikely(!set_name(p, arch_fn) &&
  61. !math_set_name(p, arch_fn) &&
  62. !cstyle_declare(p, arch_fn)))
  63. exit(generic_error("unknow keyword", psave, lno));
  64. break;
  65. }
  66. return 0;
  67. }
  68. __arch_templ static int new_var(char *p, struct arch *arch_fn) {
  69. char *name;
  70. int type, out, attr;
  71. getword(p, &type, &out);
  72. if (unlikely(type != WORDTYPE_NAME))
  73. exit(generic_error("a name for variable required", psave, lno));
  74. name = strndup(p, out);
  75. p += out;
  76. _skip_spaces(p);
  77. getword(p, &type, &out);
  78. switch (type) {
  79. case WORDTYPE_KWORD:
  80. if (unlikely(out != '='))
  81. exit(generic_error("keyword but it's not a '='", psave, lno));
  82. ++p;
  83. _skip_spaces(p);
  84. getword(p, &type, &out);
  85. if (unlikely(type != WORDTYPE_NR))
  86. exit(generic_error("a number required", psave, lno));
  87. _skip_word(p);
  88. break;
  89. case WORDTYPE_NR:
  90. /* to attribute (if have) */
  91. _skip_word(p);
  92. break;
  93. case 0:
  94. out = 0;
  95. break;
  96. default:
  97. exit(generic_error("bad word after variable name", psave, lno));
  98. }
  99. attr = getattrmask(p, vardef_attrtab);
  100. arch_fn->arch_new_var(name, out, attr);
  101. /* the name can't be free */
  102. return 0;
  103. }
  104. static int set_name(char *p, struct arch *arch_fn) {
  105. char *name;
  106. int type, out;
  107. getword(p, &type, &out);
  108. if (type != WORDTYPE_NAME)
  109. return 0;
  110. name = strndup(p, out);
  111. p += out;
  112. _skip_spaces(p);
  113. getword(p, &type, &out);
  114. if (type != WORDTYPE_KWORD || out != '=')
  115. return 0;
  116. p++;
  117. _skip_spaces(p);
  118. getword(p, &type, &out);
  119. if (unlikely(type != WORDTYPE_NR))
  120. exit(generic_error("a number required", psave, lno));
  121. arch_fn->arch_set_var(name, out, 0);
  122. free(name);
  123. return 1;
  124. }
  125. /* 9 -> a; */
  126. static int math_set_name(char *p, struct arch *arch_fn) {
  127. char *name;
  128. int type, out, nr;
  129. getword(p, &type, &out);
  130. if (type != WORDTYPE_NR)
  131. return 0;
  132. nr = out;
  133. /* this requires a space */
  134. _skip_word(p);
  135. getword(p, &type, &out);
  136. if (unlikely(type != WORDTYPE_KWORD || out != '-'))
  137. exit(generic_error("a '->' required", psave, lno));
  138. p++;
  139. getword(p, &type, &out);
  140. if (unlikely(type != WORDTYPE_KWORD || out != '>'))
  141. exit(generic_error("a '->' required", psave, lno));
  142. p++;
  143. _skip_spaces(p);
  144. getword(p, &type, &out);
  145. if (unlikely(type != WORDTYPE_NAME))
  146. exit(generic_error("a name required", psave, lno));
  147. name = strndup(p, out);
  148. arch_fn->arch_set_var(name, nr, 0);
  149. free(name);
  150. return 1;
  151. }
  152. static int name_defined(char *name) {
  153. struct klist_node *n;
  154. for (n = names_list.next; n != &names_list; n = n->next)
  155. if (!strcmp(n->payload, name))
  156. return 1;
  157. return 0;
  158. }
  159. /*
  160. * 1.
  161. * < "libfoo.a" @ dynalib;
  162. * 2.
  163. * < fn @ fn;
  164. *
  165. * 3.
  166. * < fn 2 @ fn;
  167. *
  168. * 4.
  169. * < fn (a, b) @ fn;
  170. *
  171. * 5.
  172. * < age @ var;
  173. * */
  174. static int declare_object(char *p, struct arch *arch_fn) {
  175. char *name;
  176. int type, out, attr;
  177. enum { SHARED, NAMES, FUNCTION } guess;
  178. struct klist_node *n;
  179. /* skip '<' */
  180. p++;
  181. _skip_spaces(p);
  182. if (unlikely(!getword(p, &type, &out)))
  183. exit(generic_error("a module name or object name required", psave, lno));
  184. switch (type) {
  185. case WORDTYPE_STR:
  186. /* it must be a dynamic library */
  187. name = strndup(p + 1, out);
  188. guess = SHARED;
  189. if (arch_fn->arch_import_shared)
  190. arch_fn->arch_import_shared(name);
  191. p += out + 2;
  192. /* we will not free the name because interp_libs_dump uses it */
  193. break;
  194. case WORDTYPE_NAME:
  195. /* functions or variables */
  196. name = strndup(p, out);
  197. /* join to the name list */
  198. n = malloc(sizeof(struct klist_node));
  199. klist_init(n);
  200. n->payload = name;
  201. klist_add(&names_list, n);
  202. guess = NAMES;
  203. p += out;
  204. break;
  205. default:
  206. exit(generic_error("invalid type for '<'", psave, lno));
  207. }
  208. _skip_spaces(p);
  209. if (guess == SHARED)
  210. goto skip;
  211. /* try to getword */
  212. getword(p, &type, &out);
  213. if (type == WORDTYPE_KWORD && out == '@')
  214. goto skip;
  215. /* can just be a function define */
  216. guess = FUNCTION;
  217. if (type == WORDTYPE_NR) {
  218. /* only number of arguments */
  219. if (!out)
  220. /* special case */
  221. n->value = DECLARE_NOARG;
  222. else
  223. n->value = out;
  224. _skip_word(p);
  225. _skip_spaces(p);
  226. goto skip;
  227. }
  228. /* argument list */
  229. if (type == WORDTYPE_KWORD && out == '(') {
  230. n->value = 0;
  231. p++;
  232. _skip_spaces(p);
  233. for (;;) {
  234. if (unlikely(!getword(p, &type, &out)))
  235. /* end but no ')' */
  236. exit(generic_error("argument list terminated with no ')'", psave, lno));
  237. switch (type) {
  238. case WORDTYPE_KWORD:
  239. if (unlikely(out != ')' && out != ','))
  240. exit(generic_error("invalid operators between arguments", psave, lno));
  241. if (out == ')') {
  242. if (!n->value)
  243. n->value = DECLARE_NOARG;
  244. p++;
  245. _skip_spaces(p);
  246. goto skip;
  247. }
  248. p++;
  249. _skip_spaces(p);
  250. break;
  251. case WORDTYPE_NAME:
  252. n->value++;
  253. p += out;
  254. _skip_spaces(p);
  255. break;
  256. default:
  257. exit(generic_error("invalid type between arguments", psave, lno));
  258. }
  259. }
  260. }
  261. if (!n->value)
  262. n->value = DECLARE_NOARG;
  263. skip: attr = getattrmask(p, declare_attrtab);
  264. if (unlikely(!attr && guess != SHARED))
  265. exit(generic_error("you must give attributes for declares", psave, lno));
  266. if (guess != SHARED) {
  267. if (attr & 2)
  268. n->value |= DECLARE_FN;
  269. else if (attr & 4) {
  270. if (unlikely(guess == FUNCTION))
  271. exit(generic_error("you gave us a function declare but told us this is a variable declare? ",
  272. psave, lno));
  273. /* variable */
  274. n->value = DECLARE_VAR;
  275. }
  276. else
  277. exit(generic_error("one of 'fn' or 'var' must be gave", psave, lno));
  278. }
  279. if (unlikely(name_defined(n->payload))) {
  280. error((char *) n->payload);
  281. exit(generic_error("name already defined", psave, lno));
  282. }
  283. return 0;
  284. }