Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the code for bit syntax construction #8697

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 34 additions & 22 deletions erts/emulator/beam/emu/bs_instrs.tab
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ i_bs_create_bin(Fail, Alloc, Live, Dst, N) {
const BeamInstr* p;
Uint alloc = $Alloc;
Eterm new_binary;
ERL_BITS_DECLARE_STATEP; /* Has to be last declaration */
ErlBitsState* EBS = ERL_BITS_EBS_FROM_REG(reg);

/* We count the total number of bits in an unsigned integer. To avoid
* having to check for overflow when adding to `num_bits`, we ensure that
Expand Down Expand Up @@ -546,7 +546,6 @@ i_bs_create_bin(Fail, Alloc, Live, Dst, N) {
}

/* Allocate binary. */
ERL_BITS_RELOAD_STATEP(c_p);
p = p_start;
if (p[0] == BSC_APPEND) {
Uint live = $Live;
Expand All @@ -565,15 +564,13 @@ i_bs_create_bin(Fail, Alloc, Live, Dst, N) {
}
p_start += BSC_NUM_ARGS;
} else if (p[0] == BSC_PRIVATE_APPEND) {
Uint unit;
Eterm Src;

$test_heap(alloc, $Live);

$BS_LOAD_UNIT(p, unit);
$BS_LOAD_SRC(p, Src);

new_binary = erts_bs_private_append_checked(c_p, Src, num_bits, unit);
new_binary = erts_bs_private_append_checked(EBS, c_p, Src, num_bits);

if (is_non_value(new_binary)) {
$BS_FAIL_INFO($Fail, c_p->freason, c_p->fvalue, Src);
Expand All @@ -589,7 +586,7 @@ i_bs_create_bin(Fail, Alloc, Live, Dst, N) {
/* num_bits = Number of bits to build
* alloc = Total number of words to allocate on heap
*/
erts_bin_offset = 0;
EBS->erts_bin_offset = 0;
if (num_bits <= ERL_ONHEAP_BITS_LIMIT) {
ErlHeapBits *hb;

Expand All @@ -598,7 +595,7 @@ i_bs_create_bin(Fail, Alloc, Live, Dst, N) {
HTOP += heap_bits_size(num_bits);
hb->thing_word = header_heap_bits(num_bits);
hb->size = num_bits;
erts_current_bin = (byte *) hb->data;
EBS->erts_current_bin = (byte *) hb->data;
new_binary = make_bitstring(hb);
} else {
Binary* bptr;
Expand All @@ -608,15 +605,15 @@ i_bs_create_bin(Fail, Alloc, Live, Dst, N) {
$Live);

bptr = erts_bin_nrml_alloc(NBYTES(num_bits));
erts_current_bin = (byte *)bptr->orig_bytes;
EBS->erts_current_bin = (byte *)bptr->orig_bytes;

LIGHT_SWAPOUT;

new_binary = erts_wrap_refc_bitstring(&MSO(c_p).first,
&MSO(c_p).overhead,
&HEAP_TOP(c_p),
bptr,
erts_current_bin,
EBS->erts_current_bin,
0,
num_bits);

Expand All @@ -640,7 +637,7 @@ i_bs_create_bin(Fail, Alloc, Live, Dst, N) {
byte* string;
$BS_LOAD_STRING_SRC(p, string);
$BS_LOAD_FIXED_SIZE(p, Size);
erts_new_bs_put_string(ERL_BITS_ARGS_2(string, Size));
erts_bs_put_string(EBS, string, Size);
continue;
}

Expand All @@ -649,7 +646,7 @@ i_bs_create_bin(Fail, Alloc, Live, Dst, N) {
switch (p[0]) {
case BSC_BINARY_ALL:
$BS_LOAD_UNIT(p, unit);
if (!erts_new_bs_put_binary_all(c_p, Src, unit)) {
if (!erts_bs_put_binary_all(EBS, c_p, Src, unit)) {
$BS_FAIL_INFO($Fail, BADARG, am_unit, Src);
}
break;
Expand All @@ -658,14 +655,14 @@ i_bs_create_bin(Fail, Alloc, Live, Dst, N) {
$BS_LOAD_FLAGS(p, flags);
$BS_LOAD_SIZE(p, Size);
$BS_GET_UNCHECKED_FIELD_SIZE(Size, unit, $BADARG($Fail), _size);
if (!erts_new_bs_put_binary(c_p, Src, _size)) {
if (!erts_bs_put_binary(EBS, c_p, Src, _size)) {
Eterm reason = is_bitstring(Src) ? am_short : am_type;
$BS_FAIL_INFO($Fail, BADARG, reason, Src);
}
break;
case BSC_BINARY_FIXED_SIZE:
$BS_LOAD_FIXED_SIZE(p, Size);
if (!erts_new_bs_put_binary(c_p, Src, Size)) {
if (!erts_bs_put_binary(EBS, c_p, Src, Size)) {
Eterm reason = is_bitstring(Src) ? am_short : am_type;
$BS_FAIL_INFO($Fail, BADARG, reason, Src);
}
Expand All @@ -675,49 +672,64 @@ i_bs_create_bin(Fail, Alloc, Live, Dst, N) {
$BS_LOAD_FLAGS(p, flags);
$BS_LOAD_SIZE(p, Size);
$BS_GET_UNCHECKED_FIELD_SIZE(Size, unit, $BADARG($Fail), _size);
Src = erts_new_bs_put_float(c_p, Src, _size, flags);
Src = erts_bs_put_float(EBS, c_p, Src, _size, flags);
if (is_value(Src)) {
$BS_FAIL_INFO($Fail, BADARG, c_p->fvalue, Src);
}
break;
case BSC_FLOAT_FIXED_SIZE:
$BS_LOAD_FLAGS(p, flags);
$BS_LOAD_FIXED_SIZE(p, Size);
Src = erts_new_bs_put_float(c_p, Src, Size, flags);
Src = erts_bs_put_float(EBS, c_p, Src, Size, flags);
if (is_value(Src)) {
$BS_FAIL_INFO($Fail, BADARG, c_p->fvalue, Src);
}
break;
case BSC_INTEGER:
{
Sint _size;
int result;

$BS_LOAD_UNIT(p, unit);
$BS_LOAD_FLAGS(p, flags);
$BS_LOAD_SIZE(p, Size);
$BS_GET_UNCHECKED_FIELD_SIZE(Size, unit, $BADARG($Fail), _size);
if (!erts_new_bs_put_integer(ERL_BITS_ARGS_3(Src, _size, flags))) {
if (flags & BSF_LITTLE) {
result = erts_bs_put_integer_le(EBS, Src, _size);
} else {
result = erts_bs_put_integer_be(EBS, Src, _size);
}
if (!result) {
$BS_FAIL_INFO($Fail, BADARG, am_type, Src);
}
}
break;
case BSC_INTEGER_FIXED_SIZE:
case BSC_UTF32:
$BS_LOAD_FLAGS(p, flags);
$BS_LOAD_FIXED_SIZE(p, Size);
if (!erts_new_bs_put_integer(ERL_BITS_ARGS_3(Src, Size, flags))) {
$BS_FAIL_INFO($Fail, BADARG, am_type, Src);
{
int result;

$BS_LOAD_FLAGS(p, flags);
$BS_LOAD_FIXED_SIZE(p, Size);
if (flags & BSF_LITTLE) {
result = erts_bs_put_integer_le(EBS, Src, Size);
} else {
result = erts_bs_put_integer_be(EBS, Src, Size);
}
if (!result) {
$BS_FAIL_INFO($Fail, BADARG, am_type, Src);
}
}
break;
case BSC_UTF8:
if (!erts_bs_put_utf8(ERL_BITS_ARGS_1(Src))) {
if (!erts_bs_put_utf8(EBS, Src)) {
$BS_FAIL_INFO($Fail, BADARG, am_type, Src);
}
break;
case BSC_UTF16:
$BS_LOAD_FLAGS(p, flags);
$BS_LOAD_SRC(p, Src);
if (!erts_bs_put_utf16(ERL_BITS_ARGS_2(Src, flags))) {
if (!erts_bs_put_utf16(EBS, Src, flags)) {
$BS_FAIL_INFO($Fail, BADARG, am_type, Src);
}
break;
Expand Down
Loading
Loading