tripl.c 7.1 KB

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