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
7 http://www.apache.org/licenses/LICENSE-2.0
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 ------------------------------------------- */
16 // This file contains functions used by more than one
17 // code generation system.
20 #include"generate_utils.h"
27 static char tmpstr[1000];
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) {
34 int pos = ret.find('.');
36 ret = ret.replace(pos, 1, "_");
37 pos = ret.find('.', pos + 1);
41 ret = ret.replace(pos, 1, "_XX_");
42 pos = ret.find('/', pos + 1);
48 // name of tuple struct
50 string generate_tuple_name(string node_name){
51 string ret = normalize_name(node_name);
61 // LFTA allocation function name.
63 string generate_alloc_name(string node_name){
64 string ret = normalize_name(node_name);
68 ret.append("_fta_alloc");
74 // The name of the schema definition string.
76 string generate_schema_string_name(string node_name){
77 string ret = normalize_name(node_name);
81 ret += "_schema_definition";
88 // Generate representations of a tuple.
89 // LFTA and HFTA use slightly different names.
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";
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";
99 ret.append("\tgs_int8_t tuple_type;\n");
100 ret.append("} __attribute__ ((__packed__));\n\n");
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";
113 vector<field_entry *> flds = td->get_fields();
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";
121 ret.append("\tgs_int8_t tuple_type;\n");
122 ret.append("} __attribute__ ((__packed__));\n\n");
127 string generate_host_tuple_struct(table_def *td){
128 string ret = "struct "+generate_tuple_name(normalize_name(td->get_tbl_name()))+"{\n";
130 vector<field_entry *> flds = td->get_fields();
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";
139 ret.append("} __attribute__ ((__packed__));\n\n");
145 // convert internal tuple format to exteral tuple format.
146 // mostly, perform htonl conversions.
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";
154 vector<field_entry *> flds = td->get_fields();
158 for(f=0;f<flds.size();f++){
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);
164 sprintf(tmpstr,"\ttuple->tuple_var%d.length = htonl(tuple->tuple_var%d.length);\n",f,f);
166 sprintf(tmpstr,"\ttuple->tuple_var%d.reserved = htonl(tuple->tuple_var%d.reserved);\n",f,f);
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);
177 ret.append("};\n\n");
185 // make code translation so that it embeds
186 // as a C-string -- escape the escape characters.
188 string make_C_embedded_string(string &instr){
189 string outstr = "\"";
191 for(i=0;i<instr.size();++i){
192 if(instr[i] == '\n'){
193 outstr += "\\n\"\n\t\"";
195 if(instr[i] == '\\'){
205 string generate_host_tuple_pack(table_def *td){
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";
209 "\tstruct "+generate_tuple_name(td->get_tbl_name())+" *d;\n";
211 vector<field_entry *> flds = td->get_fields();
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";
224 ret+="\t\td->"+fldnm+"=s->"+fldnm+";\n";
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";
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;
238 if(!uses_buffer_type){
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";
248 ret+="\td->"+fldnm+"=s->"+fldnm+";\n";
251 "\ttup->tuple_size=sizeof(struct "+generate_tuple_name(td->get_tbl_name())+");\n";
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";
268 // if(dt.needs_hn_translation())
269 // ret+="\td->"+fldnm+"="+dt.hton_translation()+"(s->"+fldnm+");\n";
271 ret+="\td->"+fldnm+"=s->"+fldnm+";\n";
274 ret+="\ttup->tuple_size=pos;\n";
277 ret+= "\td->tuple_type = REGULAR_TUPLE;\n";
279 ret += "\treturn 0;\n";
286 string generate_host_tuple_unpack(table_def *td){
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";
293 vector<field_entry *> flds = td->get_fields();
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";
305 // if(dt.needs_hn_translation())
306 // ret+="\td->"+fldnm+"="+dt.ntoh_translation()+"(d->"+fldnm+");\n";
310 ret += "\treturn d;\n";
315 // Functions related to parsing.
317 int split_string(char *instr,char sep, char **words,int max_words){
323 words[nwords++] = str;
324 while( (loc = strchr(str,sep)) != NULL){
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;
331 words[nwords++] = str;
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"