asm.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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: \n"
  51. " .quad %ld\n"
  52. " .text\n"
  53. };
  54. __ll_asm const static char stack_var[] = {
  55. " movq $%ld, -%d(%%rbp)\n"
  56. };
  57. __ll_asm const static char wr_stkvar[] = {
  58. " movq $%ld, -%ld(%%rbp)\n"
  59. };
  60. __ll_asm const static char wr_glbvar[] = {
  61. " movq $%ld, %s(%%rip)\n"
  62. };
  63. /*__ll_asm const static char noarg_funcall[] = {
  64. " call %s\n"
  65. };*/
  66. __ll_asm const static char funcend[] = {
  67. " popq %%rbp\n"
  68. " ret\n"
  69. };
  70. __init int x86_64_init_asm(void) {
  71. current->private_data = malloc(sizeof(struct x86_64_context));
  72. current_private->stkindex = 1;
  73. printf(asm_head, fname);
  74. return 0;
  75. }
  76. __init int x86_64_asm_fin(void) {
  77. printf(funcend);
  78. return 0;
  79. }
  80. __arch int x86_64_new_var(char *name, long value, int attr) {
  81. if (attr & 1) {
  82. /* global variable */
  83. printf(global_var, name, name, value);
  84. __push_nr(&names_list, value, name, attr);
  85. return 0;
  86. }
  87. /* local variable */
  88. printf(stack_var, value, current_private->stkindex << 3);
  89. __push_nr(&names_list, current_private->stkindex, name, attr);
  90. current_private->stkindex++;
  91. return 0;
  92. }
  93. __arch int x86_64_set_var(char *name, long value, int attr) {
  94. struct klist_node *n;
  95. if (unlikely(!(n = find_name(name))))
  96. exit(generic_error("name not found", psave, lno));
  97. if (unlikely(n->xmask & 4))
  98. exit(generic_error("variable sets to be 'read-only'", psave, lno));
  99. if (n->xmask & 1)
  100. printf(wr_glbvar, value, name);
  101. else
  102. printf(wr_stkvar, value, n->value << 3);
  103. return 0;
  104. }