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