nadd.pas 184 KB

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