mirror of https://github.com/OpenMW/openmw.git
added literal container and reworked output container handling
parent
ba847ae9d8
commit
5cf3264bd3
@ -0,0 +1,94 @@
|
||||
|
||||
#include "literals.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace Compiler
|
||||
{
|
||||
int Literals::getIntegerSize() const
|
||||
{
|
||||
return mIntegers.size() * sizeof (Interpreter::Type_Integer);
|
||||
}
|
||||
|
||||
int Literals::getFloatSize() const
|
||||
{
|
||||
return mFloats.size() * sizeof (Interpreter::Type_Float);
|
||||
}
|
||||
|
||||
int Literals::getStringSize() const
|
||||
{
|
||||
int size = 0;
|
||||
|
||||
for (std::vector<std::string>::const_iterator iter (mStrings.begin());
|
||||
iter!=mStrings.end(); ++iter)
|
||||
size += static_cast<int> (iter->size()) + 1;
|
||||
|
||||
if (size % 4) // padding
|
||||
size += 4 - size % 4;
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
void Literals::append (std::vector<Interpreter::Type_Code>& code) const
|
||||
{
|
||||
for (std::vector<Interpreter::Type_Integer>::const_iterator iter (mIntegers.begin());
|
||||
iter!=mIntegers.end(); ++iter)
|
||||
code.push_back (*reinterpret_cast<const Interpreter::Type_Code *> (&*iter));
|
||||
|
||||
for (std::vector<Interpreter::Type_Float>::const_iterator iter (mFloats.begin());
|
||||
iter!=mFloats.end(); ++iter)
|
||||
code.push_back (*reinterpret_cast<const Interpreter::Type_Code *> (&*iter));
|
||||
|
||||
int stringBlockSize = getStringSize();
|
||||
int size = static_cast<int> (code.size());
|
||||
|
||||
code.resize (size+stringBlockSize/4);
|
||||
|
||||
int offset = 0;
|
||||
|
||||
for (std::vector<std::string>::const_iterator iter (mStrings.begin());
|
||||
iter!=mStrings.end(); ++iter)
|
||||
{
|
||||
int stringSize = iter->size()+1;
|
||||
|
||||
std::copy (iter->c_str(), iter->c_str()+stringSize,
|
||||
reinterpret_cast<char *> (&code[size]) + offset);
|
||||
offset += stringSize;
|
||||
}
|
||||
}
|
||||
|
||||
int Literals::addInteger (Interpreter::Type_Integer value)
|
||||
{
|
||||
int index = static_cast<int> (mIntegers.size());
|
||||
|
||||
mIntegers.push_back (value);
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
int Literals::addFloat (Interpreter::Type_Float value)
|
||||
{
|
||||
int index = static_cast<int> (mFloats.size());
|
||||
|
||||
mFloats.push_back (value);
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
int Literals::addString (const std::string& value)
|
||||
{
|
||||
int index = static_cast<int> (mStrings.size());
|
||||
|
||||
mStrings.push_back (value);
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
void Literals::clear()
|
||||
{
|
||||
mIntegers.clear();
|
||||
mFloats.clear();
|
||||
mStrings.clear();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,49 @@
|
||||
#ifndef COMPILER_LITERALS_H_INCLUDED
|
||||
#define COMPILER_LITERALS_H_INCLUDED
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <components/interpreter/types.hpp>
|
||||
|
||||
namespace Compiler
|
||||
{
|
||||
/// \brief Literal values.
|
||||
|
||||
class Literals
|
||||
{
|
||||
std::vector<Interpreter::Type_Integer> mIntegers;
|
||||
std::vector<Interpreter::Type_Float> mFloats;
|
||||
std::vector<std::string> mStrings;
|
||||
|
||||
public:
|
||||
|
||||
int getIntegerSize() const;
|
||||
///< Return size of integer block (in bytes).
|
||||
|
||||
int getFloatSize() const;
|
||||
///< Return size of float block (in bytes).
|
||||
|
||||
int getStringSize() const;
|
||||
///< Return size of string block (in bytes).
|
||||
|
||||
void append (std::vector<Interpreter::Type_Code>& code) const;
|
||||
///< Apepnd literal blocks to code.
|
||||
/// \note code blocks will be padded for 32-bit alignment.
|
||||
|
||||
int addInteger (Interpreter::Type_Integer value);
|
||||
///< add integer liternal and return index.
|
||||
|
||||
int addFloat (Interpreter::Type_Float value);
|
||||
///< add float literal and return value.
|
||||
|
||||
int addString (const std::string& value);
|
||||
///< add string literal and return value.
|
||||
|
||||
void clear();
|
||||
///< remove all literals.
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -0,0 +1,74 @@
|
||||
|
||||
#include "output.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
|
||||
#include "locals.hpp"
|
||||
|
||||
namespace Compiler
|
||||
{
|
||||
Output::Output (Locals& locals) : mLocals (locals) {}
|
||||
|
||||
void Output::getCode (std::vector<Interpreter::Type_Code>& code) const
|
||||
{
|
||||
code.clear();
|
||||
|
||||
// header
|
||||
code.push_back (static_cast<Interpreter::Type_Code> (mCode.size()));
|
||||
|
||||
assert (mLiterals.getIntegerSize()%4==0);
|
||||
code.push_back (static_cast<Interpreter::Type_Code> (mLiterals.getIntegerSize()/4));
|
||||
|
||||
assert (mLiterals.getFloatSize()%4==0);
|
||||
code.push_back (static_cast<Interpreter::Type_Code> (mLiterals.getFloatSize()/4));
|
||||
|
||||
assert (mLiterals.getStringSize()%4==0);
|
||||
code.push_back (static_cast<Interpreter::Type_Code> (mLiterals.getStringSize()/4));
|
||||
|
||||
// code
|
||||
std::copy (mCode.begin(), mCode.end(), std::back_inserter (code));
|
||||
|
||||
// literals
|
||||
mLiterals.append (code);
|
||||
}
|
||||
|
||||
const Literals& Output::getLiterals() const
|
||||
{
|
||||
return mLiterals;
|
||||
}
|
||||
|
||||
const std::vector<Interpreter::Type_Code>& Output::getCode() const
|
||||
{
|
||||
return mCode;
|
||||
}
|
||||
|
||||
const Locals& Output::getLocals() const
|
||||
{
|
||||
return mLocals;
|
||||
}
|
||||
|
||||
Literals& Output::getLiterals()
|
||||
{
|
||||
return mLiterals;
|
||||
}
|
||||
|
||||
std::vector<Interpreter::Type_Code>& Output::getCode()
|
||||
{
|
||||
return mCode;
|
||||
}
|
||||
|
||||
Locals& Output::getLocals()
|
||||
{
|
||||
return mLocals;
|
||||
}
|
||||
|
||||
void Output::clear()
|
||||
{
|
||||
mLiterals.clear();
|
||||
mCode.clear();
|
||||
mLocals.clear();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,44 @@
|
||||
#ifndef COMPILER_OUTPUT_H_INCLUDED
|
||||
#define COMPILER_OUTPUT_H_INCLUDED
|
||||
|
||||
#include "literals.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <components/interpreter/types.hpp>
|
||||
|
||||
namespace Compiler
|
||||
{
|
||||
class Locals;
|
||||
|
||||
class Output
|
||||
{
|
||||
Literals mLiterals;
|
||||
std::vector<Interpreter::Type_Code> mCode;
|
||||
Locals& mLocals;
|
||||
|
||||
public:
|
||||
|
||||
Output (Locals& locals);
|
||||
|
||||
void getCode (std::vector<Interpreter::Type_Code>& code) const;
|
||||
///< store generated code in \æ code.
|
||||
|
||||
const Literals& getLiterals() const;
|
||||
|
||||
const Locals& getLocals() const;
|
||||
|
||||
const std::vector<Interpreter::Type_Code>& getCode() const;
|
||||
|
||||
Literals& getLiterals();
|
||||
|
||||
std::vector<Interpreter::Type_Code>& getCode();
|
||||
|
||||
Locals& getLocals();
|
||||
|
||||
void clear();
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue