x86_64_math.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* tripl/arch/x86_64/x86_64_math.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. /* parse a math-expression, with no prio
  19. * i'll write down a '****' when i feel angry */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <tripl/tripl.h>
  23. #include <tripl/arch.h>
  24. #include <tripl/limits.h>
  25. #include <klist.h>
  26. extern char *psave;
  27. extern int lno;
  28. extern struct klist_node *find_name(char *);
  29. __ll_asm const static char do_op_nr[] = {
  30. " %sq $%d, %%r12\n"
  31. };
  32. __ll_asm const static char do_op_glbvar[] = {
  33. " %sq %s(%%rip), %%r12\n"
  34. };
  35. __ll_asm const static char do_op_stkvar[] = {
  36. " %sq -%ld(%%rbp), %%r12\n"
  37. };
  38. __ll_asm const static char store_res_to_stkvar[] = {
  39. " movq %%r12, -%ld(%%rbp)\n"
  40. };
  41. __ll_asm const static char store_res_to_glbvar[] = {
  42. " movq %%r12, %s(%%rip)\n"
  43. };
  44. __ll_asm const static char store_r12_directly[] = {
  45. " movq $%ld, %%r12\n"
  46. };
  47. static int store_res(char *name) {
  48. struct klist_node *n = find_name(name);
  49. if (unlikely(!n))
  50. exit(generic_error("name not found", psave, lno));
  51. if (unlikely(n->xmask & 4))
  52. exit(generic_error("variable sets to be 'read-only'", psave, lno));
  53. if (n->xmask & 1)
  54. printf(store_res_to_glbvar, name);
  55. else
  56. printf(store_res_to_stkvar, n->value << 3);
  57. return 0;
  58. }
  59. static int operation_var(char *opr, char *name) {
  60. struct klist_node *n = find_name(name);
  61. if (unlikely(!n))
  62. exit(generic_error("name not found", psave, lno));
  63. if (n->xmask & 1)
  64. printf(do_op_glbvar, opr, name);
  65. else
  66. printf(do_op_stkvar, opr, n->value << 3);
  67. return 0;
  68. }
  69. #define operation_nr(opr, nr) printf(do_op_nr, opr, nr)
  70. int x86_64_math_expr(char *p, char *var) {
  71. int type, out;
  72. int op, i;
  73. static char *operations[] = {"add", "sub"};
  74. char *opstr;
  75. p++;
  76. _skip_spaces(p);
  77. /* no any prio supported */
  78. for (i = 0; getword(p, &type, &out); i++) {
  79. if (unlikely(!i)) {
  80. /* the first number is the initialize value of r12 */
  81. printf(store_r12_directly, (long) out);
  82. _skip_word(p);
  83. _skip_spaces(p);
  84. continue;
  85. }
  86. /* operator */
  87. op = out;
  88. switch (op) {
  89. case '+':
  90. opstr = operations[0];
  91. break;
  92. case '-':
  93. opstr = operations[1];
  94. break;
  95. default:
  96. exit(generic_error("invalid operator", psave, lno));
  97. }
  98. p++;
  99. _skip_spaces(p);
  100. getword(p, &type, &out);
  101. /* number or name, add or sub */
  102. if (type == WORDTYPE_NR) {
  103. operation_nr(opstr, out);
  104. _skip_word(p);
  105. }
  106. else if (type == WORDTYPE_NAME) {
  107. char *name = strndup(p, out);
  108. operation_var(opstr, name);
  109. free(name);
  110. p += out;
  111. }
  112. else
  113. exit(generic_error("invalid token in math expression", psave, lno));
  114. _skip_spaces(p);
  115. }
  116. store_res(var);
  117. return 0;
  118. }