nadd.pas 174 KB

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