1 /*
2  *  attrib.c - attributes of clauses
3  *
4  */
5 
6 #include "header.h"
7 
8 struct {
9   char *name;
10   int type;
11 } Attributes[MAX_ATTRIBUTES];
12 
13 /*************
14  *
15  *   init_attributes()
16  *
17  *************/
18 
init_attributes(void)19 void init_attributes(void)
20 {
21   int i;
22 
23   for (i = 0; i < MAX_ATTRIBUTES; i++)
24     Attributes[i].name = "";
25 
26   Attributes[BSUB_HINT_WT_ATTR].name = "bsub_hint_wt";
27   Attributes[BSUB_HINT_WT_ATTR].type = INT_ATTR;
28 
29   Attributes[FSUB_HINT_WT_ATTR].name = "fsub_hint_wt";
30   Attributes[FSUB_HINT_WT_ATTR].type = INT_ATTR;
31 
32   Attributes[EQUIV_HINT_WT_ATTR].name = "equiv_hint_wt";
33   Attributes[EQUIV_HINT_WT_ATTR].type = INT_ATTR;
34 
35   Attributes[BSUB_HINT_ADD_WT_ATTR].name = "bsub_hint_add_wt";
36   Attributes[BSUB_HINT_ADD_WT_ATTR].type = INT_ATTR;
37 
38   Attributes[FSUB_HINT_ADD_WT_ATTR].name = "fsub_hint_add_wt";
39   Attributes[FSUB_HINT_ADD_WT_ATTR].type = INT_ATTR;
40 
41   Attributes[EQUIV_HINT_ADD_WT_ATTR].name = "equiv_hint_add_wt";
42   Attributes[EQUIV_HINT_ADD_WT_ATTR].type = INT_ATTR;
43 
44   Attributes[LABEL_ATTR].name = "label";
45   Attributes[LABEL_ATTR].type = STRING_ATTR;
46 
47 }  /* init_attributes */
48 
49 /*************
50  *
51  *   get_attribute_index()
52  *
53  *   Return -1 if s is not any known attribute.
54  *
55  *************/
56 
get_attribute_index(char * s)57 int get_attribute_index(char *s)
58 {
59   int i, found;
60 
61   if (str_ident(s, ""))
62     return(-1);
63   else {
64     for (i = 0, found = 0; i < MAX_ATTRIBUTES && !found; i++)
65       found = str_ident(s, Attributes[i].name);
66     return(found ? i-1 : -1);
67   }
68 }  /* get_attribute_index */
69 
70 /*************
71  *
72  *   attribute_type()
73  *
74  *************/
75 
attribute_type(int name)76 int attribute_type(int name)
77 {
78   switch (name) {
79   case BSUB_HINT_WT_ATTR: return(INT_ATTR);
80   case FSUB_HINT_WT_ATTR: return(INT_ATTR);
81   case EQUIV_HINT_WT_ATTR: return(INT_ATTR);
82   case BSUB_HINT_ADD_WT_ATTR: return(INT_ATTR);
83   case FSUB_HINT_ADD_WT_ATTR: return(INT_ATTR);
84   case EQUIV_HINT_ADD_WT_ATTR: return(INT_ATTR);
85   case LABEL_ATTR: return(STRING_ATTR);
86   default:
87     printf("%d ", name);
88     abend("attribute_type: unknown attribute name");
89     return(-1);
90   }
91 }  /* attribute_type */
92 
93 /*************
94  *
95  *   get_attribute()
96  *
97  *   Return the attribute (the whole node) associated with an
98  *   attribute name.  If it does not exist, return NULL.
99  *
100  *************/
101 
get_attribute(struct clause * c,int name)102 struct cl_attribute *get_attribute(struct clause *c,
103 				   int name)
104 {
105   struct cl_attribute *a;
106   for (a = c->attributes; a && a->name != name; a = a->next);
107   return(a);
108 }  /* get_attribute */
109 
110 /*************
111  *
112  *   set_attribute()
113  *
114  *************/
115 
set_attribute(struct clause * c,int name,void * val_ptr)116 void set_attribute(struct clause *c,
117 		   int name,
118 		   void *val_ptr)
119 {
120   struct cl_attribute *a1;
121 
122   a1 = get_cl_attribute();
123   a1->next = c->attributes;
124   c->attributes = a1;
125 
126   a1->name = name;
127   switch (attribute_type(name)) {
128   case INT_ATTR:
129   case BOOL_ATTR:
130     a1->u.i = *((int *) val_ptr);
131     break;
132   case DOUBLE_ATTR:
133     a1->u.d = *((double *) val_ptr);
134     break;
135   case STRING_ATTR:
136     a1->u.s = (char *) val_ptr;
137     break;
138   case TERM_ATTR:
139     a1->u.t = (struct term *) val_ptr;
140     break;
141   }
142 
143 }  /* set_attribute */
144 
145 /*************
146  *
147  *   delete_attributes()
148  *
149  *************/
150 
delete_attributes(struct clause * c)151 void delete_attributes(struct clause *c)
152 {
153   struct cl_attribute *a1, *a2;
154 
155   a1 = c->attributes;
156   while (a1) {
157     if (attribute_type(a1->name) == TERM_ATTR)
158       zap_term(a1->u.t);
159     a2 = a1;
160     a1 = a1->next;
161     free_cl_attribute(a2);
162   }
163 }  /* delete_attributes */
164 
165 /*************
166  *
167  *   term_to_attributes()
168  *
169  *   If error, print message to stdout and return NULL;
170  *
171  *************/
172 
term_to_attributes(struct term * t)173 struct cl_attribute *term_to_attributes(struct term *t)
174 {
175   if (!is_symbol(t, "#", 2)) {
176     struct cl_attribute *a = get_cl_attribute();
177     struct term *attr_val;
178     int i;
179 
180     if (sn_to_arity(t->sym_num) != 1) {
181       printf("attributes must have arity 1: ");
182       p_term(t);
183       return(NULL);
184     }
185 
186     a->name = get_attribute_index(sn_to_str(t->sym_num));
187     attr_val = t->farg->argval;
188     switch(attribute_type(a->name)) {
189     case INT_ATTR:
190       if (str_int(sn_to_str(attr_val->sym_num), &i)) {
191 	a->u.i = i;
192 	return(a);
193       }
194       else {
195 	printf("attribute value should be integer: ");
196 	p_term(t);
197 	return(NULL);
198       }
199 
200     case BOOL_ATTR:
201       a->u.i = 0;         /* FIX THIS! */
202       return(a);
203     case DOUBLE_ATTR:
204       a->u.d = 123.4567E89;          /* FIX THIS! */
205       return(a);
206     case STRING_ATTR:
207       a->u.s = sn_to_str(attr_val->sym_num);
208       return(a);
209     case TERM_ATTR:
210       a->u.t = copy_term(attr_val);
211       return(a);
212     default:
213       return(NULL);
214     }
215   }
216   else {
217     struct cl_attribute *a1, *a2, *a3;
218 
219     a1 = term_to_attributes(t->farg->argval);
220     a2 = term_to_attributes(t->farg->narg->argval);
221     if (!a1 || !a2)
222       return(NULL);
223     else {
224       /* Append attribute lists. */
225       for (a3 = a1; a3->next; a3 = a3->next);
226       a3->next = a2;
227       return(a1);
228     }
229   }
230 }  /* term_to_attributes */
231 
232 /*************
233  *
234  *   print_attributes()
235  *
236  *************/
237 
print_attributes(FILE * fp,struct cl_attribute * a)238 void print_attributes(FILE *fp,
239 		      struct cl_attribute *a)
240 {
241   for ( ; a; a = a->next) {
242     fprintf(fp, "   # %s(", Attributes[a->name].name);
243     switch (attribute_type(a->name)) {
244     case INT_ATTR:
245       fprintf(fp, "%d", a->u.i); break;
246     case BOOL_ATTR:
247       fprintf(fp, "%s", (a->u.i ? "true" : "false")); break;
248     case DOUBLE_ATTR:
249       fprintf(fp, "%f", a->u.d); break;
250     case STRING_ATTR:
251       fprintf(fp, "%s", a->u.s); break;
252     case TERM_ATTR:
253       print_term(fp, a->u.t); break;
254     }
255     fprintf(fp, ")");
256   }
257 }  /* print_attributes */
258 
259