PageRenderTime 222ms CodeModel.GetById 28ms RepoModel.GetById 2ms app.codeStats 1ms

/erts/emulator/beam/beam_emu.c

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