nadd.pas 160 KB

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