1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/*
__________
_____ __ __\______ \_____ _______ ______ ____ _______
/ \ | | \| ___/\__ \ \_ __ \/ ___/_/ __ \\_ __ \
| Y Y \| | /| | / __ \_| | \/\___ \ \ ___/ | | \/
|__|_| /|____/ |____| (____ /|__| /____ > \___ >|__|
\/ \/ \/ \/
Copyright (C) 2004-2006 Ingo Berg
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "muParserBytecode.h"
#include <cassert>
#include <string>
#include <stack>
#include <vector>
#include <iostream>
#include "muParserDef.h"
#include "muParserError.h"
#include "muParserToken.h"
namespace mu
{
//---------------------------------------------------------------------------
/** \brief Bytecode default constructor.
\pre [assert] sizeof(value_type)>=sizeof(map_type)
\pre [assert] sizeof(value_type*)>=sizeof(map_type)
*/
ParserByteCode::ParserByteCode()
:m_iStackPos(0)
,m_vBase()
,mc_iSizeVal( sizeof(value_type) / sizeof(map_type) )
,mc_iSizePtr( std::max( (int)sizeof(value_type*) /
(int)sizeof(map_type), 1 ) )
,mc_iSizeValEntry( 2 + mc_iSizeVal)
{
m_vBase.reserve(1000);
assert( sizeof(value_type)>=sizeof(map_type) );
}
//---------------------------------------------------------------------------
/** \brief Destructor (trivial).*/
ParserByteCode::~ParserByteCode()
{}
//---------------------------------------------------------------------------
/** \brief Copy constructor.
Implemented in Terms of Assign(const ParserByteCode &a_ByteCode)
*/
ParserByteCode::ParserByteCode(const ParserByteCode &a_ByteCode)
:mc_iSizeVal( sizeof(value_type)/sizeof(map_type) )
,mc_iSizePtr( sizeof(value_type*) / sizeof(map_type) )
,mc_iSizeValEntry( 2 + mc_iSizeVal)
{
Assign(a_ByteCode);
}
//---------------------------------------------------------------------------
/** \brief Assignment operator.
Implemented in Terms of Assign(const ParserByteCode &a_ByteCode)
*/
ParserByteCode& ParserByteCode::operator=(const ParserByteCode &a_ByteCode)
{
Assign(a_ByteCode);
return *this;
}
//---------------------------------------------------------------------------
/** \brief Store an address in bytecode.
\param a_pAddr Address to be stored.
\throw nothrow
*/
void ParserByteCode::StorePtr(void *a_pAddr)
{
#if defined(_MSC_VER)
#pragma warning( disable : 4311 )
#endif
// demo code for packing / unpacking pointers into bytecode
// void *ptr(NULL);
// double **pVal;
// double fVal;
// map_type dbg[2];
// dbg[0] = *( reinterpret_cast<map_type*>(&a_pAddr) ),
// dbg[1] = *( reinterpret_cast<map_type*>(&a_pAddr) + 1 );
// Version 1:
// *( (map_type*)&ptr+0) = dbg[0];
// *( (map_type*)&ptr+1) = dbg[1];
// Version 2:
// memcpy(&ptr, dbg, sizeof(dbg));
// Version 3:
// pVal = (double**)dbg;
// fVal = **(double**)dbg;
for (int i=0; i<mc_iSizePtr; ++i)
{
m_vBase.push_back( *( reinterpret_cast<map_type*>(&a_pAddr) + i ) );
}
#if defined(_MSC_VER)
#pragma warning( default : 4311 )
#endif
}
//---------------------------------------------------------------------------
/** \brief Copy state of another object to this.
\throw nowthrow
*/
void ParserByteCode::Assign(const ParserByteCode &a_ByteCode)
{
if (this==&a_ByteCode)
return;
m_iStackPos = a_ByteCode.m_iStackPos;
m_vBase = a_ByteCode.m_vBase;
}
//---------------------------------------------------------------------------
/** \brief Add a Variable pointer to bytecode.
\param a_pVar Pointer to be added.
\throw nothrow
*/
void ParserByteCode::AddVar(value_type *a_pVar)
{
m_vBase.push_back( ++m_iStackPos );
m_vBase.push_back( cmVAR );
StorePtr(a_pVar);
int iSize = GetValSize()-GetPtrSize();
assert(iSize>=0);
// Make sure variable entries have the same size like value entries.
// (necessary for optimization; fill with zeros)
for (int i=0; i<iSize; ++i)
m_vBase.push_back(0);
}
//---------------------------------------------------------------------------
/** \brief Add a Variable pointer to bytecode.
Value entries in byte code consist of:
<ul>
<li>value array position of the value</li>
<li>the operator code according to ParserToken::cmVAL</li>
<li>the value stored in #mc_iSizeVal number of bytecode entries.</li>
</ul>
\param a_pVal Value to be added.
\throw nothrow
*/
void ParserByteCode::AddVal(value_type a_fVal)
{
m_vBase.push_back( ++m_iStackPos );
m_vBase.push_back( cmVAL );
for (int i=0; i<mc_iSizeVal; ++i)
m_vBase.push_back( *(reinterpret_cast<map_type*>(&a_fVal) + i) );
}
//---------------------------------------------------------------------------
/** \brief Add an operator identifier to bytecode.
Operator entries in byte code consist of:
<ul>
<li>value array position of the result</li>
<li>the operator code according to ParserToken::ECmdCode</li>
</ul>
\sa ParserToken::ECmdCode
*/
void ParserByteCode::AddOp(ECmdCode a_Oprt)
{
m_vBase.push_back(--m_iStackPos);
m_vBase.push_back(a_Oprt);
}
//---------------------------------------------------------------------------
/** \brief Add an assignement operator
Operator entries in byte code consist of:
<ul>
<li>cmASSIGN code</li>
<li>the pointer of the destination variable</li>
</ul>
\sa ParserToken::ECmdCode
*/
void ParserByteCode::AddAssignOp(value_type *a_pVar)
{
m_vBase.push_back(--m_iStackPos);
m_vBase.push_back(cmASSIGN);
StorePtr(a_pVar);
}
//---------------------------------------------------------------------------
/** \brief Add function to bytecode.
\param a_iArgc Number of arguments, negative numbers indicate multiarg functions.
\param a_pFun Pointer to function callback.
*/
void ParserByteCode::AddFun(void *a_pFun, int a_iArgc)
{
if (a_iArgc>=0)
{
m_iStackPos = m_iStackPos - a_iArgc + 1;
}
else
{
m_iStackPos = m_iStackPos + a_iArgc + 1;
}
m_vBase.push_back(m_iStackPos);
m_vBase.push_back(cmFUNC);
m_vBase.push_back(a_iArgc);
StorePtr(a_pFun);
}
//---------------------------------------------------------------------------
/** \brief Add Strung function entry to the parser bytecode.
\throw nothrow
A string function entry consists of the stack position of the return value,
followed by a cmSTRFUNC code, the function pointer and an index into the
string buffer maintained by the parser.
*/
void ParserByteCode::AddStrFun(void *a_pFun, int a_iArgc, int a_iIdx)
{
m_iStackPos = m_iStackPos - a_iArgc + 1;
m_vBase.push_back(m_iStackPos);
m_vBase.push_back(cmFUNC_STR);
m_vBase.push_back(a_iArgc);
m_vBase.push_back(a_iIdx);
StorePtr(a_pFun);
}
//---------------------------------------------------------------------------
/** \brief Add end marker to bytecode.
\throw nothrow
*/
void ParserByteCode::Finalize()
{
// yes we need the end code three times!! (I forgot why)
m_vBase.push_back(cmEND);
m_vBase.push_back(cmEND);
m_vBase.push_back(cmEND);
// shrink bytecode vector to fit
storage_type(m_vBase).swap(m_vBase);
}
//---------------------------------------------------------------------------
/** \brief Get Pointer to bytecode data storage. */
const ParserByteCode::map_type* ParserByteCode::GetRawData() const
{
assert(m_vBase.size());
return &m_vBase[0];
}
//---------------------------------------------------------------------------
/** \brief Delete the bytecode.
\throw nothrow
The name of this function is a violation of my own coding guidelines
but this way it's more in line with the STL functions thus more
intuitive.
*/
void ParserByteCode::clear()
{
m_vBase.clear();
m_iStackPos = 0;
}
//---------------------------------------------------------------------------
/** \brief Remove a value number of entries from the bytecode.
\attention Currently I don't test if the entries are really value entries.
*/
void ParserByteCode::RemoveValEntries(unsigned a_iNumber)
{
unsigned iSize = a_iNumber * mc_iSizeValEntry;
assert( m_vBase.size() >= iSize );
m_vBase.resize(m_vBase.size()-iSize);
assert(m_iStackPos >= a_iNumber);
m_iStackPos -= (a_iNumber);
}
//---------------------------------------------------------------------------
/** \brief Dump bytecode (for debugging only!). */
void ParserByteCode::AsciiDump()
{
if (!m_vBase.size())
{
std::cout << "No bytecode available\n";
return;
}
std::cout << "Entries:" << (int)m_vBase.size()
<< " (ValSize:" << mc_iSizeVal
<< " entries, PtrSize:" << mc_iSizePtr
<< " entries, MapSize:" << sizeof(map_type)
<< " byte)\n";
int i = 0;
while ( m_vBase[i] != cmEND && i<(int)m_vBase.size())
{
std::cout << "IDX[" << m_vBase[i++] << "]\t";
switch (m_vBase[i])
{
case cmVAL: std::cout << "VAL "; ++i;
std::cout << "[" << *( reinterpret_cast<double*>(&m_vBase[i]) ) << "]\n";
i += mc_iSizeVal;
break;
case cmVAR: std::cout << "VAR "; ++i;
std::cout << "[ADDR: 0x" << std::hex << *(value_type**)&m_vBase[i] << "]\n";
i += mc_iSizePtr;
// Variable entries have the same size like value entries
// the remaining spave must be skipped
i+= std::max(mc_iSizeVal - mc_iSizePtr, 0);
break;
case cmFUNC:
std::cout << "CALL\t"; ++i;
std::cout << "[ARG:" << std::dec << m_vBase[i] << "]"; ++i;
std::cout << "[ADDR: 0x" << std::hex << *(value_type**)&m_vBase[i] << "]\n";
i += mc_iSizePtr;
break;
case cmFUNC_STR:
std::cout << "CALL STRFUNC\t"; ++i;
std::cout << "[ARG:" << std::dec << m_vBase[i] << "]"; ++i;
std::cout << "[IDX:" << std::dec << m_vBase[i] << "]"; ++i;
std::cout << "[ADDR: 0x" << *(value_type**)&m_vBase[i] << "]\n";
i += mc_iSizePtr;
break;
case cmLT: std::cout << "LT\n"; ++i; break;
case cmGT: std::cout << "GT\n"; ++i; break;
case cmLE: std::cout << "LE\n"; ++i; break;
case cmGE: std::cout << "GE\n"; ++i; break;
case cmEQ: std::cout << "EQ\n"; ++i; break;
case cmNEQ: std::cout << "NEQ\n"; ++i; break;
case cmADD: std::cout << "ADD\n"; ++i; break;
case cmAND: std::cout << "AND\n"; ++i; break;
case cmOR: std::cout << "OR\n"; ++i; break;
case cmXOR: std::cout << "XOR\n"; ++i; break;
case cmSUB: std::cout << "SUB\n"; ++i; break;
case cmMUL: std::cout << "MUL\n"; ++i; break;
case cmDIV: std::cout << "DIV\n"; ++i; break;
case cmPOW: std::cout << "POW\n"; ++i; break;
case cmASSIGN:
std::cout << "ASSIGN\t"; ++i;
std::cout << "[ADDR: 0x" << *(value_type**)&m_vBase[i] << "]\n";
i += mc_iSizePtr;
break;
default: std::cout << "(unknown code: " << m_vBase[i] << ")\n";
++i;
break;
} // switch cmdCode
} // while bytecode
std::cout << "END" << std::endl;
}
} // namespace mu
|