mloop.in 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # Simulator main loop for or1k. -*- C -*-
  2. #
  3. # Copyright (C) 2017-2022 Free Software Foundation, Inc.
  4. #
  5. # This file is part of the GNU Simulators.
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. # Syntax:
  20. # /bin/sh mainloop.in command
  21. #
  22. # Command is one of:
  23. #
  24. # init
  25. # support
  26. # extract-{simple,scache,pbb}
  27. # {full,fast}-exec-{simple,scache,pbb}
  28. #
  29. # A target need only provide a "full" version of one of simple,scache,pbb.
  30. # If the target wants it can also provide a fast version of same, or if
  31. # the slow (full featured) version is `simple', then the fast version can be
  32. # one of scache/pbb.
  33. # A target can't provide more than this.
  34. # However for illustration's sake this file provides examples of all.
  35. # ??? After a few more ports are done, revisit.
  36. # Will eventually need to machine generate a lot of this.
  37. case "x$1" in
  38. xsupport)
  39. cat <<EOF
  40. #include <stdlib.h>
  41. static INLINE const IDESC *
  42. extract (SIM_CPU *current_cpu, PCADDR pc, CGEN_INSN_INT insn,
  43. ARGBUF *abuf, int fast_p)
  44. {
  45. const IDESC *id = @cpu@_decode (current_cpu, pc, insn, insn, abuf);
  46. @cpu@_fill_argbuf (current_cpu, abuf, id, pc, fast_p);
  47. if (!fast_p)
  48. {
  49. int trace_p = PC_IN_TRACE_RANGE_P (current_cpu, pc);
  50. int profile_p = PC_IN_PROFILE_RANGE_P (current_cpu, pc);
  51. @cpu@_fill_argbuf_tp (current_cpu, abuf, trace_p, profile_p);
  52. }
  53. return id;
  54. }
  55. static INLINE SEM_PC
  56. execute (SIM_CPU *current_cpu, SCACHE *sc, int fast_p)
  57. {
  58. SEM_PC vpc = sc;
  59. @cpu@_insn_before (current_cpu, vpc, sc->argbuf.idesc);
  60. if (fast_p)
  61. {
  62. #if ! WITH_SEM_SWITCH_FAST
  63. #if WITH_SCACHE
  64. vpc = (*sc->argbuf.semantic.sem_fast) (current_cpu, sc);
  65. #else
  66. vpc = (*sc->argbuf.semantic.sem_fast) (current_cpu, &sc->argbuf);
  67. #endif
  68. #else
  69. abort ();
  70. #endif /* WITH_SEM_SWITCH_FAST */
  71. }
  72. else
  73. {
  74. #if ! WITH_SEM_SWITCH_FULL
  75. ARGBUF *abuf = &sc->argbuf;
  76. const IDESC *idesc = abuf->idesc;
  77. const CGEN_INSN *idata = idesc->idata;
  78. #if WITH_SCACHE_PBB
  79. int virtual_p = CGEN_INSN_ATTR_VALUE (idata, CGEN_INSN_VIRTUAL);
  80. #else
  81. int virtual_p = 0;
  82. #endif
  83. if (! virtual_p)
  84. {
  85. /* FIXME: call x-before */
  86. if (ARGBUF_PROFILE_P (abuf))
  87. PROFILE_COUNT_INSN (current_cpu, abuf->addr, idesc->num);
  88. /* FIXME: Later make cover macros: PROFILE_INSN_{INIT,FINI}. */
  89. if (PROFILE_MODEL_P (current_cpu)
  90. && ARGBUF_PROFILE_P (abuf))
  91. @cpu@_model_insn_before (current_cpu, 1 /*first_p*/);
  92. CGEN_TRACE_INSN_INIT (current_cpu, abuf, 1);
  93. CGEN_TRACE_INSN (current_cpu, idata,
  94. (const struct argbuf *) abuf, abuf->addr);
  95. }
  96. #if WITH_SCACHE
  97. vpc = (*sc->argbuf.semantic.sem_full) (current_cpu, sc);
  98. #else
  99. vpc = (*sc->argbuf.semantic.sem_full) (current_cpu, abuf);
  100. #endif
  101. if (! virtual_p)
  102. {
  103. /* FIXME: call x-after */
  104. if (PROFILE_MODEL_P (current_cpu)
  105. && ARGBUF_PROFILE_P (abuf))
  106. {
  107. int cycles;
  108. cycles = (*idesc->timing->model_fn) (current_cpu, sc);
  109. @cpu@_model_insn_after (current_cpu, 1 /*last_p*/, cycles);
  110. }
  111. CGEN_TRACE_INSN_FINI (current_cpu, abuf, 1);
  112. }
  113. #else
  114. abort ();
  115. #endif /* WITH_SEM_SWITCH_FULL */
  116. }
  117. @cpu@_insn_after (current_cpu, vpc, sc->argbuf.idesc);
  118. return vpc;
  119. }
  120. EOF
  121. ;;
  122. xinit)
  123. # Nothing needed.
  124. ;;
  125. xextract-simple | xextract-scache)
  126. cat <<EOF
  127. {
  128. USI insn = GETIMEMUSI (current_cpu, pc);
  129. extract (current_cpu, pc, insn, sc, FAST_P);
  130. SEM_SKIP_COMPILE (current_cpu, sc, 1);
  131. }
  132. EOF
  133. ;;
  134. xextract-pbb)
  135. # Inputs: current_cpu, pc, sc, max_insns, FAST_P
  136. # Outputs: sc, pc
  137. # sc must be left pointing past the last created entry.
  138. # pc must be left pointing past the last created entry.
  139. # If the pbb is terminated by a cti insn, SET_CTI_VPC(sc) must be called
  140. # to record the vpc of the cti insn.
  141. # SET_INSN_COUNT(n) must be called to record number of real insns.
  142. cat <<EOF
  143. {
  144. const IDESC *idesc;
  145. int icount = 0;
  146. while (max_insns > 0) {
  147. USI insn = GETIMEMUSI (current_cpu, pc);
  148. idesc = extract (current_cpu, pc, insn, &sc->argbuf, FAST_P);
  149. SEM_SKIP_COMPILE (current_cpu, sc, 1);
  150. ++sc;
  151. --max_insns;
  152. ++icount;
  153. pc += 4;
  154. if (CGEN_ATTR_BOOLS (CGEN_INSN_ATTRS ((idesc)->idata)) & CGEN_ATTR_MASK (CGEN_INSN_FORCED_CTI))
  155. {
  156. SET_CTI_VPC (sc - 1);
  157. break;
  158. }
  159. else if (CGEN_ATTR_BOOLS (CGEN_INSN_ATTRS ((idesc)->idata)) & CGEN_ATTR_MASK (CGEN_INSN_DELAYED_CTI))
  160. {
  161. /* handle delay slot */
  162. SET_CTI_VPC (sc - 1);
  163. insn = GETIMEMUSI (current_cpu, pc);
  164. idesc = extract (current_cpu, pc, insn, &sc->argbuf, FAST_P);
  165. ++sc;
  166. --max_insns;
  167. ++icount;
  168. pc += 4;
  169. break;
  170. }
  171. }
  172. SET_INSN_COUNT (icount);
  173. }
  174. EOF
  175. ;;
  176. xfull-exec-* | xfast-exec-*)
  177. # Inputs: current_cpu, vpc, FAST_P
  178. # Outputs: vpc
  179. # vpc is the virtual program counter.
  180. cat <<EOF
  181. #if (! FAST_P && WITH_SEM_SWITCH_FULL) || (FAST_P && WITH_SEM_SWITCH_FAST)
  182. #define DEFINE_SWITCH
  183. #ifdef WANT_CPU_OR1K32BF
  184. #include "sem-switch.c"
  185. #endif
  186. #else
  187. vpc = execute (current_cpu, vpc, FAST_P);
  188. #endif
  189. EOF
  190. ;;
  191. *)
  192. echo "Invalid argument to mainloop.in: $1" >&2
  193. exit 1
  194. ;;
  195. esac