tripl.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* tripl/tripl.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 <unistd.h>
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #include <string.h>
  23. #include <klist.h>
  24. #include <tripl/tripl.h>
  25. #include <tripl/limits.h>
  26. #include <tripl/arch.h>
  27. char *linebuffer_save = NULL;
  28. char *line_buffer = NULL;
  29. int lno = 0;
  30. char *eof;
  31. char *line_eof;
  32. static int next_line(char *),
  33. parse_line(void);
  34. struct arch *arch_fn;
  35. extern struct arch interp_arch_fn,
  36. x86_64_arch_fn;
  37. struct context *current;
  38. struct context main_context = {
  39. .funcname = "main",
  40. .link = NULL,
  41. .private_data = NULL
  42. };
  43. const static struct {
  44. char *name;
  45. struct arch *arch;
  46. } arch_list[] = {
  47. { "interp", &interp_arch_fn},
  48. { "x86_64", &x86_64_arch_fn},
  49. { NULL, NULL }
  50. };
  51. const struct attr_table vardef_attrtab[] = {
  52. { "global", 1 },
  53. { "rdonly", 4 },
  54. { "ptr", 8 },
  55. { NULL, 0 }
  56. };
  57. int do_tripl_compile(char *fbuffer, off_t fsize) {
  58. char *archname = getenv("TRIPL_ARCH");
  59. int i;
  60. current = &main_context;
  61. if (!archname)
  62. arch_fn = &interp_arch_fn;
  63. else {
  64. for (i = 0; arch_list[i].name; i++)
  65. if (!strcmp(arch_list[i].name, archname)) {
  66. arch_fn = arch_list[i].arch;
  67. goto ok;
  68. }
  69. fprintf(stderr, "*** unsupported arch %s (got from $TRIPL_ARCH). \n", archname);
  70. return 1;
  71. }
  72. ok: klist_init(&stack);
  73. eof = fbuffer + fsize;
  74. arch_fn->arch_init();
  75. while (next_line(fbuffer))
  76. parse_line();
  77. arch_fn->arch_fin ? arch_fn->arch_fin() : 0;
  78. return 0;
  79. }
  80. /* skip the comment, from '# asdf\nasdf2' to 'asdf2' */
  81. static char *__skip_comment(char *fbuffer) {
  82. if (*fbuffer != '#')
  83. return fbuffer;
  84. while (*fbuffer != '\n')
  85. fbuffer++;
  86. _skip_spaces(fbuffer);
  87. if (*fbuffer == '#')
  88. return __skip_comment(fbuffer);
  89. return fbuffer;
  90. }
  91. static int next_line(char *fbuffer) {
  92. char *p;
  93. ++lno;
  94. /* first time we call this, initial */
  95. if (!line_buffer) {
  96. p = line_eof = line_buffer = fbuffer = __skip_comment(fbuffer);
  97. if (fbuffer >= eof)
  98. /* only comment in this file */
  99. return 0;
  100. /* got line_eof */
  101. while (*p != ';')
  102. p++, line_eof++;
  103. linebuffer_save = line_buffer;
  104. return 1;
  105. }
  106. /* move line_buffer to ';' + 1 and line_eof to line_buffer -> ';' */
  107. line_buffer = fbuffer = __skip_comment(line_buffer);
  108. if (fbuffer >= eof)
  109. return 0;
  110. while (*line_buffer != ';')
  111. line_buffer++;
  112. line_buffer++;
  113. /* \n */
  114. _skip_spaces(line_buffer)
  115. line_buffer = __skip_comment(line_buffer);
  116. line_eof = line_buffer;
  117. while (*line_eof != ';')
  118. line_eof++;
  119. linebuffer_save = line_buffer;
  120. if (line_buffer >= eof)
  121. return 0;
  122. return 1;
  123. }
  124. static int getnumber(char *p) {
  125. int dec = 0, log = 1, neg = 0;
  126. if (!isnr(*p)) {
  127. if (*p == '+')
  128. neg = 0;
  129. else if (*p == '-')
  130. neg = 1;
  131. else
  132. exit(generic_error("a dec number required, but: ", linebuffer_save, lno));
  133. p++;
  134. }
  135. if (*p == '0' && isnr(p[1]))
  136. exit(generic_error("number starting with '0'", linebuffer_save, lno));
  137. /* never use eof because this is calling from getword() */
  138. while (isnr(*p)) p++;
  139. --p;
  140. while (p >= line_buffer) {
  141. dec += itoa(*p) * log;
  142. log *= 10;
  143. p--;
  144. if (!isnr(*p))
  145. /* for - and + */
  146. break;
  147. }
  148. return neg ? -dec : dec;
  149. }
  150. /* NEVER MODIFY THIS! MAGIC!
  151. * THIS IS REALLY THE UGLY CODE, MOST UGLY CODE ALL OVER THE WORLD.
  152. *
  153. * BUT IT WORKS NOW, SO WE DO NOT TO CHANGE THIS AND WE WILL CHANGE
  154. * A LOT BECAUSE ALL OF THE TRIPL REQUIRES THIS FUNCTION.
  155. * */
  156. #define isfirstchr() (!i)
  157. #define notfirstchr() i
  158. char *getword(char *fbuffer, int *type, int *out) {
  159. *type = WORDTYPE_INVAL;
  160. char *p;
  161. int i, in = 0;
  162. _skip_spaces(fbuffer);
  163. for (i = 0, p = fbuffer; p < line_eof; i++, p++) {
  164. if (*p == '\"') {
  165. if (in) {
  166. /* end */
  167. *out = i - 1;
  168. return fbuffer;
  169. }
  170. in = 1;
  171. *type = WORDTYPE_STR;
  172. }
  173. if (!in) {
  174. if (isnr(*p) || *p == '-' || *p == '+') {
  175. if (notfirstchr())
  176. /* name, but not a number */
  177. continue;
  178. if (!isnr(p[1]))
  179. continue;
  180. /* get the number, and save to *out */
  181. *out = getnumber(p);
  182. /* line finished. */
  183. *type = WORDTYPE_NR;
  184. return p;
  185. }
  186. if (i == 1 && isspace(*p)) {
  187. if (ischr(p[-1])) {
  188. /* name */
  189. *type = WORDTYPE_NAME;
  190. *out = 1;
  191. return --p;
  192. }
  193. /* keyword */
  194. *type = WORDTYPE_KWORD;
  195. *out = *--p;
  196. return p;
  197. }
  198. /* it can only be a name */
  199. if (isspace(*p)) {
  200. /* end */
  201. *type = WORDTYPE_NAME;
  202. *out = i;
  203. return fbuffer;
  204. }
  205. if (!ischr(*p)) {
  206. if (!i) {
  207. *type = WORDTYPE_KWORD;
  208. *out = *p;
  209. return fbuffer;
  210. }
  211. if (i == 1 && (p[-1] == '-' || p[-1] == '+')) {
  212. *type = WORDTYPE_KWORD;
  213. *out = p[-1];
  214. return fbuffer;
  215. }
  216. if (ischr(p[-1])) {
  217. *type = WORDTYPE_NAME;
  218. *out = i;
  219. return p - i;
  220. }
  221. }
  222. }
  223. }
  224. /* there isn't a space at end of the line */
  225. if (in)
  226. exit(generic_error("no '\"' found", linebuffer_save, lno));
  227. if (i == 1) {
  228. if (ischr(p[-1])) {
  229. *type = WORDTYPE_NAME;
  230. *out = 1;
  231. return p;
  232. }
  233. *type = WORDTYPE_KWORD;
  234. *out = *--p;
  235. return p;
  236. }
  237. if (!i)
  238. return NULL;
  239. /* only can be a name */
  240. *type = WORDTYPE_NAME;
  241. *out = i;
  242. return fbuffer;
  243. }
  244. static inline int find_attr(char *attrname, const struct attr_table *tab) {
  245. int i;
  246. for (i = 0; tab[i].attrname; i++)
  247. if (!strcmp(tab[i].attrname, attrname))
  248. return tab[i].attrmask;
  249. return 0;
  250. }
  251. int getattrmask(char *p, const struct attr_table *tab) {
  252. char *attrname;
  253. int type, out, mask = 0, t;
  254. if (!getword(p, &type, &out))
  255. return 0;
  256. if (type != WORDTYPE_KWORD)
  257. exit(generic_error("non-attribute ('@') character after that", linebuffer_save, lno));
  258. if (out != '@')
  259. exit(generic_error("not a '@' after string", linebuffer_save, lno));
  260. ++p;
  261. _skip_spaces(p);
  262. while (getword(p, &type, &out)) {
  263. if (type != WORDTYPE_NAME)
  264. exit(generic_error("an attribute name required", linebuffer_save, lno));
  265. attrname = strndup(p, out);
  266. if (!(t = find_attr(attrname, tab)))
  267. exit(generic_error("unknow attribute", linebuffer_save, lno));
  268. mask |= t;
  269. free(attrname);
  270. _skip_word(p);
  271. }
  272. return mask;
  273. }
  274. static int parse_line(void) {
  275. char *p;
  276. int type, out;
  277. if ((p = getword(line_buffer, &type, &out)) >= line_eof)
  278. /* empty line */
  279. return 0;
  280. return arch_do_keyword(p, arch_fn);
  281. }