asm.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. struct x86_64_context {
  25. int stkindex;
  26. };
  27. #define current_private ((struct x86_64_context *) current->private_data)
  28. struct klist_node globvars;
  29. extern char *psave;
  30. __ll_asm const static char asm_head[] = {
  31. " .file \"%s\"\n"
  32. " .text\n"
  33. " .globl _start\n"
  34. "_start:\n"
  35. " /* call main and exit */\n"
  36. " call main\n"
  37. " movq %%rax, %%rbx\n"
  38. " movq $1, %%rax\n"
  39. " /* exit */\n"
  40. " int $0x80\n\n"
  41. " .globl main\n"
  42. /* use -x(%rbp) to access */
  43. "main: pushq %%rbp\n"
  44. " movq %%rsp, %%rbp\n"
  45. };
  46. __ll_asm const static char global_var[] = {
  47. " .data\n"
  48. " .globl %s\n"
  49. "%s: .quad %ld\n"
  50. " .text\n"
  51. };
  52. __ll_asm const static char stack_var[] = {
  53. " movq $%ld, -%d(%%rbp)\n"
  54. };
  55. __ll_asm const static char wr_stkvar[] = {
  56. " movq $%ld, -%ld(%%rbp)\n"
  57. };
  58. __ll_asm const static char wr_glbvar[] = {
  59. " movq $%ld, %s(%%rip)\n"
  60. };
  61. /*__ll_asm const static char noarg_funcall[] = {
  62. " call %s\n"
  63. };*/
  64. __ll_asm const static char funcend[] = {
  65. " popq %%rbp\n"
  66. " ret\n"
  67. };
  68. __init int x86_64_init_asm(void) {
  69. current->private_data = malloc(sizeof(struct x86_64_context));
  70. current_private->stkindex = 1;
  71. klist_init(&stack);
  72. klist_init(&globvars);
  73. printf(asm_head, fname);
  74. return 0;
  75. }
  76. static inline struct klist_node *__find_in(struct klist_node *head, char *name) {
  77. struct klist_node *n;
  78. for (n = head->next; n != head; n = n->next)
  79. if (!strcmp(n->payload, name))
  80. return n;
  81. return NULL;
  82. }
  83. static struct klist_node *find_name(char *name) {
  84. struct klist_node *res;
  85. if (!(res = __find_in(&stack, name)))
  86. if (!(res = __find_in(&globvars, name)))
  87. return NULL;
  88. return res;
  89. }
  90. __init int x86_64_asm_fin(void) {
  91. printf(funcend);
  92. return 0;
  93. }
  94. __arch int x86_64_new_var(char *name, long value, int attr) {
  95. if (attr & 1) {
  96. /* global variable */
  97. printf(global_var, name, name, value);
  98. __push_nr(&globvars, value, name, attr);
  99. return 0;
  100. }
  101. /* local variable */
  102. printf(stack_var, value, current_private->stkindex << 3);
  103. push_nr(current_private->stkindex, name, attr);
  104. current_private->stkindex++;
  105. return 0;
  106. }
  107. __arch int x86_64_set_var(char *name, long value, int attr) {
  108. struct klist_node *n;
  109. if (!(n = find_name(name)))
  110. exit(generic_error("name not found", psave, lno));
  111. if (n->xmask & 4)
  112. exit(generic_error("variable sets to be 'read-only'", psave, lno));
  113. if (n->xmask & 1)
  114. printf(wr_glbvar, value, name);
  115. else
  116. printf(wr_stkvar, value, n->value << 3);
  117. return 0;
  118. }