PageRenderTime 81ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/erts/emulator/beam/beam_emu.c

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