Use new memory pool allocator. Lots of debug code still in place...
[mesa.git] / src / mesa / shader / slang / slang_label.c
1
2
3 /**
4 * Functions for managing instruction labels.
5 * Basically, this is used to manage the problem of forward branches where
6 * we have a branch instruciton but don't know the target address yet.
7 */
8
9
10 #include "slang_label.h"
11 #include "slang_mem.h"
12
13
14
15 slang_label *
16 _slang_label_new(const char *name)
17 {
18 #if !USE_MEMPOOL
19 slang_label *l = (slang_label *) _mesa_calloc(sizeof(slang_label));
20 #else
21 slang_label *l = (slang_label *) _slang_alloc(sizeof(slang_label));
22 #endif
23 if (l) {
24 #if !USE_MEMPOOL
25 l->Name = _mesa_strdup(name);
26 #else
27 l->Name = _slang_strdup(name);
28 #endif
29 l->Location = -1;
30 }
31 return l;
32 }
33
34 /**
35 * As above, but suffix the name with a unique number.
36 */
37 slang_label *
38 _slang_label_new_unique(const char *name)
39 {
40 static int id = 1;
41 #if !USE_MEMPOOL
42 slang_label *l = (slang_label *) _mesa_calloc(sizeof(slang_label));
43 #else
44 slang_label *l = (slang_label *) _slang_alloc(sizeof(slang_label));
45 #endif
46 if (l) {
47 #if !USE_MEMPOOL
48 l->Name = (char *) _mesa_malloc(_mesa_strlen(name) + 10);
49 #else
50 l->Name = (char *) _slang_alloc(_mesa_strlen(name) + 10);
51 #endif
52 if (!l->Name) {
53 _mesa_free(l);
54 return NULL;
55 }
56 _mesa_sprintf(l->Name, "%s_%d", name, id);
57 id++;
58 l->Location = -1;
59 }
60 return l;
61 }
62
63 void
64 _slang_label_delete(slang_label *l)
65 {
66 #if !USE_MEMPOOL
67 if (l->Name)
68 _mesa_free(l->Name);
69 if (l->References)
70 _mesa_free(l->References);
71 _mesa_free(l);
72 #endif
73 }
74
75
76 void
77 _slang_label_add_reference(slang_label *l, GLuint inst)
78 {
79 const GLuint oldSize = l->NumReferences * sizeof(GLuint);
80 assert(l->Location < 0);
81 #if !USE_MEMPOOL
82 l->References = _mesa_realloc(l->References,
83 oldSize, oldSize + sizeof(GLuint));
84 #else
85 l->References = _slang_realloc(l->References,
86 oldSize, oldSize + sizeof(GLuint));
87 #endif
88 if (l->References) {
89 l->References[l->NumReferences] = inst;
90 l->NumReferences++;
91 }
92 }
93
94
95 GLint
96 _slang_label_get_location(const slang_label *l)
97 {
98 return l->Location;
99 }
100
101
102 void
103 _slang_label_set_location(slang_label *l, GLint location,
104 struct gl_program *prog)
105 {
106 GLuint i;
107
108 assert(l->Location < 0);
109 assert(location >= 0);
110
111 l->Location = location;
112
113 /* for the instructions that were waiting to learn the label's location: */
114 for (i = 0; i < l->NumReferences; i++) {
115 const GLuint j = l->References[i];
116 prog->Instructions[j].BranchTarget = location;
117 }
118
119 if (l->References) {
120 #if !USE_MEMPOOL
121 _mesa_free(l->References);
122 #endif
123 l->References = NULL;
124 }
125 }