nadd.pas 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Type checking and simplification for add nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nadd;
  18. {$i fpcdefs.inc}
  19. {$modeswitch nestedprocvars}
  20. { define addstringopt}
  21. interface
  22. uses
  23. node,symtype;
  24. type
  25. taddnode = class(tbinopnode)
  26. private
  27. resultrealdefderef: tderef;
  28. function pass_typecheck_internal:tnode;
  29. public
  30. resultrealdef : tdef;
  31. constructor create(tt : tnodetype;l,r : tnode);override;
  32. constructor create_internal(tt:tnodetype;l,r:tnode);
  33. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  34. procedure ppuwrite(ppufile:tcompilerppufile);override;
  35. procedure buildderefimpl;override;
  36. procedure derefimpl;override;
  37. function pass_1 : tnode;override;
  38. function pass_typecheck:tnode;override;
  39. function simplify(forinline: boolean) : tnode;override;
  40. function dogetcopy : tnode;override;
  41. function docompare(p: tnode): boolean; override;
  42. {$ifdef state_tracking}
  43. function track_state_pass(exec_known:boolean):boolean;override;
  44. {$endif}
  45. protected
  46. { override the following if you want to implement }
  47. { parts explicitely in the code generator (JM) }
  48. function first_addstring: tnode; virtual;
  49. function first_addset: tnode; virtual;
  50. function first_adddynarray : tnode; virtual;
  51. { only implements "muln" nodes, the rest always has to be done in }
  52. { the code generator for performance reasons (JM) }
  53. function first_add64bitint: tnode; virtual;
  54. function first_addpointer: tnode; virtual;
  55. function first_cmppointer: tnode; virtual;
  56. { override and return false if you can handle 32x32->64 }
  57. { bit multiplies directly in your code generator. If }
  58. { this function is overridden to return false, you can }
  59. { get multiplies with left/right both s32bit or u32bit, }
  60. { and resultdef of the muln s64bit or u64bit }
  61. function use_generic_mul32to64: boolean; virtual;
  62. { override and return false if code generator can handle }
  63. { full 64 bit multiplies. }
  64. function use_generic_mul64bit: boolean; virtual;
  65. {$ifdef cpuneedsmulhelper}
  66. { override to customize to decide if the code generator }
  67. { can handle a given multiply node directly, or it needs helpers }
  68. function use_mul_helper: boolean; virtual;
  69. {$endif cpuneedsmulhelper}
  70. { shall be overriden if the target cpu supports
  71. an fma instruction
  72. }
  73. function use_fma : boolean; virtual;
  74. { This routine calls internal runtime library helpers
  75. for all floating point arithmetic in the case
  76. where the emulation switches is on. Otherwise
  77. returns nil, and everything must be done in
  78. the code generation phase.
  79. }
  80. function first_addfloat : tnode; virtual;
  81. private
  82. { checks whether a muln can be calculated as a 32bit }
  83. { * 32bit -> 64 bit }
  84. function try_make_mul32to64: boolean;
  85. { Match against the ranges, i.e.:
  86. var a:1..10;
  87. begin
  88. if a>0 then
  89. ...
  90. always evaluates to true. (DM)
  91. }
  92. function cmp_of_disjunct_ranges(var res : boolean) : boolean;
  93. { tries to replace the current node by a fma node }
  94. function try_fma(ld,rd : tdef) : tnode;
  95. end;
  96. taddnodeclass = class of taddnode;
  97. var
  98. { caddnode is used to create nodes of the add type }
  99. { the virtual constructor allows to assign }
  100. { another class type to caddnode => processor }
  101. { specific node types can be created }
  102. caddnode : taddnodeclass = taddnode;
  103. implementation
  104. uses
  105. {$IFNDEF USE_FAKE_SYSUTILS}
  106. sysutils,
  107. {$ELSE}
  108. fksysutl,
  109. {$ENDIF}
  110. globtype,systems,constexp,compinnr,
  111. cutils,verbose,globals,widestr,
  112. tokens,
  113. symconst,symdef,symsym,symcpu,symtable,defutil,defcmp,
  114. cgbase,
  115. htypechk,pass_1,
  116. nld,nbas,nmat,ncnv,ncon,nset,nopt,ncal,ninl,nmem,nutils,
  117. {$ifdef state_tracking}
  118. nstate,
  119. {$endif}
  120. cpuinfo;
  121. {*****************************************************************************
  122. TADDNODE
  123. *****************************************************************************}
  124. {$maxfpuregisters 0}
  125. function getbestreal(t1,t2 : tdef) : tdef;
  126. const
  127. floatweight : array[tfloattype] of byte =
  128. (2,3,4,5,0,1,6);
  129. begin
  130. if t1.typ=floatdef then
  131. begin
  132. result:=t1;
  133. if t2.typ=floatdef then
  134. begin
  135. { when a comp or currency is used, use always the
  136. best float type to calculate the result }
  137. if (tfloatdef(t1).floattype in [s64comp,s64currency]) or
  138. (tfloatdef(t2).floattype in [s64comp,s64currency]) or
  139. (cs_excessprecision in current_settings.localswitches) then
  140. result:=pbestrealtype^
  141. else
  142. if floatweight[tfloatdef(t2).floattype]>floatweight[tfloatdef(t1).floattype] then
  143. result:=t2;
  144. end;
  145. end
  146. else if t2.typ=floatdef then
  147. result:=t2
  148. else internalerror(200508061);
  149. end;
  150. constructor taddnode.create(tt : tnodetype;l,r : tnode);
  151. begin
  152. inherited create(tt,l,r);
  153. end;
  154. constructor taddnode.create_internal(tt:tnodetype;l,r:tnode);
  155. begin
  156. create(tt,l,r);
  157. include(flags,nf_internal);
  158. end;
  159. constructor taddnode.ppuload(t: tnodetype; ppufile: tcompilerppufile);
  160. begin
  161. inherited ppuload(t, ppufile);
  162. ppufile.getderef(resultrealdefderef);
  163. end;
  164. procedure taddnode.ppuwrite(ppufile: tcompilerppufile);
  165. begin
  166. inherited ppuwrite(ppufile);
  167. ppufile.putderef(resultrealdefderef);
  168. end;
  169. procedure taddnode.buildderefimpl;
  170. begin
  171. inherited buildderefimpl;
  172. resultrealdefderef.build(resultrealdef);
  173. end;
  174. procedure taddnode.derefimpl;
  175. begin
  176. inherited derefimpl;
  177. resultrealdef:=tdef(resultrealdefderef.resolve);
  178. end;
  179. function taddnode.cmp_of_disjunct_ranges(var res : boolean) : boolean;
  180. var
  181. hp : tnode;
  182. realdef : tdef;
  183. v : tconstexprint;
  184. begin
  185. result:=false;
  186. { check for comparision with known result because the ranges of the operands don't overlap }
  187. if (is_constintnode(right) and (left.resultdef.typ=orddef) and
  188. { don't ignore type checks }
  189. is_subequal(right.resultdef,left.resultdef)) or
  190. (is_constintnode(left) and (right.resultdef.typ=orddef) and
  191. { don't ignore type checks }
  192. is_subequal(left.resultdef,right.resultdef)) then
  193. begin
  194. if is_constintnode(right) then
  195. begin
  196. hp:=left;
  197. v:=Tordconstnode(right).value;
  198. end
  199. else
  200. begin
  201. hp:=right;
  202. v:=Tordconstnode(left).value;
  203. end;
  204. realdef:=hp.resultdef;
  205. { stop with finding the real def when we either encounter
  206. a) an explicit type conversion (then the value has to be
  207. re-interpreted)
  208. b) an "absolute" type conversion (also requires
  209. re-interpretation)
  210. }
  211. while (hp.nodetype=typeconvn) and
  212. ([nf_internal,nf_explicit,nf_absolute] * hp.flags = []) do
  213. begin
  214. hp:=ttypeconvnode(hp).left;
  215. realdef:=hp.resultdef;
  216. end;
  217. if is_constintnode(left) then
  218. with torddef(realdef) do
  219. case nodetype of
  220. ltn:
  221. if v<low then
  222. begin
  223. result:=true;
  224. res:=true;
  225. end
  226. else if v>=high then
  227. begin
  228. result:=true;
  229. res:=false;
  230. end;
  231. lten:
  232. if v<=low then
  233. begin
  234. result:=true;
  235. res:=true;
  236. end
  237. else if v>high then
  238. begin
  239. result:=true;
  240. res:=false;
  241. end;
  242. gtn:
  243. if v<=low then
  244. begin
  245. result:=true;
  246. res:=false;
  247. end
  248. else if v>high then
  249. begin
  250. result:=true;
  251. res:=true;
  252. end;
  253. gten :
  254. if v<low then
  255. begin
  256. result:=true;
  257. res:=false;
  258. end
  259. else if v>=high then
  260. begin
  261. result:=true;
  262. res:=true;
  263. end;
  264. equaln:
  265. if (v<low) or (v>high) then
  266. begin
  267. result:=true;
  268. res:=false;
  269. end;
  270. unequaln:
  271. if (v<low) or (v>high) then
  272. begin
  273. result:=true;
  274. res:=true;
  275. end;
  276. else
  277. ;
  278. end
  279. else
  280. with torddef(realdef) do
  281. case nodetype of
  282. ltn:
  283. if high<v then
  284. begin
  285. result:=true;
  286. res:=true;
  287. end
  288. else if low>=v then
  289. begin
  290. result:=true;
  291. res:=false;
  292. end;
  293. lten:
  294. if high<=v then
  295. begin
  296. result:=true;
  297. res:=true;
  298. end
  299. else if low>v then
  300. begin
  301. result:=true;
  302. res:=false;
  303. end;
  304. gtn:
  305. if high<=v then
  306. begin
  307. result:=true;
  308. res:=false;
  309. end
  310. else if low>v then
  311. begin
  312. result:=true;
  313. res:=true;
  314. end;
  315. gten:
  316. if high<v then
  317. begin
  318. result:=true;
  319. res:=false;
  320. end
  321. else if low>=v then
  322. begin
  323. result:=true;
  324. res:=true;
  325. end;
  326. equaln:
  327. if (v<low) or (v>high) then
  328. begin
  329. result:=true;
  330. res:=false;
  331. end;
  332. unequaln:
  333. if (v<low) or (v>high) then
  334. begin
  335. result:=true;
  336. res:=true;
  337. end;
  338. else
  339. ;
  340. end;
  341. end;
  342. end;
  343. function taddnode.simplify(forinline : boolean) : tnode;
  344. function is_range_test(nodel, noder: taddnode; out value: tnode; var cl,cr: Tconstexprint): boolean;
  345. const
  346. is_upper_test: array[ltn..gten] of boolean = (true,true,false,false);
  347. inclusive_adjust: array[boolean,ltn..gten] of integer = ((-1,0,1,0),
  348. (1,0,-1,0));
  349. var
  350. swapl, swapr: Boolean;
  351. valuer: tnode;
  352. t: Tconstexprint;
  353. begin
  354. result:=false;
  355. swapl:=false;
  356. swapr:=false;
  357. if nodel.left.nodetype=ordconstn then
  358. begin
  359. swapl:=true;
  360. cl:=tordconstnode(nodel.left).value;
  361. value:=nodel.right;
  362. end
  363. else if nodel.right.nodetype=ordconstn then
  364. begin
  365. cl:=tordconstnode(nodel.right).value;
  366. value:=nodel.left;
  367. end
  368. else
  369. exit;
  370. if noder.left.nodetype=ordconstn then
  371. begin
  372. swapl:=true;
  373. cr:=tordconstnode(noder.left).value;
  374. valuer:=noder.right;
  375. end
  376. else if noder.right.nodetype=ordconstn then
  377. begin
  378. cr:=tordconstnode(noder.right).value;
  379. valuer:=noder.left;
  380. end
  381. else
  382. exit;
  383. if not value.isequal(valuer) then
  384. exit;
  385. { this could be simplified too, but probably never happens }
  386. if (is_upper_test[nodel.nodetype] xor swapl)=(is_upper_test[noder.nodetype] xor swapr) then
  387. exit;
  388. cl:=cl+inclusive_adjust[swapl,nodel.nodetype];
  389. cr:=cr+inclusive_adjust[swapr,noder.nodetype];
  390. if is_upper_test[nodel.nodetype] xor swapl then
  391. begin
  392. t:=cl;
  393. cl:=cr;
  394. cr:=t;
  395. end;
  396. if cl>cr then
  397. exit;
  398. result:=true;
  399. end;
  400. function IsLengthZero(n1,n2 : tnode) : Boolean;
  401. begin
  402. result:=is_inlinefunction(n1,in_length_x) and is_constintvalue(n2,0) and not(is_shortstring(tinlinenode(n1).left.resultdef));
  403. end;
  404. function TransformLengthZero(n1,n2 : tnode) : tnode;
  405. var
  406. len : Tconstexprint;
  407. begin
  408. if is_dynamic_array(tinlinenode(n1).left.resultdef) then
  409. len:=-1
  410. else
  411. len:=0;
  412. result:=caddnode.create_internal(orn,
  413. caddnode.create_internal(equaln,ctypeconvnode.create_internal(tinlinenode(n1).left.getcopy,voidpointertype),
  414. cpointerconstnode.create(0,voidpointertype)),
  415. caddnode.create_internal(equaln,
  416. ctypeconvnode.create_internal(
  417. cderefnode.create(
  418. caddnode.create_internal(subn,ctypeconvnode.create_internal(tinlinenode(n1).left.getcopy,voidpointertype),
  419. cordconstnode.create(sizesinttype.size,sizesinttype,false))
  420. ),sizesinttype
  421. ),
  422. cordconstnode.create(len,sizesinttype,false))
  423. );
  424. end;
  425. var
  426. t , vl: tnode;
  427. lt,rt : tnodetype;
  428. hdef,
  429. rd,ld : tdef;
  430. rv,lv,v : tconstexprint;
  431. rvd,lvd : bestreal;
  432. ws1,ws2 : pcompilerwidestring;
  433. concatstrings : boolean;
  434. c1,c2 : array[0..1] of char;
  435. s1,s2 : pchar;
  436. l1,l2 : longint;
  437. resultset : Tconstset;
  438. res,
  439. b : boolean;
  440. cr, cl : Tconstexprint;
  441. begin
  442. result:=nil;
  443. l1:=0;
  444. l2:=0;
  445. s1:=nil;
  446. s2:=nil;
  447. { load easier access variables }
  448. rd:=right.resultdef;
  449. ld:=left.resultdef;
  450. rt:=right.nodetype;
  451. lt:=left.nodetype;
  452. if (nodetype = slashn) and
  453. (((rt = ordconstn) and
  454. (tordconstnode(right).value = 0)) or
  455. ((rt = realconstn) and
  456. (trealconstnode(right).value_real = 0.0))) then
  457. begin
  458. if floating_point_range_check_error then
  459. begin
  460. result:=crealconstnode.create(1,pbestrealtype^);
  461. Message(parser_e_division_by_zero);
  462. exit;
  463. end;
  464. end;
  465. { both are int constants }
  466. if (
  467. is_constintnode(left) and
  468. is_constintnode(right)
  469. ) or
  470. (
  471. is_constboolnode(left) and
  472. is_constboolnode(right) and
  473. (nodetype in [slashn,ltn,lten,gtn,gten,equaln,unequaln,andn,xorn,orn])
  474. ) or
  475. (
  476. is_constenumnode(left) and
  477. is_constenumnode(right) and
  478. (allowenumop(nodetype) or (nf_internal in flags))
  479. ) or
  480. (
  481. (lt = pointerconstn) and
  482. is_constintnode(right) and
  483. (nodetype in [addn,subn])
  484. ) or
  485. (
  486. (rt = pointerconstn) and
  487. is_constintnode(left) and
  488. (nodetype=addn)
  489. ) or
  490. (
  491. (lt in [pointerconstn,niln]) and
  492. (rt in [pointerconstn,niln]) and
  493. (nodetype in [ltn,lten,gtn,gten,equaln,unequaln,subn])
  494. ) or
  495. (
  496. (lt = ordconstn) and (ld.typ = orddef) and is_currency(ld) and
  497. (rt = ordconstn) and (rd.typ = orddef) and is_currency(rd)
  498. ) then
  499. begin
  500. t:=nil;
  501. { load values }
  502. case lt of
  503. ordconstn:
  504. lv:=tordconstnode(left).value;
  505. pointerconstn:
  506. lv:=tpointerconstnode(left).value;
  507. niln:
  508. lv:=0;
  509. else
  510. internalerror(2002080202);
  511. end;
  512. case rt of
  513. ordconstn:
  514. rv:=tordconstnode(right).value;
  515. pointerconstn:
  516. rv:=tpointerconstnode(right).value;
  517. niln:
  518. rv:=0;
  519. else
  520. internalerror(2002080203);
  521. end;
  522. { type checking already took care of multiplying }
  523. { integer constants with pointeddef.size if necessary }
  524. case nodetype of
  525. addn :
  526. begin
  527. v:=lv+rv;
  528. if v.overflow then
  529. begin
  530. Message(parser_e_arithmetic_operation_overflow);
  531. { Recover }
  532. t:=genintconstnode(0)
  533. end
  534. else if (lt=pointerconstn) or (rt=pointerconstn) then
  535. t := cpointerconstnode.create(qword(v),resultdef)
  536. else
  537. if is_integer(ld) then
  538. t := create_simplified_ord_const(v,resultdef,forinline,cs_check_overflow in localswitches)
  539. else
  540. t := cordconstnode.create(v,resultdef,(ld.typ<>enumdef));
  541. end;
  542. subn :
  543. begin
  544. v:=lv-rv;
  545. if v.overflow then
  546. begin
  547. Message(parser_e_arithmetic_operation_overflow);
  548. { Recover }
  549. t:=genintconstnode(0)
  550. end
  551. else if (lt=pointerconstn) then
  552. { pointer-pointer results in an integer }
  553. if (rt=pointerconstn) then
  554. begin
  555. if not(nf_has_pointerdiv in flags) then
  556. internalerror(2008030101);
  557. t := cpointerconstnode.create(qword(v),resultdef)
  558. end
  559. else
  560. t := cpointerconstnode.create(qword(v),resultdef)
  561. else
  562. if is_integer(ld) then
  563. t := create_simplified_ord_const(v,resultdef,forinline,cs_check_overflow in localswitches)
  564. else
  565. t:=cordconstnode.create(v,resultdef,(ld.typ<>enumdef));
  566. end;
  567. muln :
  568. begin
  569. v:=lv*rv;
  570. if v.overflow then
  571. begin
  572. message(parser_e_arithmetic_operation_overflow);
  573. { Recover }
  574. t:=genintconstnode(0)
  575. end
  576. else
  577. t := create_simplified_ord_const(v,resultdef,forinline,cs_check_overflow in localswitches)
  578. end;
  579. xorn :
  580. if is_integer(ld) then
  581. t := create_simplified_ord_const(lv xor rv,resultdef,forinline,false)
  582. else
  583. t:=cordconstnode.create(lv xor rv,resultdef,true);
  584. orn :
  585. if is_integer(ld) then
  586. t:=create_simplified_ord_const(lv or rv,resultdef,forinline,false)
  587. else
  588. t:=cordconstnode.create(lv or rv,resultdef,true);
  589. andn :
  590. if is_integer(ld) then
  591. t:=create_simplified_ord_const(lv and rv,resultdef,forinline,false)
  592. else
  593. t:=cordconstnode.create(lv and rv,resultdef,true);
  594. ltn :
  595. t:=cordconstnode.create(ord(lv<rv),pasbool1type,true);
  596. lten :
  597. t:=cordconstnode.create(ord(lv<=rv),pasbool1type,true);
  598. gtn :
  599. t:=cordconstnode.create(ord(lv>rv),pasbool1type,true);
  600. gten :
  601. t:=cordconstnode.create(ord(lv>=rv),pasbool1type,true);
  602. equaln :
  603. t:=cordconstnode.create(ord(lv=rv),pasbool1type,true);
  604. unequaln :
  605. t:=cordconstnode.create(ord(lv<>rv),pasbool1type,true);
  606. slashn :
  607. begin
  608. { int/int becomes a real }
  609. rvd:=rv;
  610. lvd:=lv;
  611. t:=crealconstnode.create(lvd/rvd,resultrealdef);
  612. end;
  613. else
  614. internalerror(2008022101);
  615. end;
  616. if not forinline then
  617. include(t.flags,nf_internal);
  618. result:=t;
  619. exit;
  620. end
  621. else if cmp_of_disjunct_ranges(res) then
  622. begin
  623. if res then
  624. t:=Cordconstnode.create(1,pasbool1type,true)
  625. else
  626. t:=Cordconstnode.create(0,pasbool1type,true);
  627. { don't do this optimization, if the variable expression might
  628. have a side effect }
  629. if (is_constintnode(left) and might_have_sideeffects(right)) or
  630. (is_constintnode(right) and might_have_sideeffects(left)) then
  631. t.free
  632. else
  633. result:=t;
  634. exit;
  635. end;
  636. { Add,Sub,Mul,Or,Xor,Andn with constant 0, 1 or -1? }
  637. if is_constintnode(right) and is_integer(left.resultdef) then
  638. begin
  639. if tordconstnode(right).value = 0 then
  640. begin
  641. case nodetype of
  642. addn,subn,orn,xorn:
  643. result := left.getcopy;
  644. andn,muln:
  645. begin
  646. if (cs_opt_level4 in current_settings.optimizerswitches) or
  647. not might_have_sideeffects(left) then
  648. result:=cordconstnode.create(0,resultdef,true);
  649. end
  650. else
  651. ;
  652. end;
  653. end
  654. else if tordconstnode(right).value = 1 then
  655. begin
  656. case nodetype of
  657. muln:
  658. result := left.getcopy;
  659. else
  660. ;
  661. end;
  662. end
  663. else if tordconstnode(right).value = -1 then
  664. begin
  665. case nodetype of
  666. muln:
  667. result := cunaryminusnode.create(left.getcopy);
  668. else
  669. ;
  670. end;
  671. end;
  672. if assigned(result) then
  673. exit;
  674. end;
  675. if is_constintnode(left) and is_integer(right.resultdef) then
  676. begin
  677. if tordconstnode(left).value = 0 then
  678. begin
  679. case nodetype of
  680. addn,orn,xorn:
  681. result := right.getcopy;
  682. subn:
  683. result := cunaryminusnode.create(right.getcopy);
  684. andn,muln:
  685. begin
  686. if (cs_opt_level4 in current_settings.optimizerswitches) or
  687. not might_have_sideeffects(right) then
  688. result:=cordconstnode.create(0,resultdef,true);
  689. end;
  690. else
  691. ;
  692. end;
  693. end
  694. else if tordconstnode(left).value = 1 then
  695. begin
  696. case nodetype of
  697. muln:
  698. result := right.getcopy;
  699. else
  700. ;
  701. end;
  702. end
  703. {$ifdef VER2_2}
  704. else if (tordconstnode(left).value.svalue = -1) and (tordconstnode(left).value.signed) then
  705. {$else}
  706. else if tordconstnode(left).value = -1 then
  707. {$endif}
  708. begin
  709. case nodetype of
  710. muln:
  711. result := cunaryminusnode.create(right.getcopy);
  712. else
  713. ;
  714. end;
  715. end;
  716. if assigned(result) then
  717. exit;
  718. end;
  719. { both real constants ? }
  720. if (lt=realconstn) and (rt=realconstn) then
  721. begin
  722. lvd:=trealconstnode(left).value_real;
  723. rvd:=trealconstnode(right).value_real;
  724. case nodetype of
  725. addn :
  726. t:=crealconstnode.create(lvd+rvd,resultrealdef);
  727. subn :
  728. t:=crealconstnode.create(lvd-rvd,resultrealdef);
  729. muln :
  730. t:=crealconstnode.create(lvd*rvd,resultrealdef);
  731. starstarn:
  732. begin
  733. if lvd<0 then
  734. begin
  735. Message(parser_e_invalid_float_operation);
  736. t:=crealconstnode.create(0,resultrealdef);
  737. end
  738. else if lvd=0 then
  739. t:=crealconstnode.create(1.0,resultrealdef)
  740. else
  741. t:=crealconstnode.create(exp(ln(lvd)*rvd),resultrealdef);
  742. end;
  743. slashn :
  744. t:=crealconstnode.create(lvd/rvd,resultrealdef);
  745. ltn :
  746. t:=cordconstnode.create(ord(lvd<rvd),pasbool1type,true);
  747. lten :
  748. t:=cordconstnode.create(ord(lvd<=rvd),pasbool1type,true);
  749. gtn :
  750. t:=cordconstnode.create(ord(lvd>rvd),pasbool1type,true);
  751. gten :
  752. t:=cordconstnode.create(ord(lvd>=rvd),pasbool1type,true);
  753. equaln :
  754. t:=cordconstnode.create(ord(lvd=rvd),pasbool1type,true);
  755. unequaln :
  756. t:=cordconstnode.create(ord(lvd<>rvd),pasbool1type,true);
  757. else
  758. internalerror(2008022102);
  759. end;
  760. result:=t;
  761. exit;
  762. end;
  763. {$if (FPC_FULLVERSION>20700) and not defined(FPC_SOFT_FPUX80)}
  764. { bestrealrec is 2.7.1+ only }
  765. { replace .../const by a multiplication, but only if fastmath is enabled or
  766. the division is done by a power of 2, do not mess with special floating point values like Inf etc.
  767. do this after constant folding to avoid unnecessary precision loss if
  768. an slash expresion would be first converted into a multiplication and later
  769. folded }
  770. if (nodetype=slashn) and
  771. { do not mess with currency and comp types }
  772. (not(is_currency(right.resultdef)) and
  773. not((right.resultdef.typ=floatdef) and
  774. (tfloatdef(right.resultdef).floattype=s64comp)
  775. )
  776. ) and
  777. (((cs_opt_fastmath in current_settings.optimizerswitches) and (rt=ordconstn)) or
  778. ((cs_opt_fastmath in current_settings.optimizerswitches) and (rt=realconstn) and
  779. (bestrealrec(trealconstnode(right).value_real).SpecialType in [fsPositive,fsNegative])
  780. ) or
  781. ((rt=realconstn) and
  782. (bestrealrec(trealconstnode(right).value_real).SpecialType in [fsPositive,fsNegative]) and
  783. { mantissa returns the mantissa/fraction without the hidden 1, so power of two means only the hidden
  784. bit is set => mantissa must be 0 }
  785. (bestrealrec(trealconstnode(right).value_real).Mantissa=0)
  786. )
  787. ) then
  788. case rt of
  789. ordconstn:
  790. begin
  791. { the normal code handles div/0 }
  792. if (tordconstnode(right).value<>0) then
  793. begin
  794. nodetype:=muln;
  795. t:=crealconstnode.create(1/tordconstnode(right).value,resultdef);
  796. right.free;
  797. right:=t;
  798. exit;
  799. end;
  800. end;
  801. realconstn:
  802. begin
  803. nodetype:=muln;
  804. trealconstnode(right).value_real:=1.0/trealconstnode(right).value_real;
  805. exit;
  806. end;
  807. else
  808. ;
  809. end;
  810. {$endif FPC_FULLVERSION>20700}
  811. { first, we handle widestrings, so we can check later for }
  812. { stringconstn only }
  813. { widechars are converted above to widestrings too }
  814. { this isn't ver y efficient, but I don't think }
  815. { that it does matter that much (FK) }
  816. if (lt=stringconstn) and (rt=stringconstn) and
  817. (tstringconstnode(left).cst_type in [cst_widestring,cst_unicodestring]) and
  818. (tstringconstnode(right).cst_type in [cst_widestring,cst_unicodestring]) then
  819. begin
  820. initwidestring(ws1);
  821. initwidestring(ws2);
  822. copywidestring(pcompilerwidestring(tstringconstnode(left).value_str),ws1);
  823. copywidestring(pcompilerwidestring(tstringconstnode(right).value_str),ws2);
  824. case nodetype of
  825. addn :
  826. begin
  827. concatwidestrings(ws1,ws2);
  828. t:=cstringconstnode.createunistr(ws1);
  829. end;
  830. ltn :
  831. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<0),pasbool1type,true);
  832. lten :
  833. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<=0),pasbool1type,true);
  834. gtn :
  835. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>0),pasbool1type,true);
  836. gten :
  837. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>=0),pasbool1type,true);
  838. equaln :
  839. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)=0),pasbool1type,true);
  840. unequaln :
  841. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<>0),pasbool1type,true);
  842. else
  843. internalerror(2008022103);
  844. end;
  845. donewidestring(ws1);
  846. donewidestring(ws2);
  847. result:=t;
  848. exit;
  849. end;
  850. { concating strings ? }
  851. concatstrings:=false;
  852. if (lt=ordconstn) and (rt=ordconstn) and
  853. is_char(ld) and is_char(rd) then
  854. begin
  855. c1[0]:=char(int64(tordconstnode(left).value));
  856. c1[1]:=#0;
  857. l1:=1;
  858. c2[0]:=char(int64(tordconstnode(right).value));
  859. c2[1]:=#0;
  860. l2:=1;
  861. s1:=@c1[0];
  862. s2:=@c2[0];
  863. concatstrings:=true;
  864. end
  865. else if (lt=stringconstn) and (rt=ordconstn) and is_char(rd) then
  866. begin
  867. s1:=tstringconstnode(left).value_str;
  868. l1:=tstringconstnode(left).len;
  869. c2[0]:=char(int64(tordconstnode(right).value));
  870. c2[1]:=#0;
  871. s2:=@c2[0];
  872. l2:=1;
  873. concatstrings:=true;
  874. end
  875. else if (lt=ordconstn) and (rt=stringconstn) and is_char(ld) then
  876. begin
  877. c1[0]:=char(int64(tordconstnode(left).value));
  878. c1[1]:=#0;
  879. l1:=1;
  880. s1:=@c1[0];
  881. s2:=tstringconstnode(right).value_str;
  882. l2:=tstringconstnode(right).len;
  883. concatstrings:=true;
  884. end
  885. else if (lt=stringconstn) and (rt=stringconstn) then
  886. begin
  887. s1:=tstringconstnode(left).value_str;
  888. l1:=tstringconstnode(left).len;
  889. s2:=tstringconstnode(right).value_str;
  890. l2:=tstringconstnode(right).len;
  891. concatstrings:=true;
  892. end;
  893. if concatstrings then
  894. begin
  895. case nodetype of
  896. addn :
  897. begin
  898. t:=cstringconstnode.createpchar(concatansistrings(s1,s2,l1,l2),l1+l2,nil);
  899. typecheckpass(t);
  900. if not is_ansistring(resultdef) or
  901. (tstringdef(resultdef).encoding<>globals.CP_NONE) then
  902. tstringconstnode(t).changestringtype(resultdef)
  903. else
  904. tstringconstnode(t).changestringtype(getansistringdef)
  905. end;
  906. ltn :
  907. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<0),pasbool1type,true);
  908. lten :
  909. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<=0),pasbool1type,true);
  910. gtn :
  911. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>0),pasbool1type,true);
  912. gten :
  913. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>=0),pasbool1type,true);
  914. equaln :
  915. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)=0),pasbool1type,true);
  916. unequaln :
  917. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<>0),pasbool1type,true);
  918. else
  919. internalerror(2008022104);
  920. end;
  921. result:=t;
  922. exit;
  923. end;
  924. { set constant evaluation }
  925. if (right.nodetype=setconstn) and
  926. not assigned(tsetconstnode(right).left) and
  927. (left.nodetype=setconstn) and
  928. not assigned(tsetconstnode(left).left) then
  929. begin
  930. case nodetype of
  931. addn :
  932. begin
  933. resultset:=tsetconstnode(right).value_set^ + tsetconstnode(left).value_set^;
  934. t:=csetconstnode.create(@resultset,resultdef);
  935. end;
  936. muln :
  937. begin
  938. resultset:=tsetconstnode(right).value_set^ * tsetconstnode(left).value_set^;
  939. t:=csetconstnode.create(@resultset,resultdef);
  940. end;
  941. subn :
  942. begin
  943. resultset:=tsetconstnode(left).value_set^ - tsetconstnode(right).value_set^;
  944. t:=csetconstnode.create(@resultset,resultdef);
  945. end;
  946. symdifn :
  947. begin
  948. resultset:=tsetconstnode(right).value_set^ >< tsetconstnode(left).value_set^;
  949. t:=csetconstnode.create(@resultset,resultdef);
  950. end;
  951. unequaln :
  952. begin
  953. b:=tsetconstnode(right).value_set^ <> tsetconstnode(left).value_set^;
  954. t:=cordconstnode.create(byte(b),pasbool1type,true);
  955. end;
  956. equaln :
  957. begin
  958. b:=tsetconstnode(right).value_set^ = tsetconstnode(left).value_set^;
  959. t:=cordconstnode.create(byte(b),pasbool1type,true);
  960. end;
  961. lten :
  962. begin
  963. b:=tsetconstnode(left).value_set^ <= tsetconstnode(right).value_set^;
  964. t:=cordconstnode.create(byte(b),pasbool1type,true);
  965. end;
  966. gten :
  967. begin
  968. b:=tsetconstnode(left).value_set^ >= tsetconstnode(right).value_set^;
  969. t:=cordconstnode.create(byte(b),pasbool1type,true);
  970. end;
  971. else
  972. internalerror(2008022105);
  973. end;
  974. result:=t;
  975. exit;
  976. end;
  977. { in case of expressions having no side effect, we can simplify boolean expressions
  978. containing constants }
  979. if is_boolean(left.resultdef) and is_boolean(right.resultdef) then
  980. begin
  981. if is_constboolnode(left) and not(might_have_sideeffects(right)) then
  982. begin
  983. if ((nodetype=andn) and (tordconstnode(left).value<>0)) or
  984. ((nodetype=orn) and (tordconstnode(left).value=0)) or
  985. ((nodetype=xorn) and (tordconstnode(left).value=0)) then
  986. begin
  987. result:=right;
  988. right:=nil;
  989. exit;
  990. end
  991. else if ((nodetype=orn) and (tordconstnode(left).value<>0)) or
  992. ((nodetype=andn) and (tordconstnode(left).value=0)) then
  993. begin
  994. result:=left;
  995. left:=nil;
  996. exit;
  997. end
  998. else if ((nodetype=xorn) and (tordconstnode(left).value<>0)) then
  999. begin
  1000. result:=cnotnode.create(right);
  1001. right:=nil;
  1002. exit;
  1003. end
  1004. end
  1005. else if is_constboolnode(right) and not(might_have_sideeffects(left)) then
  1006. begin
  1007. if ((nodetype=andn) and (tordconstnode(right).value<>0)) or
  1008. ((nodetype=orn) and (tordconstnode(right).value=0)) or
  1009. ((nodetype=xorn) and (tordconstnode(right).value=0)) then
  1010. begin
  1011. result:=left;
  1012. left:=nil;
  1013. exit;
  1014. end
  1015. else if ((nodetype=orn) and (tordconstnode(right).value<>0)) or
  1016. ((nodetype=andn) and (tordconstnode(right).value=0)) then
  1017. begin
  1018. result:=right;
  1019. right:=nil;
  1020. exit;
  1021. end
  1022. else if ((nodetype=xorn) and (tordconstnode(right).value<>0)) then
  1023. begin
  1024. result:=cnotnode.create(left);
  1025. left:=nil;
  1026. exit;
  1027. end
  1028. end;
  1029. end;
  1030. { slow simplifications }
  1031. if cs_opt_level2 in current_settings.optimizerswitches then
  1032. begin
  1033. { the comparison is might be expensive and the nodes are usually only
  1034. equal if some previous optimizations were done so don't check
  1035. this simplification always
  1036. }
  1037. if is_boolean(left.resultdef) and is_boolean(right.resultdef) then
  1038. begin
  1039. { transform unsigned comparisons of (v>=x) and (v<=y)
  1040. into (v-x)<=(y-x)
  1041. }
  1042. if (nodetype=andn) and
  1043. (left.nodetype in [ltn,lten,gtn,gten]) and
  1044. (right.nodetype in [ltn,lten,gtn,gten]) and
  1045. (not might_have_sideeffects(left)) and
  1046. (not might_have_sideeffects(right)) and
  1047. is_range_test(taddnode(left),taddnode(right),vl,cl,cr) then
  1048. begin
  1049. hdef:=get_unsigned_inttype(vl.resultdef);
  1050. vl:=ctypeconvnode.create_internal(vl.getcopy,hdef);
  1051. result:=caddnode.create_internal(lten,
  1052. ctypeconvnode.create_internal(caddnode.create_internal(subn,vl,cordconstnode.create(cl,hdef,false)),hdef),
  1053. cordconstnode.create(cr-cl,hdef,false));
  1054. exit;
  1055. end;
  1056. { even when short circuit boolean evaluation is active, this
  1057. optimization cannot be performed in case the node has
  1058. side effects, because this can change the result (e.g., in an
  1059. or-node that calls the same function twice and first returns
  1060. false and then true because of a global state change }
  1061. if left.isequal(right) and not might_have_sideeffects(left) then
  1062. begin
  1063. case nodetype of
  1064. andn,orn:
  1065. begin
  1066. result:=left;
  1067. left:=nil;
  1068. exit;
  1069. end;
  1070. {
  1071. xorn:
  1072. begin
  1073. result:=cordconstnode.create(0,resultdef,true);
  1074. exit;
  1075. end;
  1076. }
  1077. else
  1078. ;
  1079. end;
  1080. end
  1081. { short to full boolean evalution possible and useful? }
  1082. else if not(might_have_sideeffects(right,[mhs_exceptions])) and not(cs_full_boolean_eval in localswitches) then
  1083. begin
  1084. case nodetype of
  1085. andn,orn:
  1086. begin
  1087. { full boolean evaluation is only useful if the nodes are not too complex and if no flags/jumps must be converted,
  1088. further, we need to know the expectloc }
  1089. if (node_complexity(right)<=2) and
  1090. not(left.expectloc in [LOC_FLAGS,LOC_JUMP,LOC_INVALID]) and not(right.expectloc in [LOC_FLAGS,LOC_JUMP,LOC_INVALID]) then
  1091. begin
  1092. { we need to copy the whole tree to force another pass_1 }
  1093. include(localswitches,cs_full_boolean_eval);
  1094. result:=getcopy;
  1095. exit;
  1096. end;
  1097. end;
  1098. else
  1099. ;
  1100. end;
  1101. end
  1102. end;
  1103. if is_integer(left.resultdef) and is_integer(right.resultdef) then
  1104. begin
  1105. if (cs_opt_level3 in current_settings.optimizerswitches) and
  1106. left.isequal(right) and not might_have_sideeffects(left) then
  1107. begin
  1108. case nodetype of
  1109. andn,orn:
  1110. begin
  1111. result:=left;
  1112. left:=nil;
  1113. exit;
  1114. end;
  1115. xorn,
  1116. subn,
  1117. unequaln,
  1118. ltn,
  1119. gtn:
  1120. begin
  1121. result:=cordconstnode.create(0,resultdef,true);
  1122. exit;
  1123. end;
  1124. equaln,
  1125. lten,
  1126. gten:
  1127. begin
  1128. result:=cordconstnode.create(1,resultdef,true);
  1129. exit;
  1130. end;
  1131. else
  1132. ;
  1133. end;
  1134. end
  1135. {$ifndef jvm}
  1136. else if (nodetype=equaln) and MatchAndTransformNodesCommutative(left,right,@IsLengthZero,@TransformLengthZero,Result) then
  1137. exit
  1138. {$endif jvm}
  1139. ;
  1140. end;
  1141. { using sqr(x) for reals instead of x*x might reduces register pressure and/or
  1142. memory accesses while sqr(<real>) has no drawback }
  1143. if
  1144. {$ifdef cpufpemu}
  1145. (current_settings.fputype<>fpu_soft) and
  1146. not(cs_fp_emulation in current_settings.moduleswitches) and
  1147. {$endif cpufpemu}
  1148. (nodetype=muln) and
  1149. is_real(left.resultdef) and is_real(right.resultdef) and
  1150. left.isequal(right) and
  1151. not(might_have_sideeffects(left)) then
  1152. begin
  1153. result:=cinlinenode.create(in_sqr_real,false,left);
  1154. left:=nil;
  1155. exit;
  1156. end;
  1157. {$ifdef cpurox}
  1158. { optimize (i shl x) or (i shr (bitsizeof(i)-x)) into rol(x,i) (and different flavours with shl/shr swapped etc.) }
  1159. if (nodetype=orn)
  1160. {$ifdef m68k}
  1161. and (CPUM68K_HAS_ROLROR in cpu_capabilities[current_settings.cputype])
  1162. {$endif m68k}
  1163. {$ifndef cpu64bitalu}
  1164. and (left.resultdef.typ=orddef) and
  1165. not(torddef(left.resultdef).ordtype in [s64bit,u64bit,scurrency])
  1166. {$endif cpu64bitalu}
  1167. then
  1168. begin
  1169. if (left.nodetype=shrn) and (right.nodetype=shln) and
  1170. is_constintnode(tshlshrnode(left).right) and
  1171. is_constintnode(tshlshrnode(right).right) and
  1172. (tordconstnode(tshlshrnode(right).right).value>0) and
  1173. (tordconstnode(tshlshrnode(left).right).value>0) and
  1174. tshlshrnode(left).left.isequal(tshlshrnode(right).left) and
  1175. not(might_have_sideeffects(tshlshrnode(left).left)) then
  1176. begin
  1177. if (tordconstnode(tshlshrnode(left).right).value=
  1178. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(right).right).value)
  1179. then
  1180. begin
  1181. result:=cinlinenode.create(in_ror_x_y,false,
  1182. ccallparanode.create(tshlshrnode(left).right,
  1183. ccallparanode.create(tshlshrnode(left).left,nil)));
  1184. tshlshrnode(left).left:=nil;
  1185. tshlshrnode(left).right:=nil;
  1186. exit;
  1187. end
  1188. else if (tordconstnode(tshlshrnode(right).right).value=
  1189. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(left).right).value)
  1190. then
  1191. begin
  1192. result:=cinlinenode.create(in_rol_x_y,false,
  1193. ccallparanode.create(tshlshrnode(right).right,
  1194. ccallparanode.create(tshlshrnode(left).left,nil)));
  1195. tshlshrnode(left).left:=nil;
  1196. tshlshrnode(right).right:=nil;
  1197. exit;
  1198. end;
  1199. end;
  1200. if (left.nodetype=shln) and (right.nodetype=shrn) and
  1201. is_constintnode(tshlshrnode(left).right) and
  1202. is_constintnode(tshlshrnode(right).right) and
  1203. (tordconstnode(tshlshrnode(right).right).value>0) and
  1204. (tordconstnode(tshlshrnode(left).right).value>0) and
  1205. tshlshrnode(left).left.isequal(tshlshrnode(right).left) and
  1206. not(might_have_sideeffects(tshlshrnode(left).left)) then
  1207. begin
  1208. if (tordconstnode(tshlshrnode(left).right).value=
  1209. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(right).right).value)
  1210. then
  1211. begin
  1212. result:=cinlinenode.create(in_rol_x_y,false,
  1213. ccallparanode.create(tshlshrnode(left).right,
  1214. ccallparanode.create(tshlshrnode(left).left,nil)));
  1215. tshlshrnode(left).left:=nil;
  1216. tshlshrnode(left).right:=nil;
  1217. exit;
  1218. end
  1219. else if (tordconstnode(tshlshrnode(right).right).value=
  1220. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(left).right).value)
  1221. then
  1222. begin
  1223. result:=cinlinenode.create(in_ror_x_y,false,
  1224. ccallparanode.create(tshlshrnode(right).right,
  1225. ccallparanode.create(tshlshrnode(left).left,nil)));
  1226. tshlshrnode(left).left:=nil;
  1227. tshlshrnode(right).right:=nil;
  1228. exit;
  1229. end;
  1230. end;
  1231. end;
  1232. {$endif cpurox}
  1233. end;
  1234. end;
  1235. function taddnode.dogetcopy: tnode;
  1236. var
  1237. n: taddnode;
  1238. begin
  1239. n:=taddnode(inherited dogetcopy);
  1240. n.resultrealdef:=resultrealdef;
  1241. result:=n;
  1242. end;
  1243. function taddnode.docompare(p: tnode): boolean;
  1244. begin
  1245. result:=
  1246. inherited docompare(p) and
  1247. equal_defs(taddnode(p).resultrealdef,resultrealdef);
  1248. end;
  1249. function taddnode.pass_typecheck:tnode;
  1250. begin
  1251. { This function is small to keep the stack small for recursive of
  1252. large + operations }
  1253. typecheckpass(left);
  1254. typecheckpass(right);
  1255. result:=pass_typecheck_internal;
  1256. end;
  1257. function taddnode.pass_typecheck_internal:tnode;
  1258. var
  1259. hp : tnode;
  1260. rd,ld,nd : tdef;
  1261. hsym : tfieldvarsym;
  1262. llow,lhigh,
  1263. rlow,rhigh : tconstexprint;
  1264. strtype : tstringtype;
  1265. res,
  1266. b : boolean;
  1267. lt,rt : tnodetype;
  1268. ot : tnodetype;
  1269. {$ifdef state_tracking}
  1270. factval : Tnode;
  1271. change : boolean;
  1272. {$endif}
  1273. function maybe_cast_ordconst(var n: tnode; adef: tdef): boolean;
  1274. begin
  1275. result:=(tordconstnode(n).value>=torddef(adef).low) and
  1276. (tordconstnode(n).value<=torddef(adef).high);
  1277. if result then
  1278. inserttypeconv(n,adef);
  1279. end;
  1280. function maybe_convert_to_insert:tnode;
  1281. function element_count(arrconstr: tarrayconstructornode):asizeint;
  1282. begin
  1283. result:=0;
  1284. while assigned(arrconstr) do
  1285. begin
  1286. if arrconstr.nodetype=arrayconstructorrangen then
  1287. internalerror(2018052501);
  1288. inc(result);
  1289. arrconstr:=tarrayconstructornode(tarrayconstructornode(arrconstr).right);
  1290. end;
  1291. end;
  1292. var
  1293. elem : tnode;
  1294. para : tcallparanode;
  1295. isarrconstrl,
  1296. isarrconstrr : boolean;
  1297. index : asizeint;
  1298. begin
  1299. result:=nil;
  1300. isarrconstrl:=left.nodetype=arrayconstructorn;
  1301. isarrconstrr:=right.nodetype=arrayconstructorn;
  1302. if not assigned(aktassignmentnode) or
  1303. (aktassignmentnode.right<>self) or
  1304. not(
  1305. isarrconstrl or
  1306. isarrconstrr
  1307. ) or
  1308. not(
  1309. left.isequal(aktassignmentnode.left) or
  1310. right.isequal(aktassignmentnode.left)
  1311. ) or
  1312. not valid_for_var(aktassignmentnode.left,false) or
  1313. (isarrconstrl and (element_count(tarrayconstructornode(left))>1)) or
  1314. (isarrconstrr and (element_count(tarrayconstructornode(right))>1)) then
  1315. exit;
  1316. if isarrconstrl then
  1317. begin
  1318. index:=0;
  1319. elem:=tarrayconstructornode(left).left;
  1320. tarrayconstructornode(left).left:=nil;
  1321. end
  1322. else
  1323. begin
  1324. index:=high(asizeint);
  1325. elem:=tarrayconstructornode(right).left;
  1326. tarrayconstructornode(right).left:=nil;
  1327. end;
  1328. { we use the fact that insert() caps the index to avoid a copy }
  1329. para:=ccallparanode.create(
  1330. cordconstnode.create(index,sizesinttype,false),
  1331. ccallparanode.create(
  1332. aktassignmentnode.left.getcopy,
  1333. ccallparanode.create(
  1334. elem,nil)));
  1335. result:=cinlinenode.create(in_insert_x_y_z,false,para);
  1336. include(aktassignmentnode.flags,nf_assign_done_in_right);
  1337. end;
  1338. begin
  1339. result:=nil;
  1340. rlow:=0;
  1341. llow:=0;
  1342. rhigh:=0;
  1343. lhigh:=0;
  1344. { avoid any problems with type parameters later on }
  1345. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  1346. begin
  1347. resultdef:=cundefinedtype;
  1348. exit;
  1349. end;
  1350. { both left and right need to be valid }
  1351. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1352. set_varstate(right,vs_read,[vsf_must_be_valid]);
  1353. if codegenerror then
  1354. exit;
  1355. { tp procvar support. Omit for converted assigned() nodes }
  1356. if not (nf_load_procvar in flags) then
  1357. begin
  1358. maybe_call_procvar(left,true);
  1359. maybe_call_procvar(right,true);
  1360. end
  1361. else
  1362. if not (nodetype in [equaln,unequaln]) then
  1363. InternalError(2013091601);
  1364. { allow operator overloading }
  1365. hp:=self;
  1366. if is_array_constructor(left.resultdef) or is_array_constructor(right.resultdef) then
  1367. begin
  1368. { check whether there is a suitable operator for the array constructor
  1369. (but only if the "+" array operator isn't used), if not fall back to sets }
  1370. if (
  1371. (nodetype<>addn) or
  1372. not (m_array_operators in current_settings.modeswitches) or
  1373. (is_array_constructor(left.resultdef) and not is_dynamic_array(right.resultdef)) or
  1374. (not is_dynamic_array(left.resultdef) and is_array_constructor(right.resultdef))
  1375. ) and
  1376. not isbinaryoverloaded(hp,[ocf_check_only]) then
  1377. begin
  1378. if is_array_constructor(left.resultdef) then
  1379. begin
  1380. arrayconstructor_to_set(left);
  1381. typecheckpass(left);
  1382. end;
  1383. if is_array_constructor(right.resultdef) then
  1384. begin
  1385. arrayconstructor_to_set(right);
  1386. typecheckpass(right);
  1387. end;
  1388. end;
  1389. end;
  1390. if is_dynamic_array(left.resultdef) and is_dynamic_array(right.resultdef) and
  1391. (nodetype=addn) and
  1392. (m_array_operators in current_settings.modeswitches) and
  1393. isbinaryoverloaded(hp,[ocf_check_non_overloadable,ocf_check_only]) then
  1394. message3(parser_w_operator_overloaded_hidden_3,left.resultdef.typename,arraytokeninfo[_PLUS].str,right.resultdef.typename);
  1395. if isbinaryoverloaded(hp,[]) then
  1396. begin
  1397. result:=hp;
  1398. exit;
  1399. end;
  1400. { Stop checking when an error was found in the operator checking }
  1401. if codegenerror then
  1402. begin
  1403. result:=cerrornode.create;
  1404. exit;
  1405. end;
  1406. { Kylix allows enum+ordconstn in an enum type declaration, we need to do
  1407. the conversion here before the constant folding }
  1408. if (m_delphi in current_settings.modeswitches) and
  1409. (blocktype in [bt_type,bt_const_type,bt_var_type]) then
  1410. begin
  1411. if (left.resultdef.typ=enumdef) and
  1412. (right.resultdef.typ=orddef) then
  1413. begin
  1414. { insert explicit typecast to default signed int }
  1415. left:=ctypeconvnode.create_internal(left,sinttype);
  1416. typecheckpass(left);
  1417. end
  1418. else
  1419. if (left.resultdef.typ=orddef) and
  1420. (right.resultdef.typ=enumdef) then
  1421. begin
  1422. { insert explicit typecast to default signed int }
  1423. right:=ctypeconvnode.create_internal(right,sinttype);
  1424. typecheckpass(right);
  1425. end;
  1426. end;
  1427. { is one a real float, then both need to be floats, this
  1428. need to be done before the constant folding so constant
  1429. operation on a float and int are also handled }
  1430. {$ifdef x86}
  1431. { use extended as default real type only when the x87 fpu is used }
  1432. {$if defined(i386) or defined(i8086)}
  1433. if not(current_settings.fputype=fpu_x87) then
  1434. resultrealdef:=s64floattype
  1435. else
  1436. resultrealdef:=pbestrealtype^;
  1437. {$endif i386 or i8086}
  1438. {$ifdef x86_64}
  1439. { x86-64 has no x87 only mode, so use always double as default }
  1440. resultrealdef:=s64floattype;
  1441. {$endif x86_6}
  1442. {$else not x86}
  1443. resultrealdef:=pbestrealtype^;
  1444. {$endif not x86}
  1445. if (right.resultdef.typ=floatdef) or (left.resultdef.typ=floatdef) then
  1446. begin
  1447. { when both floattypes are already equal then use that
  1448. floattype for results }
  1449. if (right.resultdef.typ=floatdef) and
  1450. (left.resultdef.typ=floatdef) and
  1451. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype) then
  1452. begin
  1453. if cs_excessprecision in current_settings.localswitches then
  1454. resultrealdef:=pbestrealtype^
  1455. else
  1456. resultrealdef:=left.resultdef
  1457. end
  1458. { when there is a currency type then use currency, but
  1459. only when currency is defined as float }
  1460. else
  1461. if (is_currency(right.resultdef) or
  1462. is_currency(left.resultdef)) and
  1463. ((s64currencytype.typ = floatdef) or
  1464. (nodetype <> slashn)) then
  1465. begin
  1466. resultrealdef:=s64currencytype;
  1467. inserttypeconv(right,resultrealdef);
  1468. inserttypeconv(left,resultrealdef);
  1469. end
  1470. else
  1471. begin
  1472. resultrealdef:=getbestreal(left.resultdef,right.resultdef);
  1473. inserttypeconv(right,resultrealdef);
  1474. inserttypeconv(left,resultrealdef);
  1475. end;
  1476. end;
  1477. { If both operands are constant and there is a unicodestring
  1478. or unicodestring then convert everything to unicodestring }
  1479. if is_constnode(right) and is_constnode(left) and
  1480. (is_unicodestring(right.resultdef) or
  1481. is_unicodestring(left.resultdef)) then
  1482. begin
  1483. inserttypeconv(right,cunicodestringtype);
  1484. inserttypeconv(left,cunicodestringtype);
  1485. end;
  1486. { If both operands are constant and there is a widechar
  1487. or widestring then convert everything to widestring. This
  1488. allows constant folding like char+widechar }
  1489. if is_constnode(right) and is_constnode(left) and
  1490. (is_widestring(right.resultdef) or
  1491. is_widestring(left.resultdef) or
  1492. is_widechar(right.resultdef) or
  1493. is_widechar(left.resultdef)) then
  1494. begin
  1495. inserttypeconv(right,cwidestringtype);
  1496. inserttypeconv(left,cwidestringtype);
  1497. end;
  1498. { load easier access variables }
  1499. rd:=right.resultdef;
  1500. ld:=left.resultdef;
  1501. rt:=right.nodetype;
  1502. lt:=left.nodetype;
  1503. { 4 character constant strings are compatible with orddef }
  1504. { in macpas mode (become cardinals) }
  1505. if (m_mac in current_settings.modeswitches) and
  1506. { only allow for comparisons, additions etc are }
  1507. { normally program errors }
  1508. (nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) and
  1509. (((lt=stringconstn) and
  1510. (tstringconstnode(left).len=4) and
  1511. (rd.typ=orddef)) or
  1512. ((rt=stringconstn) and
  1513. (tstringconstnode(right).len=4) and
  1514. (ld.typ=orddef))) then
  1515. begin
  1516. if (rt=stringconstn) then
  1517. begin
  1518. inserttypeconv(right,u32inttype);
  1519. rt:=right.nodetype;
  1520. rd:=right.resultdef;
  1521. end
  1522. else
  1523. begin
  1524. inserttypeconv(left,u32inttype);
  1525. lt:=left.nodetype;
  1526. ld:=left.resultdef;
  1527. end;
  1528. end;
  1529. { but an int/int gives real/real! }
  1530. if (nodetype=slashn) and not(is_vector(left.resultdef)) and not(is_vector(right.resultdef)) then
  1531. begin
  1532. if is_currency(left.resultdef) and
  1533. is_currency(right.resultdef) then
  1534. { In case of currency, converting to float means dividing by 10000 }
  1535. { However, since this is already a division, both divisions by }
  1536. { 10000 are eliminated when we divide the results -> we can skip }
  1537. { them. }
  1538. if s64currencytype.typ = floatdef then
  1539. begin
  1540. { there's no s64comptype or so, how do we avoid the type conversion?
  1541. left.resultdef := s64comptype;
  1542. right.resultdef := s64comptype; }
  1543. end
  1544. else
  1545. begin
  1546. left.resultdef := s64inttype;
  1547. right.resultdef := s64inttype;
  1548. end;
  1549. inserttypeconv(right,resultrealdef);
  1550. inserttypeconv(left,resultrealdef);
  1551. end
  1552. { if both are orddefs then check sub types }
  1553. else if (ld.typ=orddef) and (rd.typ=orddef) then
  1554. begin
  1555. { set for & and | operations in macpas mode: they only work on }
  1556. { booleans, and always short circuit evaluation }
  1557. if (nf_short_bool in flags) then
  1558. begin
  1559. if not is_boolean(ld) then
  1560. begin
  1561. inserttypeconv(left,pasbool1type);
  1562. ld := left.resultdef;
  1563. end;
  1564. if not is_boolean(rd) then
  1565. begin
  1566. inserttypeconv(right,pasbool1type);
  1567. rd := right.resultdef;
  1568. end;
  1569. end;
  1570. { 2 booleans? }
  1571. if (is_boolean(ld) and is_boolean(rd)) then
  1572. begin
  1573. case nodetype of
  1574. xorn,
  1575. andn,
  1576. orn:
  1577. begin
  1578. { in case of xor, or 'and' with full and cbool: convert both to Pascal bool and then
  1579. perform the xor/and to prevent issues with "longbool(1) and/xor
  1580. longbool(2)" }
  1581. if (is_cbool(ld) or is_cbool(rd)) and
  1582. ((nodetype=xorn) or
  1583. ((nodetype=andn) and
  1584. ((cs_full_boolean_eval in current_settings.localswitches) or
  1585. not(nf_short_bool in flags)
  1586. )
  1587. )
  1588. ) then
  1589. begin
  1590. resultdef:=nil;
  1591. if is_cbool(ld) then
  1592. begin
  1593. inserttypeconv(left,pasbool8type);
  1594. { inserttypeconv might already simplify
  1595. the typeconvnode after insertion,
  1596. thus we need to check if it still
  1597. really is a typeconv node }
  1598. if left is ttypeconvnode then
  1599. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  1600. if not is_cbool(rd) or
  1601. (ld.size>=rd.size) then
  1602. resultdef:=ld;
  1603. end;
  1604. if is_cbool(rd) then
  1605. begin
  1606. inserttypeconv(right,pasbool8type);
  1607. { inserttypeconv might already simplify
  1608. the typeconvnode after insertion,
  1609. thus we need to check if it still
  1610. really is a typeconv node }
  1611. if right is ttypeconvnode then
  1612. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  1613. if not assigned(resultdef) then
  1614. resultdef:=rd;
  1615. end;
  1616. result:=ctypeconvnode.create_explicit(caddnode.create(nodetype,left,right),resultdef);
  1617. ttypeconvnode(result).convtype:=tc_bool_2_bool;
  1618. left:=nil;
  1619. right:=nil;
  1620. exit;
  1621. end;
  1622. { Make sides equal to the largest boolean }
  1623. if (torddef(left.resultdef).size>torddef(right.resultdef).size) or
  1624. (is_cbool(left.resultdef) and not is_cbool(right.resultdef)) then
  1625. begin
  1626. right:=ctypeconvnode.create_internal(right,left.resultdef);
  1627. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  1628. typecheckpass(right);
  1629. end
  1630. else if (torddef(left.resultdef).size<torddef(right.resultdef).size) or
  1631. (not is_cbool(left.resultdef) and is_cbool(right.resultdef)) then
  1632. begin
  1633. left:=ctypeconvnode.create_internal(left,right.resultdef);
  1634. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  1635. typecheckpass(left);
  1636. end;
  1637. end;
  1638. ltn,
  1639. lten,
  1640. gtn,
  1641. gten:
  1642. begin
  1643. { convert both to pasbool to perform the comparison (so
  1644. that longbool(4) = longbool(2), since both represent
  1645. "true" }
  1646. inserttypeconv(left,pasbool1type);
  1647. inserttypeconv(right,pasbool1type);
  1648. end;
  1649. unequaln,
  1650. equaln:
  1651. begin
  1652. if not(cs_full_boolean_eval in current_settings.localswitches) or
  1653. (nf_short_bool in flags) then
  1654. begin
  1655. { Remove any compares with constants }
  1656. if (left.nodetype=ordconstn) then
  1657. begin
  1658. hp:=right;
  1659. b:=(tordconstnode(left).value<>0);
  1660. ot:=nodetype;
  1661. left.free;
  1662. left:=nil;
  1663. right:=nil;
  1664. if (not(b) and (ot=equaln)) or
  1665. (b and (ot=unequaln)) then
  1666. begin
  1667. hp:=cnotnode.create(hp);
  1668. end;
  1669. result:=hp;
  1670. exit;
  1671. end;
  1672. if (right.nodetype=ordconstn) then
  1673. begin
  1674. hp:=left;
  1675. b:=(tordconstnode(right).value<>0);
  1676. ot:=nodetype;
  1677. right.free;
  1678. right:=nil;
  1679. left:=nil;
  1680. if (not(b) and (ot=equaln)) or
  1681. (b and (ot=unequaln)) then
  1682. begin
  1683. hp:=cnotnode.create(hp);
  1684. end;
  1685. result:=hp;
  1686. exit;
  1687. end;
  1688. end;
  1689. { Delphi-compatibility: convert both to pasbool to
  1690. perform the equality comparison }
  1691. inserttypeconv(left,pasbool1type);
  1692. inserttypeconv(right,pasbool1type);
  1693. end;
  1694. else
  1695. begin
  1696. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1697. result:=cnothingnode.create;
  1698. exit;
  1699. end;
  1700. end;
  1701. end
  1702. { Both are chars? }
  1703. else if is_char(rd) and is_char(ld) then
  1704. begin
  1705. if nodetype=addn then
  1706. begin
  1707. resultdef:=cshortstringtype;
  1708. if not(is_constcharnode(left) and is_constcharnode(right)) then
  1709. begin
  1710. inserttypeconv(left,cshortstringtype);
  1711. {$ifdef addstringopt}
  1712. hp := genaddsstringcharoptnode(self);
  1713. result := hp;
  1714. exit;
  1715. {$endif addstringopt}
  1716. end
  1717. end
  1718. else if not(nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) then
  1719. begin
  1720. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1721. result:=cnothingnode.create;
  1722. exit;
  1723. end;
  1724. end
  1725. { There is a widechar? }
  1726. else if is_widechar(rd) or is_widechar(ld) then
  1727. begin
  1728. { widechar+widechar gives unicodestring }
  1729. if nodetype=addn then
  1730. begin
  1731. inserttypeconv(left,cunicodestringtype);
  1732. if (torddef(rd).ordtype<>uwidechar) then
  1733. inserttypeconv(right,cwidechartype);
  1734. resultdef:=cunicodestringtype;
  1735. end
  1736. else
  1737. begin
  1738. if (torddef(ld).ordtype<>uwidechar) then
  1739. inserttypeconv(left,cwidechartype);
  1740. if (torddef(rd).ordtype<>uwidechar) then
  1741. inserttypeconv(right,cwidechartype);
  1742. end;
  1743. end
  1744. { is there a currency type ? }
  1745. else if ((torddef(rd).ordtype=scurrency) or (torddef(ld).ordtype=scurrency)) then
  1746. begin
  1747. if (torddef(ld).ordtype<>scurrency) then
  1748. inserttypeconv(left,s64currencytype);
  1749. if (torddef(rd).ordtype<>scurrency) then
  1750. inserttypeconv(right,s64currencytype);
  1751. end
  1752. { leave some constant integer expressions alone in case the
  1753. resultdef of the integer types doesn't influence the outcome,
  1754. because the forced type conversions below can otherwise result
  1755. in unexpected results (such as high(qword)<high(int64) returning
  1756. true because high(qword) gets converted to int64) }
  1757. else if is_integer(ld) and is_integer(rd) and
  1758. (lt=ordconstn) and (rt=ordconstn) and
  1759. (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) then
  1760. begin
  1761. end
  1762. { "and" does't care about the sign of integers }
  1763. { "xor", "or" and compares don't need extension to native int }
  1764. { size either as long as both values are signed or unsigned }
  1765. { "xor" and "or" also don't care about the sign if the values }
  1766. { occupy an entire register }
  1767. { don't do it if either type is 64 bit (except for "and"), }
  1768. { since in that case we can't safely find a "common" type }
  1769. else if is_integer(ld) and is_integer(rd) and
  1770. ((nodetype=andn) or
  1771. ((nodetype in [orn,xorn,equaln,unequaln,gtn,gten,ltn,lten]) and
  1772. not is_64bitint(ld) and not is_64bitint(rd) and
  1773. (is_signed(ld)=is_signed(rd)))) then
  1774. begin
  1775. { Delphi-compatible: prefer unsigned type for "and", when the
  1776. unsigned type is bigger than the signed one, and also bigger
  1777. than min(native_int, 32-bit) }
  1778. if (is_oversizedint(rd) or is_nativeint(rd) or is_32bitint(rd)) and
  1779. (rd.size>=ld.size) and
  1780. not is_signed(rd) and is_signed(ld) then
  1781. inserttypeconv_internal(left,rd)
  1782. else if (is_oversizedint(ld) or is_nativeint(ld) or is_32bitint(ld)) and
  1783. (ld.size>=rd.size) and
  1784. not is_signed(ld) and is_signed(rd) then
  1785. inserttypeconv_internal(right,ld)
  1786. else
  1787. begin
  1788. { not to left right.resultdef, because that may
  1789. cause a range error if left and right's def don't
  1790. completely overlap }
  1791. nd:=get_common_intdef(torddef(ld),torddef(rd),true);
  1792. inserttypeconv(left,nd);
  1793. inserttypeconv(right,nd);
  1794. end;
  1795. end
  1796. { don't extend (sign-mismatched) comparisons if either side is a constant
  1797. whose value is within range of opposite side }
  1798. else if is_integer(ld) and is_integer(rd) and
  1799. (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1800. (is_signed(ld)<>is_signed(rd)) and
  1801. (
  1802. ((lt=ordconstn) and maybe_cast_ordconst(left,rd)) or
  1803. ((rt=ordconstn) and maybe_cast_ordconst(right,ld))
  1804. ) then
  1805. begin
  1806. { done here }
  1807. end
  1808. { is there a signed 64 bit type ? }
  1809. else if ((torddef(rd).ordtype=s64bit) or (torddef(ld).ordtype=s64bit)) then
  1810. begin
  1811. if (torddef(ld).ordtype<>s64bit) then
  1812. inserttypeconv(left,s64inttype);
  1813. if (torddef(rd).ordtype<>s64bit) then
  1814. inserttypeconv(right,s64inttype);
  1815. end
  1816. { is there a unsigned 64 bit type ? }
  1817. else if ((torddef(rd).ordtype=u64bit) or (torddef(ld).ordtype=u64bit)) then
  1818. begin
  1819. if (torddef(ld).ordtype<>u64bit) then
  1820. inserttypeconv(left,u64inttype);
  1821. if (torddef(rd).ordtype<>u64bit) then
  1822. inserttypeconv(right,u64inttype);
  1823. end
  1824. { is there a larger int? }
  1825. else if is_oversizedint(rd) or is_oversizedint(ld) then
  1826. begin
  1827. nd:=get_common_intdef(torddef(ld),torddef(rd),false);
  1828. inserttypeconv(right,nd);
  1829. inserttypeconv(left,nd);
  1830. end
  1831. { is there a native unsigned int? }
  1832. else if is_nativeuint(rd) or is_nativeuint(ld) then
  1833. begin
  1834. { convert positive constants to uinttype }
  1835. if (not is_nativeuint(ld)) and
  1836. is_constintnode(left) and
  1837. (tordconstnode(left).value >= 0) then
  1838. inserttypeconv(left,uinttype);
  1839. if (not is_nativeuint(rd)) and
  1840. is_constintnode(right) and
  1841. (tordconstnode(right).value >= 0) then
  1842. inserttypeconv(right,uinttype);
  1843. { when one of the operand is signed or the operation is subn then perform
  1844. the operation in a larger signed type, can't use rd/ld here because there
  1845. could be already typeconvs inserted.
  1846. This is compatible with the code below for other unsigned types (PFV) }
  1847. if is_signed(left.resultdef) or
  1848. is_signed(right.resultdef) or
  1849. (nodetype=subn) then
  1850. begin
  1851. if nodetype<>subn then
  1852. CGMessage(type_h_mixed_signed_unsigned);
  1853. { mark as internal in case added for a subn, so }
  1854. { ttypeconvnode.simplify can remove the larger }
  1855. { typecast again if semantically correct. Even }
  1856. { if we could detect that here already, we }
  1857. { mustn't do it here because that would change }
  1858. { overload choosing behaviour etc. The code in }
  1859. { ncnv.pas is run after that is already decided }
  1860. if (not is_signed(left.resultdef) and
  1861. not is_signed(right.resultdef)) or
  1862. (nodetype in [orn,xorn]) then
  1863. include(flags,nf_internal);
  1864. { get next larger signed int type }
  1865. nd:=get_common_intdef(torddef(sinttype),torddef(uinttype),false);
  1866. inserttypeconv(left,nd);
  1867. inserttypeconv(right,nd);
  1868. end
  1869. else
  1870. begin
  1871. if not is_nativeuint(left.resultdef) then
  1872. inserttypeconv(left,uinttype);
  1873. if not is_nativeuint(right.resultdef) then
  1874. inserttypeconv(right,uinttype);
  1875. end;
  1876. end
  1877. { generic ord conversion is sinttype }
  1878. else
  1879. begin
  1880. { When there is a signed type or there is a minus operation
  1881. we convert to signed int. Otherwise (both are unsigned) we keep
  1882. the result also unsigned. This is compatible with Delphi (PFV) }
  1883. if is_signed(ld) or
  1884. is_signed(rd) or
  1885. (nodetype=subn) then
  1886. begin
  1887. inserttypeconv(right,sinttype);
  1888. inserttypeconv(left,sinttype);
  1889. end
  1890. else
  1891. begin
  1892. inserttypeconv(right,uinttype);
  1893. inserttypeconv(left,uinttype);
  1894. end;
  1895. end;
  1896. end
  1897. { if both are floatdefs, conversion is already done before constant folding }
  1898. else if (ld.typ=floatdef) then
  1899. begin
  1900. if not(nodetype in [addn,subn,muln,slashn,equaln,unequaln,ltn,lten,gtn,gten]) then
  1901. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1902. end
  1903. { left side a setdef, must be before string processing,
  1904. else array constructor can be seen as array of char (PFV) }
  1905. else if (ld.typ=setdef) then
  1906. begin
  1907. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  1908. CGMessage(type_e_set_operation_unknown);
  1909. { right must either be a set or a set element }
  1910. if (rd.typ<>setdef) and
  1911. (rt<>setelementn) then
  1912. CGMessage(type_e_mismatch)
  1913. { Make operands the same setdef. If one's elementtype fits }
  1914. { entirely inside the other's, pick the one with the largest }
  1915. { range. Otherwise create a new setdef with a range which }
  1916. { can contain both. }
  1917. else if not(equal_defs(ld,rd)) then
  1918. begin
  1919. { note: ld cannot be an empty set with elementdef=nil in }
  1920. { case right is not a set, arrayconstructor_to_set takes }
  1921. { care of that }
  1922. { 1: rd is a set with an assigned elementdef, and ld is }
  1923. { either an empty set without elementdef or a set whose }
  1924. { elementdef fits in rd's elementdef -> convert to rd }
  1925. if ((rd.typ=setdef) and
  1926. assigned(tsetdef(rd).elementdef) and
  1927. (not assigned(tsetdef(ld).elementdef) or
  1928. is_in_limit(ld,rd))) then
  1929. inserttypeconv(left,rd)
  1930. { 2: rd is either an empty set without elementdef or a set }
  1931. { whose elementdef fits in ld's elementdef, or a set }
  1932. { element whose def fits in ld's elementdef -> convert }
  1933. { to ld. ld's elementdef can't be nil here, is caught }
  1934. { previous case and "note:" above }
  1935. else if ((rd.typ=setdef) and
  1936. (not assigned(tsetdef(rd).elementdef) or
  1937. is_in_limit(rd,ld))) or
  1938. ((rd.typ<>setdef) and
  1939. is_in_limit(rd,tsetdef(ld).elementdef)) then
  1940. if (rd.typ=setdef) then
  1941. inserttypeconv(right,ld)
  1942. else
  1943. inserttypeconv(right,tsetdef(ld).elementdef)
  1944. { 3: otherwise create setdef which encompasses both, taking }
  1945. { into account empty sets without elementdef }
  1946. else
  1947. begin
  1948. if assigned(tsetdef(ld).elementdef) then
  1949. begin
  1950. llow:=tsetdef(ld).setbase;
  1951. lhigh:=tsetdef(ld).setmax;
  1952. end;
  1953. if (rd.typ=setdef) then
  1954. if assigned(tsetdef(rd).elementdef) then
  1955. begin
  1956. rlow:=tsetdef(rd).setbase;
  1957. rhigh:=tsetdef(rd).setmax;
  1958. end
  1959. else
  1960. begin
  1961. { ld's elementdef must have been valid }
  1962. rlow:=llow;
  1963. rhigh:=lhigh;
  1964. end
  1965. else
  1966. getrange(rd,rlow,rhigh);
  1967. if not assigned(tsetdef(ld).elementdef) then
  1968. begin
  1969. llow:=rlow;
  1970. lhigh:=rhigh;
  1971. end;
  1972. nd:=csetdef.create(tsetdef(ld).elementdef,min(llow,rlow).svalue,max(lhigh,rhigh).svalue,true);
  1973. inserttypeconv(left,nd);
  1974. if (rd.typ=setdef) then
  1975. inserttypeconv(right,nd)
  1976. else
  1977. inserttypeconv(right,tsetdef(nd).elementdef);
  1978. end;
  1979. end;
  1980. end
  1981. { pointer comparision and subtraction }
  1982. else if (
  1983. (rd.typ=pointerdef) and (ld.typ=pointerdef)
  1984. ) or
  1985. { compare/add pchar to variable (not stringconst) char arrays
  1986. by addresses like BP/Delphi }
  1987. (
  1988. (nodetype in [equaln,unequaln,subn,addn]) and
  1989. (
  1990. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd) and (rt<>stringconstn)) or
  1991. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld) and (lt<>stringconstn))
  1992. )
  1993. ) then
  1994. begin
  1995. { convert char array to pointer }
  1996. if is_chararray(rd) then
  1997. begin
  1998. inserttypeconv(right,charpointertype);
  1999. rd:=right.resultdef;
  2000. end
  2001. else if is_chararray(ld) then
  2002. begin
  2003. inserttypeconv(left,charpointertype);
  2004. ld:=left.resultdef;
  2005. end;
  2006. case nodetype of
  2007. equaln,unequaln :
  2008. begin
  2009. if is_voidpointer(right.resultdef) then
  2010. inserttypeconv(right,left.resultdef)
  2011. else if is_voidpointer(left.resultdef) then
  2012. inserttypeconv(left,right.resultdef)
  2013. else if not(equal_defs(ld,rd)) then
  2014. IncompatibleTypes(ld,rd);
  2015. { now that the type checking is done, convert both to charpointer, }
  2016. { because methodpointers are 8 bytes even though only the first 4 }
  2017. { bytes must be compared. This can happen here if we are in }
  2018. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  2019. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  2020. { optimized away, since the result already was a voidpointer, so }
  2021. { use a charpointer instead (JM) }
  2022. {$if defined(jvm)}
  2023. inserttypeconv_internal(left,java_jlobject);
  2024. inserttypeconv_internal(right,java_jlobject);
  2025. {$elseif defined(i8086)}
  2026. if is_hugepointer(left.resultdef) then
  2027. inserttypeconv_internal(left,charhugepointertype)
  2028. else if is_farpointer(left.resultdef) then
  2029. inserttypeconv_internal(left,charfarpointertype)
  2030. else
  2031. inserttypeconv_internal(left,charnearpointertype);
  2032. if is_hugepointer(right.resultdef) then
  2033. inserttypeconv_internal(right,charhugepointertype)
  2034. else if is_farpointer(right.resultdef) then
  2035. inserttypeconv_internal(right,charfarpointertype)
  2036. else
  2037. inserttypeconv_internal(right,charnearpointertype);
  2038. {$else}
  2039. inserttypeconv_internal(left,charpointertype);
  2040. inserttypeconv_internal(right,charpointertype);
  2041. {$endif jvm}
  2042. end;
  2043. ltn,lten,gtn,gten:
  2044. begin
  2045. if (cs_extsyntax in current_settings.moduleswitches) or
  2046. (nf_internal in flags) then
  2047. begin
  2048. if is_voidpointer(right.resultdef) then
  2049. inserttypeconv(right,left.resultdef)
  2050. else if is_voidpointer(left.resultdef) then
  2051. inserttypeconv(left,right.resultdef)
  2052. else if not(equal_defs(ld,rd)) then
  2053. IncompatibleTypes(ld,rd);
  2054. end
  2055. else
  2056. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2057. end;
  2058. subn:
  2059. begin
  2060. if (cs_extsyntax in current_settings.moduleswitches) or
  2061. (nf_internal in flags) then
  2062. begin
  2063. if is_voidpointer(right.resultdef) then
  2064. begin
  2065. if is_big_untyped_addrnode(right) then
  2066. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  2067. inserttypeconv(right,left.resultdef)
  2068. end
  2069. else if is_voidpointer(left.resultdef) then
  2070. inserttypeconv(left,right.resultdef)
  2071. else if not(equal_defs(ld,rd)) then
  2072. IncompatibleTypes(ld,rd);
  2073. end
  2074. else
  2075. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2076. if not(nf_has_pointerdiv in flags) and
  2077. (tpointerdef(rd).pointeddef.size>1) then
  2078. begin
  2079. hp:=getcopy;
  2080. include(hp.flags,nf_has_pointerdiv);
  2081. result:=cmoddivnode.create(divn,hp,
  2082. cordconstnode.create(tpointerdef(rd).pointeddef.size,tpointerdef(rd).pointer_subtraction_result_type,false));
  2083. end;
  2084. resultdef:=tpointerdef(rd).pointer_subtraction_result_type;
  2085. exit;
  2086. end;
  2087. else
  2088. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2089. end;
  2090. end
  2091. { is one of the operands a string?,
  2092. chararrays are also handled as strings (after conversion), also take
  2093. care of chararray+chararray and chararray+char.
  2094. Note: Must be done after pointerdef+pointerdef has been checked, else
  2095. pchar is converted to string }
  2096. else if (rd.typ=stringdef) or
  2097. (ld.typ=stringdef) or
  2098. { stringconstn's can be arraydefs }
  2099. (lt=stringconstn) or
  2100. (rt=stringconstn) or
  2101. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  2102. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  2103. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  2104. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  2105. begin
  2106. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  2107. begin
  2108. { Is there a unicodestring? }
  2109. if is_unicodestring(rd) or is_unicodestring(ld) or
  2110. ((m_default_unicodestring in current_settings.modeswitches) and
  2111. (cs_refcountedstrings in current_settings.localswitches) and
  2112. (
  2113. is_pwidechar(rd) or is_widechararray(rd) or is_open_widechararray(rd) or (lt = stringconstn) or
  2114. is_pwidechar(ld) or is_widechararray(ld) or is_open_widechararray(ld) or (rt = stringconstn)
  2115. )
  2116. ) then
  2117. strtype:=st_unicodestring
  2118. else
  2119. { Is there a widestring? }
  2120. if is_widestring(rd) or is_widestring(ld) or
  2121. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  2122. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  2123. strtype:=st_widestring
  2124. else
  2125. if is_ansistring(rd) or is_ansistring(ld) or
  2126. ((cs_refcountedstrings in current_settings.localswitches) and
  2127. //todo: Move some of this to longstring's then they are implemented?
  2128. (
  2129. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or (lt = stringconstn) or
  2130. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld) or (rt = stringconstn)
  2131. )
  2132. ) then
  2133. strtype:=st_ansistring
  2134. else
  2135. if is_longstring(rd) or is_longstring(ld) then
  2136. strtype:=st_longstring
  2137. else
  2138. begin
  2139. { TODO: todo: add a warning/hint here if one converting a too large array}
  2140. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  2141. Note: Delphi halts with error if "array [0..xx] of char"
  2142. is assigned to ShortString and string length is less
  2143. then array size }
  2144. strtype:= st_shortstring;
  2145. end;
  2146. // Now convert nodes to common string type
  2147. case strtype of
  2148. st_widestring :
  2149. begin
  2150. if not(is_widestring(rd)) then
  2151. inserttypeconv(right,cwidestringtype);
  2152. if not(is_widestring(ld)) then
  2153. inserttypeconv(left,cwidestringtype);
  2154. end;
  2155. st_unicodestring :
  2156. begin
  2157. if not(is_unicodestring(rd)) then
  2158. inserttypeconv(right,cunicodestringtype);
  2159. if not(is_unicodestring(ld)) then
  2160. inserttypeconv(left,cunicodestringtype);
  2161. end;
  2162. st_ansistring :
  2163. begin
  2164. { use same code page if possible (don't force same code
  2165. page in case both are ansistrings with code page <>
  2166. CP_NONE, since then data loss can occur: the ansistring
  2167. helpers will convert them at run time to an encoding
  2168. that can represent both encodings) }
  2169. if is_ansistring(ld) and
  2170. (tstringdef(ld).encoding<>0) and
  2171. (tstringdef(ld).encoding<>globals.CP_NONE) and
  2172. (not is_ansistring(rd) or
  2173. (tstringdef(rd).encoding=0) or
  2174. (tstringdef(rd).encoding=globals.CP_NONE)) then
  2175. inserttypeconv(right,ld)
  2176. else if is_ansistring(rd) and
  2177. (tstringdef(rd).encoding<>0) and
  2178. (tstringdef(rd).encoding<>globals.CP_NONE) and
  2179. (not is_ansistring(ld) or
  2180. (tstringdef(ld).encoding=0) or
  2181. (tstringdef(ld).encoding=globals.CP_NONE)) then
  2182. inserttypeconv(left,rd)
  2183. else
  2184. begin
  2185. if not is_ansistring(ld) then
  2186. inserttypeconv(left,getansistringdef);
  2187. if not is_ansistring(rd) then
  2188. inserttypeconv(right,getansistringdef);
  2189. end;
  2190. end;
  2191. st_longstring :
  2192. begin
  2193. if not(is_longstring(rd)) then
  2194. inserttypeconv(right,clongstringtype);
  2195. if not(is_longstring(ld)) then
  2196. inserttypeconv(left,clongstringtype);
  2197. end;
  2198. st_shortstring :
  2199. begin
  2200. if not(is_shortstring(ld)) then
  2201. inserttypeconv(left,cshortstringtype);
  2202. { don't convert char, that can be handled by the optimized node }
  2203. if not(is_shortstring(rd) or is_char(rd)) then
  2204. inserttypeconv(right,cshortstringtype);
  2205. end;
  2206. end;
  2207. end
  2208. else
  2209. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2210. end
  2211. { implicit pointer object type comparison }
  2212. else if is_implicit_pointer_object_type(rd) or is_implicit_pointer_object_type(ld) then
  2213. begin
  2214. if (nodetype in [equaln,unequaln]) then
  2215. begin
  2216. if is_implicit_pointer_object_type(rd) and is_implicit_pointer_object_type(ld) then
  2217. begin
  2218. if def_is_related(tobjectdef(rd),tobjectdef(ld)) then
  2219. inserttypeconv(right,left.resultdef)
  2220. else
  2221. inserttypeconv(left,right.resultdef);
  2222. end
  2223. else if is_implicit_pointer_object_type(rd) then
  2224. inserttypeconv(left,right.resultdef)
  2225. else
  2226. inserttypeconv(right,left.resultdef);
  2227. end
  2228. else
  2229. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2230. end
  2231. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  2232. begin
  2233. if (nodetype in [equaln,unequaln]) then
  2234. begin
  2235. if def_is_related(tobjectdef(tclassrefdef(rd).pointeddef),
  2236. tobjectdef(tclassrefdef(ld).pointeddef)) then
  2237. inserttypeconv(right,left.resultdef)
  2238. else
  2239. inserttypeconv(left,right.resultdef);
  2240. end
  2241. else
  2242. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2243. end
  2244. { allow comparison with nil pointer }
  2245. else if is_implicit_pointer_object_type(rd) or (rd.typ=classrefdef) then
  2246. begin
  2247. if (nodetype in [equaln,unequaln]) then
  2248. inserttypeconv(left,right.resultdef)
  2249. else
  2250. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2251. end
  2252. else if is_implicit_pointer_object_type(ld) or (ld.typ=classrefdef) then
  2253. begin
  2254. if (nodetype in [equaln,unequaln]) then
  2255. inserttypeconv(right,left.resultdef)
  2256. else
  2257. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2258. end
  2259. { support procvar=nil,procvar<>nil }
  2260. else if ((ld.typ=procvardef) and (rt=niln)) or
  2261. ((rd.typ=procvardef) and (lt=niln)) then
  2262. begin
  2263. if not(nodetype in [equaln,unequaln]) then
  2264. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2265. { find proc field in methodpointer record }
  2266. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  2267. if not assigned(hsym) then
  2268. internalerror(200412043);
  2269. { For methodpointers compare only tmethodpointer.proc }
  2270. if (rd.typ=procvardef) and
  2271. (not tprocvardef(rd).is_addressonly) then
  2272. begin
  2273. right:=csubscriptnode.create(
  2274. hsym,
  2275. ctypeconvnode.create_internal(right,methodpointertype));
  2276. typecheckpass(right);
  2277. end;
  2278. if (ld.typ=procvardef) and
  2279. (not tprocvardef(ld).is_addressonly) then
  2280. begin
  2281. left:=csubscriptnode.create(
  2282. hsym,
  2283. ctypeconvnode.create_internal(left,methodpointertype));
  2284. typecheckpass(left);
  2285. end;
  2286. if lt=niln then
  2287. inserttypeconv_explicit(left,right.resultdef)
  2288. else
  2289. inserttypeconv_explicit(right,left.resultdef)
  2290. end
  2291. { <dyn. array>+<dyn. array> ? }
  2292. else if (nodetype=addn) and (is_dynamic_array(ld) or is_dynamic_array(rd)) then
  2293. begin
  2294. result:=maybe_convert_to_insert;
  2295. if assigned(result) then
  2296. exit;
  2297. if not(is_dynamic_array(ld)) then
  2298. inserttypeconv(left,rd);
  2299. if not(is_dynamic_array(rd)) then
  2300. inserttypeconv(right,ld);
  2301. end
  2302. { support dynamicarray=nil,dynamicarray<>nil }
  2303. else if (is_dynamic_array(ld) and (rt=niln)) or
  2304. (is_dynamic_array(rd) and (lt=niln)) or
  2305. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  2306. begin
  2307. if not(nodetype in [equaln,unequaln]) then
  2308. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2309. if lt=niln then
  2310. inserttypeconv_explicit(left,right.resultdef)
  2311. else
  2312. inserttypeconv_explicit(right,left.resultdef)
  2313. end
  2314. {$ifdef SUPPORT_MMX}
  2315. { mmx support, this must be before the zero based array
  2316. check }
  2317. else if (cs_mmx in current_settings.localswitches) and
  2318. is_mmx_able_array(ld) and
  2319. is_mmx_able_array(rd) and
  2320. equal_defs(ld,rd) then
  2321. begin
  2322. case nodetype of
  2323. addn,subn,xorn,orn,andn:
  2324. ;
  2325. { mul is a little bit restricted }
  2326. muln:
  2327. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  2328. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2329. else
  2330. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2331. end;
  2332. end
  2333. {$endif SUPPORT_MMX}
  2334. { vector support, this must be before the zero based array
  2335. check }
  2336. else if (cs_support_vectors in current_settings.globalswitches) and
  2337. is_vector(ld) and
  2338. is_vector(rd) and
  2339. equal_defs(ld,rd) then
  2340. begin
  2341. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  2342. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2343. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  2344. resultdef:=left.resultdef;
  2345. end
  2346. { this is a little bit dangerous, also the left type }
  2347. { pointer to should be checked! This broke the mmx support }
  2348. else if (rd.typ=pointerdef) or
  2349. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  2350. begin
  2351. if is_zero_based_array(rd) then
  2352. begin
  2353. resultdef:=cpointerdef.getreusable(tarraydef(rd).elementdef);
  2354. inserttypeconv(right,resultdef);
  2355. end
  2356. else
  2357. resultdef:=right.resultdef;
  2358. inserttypeconv(left,tpointerdef(right.resultdef).pointer_arithmetic_int_type);
  2359. if nodetype=addn then
  2360. begin
  2361. if (rt=niln) then
  2362. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,'NIL');
  2363. if (not(cs_extsyntax in current_settings.moduleswitches) and not(nf_internal in flags)) or
  2364. (not (is_pchar(ld) or is_chararray(ld) or is_open_chararray(ld) or is_widechar(ld) or is_widechararray(ld) or is_open_widechararray(ld)) and
  2365. not(cs_pointermath in current_settings.localswitches) and
  2366. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  2367. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2368. if (rd.typ=pointerdef) and
  2369. (tpointerdef(rd).pointeddef.size>1) then
  2370. begin
  2371. left:=caddnode.create(muln,left,
  2372. cordconstnode.create(tpointerdef(rd).pointeddef.size,tpointerdef(right.resultdef).pointer_arithmetic_int_type,true));
  2373. typecheckpass(left);
  2374. end;
  2375. end
  2376. else
  2377. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2378. end
  2379. else if (ld.typ=pointerdef) or
  2380. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  2381. begin
  2382. if is_zero_based_array(ld) then
  2383. begin
  2384. resultdef:=cpointerdef.getreusable(tarraydef(ld).elementdef);
  2385. inserttypeconv(left,resultdef);
  2386. end
  2387. else
  2388. resultdef:=left.resultdef;
  2389. inserttypeconv(right,tpointerdef(left.resultdef).pointer_arithmetic_int_type);
  2390. if nodetype in [addn,subn] then
  2391. begin
  2392. if (lt=niln) then
  2393. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),'NIL',rd.typename);
  2394. if (not(cs_extsyntax in current_settings.moduleswitches) and not(nf_internal in flags)) or
  2395. (not (is_pchar(ld) or is_chararray(ld) or is_open_chararray(ld) or is_widechar(ld) or is_widechararray(ld) or is_open_widechararray(ld)) and
  2396. not(cs_pointermath in current_settings.localswitches) and
  2397. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  2398. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2399. if (ld.typ=pointerdef) then
  2400. begin
  2401. if is_big_untyped_addrnode(left) then
  2402. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  2403. if (tpointerdef(ld).pointeddef.size>1) then
  2404. begin
  2405. right:=caddnode.create(muln,right,
  2406. cordconstnode.create(tpointerdef(ld).pointeddef.size,tpointerdef(left.resultdef).pointer_arithmetic_int_type,true));
  2407. typecheckpass(right);
  2408. end
  2409. end else
  2410. if is_zero_based_array(ld) and
  2411. (tarraydef(ld).elementdef.size>1) then
  2412. begin
  2413. right:=caddnode.create(muln,right,
  2414. cordconstnode.create(tarraydef(ld).elementdef.size,tpointerdef(left.resultdef).pointer_arithmetic_int_type,true));
  2415. typecheckpass(right);
  2416. end;
  2417. end
  2418. else
  2419. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2420. end
  2421. else if (rd.typ=procvardef) and
  2422. (ld.typ=procvardef) and
  2423. equal_defs(rd,ld) then
  2424. begin
  2425. if (nodetype in [equaln,unequaln]) then
  2426. begin
  2427. if tprocvardef(rd).is_addressonly then
  2428. begin
  2429. inserttypeconv_internal(right,voidcodepointertype);
  2430. inserttypeconv_internal(left,voidcodepointertype);
  2431. end
  2432. else
  2433. begin
  2434. { find proc field in methodpointer record }
  2435. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  2436. if not assigned(hsym) then
  2437. internalerror(200412043);
  2438. { Compare tmehodpointer(left).proc }
  2439. right:=csubscriptnode.create(
  2440. hsym,
  2441. ctypeconvnode.create_internal(right,methodpointertype));
  2442. typecheckpass(right);
  2443. left:=csubscriptnode.create(
  2444. hsym,
  2445. ctypeconvnode.create_internal(left,methodpointertype));
  2446. typecheckpass(left);
  2447. end;
  2448. end
  2449. else
  2450. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2451. end
  2452. { enums }
  2453. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  2454. begin
  2455. if allowenumop(nodetype) or (nf_internal in flags) then
  2456. inserttypeconv(right,left.resultdef)
  2457. else
  2458. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2459. end
  2460. { generic conversion, this is for error recovery }
  2461. else
  2462. begin
  2463. inserttypeconv(left,sinttype);
  2464. inserttypeconv(right,sinttype);
  2465. end;
  2466. if cmp_of_disjunct_ranges(res) and not(nf_internal in flags) then
  2467. begin
  2468. if res then
  2469. CGMessage(type_w_comparison_always_true)
  2470. else
  2471. CGMessage(type_w_comparison_always_false);
  2472. end;
  2473. { set resultdef if not already done }
  2474. if not assigned(resultdef) then
  2475. begin
  2476. case nodetype of
  2477. ltn,lten,gtn,gten,equaln,unequaln :
  2478. resultdef:=pasbool1type;
  2479. slashn :
  2480. resultdef:=resultrealdef;
  2481. addn:
  2482. begin
  2483. { for strings, return is always a 255 char string }
  2484. if is_shortstring(left.resultdef) then
  2485. resultdef:=cshortstringtype
  2486. else
  2487. { for ansistrings set resultdef to assignment left node
  2488. if it is an assignment and left node expects ansistring }
  2489. if is_ansistring(left.resultdef) and
  2490. assigned(aktassignmentnode) and
  2491. (aktassignmentnode.right=self) and
  2492. is_ansistring(aktassignmentnode.left.resultdef) then
  2493. resultdef:=aktassignmentnode.left.resultdef
  2494. else
  2495. resultdef:=left.resultdef;
  2496. end;
  2497. else
  2498. resultdef:=left.resultdef;
  2499. end;
  2500. end;
  2501. { when the result is currency we need some extra code for
  2502. multiplication and division. this should not be done when
  2503. the muln or slashn node is created internally }
  2504. if not(nf_is_currency in flags) and
  2505. is_currency(resultdef) then
  2506. begin
  2507. case nodetype of
  2508. slashn :
  2509. begin
  2510. { slashn will only work with floats }
  2511. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  2512. include(hp.flags,nf_is_currency);
  2513. result:=hp;
  2514. end;
  2515. muln :
  2516. begin
  2517. hp:=nil;
  2518. if s64currencytype.typ=floatdef then
  2519. begin
  2520. {$ifndef VER3_0}
  2521. { if left is a currency integer constant, we can get rid of the factor 10000 }
  2522. { int64(...) causes a cast on currency, so it is the currency value multiplied by 10000 }
  2523. if (left.nodetype=realconstn) and (is_currency(left.resultdef)) and ((int64(trealconstnode(left).value_currency) mod 10000)=0) then
  2524. begin
  2525. { trealconstnode expects that value_real and value_currency contain valid values }
  2526. trealconstnode(left).value_currency:=trealconstnode(left).value_currency {$ifdef FPC_CURRENCY_IS_INT64}div{$else}/{$endif} 10000;
  2527. trealconstnode(left).value_real:=trealconstnode(left).value_real/10000;
  2528. end
  2529. { or if right is an integer constant, we can get rid of its factor 10000 }
  2530. else if (right.nodetype=realconstn) and (is_currency(right.resultdef)) and ((int64(trealconstnode(right).value_currency) mod 10000)=0) then
  2531. begin
  2532. { trealconstnode expects that value and value_currency contain valid values }
  2533. trealconstnode(right).value_currency:=trealconstnode(right).value_currency {$ifdef FPC_CURRENCY_IS_INT64}div{$else}/{$endif} 10000;
  2534. trealconstnode(right).value_real:=trealconstnode(right).value_real/10000;
  2535. end
  2536. else
  2537. {$endif VER3_0}
  2538. begin
  2539. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype));
  2540. include(hp.flags,nf_is_currency);
  2541. end;
  2542. end
  2543. else
  2544. begin
  2545. {$ifndef VER3_0}
  2546. { if left is a currency integer constant, we can get rid of the factor 10000 }
  2547. if (left.nodetype=ordconstn) and (is_currency(left.resultdef)) and ((tordconstnode(left).value mod 10000)=0) then
  2548. tordconstnode(left).value:=tordconstnode(left).value div 10000
  2549. { or if right is an integer constant, we can get rid of its factor 10000 }
  2550. else if (right.nodetype=ordconstn) and (is_currency(right.resultdef)) and ((tordconstnode(right).value mod 10000)=0) then
  2551. tordconstnode(right).value:=tordconstnode(right).value div 10000
  2552. else
  2553. {$endif VER3_0}
  2554. begin
  2555. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  2556. include(hp.flags,nf_is_currency);
  2557. end
  2558. end;
  2559. result:=hp
  2560. end;
  2561. else
  2562. ;
  2563. end;
  2564. end;
  2565. if not codegenerror and
  2566. not assigned(result) then
  2567. result:=simplify(false);
  2568. end;
  2569. function taddnode.first_addstring: tnode;
  2570. const
  2571. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  2572. var
  2573. p: tnode;
  2574. newstatement : tstatementnode;
  2575. tempnode (*,tempnode2*) : ttempcreatenode;
  2576. cmpfuncname: string;
  2577. para: tcallparanode;
  2578. begin
  2579. result:=nil;
  2580. { when we get here, we are sure that both the left and the right }
  2581. { node are both strings of the same stringtype (JM) }
  2582. case nodetype of
  2583. addn:
  2584. begin
  2585. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  2586. begin
  2587. result:=right;
  2588. left.free;
  2589. left:=nil;
  2590. right:=nil;
  2591. exit;
  2592. end;
  2593. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  2594. begin
  2595. result:=left;
  2596. left:=nil;
  2597. right.free;
  2598. right:=nil;
  2599. exit;
  2600. end;
  2601. { create the call to the concat routine both strings as arguments }
  2602. if assigned(aktassignmentnode) and
  2603. (aktassignmentnode.right=self) and
  2604. (aktassignmentnode.left.resultdef=resultdef) and
  2605. valid_for_var(aktassignmentnode.left,false) then
  2606. begin
  2607. para:=ccallparanode.create(
  2608. right,
  2609. ccallparanode.create(
  2610. left,
  2611. ccallparanode.create(aktassignmentnode.left.getcopy,nil)
  2612. )
  2613. );
  2614. if is_ansistring(resultdef) then
  2615. para:=ccallparanode.create(
  2616. cordconstnode.create(
  2617. { don't use getparaencoding(), we have to know
  2618. when the result is rawbytestring }
  2619. tstringdef(resultdef).encoding,
  2620. u16inttype,
  2621. true
  2622. ),
  2623. para
  2624. );
  2625. result:=ccallnode.createintern(
  2626. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2627. para
  2628. );
  2629. include(aktassignmentnode.flags,nf_assign_done_in_right);
  2630. firstpass(result);
  2631. end
  2632. else
  2633. begin
  2634. result:=internalstatements(newstatement);
  2635. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2636. addstatement(newstatement,tempnode);
  2637. { initialize the temp, since it will be passed to a
  2638. var-parameter (and finalization, which is performed by the
  2639. ttempcreate node and which takes care of the initialization
  2640. on native targets, is a noop on managed VM targets) }
  2641. if (target_info.system in systems_managed_vm) and
  2642. is_managed_type(resultdef) then
  2643. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  2644. false,
  2645. ccallparanode.create(genintconstnode(0),
  2646. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  2647. para:=ccallparanode.create(
  2648. right,
  2649. ccallparanode.create(
  2650. left,
  2651. ccallparanode.create(ctemprefnode.create(tempnode),nil)
  2652. )
  2653. );
  2654. if is_ansistring(resultdef) then
  2655. para:=ccallparanode.create(
  2656. cordconstnode.create(
  2657. { don't use getparaencoding(), we have to know
  2658. when the result is rawbytestring }
  2659. tstringdef(resultdef).encoding,
  2660. u16inttype,
  2661. true
  2662. ),
  2663. para
  2664. );
  2665. addstatement(
  2666. newstatement,
  2667. ccallnode.createintern(
  2668. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2669. para
  2670. )
  2671. );
  2672. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2673. addstatement(newstatement,ctemprefnode.create(tempnode));
  2674. end;
  2675. { we reused the arguments }
  2676. left := nil;
  2677. right := nil;
  2678. end;
  2679. ltn,lten,gtn,gten,equaln,unequaln :
  2680. begin
  2681. { generate better code for comparison with empty string, we
  2682. only need to compare the length with 0 }
  2683. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  2684. { windows widestrings are too complicated to be handled optimized }
  2685. not(is_widestring(left.resultdef) and (target_info.system in systems_windows)) and
  2686. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  2687. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  2688. begin
  2689. { switch so that the constant is always on the right }
  2690. if left.nodetype = stringconstn then
  2691. begin
  2692. p := left;
  2693. left := right;
  2694. right := p;
  2695. nodetype:=swap_relation[nodetype];
  2696. end;
  2697. if is_shortstring(left.resultdef) or
  2698. (nodetype in [gtn,gten,ltn,lten]) or
  2699. (target_info.system in systems_managed_vm) then
  2700. { compare the length with 0 }
  2701. result := caddnode.create(nodetype,
  2702. cinlinenode.create(in_length_x,false,left),
  2703. cordconstnode.create(0,s8inttype,false))
  2704. else
  2705. begin
  2706. (*
  2707. if is_widestring(left.resultdef) and
  2708. (target_info.system in system_windows) then
  2709. begin
  2710. { windows like widestrings requires that we also check the length }
  2711. result:=internalstatements(newstatement);
  2712. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2713. tempnode2:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2714. addstatement(newstatement,tempnode);
  2715. addstatement(newstatement,tempnode2);
  2716. { poor man's cse }
  2717. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2718. ctypeconvnode.create_internal(left,voidpointertype))
  2719. );
  2720. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode2),
  2721. caddnode.create(orn,
  2722. caddnode.create(nodetype,
  2723. ctemprefnode.create(tempnode),
  2724. cpointerconstnode.create(0,voidpointertype)
  2725. ),
  2726. caddnode.create(nodetype,
  2727. ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),s32inttype),
  2728. cordconstnode.create(0,s32inttype,false)
  2729. )
  2730. )
  2731. ));
  2732. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2733. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode2));
  2734. addstatement(newstatement,ctemprefnode.create(tempnode2));
  2735. end
  2736. else
  2737. *)
  2738. begin
  2739. { compare the pointer with nil (for ansistrings etc), }
  2740. { faster than getting the length (JM) }
  2741. result:= caddnode.create(nodetype,
  2742. ctypeconvnode.create_internal(left,voidpointertype),
  2743. cpointerconstnode.create(0,voidpointertype));
  2744. end;
  2745. end;
  2746. { left is reused }
  2747. left := nil;
  2748. { right isn't }
  2749. right.free;
  2750. right := nil;
  2751. exit;
  2752. end;
  2753. { no string constant -> call compare routine }
  2754. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  2755. { for equality checks use optimized version }
  2756. if nodetype in [equaln,unequaln] then
  2757. cmpfuncname := cmpfuncname + '_equal';
  2758. result := ccallnode.createintern(cmpfuncname,
  2759. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2760. { and compare its result with 0 according to the original operator }
  2761. result := caddnode.create(nodetype,result,
  2762. cordconstnode.create(0,s8inttype,false));
  2763. left := nil;
  2764. right := nil;
  2765. end;
  2766. else
  2767. internalerror(2019050520);
  2768. end;
  2769. end;
  2770. function taddnode.first_addset : tnode;
  2771. procedure call_varset_helper(const n : string);
  2772. var
  2773. newstatement : tstatementnode;
  2774. temp : ttempcreatenode;
  2775. begin
  2776. { add two var sets }
  2777. result:=internalstatements(newstatement);
  2778. { create temp for result }
  2779. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2780. addstatement(newstatement,temp);
  2781. addstatement(newstatement,ccallnode.createintern(n,
  2782. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2783. ccallparanode.create(ctemprefnode.create(temp),
  2784. ccallparanode.create(right,
  2785. ccallparanode.create(left,nil)))))
  2786. );
  2787. { remove reused parts from original node }
  2788. left:=nil;
  2789. right:=nil;
  2790. { the last statement should return the value as
  2791. location and type, this is done be referencing the
  2792. temp and converting it first from a persistent temp to
  2793. normal temp }
  2794. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2795. addstatement(newstatement,ctemprefnode.create(temp));
  2796. end;
  2797. var
  2798. procname: string[31];
  2799. tempn: tnode;
  2800. newstatement : tstatementnode;
  2801. temp : ttempcreatenode;
  2802. begin
  2803. result:=nil;
  2804. case nodetype of
  2805. equaln,unequaln,lten,gten:
  2806. begin
  2807. case nodetype of
  2808. equaln,unequaln:
  2809. procname := 'fpc_varset_comp_sets';
  2810. lten,gten:
  2811. begin
  2812. procname := 'fpc_varset_contains_sets';
  2813. { (left >= right) = (right <= left) }
  2814. if nodetype = gten then
  2815. begin
  2816. tempn := left;
  2817. left := right;
  2818. right := tempn;
  2819. end;
  2820. end;
  2821. else
  2822. internalerror(2013112911);
  2823. end;
  2824. result := ccallnode.createinternres(procname,
  2825. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2826. ccallparanode.create(right,
  2827. ccallparanode.create(left,nil))),resultdef);
  2828. { left and right are reused as parameters }
  2829. left := nil;
  2830. right := nil;
  2831. { for an unequaln, we have to negate the result of comp_sets }
  2832. if nodetype = unequaln then
  2833. result := cnotnode.create(result);
  2834. end;
  2835. addn:
  2836. begin
  2837. { optimize first loading of a set }
  2838. if (right.nodetype=setelementn) and
  2839. not(assigned(tsetelementnode(right).right)) and
  2840. is_emptyset(left) then
  2841. begin
  2842. result:=internalstatements(newstatement);
  2843. { create temp for result }
  2844. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2845. addstatement(newstatement,temp);
  2846. { adjust for set base }
  2847. tsetelementnode(right).left:=caddnode.create(subn,
  2848. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2849. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2850. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  2851. ccallparanode.create(ctemprefnode.create(temp),
  2852. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2853. ccallparanode.create(tsetelementnode(right).left,nil))))
  2854. );
  2855. { the last statement should return the value as
  2856. location and type, this is done be referencing the
  2857. temp and converting it first from a persistent temp to
  2858. normal temp }
  2859. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2860. addstatement(newstatement,ctemprefnode.create(temp));
  2861. tsetelementnode(right).left := nil;
  2862. end
  2863. else
  2864. begin
  2865. if right.nodetype=setelementn then
  2866. begin
  2867. result:=internalstatements(newstatement);
  2868. { create temp for result }
  2869. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2870. addstatement(newstatement,temp);
  2871. { adjust for set base }
  2872. tsetelementnode(right).left:=caddnode.create(subn,
  2873. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2874. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2875. { add a range or a single element? }
  2876. if assigned(tsetelementnode(right).right) then
  2877. begin
  2878. { adjust for set base }
  2879. tsetelementnode(right).right:=caddnode.create(subn,
  2880. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  2881. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2882. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  2883. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2884. ccallparanode.create(tsetelementnode(right).right,
  2885. ccallparanode.create(tsetelementnode(right).left,
  2886. ccallparanode.create(ctemprefnode.create(temp),
  2887. ccallparanode.create(left,nil))))))
  2888. );
  2889. end
  2890. else
  2891. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  2892. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2893. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2894. ccallparanode.create(ctemprefnode.create(temp),
  2895. ccallparanode.create(left,nil)))))
  2896. );
  2897. { remove reused parts from original node }
  2898. tsetelementnode(right).right:=nil;
  2899. tsetelementnode(right).left:=nil;
  2900. left:=nil;
  2901. { the last statement should return the value as
  2902. location and type, this is done be referencing the
  2903. temp and converting it first from a persistent temp to
  2904. normal temp }
  2905. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2906. addstatement(newstatement,ctemprefnode.create(temp));
  2907. end
  2908. else
  2909. call_varset_helper('fpc_varset_add_sets');
  2910. end
  2911. end;
  2912. subn:
  2913. call_varset_helper('fpc_varset_sub_sets');
  2914. symdifn:
  2915. call_varset_helper('fpc_varset_symdif_sets');
  2916. muln:
  2917. call_varset_helper('fpc_varset_mul_sets');
  2918. else
  2919. internalerror(200609241);
  2920. end;
  2921. end;
  2922. function taddnode.first_adddynarray : tnode;
  2923. var
  2924. newstatement : tstatementnode;
  2925. tempnode (*,tempnode2*) : ttempcreatenode;
  2926. para: tcallparanode;
  2927. begin
  2928. result:=nil;
  2929. { when we get here, we are sure that both the left and the right }
  2930. { node are both strings of the same stringtype (JM) }
  2931. case nodetype of
  2932. addn:
  2933. begin
  2934. if (left.nodetype=arrayconstructorn) and (tarrayconstructornode(left).isempty) then
  2935. begin
  2936. result:=right;
  2937. left.free;
  2938. left:=nil;
  2939. right:=nil;
  2940. exit;
  2941. end;
  2942. if (right.nodetype=arrayconstructorn) and (tarrayconstructornode(right).isempty) then
  2943. begin
  2944. result:=left;
  2945. left:=nil;
  2946. right.free;
  2947. right:=nil;
  2948. exit;
  2949. end;
  2950. { create the call to the concat routine both strings as arguments }
  2951. if assigned(aktassignmentnode) and
  2952. (aktassignmentnode.right=self) and
  2953. (aktassignmentnode.left.resultdef=resultdef) and
  2954. valid_for_var(aktassignmentnode.left,false) then
  2955. begin
  2956. para:=ccallparanode.create(
  2957. ctypeconvnode.create_internal(right,voidcodepointertype),
  2958. ccallparanode.create(
  2959. ctypeconvnode.create_internal(left,voidcodepointertype),
  2960. ccallparanode.create(
  2961. caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  2962. ccallparanode.create(
  2963. ctypeconvnode.create_internal(aktassignmentnode.left.getcopy,voidcodepointertype),nil)
  2964. )));
  2965. result:=ccallnode.createintern(
  2966. 'fpc_dynarray_concat',
  2967. para
  2968. );
  2969. include(aktassignmentnode.flags,nf_assign_done_in_right);
  2970. firstpass(result);
  2971. end
  2972. else
  2973. begin
  2974. result:=internalstatements(newstatement);
  2975. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2976. addstatement(newstatement,tempnode);
  2977. { initialize the temp, since it will be passed to a
  2978. var-parameter (and finalization, which is performed by the
  2979. ttempcreate node and which takes care of the initialization
  2980. on native targets, is a noop on managed VM targets) }
  2981. if (target_info.system in systems_managed_vm) and
  2982. is_managed_type(resultdef) then
  2983. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  2984. false,
  2985. ccallparanode.create(genintconstnode(0),
  2986. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  2987. para:=ccallparanode.create(
  2988. ctypeconvnode.create_internal(right,voidcodepointertype),
  2989. ccallparanode.create(
  2990. ctypeconvnode.create_internal(left,voidcodepointertype),
  2991. ccallparanode.create(
  2992. caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  2993. ccallparanode.create(
  2994. ctypeconvnode.create_internal(ctemprefnode.create(tempnode),voidcodepointertype),nil)
  2995. )));
  2996. addstatement(
  2997. newstatement,
  2998. ccallnode.createintern(
  2999. 'fpc_dynarray_concat',
  3000. para
  3001. )
  3002. );
  3003. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  3004. addstatement(newstatement,ctemprefnode.create(tempnode));
  3005. end;
  3006. { we reused the arguments }
  3007. left := nil;
  3008. right := nil;
  3009. end;
  3010. unequaln,equaln:
  3011. { nothing to do }
  3012. ;
  3013. else
  3014. Internalerror(2018030301);
  3015. end;
  3016. end;
  3017. function taddnode.use_generic_mul32to64: boolean;
  3018. begin
  3019. result := true;
  3020. end;
  3021. function taddnode.use_generic_mul64bit: boolean;
  3022. begin
  3023. result := true;
  3024. end;
  3025. function taddnode.try_make_mul32to64: boolean;
  3026. function canbe32bitint(v: tconstexprint; out canbesignedconst, canbeunsignedconst: boolean): boolean;
  3027. begin
  3028. result := ((v >= int64(low(longint))) and (v <= int64(high(longint)))) or
  3029. ((v >= qword(low(cardinal))) and (v <= qword(high(cardinal))));
  3030. canbesignedconst:=v<=int64(high(longint));
  3031. canbeunsignedconst:=v>=0;
  3032. end;
  3033. function is_32bitordconst(n: tnode; out canbesignedconst, canbeunsignedconst: boolean): boolean;
  3034. begin
  3035. canbesignedconst:=false;
  3036. canbeunsignedconst:=false;
  3037. result := (n.nodetype = ordconstn) and
  3038. canbe32bitint(tordconstnode(n).value, canbesignedconst, canbeunsignedconst);
  3039. end;
  3040. function is_32to64typeconv(n: tnode): boolean;
  3041. begin
  3042. result := (n.nodetype = typeconvn) and
  3043. is_integer(ttypeconvnode(n).left.resultdef) and
  3044. not is_64bit(ttypeconvnode(n).left.resultdef);
  3045. end;
  3046. var
  3047. temp: tnode;
  3048. leftoriginallysigned,
  3049. canbesignedconst, canbeunsignedconst: boolean;
  3050. begin
  3051. result := false;
  3052. if is_32to64typeconv(left) then
  3053. begin
  3054. leftoriginallysigned:=is_signed(ttypeconvnode(left).left.resultdef);
  3055. if ((is_32bitordconst(right,canbesignedconst, canbeunsignedconst) and
  3056. ((leftoriginallysigned and canbesignedconst) or
  3057. (not leftoriginallysigned and canbeunsignedconst))) or
  3058. (is_32to64typeconv(right) and
  3059. ((leftoriginallysigned =
  3060. is_signed(ttypeconvnode(right).left.resultdef)) or
  3061. (leftoriginallysigned and
  3062. (torddef(ttypeconvnode(right).left.resultdef).ordtype in [u8bit,u16bit]))))) then
  3063. begin
  3064. temp := ttypeconvnode(left).left;
  3065. ttypeconvnode(left).left := nil;
  3066. left.free;
  3067. left := temp;
  3068. if (right.nodetype = typeconvn) then
  3069. begin
  3070. temp := ttypeconvnode(right).left;
  3071. ttypeconvnode(right).left := nil;
  3072. right.free;
  3073. right := temp;
  3074. end;
  3075. if (is_signed(left.resultdef)) then
  3076. begin
  3077. inserttypeconv_internal(left,s32inttype);
  3078. inserttypeconv_internal(right,s32inttype);
  3079. end
  3080. else
  3081. begin
  3082. inserttypeconv_internal(left,u32inttype);
  3083. inserttypeconv_internal(right,u32inttype);
  3084. end;
  3085. firstpass(left);
  3086. firstpass(right);
  3087. result := true;
  3088. end;
  3089. end;
  3090. end;
  3091. function taddnode.use_fma : boolean;
  3092. begin
  3093. result:=false;
  3094. end;
  3095. function taddnode.try_fma(ld,rd : tdef) : tnode;
  3096. var
  3097. inlinennr : tinlinenumber;
  3098. begin
  3099. result:=nil;
  3100. if (cs_opt_fastmath in current_settings.optimizerswitches) and
  3101. use_fma and
  3102. (nodetype in [addn,subn]) and
  3103. (rd.typ=floatdef) and (ld.typ=floatdef) and
  3104. (is_single(rd) or is_double(rd)) and
  3105. equal_defs(rd,ld) and
  3106. { transforming a*b+c into fma(a,b,c) makes only sense if c can be
  3107. calculated easily. Consider a*b+c*d which results in
  3108. fmul
  3109. fmul
  3110. fadd
  3111. and in
  3112. fmul
  3113. fma
  3114. when using the fma optimization. On a super scalar architecture, the first instruction
  3115. sequence requires clock_cycles(fmul)+clock_cycles(fadd) clock cycles because the fmuls can be executed in parallel.
  3116. The second sequence requires clock_cycles(fmul)+clock_cycles(fma) because the fma has to wait for the
  3117. result of the fmul. Since typically clock_cycles(fma)>clock_cycles(fadd) applies, the first sequence is better.
  3118. }
  3119. (((left.nodetype=muln) and (node_complexity(right)<3)) or
  3120. ((right.nodetype=muln) and (node_complexity(left)<3)) or
  3121. ((left.nodetype=inlinen) and
  3122. (tinlinenode(left).inlinenumber=in_sqr_real) and
  3123. (node_complexity(right)<3)) or
  3124. ((right.nodetype=inlinen) and
  3125. (tinlinenode(right).inlinenumber=in_sqr_real) and
  3126. (node_complexity(left)<3))
  3127. ) then
  3128. begin
  3129. case tfloatdef(ld).floattype of
  3130. s32real:
  3131. inlinennr:=in_fma_single;
  3132. s64real:
  3133. inlinennr:=in_fma_double;
  3134. s80real:
  3135. inlinennr:=in_fma_extended;
  3136. s128real:
  3137. inlinennr:=in_fma_float128;
  3138. else
  3139. internalerror(2014042601);
  3140. end;
  3141. if left.nodetype=muln then
  3142. begin
  3143. if nodetype=subn then
  3144. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(cunaryminusnode.create(right),
  3145. ccallparanode.create(taddnode(left).right,
  3146. ccallparanode.create(taddnode(left).left,nil
  3147. ))))
  3148. else
  3149. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(right,
  3150. ccallparanode.create(taddnode(left).right,
  3151. ccallparanode.create(taddnode(left).left,nil
  3152. ))));
  3153. right:=nil;
  3154. taddnode(left).right:=nil;
  3155. taddnode(left).left:=nil;
  3156. end
  3157. else if right.nodetype=muln then
  3158. begin
  3159. if nodetype=subn then
  3160. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3161. ccallparanode.create(cunaryminusnode.create(taddnode(right).right),
  3162. ccallparanode.create(taddnode(right).left,nil
  3163. ))))
  3164. else
  3165. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3166. ccallparanode.create(taddnode(right).right,
  3167. ccallparanode.create(taddnode(right).left,nil
  3168. ))));
  3169. left:=nil;
  3170. taddnode(right).right:=nil;
  3171. taddnode(right).left:=nil;
  3172. end
  3173. else if (left.nodetype=inlinen) and (tinlinenode(left).inlinenumber=in_sqr_real) then
  3174. begin
  3175. if nodetype=subn then
  3176. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(cunaryminusnode.create(right),
  3177. ccallparanode.create(tinlinenode(left).left.getcopy,
  3178. ccallparanode.create(tinlinenode(left).left.getcopy,nil
  3179. ))))
  3180. else
  3181. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(right,
  3182. ccallparanode.create(tinlinenode(left).left.getcopy,
  3183. ccallparanode.create(tinlinenode(left).left.getcopy,nil
  3184. ))));
  3185. right:=nil;
  3186. end
  3187. { we get here only if right is a sqr node }
  3188. else if (right.nodetype=inlinen) and (tinlinenode(right).inlinenumber=in_sqr_real) then
  3189. begin
  3190. if nodetype=subn then
  3191. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3192. ccallparanode.create(cunaryminusnode.create(tinlinenode(right).left.getcopy),
  3193. ccallparanode.create(tinlinenode(right).left.getcopy,nil
  3194. ))))
  3195. else
  3196. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3197. ccallparanode.create(tinlinenode(right).left.getcopy,
  3198. ccallparanode.create(tinlinenode(right).left.getcopy,nil
  3199. ))));
  3200. left:=nil;
  3201. end;
  3202. end;
  3203. end;
  3204. function taddnode.first_add64bitint: tnode;
  3205. var
  3206. procname: string[31];
  3207. temp: tnode;
  3208. power: longint;
  3209. begin
  3210. result := nil;
  3211. { create helper calls mul }
  3212. if nodetype <> muln then
  3213. exit;
  3214. { make sure that if there is a constant, that it's on the right }
  3215. if left.nodetype = ordconstn then
  3216. begin
  3217. temp := right;
  3218. right := left;
  3219. left := temp;
  3220. end;
  3221. { can we use a shift instead of a mul? }
  3222. if not (cs_check_overflow in current_settings.localswitches) and
  3223. (right.nodetype = ordconstn) and
  3224. ispowerof2(tordconstnode(right).value,power) then
  3225. begin
  3226. tordconstnode(right).value := power;
  3227. result := cshlshrnode.create(shln,left,right);
  3228. { left and right are reused }
  3229. left := nil;
  3230. right := nil;
  3231. { return firstpassed new node }
  3232. exit;
  3233. end;
  3234. if try_make_mul32to64 then
  3235. begin
  3236. { this uses the same criteria for signedness as the 32 to 64-bit mul
  3237. handling in the i386 code generator }
  3238. if is_signed(left.resultdef) and is_signed(right.resultdef) then
  3239. procname := 'fpc_mul_longint_to_int64'
  3240. else
  3241. procname := 'fpc_mul_dword_to_qword';
  3242. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  3243. result := ccallnode.createintern(procname,right);
  3244. left := nil;
  3245. right := nil;
  3246. end
  3247. else
  3248. begin
  3249. { can full 64-bit multiplication be handled inline? }
  3250. if not use_generic_mul64bit then
  3251. begin
  3252. { generic handling replaces this node with call to fpc_mul_int64,
  3253. whose result is int64 }
  3254. if is_currency(resultdef) then
  3255. resultdef:=s64inttype;
  3256. exit;
  3257. end;
  3258. { when currency is used set the result of the
  3259. parameters to s64bit, so they are not converted }
  3260. if is_currency(resultdef) then
  3261. begin
  3262. left.resultdef:=s64inttype;
  3263. right.resultdef:=s64inttype;
  3264. end;
  3265. { otherwise, create the parameters for the helper }
  3266. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  3267. left := nil;
  3268. { only qword needs the unsigned code, the
  3269. signed code is also used for currency }
  3270. if is_signed(resultdef) then
  3271. procname := 'fpc_mul_int64'
  3272. else
  3273. procname := 'fpc_mul_qword';
  3274. if cs_check_overflow in current_settings.localswitches then
  3275. procname := procname + '_checkoverflow';
  3276. result := ccallnode.createintern(procname,right);
  3277. right := nil;
  3278. end;
  3279. end;
  3280. function taddnode.first_addpointer: tnode;
  3281. begin
  3282. result:=nil;
  3283. expectloc:=LOC_REGISTER;
  3284. end;
  3285. function taddnode.first_cmppointer: tnode;
  3286. begin
  3287. result:=nil;
  3288. expectloc:=LOC_FLAGS;
  3289. end;
  3290. function taddnode.first_addfloat : tnode;
  3291. var
  3292. procname: string[31];
  3293. { do we need to reverse the result ? }
  3294. notnode : boolean;
  3295. fdef : tdef;
  3296. begin
  3297. result := nil;
  3298. notnode := false;
  3299. fdef := nil;
  3300. { In non-emulation mode, real opcodes are
  3301. emitted for floating point values.
  3302. }
  3303. if not ((cs_fp_emulation in current_settings.moduleswitches)
  3304. {$ifdef cpufpemu}
  3305. or (current_settings.fputype=fpu_soft)
  3306. {$endif cpufpemu}
  3307. ) then
  3308. exit;
  3309. if not(target_info.system in systems_wince) then
  3310. begin
  3311. case tfloatdef(left.resultdef).floattype of
  3312. s32real:
  3313. begin
  3314. fdef:=search_system_type('FLOAT32REC').typedef;
  3315. procname:='float32';
  3316. end;
  3317. s64real:
  3318. begin
  3319. fdef:=search_system_type('FLOAT64').typedef;
  3320. procname:='float64';
  3321. end;
  3322. {!!! not yet implemented
  3323. s128real:
  3324. }
  3325. else
  3326. internalerror(2005082601);
  3327. end;
  3328. case nodetype of
  3329. addn:
  3330. procname:=procname+'_add';
  3331. muln:
  3332. procname:=procname+'_mul';
  3333. subn:
  3334. procname:=procname+'_sub';
  3335. slashn:
  3336. procname:=procname+'_div';
  3337. ltn:
  3338. procname:=procname+'_lt';
  3339. lten:
  3340. procname:=procname+'_le';
  3341. gtn:
  3342. begin
  3343. procname:=procname+'_lt';
  3344. swapleftright;
  3345. end;
  3346. gten:
  3347. begin
  3348. procname:=procname+'_le';
  3349. swapleftright;
  3350. end;
  3351. equaln:
  3352. procname:=procname+'_eq';
  3353. unequaln:
  3354. begin
  3355. procname:=procname+'_eq';
  3356. notnode:=true;
  3357. end;
  3358. else
  3359. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  3360. end;
  3361. end
  3362. else
  3363. begin
  3364. case nodetype of
  3365. addn:
  3366. procname:='add';
  3367. muln:
  3368. procname:='mul';
  3369. subn:
  3370. procname:='sub';
  3371. slashn:
  3372. procname:='div';
  3373. ltn:
  3374. procname:='lt';
  3375. lten:
  3376. procname:='le';
  3377. gtn:
  3378. procname:='gt';
  3379. gten:
  3380. procname:='ge';
  3381. equaln:
  3382. procname:='eq';
  3383. unequaln:
  3384. procname:='ne';
  3385. else
  3386. begin
  3387. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  3388. exit;
  3389. end;
  3390. end;
  3391. case tfloatdef(left.resultdef).floattype of
  3392. s32real:
  3393. begin
  3394. procname:=procname+'s';
  3395. if nodetype in [addn,muln,subn,slashn] then
  3396. procname:=lower(procname);
  3397. end;
  3398. s64real:
  3399. procname:=procname+'d';
  3400. {!!! not yet implemented
  3401. s128real:
  3402. }
  3403. else
  3404. internalerror(2005082602);
  3405. end;
  3406. end;
  3407. { cast softfpu result? }
  3408. if not(target_info.system in systems_wince) then
  3409. begin
  3410. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  3411. resultdef:=pasbool1type;
  3412. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  3413. ctypeconvnode.create_internal(right,fdef),
  3414. ccallparanode.create(
  3415. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  3416. end
  3417. else
  3418. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  3419. ccallparanode.create(left,nil)));
  3420. left:=nil;
  3421. right:=nil;
  3422. { do we need to reverse the result }
  3423. if notnode then
  3424. result:=cnotnode.create(result);
  3425. end;
  3426. {$ifdef cpuneedsmulhelper}
  3427. function taddnode.use_mul_helper: boolean;
  3428. begin
  3429. result:=(nodetype=muln) and
  3430. not(torddef(resultdef).ordtype in [u8bit,s8bit
  3431. {$if defined(cpu16bitalu) or defined(avr)},u16bit,s16bit{$endif}]);
  3432. end;
  3433. {$endif cpuneedsmulhelper}
  3434. function taddnode.pass_1 : tnode;
  3435. function isconstsetfewelements(p : tnode) : boolean;
  3436. begin
  3437. result:=(p.nodetype=setconstn) and (tsetconstnode(p).elements<=4);
  3438. end;
  3439. var
  3440. {$ifdef addstringopt}
  3441. hp : tnode;
  3442. {$endif addstringopt}
  3443. rd,ld : tdef;
  3444. i,i2 : longint;
  3445. lt,rt : tnodetype;
  3446. {$ifdef cpuneedsmulhelper}
  3447. procname : string[32];
  3448. {$endif cpuneedsmulhelper}
  3449. tempn,varsetnode: tnode;
  3450. mulnode : taddnode;
  3451. constsetnode : tsetconstnode;
  3452. trycreateinnodes : Boolean;
  3453. begin
  3454. result:=nil;
  3455. { Can we optimize multiple string additions into a single call?
  3456. This need to be done on a complete tree to detect the multiple
  3457. add nodes and is therefor done before the subtrees are processed }
  3458. if canbemultistringadd(self) then
  3459. begin
  3460. result:=genmultistringadd(self);
  3461. exit;
  3462. end;
  3463. { Can we optimize multiple dyn. array additions into a single call?
  3464. This need to be done on a complete tree to detect the multiple
  3465. add nodes and is therefor done before the subtrees are processed }
  3466. if (m_array_operators in current_settings.modeswitches) and canbemultidynarrayadd(self) then
  3467. begin
  3468. result:=genmultidynarrayadd(self);
  3469. exit;
  3470. end;
  3471. { typical set tests like (s*[const. set])<>/=[] can be converted into an or'ed chain of in tests
  3472. for var sets if const. set contains only a few elements }
  3473. if (cs_opt_level1 in current_settings.optimizerswitches) and (nodetype in [unequaln,equaln]) and (left.resultdef.typ=setdef) and not(is_smallset(left.resultdef)) then
  3474. begin
  3475. trycreateinnodes:=false;
  3476. mulnode:=nil;
  3477. if (is_emptyset(right) and (left.nodetype=muln) and
  3478. (isconstsetfewelements(taddnode(left).right) or isconstsetfewelements(taddnode(left).left))) then
  3479. begin
  3480. trycreateinnodes:=true;
  3481. mulnode:=taddnode(left);
  3482. end
  3483. else if (is_emptyset(left) and (right.nodetype=muln) and
  3484. (isconstsetfewelements(taddnode(right).right) or isconstsetfewelements(taddnode(right).left))) then
  3485. begin
  3486. trycreateinnodes:=true;
  3487. mulnode:=taddnode(right);
  3488. end;
  3489. if trycreateinnodes then
  3490. begin
  3491. constsetnode:=nil;
  3492. varsetnode:=nil;
  3493. if isconstsetfewelements(mulnode.right) then
  3494. begin
  3495. constsetnode:=tsetconstnode(mulnode.right);
  3496. varsetnode:=mulnode.left;
  3497. end
  3498. else
  3499. begin
  3500. constsetnode:=tsetconstnode(mulnode.left);
  3501. varsetnode:=mulnode.right;
  3502. end;
  3503. { the node is copied so it might have no side effects, if the complexity is too, cse should fix it, so
  3504. do not check complexity }
  3505. if not(might_have_sideeffects(varsetnode)) then
  3506. begin
  3507. result:=nil;
  3508. for i:=low(tconstset) to high(tconstset) do
  3509. if i in constsetnode.value_set^ then
  3510. begin
  3511. tempn:=cinnode.create(cordconstnode.create(i,tsetdef(constsetnode.resultdef).elementdef,false),varsetnode.getcopy);
  3512. if assigned(result) then
  3513. result:=caddnode.create_internal(orn,result,tempn)
  3514. else
  3515. result:=tempn;
  3516. end;
  3517. if nodetype=equaln then
  3518. result:=cnotnode.create(result);
  3519. exit;
  3520. end;
  3521. end;
  3522. end;
  3523. { first do the two subtrees }
  3524. firstpass(left);
  3525. firstpass(right);
  3526. if codegenerror then
  3527. exit;
  3528. { load easier access variables }
  3529. rd:=right.resultdef;
  3530. ld:=left.resultdef;
  3531. rt:=right.nodetype;
  3532. lt:=left.nodetype;
  3533. { int/int gives real/real! }
  3534. if nodetype=slashn then
  3535. begin
  3536. {$ifdef cpufpemu}
  3537. result:=first_addfloat;
  3538. if assigned(result) then
  3539. exit;
  3540. {$endif cpufpemu}
  3541. expectloc:=LOC_FPUREGISTER;
  3542. end
  3543. { if both are orddefs then check sub types }
  3544. else if (ld.typ=orddef) and (rd.typ=orddef) then
  3545. begin
  3546. { optimize multiplacation by a power of 2 }
  3547. if not(cs_check_overflow in current_settings.localswitches) and
  3548. (nodetype = muln) and
  3549. (((left.nodetype = ordconstn) and
  3550. ispowerof2(tordconstnode(left).value,i)) or
  3551. ((right.nodetype = ordconstn) and
  3552. ispowerof2(tordconstnode(right).value,i2))) then
  3553. begin
  3554. if ((left.nodetype = ordconstn) and
  3555. ispowerof2(tordconstnode(left).value,i)) then
  3556. begin
  3557. tordconstnode(left).value := i;
  3558. result := cshlshrnode.create(shln,right,left);
  3559. end
  3560. else
  3561. begin
  3562. tordconstnode(right).value := i2;
  3563. result := cshlshrnode.create(shln,left,right);
  3564. end;
  3565. result.resultdef := resultdef;
  3566. left := nil;
  3567. right := nil;
  3568. exit;
  3569. end;
  3570. { 2 booleans ? }
  3571. if is_boolean(ld) and is_boolean(rd) then
  3572. begin
  3573. if (not(cs_full_boolean_eval in current_settings.localswitches) or
  3574. (nf_short_bool in flags)) and
  3575. (nodetype in [andn,orn]) then
  3576. expectloc:=LOC_JUMP
  3577. else
  3578. begin
  3579. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  3580. expectloc:=LOC_FLAGS
  3581. else
  3582. expectloc:=LOC_REGISTER;
  3583. end;
  3584. end
  3585. else
  3586. { Both are chars? only convert to shortstrings for addn }
  3587. if is_char(ld) then
  3588. begin
  3589. if nodetype=addn then
  3590. internalerror(200103291);
  3591. expectloc:=LOC_FLAGS;
  3592. end
  3593. else if (nodetype=muln) and
  3594. is_64bitint(resultdef) and
  3595. not use_generic_mul32to64 and
  3596. try_make_mul32to64 then
  3597. begin
  3598. { if the code generator can handle 32 to 64-bit muls,
  3599. we're done here }
  3600. expectloc:=LOC_REGISTER;
  3601. end
  3602. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  3603. { is there a 64 bit type ? }
  3604. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  3605. begin
  3606. result := first_add64bitint;
  3607. if assigned(result) then
  3608. exit;
  3609. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3610. expectloc:=LOC_REGISTER
  3611. else
  3612. expectloc:=LOC_JUMP;
  3613. end
  3614. {$else if defined(llvm) and cpu32bitalu}
  3615. { llvm does not support 128 bit math on 32 bit targets, which is
  3616. necessary for overflow checking 64 bit operations }
  3617. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) and
  3618. (cs_check_overflow in current_settings.localswitches) and
  3619. (nodetype in [addn,subn,muln]) then
  3620. begin
  3621. result := first_add64bitint;
  3622. if assigned(result) then
  3623. exit;
  3624. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3625. expectloc:=LOC_REGISTER
  3626. else
  3627. expectloc:=LOC_JUMP;
  3628. end
  3629. {$endif not(cpu64bitalu) and not(cpuhighleveltarget)}
  3630. { generic 32bit conversion }
  3631. else
  3632. begin
  3633. {$ifdef cpuneedsmulhelper}
  3634. if use_mul_helper then
  3635. begin
  3636. result := nil;
  3637. case torddef(resultdef).ordtype of
  3638. s16bit:
  3639. procname := 'fpc_mul_integer';
  3640. u16bit:
  3641. procname := 'fpc_mul_word';
  3642. s32bit:
  3643. procname := 'fpc_mul_longint';
  3644. u32bit:
  3645. procname := 'fpc_mul_dword';
  3646. else
  3647. internalerror(2011022301);
  3648. end;
  3649. if cs_check_overflow in current_settings.localswitches then
  3650. procname:=procname+'_checkoverflow';
  3651. result := ccallnode.createintern(procname,
  3652. ccallparanode.create(right,
  3653. ccallparanode.create(left,nil)));
  3654. left := nil;
  3655. right := nil;
  3656. firstpass(result);
  3657. exit;
  3658. end;
  3659. {$endif cpuneedsmulhelper}
  3660. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3661. expectloc:=LOC_REGISTER
  3662. {$if not defined(cpuhighleveltarget)}
  3663. else if torddef(ld).size>sizeof(aint) then
  3664. expectloc:=LOC_JUMP
  3665. {$endif}
  3666. else
  3667. expectloc:=LOC_FLAGS;
  3668. end;
  3669. end
  3670. { left side a setdef, must be before string processing,
  3671. else array constructor can be seen as array of char (PFV) }
  3672. else if (ld.typ=setdef) then
  3673. begin
  3674. { small sets are handled inline by the compiler.
  3675. small set doesn't have support for adding ranges }
  3676. if is_smallset(ld) and
  3677. not(
  3678. (right.nodetype=setelementn) and
  3679. assigned(tsetelementnode(right).right)
  3680. ) then
  3681. begin
  3682. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  3683. expectloc:=LOC_FLAGS
  3684. else
  3685. expectloc:=LOC_REGISTER;
  3686. end
  3687. else
  3688. begin
  3689. result := first_addset;
  3690. if assigned(result) then
  3691. exit;
  3692. expectloc:=LOC_CREFERENCE;
  3693. end;
  3694. end
  3695. { compare pchar by addresses like BP/Delphi }
  3696. else if is_pchar(ld) then
  3697. begin
  3698. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3699. result:=first_addpointer
  3700. else
  3701. result:=first_cmppointer;
  3702. end
  3703. { is one of the operands a string }
  3704. else if (ld.typ=stringdef) then
  3705. begin
  3706. if is_widestring(ld) then
  3707. begin
  3708. { this is only for add, the comparisaion is handled later }
  3709. expectloc:=LOC_REGISTER;
  3710. end
  3711. else if is_unicodestring(ld) then
  3712. begin
  3713. { this is only for add, the comparisaion is handled later }
  3714. expectloc:=LOC_REGISTER;
  3715. end
  3716. else if is_ansistring(ld) then
  3717. begin
  3718. { this is only for add, the comparisaion is handled later }
  3719. expectloc:=LOC_REGISTER;
  3720. end
  3721. else if is_longstring(ld) then
  3722. begin
  3723. { this is only for add, the comparisaion is handled later }
  3724. expectloc:=LOC_REFERENCE;
  3725. end
  3726. else
  3727. begin
  3728. {$ifdef addstringopt}
  3729. { can create a call which isn't handled by callparatemp }
  3730. if canbeaddsstringcharoptnode(self) then
  3731. begin
  3732. hp := genaddsstringcharoptnode(self);
  3733. pass_1 := hp;
  3734. exit;
  3735. end
  3736. else
  3737. {$endif addstringopt}
  3738. begin
  3739. { Fix right to be shortstring }
  3740. if is_char(right.resultdef) then
  3741. begin
  3742. inserttypeconv(right,cshortstringtype);
  3743. firstpass(right);
  3744. end;
  3745. end;
  3746. {$ifdef addstringopt}
  3747. { can create a call which isn't handled by callparatemp }
  3748. if canbeaddsstringcsstringoptnode(self) then
  3749. begin
  3750. hp := genaddsstringcsstringoptnode(self);
  3751. pass_1 := hp;
  3752. exit;
  3753. end;
  3754. {$endif addstringopt}
  3755. end;
  3756. { otherwise, let addstring convert everything }
  3757. result := first_addstring;
  3758. exit;
  3759. end
  3760. { is one a real float ? }
  3761. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  3762. begin
  3763. {$ifdef cpufpemu}
  3764. result:=first_addfloat;
  3765. if assigned(result) then
  3766. exit;
  3767. {$endif cpufpemu}
  3768. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3769. expectloc:=LOC_FPUREGISTER
  3770. else
  3771. expectloc:=LOC_FLAGS;
  3772. result:=try_fma(ld,rd);
  3773. if assigned(result) then
  3774. exit;
  3775. end
  3776. { pointer comperation and subtraction }
  3777. else if (ld.typ=pointerdef) then
  3778. begin
  3779. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3780. result:=first_addpointer
  3781. else
  3782. result:=first_cmppointer;
  3783. end
  3784. else if is_implicit_pointer_object_type(ld) then
  3785. begin
  3786. expectloc:=LOC_FLAGS;
  3787. end
  3788. else if (ld.typ=classrefdef) then
  3789. begin
  3790. expectloc:=LOC_FLAGS;
  3791. end
  3792. { support procvar=nil,procvar<>nil }
  3793. else if ((ld.typ=procvardef) and (rt=niln)) or
  3794. ((rd.typ=procvardef) and (lt=niln)) then
  3795. begin
  3796. expectloc:=LOC_FLAGS;
  3797. end
  3798. {$ifdef SUPPORT_MMX}
  3799. { mmx support, this must be before the zero based array
  3800. check }
  3801. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  3802. is_mmx_able_array(rd) then
  3803. begin
  3804. expectloc:=LOC_MMXREGISTER;
  3805. end
  3806. {$endif SUPPORT_MMX}
  3807. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  3808. begin
  3809. result:=first_addpointer;
  3810. end
  3811. else if (rd.typ=procvardef) and
  3812. (ld.typ=procvardef) and
  3813. equal_defs(rd,ld) then
  3814. begin
  3815. expectloc:=LOC_FLAGS;
  3816. end
  3817. else if (ld.typ=enumdef) then
  3818. begin
  3819. expectloc:=LOC_FLAGS;
  3820. end
  3821. {$ifdef SUPPORT_MMX}
  3822. else if (cs_mmx in current_settings.localswitches) and
  3823. is_mmx_able_array(ld) and
  3824. is_mmx_able_array(rd) then
  3825. begin
  3826. expectloc:=LOC_MMXREGISTER;
  3827. end
  3828. {$endif SUPPORT_MMX}
  3829. else if is_dynamic_array(ld) or is_dynamic_array(rd) then
  3830. begin
  3831. result:=first_adddynarray;
  3832. exit;
  3833. end
  3834. { the general solution is to convert to 32 bit int }
  3835. else
  3836. begin
  3837. expectloc:=LOC_REGISTER;
  3838. end;
  3839. end;
  3840. {$ifdef state_tracking}
  3841. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  3842. var factval:Tnode;
  3843. begin
  3844. track_state_pass:=false;
  3845. if left.track_state_pass(exec_known) then
  3846. begin
  3847. track_state_pass:=true;
  3848. left.resultdef:=nil;
  3849. do_typecheckpass(left);
  3850. end;
  3851. factval:=aktstate.find_fact(left);
  3852. if factval<>nil then
  3853. begin
  3854. track_state_pass:=true;
  3855. left.destroy;
  3856. left:=factval.getcopy;
  3857. end;
  3858. if right.track_state_pass(exec_known) then
  3859. begin
  3860. track_state_pass:=true;
  3861. right.resultdef:=nil;
  3862. do_typecheckpass(right);
  3863. end;
  3864. factval:=aktstate.find_fact(right);
  3865. if factval<>nil then
  3866. begin
  3867. track_state_pass:=true;
  3868. right.destroy;
  3869. right:=factval.getcopy;
  3870. end;
  3871. end;
  3872. {$endif}
  3873. end.