aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Beregszaszi <alex@rtfs.hu>2017-06-23 22:50:29 +0800
committerGitHub <noreply@github.com>2017-06-23 22:50:29 +0800
commit367389472bf9580fabede5e50d2e3264072fce24 (patch)
tree454960a62e6291defb7bffd528390786c16d1b9f
parent1f7697eee318445e770d4e634477c38685af5110 (diff)
parentb46bb70b969aaa2d38ee84b3732999ec141dbd85 (diff)
downloaddexon-solidity-367389472bf9580fabede5e50d2e3264072fce24.tar.gz
dexon-solidity-367389472bf9580fabede5e50d2e3264072fce24.tar.zst
dexon-solidity-367389472bf9580fabede5e50d2e3264072fce24.zip
Merge pull request #2439 from ethereum/lll-vars
LLL: fix variable (set/get/ref) handling
-rw-r--r--liblll/All.h6
-rw-r--r--liblll/CodeFragment.cpp18
-rw-r--r--test/liblll/EndToEndTest.cpp20
3 files changed, 35 insertions, 9 deletions
diff --git a/liblll/All.h b/liblll/All.h
deleted file mode 100644
index 7c4192f6..00000000
--- a/liblll/All.h
+++ /dev/null
@@ -1,6 +0,0 @@
-#pragma once
-
-#include "CodeFragment.h"
-#include "Compiler.h"
-#include "CompilerState.h"
-#include "Parser.h"
diff --git a/liblll/CodeFragment.cpp b/liblll/CodeFragment.cpp
index 1329ec9b..7496fe83 100644
--- a/liblll/CodeFragment.cpp
+++ b/liblll/CodeFragment.cpp
@@ -171,11 +171,23 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
return string();
};
- auto varAddress = [&](string const& n)
+ auto varAddress = [&](string const& n, bool createMissing = false)
{
+ if (n.empty())
+ error<InvalidName>("Empty variable name not allowed");
auto it = _s.vars.find(n);
if (it == _s.vars.end())
- error<InvalidName>(std::string("Symbol not found: ") + s);
+ {
+ if (createMissing)
+ {
+ // Create new variable
+ bool ok;
+ tie(it, ok) = _s.vars.insert(make_pair(n, make_pair(_s.stackSize, 32)));
+ _s.stackSize += 32;
+ }
+ else
+ error<InvalidName>(std::string("Symbol not found: ") + n);
+ }
return it->second.first;
};
@@ -208,7 +220,7 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
for (auto const& i: _t)
if (c++ == 2)
m_asm.append(CodeFragment(i, _s, false).m_asm);
- m_asm.append((u256)varAddress(firstAsString()));
+ m_asm.append((u256)varAddress(firstAsString(), true));
m_asm.append(Instruction::MSTORE);
}
else if (us == "GET")
diff --git a/test/liblll/EndToEndTest.cpp b/test/liblll/EndToEndTest.cpp
index 02d024a4..3037b14b 100644
--- a/test/liblll/EndToEndTest.cpp
+++ b/test/liblll/EndToEndTest.cpp
@@ -57,6 +57,26 @@ BOOST_AUTO_TEST_CASE(panic)
BOOST_REQUIRE(m_output.empty());
}
+BOOST_AUTO_TEST_CASE(variables)
+{
+ char const* sourceCode = R"(
+ (returnlll
+ (seq
+ (set 'x 1)
+ (set 'y 2)
+ ;; this should equal to 3
+ (set 'z (add (get 'x) (get 'y)))
+ ;; overwriting it here
+ (set 'y 4)
+ ;; each variable has a 32 byte slot, starting from memory location 0x80
+ ;; variable addresses can also be retrieved by x or (ref 'x)
+ (set 'k (add (add (ref 'x) (ref 'y)) z))
+ (return (add (add (get 'x) (add (get 'y) (get 'z))) (get 'k)))))
+ )";
+ compileAndRun(sourceCode);
+ BOOST_CHECK(callFallback() == encodeArgs(u256(488)));
+}
+
BOOST_AUTO_TEST_CASE(when)
{
char const* sourceCode = R"(