PageRenderTime 110ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 2ms

/erts/emulator/beam/beam_emu.c

https://github.com/mortior/otp
C | 6388 lines | 4735 code | 676 blank | 977 comment | 908 complexity | 4997f2060c17a19d8c40913d9c3c4f1f MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-2-Clause
  1. /*
  2. * %CopyrightBegin%
  3. *
  4. * Copyright Ericsson AB 1996-2010. All Rights Reserved.
  5. *
  6. * The contents of this file are subject to the Erlang Public License,
  7. * Version 1.1, (the "License"); you may not use this file except in
  8. * compliance with the License. You should have received a copy of the
  9. * Erlang Public License along with this software. If not, it can be
  10. * retrieved online at http://www.erlang.org/.
  11. *
  12. * Software distributed under the License is distributed on an "AS IS"
  13. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  14. * the License for the specific language governing rights and limitations
  15. * under the License.
  16. *
  17. * %CopyrightEnd%
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include "config.h"
  21. #endif
  22. #include <stddef.h> /* offsetof() */
  23. #include "sys.h"
  24. #include "erl_vm.h"
  25. #include "global.h"
  26. #include "erl_process.h"
  27. #include "erl_nmgc.h"
  28. #include "error.h"
  29. #include "bif.h"
  30. #include "big.h"
  31. #include "beam_load.h"
  32. #include "erl_binary.h"
  33. #include "erl_bits.h"
  34. #include "dist.h"
  35. #include "beam_bp.h"
  36. #include "beam_catches.h"
  37. #ifdef HIPE
  38. #include "hipe_mode_switch.h"
  39. #include "hipe_bif1.h"
  40. #endif
  41. /* #define HARDDEBUG 1 */
  42. #if defined(NO_JUMP_TABLE)
  43. # define OpCase(OpCode) case op_##OpCode: lb_##OpCode
  44. # define CountCase(OpCode) case op_count_##OpCode
  45. # define OpCode(OpCode) ((Uint*)op_##OpCode)
  46. # define Goto(Rel) {Go = (int)(Rel); goto emulator_loop;}
  47. # define LabelAddr(Addr) &&##Addr
  48. #else
  49. # define OpCase(OpCode) lb_##OpCode
  50. # define CountCase(OpCode) lb_count_##OpCode
  51. # define Goto(Rel) goto *(Rel)
  52. # define LabelAddr(Label) &&Label
  53. # define OpCode(OpCode) (&&lb_##OpCode)
  54. #endif
  55. #ifdef ERTS_ENABLE_LOCK_CHECK
  56. # ifdef ERTS_SMP
  57. # define PROCESS_MAIN_CHK_LOCKS(P) \
  58. do { \
  59. if ((P)) { \
  60. erts_pix_lock_t *pix_lock__ = ERTS_PIX2PIXLOCK(internal_pid_index((P)->id));\
  61. erts_proc_lc_chk_only_proc_main((P)); \
  62. erts_pix_lock(pix_lock__); \
  63. ASSERT(0 < (P)->lock.refc && (P)->lock.refc < erts_no_schedulers*5);\
  64. erts_pix_unlock(pix_lock__); \
  65. } \
  66. else \
  67. erts_lc_check_exact(NULL, 0); \
  68. ERTS_SMP_LC_ASSERT(!ERTS_LC_IS_BLOCKING); \
  69. } while (0)
  70. # define ERTS_SMP_REQ_PROC_MAIN_LOCK(P) \
  71. if ((P)) erts_proc_lc_require_lock((P), ERTS_PROC_LOCK_MAIN)
  72. # define ERTS_SMP_UNREQ_PROC_MAIN_LOCK(P) \
  73. if ((P)) erts_proc_lc_unrequire_lock((P), ERTS_PROC_LOCK_MAIN)
  74. # else
  75. # define ERTS_SMP_REQ_PROC_MAIN_LOCK(P)
  76. # define ERTS_SMP_UNREQ_PROC_MAIN_LOCK(P)
  77. # define PROCESS_MAIN_CHK_LOCKS(P) erts_lc_check_exact(NULL, 0)
  78. # endif
  79. #else
  80. # define PROCESS_MAIN_CHK_LOCKS(P)
  81. # define ERTS_SMP_REQ_PROC_MAIN_LOCK(P)
  82. # define ERTS_SMP_UNREQ_PROC_MAIN_LOCK(P)
  83. #endif
  84. /*
  85. * Define macros for deep checking of terms.
  86. */
  87. #if defined(HARDDEBUG)
  88. # define CHECK_TERM(T) size_object(T)
  89. # define CHECK_ARGS(PC) \
  90. do { \
  91. int i_; \
  92. int Arity_ = PC[-1]; \
  93. if (Arity_ > 0) { \
  94. CHECK_TERM(r(0)); \
  95. } \
  96. for (i_ = 1; i_ < Arity_; i_++) { \
  97. CHECK_TERM(x(i_)); \
  98. } \
  99. } while (0)
  100. #else
  101. # define CHECK_TERM(T) ASSERT(!is_CP(T))
  102. # define CHECK_ARGS(T)
  103. #endif
  104. #ifndef MAX
  105. #define MAX(x, y) (((x) > (y)) ? (x) : (y))
  106. #endif
  107. #define GET_BIF_ADDRESS(p) ((BifFunction) (((Export *) p)->code[4]))
  108. #define TermWords(t) (((t) / (sizeof(BeamInstr)/sizeof(Eterm))) + !!((t) % (sizeof(BeamInstr)/sizeof(Eterm))))
  109. /*
  110. * We reuse some of fields in the save area in the process structure.
  111. * This is safe to do, since this space is only activly used when
  112. * the process is switched out.
  113. */
  114. #define REDS_IN(p) ((p)->def_arg_reg[5])
  115. /*
  116. * Add a byte offset to a pointer to Eterm. This is useful when the
  117. * the loader has precalculated a byte offset.
  118. */
  119. #define ADD_BYTE_OFFSET(ptr, offset) \
  120. ((Eterm *) (((unsigned char *)ptr) + (offset)))
  121. /* We don't check the range if an ordinary switch is used */
  122. #ifdef NO_JUMP_TABLE
  123. #define VALID_INSTR(IP) (0 <= (int)(IP) && ((int)(IP) < (NUMBER_OF_OPCODES*2+10)))
  124. #else
  125. #define VALID_INSTR(IP) \
  126. ((SWord)LabelAddr(emulator_loop) <= (SWord)(IP) && \
  127. (SWord)(IP) < (SWord)LabelAddr(end_emulator_loop))
  128. #endif /* NO_JUMP_TABLE */
  129. #define SET_CP(p, ip) \
  130. ASSERT(VALID_INSTR(*(ip))); \
  131. (p)->cp = (ip)
  132. #define SET_I(ip) \
  133. ASSERT(VALID_INSTR(* (Eterm *)(ip))); \
  134. I = (ip)
  135. #define FetchArgs(S1, S2) tmp_arg1 = (S1); tmp_arg2 = (S2)
  136. /*
  137. * Store a result into a register given a destination descriptor.
  138. */
  139. #define StoreResult(Result, DestDesc) \
  140. do { \
  141. Eterm stb_reg; \
  142. stb_reg = (DestDesc); \
  143. CHECK_TERM(Result); \
  144. switch (beam_reg_tag(stb_reg)) { \
  145. case R_REG_DEF: \
  146. r(0) = (Result); break; \
  147. case X_REG_DEF: \
  148. xb(x_reg_offset(stb_reg)) = (Result); break; \
  149. default: \
  150. yb(y_reg_offset(stb_reg)) = (Result); break; \
  151. } \
  152. } while (0)
  153. #define StoreSimpleDest(Src, Dest) Dest = (Src)
  154. /*
  155. * Store a result into a register and execute the next instruction.
  156. * Dst points to the word with a destination descriptor, which MUST
  157. * be just before the next instruction.
  158. */
  159. #define StoreBifResult(Dst, Result) \
  160. do { \
  161. BeamInstr* stb_next; \
  162. Eterm stb_reg; \
  163. stb_reg = Arg(Dst); \
  164. I += (Dst) + 2; \
  165. stb_next = (BeamInstr *) *I; \
  166. CHECK_TERM(Result); \
  167. switch (beam_reg_tag(stb_reg)) { \
  168. case R_REG_DEF: \
  169. r(0) = (Result); Goto(stb_next); \
  170. case X_REG_DEF: \
  171. xb(x_reg_offset(stb_reg)) = (Result); Goto(stb_next); \
  172. default: \
  173. yb(y_reg_offset(stb_reg)) = (Result); Goto(stb_next); \
  174. } \
  175. } while (0)
  176. #define ClauseFail() goto lb_jump_f
  177. #define SAVE_CP(X) \
  178. do { \
  179. *(X) = make_cp(c_p->cp); \
  180. c_p->cp = 0; \
  181. } while(0)
  182. #define RESTORE_CP(X) SET_CP(c_p, (BeamInstr *) cp_val(*(X)))
  183. #define ISCATCHEND(instr) ((Eterm *) *(instr) == OpCode(catch_end_y))
  184. /*
  185. * Special Beam instructions.
  186. */
  187. BeamInstr beam_apply[2];
  188. BeamInstr beam_exit[1];
  189. BeamInstr beam_continue_exit[1];
  190. BeamInstr* em_call_error_handler;
  191. BeamInstr* em_apply_bif;
  192. BeamInstr* em_call_traced_function;
  193. /* NOTE These should be the only variables containing trace instructions.
  194. ** Sometimes tests are form the instruction value, and sometimes
  195. ** for the refering variable (one of these), and rouge references
  196. ** will most likely cause chaos.
  197. */
  198. BeamInstr beam_return_to_trace[1]; /* OpCode(i_return_to_trace) */
  199. BeamInstr beam_return_trace[1]; /* OpCode(i_return_trace) */
  200. BeamInstr beam_exception_trace[1]; /* UGLY also OpCode(i_return_trace) */
  201. BeamInstr beam_return_time_trace[1]; /* OpCode(i_return_time_trace) */
  202. /*
  203. * All Beam instructions in numerical order.
  204. */
  205. #ifndef NO_JUMP_TABLE
  206. void** beam_ops;
  207. #endif
  208. #ifndef ERTS_SMP /* Not supported with smp emulator */
  209. extern int count_instructions;
  210. #endif
  211. #if defined(HYBRID)
  212. #define SWAPIN \
  213. g_htop = global_htop; \
  214. g_hend = global_hend; \
  215. HTOP = HEAP_TOP(c_p); \
  216. E = c_p->stop
  217. #define SWAPOUT \
  218. global_htop = g_htop; \
  219. global_hend = g_hend; \
  220. HEAP_TOP(c_p) = HTOP; \
  221. c_p->stop = E
  222. #else
  223. #define SWAPIN \
  224. HTOP = HEAP_TOP(c_p); \
  225. E = c_p->stop
  226. #define SWAPOUT \
  227. HEAP_TOP(c_p) = HTOP; \
  228. c_p->stop = E
  229. /*
  230. * Use LIGHT_SWAPOUT when the called function
  231. * will call HeapOnlyAlloc() (and never HAlloc()).
  232. */
  233. #ifdef DEBUG
  234. # /* The stack pointer is used in an assertion. */
  235. # define LIGHT_SWAPOUT SWAPOUT
  236. #else
  237. # define LIGHT_SWAPOUT HEAP_TOP(c_p) = HTOP
  238. #endif
  239. /*
  240. * Use LIGHT_SWAPIN when we know that c_p->stop cannot
  241. * have been updated (i.e. if there cannot have been
  242. * a garbage-collection).
  243. */
  244. #define LIGHT_SWAPIN HTOP = HEAP_TOP(c_p)
  245. #endif
  246. #ifdef FORCE_HEAP_FRAGS
  247. # define HEAP_SPACE_VERIFIED(Words) do { \
  248. c_p->space_verified = (Words); \
  249. c_p->space_verified_from = HTOP; \
  250. }while(0)
  251. #else
  252. # define HEAP_SPACE_VERIFIED(Words) ((void)0)
  253. #endif
  254. #define PRE_BIF_SWAPOUT(P) \
  255. HEAP_TOP((P)) = HTOP; \
  256. (P)->stop = E; \
  257. PROCESS_MAIN_CHK_LOCKS((P)); \
  258. ERTS_SMP_UNREQ_PROC_MAIN_LOCK((P))
  259. #if defined(HYBRID)
  260. # define POST_BIF_GC_SWAPIN_0(_p, _res) \
  261. if (((_p)->mbuf) || (MSO(_p).overhead >= BIN_VHEAP_SZ(_p)) ) { \
  262. _res = erts_gc_after_bif_call((_p), (_res), NULL, 0); \
  263. } \
  264. SWAPIN
  265. # define POST_BIF_GC_SWAPIN(_p, _res, _regs, _arity) \
  266. if (((_p)->mbuf) || (MSO(_p).overhead >= BIN_VHEAP_SZ(_p)) ) { \
  267. _regs[0] = r(0); \
  268. _res = erts_gc_after_bif_call((_p), (_res), _regs, (_arity)); \
  269. r(0) = _regs[0]; \
  270. } \
  271. SWAPIN
  272. #else
  273. # define POST_BIF_GC_SWAPIN_0(_p, _res) \
  274. ERTS_SMP_REQ_PROC_MAIN_LOCK((_p)); \
  275. PROCESS_MAIN_CHK_LOCKS((_p)); \
  276. if (((_p)->mbuf) || (MSO(_p).overhead >= BIN_VHEAP_SZ(_p)) ) { \
  277. _res = erts_gc_after_bif_call((_p), (_res), NULL, 0); \
  278. E = (_p)->stop; \
  279. } \
  280. HTOP = HEAP_TOP((_p))
  281. # define POST_BIF_GC_SWAPIN(_p, _res, _regs, _arity) \
  282. ERTS_SMP_REQ_PROC_MAIN_LOCK((_p)); \
  283. PROCESS_MAIN_CHK_LOCKS((_p)); \
  284. if (((_p)->mbuf) || (MSO(_p).overhead >= BIN_VHEAP_SZ(_p)) ) { \
  285. _regs[0] = r(0); \
  286. _res = erts_gc_after_bif_call((_p), (_res), _regs, (_arity)); \
  287. r(0) = _regs[0]; \
  288. E = (_p)->stop; \
  289. } \
  290. HTOP = HEAP_TOP((_p))
  291. #endif
  292. #define db(N) (N)
  293. #define tb(N) (N)
  294. #define xb(N) (*(Eterm *) (((unsigned char *)reg) + (N)))
  295. #define yb(N) (*(Eterm *) (((unsigned char *)E) + (N)))
  296. #define fb(N) (*(double *) (((unsigned char *)&(freg[0].fd)) + (N)))
  297. #define x(N) reg[N]
  298. #define y(N) E[N]
  299. #define r(N) x##N
  300. /*
  301. * Makes sure that there are StackNeed + HeapNeed + 1 words available
  302. * on the combined heap/stack segment, then allocates StackNeed + 1
  303. * words on the stack and saves CP.
  304. *
  305. * M is number of live registers to preserve during garbage collection
  306. */
  307. #define AH(StackNeed, HeapNeed, M) \
  308. do { \
  309. int needed; \
  310. needed = (StackNeed) + 1; \
  311. if (E - HTOP < (needed + (HeapNeed))) { \
  312. SWAPOUT; \
  313. reg[0] = r(0); \
  314. PROCESS_MAIN_CHK_LOCKS(c_p); \
  315. FCALLS -= erts_garbage_collect(c_p, needed + (HeapNeed), reg, (M)); \
  316. PROCESS_MAIN_CHK_LOCKS(c_p); \
  317. r(0) = reg[0]; \
  318. SWAPIN; \
  319. } \
  320. E -= needed; \
  321. SAVE_CP(E); \
  322. } while (0)
  323. #define Allocate(Ns, Live) AH(Ns, 0, Live)
  324. #define AllocateZero(Ns, Live) \
  325. do { Eterm* ptr; \
  326. int i = (Ns); \
  327. AH(i, 0, Live); \
  328. for (ptr = E + i; ptr > E; ptr--) { \
  329. make_blank(*ptr); \
  330. } \
  331. } while (0)
  332. #define AllocateHeap(Ns, Nh, Live) AH(Ns, Nh, Live)
  333. #define AllocateHeapZero(Ns, Nh, Live) \
  334. do { Eterm* ptr; \
  335. int i = (Ns); \
  336. AH(i, Nh, Live); \
  337. for (ptr = E + i; ptr > E; ptr--) { \
  338. make_blank(*ptr); \
  339. } \
  340. } while (0)
  341. #define AllocateInit(Ns, Live, Y) \
  342. do { AH(Ns, 0, Live); make_blank(Y); } while (0)
  343. /*
  344. * Like the AH macro, but allocates no additional heap space.
  345. */
  346. #define A(StackNeed, M) AH(StackNeed, 0, M)
  347. #define D(N) \
  348. RESTORE_CP(E); \
  349. E += (N) + 1;
  350. #define TestBinVHeap(VNh, Nh, Live) \
  351. do { \
  352. unsigned need = (Nh); \
  353. if ((E - HTOP < need) || (MSO(c_p).overhead + (VNh) >= BIN_VHEAP_SZ(c_p))) {\
  354. SWAPOUT; \
  355. reg[0] = r(0); \
  356. PROCESS_MAIN_CHK_LOCKS(c_p); \
  357. FCALLS -= erts_garbage_collect(c_p, need, reg, (Live)); \
  358. PROCESS_MAIN_CHK_LOCKS(c_p); \
  359. r(0) = reg[0]; \
  360. SWAPIN; \
  361. } \
  362. HEAP_SPACE_VERIFIED(need); \
  363. } while (0)
  364. /*
  365. * Check if Nh words of heap are available; if not, do a garbage collection.
  366. * Live is number of active argument registers to be preserved.
  367. */
  368. #define TestHeap(Nh, Live) \
  369. do { \
  370. unsigned need = (Nh); \
  371. if (E - HTOP < need) { \
  372. SWAPOUT; \
  373. reg[0] = r(0); \
  374. PROCESS_MAIN_CHK_LOCKS(c_p); \
  375. FCALLS -= erts_garbage_collect(c_p, need, reg, (Live)); \
  376. PROCESS_MAIN_CHK_LOCKS(c_p); \
  377. r(0) = reg[0]; \
  378. SWAPIN; \
  379. } \
  380. HEAP_SPACE_VERIFIED(need); \
  381. } while (0)
  382. /*
  383. * Check if Nh words of heap are available; if not, do a garbage collection.
  384. * Live is number of active argument registers to be preserved.
  385. * Takes special care to preserve Extra if a garbage collection occurs.
  386. */
  387. #define TestHeapPreserve(Nh, Live, Extra) \
  388. do { \
  389. unsigned need = (Nh); \
  390. if (E - HTOP < need) { \
  391. SWAPOUT; \
  392. reg[0] = r(0); \
  393. reg[Live] = Extra; \
  394. PROCESS_MAIN_CHK_LOCKS(c_p); \
  395. FCALLS -= erts_garbage_collect(c_p, need, reg, (Live)+1); \
  396. PROCESS_MAIN_CHK_LOCKS(c_p); \
  397. if (Live > 0) { \
  398. r(0) = reg[0]; \
  399. } \
  400. Extra = reg[Live]; \
  401. SWAPIN; \
  402. } \
  403. HEAP_SPACE_VERIFIED(need); \
  404. } while (0)
  405. #ifdef HYBRID
  406. #ifdef INCREMENTAL
  407. #define TestGlobalHeap(Nh, Live, hp) \
  408. do { \
  409. unsigned need = (Nh); \
  410. ASSERT(global_heap <= g_htop && g_htop <= global_hend); \
  411. SWAPOUT; \
  412. reg[0] = r(0); \
  413. FCALLS -= need; \
  414. (hp) = IncAlloc(c_p,need,reg,(Live)); \
  415. r(0) = reg[0]; \
  416. SWAPIN; \
  417. } while (0)
  418. #else
  419. #define TestGlobalHeap(Nh, Live, hp) \
  420. do { \
  421. unsigned need = (Nh); \
  422. ASSERT(global_heap <= g_htop && g_htop <= global_hend); \
  423. if (g_hend - g_htop < need) { \
  424. SWAPOUT; \
  425. reg[0] = r(0); \
  426. FCALLS -= erts_global_garbage_collect(c_p, need, reg, (Live)); \
  427. r(0) = reg[0]; \
  428. SWAPIN; \
  429. } \
  430. (hp) = global_htop; \
  431. } while (0)
  432. #endif
  433. #endif /* HYBRID */
  434. #define Init(N) make_blank(yb(N))
  435. #define Init2(Y1, Y2) do { make_blank(Y1); make_blank(Y2); } while (0)
  436. #define Init3(Y1, Y2, Y3) \
  437. do { make_blank(Y1); make_blank(Y2); make_blank(Y3); } while (0)
  438. #define MakeFun(FunP, NumFree) \
  439. do { \
  440. SWAPOUT; \
  441. reg[0] = r(0); \
  442. r(0) = new_fun(c_p, reg, (ErlFunEntry *) FunP, NumFree); \
  443. SWAPIN; \
  444. } while (0)
  445. /*
  446. * Check that we haven't used the reductions and jump to function pointed to by
  447. * the I register. If we are out of reductions, do a context switch.
  448. */
  449. #define DispatchMacro() \
  450. do { \
  451. BeamInstr* dis_next; \
  452. dis_next = (BeamInstr *) *I; \
  453. CHECK_ARGS(I); \
  454. if (FCALLS > 0 || FCALLS > neg_o_reds) { \
  455. FCALLS--; \
  456. Goto(dis_next); \
  457. } else { \
  458. goto context_switch; \
  459. } \
  460. } while (0)
  461. #define DispatchMacroFun() \
  462. do { \
  463. BeamInstr* dis_next; \
  464. dis_next = (BeamInstr *) *I; \
  465. CHECK_ARGS(I); \
  466. if (FCALLS > 0 || FCALLS > neg_o_reds) { \
  467. FCALLS--; \
  468. Goto(dis_next); \
  469. } else { \
  470. goto context_switch_fun; \
  471. } \
  472. } while (0)
  473. #define DispatchMacrox() \
  474. do { \
  475. if (FCALLS > 0) { \
  476. Eterm* dis_next; \
  477. SET_I(((Export *) Arg(0))->address); \
  478. dis_next = (Eterm *) *I; \
  479. FCALLS--; \
  480. CHECK_ARGS(I); \
  481. Goto(dis_next); \
  482. } else if (ERTS_PROC_GET_SAVED_CALLS_BUF(c_p) \
  483. && FCALLS > neg_o_reds) { \
  484. goto save_calls1; \
  485. } else { \
  486. SET_I(((Export *) Arg(0))->address); \
  487. CHECK_ARGS(I); \
  488. goto context_switch; \
  489. } \
  490. } while (0)
  491. #ifdef DEBUG
  492. /*
  493. * To simplify breakpoint setting, put the code in one place only and jump to it.
  494. */
  495. # define Dispatch() goto do_dispatch
  496. # define Dispatchx() goto do_dispatchx
  497. # define Dispatchfun() goto do_dispatchfun
  498. #else
  499. /*
  500. * Inline for speed.
  501. */
  502. # define Dispatch() DispatchMacro()
  503. # define Dispatchx() DispatchMacrox()
  504. # define Dispatchfun() DispatchMacroFun()
  505. #endif
  506. #define Self(R) R = c_p->id
  507. #define Node(R) R = erts_this_node->sysname
  508. #define Arg(N) I[(N)+1]
  509. #define Next(N) \
  510. I += (N) + 1; \
  511. ASSERT(VALID_INSTR(*I)); \
  512. Goto(*I)
  513. #define PreFetch(N, Dst) do { Dst = (BeamInstr *) *(I + N + 1); } while (0)
  514. #define NextPF(N, Dst) \
  515. I += N + 1; \
  516. ASSERT(VALID_INSTR(Dst)); \
  517. Goto(Dst)
  518. #define GetR(pos, tr) \
  519. do { \
  520. tr = Arg(pos); \
  521. switch (beam_reg_tag(tr)) { \
  522. case R_REG_DEF: tr = r(0); break; \
  523. case X_REG_DEF: tr = xb(x_reg_offset(tr)); break; \
  524. case Y_REG_DEF: ASSERT(y_reg_offset(tr) >= 1); tr = yb(y_reg_offset(tr)); break; \
  525. } \
  526. CHECK_TERM(tr); \
  527. } while (0)
  528. #define GetArg1(N, Dst) GetR((N), Dst)
  529. #define GetArg2(N, Dst1, Dst2) \
  530. do { \
  531. GetR(N, Dst1); \
  532. GetR((N)+1, Dst2); \
  533. } while (0)
  534. #define PutList(H, T, Dst, Store) \
  535. do { \
  536. HTOP[0] = (H); HTOP[1] = (T); \
  537. Store(make_list(HTOP), Dst); \
  538. HTOP += 2; \
  539. } while (0)
  540. #define Move(Src, Dst, Store) \
  541. do { \
  542. Eterm term = (Src); \
  543. Store(term, Dst); \
  544. } while (0)
  545. #define Move2(src1, dst1, src2, dst2) dst1 = (src1); dst2 = (src2)
  546. #define MoveGenDest(src, dstp) \
  547. if ((dstp) == NULL) { r(0) = (src); } else { *(dstp) = src; }
  548. #define MoveReturn(Src, Dest) \
  549. (Dest) = (Src); \
  550. I = c_p->cp; \
  551. ASSERT(VALID_INSTR(*c_p->cp)); \
  552. c_p->cp = 0; \
  553. CHECK_TERM(r(0)); \
  554. Goto(*I)
  555. #define DeallocateReturn(Deallocate) \
  556. do { \
  557. int words_to_pop = (Deallocate); \
  558. SET_I((BeamInstr *) cp_val(*E)); \
  559. E = ADD_BYTE_OFFSET(E, words_to_pop); \
  560. CHECK_TERM(r(0)); \
  561. Goto(*I); \
  562. } while (0)
  563. #define MoveDeallocateReturn(Src, Dest, Deallocate) \
  564. (Dest) = (Src); \
  565. DeallocateReturn(Deallocate)
  566. #define MoveCall(Src, Dest, CallDest, Size) \
  567. (Dest) = (Src); \
  568. SET_CP(c_p, I+Size+1); \
  569. SET_I((BeamInstr *) CallDest); \
  570. Dispatch();
  571. #define MoveCallLast(Src, Dest, CallDest, Deallocate) \
  572. (Dest) = (Src); \
  573. RESTORE_CP(E); \
  574. E = ADD_BYTE_OFFSET(E, (Deallocate)); \
  575. SET_I((BeamInstr *) CallDest); \
  576. Dispatch();
  577. #define MoveCallOnly(Src, Dest, CallDest) \
  578. (Dest) = (Src); \
  579. SET_I((BeamInstr *) CallDest); \
  580. Dispatch();
  581. #define GetList(Src, H, T) do { \
  582. Eterm* tmp_ptr = list_val(Src); \
  583. H = CAR(tmp_ptr); \
  584. T = CDR(tmp_ptr); } while (0)
  585. #define GetTupleElement(Src, Element, Dest) \
  586. do { \
  587. tmp_arg1 = (Eterm) COMPRESS_POINTER(((unsigned char *) tuple_val(Src)) + \
  588. (Element)); \
  589. (Dest) = (*(Eterm *) EXPAND_POINTER(tmp_arg1)); \
  590. } while (0)
  591. #define ExtractNextElement(Dest) \
  592. tmp_arg1 += sizeof(Eterm); \
  593. (Dest) = (* (Eterm *) (((unsigned char *) EXPAND_POINTER(tmp_arg1))))
  594. #define ExtractNextElement2(Dest) \
  595. do { \
  596. Eterm* ene_dstp = &(Dest); \
  597. ene_dstp[0] = ((Eterm *) EXPAND_POINTER(tmp_arg1))[1]; \
  598. ene_dstp[1] = ((Eterm *) EXPAND_POINTER(tmp_arg1))[2]; \
  599. tmp_arg1 += sizeof(Eterm) + sizeof(Eterm); \
  600. } while (0)
  601. #define ExtractNextElement3(Dest) \
  602. do { \
  603. Eterm* ene_dstp = &(Dest); \
  604. ene_dstp[0] = ((Eterm *) EXPAND_POINTER(tmp_arg1))[1]; \
  605. ene_dstp[1] = ((Eterm *) EXPAND_POINTER(tmp_arg1))[2]; \
  606. ene_dstp[2] = ((Eterm *) EXPAND_POINTER(tmp_arg1))[3]; \
  607. tmp_arg1 += 3*sizeof(Eterm); \
  608. } while (0)
  609. #define ExtractNextElement4(Dest) \
  610. do { \
  611. Eterm* ene_dstp = &(Dest); \
  612. ene_dstp[0] = ((Eterm *) EXPAND_POINTER(tmp_arg1))[1]; \
  613. ene_dstp[1] = ((Eterm *) EXPAND_POINTER(tmp_arg1))[2]; \
  614. ene_dstp[2] = ((Eterm *) EXPAND_POINTER(tmp_arg1))[3]; \
  615. ene_dstp[3] = ((Eterm *) EXPAND_POINTER(tmp_arg1))[4]; \
  616. tmp_arg1 += 4*sizeof(Eterm); \
  617. } while (0)
  618. #define ExtractElement(Element, Dest) \
  619. do { \
  620. tmp_arg1 += (Element); \
  621. (Dest) = (* (Eterm *) EXPAND_POINTER(tmp_arg1)); \
  622. } while (0)
  623. #define PutTuple(Arity, Src, Dest) \
  624. ASSERT(is_arity_value(Arity)); \
  625. Dest = make_tuple(HTOP); \
  626. HTOP[0] = (Arity); \
  627. HTOP[1] = (Src); \
  628. HTOP += 2
  629. #define Put(Word) *HTOP++ = (Word)
  630. #define EqualImmed(X, Y, Action) if (X != Y) { Action; }
  631. #define IsFloat(Src, Fail) if (is_not_float(Src)) { Fail; }
  632. #define IsInteger(Src, Fail) if (is_not_integer(Src)) { Fail; }
  633. #define IsNumber(X, Fail) if (is_not_integer(X) && is_not_float(X)) { Fail; }
  634. #define IsAtom(Src, Fail) if (is_not_atom(Src)) { Fail; }
  635. #define IsIntegerAllocate(Src, Need, Alive, Fail) \
  636. if (is_not_integer(Src)) { Fail; } \
  637. A(Need, Alive)
  638. #define IsNil(Src, Fail) if (is_not_nil(Src)) { Fail; }
  639. #define IsList(Src, Fail) if (is_not_list(Src) && is_not_nil(Src)) { Fail; }
  640. #define IsNonemptyList(Src, Fail) if (is_not_list(Src)) { Fail; }
  641. #define IsNonemptyListAllocate(Src, Need, Alive, Fail) \
  642. if (is_not_list(Src)) { Fail; } \
  643. A(Need, Alive)
  644. #define IsNonemptyListTestHeap(Src, Need, Alive, Fail) \
  645. if (is_not_list(Src)) { Fail; } \
  646. TestHeap(Need, Alive)
  647. #define IsTuple(X, Action) if (is_not_tuple(X)) Action
  648. #define IsArity(Pointer, Arity, Fail) \
  649. if (*(Eterm *) \
  650. EXPAND_POINTER(tmp_arg1 = (Eterm) \
  651. COMPRESS_POINTER(tuple_val(Pointer))) != (Arity)) \
  652. { \
  653. Fail; \
  654. }
  655. #define IsFunction(X, Action) \
  656. do { \
  657. if ( !(is_any_fun(X)) ) { \
  658. Action; \
  659. } \
  660. } while (0)
  661. #define IsFunction2(F, A, Action) \
  662. do { \
  663. if (is_function_2(c_p, F, A) != am_true ) {\
  664. Action; \
  665. } \
  666. } while (0)
  667. #define IsTupleOfArity(Src, Arity, Fail) \
  668. do { \
  669. if (is_not_tuple(Src) || \
  670. *(Eterm *) \
  671. EXPAND_POINTER(tmp_arg1 = \
  672. (Eterm) COMPRESS_POINTER(tuple_val(Src))) != Arity) { \
  673. Fail; \
  674. } \
  675. } while (0)
  676. #define IsBoolean(X, Fail) if ((X) != am_true && (X) != am_false) { Fail; }
  677. #define IsBinary(Src, Fail) \
  678. if (is_not_binary(Src) || binary_bitsize(Src) != 0) { Fail; }
  679. #define IsBitstring(Src, Fail) \
  680. if (is_not_binary(Src)) { Fail; }
  681. #if defined(ARCH_64) && !HALFWORD_HEAP
  682. #define BsSafeMul(A, B, Fail, Target) \
  683. do { Uint64 _res = (A) * (B); \
  684. if (_res / B != A) { Fail; } \
  685. Target = _res; \
  686. } while (0)
  687. #else
  688. #define BsSafeMul(A, B, Fail, Target) \
  689. do { Uint64 _res = (Uint64)(A) * (Uint64)(B); \
  690. if ((_res >> (8*sizeof(Uint))) != 0) { Fail; } \
  691. Target = _res; \
  692. } while (0)
  693. #endif
  694. #define BsGetFieldSize(Bits, Unit, Fail, Target) \
  695. do { \
  696. Sint _signed_size; Uint _uint_size; \
  697. if (is_small(Bits)) { \
  698. _signed_size = signed_val(Bits); \
  699. if (_signed_size < 0) { Fail; } \
  700. _uint_size = (Uint) _signed_size; \
  701. } else { \
  702. if (!term_to_Uint(Bits, &temp_bits)) { Fail; } \
  703. _uint_size = temp_bits; \
  704. } \
  705. BsSafeMul(_uint_size, Unit, Fail, Target); \
  706. } while (0)
  707. #define BsGetUncheckedFieldSize(Bits, Unit, Fail, Target) \
  708. do { \
  709. Sint _signed_size; Uint _uint_size; \
  710. if (is_small(Bits)) { \
  711. _signed_size = signed_val(Bits); \
  712. if (_signed_size < 0) { Fail; } \
  713. _uint_size = (Uint) _signed_size; \
  714. } else { \
  715. if (!term_to_Uint(Bits, &temp_bits)) { Fail; } \
  716. _uint_size = (Uint) temp_bits; \
  717. } \
  718. Target = _uint_size * Unit; \
  719. } while (0)
  720. #define BsGetFloat2(Ms, Live, Sz, Flags, Dst, Store, Fail) \
  721. do { \
  722. ErlBinMatchBuffer *_mb; \
  723. Eterm _result; Sint _size; \
  724. if (!is_small(Sz) || (_size = unsigned_val(Sz)) > 64) { Fail; } \
  725. _size *= ((Flags) >> 3); \
  726. TestHeap(FLOAT_SIZE_OBJECT, Live); \
  727. _mb = ms_matchbuffer(Ms); \
  728. LIGHT_SWAPOUT; \
  729. _result = erts_bs_get_float_2(c_p, _size, (Flags), _mb); \
  730. LIGHT_SWAPIN; \
  731. HEAP_SPACE_VERIFIED(0); \
  732. if (is_non_value(_result)) { Fail; } \
  733. else { Store(_result, Dst); } \
  734. } while (0)
  735. #define BsGetBinaryImm_2(Ms, Live, Sz, Flags, Dst, Store, Fail) \
  736. do { \
  737. ErlBinMatchBuffer *_mb; \
  738. Eterm _result; \
  739. TestHeap(heap_bin_size(ERL_ONHEAP_BIN_LIMIT), Live); \
  740. _mb = ms_matchbuffer(Ms); \
  741. LIGHT_SWAPOUT; \
  742. _result = erts_bs_get_binary_2(c_p, (Sz), (Flags), _mb); \
  743. LIGHT_SWAPIN; \
  744. HEAP_SPACE_VERIFIED(0); \
  745. if (is_non_value(_result)) { Fail; } \
  746. else { Store(_result, Dst); } \
  747. } while (0)
  748. #define BsGetBinary_2(Ms, Live, Sz, Flags, Dst, Store, Fail) \
  749. do { \
  750. ErlBinMatchBuffer *_mb; \
  751. Eterm _result; Uint _size; \
  752. BsGetFieldSize(Sz, ((Flags) >> 3), Fail, _size); \
  753. TestHeap(ERL_SUB_BIN_SIZE, Live); \
  754. _mb = ms_matchbuffer(Ms); \
  755. LIGHT_SWAPOUT; \
  756. _result = erts_bs_get_binary_2(c_p, _size, (Flags), _mb); \
  757. LIGHT_SWAPIN; \
  758. HEAP_SPACE_VERIFIED(0); \
  759. if (is_non_value(_result)) { Fail; } \
  760. else { Store(_result, Dst); } \
  761. } while (0)
  762. #define BsGetBinaryAll_2(Ms, Live, Unit, Dst, Store, Fail) \
  763. do { \
  764. ErlBinMatchBuffer *_mb; \
  765. Eterm _result; \
  766. TestHeap(ERL_SUB_BIN_SIZE, Live); \
  767. _mb = ms_matchbuffer(Ms); \
  768. if (((_mb->size - _mb->offset) % Unit) == 0) { \
  769. LIGHT_SWAPOUT; \
  770. _result = erts_bs_get_binary_all_2(c_p, _mb); \
  771. LIGHT_SWAPIN; \
  772. HEAP_SPACE_VERIFIED(0); \
  773. ASSERT(is_value(_result)); \
  774. Store(_result, Dst); \
  775. } else { \
  776. HEAP_SPACE_VERIFIED(0); \
  777. Fail; } \
  778. } while (0)
  779. #define BsSkipBits2(Ms, Bits, Unit, Fail) \
  780. do { \
  781. ErlBinMatchBuffer *_mb; \
  782. size_t new_offset; \
  783. Uint _size; \
  784. _mb = ms_matchbuffer(Ms); \
  785. BsGetFieldSize(Bits, Unit, Fail, _size); \
  786. new_offset = _mb->offset + _size; \
  787. if (new_offset <= _mb->size) { _mb->offset = new_offset; } \
  788. else { Fail; } \
  789. } while (0)
  790. #define BsSkipBitsAll2(Ms, Unit, Fail) \
  791. do { \
  792. ErlBinMatchBuffer *_mb; \
  793. _mb = ms_matchbuffer(Ms); \
  794. if (((_mb->size - _mb->offset) % Unit) == 0) {_mb->offset = _mb->size; } \
  795. else { Fail; } \
  796. } while (0)
  797. #define BsSkipBitsImm2(Ms, Bits, Fail) \
  798. do { \
  799. ErlBinMatchBuffer *_mb; \
  800. size_t new_offset; \
  801. _mb = ms_matchbuffer(Ms); \
  802. new_offset = _mb->offset + (Bits); \
  803. if (new_offset <= _mb->size) { _mb->offset = new_offset; } \
  804. else { Fail; } \
  805. } while (0)
  806. #define NewBsPutIntegerImm(Sz, Flags, Src) \
  807. do { \
  808. if (!erts_new_bs_put_integer(ERL_BITS_ARGS_3((Src), (Sz), (Flags)))) { goto badarg; } \
  809. } while (0)
  810. #define NewBsPutInteger(Sz, Flags, Src) \
  811. do { \
  812. Sint _size; \
  813. BsGetUncheckedFieldSize(Sz, ((Flags) >> 3), goto badarg, _size); \
  814. if (!erts_new_bs_put_integer(ERL_BITS_ARGS_3((Src), _size, (Flags)))) \
  815. { goto badarg; } \
  816. } while (0)
  817. #define NewBsPutFloatImm(Sz, Flags, Src) \
  818. do { \
  819. if (!erts_new_bs_put_float(c_p, (Src), (Sz), (Flags))) { goto badarg; } \
  820. } while (0)
  821. #define NewBsPutFloat(Sz, Flags, Src) \
  822. do { \
  823. Sint _size; \
  824. BsGetUncheckedFieldSize(Sz, ((Flags) >> 3), goto badarg, _size); \
  825. if (!erts_new_bs_put_float(c_p, (Src), _size, (Flags))) { goto badarg; } \
  826. } while (0)
  827. #define NewBsPutBinary(Sz, Flags, Src) \
  828. do { \
  829. Sint _size; \
  830. BsGetUncheckedFieldSize(Sz, ((Flags) >> 3), goto badarg, _size); \
  831. if (!erts_new_bs_put_binary(ERL_BITS_ARGS_2((Src), _size))) { goto badarg; } \
  832. } while (0)
  833. #define NewBsPutBinaryImm(Sz, Src) \
  834. do { \
  835. if (!erts_new_bs_put_binary(ERL_BITS_ARGS_2((Src), (Sz)))) { goto badarg; } \
  836. } while (0)
  837. #define NewBsPutBinaryAll(Src, Unit) \
  838. do { \
  839. if (!erts_new_bs_put_binary_all(ERL_BITS_ARGS_2((Src), (Unit)))) { goto badarg; } \
  840. } while (0)
  841. #define IsPort(Src, Fail) if (is_not_port(Src)) { Fail; }
  842. #define IsPid(Src, Fail) if (is_not_pid(Src)) { Fail; }
  843. #define IsRef(Src, Fail) if (is_not_ref(Src)) { Fail; }
  844. static BifFunction translate_gc_bif(void* gcf);
  845. static BeamInstr* handle_error(Process* c_p, BeamInstr* pc, Eterm* reg, BifFunction bf);
  846. static BeamInstr* next_catch(Process* c_p, Eterm *reg);
  847. static void terminate_proc(Process* c_p, Eterm Value);
  848. static Eterm add_stacktrace(Process* c_p, Eterm Value, Eterm exc);
  849. static void save_stacktrace(Process* c_p, BeamInstr* pc, Eterm* reg,
  850. BifFunction bf, Eterm args);
  851. static struct StackTrace * get_trace_from_exc(Eterm exc);
  852. static Eterm make_arglist(Process* c_p, Eterm* reg, int a);
  853. static Eterm call_error_handler(Process* p, BeamInstr* ip, Eterm* reg);
  854. static Eterm call_breakpoint_handler(Process* p, BeamInstr* fi, Eterm* reg);
  855. static BeamInstr* fixed_apply(Process* p, Eterm* reg, Uint arity);
  856. static BeamInstr* apply(Process* p, Eterm module, Eterm function,
  857. Eterm args, Eterm* reg);
  858. static int hibernate(Process* c_p, Eterm module, Eterm function,
  859. Eterm args, Eterm* reg);
  860. static BeamInstr* call_fun(Process* p, int arity, Eterm* reg, Eterm args);
  861. static BeamInstr* apply_fun(Process* p, Eterm fun, Eterm args, Eterm* reg);
  862. static Eterm new_fun(Process* p, Eterm* reg, ErlFunEntry* fe, int num_free);
  863. #if defined(VXWORKS)
  864. static int init_done;
  865. #endif
  866. void
  867. init_emulator(void)
  868. {
  869. #if defined(VXWORKS)
  870. init_done = 0;
  871. #endif
  872. process_main();
  873. }
  874. /*
  875. * On certain platforms, make sure that the main variables really are placed
  876. * in registers.
  877. */
  878. #if defined(__GNUC__) && defined(sparc) && !defined(DEBUG)
  879. # define REG_x0 asm("%l0")
  880. # define REG_xregs asm("%l1")
  881. # define REG_htop asm("%l2")
  882. # define REG_stop asm("%l3")
  883. # define REG_I asm("%l4")
  884. # define REG_fcalls asm("%l5")
  885. # define REG_tmp_arg1 asm("%l6")
  886. # define REG_tmp_arg2 asm("%l7")
  887. #else
  888. # define REG_x0
  889. # define REG_xregs
  890. # define REG_htop
  891. # define REG_stop
  892. # define REG_I
  893. # define REG_fcalls
  894. # define REG_tmp_arg1
  895. # define REG_tmp_arg2
  896. #endif
  897. /*
  898. * process_main() is called twice:
  899. * The first call performs some initialisation, including exporting
  900. * the instructions' C labels to the loader.
  901. * The second call starts execution of BEAM code. This call never returns.
  902. */
  903. void process_main(void)
  904. {
  905. #if !defined(VXWORKS)
  906. static int init_done = 0;
  907. #endif
  908. Process* c_p = NULL;
  909. int reds_used;
  910. #ifdef DEBUG
  911. Eterm pid;
  912. #endif
  913. /*
  914. * X register zero; also called r(0)
  915. */
  916. register Eterm x0 REG_x0 = NIL;
  917. /* Pointer to X registers: x(1)..x(N); reg[0] is used when doing GC,
  918. * in all other cases x0 is used.
  919. */
  920. register Eterm* reg REG_xregs = NULL;
  921. /*
  922. * Top of heap (next free location); grows upwards.
  923. */
  924. register Eterm* HTOP REG_htop = NULL;
  925. #ifdef HYBRID
  926. Eterm *g_htop;
  927. Eterm *g_hend;
  928. #endif
  929. /* Stack pointer. Grows downwards; points
  930. * to last item pushed (normally a saved
  931. * continuation pointer).
  932. */
  933. register Eterm* E REG_stop = NULL;
  934. /*
  935. * Pointer to next threaded instruction.
  936. */
  937. register BeamInstr *I REG_I = NULL;
  938. /* Number of reductions left. This function
  939. * returns to the scheduler when FCALLS reaches zero.
  940. */
  941. register Sint FCALLS REG_fcalls = 0;
  942. /*
  943. * Temporaries used for picking up arguments for instructions.
  944. */
  945. register Eterm tmp_arg1 REG_tmp_arg1 = NIL;
  946. register Eterm tmp_arg2 REG_tmp_arg2 = NIL;
  947. #if HEAP_ON_C_STACK
  948. Eterm tmp_big[2]; /* Temporary buffer for small bignums if HEAP_ON_C_STACK. */
  949. #else
  950. Eterm *tmp_big; /* Temporary buffer for small bignums if !HEAP_ON_C_STACK. */
  951. #endif
  952. #ifndef ERTS_SMP
  953. #if !HALFWORD_HEAP
  954. static Eterm save_reg[ERTS_X_REGS_ALLOCATED];
  955. /* X registers -- not used directly, but
  956. * through 'reg', because using it directly
  957. * needs two instructions on a SPARC,
  958. * while using it through reg needs only
  959. * one.
  960. */
  961. #endif
  962. /*
  963. * Floating point registers.
  964. */
  965. static FloatDef freg[MAX_REG];
  966. #else
  967. /* X regisers and floating point registers are located in
  968. * scheduler specific data.
  969. */
  970. register FloatDef *freg;
  971. #endif
  972. /*
  973. * For keeping the negative old value of 'reds' when call saving is active.
  974. */
  975. int neg_o_reds = 0;
  976. Eterm (*arith_func)(Process* p, Eterm* reg, Uint live);
  977. #ifndef NO_JUMP_TABLE
  978. static void* opcodes[] = { DEFINE_OPCODES };
  979. #ifdef ERTS_OPCODE_COUNTER_SUPPORT
  980. static void* counting_opcodes[] = { DEFINE_COUNTING_OPCODES };
  981. #endif
  982. #else
  983. int Go;
  984. #endif
  985. Uint temp_bits; /* Temporary used by BsSkipBits2 & BsGetInteger2 */
  986. ERL_BITS_DECLARE_STATEP; /* Has to be last declaration */
  987. /*
  988. * Note: In this function, we attempt to place rarely executed code towards
  989. * the end of the function, in the hope that the cache hit rate will be better.
  990. * The initialization code is only run once, so it is at the very end.
  991. *
  992. * Note: c_p->arity must be set to reflect the number of useful terms in
  993. * c_p->arg_reg before calling the scheduler.
  994. */
  995. if (!init_done) {
  996. init_done = 1;
  997. goto init_emulator;
  998. }
  999. #ifndef ERTS_SMP
  1000. #if !HALFWORD_HEAP
  1001. reg = save_reg; /* XXX: probably wastes a register on x86 */
  1002. #else
  1003. /* Registers need to be heap allocated (correct memory range) for tracing to work */
  1004. reg = erts_alloc(ERTS_ALC_T_BEAM_REGISTER, ERTS_X_REGS_ALLOCATED * sizeof(Eterm));
  1005. #endif
  1006. #endif
  1007. c_p = NULL;
  1008. reds_used = 0;
  1009. goto do_schedule1;
  1010. do_schedule:
  1011. reds_used = REDS_IN(c_p) - FCALLS;
  1012. do_schedule1:
  1013. PROCESS_MAIN_CHK_LOCKS(c_p);
  1014. ERTS_SMP_UNREQ_PROC_MAIN_LOCK(c_p);
  1015. c_p = schedule(c_p, reds_used);
  1016. #ifdef DEBUG
  1017. pid = c_p->id;
  1018. #endif
  1019. ERTS_SMP_REQ_PROC_MAIN_LOCK(c_p);
  1020. PROCESS_MAIN_CHK_LOCKS(c_p);
  1021. #ifdef ERTS_SMP
  1022. reg = c_p->scheduler_data->save_reg;
  1023. freg = c_p->scheduler_data->freg;
  1024. #endif
  1025. #if !HEAP_ON_C_STACK
  1026. tmp_big = ERTS_PROC_GET_SCHDATA(c_p)->beam_emu_tmp_heap;
  1027. #endif
  1028. ERL_BITS_RELOAD_STATEP(c_p);
  1029. {
  1030. int reds;
  1031. Eterm* argp;
  1032. BeamInstr *next;
  1033. int i;
  1034. argp = c_p->arg_reg;
  1035. for (i = c_p->arity - 1; i > 0; i--) {
  1036. reg[i] = argp[i];
  1037. CHECK_TERM(reg[i]);
  1038. }
  1039. /*
  1040. * We put the original reduction count in the process structure, to reduce
  1041. * the code size (referencing a field in a struct through a pointer stored
  1042. * in a register gives smaller code than referencing a global variable).
  1043. */
  1044. SET_I(c_p->i);
  1045. reds = c_p->fcalls;
  1046. if (ERTS_PROC_GET_SAVED_CALLS_BUF(c_p)
  1047. && (c_p->trace_flags & F_SENSITIVE) == 0) {
  1048. neg_o_reds = -reds;
  1049. FCALLS = REDS_IN(c_p) = 0;
  1050. } else {
  1051. neg_o_reds = 0;
  1052. FCALLS = REDS_IN(c_p) = reds;
  1053. }
  1054. next = (BeamInstr *) *I;
  1055. r(0) = c_p->arg_reg[0];
  1056. #ifdef HARDDEBUG
  1057. if (c_p->arity > 0) {
  1058. CHECK_TERM(r(0));
  1059. }
  1060. #endif
  1061. SWAPIN;
  1062. ASSERT(VALID_INSTR(next));
  1063. Goto(next);
  1064. }
  1065. #if defined(DEBUG) || defined(NO_JUMP_TABLE)
  1066. emulator_loop:
  1067. #endif
  1068. #ifdef NO_JUMP_TABLE
  1069. switch (Go) {
  1070. #endif
  1071. #include "beam_hot.h"
  1072. #define STORE_ARITH_RESULT(res) StoreBifResult(2, (res));
  1073. #define ARITH_FUNC(name) erts_gc_##name
  1074. OpCase(i_plus_jId):
  1075. {
  1076. Eterm result;
  1077. if (is_both_small(tmp_arg1, tmp_arg2)) {
  1078. Sint i = signed_val(tmp_arg1) + signed_val(tmp_arg2);
  1079. ASSERT(MY_IS_SSMALL(i) == IS_SSMALL(i));
  1080. if (MY_IS_SSMALL(i)) {
  1081. result = make_small(i);
  1082. STORE_ARITH_RESULT(result);
  1083. }
  1084. }
  1085. arith_func = ARITH_FUNC(mixed_plus);
  1086. goto do_big_arith2;
  1087. }
  1088. OpCase(i_minus_jId):
  1089. {
  1090. Eterm result;
  1091. if (is_both_small(tmp_arg1, tmp_arg2)) {
  1092. Sint i = signed_val(tmp_arg1) - signed_val(tmp_arg2);
  1093. ASSERT(MY_IS_SSMALL(i) == IS_SSMALL(i));
  1094. if (MY_IS_SSMALL(i)) {
  1095. result = make_small(i);
  1096. STORE_ARITH_RESULT(result);
  1097. }
  1098. }
  1099. arith_func = ARITH_FUNC(mixed_minus);
  1100. goto do_big_arith2;
  1101. }
  1102. OpCase(i_is_lt_f):
  1103. if (CMP_GE(tmp_arg1, tmp_arg2)) {
  1104. ClauseFail();
  1105. }
  1106. Next(1);
  1107. OpCase(i_is_ge_f):
  1108. if (CMP_LT(tmp_arg1, tmp_arg2)) {
  1109. ClauseFail();
  1110. }
  1111. Next(1);
  1112. OpCase(i_is_eq_f):
  1113. if (CMP_NE(tmp_arg1, tmp_arg2)) {
  1114. ClauseFail();
  1115. }
  1116. Next(1);
  1117. OpCase(i_is_ne_f):
  1118. if (CMP_EQ(tmp_arg1, tmp_arg2)) {
  1119. ClauseFail();
  1120. }
  1121. Next(1);
  1122. OpCase(i_is_eq_exact_f):
  1123. if (!EQ(tmp_arg1, tmp_arg2)) {
  1124. ClauseFail();
  1125. }
  1126. Next(1);
  1127. OpCase(i_move_call_only_fcr): {
  1128. r(0) = Arg(1);
  1129. }
  1130. /* FALL THROUGH */
  1131. OpCase(i_call_only_f): {
  1132. SET_I((BeamInstr *) Arg(0));
  1133. Dispatch();
  1134. }
  1135. OpCase(i_move_call_last_fPcr): {
  1136. r(0) = Arg(2);
  1137. }
  1138. /* FALL THROUGH */
  1139. OpCase(i_call_last_fP): {
  1140. RESTORE_CP(E);
  1141. E = ADD_BYTE_OFFSET(E, Arg(1));
  1142. SET_I((BeamInstr *) Arg(0));
  1143. Dispatch();
  1144. }
  1145. OpCase(i_move_call_crf): {
  1146. r(0) = Arg(0);
  1147. I++;
  1148. }
  1149. /* FALL THROUGH */
  1150. OpCase(i_call_f): {
  1151. SET_CP(c_p, I+2);
  1152. SET_I((BeamInstr *) Arg(0));
  1153. Dispatch();
  1154. }
  1155. OpCase(i_move_call_ext_last_ePcr): {
  1156. r(0) = Arg(2);
  1157. }
  1158. /* FALL THROUGH */
  1159. OpCase(i_call_ext_last_eP):
  1160. RESTORE_CP(E);
  1161. E = ADD_BYTE_OFFSET(E, Arg(1));
  1162. /*
  1163. * Note: The pointer to the export entry is never NULL; if the module
  1164. * is not loaded, it points to code which will invoke the error handler
  1165. * (see lb_call_error_handler below).
  1166. */
  1167. Dispatchx();
  1168. OpCase(i_move_call_ext_cre): {
  1169. r(0) = Arg(0);
  1170. I++;
  1171. }
  1172. /* FALL THROUGH */
  1173. OpCase(i_call_ext_e):
  1174. SET_CP(c_p, I+2);
  1175. Dispatchx();
  1176. OpCase(i_move_call_ext_only_ecr): {
  1177. r(0) = Arg(1);
  1178. }
  1179. /* FALL THROUGH */
  1180. OpCase(i_call_ext_only_e):
  1181. Dispatchx();
  1182. OpCase(init_y): {
  1183. BeamInstr *next;
  1184. PreFetch(1, next);
  1185. make_blank(yb(Arg(0)));
  1186. NextPF(1, next);
  1187. }
  1188. OpCase(i_trim_I): {
  1189. BeamInstr *next;
  1190. Uint words;
  1191. Uint cp;
  1192. words = Arg(0);
  1193. cp = E[0];
  1194. PreFetch(1, next);
  1195. E += words;
  1196. E[0] = cp;
  1197. NextPF(1, next);
  1198. }
  1199. OpCase(return): {
  1200. SET_I(c_p->cp);
  1201. /*
  1202. * We must clear the CP to make sure that a stale value do not
  1203. * create a false module dependcy preventing code upgrading.
  1204. * It also means that we can use the CP in stack backtraces.
  1205. */
  1206. c_p->cp = 0;
  1207. CHECK_TERM(r(0));
  1208. HEAP_SPACE_VERIFIED(0);
  1209. Goto(*I);
  1210. }
  1211. OpCase(test_heap_1_put_list_Iy): {
  1212. BeamInstr *next;
  1213. PreFetch(2, next);
  1214. TestHeap(Arg(0), 1);
  1215. PutList(yb(Arg(1)), r(0), r(0), StoreSimpleDest);
  1216. CHECK_TERM(r(0));
  1217. NextPF(2, next);
  1218. }
  1219. /*
  1220. * Send is almost a standard call-BIF with two arguments, except for:
  1221. * 1) It cannot be traced.
  1222. * 2) There is no pointer to the send_2 function stored in
  1223. * the instruction.
  1224. */
  1225. OpCase(send): {
  1226. BeamInstr *next;
  1227. Eterm result;
  1228. PRE_BIF_SWAPOUT(c_p);
  1229. c_p->fcalls = FCALLS - 1;
  1230. result = send_2(c_p, r(0), x(1));
  1231. PreFetch(0, next);
  1232. POST_BIF_GC_SWAPIN(c_p, result, reg, 2);
  1233. FCALLS = c_p->fcalls;
  1234. if (is_value(result)) {
  1235. r(0) = result;
  1236. CHECK_TERM(r(0));
  1237. NextPF(0, next);
  1238. } else if (c_p->freason == TRAP) {
  1239. SET_CP(c_p, I+1);
  1240. SET_I(*((BeamInstr **) (BeamInstr) ((c_p)->def_arg_reg + 3)));
  1241. SWAPIN;
  1242. r(0) = c_p->def_arg_reg[0];
  1243. x(1) = c_p->def_arg_reg[1];
  1244. Dispatch();
  1245. }
  1246. goto find_func_info;
  1247. }
  1248. OpCase(i_element_jssd): {
  1249. Eterm index;
  1250. Eterm tuple;
  1251. /*
  1252. * Inlined version of element/2 for speed.
  1253. */
  1254. GetArg2(1, index, tuple);
  1255. if (is_small(index) && is_tuple(tuple)) {
  1256. Eterm* tp = tuple_val(tuple);
  1257. if ((signed_val(index) >= 1) &&
  1258. (signed_val(index) <= arityval(*tp))) {
  1259. Eterm result = tp[signed_val(index)];
  1260. StoreBifResult(3, result);
  1261. }
  1262. }
  1263. }
  1264. /* Fall through */
  1265. OpCase(badarg_j):
  1266. badarg:
  1267. c_p->freason = BADARG;
  1268. goto lb_Cl_error;
  1269. OpCase(i_fast_element_jIsd): {
  1270. Eterm tuple;
  1271. /*
  1272. * Inlined version of element/2 for even more speed.
  1273. * The first argument is an untagged integer >= 1.
  1274. * The second argument is guaranteed to be a register operand.
  1275. */
  1276. GetArg1(2, tuple);
  1277. if (is_tuple(tuple)) {
  1278. Eterm* tp = tuple_val(tuple);
  1279. tmp_arg2 = Arg(1);
  1280. if (tmp_arg2 <= arityval(*tp)) {
  1281. Eterm result = tp[tmp_arg2];
  1282. StoreBifResult(3, result);
  1283. }
  1284. }
  1285. goto badarg;
  1286. }
  1287. OpCase(catch_yf):
  1288. c_p->catches++;
  1289. yb(Arg(0)) = Arg(1);
  1290. Next(2);
  1291. OpCase(catch_end_y): {
  1292. c_p->catches--;
  1293. make_blank(yb(Arg(0)));
  1294. if (is_non_value(r(0))) {
  1295. if (x(1) == am_throw) {
  1296. r(0) = x(2);
  1297. } else {
  1298. if (x(1) == am_error) {
  1299. SWAPOUT;
  1300. x(2) = add_stacktrace(c_p, x(2), x(3));
  1301. SWAPIN;
  1302. }
  1303. /* only x(2) is included in the rootset here */
  1304. if (E - HTOP < 3 || c_p->mbuf) { /* Force GC in case add_stacktrace()
  1305. * created heap fragments */
  1306. SWAPOUT;
  1307. PROCESS_MAIN_CHK_LOCKS(c_p);
  1308. FCALLS -= erts_garbage_collect(c_p, 3, reg+2, 1);
  1309. PROCESS_MAIN_CHK_LOCKS(c_p);
  1310. SWAPIN;
  1311. }
  1312. r(0) = TUPLE2(HTOP, am_EXIT, x(2));
  1313. HTOP += 3;
  1314. }
  1315. }
  1316. CHECK_TERM(r(0));
  1317. Next(1);
  1318. }
  1319. OpCase(try_end_y): {
  1320. c_p->catches--;
  1321. make_blank(yb(Arg(0)));
  1322. if (is_non_value(r(0))) {
  1323. r(0) = x(1);
  1324. x(1) = x(2);
  1325. x(2) = x(3);
  1326. }
  1327. Next(1);
  1328. }
  1329. /*
  1330. * Skeleton for receive statement:
  1331. *
  1332. * recv_mark L1 Optional
  1333. * call make_ref/monitor Optional
  1334. * ...
  1335. * recv_set L1 Optional
  1336. * L1: <-------------------+
  1337. * <-----------+ |
  1338. * | |
  1339. * loop_rec L2 ------+---+ |
  1340. * ... | | |
  1341. * remove_message | | |
  1342. * jump L3 | | |
  1343. * ... | | |
  1344. * loop_rec_end L1 --+ | |
  1345. * L2: <---------------+ |
  1346. * wait L1 -----------------+ or wait_timeout
  1347. * timeout
  1348. *
  1349. * L3: Code after receive...
  1350. *
  1351. *
  1352. */
  1353. OpCase(recv_mark_f): {
  1354. /*
  1355. * Save the current position in message buffer and the
  1356. * the label for the loop_rec/2 instruction for the
  1357. * the receive statement.
  1358. */
  1359. c_p->msg.mark = (BeamInstr *) Arg(0);
  1360. c_p->msg.saved_last = c_p->msg.last;
  1361. Next(1);
  1362. }
  1363. OpCase(i_recv_set): {
  1364. /*
  1365. * If the mark is valid (points to the loop_rec/2
  1366. * instruction that follows), we know that the saved
  1367. * position points to the first message that could
  1368. * possibly be matched out.
  1369. *
  1370. * If the mark is invalid, we do nothing, meaning that
  1371. * we will look through all messages in the message queue.
  1372. */
  1373. if (c_p->msg.mark == (BeamInstr *) (I+1)) {
  1374. c_p->msg.save = c_p->msg.saved_last;
  1375. }
  1376. I++;
  1377. /* Fall through to the loop_rec/2 instruction */
  1378. }
  1379. /*
  1380. * Pick up the next message and place it in x(0).
  1381. * If no message, jump to a wait or wait_timeout instruction.
  1382. */
  1383. OpCase(i_loop_rec_fr):
  1384. {
  1385. BeamInstr *next;
  1386. ErlMessage* msgp;
  1387. loop_rec__:
  1388. PROCESS_MAIN_CHK_LOCKS(c_p);
  1389. msgp = PEEK_MESSAGE(c_p);
  1390. if (!msgp) {
  1391. #ifdef ERTS_SMP
  1392. erts_smp_proc_lock(c_p, ERTS_PROC_LOCKS_MSG_RECEIVE);
  1393. /* Make sure messages wont pass exit signals... */
  1394. if (ERTS_PROC_PENDING_EXIT(c_p)) {
  1395. erts_smp_proc_unlock(c_p, ERTS_PROC_LOCKS_MSG_RECEIVE);
  1396. SWAPOUT;
  1397. goto do_schedule; /* Will be rescheduled for exit */
  1398. }
  1399. ERTS_SMP_MSGQ_MV_INQ2PRIVQ(c_p);
  1400. msgp = PEEK_MESSAGE(c_p);
  1401. if (msgp)
  1402. erts_smp_proc_unlock(c_p, ERTS_PROC_LOCKS_MSG_RECEIVE);
  1403. else {
  1404. #endif
  1405. SET_I((BeamInstr *) Arg(0));
  1406. Goto(*I); /* Jump to a wait or wait_timeout instruction */
  1407. #ifdef ERTS_SMP
  1408. }
  1409. #endif
  1410. }
  1411. ErtsMoveMsgAttachmentIntoProc(msgp, c_p, E, HTOP, FCALLS,
  1412. {
  1413. SWAPOUT;
  1414. reg[0] = r(0);
  1415. PROCESS_MAIN_CHK_LOCKS(c_p);
  1416. },
  1417. {
  1418. PROCESS_MAIN_CHK_LOCKS(c_p);
  1419. r(0) = reg[0];
  1420. SWAPIN;
  1421. });
  1422. if (is_non_value(ERL_MESSAGE_TERM(msgp))) {
  1423. /*
  1424. * A corrupt distribution message that we weren't able to decode;
  1425. * remove it...
  1426. */
  1427. ASSERT(!msgp->data.attached);
  1428. UNLINK_MESSAGE(c_p, msgp);
  1429. free_message(msgp);
  1430. goto loop_rec__;
  1431. }
  1432. PreFetch(1, next);
  1433. r(0) = ERL_MESSAGE_TERM(msgp);
  1434. NextPF(1, next);
  1435. }
  1436. /*
  1437. * Remove a (matched) message from the message queue.
  1438. */
  1439. OpCase(remove_message): {
  1440. BeamInstr *next;
  1441. ErlMessage* msgp;
  1442. PROCESS_MAIN_CHK_LOCKS(c_p);
  1443. PreFetch(0, next);
  1444. msgp = PEEK_MESSAGE(c_p);
  1445. if (ERTS_PROC_GET_SAVED_CALLS_BUF(c_p)) {
  1446. save_calls(c_p, &exp_receive);
  1447. }
  1448. if (ERL_MESSAGE_TOKEN(msgp) == NIL) {
  1449. SEQ_TRACE_TOKEN(c_p) = NIL;
  1450. } else if (ERL_MESSAGE_TOKEN(msgp) != am_undefined) {
  1451. Eterm msg;
  1452. SEQ_TRACE_TOKEN(c_p) = ERL_MESSAGE_TOKEN(msgp);
  1453. ASSERT(is_tuple(SEQ_TRACE_TOKEN(c_p)));
  1454. ASSERT(SEQ_TRACE_TOKEN_ARITY(c_p) == 5);
  1455. ASSERT(is_small(SEQ_TRACE_TOKEN_SERIAL(c_p)));
  1456. ASSERT(is_small(SEQ_TRACE_TOKEN_LASTCNT(c_p)));
  1457. ASSERT(is_small(SEQ_TRACE_TOKEN_FLAGS(c_p)));
  1458. ASSERT(is_pid(SEQ_TRACE_TOKEN_SENDER(c_p)));
  1459. c_p->seq_trace_lastcnt = unsigned_val(SEQ_TRACE_TOKEN_SERIAL(c_p));
  1460. if (c_p->seq_trace_clock < unsigned_val(SEQ_TRACE_TOKEN_SERIAL(c_p))) {
  1461. c_p->seq_trace_clock = unsigned_val(SEQ_TRACE_TOKEN_SERIAL(c_p));
  1462. }
  1463. msg = ERL_MESSAGE_TERM(msgp);
  1464. seq_trace_output(SEQ_TRACE_TOKEN(c_p), msg, SEQ_TRACE_RECEIVE,
  1465. c_p->id, c_p);
  1466. }
  1467. UNLINK_MESSAGE(c_p, msgp);
  1468. JOIN_MESSAGE(c_p);
  1469. CANCEL_TIMER(c_p);
  1470. free_message(msgp);
  1471. PROCESS_MAIN_CHK_LOCKS(c_p);
  1472. NextPF(0, next);
  1473. }
  1474. /*
  1475. * Advance the save pointer to the next message (the current
  1476. * message didn't match), then jump to the loop_rec instruction.
  1477. */
  1478. OpCase(loop_rec_end_f): {
  1479. SET_I((BeamInstr *) Arg(0));
  1480. SAVE_MESSAGE(c_p);
  1481. goto loop_rec__;
  1482. }
  1483. /*
  1484. * Prepare to wait for a message or a timeout, whichever occurs first.
  1485. *
  1486. * Note: In order to keep the compatibility between 32 and 64 bits
  1487. * emulators, only timeout values that can be represented in 32 bits
  1488. * (unsigned) or less are allowed.
  1489. */
  1490. OpCase(i_wait_timeout_fs): {
  1491. erts_smp_proc_lock(c_p, ERTS_PROC_LOCKS_MSG_RECEIVE);
  1492. /* Fall through */
  1493. }
  1494. OpCase(i_wait_timeout_locked_fs): {
  1495. Eterm timeout_value;
  1496. /*
  1497. * If we have already set the timer, we must NOT set it again. Therefore,
  1498. * we must test the F_INSLPQUEUE flag as well as the F_TIMO flag.
  1499. */
  1500. if (c_p->flags & (F_INSLPQUEUE | F_TIMO)) {
  1501. goto wait2;
  1502. }
  1503. GetArg1(1, timeout_value);
  1504. if (timeout_value != make_small(0)) {
  1505. #if !defined(ARCH_64) || HALFWORD_HEAP
  1506. Uint time_val;
  1507. #endif
  1508. if (is_small(timeout_value) && signed_val(timeout_value) > 0 &&
  1509. #if defined(ARCH_64) && !HALFWORD_HEAP
  1510. ((unsigned_val(timeout_value) >> 32) == 0)
  1511. #else
  1512. 1
  1513. #endif
  1514. ) {
  1515. /*
  1516. * The timer routiner will set c_p->i to the value in
  1517. * c_p->def_arg_reg[0]. Note that it is safe to use this
  1518. * location because there are no living x registers in
  1519. * a receive statement.
  1520. * Note that for the halfword emulator, the two first elements
  1521. * of the array are used.
  1522. */
  1523. *((BeamInstr **) (UWord) c_p->def_arg_reg) = I+3;
  1524. set_timer(c_p, unsigned_val(timeout_value));
  1525. } else if (timeout_value == am_infinity) {
  1526. c_p->flags |= F_TIMO;
  1527. #if !defined(ARCH_64) || HALFWORD_HEAP
  1528. } else if (term_to_Uint(timeout_value, &time_val)) {
  1529. *((BeamInstr **) (UWord) c_p->def_arg_reg) = I+3;
  1530. set_timer(c_p, time_val);
  1531. #endif
  1532. } else { /* Wrong time */
  1533. OpCase(i_wait_error_locked): {
  1534. erts_smp_proc_unlock(c_p, ERTS_PROC_LOCKS_MSG_RECEIVE);
  1535. /* Fall through */
  1536. }
  1537. OpCase(i_wait_error): {
  1538. c_p->freason = EXC_TIMEOUT_VALUE;
  1539. goto find_func_info;
  1540. }
  1541. }
  1542. /*
  1543. * Prepare to wait indefinitely for a new message to arrive
  1544. * (or the time set above if falling through from above).
  1545. *
  1546. * When a new message arrives, control will be transferred
  1547. * the loop_rec instruction (at label L1). In case of
  1548. * of timeout, control will be transferred to the timeout
  1549. * instruction following the wait_timeout instruction.
  1550. */
  1551. OpCase(wait_locked_f):
  1552. OpCase(wait_f):
  1553. wait2: {
  1554. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  1555. c_p->i = (BeamInstr *) Arg(0); /* L1 */
  1556. SWAPOUT;
  1557. c_p->arity = 0;
  1558. c_p->status = P_WAITING;
  1559. erts_smp_proc_unlock(c_p, ERTS_PROC_LOCKS_MSG_RECEIVE);
  1560. c_p->current = NULL;
  1561. goto do_schedule;
  1562. }
  1563. OpCase(wait_unlocked_f): {
  1564. erts_smp_proc_lock(c_p, ERTS_PROC_LOCKS_MSG_RECEIVE);
  1565. goto wait2;
  1566. }
  1567. }
  1568. erts_smp_proc_unlock(c_p, ERTS_PROC_LOCKS_MSG_RECEIVE);
  1569. Next(2);
  1570. }
  1571. OpCase(i_wait_timeout_fI): {
  1572. erts_smp_proc_lock(c_p, ERTS_PROC_LOCKS_MSG_RECEIVE);
  1573. }
  1574. OpCase(i_wait_timeout_locked_fI):
  1575. {
  1576. /*
  1577. * If we have already set the timer, we must NOT set it again. Therefore,
  1578. * we must test the F_INSLPQUEUE flag as well as the F_TIMO flag.
  1579. */
  1580. if ((c_p->flags & (F_INSLPQUEUE | F_TIMO)) == 0) {
  1581. *((BeamInstr **) (UWord) c_p->def_arg_reg) = I+3;
  1582. set_timer(c_p, Arg(1));
  1583. }
  1584. goto wait2;
  1585. }
  1586. /*
  1587. * A timeout has occurred. Reset the save pointer so that the next
  1588. * receive statement will examine the first message first.
  1589. */
  1590. OpCase(timeout_locked): {
  1591. erts_smp_proc_unlock(c_p, ERTS_PROC_LOCKS_MSG_RECEIVE);
  1592. }
  1593. OpCase(timeout): {
  1594. BeamInstr *next;
  1595. PreFetch(0, next);
  1596. if (IS_TRACED_FL(c_p, F_TRACE_RECEIVE)) {
  1597. trace_receive(c_p, am_timeout);
  1598. }
  1599. if (ERTS_PROC_GET_SAVED_CALLS_BUF(c_p)) {
  1600. save_calls(c_p, &exp_timeout);
  1601. }
  1602. c_p->flags &= ~F_TIMO;
  1603. JOIN_MESSAGE(c_p);
  1604. NextPF(0, next);
  1605. }
  1606. OpCase(i_select_val_sfI):
  1607. GetArg1(0, tmp_arg1);
  1608. do_binary_search:
  1609. {
  1610. struct Pairs {
  1611. BeamInstr val;
  1612. BeamInstr* addr;
  1613. };
  1614. struct Pairs* low;
  1615. struct Pairs* high;
  1616. struct Pairs* mid;
  1617. int bdiff; /* int not long because the arrays aren't that large */
  1618. low = (struct Pairs *) &Arg(3);
  1619. high = low + Arg(2);
  1620. /* The pointer subtraction (high-low) below must produce
  1621. * a signed result, because high could be < low. That
  1622. * requires the compiler to insert quite a bit of code.
  1623. *
  1624. * However, high will be > low so the result will be
  1625. * positive. We can use that knowledge to optimise the
  1626. * entire sequence, from the initial comparison to the
  1627. * computation of mid.
  1628. *
  1629. * -- Mikael Pettersson, Acumem AB
  1630. *
  1631. * Original loop control code:
  1632. *
  1633. * while (low < high) {
  1634. * mid = low + (high-low) / 2;
  1635. *
  1636. */
  1637. while ((bdiff = (int)((char*)high - (char*)low)) > 0) {
  1638. unsigned int boffset = ((unsigned int)bdiff >> 1) & ~(sizeof(struct Pairs)-1);
  1639. mid = (struct Pairs*)((char*)low + boffset);
  1640. if (tmp_arg1 < mid->val) {
  1641. high = mid;
  1642. } else if (tmp_arg1 > mid->val) {
  1643. low = mid + 1;
  1644. } else {
  1645. SET_I(mid->addr);
  1646. Goto(*I);
  1647. }
  1648. }
  1649. SET_I((BeamInstr *) Arg(1));
  1650. Goto(*I);
  1651. }
  1652. OpCase(i_jump_on_val_zero_sfI):
  1653. {
  1654. Eterm index;
  1655. GetArg1(0, index);
  1656. if (is_small(index)) {
  1657. index = signed_val(index);
  1658. if (index < Arg(2)) {
  1659. SET_I((BeamInstr *) (&Arg(3))[index]);
  1660. Goto(*I);
  1661. }
  1662. }
  1663. SET_I((BeamInstr *) Arg(1));
  1664. Goto(*I);
  1665. }
  1666. OpCase(i_jump_on_val_sfII):
  1667. {
  1668. Eterm index;
  1669. GetArg1(0, index);
  1670. if (is_small(index)) {
  1671. index = (Uint) (signed_val(index) - Arg(3));
  1672. if (index < Arg(2)) {
  1673. SET_I((BeamInstr *) (&Arg(4))[index]);
  1674. Goto(*I);
  1675. }
  1676. }
  1677. SET_I((BeamInstr *) Arg(1));
  1678. Goto(*I);
  1679. }
  1680. /*
  1681. * All guards with zero arguments have special instructions:
  1682. * self/0
  1683. * node/0
  1684. *
  1685. * All other guard BIFs take one or two arguments.
  1686. */
  1687. /*
  1688. * Guard BIF in head. On failure, ignore the error and jump
  1689. * to the code for the next clause. We don't support tracing
  1690. * of guard BIFs.
  1691. */
  1692. OpCase(bif1_fbsd):
  1693. {
  1694. Eterm (*bf)(Process*, Eterm);
  1695. Eterm arg;
  1696. Eterm result;
  1697. GetArg1(2, arg);
  1698. bf = (BifFunction) Arg(1);
  1699. c_p->fcalls = FCALLS;
  1700. PROCESS_MAIN_CHK_LOCKS(c_p);
  1701. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  1702. result = (*bf)(c_p, arg);
  1703. ASSERT(!ERTS_PROC_IS_EXITING(c_p) || is_non_value(result));
  1704. PROCESS_MAIN_CHK_LOCKS(c_p);
  1705. ERTS_HOLE_CHECK(c_p);
  1706. FCALLS = c_p->fcalls;
  1707. if (is_value(result)) {
  1708. StoreBifResult(3, result);
  1709. }
  1710. SET_I((BeamInstr *) Arg(0));
  1711. Goto(*I);
  1712. }
  1713. /*
  1714. * Guard BIF in body. It can fail like any BIF. No trace support.
  1715. */
  1716. OpCase(bif1_body_bsd):
  1717. {
  1718. Eterm (*bf)(Process*, Eterm);
  1719. Eterm arg;
  1720. Eterm result;
  1721. GetArg1(1, arg);
  1722. bf = (BifFunction) Arg(0);
  1723. c_p->fcalls = FCALLS;
  1724. PROCESS_MAIN_CHK_LOCKS(c_p);
  1725. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  1726. result = (*bf)(c_p, arg);
  1727. ASSERT(!ERTS_PROC_IS_EXITING(c_p) || is_non_value(result));
  1728. PROCESS_MAIN_CHK_LOCKS(c_p);
  1729. ERTS_HOLE_CHECK(c_p);
  1730. FCALLS = c_p->fcalls;
  1731. if (is_value(result)) {
  1732. StoreBifResult(2, result);
  1733. }
  1734. reg[0] = arg;
  1735. SWAPOUT;
  1736. I = handle_error(c_p, I, reg, bf);
  1737. goto post_error_handling;
  1738. }
  1739. OpCase(i_gc_bif1_jIsId):
  1740. {
  1741. typedef Eterm (*GcBifFunction)(Process*, Eterm*, Uint);
  1742. GcBifFunction bf;
  1743. Eterm arg;
  1744. Eterm result;
  1745. Uint live = (Uint) Arg(3);
  1746. GetArg1(2, arg);
  1747. reg[0] = r(0);
  1748. reg[live] = arg;
  1749. bf = (GcBifFunction) Arg(1);
  1750. c_p->fcalls = FCALLS;
  1751. SWAPOUT;
  1752. PROCESS_MAIN_CHK_LOCKS(c_p);
  1753. ERTS_SMP_UNREQ_PROC_MAIN_LOCK(c_p);
  1754. result = (*bf)(c_p, reg, live);
  1755. ERTS_SMP_REQ_PROC_MAIN_LOCK(c_p);
  1756. PROCESS_MAIN_CHK_LOCKS(c_p);
  1757. SWAPIN;
  1758. r(0) = reg[0];
  1759. ERTS_HOLE_CHECK(c_p);
  1760. FCALLS = c_p->fcalls;
  1761. if (is_value(result)) {
  1762. StoreBifResult(4, result);
  1763. }
  1764. if (Arg(0) != 0) {
  1765. SET_I((BeamInstr *) Arg(0));
  1766. Goto(*I);
  1767. }
  1768. reg[0] = arg;
  1769. I = handle_error(c_p, I, reg, translate_gc_bif((void *) bf));
  1770. goto post_error_handling;
  1771. }
  1772. OpCase(i_gc_bif2_jIId): /* Note, one less parameter than the i_gc_bif1
  1773. and i_gc_bif3 */
  1774. {
  1775. typedef Eterm (*GcBifFunction)(Process*, Eterm*, Uint);
  1776. GcBifFunction bf;
  1777. Eterm result;
  1778. Uint live = (Uint) Arg(2);
  1779. reg[0] = r(0);
  1780. reg[live++] = tmp_arg1;
  1781. reg[live] = tmp_arg2;
  1782. bf = (GcBifFunction) Arg(1);
  1783. c_p->fcalls = FCALLS;
  1784. SWAPOUT;
  1785. PROCESS_MAIN_CHK_LOCKS(c_p);
  1786. ERTS_SMP_UNREQ_PROC_MAIN_LOCK(c_p);
  1787. result = (*bf)(c_p, reg, live);
  1788. ERTS_SMP_REQ_PROC_MAIN_LOCK(c_p);
  1789. PROCESS_MAIN_CHK_LOCKS(c_p);
  1790. SWAPIN;
  1791. r(0) = reg[0];
  1792. ERTS_HOLE_CHECK(c_p);
  1793. FCALLS = c_p->fcalls;
  1794. if (is_value(result)) {
  1795. StoreBifResult(3, result);
  1796. }
  1797. if (Arg(0) != 0) {
  1798. SET_I((BeamInstr *) Arg(0));
  1799. Goto(*I);
  1800. }
  1801. reg[0] = tmp_arg1;
  1802. reg[1] = tmp_arg2;
  1803. I = handle_error(c_p, I, reg, translate_gc_bif((void *) bf));
  1804. goto post_error_handling;
  1805. }
  1806. OpCase(i_gc_bif3_jIsId):
  1807. {
  1808. typedef Eterm (*GcBifFunction)(Process*, Eterm*, Uint);
  1809. GcBifFunction bf;
  1810. Eterm arg;
  1811. Eterm result;
  1812. Uint live = (Uint) Arg(3);
  1813. GetArg1(2, arg);
  1814. reg[0] = r(0);
  1815. reg[live++] = arg;
  1816. reg[live++] = tmp_arg1;
  1817. reg[live] = tmp_arg2;
  1818. bf = (GcBifFunction) Arg(1);
  1819. c_p->fcalls = FCALLS;
  1820. SWAPOUT;
  1821. PROCESS_MAIN_CHK_LOCKS(c_p);
  1822. ERTS_SMP_UNREQ_PROC_MAIN_LOCK(c_p);
  1823. result = (*bf)(c_p, reg, live);
  1824. ERTS_SMP_REQ_PROC_MAIN_LOCK(c_p);
  1825. PROCESS_MAIN_CHK_LOCKS(c_p);
  1826. SWAPIN;
  1827. r(0) = reg[0];
  1828. ERTS_HOLE_CHECK(c_p);
  1829. FCALLS = c_p->fcalls;
  1830. if (is_value(result)) {
  1831. StoreBifResult(4, result);
  1832. }
  1833. if (Arg(0) != 0) {
  1834. SET_I((BeamInstr *) Arg(0));
  1835. Goto(*I);
  1836. }
  1837. reg[0] = arg;
  1838. reg[1] = tmp_arg1;
  1839. reg[2] = tmp_arg2;
  1840. I = handle_error(c_p, I, reg, translate_gc_bif((void *) bf));
  1841. goto post_error_handling;
  1842. }
  1843. /*
  1844. * Guards bifs and, or, xor in guards.
  1845. */
  1846. OpCase(i_bif2_fbd):
  1847. {
  1848. Eterm (*bf)(Process*, Eterm, Eterm);
  1849. Eterm result;
  1850. bf = (BifFunction) Arg(1);
  1851. c_p->fcalls = FCALLS;
  1852. PROCESS_MAIN_CHK_LOCKS(c_p);
  1853. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  1854. result = (*bf)(c_p, tmp_arg1, tmp_arg2);
  1855. ASSERT(!ERTS_PROC_IS_EXITING(c_p) || is_non_value(result));
  1856. PROCESS_MAIN_CHK_LOCKS(c_p);
  1857. ERTS_HOLE_CHECK(c_p);
  1858. FCALLS = c_p->fcalls;
  1859. if (is_value(result)) {
  1860. StoreBifResult(2, result);
  1861. }
  1862. SET_I((BeamInstr *) Arg(0));
  1863. Goto(*I);
  1864. }
  1865. /*
  1866. * Guards bifs and, or, xor, relational operators in body.
  1867. */
  1868. OpCase(i_bif2_body_bd):
  1869. {
  1870. Eterm (*bf)(Process*, Eterm, Eterm);
  1871. Eterm result;
  1872. bf = (BifFunction) Arg(0);
  1873. PROCESS_MAIN_CHK_LOCKS(c_p);
  1874. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  1875. result = (*bf)(c_p, tmp_arg1, tmp_arg2);
  1876. ASSERT(!ERTS_PROC_IS_EXITING(c_p) || is_non_value(result));
  1877. PROCESS_MAIN_CHK_LOCKS(c_p);
  1878. ERTS_HOLE_CHECK(c_p);
  1879. if (is_value(result)) {
  1880. ASSERT(!is_CP(result));
  1881. StoreBifResult(1, result);
  1882. }
  1883. reg[0] = tmp_arg1;
  1884. reg[1] = tmp_arg2;
  1885. SWAPOUT;
  1886. I = handle_error(c_p, I, reg, bf);
  1887. goto post_error_handling;
  1888. }
  1889. /*
  1890. * The most general BIF call. The BIF may build any amount of data
  1891. * on the heap. The result is always returned in r(0).
  1892. */
  1893. OpCase(call_bif0_e):
  1894. {
  1895. Eterm (*bf)(Process*, BeamInstr*) = GET_BIF_ADDRESS(Arg(0));
  1896. PRE_BIF_SWAPOUT(c_p);
  1897. c_p->fcalls = FCALLS - 1;
  1898. if (FCALLS <= 0) {
  1899. save_calls(c_p, (Export *) Arg(0));
  1900. }
  1901. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  1902. r(0) = (*bf)(c_p, I);
  1903. ASSERT(!ERTS_PROC_IS_EXITING(c_p) || is_non_value(r(0)));
  1904. ERTS_HOLE_CHECK(c_p);
  1905. POST_BIF_GC_SWAPIN_0(c_p, r(0));
  1906. FCALLS = c_p->fcalls;
  1907. if (is_value(r(0))) {
  1908. CHECK_TERM(r(0));
  1909. Next(1);
  1910. }
  1911. else if (c_p->freason == TRAP) {
  1912. goto call_bif_trap3;
  1913. }
  1914. /*
  1915. * Error handling. SWAPOUT is not needed because it was done above.
  1916. */
  1917. ASSERT(c_p->stop == E);
  1918. reg[0] = r(0);
  1919. I = handle_error(c_p, I, reg, bf);
  1920. goto post_error_handling;
  1921. }
  1922. OpCase(call_bif1_e):
  1923. {
  1924. Eterm (*bf)(Process*, Eterm, BeamInstr*) = GET_BIF_ADDRESS(Arg(0));
  1925. Eterm result;
  1926. BeamInstr *next;
  1927. c_p->fcalls = FCALLS - 1;
  1928. if (FCALLS <= 0) {
  1929. save_calls(c_p, (Export *) Arg(0));
  1930. }
  1931. PreFetch(1, next);
  1932. PRE_BIF_SWAPOUT(c_p);
  1933. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  1934. result = (*bf)(c_p, r(0), I);
  1935. ASSERT(!ERTS_PROC_IS_EXITING(c_p) || is_non_value(result));
  1936. ERTS_HOLE_CHECK(c_p);
  1937. POST_BIF_GC_SWAPIN(c_p, result, reg, 1);
  1938. FCALLS = c_p->fcalls;
  1939. if (is_value(result)) {
  1940. r(0) = result;
  1941. CHECK_TERM(r(0));
  1942. NextPF(1, next);
  1943. } else if (c_p->freason == TRAP) {
  1944. goto call_bif_trap3;
  1945. }
  1946. /*
  1947. * Error handling. SWAPOUT is not needed because it was done above.
  1948. */
  1949. ASSERT(c_p->stop == E);
  1950. reg[0] = r(0);
  1951. I = handle_error(c_p, I, reg, bf);
  1952. goto post_error_handling;
  1953. }
  1954. OpCase(call_bif2_e):
  1955. {
  1956. Eterm (*bf)(Process*, Eterm, Eterm, BeamInstr*) = GET_BIF_ADDRESS(Arg(0));
  1957. Eterm result;
  1958. BeamInstr *next;
  1959. PRE_BIF_SWAPOUT(c_p);
  1960. c_p->fcalls = FCALLS - 1;
  1961. if (FCALLS <= 0) {
  1962. save_calls(c_p, (Export *) Arg(0));
  1963. }
  1964. PreFetch(1, next);
  1965. CHECK_TERM(r(0));
  1966. CHECK_TERM(x(1));
  1967. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  1968. result = (*bf)(c_p, r(0), x(1), I);
  1969. ASSERT(!ERTS_PROC_IS_EXITING(c_p) || is_non_value(result));
  1970. ERTS_HOLE_CHECK(c_p);
  1971. POST_BIF_GC_SWAPIN(c_p, result, reg, 2);
  1972. FCALLS = c_p->fcalls;
  1973. if (is_value(result)) {
  1974. r(0) = result;
  1975. CHECK_TERM(r(0));
  1976. NextPF(1, next);
  1977. } else if (c_p->freason == TRAP) {
  1978. goto call_bif_trap3;
  1979. }
  1980. /*
  1981. * Error handling. SWAPOUT is not needed because it was done above.
  1982. */
  1983. ASSERT(c_p->stop == E);
  1984. reg[0] = r(0);
  1985. I = handle_error(c_p, I, reg, bf);
  1986. goto post_error_handling;
  1987. }
  1988. OpCase(call_bif3_e):
  1989. {
  1990. Eterm (*bf)(Process*, Eterm, Eterm, Eterm, BeamInstr*) = GET_BIF_ADDRESS(Arg(0));
  1991. Eterm result;
  1992. BeamInstr *next;
  1993. PRE_BIF_SWAPOUT(c_p);
  1994. c_p->fcalls = FCALLS - 1;
  1995. if (FCALLS <= 0) {
  1996. save_calls(c_p, (Export *) Arg(0));
  1997. }
  1998. PreFetch(1, next);
  1999. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  2000. result = (*bf)(c_p, r(0), x(1), x(2), I);
  2001. ASSERT(!ERTS_PROC_IS_EXITING(c_p) || is_non_value(result));
  2002. ERTS_HOLE_CHECK(c_p);
  2003. POST_BIF_GC_SWAPIN(c_p, result, reg, 3);
  2004. FCALLS = c_p->fcalls;
  2005. if (is_value(result)) {
  2006. r(0) = result;
  2007. CHECK_TERM(r(0));
  2008. NextPF(1, next);
  2009. } else if (c_p->freason == TRAP) {
  2010. call_bif_trap3:
  2011. SET_CP(c_p, I+2);
  2012. SET_I(*((BeamInstr **) (UWord) ((c_p)->def_arg_reg + 3)));
  2013. SWAPIN;
  2014. r(0) = c_p->def_arg_reg[0];
  2015. x(1) = c_p->def_arg_reg[1];
  2016. x(2) = c_p->def_arg_reg[2];
  2017. Dispatch();
  2018. }
  2019. /*
  2020. * Error handling. SWAPOUT is not needed because it was done above.
  2021. */
  2022. ASSERT(c_p->stop == E);
  2023. reg[0] = r(0);
  2024. I = handle_error(c_p, I, reg, bf);
  2025. goto post_error_handling;
  2026. }
  2027. /*
  2028. * Arithmetic operations.
  2029. */
  2030. OpCase(i_times_jId):
  2031. {
  2032. arith_func = ARITH_FUNC(mixed_times);
  2033. goto do_big_arith2;
  2034. }
  2035. OpCase(i_m_div_jId):
  2036. {
  2037. arith_func = ARITH_FUNC(mixed_div);
  2038. goto do_big_arith2;
  2039. }
  2040. OpCase(i_int_div_jId):
  2041. {
  2042. Eterm result;
  2043. if (tmp_arg2 == SMALL_ZERO) {
  2044. goto badarith;
  2045. } else if (is_both_small(tmp_arg1, tmp_arg2)) {
  2046. Sint ires = signed_val(tmp_arg1) / signed_val(tmp_arg2);
  2047. if (MY_IS_SSMALL(ires)) {
  2048. result = make_small(ires);
  2049. STORE_ARITH_RESULT(result);
  2050. }
  2051. }
  2052. arith_func = ARITH_FUNC(int_div);
  2053. goto do_big_arith2;
  2054. }
  2055. OpCase(i_rem_jId):
  2056. {
  2057. Eterm result;
  2058. if (tmp_arg2 == SMALL_ZERO) {
  2059. goto badarith;
  2060. } else if (is_both_small(tmp_arg1, tmp_arg2)) {
  2061. result = make_small(signed_val(tmp_arg1) % signed_val(tmp_arg2));
  2062. STORE_ARITH_RESULT(result);
  2063. } else {
  2064. arith_func = ARITH_FUNC(int_rem);
  2065. goto do_big_arith2;
  2066. }
  2067. }
  2068. OpCase(i_band_jId):
  2069. {
  2070. Eterm result;
  2071. if (is_both_small(tmp_arg1, tmp_arg2)) {
  2072. /*
  2073. * No need to untag -- TAG & TAG == TAG.
  2074. */
  2075. result = tmp_arg1 & tmp_arg2;
  2076. STORE_ARITH_RESULT(result);
  2077. }
  2078. arith_func = ARITH_FUNC(band);
  2079. goto do_big_arith2;
  2080. }
  2081. do_big_arith2:
  2082. {
  2083. Eterm result;
  2084. Uint live = Arg(1);
  2085. SWAPOUT;
  2086. reg[0] = r(0);
  2087. reg[live] = tmp_arg1;
  2088. reg[live+1] = tmp_arg2;
  2089. result = arith_func(c_p, reg, live);
  2090. r(0) = reg[0];
  2091. SWAPIN;
  2092. ERTS_HOLE_CHECK(c_p);
  2093. if (is_value(result)) {
  2094. STORE_ARITH_RESULT(result);
  2095. }
  2096. goto lb_Cl_error;
  2097. }
  2098. /*
  2099. * An error occured in an arithmetic operation or test that could
  2100. * appear either in a head or in a body.
  2101. * In a head, execution should continue at failure address in Arg(0).
  2102. * In a body, Arg(0) == 0 and an exception should be raised.
  2103. */
  2104. lb_Cl_error: {
  2105. if (Arg(0) != 0) {
  2106. OpCase(jump_f): {
  2107. SET_I((BeamInstr *) Arg(0));
  2108. Goto(*I);
  2109. }
  2110. }
  2111. ASSERT(c_p->freason != BADMATCH || is_value(c_p->fvalue));
  2112. goto find_func_info;
  2113. }
  2114. OpCase(i_bor_jId):
  2115. {
  2116. Eterm result;
  2117. if (is_both_small(tmp_arg1, tmp_arg2)) {
  2118. /*
  2119. * No need to untag -- TAG | TAG == TAG.
  2120. */
  2121. result = tmp_arg1 | tmp_arg2;
  2122. STORE_ARITH_RESULT(result);
  2123. }
  2124. arith_func = ARITH_FUNC(bor);
  2125. goto do_big_arith2;
  2126. }
  2127. OpCase(i_bxor_jId):
  2128. {
  2129. Eterm result;
  2130. if (is_both_small(tmp_arg1, tmp_arg2)) {
  2131. /*
  2132. * We could extract the tag from one argument, but a tag extraction
  2133. * could mean a shift. Therefore, play it safe here.
  2134. */
  2135. result = make_small(signed_val(tmp_arg1) ^ signed_val(tmp_arg2));
  2136. STORE_ARITH_RESULT(result);
  2137. }
  2138. arith_func = ARITH_FUNC(bxor);
  2139. goto do_big_arith2;
  2140. }
  2141. {
  2142. Sint i;
  2143. Sint ires;
  2144. Eterm* bigp;
  2145. OpCase(i_bsr_jId):
  2146. if (is_small(tmp_arg2)) {
  2147. i = -signed_val(tmp_arg2);
  2148. if (is_small(tmp_arg1)) {
  2149. goto small_shift;
  2150. } else if (is_big(tmp_arg1)) {
  2151. if (i == 0) {
  2152. StoreBifResult(2, tmp_arg1);
  2153. }
  2154. goto big_shift;
  2155. }
  2156. } else if (is_big(tmp_arg2)) {
  2157. /*
  2158. * N bsr NegativeBigNum == N bsl MAX_SMALL
  2159. * N bsr PositiveBigNum == N bsl MIN_SMALL
  2160. */
  2161. tmp_arg2 = make_small(bignum_header_is_neg(*big_val(tmp_arg2)) ?
  2162. MAX_SMALL : MIN_SMALL);
  2163. goto do_bsl;
  2164. }
  2165. goto badarith;
  2166. OpCase(i_bsl_jId):
  2167. do_bsl:
  2168. if (is_small(tmp_arg2)) {
  2169. i = signed_val(tmp_arg2);
  2170. if (is_small(tmp_arg1)) {
  2171. small_shift:
  2172. ires = signed_val(tmp_arg1);
  2173. if (i == 0 || ires == 0) {
  2174. StoreBifResult(2, tmp_arg1);
  2175. } else if (i < 0) { /* Right shift */
  2176. i = -i;
  2177. if (i >= SMALL_BITS-1) {
  2178. tmp_arg1 = (ires < 0) ? SMALL_MINUS_ONE : SMALL_ZERO;
  2179. } else {
  2180. tmp_arg1 = make_small(ires >> i);
  2181. }
  2182. StoreBifResult(2, tmp_arg1);
  2183. } else if (i < SMALL_BITS-1) { /* Left shift */
  2184. if ((ires > 0 && ((~(Uint)0 << ((SMALL_BITS-1)-i)) & ires) == 0) ||
  2185. ((~(Uint)0 << ((SMALL_BITS-1)-i)) & ~ires) == 0) {
  2186. tmp_arg1 = make_small(ires << i);
  2187. StoreBifResult(2, tmp_arg1);
  2188. }
  2189. }
  2190. tmp_arg1 = small_to_big(ires, tmp_big);
  2191. big_shift:
  2192. if (i > 0) { /* Left shift. */
  2193. ires = big_size(tmp_arg1) + (i / D_EXP);
  2194. } else { /* Right shift. */
  2195. ires = big_size(tmp_arg1);
  2196. if (ires <= (-i / D_EXP))
  2197. ires = 3; /* ??? */
  2198. else
  2199. ires -= (-i / D_EXP);
  2200. }
  2201. {
  2202. ires = BIG_NEED_SIZE(ires+1);
  2203. /*
  2204. * Slightly conservative check the size to avoid
  2205. * allocating huge amounts of memory for bignums that
  2206. * clearly would overflow the arity in the header
  2207. * word.
  2208. */
  2209. if (ires-8 > BIG_ARITY_MAX) {
  2210. c_p->freason = SYSTEM_LIMIT;
  2211. goto lb_Cl_error;
  2212. }
  2213. TestHeapPreserve(ires+1, Arg(1), tmp_arg1);
  2214. bigp = HTOP;
  2215. tmp_arg1 = big_lshift(tmp_arg1, i, bigp);
  2216. if (is_big(tmp_arg1)) {
  2217. HTOP += bignum_header_arity(*HTOP) + 1;
  2218. }
  2219. HEAP_SPACE_VERIFIED(0);
  2220. if (is_nil(tmp_arg1)) {
  2221. /*
  2222. * This result must have been only slight larger
  2223. * than allowed since it wasn't caught by the
  2224. * previous test.
  2225. */
  2226. c_p->freason = SYSTEM_LIMIT;
  2227. goto lb_Cl_error;
  2228. }
  2229. ERTS_HOLE_CHECK(c_p);
  2230. StoreBifResult(2, tmp_arg1);
  2231. }
  2232. } else if (is_big(tmp_arg1)) {
  2233. if (i == 0) {
  2234. StoreBifResult(2, tmp_arg1);
  2235. }
  2236. goto big_shift;
  2237. }
  2238. } else if (is_big(tmp_arg2)) {
  2239. if (bignum_header_is_neg(*big_val(tmp_arg2))) {
  2240. /*
  2241. * N bsl NegativeBigNum is either 0 or -1, depending on
  2242. * the sign of N. Since we don't believe this case
  2243. * is common, do the calculation with the minimum
  2244. * amount of code.
  2245. */
  2246. tmp_arg2 = make_small(MIN_SMALL);
  2247. goto do_bsl;
  2248. } else if (is_small(tmp_arg1) || is_big(tmp_arg1)) {
  2249. /*
  2250. * N bsl PositiveBigNum is too large to represent.
  2251. */
  2252. c_p->freason = SYSTEM_LIMIT;
  2253. goto lb_Cl_error;
  2254. }
  2255. /* Fall through if the left argument is not an integer. */
  2256. }
  2257. /*
  2258. * One or more non-integer arguments.
  2259. */
  2260. goto badarith;
  2261. }
  2262. OpCase(i_int_bnot_jsId):
  2263. {
  2264. GetArg1(1, tmp_arg1);
  2265. if (is_small(tmp_arg1)) {
  2266. tmp_arg1 = make_small(~signed_val(tmp_arg1));
  2267. } else {
  2268. Uint live = Arg(2);
  2269. SWAPOUT;
  2270. reg[0] = r(0);
  2271. reg[live] = tmp_arg1;
  2272. tmp_arg1 = erts_gc_bnot(c_p, reg, live);
  2273. r(0) = reg[0];
  2274. SWAPIN;
  2275. ERTS_HOLE_CHECK(c_p);
  2276. if (is_nil(tmp_arg1)) {
  2277. goto lb_Cl_error;
  2278. }
  2279. }
  2280. StoreBifResult(3, tmp_arg1);
  2281. }
  2282. badarith:
  2283. c_p->freason = BADARITH;
  2284. goto lb_Cl_error;
  2285. OpCase(i_apply): {
  2286. BeamInstr *next;
  2287. SWAPOUT;
  2288. next = apply(c_p, r(0), x(1), x(2), reg);
  2289. SWAPIN;
  2290. if (next != NULL) {
  2291. r(0) = reg[0];
  2292. SET_CP(c_p, I+1);
  2293. SET_I(next);
  2294. Dispatch();
  2295. }
  2296. I = handle_error(c_p, I, reg, apply_3);
  2297. goto post_error_handling;
  2298. }
  2299. OpCase(i_apply_last_P): {
  2300. BeamInstr *next;
  2301. SWAPOUT;
  2302. next = apply(c_p, r(0), x(1), x(2), reg);
  2303. SWAPIN;
  2304. if (next != NULL) {
  2305. r(0) = reg[0];
  2306. SET_CP(c_p, (BeamInstr *) EXPAND_POINTER(E[0]));
  2307. E = ADD_BYTE_OFFSET(E, Arg(0));
  2308. SET_I(next);
  2309. Dispatch();
  2310. }
  2311. I = handle_error(c_p, I, reg, apply_3);
  2312. goto post_error_handling;
  2313. }
  2314. OpCase(i_apply_only): {
  2315. BeamInstr *next;
  2316. SWAPOUT;
  2317. next = apply(c_p, r(0), x(1), x(2), reg);
  2318. SWAPIN;
  2319. if (next != NULL) {
  2320. r(0) = reg[0];
  2321. SET_I(next);
  2322. Dispatch();
  2323. }
  2324. I = handle_error(c_p, I, reg, apply_3);
  2325. goto post_error_handling;
  2326. }
  2327. OpCase(apply_I): {
  2328. BeamInstr *next;
  2329. reg[0] = r(0);
  2330. SWAPOUT;
  2331. next = fixed_apply(c_p, reg, Arg(0));
  2332. SWAPIN;
  2333. if (next != NULL) {
  2334. r(0) = reg[0];
  2335. SET_CP(c_p, I+2);
  2336. SET_I(next);
  2337. Dispatch();
  2338. }
  2339. I = handle_error(c_p, I, reg, apply_3);
  2340. goto post_error_handling;
  2341. }
  2342. OpCase(apply_last_IP): {
  2343. BeamInstr *next;
  2344. reg[0] = r(0);
  2345. SWAPOUT;
  2346. next = fixed_apply(c_p, reg, Arg(0));
  2347. SWAPIN;
  2348. if (next != NULL) {
  2349. r(0) = reg[0];
  2350. SET_CP(c_p, (BeamInstr *) EXPAND_POINTER(E[0]));
  2351. E = ADD_BYTE_OFFSET(E, Arg(1));
  2352. SET_I(next);
  2353. Dispatch();
  2354. }
  2355. I = handle_error(c_p, I, reg, apply_3);
  2356. goto post_error_handling;
  2357. }
  2358. OpCase(i_apply_fun): {
  2359. BeamInstr *next;
  2360. SWAPOUT;
  2361. next = apply_fun(c_p, r(0), x(1), reg);
  2362. SWAPIN;
  2363. if (next != NULL) {
  2364. r(0) = reg[0];
  2365. SET_CP(c_p, I+1);
  2366. SET_I(next);
  2367. Dispatchfun();
  2368. }
  2369. goto find_func_info;
  2370. }
  2371. OpCase(i_apply_fun_last_P): {
  2372. BeamInstr *next;
  2373. SWAPOUT;
  2374. next = apply_fun(c_p, r(0), x(1), reg);
  2375. SWAPIN;
  2376. if (next != NULL) {
  2377. r(0) = reg[0];
  2378. SET_CP(c_p, (BeamInstr *) EXPAND_POINTER(E[0]));
  2379. E = ADD_BYTE_OFFSET(E, Arg(0));
  2380. SET_I(next);
  2381. Dispatchfun();
  2382. }
  2383. goto find_func_info;
  2384. }
  2385. OpCase(i_apply_fun_only): {
  2386. BeamInstr *next;
  2387. SWAPOUT;
  2388. next = apply_fun(c_p, r(0), x(1), reg);
  2389. SWAPIN;
  2390. if (next != NULL) {
  2391. r(0) = reg[0];
  2392. SET_I(next);
  2393. Dispatchfun();
  2394. }
  2395. goto find_func_info;
  2396. }
  2397. OpCase(i_call_fun_I): {
  2398. BeamInstr *next;
  2399. SWAPOUT;
  2400. reg[0] = r(0);
  2401. next = call_fun(c_p, Arg(0), reg, THE_NON_VALUE);
  2402. SWAPIN;
  2403. if (next != NULL) {
  2404. r(0) = reg[0];
  2405. SET_CP(c_p, I+2);
  2406. SET_I(next);
  2407. Dispatchfun();
  2408. }
  2409. goto find_func_info;
  2410. }
  2411. OpCase(i_call_fun_last_IP): {
  2412. BeamInstr *next;
  2413. SWAPOUT;
  2414. reg[0] = r(0);
  2415. next = call_fun(c_p, Arg(0), reg, THE_NON_VALUE);
  2416. SWAPIN;
  2417. if (next != NULL) {
  2418. r(0) = reg[0];
  2419. SET_CP(c_p, (BeamInstr *) EXPAND_POINTER(E[0]));
  2420. E = ADD_BYTE_OFFSET(E, Arg(1));
  2421. SET_I(next);
  2422. Dispatchfun();
  2423. }
  2424. goto find_func_info;
  2425. }
  2426. #ifdef DEBUG
  2427. /*
  2428. * Set a breakpoint here to get control just after a call instruction.
  2429. * I points to the first instruction in the called function.
  2430. *
  2431. * In gdb, use 'call dis(I-5, 1)' to show the name of the function.
  2432. */
  2433. do_dispatch:
  2434. DispatchMacro();
  2435. do_dispatchx:
  2436. DispatchMacrox();
  2437. do_dispatchfun:
  2438. DispatchMacroFun();
  2439. #endif
  2440. /*
  2441. * Jumped to from the Dispatch() macro when the reductions are used up.
  2442. *
  2443. * Since the I register points just beyond the FuncBegin instruction, we
  2444. * can get the module, function, and arity for the function being
  2445. * called from I[-3], I[-2], and I[-1] respectively.
  2446. */
  2447. context_switch_fun:
  2448. c_p->arity = I[-1] + 1;
  2449. goto context_switch2;
  2450. context_switch:
  2451. c_p->arity = I[-1];
  2452. context_switch2: /* Entry for fun calls. */
  2453. c_p->current = I-3; /* Pointer to Mod, Func, Arity */
  2454. {
  2455. Eterm* argp;
  2456. int i;
  2457. /*
  2458. * Make sure that there is enough room for the argument registers to be saved.
  2459. */
  2460. if (c_p->arity > c_p->max_arg_reg) {
  2461. /*
  2462. * Yes, this is an expensive operation, but you only pay it the first
  2463. * time you call a function with more than 6 arguments which is
  2464. * scheduled out. This is better than paying for 26 words of wasted
  2465. * space for most processes which never call functions with more than
  2466. * 6 arguments.
  2467. */
  2468. Uint size = c_p->arity * sizeof(c_p->arg_reg[0]);
  2469. if (c_p->arg_reg != c_p->def_arg_reg) {
  2470. c_p->arg_reg = (Eterm *) erts_realloc(ERTS_ALC_T_ARG_REG,
  2471. (void *) c_p->arg_reg,
  2472. size);
  2473. } else {
  2474. c_p->arg_reg = (Eterm *) erts_alloc(ERTS_ALC_T_ARG_REG, size);
  2475. }
  2476. c_p->max_arg_reg = c_p->arity;
  2477. }
  2478. /*
  2479. * Since REDS_IN(c_p) is stored in the save area (c_p->arg_reg) we must read it
  2480. * now before saving registers.
  2481. *
  2482. * The '+ 1' compensates for the last increment which was not done
  2483. * (beacuse the code for the Dispatch() macro becomes shorter that way).
  2484. */
  2485. reds_used = REDS_IN(c_p) - FCALLS + 1;
  2486. /*
  2487. * Save the argument registers and everything else.
  2488. */
  2489. argp = c_p->arg_reg;
  2490. for (i = c_p->arity - 1; i > 0; i--) {
  2491. argp[i] = reg[i];
  2492. }
  2493. c_p->arg_reg[0] = r(0);
  2494. SWAPOUT;
  2495. c_p->i = I;
  2496. erts_smp_proc_lock(c_p, ERTS_PROC_LOCK_STATUS);
  2497. if (c_p->status != P_SUSPENDED)
  2498. erts_add_to_runq(c_p);
  2499. erts_smp_proc_unlock(c_p, ERTS_PROC_LOCK_STATUS);
  2500. goto do_schedule1;
  2501. }
  2502. OpCase(i_select_tuple_arity_sfI):
  2503. {
  2504. GetArg1(0, tmp_arg1);
  2505. if (is_tuple(tmp_arg1)) {
  2506. tmp_arg1 = *tuple_val(tmp_arg1);
  2507. goto do_binary_search;
  2508. }
  2509. SET_I((BeamInstr *) Arg(1));
  2510. Goto(*I);
  2511. }
  2512. OpCase(i_select_big_sf):
  2513. {
  2514. Eterm* bigp;
  2515. Uint arity;
  2516. Eterm* given;
  2517. Uint given_arity;
  2518. Uint given_size;
  2519. GetArg1(0, tmp_arg1);
  2520. if (is_big(tmp_arg1)) {
  2521. /*
  2522. * The loader has sorted the bignumbers in descending order
  2523. * on the arity word. Therefore, we know that the search
  2524. * has failed as soon as we encounter an arity word less than
  2525. * the arity word of the given number. There is a zero word
  2526. * (less than any valid arity word) stored after the last bignumber.
  2527. */
  2528. given = big_val(tmp_arg1);
  2529. given_arity = given[0];
  2530. given_size = thing_arityval(given_arity);
  2531. bigp = (Eterm *) &Arg(2);
  2532. while ((arity = bigp[0]) > given_arity) {
  2533. bigp += (TermWords(thing_arityval(arity) + 1) + 1) * (sizeof(BeamInstr)/sizeof(Eterm));
  2534. }
  2535. while (bigp[0] == given_arity) {
  2536. if (memcmp(bigp+1, given+1, sizeof(Eterm)*given_size) == 0) {
  2537. BeamInstr *tmp =
  2538. ((BeamInstr *) (UWord) bigp) + TermWords(given_size + 1);
  2539. SET_I((BeamInstr *) *tmp);
  2540. Goto(*I);
  2541. }
  2542. bigp += (TermWords(thing_arityval(arity) + 1) + 1) * (sizeof(BeamInstr)/sizeof(Eterm));
  2543. }
  2544. }
  2545. /*
  2546. * Failed.
  2547. */
  2548. SET_I((BeamInstr *) Arg(1));
  2549. Goto(*I);
  2550. }
  2551. #if defined(ARCH_64) && !HALFWORD_HEAP
  2552. OpCase(i_select_float_sfI):
  2553. {
  2554. Uint f;
  2555. int n;
  2556. struct ValLabel {
  2557. Uint f;
  2558. BeamInstr* addr;
  2559. };
  2560. struct ValLabel* ptr;
  2561. GetArg1(0, tmp_arg1);
  2562. ASSERT(is_float(tmp_arg1));
  2563. f = float_val(tmp_arg1)[1];
  2564. n = Arg(2);
  2565. ptr = (struct ValLabel *) &Arg(3);
  2566. while (n-- > 0) {
  2567. if (ptr->f == f) {
  2568. SET_I(ptr->addr);
  2569. Goto(*I);
  2570. }
  2571. ptr++;
  2572. }
  2573. SET_I((Eterm *) Arg(1));
  2574. Goto(*I);
  2575. }
  2576. #else
  2577. OpCase(i_select_float_sfI):
  2578. {
  2579. Uint fpart1;
  2580. Uint fpart2;
  2581. int n;
  2582. struct ValLabel {
  2583. Uint fpart1;
  2584. Uint fpart2;
  2585. BeamInstr* addr;
  2586. };
  2587. struct ValLabel* ptr;
  2588. GetArg1(0, tmp_arg1);
  2589. ASSERT(is_float(tmp_arg1));
  2590. fpart1 = float_val(tmp_arg1)[1];
  2591. fpart2 = float_val(tmp_arg1)[2];
  2592. n = Arg(2);
  2593. ptr = (struct ValLabel *) &Arg(3);
  2594. while (n-- > 0) {
  2595. if (ptr->fpart1 == fpart1 && ptr->fpart2 == fpart2) {
  2596. SET_I(ptr->addr);
  2597. Goto(*I);
  2598. }
  2599. ptr++;
  2600. }
  2601. SET_I((BeamInstr *) Arg(1));
  2602. Goto(*I);
  2603. }
  2604. #endif
  2605. OpCase(set_tuple_element_sdP): {
  2606. Eterm element;
  2607. Eterm tuple;
  2608. BeamInstr *next;
  2609. Eterm* p;
  2610. PreFetch(3, next);
  2611. GetArg2(0, element, tuple);
  2612. ASSERT(is_tuple(tuple));
  2613. p = (Eterm *) ((unsigned char *) tuple_val(tuple) + Arg(2));
  2614. *p = element;
  2615. NextPF(3, next);
  2616. }
  2617. OpCase(i_is_ne_exact_f):
  2618. if (EQ(tmp_arg1, tmp_arg2)) {
  2619. ClauseFail();
  2620. }
  2621. Next(1);
  2622. OpCase(normal_exit): {
  2623. SWAPOUT;
  2624. c_p->freason = EXC_NORMAL;
  2625. c_p->arity = 0; /* In case this process will ever be garbed again. */
  2626. ERTS_SMP_UNREQ_PROC_MAIN_LOCK(c_p);
  2627. erts_do_exit_process(c_p, am_normal);
  2628. ERTS_SMP_REQ_PROC_MAIN_LOCK(c_p);
  2629. goto do_schedule;
  2630. }
  2631. OpCase(continue_exit): {
  2632. ERTS_SMP_UNREQ_PROC_MAIN_LOCK(c_p);
  2633. erts_continue_exit_process(c_p);
  2634. ERTS_SMP_REQ_PROC_MAIN_LOCK(c_p);
  2635. goto do_schedule;
  2636. }
  2637. OpCase(raise_ss): {
  2638. /* This was not done very well in R10-0; then, we passed the tag in
  2639. the first argument and hoped that the existing c_p->ftrace was
  2640. still correct. But the ftrace-object already includes the tag
  2641. (or rather, the freason). Now, we pass the original ftrace in
  2642. the first argument. We also handle atom tags in the first
  2643. argument for backwards compatibility.
  2644. */
  2645. GetArg2(0, tmp_arg1, tmp_arg2);
  2646. c_p->fvalue = tmp_arg2;
  2647. if (c_p->freason == EXC_NULL) {
  2648. /* a safety check for the R10-0 case; should not happen */
  2649. c_p->ftrace = NIL;
  2650. c_p->freason = EXC_ERROR;
  2651. }
  2652. /* for R10-0 code, keep existing c_p->ftrace and hope it's correct */
  2653. switch (tmp_arg1) {
  2654. case am_throw:
  2655. c_p->freason = EXC_THROWN & ~EXF_SAVETRACE;
  2656. break;
  2657. case am_error:
  2658. c_p->freason = EXC_ERROR & ~EXF_SAVETRACE;
  2659. break;
  2660. case am_exit:
  2661. c_p->freason = EXC_EXIT & ~EXF_SAVETRACE;
  2662. break;
  2663. default:
  2664. {/* R10-1 and later
  2665. XXX note: should do sanity check on given trace if it can be
  2666. passed from a user! Currently only expecting generated calls.
  2667. */
  2668. struct StackTrace *s;
  2669. c_p->ftrace = tmp_arg1;
  2670. s = get_trace_from_exc(tmp_arg1);
  2671. if (s == NULL) {
  2672. c_p->freason = EXC_ERROR;
  2673. } else {
  2674. c_p->freason = PRIMARY_EXCEPTION(s->freason);
  2675. }
  2676. }
  2677. }
  2678. goto find_func_info;
  2679. }
  2680. OpCase(badmatch_s): {
  2681. GetArg1(0, tmp_arg1);
  2682. c_p->fvalue = tmp_arg1;
  2683. c_p->freason = BADMATCH;
  2684. }
  2685. /* Fall through here */
  2686. find_func_info: {
  2687. reg[0] = r(0);
  2688. SWAPOUT;
  2689. I = handle_error(c_p, I, reg, NULL);
  2690. goto post_error_handling;
  2691. }
  2692. OpCase(call_error_handler):
  2693. /*
  2694. * At this point, I points to the code[3] in the export entry for
  2695. * a function which is not loaded.
  2696. *
  2697. * code[0]: Module
  2698. * code[1]: Function
  2699. * code[2]: Arity
  2700. * code[3]: &&call_error_handler
  2701. * code[4]: Not used
  2702. */
  2703. SWAPOUT;
  2704. reg[0] = r(0);
  2705. tmp_arg1 = call_error_handler(c_p, I-3, reg);
  2706. r(0) = reg[0];
  2707. SWAPIN;
  2708. if (tmp_arg1) {
  2709. SET_I(c_p->i);
  2710. Dispatch();
  2711. }
  2712. /* Fall through */
  2713. OpCase(error_action_code): {
  2714. no_error_handler:
  2715. reg[0] = r(0);
  2716. SWAPOUT;
  2717. I = handle_error(c_p, NULL, reg, NULL);
  2718. post_error_handling:
  2719. if (I == 0) {
  2720. goto do_schedule;
  2721. } else {
  2722. r(0) = reg[0];
  2723. ASSERT(!is_value(r(0)));
  2724. if (c_p->mbuf) {
  2725. erts_garbage_collect(c_p, 0, reg+1, 3);
  2726. }
  2727. SWAPIN;
  2728. Goto(*I);
  2729. }
  2730. }
  2731. OpCase(call_nif):
  2732. {
  2733. /*
  2734. * call_nif is always first instruction in function:
  2735. *
  2736. * I[-3]: Module
  2737. * I[-2]: Function
  2738. * I[-1]: Arity
  2739. * I[0]: &&call_nif
  2740. * I[1]: Function pointer to NIF function
  2741. * I[2]: Pointer to erl_module_nif
  2742. */
  2743. BifFunction vbf;
  2744. c_p->current = I-3; /* current and vbf set to please handle_error */
  2745. SWAPOUT;
  2746. c_p->fcalls = FCALLS - 1;
  2747. PROCESS_MAIN_CHK_LOCKS(c_p);
  2748. tmp_arg2 = I[-1];
  2749. ERTS_SMP_UNREQ_PROC_MAIN_LOCK(c_p);
  2750. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  2751. {
  2752. typedef Eterm NifF(struct enif_environment_t*, int argc, Eterm argv[]);
  2753. NifF* fp = vbf = (NifF*) I[1];
  2754. struct enif_environment_t env;
  2755. erts_pre_nif(&env, c_p, (struct erl_module_nif*)I[2]);
  2756. reg[0] = r(0);
  2757. tmp_arg1 = (*fp)(&env, tmp_arg2, reg);
  2758. erts_post_nif(&env);
  2759. }
  2760. ASSERT(!ERTS_PROC_IS_EXITING(c_p) || is_non_value(tmp_arg1));
  2761. PROCESS_MAIN_CHK_LOCKS(c_p);
  2762. goto apply_bif_or_nif_epilogue;
  2763. OpCase(apply_bif):
  2764. /*
  2765. * At this point, I points to the code[3] in the export entry for
  2766. * the BIF:
  2767. *
  2768. * code[0]: Module
  2769. * code[1]: Function
  2770. * code[2]: Arity
  2771. * code[3]: &&apply_bif
  2772. * code[4]: Function pointer to BIF function
  2773. */
  2774. c_p->current = I-3; /* In case we apply process_info/1,2 or load_nif/1 */
  2775. c_p->i = I; /* In case we apply check_process_code/2. */
  2776. c_p->arity = 0; /* To allow garbage collection on ourselves
  2777. * (check_process_code/2).
  2778. */
  2779. SWAPOUT;
  2780. c_p->fcalls = FCALLS - 1;
  2781. vbf = (BifFunction) Arg(0);
  2782. PROCESS_MAIN_CHK_LOCKS(c_p);
  2783. tmp_arg2 = I[-1];
  2784. ASSERT(tmp_arg2 <= 3);
  2785. ERTS_SMP_UNREQ_PROC_MAIN_LOCK(c_p);
  2786. switch (tmp_arg2) {
  2787. case 3:
  2788. {
  2789. Eterm (*bf)(Process*, Eterm, Eterm, Eterm, BeamInstr*) = vbf;
  2790. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  2791. tmp_arg1 = (*bf)(c_p, r(0), x(1), x(2), I);
  2792. ASSERT(!ERTS_PROC_IS_EXITING(c_p) || is_non_value(tmp_arg1));
  2793. PROCESS_MAIN_CHK_LOCKS(c_p);
  2794. }
  2795. break;
  2796. case 2:
  2797. {
  2798. Eterm (*bf)(Process*, Eterm, Eterm, BeamInstr*) = vbf;
  2799. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  2800. tmp_arg1 = (*bf)(c_p, r(0), x(1), I);
  2801. ASSERT(!ERTS_PROC_IS_EXITING(c_p) || is_non_value(tmp_arg1));
  2802. PROCESS_MAIN_CHK_LOCKS(c_p);
  2803. }
  2804. break;
  2805. case 1:
  2806. {
  2807. Eterm (*bf)(Process*, Eterm, BeamInstr*) = vbf;
  2808. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  2809. tmp_arg1 = (*bf)(c_p, r(0), I);
  2810. ASSERT(!ERTS_PROC_IS_EXITING(c_p) || is_non_value(tmp_arg1));
  2811. PROCESS_MAIN_CHK_LOCKS(c_p);
  2812. }
  2813. break;
  2814. case 0:
  2815. {
  2816. Eterm (*bf)(Process*, BeamInstr*) = vbf;
  2817. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  2818. tmp_arg1 = (*bf)(c_p, I);
  2819. ASSERT(!ERTS_PROC_IS_EXITING(c_p) || is_non_value(tmp_arg1));
  2820. PROCESS_MAIN_CHK_LOCKS(c_p);
  2821. break;
  2822. }
  2823. }
  2824. apply_bif_or_nif_epilogue:
  2825. ERTS_SMP_REQ_PROC_MAIN_LOCK(c_p);
  2826. ERTS_HOLE_CHECK(c_p);
  2827. if (c_p->mbuf) {
  2828. reg[0] = r(0);
  2829. tmp_arg1 = erts_gc_after_bif_call(c_p, tmp_arg1, reg, tmp_arg2);
  2830. r(0) = reg[0];
  2831. }
  2832. SWAPIN; /* There might have been a garbage collection. */
  2833. FCALLS = c_p->fcalls;
  2834. if (is_value(tmp_arg1)) {
  2835. r(0) = tmp_arg1;
  2836. CHECK_TERM(r(0));
  2837. SET_I(c_p->cp);
  2838. Goto(*I);
  2839. } else if (c_p->freason == TRAP) {
  2840. SET_I(*((BeamInstr **) (UWord) ((c_p)->def_arg_reg + 3)));
  2841. r(0) = c_p->def_arg_reg[0];
  2842. x(1) = c_p->def_arg_reg[1];
  2843. x(2) = c_p->def_arg_reg[2];
  2844. Dispatch();
  2845. }
  2846. reg[0] = r(0);
  2847. I = handle_error(c_p, c_p->cp, reg, vbf);
  2848. goto post_error_handling;
  2849. }
  2850. OpCase(i_get_sd):
  2851. {
  2852. Eterm arg;
  2853. Eterm result;
  2854. GetArg1(0, arg);
  2855. result = erts_pd_hash_get(c_p, arg);
  2856. StoreBifResult(1, result);
  2857. }
  2858. OpCase(case_end_s):
  2859. GetArg1(0, tmp_arg1);
  2860. c_p->fvalue = tmp_arg1;
  2861. c_p->freason = EXC_CASE_CLAUSE;
  2862. goto find_func_info;
  2863. OpCase(if_end):
  2864. c_p->freason = EXC_IF_CLAUSE;
  2865. goto find_func_info;
  2866. OpCase(i_func_info_IaaI): {
  2867. c_p->freason = EXC_FUNCTION_CLAUSE;
  2868. c_p->current = I + 2;
  2869. goto lb_error_action_code;
  2870. }
  2871. OpCase(try_case_end_s):
  2872. GetArg1(0, tmp_arg1);
  2873. c_p->fvalue = tmp_arg1;
  2874. c_p->freason = EXC_TRY_CLAUSE;
  2875. goto find_func_info;
  2876. /*
  2877. * Construction of binaries using new instructions.
  2878. */
  2879. {
  2880. Eterm new_binary;
  2881. Eterm num_bits_term;
  2882. Uint num_bits;
  2883. Uint alloc;
  2884. Uint num_bytes;
  2885. OpCase(i_bs_init_bits_heap_IIId): {
  2886. num_bits = Arg(0);
  2887. alloc = Arg(1);
  2888. I++;
  2889. goto do_bs_init_bits_known;
  2890. }
  2891. OpCase(i_bs_init_bits_IId): {
  2892. num_bits = Arg(0);
  2893. alloc = 0;
  2894. goto do_bs_init_bits_known;
  2895. }
  2896. OpCase(i_bs_init_bits_fail_heap_IjId): {
  2897. /* tmp_arg1 was fetched by an i_fetch instruction */
  2898. num_bits_term = tmp_arg1;
  2899. alloc = Arg(0);
  2900. I++;
  2901. goto do_bs_init_bits;
  2902. }
  2903. OpCase(i_bs_init_bits_fail_rjId): {
  2904. num_bits_term = r(0);
  2905. alloc = 0;
  2906. goto do_bs_init_bits;
  2907. }
  2908. OpCase(i_bs_init_bits_fail_yjId): {
  2909. num_bits_term = yb(Arg(0));
  2910. I++;
  2911. alloc = 0;
  2912. goto do_bs_init_bits;
  2913. }
  2914. OpCase(i_bs_init_bits_fail_xjId): {
  2915. num_bits_term = xb(Arg(0));
  2916. I++;
  2917. alloc = 0;
  2918. /* FALL THROUGH */
  2919. }
  2920. /* num_bits_term = Term for number of bits to build (small/big)
  2921. * alloc = Number of words to allocate on heap
  2922. * Operands: Fail Live Dst
  2923. */
  2924. do_bs_init_bits:
  2925. if (is_small(num_bits_term)) {
  2926. Sint size = signed_val(num_bits_term);
  2927. if (size < 0) {
  2928. goto badarg;
  2929. }
  2930. num_bits = (Uint) size;
  2931. } else {
  2932. Uint bits;
  2933. if (!term_to_Uint(num_bits_term, &bits)) {
  2934. c_p->freason = bits;
  2935. goto lb_Cl_error;
  2936. }
  2937. num_bits = (Eterm) bits;
  2938. }
  2939. /* num_bits = Number of bits to build
  2940. * alloc = Number of extra words to allocate on heap
  2941. * Operands: NotUsed Live Dst
  2942. */
  2943. do_bs_init_bits_known:
  2944. num_bytes = (num_bits+7) >> 3;
  2945. if (num_bits & 7) {
  2946. alloc += ERL_SUB_BIN_SIZE;
  2947. }
  2948. if (num_bytes <= ERL_ONHEAP_BIN_LIMIT) {
  2949. alloc += heap_bin_size(num_bytes);
  2950. } else {
  2951. alloc += PROC_BIN_SIZE;
  2952. }
  2953. TestHeap(alloc, Arg(1));
  2954. /* num_bits = Number of bits to build
  2955. * num_bytes = Number of bytes to allocate in the binary
  2956. * alloc = Total number of words to allocate on heap
  2957. * Operands: NotUsed NotUsed Dst
  2958. */
  2959. if (num_bytes <= ERL_ONHEAP_BIN_LIMIT) {
  2960. ErlHeapBin* hb;
  2961. erts_bin_offset = 0;
  2962. erts_writable_bin = 0;
  2963. hb = (ErlHeapBin *) HTOP;
  2964. HTOP += heap_bin_size(num_bytes);
  2965. hb->thing_word = header_heap_bin(num_bytes);
  2966. hb->size = num_bytes;
  2967. erts_current_bin = (byte *) hb->data;
  2968. new_binary = make_binary(hb);
  2969. do_bits_sub_bin:
  2970. if (num_bits & 7) {
  2971. ErlSubBin* sb;
  2972. sb = (ErlSubBin *) HTOP;
  2973. HTOP += ERL_SUB_BIN_SIZE;
  2974. sb->thing_word = HEADER_SUB_BIN;
  2975. sb->size = num_bytes - 1;
  2976. sb->bitsize = num_bits & 7;
  2977. sb->offs = 0;
  2978. sb->bitoffs = 0;
  2979. sb->is_writable = 0;
  2980. sb->orig = new_binary;
  2981. new_binary = make_binary(sb);
  2982. }
  2983. HEAP_SPACE_VERIFIED(0);
  2984. StoreBifResult(2, new_binary);
  2985. } else {
  2986. Binary* bptr;
  2987. ProcBin* pb;
  2988. erts_bin_offset = 0;
  2989. erts_writable_bin = 0;
  2990. /*
  2991. * Allocate the binary struct itself.
  2992. */
  2993. bptr = erts_bin_nrml_alloc(num_bytes);
  2994. bptr->flags = 0;
  2995. bptr->orig_size = num_bytes;
  2996. erts_refc_init(&bptr->refc, 1);
  2997. erts_current_bin = (byte *) bptr->orig_bytes;
  2998. /*
  2999. * Now allocate the ProcBin on the heap.
  3000. */
  3001. pb = (ProcBin *) HTOP;
  3002. HTOP += PROC_BIN_SIZE;
  3003. pb->thing_word = HEADER_PROC_BIN;
  3004. pb->size = num_bytes;
  3005. pb->next = MSO(c_p).mso;
  3006. MSO(c_p).mso = pb;
  3007. pb->val = bptr;
  3008. pb->bytes = (byte*) bptr->orig_bytes;
  3009. pb->flags = 0;
  3010. MSO(c_p).overhead += pb->size / sizeof(Eterm);
  3011. new_binary = make_binary(pb);
  3012. goto do_bits_sub_bin;
  3013. }
  3014. }
  3015. {
  3016. OpCase(i_bs_init_fail_heap_IjId): {
  3017. /* tmp_arg1 was fetched by an i_fetch instruction */
  3018. tmp_arg2 = Arg(0);
  3019. I++;
  3020. goto do_bs_init;
  3021. }
  3022. OpCase(i_bs_init_fail_rjId): {
  3023. tmp_arg1 = r(0);
  3024. tmp_arg2 = 0;
  3025. goto do_bs_init;
  3026. }
  3027. OpCase(i_bs_init_fail_yjId): {
  3028. tmp_arg1 = yb(Arg(0));
  3029. tmp_arg2 = 0;
  3030. I++;
  3031. goto do_bs_init;
  3032. }
  3033. OpCase(i_bs_init_fail_xjId): {
  3034. tmp_arg1 = xb(Arg(0));
  3035. tmp_arg2 = 0;
  3036. I++;
  3037. }
  3038. /* FALL THROUGH */
  3039. do_bs_init:
  3040. if (is_small(tmp_arg1)) {
  3041. Sint size = signed_val(tmp_arg1);
  3042. if (size < 0) {
  3043. goto badarg;
  3044. }
  3045. tmp_arg1 = (Eterm) size;
  3046. } else {
  3047. Uint bytes;
  3048. if (!term_to_Uint(tmp_arg1, &bytes)) {
  3049. c_p->freason = bytes;
  3050. goto lb_Cl_error;
  3051. }
  3052. if ((bytes >> (8*sizeof(Uint)-3)) != 0) {
  3053. goto system_limit;
  3054. }
  3055. tmp_arg1 = (Eterm) bytes;
  3056. }
  3057. if (tmp_arg1 <= ERL_ONHEAP_BIN_LIMIT) {
  3058. goto do_heap_bin_alloc;
  3059. } else {
  3060. goto do_proc_bin_alloc;
  3061. }
  3062. OpCase(i_bs_init_heap_IIId): {
  3063. tmp_arg1 = Arg(0);
  3064. tmp_arg2 = Arg(1);
  3065. I++;
  3066. goto do_proc_bin_alloc;
  3067. }
  3068. OpCase(i_bs_init_IId): {
  3069. tmp_arg1 = Arg(0);
  3070. tmp_arg2 = 0;
  3071. }
  3072. /* FALL THROUGH */
  3073. do_proc_bin_alloc: {
  3074. Binary* bptr;
  3075. ProcBin* pb;
  3076. erts_bin_offset = 0;
  3077. erts_writable_bin = 0;
  3078. TestBinVHeap(tmp_arg1 / sizeof(Eterm),
  3079. tmp_arg2 + PROC_BIN_SIZE + ERL_SUB_BIN_SIZE, Arg(1));
  3080. /*
  3081. * Allocate the binary struct itself.
  3082. */
  3083. bptr = erts_bin_nrml_alloc(tmp_arg1);
  3084. bptr->flags = 0;
  3085. bptr->orig_size = tmp_arg1;
  3086. erts_refc_init(&bptr->refc, 1);
  3087. erts_current_bin = (byte *) bptr->orig_bytes;
  3088. /*
  3089. * Now allocate the ProcBin on the heap.
  3090. */
  3091. pb = (ProcBin *) HTOP;
  3092. HTOP += PROC_BIN_SIZE;
  3093. pb->thing_word = HEADER_PROC_BIN;
  3094. pb->size = tmp_arg1;
  3095. pb->next = MSO(c_p).mso;
  3096. MSO(c_p).mso = pb;
  3097. pb->val = bptr;
  3098. pb->bytes = (byte*) bptr->orig_bytes;
  3099. pb->flags = 0;
  3100. MSO(c_p).overhead += tmp_arg1 / sizeof(Eterm);
  3101. StoreBifResult(2, make_binary(pb));
  3102. }
  3103. OpCase(i_bs_init_heap_bin_heap_IIId): {
  3104. tmp_arg1 = Arg(0);
  3105. tmp_arg2 = Arg(1);
  3106. I++;
  3107. goto do_heap_bin_alloc;
  3108. }
  3109. OpCase(i_bs_init_heap_bin_IId): {
  3110. tmp_arg1 = Arg(0);
  3111. tmp_arg2 = 0;
  3112. }
  3113. /* Fall through */
  3114. do_heap_bin_alloc:
  3115. {
  3116. ErlHeapBin* hb;
  3117. Uint bin_need;
  3118. bin_need = heap_bin_size(tmp_arg1);
  3119. erts_bin_offset = 0;
  3120. erts_writable_bin = 0;
  3121. TestHeap(bin_need+tmp_arg2+ERL_SUB_BIN_SIZE, Arg(1));
  3122. hb = (ErlHeapBin *) HTOP;
  3123. HTOP += bin_need;
  3124. hb->thing_word = header_heap_bin(tmp_arg1);
  3125. hb->size = tmp_arg1;
  3126. erts_current_bin = (byte *) hb->data;
  3127. tmp_arg1 = make_binary(hb);
  3128. StoreBifResult(2, tmp_arg1);
  3129. }
  3130. }
  3131. OpCase(i_bs_add_jId): {
  3132. Uint Unit = Arg(1);
  3133. if (is_both_small(tmp_arg1, tmp_arg2)) {
  3134. Sint Arg1 = signed_val(tmp_arg1);
  3135. Sint Arg2 = signed_val(tmp_arg2);
  3136. if (Arg1 >= 0 && Arg2 >= 0) {
  3137. BsSafeMul(Arg2, Unit, goto system_limit, tmp_arg1);
  3138. tmp_arg1 += Arg1;
  3139. store_bs_add_result:
  3140. if (MY_IS_SSMALL((Sint) tmp_arg1)) {
  3141. tmp_arg1 = make_small(tmp_arg1);
  3142. } else {
  3143. /*
  3144. * May generate a heap fragment, but in this
  3145. * particular case it is OK, since the value will be
  3146. * stored into an x register (the GC will scan x
  3147. * registers for references to heap fragments) and
  3148. * there is no risk that value can be stored into a
  3149. * location that is not scanned for heap-fragment
  3150. * references (such as the heap).
  3151. */
  3152. SWAPOUT;
  3153. tmp_arg1 = erts_make_integer(tmp_arg1, c_p);
  3154. HTOP = HEAP_TOP(c_p);
  3155. }
  3156. StoreBifResult(2, tmp_arg1);
  3157. }
  3158. goto badarg;
  3159. } else {
  3160. Uint a;
  3161. Uint b;
  3162. Uint c;
  3163. /*
  3164. * Now we know that one of the arguments is
  3165. * not a small. We must convert both arguments
  3166. * to Uints and check for errors at the same time.
  3167. *
  3168. * Error checking is tricky.
  3169. *
  3170. * If one of the arguments is not numeric or
  3171. * not positive, the error reason is BADARG.
  3172. *
  3173. * Otherwise if both arguments are numeric,
  3174. * but at least one argument does not fit in
  3175. * an Uint, the reason is SYSTEM_LIMIT.
  3176. */
  3177. if (!term_to_Uint(tmp_arg1, &a)) {
  3178. if (a == BADARG) {
  3179. goto badarg;
  3180. }
  3181. if (!term_to_Uint(tmp_arg2, &b)) {
  3182. c_p->freason = b;
  3183. goto lb_Cl_error;
  3184. }
  3185. goto system_limit;
  3186. } else if (!term_to_Uint(tmp_arg2, &b)) {
  3187. c_p->freason = b;
  3188. goto lb_Cl_error;
  3189. }
  3190. /*
  3191. * The arguments are now correct and stored in a and b.
  3192. */
  3193. BsSafeMul(b, Unit, goto system_limit, c);
  3194. tmp_arg1 = a + c;
  3195. if (tmp_arg1 < a) {
  3196. /*
  3197. * If the result is less than one of the
  3198. * arguments, there must have been an overflow.
  3199. */
  3200. goto system_limit;
  3201. }
  3202. goto store_bs_add_result;
  3203. }
  3204. /* No fallthrough */
  3205. ASSERT(0);
  3206. }
  3207. OpCase(bs_put_string_II):
  3208. {
  3209. BeamInstr *next;
  3210. PreFetch(2, next);
  3211. erts_new_bs_put_string(ERL_BITS_ARGS_2((byte *) Arg(1), Arg(0)));
  3212. NextPF(2, next);
  3213. }
  3214. /*
  3215. * tmp_arg1 = Number of bytes to build
  3216. * tmp_arg2 = Source binary
  3217. * Operands: Fail ExtraHeap Live Unit Dst
  3218. */
  3219. OpCase(i_bs_append_jIIId): {
  3220. Uint live = Arg(2);
  3221. Uint res;
  3222. SWAPOUT;
  3223. reg[0] = r(0);
  3224. reg[live] = tmp_arg2;
  3225. res = erts_bs_append(c_p, reg, live, tmp_arg1, Arg(1), Arg(3));
  3226. r(0) = reg[0];
  3227. SWAPIN;
  3228. if (is_non_value(res)) {
  3229. /* c_p->freason is already set (may be either BADARG or SYSTEM_LIMIT). */
  3230. goto lb_Cl_error;
  3231. }
  3232. StoreBifResult(4, res);
  3233. }
  3234. /*
  3235. * tmp_arg1 = Number of bytes to build
  3236. * tmp_arg2 = Source binary
  3237. * Operands: Fail Unit Dst
  3238. */
  3239. OpCase(i_bs_private_append_jId): {
  3240. Eterm res;
  3241. res = erts_bs_private_append(c_p, tmp_arg2, tmp_arg1, Arg(1));
  3242. if (is_non_value(res)) {
  3243. /* c_p->freason is already set (may be either BADARG or SYSTEM_LIMIT). */
  3244. goto lb_Cl_error;
  3245. }
  3246. StoreBifResult(2, res);
  3247. }
  3248. /*
  3249. * tmp_arg1 = Initial size of writable binary
  3250. * Operands: Live Dst
  3251. */
  3252. OpCase(bs_init_writable): {
  3253. SWAPOUT;
  3254. r(0) = erts_bs_init_writable(c_p, r(0));
  3255. SWAPIN;
  3256. Next(0);
  3257. }
  3258. /*
  3259. * Calculate the number of bytes needed to encode the source
  3260. * operarand to UTF-8. If the source operand is invalid (e.g. wrong
  3261. * type or range) we return a nonsense integer result (0 or 4). We
  3262. * can get away with that because we KNOW that bs_put_utf8 will do
  3263. * full error checking.
  3264. */
  3265. OpCase(i_bs_utf8_size_sd): {
  3266. Eterm arg;
  3267. Eterm result;
  3268. GetArg1(0, arg);
  3269. if (arg < make_small(0x80UL)) {
  3270. result = make_small(1);
  3271. } else if (arg < make_small(0x800UL)) {
  3272. result = make_small(2);
  3273. } else if (arg < make_small(0x10000UL)) {
  3274. result = make_small(3);
  3275. } else {
  3276. result = make_small(4);
  3277. }
  3278. StoreBifResult(1, result);
  3279. }
  3280. OpCase(i_bs_put_utf8_js): {
  3281. Eterm arg;
  3282. GetArg1(1, arg);
  3283. if (!erts_bs_put_utf8(ERL_BITS_ARGS_1(arg))) {
  3284. goto badarg;
  3285. }
  3286. Next(2);
  3287. }
  3288. /*
  3289. * Calculate the number of bytes needed to encode the source
  3290. * operarand to UTF-8. If the source operand is invalid (e.g. wrong
  3291. * type or range) we return a nonsense integer result (2 or 4). We
  3292. * can get away with that because we KNOW that bs_put_utf16 will do
  3293. * full error checking.
  3294. */
  3295. OpCase(i_bs_utf16_size_sd): {
  3296. Eterm arg;
  3297. Eterm result = make_small(2);
  3298. GetArg1(0, arg);
  3299. if (arg >= make_small(0x10000UL)) {
  3300. result = make_small(4);
  3301. }
  3302. StoreBifResult(1, result);
  3303. }
  3304. OpCase(i_bs_put_utf16_jIs): {
  3305. Eterm arg;
  3306. GetArg1(2, arg);
  3307. if (!erts_bs_put_utf16(ERL_BITS_ARGS_2(arg, Arg(1)))) {
  3308. goto badarg;
  3309. }
  3310. Next(3);
  3311. }
  3312. /*
  3313. * Only used for validating a value about to be stored in a binary.
  3314. */
  3315. OpCase(i_bs_validate_unicode_js): {
  3316. Eterm val;
  3317. GetArg1(1, val);
  3318. /*
  3319. * There is no need to untag the integer, but it IS necessary
  3320. * to make sure it is small (if the term is a bignum, it could
  3321. * slip through the test, and there is no further test that
  3322. * would catch it, since bit syntax construction silently masks
  3323. * too big numbers).
  3324. */
  3325. if (is_not_small(val) || val > make_small(0x10FFFFUL) ||
  3326. (make_small(0xD800UL) <= val && val <= make_small(0xDFFFUL)) ||
  3327. val == make_small(0xFFFEUL) || val == make_small(0xFFFFUL)) {
  3328. goto badarg;
  3329. }
  3330. Next(2);
  3331. }
  3332. /*
  3333. * Only used for validating a value matched out.
  3334. *
  3335. * tmp_arg1 = Integer to validate
  3336. * tmp_arg2 = Match context
  3337. */
  3338. OpCase(i_bs_validate_unicode_retract_j): {
  3339. /*
  3340. * There is no need to untag the integer, but it IS necessary
  3341. * to make sure it is small (a bignum pointer could fall in
  3342. * the valid range).
  3343. */
  3344. if (is_not_small(tmp_arg1) || tmp_arg1 > make_small(0x10FFFFUL) ||
  3345. (make_small(0xD800UL) <= tmp_arg1 && tmp_arg1 <= make_small(0xDFFFUL)) ||
  3346. tmp_arg1 == make_small(0xFFFEUL) || tmp_arg1 == make_small(0xFFFFUL)) {
  3347. ErlBinMatchBuffer *mb = ms_matchbuffer(tmp_arg2);
  3348. mb->offset -= 32;
  3349. goto badarg;
  3350. }
  3351. Next(1);
  3352. }
  3353. /*
  3354. * Matching of binaries.
  3355. */
  3356. {
  3357. Eterm header;
  3358. BeamInstr *next;
  3359. Uint slots;
  3360. OpCase(i_bs_start_match2_rfIId): {
  3361. tmp_arg1 = r(0);
  3362. do_start_match:
  3363. slots = Arg(2);
  3364. if (!is_boxed(tmp_arg1)) {
  3365. ClauseFail();
  3366. }
  3367. PreFetch(4, next);
  3368. header = *boxed_val(tmp_arg1);
  3369. if (header_is_bin_matchstate(header)) {
  3370. ErlBinMatchState* ms = (ErlBinMatchState *) boxed_val(tmp_arg1);
  3371. Uint actual_slots = HEADER_NUM_SLOTS(header);
  3372. ms->save_offset[0] = ms->mb.offset;
  3373. if (actual_slots < slots) {
  3374. ErlBinMatchState* dst;
  3375. Uint live = Arg(1);
  3376. Uint wordsneeded = ERL_BIN_MATCHSTATE_SIZE(slots);
  3377. TestHeapPreserve(wordsneeded, live, tmp_arg1);
  3378. ms = (ErlBinMatchState *) boxed_val(tmp_arg1);
  3379. dst = (ErlBinMatchState *) HTOP;
  3380. *dst = *ms;
  3381. *HTOP = HEADER_BIN_MATCHSTATE(slots);
  3382. HTOP += wordsneeded;
  3383. HEAP_SPACE_VERIFIED(0);
  3384. StoreResult(make_matchstate(dst), Arg(3));
  3385. }
  3386. } else if (is_binary_header(header)) {
  3387. Eterm result;
  3388. Uint live = Arg(1);
  3389. Uint wordsneeded = ERL_BIN_MATCHSTATE_SIZE(slots);
  3390. TestHeapPreserve(wordsneeded, live, tmp_arg1);
  3391. HEAP_TOP(c_p) = HTOP;
  3392. #ifdef DEBUG
  3393. c_p->stop = E; /* Needed for checking in HeapOnlyAlloc(). */
  3394. #endif
  3395. result = erts_bs_start_match_2(c_p, tmp_arg1, slots);
  3396. HTOP = HEAP_TOP(c_p);
  3397. HEAP_SPACE_VERIFIED(0);
  3398. if (is_non_value(result)) {
  3399. ClauseFail();
  3400. } else {
  3401. StoreResult(result, Arg(3));
  3402. }
  3403. } else {
  3404. ClauseFail();
  3405. }
  3406. NextPF(4, next);
  3407. }
  3408. OpCase(i_bs_start_match2_xfIId): {
  3409. tmp_arg1 = xb(Arg(0));
  3410. I++;
  3411. goto do_start_match;
  3412. }
  3413. OpCase(i_bs_start_match2_yfIId): {
  3414. tmp_arg1 = yb(Arg(0));
  3415. I++;
  3416. goto do_start_match;
  3417. }
  3418. }
  3419. OpCase(bs_test_zero_tail2_fr): {
  3420. BeamInstr *next;
  3421. ErlBinMatchBuffer *_mb;
  3422. PreFetch(1, next);
  3423. _mb = (ErlBinMatchBuffer*) ms_matchbuffer(r(0));
  3424. if (_mb->size != _mb->offset) {
  3425. ClauseFail();
  3426. }
  3427. NextPF(1, next);
  3428. }
  3429. OpCase(bs_test_zero_tail2_fx): {
  3430. BeamInstr *next;
  3431. ErlBinMatchBuffer *_mb;
  3432. PreFetch(2, next);
  3433. _mb = (ErlBinMatchBuffer*) ms_matchbuffer(xb(Arg(1)));
  3434. if (_mb->size != _mb->offset) {
  3435. ClauseFail();
  3436. }
  3437. NextPF(2, next);
  3438. }
  3439. OpCase(bs_test_tail_imm2_frI): {
  3440. BeamInstr *next;
  3441. ErlBinMatchBuffer *_mb;
  3442. PreFetch(2, next);
  3443. _mb = ms_matchbuffer(r(0));
  3444. if (_mb->size - _mb->offset != Arg(1)) {
  3445. ClauseFail();
  3446. }
  3447. NextPF(2, next);
  3448. }
  3449. OpCase(bs_test_tail_imm2_fxI): {
  3450. BeamInstr *next;
  3451. ErlBinMatchBuffer *_mb;
  3452. PreFetch(3, next);
  3453. _mb = ms_matchbuffer(xb(Arg(1)));
  3454. if (_mb->size - _mb->offset != Arg(2)) {
  3455. ClauseFail();
  3456. }
  3457. NextPF(3, next);
  3458. }
  3459. OpCase(bs_test_unit_frI): {
  3460. BeamInstr *next;
  3461. ErlBinMatchBuffer *_mb;
  3462. PreFetch(2, next);
  3463. _mb = ms_matchbuffer(r(0));
  3464. if ((_mb->size - _mb->offset) % Arg(1)) {
  3465. ClauseFail();
  3466. }
  3467. NextPF(2, next);
  3468. }
  3469. OpCase(bs_test_unit_fxI): {
  3470. BeamInstr *next;
  3471. ErlBinMatchBuffer *_mb;
  3472. PreFetch(3, next);
  3473. _mb = ms_matchbuffer(xb(Arg(1)));
  3474. if ((_mb->size - _mb->offset) % Arg(2)) {
  3475. ClauseFail();
  3476. }
  3477. NextPF(3, next);
  3478. }
  3479. OpCase(bs_test_unit8_fr): {
  3480. BeamInstr *next;
  3481. ErlBinMatchBuffer *_mb;
  3482. PreFetch(1, next);
  3483. _mb = ms_matchbuffer(r(0));
  3484. if ((_mb->size - _mb->offset) & 7) {
  3485. ClauseFail();
  3486. }
  3487. NextPF(1, next);
  3488. }
  3489. OpCase(bs_test_unit8_fx): {
  3490. BeamInstr *next;
  3491. ErlBinMatchBuffer *_mb;
  3492. PreFetch(2, next);
  3493. _mb = ms_matchbuffer(xb(Arg(1)));
  3494. if ((_mb->size - _mb->offset) & 7) {
  3495. ClauseFail();
  3496. }
  3497. NextPF(2, next);
  3498. }
  3499. OpCase(i_bs_get_integer_8_rfd): {
  3500. tmp_arg1 = r(0);
  3501. goto do_bs_get_integer_8;
  3502. }
  3503. OpCase(i_bs_get_integer_8_xfd): {
  3504. tmp_arg1 = xb(Arg(0));
  3505. I++;
  3506. }
  3507. do_bs_get_integer_8: {
  3508. ErlBinMatchBuffer *_mb;
  3509. Eterm _result;
  3510. _mb = ms_matchbuffer(tmp_arg1);
  3511. if (_mb->size - _mb->offset < 8) {
  3512. ClauseFail();
  3513. }
  3514. if (BIT_OFFSET(_mb->offset) != 0) {
  3515. _result = erts_bs_get_integer_2(c_p, 8, 0, _mb);
  3516. } else {
  3517. _result = make_small(_mb->base[BYTE_OFFSET(_mb->offset)]);
  3518. _mb->offset += 8;
  3519. }
  3520. StoreBifResult(1, _result);
  3521. }
  3522. OpCase(i_bs_get_integer_16_rfd): {
  3523. tmp_arg1 = r(0);
  3524. goto do_bs_get_integer_16;
  3525. }
  3526. OpCase(i_bs_get_integer_16_xfd): {
  3527. tmp_arg1 = xb(Arg(0));
  3528. I++;
  3529. }
  3530. do_bs_get_integer_16: {
  3531. ErlBinMatchBuffer *_mb;
  3532. Eterm _result;
  3533. _mb = ms_matchbuffer(tmp_arg1);
  3534. if (_mb->size - _mb->offset < 16) {
  3535. ClauseFail();
  3536. }
  3537. if (BIT_OFFSET(_mb->offset) != 0) {
  3538. _result = erts_bs_get_integer_2(c_p, 16, 0, _mb);
  3539. } else {
  3540. _result = make_small(get_int16(_mb->base+BYTE_OFFSET(_mb->offset)));
  3541. _mb->offset += 16;
  3542. }
  3543. StoreBifResult(1, _result);
  3544. }
  3545. OpCase(i_bs_get_integer_32_rfId): {
  3546. tmp_arg1 = r(0);
  3547. goto do_bs_get_integer_32;
  3548. }
  3549. OpCase(i_bs_get_integer_32_xfId): {
  3550. tmp_arg1 = xb(Arg(0));
  3551. I++;
  3552. }
  3553. do_bs_get_integer_32: {
  3554. ErlBinMatchBuffer *_mb;
  3555. Uint32 _integer;
  3556. Eterm _result;
  3557. _mb = ms_matchbuffer(tmp_arg1);
  3558. if (_mb->size - _mb->offset < 32) { ClauseFail(); }
  3559. if (BIT_OFFSET(_mb->offset) != 0) {
  3560. _integer = erts_bs_get_unaligned_uint32(_mb);
  3561. } else {
  3562. _integer = get_int32(_mb->base + _mb->offset/8);
  3563. }
  3564. _mb->offset += 32;
  3565. #if !defined(ARCH_64) || HALFWORD_HEAP
  3566. if (IS_USMALL(0, _integer)) {
  3567. #endif
  3568. _result = make_small(_integer);
  3569. #if !defined(ARCH_64) || HALFWORD_HEAP
  3570. } else {
  3571. TestHeap(BIG_UINT_HEAP_SIZE, Arg(1));
  3572. _result = uint_to_big((Uint) _integer, HTOP);
  3573. HTOP += BIG_UINT_HEAP_SIZE;
  3574. HEAP_SPACE_VERIFIED(0);
  3575. }
  3576. #endif
  3577. StoreBifResult(2, _result);
  3578. }
  3579. /* Operands: Size Live Fail Flags Dst */
  3580. OpCase(i_bs_get_integer_imm_rIIfId): {
  3581. tmp_arg1 = r(0);
  3582. /* Operands: Size Live Fail Flags Dst */
  3583. goto do_bs_get_integer_imm_test_heap;
  3584. }
  3585. /* Operands: x(Reg) Size Live Fail Flags Dst */
  3586. OpCase(i_bs_get_integer_imm_xIIfId): {
  3587. tmp_arg1 = xb(Arg(0));
  3588. I++;
  3589. /* Operands: Size Live Fail Flags Dst */
  3590. goto do_bs_get_integer_imm_test_heap;
  3591. }
  3592. /*
  3593. * tmp_arg1 = match context
  3594. * Operands: Size Live Fail Flags Dst
  3595. */
  3596. do_bs_get_integer_imm_test_heap: {
  3597. Uint wordsneeded;
  3598. tmp_arg2 = Arg(0);
  3599. wordsneeded = 1+WSIZE(NBYTES(tmp_arg2));
  3600. TestHeapPreserve(wordsneeded, Arg(1), tmp_arg1);
  3601. I += 2;
  3602. /* Operands: Fail Flags Dst */
  3603. goto do_bs_get_integer_imm;
  3604. }
  3605. /* Operands: Size Fail Flags Dst */
  3606. OpCase(i_bs_get_integer_small_imm_rIfId): {
  3607. tmp_arg1 = r(0);
  3608. tmp_arg2 = Arg(0);
  3609. I++;
  3610. /* Operands: Fail Flags Dst */
  3611. goto do_bs_get_integer_imm;
  3612. }
  3613. /* Operands: x(Reg) Size Fail Flags Dst */
  3614. OpCase(i_bs_get_integer_small_imm_xIfId): {
  3615. tmp_arg1 = xb(Arg(0));
  3616. tmp_arg2 = Arg(1);
  3617. I += 2;
  3618. /* Operands: Fail Flags Dst */
  3619. goto do_bs_get_integer_imm;
  3620. }
  3621. /*
  3622. * tmp_arg1 = match context
  3623. * tmp_arg2 = size of field
  3624. * Operands: Fail Flags Dst
  3625. */
  3626. do_bs_get_integer_imm: {
  3627. ErlBinMatchBuffer* mb;
  3628. Eterm result;
  3629. mb = ms_matchbuffer(tmp_arg1);
  3630. LIGHT_SWAPOUT;
  3631. result = erts_bs_get_integer_2(c_p, tmp_arg2, Arg(1), mb);
  3632. LIGHT_SWAPIN;
  3633. HEAP_SPACE_VERIFIED(0);
  3634. if (is_non_value(result)) {
  3635. ClauseFail();
  3636. }
  3637. StoreBifResult(2, result);
  3638. }
  3639. /*
  3640. * tmp_arg1 = Match context
  3641. * tmp_arg2 = Size field
  3642. * Operands: Fail Live FlagsAndUnit Dst
  3643. */
  3644. OpCase(i_bs_get_integer_fIId): {
  3645. Uint flags;
  3646. Uint size;
  3647. ErlBinMatchBuffer* mb;
  3648. Eterm result;
  3649. flags = Arg(2);
  3650. BsGetFieldSize(tmp_arg2, (flags >> 3), ClauseFail(), size);
  3651. if (size >= SMALL_BITS) {
  3652. Uint wordsneeded = 1+WSIZE(NBYTES((Uint) size));
  3653. TestHeapPreserve(wordsneeded, Arg(1), tmp_arg1);
  3654. }
  3655. mb = ms_matchbuffer(tmp_arg1);
  3656. LIGHT_SWAPOUT;
  3657. result = erts_bs_get_integer_2(c_p, size, flags, mb);
  3658. LIGHT_SWAPIN;
  3659. HEAP_SPACE_VERIFIED(0);
  3660. if (is_non_value(result)) {
  3661. ClauseFail();
  3662. }
  3663. StoreBifResult(3, result);
  3664. }
  3665. /* Operands: MatchContext Fail Dst */
  3666. OpCase(i_bs_get_utf8_rfd): {
  3667. tmp_arg1 = r(0);
  3668. goto do_bs_get_utf8;
  3669. }
  3670. OpCase(i_bs_get_utf8_xfd): {
  3671. tmp_arg1 = xb(Arg(0));
  3672. I++;
  3673. }
  3674. /*
  3675. * tmp_arg1 = match_context
  3676. * Operands: Fail Dst
  3677. */
  3678. do_bs_get_utf8: {
  3679. Eterm result = erts_bs_get_utf8(ms_matchbuffer(tmp_arg1));
  3680. if (is_non_value(result)) {
  3681. ClauseFail();
  3682. }
  3683. StoreBifResult(1, result);
  3684. }
  3685. /* Operands: MatchContext Fail Flags Dst */
  3686. OpCase(i_bs_get_utf16_rfId): {
  3687. tmp_arg1 = r(0);
  3688. goto do_bs_get_utf16;
  3689. }
  3690. OpCase(i_bs_get_utf16_xfId): {
  3691. tmp_arg1 = xb(Arg(0));
  3692. I++;
  3693. }
  3694. /*
  3695. * tmp_arg1 = match_context
  3696. * Operands: Fail Flags Dst
  3697. */
  3698. do_bs_get_utf16: {
  3699. Eterm result = erts_bs_get_utf16(ms_matchbuffer(tmp_arg1), Arg(1));
  3700. if (is_non_value(result)) {
  3701. ClauseFail();
  3702. }
  3703. StoreBifResult(2, result);
  3704. }
  3705. {
  3706. ErlBinMatchBuffer* mb;
  3707. ErlSubBin* sb;
  3708. Uint size;
  3709. Uint offs;
  3710. Uint orig;
  3711. Uint hole_size;
  3712. OpCase(bs_context_to_binary_r): {
  3713. tmp_arg1 = x0;
  3714. I -= 2;
  3715. goto do_context_to_binary;
  3716. }
  3717. /* Unfortunately, inlining can generate this instruction. */
  3718. OpCase(bs_context_to_binary_y): {
  3719. tmp_arg1 = yb(Arg(0));
  3720. goto do_context_to_binary0;
  3721. }
  3722. OpCase(bs_context_to_binary_x): {
  3723. tmp_arg1 = xb(Arg(0));
  3724. do_context_to_binary0:
  3725. I--;
  3726. }
  3727. do_context_to_binary:
  3728. if (is_boxed(tmp_arg1) && header_is_bin_matchstate(*boxed_val(tmp_arg1))) {
  3729. ErlBinMatchState* ms = (ErlBinMatchState *) boxed_val(tmp_arg1);
  3730. mb = &ms->mb;
  3731. offs = ms->save_offset[0];
  3732. size = mb->size - offs;
  3733. goto do_bs_get_binary_all_reuse_common;
  3734. }
  3735. Next(2);
  3736. OpCase(i_bs_get_binary_all_reuse_rfI): {
  3737. tmp_arg1 = x0;
  3738. goto do_bs_get_binary_all_reuse;
  3739. }
  3740. OpCase(i_bs_get_binary_all_reuse_xfI): {
  3741. tmp_arg1 = xb(Arg(0));
  3742. I++;
  3743. }
  3744. do_bs_get_binary_all_reuse:
  3745. mb = ms_matchbuffer(tmp_arg1);
  3746. size = mb->size - mb->offset;
  3747. if (size % Arg(1) != 0) {
  3748. ClauseFail();
  3749. }
  3750. offs = mb->offset;
  3751. do_bs_get_binary_all_reuse_common:
  3752. orig = mb->orig;
  3753. sb = (ErlSubBin *) boxed_val(tmp_arg1);
  3754. hole_size = 1 + header_arity(sb->thing_word) - ERL_SUB_BIN_SIZE;
  3755. sb->thing_word = HEADER_SUB_BIN;
  3756. sb->size = BYTE_OFFSET(size);
  3757. sb->bitsize = BIT_OFFSET(size);
  3758. sb->offs = BYTE_OFFSET(offs);
  3759. sb->bitoffs = BIT_OFFSET(offs);
  3760. sb->is_writable = 0;
  3761. sb->orig = orig;
  3762. if (hole_size) {
  3763. sb[1].thing_word = make_pos_bignum_header(hole_size-1);
  3764. }
  3765. Next(2);
  3766. }
  3767. {
  3768. OpCase(i_bs_match_string_rfII): {
  3769. tmp_arg1 = r(0);
  3770. goto do_bs_match_string;
  3771. }
  3772. OpCase(i_bs_match_string_xfII): {
  3773. tmp_arg1 = xb(Arg(0));
  3774. I++;
  3775. }
  3776. do_bs_match_string:
  3777. {
  3778. BeamInstr *next;
  3779. byte* bytes;
  3780. Uint bits;
  3781. ErlBinMatchBuffer* mb;
  3782. Uint offs;
  3783. PreFetch(3, next);
  3784. bits = Arg(1);
  3785. bytes = (byte *) Arg(2);
  3786. mb = ms_matchbuffer(tmp_arg1);
  3787. if (mb->size - mb->offset < bits) {
  3788. ClauseFail();
  3789. }
  3790. offs = mb->offset & 7;
  3791. if (offs == 0 && (bits & 7) == 0) {
  3792. if (sys_memcmp(bytes, mb->base+(mb->offset>>3), bits>>3)) {
  3793. ClauseFail();
  3794. }
  3795. } else if (erts_cmp_bits(bytes, 0, mb->base+(mb->offset>>3), mb->offset & 7, bits)) {
  3796. ClauseFail();
  3797. }
  3798. mb->offset += bits;
  3799. NextPF(3, next);
  3800. }
  3801. }
  3802. OpCase(i_bs_save2_rI): {
  3803. BeamInstr *next;
  3804. ErlBinMatchState *_ms;
  3805. PreFetch(1, next);
  3806. _ms = (ErlBinMatchState*) boxed_val((Eterm) r(0));
  3807. _ms->save_offset[Arg(0)] = _ms->mb.offset;
  3808. NextPF(1, next);
  3809. }
  3810. OpCase(i_bs_save2_xI): {
  3811. BeamInstr *next;
  3812. ErlBinMatchState *_ms;
  3813. PreFetch(2, next);
  3814. _ms = (ErlBinMatchState*) boxed_val((Eterm) xb(Arg(0)));
  3815. _ms->save_offset[Arg(1)] = _ms->mb.offset;
  3816. NextPF(2, next);
  3817. }
  3818. OpCase(i_bs_restore2_rI): {
  3819. BeamInstr *next;
  3820. ErlBinMatchState *_ms;
  3821. PreFetch(1, next);
  3822. _ms = (ErlBinMatchState*) boxed_val((Eterm) r(0));
  3823. _ms->mb.offset = _ms->save_offset[Arg(0)];
  3824. NextPF(1, next);
  3825. }
  3826. OpCase(i_bs_restore2_xI): {
  3827. BeamInstr *next;
  3828. ErlBinMatchState *_ms;
  3829. PreFetch(2, next);
  3830. _ms = (ErlBinMatchState*) boxed_val((Eterm) xb(Arg(0)));
  3831. _ms->mb.offset = _ms->save_offset[Arg(1)];
  3832. NextPF(2, next);
  3833. }
  3834. #include "beam_cold.h"
  3835. /*
  3836. * This instruction is probably never used (because it is combined with a
  3837. * a return). However, a future compiler might for some reason emit a
  3838. * deallocate not followed by a return, and that should work.
  3839. */
  3840. OpCase(deallocate_I): {
  3841. BeamInstr *next;
  3842. PreFetch(1, next);
  3843. D(Arg(0));
  3844. NextPF(1, next);
  3845. }
  3846. /*
  3847. * Trace and debugging support.
  3848. */
  3849. /*
  3850. * At this point, I points to the code[3] in the export entry for
  3851. * a trace-enabled function.
  3852. *
  3853. * code[0]: Module
  3854. * code[1]: Function
  3855. * code[2]: Arity
  3856. * code[3]: &&call_traced_function
  3857. * code[4]: Address of function.
  3858. */
  3859. OpCase(call_traced_function): {
  3860. if (IS_TRACED_FL(c_p, F_TRACE_CALLS)) {
  3861. unsigned offset = offsetof(Export, code) + 3*sizeof(BeamInstr);
  3862. Export* ep = (Export *) (((char *)I)-offset);
  3863. Uint32 flags;
  3864. SWAPOUT;
  3865. reg[0] = r(0);
  3866. PROCESS_MAIN_CHK_LOCKS(c_p);
  3867. ERTS_SMP_UNREQ_PROC_MAIN_LOCK(c_p);
  3868. flags = erts_call_trace(c_p, ep->code, ep->match_prog_set, reg,
  3869. 0, &c_p->tracer_proc);
  3870. ERTS_SMP_REQ_PROC_MAIN_LOCK(c_p);
  3871. PROCESS_MAIN_CHK_LOCKS(c_p);
  3872. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  3873. SWAPIN;
  3874. if (flags & MATCH_SET_RX_TRACE) {
  3875. ASSERT(c_p->htop <= E && E <= c_p->hend);
  3876. if (E - 3 < HTOP) {
  3877. /* SWAPOUT, SWAPIN was done and r(0) was saved above */
  3878. PROCESS_MAIN_CHK_LOCKS(c_p);
  3879. FCALLS -= erts_garbage_collect(c_p, 3, reg, ep->code[2]);
  3880. PROCESS_MAIN_CHK_LOCKS(c_p);
  3881. r(0) = reg[0];
  3882. SWAPIN;
  3883. }
  3884. E -= 3;
  3885. ASSERT(c_p->htop <= E && E <= c_p->hend);
  3886. ASSERT(is_CP((BeamInstr)(ep->code)));
  3887. ASSERT(is_internal_pid(c_p->tracer_proc) ||
  3888. is_internal_port(c_p->tracer_proc));
  3889. E[2] = make_cp(c_p->cp); /* XXX:PaN - code in lower range on halfword */
  3890. E[1] = am_true; /* Process tracer */
  3891. E[0] = make_cp(ep->code);
  3892. c_p->cp = (flags & MATCH_SET_EXCEPTION_TRACE)
  3893. ? beam_exception_trace : beam_return_trace;
  3894. erts_smp_proc_lock(c_p, ERTS_PROC_LOCKS_ALL_MINOR);
  3895. c_p->trace_flags |= F_EXCEPTION_TRACE;
  3896. erts_smp_proc_unlock(c_p, ERTS_PROC_LOCKS_ALL_MINOR);
  3897. }
  3898. }
  3899. SET_I((BeamInstr *)Arg(0));
  3900. Dispatch();
  3901. }
  3902. OpCase(return_trace): {
  3903. BeamInstr* code = (BeamInstr *) (UWord) E[0];
  3904. SWAPOUT; /* Needed for shared heap */
  3905. ERTS_SMP_UNREQ_PROC_MAIN_LOCK(c_p);
  3906. erts_trace_return(c_p, code, r(0), E+1/*Process tracer*/);
  3907. ERTS_SMP_REQ_PROC_MAIN_LOCK(c_p);
  3908. SWAPIN;
  3909. c_p->cp = NULL;
  3910. SET_I((BeamInstr *) cp_val(E[2]));
  3911. E += 3;
  3912. Goto(*I);
  3913. }
  3914. OpCase(i_count_breakpoint): {
  3915. BeamInstr real_I;
  3916. ErtsCountBreak(c_p, (BeamInstr *) I, &real_I);
  3917. ASSERT(VALID_INSTR(real_I));
  3918. Goto(real_I);
  3919. }
  3920. /* need to send mfa instead of bdt pointer
  3921. * the pointer might be deallocated.
  3922. */
  3923. OpCase(i_time_breakpoint): {
  3924. BeamInstr real_I;
  3925. BpData **bds = (BpData **) (I)[-4];
  3926. BpDataTime *bdt = NULL;
  3927. Uint ix = 0;
  3928. #ifdef ERTS_SMP
  3929. ix = c_p->scheduler_data->no - 1;
  3930. #else
  3931. ix = 0;
  3932. #endif
  3933. bdt = (BpDataTime *)bds[ix];
  3934. ASSERT((I)[-5] == (BeamInstr) BeamOp(op_i_func_info_IaaI));
  3935. ASSERT(bdt);
  3936. bdt = (BpDataTime *) bdt->next;
  3937. ASSERT(bdt);
  3938. bds[ix] = (BpData *) bdt;
  3939. real_I = bdt->orig_instr;
  3940. ASSERT(VALID_INSTR(real_I));
  3941. if (IS_TRACED_FL(c_p, F_TRACE_CALLS) && !(bdt->pause)) {
  3942. if ( (*(c_p->cp) == (BeamInstr) OpCode(i_return_time_trace)) ||
  3943. (*(c_p->cp) == (BeamInstr) OpCode(return_trace)) ||
  3944. (*(c_p->cp) == (BeamInstr) OpCode(i_return_to_trace))) {
  3945. /* This _IS_ a tail recursive call */
  3946. SWAPOUT;
  3947. erts_trace_time_break(c_p, I, bdt, ERTS_BP_CALL_TIME_TAIL_CALL);
  3948. SWAPIN;
  3949. } else {
  3950. SWAPOUT;
  3951. erts_trace_time_break(c_p, I, bdt, ERTS_BP_CALL_TIME_CALL);
  3952. /* r register needs to be copied to the array
  3953. * for the garbage collector
  3954. */
  3955. ASSERT(c_p->htop <= E && E <= c_p->hend);
  3956. if (E - 2 < HTOP) {
  3957. reg[0] = r(0);
  3958. PROCESS_MAIN_CHK_LOCKS(c_p);
  3959. FCALLS -= erts_garbage_collect(c_p, 2, reg, I[-1]);
  3960. PROCESS_MAIN_CHK_LOCKS(c_p);
  3961. r(0) = reg[0];
  3962. }
  3963. SWAPIN;
  3964. ASSERT(c_p->htop <= E && E <= c_p->hend);
  3965. E -= 2;
  3966. E[0] = make_cp(I);
  3967. E[1] = make_cp(c_p->cp); /* original return address */
  3968. c_p->cp = (BeamInstr *) make_cp(beam_return_time_trace);
  3969. }
  3970. }
  3971. Goto(real_I);
  3972. }
  3973. OpCase(i_return_time_trace): {
  3974. BeamInstr *pc = (BeamInstr *) (UWord) E[0];
  3975. SWAPOUT;
  3976. erts_trace_time_break(c_p, pc, NULL, ERTS_BP_CALL_TIME_RETURN);
  3977. SWAPIN;
  3978. c_p->cp = NULL;
  3979. SET_I((BeamInstr *) cp_val(E[1]));
  3980. E += 2;
  3981. Goto(*I);
  3982. }
  3983. OpCase(i_trace_breakpoint):
  3984. if (! IS_TRACED_FL(c_p, F_TRACE_CALLS)) {
  3985. BeamInstr real_I;
  3986. ErtsBreakSkip(c_p, (BeamInstr *) I, &real_I);
  3987. Goto(real_I);
  3988. }
  3989. /* Fall through to next case */
  3990. OpCase(i_mtrace_breakpoint): {
  3991. BeamInstr real_I;
  3992. Uint32 flags;
  3993. Eterm tracer_pid;
  3994. BeamInstr *cpp;
  3995. int return_to_trace = 0, need = 0;
  3996. flags = 0;
  3997. SWAPOUT;
  3998. reg[0] = r(0);
  3999. if (*(c_p->cp) == (BeamInstr) OpCode(return_trace)) {
  4000. cpp = (BeamInstr*)&E[2];
  4001. } else if (*(c_p->cp) == (BeamInstr) OpCode(i_return_to_trace)) {
  4002. return_to_trace = !0;
  4003. cpp = (BeamInstr*)&E[0];
  4004. } else if (*(c_p->cp) == (BeamInstr) OpCode(i_return_time_trace)) {
  4005. return_to_trace = !0;
  4006. cpp = (BeamInstr*)&E[0];
  4007. } else {
  4008. cpp = NULL;
  4009. }
  4010. if (cpp) {
  4011. /* This _IS_ a tail recursive call, if there are
  4012. * return_trace and/or i_return_to_trace stackframes
  4013. * on the stack, they are not intermixed with y registers
  4014. */
  4015. BeamInstr *cp_save = c_p->cp;
  4016. for (;;) {
  4017. ASSERT(is_CP(*cpp));
  4018. if (*cp_val(*cpp) == (BeamInstr) OpCode(return_trace)) {
  4019. cpp += 3;
  4020. } else if (*cp_val(*cpp) == (BeamInstr) OpCode(i_return_to_trace)) {
  4021. return_to_trace = !0;
  4022. cpp += 1;
  4023. } else if (*cp_val(*cpp) == (BeamInstr) OpCode(i_return_time_trace)) {
  4024. cpp += 2;
  4025. } else
  4026. break;
  4027. }
  4028. c_p->cp = (BeamInstr *) cp_val(*cpp);
  4029. ASSERT(is_CP(*cpp));
  4030. ERTS_SMP_UNREQ_PROC_MAIN_LOCK(c_p);
  4031. real_I = erts_trace_break(c_p, I, reg, &flags, &tracer_pid);
  4032. ERTS_SMP_REQ_PROC_MAIN_LOCK(c_p);
  4033. SWAPIN; /* Needed by shared heap. */
  4034. c_p->cp = cp_save;
  4035. } else {
  4036. ERTS_SMP_UNREQ_PROC_MAIN_LOCK(c_p);
  4037. real_I = erts_trace_break(c_p, I, reg, &flags, &tracer_pid);
  4038. ERTS_SMP_REQ_PROC_MAIN_LOCK(c_p);
  4039. SWAPIN; /* Needed by shared heap. */
  4040. }
  4041. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  4042. if ((flags & MATCH_SET_RETURN_TO_TRACE) && !return_to_trace) {
  4043. need += 1;
  4044. }
  4045. if (flags & MATCH_SET_RX_TRACE) {
  4046. need += 3;
  4047. }
  4048. if (need) {
  4049. ASSERT(c_p->htop <= E && E <= c_p->hend);
  4050. if (E - need < HTOP) {
  4051. /* SWAPOUT was done and r(0) was saved above */
  4052. PROCESS_MAIN_CHK_LOCKS(c_p);
  4053. FCALLS -= erts_garbage_collect(c_p, need, reg, I[-1]);
  4054. PROCESS_MAIN_CHK_LOCKS(c_p);
  4055. r(0) = reg[0];
  4056. SWAPIN;
  4057. }
  4058. }
  4059. if ((flags & MATCH_SET_RETURN_TO_TRACE) && !return_to_trace) {
  4060. E -= 1;
  4061. ASSERT(c_p->htop <= E && E <= c_p->hend);
  4062. E[0] = make_cp(c_p->cp);
  4063. c_p->cp = (BeamInstr *) beam_return_to_trace;
  4064. }
  4065. if (flags & MATCH_SET_RX_TRACE) {
  4066. E -= 3;
  4067. ASSERT(c_p->htop <= E && E <= c_p->hend);
  4068. ASSERT(is_CP((Eterm) (UWord) (I - 3)));
  4069. ASSERT(am_true == tracer_pid ||
  4070. is_internal_pid(tracer_pid) || is_internal_port(tracer_pid));
  4071. E[2] = make_cp(c_p->cp);
  4072. E[1] = tracer_pid;
  4073. E[0] = make_cp(I - 3); /* We ARE at the beginning of an
  4074. instruction,
  4075. the funcinfo is above i. */
  4076. c_p->cp =
  4077. (flags & MATCH_SET_EXCEPTION_TRACE)
  4078. ? beam_exception_trace : beam_return_trace;
  4079. erts_smp_proc_lock(c_p, ERTS_PROC_LOCKS_ALL_MINOR);
  4080. c_p->trace_flags |= F_EXCEPTION_TRACE;
  4081. erts_smp_proc_unlock(c_p, ERTS_PROC_LOCKS_ALL_MINOR);
  4082. }
  4083. Goto(real_I);
  4084. }
  4085. OpCase(i_return_to_trace): {
  4086. if (IS_TRACED_FL(c_p, F_TRACE_RETURN_TO)) {
  4087. Uint *cpp = (Uint*) E;
  4088. for(;;) {
  4089. ASSERT(is_CP(*cpp));
  4090. if (*cp_val(*cpp) == (BeamInstr) OpCode(return_trace)) {
  4091. do ++cpp; while(is_not_CP(*cpp));
  4092. cpp += 2;
  4093. } else if (*cp_val(*cpp) == (BeamInstr) OpCode(i_return_to_trace)) {
  4094. do ++cpp; while(is_not_CP(*cpp));
  4095. } else break;
  4096. }
  4097. SWAPOUT; /* Needed for shared heap */
  4098. ERTS_SMP_UNREQ_PROC_MAIN_LOCK(c_p);
  4099. erts_trace_return_to(c_p, cp_val(*cpp));
  4100. ERTS_SMP_REQ_PROC_MAIN_LOCK(c_p);
  4101. SWAPIN;
  4102. }
  4103. c_p->cp = NULL;
  4104. SET_I((BeamInstr *) cp_val(E[0]));
  4105. E += 1;
  4106. Goto(*I);
  4107. }
  4108. /*
  4109. * Instructions for allocating on the message area.
  4110. */
  4111. OpCase(i_global_cons):
  4112. {
  4113. BeamInstr *next;
  4114. #ifdef HYBRID
  4115. Eterm *hp;
  4116. PreFetch(0,next);
  4117. TestGlobalHeap(2,2,hp);
  4118. hp[0] = r(0);
  4119. hp[1] = x(1);
  4120. r(0) = make_list(hp);
  4121. #ifndef INCREMENTAL
  4122. global_htop += 2;
  4123. #endif
  4124. NextPF(0,next);
  4125. #else
  4126. PreFetch(0,next);
  4127. c_p->freason = EXC_INTERNAL_ERROR;
  4128. goto find_func_info;
  4129. #endif
  4130. }
  4131. OpCase(i_global_tuple):
  4132. {
  4133. BeamInstr *next;
  4134. int len;
  4135. #ifdef HYBRID
  4136. Eterm list;
  4137. Eterm *hp;
  4138. #endif
  4139. if ((len = list_length(r(0))) < 0) {
  4140. goto badarg;
  4141. }
  4142. PreFetch(0,next);
  4143. #ifdef HYBRID
  4144. TestGlobalHeap(len + 1,1,hp);
  4145. list = r(0);
  4146. r(0) = make_tuple(hp);
  4147. *hp++ = make_arityval(len);
  4148. while(is_list(list))
  4149. {
  4150. Eterm* cons = list_val(list);
  4151. *hp++ = CAR(cons);
  4152. list = CDR(cons);
  4153. }
  4154. #ifndef INCREMENTAL
  4155. global_htop += len + 1;
  4156. #endif
  4157. NextPF(0,next);
  4158. #else
  4159. c_p->freason = EXC_INTERNAL_ERROR;
  4160. goto find_func_info;
  4161. #endif
  4162. }
  4163. OpCase(i_global_copy):
  4164. {
  4165. BeamInstr *next;
  4166. PreFetch(0,next);
  4167. #ifdef HYBRID
  4168. if (!IS_CONST(r(0)))
  4169. {
  4170. BM_SWAP_TIMER(system,copy);
  4171. SWAPOUT;
  4172. reg[0] = r(0);
  4173. reg[1] = NIL;
  4174. r(0) = copy_struct_lazy(c_p,r(0),0);
  4175. ASSERT(ma_src_top == 0);
  4176. ASSERT(ma_dst_top == 0);
  4177. ASSERT(ma_offset_top == 0);
  4178. SWAPIN;
  4179. BM_SWAP_TIMER(copy,system);
  4180. }
  4181. NextPF(0,next);
  4182. #else
  4183. c_p->freason = EXC_INTERNAL_ERROR;
  4184. goto find_func_info;
  4185. #endif
  4186. }
  4187. /*
  4188. * New floating point instructions.
  4189. */
  4190. OpCase(fmove_ql): {
  4191. Eterm fr = Arg(1);
  4192. BeamInstr *next;
  4193. PreFetch(2, next);
  4194. GET_DOUBLE(Arg(0), *(FloatDef*)ADD_BYTE_OFFSET(freg, fr));
  4195. NextPF(2, next);
  4196. }
  4197. OpCase(fmove_dl): {
  4198. Eterm targ1;
  4199. Eterm fr = Arg(1);
  4200. BeamInstr *next;
  4201. PreFetch(2, next);
  4202. GetR(0, targ1);
  4203. /* Arg(0) == HEADER_FLONUM */
  4204. GET_DOUBLE(targ1, *(FloatDef*)ADD_BYTE_OFFSET(freg, fr));
  4205. NextPF(2, next);
  4206. }
  4207. OpCase(fmove_new_ld): {
  4208. Eterm fr = Arg(0);
  4209. Eterm dest = make_float(HTOP);
  4210. PUT_DOUBLE(*(FloatDef*)ADD_BYTE_OFFSET(freg, fr), HTOP);
  4211. HTOP += FLOAT_SIZE_OBJECT;
  4212. StoreBifResult(1, dest);
  4213. }
  4214. OpCase(fconv_dl): {
  4215. Eterm targ1;
  4216. Eterm fr = Arg(1);
  4217. BeamInstr *next;
  4218. GetR(0, targ1);
  4219. PreFetch(2, next);
  4220. if (is_small(targ1)) {
  4221. fb(fr) = (double) signed_val(targ1);
  4222. } else if (is_big(targ1)) {
  4223. if (big_to_double(targ1, &fb(fr)) < 0) {
  4224. goto fbadarith;
  4225. }
  4226. } else if (is_float(targ1)) {
  4227. GET_DOUBLE(targ1, *(FloatDef*)ADD_BYTE_OFFSET(freg, fr));
  4228. } else {
  4229. goto fbadarith;
  4230. }
  4231. NextPF(2, next);
  4232. }
  4233. /*
  4234. * Old allocating fmove.
  4235. */
  4236. #ifdef NO_FPE_SIGNALS
  4237. OpCase(fclearerror):
  4238. OpCase(i_fcheckerror):
  4239. erl_exit(1, "fclearerror/i_fcheckerror without fpe signals (beam_emu)");
  4240. #else
  4241. OpCase(fclearerror): {
  4242. BeamInstr *next;
  4243. PreFetch(0, next);
  4244. ERTS_FP_CHECK_INIT(c_p);
  4245. NextPF(0, next);
  4246. }
  4247. OpCase(i_fcheckerror): {
  4248. BeamInstr *next;
  4249. PreFetch(0, next);
  4250. ERTS_FP_ERROR(c_p, freg[0].fd, goto fbadarith);
  4251. NextPF(0, next);
  4252. }
  4253. # undef ERTS_FP_CHECK_INIT
  4254. # undef ERTS_FP_ERROR
  4255. # define ERTS_FP_CHECK_INIT(p)
  4256. # define ERTS_FP_ERROR(p, a, b)
  4257. #endif
  4258. OpCase(i_fadd_lll): {
  4259. BeamInstr *next;
  4260. PreFetch(3, next);
  4261. ERTS_FP_CHECK_INIT(c_p);
  4262. fb(Arg(2)) = fb(Arg(0)) + fb(Arg(1));
  4263. ERTS_FP_ERROR(c_p, fb(Arg(2)), goto fbadarith);
  4264. NextPF(3, next);
  4265. }
  4266. OpCase(i_fsub_lll): {
  4267. BeamInstr *next;
  4268. PreFetch(3, next);
  4269. ERTS_FP_CHECK_INIT(c_p);
  4270. fb(Arg(2)) = fb(Arg(0)) - fb(Arg(1));
  4271. ERTS_FP_ERROR(c_p, fb(Arg(2)), goto fbadarith);
  4272. NextPF(3, next);
  4273. }
  4274. OpCase(i_fmul_lll): {
  4275. BeamInstr *next;
  4276. PreFetch(3, next);
  4277. ERTS_FP_CHECK_INIT(c_p);
  4278. fb(Arg(2)) = fb(Arg(0)) * fb(Arg(1));
  4279. ERTS_FP_ERROR(c_p, fb(Arg(2)), goto fbadarith);
  4280. NextPF(3, next);
  4281. }
  4282. OpCase(i_fdiv_lll): {
  4283. BeamInstr *next;
  4284. PreFetch(3, next);
  4285. ERTS_FP_CHECK_INIT(c_p);
  4286. fb(Arg(2)) = fb(Arg(0)) / fb(Arg(1));
  4287. ERTS_FP_ERROR(c_p, fb(Arg(2)), goto fbadarith);
  4288. NextPF(3, next);
  4289. }
  4290. OpCase(i_fnegate_ll): {
  4291. BeamInstr *next;
  4292. PreFetch(2, next);
  4293. ERTS_FP_CHECK_INIT(c_p);
  4294. fb(Arg(1)) = -fb(Arg(0));
  4295. ERTS_FP_ERROR(c_p, fb(Arg(1)), goto fbadarith);
  4296. NextPF(2, next);
  4297. fbadarith:
  4298. c_p->freason = BADARITH;
  4299. goto find_func_info;
  4300. }
  4301. #ifdef HIPE
  4302. {
  4303. unsigned cmd;
  4304. OpCase(hipe_trap_call): {
  4305. /*
  4306. * I[-5]: &&lb_i_func_info_IaaI
  4307. * I[-4]: Native code callee (inserted by HiPE)
  4308. * I[-3]: Module (tagged atom)
  4309. * I[-2]: Function (tagged atom)
  4310. * I[-1]: Arity (untagged integer)
  4311. * I[ 0]: &&lb_hipe_trap_call
  4312. * ... remainder of original BEAM code
  4313. */
  4314. ASSERT(I[-5] == (Uint) OpCode(i_func_info_IaaI));
  4315. c_p->hipe.ncallee = (void(*)(void)) I[-4];
  4316. cmd = HIPE_MODE_SWITCH_CMD_CALL | (I[-1] << 8);
  4317. ++hipe_trap_count;
  4318. goto L_hipe_mode_switch;
  4319. }
  4320. OpCase(hipe_trap_call_closure): {
  4321. ASSERT(I[-5] == (Uint) OpCode(i_func_info_IaaI));
  4322. c_p->hipe.ncallee = (void(*)(void)) I[-4];
  4323. cmd = HIPE_MODE_SWITCH_CMD_CALL_CLOSURE | (I[-1] << 8);
  4324. ++hipe_trap_count;
  4325. goto L_hipe_mode_switch;
  4326. }
  4327. OpCase(hipe_trap_return): {
  4328. cmd = HIPE_MODE_SWITCH_CMD_RETURN;
  4329. goto L_hipe_mode_switch;
  4330. }
  4331. OpCase(hipe_trap_throw): {
  4332. cmd = HIPE_MODE_SWITCH_CMD_THROW;
  4333. goto L_hipe_mode_switch;
  4334. }
  4335. OpCase(hipe_trap_resume): {
  4336. cmd = HIPE_MODE_SWITCH_CMD_RESUME;
  4337. goto L_hipe_mode_switch;
  4338. }
  4339. L_hipe_mode_switch:
  4340. /* XXX: this abuse of def_arg_reg[] is horrid! */
  4341. SWAPOUT;
  4342. c_p->fcalls = FCALLS;
  4343. c_p->def_arg_reg[4] = -neg_o_reds;
  4344. reg[0] = r(0);
  4345. c_p = hipe_mode_switch(c_p, cmd, reg);
  4346. #ifdef ERTS_SMP
  4347. reg = c_p->scheduler_data->save_reg;
  4348. freg = c_p->scheduler_data->freg;
  4349. #endif
  4350. ERL_BITS_RELOAD_STATEP(c_p);
  4351. neg_o_reds = -c_p->def_arg_reg[4];
  4352. FCALLS = c_p->fcalls;
  4353. SWAPIN;
  4354. switch( c_p->def_arg_reg[3] ) { /* XXX:PaN - Halfword wont work with hipe yet... */
  4355. case HIPE_MODE_SWITCH_RES_RETURN:
  4356. ASSERT(is_value(reg[0]));
  4357. MoveReturn(reg[0], r(0));
  4358. case HIPE_MODE_SWITCH_RES_CALL:
  4359. SET_I(c_p->i);
  4360. r(0) = reg[0];
  4361. Dispatch();
  4362. case HIPE_MODE_SWITCH_RES_CALL_CLOSURE:
  4363. /* This can be used to call any function value, but currently it's
  4364. only used to call closures referring to unloaded modules. */
  4365. {
  4366. BeamInstr *next;
  4367. next = call_fun(c_p, c_p->arity - 1, reg, THE_NON_VALUE);
  4368. SWAPIN;
  4369. if (next != NULL) {
  4370. r(0) = reg[0];
  4371. SET_I(next);
  4372. Dispatchfun();
  4373. }
  4374. goto find_func_info;
  4375. }
  4376. case HIPE_MODE_SWITCH_RES_THROW:
  4377. c_p->cp = NULL;
  4378. I = handle_error(c_p, I, reg, NULL);
  4379. goto post_error_handling;
  4380. default:
  4381. erl_exit(1, "hipe_mode_switch: result %u\n", c_p->def_arg_reg[3]);
  4382. }
  4383. }
  4384. OpCase(hipe_call_count): {
  4385. /*
  4386. * I[-5]: &&lb_i_func_info_IaaI
  4387. * I[-4]: pointer to struct hipe_call_count (inserted by HiPE)
  4388. * I[-3]: Module (tagged atom)
  4389. * I[-2]: Function (tagged atom)
  4390. * I[-1]: Arity (untagged integer)
  4391. * I[ 0]: &&lb_hipe_call_count
  4392. * ... remainder of original BEAM code
  4393. */
  4394. struct hipe_call_count *hcc = (struct hipe_call_count*)I[-4];
  4395. ASSERT(I[-5] == (Uint) OpCode(i_func_info_IaaI));
  4396. ASSERT(hcc != NULL);
  4397. ASSERT(VALID_INSTR(hcc->opcode));
  4398. ++(hcc->count);
  4399. Goto(hcc->opcode);
  4400. }
  4401. #endif /* HIPE */
  4402. OpCase(i_yield):
  4403. {
  4404. /* This is safe as long as REDS_IN(c_p) is never stored
  4405. * in c_p->arg_reg[0]. It is currently stored in c_p->def_arg_reg[5],
  4406. * which may be c_p->arg_reg[5], which is close, but no banana.
  4407. */
  4408. c_p->arg_reg[0] = am_true;
  4409. c_p->arity = 1; /* One living register (the 'true' return value) */
  4410. SWAPOUT;
  4411. c_p->i = I + 1; /* Next instruction */
  4412. erts_smp_proc_lock(c_p, ERTS_PROC_LOCK_STATUS);
  4413. erts_add_to_runq(c_p);
  4414. erts_smp_proc_unlock(c_p, ERTS_PROC_LOCK_STATUS);
  4415. c_p->current = NULL;
  4416. goto do_schedule;
  4417. }
  4418. OpCase(i_hibernate): {
  4419. SWAPOUT;
  4420. if (hibernate(c_p, r(0), x(1), x(2), reg)) {
  4421. goto do_schedule;
  4422. } else {
  4423. I = handle_error(c_p, I, reg, hibernate_3);
  4424. goto post_error_handling;
  4425. }
  4426. }
  4427. OpCase(i_debug_breakpoint): {
  4428. SWAPOUT;
  4429. reg[0] = r(0);
  4430. tmp_arg1 = call_breakpoint_handler(c_p, I-3, reg);
  4431. r(0) = reg[0];
  4432. SWAPIN;
  4433. if (tmp_arg1) {
  4434. SET_I(c_p->i);
  4435. Dispatch();
  4436. }
  4437. goto no_error_handler;
  4438. }
  4439. OpCase(system_limit_j):
  4440. system_limit:
  4441. c_p->freason = SYSTEM_LIMIT;
  4442. goto lb_Cl_error;
  4443. #ifdef ERTS_OPCODE_COUNTER_SUPPORT
  4444. DEFINE_COUNTING_LABELS;
  4445. #endif
  4446. #ifndef NO_JUMP_TABLE
  4447. #ifdef DEBUG
  4448. end_emulator_loop:
  4449. #endif
  4450. #endif
  4451. OpCase(int_code_end):
  4452. OpCase(label_L):
  4453. OpCase(too_old_compiler):
  4454. OpCase(on_load):
  4455. erl_exit(1, "meta op\n");
  4456. /*
  4457. * One-time initialization of Beam emulator.
  4458. */
  4459. init_emulator:
  4460. {
  4461. int i;
  4462. Export* ep;
  4463. #ifndef NO_JUMP_TABLE
  4464. #ifdef ERTS_OPCODE_COUNTER_SUPPORT
  4465. /* Are tables correctly generated by beam_makeops? */
  4466. ASSERT(sizeof(counting_opcodes) == sizeof(opcodes));
  4467. if (count_instructions) {
  4468. #ifdef DEBUG
  4469. counting_opcodes[op_catch_end_y] = LabelAddr(lb_catch_end_y);
  4470. #endif
  4471. counting_opcodes[op_i_func_info_IaaI] = LabelAddr(lb_i_func_info_IaaI);
  4472. beam_ops = counting_opcodes;
  4473. }
  4474. else
  4475. #endif /* #ifndef ERTS_OPCODE_COUNTER_SUPPORT */
  4476. {
  4477. beam_ops = opcodes;
  4478. }
  4479. #endif /* NO_JUMP_TABLE */
  4480. em_call_error_handler = OpCode(call_error_handler);
  4481. em_call_traced_function = OpCode(call_traced_function);
  4482. em_apply_bif = OpCode(apply_bif);
  4483. beam_apply[0] = (BeamInstr) OpCode(i_apply);
  4484. beam_apply[1] = (BeamInstr) OpCode(normal_exit);
  4485. beam_exit[0] = (BeamInstr) OpCode(error_action_code);
  4486. beam_continue_exit[0] = (BeamInstr) OpCode(continue_exit);
  4487. beam_return_to_trace[0] = (BeamInstr) OpCode(i_return_to_trace);
  4488. beam_return_trace[0] = (BeamInstr) OpCode(return_trace);
  4489. beam_exception_trace[0] = (BeamInstr) OpCode(return_trace); /* UGLY */
  4490. beam_return_time_trace[0] = (BeamInstr) OpCode(i_return_time_trace);
  4491. /*
  4492. * Enter all BIFs into the export table.
  4493. */
  4494. for (i = 0; i < BIF_SIZE; i++) {
  4495. ep = erts_export_put(bif_table[i].module,
  4496. bif_table[i].name,
  4497. bif_table[i].arity);
  4498. bif_export[i] = ep;
  4499. ep->code[3] = (BeamInstr) OpCode(apply_bif);
  4500. ep->code[4] = (BeamInstr) bif_table[i].f;
  4501. /* XXX: set func info for bifs */
  4502. ((BeamInstr*)ep->code + 3)[-5] = (BeamInstr) BeamOp(op_i_func_info_IaaI);
  4503. }
  4504. return;
  4505. }
  4506. #ifdef NO_JUMP_TABLE
  4507. default:
  4508. erl_exit(1, "unexpected op code %d\n",Go);
  4509. }
  4510. #endif
  4511. return; /* Never executed */
  4512. save_calls1:
  4513. {
  4514. Eterm* dis_next;
  4515. save_calls(c_p, (Export *) Arg(0));
  4516. SET_I(((Export *) Arg(0))->address);
  4517. dis_next = (Eterm *) *I;
  4518. FCALLS--;
  4519. Goto(dis_next);
  4520. }
  4521. }
  4522. static BifFunction
  4523. translate_gc_bif(void* gcf)
  4524. {
  4525. if (gcf == erts_gc_length_1) {
  4526. return length_1;
  4527. } else if (gcf == erts_gc_size_1) {
  4528. return size_1;
  4529. } else if (gcf == erts_gc_bit_size_1) {
  4530. return bit_size_1;
  4531. } else if (gcf == erts_gc_byte_size_1) {
  4532. return byte_size_1;
  4533. } else if (gcf == erts_gc_abs_1) {
  4534. return abs_1;
  4535. } else if (gcf == erts_gc_float_1) {
  4536. return float_1;
  4537. } else if (gcf == erts_gc_round_1) {
  4538. return round_1;
  4539. } else if (gcf == erts_gc_trunc_1) {
  4540. return round_1;
  4541. } else if (gcf == erts_gc_binary_part_2) {
  4542. return binary_part_2;
  4543. } else if (gcf == erts_gc_binary_part_3) {
  4544. return binary_part_3;
  4545. } else {
  4546. erl_exit(1, "bad gc bif");
  4547. }
  4548. }
  4549. /*
  4550. * Mapping from the error code 'class tag' to atoms.
  4551. */
  4552. Eterm exception_tag[NUMBER_EXC_TAGS] = {
  4553. am_error, /* 0 */
  4554. am_exit, /* 1 */
  4555. am_throw, /* 2 */
  4556. };
  4557. /*
  4558. * Mapping from error code 'index' to atoms.
  4559. */
  4560. Eterm error_atom[NUMBER_EXIT_CODES] = {
  4561. am_internal_error, /* 0 */
  4562. am_normal, /* 1 */
  4563. am_internal_error, /* 2 */
  4564. am_badarg, /* 3 */
  4565. am_badarith, /* 4 */
  4566. am_badmatch, /* 5 */
  4567. am_function_clause, /* 6 */
  4568. am_case_clause, /* 7 */
  4569. am_if_clause, /* 8 */
  4570. am_undef, /* 9 */
  4571. am_badfun, /* 10 */
  4572. am_badarity, /* 11 */
  4573. am_timeout_value, /* 12 */
  4574. am_noproc, /* 13 */
  4575. am_notalive, /* 14 */
  4576. am_system_limit, /* 15 */
  4577. am_try_clause, /* 16 */
  4578. am_notsup /* 17 */
  4579. };
  4580. /*
  4581. * To fully understand the error handling, one must keep in mind that
  4582. * when an exception is thrown, the search for a handler can jump back
  4583. * and forth between Beam and native code. Upon each mode switch, a
  4584. * dummy handler is inserted so that if an exception reaches that point,
  4585. * the handler is invoked (like any handler) and transfers control so
  4586. * that the search for a real handler is continued in the other mode.
  4587. * Therefore, c_p->freason and c_p->fvalue must still hold the exception
  4588. * info when the handler is executed, but normalized so that creation of
  4589. * error terms and saving of the stack trace is only done once, even if
  4590. * we pass through the error handling code several times.
  4591. *
  4592. * When a new exception is raised, the current stack trace information
  4593. * is quick-saved in a small structure allocated on the heap. Depending
  4594. * on how the exception is eventually caught (perhaps by causing the
  4595. * current process to terminate), the saved information may be used to
  4596. * create a symbolic (human-readable) representation of the stack trace
  4597. * at the point of the original exception.
  4598. */
  4599. static BeamInstr*
  4600. handle_error(Process* c_p, BeamInstr* pc, Eterm* reg, BifFunction bf)
  4601. {
  4602. Eterm* hp;
  4603. Eterm Value = c_p->fvalue;
  4604. Eterm Args = am_true;
  4605. c_p->i = pc; /* In case we call erl_exit(). */
  4606. ASSERT(c_p->freason != TRAP); /* Should have been handled earlier. */
  4607. /*
  4608. * Check if we have an arglist for the top level call. If so, this
  4609. * is encoded in Value, so we have to dig out the real Value as well
  4610. * as the Arglist.
  4611. */
  4612. if (c_p->freason & EXF_ARGLIST) {
  4613. Eterm* tp;
  4614. ASSERT(is_tuple(Value));
  4615. tp = tuple_val(Value);
  4616. Value = tp[1];
  4617. Args = tp[2];
  4618. }
  4619. /*
  4620. * Save the stack trace info if the EXF_SAVETRACE flag is set. The
  4621. * main reason for doing this separately is to allow throws to later
  4622. * become promoted to errors without losing the original stack
  4623. * trace, even if they have passed through one or more catch and
  4624. * rethrow. It also makes the creation of symbolic stack traces much
  4625. * more modular.
  4626. */
  4627. if (c_p->freason & EXF_SAVETRACE) {
  4628. save_stacktrace(c_p, pc, reg, bf, Args);
  4629. }
  4630. /*
  4631. * Throws that are not caught are turned into 'nocatch' errors
  4632. */
  4633. if ((c_p->freason & EXF_THROWN) && (c_p->catches <= 0) ) {
  4634. hp = HAlloc(c_p, 3);
  4635. Value = TUPLE2(hp, am_nocatch, Value);
  4636. c_p->freason = EXC_ERROR;
  4637. }
  4638. /* Get the fully expanded error term */
  4639. Value = expand_error_value(c_p, c_p->freason, Value);
  4640. /* Save final error term and stabilize the exception flags so no
  4641. further expansion is done. */
  4642. c_p->fvalue = Value;
  4643. c_p->freason = PRIMARY_EXCEPTION(c_p->freason);
  4644. /* Find a handler or die */
  4645. if ((c_p->catches > 0 || IS_TRACED_FL(c_p, F_EXCEPTION_TRACE))
  4646. && !(c_p->freason & EXF_PANIC)) {
  4647. BeamInstr *new_pc;
  4648. /* The Beam handler code (catch_end or try_end) checks reg[0]
  4649. for THE_NON_VALUE to see if the previous code finished
  4650. abnormally. If so, reg[1], reg[2] and reg[3] should hold the
  4651. exception class, term and trace, respectively. (If the
  4652. handler is just a trap to native code, these registers will
  4653. be ignored.) */
  4654. reg[0] = THE_NON_VALUE;
  4655. reg[1] = exception_tag[GET_EXC_CLASS(c_p->freason)];
  4656. reg[2] = Value;
  4657. reg[3] = c_p->ftrace;
  4658. if ((new_pc = next_catch(c_p, reg))) {
  4659. c_p->cp = 0; /* To avoid keeping stale references. */
  4660. return new_pc;
  4661. }
  4662. if (c_p->catches > 0) erl_exit(1, "Catch not found");
  4663. }
  4664. ERTS_SMP_UNREQ_PROC_MAIN_LOCK(c_p);
  4665. terminate_proc(c_p, Value);
  4666. ERTS_SMP_REQ_PROC_MAIN_LOCK(c_p);
  4667. return NULL;
  4668. }
  4669. /*
  4670. * Find the nearest catch handler
  4671. */
  4672. static BeamInstr*
  4673. next_catch(Process* c_p, Eterm *reg) {
  4674. int active_catches = c_p->catches > 0;
  4675. int have_return_to_trace = 0;
  4676. Eterm *ptr, *prev, *return_to_trace_ptr = NULL;
  4677. BeamInstr i_return_trace = beam_return_trace[0];
  4678. BeamInstr i_return_to_trace = beam_return_to_trace[0];
  4679. BeamInstr i_return_time_trace = beam_return_time_trace[0];
  4680. ptr = prev = c_p->stop;
  4681. ASSERT(is_CP(*ptr));
  4682. ASSERT(ptr <= STACK_START(c_p));
  4683. if (ptr == STACK_START(c_p)) return NULL;
  4684. if ((is_not_CP(*ptr) || (*cp_val(*ptr) != i_return_trace &&
  4685. *cp_val(*ptr) != i_return_to_trace &&
  4686. *cp_val(*ptr) != i_return_time_trace ))
  4687. && c_p->cp) {
  4688. /* Can not follow cp here - code may be unloaded */
  4689. BeamInstr *cpp = c_p->cp;
  4690. if (cpp == beam_exception_trace) {
  4691. erts_trace_exception(c_p, cp_val(ptr[0]),
  4692. reg[1], reg[2], ptr+1);
  4693. /* Skip return_trace parameters */
  4694. ptr += 2;
  4695. } else if (cpp == beam_return_trace) {
  4696. /* Skip return_trace parameters */
  4697. ptr += 2;
  4698. } else if (cpp == beam_return_time_trace) {
  4699. /* Skip return_trace parameters */
  4700. ptr += 1;
  4701. } else if (cpp == beam_return_to_trace) {
  4702. have_return_to_trace = !0; /* Record next cp */
  4703. }
  4704. }
  4705. while (ptr < STACK_START(c_p)) {
  4706. if (is_catch(*ptr)) {
  4707. if (active_catches) goto found_catch;
  4708. ptr++;
  4709. }
  4710. else if (is_CP(*ptr)) {
  4711. prev = ptr;
  4712. if (*cp_val(*prev) == i_return_trace) {
  4713. /* Skip stack frame variables */
  4714. while (++ptr, ptr < STACK_START(c_p) && is_not_CP(*ptr)) {
  4715. if (is_catch(*ptr) && active_catches) goto found_catch;
  4716. }
  4717. if (cp_val(*prev) == beam_exception_trace) {
  4718. erts_trace_exception(c_p, cp_val(ptr[0]),
  4719. reg[1], reg[2], ptr+1);
  4720. }
  4721. /* Skip return_trace parameters */
  4722. ptr += 2;
  4723. } else if (*cp_val(*prev) == i_return_to_trace) {
  4724. /* Skip stack frame variables */
  4725. while (++ptr, ptr < STACK_START(c_p) && is_not_CP(*ptr)) {
  4726. if (is_catch(*ptr) && active_catches) goto found_catch;
  4727. }
  4728. have_return_to_trace = !0; /* Record next cp */
  4729. return_to_trace_ptr = NULL;
  4730. } else if (*cp_val(*prev) == i_return_time_trace) {
  4731. /* Skip stack frame variables */
  4732. while (++ptr, ptr < STACK_START(c_p) && is_not_CP(*ptr)) {
  4733. if (is_catch(*ptr) && active_catches) goto found_catch;
  4734. }
  4735. /* Skip return_trace parameters */
  4736. ptr += 1;
  4737. } else {
  4738. if (have_return_to_trace) {
  4739. /* Record this cp as possible return_to trace cp */
  4740. have_return_to_trace = 0;
  4741. return_to_trace_ptr = ptr;
  4742. } else return_to_trace_ptr = NULL;
  4743. ptr++;
  4744. }
  4745. } else ptr++;
  4746. }
  4747. return NULL;
  4748. found_catch:
  4749. ASSERT(ptr < STACK_START(c_p));
  4750. c_p->stop = prev;
  4751. if (IS_TRACED_FL(c_p, F_TRACE_RETURN_TO) && return_to_trace_ptr) {
  4752. /* The stackframe closest to the catch contained an
  4753. * return_to_trace entry, so since the execution now
  4754. * continues after the catch, a return_to trace message
  4755. * would be appropriate.
  4756. */
  4757. erts_trace_return_to(c_p, cp_val(*return_to_trace_ptr));
  4758. }
  4759. return catch_pc(*ptr);
  4760. }
  4761. /*
  4762. * Terminating the process when an exception is not caught
  4763. */
  4764. static void
  4765. terminate_proc(Process* c_p, Eterm Value)
  4766. {
  4767. /* Add a stacktrace if this is an error. */
  4768. if (GET_EXC_CLASS(c_p->freason) == EXTAG_ERROR) {
  4769. Value = add_stacktrace(c_p, Value, c_p->ftrace);
  4770. }
  4771. /* EXF_LOG is a primary exception flag */
  4772. if (c_p->freason & EXF_LOG) {
  4773. erts_dsprintf_buf_t *dsbufp = erts_create_logger_dsbuf();
  4774. erts_dsprintf(dsbufp, "Error in process %T ", c_p->id);
  4775. if (erts_is_alive)
  4776. erts_dsprintf(dsbufp, "on node %T ", erts_this_node->sysname);
  4777. erts_dsprintf(dsbufp,"with exit value: %0.*T\n", display_items, Value);
  4778. erts_send_error_to_logger(c_p->group_leader, dsbufp);
  4779. }
  4780. /*
  4781. * If we use a shared heap, the process will be garbage-collected.
  4782. * Must zero c_p->arity to indicate that there are no live registers.
  4783. */
  4784. c_p->arity = 0;
  4785. erts_do_exit_process(c_p, Value);
  4786. }
  4787. /*
  4788. * Build and add a symbolic stack trace to the error value.
  4789. */
  4790. static Eterm
  4791. add_stacktrace(Process* c_p, Eterm Value, Eterm exc) {
  4792. Eterm Where = build_stacktrace(c_p, exc);
  4793. Eterm* hp = HAlloc(c_p, 3);
  4794. return TUPLE2(hp, Value, Where);
  4795. }
  4796. /*
  4797. * Forming the correct error value from the internal error code.
  4798. * This does not update c_p->fvalue or c_p->freason.
  4799. */
  4800. Eterm
  4801. expand_error_value(Process* c_p, Uint freason, Eterm Value) {
  4802. Eterm* hp;
  4803. Uint r;
  4804. r = GET_EXC_INDEX(freason);
  4805. ASSERT(r < NUMBER_EXIT_CODES); /* range check */
  4806. ASSERT(is_value(Value));
  4807. switch (r) {
  4808. case (GET_EXC_INDEX(EXC_PRIMARY)):
  4809. /* Primary exceptions use fvalue as it is */
  4810. break;
  4811. case (GET_EXC_INDEX(EXC_BADMATCH)):
  4812. case (GET_EXC_INDEX(EXC_CASE_CLAUSE)):
  4813. case (GET_EXC_INDEX(EXC_TRY_CLAUSE)):
  4814. case (GET_EXC_INDEX(EXC_BADFUN)):
  4815. case (GET_EXC_INDEX(EXC_BADARITY)):
  4816. /* Some common exceptions: value -> {atom, value} */
  4817. ASSERT(is_value(Value));
  4818. hp = HAlloc(c_p, 3);
  4819. Value = TUPLE2(hp, error_atom[r], Value);
  4820. break;
  4821. default:
  4822. /* Other exceptions just use an atom as descriptor */
  4823. Value = error_atom[r];
  4824. break;
  4825. }
  4826. #ifdef DEBUG
  4827. ASSERT(Value != am_internal_error);
  4828. #endif
  4829. return Value;
  4830. }
  4831. /*
  4832. * Quick-saving the stack trace in an internal form on the heap. Note
  4833. * that c_p->ftrace will point to a cons cell which holds the given args
  4834. * and the saved data (encoded as a bignum).
  4835. *
  4836. * (It would be much better to put the arglist - when it exists - in the
  4837. * error value instead of in the actual trace; e.g. '{badarg, Args}'
  4838. * instead of using 'badarg' with Args in the trace. The arglist may
  4839. * contain very large values, and right now they will be kept alive as
  4840. * long as the stack trace is live. Preferably, the stack trace should
  4841. * always be small, so that it does not matter if it is long-lived.
  4842. * However, it is probably not possible to ever change the format of
  4843. * error terms.)
  4844. */
  4845. static void
  4846. save_stacktrace(Process* c_p, BeamInstr* pc, Eterm* reg, BifFunction bf,
  4847. Eterm args) {
  4848. struct StackTrace* s;
  4849. int sz;
  4850. int depth = erts_backtrace_depth; /* max depth (never negative) */
  4851. if (depth > 0) {
  4852. /* There will always be a current function */
  4853. depth --;
  4854. }
  4855. /* Create a container for the exception data */
  4856. sz = (offsetof(struct StackTrace, trace) + sizeof(BeamInstr *)*depth
  4857. + sizeof(Eterm) - 1) / sizeof(Eterm);
  4858. s = (struct StackTrace *) HAlloc(c_p, 1 + sz);
  4859. /* The following fields are inside the bignum */
  4860. s->header = make_pos_bignum_header(sz);
  4861. s->freason = c_p->freason;
  4862. s->depth = 0;
  4863. /*
  4864. * If the failure was in a BIF other than 'error', 'exit' or
  4865. * 'throw', find the bif-table index and save the argument
  4866. * registers by consing up an arglist.
  4867. */
  4868. if (bf != NULL && bf != error_1 && bf != error_2 &&
  4869. bf != exit_1 && bf != throw_1) {
  4870. int i;
  4871. int a = 0;
  4872. for (i = 0; i < BIF_SIZE; i++) {
  4873. if (bf == bif_table[i].f || bf == bif_table[i].traced) {
  4874. Export *ep = bif_export[i];
  4875. s->current = ep->code;
  4876. a = bif_table[i].arity;
  4877. break;
  4878. }
  4879. }
  4880. if (i >= BIF_SIZE) {
  4881. /*
  4882. * The Bif does not really exist (no BIF entry). It is a
  4883. * TRAP and traps are called through apply_bif, which also
  4884. * sets c_p->current (luckily).
  4885. * OR it is a NIF called by call_nif where current is also set.
  4886. */
  4887. ASSERT(c_p->current);
  4888. s->current = c_p->current;
  4889. a = s->current[2];
  4890. }
  4891. /* Save first stack entry */
  4892. ASSERT(pc);
  4893. if (depth > 0) {
  4894. s->trace[s->depth++] = pc;
  4895. depth--;
  4896. }
  4897. /* Save second stack entry if CP is valid and different from pc */
  4898. if (depth > 0 && c_p->cp != 0 && c_p->cp != pc) {
  4899. s->trace[s->depth++] = c_p->cp;
  4900. depth--;
  4901. }
  4902. s->pc = NULL;
  4903. args = make_arglist(c_p, reg, a); /* Overwrite CAR(c_p->ftrace) */
  4904. } else {
  4905. s->current = c_p->current;
  4906. /*
  4907. * For a function_clause error, the arguments are in the beam
  4908. * registers, c_p->cp is valid, and c_p->current is set.
  4909. */
  4910. if ( (GET_EXC_INDEX(s->freason)) ==
  4911. (GET_EXC_INDEX(EXC_FUNCTION_CLAUSE)) ) {
  4912. int a;
  4913. ASSERT(s->current);
  4914. a = s->current[2];
  4915. args = make_arglist(c_p, reg, a); /* Overwrite CAR(c_p->ftrace) */
  4916. /* Save first stack entry */
  4917. ASSERT(c_p->cp);
  4918. if (depth > 0) {
  4919. s->trace[s->depth++] = c_p->cp;
  4920. depth--;
  4921. }
  4922. s->pc = NULL; /* Ignore pc */
  4923. } else {
  4924. if (depth > 0 && c_p->cp != 0 && c_p->cp != pc) {
  4925. s->trace[s->depth++] = c_p->cp;
  4926. depth--;
  4927. }
  4928. s->pc = pc;
  4929. }
  4930. }
  4931. /* Package args and stack trace */
  4932. {
  4933. Eterm *hp;
  4934. hp = HAlloc(c_p, 2);
  4935. c_p->ftrace = CONS(hp, args, make_big((Eterm *) s));
  4936. }
  4937. /* Save the actual stack trace */
  4938. if (depth > 0) {
  4939. Eterm *ptr;
  4940. BeamInstr *prev = s->depth ? s->trace[s->depth-1] : NULL;
  4941. BeamInstr i_return_trace = beam_return_trace[0];
  4942. BeamInstr i_return_to_trace = beam_return_to_trace[0];
  4943. /*
  4944. * Traverse the stack backwards and add all unique continuation
  4945. * pointers to the buffer, up to the maximum stack trace size.
  4946. *
  4947. * Skip trace stack frames.
  4948. */
  4949. ptr = c_p->stop;
  4950. if (ptr < STACK_START(c_p)
  4951. && (is_not_CP(*ptr)|| (*cp_val(*ptr) != i_return_trace &&
  4952. *cp_val(*ptr) != i_return_to_trace))
  4953. && c_p->cp) {
  4954. /* Can not follow cp here - code may be unloaded */
  4955. BeamInstr *cpp = c_p->cp;
  4956. if (cpp == beam_exception_trace || cpp == beam_return_trace) {
  4957. /* Skip return_trace parameters */
  4958. ptr += 2;
  4959. } else if (cpp == beam_return_to_trace) {
  4960. /* Skip return_to_trace parameters */
  4961. ptr += 1;
  4962. }
  4963. }
  4964. while (ptr < STACK_START(c_p) && depth > 0) {
  4965. if (is_CP(*ptr)) {
  4966. if (*cp_val(*ptr) == i_return_trace) {
  4967. /* Skip stack frame variables */
  4968. do ++ptr; while (is_not_CP(*ptr));
  4969. /* Skip return_trace parameters */
  4970. ptr += 2;
  4971. } else if (*cp_val(*ptr) == i_return_to_trace) {
  4972. /* Skip stack frame variables */
  4973. do ++ptr; while (is_not_CP(*ptr));
  4974. } else {
  4975. BeamInstr *cp = cp_val(*ptr);
  4976. if (cp != prev) {
  4977. /* Record non-duplicates only */
  4978. prev = cp;
  4979. s->trace[s->depth++] = cp;
  4980. depth--;
  4981. }
  4982. ptr++;
  4983. }
  4984. } else ptr++;
  4985. }
  4986. }
  4987. }
  4988. /*
  4989. * Getting the relevant fields from the term pointed to by ftrace
  4990. */
  4991. static struct StackTrace *get_trace_from_exc(Eterm exc) {
  4992. if (exc == NIL) {
  4993. return NULL;
  4994. } else {
  4995. ASSERT(is_list(exc));
  4996. return (struct StackTrace *) big_val(CDR(list_val(exc)));
  4997. }
  4998. }
  4999. static Eterm get_args_from_exc(Eterm exc) {
  5000. if (exc == NIL) {
  5001. return NIL;
  5002. } else {
  5003. ASSERT(is_list(exc));
  5004. return CAR(list_val(exc));
  5005. }
  5006. }
  5007. static int is_raised_exc(Eterm exc) {
  5008. if (exc == NIL) {
  5009. return 0;
  5010. } else {
  5011. ASSERT(is_list(exc));
  5012. return bignum_header_is_neg(*big_val(CDR(list_val(exc))));
  5013. }
  5014. }
  5015. /*
  5016. * Creating a list with the argument registers
  5017. */
  5018. static Eterm
  5019. make_arglist(Process* c_p, Eterm* reg, int a) {
  5020. Eterm args = NIL;
  5021. Eterm* hp = HAlloc(c_p, 2*a);
  5022. while (a > 0) {
  5023. args = CONS(hp, reg[a-1], args);
  5024. hp += 2;
  5025. a--;
  5026. }
  5027. return args;
  5028. }
  5029. /*
  5030. * Building a symbolic representation of a saved stack trace. Note that
  5031. * the exception object 'exc', unless NIL, points to a cons cell which
  5032. * holds the given args and the quick-saved data (encoded as a bignum).
  5033. *
  5034. * If the bignum is negative, the given args is a complete stacktrace.
  5035. */
  5036. Eterm
  5037. build_stacktrace(Process* c_p, Eterm exc) {
  5038. struct StackTrace* s;
  5039. Eterm args;
  5040. int depth;
  5041. BeamInstr* current;
  5042. #if HALFWORD_HEAP
  5043. BeamInstr current_buff[3];
  5044. #endif
  5045. Eterm Where = NIL;
  5046. Eterm *next_p = &Where;
  5047. if (! (s = get_trace_from_exc(exc))) {
  5048. return NIL;
  5049. }
  5050. #ifdef HIPE
  5051. if (s->freason & EXF_NATIVE) {
  5052. return hipe_build_stacktrace(c_p, s);
  5053. }
  5054. #endif
  5055. if (is_raised_exc(exc)) {
  5056. return get_args_from_exc(exc);
  5057. }
  5058. /*
  5059. * Find the current function. If the saved s->pc is null, then the
  5060. * saved s->current should already contain the proper value.
  5061. */
  5062. if (s->pc != NULL) {
  5063. current = find_function_from_pc(s->pc);
  5064. } else {
  5065. current = s->current;
  5066. }
  5067. /*
  5068. * If current is still NULL, default to the initial function
  5069. * (e.g. spawn_link(erlang, abs, [1])).
  5070. */
  5071. if (current == NULL) {
  5072. #if HALFWORD_HEAP
  5073. current = current_buff;
  5074. current[0] = (BeamInstr) c_p->initial[0];
  5075. current[1] = (BeamInstr) c_p->initial[1];
  5076. current[2] = (BeamInstr) c_p->initial[2];
  5077. #else
  5078. current = c_p->initial;
  5079. #endif
  5080. args = am_true; /* Just in case */
  5081. } else {
  5082. args = get_args_from_exc(exc);
  5083. }
  5084. depth = s->depth;
  5085. /*
  5086. * Add the {M,F,A} for the current function
  5087. * (where A is arity or [Argument]).
  5088. */
  5089. {
  5090. int i;
  5091. Eterm mfa;
  5092. Uint heap_size = 6*(depth+1);
  5093. Eterm* hp = HAlloc(c_p, heap_size);
  5094. Eterm* hp_end = hp + heap_size;
  5095. if (args != am_true) {
  5096. /* We have an arglist - use it */
  5097. mfa = TUPLE3(hp, current[0], current[1], args);
  5098. } else {
  5099. Eterm arity = make_small(current[2]);
  5100. mfa = TUPLE3(hp, current[0], current[1], arity);
  5101. }
  5102. hp += 4;
  5103. ASSERT(*next_p == NIL);
  5104. *next_p = CONS(hp, mfa, NIL);
  5105. next_p = &CDR(list_val(*next_p));
  5106. hp += 2;
  5107. /*
  5108. * Finally, we go through the saved continuation pointers.
  5109. */
  5110. for (i = 0; i < depth; i++) {
  5111. BeamInstr *fi = find_function_from_pc((BeamInstr *) s->trace[i]);
  5112. if (fi == NULL) continue;
  5113. mfa = TUPLE3(hp, fi[0], fi[1], make_small(fi[2]));
  5114. hp += 4;
  5115. ASSERT(*next_p == NIL);
  5116. *next_p = CONS(hp, mfa, NIL);
  5117. next_p = &CDR(list_val(*next_p));
  5118. hp += 2;
  5119. }
  5120. ASSERT(hp <= hp_end);
  5121. HRelease(c_p, hp_end, hp);
  5122. }
  5123. return Where;
  5124. }
  5125. static Eterm
  5126. call_error_handler(Process* p, BeamInstr* fi, Eterm* reg)
  5127. {
  5128. Eterm* hp;
  5129. Export* ep;
  5130. int arity;
  5131. Eterm args;
  5132. Uint sz;
  5133. int i;
  5134. /*
  5135. * Search for the error_handler module.
  5136. */
  5137. ep = erts_find_function(erts_proc_get_error_handler(p),
  5138. am_undefined_function, 3);
  5139. if (ep == NULL) { /* No error handler */
  5140. p->current = fi;
  5141. p->freason = EXC_UNDEF;
  5142. return 0;
  5143. }
  5144. p->i = ep->address;
  5145. /*
  5146. * Create a list with all arguments in the x registers.
  5147. */
  5148. arity = fi[2];
  5149. sz = 2 * arity;
  5150. if (HeapWordsLeft(p) < sz) {
  5151. erts_garbage_collect(p, sz, reg, arity);
  5152. }
  5153. hp = HEAP_TOP(p);
  5154. HEAP_TOP(p) += sz;
  5155. args = NIL;
  5156. for (i = arity-1; i >= 0; i--) {
  5157. args = CONS(hp, reg[i], args);
  5158. hp += 2;
  5159. }
  5160. /*
  5161. * Set up registers for call to error_handler:undefined_function/3.
  5162. */
  5163. reg[0] = fi[0];
  5164. reg[1] = fi[1];
  5165. reg[2] = args;
  5166. return 1;
  5167. }
  5168. static Eterm
  5169. call_breakpoint_handler(Process* p, BeamInstr* fi, Eterm* reg)
  5170. {
  5171. Eterm* hp;
  5172. Export* ep;
  5173. int arity;
  5174. Eterm args;
  5175. Uint sz;
  5176. int i;
  5177. /*
  5178. * Search for error handler module.
  5179. */
  5180. ep = erts_find_function(erts_proc_get_error_handler(p),
  5181. am_breakpoint, 3);
  5182. if (ep == NULL) { /* No error handler */
  5183. p->current = fi;
  5184. p->freason = EXC_UNDEF;
  5185. return 0;
  5186. }
  5187. p->i = ep->address;
  5188. /*
  5189. * Create a list with all arguments in the x registers.
  5190. */
  5191. arity = fi[2];
  5192. sz = 2 * arity;
  5193. if (HeapWordsLeft(p) < sz) {
  5194. erts_garbage_collect(p, sz, reg, arity);
  5195. }
  5196. hp = HEAP_TOP(p);
  5197. HEAP_TOP(p) += sz;
  5198. args = NIL;
  5199. for (i = arity-1; i >= 0; i--) {
  5200. args = CONS(hp, reg[i], args);
  5201. hp += 2;
  5202. }
  5203. /*
  5204. * Set up registers for call to error_handler:breakpoint/3.
  5205. */
  5206. reg[0] = fi[0];
  5207. reg[1] = fi[1];
  5208. reg[2] = args;
  5209. return 1;
  5210. }
  5211. static Export*
  5212. apply_setup_error_handler(Process* p, Eterm module, Eterm function, Uint arity, Eterm* reg)
  5213. {
  5214. Export* ep;
  5215. /*
  5216. * Find the export table index for the error handler. Return NULL if
  5217. * there is no error handler module.
  5218. */
  5219. if ((ep = erts_find_export_entry(erts_proc_get_error_handler(p),
  5220. am_undefined_function, 3)) == NULL) {
  5221. return NULL;
  5222. } else {
  5223. int i;
  5224. Uint sz = 2*arity;
  5225. Eterm* hp;
  5226. Eterm args = NIL;
  5227. /*
  5228. * Always copy args from registers to a new list; this ensures
  5229. * that we have the same behaviour whether or not this was
  5230. * called from apply or fixed_apply (any additional last
  5231. * THIS-argument will be included, assuming that arity has been
  5232. * properly adjusted).
  5233. */
  5234. if (HeapWordsLeft(p) < sz) {
  5235. erts_garbage_collect(p, sz, reg, arity);
  5236. }
  5237. hp = HEAP_TOP(p);
  5238. HEAP_TOP(p) += sz;
  5239. for (i = arity-1; i >= 0; i--) {
  5240. args = CONS(hp, reg[i], args);
  5241. hp += 2;
  5242. }
  5243. reg[0] = module;
  5244. reg[1] = function;
  5245. reg[2] = args;
  5246. }
  5247. return ep;
  5248. }
  5249. static BeamInstr*
  5250. apply(Process* p, Eterm module, Eterm function, Eterm args, Eterm* reg)
  5251. {
  5252. int arity;
  5253. Export* ep;
  5254. Eterm tmp, this;
  5255. /*
  5256. * Check the arguments which should be of the form apply(Module,
  5257. * Function, Arguments) where Function is an atom and
  5258. * Arguments is an arity long list of terms.
  5259. */
  5260. if (is_not_atom(function)) {
  5261. /*
  5262. * No need to test args here -- done below.
  5263. */
  5264. error:
  5265. p->freason = BADARG;
  5266. error2:
  5267. reg[0] = module;
  5268. reg[1] = function;
  5269. reg[2] = args;
  5270. return 0;
  5271. }
  5272. /* The module argument may be either an atom or an abstract module
  5273. * (currently implemented using tuples, but this might change).
  5274. */
  5275. this = THE_NON_VALUE;
  5276. if (is_not_atom(module)) {
  5277. Eterm* tp;
  5278. if (is_not_tuple(module)) goto error;
  5279. tp = tuple_val(module);
  5280. if (arityval(tp[0]) < 1) goto error;
  5281. this = module;
  5282. module = tp[1];
  5283. if (is_not_atom(module)) goto error;
  5284. }
  5285. /*
  5286. * Walk down the 3rd parameter of apply (the argument list) and copy
  5287. * the parameters to the x registers (reg[]). If the module argument
  5288. * was an abstract module, add 1 to the function arity and put the
  5289. * module argument in the n+1st x register as a THIS reference.
  5290. */
  5291. tmp = args;
  5292. arity = 0;
  5293. while (is_list(tmp)) {
  5294. if (arity < (MAX_REG - 1)) {
  5295. reg[arity++] = CAR(list_val(tmp));
  5296. tmp = CDR(list_val(tmp));
  5297. } else {
  5298. p->freason = SYSTEM_LIMIT;
  5299. goto error2;
  5300. }
  5301. }
  5302. if (is_not_nil(tmp)) { /* Must be well-formed list */
  5303. goto error;
  5304. }
  5305. if (this != THE_NON_VALUE) {
  5306. reg[arity++] = this;
  5307. }
  5308. /*
  5309. * Get the index into the export table, or failing that the export
  5310. * entry for the error handler.
  5311. *
  5312. * Note: All BIFs have export entries; thus, no special case is needed.
  5313. */
  5314. if ((ep = erts_find_export_entry(module, function, arity)) == NULL) {
  5315. if ((ep = apply_setup_error_handler(p, module, function, arity, reg)) == NULL) goto error;
  5316. } else if (ERTS_PROC_GET_SAVED_CALLS_BUF(p)) {
  5317. save_calls(p, ep);
  5318. }
  5319. return ep->address;
  5320. }
  5321. static BeamInstr*
  5322. fixed_apply(Process* p, Eterm* reg, Uint arity)
  5323. {
  5324. Export* ep;
  5325. Eterm module;
  5326. Eterm function;
  5327. module = reg[arity]; /* The THIS pointer already in place */
  5328. function = reg[arity+1];
  5329. if (is_not_atom(function)) {
  5330. error:
  5331. p->freason = BADARG;
  5332. reg[0] = module;
  5333. reg[1] = function;
  5334. reg[2] = NIL;
  5335. return 0;
  5336. }
  5337. /* The module argument may be either an atom or an abstract module
  5338. * (currently implemented using tuples, but this might change).
  5339. */
  5340. if (is_not_atom(module)) {
  5341. Eterm* tp;
  5342. if (is_not_tuple(module)) goto error;
  5343. tp = tuple_val(module);
  5344. if (arityval(tp[0]) < 1) goto error;
  5345. module = tp[1];
  5346. if (is_not_atom(module)) goto error;
  5347. ++arity;
  5348. }
  5349. /*
  5350. * Get the index into the export table, or failing that the export
  5351. * entry for the error handler module.
  5352. *
  5353. * Note: All BIFs have export entries; thus, no special case is needed.
  5354. */
  5355. if ((ep = erts_find_export_entry(module, function, arity)) == NULL) {
  5356. if ((ep = apply_setup_error_handler(p, module, function, arity, reg)) == NULL)
  5357. goto error;
  5358. } else if (ERTS_PROC_GET_SAVED_CALLS_BUF(p)) {
  5359. save_calls(p, ep);
  5360. }
  5361. return ep->address;
  5362. }
  5363. static int
  5364. hibernate(Process* c_p, Eterm module, Eterm function, Eterm args, Eterm* reg)
  5365. {
  5366. int arity;
  5367. Eterm tmp;
  5368. if (is_not_atom(module) || is_not_atom(function)) {
  5369. /*
  5370. * No need to test args here -- done below.
  5371. */
  5372. error:
  5373. c_p->freason = BADARG;
  5374. error2:
  5375. reg[0] = module;
  5376. reg[1] = function;
  5377. reg[2] = args;
  5378. return 0;
  5379. }
  5380. arity = 0;
  5381. tmp = args;
  5382. while (is_list(tmp)) {
  5383. if (arity < MAX_REG) {
  5384. tmp = CDR(list_val(tmp));
  5385. arity++;
  5386. } else {
  5387. c_p->freason = SYSTEM_LIMIT;
  5388. goto error2;
  5389. }
  5390. }
  5391. if (is_not_nil(tmp)) { /* Must be well-formed list */
  5392. goto error;
  5393. }
  5394. /*
  5395. * At this point, arguments are known to be good.
  5396. */
  5397. if (c_p->arg_reg != c_p->def_arg_reg) {
  5398. /* Save some memory */
  5399. erts_free(ERTS_ALC_T_ARG_REG, c_p->arg_reg);
  5400. c_p->arg_reg = c_p->def_arg_reg;
  5401. c_p->max_arg_reg = sizeof(c_p->def_arg_reg)/sizeof(c_p->def_arg_reg[0]);
  5402. }
  5403. /*
  5404. * Arrange for the process to be resumed at the given MFA with
  5405. * the stack cleared.
  5406. */
  5407. c_p->arity = 3;
  5408. c_p->arg_reg[0] = module;
  5409. c_p->arg_reg[1] = function;
  5410. c_p->arg_reg[2] = args;
  5411. c_p->stop = STACK_START(c_p);
  5412. c_p->catches = 0;
  5413. c_p->i = beam_apply;
  5414. c_p->cp = (BeamInstr *) beam_apply+1;
  5415. /*
  5416. * If there are no waiting messages, garbage collect and
  5417. * shrink the heap.
  5418. */
  5419. erts_smp_proc_lock(c_p, ERTS_PROC_LOCK_MSGQ|ERTS_PROC_LOCK_STATUS);
  5420. ERTS_SMP_MSGQ_MV_INQ2PRIVQ(c_p);
  5421. if (c_p->msg.len > 0) {
  5422. erts_add_to_runq(c_p);
  5423. } else {
  5424. erts_smp_proc_unlock(c_p, ERTS_PROC_LOCK_MSGQ|ERTS_PROC_LOCK_STATUS);
  5425. c_p->fvalue = NIL;
  5426. PROCESS_MAIN_CHK_LOCKS(c_p);
  5427. erts_garbage_collect_hibernate(c_p);
  5428. PROCESS_MAIN_CHK_LOCKS(c_p);
  5429. erts_smp_proc_lock(c_p, ERTS_PROC_LOCK_MSGQ|ERTS_PROC_LOCK_STATUS);
  5430. ASSERT(!ERTS_PROC_IS_EXITING(c_p));
  5431. c_p->status = P_WAITING;
  5432. #ifdef ERTS_SMP
  5433. ERTS_SMP_MSGQ_MV_INQ2PRIVQ(c_p);
  5434. if (c_p->msg.len > 0)
  5435. erts_add_to_runq(c_p);
  5436. #endif
  5437. }
  5438. erts_smp_proc_unlock(c_p, ERTS_PROC_LOCK_MSGQ|ERTS_PROC_LOCK_STATUS);
  5439. c_p->current = bif_export[BIF_hibernate_3]->code;
  5440. return 1;
  5441. }
  5442. static BeamInstr*
  5443. call_fun(Process* p, /* Current process. */
  5444. int arity, /* Number of arguments for Fun. */
  5445. Eterm* reg, /* Contents of registers. */
  5446. Eterm args) /* THE_NON_VALUE or pre-built list of arguments. */
  5447. {
  5448. Eterm fun = reg[arity];
  5449. Eterm hdr;
  5450. int i;
  5451. Eterm function;
  5452. Eterm* hp;
  5453. if (!is_boxed(fun)) {
  5454. goto badfun;
  5455. }
  5456. hdr = *boxed_val(fun);
  5457. if (is_fun_header(hdr)) {
  5458. ErlFunThing* funp = (ErlFunThing *) fun_val(fun);
  5459. ErlFunEntry* fe;
  5460. BeamInstr* code_ptr;
  5461. Eterm* var_ptr;
  5462. int actual_arity;
  5463. unsigned num_free;
  5464. fe = funp->fe;
  5465. num_free = funp->num_free;
  5466. code_ptr = fe->address;
  5467. actual_arity = (int) code_ptr[-1];
  5468. if (actual_arity == arity+num_free) {
  5469. if (num_free == 0) {
  5470. return code_ptr;
  5471. } else {
  5472. var_ptr = funp->env;
  5473. reg += arity;
  5474. i = 0;
  5475. do {
  5476. reg[i] = var_ptr[i];
  5477. i++;
  5478. } while (i < num_free);
  5479. reg[i] = fun;
  5480. return code_ptr;
  5481. }
  5482. return code_ptr;
  5483. } else {
  5484. /*
  5485. * Something wrong here. First build a list of the arguments.
  5486. */
  5487. if (is_non_value(args)) {
  5488. Uint sz = 2 * arity;
  5489. args = NIL;
  5490. if (HeapWordsLeft(p) < sz) {
  5491. erts_garbage_collect(p, sz, reg, arity+1);
  5492. fun = reg[arity];
  5493. }
  5494. hp = HEAP_TOP(p);
  5495. HEAP_TOP(p) += sz;
  5496. for (i = arity-1; i >= 0; i--) {
  5497. args = CONS(hp, reg[i], args);
  5498. hp += 2;
  5499. }
  5500. }
  5501. if (actual_arity >= 0) {
  5502. /*
  5503. * There is a fun defined, but the call has the wrong arity.
  5504. */
  5505. hp = HAlloc(p, 3);
  5506. p->freason = EXC_BADARITY;
  5507. p->fvalue = TUPLE2(hp, fun, args);
  5508. return NULL;
  5509. } else {
  5510. Export* ep;
  5511. Module* modp;
  5512. Eterm module;
  5513. /*
  5514. * No arity. There is no module loaded that defines the fun,
  5515. * either because the fun is newly created from the external
  5516. * representation (the module has never been loaded),
  5517. * or the module defining the fun has been unloaded.
  5518. */
  5519. module = fe->module;
  5520. if ((modp = erts_get_module(module)) != NULL && modp->code != NULL) {
  5521. /*
  5522. * There is a module loaded, but obviously the fun is not
  5523. * defined in it. We must not call the error_handler
  5524. * (or we will get into an infinite loop).
  5525. */
  5526. goto badfun;
  5527. }
  5528. /*
  5529. * No current code for this module. Call the error_handler module
  5530. * to attempt loading the module.
  5531. */
  5532. ep = erts_find_function(erts_proc_get_error_handler(p),
  5533. am_undefined_lambda, 3);
  5534. if (ep == NULL) { /* No error handler */
  5535. p->current = NULL;
  5536. p->freason = EXC_UNDEF;
  5537. return NULL;
  5538. }
  5539. reg[0] = module;
  5540. reg[1] = fun;
  5541. reg[2] = args;
  5542. return ep->address;
  5543. }
  5544. }
  5545. } else if (is_export_header(hdr)) {
  5546. Export *ep;
  5547. int actual_arity;
  5548. ep = *((Export **) (export_val(fun) + 1));
  5549. actual_arity = (int) ep->code[2];
  5550. if (arity == actual_arity) {
  5551. return ep->address;
  5552. } else {
  5553. /*
  5554. * Wrong arity. First build a list of the arguments.
  5555. */
  5556. if (is_non_value(args)) {
  5557. args = NIL;
  5558. hp = HAlloc(p, arity*2);
  5559. for (i = arity-1; i >= 0; i--) {
  5560. args = CONS(hp, reg[i], args);
  5561. hp += 2;
  5562. }
  5563. }
  5564. hp = HAlloc(p, 3);
  5565. p->freason = EXC_BADARITY;
  5566. p->fvalue = TUPLE2(hp, fun, args);
  5567. return NULL;
  5568. }
  5569. } else if (hdr == make_arityval(2)) {
  5570. Eterm* tp;
  5571. Export* ep;
  5572. Eterm module;
  5573. tp = tuple_val(fun);
  5574. module = tp[1];
  5575. function = tp[2];
  5576. if (!is_atom(module) || !is_atom(function)) {
  5577. goto badfun;
  5578. }
  5579. if ((ep = erts_find_export_entry(module, function, arity)) == NULL) {
  5580. ep = erts_find_export_entry(erts_proc_get_error_handler(p),
  5581. am_undefined_function, 3);
  5582. if (ep == NULL) {
  5583. p->freason = EXC_UNDEF;
  5584. return 0;
  5585. }
  5586. if (is_non_value(args)) {
  5587. Uint sz = 2 * arity;
  5588. if (HeapWordsLeft(p) < sz) {
  5589. erts_garbage_collect(p, sz, reg, arity);
  5590. }
  5591. hp = HEAP_TOP(p);
  5592. HEAP_TOP(p) += sz;
  5593. args = NIL;
  5594. while (arity-- > 0) {
  5595. args = CONS(hp, reg[arity], args);
  5596. hp += 2;
  5597. }
  5598. }
  5599. reg[0] = module;
  5600. reg[1] = function;
  5601. reg[2] = args;
  5602. }
  5603. return ep->address;
  5604. } else {
  5605. badfun:
  5606. p->current = NULL;
  5607. p->freason = EXC_BADFUN;
  5608. p->fvalue = fun;
  5609. return NULL;
  5610. }
  5611. }
  5612. static BeamInstr*
  5613. apply_fun(Process* p, Eterm fun, Eterm args, Eterm* reg)
  5614. {
  5615. int arity;
  5616. Eterm tmp;
  5617. /*
  5618. * Walk down the 3rd parameter of apply (the argument list) and copy
  5619. * the parameters to the x registers (reg[]).
  5620. */
  5621. tmp = args;
  5622. arity = 0;
  5623. while (is_list(tmp)) {
  5624. if (arity < MAX_REG-1) {
  5625. reg[arity++] = CAR(list_val(tmp));
  5626. tmp = CDR(list_val(tmp));
  5627. } else {
  5628. p->freason = SYSTEM_LIMIT;
  5629. return NULL;
  5630. }
  5631. }
  5632. if (is_not_nil(tmp)) { /* Must be well-formed list */
  5633. p->freason = EXC_UNDEF;
  5634. return NULL;
  5635. }
  5636. reg[arity] = fun;
  5637. return call_fun(p, arity, reg, args);
  5638. }
  5639. static Eterm
  5640. new_fun(Process* p, Eterm* reg, ErlFunEntry* fe, int num_free)
  5641. {
  5642. unsigned needed = ERL_FUN_SIZE + num_free;
  5643. ErlFunThing* funp;
  5644. Eterm* hp;
  5645. int i;
  5646. if (HEAP_LIMIT(p) - HEAP_TOP(p) <= needed) {
  5647. PROCESS_MAIN_CHK_LOCKS(p);
  5648. erts_garbage_collect(p, needed, reg, num_free);
  5649. PROCESS_MAIN_CHK_LOCKS(p);
  5650. }
  5651. hp = p->htop;
  5652. p->htop = hp + needed;
  5653. funp = (ErlFunThing *) hp;
  5654. hp = funp->env;
  5655. erts_refc_inc(&fe->refc, 2);
  5656. funp->thing_word = HEADER_FUN;
  5657. #ifndef HYBRID /* FIND ME! */
  5658. funp->next = MSO(p).funs;
  5659. MSO(p).funs = funp;
  5660. #endif
  5661. funp->fe = fe;
  5662. funp->num_free = num_free;
  5663. funp->creator = p->id;
  5664. #ifdef HIPE
  5665. funp->native_address = fe->native_address;
  5666. #endif
  5667. funp->arity = (int)fe->address[-1] - num_free;
  5668. for (i = 0; i < num_free; i++) {
  5669. *hp++ = reg[i];
  5670. }
  5671. return make_fun(funp);
  5672. }
  5673. int catchlevel(Process *p)
  5674. {
  5675. return p->catches;
  5676. }
  5677. /*
  5678. * Check if the given function is built-in (i.e. a BIF implemented in C).
  5679. *
  5680. * Returns 0 if not built-in, and a non-zero value if built-in.
  5681. */
  5682. int
  5683. erts_is_builtin(Eterm Mod, Eterm Name, int arity)
  5684. {
  5685. Export e;
  5686. Export* ep;
  5687. e.code[0] = Mod;
  5688. e.code[1] = Name;
  5689. e.code[2] = arity;
  5690. if ((ep = export_get(&e)) == NULL) {
  5691. return 0;
  5692. }
  5693. return ep->address == ep->code+3 && (ep->code[3] == (BeamInstr) em_apply_bif);
  5694. }
  5695. /*
  5696. * Return the current number of reductions for the given process.
  5697. * To get the total number of reductions, p->reds must be added.
  5698. */
  5699. Uint
  5700. erts_current_reductions(Process *current, Process *p)
  5701. {
  5702. if (current != p) {
  5703. return 0;
  5704. } else if (current->fcalls < 0 && ERTS_PROC_GET_SAVED_CALLS_BUF(current)) {
  5705. return -current->fcalls;
  5706. } else {
  5707. return REDS_IN(current) - current->fcalls;
  5708. }
  5709. }