/*************************************************************** * Function: AssignStmt::getSymbolUses() * Purpose : Get all symbols read/used by this expression * Initial : Maxime Chevalier-Boisvert on March 17, 2009 **************************************************************** Revisions and bug fixes: */ Expression::SymbolSet AssignStmt::getSymbolUses() const { // Create a set to store the defined symbols Expression::SymbolSet symbols; // Get the symbols used by the right-expression Expression::SymbolSet rightSymbols = m_pRightExpr->getSymbolUses(); // Add those symbols to the set symbols.insert(rightSymbols.begin(), rightSymbols.end()); // For each left-expression for (ExprVector::const_iterator itr = m_leftExprs.begin(); itr != m_leftExprs.end(); ++itr) { // Get a pointer to this expression Expression* pExpr = *itr; // Switch on the expression type switch (pExpr->getExprType()) { // Symbol expression case Expression::SYMBOL: break; // Parameterized expression case Expression::PARAM: { // Get a typed pointer to the expression ParamExpr* pLeftExpr = (ParamExpr*)pExpr; // Get the symbols used by this expression Expression::SymbolSet exprSymbols = pLeftExpr->getSymbolUses(); // Add those symbols to the main set symbols.insert(exprSymbols.begin(), exprSymbols.end()); } break; // Cell indexing expression case Expression::CELL_INDEX: { // Get a typed pointer to the expression CellIndexExpr* pLeftExpr = (CellIndexExpr*)pExpr; // Get the symbols used by this expression Expression::SymbolSet exprSymbols = pLeftExpr->getSymbolUses(); // Add those symbols to the main set symbols.insert(exprSymbols.begin(), exprSymbols.end()); } break; // Invalid expression type default: assert (false); } } // Return the set of symbols return symbols; }
/*************************************************************** * Function: CodeParser::parseCellIndexExpr() * Purpose : Parse the XML code of a cell indexing expression * Initial : Maxime Chevalier-Boisvert on February 15, 2009 **************************************************************** Revisions and bug fixes: */ Expression* CodeParser::parseCellIndexExpr(const XML::Element* pElement) { // Parse the symbol expression Expression* pSymExpr = parseExpression(pElement->getChildElement(0)); // If the symbol expression is not a symbol, throw an exception if (pSymExpr->getExprType() != Expression::SYMBOL) throw XML::ParseError("Expected symbol expression", pElement->getTextPos()); // Compute the number of arguments size_t numArgs = pElement->getNumChildren() - 1; // Declare a vector for the arguments ParamExpr::ExprVector arguments; // For each child element for (size_t i = 0; i < numArgs; ++i) { // Parse this argument arguments.push_back(parseExpression(pElement->getChildElement(i + 1))); } // Create and return the new cell indexing expression return new CellIndexExpr( (SymbolExpr*)pSymExpr, arguments ); }
/*************************************************************** * Function: AssignStmt::getSymbolDefs() * Purpose : Get all symbols written/defined by this expression * Initial : Maxime Chevalier-Boisvert on March 17, 2009 **************************************************************** Revisions and bug fixes: */ Expression::SymbolSet AssignStmt::getSymbolDefs() const { // Create a set to store the defined symbols Expression::SymbolSet symbols; // For each left-expression for (ExprVector::const_iterator itr = m_leftExprs.begin(); itr != m_leftExprs.end(); ++itr) { // Get a pointer to this expression Expression* pExpr = *itr; // Switch on the expression type switch (pExpr->getExprType()) { // Symbol expression case Expression::SYMBOL: symbols.insert((SymbolExpr*)pExpr); break; // Parameterized expression case Expression::PARAM: symbols.insert(((ParamExpr*)pExpr)->getSymExpr()); break; // Cell indexing expression case Expression::CELL_INDEX: symbols.insert(((CellIndexExpr*)pExpr)->getSymExpr()); break; // Invalid expression type default: assert (false); } } // Return the set of symbols return symbols; }
/*************************************************************** * Function: splitExpression() * Purpose : Convert an expression to split form * Initial : Maxime Chevalier-Boisvert on January 7, 2009 **************************************************************** Revisions and bug fixes: */ bool splitExpression(Expression* pExpr, StmtSequence::StmtVector& stmtVector, Expression*& pTopExpr, ProgFunction* pFunction) { // // For each sub-expression: // - Split it recursively // - Assign top-level expression to a temp // - Replace sub-expr by temp var // // Take a temp from list to assign sub-expr to temp // Once temps are used, they become free again // // Make a copy of the expression to use as the top-level expression pTopExpr = pExpr->copy(); // If the expression is a lambda expression, perform no splitting and return early if (pTopExpr->getExprType() == Expression::ExprType::LAMBDA) return false; if (pTopExpr->getExprType() == Expression::ExprType::DOT) return false; // Get the list of sub-expressions for this expression Expression::ExprVector subExprs = pTopExpr->getSubExprs(); // If there are no sub-expressions, perform no splitting and return early if (subExprs.empty()) return false; // For each sub-expression for (size_t i = 0; i < subExprs.size(); ++i) { // Get a pointer to the sub-expression Expression* pSubExpr = subExprs[i]; // If the sub-expression is null, skip it if (pSubExpr == NULL) continue; // Declare a variable to store the top-level expression Expression* pTopSubExpr = NULL; // Split the sub-expression bool exprSplit = splitExpression(pSubExpr, stmtVector, pTopSubExpr, pFunction); // If the sub-expression was not split, skip it if (!exprSplit) continue; // If the current expression is a parameterized or // cell indexing expression and the sub-expression // is a range expressions if ((pTopExpr->getExprType() == Expression::ExprType::PARAM || pTopExpr->getExprType() == Expression::ExprType::CELL_INDEX) && pTopSubExpr->getExprType() == Expression::ExprType::RANGE) { // Replace the sub-expression by the top sub-expression directly pTopExpr->replaceSubExpr(i, pTopSubExpr); } // If the current expression is a parameterized // expression and the sub-expression is a cell-indexing // expression else if (pTopExpr->getExprType() == Expression::ExprType::PARAM && pTopSubExpr->getExprType() == Expression::ExprType::CELL_INDEX) { // Replace the sub-expression by the top sub-expression directly pTopExpr->replaceSubExpr(i, pTopSubExpr); } else { // Create a new symbol object for the temp variable SymbolExpr* pTempVar = pFunction->createTemp(); // Assign the sub-expression to the temp variable stmtVector.push_back(new AssignStmt(pTempVar, pTopSubExpr)); // Replace the sub-expression use by the temp variable pTopExpr->replaceSubExpr(i, pTempVar); } } // Expression split return true; }