nadd.pas 181 KB

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