Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r69429 - trunk/libs/spirit/example/qi/compiler_tutorial/calc7
From: joel_at_[hidden]
Date: 2011-02-28 22:06:47


Author: djowel
Date: 2011-02-28 22:06:33 EST (Mon, 28 Feb 2011)
New Revision: 69429
URL: http://svn.boost.org/trac/boost/changeset/69429

Log:
added assembler printing
Text files modified:
   trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.cpp | 56 ++++++++++++++++++++++++++++++++++++++++
   trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.hpp | 1
   trunk/libs/spirit/example/qi/compiler_tutorial/calc7/main.cpp | 6 ++++
   3 files changed, 63 insertions(+), 0 deletions(-)

Modified: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.cpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.cpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.cpp 2011-02-28 22:06:33 EST (Mon, 28 Feb 2011)
@@ -52,6 +52,62 @@
         }
     }
 
+ void program::print_assembler() const
+ {
+ std::vector<int>::const_iterator pc = code.begin();
+
+ std::vector<std::string> locals(variables.size());
+ typedef std::pair<std::string, int> pair;
+ BOOST_FOREACH(pair const& p, variables)
+ {
+ locals[p.second] = p.first;
+ std::cout << "local "
+ << p.first << ", @" << p.second << std::endl;
+ }
+
+ while (pc != code.end())
+ {
+ switch (*pc++)
+ {
+ case op_neg:
+ std::cout << "op_neg" << std::endl;
+ break;
+
+ case op_add:
+ std::cout << "op_add" << std::endl;
+ break;
+
+ case op_sub:
+ std::cout << "op_sub" << std::endl;
+ break;
+
+ case op_mul:
+ std::cout << "op_mul" << std::endl;
+ break;
+
+ case op_div:
+ std::cout << "op_div" << std::endl;
+ break;
+
+ case op_load:
+ std::cout << "op_load " << locals[*pc++] << std::endl;
+ break;
+
+ case op_store:
+ std::cout << "op_store " << locals[*pc++] << std::endl;
+ break;
+
+ case op_int:
+ std::cout << "op_int " << *pc++ << std::endl;
+ break;
+
+ case op_adstk:
+ std::cout << "op_adstk " << *pc++ << std::endl;
+ break;
+ }
+ }
+ }
+
     bool compiler::operator()(unsigned int x) const
     {
         program.op(op_int, x);

Modified: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.hpp 2011-02-28 22:06:33 EST (Mon, 28 Feb 2011)
@@ -37,6 +37,7 @@
         void add_var(std::string const& name);
 
         void print_variables(std::vector<int> const& stack) const;
+ void print_assembler() const;
 
     private:
 

Modified: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/main.cpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/calc7/main.cpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/main.cpp 2011-02-28 22:06:33 EST (Mon, 28 Feb 2011)
@@ -66,8 +66,14 @@
         if (compile(ast))
         {
             std::cout << "Success\n";
+ std::cout << "-------------------------\n";
             vm.execute(program());
 
+ std::cout << "-------------------------\n";
+ std::cout << "Assembler----------------\n\n";
+ program.print_assembler();
+
+ std::cout << "-------------------------\n";
             std::cout << "Results------------------\n\n";
             program.print_variables(vm.get_stack());
         }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk