aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Beregszaszi <alex@rtfs.hu>2017-10-02 20:17:52 +0800
committerAlex Beregszaszi <alex@rtfs.hu>2017-10-03 04:19:36 +0800
commit26f3ea8cf7fba8d8222ab1889cfbf89046371d10 (patch)
treec19c2cc8cf9db681ba84f4789fa401d43668b848
parent91b20b4bd247ca5163fc562d54ae639eda2532ef (diff)
downloaddexon-solidity-26f3ea8cf7fba8d8222ab1889cfbf89046371d10.tar
dexon-solidity-26f3ea8cf7fba8d8222ab1889cfbf89046371d10.tar.gz
dexon-solidity-26f3ea8cf7fba8d8222ab1889cfbf89046371d10.tar.bz2
dexon-solidity-26f3ea8cf7fba8d8222ab1889cfbf89046371d10.tar.lz
dexon-solidity-26f3ea8cf7fba8d8222ab1889cfbf89046371d10.tar.xz
dexon-solidity-26f3ea8cf7fba8d8222ab1889cfbf89046371d10.tar.zst
dexon-solidity-26f3ea8cf7fba8d8222ab1889cfbf89046371d10.zip
LLL: change (include) to use a callback
-rw-r--r--liblll/CodeFragment.cpp25
-rw-r--r--liblll/CodeFragment.h7
-rw-r--r--liblll/Compiler.cpp9
-rw-r--r--liblll/Compiler.h6
-rw-r--r--liblll/CompilerState.cpp2
-rw-r--r--lllc/main.cpp4
6 files changed, 30 insertions, 23 deletions
diff --git a/liblll/CodeFragment.cpp b/liblll/CodeFragment.cpp
index 6951c40a..49c48027 100644
--- a/liblll/CodeFragment.cpp
+++ b/liblll/CodeFragment.cpp
@@ -47,7 +47,8 @@ void CodeFragment::finalise(CompilerState const& _cs)
}
}
-CodeFragment::CodeFragment(sp::utree const& _t, CompilerState& _s, bool _allowASM)
+CodeFragment::CodeFragment(sp::utree const& _t, CompilerState& _s, ReadCallback const& _readFile, bool _allowASM):
+ m_readFile(_readFile)
{
/*
std::cout << "CodeFragment. Locals:";
@@ -214,7 +215,7 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
int c = 0;
for (auto const& i: _t)
if (c++)
- m_asm.append(CodeFragment(i, _s, true).m_asm);
+ m_asm.append(CodeFragment(i, _s, m_readFile, true).m_asm);
}
else if (us == "INCLUDE")
{
@@ -223,10 +224,10 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
string fileName = firstAsString();
if (fileName.empty())
error<InvalidName>("Empty file name provided");
- string contents = contentsString(fileName);
+ string contents = m_readFile(fileName);
if (contents.empty())
error<InvalidName>(std::string("File not found (or empty): ") + fileName);
- m_asm.append(CodeFragment::compile(contents, _s).m_asm);
+ m_asm.append(CodeFragment::compile(contents, _s, m_readFile).m_asm);
}
else if (us == "SET")
{
@@ -235,7 +236,7 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
int c = 0;
for (auto const& i: _t)
if (c++ == 2)
- m_asm.append(CodeFragment(i, _s, false).m_asm);
+ m_asm.append(CodeFragment(i, _s, m_readFile, false).m_asm);
m_asm.append((u256)varAddress(firstAsString(), true));
m_asm.append(Instruction::MSTORE);
}
@@ -276,7 +277,7 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
if (_t.size() == 3)
{
/// NOTE: some compilers could do the assignment first if this is done in a single line
- CodeFragment code = CodeFragment(i, _s);
+ CodeFragment code = CodeFragment(i, _s, m_readFile);
_s.defs[n] = code;
}
else
@@ -317,7 +318,7 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
}
else if (ii == 1)
{
- pos = CodeFragment(i, _s);
+ pos = CodeFragment(i, _s, m_readFile);
if (pos.m_asm.deposit() != 1)
error<InvalidDeposit>(toString(i));
}
@@ -396,9 +397,9 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
if (c++)
{
if (us == "LLL" && c == 1)
- code.push_back(CodeFragment(i, ns));
+ code.push_back(CodeFragment(i, ns, m_readFile));
else
- code.push_back(CodeFragment(i, _s));
+ code.push_back(CodeFragment(i, _s, m_readFile));
}
auto requireSize = [&](unsigned s) { if (code.size() != s) error<IncorrectParameterCount>(us); };
auto requireMinSize = [&](unsigned s) { if (code.size() < s) error<IncorrectParameterCount>(us); };
@@ -419,7 +420,7 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
//requireDeposit(i, 1);
cs.args[m.args[i]] = code[i];
}
- m_asm.append(CodeFragment(m.code, cs).m_asm);
+ m_asm.append(CodeFragment(m.code, cs, m_readFile).m_asm);
for (auto const& i: cs.defs)
_s.defs[i.first] = i.second;
for (auto const& i: cs.macros)
@@ -676,13 +677,13 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
}
}
-CodeFragment CodeFragment::compile(string const& _src, CompilerState& _s)
+CodeFragment CodeFragment::compile(string const& _src, CompilerState& _s, ReadCallback const& _readFile)
{
CodeFragment ret;
sp::utree o;
parseTreeLLL(_src, o);
if (!o.empty())
- ret = CodeFragment(o, _s);
+ ret = CodeFragment(o, _s, _readFile);
_s.treesToKill.push_back(o);
return ret;
}
diff --git a/liblll/CodeFragment.h b/liblll/CodeFragment.h
index 95d21563..e5cac34e 100644
--- a/liblll/CodeFragment.h
+++ b/liblll/CodeFragment.h
@@ -39,10 +39,12 @@ struct CompilerState;
class CodeFragment
{
public:
+ using ReadCallback = std::function<std::string(std::string const&)>;
+
CodeFragment() {}
- CodeFragment(sp::utree const& _t, CompilerState& _s, bool _allowASM = false);
+ CodeFragment(sp::utree const& _t, CompilerState& _s, ReadCallback const& _readFile, bool _allowASM = false);
- static CodeFragment compile(std::string const& _src, CompilerState& _s);
+ static CodeFragment compile(std::string const& _src, CompilerState& _s, ReadCallback const& _readFile);
/// Consolidates data and compiles code.
Assembly& assembly(CompilerState const& _cs) { finalise(_cs); return m_asm; }
@@ -60,6 +62,7 @@ private:
bool m_finalised = false;
Assembly m_asm;
+ ReadCallback m_readFile;
};
static const CodeFragment NullCodeFragment;
diff --git a/liblll/Compiler.cpp b/liblll/Compiler.cpp
index b69675aa..1638f69e 100644
--- a/liblll/Compiler.cpp
+++ b/liblll/Compiler.cpp
@@ -28,13 +28,14 @@ using namespace std;
using namespace dev;
using namespace dev::eth;
-bytes dev::eth::compileLLL(string const& _src, bool _opt, vector<string>* _errors)
+
+bytes dev::eth::compileLLL(string const& _src, bool _opt, vector<string>* _errors, ReadCallback const& _readFile)
{
try
{
CompilerState cs;
cs.populateStandard();
- auto assembly = CodeFragment::compile(_src, cs).assembly(cs);
+ auto assembly = CodeFragment::compile(_src, cs, _readFile).assembly(cs);
if (_opt)
assembly = assembly.optimise(true);
bytes ret = assembly.assemble().bytecode;
@@ -66,13 +67,13 @@ bytes dev::eth::compileLLL(string const& _src, bool _opt, vector<string>* _error
return bytes();
}
-std::string dev::eth::compileLLLToAsm(std::string const& _src, bool _opt, std::vector<std::string>* _errors)
+std::string dev::eth::compileLLLToAsm(std::string const& _src, bool _opt, std::vector<std::string>* _errors, ReadCallback const& _readFile)
{
try
{
CompilerState cs;
cs.populateStandard();
- auto assembly = CodeFragment::compile(_src, cs).assembly(cs);
+ auto assembly = CodeFragment::compile(_src, cs, _readFile).assembly(cs);
if (_opt)
assembly = assembly.optimise(true);
string ret = assembly.assemblyString();
diff --git a/liblll/Compiler.h b/liblll/Compiler.h
index 04aa1e26..c3395b66 100644
--- a/liblll/Compiler.h
+++ b/liblll/Compiler.h
@@ -30,9 +30,11 @@ namespace dev
namespace eth
{
+using ReadCallback = std::function<std::string(std::string const&)>;
+
std::string parseLLL(std::string const& _src);
-std::string compileLLLToAsm(std::string const& _src, bool _opt = true, std::vector<std::string>* _errors = nullptr);
-bytes compileLLL(std::string const& _src, bool _opt = true, std::vector<std::string>* _errors = nullptr);
+std::string compileLLLToAsm(std::string const& _src, bool _opt = true, std::vector<std::string>* _errors = nullptr, ReadCallback const& _readFile = ReadCallback());
+bytes compileLLL(std::string const& _src, bool _opt = true, std::vector<std::string>* _errors = nullptr, ReadCallback const& _readFile = ReadCallback());
}
}
diff --git a/liblll/CompilerState.cpp b/liblll/CompilerState.cpp
index d53dec7e..c0e344b2 100644
--- a/liblll/CompilerState.cpp
+++ b/liblll/CompilerState.cpp
@@ -82,5 +82,5 @@ void CompilerState::populateStandard()
"(def 'shl (val shift) (mul val (exp 2 shift)))"
"(def 'shr (val shift) (div val (exp 2 shift)))"
"}";
- CodeFragment::compile(s, *this);
+ CodeFragment::compile(s, *this, CodeFragment::ReadCallback());
}
diff --git a/lllc/main.cpp b/lllc/main.cpp
index 06a0fc81..912ce16a 100644
--- a/lllc/main.cpp
+++ b/lllc/main.cpp
@@ -138,7 +138,7 @@ int main(int argc, char** argv)
}
else if (mode == Binary || mode == Hex)
{
- auto bs = compileLLL(src, optimise ? true : false, &errors);
+ auto bs = compileLLL(src, optimise ? true : false, &errors, contentsString);
if (mode == Hex)
cout << toHex(bs) << endl;
else if (mode == Binary)
@@ -147,7 +147,7 @@ int main(int argc, char** argv)
else if (mode == ParseTree)
cout << parseLLL(src) << endl;
else if (mode == Assembly)
- cout << compileLLLToAsm(src, optimise ? true : false, &errors) << endl;
+ cout << compileLLLToAsm(src, optimise ? true : false, &errors, contentsString) << endl;
for (auto const& i: errors)
cerr << i << endl;
if ( errors.size() )