asm.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. static int do_funcall(char *p);
  27. static int set_name(char *p, char *name);
  28. const static char asm_head[] = {
  29. " .file \"%s\"\n"
  30. " .text\n"
  31. " .globl _start\n"
  32. "_start:\n"
  33. " /* call main and exit */\n"
  34. " call main\n"
  35. " movq %%rax, %%rbx\n"
  36. " movq $1, %%rax\n"
  37. " /* exit */\n"
  38. " int $0x80\n\n"
  39. " .globl main\n"
  40. /* use -x(%rbp) to access */
  41. "main: pushq %%rbp\n"
  42. " movq %%rsp, %%rbp\n"
  43. };
  44. const static char global_var[] = {
  45. " .data\n"
  46. " .globl %s\n"
  47. "%s: .long %d\n"
  48. " .text\n"
  49. };
  50. const static char stack_var[] = {
  51. " movq $%d, -%d(%%rbp)\n"
  52. };
  53. const static char wr_stkvar[] = {
  54. " movq $%d, -%d(%%rbp)\n"
  55. };
  56. const static char wr_glbvar[] = {
  57. " movq $%d, %s(%%rip)\n"
  58. };
  59. const static char noarg_funcall[] = {
  60. " call %s\n"
  61. };
  62. const static char funcend[] = {
  63. " popq %%rbp\n"
  64. " ret\n"
  65. };
  66. int x86_64_init_asm(void) {
  67. stack_index = 0;
  68. printf(asm_head, fname);
  69. return 0;
  70. }
  71. int x86_64_asm_fin(void) {
  72. printf(funcend);
  73. return 0;
  74. }
  75. int x86_64_do_asm(char *p, char c) {
  76. char *name = NULL;
  77. int out, type, attr;
  78. lbsave = p;
  79. switch (c) {
  80. case '$':
  81. ++p;
  82. _skip_spaces(p);
  83. getword(p, &type, &out);
  84. if (type != WORDTYPE_NAME)
  85. exit(generic_error("'$' requires a name", lbsave, lno));
  86. name = strndup(p, out);
  87. p += out;
  88. getword(p, &type, &out);
  89. if (type != WORDTYPE_NR)
  90. exit(generic_error("'$' requires a number", lbsave, lno));
  91. _skip_spaces(p);
  92. _skip_word(p);
  93. _skip_spaces(p);
  94. attr = getattrmask(p, vardef_attrtab);
  95. if (attr & 1)
  96. /* global variable */
  97. printf(global_var, name, name, out);
  98. else {
  99. /* variables on stack */
  100. printf(stack_var, out, stack_index << 3);
  101. push_nr(stack_index, name, attr);
  102. stack_index++;
  103. }
  104. break;
  105. case '@':
  106. p++;
  107. _skip_spaces(p);
  108. do_funcall(p);
  109. break;
  110. default:
  111. /* setting */
  112. getword(p, &type, &out);
  113. if (type != WORDTYPE_NAME)
  114. exit(generic_error("unknow keyword", lbsave, lno));
  115. name = strndup(p, out);
  116. set_name(p, name);
  117. free(name);
  118. break;
  119. }
  120. return 0;
  121. }
  122. static inline struct klist_node *find_name_stack(char *name) {
  123. struct klist_node *n;
  124. for (n = stack.next; n != &stack; n = n->next)
  125. if (!strcmp(n->payload, name))
  126. return n;
  127. return NULL;
  128. }
  129. static int set_name(char *p, char *name) {
  130. int type, out;
  131. struct klist_node *n;
  132. /* find the name,
  133. * first find in stack */
  134. if ((n = find_name_stack(name))) {
  135. if (n->xmask & 4)
  136. exit(generic_error("variable sets attribute 'read-only'", lbsave, lno));
  137. _skip_word(p);
  138. _skip_spaces(p);
  139. if (!getword(p, &type, &out))
  140. exit(generic_error("no equal", lbsave, lno));
  141. if (out != '=')
  142. exit(generic_error("not a '='", lbsave, lno));
  143. p++;
  144. _skip_spaces(p);
  145. if (!getword(p, &type, &out))
  146. exit(generic_error("no value", lbsave, lno));
  147. printf(wr_stkvar, out, n->value << 3);
  148. return 0;
  149. }
  150. /* global variables */
  151. if (name[0] != '&')
  152. /* we need a '&' for global variables */
  153. exit(generic_error("name not found (did you forget '&')", lbsave, lno));
  154. _skip_word(p);
  155. _skip_spaces(p);
  156. if (!getword(p, &type, &out))
  157. exit(generic_error("no equal", lbsave, lno));
  158. if (out != '=')
  159. exit(generic_error("not a '='", lbsave, lno));
  160. p++;
  161. _skip_spaces(p);
  162. if (!getword(p, &type, &out))
  163. exit(generic_error("no value", lbsave, lno));
  164. printf(wr_glbvar, out, name + 1);
  165. return 0;
  166. }
  167. int do_funcall(char *p) {
  168. // struct arg_entry *args, *next_arg;
  169. int out, type;
  170. char *funcname;
  171. getword(p, &type, &out);
  172. if (type != WORDTYPE_NAME)
  173. exit(generic_error("a function name required", lbsave, lno));
  174. // args = malloc(sizeof(struct arg_entry));
  175. // next_arg = args;
  176. funcname = strndup(p, out);
  177. p += out;
  178. if (*p == ';') {
  179. printf(noarg_funcall, funcname);
  180. return 0;
  181. }
  182. free(funcname);
  183. return 0;
  184. }