tripl.c 7.1 KB

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