nadd.pas 180 KB

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