asm.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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;
  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. exit(generic_error("'$' requires a number", lbsave, lno));
  100. _skip_spaces(p);
  101. _skip_word(p);
  102. _skip_spaces(p);
  103. attr = getattrmask(p, vardef_attrtab);
  104. if (attr & 1) {
  105. /* global variable */
  106. printf(global_var, name, name, out);
  107. __push_nr(&globvars, 0, name, attr);
  108. }
  109. else {
  110. /* variables on stack */
  111. printf(stack_var, out, stack_index << 3);
  112. push_nr(stack_index, name, attr);
  113. stack_index++;
  114. }
  115. break;
  116. case '@':
  117. p++;
  118. _skip_spaces(p);
  119. do_funcall(p);
  120. break;
  121. default:
  122. /* setting */
  123. getword(p, &type, &out);
  124. if (type != WORDTYPE_NAME)
  125. exit(generic_error("unknow keyword", lbsave, lno));
  126. name = strndup(p, out);
  127. set_name(p, name);
  128. free(name);
  129. break;
  130. }
  131. return 0;
  132. }
  133. static inline struct klist_node *find_name_stack(char *name) {
  134. struct klist_node *n;
  135. for (n = stack.next; n != &stack; n = n->next)
  136. if (!strcmp(n->payload, name))
  137. return n;
  138. return NULL;
  139. }
  140. static inline struct klist_node *find_name_global(char *name) {
  141. struct klist_node *n;
  142. for (n = globvars.next; n != &globvars; n = n->next)
  143. if (!strcmp(n->payload, name))
  144. return n;
  145. return NULL;
  146. }
  147. static int set_name(char *p, char *name) {
  148. int type, out;
  149. struct klist_node *n;
  150. /* find the name,
  151. * first find in stack */
  152. if ((n = find_name_stack(name))) {
  153. if (n->xmask & 4)
  154. exit(generic_error("variable sets attribute 'read-only'", lbsave, lno));
  155. _skip_word(p);
  156. _skip_spaces(p);
  157. if (!getword(p, &type, &out))
  158. exit(generic_error("no equal", lbsave, lno));
  159. if (out != '=')
  160. exit(generic_error("not a '='", lbsave, lno));
  161. p++;
  162. _skip_spaces(p);
  163. if (!getword(p, &type, &out))
  164. exit(generic_error("no value", lbsave, lno));
  165. printf(wr_stkvar, out, n->value << 3);
  166. return 0;
  167. }
  168. /* global variables */
  169. if (name[0] != '&')
  170. /* we need a '&' for global variables */
  171. exit(generic_error("name not found (did you forget '&')", lbsave, lno));
  172. if (!find_name_global(name + 1))
  173. exit(generic_error("name not found", lbsave, lno));
  174. _skip_word(p);
  175. _skip_spaces(p);
  176. if (!getword(p, &type, &out))
  177. exit(generic_error("no equal", lbsave, lno));
  178. if (out != '=')
  179. exit(generic_error("not a '='", lbsave, lno));
  180. p++;
  181. _skip_spaces(p);
  182. if (!getword(p, &type, &out))
  183. exit(generic_error("no value", lbsave, lno));
  184. printf(wr_glbvar, out, name + 1);
  185. return 0;
  186. }
  187. int do_funcall(char *p) {
  188. struct klist_node *stknr;
  189. int out, type;
  190. char *funcname, *arg;
  191. getword(p, &type, &out);
  192. if (type != WORDTYPE_NAME)
  193. exit(generic_error("a function name required", lbsave, lno));
  194. funcname = strndup(p, out);
  195. p += out;
  196. if (*p == ';') {
  197. printf(noarg_funcall, funcname);
  198. return 0;
  199. }
  200. _skip_spaces(p);
  201. /* get all arguments and call them */
  202. while (getword(p, &type, &out)) {
  203. switch (type) {
  204. case WORDTYPE_STR:
  205. arg = strndup(p + 1, out);
  206. printf("string: %s\n", arg);
  207. break;
  208. case WORDTYPE_NAME:
  209. arg = strndup(p, out);
  210. stknr = find_name_stack(arg);
  211. if (!stknr) {
  212. printf("global variable\n");
  213. break;
  214. }
  215. out = stknr->value;
  216. /* fall through */
  217. case WORDTYPE_NR:
  218. printf("number %d\n", out);
  219. break;
  220. case WORDTYPE_KWORD:
  221. if (out != ',')
  222. exit(generic_error("unknow keyword after arguments", lbsave, lno));
  223. break;
  224. default:
  225. exit(generic_error("error type passing argument", lbsave, lno));
  226. }
  227. if (type == WORDTYPE_STR)
  228. p += out + 2;
  229. else if (type == WORDTYPE_KWORD) {
  230. p++;
  231. _skip_spaces(p);
  232. }
  233. else
  234. _skip_word(p);
  235. free(arg);
  236. arg = NULL;
  237. }
  238. free(funcname);
  239. return 0;
  240. }