nadd.pas 157 KB

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