summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/chillAST/chillAST_def.hh2
-rw-r--r--include/chillAST/chillASTs.hh13
-rwxr-xr-xlib/chillcg/src/CG_chillBuilder.cc11
-rw-r--r--src/chillASTs.cc86
-rw-r--r--src/printer/cfamily.cpp12
5 files changed, 50 insertions, 74 deletions
diff --git a/include/chillAST/chillAST_def.hh b/include/chillAST/chillAST_def.hh
index 7a9528e..e246943 100644
--- a/include/chillAST/chillAST_def.hh
+++ b/include/chillAST/chillAST_def.hh
@@ -199,7 +199,7 @@ chillAST_VarDecl *symbolTableFindVariableNamed(chillAST_SymbolTable *table,
chillAST_TypedefDecl *typedefTableFindName(chillAST_TypedefTable *table, const char *name);
-chillAST_Node *lessthanmacro(chillAST_Node *left, chillAST_Node *right);
+chillAST_Node *minmaxTernary(const char *op, chillAST_Node *left, chillAST_Node *right);
chillAST_SymbolTable *addSymbolToTable(chillAST_SymbolTable *st, chillAST_VarDecl *vd);
diff --git a/include/chillAST/chillASTs.hh b/include/chillAST/chillASTs.hh
index f49cf6f..67189dc 100644
--- a/include/chillAST/chillASTs.hh
+++ b/include/chillAST/chillASTs.hh
@@ -265,7 +265,6 @@ public:
// required methods
void replaceChild(chillAST_Node *old, chillAST_Node *newchild);
- chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -409,8 +408,6 @@ public:
//void replaceVarDecls( chillAST_VarDecl *olddecl, chillAST_VarDecl *newdecl);
bool findLoopIndexesToReplace(chillAST_SymbolTable *symtab, bool forcesync = false);
- chillAST_Node *constantFold();
-
}; // end FunctionDecl
class chillAST_SourceFile : public chillAST_Node {
@@ -596,9 +593,9 @@ public:
chillAST_Node *getCond() { return getChild(0); };
- chillAST_Node *getRHS() { return getChild(1); };
+ chillAST_Node *getLHS() { return getChild(1); };
- chillAST_Node *getLHS() { return getChild(2); };
+ chillAST_Node *getRHS() { return getChild(2); };
void setCond(chillAST_Node *newc) { setChild(0, newc); }
@@ -811,8 +808,6 @@ public:
int evalAsInt() { return value; }
// required methods that I can't seem to get to inherit
- chillAST_Node *constantFold();
-
chillAST_Node *clone();
bool findLoopIndexesToReplace(chillAST_SymbolTable *symtab,
@@ -843,8 +838,6 @@ public:
int getPrecision() { return precision; }
// required methods that I can't seem to get to inherit
- chillAST_Node *constantFold();
-
chillAST_Node *clone();
bool findLoopIndexesToReplace(chillAST_SymbolTable *symtab,
@@ -944,6 +937,8 @@ public:
// required methods that I can't seem to get to inherit
chillAST_Node *getSubExpr() { return getChild(0); }
+ chillAST_Node *constantFold();
+
void setSubExpr(chillAST_Node *sub) { return setChild(0, sub); }
chillAST_Node *clone();
diff --git a/lib/chillcg/src/CG_chillBuilder.cc b/lib/chillcg/src/CG_chillBuilder.cc
index 966c20f..74622d7 100755
--- a/lib/chillcg/src/CG_chillBuilder.cc
+++ b/lib/chillcg/src/CG_chillBuilder.cc
@@ -246,12 +246,11 @@ namespace omega {
CG_outputRepr *CGOR;
CG_chillRepr *CGCR;
char macroname[32];
- char op;
- if (fname == std::string("max")) op = '>';
- else op = '<';
- // TODO >, check number of args etc
- chillAST_Node *ternary = lessthanmacro( ((CG_chillRepr*) list[0])->chillnodes[0],
- ((CG_chillRepr*) list[1])->chillnodes[0]);
+ const char * op;
+ if (fname == std::string("max")) op = ">";
+ else op = "<";
+ chillAST_Node *ternary = minmaxTernary( op, ((CG_chillRepr*) list[0])->chillnodes[0],
+ ((CG_chillRepr*) list[1])->chillnodes[0]);
CG_chillRepr *repr = new CG_chillRepr( ternary );
return repr;
}
diff --git a/src/chillASTs.cc b/src/chillASTs.cc
index 6f9f845..88d2df5 100644
--- a/src/chillASTs.cc
+++ b/src/chillASTs.cc
@@ -521,12 +521,6 @@ bool chillAST_FunctionDecl::findLoopIndexesToReplace(chillAST_SymbolTable *symta
return false;
}
-
-chillAST_Node *chillAST_FunctionDecl::constantFold() {
- if (getBody()) setBody((chillAST_CompoundStmt *) getBody()->constantFold());
- return this;
-}
-
chillAST_MacroDefinition::chillAST_MacroDefinition(const char *mname = NULL, const char *rhs = NULL) {
if (mname) macroName = strdup(mname); else macroName = strdup("UNDEFINEDMACRO");
metacomment = NULL;
@@ -1039,13 +1033,8 @@ chillAST_BinaryOperator::chillAST_BinaryOperator() {
chillAST_BinaryOperator::chillAST_BinaryOperator(chillAST_Node *l, const char *oper, chillAST_Node *r)
: chillAST_BinaryOperator() {
- //fprintf(stderr, "chillAST_BinaryOperator::chillAST_BinaryOperator( l %p %s r %p, parent %p) this %p\n", l, oper, r, par, this);
CHILL_DEBUG_PRINT("( l %s r )\n", oper);
- //if (l && r ) {
- // fprintf(stderr, "("); l->print(0,stderr); fprintf(stderr, ") %s (", oper); r->print(0,stderr); fprintf(stderr, ")\n\n");
- //}
-
setLHS(l);
setRHS(r);
@@ -1055,13 +1044,10 @@ chillAST_BinaryOperator::chillAST_BinaryOperator(chillAST_Node *l, const char *o
if (isAssignmentOp()) {
if (l && l->isArraySubscriptExpr()) {
((chillAST_ArraySubscriptExpr *) l)->imwrittento = true;
- //fprintf(stderr, "chillAST_BinaryOperator, op '=', lhs is an array reference LVALUE\n");
}
}
if (isAugmentedAssignmentOp()) { // += etc
- //fprintf(stderr, "isAugmentedAssignmentOp() "); print(); fflush(stdout);
if (l && l->isArraySubscriptExpr()) {
- //fprintf(stderr, "lhs is also read from "); lhs->print(); fflush(stdout);
((chillAST_ArraySubscriptExpr *) l)->imreadfrom = true; // note will ALSO have imwrittento true
}
}
@@ -1097,8 +1083,6 @@ class chillAST_Node *chillAST_BinaryOperator::constantFold() {
chillAST_Node *returnval = this;
if (getLHS()->isConstant() && getRHS()->isConstant()) {
- //fprintf(stderr, "binop folding constants\n"); print(0,stderr); fprintf(stderr, "\n");
-
if (!strcmp(op, "+") || !strcmp(op, "-") || !strcmp(op, "*")) {
if (getLHS()->isIntegerLiteral() && getRHS()->isIntegerLiteral()) {
chillAST_IntegerLiteral *l = (chillAST_IntegerLiteral *) getLHS();
@@ -1110,34 +1094,33 @@ class chillAST_Node *chillAST_BinaryOperator::constantFold() {
if (!strcmp(op, "*")) I = new chillAST_IntegerLiteral(l->value * r->value);
returnval = I;
- //fprintf(stderr, "%d %s %d becomes %d\n", l->value,op, r->value, I->value);
- } else { // at least one is a float
+ } else {
// usually don't want to do this for floats or doubles
// could probably check for special cases like 0.0/30.0 or X/X or X/1.0
-#ifdef FOLDFLOATS
- float lval, rval;
- if (lhs->isIntegerLiteral()) {
- lval = (float) ((chillAST_IntegerLiteral *)lhs)->value;
- }
+ double lval, rval;
+ int prec = 1;
+ if (getLHS()->isIntegerLiteral())
+ lval = ((chillAST_IntegerLiteral *)getLHS())->value;
else {
- lval = ((chillAST_FloatingLiteral *)lhs)->value;
+ lval = ((chillAST_FloatingLiteral *)getLHS())->value;
+ prec = max(prec, ((chillAST_FloatingLiteral *)getLHS())->getPrecision());
}
- if (rhs->isIntegerLiteral()) {
- rval = (float) ((chillAST_IntegerLiteral *)rhs)->value;
- }
+ if (getRHS()->isIntegerLiteral())
+ rval = ((chillAST_IntegerLiteral *)getRHS())->value;
else {
- rval = ((chillAST_FloatingLiteral *)rhs)->value;
+ rval = ((chillAST_FloatingLiteral *) getRHS())->value;
+ prec = max(prec, ((chillAST_FloatingLiteral *) getRHS())->getPrecision());
}
chillAST_FloatingLiteral *F;
- if (streq(op, "+")) F = new chillAST_FloatingLiteral(lval + rval, parent);
- if (streq(op, "-")) F = new chillAST_FloatingLiteral(lval - rval, parent);
- if (streq(op, "*")) F = new chillAST_FloatingLiteral(lval * rval, parent);
+ // NOT SAFE
+ // if (streq(op, "+")) F = new chillAST_FloatingLiteral(lval + rval, parent);
+ // if (streq(op, "-")) F = new chillAST_FloatingLiteral(lval - rval, parent);
+ if (streq(op, "*")) F = new chillAST_FloatingLiteral(lval * rval, prec, NULL);
returnval = F;
-#endif
}
}
@@ -1556,8 +1539,6 @@ chillAST_Node *chillAST_VarDecl::clone() {
vd->arraypointerpart = NULL;
if (arraypart != NULL && NULL != arraysizes) { // !strcmp(arraypart, "")) {
- //fprintf(stderr, "in chillAST_VarDecl::clone(), cloning the array info\n");
- //fprintf(stderr, "numdimensions %d arraysizes 0x%x\n", numdimensions, arraysizes) ;
vd->numdimensions = numdimensions;
if (arraysizes) {
@@ -1569,18 +1550,12 @@ chillAST_Node *chillAST_VarDecl::clone() {
}
}
- if (arraypointerpart) {
- //fprintf(stderr, "copying arraypointerpart\n");
+ if (arraypointerpart)
vd->arraypointerpart = strdup(arraypointerpart);
- }
vd->isStruct = this->isStruct;
//vd->insideAStruct = this->insideAStruct;
- //if (vd->isStruct) fprintf(stderr, "vardecl::clone() %s is a struct\n", varname);
- //else fprintf(stderr, "vardecl::clone() %s is NOT a struct\n", varname);
-
-
vd->knownArraySizes = this->knownArraySizes;
vd->isFromSourceFile = isFromSourceFile;
if (filename) vd->filename = strdup(filename);
@@ -1633,9 +1608,6 @@ chillAST_IntegerLiteral::chillAST_IntegerLiteral(int val) {
filename = NULL;
}
-class chillAST_Node *chillAST_IntegerLiteral::constantFold() { return this; } // can never do anything
-
-
class chillAST_Node *chillAST_IntegerLiteral::clone() {
chillAST_IntegerLiteral *IL = new chillAST_IntegerLiteral(value);
@@ -1665,8 +1637,6 @@ chillAST_FloatingLiteral::chillAST_FloatingLiteral(chillAST_FloatingLiteral *old
filename = NULL;
}
-chillAST_Node *chillAST_FloatingLiteral::constantFold() { return this; }; // NOOP
-
chillAST_Node *chillAST_FloatingLiteral::clone() {
//fprintf(stderr, "chillAST_FloatingLiteral::clone() ");
//fprintf(stderr, "allthedigits %p \n", allthedigits);
@@ -1713,7 +1683,6 @@ chillAST_Node *chillAST_UnaryOperator::constantFold() {
chillAST_IntegerLiteral *I = new chillAST_IntegerLiteral(-intval);
I->setParent(parent);
returnval = I;
- //fprintf(stderr, "integer -%d becomes %d\n", intval, I->value);
} else {
chillAST_FloatingLiteral *FL = (chillAST_FloatingLiteral *) getSubExpr();
chillAST_FloatingLiteral *F = new chillAST_FloatingLiteral(FL); // clone
@@ -1778,6 +1747,18 @@ chillAST_CStyleCastExpr::chillAST_CStyleCastExpr(const char *to, chillAST_Node *
setSubExpr(sub);
}
+chillAST_Node * chillAST_CStyleCastExpr::constantFold() {
+ chillAST_Node::constantFold();
+ if (getSubExpr()->isConstant() && getSubExpr()->isIntegerLiteral()) {
+ long long val = getSubExpr()->evalAsInt();
+ if (!strcmp(towhat,"float"))
+ return new chillAST_FloatingLiteral(val,1,NULL);
+ if (!strcmp(towhat, "double"))
+ return new chillAST_FloatingLiteral(val,2,NULL);
+ }
+ return this;
+}
+
class chillAST_Node *chillAST_CStyleCastExpr::clone() {
chillAST_CStyleCastExpr *CSCE = new chillAST_CStyleCastExpr(towhat, getSubExpr()->clone());
CSCE->setParent(getParent());
@@ -2337,13 +2318,6 @@ void chillAST_CompoundStmt::loseLoopWithLoopVar(char *var) {
//fprintf(stderr, "CompoundStmt node 0x%x done recursing\n", this );
}
-chillAST_Node *chillAST_CompoundStmt::constantFold() {
- //fprintf(stderr, "chillAST_CompoundStmt::constantFold()\n");
- for (int i = 0; i < children.size(); i++) children[i] = children[i]->constantFold();
- return this;
-}
-
-
chillAST_Node *chillAST_CompoundStmt::clone() {
chillAST_CompoundStmt *cs = new chillAST_CompoundStmt();
for (int i = 0; i < children.size(); i++) cs->addChild(children[i]->clone());
@@ -2610,11 +2584,11 @@ bool chillAST_IfStmt::findLoopIndexesToReplace(chillAST_SymbolTable *symtab, boo
}
-chillAST_Node *lessthanmacro(chillAST_Node *left, chillAST_Node *right) {
+chillAST_Node *minmaxTernary(const char * op, chillAST_Node *left, chillAST_Node *right) {
chillAST_Node *lp1 = left->clone();
chillAST_Node *rp1 = right->clone();
- chillAST_BinaryOperator *cond = new chillAST_BinaryOperator(lp1, "<", rp1);
+ chillAST_BinaryOperator *cond = new chillAST_BinaryOperator(lp1, op, rp1);
chillAST_Node *lp2 = left->clone();
chillAST_Node *rp2 = right->clone();
diff --git a/src/printer/cfamily.cpp b/src/printer/cfamily.cpp
index c328c67..37c3dd4 100644
--- a/src/printer/cfamily.cpp
+++ b/src/printer/cfamily.cpp
@@ -4,6 +4,7 @@
#include "printer/cfamily.h"
#include <iomanip>
+#include <limits>
using namespace std;
@@ -169,9 +170,16 @@ void CFamily::printS(std::string ident, chillAST_FloatingLiteral *n, std::ostrea
if (n->allthedigits)
o << n->allthedigits;
else {
- o << showpoint << n->value;
- if (n->getPrecision() == 1)
+ if (n->getPrecision() == 2)
+ o << setprecision(std::numeric_limits<double>::max_digits10) << n->value;
+ else {
+ ostringstream st;
+ st << setprecision(std::numeric_limits<float>::max_digits10) << n->value;
+ o << st.str();
+ if (st.str().find('.')==string::npos)
+ o << ".0";
o << "f";
+ }
}
}