summaryrefslogtreecommitdiff
path: root/lib/chillcg/src/CG_chillRepr.cc
blob: a47ecbc7a8c61b6c9fd1cc8a9a4c3fd2a32f7fd1 (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
/*****************************************************************************
 Copyright (C) 2008 University of Southern California. 
 All Rights Reserved.

 Purpose:
   omega holder for chill AST  implementaion

 Notes:

 History:
   02/01/06 - Chun Chen - created
   LLVM/CLANG interface created by Saurav Muralidharan
*****************************************************************************/

#include <code_gen/CG_chillRepr.h>
#include <stdio.h>
#include <stdlib.h>  // for exit()

namespace omega { 

  CG_chillRepr::~CG_chillRepr() {
  }
  
  //void CG_chillRepr::AppendStmt(Stmt *s) const {
  //      tree_node_list_->push_back(s);
  //}
  
  //void CG_chillRepr::AppendV(StmtList *sl) const {
  //      for(int i=0; i<sl->size(); ++i) tree_node_list_->push_back((*sl)[i]);
  //}
  

  chillAST_Node * CG_chillRepr::GetCode() {
    //fprintf(stderr, "CG_chillRepr::GetCode() this %p   size %d\n", this, chillnodes.size()); 

    if (0 == chillnodes.size()) return NULL; // error?
        
    if (1 == chillnodes.size()) return chillnodes[0];

    // make a compoundstatement with all the code ???  probably should be that way already
    fprintf(stderr, "CG_chillRepr GetCode(), multiple (%d) statements in the code??\n", chillnodes.size());
    for (int i=0; i<chillnodes.size(); i++) {
      fprintf(stderr, "chillnode %d  %p\n", i, chillnodes[i] );
    }


    chillAST_CompoundStmt *CS = new chillAST_CompoundStmt();
    for (int i=0; i<chillnodes.size(); i++) {
      CS->addChild( chillnodes[i] );
    }
    return CS; 
  }
  


  CG_outputRepr* CG_chillRepr::clone() const {  // make a deep/shallow  COPY of all the nodes
    //fprintf(stderr, "CG_chillRepr::clone()  %d chill nodes\n", chillnodes.size()); 
    //for (int i=0; i<chillnodes.size(); i++) { chillnodes[i]->print(); printf("\n"); } fflush(stdout);

    CG_chillRepr *newrepr = new  CG_chillRepr(); // empty
    
    // deep ( nothing is the same as in the source )
    for (int i=0; i<chillnodes.size(); i++) newrepr->addStatement( chillnodes[i]->clone() ); 

    // shallow (the submembers are the same ) 
    //for (int i=0; i<chillnodes.size(); i++) newrepr->addStatement( chillnodes[i] ); 
    //fprintf(stderr, "done cloning\n"); 
    return newrepr; 
  }


  
  void CG_chillRepr::clear() {
    chillnodes.clear();
  }
  
  
  // TODO this is duplicated and shouldn't be here anyway 
  static std::string binops[] = {
    " ", " ",             // BO_PtrMemD, BO_PtrMemI,       // [C++ 5.5] Pointer-to-member operators.
  "*", "/", "%",        // BO_Mul, BO_Div, BO_Rem,       // [C99 6.5.5] Multiplicative operators.
  "+", "-",             // BO_Add, BO_Sub,               // [C99 6.5.6] Additive operators.
  "<<", ">>",           // BO_Shl, BO_Shr,               // [C99 6.5.7] Bitwise shift operators.
  "<", ">", "<=", ">=", // BO_LT, BO_GT, BO_LE, BO_GE,   // [C99 6.5.8] Relational operators.
  "==", "!=",           // BO_EQ, BO_NE,                 // [C99 6.5.9] Equality operators.
  "&",                  // BO_And,                       // [C99 6.5.10] Bitwise AND operator.
  "??",                 // BO_Xor,                       // [C99 6.5.11] Bitwise XOR operator.
  "|",                  // BO_Or,                        // [C99 6.5.12] Bitwise OR operator.
  "&&",                 // BO_LAnd,                      // [C99 6.5.13] Logical AND operator.
  "||",                 // BO_LOr,                       // [C99 6.5.14] Logical OR operator.
  "=", "*=",            // BO_Assign, BO_MulAssign,      // [C99 6.5.16] Assignment operators.
  "/=", "%=",           // BO_DivAssign, BO_RemAssign,
  "+=", "-=",           // BO_AddAssign, BO_SubAssign,
  "???", "???",         // BO_ShlAssign, BO_ShrAssign,
  "&&=", "???",         // BO_AndAssign, BO_XorAssign,
  "||=",                // BO_OrAssign,
  ","};                 // BO_Comma                      // [C99 6.5.17] Comma operator.


  static std::string unops[] = {
  "++", "--",           // [C99 6.5.2.4] Postfix increment and decrement
  "++", "--",           // [C99 6.5.3.1] Prefix increment and decrement
  "@",  "*",            // [C99 6.5.3.2] Address and indirection
  "+", "-",             // [C99 6.5.3.3] Unary arithmetic
  "~", "!",             // [C99 6.5.3.3] Unary arithmetic
  "__real", "__imag",   // "__real expr"/"__imag expr" Extension.
  "__extension"          // __extension__ marker.
  };



  //void CG_chillRepr::dump() const { Dump(); }


  void CG_chillRepr::Dump() const {
    CG_chillRepr *me = (CG_chillRepr *)this;  // ?? 
    //fprintf(stderr, "repr of type ");
    //fprintf(stderr, "%s\n", this->type()); 
    int numnodes = me->chillnodes.size();
    //fprintf(stderr, "repr %p  %d nodes\n", this, numnodes); 
    for (int i=0; i<numnodes; i++) {
      me->chillnodes[i]->print();  printf("\n"); fflush(stdout);
    }
    return; 
  }  
  
} // namespace