|
|
|
@ -73,9 +73,57 @@ VOID NcConcat(PNATIVE_CODE_BLOCK Block1, PNATIVE_CODE_BLOCK Block2)
|
|
|
|
|
//update the label names so that there are no conflicts between the two blocks
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ULONG NcGenUnusedLabelId(STDVECTOR<ULONG> CONST& LabelIds)
|
|
|
|
|
{
|
|
|
|
|
ULONG ReturnLabelId = rand();
|
|
|
|
|
while (StdFind(LabelIds.begin(), LabelIds.end(), ReturnLabelId) != LabelIds.end())
|
|
|
|
|
ReturnLabelId = rand();
|
|
|
|
|
return ReturnLabelId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VOID NcChangeLabelId(PNATIVE_CODE_BLOCK Block1, ULONG Original, ULONG New)
|
|
|
|
|
{
|
|
|
|
|
for (PNATIVE_CODE_LINK T = Block1->Start; T; T = T->Next)
|
|
|
|
|
{
|
|
|
|
|
if ((T->Flags & CODE_FLAG_IS_LABEL) && T->Label == Original)
|
|
|
|
|
T->Label = New;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VOID NcFixLabelsForBlocks(PNATIVE_CODE_BLOCK Block1, PNATIVE_CODE_BLOCK Block2)
|
|
|
|
|
{
|
|
|
|
|
STDVECTOR<ULONG> BlockOneLabels;
|
|
|
|
|
for (PNATIVE_CODE_LINK T = Block1->Start; T; T = T->Next)
|
|
|
|
|
{
|
|
|
|
|
if ((T->Flags & CODE_FLAG_IS_LABEL) && StdFind(BlockOneLabels.begin(), BlockOneLabels.end(), T->Label) != BlockOneLabels.end())
|
|
|
|
|
BlockOneLabels.push_back(T->Label);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (PNATIVE_CODE_LINK T = Block2->Start; T; T = T->Next)
|
|
|
|
|
{
|
|
|
|
|
if ((T->Flags & CODE_FLAG_IS_LABEL) && StdFind(BlockOneLabels.begin(), BlockOneLabels.end(), T->Label) != BlockOneLabels.end())
|
|
|
|
|
NcChangeLabelId(Block2, T->Label, NcGenUnusedLabelId(BlockOneLabels));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOL NcInsertBlockAfter(PNATIVE_CODE_LINK Link, PNATIVE_CODE_BLOCK Block)
|
|
|
|
|
{
|
|
|
|
|
return FALSE;
|
|
|
|
|
if (!Link || !Link->Block || !Block || !Block->Start || !Block->End || Link->Block == Block)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
if (Block->HasRelativeJumps && Link->Block->HasRelativeJumps)
|
|
|
|
|
NcFixLabelsForBlocks(Link->Block, Block);
|
|
|
|
|
|
|
|
|
|
if (Link->Next)
|
|
|
|
|
Link->Next->Prev = Block->End;
|
|
|
|
|
Block->End->Next = Link->Next;
|
|
|
|
|
Block->Start->Prev = Link;
|
|
|
|
|
Link->Next = Block->Start;
|
|
|
|
|
|
|
|
|
|
for (PNATIVE_CODE_LINK T = Block->Start; T; T = T->Next)
|
|
|
|
|
T->Block = Link->Block;
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOL NcInsertBlockBefore(PNATIVE_CODE_LINK Link, PNATIVE_CODE_BLOCK Block)
|
|
|
|
@ -84,24 +132,16 @@ BOOL NcInsertBlockBefore(PNATIVE_CODE_LINK Link, PNATIVE_CODE_BLOCK Block)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
if (Block->HasRelativeJumps && Link->Block->HasRelativeJumps)
|
|
|
|
|
{
|
|
|
|
|
//TODO: increment all labels inside of the block being added
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (Link->Prev)
|
|
|
|
|
Link->Prev->Next = Block->Start;
|
|
|
|
|
Block->Start->Prev = Link->Prev;
|
|
|
|
|
|
|
|
|
|
Block->End->Next = Link;
|
|
|
|
|
Link->Prev = Block->End;
|
|
|
|
|
}
|
|
|
|
|
NcFixLabelsForBlocks(Link->Block, Block);
|
|
|
|
|
|
|
|
|
|
if (Link->Prev)
|
|
|
|
|
Link->Prev->Next = Block->Start;
|
|
|
|
|
Block->Start->Prev = Link->Prev;
|
|
|
|
|
Block->End->Next = Link;
|
|
|
|
|
Link->Prev = Block->End;
|
|
|
|
|
|
|
|
|
|
for (PNATIVE_CODE_LINK T = Block->Start; T; T = T->Next)
|
|
|
|
|
{
|
|
|
|
|
T->Block = Link->Block;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|