newrpl/returnstack.c

71 lines
1.4 KiB
C
Raw Normal View History

2014-05-21 19:47:14 +02:00
/*
* Copyright (c) 2014, Claudio Lapilli and the newRPL Team
* All rights reserved.
* This file is released under the 3-clause BSD license.
* See the file LICENSE.txt that shipped with this distribution.
*/
2014-04-28 00:53:11 +02:00
#include "newrpl.h"
#include "hal.h"
// GROW THE RETURN STACK
void growRStk(WORD newtotalsize)
{
WORDPTR *newrstk;
BINT gc_done=0;
do {
newtotalsize=(newtotalsize+1023)&~1023;
newrstk=hal_growmem(RStk,newtotalsize);
if(!newrstk) {
if(!gc_done) { rplGCollect(); ++gc_done; }
else {
Exceptions|=EX_OUTOFMEM;
ExceptionPointer=IPtr;
return;
}
}
} while(!newrstk);
RSTop+=newrstk-RStk;
RStk=newrstk;
RStkSize=newtotalsize;
}
// RETURN STACK ONLY STORES ADDRESS POINTERS
// NEVER DIRECTLY A WORD OR A COMMAND
// STACK IS "INCREASE AFTER" FOR STORE, "DECREASE BEFORE" FOR READ
void rplPushRet(WORDPTR p)
{
// PUSH FIRST (USE THE GUARANTEED SLACK)
// SO THAT IF GROWING THE RETURN STACK TRIGGERS A GC, THE POINTER WILL BE AUTOMATICALLY FIXED
*RSTop++=p;
if(RStkSize<=RSTop-RStk+RSTKSLACK) growRStk((WORD)(RSTop-RStk+RSTKSLACK+1024));
if(Exceptions) return;
}
WORDPTR rplPopRet()
{
if(RSTop<=RStk) {
Exceptions|=EX_EMPTYRSTK;
ExceptionPointer=IPtr;
return 0;
}
return *(--RSTop);
}
// LOW LEVEL VERSION FOR USE IN LIBRARIES
inline WORDPTR rplPeekRet(int level)
{
return (RSTop-level<RStk)? 0: *(RSTop-level);
}