nadd.pas 162 KB

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