1 /*-
2 * Copyright (c) 2004 - 2010 CTPP Team
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 4. Neither the name of the CTPP Team nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * CTPP2SimpleVM.hpp
29 *
30 * $CTPP$
31 */
32 #include "CTPP2SimpleVM.hpp"
33
34 #include "CTPP2FileOutputCollector.hpp"
35 #include "CTPP2Logger.hpp"
36 #include "CTPP2StringOutputCollector.hpp"
37 #include "CTPP2VMSTDLib.hpp"
38 #include "CTPP2SyscallFactory.hpp"
39 #include "CTPP2VM.hpp"
40 #include "CTPP2VMLoader.hpp"
41
42 namespace CTPP // C++ Template Engine
43 {
44
45 //
46 // PIMPL
47 //
48 struct SimpleVM::_SimpleVM
49 {
50 // Syscall factory
51 SyscallFactory syscall_factory;
52 // Virtual machine
53 VM vm;
54
55 _SimpleVM(const UINT_32 & iIMaxFunctions,
56 const UINT_32 & iIMaxArgStackSize,
57 const UINT_32 & iIMaxCodeStackSize,
58 const UINT_32 & iIMaxSteps,
59 const UINT_32 & iIDebugLevel);
60
61 ~_SimpleVM() throw();
62 };
63
64 //
65 // C-tor
66 //
_SimpleVM(const UINT_32 & iIMaxFunctions,const UINT_32 & iIMaxArgStackSize,const UINT_32 & iIMaxCodeStackSize,const UINT_32 & iIMaxSteps,const UINT_32 & iIDebugLevel)67 SimpleVM::_SimpleVM::_SimpleVM(const UINT_32 & iIMaxFunctions,
68 const UINT_32 & iIMaxArgStackSize,
69 const UINT_32 & iIMaxCodeStackSize,
70 const UINT_32 & iIMaxSteps,
71 const UINT_32 & iIDebugLevel): syscall_factory(iIMaxFunctions),
72 vm(&syscall_factory, iIMaxArgStackSize, iIMaxCodeStackSize, iIMaxSteps, iIDebugLevel)
73 {
74 STDLibInitializer::InitLibrary(syscall_factory);
75 }
76
77 //
78 // A destructor
79 //
~_SimpleVM()80 SimpleVM::_SimpleVM::~_SimpleVM() throw()
81 {
82 STDLibInitializer::DestroyLibrary(syscall_factory);
83 }
84
85 //
86 // C-tor
87 //
SimpleVM(const UINT_32 & iIMaxFunctions,const UINT_32 & iIMaxArgStackSize,const UINT_32 & iIMaxCodeStackSize,const UINT_32 & iIMaxSteps,const UINT_32 & iIDebugLevel)88 SimpleVM::SimpleVM(const UINT_32 & iIMaxFunctions,
89 const UINT_32 & iIMaxArgStackSize,
90 const UINT_32 & iIMaxCodeStackSize,
91 const UINT_32 & iIMaxSteps,
92 const UINT_32 & iIDebugLevel): pSimpleVM(NULL)
93 {
94 pSimpleVM = new _SimpleVM(iIMaxFunctions, iIMaxArgStackSize, iIMaxCodeStackSize, iIMaxSteps, iIDebugLevel);
95 }
96
97 //
98 // Write Output to STL string
99 //
Run(CDT & oData,const VMLoader & oLoader,std::string & sResult,Logger & oLogger)100 UINT_32 SimpleVM::Run(CDT & oData, const VMLoader & oLoader, std::string & sResult, Logger & oLogger)
101 {
102 StringOutputCollector oOutputCollector(sResult);
103
104 return Run(oData, oLoader, oOutputCollector, oLogger);
105 }
106
107 //
108 // Write Output to file
109 //
Run(CDT & oData,const VMLoader & oLoader,FILE * F,Logger & oLogger)110 UINT_32 SimpleVM::Run(CDT & oData, const VMLoader & oLoader, FILE * F, Logger & oLogger)
111 {
112 FileOutputCollector oOutputCollector(F);
113
114 return Run(oData, oLoader, oOutputCollector, oLogger);
115 }
116
117 //
118 // Output to specified data collector
119 //
Run(CDT & oData,const VMLoader & oLoader,OutputCollector & oCollector,Logger & oLogger)120 UINT_32 SimpleVM::Run(CDT & oData, const VMLoader & oLoader, OutputCollector & oCollector, Logger & oLogger)
121 {
122 const VMMemoryCore * pCore = oLoader.GetCore();
123
124 return Run(oData, pCore, oCollector, oLogger);
125 }
126
127 //
128 // Write Output to string
129 //
Run(CDT & oData,const VMMemoryCore * pCore,std::string & sResult,Logger & oLogger)130 UINT_32 SimpleVM::Run(CDT & oData, const VMMemoryCore * pCore, std::string & sResult, Logger & oLogger)
131 {
132 StringOutputCollector oOutputCollector(sResult);
133
134 return Run(oData, pCore, oOutputCollector, oLogger);
135 }
136
137 //
138 // Write Output to file
139 //
Run(CDT & oData,const VMMemoryCore * pCore,FILE * F,Logger & oLogger)140 UINT_32 SimpleVM::Run(CDT & oData, const VMMemoryCore * pCore, FILE * F, Logger & oLogger)
141 {
142 FileOutputCollector oOutputCollector(F);
143
144 return Run(oData, pCore, oOutputCollector, oLogger);
145 }
146
147 //
148 // Output to specified data collector
149 //
Run(CDT & oData,const VMMemoryCore * pCore,OutputCollector & oCollector,Logger & oLogger)150 UINT_32 SimpleVM::Run(CDT & oData, const VMMemoryCore * pCore, OutputCollector & oCollector, Logger & oLogger)
151 {
152 UINT_32 iIP = 0;
153
154 pSimpleVM -> vm.Init(pCore, &oCollector, &oLogger);
155 pSimpleVM -> vm.Run(pCore, &oCollector, iIP, oData, &oLogger);
156
157 return iIP;
158 }
159
160 //
161 // A destructor
162 //
~SimpleVM()163 SimpleVM::~SimpleVM() throw()
164 {
165 delete pSimpleVM;
166 }
167
168 } // namespace CTPP
169 // End.
170