asm.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. __ll_asm const static char asm_head[] = {
  30. " .file \"%s\"\n"
  31. " .text\n"
  32. " .globl _start\n"
  33. "_start:\n"
  34. " /* call main and exit */\n"
  35. " call main\n"
  36. " movq %%rax, %%rbx\n"
  37. " movq $1, %%rax\n"
  38. " /* exit */\n"
  39. " int $0x80\n\n"
  40. " .globl main\n"
  41. /* use -x(%rbp) to access */
  42. "main: pushq %%rbp\n"
  43. " movq %%rsp, %%rbp\n"
  44. };
  45. __ll_asm const static char global_var[] = {
  46. " .data\n"
  47. " .globl %s\n"
  48. "%s: .quad %ld\n"
  49. " .text\n"
  50. };
  51. __ll_asm const static char stack_var[] = {
  52. " movq $%ld, -%d(%%rbp)\n"
  53. };
  54. /*__ll_asm const static char wr_stkvar[] = {
  55. " movq $%d, -%d(%%rbp)\n"
  56. };*/
  57. /*__ll_asm const static char wr_glbvar[] = {
  58. " movq $%d, %s(%%rip)\n"
  59. };*/
  60. /*__ll_asm const static char noarg_funcall[] = {
  61. " call %s\n"
  62. };*/
  63. __ll_asm const static char funcend[] = {
  64. " popq %%rbp\n"
  65. " ret\n"
  66. };
  67. __init int x86_64_init_asm(void) {
  68. current->private_data = malloc(sizeof(struct x86_64_context));
  69. current_private->stkindex = 1;
  70. klist_init(&stack);
  71. klist_init(&globvars);
  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(&globvars, value, name, attr);
  84. return 0;
  85. }
  86. /* local variable */
  87. printf(stack_var, value, current_private->stkindex << 3);
  88. push_nr(current_private->stkindex, name, attr);
  89. current_private->stkindex++;
  90. return 0;
  91. }