nv50: fix build-predicate function
[mesa.git] / src / mesa / 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 "main/mtypes.h"
11 #include "program/prog_instruction.h"
12 #include "slang_label.h"
13 #include "slang_mem.h"
14
15
16
17 slang_label *
18 _slang_label_new(const char *name)
19 {
20 slang_label *l = (slang_label *) _slang_alloc(sizeof(slang_label));
21 if (l) {
22 l->Name = _slang_strdup(name);
23 l->Location = -1;
24 }
25 return l;
26 }
27
28 /**
29 * As above, but suffix the name with a unique number.
30 */
31 slang_label *
32 _slang_label_new_unique(const char *name)
33 {
34 static int id = 1;
35 slang_label *l = (slang_label *) _slang_alloc(sizeof(slang_label));
36 if (l) {
37 l->Name = (char *) _slang_alloc(strlen(name) + 10);
38 if (!l->Name) {
39 free(l);
40 return NULL;
41 }
42 _mesa_snprintf(l->Name, strlen(name) + 10, "%s_%d", name, id);
43 id++;
44 l->Location = -1;
45 }
46 return l;
47 }
48
49 void
50 _slang_label_delete(slang_label *l)
51 {
52 if (l->Name) {
53 _slang_free(l->Name);
54 l->Name = NULL;
55 }
56 if (l->References) {
57 _slang_free(l->References);
58 l->References = NULL;
59 }
60 _slang_free(l);
61 }
62
63
64 void
65 _slang_label_add_reference(slang_label *l, GLuint inst)
66 {
67 const GLuint oldSize = l->NumReferences * sizeof(GLuint);
68 assert(l->Location < 0);
69 l->References = _slang_realloc(l->References,
70 oldSize, oldSize + sizeof(GLuint));
71 if (l->References) {
72 l->References[l->NumReferences] = inst;
73 l->NumReferences++;
74 }
75 }
76
77
78 GLint
79 _slang_label_get_location(const slang_label *l)
80 {
81 return l->Location;
82 }
83
84
85 void
86 _slang_label_set_location(slang_label *l, GLint location,
87 struct gl_program *prog)
88 {
89 GLuint i;
90
91 assert(l->Location < 0);
92 assert(location >= 0);
93
94 l->Location = location;
95
96 /* for the instructions that were waiting to learn the label's location: */
97 for (i = 0; i < l->NumReferences; i++) {
98 const GLuint j = l->References[i];
99 prog->Instructions[j].BranchTarget = location;
100 }
101
102 if (l->References) {
103 _slang_free(l->References);
104 l->References = NULL;
105 }
106 }