4297d7c4fa64f290062285b3d20b4248d11bc0ca
[com/gs-lite.git] / src / ftacmp / generate_utils.cc
1 /* ------------------------------------------------\r
2 Copyright 2014 AT&T Intellectual Property\r
3    Licensed under the Apache License, Version 2.0 (the "License");\r
4    you may not use this file except in compliance with the License.\r
5    You may obtain a copy of the License at\r
6 \r
7      http://www.apache.org/licenses/LICENSE-2.0\r
8 \r
9    Unless required by applicable law or agreed to in writing, software\r
10    distributed under the License is distributed on an "AS IS" BASIS,\r
11    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12    See the License for the specific language governing permissions and\r
13    limitations under the License.\r
14  ------------------------------------------- */\r
15 \r
16 //              This file contains functions used by more than one\r
17 //              code generation system.\r
18 \r
19 \r
20 #include"generate_utils.h"\r
21 #include"lapp.h"\r
22 \r
23 \r
24 using namespace std;\r
25 \r
26 \r
27 static char tmpstr[1000];\r
28 \r
29 \r
30 //                      Replace dots in node name with underscore to prevent generating variable\r
31 //                      and structure names with dots inside\r
32 string normalize_name(string name) {\r
33         string ret = name;\r
34         int pos = ret.find('.');\r
35         while (pos >=0) {\r
36                 ret = ret.replace(pos, 1, "_");\r
37                 pos = ret.find('.', pos + 1);\r
38         }\r
39         pos = ret.find('/');\r
40         while (pos >=0) {\r
41                 ret = ret.replace(pos, 1, "_XX_");\r
42                 pos = ret.find('/', pos + 1);\r
43         }\r
44         return ret;\r
45 }\r
46 \r
47 \r
48 //      name of tuple struct\r
49 \r
50 string generate_tuple_name(string node_name){\r
51         string ret = normalize_name(node_name);\r
52         if(ret == ""){\r
53                 ret = "default";\r
54         }\r
55         ret.append("_tuple");\r
56 \r
57         return(ret);\r
58 }\r
59 \r
60 \r
61 //              LFTA allocation function name.\r
62 \r
63 string generate_alloc_name(string node_name){\r
64         string ret = normalize_name(node_name);\r
65         if(ret == ""){\r
66                 ret = "default";\r
67         }\r
68         ret.append("_fta_alloc");\r
69 \r
70         return(ret);\r
71 }\r
72 \r
73 \r
74 //              The name of the schema definition string.\r
75 \r
76 string generate_schema_string_name(string node_name){\r
77         string ret = normalize_name(node_name);\r
78         if(ret == ""){\r
79                 ret = "default";\r
80         }\r
81         ret += "_schema_definition";\r
82 \r
83         return(ret);\r
84 }\r
85 \r
86 \r
87 \r
88 //              Generate representations of a tuple.\r
89 //              LFTA and HFTA use slightly different names.\r
90 \r
91 string generate_tuple_struct(string node_name, vector<scalarexp_t *> sl_list){\r
92   string ret = "struct "+generate_tuple_name(normalize_name(node_name))+"{\n";\r
93 \r
94   int s;\r
95   for(s=0;s<sl_list.size();s++){\r
96      sprintf(tmpstr,"tuple_var%d",s);\r
97      ret += "\t" + sl_list[s]->get_data_type()->make_tuple_cvar(tmpstr)+";\n";\r
98   }\r
99   ret.append("\tgs_int8_t tuple_type;\n");\r
100   ret.append("} __attribute__ ((__packed__));\n\n");\r
101 \r
102   return(ret);\r
103 }\r
104 \r
105 \r
106 //              generate_host_tuple_struct uses numbered fields,\r
107 //              while generate_host_name_tuple_struct uses the filed names themselves.\r
108 //                      TODO: change over from generate_host_tuple_struct\r
109 //                      to generate_host_name_tuple_struct\r
110 string generate_host_name_tuple_struct(table_def *td){\r
111   string ret = "struct "+generate_tuple_name(normalize_name(td->get_tbl_name()))+"{\n";\r
112 \r
113   vector<field_entry *> flds = td->get_fields();\r
114 \r
115   int f;\r
116   for(f=0;f<flds.size();f++){\r
117          data_type dt(flds[f]->get_type());\r
118 //fprintf(stderr,"tbl=%s, fld=%s, type=%s, dt.type=%d, dt.schema_type=%s\n",td->get_tbl_name().c_str(), flds[f]->get_name().c_str(),flds[f]->get_fcn().c_str(), dt.get_type(),dt.get_type_str().c_str());\r
119          ret+="\t"+dt.make_host_cvar(flds[f]->get_name())+";\n";\r
120   }\r
121   ret.append("\tgs_int8_t tuple_type;\n");\r
122   ret.append("} __attribute__ ((__packed__));\n\n");\r
123 \r
124   return(ret);\r
125 }\r
126 \r
127 string generate_host_tuple_struct(table_def *td){\r
128   string ret = "struct "+generate_tuple_name(normalize_name(td->get_tbl_name()))+"{\n";\r
129 \r
130   vector<field_entry *> flds = td->get_fields();\r
131 \r
132   int f;\r
133   for(f=0;f<flds.size();f++){\r
134          data_type dt(flds[f]->get_type());\r
135 //fprintf(stderr,"tbl=%s, fld=%s, type=%s, dt.type=%d, dt.schema_type=%s out=%s\n",td->get_tbl_name().c_str(), flds[f]->get_name().c_str(),flds[f]->get_type().c_str(), dt.get_type(),dt.get_type_str().c_str(),dt.make_host_cvar(tmpstr).c_str());\r
136      sprintf(tmpstr,"tuple_var%d",f);\r
137          ret+="\t"+dt.make_host_tuple_cvar(tmpstr)+";\n";\r
138   }\r
139   ret.append("} __attribute__ ((__packed__));\n\n");\r
140 \r
141   return(ret);\r
142 }\r
143 \r
144 \r
145 //              convert internal tuple format to exteral tuple format.\r
146 //              mostly, perform htonl conversions.\r
147 \r
148 string generate_hfta_finalize_tuple(table_def *td){\r
149   string ret = "void finalize_tuple(host_tuple &tup){\n";\r
150   ret += "\tstruct "+generate_tuple_name(normalize_name(td->get_tbl_name()))+" *tuple = ("+\r
151                 generate_tuple_name(td->get_tbl_name())+" *)(tup.data);\n";\r
152 \r
153 \r
154   vector<field_entry *> flds = td->get_fields();\r
155 \r
156 /*\r
157   int f;\r
158   for(f=0;f<flds.size();f++){\r
159          ret.append("\t");\r
160          data_type dt(flds[f]->get_type());\r
161          if(dt.get_type() == v_str_t){\r
162                 sprintf(tmpstr,"\ttuple->tuple_var%d.offset = htonl(tuple->tuple_var%d.offset);\n",f,f);\r
163                 ret += tmpstr;\r
164                 sprintf(tmpstr,"\ttuple->tuple_var%d.length = htonl(tuple->tuple_var%d.length);\n",f,f);\r
165                 ret += tmpstr;\r
166                 sprintf(tmpstr,"\ttuple->tuple_var%d.reserved = htonl(tuple->tuple_var%d.reserved);\n",f,f);\r
167                 ret += tmpstr;\r
168          }else{\r
169                 if(dt.needs_hn_translation()){\r
170                         sprintf(tmpstr,"\ttuple->tuple_var%d = %s(tuple->tuple_var%d);\n",\r
171                                 f, dt.hton_translation().c_str(), f);\r
172                         ret += tmpstr;\r
173                 }\r
174          }\r
175   }\r
176 */\r
177   ret.append("};\n\n");\r
178 \r
179   return(ret);\r
180 }\r
181 \r
182 \r
183 \r
184 \r
185 //              make code translation so that it embeds\r
186 //              as a C-string -- escape the escape characters.\r
187 \r
188 string make_C_embedded_string(string &instr){\r
189         string outstr = "\"";\r
190         int i;\r
191         for(i=0;i<instr.size();++i){\r
192                 if(instr[i] == '\n'){\r
193                         outstr += "\\n\"\n\t\"";\r
194                 }else{\r
195                         if(instr[i] == '\\'){\r
196                                 outstr += "\\\\";\r
197                         }else\r
198                                 outstr += instr[i];\r
199                 }\r
200         }\r
201         outstr += "\"";\r
202         return(outstr);\r
203 }\r
204 \r
205 string generate_host_tuple_pack(table_def *td){\r
206   int f;\r
207   string ret = "int pack_"+normalize_name(td->get_tbl_name())+"_tuple(host_tuple *tup, char *buf, int len, struct "+generate_tuple_name(normalize_name(td->get_tbl_name()))+" *s, gs_int8_t tuple_type){\n";\r
208         ret +=\r
209 "\tstruct "+generate_tuple_name(td->get_tbl_name())+" *d;\n";\r
210 \r
211   vector<field_entry *> flds = td->get_fields();\r
212 \r
213   ret+="\tif (tuple_type == TEMPORAL_TUPLE) {\n";\r
214   ret+="\t\td=(struct "+generate_tuple_name(td->get_tbl_name())+" *)buf;\n";\r
215   ret+="\t\ttup->data = (char *)d;\n";\r
216   ret+="\t\ttup->heap_resident=false;\n";\r
217   for(f=0;f<flds.size();f++){\r
218         data_type dt(flds[f]->get_type(),flds[f]->get_modifier_list());\r
219         if (dt.is_temporal()) {\r
220                 string fldnm = flds[f]->get_name();\r
221 //              if(dt.needs_hn_translation())\r
222 //                      ret+="\t\td->"+fldnm+"="+dt.hton_translation()+"(s->"+fldnm+");\n";\r
223 //              else\r
224                         ret+="\t\td->"+fldnm+"=s->"+fldnm+";\n";\r
225         }\r
226   }\r
227   ret += "\t\td->tuple_type = TEMPORAL_TUPLE;\n";\r
228   ret += "\t\ttup->tuple_size=sizeof(struct "+generate_tuple_name(td->get_tbl_name())+");\n";\r
229   ret += "\t\treturn 0;\n}\n";\r
230 \r
231 \r
232   bool uses_buffer_type = false;\r
233   for(f=0;f<flds.size();f++){\r
234          data_type dt(flds[f]->get_type());\r
235          if(dt.is_buffer_type()) uses_buffer_type = true;\r
236   }\r
237 \r
238   if(!uses_buffer_type){\r
239         ret+="\td=s;\n";\r
240         ret+="\ttup->data = (char *)d;\n";\r
241         ret+="\ttup->heap_resident=false;\n";\r
242         for(f=0;f<flds.size();f++){\r
243                 data_type dt(flds[f]->get_type());\r
244                 string fldnm = flds[f]->get_name();\r
245 //              if(dt.needs_hn_translation())\r
246 //                      ret+="\td->"+fldnm+"="+dt.hton_translation()+"(s->"+fldnm+");\n";\r
247 //              else\r
248                         ret+="\td->"+fldnm+"=s->"+fldnm+";\n";\r
249         }\r
250         ret+=\r
251 "\ttup->tuple_size=sizeof(struct "+generate_tuple_name(td->get_tbl_name())+");\n";\r
252   }else{\r
253         ret+="\tint pos=sizeof(struct "+generate_tuple_name(td->get_tbl_name())+");\n";\r
254         ret+="\td=(struct "+generate_tuple_name(td->get_tbl_name())+" *)buf;\n";\r
255         ret+="\ttup->data = (char *)d;\n";\r
256         ret+="\ttup->heap_resident=false;\n";\r
257         for(f=0;f<flds.size();f++){\r
258                 data_type dt(flds[f]->get_type());\r
259                 string fldnm = flds[f]->get_name();\r
260                 if(dt.is_buffer_type()){\r
261                         ret+="\tif(pos+"+dt.get_hfta_buffer_size()+"(&(s->"+fldnm+"))>len) return 1;\n";\r
262                         ret+="\t"+dt.get_hfta_buffer_tuple_copy()+"(&(d->"+fldnm+"),&(s->"+fldnm+"), buf+pos, pos);\n";\r
263                         ret+="\tpos+="+dt.get_hfta_buffer_size()+"(&(s->"+fldnm+"));\n";\r
264 //                      ret+="\td->"+fldnm+".length = htonl(d->"+fldnm+".length);\n";\r
265 //                      ret+="\td->"+fldnm+".offset = htonl(d->"+fldnm+".offset);\n";\r
266 //                      ret+="\td->"+fldnm+".reserved = htonl(d->"+fldnm+".reserved);\n";\r
267                 }else{\r
268 //                if(dt.needs_hn_translation())\r
269 //                      ret+="\td->"+fldnm+"="+dt.hton_translation()+"(s->"+fldnm+");\n";\r
270 //                else\r
271                         ret+="\td->"+fldnm+"=s->"+fldnm+";\n";\r
272                 }\r
273         }\r
274         ret+="\ttup->tuple_size=pos;\n";\r
275   }\r
276 \r
277   ret+=   "\td->tuple_type = REGULAR_TUPLE;\n";\r
278 \r
279   ret += "\treturn 0;\n";\r
280   ret+="}\n\n";\r
281 \r
282   return(ret);\r
283 }\r
284 \r
285 \r
286 string generate_host_tuple_unpack(table_def *td){\r
287   int f;\r
288   string ret = "struct "+generate_tuple_name(normalize_name(td->get_tbl_name())) +" *unpack_"+normalize_name(td->get_tbl_name())+"_tuple(host_tuple *tup){\n";\r
289         ret += "\tstruct "+generate_tuple_name(normalize_name(td->get_tbl_name()))+" *d;\n";\r
290         ret+="\td=(struct "+generate_tuple_name(normalize_name(td->get_tbl_name()))+" *)(tup->data);\n";\r
291         ret+="\tif(tup->tuple_size<sizeof(struct "+generate_tuple_name(normalize_name(td->get_tbl_name())) +")) return NULL;\n";\r
292 \r
293   vector<field_entry *> flds = td->get_fields();\r
294 \r
295         for(f=0;f<flds.size();f++){\r
296                 data_type dt(flds[f]->get_type());\r
297                 string fldnm = flds[f]->get_name();\r
298                 if(dt.is_buffer_type()){\r
299 //                      ret+="\td->"+fldnm+".length = ntohl(d->"+fldnm+".length);\n";\r
300 //                      ret+="\td->"+fldnm+".offset = ntohl(d->"+fldnm+".offset);\n";\r
301                         ret+="\td->"+fldnm+".reserved = SHALLOW_COPY;\n";\r
302                         ret+="\tif(d->"+fldnm+".offset+d->"+fldnm+".length>tup->tuple_size) return NULL;\n";\r
303                         ret+="\td->"+fldnm+".offset+=(unsigned int)(tup->data);\n";\r
304                 }else{\r
305 //                if(dt.needs_hn_translation())\r
306 //                      ret+="\td->"+fldnm+"="+dt.ntoh_translation()+"(d->"+fldnm+");\n";\r
307                 }\r
308         }\r
309 \r
310   ret += "\treturn d;\n";\r
311   ret+="}\n\n";\r
312 \r
313   return(ret);\r
314 }\r
315 //              Functions related to parsing.\r
316 \r
317  int split_string(char *instr,char sep, char **words,int max_words){\r
318    char *loc;\r
319    char *str;\r
320    int nwords = 0;\r
321 \r
322    str = instr;\r
323    words[nwords++] = str;\r
324    while( (loc = strchr(str,sep)) != NULL){\r
325         *loc = '\0';\r
326         str = loc+1;\r
327         if(nwords >= max_words){\r
328                 fprintf(stderr,"Error in split_string, too many words discovered (max is %d)\n",max_words);\r
329                 nwords = max_words-1;\r
330         }\r
331         words[nwords++] = str;\r
332    }\r
333 \r
334    return(nwords);\r
335 }\r
336 \r
337 //      For fast hashing\r
338 string hash_nums[NRANDS] = {\r
339 "12916008961267169387ull", "13447227858232756685ull",\r
340 "15651770379918602919ull", "1154671861688431608ull",\r
341 "6777078091984849858ull", "14217205709582564356ull",\r
342 "4955408621820609982ull", "15813680319165523695ull",\r
343 "9897969721407807129ull", "5799700135519793083ull",\r
344 "3446529189623437397ull", "2766403683465910630ull",\r
345 "3759321430908793328ull", "6569396511892890354ull",\r
346 "11124853911180290924ull", "17425412145238035549ull",\r
347 "6879931585355039943ull", "16598635011539670441ull",\r
348 "9615975578494811651ull", "4378135509538422740ull",\r
349 "741282195344332574ull", "17368612862906255584ull",\r
350 "17294299200556814618ull", "518343398779663051ull",\r
351 "3861893449302272757ull", "8951107288843549591ull",\r
352 "15785139392894559409ull", "5917810836789601602ull",\r
353 "16169988133001117004ull", "9792861259254509262ull",\r
354 "5089058010244872136ull", "2130075224835397689ull",\r
355 "844136788226150435ull", "1303298091153875333ull",\r
356 "3579898206894361183ull", "7529542662845336496ull",\r
357 "13151949992653382522ull", "2145333536541545660ull",\r
358 "11258221828939586934ull", "3741808146124570279ull",\r
359 "16272841626371307089ull", "12174572036188391283ull",\r
360 "9749343496254107661ull", "9141275584134508830ull",\r
361 "10134192232065698216ull", "12944268412561423018ull",\r
362 "17499725811865666340ull", "5281482378159088661ull",\r
363 "13254803486023572607ull", "4526762838498717025ull",\r
364 "15990846379668494011ull", "10680949816169027468ull",\r
365 "7116154096012931030ull", "5296740689865236632ull",\r
366 "5222427027515795922ull", "6893215299448261251ull",\r
367 "10164707755932877485ull", "15325979189512082255ull",\r
368 "3713267224148573289ull", "12292682741753167354ull",\r
369 "4098115959960163588ull", "16095675565885113990ull",\r
370 "11391590846210510720ull", "8432889531466002673ull",\r
371 "7146668520368482523ull", "7678169991822407997ull",\r
372 "9882712513525031447ull", "13904414563513869160ull",\r
373 "1080076724395768626ull", "8448147843172150388ull",\r
374 "17633093729608185134ull", "10044622457050142303ull",\r
375 "4128911859292425737ull", "30642269109444395ull",\r
376 "16124215396922640581ull", "15444089895060081110ull",\r
377 "16437006538696302944ull", "800338649777443426ull",\r
378 "5355794945275091932ull", "11656354278827687117ull",\r
379 "1110873718944691255ull", "10829576045617693977ull",\r
380 "3846916616884579955ull", "17055821716837625668ull",\r
381 "13418968402643535758ull", "11671612594828802128ull",\r
382 "11597298928184328586ull", "13196028510862205499ull",\r
383 "16539578557089782373ull", "3182048322921507591ull",\r
384 "10016080431267550241ull", "148751875162592690ull",\r
385 "10400930266590768572ull", "4023803397139127870ull",\r
386 "17766462746879108920ull", "14807761432134600873ull",\r
387 "13521540421053792403ull", "13980983198941385205ull",\r
388 "16257584414193564367ull", "1760484796451765024ull"\r
389 };\r
390 \r
391 \r