dos.library: add special handling for ACTION_PARENT_FH on NIL: handler
[AROS.git] / compiler / purify / src / stack.c
blob10923cff7aff686deb28688f4093ae51e9cd26d4
1 #include <stdio.h>
2 #include <assert.h>
3 #include "debug.h"
4 #include "hash.h"
6 static MemHash * Purify_CurrentStackNode;
8 void Purify_InitStack (char * stackBase, long stackSize)
10 MemHash * node;
11 char ** argv;
12 int argc, t;
13 long offset;
15 stackBase +=
16 4 /* two arguments */
17 +4 /* return address for main() */
18 +8 /* two arguments for main() */
21 #if DEBUG
22 printf ("Stack goes from %p to %p\n", stackBase - stackSize, stackBase);
23 #endif
25 node = Purify_AddMemory (stackBase - stackSize
26 , stackSize
27 , PURIFY_MemFlag_Empty
28 , PURIFY_MemType_Stack
31 node->data = "Stack";
33 Purify_CurrentStackNode = node;
35 /* Major hack :-/ */
36 argv = ((char ***)stackBase)[-1];
37 argc = ((int *)stackBase)[-2];
39 #if 0
40 printf ("&argc=%p\n", &((int *)stackBase)[-2]);
41 printf ("argc=%d argv=%p\n", argc, argv);
42 #endif
44 offset = (long)(&((int *)stackBase)[-2]) - (long)(node->mem);
46 Purify_SetMemoryFlags (node, offset, 8,
47 PURIFY_MemFlag_Readable|PURIFY_MemFlag_Writable
50 node = Purify_AddMemory (argv
51 , (argc+1) * sizeof (char *)
52 , PURIFY_MemFlag_Readable|PURIFY_MemFlag_Writable
53 , PURIFY_MemType_Stack
56 node->data = "argv[] array";
58 for (t=0; t<argc; t++)
60 node = Purify_AddMemory (argv[t]
61 , strlen (argv[t]) + 1
62 , PURIFY_MemFlag_Readable|PURIFY_MemFlag_Writable
63 , PURIFY_MemType_Stack
66 node->data = "argument string";
69 #if DEBUG
70 Purify_PrintMemory ();
71 #endif
74 void Purify_Push (char * stackBase, long pushSize)
76 long offset;
78 passert (Purify_CurrentStackNode);
79 passert (Purify_CurrentStackNode->mem);
81 #if 0
82 printf ("Push(): sp=%p, size=%ld fp=%p stackBase=%p\n",
83 stackBase, pushSize, (&offset)-1, Purify_CurrentStackNode->mem);
84 #endif
86 stackBase += 4;
87 stackBase -= pushSize;
89 offset = (long)stackBase - (long)(Purify_CurrentStackNode->mem);
91 #if 0
92 printf ("offset=%d\n", offset);
93 #endif
95 passert (offset >= 0);
97 Purify_SetMemoryFlags (Purify_CurrentStackNode, offset, pushSize,
98 PURIFY_MemFlag_Readable|PURIFY_MemFlag_Writable
102 void Purify_Pop (char * stackBase, long popSize)
104 long offset;
106 passert (Purify_CurrentStackNode);
107 passert (Purify_CurrentStackNode->mem);
109 stackBase += 4;
111 offset = (long)stackBase - (long)(Purify_CurrentStackNode->mem);
113 passert (offset >= 0);
115 Purify_SetMemoryFlags (Purify_CurrentStackNode, offset, popSize,
116 PURIFY_MemFlag_Free
120 void Purify_Alloca (char * stackBase, long allocSize)
122 long offset;
124 passert (Purify_CurrentStackNode);
125 passert (Purify_CurrentStackNode->mem);
127 #if 0
128 printf ("alloca(): sp=%p, size=%ld fp=%p stackBase=%p\n",
129 stackBase, allocSize, (&offset)-1, Purify_CurrentStackNode->mem);
130 #endif
132 stackBase += 4;
133 stackBase -= allocSize;
135 offset = (long)stackBase - (long)(Purify_CurrentStackNode->mem);
137 #if 0
138 printf ("offset=%ld\n", offset);
139 #endif
141 passert (offset >= 0);
143 Purify_SetMemoryFlags (Purify_CurrentStackNode, offset, allocSize,
144 PURIFY_MemFlag_Empty|PURIFY_MemFlag_Writable
148 void Purify_MoveSP (char * SP, char * newSP)
150 long dist;
152 dist = (long)newSP - (long)SP;
154 if (dist > 0)
155 Purify_Pop (SP, dist);
156 else if (dist < 0)
157 Purify_Alloca (SP, -dist);