12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904 |
- /*##############################################################################
- Copyright (C) 2011 HPCC Systems.
- All rights reserved. This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- ############################################################################## */
- #include "jliball.hpp"
- #include "hql.hpp"
- #include "platform.h"
- #include "jlib.hpp"
- #include "jexcept.hpp"
- #include "jmisc.hpp"
- #include "hqlexpr.hpp"
- #include "hqlstmt.hpp"
- #include "hqlstmt.ipp"
- #include "hqlfunc.hpp"
- #include "hqlcpp.ipp"
- #include "hqlcatom.hpp"
- #include "hqlcpputil.hpp"
- #include "hqlutil.hpp"
- #define CLEAR_COPY_THRESHOLD 100
- //---------------------------------------------------------------------------
- struct HQLCPP_API HqlBoundDefinedValue : public HqlDefinedValue
- {
- public:
- HqlBoundDefinedValue(IHqlExpression * _original, const CHqlBoundExpr & _bound) : HqlDefinedValue(_original)
- { bound.set(_bound); }
- virtual IHqlExpression * queryExpr() const { return bound.expr; }
- virtual void getBound(CHqlBoundExpr & result) { result.set(bound); }
- public:
- CHqlBoundExpr bound;
- };
- void HqlExprAssociation::getBound(CHqlBoundExpr & result)
- {
- result.expr.set(queryExpr());
- }
-
- //---------------------------------------------------------------------------
- BuildCtx::BuildCtx(HqlCppInstance & _state, _ATOM section) : state(_state)
- {
- init(state.ensureSection(section));
- }
- BuildCtx::BuildCtx(HqlCppInstance & _state) : state(_state)
- {
- init(NULL);
- }
- BuildCtx::BuildCtx(BuildCtx & _owner) : state(_owner.state)
- {
- init(_owner.curStmts);
- ignoreInput = _owner.ignoreInput;
- curPriority = _owner.curPriority;
- nextPriority = _owner.nextPriority;
- ignoreInput = false;
- }
- BuildCtx::BuildCtx(BuildCtx & _owner, HqlStmts * _root) : state(_owner.state)
- {
- init(_root);
- }
- BuildCtx::~BuildCtx()
- {
- }
- void BuildCtx::set(_ATOM section)
- {
- init(state.ensureSection(section));
- }
- void BuildCtx::set(BuildCtx & _owner)
- {
- init(_owner.curStmts);
- ignoreInput = _owner.ignoreInput;
- curPriority = _owner.curPriority;
- nextPriority = _owner.nextPriority;
- ignoreInput = false;
- }
- IHqlStmt * BuildCtx::addAlias(IHqlStmt * aliased)
- {
- if (ignoreInput)
- return NULL;
- HqlCompoundStmt * next = new HqlCompoundStmt(alias_stmt, curStmts);
- HqlStmt * cast = dynamic_cast<HqlStmt *>(aliased);
- assertex(cast);
- next->code.append(*LINK(cast));
- return appendSimple(next);
- }
- IHqlStmt * BuildCtx::addAssign(IHqlExpression * target, IHqlExpression * value)
- {
- if (ignoreInput)
- return NULL;
- HqlStmt * next = new HqlStmt(assign_stmt, curStmts);
- next->addExpr(LINK(target));
- next->addExpr(LINK(value));
- return appendSimple(next);
- }
- IHqlStmt * BuildCtx::addAssignIncrement(IHqlExpression * target, IHqlExpression * value)
- {
- if (ignoreInput)
- return NULL;
- if (value && !matchesConstantValue(value, 1))
- {
- HqlStmt * next = new HqlStmt(assigninc_stmt, curStmts);
- next->addExpr(LINK(target));
- next->addExpr(LINK(value));
- return appendSimple(next);
- }
- else
- {
- OwnedHqlExpr inc = createValue(no_postinc, target->getType(), LINK(target));
- return addExprOwn(inc.getClear());
- }
- }
- IHqlStmt * BuildCtx::addAssignDecrement(IHqlExpression * target, IHqlExpression * value)
- {
- if (ignoreInput)
- return NULL;
- if (value && !matchesConstantValue(value, 1))
- {
- HqlStmt * next = new HqlStmt(assigndec_stmt, curStmts);
- next->addExpr(LINK(target));
- next->addExpr(LINK(value));
- return appendSimple(next);
- }
- else
- {
- OwnedHqlExpr inc = createValue(no_postdec, LINK(target->getType()), LINK(target));
- return addExprOwn(inc.getClear());
- }
- }
- IHqlStmt * BuildCtx::addBlock()
- {
- if (ignoreInput)
- return NULL;
- HqlCompoundStmt * next = new HqlCompoundStmt(block_stmt, curStmts);
- return appendCompound(next);
- }
- IHqlStmt * BuildCtx::addBreak()
- {
- if (ignoreInput)
- return NULL;
- HqlStmt * next = new HqlStmt(break_stmt, curStmts);
- return appendSimple(next);
- }
- IHqlStmt * BuildCtx::addCase(IHqlStmt * _owner, IHqlExpression * source)
- {
- if (ignoreInput)
- return NULL;
- assertThrow(_owner->getStmt() == switch_stmt);
- HqlCompoundStmt & owner = (HqlCompoundStmt &)*_owner;
- curStmts = &owner.code;
- HqlCompoundStmt * next = new HqlCompoundStmt(case_stmt, curStmts);
- next->addExpr(LINK(source));
- return appendCompound(next);
- }
- IHqlStmt * BuildCtx::addContinue()
- {
- if (ignoreInput)
- return NULL;
- HqlStmt * next = new HqlStmt(continue_stmt, curStmts);
- return appendSimple(next);
- }
- IHqlStmt * BuildCtx::addDeclare(IHqlExpression * name, IHqlExpression * value)
- {
- assertex(name->getOperator() == no_variable);
- if (ignoreInput)
- return NULL;
- HqlStmt * next = new HqlStmt(declare_stmt, curStmts);
- next->addExpr(LINK(name));
- if (value)
- next->addExpr(LINK(value));
- appendSimple(next);
- return next;
- }
- IHqlStmt * BuildCtx::addDeclareExternal(IHqlExpression * name)
- {
- assertex(name->getOperator() == no_variable);
- if (ignoreInput)
- return NULL;
- HqlStmt * next = new HqlStmt(external_stmt, curStmts);
- next->addExpr(LINK(name));
- appendSimple(next);
- return next;
- }
- IHqlStmt * BuildCtx::addDeclareAssign(IHqlExpression * name, IHqlExpression * value)
- {
- addDeclare(name);
- return addAssign(name, value);
- }
- IHqlStmt * BuildCtx::addDefault(IHqlStmt * _owner)
- {
- if (ignoreInput)
- return NULL;
- assertThrow(_owner->getStmt() == switch_stmt);
- selectCompound(_owner);
- HqlCompoundStmt * next = new HqlCompoundStmt(default_stmt, curStmts);
- return appendCompound(next);
- }
- IHqlStmt * BuildCtx::addExpr(IHqlExpression * value)
- {
- if (ignoreInput)
- return NULL;
- HqlStmt * next = new HqlStmt(expr_stmt, curStmts);
- next->addExpr(LINK(value));
- return appendSimple(next);
- }
- IHqlStmt * BuildCtx::addExprOwn(IHqlExpression * value)
- {
- if (ignoreInput)
- {
- value->Release();
- return NULL;
- }
- HqlStmt * next = new HqlStmt(expr_stmt, curStmts);
- next->addExpr(value);
- return appendSimple(next);
- }
- IHqlStmt * BuildCtx::addReturn(IHqlExpression * value)
- {
- if (ignoreInput)
- return NULL;
- HqlStmt * next = new HqlStmt(return_stmt, curStmts);
- if (value)
- next->addExpr( LINK(value));
- return appendSimple(next);
- }
- IHqlStmt * BuildCtx::addFilter(IHqlExpression * condition)
- {
- HqlCompoundStmt * next = new HqlCompoundStmt(filter_stmt, curStmts);
- next->addExpr(LINK(condition));
- appendCompound(next);
- addBlock();
- return next;
- }
- IHqlStmt * BuildCtx::addFunction(IHqlExpression * funcdef)
- {
- if (ignoreInput)
- return NULL;
- HqlCompoundStmt * next = new HqlCompoundStmt(function_stmt, curStmts);
- next->addExpr(LINK(funcdef));
- return appendCompound(next);
- }
- IHqlStmt * BuildCtx::addIndirection(const BuildCtx & _parent)
- {
- HqlCompoundStmt * next = new HqlCompoundStmt(indirect_stmt, _parent.curStmts);
- return appendCompound(next);
- }
- IHqlStmt * BuildCtx::addLoop(IHqlExpression * cond, IHqlExpression * inc, bool atEnd)
- {
- if (ignoreInput)
- return NULL;
- HqlCompoundStmt * next = new HqlCompoundStmt(loop_stmt, curStmts);
- if (cond)
- next->addExpr(LINK(cond));
- if (inc)
- next->addExpr(LINK(inc));
- if (atEnd)
- next->addExpr(createAttribute(endAtom));
- return appendCompound(next);
- }
- IHqlStmt * BuildCtx::addGoto(const char * labelText)
- {
- if (ignoreInput)
- return NULL;
- HqlStmt * next = new HqlStmt(goto_stmt, curStmts);
- IHqlExpression * label= createVariable(labelText, makeBoolType());
- next->addExpr(label);
- return appendSimple(next);
- }
- IHqlStmt * BuildCtx::addGroup()
- {
- if (ignoreInput)
- return NULL;
- HqlCompoundStmt * next = new HqlCompoundStmt(group_stmt, curStmts);
- return appendCompound(next);
- }
- IHqlStmt * BuildCtx::addGroupPass(IHqlExpression * pass)
- {
- if (ignoreInput)
- return NULL;
- HqlCompoundStmt * next = new HqlCompoundStmt(pass_stmt, curStmts);
- next->addExpr(LINK(pass));
- return appendCompound(next);
- }
- IHqlStmt * BuildCtx::addLabel(const char * labelText)
- {
- if (ignoreInput)
- return NULL;
- HqlStmt * next = new HqlStmt(label_stmt, curStmts);
- IHqlExpression * label= createVariable(labelText, makeBoolType());
- next->addExpr(label);
- return appendSimple(next);
- }
- IHqlStmt * BuildCtx::addLine(const char * filename, unsigned lineNum)
- {
- if (ignoreInput)
- return NULL;
- HqlStmt * next = new HqlStmt(line_stmt, curStmts);
- if (filename)
- {
- next->addExpr(createConstant(filename));
- next->addExpr(createConstant(createIntValue(lineNum, sizeof(int), true)));
- }
- return appendSimple(next);
- }
- IHqlStmt * BuildCtx::addQuoted(const char * text)
- {
- if (ignoreInput)
- return NULL;
- HqlStmt * next = new HqlQuoteStmt(quote_stmt, curStmts, text);
- return appendSimple(next);
- }
- IHqlStmt * BuildCtx::addQuotedF(const char * format, ...)
- {
- if (ignoreInput)
- return NULL;
- StringBuffer text;
- va_list args;
- va_start(args, format);
- text.valist_appendf(format, args);
- va_end(args);
- HqlStmt * next = new HqlQuoteStmt(quote_stmt, curStmts, text.str());
- return appendSimple(next);
- }
- IHqlStmt * BuildCtx::addQuotedCompound(const char * text, const char * extra)
- {
- if (ignoreInput)
- return NULL;
- HqlCompoundStmt * next = new HqlQuoteStmt(quote_compound_stmt, curStmts, text);
- if (extra)
- next->addExpr(createQuoted(extra, makeVoidType()));
- return appendCompound(next);
- }
- IHqlStmt * BuildCtx::addQuotedCompoundOpt(const char * text, const char * extra)
- {
- if (ignoreInput)
- return NULL;
- HqlCompoundStmt * next = new HqlQuoteStmt(quote_compoundopt_stmt, curStmts, text);
- if (extra)
- next->addExpr(createQuoted(extra, makeVoidType()));
- return appendCompound(next);
- }
- IHqlStmt * BuildCtx::addSwitch(IHqlExpression * source)
- {
- if (ignoreInput)
- return NULL;
- HqlCompoundStmt * next = new HqlCompoundStmt(switch_stmt, curStmts);
- next->addExpr(LINK(source));
- return appendCompound(next);
- }
- HqlStmt * BuildCtx::appendCompound(HqlCompoundStmt * next)
- {
- assertThrow(!ignoreInput);
- appendSimple(next);
- curStmts = &next->code;
- return next;
- }
- HqlStmt * BuildCtx::appendSimple(HqlStmt * next)
- {
- assertThrow(!ignoreInput);
- if (nextPriority == OutermostScopePrio)
- {
- appendToOutermostScope(next);
- }
- else
- {
- next->setPriority(nextPriority);
- curStmts->appendStmt(*next);
- }
- nextPriority = curPriority;
- return next;
- }
- void BuildCtx::appendToOutermostScope(HqlStmt * next)
- {
- HqlStmts * searchStmts = curStmts;
- HqlStmts * insertStmts = NULL;
- HqlStmt * insertBefore = NULL;
- loop
- {
- HqlStmt * owner = searchStmts->owner;
- if (!owner)
- break;
- HqlStmts * ownerStmts = owner->queryContainer();
- switch (owner->getStmt())
- {
- case quote_compound_stmt:
- case quote_compoundopt_stmt:
- case indirect_stmt:
- goto found;
- case group_stmt:
- break;
- default:
- insertBefore = owner;
- insertStmts = ownerStmts;
- break;
- }
- searchStmts = ownerStmts;
- }
- found:
- if (insertBefore)
- {
- next->setPriority(insertBefore->queryPriority());
- insertStmts->add(*next, insertStmts->find(*insertBefore));
- }
- else
- {
- next->setPriority(curPriority);
- curStmts->appendStmt(*next);
- }
- }
- void BuildCtx::associate(HqlExprAssociation & next)
- {
- assertex(next.represents->queryBody() == next.represents);
- if (!ignoreInput)
- {
- curStmts->appendOwn(OLINK(next));
- }
- }
- void BuildCtx::associateOwn(HqlExprAssociation & next)
- {
- assertex(next.represents->queryBody() == next.represents);
- if (!ignoreInput)
- {
- curStmts->appendOwn(next);
- }
- else
- next.Release(); // can cause serious problems....
- }
- HqlExprAssociation *BuildCtx::associateExpr(IHqlExpression * represents, IHqlExpression * expr)
- {
- if (!ignoreInput)
- {
- HqlExprAssociation * assoc = new HqlSimpleDefinedValue(represents->queryBody(), expr);
- curStmts->appendOwn(*assoc);
- return assoc;
- }
- return NULL;
- }
- HqlExprAssociation * BuildCtx::associateExpr(IHqlExpression * represents, const CHqlBoundExpr & bound)
- {
- if (!ignoreInput)
- {
- HqlExprAssociation * assoc = new HqlBoundDefinedValue(represents->queryBody(), bound);
- curStmts->appendOwn(*assoc);
- return assoc;
- }
- return NULL;
- }
- IHqlExpression * BuildCtx::getTempDeclare(ITypeInfo * type, IHqlExpression * value)
- {
- IHqlExpression * temp = createVariable(LINK(type));
- addDeclare(temp, value);
- return temp;
- }
- bool BuildCtx::hasAssociation(HqlExprAssociation & search, bool unconditional)
- {
- HqlStmts * searchStmts = curStmts;
- loop
- {
- if (searchStmts->defs.contains(search))
- return true;
- HqlStmt * limitStmt = searchStmts->queryStmt();
- if (!limitStmt)
- return false;
- if (!unconditional)
- {
- switch (limitStmt->getStmt())
- {
- case filter_stmt:
- if (!matchesBoolean(limitStmt->queryExpr(0), true))
- return false;
- break;
- case quote_compound_stmt:
- case quote_compoundopt_stmt:
- case switch_stmt:
- case case_stmt:
- case default_stmt:
- case loop_stmt:
- return false;
- }
- }
- searchStmts = limitStmt->queryContainer();
- }
- }
- void BuildCtx::walkAssociations(IAssociationVisitor & visitor)
- {
- HqlStmts * searchStmts = curStmts;
- // walk all associations in the tree before this one
- loop
- {
- CIArrayOf<HqlExprAssociation> & defs = searchStmts->defs;
- ForEachItemInRev(idx, defs)
- {
- HqlExprAssociation & cur = defs.item(idx);
- if (visitor.visit(cur))
- return;
- }
- HqlStmt * limitStmt = searchStmts->queryStmt();
- if (!limitStmt)
- break;
- searchStmts = limitStmt->queryContainer();
- }
- }
- HqlExprAssociation * BuildCtx::queryAssociation(IHqlExpression * search, AssocKind kind, HqlExprCopyArray * selectors)
- {
- HqlStmts * searchStmts = curStmts;
- if (!search)
- return NULL;
- search = search->queryBody();
- // search all statements in the tree before this one, to see
- // if an expression already exists... If so return the target
- // of the assignment.
- loop
- {
- const CIArrayOf<HqlExprAssociation> & defs = searchStmts->defs;
- #ifdef CACHE_DEFINITION_HASHES
- if (!selectors)
- {
- unsigned searchHash = getSearchHash(search);
- const DefinitionHashArray & hashes = searchStmts->exprHashes;
- ForEachItemInRev(idx, hashes)
- {
- if (hashes.item(idx) == searchHash)
- {
- HqlExprAssociation & cur = defs.item(idx);
- if (cur.represents == search)
- {
- if ((kind == AssocAny) || (cur.getKind() == kind))
- return &cur;
- }
- }
- }
- }
- else
- #endif
- {
- ForEachItemInRev(idx, defs)
- {
- HqlExprAssociation & cur = defs.item(idx);
- IHqlExpression * represents = cur.represents.get();
- if (selectors && (cur.getKind() == AssocCursor))
- {
- if (selectors->contains(*represents))
- return NULL;
- }
- if (represents == search)
- if ((kind == AssocAny) || (cur.getKind() == kind))
- return &cur;
- }
- }
- HqlStmt * limitStmt = searchStmts->queryStmt();
- if (!limitStmt)
- break;
- searchStmts = limitStmt->queryContainer();
- }
- return NULL;
- }
- void BuildCtx::removeAssociation(HqlExprAssociation * search)
- {
- if (!search)
- return;
- HqlStmts * searchStmts = curStmts;
- loop
- {
- bool matched = searchStmts->zap(*search);
- if (matched)
- return;
- HqlStmt * limitStmt = searchStmts->queryStmt();
- if (!limitStmt)
- break;
- searchStmts = limitStmt->queryContainer();
- }
-
- assertex(!"Association not found");
- }
- HqlExprAssociation * BuildCtx::queryFirstAssociation(AssocKind searchKind)
- {
- HqlStmts * searchStmts = curStmts;
- // search all statements in the tree before this one, to see
- // if an expression already exists... If so return the target
- // of the assignment.
- loop
- {
- CIArray & defs = searchStmts->defs;
- ForEachItemInRev(idx, defs)
- {
- HqlExprAssociation & cur = (HqlExprAssociation &)defs.item(idx);
- if (cur.getKind() == searchKind)
- return &cur;
- }
- HqlStmt * limitStmt = searchStmts->queryStmt();
- if (!limitStmt)
- break;
- searchStmts = limitStmt->queryContainer();
- }
- return NULL;
- }
- //Search for an association, but don't allow it to be conditional, or be hidden by the definition of a cursor.
- HqlExprAssociation * BuildCtx::queryFirstCommonAssociation(AssocKind searchKind)
- {
- HqlStmts * searchStmts = curStmts;
- // search all statements in the tree before this one, to see
- // if an expression already exists... If so return the target
- // of the assignment.
- loop
- {
- CIArray & defs = searchStmts->defs;
- ForEachItemInRev(idx, defs)
- {
- HqlExprAssociation & cur = (HqlExprAssociation &)defs.item(idx);
- AssocKind kind = cur.getKind();
- if (kind == searchKind)
- return &cur;
- if (kind == AssocCursor)
- return NULL;
- }
- HqlStmt * limitStmt = searchStmts->queryStmt();
- if (!limitStmt)
- break;
- switch (limitStmt->getStmt())
- {
- //case quote_compound_stmt:
- //case quote_compoundopt_stmt,
- case filter_stmt:
- case label_stmt:
- case switch_stmt:
- case case_stmt:
- case default_stmt:
- case break_stmt:
- case continue_stmt:
- return NULL;
- }
- searchStmts = limitStmt->queryContainer();
- }
- return NULL;
- }
- HqlExprAssociation * BuildCtx::queryMatchExpr(IHqlExpression * search)
- {
- HqlExprCopyArray selectors;
- search->gatherTablesUsed(NULL, &selectors);
- return queryAssociation(search, AssocExpr, selectors.ordinality() ? &selectors : NULL);
- }
- bool BuildCtx::getMatchExpr(IHqlExpression * expr, CHqlBoundExpr & tgt)
- {
- HqlExprAssociation * match = queryMatchExpr(expr);
- if (match)
- {
- match->getBound(tgt);
- return true;
- }
- return false;
- }
-
- void BuildCtx::init(HqlStmts * _root)
- {
- root = _root;
- curStmts = root;
- curPriority = NormalPrio;
- nextPriority = curPriority;
- ignoreInput = false;
- }
- bool BuildCtx::isChildOf(HqlStmt * stmt, HqlStmts * stmts)
- {
- //MORE: Could improve by using depths
- do
- {
- if (stmts->find(*stmt) != NotFound)
- return true;
- stmt = stmt->queryContainer()->queryStmt();
- } while (stmt);
- return false;
- }
- void BuildCtx::selectCompound(IHqlStmt * stmt)
- {
- HqlCompoundStmt & owner = (HqlCompoundStmt &)*stmt;
- curStmts = &owner.code;
- }
- void BuildCtx::selectContainer()
- {
- HqlStmt * limitStmt = curStmts->queryStmt();
- assertex(limitStmt);
- curStmts = limitStmt->queryContainer();
- assertex(curStmts);
- }
- void BuildCtx::selectElse(IHqlStmt * stmt)
- {
- //Ignoring input does not work, because code expects associations to be kept
- assertex(stmt);
- switch (stmt->getStmt())
- {
- case filter_stmt:
- assertThrow(stmt->numChildren() == 1);
- selectCompound(stmt);
- addBlock();
- break;
- default:
- throwUnexpected();
- break;
- }
- }
- unsigned BuildCtx::setPriority(unsigned newPrio)
- {
- unsigned oldPriority = curPriority;
- if (!ignoreInput)
- {
- curPriority = newPrio;
- nextPriority = curPriority;
- }
- return oldPriority;
- }
- void BuildCtx::setNextPriority(unsigned newPrio)
- {
- if (!ignoreInput)
- nextPriority = newPrio;
- }
- IHqlStmt * BuildCtx::recursiveGetBestContext(HqlStmts * searchStmts, HqlExprCopyArray & required)
- {
- //Is it ok to move the expression before the owner statement?
- //First fail if any of the datasets that this expression is dependent on are defined in this scope.
- if (required.ordinality())
- {
- ForEachItemIn(i, searchStmts->defs)
- {
- HqlExprAssociation & cur = (HqlExprAssociation &)searchStmts->defs.item(i);
- if (required.contains(*cur.represents.get()))
- return NULL;
- }
- }
- HqlStmt * owner = searchStmts->owner;
- //Now check for poor places to hoist
- bool worthHoistingHere = false;
- switch (owner->getStmt())
- {
- case block_stmt:
- case group_stmt:
- break;
- case quote_compound_stmt:
- case quote_compoundopt_stmt:
- case indirect_stmt:
- return NULL;
- case filter_stmt:
- case switch_stmt:
- //MORE: Should make whether something ishoisted dependent on efficiency of the filter condition
- break;
- case loop_stmt:
- //MORE: Should make it dependent on the ordinality of the loop condition.
- worthHoistingHere = true;
- break;
- case case_stmt:
- case default_stmt:
- //Can't hoist here
- break;
- default:
- throwUnexpected();
- }
- HqlStmts * container = owner->queryContainer();
- if (container->owner)// && canHoistInParent)
- {
- IHqlStmt * match = recursiveGetBestContext(container, required);
- if (match)
- return match;
- }
- if (!worthHoistingHere)
- return NULL;
- //We've found somewhere we can insert the expression....
- unsigned existingPos = container->find(*owner);
- assertex(existingPos != NotFound);
- //insert a group just before the current statement
- HqlCompoundStmt * next = new HqlCompoundStmt(group_stmt, container);
- next->setPriority(owner->queryPriority());
- container->add(*next, existingPos);
- curStmts = &next->code;
- return next;
- }
- IHqlStmt * BuildCtx::replaceExpr(IHqlStmt * stmt, IHqlExpression * expr)
- {
- //Highly dangerous - use with utmost care!
- assertex(stmt->getStmt() == expr_stmt);
- HqlStmt * castStmt = static_cast<HqlStmt *>(stmt);
- castStmt->killExprs();
- castStmt->addExpr(LINK(expr));
- return castStmt;
- }
- IHqlStmt * BuildCtx::selectBestContext(IHqlExpression * expr)
- {
- if (containsTranslated(expr) || !curStmts->owner)
- return NULL;
- //MORE: Access to global context, context and other things...
- HqlExprCopyArray inScope;
- expr->gatherTablesUsed(NULL, &inScope);
- return recursiveGetBestContext(curStmts, inScope);
- }
- //---------------------------------------------------------------------------
- HqlStmts::HqlStmts(HqlStmt * _owner) : owner(_owner)
- {
- }
- void HqlStmts::appendStmt(HqlStmt & stmt)
- {
- unsigned newPrio = stmt.queryPriority();
- unsigned right = ordinality();
- if (right == 0)
- {
- append(stmt);
- }
- else if (newPrio >= item(right-1).queryPriority())
- {
- while (item(right-1).isIncomplete())
- {
- if (newPrio > item(right-1).queryPriority())
- break;
- right--;
- if (right == 0)
- break;
- }
- add(stmt, right);
- }
- else
- {
- unsigned left = 0;
- while (right - left >= 2)
- {
- unsigned mid = (left + right - 1) / 2;
- HqlStmt & cur = item(mid);
- if (newPrio >= cur.queryPriority())
- left = mid+1;
- else
- right = mid+1;
- }
- if (newPrio >= item(left).queryPriority())
- ++left;
- while (left && item(left-1).isIncomplete() && (newPrio == item(left-1).queryPriority()))
- --left;
- add(stmt, left);
- }
- }
- //---------------------------------------------------------------------------
- HqlStmt::HqlStmt(StmtKind _kind, HqlStmts * _container)
- {
- kind = _kind;
- container = _container;
- incomplete = false;
- included = true;
- }
- void HqlStmt::addExpr(IHqlExpression * expr)
- {
- exprs.append(*expr);
- }
- StmtKind HqlStmt::getStmt()
- {
- return (StmtKind)kind;
- }
- StringBuffer & HqlStmt::getTextExtra(StringBuffer & out)
- {
- return out;
- }
- static bool isEmptyGroup(IHqlStmt * stmt)
- {
- if (!stmt)
- return true;
- switch (stmt->getStmt())
- {
- case group_stmt:
- case block_stmt:
- return stmt->numChildren() == 0;
- }
- return false;
- }
- bool HqlStmt::hasChildren() const
- {
- if (numChildren() == 0)
- return false;
- unsigned count = numChildren();
- for (unsigned index = 0; index < count; index++)
- {
- IHqlStmt * cur = queryChild(index);
- if (cur->isIncluded())
- return true;
- }
- return false;
- }
-
- bool HqlStmt::isIncluded() const
- {
- if (!included)
- return false;
- switch (kind)
- {
- case quote_compoundopt_stmt:
- case group_stmt:
- return hasChildren();
- case filter_stmt:
- if (isEmptyGroup(queryChild(0)) && isEmptyGroup(queryChild(1)))
- return false;
- break;
- }
- return true;
- }
- unsigned HqlStmt::numChildren() const
- {
- return 0;
- }
- IHqlStmt * HqlStmt::queryChild(unsigned index) const
- {
- return NULL;
- }
- HqlStmts * HqlStmt::queryContainer()
- {
- return container;
- }
- IHqlExpression * HqlStmt::queryExpr(unsigned index)
- {
- if (exprs.isItem(index))
- return &exprs.item(index);
- return NULL;
- }
- void HqlStmts::inheritDefinitions(HqlStmts & other)
- {
- ForEachItemIn(i, other.defs)
- {
- defs.append(OLINK(other.defs.item(i)));
- #ifdef CACHE_DEFINITION_HASHES
- exprHashes.append(other.exprHashes.item(i));
- #endif
- }
- }
- //---------------------------------------------------------------------------
- #ifdef _MSC_VER
- #pragma warning(push)
- #pragma warning( disable : 4355 ) // 'this' : used in base member initializer list
- #endif
- HqlCompoundStmt::HqlCompoundStmt(StmtKind _kind, HqlStmts * _container) : HqlStmt(_kind, _container), code(this)
- {
- }
- #ifdef _MSC_VER
- #pragma warning(pop)
- #endif
- void HqlCompoundStmt::mergeScopeWithContainer()
- {
- container->inheritDefinitions(code);
- }
- unsigned HqlCompoundStmt::numChildren() const
- {
- return code.ordinality();
- }
- IHqlStmt * HqlCompoundStmt::queryChild(unsigned index) const
- {
- if (code.isItem(index))
- return &code.item(index);
- return NULL;
- }
- //---------------------------------------------------------------------------
- StringBuffer & HqlQuoteStmt::getTextExtra(StringBuffer & out)
- {
- return out.append(text);
- }
- //---------------------------------------------------------------------------
- int queryMemsetChar(IHqlExpression * expr)
- {
- if (expr->getOperator() != no_constant)
- return -1;
- unsigned size = expr->queryType()->getSize();
- if (size == 0)
- return -1;
- const byte * data = (const byte *)expr->queryValue()->queryValue();
- byte match = data[0];
- while (--size)
- if (*++data != match)
- return -1;
- return match;
- }
- static IHqlExpression * extractNonConstant(IHqlExpression * expr, unsigned & delta)
- {
- switch (expr->getOperator())
- {
- case no_constant:
- delta += (unsigned)getIntValue(expr);
- return NULL;
- case no_add:
- {
- IHqlExpression * left = expr->queryChild(0);
- OwnedHqlExpr newLeft = extractNonConstant(left, delta);
- IHqlExpression * right = expr->queryChild(1);
- OwnedHqlExpr newRight = extractNonConstant(right, delta);
- if (!newLeft) return newRight.getClear();
- if (!newRight) return newLeft.getClear();
- if ((left == newLeft) && (right == newRight))
- return LINK(expr);
- return createValue(no_add, expr->getType(), newLeft.getClear(), newRight.getClear());
- }
- }
- return LINK(expr);
- }
- IHqlExpression * peepholeAddExpr(IHqlExpression * left, IHqlExpression * right)
- {
- unsigned delta = 0;
- IHqlExpression * simpleLeft = extractNonConstant(left, delta);
- IHqlExpression * simpleRight = extractNonConstant(right, delta);
- IHqlExpression * ret;
- if (simpleLeft)
- {
- if (simpleRight)
- ret = createValue(no_add, left->getType(), simpleLeft, simpleRight);
- else
- ret = simpleLeft;
- }
- else
- ret = simpleRight;
- if (!delta && ret)
- return ret;
- IHqlExpression * value = getSizetConstant(delta);
- if (!ret)
- return value;
- return createValue(no_add, left->getType(), ret, value);
- if ((left->getOperator() == no_constant) && (right->getOperator() == no_constant))
- return getSizetConstant((size32_t)left->queryValue()->getIntValue() + (size32_t)right->queryValue()->getIntValue());
- return createValue(no_add, left->getType(), LINK(left), LINK(right));
- }
- bool rightFollowsLeft(IHqlExpression * left, IHqlExpression * leftLen, IHqlExpression * right)
- {
- OwnedHqlExpr sum = peepholeAddExpr(left, leftLen);
- if (sum == right)
- return true;
- if (left->getOperator() != right->getOperator())
- {
- if (right->getOperator() == no_add)
- {
- if ((left == right->queryChild(0)) && (leftLen == right->queryChild(1)))
- return true;
- if ((left == right->queryChild(1)) && (leftLen == right->queryChild(0)))
- return true;
- }
- return false;
- }
- switch (left->getOperator())
- {
- case no_constant:
- {
- ITypeInfo * leftType = left->queryType();
- ITypeInfo * rightType = right->queryType();
- if (leftType->getTypeCode() != rightType->getTypeCode())
- return false;
- switch (leftType->getTypeCode())
- {
- case type_int:
- if (leftLen->getOperator() != no_constant)
- return false;
- if (left->queryValue()->getIntValue() + leftLen->queryValue()->getIntValue() == right->queryValue()->getIntValue())
- return true;
- return false;
- }
- return false;
- }
- case no_add:
- if ((left == right->queryChild(0)) && (leftLen == right->queryChild(1)))
- return true;
- if (left->queryChild(1) == right->queryChild(1))
- {
- if (rightFollowsLeft(left->queryChild(0), leftLen, right->queryChild(0)))
- return true;
- }
- //fall through
- case no_index:
- if (left->queryChild(0) != right->queryChild(0))
- return false;
- return rightFollowsLeft(left->queryChild(1), leftLen, right->queryChild(1));
- }
- unsigned numLeft = left->numChildren();
- if (numLeft == 0 || (numLeft != right->numChildren()))
- return false;
- ForEachChild(idx, left)
- {
- if (!rightFollowsLeft(left->queryChild(idx), leftLen, right->queryChild(idx)))
- return false;
- }
- return true;
- }
- static IHqlExpression * createDataForMemset(unsigned size, byte value)
- {
- if (size < 100)
- {
- void * temp = alloca(size);
- memset(temp, value, size);
- return createConstant(createDataValue((char *)temp, size));
- }
- void * temp = malloc(size);
- memset(temp, value, size);
- IHqlExpression * ret = createConstant(createDataValue((char *)temp, size));
- free(temp);
- return ret;
- }
- static IHqlExpression * createDataForIntegerZero(unsigned size)
- {
- return createDataForMemset(size, 0);
- }
- class SpecialFunction
- {
- public:
- SpecialFunction() { wasAssign = false; name = NULL; }
- HqlStmt * createStmt(HqlStmts & curStmts, HqlCppTranslator & translator);
- bool canOptimize() const;
- void expandValue(void * target) const;
- bool extractIsSpecial(IHqlStmt & stmt, bool memsetOnly, unsigned peepholeOptions);
- bool isBigClear() const;
- int queryClearValue() const;
- bool queryCombine(const SpecialFunction & next, bool memsetOnly, size32_t combineStringLimit);
- private:
- _ATOM name;
- HqlExprAttr src;
- HqlExprAttr tgt;
- HqlExprAttr srcLen;
- HqlExprAttr tgtLen;
- bool wasAssign;
- };
- //Always convert rtlWriteInt(rtlReadInt()) the inline memcpy is going to be much better.
- //It should probably be converted earlier....
- bool isAwkwardIntSize(IHqlExpression * size)
- {
- IValue * value = size->queryValue();
- if (value)
- {
- switch (value->getIntValue())
- {
- case 3:
- case 5:
- case 6:
- case 7:
- return true;
- }
- }
- return false;
- }
- bool SpecialFunction::canOptimize() const
- {
- if ((name == memcpyAtom) && (queryMemsetChar(src) >= 0))
- {
- if ((getIntValue(srcLen, 0) > 1) || !wasAssign)
- return true;
- }
- if ((name == memcpyAtom) && isAwkwardIntSize(srcLen))
- return true;
- return false;
- }
- HqlStmt * SpecialFunction::createStmt(HqlStmts & curStmts, HqlCppTranslator & translator)
- {
- HqlExprArray args;
- _ATOM func = name;
- if (name == memsetAtom)
- {
- func = memsetAtom;
- args.append(*LINK(tgt));
- args.append(*LINK(src));
- args.append(*LINK(srcLen));
- }
- else if (name == memcpyAtom)
- {
- int clearByte = queryMemsetChar(src);
- size32_t size = (size32_t)getIntValue(srcLen, 0);
- if (clearByte == 0)
- {
- //if length is 1,2,4 then use an assignment instead.
- switch (size)
- {
- case 1:
- case 2:
- case 4:
- case 8:
- {
- OwnedITypeInfo type = makeIntType(size, false);
- OwnedHqlExpr castTgt = createValue(no_cast, makePointerType(LINK(type)), LINK(tgt));
- OwnedHqlExpr deref = createValue(no_deref, makeReferenceModifier(LINK(type)), LINK(castTgt));
- OwnedHqlExpr src = createConstant(type->castFrom(true, (__int64)0));
- HqlStmt * next = new HqlStmt(assign_stmt, &curStmts);
- next->addExpr(LINK(deref));
- next->addExpr(LINK(src));
- return next;
- }
- }
- }
- //MORE: assignment of 1,2,4 bytes possibly better as an assign?
- if (clearByte >= 0)
- {
- func = memsetAtom;
- args.append(*LINK(tgt));
- args.append(*createConstant(createIntValue(clearByte, sizeof(int), true)));
- args.append(*LINK(srcLen));
- }
- else
- {
- args.append(*LINK(tgt));
- args.append(*LINK(src));
- args.append(*LINK(srcLen));
- }
- }
- else if (name == deserializerReadNAtom || name == serializerPutAtom)
- {
- args.append(*LINK(src));
- args.append(*LINK(tgtLen));
- args.append(*LINK(tgt));
- }
- else if ((name == ebcdic2asciiAtom) || (name == ascii2ebcdicAtom))
- {
- args.append(*LINK(tgtLen));
- args.append(*LINK(tgt));
- args.append(*LINK(srcLen));
- args.append(*LINK(src));
- }
- else if (name == deserializerSkipNAtom)
- {
- args.append(*LINK(src));
- args.append(*LINK(srcLen));
- }
- else
- UNIMPLEMENTED;
- HqlStmt * next = new HqlStmt(expr_stmt, &curStmts);
- next->addExpr(translator.bindTranslatedFunctionCall(func, args));
- return next;
- }
- IHqlExpression * stripTranslatedCasts(IHqlExpression * e)
- {
- loop
- {
- switch (e->getOperator())
- {
- case no_cast:
- case no_implicitcast:
- case no_typetransfer:
- {
- IHqlExpression * child = e->queryChild(0);
- if (hasWrapperModifier(child->queryType()))
- return e;
- e = child;
- break;
- }
- default:
- return e;
- }
- }
- }
- void SpecialFunction::expandValue(void * target) const
- {
- size32_t size = (size32_t)getIntValue(srcLen);
- if (name == memsetAtom)
- memset(target, (int)getIntValue(src), size);
- else
- memcpy(target, src->queryValue()->queryValue(), size);
- }
- bool SpecialFunction::extractIsSpecial(IHqlStmt & stmt, bool memsetOnly, unsigned peepholeOptions)
- {
- if (stmt.getStmt() == expr_stmt)
- {
- IHqlExpression * expr = stmt.queryExpr(0);
- if (expr->getOperator() != no_externalcall)
- return false;
- name = expr->queryName();
- if (name == memcpyAtom)
- {
- src.set(stripTranslatedCasts(expr->queryChild(1)));
- if (memsetOnly && (queryMemsetChar(src) == -1))
- return false;
- tgt.set(stripTranslatedCasts(expr->queryChild(0)));
- srcLen.set(expr->queryChild(2));
- tgtLen.set(srcLen);
- return true;
- }
- if (name == deserializerReadNAtom || name == serializerPutAtom)
- {
- if (memsetOnly)
- return false;
- src.set(expr->queryChild(0));
- tgt.set(stripTranslatedCasts(expr->queryChild(2)));
- tgtLen.set(expr->queryChild(1));
- return true;
- }
- if ((name == ebcdic2asciiAtom) || (name == ascii2ebcdicAtom))
- {
- if (memsetOnly)
- return false;
- src.set(expr->queryChild(3));
- tgt.set(expr->queryChild(1));
- srcLen.set(expr->queryChild(2));
- tgtLen.set(expr->queryChild(0));
- return srcLen == tgtLen;
- }
- if (name == memsetAtom)
- {
- IHqlExpression * value = expr->queryChild(1);
- IHqlExpression * len = expr->queryChild(2);
- if (len->queryValue() && value->queryValue())
- {
- tgt.set(stripTranslatedCasts(expr->queryChild(0)));
- src.set(value);
- srcLen.set(len);
- tgtLen.set(srcLen);
- }
- return true;
- }
- if (name == deserializerSkipNAtom)
- {
- src.set(expr->queryChild(0));
- srcLen.set(expr->queryChild(1));
- return true;
- }
- unsigned size = 0;
- if (name == writeIntAtom[3])
- size = 3;
- else if (name == writeIntAtom[5])
- size = 5;
- else if (name == writeIntAtom[6])
- size = 6;
- else if (name == writeIntAtom[7])
- size = 7;
- if (size)
- {
- IHqlExpression * value = expr->queryChild(1);
- if (isZero(value))
- {
- name = memcpyAtom;
- src.setown(createDataForIntegerZero(size));
- tgt.set(stripTranslatedCasts(expr->queryChild(0)));
- srcLen.setown(getSizetConstant(size));
- tgtLen.set(srcLen);
- return true;
- }
- if (memsetOnly)
- return false;
- while (value->getOperator() == no_typetransfer)
- value = value->queryChild(0);
- if (value->getOperator() == no_externalcall)
- {
- if ((value->queryName() == readIntAtom[size][true]) ||
- (value->queryName() == readIntAtom[size][false]))
- {
- name = memcpyAtom;
- src.set(stripTranslatedCasts(value->queryChild(0)));
- tgt.set(stripTranslatedCasts(expr->queryChild(0)));
- srcLen.setown(getSizetConstant(size));
- tgtLen.set(srcLen);
- return true;
- }
- }
- }
- return false;
- }
- else if (stmt.getStmt() == assign_stmt)
- {
- wasAssign = true;
- tgt.set(stmt.queryExpr(0));
- src.set(stmt.queryExpr(1));
- ITypeInfo * tgtType = tgt->queryType();
- ITypeInfo * srcType = src->queryType();
- if (!isSameBasicType(srcType, tgtType))
- {
- if (!isSameFullyUnqualifiedType(srcType, tgtType))
- return false;
- }
- while (tgt->getOperator() == no_typetransfer)
- tgt.set(tgt->queryChild(0));
- if (tgt->getOperator() != no_deref)
- return false;
- while (src->getOperator() == no_typetransfer)
- src.set(src->queryChild(0));
- size32_t targetSize = tgtType->getSize();
- if (src->getOperator() != no_deref)
- {
- type_t tc = tgtType->getTypeCode();
- OwnedHqlExpr newSrcExpr;
- switch (tc)
- {
- case type_pointer:
- case type_table:
- //case type_row:
- if (src->getOperator() == no_nullptr)
- {
- void * ptr = NULL;
- targetSize = sizeof(void *);
- newSrcExpr.setown(createConstant(createDataValue((const char *)&ptr, targetSize)));
- }
- break;
- }
-
- if (!memsetOnly)
- {
- switch (tc)
- {
- case type_int:
- case type_swapint:
- case type_boolean:
- if (src->getOperator() == no_constant)
- {
- OwnedHqlExpr cast = ensureExprType(src, tgtType);
- IValue * castValue = cast->queryValue();
- if (castValue)
- {
- void * temp = alloca(targetSize);
- castValue->toMem(temp);
- newSrcExpr.setown(createConstant(createDataValue((const char *)temp, targetSize)));
- }
- }
- break;
- case type_real:
- if ((peepholeOptions & PHOconvertReal) && (src->getOperator() == no_constant))
- {
- OwnedHqlExpr cast = ensureExprType(src, tgtType);
- IValue * castValue = cast->queryValue();
- if (castValue)
- {
- void * temp = alloca(targetSize);
- castValue->toMem(temp);
- newSrcExpr.setown(createConstant(createDataValue((const char *)temp, targetSize)));
- }
- }
- break;
- }
- }
- if (!newSrcExpr && isZero(src))
- newSrcExpr.setown(createDataForIntegerZero(targetSize));
- if (!newSrcExpr)
- return false;
- src.set(newSrcExpr);
- }
- else
- {
- if (memsetOnly)
- return false;
- src.set(src->queryChild(0));
- }
- srcLen.setown(getSizetConstant(targetSize));
- tgtLen.set(srcLen);
- tgt.set(stripTranslatedCasts(tgt->queryChild(0)));
- src.set(stripTranslatedCasts(src));
- name = memcpyAtom;
- return true;
- }
- return false;
- }
- bool SpecialFunction::isBigClear() const
- {
- if ((name == memsetAtom) || (name == memcpyAtom))
- return getIntValue(srcLen, 0) > CLEAR_COPY_THRESHOLD;
- return false;
- }
- int SpecialFunction::queryClearValue() const
- {
- if (name == memcpyAtom)
- return queryMemsetChar(src);
- if (name == memsetAtom)
- return (int)getIntValue(src, -1);
- return -1;
- }
- bool SpecialFunction::queryCombine(const SpecialFunction & next, bool memsetOnly, size32_t combineStringLimit)
- {
- if (name != next.name)
- {
- if (!((name == memsetAtom) && (next.name == memcpyAtom)) &&
- !((name == memcpyAtom) && (next.name == memsetAtom)))
- return false;
- }
- if (name == deserializerSkipNAtom)
- {
- if (src != next.src)
- return false;
- srcLen.setown(peepholeAddExpr(srcLen, next.srcLen));
- return true;
- }
- if (rightFollowsLeft(tgt, tgtLen, next.tgt))
- {
- if ((name != memsetAtom) && (next.name != memsetAtom))
- {
- if (name == deserializerReadNAtom || name == serializerPutAtom)
- {
- tgtLen.setown(peepholeAddExpr(tgtLen, next.tgtLen));
- return true;
- }
- if (rightFollowsLeft(src, srcLen, next.src))
- {
- srcLen.setown(peepholeAddExpr(srcLen, next.srcLen));
- tgtLen.setown(peepholeAddExpr(tgtLen, next.tgtLen));
- return true;
- }
- }
- IValue * srcValue = src->queryValue();
- IValue * nextValue = next.src->queryValue();
- if (srcValue && nextValue)
- {
- //Don't combine things that can be converted to different memsets.
- //This would work better if we first combined items that could be done as memsets and then combined strings
- //processing in order we hit them doesn't work as well.
- int clearValue = queryClearValue();
- int nextClearValue = next.queryClearValue();
- if (memsetOnly)
- {
- assertex((clearValue != -1) && (nextClearValue != -1));
- if (clearValue != nextClearValue)
- return false;
- }
- if ((isBigClear() && (clearValue != -1)) || (next.isBigClear() && (nextClearValue != -1)))
- {
- if (clearValue != nextClearValue)
- return false;
- }
- size32_t curSize = (size32_t)getIntValue(srcLen);
- size32_t nextSize = (size32_t)getIntValue(next.srcLen);
- if (curSize + nextSize > combineStringLimit)
- return false;
- byte * temp = (byte *)malloc(curSize + nextSize);
- expandValue(temp);
- next.expandValue(temp+curSize);
- src.setown(createConstant(createDataValue((const char *)temp, curSize + nextSize)));
- free(temp);
- srcLen.setown(getSizetConstant(curSize + nextSize));
- tgtLen.set(srcLen);
- if (name == memsetAtom)
- name = memcpyAtom;
- return true;
- }
- }
- return false;
- }
- PeepHoleOptimizer::PeepHoleOptimizer(HqlCppTranslator & _translator) : translator(_translator)
- {
- combineStringLimit = -1;
- peepholeOptions = 0;
- if (translator.queryOptions().convertRealAssignToMemcpy)
- peepholeOptions |= PHOconvertReal;
- switch (translator.queryOptions().targetCompiler)
- {
- case Vs6CppCompiler:
- combineStringLimit = 32000;
- break;
- }
- }
- void PeepHoleOptimizer::optimize(HqlStmts & stmts)
- {
- unsigned max = stmts.ordinality(); // can change as processing proceeds.
- SpecialFunction prevMatch;
- for (unsigned pass=0; pass < 2; pass++)
- {
- bool memsetOnly = (pass == 0);
- for (unsigned i =0; i < max; i++)
- {
- IHqlStmt & cur = stmts.item(i);
- if (prevMatch.extractIsSpecial(cur, memsetOnly, peepholeOptions))
- {
- unsigned j = i+1;
- unsigned prevMax = max;
- SpecialFunction nextMatch;
- while (j < max)
- {
- IHqlStmt & next = stmts.item(j);
- if (!nextMatch.extractIsSpecial(next, memsetOnly, peepholeOptions))
- break;
- if (!prevMatch.queryCombine(nextMatch, memsetOnly, combineStringLimit))
- break;
- stmts.remove(i);
- max--;
- }
- if (max != prevMax || prevMatch.canOptimize())
- {
- stmts.remove(i);
- stmts.add(*prevMatch.createStmt(stmts, translator), i);
- }
- }
- }
- }
- for (unsigned i2=0; i2 < max; i2++)
- {
- IHqlStmt & cur = stmts.item(i2);
- if (cur.numChildren() != 0)
- optimize(((HqlCompoundStmt&)cur).code);
- }
- }
- void peepholeOptimize(HqlCppInstance & instance, HqlCppTranslator & translator)
- {
- PeepHoleOptimizer optimizer(translator);
- ForEachItemIn(idx, instance.sections)
- optimizer.optimize(((HqlCppSection&)instance.sections.item(idx)).stmts);
- }
- //---------------------------------------------------------------------------
- AssociationIterator::AssociationIterator(BuildCtx & ctx)
- {
- rootStmts = ctx.curStmts;
- curStmts = NULL;
- curIdx = 0;
- }
- bool AssociationIterator::first()
- {
- curStmts = rootStmts;
- curIdx = curStmts->defs.ordinality();
- return doNext();
- }
- bool AssociationIterator::doNext()
- {
- loop
- {
- if (curIdx-- != 0)
- return true;
- HqlStmt * limitStmt = curStmts->queryStmt();
- if (!limitStmt)
- {
- curStmts = NULL;
- return false;
- }
- curStmts = limitStmt->queryContainer();
- curIdx = curStmts->defs.ordinality();
- }
- }
- HqlExprAssociation & AssociationIterator::get()
- {
- CIArray & defs = curStmts->defs;
- return (HqlExprAssociation &)defs.item(curIdx);
- }
- //---------------------------------------------------------------------------
- bool FilteredAssociationIterator::doNext()
- {
- while (AssociationIterator::doNext())
- {
- HqlExprAssociation & cur = AssociationIterator::get();
- if (cur.getKind() == searchKind)
- return true;
- }
- return false;
- }
- bool RowAssociationIterator::doNext()
- {
- while (AssociationIterator::doNext())
- {
- HqlExprAssociation & cur = AssociationIterator::get();
- if (cur.isRowAssociation())
- return true;
- }
- return false;
- };
- unsigned calcTotalChildren(IHqlStmt * stmt)
- {
- if (!stmt->isIncluded())
- return 0;
- unsigned num = stmt->numChildren();
- unsigned total = 1;
- switch (stmt->getStmt())
- {
- case alias_stmt:
- case group_stmt:
- case pass_stmt:
- case indirect_stmt:
- total = 0;
- break;
- }
- for (unsigned i=0; i < num; i++)
- total += calcTotalChildren(stmt->queryChild(i));
- return total;
- }
|