nadd.pas 173 KB

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