1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
// -*- c -*-
//
// %CopyrightBegin%
//
// Copyright Ericsson AB 2017-2018. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// %CopyrightEnd%
//
// ================================================================
// All guards with zero arguments have special instructions,
// for example:
//
// self/0
// node/0
//
// All other guard BIFs take one or two arguments.
// ================================================================
CALL_GUARD_BIF(BF, TmpReg, Dst) {
Eterm result;
#ifdef DEBUG
Eterm* orig_htop = HTOP;
Eterm* orig_stop = E;
#endif
ERTS_DBG_CHK_REDS(c_p, FCALLS);
c_p->fcalls = FCALLS;
PROCESS_MAIN_CHK_LOCKS(c_p);
ASSERT(!ERTS_PROC_IS_EXITING(c_p));
ERTS_CHK_MBUF_SZ(c_p);
DEBUG_SWAPOUT;
result = (*$BF)(c_p, $TmpReg, I);
DEBUG_SWAPIN;
ASSERT(orig_htop == HTOP && orig_stop == E);
ERTS_CHK_MBUF_SZ(c_p);
ASSERT(!ERTS_PROC_IS_EXITING(c_p) || is_non_value(result));
ERTS_VERIFY_UNUSED_TEMP_ALLOC(c_p);
PROCESS_MAIN_CHK_LOCKS(c_p);
ERTS_HOLE_CHECK(c_p);
FCALLS = c_p->fcalls;
ERTS_DBG_CHK_REDS(c_p, FCALLS);
if (ERTS_LIKELY(is_value(result))) {
$Dst = result;
$NEXT0();
}
}
// Guard BIF in head. On failure, ignore the error and jump
// to the code for the next clause. We don't support tracing
// of guard BIFs.
i_bif1 := i_bif.fetch0.call;
i_bif2 := i_bif.fetch1.fetch0.call;
i_bif3 := i_bif.fetch2.fetch1.fetch0.call;
i_bif.head() {
ErtsBifFunc bf;
Eterm tmp_reg[3];
}
i_bif.fetch0(Src) {
tmp_reg[0] = $Src;
}
i_bif.fetch1(Src) {
tmp_reg[1] = $Src;
}
i_bif.fetch2(Src) {
tmp_reg[2] = $Src;
}
i_bif.call(Fail, Bif, Dst) {
bf = (BifFunction) $Bif;
$CALL_GUARD_BIF(bf, tmp_reg, $Dst);
$FAIL($Fail);
}
//
// Guard BIF in body. It can fail like any BIF. No trace support.
//
i_bif1_body := i_bif_body.fetch0.call;
i_bif2_body := i_bif_body.fetch1.fetch0.call;
i_bif3_body := i_bif_body.fetch2.fetch1.fetch0.call;
i_bif_body.head() {
ErtsBifFunc bf;
Eterm tmp_reg[3];
}
i_bif_body.fetch0(Src) {
tmp_reg[0] = $Src;
}
i_bif_body.fetch1(Src) {
tmp_reg[1] = $Src;
}
i_bif_body.fetch2(Src) {
tmp_reg[2] = $Src;
}
i_bif_body.call(Bif, Dst) {
bf = (BifFunction) $Bif;
$CALL_GUARD_BIF(bf, tmp_reg, $Dst);
reg[0] = tmp_reg[0];
reg[1] = tmp_reg[1];
reg[2] = tmp_reg[2];
SWAPOUT;
I = handle_error(c_p, I, reg, ubif2mfa((void *) bf));
goto post_error_handling;
}
//
// length/1 is the only guard BIF that does not execute in constant
// time. Here follows special instructions to allow the calculation of
// the list length to be broken in several chunks to avoid hogging
// the scheduler for a long time.
//
i_length_setup(Live, Src) {
Uint live = $Live;
Eterm src = $Src;
reg[live] = src;
reg[live+1] = make_small(0);
reg[live+2] = src;
/* This instruction is always followed by i_length */
SET_I($NEXT_INSTRUCTION);
goto i_length_start__;
//| -no_next
}
//
// This instruction can be executed one or more times. When entering
// this instruction, the X registers have the following contents:
//
// reg[live+0] The remainder of the list.
// reg[live+1] The length so far (tagged integer).
// reg[live+2] The original list. Only used if an error is generated
// (if the final tail of the list is not []).
//
i_length := i_length.start.execute;
i_length.start() {
i_length_start__:
;
}
i_length.execute(Fail, Live, Dst) {
Eterm result;
Uint live;
ERTS_DBG_CHK_REDS(c_p, FCALLS);
c_p->fcalls = FCALLS;
PROCESS_MAIN_CHK_LOCKS(c_p);
ASSERT(!ERTS_PROC_IS_EXITING(c_p));
ERTS_CHK_MBUF_SZ(c_p);
DEBUG_SWAPOUT;
live = $Live;
result = erts_trapping_length_1(c_p, reg+live);
DEBUG_SWAPIN;
ERTS_CHK_MBUF_SZ(c_p);
ASSERT(!ERTS_PROC_IS_EXITING(c_p) || is_non_value(result));
ERTS_VERIFY_UNUSED_TEMP_ALLOC(c_p);
PROCESS_MAIN_CHK_LOCKS(c_p);
ERTS_HOLE_CHECK(c_p);
FCALLS = c_p->fcalls;
ERTS_DBG_CHK_REDS(c_p, FCALLS);
if (ERTS_LIKELY(is_value(result))) {
/* Successful calculation of the list length. */
$REFRESH_GEN_DEST();
$Dst = result;
$NEXT0();
} else if (c_p->freason == TRAP) {
/*
* Good so far, but there is more work to do. Yield.
*/
$SET_CP_I_ABS(I);
SWAPOUT;
c_p->arity = live + 3;
c_p->current = NULL;
goto context_switch3;
} else {
/* Error. */
$BIF_ERROR_ARITY_1($Fail, BIF_length_1, reg[live+2]);
}
//| -no_next
}
//
// The most general BIF call. The BIF may build any amount of data
// on the heap. The result is always returned in r(0).
//
call_bif(Exp) {
ErtsBifFunc bf;
Eterm result;
ErlHeapFragment *live_hf_end;
Export *export = (Export*) $Exp;
if (!((FCALLS - 1) > 0 || (FCALLS-1) > neg_o_reds)) {
/* If we have run out of reductions, we do a context
switch before calling the bif */
c_p->arity = GET_BIF_ARITY(export);
c_p->current = &export->info.mfa;
goto context_switch3;
}
ERTS_MSACC_SET_BIF_STATE_CACHED_X(GET_BIF_MODULE(export),
GET_BIF_ADDRESS(export));
bf = GET_BIF_ADDRESS(export);
PRE_BIF_SWAPOUT(c_p);
ERTS_DBG_CHK_REDS(c_p, FCALLS);
c_p->fcalls = FCALLS - 1;
if (FCALLS <= 0) {
save_calls(c_p, export);
}
ASSERT(!ERTS_PROC_IS_EXITING(c_p));
ERTS_VERIFY_UNUSED_TEMP_ALLOC(c_p);
live_hf_end = c_p->mbuf;
ERTS_CHK_MBUF_SZ(c_p);
result = (*bf)(c_p, reg, I);
ERTS_CHK_MBUF_SZ(c_p);
ASSERT(!ERTS_PROC_IS_EXITING(c_p) || is_non_value(result));
ERTS_VERIFY_UNUSED_TEMP_ALLOC(c_p);
ERTS_HOLE_CHECK(c_p);
ERTS_REQ_PROC_MAIN_LOCK(c_p);
if (ERTS_IS_GC_DESIRED(c_p)) {
Uint arity = GET_BIF_ARITY(export);
result = erts_gc_after_bif_call_lhf(c_p, live_hf_end, result,
reg, arity);
E = c_p->stop;
}
PROCESS_MAIN_CHK_LOCKS(c_p);
HTOP = HEAP_TOP(c_p);
FCALLS = c_p->fcalls;
ERTS_DBG_CHK_REDS(c_p, FCALLS);
/* We have to update the cache if we are enabled in order
to make sure no book keeping is done after we disabled
msacc. We don't always do this as it is quite expensive. */
if (ERTS_MSACC_IS_ENABLED_CACHED_X()) {
ERTS_MSACC_UPDATE_CACHE_X();
}
ERTS_MSACC_SET_STATE_CACHED_M_X(ERTS_MSACC_STATE_EMULATOR);
if (ERTS_LIKELY(is_value(result))) {
r(0) = result;
CHECK_TERM(r(0));
$NEXT0();
} else if (c_p->freason == TRAP) {
SET_CP(c_p, $NEXT_INSTRUCTION);
SET_I(c_p->i);
SWAPIN;
Dispatch();
}
/*
* Error handling. SWAPOUT is not needed because it was done above.
*/
ASSERT(c_p->stop == E);
I = handle_error(c_p, I, reg, &export->info.mfa);
goto post_error_handling;
}
//
// Send is almost a standard call-BIF with two arguments, except for:
// 1. It cannot be traced.
// 2. There is no pointer to the send_2 function stored in
// the instruction.
//
send() {
Eterm result;
if (!(FCALLS > 0 || FCALLS > neg_o_reds)) {
/* If we have run out of reductions, we do a context
switch before calling the bif */
c_p->arity = 2;
c_p->current = NULL;
goto context_switch3;
}
PRE_BIF_SWAPOUT(c_p);
c_p->fcalls = FCALLS - 1;
result = erl_send(c_p, r(0), x(1));
ERTS_VERIFY_UNUSED_TEMP_ALLOC(c_p);
ERTS_REQ_PROC_MAIN_LOCK(c_p);
PROCESS_MAIN_CHK_LOCKS(c_p);
HTOP = HEAP_TOP(c_p);
FCALLS = c_p->fcalls;
if (ERTS_LIKELY(is_value(result))) {
r(0) = result;
CHECK_TERM(r(0));
} else if (c_p->freason == TRAP) {
SET_CP(c_p, $NEXT_INSTRUCTION);
SET_I(c_p->i);
SWAPIN;
Dispatch();
} else {
goto find_func_info;
}
}
call_nif := nif_bif.call_nif.epilogue;
apply_bif := nif_bif.apply_bif.epilogue;
nif_bif.head() {
Eterm nif_bif_result;
Eterm bif_nif_arity;
BifFunction vbf;
ErlHeapFragment *live_hf_end;
ErtsCodeMFA *codemfa;
}
nif_bif.call_nif() {
/*
* call_nif is always first instruction in function:
*
* I[-3]: Module
* I[-2]: Function
* I[-1]: Arity
* I[0]: &&call_nif
* I[1]: Function pointer to NIF function
* I[2]: Pointer to erl_module_nif
* I[3]: Function pointer to dirty NIF
*
* This layout is determined by the NifExport struct
*/
ERTS_MSACC_SET_STATE_CACHED_M_X(ERTS_MSACC_STATE_NIF);
codemfa = erts_code_to_codemfa(I);
c_p->current = codemfa; /* current and vbf set to please handle_error */
DTRACE_NIF_ENTRY(c_p, codemfa);
HEAVY_SWAPOUT;
PROCESS_MAIN_CHK_LOCKS(c_p);
bif_nif_arity = codemfa->arity;
ERTS_UNREQ_PROC_MAIN_LOCK(c_p);
ASSERT(!ERTS_PROC_IS_EXITING(c_p));
{
typedef Eterm NifF(struct enif_environment_t*, int argc, Eterm argv[]);
NifF* fp = vbf = (NifF*) I[1];
struct enif_environment_t env;
ASSERT(c_p->scheduler_data);
live_hf_end = c_p->mbuf;
ERTS_CHK_MBUF_SZ(c_p);
erts_pre_nif(&env, c_p, (struct erl_module_nif*)I[2], NULL);
ASSERT((c_p->scheduler_data)->current_nif == NULL);
(c_p->scheduler_data)->current_nif = &env;
nif_bif_result = (*fp)(&env, bif_nif_arity, reg);
if (env.exception_thrown)
nif_bif_result = THE_NON_VALUE;
ASSERT((c_p->scheduler_data)->current_nif == &env);
(c_p->scheduler_data)->current_nif = NULL;
erts_post_nif(&env);
ERTS_CHK_MBUF_SZ(c_p);
PROCESS_MAIN_CHK_LOCKS(c_p);
ERTS_VERIFY_UNUSED_TEMP_ALLOC(c_p);
ERTS_MSACC_SET_STATE_CACHED_M_X(ERTS_MSACC_STATE_EMULATOR);
ASSERT(!env.exiting);
ASSERT(!ERTS_PROC_IS_EXITING(c_p));
}
DTRACE_NIF_RETURN(c_p, codemfa);
}
nif_bif.apply_bif() {
/*
* At this point, I points to the code[0] in the export entry for
* the BIF:
*
* code[-3]: Module
* code[-2]: Function
* code[-1]: Arity
* code[0]: &&apply_bif
* code[1]: Function pointer to BIF function
*/
if (!((FCALLS - 1) > 0 || (FCALLS - 1) > neg_o_reds)) {
/* If we have run out of reductions, we do a context
switch before calling the bif */
goto context_switch;
}
codemfa = erts_code_to_codemfa(I);
ERTS_MSACC_SET_BIF_STATE_CACHED_X(codemfa->module, (BifFunction)Arg(0));
/* In case we apply process_info/1,2 or load_nif/1 */
c_p->current = codemfa;
$SET_CP_I_ABS(I); /* In case we apply check_process_code/2. */
c_p->arity = 0; /* To allow garbage collection on ourselves
* (check_process_code/2).
*/
DTRACE_BIF_ENTRY(c_p, codemfa);
SWAPOUT;
ERTS_DBG_CHK_REDS(c_p, FCALLS - 1);
c_p->fcalls = FCALLS - 1;
vbf = (BifFunction) Arg(0);
PROCESS_MAIN_CHK_LOCKS(c_p);
bif_nif_arity = codemfa->arity;
ASSERT(bif_nif_arity <= 4);
ERTS_UNREQ_PROC_MAIN_LOCK(c_p);
ERTS_VERIFY_UNUSED_TEMP_ALLOC(c_p);
{
ErtsBifFunc bf = vbf;
ASSERT(!ERTS_PROC_IS_EXITING(c_p));
live_hf_end = c_p->mbuf;
ERTS_CHK_MBUF_SZ(c_p);
nif_bif_result = (*bf)(c_p, reg, I);
ERTS_CHK_MBUF_SZ(c_p);
ASSERT(!ERTS_PROC_IS_EXITING(c_p) ||
is_non_value(nif_bif_result));
ERTS_VERIFY_UNUSED_TEMP_ALLOC(c_p);
PROCESS_MAIN_CHK_LOCKS(c_p);
}
/* We have to update the cache if we are enabled in order
to make sure no book keeping is done after we disabled
msacc. We don't always do this as it is quite expensive. */
if (ERTS_MSACC_IS_ENABLED_CACHED_X())
ERTS_MSACC_UPDATE_CACHE_X();
ERTS_MSACC_SET_STATE_CACHED_M_X(ERTS_MSACC_STATE_EMULATOR);
DTRACE_BIF_RETURN(c_p, codemfa);
}
nif_bif.epilogue() {
ERTS_REQ_PROC_MAIN_LOCK(c_p);
ERTS_HOLE_CHECK(c_p);
if (ERTS_IS_GC_DESIRED(c_p)) {
nif_bif_result = erts_gc_after_bif_call_lhf(c_p, live_hf_end,
nif_bif_result,
reg, bif_nif_arity);
}
SWAPIN; /* There might have been a garbage collection. */
FCALLS = c_p->fcalls;
ERTS_DBG_CHK_REDS(c_p, FCALLS);
if (ERTS_LIKELY(is_value(nif_bif_result))) {
r(0) = nif_bif_result;
CHECK_TERM(r(0));
SET_I(c_p->cp);
c_p->cp = 0;
Goto(*I);
} else if (c_p->freason == TRAP) {
SET_I(c_p->i);
if (c_p->flags & F_HIBERNATE_SCHED) {
c_p->flags &= ~F_HIBERNATE_SCHED;
goto do_schedule;
}
Dispatch();
}
I = handle_error(c_p, c_p->cp, reg, c_p->current);
goto post_error_handling;
}
|