Improve handling of single member structs

This commit is contained in:
drmortalwombat 2023-10-15 20:17:04 +02:00
parent 454c4f5dbe
commit 7858e32d12
6 changed files with 439 additions and 55 deletions

80
include/opp/slab.h Normal file
View File

@ -0,0 +1,80 @@
#ifndef OPP_SLAB_H
#define OPP_SLAB_H
template<class T, int N>
class slabptr
{
public:
char index;
slabptr(void)
: index(N)
{}
slabptr(char i)
: index(i)
{}
slabptr(const slabptr & i)
: index(i.index)
{}
auto operator-> ();
auto & operator* ();
};
template <class T, int N>
class slab
{
protected:
static __striped T buffer[N];
static char head;
static char next[N];
public:
typedef slabptr<T, N> ptr;
static void init(void);
static auto alloc(void);
static void free(ptr p);
};
template<class T, int N>
inline auto slabptr<T, N>::operator-> ()
{
return slab<T, N>::buffer + index;
}
template<class T, int N>
inline auto & slabptr<T, N>::operator* ()
{
return slab<T, N>::buffer[index];
}
template <class T, int N>
void slab<T, N>::init(void)
{
head = 0;
for(char i=0; i<N; i++)
next[i] = i + 1;
}
template <class T, int N>
auto slab<T, N>::alloc(void)
{
char i = head;
head = next[head];
return slabptr<T, N>(i);
}
template <class T, int N>
void slab<T, N>::free(slabptr<T, N> p)
{
next[p.index] = head;
head = p.index;
}
#endif

258
include/opp/static_vector.h Normal file
View File

@ -0,0 +1,258 @@
#ifndef OPP_STATIC_VECTOR_H
#define OPP_STATIC_VECTOR_H
#include <new>
#include <stdlib.h>
#include <opp/utility.h>
namespace opp {
template <class T, int N>
class static_vector
{
protected:
char _space[N * sizeof(T)];
enum { m = N } _size;
public:
typedef T element_type;
static_vector(void) : _size(0) {}
static_vector(size_t n) : _size(n)
{
T * data = (T*)_space;
for(size_t i=0; i<n; i++)
new (data + i) T;
}
static_vector(const static_vector & v)
: _size(v._size)
{
T * data = (T*)_space, * vdata = (T*)(v._space);
for(size_t i=0; i<_size; i++)
new (data + i)T(vdata[i]);
}
~static_vector(void)
{
T * data = (T*)_space;
for(size_t i=0; i<_size; i++)
data[i].~T();
}
static_vector & operator=(const static_vector & v)
{
if (this != &v)
{
T * data = (T*)_space, * vdata = (T*)(v._space);
for(size_t i=0; i<_size; i++)
data[i].~T();
_size = v._size;
for(size_t i=0; i<_size; i++)
new (data + i)T(vdata[i]);
}
return *this;
}
size_t size(void) const
{
return _size;
}
size_t max_size(void) const
{
return N;
}
bool empty(void) const
{
return _size == 0;
}
size_t capacity(void) const
{
return N;
}
void resize(size_t n);
T & at(size_t at)
{
return ((T*)_space)[at];
}
const T & at(size_t at) const
{
return ((T*)_space)[at];
}
T & operator[](size_t at)
{
return ((T*)_space)[at];
}
const T & operator[](size_t at) const
{
return ((T*)_space)[at];
}
T * begin(void)
{
return (T*)_space;
}
const T * begin(void) const
{
return (T*)_space;
}
const T * cbegin(void) const
{
return (T*)_space;
}
T * end(void)
{
return (T*)_space + _size;
}
const T * end(void) const
{
return (T*)_space + _size;
}
const T * cend(void) const
{
return (T*)_space + _size;
}
T & front(void)
{
return ((T*)_space)[0];
}
const T & front(void) const
{
return ((T*)_space)[0];
}
T & back(void)
{
return ((T*)_space)[_size - 1];
}
const T & back(void) const
{
return ((T*)_space)[_size - 1];
}
T * data(void)
{
return (T*)_space;
}
const T * at(void) const
{
return (T*)_space;
}
void push_back(const T & t);
void push_back(T && t);
void pop_back(void)
{
_size--;
((T*)_space)[_size].~T();
}
void insert(size_t at, const T & t);
void erase(size_t at, size_t n = 1);
T * insert(T * at, const T & t);
template <typename ...P>
void emplace_back(const P&... p);
};
template <class T, int N>
void static_vector<T, N>::resize(size_t n)
{
T * data = (T*)_space;
if (n < _size)
{
for(size_t i=n; i<_size; i++)
data[i].~T();
_size = n;
}
else
{
for(size_t i=_size; i<n; i++)
new(data + i)T;
_size = n;
}
}
template <class T, int N>
void static_vector<T, N>::push_back(const T & t)
{
new ((T*)_space + _size++)T(t);
}
template <class T, int N>
void static_vector<T, N>::push_back(T && t)
{
new ((T*)_space + _size++)T(t);
}
template <class T, int N>
template <typename ...P>
void static_vector<T, N>::emplace_back(const P&... p)
{
new ((T*)_space + _size++)T(p...);
}
template <class T, int N>
void static_vector<T, N>::insert(size_t at, const T & t)
{
T * data = (T*)_space;
new (data + _size)T;
for(size_t i=_size; i>at; i--)
data[i] = move(data[i - 1]);
data[at] = t;
_size++;
}
template <class T, int N>
void static_vector<T, N>::erase(size_t at, size_t n)
{
T * data = (T*)_space;
_size -= n;
for(size_t i=at; i<_size; i++)
data[i] = move(data[i + n]);
for(size_t i=0; i<n; i++)
data[_size + i].~T();
}
template <class T, int N>
T * static_vector<T, N>::insert(T * at, const T & t)
{
T * data = (T*)_space;
T * dp = data + _size;
new (dp)T;
while (dp != at)
{
dp--;
dp[1] = move(dp[0]);
}
dp[0] = t;
_size++;
return dp + 1;
}
}
#endif

