asm.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 = KLIST_NODE_INIT(&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(&globvars);
  72. printf(asm_head, fname);
  73. return 0;
  74. }
  75. static inline struct klist_node *__find_in(struct klist_node *head, char *name) {
  76. struct klist_node *n;
  77. for (n = head->next; n != head; n = n->next)
  78. if (!strcmp(n->payload, name))
  79. return n;
  80. return NULL;
  81. }
  82. static struct klist_node *find_name(char *name) {
  83. struct klist_node *res;
  84. if (!(res = __find_in(&stack, name)))
  85. if (!(res = __find_in(&globvars, name)))
  86. return NULL;
  87. return res;
  88. }
  89. __init int x86_64_asm_fin(void) {
  90. printf(funcend);
  91. return 0;
  92. }
  93. __arch int x86_64_new_var(char *name, long value, int attr) {
  94. if (attr & 1) {
  95. /* global variable */
  96. printf(global_var, name, name, value);
  97. __push_nr(&globvars, value, name, attr);
  98. return 0;
  99. }
  100. /* local variable */
  101. printf(stack_var, value, current_private->stkindex << 3);
  102. push_nr(current_private->stkindex, name, attr);
  103. current_private->stkindex++;
  104. return 0;
  105. }
  106. __arch int x86_64_set_var(char *name, long value, int attr) {
  107. struct klist_node *n;
  108. if (unlikely(!(n = find_name(name))))
  109. exit(generic_error("name not found", psave, lno));
  110. if (unlikely(n->xmask & 4))
  111. exit(generic_error("variable sets to be 'read-only'", psave, lno));
  112. if (n->xmask & 1)
  113. printf(wr_glbvar, value, name);
  114. else
  115. printf(wr_stkvar, value, n->value << 3);
  116. return 0;
  117. }