vm-engine.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. #elif defined __x86_64__
  55. /* GCC 4.6 chooses %rbp for IP_REG and %rbx for SP_REG, which works
  56. well. Tell it to keep the jump table in a r12, which is
  57. callee-saved. */
  58. #define JT_REG asm ("r12")
  59. #endif
  60. #if defined(PPC) || defined(_POWER) || defined(_IBMR2)
  61. #define IP_REG asm("26")
  62. #define SP_REG asm("27")
  63. #define FP_REG asm("28")
  64. #endif
  65. #ifdef __hppa__
  66. #define IP_REG asm("%r18")
  67. #define SP_REG asm("%r17")
  68. #define FP_REG asm("%r16")
  69. #endif
  70. #ifdef __mc68000__
  71. #define IP_REG asm("a5")
  72. #define SP_REG asm("a4")
  73. #define FP_REG
  74. #endif
  75. #ifdef __arm__
  76. #define IP_REG asm("r9")
  77. #define SP_REG asm("r8")
  78. #define FP_REG asm("r7")
  79. #endif
  80. #endif
  81. #ifndef IP_REG
  82. #define IP_REG
  83. #endif
  84. #ifndef SP_REG
  85. #define SP_REG
  86. #endif
  87. #ifndef FP_REG
  88. #define FP_REG
  89. #endif
  90. #ifndef JT_REG
  91. #define JT_REG
  92. #endif
  93. /*
  94. * Cache/Sync
  95. */
  96. #ifdef VM_ENABLE_ASSERTIONS
  97. # define ASSERT(condition) if (SCM_UNLIKELY (!(condition))) abort()
  98. #else
  99. # define ASSERT(condition)
  100. #endif
  101. /* Cache the VM's instruction, stack, and frame pointer in local variables. */
  102. #define CACHE_REGISTER() \
  103. { \
  104. ip = vp->ip; \
  105. sp = vp->sp; \
  106. fp = vp->fp; \
  107. }
  108. /* Update the registers in VP, a pointer to the current VM. This must be done
  109. at least before any GC invocation so that `vp->sp' is up-to-date and the
  110. whole stack gets marked. */
  111. #define SYNC_REGISTER() \
  112. { \
  113. vp->ip = ip; \
  114. vp->sp = sp; \
  115. vp->fp = fp; \
  116. }
  117. /* FIXME */
  118. #define ASSERT_VARIABLE(x) \
  119. do { if (!SCM_VARIABLEP (x)) { SYNC_REGISTER (); abort(); } \
  120. } while (0)
  121. #define ASSERT_BOUND_VARIABLE(x) \
  122. do { ASSERT_VARIABLE (x); \
  123. if (scm_is_eq (SCM_VARIABLE_REF (x), SCM_UNDEFINED)) \
  124. { SYNC_REGISTER (); abort(); } \
  125. } while (0)
  126. #ifdef VM_ENABLE_PARANOID_ASSERTIONS
  127. #define CHECK_IP() \
  128. do { if (ip < bp->base || ip - bp->base > bp->len) abort (); } while (0)
  129. #define ASSERT_ALIGNED_PROCEDURE() \
  130. do { if ((scm_t_bits)bp % 8) abort (); } while (0)
  131. #define ASSERT_BOUND(x) \
  132. do { if (scm_is_eq ((x), SCM_UNDEFINED)) { SYNC_REGISTER (); abort(); } \
  133. } while (0)
  134. #else
  135. #define CHECK_IP()
  136. #define ASSERT_ALIGNED_PROCEDURE()
  137. #define ASSERT_BOUND(x)
  138. #endif
  139. #if VM_CHECK_OBJECT
  140. #define SET_OBJECT_COUNT(n) object_count = n
  141. #else
  142. #define SET_OBJECT_COUNT(n) /* nop */
  143. #endif
  144. /* Cache the object table and free variables. */
  145. #define CACHE_PROGRAM() \
  146. { \
  147. if (bp != SCM_PROGRAM_DATA (program)) { \
  148. bp = SCM_PROGRAM_DATA (program); \
  149. ASSERT_ALIGNED_PROCEDURE (); \
  150. if (SCM_I_IS_VECTOR (SCM_PROGRAM_OBJTABLE (program))) { \
  151. objects = SCM_I_VECTOR_WELTS (SCM_PROGRAM_OBJTABLE (program)); \
  152. SET_OBJECT_COUNT (SCM_I_VECTOR_LENGTH (SCM_PROGRAM_OBJTABLE (program))); \
  153. } else { \
  154. objects = NULL; \
  155. SET_OBJECT_COUNT (0); \
  156. } \
  157. } \
  158. }
  159. #define SYNC_BEFORE_GC() \
  160. { \
  161. SYNC_REGISTER (); \
  162. }
  163. #define SYNC_ALL() \
  164. { \
  165. SYNC_REGISTER (); \
  166. }
  167. /*
  168. * Error check
  169. */
  170. /* Accesses to a program's object table. */
  171. #if VM_CHECK_OBJECT
  172. #define CHECK_OBJECT(_num) \
  173. do { if (SCM_UNLIKELY ((_num) >= object_count)) goto vm_error_object; } while (0)
  174. #else
  175. #define CHECK_OBJECT(_num)
  176. #endif
  177. #if VM_CHECK_FREE_VARIABLES
  178. #define CHECK_FREE_VARIABLE(_num) \
  179. do { \
  180. if (SCM_UNLIKELY ((_num) >= SCM_PROGRAM_NUM_FREE_VARIABLES (program))) \
  181. goto vm_error_free_variable; \
  182. } while (0)
  183. #else
  184. #define CHECK_FREE_VARIABLE(_num)
  185. #endif
  186. /*
  187. * Hooks
  188. */
  189. #undef RUN_HOOK
  190. #undef RUN_HOOK1
  191. #if VM_USE_HOOKS
  192. #define RUN_HOOK(h) \
  193. { \
  194. if (SCM_UNLIKELY (vp->trace_level > 0)) \
  195. { \
  196. SYNC_REGISTER (); \
  197. vm_dispatch_hook (vm, h); \
  198. } \
  199. }
  200. #define RUN_HOOK1(h, x) \
  201. { \
  202. if (SCM_UNLIKELY (vp->trace_level > 0)) \
  203. { \
  204. PUSH (x); \
  205. SYNC_REGISTER (); \
  206. vm_dispatch_hook (vm, h); \
  207. DROP(); \
  208. } \
  209. }
  210. #else
  211. #define RUN_HOOK(h)
  212. #define RUN_HOOK1(h, x)
  213. #endif
  214. #define APPLY_HOOK() \
  215. RUN_HOOK (SCM_VM_APPLY_HOOK)
  216. #define PUSH_CONTINUATION_HOOK() \
  217. RUN_HOOK (SCM_VM_PUSH_CONTINUATION_HOOK)
  218. #define POP_CONTINUATION_HOOK(n) \
  219. RUN_HOOK1 (SCM_VM_POP_CONTINUATION_HOOK, SCM_I_MAKINUM (n))
  220. #define NEXT_HOOK() \
  221. RUN_HOOK (SCM_VM_NEXT_HOOK)
  222. #define ABORT_CONTINUATION_HOOK() \
  223. RUN_HOOK (SCM_VM_ABORT_CONTINUATION_HOOK)
  224. #define RESTORE_CONTINUATION_HOOK() \
  225. RUN_HOOK (SCM_VM_RESTORE_CONTINUATION_HOOK)
  226. #define VM_HANDLE_INTERRUPTS \
  227. SCM_ASYNC_TICK_WITH_CODE (current_thread, SYNC_REGISTER ())
  228. /*
  229. * Stack operation
  230. */
  231. #ifdef VM_ENABLE_STACK_NULLING
  232. # define CHECK_STACK_LEAKN(_n) ASSERT (!sp[_n]);
  233. # define CHECK_STACK_LEAK() CHECK_STACK_LEAKN(1)
  234. # define NULLSTACK(_n) { int __x = _n; CHECK_STACK_LEAKN (_n+1); while (__x > 0) sp[__x--] = NULL; }
  235. /* If you have a nonlocal exit in a pre-wind proc while invoking a continuation
  236. inside a dynwind (phew!), the stack is fully rewound but vm_reset_stack for
  237. that continuation doesn't have a chance to run. It's not important on a
  238. semantic level, but it does mess up our stack nulling -- so this macro is to
  239. fix that. */
  240. # define NULLSTACK_FOR_NONLOCAL_EXIT() if (vp->sp > sp) NULLSTACK (vp->sp - sp);
  241. #else
  242. # define CHECK_STACK_LEAKN(_n)
  243. # define CHECK_STACK_LEAK()
  244. # define NULLSTACK(_n)
  245. # define NULLSTACK_FOR_NONLOCAL_EXIT()
  246. #endif
  247. #define CHECK_OVERFLOW() \
  248. if (SCM_UNLIKELY (sp >= stack_limit)) \
  249. goto vm_error_stack_overflow
  250. #ifdef VM_CHECK_UNDERFLOW
  251. #define CHECK_UNDERFLOW() \
  252. if (SCM_UNLIKELY (sp <= SCM_FRAME_UPPER_ADDRESS (fp))) \
  253. goto vm_error_stack_underflow
  254. #define PRE_CHECK_UNDERFLOW(N) \
  255. if (SCM_UNLIKELY (sp - N <= SCM_FRAME_UPPER_ADDRESS (fp))) \
  256. goto vm_error_stack_underflow
  257. #else
  258. #define CHECK_UNDERFLOW() /* nop */
  259. #define PRE_CHECK_UNDERFLOW(N) /* nop */
  260. #endif
  261. #define PUSH(x) do { sp++; CHECK_OVERFLOW (); *sp = x; } while (0)
  262. #define DROP() do { sp--; CHECK_UNDERFLOW (); NULLSTACK (1); } while (0)
  263. #define DROPN(_n) do { sp -= (_n); CHECK_UNDERFLOW (); NULLSTACK (_n); } while (0)
  264. #define POP(x) do { PRE_CHECK_UNDERFLOW (1); x = *sp--; NULLSTACK (1); } while (0)
  265. #define POP2(x,y) do { PRE_CHECK_UNDERFLOW (2); x = *sp--; y = *sp--; NULLSTACK (2); } while (0)
  266. #define POP3(x,y,z) do { PRE_CHECK_UNDERFLOW (3); x = *sp--; y = *sp--; z = *sp--; NULLSTACK (3); } while (0)
  267. /* A fast CONS. This has to be fast since its used, for instance, by
  268. POP_LIST when fetching a function's argument list. Note: `scm_cell' is an
  269. inlined function in Guile 1.7. Unfortunately, it calls
  270. `scm_gc_for_newcell ()' which is _not_ inlined and allocated cells on the
  271. heap. XXX */
  272. #define CONS(x,y,z) \
  273. { \
  274. SYNC_BEFORE_GC (); \
  275. x = scm_cell (SCM_UNPACK (y), SCM_UNPACK (z)); \
  276. }
  277. /* Pop the N objects on top of the stack and push a list that contains
  278. them. */
  279. #define POP_LIST(n) \
  280. do \
  281. { \
  282. int i; \
  283. SCM l = SCM_EOL, x; \
  284. for (i = n; i; i--) \
  285. { \
  286. POP (x); \
  287. CONS (l, x, l); \
  288. } \
  289. PUSH (l); \
  290. } while (0)
  291. /* The opposite: push all of the elements in L onto the list. */
  292. #define PUSH_LIST(l, NILP) \
  293. do \
  294. { \
  295. for (; scm_is_pair (l); l = SCM_CDR (l)) \
  296. PUSH (SCM_CAR (l)); \
  297. if (SCM_UNLIKELY (!NILP (l))) { \
  298. finish_args = scm_list_1 (l); \
  299. goto vm_error_improper_list; \
  300. } \
  301. } while (0)
  302. #define POP_LIST_MARK() \
  303. do { \
  304. SCM o; \
  305. SCM l = SCM_EOL; \
  306. POP (o); \
  307. while (!SCM_UNBNDP (o)) \
  308. { \
  309. CONS (l, o, l); \
  310. POP (o); \
  311. } \
  312. PUSH (l); \
  313. } while (0)
  314. #define POP_CONS_MARK() \
  315. do { \
  316. SCM o, l; \
  317. POP (l); \
  318. POP (o); \
  319. while (!SCM_UNBNDP (o)) \
  320. { \
  321. CONS (l, o, l); \
  322. POP (o); \
  323. } \
  324. PUSH (l); \
  325. } while (0)
  326. /*
  327. * Instruction operation
  328. */
  329. #define FETCH() (*ip++)
  330. #define FETCH_LENGTH(len) do { len=*ip++; len<<=8; len+=*ip++; len<<=8; len+=*ip++; } while (0)
  331. #undef NEXT_JUMP
  332. #ifdef HAVE_LABELS_AS_VALUES
  333. #define NEXT_JUMP() goto *jump_table[FETCH () & SCM_VM_INSTRUCTION_MASK]
  334. #else
  335. #define NEXT_JUMP() goto vm_start
  336. #endif
  337. #define NEXT \
  338. { \
  339. NEXT_HOOK (); \
  340. CHECK_STACK_LEAK (); \
  341. NEXT_JUMP (); \
  342. }
  343. /* See frames.h for the layout of stack frames */
  344. /* When this is called, bp points to the new program data,
  345. and the arguments are already on the stack */
  346. #define DROP_FRAME() \
  347. { \
  348. sp -= 3; \
  349. NULLSTACK (3); \
  350. CHECK_UNDERFLOW (); \
  351. }
  352. /*
  353. Local Variables:
  354. c-file-style: "gnu"
  355. End:
  356. */