View File

@ -16173,6 +16173,35 @@ bool InterCodeBasicBlock::PeepholeReplaceOptimization(const GrowingVariableArray
changed = true;
}
}
#endif
#if 1
if (i + 1 < mInstructions.Size())
{
if (mInstructions[i + 0]->mCode == IC_LEA && mInstructions[i + 0]->mSrc[1].mTemp < 0 &&
mInstructions[i + 1]->mCode == IC_RELATIONAL_OPERATOR && mInstructions[i + 1]->mSrc[1].mTemp == mInstructions[i + 0]->mDst.mTemp && mInstructions[i + 1]->mSrc[0].mTemp < 0 &&
mInstructions[i + 1]->mSrc[0].mMemory == IM_ABSOLUTE && mInstructions[i + 1]->mSrc[0].mIntConst == 0)
{
if (mInstructions[i + 0]->mSrc[1].mMemory != IM_ABSOLUTE)
{
if (mInstructions[i + 1]->mOperator == IA_CMPEQ)
{
mInstructions[i + 1]->mNumOperands = 0;
mInstructions[i + 1]->mCode = IC_CONSTANT;
mInstructions[i + 1]->mConst.mType = IT_BOOL;
mInstructions[i + 1]->mConst.mIntConst = 0;
changed = true;
}
else if (mInstructions[i + 1]->mOperator == IA_CMPNE)
{
mInstructions[i + 1]->mNumOperands = 0;
mInstructions[i + 1]->mCode = IC_CONSTANT;
mInstructions[i + 1]->mConst.mType = IT_BOOL;
mInstructions[i + 1]->mConst.mIntConst = 1;
changed = true;
}
}
}
}
#endif
}

View File

