nadd.pas 157 KB

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