arch_funcall.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* tripl/arch/interp/arch_funcall.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 <tripl/tripl.h>
  19. #include <tripl/limits.h>
  20. /* danger as your own risk */
  21. int arch_funcall(void *p, struct arg_entry *args, int nargs) {
  22. register long result __asm__ ("ax");
  23. struct arg_entry *arg = args;
  24. int i;
  25. unsigned long arg_int[6] = { 0, 0, 0, 0, 0, 0 };
  26. unsigned long *args_linear = malloc((nargs - 6) << 3);
  27. for (i = 0; i < 6; i++) {
  28. if (!arg)
  29. break;
  30. arg_int[i] = arg->arg_s ? (unsigned long) arg->arg_s :
  31. (unsigned long) arg->arg_v;
  32. arg = arg->next;
  33. }
  34. for (i = 0; i < nargs - 6; i++) {
  35. if (!arg)
  36. break;
  37. args_linear[i] = arg->arg_s ? (unsigned long) arg->arg_s :
  38. (unsigned long) arg->arg_v;
  39. arg = arg->next;
  40. }
  41. /*
  42. * preparing arguments:
  43. * rdi rsi rdx rcx r8 r9 stack
  44. * */
  45. /* __asm__ volatile (
  46. "cmpq $0, %%rdi\n\t"
  47. "je 2f\n\t"
  48. "addq %2, %%rsp\n\t"
  49. "1:"
  50. "movq %1, %%rax\n\t"
  51. "movq (%%rdi, %%rax, 8), %%rcx\n\t"
  52. "pushq %%rcx\n\t"
  53. "subq $1, %%rax\n\t"
  54. "cmpq $0, %%rax\n\t"
  55. "jb 1b\n\t"
  56. : "=D" (args_linear) : "r" ((long) nargs - 6), "r" ((long) (nargs - 6) << 3)
  57. : "ax", "cx"
  58. );*/
  59. __asm__ volatile (
  60. "2:"
  61. "movq %1, %%rdi\n\t"
  62. "movq %2, %%rsi\n\t"
  63. "movq %3, %%rdx\n\t"
  64. "movq %4, %%rcx\n\t"
  65. "movq %5, %%r8\n\t"
  66. "movq %6, %%r9\n\t"
  67. /* real calling: push returning address first and then
  68. * function address */
  69. "leaq 1f(%%rip), %%rax\n\t"
  70. "pushq %%rax\n\t"
  71. "pushq %0\n\t"
  72. "retq\n\t"
  73. "1:"
  74. :: "m" (p),
  75. "m" (arg_int[0]), "m" (arg_int[1]), "m" (arg_int[2]), "m" (arg_int[3]),
  76. "m" (arg_int[4]), "m" (arg_int[5])
  77. : "ax"
  78. );
  79. return result;
  80. }