From 14c92d83974e801f849d2ed69473d9e2bdacb2fb Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 26 May 2014 20:09:15 +0200 Subject: [PATCH] Convenience fixups. --- liblll/Assembly.cpp | 4 ++++ liblll/Assembly.h | 1 + liblll/CodeFragment.cpp | 9 ++++++--- liblll/CodeFragment.h | 4 ++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/liblll/Assembly.cpp b/liblll/Assembly.cpp index 1f53cfb5f..3614bfc48 100644 --- a/liblll/Assembly.cpp +++ b/liblll/Assembly.cpp @@ -142,6 +142,10 @@ AssemblyItem const& Assembly::append(AssemblyItem const& _i) return back(); } +void Assembly::optimise() +{ +} + bytes Assembly::assemble() const { bytes ret; diff --git a/liblll/Assembly.h b/liblll/Assembly.h index 86b37622e..26392d141 100644 --- a/liblll/Assembly.h +++ b/liblll/Assembly.h @@ -92,6 +92,7 @@ public: std::string out() const { std::stringstream ret; streamOut(ret); return ret.str(); } int deposit() const { return m_deposit; } bytes assemble() const; + void optimise(); std::ostream& streamOut(std::ostream& _out) const; private: diff --git a/liblll/CodeFragment.cpp b/liblll/CodeFragment.cpp index 59ff729e7..299cda5f6 100644 --- a/liblll/CodeFragment.cpp +++ b/liblll/CodeFragment.cpp @@ -102,7 +102,7 @@ CodeFragment::CodeFragment(sp::utree const& _t, CompilerState& _s, bool _allowAS void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s) { - if (_t.empty()) + if (_t.tag() == 0 && _t.empty()) error(); else if (_t.tag() == 0 && _t.front().which() != sp::utree_type::symbol_type) error(); @@ -361,14 +361,17 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s) { requireSize(3); requireDeposit(0, 1); + int minDep = min(code[1].m_asm.deposit(), code[2].m_asm.deposit()); m_asm.append(code[0].m_asm); auto pos = m_asm.appendJumpI(); m_asm.onePath(); - m_asm << code[2].m_asm; + m_asm.append(code[2].m_asm, minDep); auto end = m_asm.appendJump(); m_asm.otherPath(); - m_asm << pos.tag() << code[1].m_asm << end.tag(); + m_asm << pos.tag(); + m_asm.append(code[1].m_asm, minDep); + m_asm << end.tag(); m_asm.donePaths(); } else if (us == "WHEN" || us == "UNLESS") diff --git a/liblll/CodeFragment.h b/liblll/CodeFragment.h index 647604849..6935a111c 100644 --- a/liblll/CodeFragment.h +++ b/liblll/CodeFragment.h @@ -43,10 +43,10 @@ public: static CodeFragment compile(std::string const& _src, CompilerState& _s); /// Consolidates data and compiles code. - bytes code() const { return m_asm.assemble(); } + bytes code() { m_asm.optimise(); return m_asm.assemble(); } /// Consolidates data and compiles code. - std::string assembly() const { return m_asm.out(); } + std::string assembly() { m_asm.optimise(); return m_asm.out(); } private: template void error() const { throw T(); }