#include "context.hh"
#include "slghsymbol.hh"
#include "translate.hh"
ParserContext::ParserContext(ContextCache *ccache)
{
parsestate = 0;
contcache = ccache;
if (ccache != (ContextCache *)0) {
contextsize = ccache->getDatabase()->getContextSize();
context = new uintm[ contextsize ];
}
else {
contextsize = 0;
context = (uintm *)0;
}
}
void ParserContext::initialize(int4 maxstate,int4 maxparam,AddrSpace *spc)
{
const_space = spc;
state.resize(maxstate);
state[0].parent = (ConstructState *)0;
for(int4 i=0;i<maxstate;++i)
state[i].resolve.resize(maxparam);
base_state = &state[0];
}
uintm ParserContext::getInstructionBytes(int4 bytestart,int4 size,uint4 off) const
{ off += bytestart;
if (off >=16)
throw BadDataError("Instruction is using more than 16 bytes");
const uint1 *ptr = buf + off;
uintm res = 0;
for(int4 i=0;i<size;++i) {
res <<= 8;
res |= ptr[i];
}
return res;
}
uintm ParserContext::getInstructionBits(int4 startbit,int4 size,uint4 off) const
{
off += (startbit/8);
if (off >= 16)
throw BadDataError("Instruction is using more than 16 bytes");
const uint1 *ptr = buf + off;
startbit = startbit % 8;
int4 bytesize = (startbit+size-1)/8 + 1;
uintm res = 0;
for(int4 i=0;i<bytesize;++i) {
res <<= 8;
res |= ptr[i];
}
res <<= 8*(sizeof(uintm)-bytesize)+startbit; res >>= 8*sizeof(uintm)-size; return res;
}
uintm ParserContext::getContextBytes(int4 bytestart,int4 size) const
{ int4 intstart = bytestart / sizeof(uintm);
uintm res = context[ intstart ];
int4 byteOffset = bytestart % sizeof(uintm);
int4 unusedBytes = sizeof(uintm) - size;
res <<= byteOffset*8;
res >>= unusedBytes*8;
int4 remaining = size - sizeof(uintm) + byteOffset;
if ((remaining > 0)&&(++intstart < contextsize)) { uintm res2 = context[ intstart ];
unusedBytes = sizeof(uintm) - remaining;
res2 >>= unusedBytes * 8;
res |= res2;
}
return res;
}
uintm ParserContext::getContextBits(int4 startbit,int4 size) const
{
int4 intstart = startbit / (8*sizeof(uintm));
uintm res = context[ intstart ]; int4 bitOffset = startbit % (8*sizeof(uintm));
int4 unusedBits = 8*sizeof(uintm) - size;
res <<= bitOffset; res >>= unusedBits;
int4 remaining = size - 8*sizeof(uintm) + bitOffset;
if ((remaining > 0) && (++intstart < contextsize)) {
uintm res2 = context[ intstart ];
unusedBits = 8*sizeof(uintm) - remaining;
res2 >>= unusedBits;
res |= res2;
}
return res;
}
void ParserContext::addCommit(TripleSymbol *sym,int4 num,uintm mask,bool flow,ConstructState *point)
{
contextcommit.emplace_back();
ContextSet &set(contextcommit.back());
set.sym = sym;
set.point = point; set.num = num;
set.mask = mask;
set.value = context[num] & mask;
set.flow = flow;
}
void ParserContext::applyCommits(void)
{
if (contextcommit.empty()) return;
ParserWalker walker(this);
walker.baseState();
vector<ContextSet>::iterator iter;
for(iter=contextcommit.begin();iter!=contextcommit.end();++iter) {
TripleSymbol *sym = (*iter).sym;
Address commitaddr;
if (sym->getType() == SleighSymbol::operand_symbol) {
int4 i = ((OperandSymbol *)sym)->getIndex();
FixedHandle &h((*iter).point->resolve[i]->hand);
commitaddr = Address(h.space,h.offset_offset);
}
else {
FixedHandle hand;
sym->getFixedHandle(hand,walker);
commitaddr = Address(hand.space,hand.offset_offset);
}
if (commitaddr.isConstant()) {
uintb newoff = AddrSpace::addressToByte(commitaddr.getOffset(),addr.getSpace()->getWordSize());
commitaddr = Address(addr.getSpace(),newoff);
}
if ((*iter).flow) contcache->setContext(commitaddr,(*iter).num,(*iter).mask,(*iter).value);
else { Address nextaddr = commitaddr + 1;
if (nextaddr.getOffset() < commitaddr.getOffset())
contcache->setContext(commitaddr,(*iter).num,(*iter).mask,(*iter).value);
else
contcache->setContext(commitaddr,nextaddr,(*iter).num,(*iter).mask,(*iter).value);
}
}
}
void ParserWalker::setOutOfBandState(Constructor *ct,int4 index,ConstructState *tempstate,const ParserWalker &otherwalker)
{ const ConstructState *pt = otherwalker.point;
int4 curdepth = otherwalker.depth;
while(pt->ct != ct) {
if (curdepth <= 0) return;
curdepth -= 1;
pt = pt->parent;
}
OperandSymbol *sym = ct->getOperand(index);
int4 i = sym->getOffsetBase();
if (i<0)
tempstate->offset = pt->offset + sym->getRelativeOffset();
else
tempstate->offset = pt->resolve[index]->offset;
tempstate->ct = ct;
tempstate->length = pt->length;
point = tempstate;
depth = 0;
breadcrumb[0] = 0;
}
void ParserWalkerChange::calcCurrentLength(int4 length,int4 numopers)
{ length += point->offset; for(int4 i=0;i<numopers;++i) {
ConstructState *subpoint = point->resolve[i];
int4 sublength = subpoint->length + subpoint->offset;
if (sublength > length) length = sublength;
}
point->length = length - point->offset; }