nadd.pas 158 KB

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