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
|
#include "fpconfig.hh"
#include "fparser.hh"
#include "extrasrc/fptypes.hh"
#ifdef FP_SUPPORT_OPTIMIZER
#include <vector>
#include <utility>
#include "codetree.hh"
#ifndef FP_GENERATING_POWI_TABLE
enum { MAX_POWI_BYTECODE_LENGTH = 20 };
#else
enum { MAX_POWI_BYTECODE_LENGTH = 999 };
#endif
enum { MAX_MULI_BYTECODE_LENGTH = 3 };
namespace FPoptimizer_ByteCode
{
template<typename Value_t>
class ByteCodeSynth
{
public:
ByteCodeSynth()
: ByteCode(), Immed(), StackState(), StackTop(0), StackMax(0)
{
/* estimate the initial requirements as such */
ByteCode.reserve(64);
Immed.reserve(8);
StackState.reserve(16);
}
void Pull(std::vector<unsigned>& bc,
std::vector<Value_t>& imm,
size_t& StackTop_max)
{
/* The bitmask 0x80000000u was added to each non-opcode
* value within ByteCode[] (opcode parameters) to prevent
* them being interpreted as opcodes by fp_opcode_add.inc.
* fparser uses cNop for the same purpose.
*/
for(unsigned a=0; a<ByteCode.size(); ++a)
{
ByteCode[a] &= ~0x80000000u;
}
ByteCode.swap(bc);
Immed.swap(imm);
StackTop_max = StackMax;
}
size_t GetByteCodeSize() const { return ByteCode.size(); }
size_t GetStackTop() const { return StackTop; }
void PushVar(unsigned varno)
{
ByteCode.push_back(varno);
SetStackTop(StackTop+1);
}
void PushImmed(Value_t immed)
{
using namespace FUNCTIONPARSERTYPES;
ByteCode.push_back(cImmed);
Immed.push_back(immed);
SetStackTop(StackTop+1);
}
void StackTopIs(const FPoptimizer_CodeTree::CodeTree<Value_t>& tree, int offset = 0)
{
if((int)StackTop > offset)
{
StackState[StackTop-1-offset].first = true;
StackState[StackTop-1-offset].second = tree;
}
}
bool IsStackTop(const FPoptimizer_CodeTree::CodeTree<Value_t>& tree, int offset = 0) const
{
return (int)StackTop > offset
&& StackState[StackTop-1-offset].first
&& StackState[StackTop-1-offset].second.IsIdenticalTo(tree);
}
inline void EatNParams(unsigned eat_count)
{
StackTop -= eat_count;
}
void ProducedNParams(unsigned produce_count)
{
SetStackTop(StackTop + produce_count);
}
void DoPopNMov(size_t targetpos, size_t srcpos)
{
using namespace FUNCTIONPARSERTYPES;
ByteCode.push_back(cPopNMov);
ByteCode.push_back( 0x80000000u | (unsigned) targetpos);
ByteCode.push_back( 0x80000000u | (unsigned) srcpos);
SetStackTop(srcpos+1);
StackState[targetpos] = StackState[srcpos];
SetStackTop(targetpos+1);
}
void DoDup(size_t src_pos)
{
using namespace FUNCTIONPARSERTYPES;
if(src_pos == StackTop-1)
{
ByteCode.push_back(cDup);
}
else
{
ByteCode.push_back(cFetch);
ByteCode.push_back( 0x80000000u | (unsigned) src_pos);
}
SetStackTop(StackTop + 1);
StackState[StackTop-1] = StackState[src_pos];
}
#ifdef FUNCTIONPARSER_SUPPORT_DEBUGGING
template<int/*defer*/>
void Dump()
{
std::ostream& o = std::cout;
o << "Stack state now(" << StackTop << "):\n";
for(size_t a=0; a<StackTop; ++a)
{
o << a << ": ";
if(StackState[a].first)
{
const FPoptimizer_CodeTree::CodeTree<Value_t>
& tree = StackState[a].second;
o << '[' << std::hex << (void*)(&tree.GetParams())
<< std::dec
<< ',' << tree.GetRefCount()
<< ']';
DumpTree(tree, o);
}
else
o << "?";
o << "\n";
}
o << std::flush;
}
#endif
size_t FindPos(const FPoptimizer_CodeTree::CodeTree<Value_t>& tree) const
{
for(size_t a=StackTop; a-->0; )
if(StackState[a].first && StackState[a].second.IsIdenticalTo(tree))
return a;
return ~size_t(0);
}
bool Find(const FPoptimizer_CodeTree::CodeTree<Value_t>& tree) const
{
return FindPos(tree) != ~size_t(0);
}
bool FindAndDup(const FPoptimizer_CodeTree::CodeTree<Value_t>& tree)
{
size_t pos = FindPos(tree);
if(pos != ~size_t(0))
{
#ifdef DEBUG_SUBSTITUTIONS
std::cout << "Found duplicate at [" << pos <<"]: ";
DumpTree(tree);
std::cout << " -- issuing cDup or cFetch\n";
#endif
DoDup(pos);
return true;
}
return false;
}
struct IfData
{
size_t ofs;
};
void SynthIfStep1(IfData& ifdata, FUNCTIONPARSERTYPES::OPCODE op)
{
using namespace FUNCTIONPARSERTYPES;
SetStackTop(StackTop-1); // the If condition was popped.
ifdata.ofs = ByteCode.size();
ByteCode.push_back(op);
ByteCode.push_back(0x80000000u); // code index
ByteCode.push_back(0x80000000u); // Immed index
}
void SynthIfStep2(IfData& ifdata)
{
using namespace FUNCTIONPARSERTYPES;
SetStackTop(StackTop-1); // ignore the pushed then-branch result.
ByteCode[ifdata.ofs+1] = 0x80000000u | unsigned( ByteCode.size()+2 );
ByteCode[ifdata.ofs+2] = 0x80000000u | unsigned( Immed.size() );
ifdata.ofs = ByteCode.size();
ByteCode.push_back(cJump);
ByteCode.push_back(0x80000000u); // code index
ByteCode.push_back(0x80000000u); // Immed index
}
void SynthIfStep3(IfData& ifdata)
{
using namespace FUNCTIONPARSERTYPES;
SetStackTop(StackTop-1); // ignore the pushed else-branch result.
ByteCode.back() |= 0x80000000u;
// ^Necessary for guarding against if(x,1,2)+1 being changed
// into if(x,1,3) by fp_opcode_add.inc
ByteCode[ifdata.ofs+1] = 0x80000000u | unsigned( ByteCode.size()-1 );
ByteCode[ifdata.ofs+2] = 0x80000000u | unsigned( Immed.size() );
SetStackTop(StackTop+1); // one or the other was pushed.
/* Threading jumps:
* If there are any cJumps that point
* to the cJump instruction we just changed,
* change them to point to this target as well.
* This screws up PrintByteCode() majorly.
*/
for(size_t a=0; a<ifdata.ofs; ++a)
{
if(ByteCode[a] == cJump
&& ByteCode[a+1] == (0x80000000u | (ifdata.ofs-1)))
{
ByteCode[a+1] = 0x80000000u | unsigned( ByteCode.size()-1 );
ByteCode[a+2] = 0x80000000u | unsigned( Immed.size() );
}
switch(ByteCode[a])
{
case cAbsIf:
case cIf:
case cJump:
case cPopNMov: a += 2; break;
case cFCall:
case cPCall:
case cFetch: a += 1; break;
default: break;
}
}
}
protected:
void SetStackTop(size_t value)
{
StackTop = value;
if(StackTop > StackMax)
{
StackMax = StackTop;
StackState.resize(StackMax);
}
}
protected:
std::vector<unsigned> ByteCode;
std::vector<Value_t> Immed;
std::vector<
std::pair<bool/*known*/,
FPoptimizer_CodeTree::CodeTree<Value_t>/*tree*/>
> StackState;
size_t StackTop;
size_t StackMax;
private:
void incStackPtr()
{
if(StackTop+2 > StackMax) StackState.resize(StackMax=StackTop+2);
}
template<bool IsIntType, bool IsComplexType>
struct Specializer { };
public:
void AddOperation(unsigned opcode, unsigned eat_count, unsigned produce_count = 1)
{
EatNParams(eat_count);
AddFunctionOpcode(opcode);
ProducedNParams(produce_count);
}
void AddFunctionOpcode(unsigned opcode, Specializer<false,false>);
void AddFunctionOpcode(unsigned opcode, Specializer<false,true>);
void AddFunctionOpcode(unsigned opcode, Specializer<true,false>);
void AddFunctionOpcode(unsigned opcode, Specializer<true,true>);
inline void AddFunctionOpcode(unsigned opcode)
{
AddFunctionOpcode
(opcode,
Specializer< bool(FUNCTIONPARSERTYPES::IsIntType<Value_t>::result),
bool(FUNCTIONPARSERTYPES::IsComplexType<Value_t>::result)
> ()
);
}
};
template<typename Value_t>
struct SequenceOpCode;
template<typename Value_t>
struct SequenceOpcodes
{
/* Multiplication implemented with adds */
static const SequenceOpCode<Value_t> AddSequence;
/* Exponentiation implemented with muls */
static const SequenceOpCode<Value_t> MulSequence;
};
/* Generate a sequence that multiplies or exponentifies the
* last operand in the stack by the given constant integer
* amount (positive or negative).
*/
template<typename Value_t>
void AssembleSequence(
long count,
const SequenceOpCode<Value_t>& sequencing,
ByteCodeSynth<Value_t>& synth);
}
#endif
|