nadd.pas 165 KB

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