vm-engine.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* Copyright (C) 2001, 2009, 2010, 2011 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. /* This file is included in vm_engine.c */
  19. /*
  20. * Registers
  21. */
  22. /* Register optimization. [ stolen from librep/src/lispmach.h,v 1.3 ]
  23. Some compilers underestimate the use of the local variables representing
  24. the abstract machine registers, and don't put them in hardware registers,
  25. which slows down the interpreter considerably.
  26. For GCC, I have hand-assigned hardware registers for several architectures.
  27. */
  28. #ifdef __GNUC__
  29. #ifdef __mips__
  30. #define IP_REG asm("$16")
  31. #define SP_REG asm("$17")
  32. #define FP_REG asm("$18")
  33. #endif
  34. #ifdef __sparc__
  35. #define IP_REG asm("%l0")
  36. #define SP_REG asm("%l1")
  37. #define FP_REG asm("%l2")
  38. #endif
  39. #ifdef __alpha__
  40. #ifdef __CRAY__
  41. #define IP_REG asm("r9")
  42. #define SP_REG asm("r10")
  43. #define FP_REG asm("r11")
  44. #else
  45. #define IP_REG asm("$9")
  46. #define SP_REG asm("$10")
  47. #define FP_REG asm("$11")
  48. #endif
  49. #endif
  50. #ifdef __i386__
  51. /* too few registers! because of register allocation errors with various gcs,
  52. just punt on explicit assignments on i386, hoping that the "register"
  53. declaration will be sufficient. */
  54. #endif
  55. #if defined(PPC) || defined(_POWER) || defined(_IBMR2)
  56. #define IP_REG asm("26")
  57. #define SP_REG asm("27")
  58. #define FP_REG asm("28")
  59. #endif
  60. #ifdef __hppa__
  61. #define IP_REG asm("%r18")
  62. #define SP_REG asm("%r17")
  63. #define FP_REG asm("%r16")
  64. #endif
  65. #ifdef __mc68000__
  66. #define IP_REG asm("a5")
  67. #define SP_REG asm("a4")
  68. #define FP_REG
  69. #endif
  70. #ifdef __arm__
  71. #define IP_REG asm("r9")
  72. #define SP_REG asm("r8")
  73. #define FP_REG asm("r7")
  74. #endif
  75. #endif
  76. #ifndef IP_REG
  77. #define IP_REG
  78. #endif
  79. #ifndef SP_REG
  80. #define SP_REG
  81. #endif
  82. #ifndef FP_REG
  83. #define FP_REG
  84. #endif
  85. /*
  86. * Cache/Sync
  87. */
  88. #ifdef VM_ENABLE_ASSERTIONS
  89. # define ASSERT(condition) if (SCM_UNLIKELY (!(condition))) abort()
  90. #else
  91. # define ASSERT(condition)
  92. #endif
  93. /* Cache the VM's instruction, stack, and frame pointer in local variables. */
  94. #define CACHE_REGISTER() \
  95. { \
  96. ip = vp->ip; \
  97. sp = vp->sp; \
  98. fp = vp->fp; \
  99. }
  100. /* Update the registers in VP, a pointer to the current VM. This must be done
  101. at least before any GC invocation so that `vp->sp' is up-to-date and the
  102. whole stack gets marked. */
  103. #define SYNC_REGISTER() \
  104. { \
  105. vp->ip = ip; \
  106. vp->sp = sp; \
  107. vp->fp = fp; \
  108. }
  109. /* FIXME */
  110. #define ASSERT_VARIABLE(x) \
  111. do { if (!SCM_VARIABLEP (x)) { SYNC_REGISTER (); abort(); } \
  112. } while (0)
  113. #define ASSERT_BOUND_VARIABLE(x) \
  114. do { ASSERT_VARIABLE (x); \
  115. if (scm_is_eq (SCM_VARIABLE_REF (x), SCM_UNDEFINED)) \
  116. { SYNC_REGISTER (); abort(); } \
  117. } while (0)
  118. #ifdef VM_ENABLE_PARANOID_ASSERTIONS
  119. #define CHECK_IP() \
  120. do { if (ip < bp->base || ip - bp->base > bp->len) abort (); } while (0)
  121. #define ASSERT_ALIGNED_PROCEDURE() \
  122. do { if ((scm_t_bits)bp % 8) abort (); } while (0)
  123. #define ASSERT_BOUND(x) \
  124. do { if (scm_is_eq ((x), SCM_UNDEFINED)) { SYNC_REGISTER (); abort(); } \
  125. } while (0)
  126. #else
  127. #define CHECK_IP()
  128. #define ASSERT_ALIGNED_PROCEDURE()
  129. #define ASSERT_BOUND(x)
  130. #endif
  131. #if VM_CHECK_OBJECT
  132. #define SET_OBJECT_COUNT(n) object_count = n
  133. #else
  134. #define SET_OBJECT_COUNT(n) /* nop */
  135. #endif
  136. /* Cache the object table and free variables. */
  137. #define CACHE_PROGRAM() \
  138. { \
  139. if (bp != SCM_PROGRAM_DATA (program)) { \
  140. bp = SCM_PROGRAM_DATA (program); \
  141. ASSERT_ALIGNED_PROCEDURE (); \
  142. if (SCM_I_IS_VECTOR (SCM_PROGRAM_OBJTABLE (program))) { \
  143. objects = SCM_I_VECTOR_WELTS (SCM_PROGRAM_OBJTABLE (program)); \
  144. SET_OBJECT_COUNT (SCM_I_VECTOR_LENGTH (SCM_PROGRAM_OBJTABLE (program))); \
  145. } else { \
  146. objects = NULL; \
  147. SET_OBJECT_COUNT (0); \
  148. } \
  149. } \
  150. }
  151. #define SYNC_BEFORE_GC() \
  152. { \
  153. SYNC_REGISTER (); \
  154. }
  155. #define SYNC_ALL() \
  156. { \
  157. SYNC_REGISTER (); \
  158. }
  159. /*
  160. * Error check
  161. */
  162. /* Accesses to a program's object table. */
  163. #if VM_CHECK_OBJECT
  164. #define CHECK_OBJECT(_num) \
  165. do { if (SCM_UNLIKELY ((_num) >= object_count)) goto vm_error_object; } while (0)
  166. #else
  167. #define CHECK_OBJECT(_num)
  168. #endif
  169. #if VM_CHECK_FREE_VARIABLES
  170. #define CHECK_FREE_VARIABLE(_num) \
  171. do { \
  172. if (SCM_UNLIKELY ((_num) >= SCM_PROGRAM_NUM_FREE_VARIABLES (program))) \
  173. goto vm_error_free_variable; \
  174. } while (0)
  175. #else
  176. #define CHECK_FREE_VARIABLE(_num)
  177. #endif
  178. /*
  179. * Hooks
  180. */
  181. #undef RUN_HOOK
  182. #undef RUN_HOOK1
  183. #if VM_USE_HOOKS
  184. #define RUN_HOOK(h) \
  185. { \
  186. if (SCM_UNLIKELY (vp->trace_level > 0)) \
  187. { \
  188. SYNC_REGISTER (); \
  189. vm_dispatch_hook (vm, h); \
  190. } \
  191. }
  192. #define RUN_HOOK1(h, x) \
  193. { \
  194. if (SCM_UNLIKELY (vp->trace_level > 0)) \
  195. { \
  196. PUSH (x); \
  197. SYNC_REGISTER (); \
  198. vm_dispatch_hook (vm, h); \
  199. DROP(); \
  200. } \
  201. }
  202. #else
  203. #define RUN_HOOK(h)
  204. #define RUN_HOOK1(h, x)
  205. #endif
  206. #define APPLY_HOOK() \
  207. RUN_HOOK (SCM_VM_APPLY_HOOK)
  208. #define PUSH_CONTINUATION_HOOK() \
  209. RUN_HOOK (SCM_VM_PUSH_CONTINUATION_HOOK)
  210. #define POP_CONTINUATION_HOOK(n) \
  211. RUN_HOOK1 (SCM_VM_POP_CONTINUATION_HOOK, SCM_I_MAKINUM (n))
  212. #define NEXT_HOOK() \
  213. RUN_HOOK (SCM_VM_NEXT_HOOK)
  214. #define ABORT_CONTINUATION_HOOK() \
  215. RUN_HOOK (SCM_VM_ABORT_CONTINUATION_HOOK)
  216. #define RESTORE_CONTINUATION_HOOK() \
  217. RUN_HOOK (SCM_VM_RESTORE_CONTINUATION_HOOK)
  218. #define VM_HANDLE_INTERRUPTS \
  219. SCM_ASYNC_TICK_WITH_CODE (current_thread, SYNC_REGISTER ())
  220. /*
  221. * Stack operation
  222. */
  223. #ifdef VM_ENABLE_STACK_NULLING
  224. # define CHECK_STACK_LEAKN(_n) ASSERT (!sp[_n]);
  225. # define CHECK_STACK_LEAK() CHECK_STACK_LEAKN(1)
  226. # define NULLSTACK(_n) { int __x = _n; CHECK_STACK_LEAKN (_n+1); while (__x > 0) sp[__x--] = NULL; }
  227. /* If you have a nonlocal exit in a pre-wind proc while invoking a continuation
  228. inside a dynwind (phew!), the stack is fully rewound but vm_reset_stack for
  229. that continuation doesn't have a chance to run. It's not important on a
  230. semantic level, but it does mess up our stack nulling -- so this macro is to
  231. fix that. */
  232. # define NULLSTACK_FOR_NONLOCAL_EXIT() if (vp->sp > sp) NULLSTACK (vp->sp - sp);
  233. #else
  234. # define CHECK_STACK_LEAKN(_n)
  235. # define CHECK_STACK_LEAK()
  236. # define NULLSTACK(_n)
  237. # define NULLSTACK_FOR_NONLOCAL_EXIT()
  238. #endif
  239. #define CHECK_OVERFLOW() \
  240. if (SCM_UNLIKELY (sp >= stack_limit)) \
  241. goto vm_error_stack_overflow
  242. #ifdef VM_CHECK_UNDERFLOW
  243. #define CHECK_UNDERFLOW() \
  244. if (SCM_UNLIKELY (sp <= SCM_FRAME_UPPER_ADDRESS (fp))) \
  245. goto vm_error_stack_underflow
  246. #define PRE_CHECK_UNDERFLOW(N) \
  247. if (SCM_UNLIKELY (sp - N <= SCM_FRAME_UPPER_ADDRESS (fp))) \
  248. goto vm_error_stack_underflow
  249. #else
  250. #define CHECK_UNDERFLOW() /* nop */
  251. #define PRE_CHECK_UNDERFLOW(N) /* nop */
  252. #endif
  253. #define PUSH(x) do { sp++; CHECK_OVERFLOW (); *sp = x; } while (0)
  254. #define DROP() do { sp--; CHECK_UNDERFLOW (); NULLSTACK (1); } while (0)
  255. #define DROPN(_n) do { sp -= (_n); CHECK_UNDERFLOW (); NULLSTACK (_n); } while (0)
  256. #define POP(x) do { PRE_CHECK_UNDERFLOW (1); x = *sp--; NULLSTACK (1); } while (0)
  257. #define POP2(x,y) do { PRE_CHECK_UNDERFLOW (2); x = *sp--; y = *sp--; NULLSTACK (2); } while (0)
  258. #define POP3(x,y,z) do { PRE_CHECK_UNDERFLOW (3); x = *sp--; y = *sp--; z = *sp--; NULLSTACK (3); } while (0)
  259. /* A fast CONS. This has to be fast since its used, for instance, by
  260. POP_LIST when fetching a function's argument list. Note: `scm_cell' is an
  261. inlined function in Guile 1.7. Unfortunately, it calls
  262. `scm_gc_for_newcell ()' which is _not_ inlined and allocated cells on the
  263. heap. XXX */
  264. #define CONS(x,y,z) \
  265. { \
  266. SYNC_BEFORE_GC (); \
  267. x = scm_cell (SCM_UNPACK (y), SCM_UNPACK (z)); \
  268. }
  269. /* Pop the N objects on top of the stack and push a list that contains
  270. them. */
  271. #define POP_LIST(n) \
  272. do \
  273. { \
  274. int i; \
  275. SCM l = SCM_EOL, x; \
  276. for (i = n; i; i--) \
  277. { \
  278. POP (x); \
  279. CONS (l, x, l); \
  280. } \
  281. PUSH (l); \
  282. } while (0)
  283. /* The opposite: push all of the elements in L onto the list. */
  284. #define PUSH_LIST(l, NILP) \
  285. do \
  286. { \
  287. for (; scm_is_pair (l); l = SCM_CDR (l)) \
  288. PUSH (SCM_CAR (l)); \
  289. if (SCM_UNLIKELY (!NILP (l))) { \
  290. finish_args = scm_list_1 (l); \
  291. goto vm_error_improper_list; \
  292. } \
  293. } while (0)
  294. #define POP_LIST_MARK() \
  295. do { \
  296. SCM o; \
  297. SCM l = SCM_EOL; \
  298. POP (o); \
  299. while (!SCM_UNBNDP (o)) \
  300. { \
  301. CONS (l, o, l); \
  302. POP (o); \
  303. } \
  304. PUSH (l); \
  305. } while (0)
  306. #define POP_CONS_MARK() \
  307. do { \
  308. SCM o, l; \
  309. POP (l); \
  310. POP (o); \
  311. while (!SCM_UNBNDP (o)) \
  312. { \
  313. CONS (l, o, l); \
  314. POP (o); \
  315. } \
  316. PUSH (l); \
  317. } while (0)
  318. /*
  319. * Instruction operation
  320. */
  321. #define FETCH() (*ip++)
  322. #define FETCH_LENGTH(len) do { len=*ip++; len<<=8; len+=*ip++; len<<=8; len+=*ip++; } while (0)
  323. #undef NEXT_JUMP
  324. #ifdef HAVE_LABELS_AS_VALUES
  325. #define NEXT_JUMP() goto *jump_table[FETCH () & SCM_VM_INSTRUCTION_MASK]
  326. #else
  327. #define NEXT_JUMP() goto vm_start
  328. #endif
  329. #define NEXT \
  330. { \
  331. NEXT_HOOK (); \
  332. CHECK_STACK_LEAK (); \
  333. NEXT_JUMP (); \
  334. }
  335. /* See frames.h for the layout of stack frames */
  336. /* When this is called, bp points to the new program data,
  337. and the arguments are already on the stack */
  338. #define DROP_FRAME() \
  339. { \
  340. sp -= 3; \
  341. NULLSTACK (3); \
  342. CHECK_UNDERFLOW (); \
  343. }
  344. /*
  345. Local Variables:
  346. c-file-style: "gnu"
  347. End:
  348. */