nadd.pas 143 KB

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