summaryrefslogtreecommitdiff
path: root/omega/code_gen/src/CG_stringBuilder.cc
blob: 2f9286f65072bfca682d8337f84ce3915597d0c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*****************************************************************************
 Copyright (C) 1994-2000 the Omega Project Team
 Copyright (C) 2005-2011 Chun Chen
 All Rights Reserved.

 Purpose:
   generate pseudo string code

 Notes:
   There is no need to check illegal NULL parameter and throw invalid_argument
 in other IR interface implementation. They are for debugging purpose.
   intMod implements modular function that returns positve remainder no matter
 lop is postive or nagative and rop is guranteed to be positive here.
     
 History:
   04/17/96 - Lei Zhou - created
   08/31/09 add parenthesis to string operands, Chun Chen
*****************************************************************************/

#include <code_gen/CG_stringBuilder.h>
#include <code_gen/codegen_error.h>
#include <basic/util.h>
#include <string>
#include <stdexcept>
#include <ctype.h>
#include <string.h>

namespace {

std::string SafeguardString(const std::string &s, char op) {
  int len = s.length();
  int paren_level = 0;
  int num_plusminus = 0;
  int num_mul = 0;
  int num_div = 0;
  for (int i = 0; i < len; i++)
    switch (s[i]) {
    case '(':
      paren_level++;
      break;
    case ')':
      paren_level--;
      break;
    case '+':
    case '-':
      if (paren_level == 0)
        num_plusminus++;
      break;
    case '*':
      if (paren_level == 0)
        num_mul++;
      break;
    case '/':
      if (paren_level == 0)
        num_div++;
      break;
    default:
      break;
    }

  bool need_paren = false;
  switch (op) {
  case '-':
    if (num_plusminus > 0)
      need_paren = true;
    break;
  case '*':
    if (num_plusminus > 0 || num_div > 0)
      need_paren = true;
    break;
  case '/':
    if (num_plusminus > 0 || num_div > 0 || num_mul > 0)
      need_paren = true;
    break;
  default:
    break;
  }

  if (need_paren)
    return "(" + s + ")";
  else
    return s;
}


std::string GetIndentSpaces(int indent) {
  std::string indentStr;
  for (int i = 1; i < indent; i++) {
    indentStr += "  ";
  }
  return indentStr;
}


// A shortcut to extract the string enclosed in the CG_outputRepr and delete
// the original holder.
std::string GetString(omega::CG_outputRepr *repr) {
  std::string result = static_cast<omega::CG_stringRepr *>(repr)->GetString();
  delete repr;
  return result;
}

}


