Align heap allocation on four byte boundaries

This commit is contained in:
drmortalwombat 2024-08-04 18:02:04 +02:00
parent f49027b2d7
commit 3df85b09fc
4 changed files with 109 additions and 59 deletions

View File

@ -4293,17 +4293,20 @@ __asm malloc
// to store pointer to end of used memory // to store pointer to end of used memory
// in case of heap check we add six bytes to // in case of heap check we add six bytes to
// add some guards // add some guards
// round up to multiple of four bytes to
// keep heap aligned
clc clc
lda accu + 0 lda accu + 0
#ifdef HEAPCHECK #ifdef HEAPCHECK
adc #6 adc #2 + 4 + 4 + 3
#else #else
adc #2 adc #2 + 3
#endif #endif
and #$fc
sta tmp sta tmp
lda accu + 1 lda accu + 1
adc #$00 adc #0
sta tmp + 1 sta tmp + 1
// check if heap is initialized // check if heap is initialized
@ -4315,23 +4318,27 @@ __asm malloc
// set next pointer to null // set next pointer to null
lda #0 lda #0
sta HeapStart + 0 sta HeapStart + 2
sta HeapStart + 1 sta HeapStart + 3
// set size of dummy node to null // set size of dummy node to null
inc HeapNode + 2 inc HeapNode + 2
// set next pointer of dummy node to first free heap block // set next pointer of dummy node to first free heap block
lda #<HeapStart lda #<HeapStart
ora #2
sta HeapNode + 0 sta HeapNode + 0
lda #>HeapStart lda #>HeapStart
sta HeapNode + 1 sta HeapNode + 1
// set end of memory block to end of heap // set end of memory block to end of heap
sec
lda #<HeapEnd lda #<HeapEnd
sta HeapStart + 2 sbc #2
sta HeapStart + 4
lda #>HeapEnd lda #>HeapEnd
sta HeapStart + 3 sbc #0
sta HeapStart + 5
hasHeap: hasHeap:
// remember address of pointer to this // remember address of pointer to this
@ -4392,18 +4399,9 @@ hempty:
avail: avail:
// calculate new end of block // calculate new end of block
clc
lda tmp + 2 lda tmp + 2
#ifdef HEAPCHECK
adc #7
and #$f8
#else
adc #3
and #$fc
#endif
sta tmp + 4 sta tmp + 4
lda tmp + 3 lda tmp + 3
adc #0
sta tmp + 5 sta tmp + 5
// compare with end of free block // compare with end of free block
@ -4469,10 +4467,14 @@ found:
sta (accu), y sta (accu), y
iny iny
sta (accu), y sta (accu), y
iny
sta (accu), y
iny
sta (accu), y
sec sec
lda tmp + 2 lda tmp + 2
sbc #2 sbc #4
sta tmp + 2 sta tmp + 2
bcs hc1 bcs hc1
dec tmp + 3 dec tmp + 3
@ -4482,16 +4484,24 @@ hc1:
sta (tmp + 2), y sta (tmp + 2), y
iny iny
sta (tmp + 2), y sta (tmp + 2), y
iny
sta (tmp + 2), y
iny
sta (tmp + 2), y
clc
lda accu lda accu
ora #4 adc #6
sta accu
#else #else
// advanve by two bytes to skip size // advance by two bytes to skip size
clc
lda accu lda accu
ora #2 adc #2
sta accu
#endif #endif
sta accu
bcc hc2
inc accu + 1
hc2:
rts rts
} }
@ -4508,39 +4518,50 @@ __asm inp_malloc
__asm free __asm free
{ {
// two bytes back to fix remembered end of block // check nullptr free
lda accu lda accu
#ifdef HEAPCHECK
ora accu + 1 ora accu + 1
bne hcnn bne notnull
rts rts
hcnn: notnull:
// two bytes back to fix remembered end of block
sec
lda accu lda accu
and #$07 #ifdef HEAPCHECK
cmp #$04 and #3
bne hfail bne hfail
lda accu lda accu
and #$f8 sbc #6
#else #else
and #$fc sbc #2
#endif #endif
sta accu sta accu
bcs fc1
dec accu + 1
fc1:
#ifdef HEAPCHECK #ifdef HEAPCHECK
ldy #2 ldy #2
lda (accu), y lda #$bd
cmp #$bd cmp (accu), y
bne hfail bne hfail
iny iny
lda (accu), y cmp (accu), y
cmp #$bd bne hfail
iny
cmp (accu), y
bne hfail
iny
cmp (accu), y
bne hfail bne hfail
ldy #0 ldy #0
sec sec
lda (accu), y lda (accu), y
sbc #2 sbc #4
sta tmp + 0 sta tmp + 0
iny iny
lda (accu), y lda (accu), y
@ -4548,24 +4569,20 @@ hcnn:
sta tmp + 1 sta tmp + 1
ldy #0 ldy #0
lda (tmp), y lda #$be
cmp #$be cmp (tmp), y
bne hfail bne hfail
iny iny
lda (tmp), y cmp (tmp), y
cmp #$be bne hfail
iny
cmp (tmp), y
bne hfail
iny
cmp (tmp), y
bne hfail bne hfail
lda accu
#endif #endif
// check nullptr free
ora accu + 1
bne notnull
rts
notnull:
#ifdef HEAPCHECK #ifdef HEAPCHECK
lda accu + 1 lda accu + 1
ldx accu ldx accu
@ -4593,20 +4610,11 @@ hchk2:
// cache end of block, rounding to next four byte // cache end of block, rounding to next four byte
// address // address
clc
ldy #0 ldy #0
lda (accu), y lda (accu), y
#ifdef HEAPCHECK
adc #7
and #$f8
#else
adc #3
and #$fc
#endif
sta accu + 2 sta accu + 2
iny iny
lda (accu), y lda (accu), y
adc #0
sta accu + 3 sta accu + 3
// pointer to heap block, starting with // pointer to heap block, starting with

View File

@ -21761,7 +21761,7 @@ void InterCodeProcedure::Close(void)
{ {
GrowingTypeArray tstack(IT_NONE); GrowingTypeArray tstack(IT_NONE);
CheckFunc = !strcmp(mIdent->mString, "draw"); CheckFunc = !strcmp(mIdent->mString, "shots_move");
CheckCase = false; CheckCase = false;
mEntryBlock = mBlocks[0]; mEntryBlock = mBlocks[0];
@ -22664,6 +22664,8 @@ void InterCodeProcedure::Close(void)
RemoveUnusedPartialStoreInstructions(); RemoveUnusedPartialStoreInstructions();
PeepholeOptimization();
MapVariables(); MapVariables();
DisassembleDebug("mapped variabled"); DisassembleDebug("mapped variabled");

View File

@ -14204,6 +14204,29 @@ NativeCodeBasicBlock* NativeCodeBasicBlock::ForwardAccuBranch(bool eq, bool ne,
} }
void NativeCodeBasicBlock::RemoveJumpToBranch(void)
{
if (!mVisited)
{
mVisited = true;
if (mTrueJump) mTrueJump->RemoveJumpToBranch();
if (mFalseJump) mFalseJump->RemoveJumpToBranch();
if (mTrueJump && !mFalseJump && mTrueJump != this && mTrueJump->mIns.Size() == 0)
{
mTrueJump->mNumEntries--;
mBranch = mTrueJump->mBranch;
mFalseJump = mTrueJump->mFalseJump;
mTrueJump = mTrueJump->mTrueJump;
if (mTrueJump)
mTrueJump->mNumEntries++;
if (mFalseJump)
mFalseJump->mNumEntries++;
}
}
}
bool NativeCodeBasicBlock::MergeBasicBlocks(void) bool NativeCodeBasicBlock::MergeBasicBlocks(void)
{ {
bool changed = false; bool changed = false;
@ -49904,6 +49927,12 @@ bool NativeCodeBasicBlock::CalculateOffset(int& total)
total += BranchByteSize(mTrueJump, total, mTrueJump->mOffset); total += BranchByteSize(mTrueJump, total, mTrueJump->mOffset);
else if (mTrueJump->mPlace == mPlace + 1) else if (mTrueJump->mPlace == mPlace + 1)
total += BranchByteSize(mFalseJump, total, mFalseJump->mOffset); total += BranchByteSize(mFalseJump, total, mFalseJump->mOffset);
else if (mFalseJump->mPlace < mPlace && mTrueJump->mPlace < mPlace && mPlace - mTrueJump->mPlace < 126 &&
mFalseJump->mIns.Size() == 1 && mFalseJump->mIns[0].mType == ASMIT_RTS)
{
total += BranchByteSize(mTrueJump, total, mTrueJump->mOffset);
total += JumpByteSize(mFalseJump, mFalseJump->mOffset - total);
}
else if ( else if (
mFalseJump->mPlace > mTrueJump->mPlace && mFalseJump->mPlace < mPlace || mFalseJump->mPlace > mTrueJump->mPlace && mFalseJump->mPlace < mPlace ||
mFalseJump->mPlace < mTrueJump->mPlace && mFalseJump->mPlace > mPlace) mFalseJump->mPlace < mTrueJump->mPlace && mFalseJump->mPlace > mPlace)
@ -50036,6 +50065,12 @@ void NativeCodeBasicBlock::CopyCode(NativeCodeProcedure * proc, uint8* target)
end += PutBranch(proc, mTrueJump, mBranch, mTrueJump->mOffset - end); end += PutBranch(proc, mTrueJump, mBranch, mTrueJump->mOffset - end);
else if (mTrueJump->mPlace == mPlace + 1) else if (mTrueJump->mPlace == mPlace + 1)
end += PutBranch(proc, mFalseJump, InvertBranchCondition(mBranch), mFalseJump->mOffset - end); end += PutBranch(proc, mFalseJump, InvertBranchCondition(mBranch), mFalseJump->mOffset - end);
else if (mFalseJump->mPlace < mPlace && mTrueJump->mPlace < mPlace && mPlace - mTrueJump->mPlace < 126 &&
mFalseJump->mIns.Size() == 1 && mFalseJump->mIns[0].mType == ASMIT_RTS)
{
end += PutBranch(proc, mTrueJump, mBranch, mTrueJump->mOffset - end);
end += PutJump(proc, mFalseJump, mFalseJump->mOffset - end);
}
else if ( else if (
mFalseJump->mPlace > mTrueJump->mPlace && mFalseJump->mPlace < mPlace || mFalseJump->mPlace > mTrueJump->mPlace && mFalseJump->mPlace < mPlace ||
mFalseJump->mPlace < mTrueJump->mPlace && mFalseJump->mPlace > mPlace) mFalseJump->mPlace < mTrueJump->mPlace && mFalseJump->mPlace > mPlace)
@ -50338,7 +50373,7 @@ void NativeCodeProcedure::Compile(InterCodeProcedure* proc)
mInterProc = proc; mInterProc = proc;
mInterProc->mLinkerObject->mNativeProc = this; mInterProc->mLinkerObject->mNativeProc = this;
CheckFunc = !strcmp(mInterProc->mIdent->mString, "shots_clear"); CheckFunc = !strcmp(mInterProc->mIdent->mString, "opp::vector<struct opp::pair<i16,i16>>::reserve");
int nblocks = proc->mBlocks.Size(); int nblocks = proc->mBlocks.Size();
tblocks = new NativeCodeBasicBlock * [nblocks]; tblocks = new NativeCodeBasicBlock * [nblocks];
@ -52152,6 +52187,9 @@ void NativeCodeProcedure::Optimize(void)
ResetVisited(); ResetVisited();
mEntryBlock->RemoveUnusedResultInstructions(); mEntryBlock->RemoveUnusedResultInstructions();
ResetVisited();
mEntryBlock->RemoveJumpToBranch();
#if 1 #if 1
ResetVisited(); ResetVisited();
data.Reset(); data.Reset();

View File

@ -419,6 +419,8 @@ public:
void CountEntries(NativeCodeBasicBlock* fromJump); void CountEntries(NativeCodeBasicBlock* fromJump);
NativeCodeBasicBlock * ForwardAccuBranch(bool eq, bool ne, bool pl, bool mi, int limit); NativeCodeBasicBlock * ForwardAccuBranch(bool eq, bool ne, bool pl, bool mi, int limit);
bool MergeBasicBlocks(void); bool MergeBasicBlocks(void);
void RemoveJumpToBranch(void);
void MarkLoopHead(void); void MarkLoopHead(void);
struct DominatorStacks struct DominatorStacks