nadd.pas 170 KB

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