asm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* tripl/arch/x86_64/asm.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 <string.h>
  20. #include <klist.h>
  21. #include <tripl/tripl.h>
  22. #include <tripl/limits.h>
  23. #include <tripl/arch.h>
  24. static char *lbsave;
  25. static int stack_index;
  26. struct klist_node globvars;
  27. static int do_funcall(char *p);
  28. static int set_name(char *p, char *name);
  29. const static char asm_head[] = {
  30. " .file \"%s\"\n"
  31. " .text\n"
  32. " .globl _start\n"
  33. "_start:\n"
  34. " /* call main and exit */\n"
  35. " call main\n"
  36. " movq %%rax, %%rbx\n"
  37. " movq $1, %%rax\n"
  38. " /* exit */\n"
  39. " int $0x80\n\n"
  40. " .globl main\n"
  41. /* use -x(%rbp) to access */
  42. "main: pushq %%rbp\n"
  43. " movq %%rsp, %%rbp\n"
  44. };
  45. const static char global_var[] = {
  46. " .data\n"
  47. " .globl %s\n"
  48. "%s: .long %d\n"
  49. " .text\n"
  50. };
  51. const static char stack_var[] = {
  52. " movq $%d, -%d(%%rbp)\n"
  53. };
  54. const static char wr_stkvar[] = {
  55. " movq $%d, -%d(%%rbp)\n"
  56. };
  57. const static char wr_glbvar[] = {
  58. " movq $%d, %s(%%rip)\n"
  59. };
  60. const static char noarg_funcall[] = {
  61. " call %s\n"
  62. };
  63. const static char funcend[] = {
  64. " popq %%rbp\n"
  65. " ret\n"
  66. };
  67. int x86_64_init_asm(void) {
  68. stack_index = 0;
  69. printf(asm_head, fname);
  70. return 0;
  71. }
  72. int x86_64_asm_fin(void) {
  73. printf(funcend);
  74. return 0;
  75. }
  76. int x86_64_do_asm(char *p, char c) {
  77. char *name = NULL, *save;
  78. int out, type, attr;
  79. lbsave = p;
  80. switch (c) {
  81. case '$':
  82. ++p;
  83. _skip_spaces(p);
  84. getword(p, &type, &out);
  85. if (type != WORDTYPE_NAME)
  86. exit(generic_error("'$' requires a name", lbsave, lno));
  87. name = strndup(p, out);
  88. p += out;
  89. _skip_spaces(p);
  90. getword(p, &type, &out);
  91. if (type == WORDTYPE_KWORD) {
  92. if (out != '=')
  93. exit(generic_error("invalid keyword after name", lbsave, lno));
  94. p++;
  95. _skip_spaces(p);
  96. getword(p, &type, &out);
  97. }
  98. if (type != WORDTYPE_NR) {
  99. if (type)
  100. exit(generic_error("'$' requires a number", lbsave, lno));
  101. out = 0;
  102. }
  103. _skip_spaces(p);
  104. _skip_word(p);
  105. _skip_spaces(p);
  106. attr = getattrmask(p, vardef_attrtab);
  107. if (attr & 1) {
  108. /* global variable */
  109. printf(global_var, name, name, out);
  110. __push_nr(&globvars, 0, name, attr);
  111. }
  112. else {
  113. /* variables on stack */
  114. printf(stack_var, out, stack_index << 3);
  115. push_nr(stack_index, name, attr);
  116. stack_index++;
  117. }
  118. break;
  119. case '@':
  120. p++;
  121. _skip_spaces(p);
  122. do_funcall(p);
  123. break;
  124. default:
  125. save = p;
  126. /* setting or function calling */
  127. getword(p, &type, &out);
  128. if (type != WORDTYPE_NAME)
  129. exit(generic_error("unknow keyword", lbsave, lno));
  130. name = strndup(p, out);
  131. _skip_word(p);
  132. _skip_spaces(p);
  133. if (*p == '(') {
  134. do_funcall(save);
  135. break;
  136. }
  137. set_name(p, name);
  138. free(name);
  139. break;
  140. }
  141. return 0;
  142. }
  143. static inline struct klist_node *find_name_stack(char *name) {
  144. struct klist_node *n;
  145. for (n = stack.next; n != &stack; n = n->next)
  146. if (!strcmp(n->payload, name))
  147. return n;
  148. return NULL;
  149. }
  150. static inline struct klist_node *find_name_global(char *name) {
  151. struct klist_node *n;
  152. for (n = globvars.next; n != &globvars; n = n->next)
  153. if (!strcmp(n->payload, name))
  154. return n;
  155. return NULL;
  156. }
  157. static int set_name(char *p, char *name) {
  158. int type, out;
  159. struct klist_node *n;
  160. /* find the name,
  161. * first find in stack */
  162. if ((n = find_name_stack(name))) {
  163. if (n->xmask & 4)
  164. exit(generic_error("variable sets attribute 'read-only'", lbsave, lno));
  165. if (!getword(p, &type, &out))
  166. exit(generic_error("no equal", lbsave, lno));
  167. if (out != '=')
  168. exit(generic_error("not a '='", lbsave, lno));
  169. p++;
  170. _skip_spaces(p);
  171. if (!getword(p, &type, &out))
  172. exit(generic_error("no value", lbsave, lno));
  173. printf(wr_stkvar, out, n->value << 3);
  174. return 0;
  175. }
  176. /* global variables */
  177. if (name[0] != '&')
  178. /* we need a '&' for global variables */
  179. exit(generic_error("name not found (did you forget '&')", lbsave, lno));
  180. if (!find_name_global(name + 1))
  181. exit(generic_error("name not found", lbsave, lno));
  182. _skip_word(p);
  183. _skip_spaces(p);
  184. if (!getword(p, &type, &out))
  185. exit(generic_error("no equal", lbsave, lno));
  186. if (out != '=')
  187. exit(generic_error("not a '='", lbsave, lno));
  188. p++;
  189. _skip_spaces(p);
  190. if (!getword(p, &type, &out))
  191. exit(generic_error("no value", lbsave, lno));
  192. printf(wr_glbvar, out, name + 1);
  193. return 0;
  194. }
  195. int do_funcall(char *p) {
  196. struct klist_node *stknr;
  197. int out, type, n;
  198. char *funcname, *arg;
  199. getword(p, &type, &out);
  200. if (type != WORDTYPE_NAME)
  201. exit(generic_error("a function name required", lbsave, lno));
  202. funcname = strndup(p, out);
  203. p += out;
  204. _skip_spaces(p);
  205. if (*p == '(') {
  206. p++;
  207. _skip_spaces(p);
  208. if (*p == ')') {
  209. printf(noarg_funcall, funcname);
  210. return 0;
  211. }
  212. }
  213. if (*p == ';') {
  214. printf(noarg_funcall, funcname);
  215. return 0;
  216. }
  217. _skip_spaces(p);
  218. /* get all arguments and call them */
  219. while (getword(p, &type, &out)) {
  220. switch (type) {
  221. case WORDTYPE_STR:
  222. arg = strndup(p + 1, out);
  223. printf("string: %s\n", arg);
  224. break;
  225. case WORDTYPE_NAME:
  226. arg = strndup(p, out);
  227. stknr = find_name_stack(arg);
  228. if (!stknr) {
  229. printf("global variable\n");
  230. break;
  231. }
  232. out = stknr->value;
  233. /* fall through */
  234. case WORDTYPE_NR:
  235. printf("number %d\n", out);
  236. break;
  237. case WORDTYPE_KWORD:
  238. if (out != ',' && out != '(' && out != ')')
  239. exit(generic_error("unknow keyword after arguments", lbsave, lno));
  240. if (out == '(' && n)
  241. exit(generic_error("invalid '(' after arguments", lbsave, lno));
  242. if (out == ')') {
  243. p++;
  244. _skip_spaces(p);
  245. getword(p, &type, &out);
  246. if (type)
  247. exit(generic_error("arguments after ')'", lbsave, lno));
  248. goto ok;
  249. }
  250. break;
  251. default:
  252. exit(generic_error("error type passing argument", lbsave, lno));
  253. }
  254. if (type == WORDTYPE_STR)
  255. p += out + 2;
  256. else if (type == WORDTYPE_KWORD) {
  257. p++;
  258. _skip_spaces(p);
  259. }
  260. else
  261. _skip_word(p);
  262. free(arg);
  263. arg = NULL;
  264. n++;
  265. }
  266. ok: free(funcname);
  267. return 0;
  268. }