namespace omega {



//-----------------------------------------------------------------------------
// Class: CG_stringBuilder
//-----------------------------------------------------------------------------

CG_stringRepr *CG_stringBuilder::CreateSubstitutedStmt(int indent, CG_outputRepr *stmt,
                                                       const std::vector<std::string> &vars,
                                                       std::vector<CG_outputRepr *> &subs) const {
  std::string listStr = "";

  for (int i = 0; i < subs.size(); i++) {
    if (subs[i] == NULL)
      listStr += "N/A";
    else 
      listStr += GetString(subs[i]);
    if (i < subs.size() - 1)
      listStr += ",";
  } 

  std::string stmtName = GetString(stmt);
  std::string indentStr = GetIndentSpaces(indent);

  return new CG_stringRepr(indentStr + stmtName + "(" + listStr + ");\n");
}

CG_stringRepr *CG_stringBuilder::CreateAssignment(int indent, 
                                                  CG_outputRepr *lhs,
                                                  CG_outputRepr *rhs) const {
  if (lhs == NULL || rhs == NULL)
    throw std::invalid_argument("missing lhs or rhs in assignment");

  std::string lhsStr = GetString(lhs);
  std::string rhsStr = GetString(rhs);

  std::string indentStr = GetIndentSpaces(indent);

  return new CG_stringRepr(indentStr + lhsStr + "=" + rhsStr + ";\n");
}


CG_stringRepr *CG_stringBuilder::CreateInvoke(const std::string &funcName,
                                              std::vector<CG_outputRepr *> &list) const {
  std::string listStr = "";

  for (int i = 0; i < list.size(); i++) {
    listStr += GetString(list[i]);
    if ( i < list.size()-1)
      listStr += ",";
  }

  return new CG_stringRepr(funcName + "(" + listStr + ")");
}
    

CG_stringRepr *CG_stringBuilder::CreateComment(int indent, const std::string &commentText) const {
  if (commentText == std::string("")) {
    return NULL;
  }

  std::string indentStr = GetIndentSpaces(indent);

  return new CG_stringRepr(indentStr + "// " + commentText + "\n");
}

CG_stringRepr* CG_stringBuilder::CreateAttribute(CG_outputRepr *control,
		const std::string &commentText) const {
	if (commentText == std::string("")) {
		return static_cast<CG_stringRepr *> (control);
	}

	std::string controlString = GetString(control);

	return new CG_stringRepr("// " + commentText + "\n" + controlString);

}

CG_outputRepr* CG_stringBuilder::CreatePragmaAttribute(CG_outputRepr *scopeStmt, int looplevel, const std::string &pragmaText) const {
	// -- Not Implemented
	return scopeStmt;
}

CG_outputRepr* CG_stringBuilder::CreatePrefetchAttribute(CG_outputRepr* scopeStmt, int looplevel, const std::string& arrName, int hint) const {
	// -- Not Implemented
	return scopeStmt;
}

CG_stringRepr *CG_stringBuilder::CreateIf(int indent, CG_outputRepr *guardList,
                                          CG_outputRepr *true_stmtList, CG_outputRepr *false_stmtList) const {
  if (guardList == NULL)
    throw std::invalid_argument("missing if condition");
  
  if (true_stmtList == NULL && false_stmtList == NULL) {
    delete guardList;
    return NULL;
  }

  std::string guardListStr = GetString(guardList);
  std::string indentStr = GetIndentSpaces(indent);
  std::string s;
  if (true_stmtList != NULL && false_stmtList == NULL) {
    s = indentStr + "if (" + guardListStr + ") {\n"
      + GetString(true_stmtList)
      + indentStr + "}\n";
  }
  else if (true_stmtList == NULL && false_stmtList != NULL) {
    s = indentStr + "if !(" + guardListStr + ") {\n"
      + GetString(false_stmtList)
      + indentStr + "}\n";
  }
  else {
    s = indentStr + "if (" + guardListStr + ") {\n" 
      + GetString(true_stmtList)
      + indentStr + "}\n"
      + indentStr + "else {\n"
      + GetString(false_stmtList)
      + indentStr + "}\n";
  }
  
  return new CG_stringRepr(s);
}



CG_stringRepr *CG_stringBuilder::CreateInductive(CG_outputRepr *index,
                                                 CG_outputRepr *lower, CG_outputRepr *upper,
                                                 CG_outputRepr *step) const {
  if (index == NULL)
    throw std::invalid_argument("missing loop index");
  if (lower == NULL)
    throw std::invalid_argument("missing loop lower bound");
  if (upper == NULL)
    throw std::invalid_argument("missing loop upper bound");
  if (step == NULL)
    throw std::invalid_argument("missing loop step size");
  
  std::string indexStr = GetString(index);
  std::string lowerStr = GetString(lower);
  std::string upperStr = GetString(upper);

  std::string doStr = "for(" + indexStr + " = " + lowerStr + "; "
    + indexStr + " <= " + upperStr + "; " 
    + indexStr;
  
  std::string stepStr = GetString(step);
  if (stepStr == to_string(1))
    doStr += "++";
  else
    doStr += " += " + stepStr;
        
  doStr += ")";
      
  return new CG_stringRepr(doStr);
}


CG_stringRepr *CG_stringBuilder::CreateLoop(int indent, CG_outputRepr *control,
                                            CG_outputRepr *stmtList) const {
  if (stmtList == NULL) {
    delete control;
    return NULL;
  }
  else if (control == NULL)
    return static_cast<CG_stringRepr *>(stmtList);

  std::string ctrlStr = GetString(control);
  std::string stmtStr = GetString(stmtList);

  std::string indentStr = GetIndentSpaces(indent);

  std::string s = indentStr + ctrlStr + " {\n"
    + stmtStr 
    + indentStr + "}\n";

  return new CG_stringRepr(s);
}



CG_stringRepr *CG_stringBuilder::CreateInt(int num) const {
  std::string s = to_string(num);
  return new CG_stringRepr(s);
}



bool CG_stringBuilder::isInteger(CG_outputRepr *op) const {

	 char * cstr;
     std::string s = GetString(op);
     cstr = new char [s.size()+1];
     strcpy (cstr, s.c_str());
     int count = 0;
     while(cstr[count] != '\n' && cstr[count] != '\0' )
        if( !isdigit(cstr[count]))
    	   return false;


     return true;
}



CG_stringRepr *CG_stringBuilder::CreateIdent(const std::string &varName) const {
  if (varName == std::string("")) {
    return NULL;
  }

  return new CG_stringRepr(varName);
}


CG_stringRepr *CG_stringBuilder::CreatePlus(CG_outputRepr *lop, CG_outputRepr *rop) const {
  if (rop == NULL) {
    return static_cast<CG_stringRepr *>(lop);
  }
  else if (lop == NULL) {
    return static_cast<CG_stringRepr *>(rop);
  }

  std::string lopStr = GetString(lop);
  std::string ropStr = GetString(rop);

  return new CG_stringRepr(lopStr + "+" + ropStr);
}


CG_stringRepr *CG_stringBuilder::CreateMinus(CG_outputRepr *lop, CG_outputRepr *rop) const {
  if (rop == NULL) {
    return static_cast<CG_stringRepr *>(lop);
  }
  else if (lop == NULL) {
    std::string ropStr = GetString(rop);
    return new CG_stringRepr("-" + SafeguardString(ropStr, '-'));
  }

  std::string lopStr = GetString(lop);
  std::string ropStr = GetString(rop);

  return new CG_stringRepr(lopStr + "-" + SafeguardString(ropStr, '-'));
}


CG_stringRepr *CG_stringBuilder::CreateTimes(CG_outputRepr *lop, CG_outputRepr *rop) const {
  if (rop == NULL || lop == NULL) {
    delete rop;
    delete lop;
    return NULL;
  }

  std::string lopStr = GetString(lop);
  std::string ropStr = GetString(rop);

  return new CG_stringRepr(SafeguardString(lopStr, '*') + "*" + SafeguardString(ropStr, '*'));
}


CG_stringRepr *CG_stringBuilder::CreateDivide(CG_outputRepr *lop, CG_outputRepr *rop) const {
  if (rop == NULL)
    throw codegen_error("integer division by zero");
  else if (lop == NULL) {
    delete rop;
    return NULL;
  }

  std::string lopStr = GetString(lop);
  std::string ropStr = GetString(rop);

  return new CG_stringRepr(SafeguardString(lopStr, '/') + "/" + SafeguardString(ropStr, '/'));
}


CG_stringRepr *CG_stringBuilder::CreateIntegerFloor(CG_outputRepr *lop, CG_outputRepr *rop) const {
  if (rop == NULL)
    throw codegen_error("integer division by zero");
  else if (lop == NULL) {
    delete rop;
    return NULL;
  }

  std::string lopStr = GetString(lop);
  std::string ropStr = GetString(rop);

  return new CG_stringRepr("intFloor(" + lopStr + "," + ropStr + ")");
}


CG_stringRepr *CG_stringBuilder::CreateIntegerMod(CG_outputRepr *lop, CG_outputRepr *rop) const {
  if (rop == NULL)
    throw codegen_error("integer modulo by zero");
  else if (lop == NULL) {
    delete rop;
    return NULL;
  }

  std::string lopStr = GetString(lop);
  std::string ropStr = GetString(rop);

  return new CG_stringRepr("intMod(" + lopStr + "," + ropStr + ")");
}

CG_stringRepr *CG_stringBuilder::CreateIntegerCeil(CG_outputRepr *lop, CG_outputRepr *rop) const {
  if (rop == 0)
    throw codegen_error("integer ceiling by zero");
  else if (lop == NULL) {
    delete rop;
    return NULL;
  }

  std::string lopStr = GetString(lop);
  std::string ropStr = GetString(rop);

  return new CG_stringRepr("intCeil(" + lopStr + "," + ropStr + ")");
}


CG_stringRepr *CG_stringBuilder::CreateAnd(CG_outputRepr *lop, CG_outputRepr *rop) const {
  if (rop == NULL)
    return static_cast<CG_stringRepr *>(lop);
  else if (lop == NULL)
    return static_cast<CG_stringRepr *>(rop);

  std::string lopStr = GetString(lop);
  std::string ropStr = GetString(rop);

  return new CG_stringRepr(lopStr + " && " + ropStr);
}


CG_stringRepr *CG_stringBuilder::CreateGE(CG_outputRepr *lop, CG_outputRepr *rop) const {
  if (rop == NULL || lop == NULL)
    throw std::invalid_argument("missing operand in greater than equal comparison condition");

  std::string lopStr = GetString(lop);
  std::string ropStr = GetString(rop);

  return new CG_stringRepr(lopStr + " >= " + ropStr);
}



CG_stringRepr *CG_stringBuilder::CreateLE(CG_outputRepr *lop, CG_outputRepr *rop) const {
  if (rop == NULL || lop == NULL)
    throw std::invalid_argument("missing operand in less than equal comparison condition");

  std::string lopStr = GetString(lop);
  std::string ropStr = GetString(rop);

  return new CG_stringRepr(lopStr + " <= " + ropStr);
}



CG_stringRepr *CG_stringBuilder::CreateEQ(CG_outputRepr *lop, CG_outputRepr *rop) const {
  if (rop == NULL || lop == NULL)
    throw std::invalid_argument("missing operand in equal comparison condition");

  std::string lopStr = GetString(lop);
  std::string ropStr = GetString(rop);

  return new CG_stringRepr(lopStr + " == " + ropStr);
}



CG_stringRepr *CG_stringBuilder::StmtListAppend(CG_outputRepr *list1, CG_outputRepr *list2) const {
  if (list2 == NULL) {
    return static_cast<CG_stringRepr *>(list1);
  }
  else if (list1 == NULL) {
    return static_cast<CG_stringRepr *>(list2);
  }

  std::string list1Str = GetString(list1);
  std::string list2Str = GetString(list2);

  return new CG_stringRepr(list1Str + list2Str);
}

}