asm.c 2.9 KB

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