aboutsummaryrefslogtreecommitdiffstats
path: root/solidityExpressionCompiler.cpp
diff options
context:
space:
mode:
authorChristian <c@ethdev.com>2014-11-05 15:40:21 +0800
committerChristian <c@ethdev.com>2014-11-06 09:44:43 +0800
commit08cba0653f781dec66b3211990506066a48390c2 (patch)
tree7f365b55e3add262011c444acdcbb9a5d98c4a21 /solidityExpressionCompiler.cpp
parentb9cc3baf40f7235b0c4302742f294e6228cff070 (diff)
downloaddexon-solidity-08cba0653f781dec66b3211990506066a48390c2.tar
dexon-solidity-08cba0653f781dec66b3211990506066a48390c2.tar.gz
dexon-solidity-08cba0653f781dec66b3211990506066a48390c2.tar.bz2
dexon-solidity-08cba0653f781dec66b3211990506066a48390c2.tar.lz
dexon-solidity-08cba0653f781dec66b3211990506066a48390c2.tar.xz
dexon-solidity-08cba0653f781dec66b3211990506066a48390c2.tar.zst
dexon-solidity-08cba0653f781dec66b3211990506066a48390c2.zip
Support for negative literals.
Diffstat (limited to 'solidityExpressionCompiler.cpp')
-rw-r--r--solidityExpressionCompiler.cpp41
1 files changed, 40 insertions, 1 deletions
diff --git a/solidityExpressionCompiler.cpp b/solidityExpressionCompiler.cpp
index 3be909c3..8fc4a1a2 100644
--- a/solidityExpressionCompiler.cpp
+++ b/solidityExpressionCompiler.cpp
@@ -227,7 +227,7 @@ BOOST_AUTO_TEST_CASE(arithmetics)
BOOST_AUTO_TEST_CASE(unary_operators)
{
char const* sourceCode = "contract test {\n"
- " function f() { var x = !(~+-1 == 2); }"
+ " function f() { var x = !(~+- 1 == 2); }"
"}\n";
bytes code = compileFirstExpression(sourceCode);
@@ -347,6 +347,45 @@ BOOST_AUTO_TEST_CASE(function_call)
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
}
+BOOST_AUTO_TEST_CASE(negative_literals_8bits)
+{
+ // these all fit in 8 bits
+ char const* sourceCode = "contract test {\n"
+ " function f() { int8 x = -0 + -1 + -0x01 + -127 + -128; }\n"
+ "}\n";
+ bytes code = compileFirstExpression(sourceCode);
+
+ bytes expectation(bytes({byte(eth::Instruction::PUSH1), 0x00,
+ byte(eth::Instruction::PUSH32)}) + bytes(32, 0xff) +
+ bytes({byte(eth::Instruction::ADD),
+ byte(eth::Instruction::PUSH32)}) + bytes(32, 0xff) +
+ bytes({byte(eth::Instruction::ADD),
+ byte(eth::Instruction::PUSH32)}) + bytes(31, 0xff) + bytes(1, 0x81) +
+ bytes({byte(eth::Instruction::ADD),
+ byte(eth::Instruction::PUSH32)}) + bytes(31, 0xff) + bytes(1, 0x80) +
+ bytes({byte(eth::Instruction::ADD)}));
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
+BOOST_AUTO_TEST_CASE(negative_literals_16bits)
+{
+ // -1 should need 8 bits, -129 should need 16 bits, how many bits are used is visible
+ // from the SIGNEXTEND opcodes
+ char const* sourceCode = "contract test {\n"
+ " function f() { int64 x = int64(-1 + -129); }\n"
+ "}\n";
+ bytes code = compileFirstExpression(sourceCode);
+
+ bytes expectation(bytes({byte(eth::Instruction::PUSH32)}) + bytes(32, 0xff) +
+ bytes({byte(eth::Instruction::PUSH1), 0x00,
+ byte(eth::Instruction::SIGNEXTEND),
+ byte(eth::Instruction::PUSH32)}) + bytes(31, 0xff) + bytes(1, 0x7f) +
+ bytes({byte(eth::Instruction::ADD),
+ byte(eth::Instruction::PUSH1), 0x01,
+ byte(eth::Instruction::SIGNEXTEND)}));
+ BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
+}
+
BOOST_AUTO_TEST_SUITE_END()
}