On Monday, 18 September 2006 at  0:06, Dennis Schridde wrote:
> Hi!
> 
> I think we need to have a look at lib/script/interp.c : interpRunScript() 
> very 
> quickly.
> 
> There is this UDWORD * ip which I don't fully understand.
> It doesn't seem to be used like a pointer for a while and then it gets 
> dereferenced. It also gets compared with UDWORD * pCodeEnd.
> 
> Looks very weird to me. Can someone figure out what it does?

ip is a pointer to a compiled (binary) script. The comparison is to see
if the end of the script (allocated memory) has been reached. 

(Note: The size of the three opcodes PUSHLOCAL, POPLOCAL and
PUSHLOCALREF should be entered into the aOpSize array and used instead
of numeric values.)

> And when doing that please remember that the dereferencing in:
> if(!RetStackRemember(CurEvent, *(ip + 2)))    //Remember where to jump back 
> later
> was done by Christian Ohm and _not_ in the original code...

Looking at it again I guess line 942 should be 

UDWORD RetStackRemember(UDWORD EvTrigIndex, UDWORD *address)

instead of

UDWORD RetStackRemember(UDWORD EvTrigIndex, UDWORD address)

so my dereferenciation is wrong. Argh... the joys of C...

In line 332 we want the address of the next instruction, that's
(ip + 2). Then it gets confused... I'll attach a patch to unconfuse
things a bit...

Question: Did this actually work? RetStackRemember increases retStackPos
by 2, but PopRetStack just decreases it by 1.

(Note: Why is RetStackRemember mentioned in script.h? It isn't used
anywhere except in interp.c.)

> Would be nice if someone could explain it's purpose to me,

According to svn blame/log, Troman wrote (some of) that code, so he can
probably tell you where I'm wrong.

-- 
It is a human characteristic to love little animals, especially if
they're attractive in some way.
                -- McCoy, "The Trouble with Tribbles", stardate 4525.6
Index: lib/script/interp.c
===================================================================
--- lib/script/interp.c (revision 361)
+++ lib/script/interp.c (working copy)
@@ -329,7 +329,7 @@
                                case OP_FUNC:
                                        //debug( LOG_SCRIPT, "-OP_FUNC" );
                                        //debug( LOG_SCRIPT, "OP_FUNC: remember 
event %d, ip=%d", CurEvent, (ip + 2) );
-                                       if(!RetStackRemember(CurEvent, *(ip + 
2)))      //Remember where to jump back later
+                                       if(!RetStackRemember(CurEvent, (ip + 
2)))       //Remember where to jump back later
                                        {
                                                debug( LOG_ERROR, 
"interpRunScript() - RetStackRemember() failed.");
                                                return FALSE;
@@ -930,7 +930,13 @@
 
 /* Call stack stuff */
 
-static UDWORD  retStack[200];  //Primitive stack
+typedef struct _retstack_type
+{
+       UDWORD event;
+       UDWORD *address;
+} RETSTACK_TYPE;
+
+static RETSTACK_TYPE   retStack[100];  //Primitive stack
 static SDWORD  retStackPos;    //Current Position, always points to the last 
valid element
 
 void retStackReset(void)
@@ -939,19 +945,18 @@
 }
 
 //Remember current script execution adress
-UDWORD RetStackRemember(UDWORD EvTrigIndex, UDWORD address)
+BOOL RetStackRemember(UDWORD EvTrigIndex, UDWORD *address)
 {
        //DbgMsg("To remember: %d, %d, %d", EvTrigIndex, address, retStackPos);
 
-       if (retStackPos >= 200)
+       if (retStackPos >= 100)
        {
                debug( LOG_ERROR, "RetStackRemember() - return address stack is 
full");
                return FALSE;   //Stack full
        }
 
-       retStackPos = retStackPos + 2;
-       retStack[retStackPos - 1] = EvTrigIndex;        //First store event 
index
-       retStack[retStackPos] = address;                        //current ip
+       retStack[++retStackPos].event = EvTrigIndex;    //First store event 
index
+       retStack[retStackPos].address = address;                        
//current ip
 
        //debug( LOG_SCRIPT, "RetStackRemember: ip=%d, event=%d", address, 
EvTrigIndex);
 
@@ -972,10 +977,8 @@
                return FALSE;
        }
 
-       *psVal = retStack[retStackPos];
+       *psVal = retStack[retStackPos--].address;
 
-       retStackPos = retStackPos - 1;
-
        //debug( LOG_SCRIPT, "PopRetStack: val=%d", *psVal);
 
        return TRUE;
@@ -983,5 +986,5 @@
 
 SDWORD GetCallDepth(void)
 {
-       return (retStackPos + 1) / 2;
+       return (retStackPos + 1);
 }
Index: lib/script/script.h
===================================================================
--- lib/script/script.h (revision 361)
+++ lib/script/script.h (working copy)
@@ -61,7 +61,7 @@
  * Return stack stuff
  */
 extern void retStackReset(void);
-extern UDWORD RetStackRemember(UDWORD EvTrigIndex, UDWORD address);
+extern BOOL RetStackRemember(UDWORD EvTrigIndex, UDWORD *address);
 
 extern BOOL IsRetStackEmpty(void);
 extern BOOL PopRetStack(UDWORD  *psVal);
_______________________________________________
Warzone-dev mailing list
[email protected]
https://mail.gna.org/listinfo/warzone-dev

Reply via email to