nadd.pas 161 KB

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