Copyright clean-up (part 1):
[AROS.git] / compiler / purify / src / stack.c
blob02fda8495bfcea4a6ff2652213f78ca5019be07d
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <stdio.h>
7 #include <assert.h>
8 #include "debug.h"
9 #include "hash.h"
11 static MemHash * Purify_CurrentStackNode;
13 void Purify_InitStack (char * stackBase, long stackSize)
15 MemHash * node;
16 char ** argv;
17 int argc, t;
18 long offset;
20 stackBase +=
21 4 /* two arguments */
22 +4 /* return address for main() */
23 +8 /* two arguments for main() */
26 #if DEBUG
27 printf ("Stack goes from %p to %p\n", stackBase - stackSize, stackBase);
28 #endif
30 node = Purify_AddMemory (stackBase - stackSize
31 , stackSize
32 , PURIFY_MemFlag_Empty
33 , PURIFY_MemType_Stack
36 node->data = "Stack";
38 Purify_CurrentStackNode = node;
40 /* Major hack :-/ */
41 argv = ((char ***)stackBase)[-1];
42 argc = ((int *)stackBase)[-2];
44 #if 0
45 printf ("&argc=%p\n", &((int *)stackBase)[-2]);
46 printf ("argc=%d argv=%p\n", argc, argv);
47 #endif
49 offset = (long)(&((int *)stackBase)[-2]) - (long)(node->mem);
51 Purify_SetMemoryFlags (node, offset, 8,
52 PURIFY_MemFlag_Readable|PURIFY_MemFlag_Writable
55 node = Purify_AddMemory (argv
56 , (argc+1) * sizeof (char *)
57 , PURIFY_MemFlag_Readable|PURIFY_MemFlag_Writable
58 , PURIFY_MemType_Stack
61 node->data = "argv[] array";
63 for (t=0; t<argc; t++)
65 node = Purify_AddMemory (argv[t]
66 , strlen (argv[t]) + 1
67 , PURIFY_MemFlag_Readable|PURIFY_MemFlag_Writable
68 , PURIFY_MemType_Stack
71 node->data = "argument string";
74 #if DEBUG
75 Purify_PrintMemory ();
76 #endif
79 void Purify_Push (char * stackBase, long pushSize)
81 long offset;
83 passert (Purify_CurrentStackNode);
84 passert (Purify_CurrentStackNode->mem);
86 #if 0
87 printf ("Push(): sp=%p, size=%ld fp=%p stackBase=%p\n",
88 stackBase, pushSize, (&offset)-1, Purify_CurrentStackNode->mem);
89 #endif
91 stackBase += 4;
92 stackBase -= pushSize;
94 offset = (long)stackBase - (long)(Purify_CurrentStackNode->mem);
96 #if 0
97 printf ("offset=%d\n", offset);
98 #endif
100 passert (offset >= 0);
102 Purify_SetMemoryFlags (Purify_CurrentStackNode, offset, pushSize,
103 PURIFY_MemFlag_Readable|PURIFY_MemFlag_Writable
107 void Purify_Pop (char * stackBase, long popSize)
109 long offset;
111 passert (Purify_CurrentStackNode);
112 passert (Purify_CurrentStackNode->mem);
114 stackBase += 4;
116 offset = (long)stackBase - (long)(Purify_CurrentStackNode->mem);
118 passert (offset >= 0);
120 Purify_SetMemoryFlags (Purify_CurrentStackNode, offset, popSize,
121 PURIFY_MemFlag_Free
125 void Purify_Alloca (char * stackBase, long allocSize)
127 long offset;
129 passert (Purify_CurrentStackNode);
130 passert (Purify_CurrentStackNode->mem);
132 #if 0
133 printf ("alloca(): sp=%p, size=%ld fp=%p stackBase=%p\n",
134 stackBase, allocSize, (&offset)-1, Purify_CurrentStackNode->mem);
135 #endif
137 stackBase += 4;
138 stackBase -= allocSize;
140 offset = (long)stackBase - (long)(Purify_CurrentStackNode->mem);
142 #if 0
143 printf ("offset=%ld\n", offset);
144 #endif
146 passert (offset >= 0);
148 Purify_SetMemoryFlags (Purify_CurrentStackNode, offset, allocSize,
149 PURIFY_MemFlag_Empty|PURIFY_MemFlag_Writable
153 void Purify_MoveSP (char * SP, char * newSP)
155 long dist;
157 dist = (long)newSP - (long)SP;
159 if (dist > 0)
160 Purify_Pop (SP, dist);
161 else if (dist < 0)
162 Purify_Alloca (SP, -dist);