Skip to content

Commit

Permalink
Refactor macros in arrayop.c
Browse files Browse the repository at this point in the history
  • Loading branch information
yebblies committed Jun 22, 2013
1 parent 2c6b5e6 commit c5297b8
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 110 deletions.
182 changes: 92 additions & 90 deletions src/arrayop.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,30 +465,30 @@ void AssignExp::buildArrayIdent(OutBuffer *buf, Expressions *arguments)
buf->writestring("Assign");
}

#define X(Str) \
void Str##AssignExp::buildArrayIdent(OutBuffer *buf, Expressions *arguments) \
{ \
/* Evaluate assign expressions right to left \
*/ \
e2->buildArrayIdent(buf, arguments); \
e1->buildArrayIdent(buf, arguments); \
buf->writestring(#Str); \
buf->writestring("ass"); \
}

X(Add)
X(Min)
X(Mul)
X(Div)
X(Mod)
X(Xor)
X(And)
X(Or)
void BinAssignExp::buildArrayIdent(OutBuffer *buf, Expressions *arguments)
{
/* Evaluate assign expressions right to left
*/
e2->buildArrayIdent(buf, arguments);
e1->buildArrayIdent(buf, arguments);
const char *s;
switch(op)
{
case TOKaddass: s = "Addass"; break;
case TOKminass: s = "Subass"; break;
case TOKmulass: s = "Mulass"; break;
case TOKdivass: s = "Divass"; break;
case TOKmodass: s = "Modass"; break;
case TOKxorass: s = "Xorass"; break;
case TOKandass: s = "Andass"; break;
case TOKorass: s = "Orass"; break;
#if DMDV2
X(Pow)
case TOKpowass: s = "Powass"; break;
#endif

#undef X
default: assert(0);
}
buf->writestring(s);
}

void NegExp::buildArrayIdent(OutBuffer *buf, Expressions *arguments)
{
Expand All @@ -502,29 +502,30 @@ void ComExp::buildArrayIdent(OutBuffer *buf, Expressions *arguments)
buf->writestring("Com");
}

#define X(Str) \
void Str##Exp::buildArrayIdent(OutBuffer *buf, Expressions *arguments) \
{ \
/* Evaluate assign expressions left to right \
*/ \
e1->buildArrayIdent(buf, arguments); \
e2->buildArrayIdent(buf, arguments); \
buf->writestring(#Str); \
}

X(Add)
X(Min)
X(Mul)
X(Div)
X(Mod)
X(Xor)
X(And)
X(Or)
void BinExp::buildArrayIdent(OutBuffer *buf, Expressions *arguments)
{
/* Evaluate assign expressions left to right
*/
e1->buildArrayIdent(buf, arguments);
e2->buildArrayIdent(buf, arguments);
const char *s;
switch(op)
{
case TOKadd: s = "Add"; break;
case TOKmin: s = "Sub"; break;
case TOKmul: s = "Mul"; break;
case TOKdiv: s = "Div"; break;
case TOKmod: s = "Mod"; break;
case TOKxor: s = "Xor"; break;
case TOKand: s = "And"; break;
case TOKor: s = "Or"; break;
#if DMDV2
X(Pow)
case TOKpow: s = "Pow"; break;
#endif

#undef X
default: assert(0);
}
buf->writestring(s);
}

/******************************************
* Construct the inner loop for the array operation function,
Expand Down Expand Up @@ -584,32 +585,33 @@ Expression *AssignExp::buildArrayLoop(Parameters *fparams)
return e;
}

#define X(Str) \
Expression *Str##AssignExp::buildArrayLoop(Parameters *fparams) \
{ \
/* Evaluate assign expressions right to left \
*/ \
Expression *ex2 = e2->buildArrayLoop(fparams); \
Expression *ex1 = e1->buildArrayLoop(fparams); \
Parameter *param = (*fparams)[0]; \
param->storageClass = 0; \
Expression *e = new Str##AssignExp(loc, ex1, ex2); \
return e; \
}

X(Add)
X(Min)
X(Mul)
X(Div)
X(Mod)
X(Xor)
X(And)
X(Or)
Expression *BinAssignExp::buildArrayLoop(Parameters *fparams)
{
/* Evaluate assign expressions right to left
*/
Expression *ex2 = e2->buildArrayLoop(fparams);
Expression *ex1 = e1->buildArrayLoop(fparams);
Parameter *param = (*fparams)[0];
param->storageClass = 0;
Expression *e;
switch(op)
{
case TOKaddass: return new AddAssignExp(loc, ex1, ex2);
case TOKminass: return new MinAssignExp(loc, ex1, ex2);
case TOKmulass: return new MulAssignExp(loc, ex1, ex2);
case TOKdivass: return new DivAssignExp(loc, ex1, ex2);
case TOKmodass: return new ModAssignExp(loc, ex1, ex2);
case TOKxorass: return new XorAssignExp(loc, ex1, ex2);
case TOKandass: return new AndAssignExp(loc, ex1, ex2);
case TOKorass: return new OrAssignExp(loc, ex1, ex2);
#if DMDV2
X(Pow)
case TOKpowass: return new PowAssignExp(loc, ex1, ex2);
#endif

#undef X
default:
assert(0);
return NULL;
}
}

Expression *NegExp::buildArrayLoop(Parameters *fparams)
{
Expand All @@ -625,31 +627,31 @@ Expression *ComExp::buildArrayLoop(Parameters *fparams)
return e;
}

#define X(Str) \
Expression *Str##Exp::buildArrayLoop(Parameters *fparams) \
{ \
/* Evaluate assign expressions left to right \
*/ \
Expression *ex1 = e1->buildArrayLoop(fparams); \
Expression *ex2 = e2->buildArrayLoop(fparams); \
Expression *e = new Str##Exp(Loc(), ex1, ex2); \
return e; \
}

X(Add)
X(Min)
X(Mul)
X(Div)
X(Mod)
X(Xor)
X(And)
X(Or)
Expression *BinExp::buildArrayLoop(Parameters *fparams)
{
/* Evaluate assign expressions left to right
*/
Expression *ex1 = e1->buildArrayLoop(fparams);
Expression *ex2 = e2->buildArrayLoop(fparams);
Expression *e;
switch(op)
{
case TOKadd: return new AddExp(Loc(), ex1, ex2);
case TOKmin: return new MinExp(Loc(), ex1, ex2);
case TOKmul: return new MulExp(Loc(), ex1, ex2);
case TOKdiv: return new DivExp(Loc(), ex1, ex2);
case TOKmod: return new ModExp(Loc(), ex1, ex2);
case TOKxor: return new XorExp(Loc(), ex1, ex2);
case TOKand: return new AndExp(Loc(), ex1, ex2);
case TOKor: return new OrExp(Loc(), ex1, ex2);
#if DMDV2
X(Pow)
case TOKpow: return new PowExp(Loc(), ex1, ex2);
#endif

#undef X

default:
assert(0);
return NULL;
}
}

/***********************************************
* Test if operand is a valid array op operand.
Expand Down
26 changes: 6 additions & 20 deletions src/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,9 @@ class BinExp : public Expression
Expression *compare_overload(Scope *sc, Identifier *id);
Expression *reorderSettingAAElem(Scope *sc);

void buildArrayIdent(OutBuffer *buf, Expressions *arguments);
Expression *buildArrayLoop(Parameters *fparams);

elem *toElemBin(IRState *irs, int op);
};

Expand All @@ -926,6 +929,9 @@ class BinAssignExp : public BinExp

Expression *op_overload(Scope *sc);

void buildArrayIdent(OutBuffer *buf, Expressions *arguments);
Expression *buildArrayLoop(Parameters *fparams);

int isLvalue();
Expression *toLvalue(Scope *sc, Expression *ex);
Expression *modifiableLvalue(Scope *sc, Expression *e);
Expand Down Expand Up @@ -1400,8 +1406,6 @@ class op##AssignExp : public BinAssignExp \
public: \
op##AssignExp(Loc loc, Expression *e1, Expression *e2); \
S(Expression *semantic(Scope *sc);) \
X(void buildArrayIdent(OutBuffer *buf, Expressions *arguments);) \
X(Expression *buildArrayLoop(Parameters *fparams);) \
\
Identifier *opId(); /* For operator overloading */ \
\
Expand Down Expand Up @@ -1450,8 +1454,6 @@ class AddExp : public BinExp
AddExp(Loc loc, Expression *e1, Expression *e2);
Expression *semantic(Scope *sc);
Expression *optimize(int result, bool keepLvalue = false);
void buildArrayIdent(OutBuffer *buf, Expressions *arguments);
Expression *buildArrayLoop(Parameters *fparams);
IntRange getIntRange();

// For operator overloading
Expand All @@ -1468,8 +1470,6 @@ class MinExp : public BinExp
MinExp(Loc loc, Expression *e1, Expression *e2);
Expression *semantic(Scope *sc);
Expression *optimize(int result, bool keepLvalue = false);
void buildArrayIdent(OutBuffer *buf, Expressions *arguments);
Expression *buildArrayLoop(Parameters *fparams);
IntRange getIntRange();

// For operator overloading
Expand Down Expand Up @@ -1500,8 +1500,6 @@ class MulExp : public BinExp
MulExp(Loc loc, Expression *e1, Expression *e2);
Expression *semantic(Scope *sc);
Expression *optimize(int result, bool keepLvalue = false);
void buildArrayIdent(OutBuffer *buf, Expressions *arguments);
Expression *buildArrayLoop(Parameters *fparams);
IntRange getIntRange();

// For operator overloading
Expand All @@ -1518,8 +1516,6 @@ class DivExp : public BinExp
DivExp(Loc loc, Expression *e1, Expression *e2);
Expression *semantic(Scope *sc);
Expression *optimize(int result, bool keepLvalue = false);
void buildArrayIdent(OutBuffer *buf, Expressions *arguments);
Expression *buildArrayLoop(Parameters *fparams);
IntRange getIntRange();

// For operator overloading
Expand All @@ -1535,8 +1531,6 @@ class ModExp : public BinExp
ModExp(Loc loc, Expression *e1, Expression *e2);
Expression *semantic(Scope *sc);
Expression *optimize(int result, bool keepLvalue = false);
void buildArrayIdent(OutBuffer *buf, Expressions *arguments);
Expression *buildArrayLoop(Parameters *fparams);
IntRange getIntRange();

// For operator overloading
Expand All @@ -1553,8 +1547,6 @@ class PowExp : public BinExp
PowExp(Loc loc, Expression *e1, Expression *e2);
Expression *semantic(Scope *sc);
Expression *optimize(int result, bool keepLvalue = false);
void buildArrayIdent(OutBuffer *buf, Expressions *arguments);
Expression *buildArrayLoop(Parameters *fparams);

// For operator overloading
Identifier *opId();
Expand Down Expand Up @@ -1615,8 +1607,6 @@ class AndExp : public BinExp
AndExp(Loc loc, Expression *e1, Expression *e2);
Expression *semantic(Scope *sc);
Expression *optimize(int result, bool keepLvalue = false);
void buildArrayIdent(OutBuffer *buf, Expressions *arguments);
Expression *buildArrayLoop(Parameters *fparams);
IntRange getIntRange();

// For operator overloading
Expand All @@ -1633,8 +1623,6 @@ class OrExp : public BinExp
OrExp(Loc loc, Expression *e1, Expression *e2);
Expression *semantic(Scope *sc);
Expression *optimize(int result, bool keepLvalue = false);
void buildArrayIdent(OutBuffer *buf, Expressions *arguments);
Expression *buildArrayLoop(Parameters *fparams);
MATCH implicitConvTo(Type *t);
IntRange getIntRange();

Expand All @@ -1652,8 +1640,6 @@ class XorExp : public BinExp
XorExp(Loc loc, Expression *e1, Expression *e2);
Expression *semantic(Scope *sc);
Expression *optimize(int result, bool keepLvalue = false);
void buildArrayIdent(OutBuffer *buf, Expressions *arguments);
Expression *buildArrayLoop(Parameters *fparams);
MATCH implicitConvTo(Type *t);
IntRange getIntRange();

Expand Down

0 comments on commit c5297b8

Please sign in to comment.