@ -1252,24 +1252,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateInline(Declaration* pro
mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not an addressable expression");
if (vp.mTemp != vr.mTemp)
{
InterInstruction* cins = new InterInstruction(exp->mLocation, IC_COPY);
cins->mNumOperands = 2;
cins->mSrc[0].mType = IT_POINTER;
cins->mSrc[0].mTemp = vr.mTemp;
cins->mSrc[0].mMemory = IM_INDIRECT;
cins->mSrc[0].mOperandSize = vp.mType->mSize;
cins->mSrc[0].mStride = vr.mType->mStripe;
cins->mSrc[1].mType = IT_POINTER;
cins->mSrc[1].mTemp = ains->mDst.mTemp;
cins->mSrc[1].mMemory = IM_INDIRECT;
cins->mSrc[1].mOperandSize = vp.mType->mSize;
cins->mConst.mOperandSize = vp.mType->mSize;
block->Append(cins);
}
CopyStructSimple(proc, exp, block, vp, vr);
}
else
{
@ -1390,6 +1373,73 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateInline(Declaration* pro
return ExValue(TheVoidTypeDeclaration);
}
void InterCodeGenerator::CopyStructSimple(InterCodeProcedure* proc, Expression * exp, InterCodeBasicBlock* block, ExValue vl, ExValue vr)
{
int ne = 0;
Declaration* mdec = nullptr;
if (vl.mType->mType == DT_TYPE_STRUCT)
{
Declaration* dec = vl.mType->mParams;
while (dec)
{
if (dec->mType == DT_ELEMENT && !(dec->mFlags & DTF_STATIC))
{
mdec = dec->mBase;
ne++;
}
dec = dec->mNext;
}
}
// Single element structs are copied as individual value
if (ne == 1 && mdec->mSize == vl.mType->mSize)
{
InterInstruction* lins = new InterInstruction(exp->mLocation, IC_LOAD);
lins->mNumOperands = 1;
lins->mSrc[0].mType = IT_POINTER;
lins->mSrc[0].mTemp = vr.mTemp;
lins->mSrc[0].mMemory = IM_INDIRECT;
lins->mSrc[0].mOperandSize = mdec->mSize;
lins->mSrc[0].mStride = mdec->mStripe;
lins->mDst.mType = InterTypeOf(mdec);
lins->mDst.mTemp = proc->AddTemporary(lins->mDst.mType);
block->Append(lins);
InterInstruction* sins = new InterInstruction(exp->mLocation, IC_STORE);
sins->mNumOperands = 2;
sins->mSrc[1].mType = IT_POINTER;
sins->mSrc[1].mTemp = vl.mTemp;
sins->mSrc[1].mMemory = IM_INDIRECT;
sins->mSrc[1].mOperandSize = mdec->mSize;
sins->mSrc[1].mStride = mdec->mStripe;
sins->mSrc[0] = lins->mDst;
block->Append(sins);
}
else
{
InterInstruction* cins = new InterInstruction(exp->mLocation, IC_COPY);
cins->mNumOperands = 2;
cins->mSrc[0].mType = IT_POINTER;
cins->mSrc[0].mTemp = vr.mTemp;
cins->mSrc[0].mMemory = IM_INDIRECT;
cins->mSrc[0].mOperandSize = vr.mType->mSize;
cins->mSrc[0].mStride = vr.mType->mStripe;
cins->mSrc[1].mOperandSize = vl.mType->mSize;
cins->mSrc[1].mType = IT_POINTER;
cins->mSrc[1].mTemp = vl.mTemp;
cins->mSrc[1].mMemory = IM_INDIRECT;
cins->mConst.mOperandSize = vl.mType->mSize;
block->Append(cins);
}
}
void InterCodeGenerator::CopyStruct(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue vl, ExValue vr, InlineMapper* inlineMapper, bool moving)
{
if (vr.mTemp == vl.mTemp)
@ -1587,24 +1637,7 @@ void InterCodeGenerator::CopyStruct(InterCodeProcedure* proc, Expression* exp, I
}
}
else
{
InterInstruction* cins = new InterInstruction(exp->mLocation, IC_COPY);
cins->mNumOperands = 2;
cins->mSrc[0].mType = IT_POINTER;
cins->mSrc[0].mTemp = vr.mTemp;
cins->mSrc[0].mMemory = IM_INDIRECT;
cins->mSrc[0].mOperandSize = vr.mType->mSize;
cins->mSrc[0].mStride = vr.mType->mStripe;
cins->mSrc[1].mOperandSize = vl.mType->mSize;
cins->mSrc[1].mType = IT_POINTER;
cins->mSrc[1].mTemp = vl.mTemp;
cins->mSrc[1].mMemory = IM_INDIRECT;
cins->mConst.mOperandSize = vl.mType->mSize;
block->Append(cins);
}
CopyStructSimple(proc, exp, block, vl, vr);
}
InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock*& block, Expression* exp, DestructStack*& destack, const BranchTarget& breakBlock, const BranchTarget& continueBlock, InlineMapper* inlineMapper, ExValue* lrexp)
@ -2104,24 +2137,7 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
mErrors->Error(exp->mLeft->mLocation, EERR_NOT_AN_LVALUE, "Not an addressable expression");
if (vr.mTemp != vl.mTemp)
{
InterInstruction* ins = new InterInstruction(exp->mLocation, IC_COPY);
ins->mNumOperands = 2;
ins->mSrc[0].mType = IT_POINTER;
ins->mSrc[0].mTemp = vr.mTemp;
ins->mSrc[0].mMemory = IM_INDIRECT;
ins->mSrc[0].mOperandSize = vl.mType->mSize;
ins->mSrc[0].mStride = vr.mType->mStripe;
ins->mSrc[1].mType = IT_POINTER;
ins->mSrc[1].mTemp = vl.mTemp;
ins->mSrc[1].mMemory = IM_INDIRECT;
ins->mSrc[1].mOperandSize = vl.mType->mSize;
ins->mSrc[1].mStride = vl.mType->mStripe;
ins->mConst.mOperandSize = vl.mType->mSize;
block->Append(ins);
}
CopyStructSimple(proc, exp, block, vl, vr);
}
else
{

View File

@ -87,6 +87,7 @@ protected:
void TranslateLogic(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock* block, InterCodeBasicBlock* tblock, InterCodeBasicBlock* fblock, Expression* exp, DestructStack*& destack, InlineMapper* inlineMapper);
ExValue TranslateInline(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock*& block, Expression* exp, const BranchTarget& breakBlock, const BranchTarget& continueBlock, InlineMapper* inlineMapper, bool inlineConstexpr, ExValue* lrexp);
void CopyStruct(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue vl, ExValue vr, InlineMapper* inlineMapper, bool moving);
void CopyStructSimple(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock * block, ExValue vl, ExValue vr);
void StoreValue(InterCodeProcedure* proc, Expression* exp, InterCodeBasicBlock*& block, ExValue vl, ExValue vr);
void UnwindDestructStack(Declaration* procType, InterCodeProcedure* proc, InterCodeBasicBlock*& block, DestructStack* stack, DestructStack * bottom, InlineMapper* inlineMapper);

View File

@ -5334,7 +5334,7 @@ Expression* Parser::ParseSimpleExpression(bool lhs)
if (dec)
{
if (dec->mType == DT_ELEMENT || dec->mType == DT_CONST_FUNCTION)
if ((dec->mType == DT_ELEMENT || dec->mType == DT_CONST_FUNCTION) && !(dec->mFlags & DTF_STATIC))
{
Expression* texp = new Expression(mScanner->mLocation, EX_VARIABLE);
texp->mDecType = mThisPointer->mBase;