summaryrefslogtreecommitdiff
path: root/lib/codegen/src/CG_stringBuilder.cc
blob: d0f66932aa974239f4dd72a1f880dd21141f2c95 (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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/*****************************************************************************
 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, 
                                                         bool actuallyPrint) 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::CreateStatementFromExpression(CG_outputRepr *exp) const {
    std::string expStr = GetString(exp);
    return new CG_stringRepr(expStr + ";\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::CreatePlusAssignment(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::CreateAddressOf(CG_outputRepr *op) const {
	  if (op == NULL)
	    throw std::invalid_argument("missining op in create address of");
    
    std::string opStr = GetString(op);
    
    
    return new CG_stringRepr( "&" + opStr);
  }
  
  
  CG_stringRepr *CG_stringBuilder::CreateInvoke(const std::string &funcName,
                                                std::vector<CG_outputRepr *> &list,
                                                bool is_array) const {
    fprintf(stderr, "CG_stringBuilder::CreateInvoke( %s, ..., is_array  ", funcName.c_str());
    if (is_array) fprintf(stderr, " true )\n"); 
    else fprintf(stderr, " false )\n"); 


    std::string listStr = "";
    
    fprintf(stderr, "list has %d elements\n", list.size());

    for (int i = 0; i < list.size(); i++) {
      fprintf(stderr, "accessing list[%d]\n", i); 
      listStr += GetString(list[i]);
      if ( i < list.size()-1)
        listStr += ",";
    }

    fprintf(stderr, "returning %s\n", (funcName + "(" + listStr + ")").c_str()); 
    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 {
    
    //fprintf(stderr, "CG_stringBuilder::CreateAttribute( jkadskjh, '%s')\n", commentText.c_str());

    if (commentText == std::string("")) {
      return static_cast<CG_stringRepr *> (control);
    }
    
    std::string controlString = GetString(control);
    
    std::string spaces = "";
    const char *con = controlString.c_str();
    const char *ptr = con;
    while (*ptr++ == ' ') spaces = spaces + " "; 
    return new CG_stringRepr(spaces + "// " + 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::CreateBreakStatement(void) const {
    std::string s= "break;\n";
    return new CG_stringRepr(s);
  }
  
  
  
  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;
    
    if (step != NULL) {
      std::string stepStr = GetString(step);
      if (stepStr == to_string(1))
        doStr += "++";
      else
        doStr += " += " + stepStr;
    }
    else
      doStr += "++";  // a default ?? 
    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);
  }
  
  CG_stringRepr *CG_stringBuilder::CreateFloat(float num) const {
    std::string s = to_string(num);
    return new CG_stringRepr(s);
  }
  
  CG_stringRepr *CG_stringBuilder::CreateDouble(double 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;
  }
  
  bool CG_stringBuilder::QueryInspectorType(const std::string &varName) const{
    if (varName == std::string("index")) {  // special cased? ??????  TODO 
	    return true;
	  }
	  return false;
  }
  
  CG_stringRepr* CG_stringBuilder::ObtainInspectorData(const std::string &_s, const std::string &member_name) const {
    return new CG_stringRepr(_s);
  }
  
  
  
  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::CreateDotExpression(CG_outputRepr *lop,
                                                       CG_outputRepr *rop) const {
    
    std::string op1 = GetString(lop);
    std::string op2 = GetString(rop);
    
    std::string s = op1 + "." + op2;
    return new CG_stringRepr(s);
  }
  
  
  
  CG_stringRepr* CG_stringBuilder::CreateNullStatement() const{
    return new CG_stringRepr("");
  }
  
  
  
  CG_stringRepr* CG_stringBuilder::CreateArrayRefExpression(const std::string &_s,
                                                            CG_outputRepr *rop) const {
    if (_s == std::string("")) {
      return NULL;
    }
    
    std::string refStr = GetString(rop);
    //std::string s = _s + "[" + refStr + "]";
    
    return new CG_stringRepr( _s + "[" + refStr + "]");
  }
  
  
  
  CG_stringRepr* CG_stringBuilder::CreateArrayRefExpression(CG_outputRepr *lop,
                                                            CG_outputRepr *rop) const {
    
    std::string refStr1 = GetString(lop);
    std::string refStr2 = GetString(rop);
    //std::string s = _s + "[" + refStr + "]";
    
    return new CG_stringRepr( refStr1 + "[" + refStr2 + "]");
    
  }
  
  
  
  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::CreateNEQ(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);
  }
  
  CG_outputRepr *CG_stringBuilder::CreateStruct(const std::string struct_name,
                                                std::vector<std::string> data_members,
                                                std::vector<CG_outputRepr *> data_types)
  { 
    fprintf(stderr, "CG_stringBuilder::CreateStruct( %s )\n", struct_name.c_str()); 
    fprintf(stderr, "that makes no sense\n"); 
    exit(0); 
  }
  
  CG_outputRepr *CG_stringBuilder::CreateClassInstance(std::string name , 
                                                      CG_outputRepr *class_def){
    fprintf(stderr, "CG_stringBuilder::CreateClassInstance( %s )\n", name.c_str()); 
    exit(0); 
    
  }
  
	CG_outputRepr *CG_stringBuilder::lookup_member_data(CG_outputRepr* scope, 
                                                     std::string varName, 
                                                     CG_outputRepr *instance) {
    fprintf(stderr, "CG_stringBuilder::lookup_member_data( )\n"); 
    exit(0); 
  }
  
  CG_outputRepr* CG_stringBuilder::CreatePointer(std::string  &name) const { 
    fprintf(stderr, "CG_chillBuilder::CreatePointer( %s )\n", name.c_str()); 
    exit(0); 
  }

	CG_outputRepr* CG_stringBuilder::ObtainInspectorRange(const std::string &_s, const std::string &_name) const {
    fprintf(stderr, "CG_stringBuilder::ObtainInspectorRange(%s,  %s )\n", _s.c_str(), _name.c_str()); 
    exit(0); 
  }


  
}