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

Add binary {string|literal|number} to solidity and yul #15003

Closed
wants to merge 19 commits into from

Conversation

pcw109550
Copy link
Contributor

@pcw109550 pcw109550 commented Apr 9, 2024

Feature Description

Added binary {string|literal|number} to solidity and yul. Below works.

contract Test {
    bytes constant c_ = bin"11110000_0000111100001111";
    function binaryTest() public {
        bytes32 a = bin"11111111";
        // bytes4 b_ = bin"11"; // error, must be multiple of 8
        uint b = 0b101;
        uint c = 0b10101_101_10101;
        bytes memory d = bin'1111111100000000';
        uint256 e = 0b1011 * 1 days;
        bytes4 f = 0b0;
        // bytes2 g_ = 0b000011110000111100001111; // error, rvalue too big
        bytes3 g = 0b1111000011111111_000_00000;
        assembly {
            mstore(0b1001, 1)
            mstore(0b10, bin'10101010')
            let h := 0b1110
            mstore(bin"11001111", bin'11110000_11010111')
            switch h
            case 0b1 { }
            case bin"00010011" { }
            default { }
        }
    }
}

Syntax rules referred from hex string/literal/numbers. Please check ANTLR rules and docs for exact behavior. May subject to change!

Tests

Mirrored all hex related tests, and added binary literal related test if necessary. Please suggest if i missed anything.

Design

I manually implemented parsing binary strings to u256. Currently boost's multi precision lib, which is the dependency for solidity does not support binary parsing. Solidity internally uses u256 type, so I parsed manually.

New Errors

5146_error and 5409_error are introduced

TODOs

There is a single TODO left at libsolidity/AST/Types.cpp:

std::string StringLiteralType::toString(bool) const
{
	auto isPrintableASCII = [](std::string const& s)
	{
		for (auto c: s)
		{
			if (static_cast<unsigned>(c) <= 0x1f || static_cast<unsigned>(c) >= 0x7f)
				return false;
		}
		return true;
	};

	// TODO(pcw109550) can we fix this?

	return isPrintableASCII(m_value) ?
		("literal_string \"" + m_value + "\"") :
		("literal_string hex\"" + util::toHex(util::asBytes(m_value)) + "\"");
}

When control flow reaches TODO, we do not know m_value originated from hex string, string, or bin string. I just left as is. If this is okay, will remove TODO.

Copy link

github-actions bot commented Apr 9, 2024

Thank you for your contribution to the Solidity compiler! A team member will follow up shortly.

If you haven't read our contributing guidelines and our review checklist before, please do it now, this makes the reviewing process and accepting your contribution smoother.

If you have any questions or need our help, feel free to post them in the PR or talk to us directly on the #solidity-dev channel on Matrix.

@mehtavishwa30
Copy link
Contributor

Hey @pcw109550! Thank you for your contribution.

We reviewed the PR and had a few inputs. It is generally not a common practice to work on closed issues. If you see an issue that you would like to work on in the future (especially closed ones), it is best to discuss with the team on matrix (I noticed that you joined recently) or join our language design calls prior to getting started to avoid spending time and effort on PRs that might need certain considerations before they can be accepted.

That being said, we are unable to accept the PR in its current state due to the following:

  • The addition of the bin keyword makes this a breaking change. We're open to reviewing again a version of the PR that only adds support for the 0b prefix.
  • It makes more sense to first add support for binary literals in Solidity and only implement support for Yul in a separate PR once the former is merged. That way, we can accept the changes in smaller and easily reviewable steps.

Feel free to reach out to the team through the channels mentioned earlier to take this forward. :)

@pcw109550
Copy link
Contributor Author

@mehtavishwa30 Thanks for your input. Will first remove the bin keyword and separate the 0b prefix to several PRs.

@pcw109550 pcw109550 closed this Apr 11, 2024
@pcw109550
Copy link
Contributor Author

@mehtavishwa30 Can you reopen this #14546 and remove stale status?

@cameel cameel reopened this Apr 11, 2024
@cameel cameel closed this Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants