nadd.pas 145 KB

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