nadd.pas 145 KB

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