nadd.pas 140 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493
  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 then
  997. begin
  998. result:=cinlinenode.create(in_ror_x_y,false,
  999. ccallparanode.create(tshlshrnode(left).right,
  1000. ccallparanode.create(tshlshrnode(left).left,nil)));
  1001. tshlshrnode(left).left:=nil;
  1002. tshlshrnode(left).right:=nil;
  1003. exit;
  1004. end
  1005. else if tordconstnode(tshlshrnode(right).right).value=
  1006. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(left).right).value then
  1007. begin
  1008. result:=cinlinenode.create(in_rol_x_y,false,
  1009. ccallparanode.create(tshlshrnode(right).right,
  1010. ccallparanode.create(tshlshrnode(left).left,nil)));
  1011. tshlshrnode(left).left:=nil;
  1012. tshlshrnode(right).right:=nil;
  1013. exit;
  1014. end;
  1015. end;
  1016. if (left.nodetype=shln) and (right.nodetype=shrn) and
  1017. is_constintnode(tshlshrnode(left).right) and
  1018. is_constintnode(tshlshrnode(right).right) and
  1019. (tordconstnode(tshlshrnode(right).right).value>0) and
  1020. (tordconstnode(tshlshrnode(left).right).value>0) and
  1021. tshlshrnode(left).left.isequal(tshlshrnode(right).left) and
  1022. not(might_have_sideeffects(tshlshrnode(left).left)) then
  1023. begin
  1024. if tordconstnode(tshlshrnode(left).right).value=
  1025. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(right).right).value then
  1026. begin
  1027. result:=cinlinenode.create(in_rol_x_y,false,
  1028. ccallparanode.create(tshlshrnode(left).right,
  1029. ccallparanode.create(tshlshrnode(left).left,nil)));
  1030. tshlshrnode(left).left:=nil;
  1031. tshlshrnode(left).right:=nil;
  1032. exit;
  1033. end
  1034. else if tordconstnode(tshlshrnode(right).right).value=
  1035. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(left).right).value then
  1036. begin
  1037. result:=cinlinenode.create(in_ror_x_y,false,
  1038. ccallparanode.create(tshlshrnode(right).right,
  1039. ccallparanode.create(tshlshrnode(left).left,nil)));
  1040. tshlshrnode(left).left:=nil;
  1041. tshlshrnode(right).right:=nil;
  1042. exit;
  1043. end;
  1044. end;
  1045. end;
  1046. {$endif cpurox}
  1047. end;
  1048. end;
  1049. function taddnode.dogetcopy: tnode;
  1050. var
  1051. n: taddnode;
  1052. begin
  1053. n:=taddnode(inherited dogetcopy);
  1054. n.resultrealdef:=resultrealdef;
  1055. result:=n;
  1056. end;
  1057. function taddnode.docompare(p: tnode): boolean;
  1058. begin
  1059. result:=
  1060. inherited docompare(p) and
  1061. equal_defs(taddnode(p).resultrealdef,resultrealdef);
  1062. end;
  1063. function taddnode.pass_typecheck:tnode;
  1064. begin
  1065. { This function is small to keep the stack small for recursive of
  1066. large + operations }
  1067. typecheckpass(left);
  1068. typecheckpass(right);
  1069. result:=pass_typecheck_internal;
  1070. end;
  1071. function taddnode.pass_typecheck_internal:tnode;
  1072. var
  1073. hp : tnode;
  1074. rd,ld,nd : tdef;
  1075. hsym : tfieldvarsym;
  1076. llow,lhigh,
  1077. rlow,rhigh : tconstexprint;
  1078. strtype : tstringtype;
  1079. res,
  1080. b : boolean;
  1081. lt,rt : tnodetype;
  1082. ot : tnodetype;
  1083. {$ifdef state_tracking}
  1084. factval : Tnode;
  1085. change : boolean;
  1086. {$endif}
  1087. function maybe_cast_ordconst(var n: tnode; adef: tdef): boolean;
  1088. begin
  1089. result:=(tordconstnode(n).value>=torddef(adef).low) and
  1090. (tordconstnode(n).value<=torddef(adef).high);
  1091. if result then
  1092. inserttypeconv(n,adef);
  1093. end;
  1094. begin
  1095. result:=nil;
  1096. rlow:=0;
  1097. llow:=0;
  1098. rhigh:=0;
  1099. lhigh:=0;
  1100. { avoid any problems with type parameters later on }
  1101. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  1102. begin
  1103. resultdef:=cundefinedtype;
  1104. exit;
  1105. end;
  1106. { both left and right need to be valid }
  1107. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1108. set_varstate(right,vs_read,[vsf_must_be_valid]);
  1109. if codegenerror then
  1110. exit;
  1111. { tp procvar support. Omit for converted assigned() nodes }
  1112. if not (nf_load_procvar in flags) then
  1113. begin
  1114. maybe_call_procvar(left,true);
  1115. maybe_call_procvar(right,true);
  1116. end
  1117. else
  1118. if not (nodetype in [equaln,unequaln]) then
  1119. InternalError(2013091601);
  1120. { convert array constructors to sets, because there is no other operator
  1121. possible for array constructors }
  1122. if is_array_constructor(left.resultdef) then
  1123. begin
  1124. arrayconstructor_to_set(left);
  1125. typecheckpass(left);
  1126. end;
  1127. if is_array_constructor(right.resultdef) then
  1128. begin
  1129. arrayconstructor_to_set(right);
  1130. typecheckpass(right);
  1131. end;
  1132. { allow operator overloading }
  1133. hp:=self;
  1134. if isbinaryoverloaded(hp) then
  1135. begin
  1136. result:=hp;
  1137. exit;
  1138. end;
  1139. { Stop checking when an error was found in the operator checking }
  1140. if codegenerror then
  1141. begin
  1142. result:=cerrornode.create;
  1143. exit;
  1144. end;
  1145. { Kylix allows enum+ordconstn in an enum type declaration, we need to do
  1146. the conversion here before the constant folding }
  1147. if (m_delphi in current_settings.modeswitches) and
  1148. (blocktype in [bt_type,bt_const_type,bt_var_type]) then
  1149. begin
  1150. if (left.resultdef.typ=enumdef) and
  1151. (right.resultdef.typ=orddef) then
  1152. begin
  1153. { insert explicit typecast to default signed int }
  1154. left:=ctypeconvnode.create_internal(left,sinttype);
  1155. typecheckpass(left);
  1156. end
  1157. else
  1158. if (left.resultdef.typ=orddef) and
  1159. (right.resultdef.typ=enumdef) then
  1160. begin
  1161. { insert explicit typecast to default signed int }
  1162. right:=ctypeconvnode.create_internal(right,sinttype);
  1163. typecheckpass(right);
  1164. end;
  1165. end;
  1166. { is one a real float, then both need to be floats, this
  1167. need to be done before the constant folding so constant
  1168. operation on a float and int are also handled }
  1169. {$ifdef x86}
  1170. { use extended as default real type only when the x87 fpu is used }
  1171. {$if defined(i386) or defined(i8086)}
  1172. if not(current_settings.fputype=fpu_x87) then
  1173. resultrealdef:=s64floattype
  1174. else
  1175. resultrealdef:=pbestrealtype^;
  1176. {$endif i386 or i8086}
  1177. {$ifdef x86_64}
  1178. { x86-64 has no x87 only mode, so use always double as default }
  1179. resultrealdef:=s64floattype;
  1180. {$endif x86_6}
  1181. {$else not x86}
  1182. resultrealdef:=pbestrealtype^;
  1183. {$endif not x86}
  1184. if (right.resultdef.typ=floatdef) or (left.resultdef.typ=floatdef) then
  1185. begin
  1186. { when both floattypes are already equal then use that
  1187. floattype for results }
  1188. if (right.resultdef.typ=floatdef) and
  1189. (left.resultdef.typ=floatdef) and
  1190. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype) then
  1191. resultrealdef:=left.resultdef
  1192. { when there is a currency type then use currency, but
  1193. only when currency is defined as float }
  1194. else
  1195. if (is_currency(right.resultdef) or
  1196. is_currency(left.resultdef)) and
  1197. ((s64currencytype.typ = floatdef) or
  1198. (nodetype <> slashn)) then
  1199. begin
  1200. resultrealdef:=s64currencytype;
  1201. inserttypeconv(right,resultrealdef);
  1202. inserttypeconv(left,resultrealdef);
  1203. end
  1204. else
  1205. begin
  1206. resultrealdef:=getbestreal(left.resultdef,right.resultdef);
  1207. inserttypeconv(right,resultrealdef);
  1208. inserttypeconv(left,resultrealdef);
  1209. end;
  1210. end;
  1211. { If both operands are constant and there is a unicodestring
  1212. or unicodestring then convert everything to unicodestring }
  1213. if is_constnode(right) and is_constnode(left) and
  1214. (is_unicodestring(right.resultdef) or
  1215. is_unicodestring(left.resultdef)) then
  1216. begin
  1217. inserttypeconv(right,cunicodestringtype);
  1218. inserttypeconv(left,cunicodestringtype);
  1219. end;
  1220. { If both operands are constant and there is a widechar
  1221. or widestring then convert everything to widestring. This
  1222. allows constant folding like char+widechar }
  1223. if is_constnode(right) and is_constnode(left) and
  1224. (is_widestring(right.resultdef) or
  1225. is_widestring(left.resultdef) or
  1226. is_widechar(right.resultdef) or
  1227. is_widechar(left.resultdef)) then
  1228. begin
  1229. inserttypeconv(right,cwidestringtype);
  1230. inserttypeconv(left,cwidestringtype);
  1231. end;
  1232. { load easier access variables }
  1233. rd:=right.resultdef;
  1234. ld:=left.resultdef;
  1235. rt:=right.nodetype;
  1236. lt:=left.nodetype;
  1237. { 4 character constant strings are compatible with orddef }
  1238. { in macpas mode (become cardinals) }
  1239. if (m_mac in current_settings.modeswitches) and
  1240. { only allow for comparisons, additions etc are }
  1241. { normally program errors }
  1242. (nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) and
  1243. (((lt=stringconstn) and
  1244. (tstringconstnode(left).len=4) and
  1245. (rd.typ=orddef)) or
  1246. ((rt=stringconstn) and
  1247. (tstringconstnode(right).len=4) and
  1248. (ld.typ=orddef))) then
  1249. begin
  1250. if (rt=stringconstn) then
  1251. begin
  1252. inserttypeconv(right,u32inttype);
  1253. rt:=right.nodetype;
  1254. rd:=right.resultdef;
  1255. end
  1256. else
  1257. begin
  1258. inserttypeconv(left,u32inttype);
  1259. lt:=left.nodetype;
  1260. ld:=left.resultdef;
  1261. end;
  1262. end;
  1263. { but an int/int gives real/real! }
  1264. if (nodetype=slashn) and not(is_vector(left.resultdef)) and not(is_vector(right.resultdef)) then
  1265. begin
  1266. if is_currency(left.resultdef) and
  1267. is_currency(right.resultdef) then
  1268. { In case of currency, converting to float means dividing by 10000 }
  1269. { However, since this is already a division, both divisions by }
  1270. { 10000 are eliminated when we divide the results -> we can skip }
  1271. { them. }
  1272. if s64currencytype.typ = floatdef then
  1273. begin
  1274. { there's no s64comptype or so, how do we avoid the type conversion?
  1275. left.resultdef := s64comptype;
  1276. right.resultdef := s64comptype; }
  1277. end
  1278. else
  1279. begin
  1280. left.resultdef := s64inttype;
  1281. right.resultdef := s64inttype;
  1282. end;
  1283. inserttypeconv(right,resultrealdef);
  1284. inserttypeconv(left,resultrealdef);
  1285. end
  1286. { if both are orddefs then check sub types }
  1287. else if (ld.typ=orddef) and (rd.typ=orddef) then
  1288. begin
  1289. { set for & and | operations in macpas mode: they only work on }
  1290. { booleans, and always short circuit evaluation }
  1291. if (nf_short_bool in flags) then
  1292. begin
  1293. if not is_boolean(ld) then
  1294. begin
  1295. inserttypeconv(left,pasbool8type);
  1296. ld := left.resultdef;
  1297. end;
  1298. if not is_boolean(rd) then
  1299. begin
  1300. inserttypeconv(right,pasbool8type);
  1301. rd := right.resultdef;
  1302. end;
  1303. end;
  1304. { 2 booleans? }
  1305. if (is_boolean(ld) and is_boolean(rd)) then
  1306. begin
  1307. case nodetype of
  1308. xorn,
  1309. andn,
  1310. orn:
  1311. begin
  1312. { Make sides equal to the largest boolean }
  1313. if (torddef(left.resultdef).size>torddef(right.resultdef).size) or
  1314. (is_cbool(left.resultdef) and not is_cbool(right.resultdef)) then
  1315. begin
  1316. right:=ctypeconvnode.create_internal(right,left.resultdef);
  1317. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  1318. typecheckpass(right);
  1319. end
  1320. else if (torddef(left.resultdef).size<torddef(right.resultdef).size) or
  1321. (not is_cbool(left.resultdef) and is_cbool(right.resultdef)) then
  1322. begin
  1323. left:=ctypeconvnode.create_internal(left,right.resultdef);
  1324. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  1325. typecheckpass(left);
  1326. end;
  1327. end;
  1328. ltn,
  1329. lten,
  1330. gtn,
  1331. gten:
  1332. begin
  1333. { convert both to pasbool to perform the comparison (so
  1334. that longbool(4) = longbool(2), since both represent
  1335. "true" }
  1336. inserttypeconv(left,pasbool8type);
  1337. inserttypeconv(right,pasbool8type);
  1338. end;
  1339. unequaln,
  1340. equaln:
  1341. begin
  1342. if not(cs_full_boolean_eval in current_settings.localswitches) or
  1343. (nf_short_bool in flags) then
  1344. begin
  1345. { Remove any compares with constants }
  1346. if (left.nodetype=ordconstn) then
  1347. begin
  1348. hp:=right;
  1349. b:=(tordconstnode(left).value<>0);
  1350. ot:=nodetype;
  1351. left.free;
  1352. left:=nil;
  1353. right:=nil;
  1354. if (not(b) and (ot=equaln)) or
  1355. (b and (ot=unequaln)) then
  1356. begin
  1357. hp:=cnotnode.create(hp);
  1358. end;
  1359. result:=hp;
  1360. exit;
  1361. end;
  1362. if (right.nodetype=ordconstn) then
  1363. begin
  1364. hp:=left;
  1365. b:=(tordconstnode(right).value<>0);
  1366. ot:=nodetype;
  1367. right.free;
  1368. right:=nil;
  1369. left:=nil;
  1370. if (not(b) and (ot=equaln)) or
  1371. (b and (ot=unequaln)) then
  1372. begin
  1373. hp:=cnotnode.create(hp);
  1374. end;
  1375. result:=hp;
  1376. exit;
  1377. end;
  1378. end;
  1379. { Delphi-compatibility: convert both to pasbool to
  1380. perform the equality comparison }
  1381. inserttypeconv(left,pasbool8type);
  1382. inserttypeconv(right,pasbool8type);
  1383. end;
  1384. else
  1385. begin
  1386. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1387. result:=cnothingnode.create;
  1388. exit;
  1389. end;
  1390. end;
  1391. end
  1392. { Both are chars? }
  1393. else if is_char(rd) and is_char(ld) then
  1394. begin
  1395. if nodetype=addn then
  1396. begin
  1397. resultdef:=cshortstringtype;
  1398. if not(is_constcharnode(left) and is_constcharnode(right)) then
  1399. begin
  1400. inserttypeconv(left,cshortstringtype);
  1401. {$ifdef addstringopt}
  1402. hp := genaddsstringcharoptnode(self);
  1403. result := hp;
  1404. exit;
  1405. {$endif addstringopt}
  1406. end
  1407. end
  1408. else if not(nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) then
  1409. begin
  1410. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1411. result:=cnothingnode.create;
  1412. exit;
  1413. end;
  1414. end
  1415. { There is a widechar? }
  1416. else if is_widechar(rd) or is_widechar(ld) then
  1417. begin
  1418. { widechar+widechar gives unicodestring }
  1419. if nodetype=addn then
  1420. begin
  1421. inserttypeconv(left,cunicodestringtype);
  1422. if (torddef(rd).ordtype<>uwidechar) then
  1423. inserttypeconv(right,cwidechartype);
  1424. resultdef:=cunicodestringtype;
  1425. end
  1426. else
  1427. begin
  1428. if (torddef(ld).ordtype<>uwidechar) then
  1429. inserttypeconv(left,cwidechartype);
  1430. if (torddef(rd).ordtype<>uwidechar) then
  1431. inserttypeconv(right,cwidechartype);
  1432. end;
  1433. end
  1434. { is there a currency type ? }
  1435. else if ((torddef(rd).ordtype=scurrency) or (torddef(ld).ordtype=scurrency)) then
  1436. begin
  1437. if (torddef(ld).ordtype<>scurrency) then
  1438. inserttypeconv(left,s64currencytype);
  1439. if (torddef(rd).ordtype<>scurrency) then
  1440. inserttypeconv(right,s64currencytype);
  1441. end
  1442. { leave some constant integer expressions alone in case the
  1443. resultdef of the integer types doesn't influence the outcome,
  1444. because the forced type conversions below can otherwise result
  1445. in unexpected results (such as high(qword)<high(int64) returning
  1446. true because high(qword) gets converted to int64) }
  1447. else if is_integer(ld) and is_integer(rd) and
  1448. (lt=ordconstn) and (rt=ordconstn) and
  1449. (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) then
  1450. begin
  1451. end
  1452. { "and" does't care about the sign of integers }
  1453. { "xor", "or" and compares don't need extension to native int }
  1454. { size either as long as both values are signed or unsigned }
  1455. { "xor" and "or" also don't care about the sign if the values }
  1456. { occupy an entire register }
  1457. { don't do it if either type is 64 bit (except for "and"), }
  1458. { since in that case we can't safely find a "common" type }
  1459. else if is_integer(ld) and is_integer(rd) and
  1460. ((nodetype=andn) or
  1461. ((nodetype in [orn,xorn,equaln,unequaln,gtn,gten,ltn,lten]) and
  1462. not is_64bitint(ld) and not is_64bitint(rd) and
  1463. (is_signed(ld)=is_signed(rd)))) then
  1464. begin
  1465. { Delphi-compatible: prefer unsigned type for "and", when the
  1466. unsigned type is bigger than the signed one, and also bigger
  1467. than min(native_int, 32-bit) }
  1468. if (is_oversizedint(rd) or is_nativeint(rd) or is_32bitint(rd)) and
  1469. (rd.size>=ld.size) and
  1470. not is_signed(rd) and is_signed(ld) then
  1471. inserttypeconv_internal(left,rd)
  1472. else if (is_oversizedint(ld) or is_nativeint(ld) or is_32bitint(ld)) and
  1473. (ld.size>=rd.size) and
  1474. not is_signed(ld) and is_signed(rd) then
  1475. inserttypeconv_internal(right,ld)
  1476. else
  1477. begin
  1478. { not to left right.resultdef, because that may
  1479. cause a range error if left and right's def don't
  1480. completely overlap }
  1481. nd:=get_common_intdef(torddef(ld),torddef(rd),true);
  1482. inserttypeconv(left,nd);
  1483. inserttypeconv(right,nd);
  1484. end;
  1485. end
  1486. { don't extend (sign-mismatched) comparisons if either side is a constant
  1487. whose value is within range of opposite side }
  1488. else if is_integer(ld) and is_integer(rd) and
  1489. (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1490. (is_signed(ld)<>is_signed(rd)) and
  1491. (
  1492. ((lt=ordconstn) and maybe_cast_ordconst(left,rd)) or
  1493. ((rt=ordconstn) and maybe_cast_ordconst(right,ld))
  1494. ) then
  1495. begin
  1496. { done here }
  1497. end
  1498. { is there a signed 64 bit type ? }
  1499. else if ((torddef(rd).ordtype=s64bit) or (torddef(ld).ordtype=s64bit)) then
  1500. begin
  1501. if (torddef(ld).ordtype<>s64bit) then
  1502. inserttypeconv(left,s64inttype);
  1503. if (torddef(rd).ordtype<>s64bit) then
  1504. inserttypeconv(right,s64inttype);
  1505. end
  1506. { is there a unsigned 64 bit type ? }
  1507. else if ((torddef(rd).ordtype=u64bit) or (torddef(ld).ordtype=u64bit)) then
  1508. begin
  1509. if (torddef(ld).ordtype<>u64bit) then
  1510. inserttypeconv(left,u64inttype);
  1511. if (torddef(rd).ordtype<>u64bit) then
  1512. inserttypeconv(right,u64inttype);
  1513. end
  1514. { is there a larger int? }
  1515. else if is_oversizedint(rd) or is_oversizedint(ld) then
  1516. begin
  1517. nd:=get_common_intdef(torddef(ld),torddef(rd),false);
  1518. inserttypeconv(right,nd);
  1519. inserttypeconv(left,nd);
  1520. end
  1521. { is there a native unsigned int? }
  1522. else if is_nativeuint(rd) or is_nativeuint(ld) then
  1523. begin
  1524. { convert positive constants to uinttype }
  1525. if (not is_nativeuint(ld)) and
  1526. is_constintnode(left) and
  1527. (tordconstnode(left).value >= 0) then
  1528. inserttypeconv(left,uinttype);
  1529. if (not is_nativeuint(rd)) and
  1530. is_constintnode(right) and
  1531. (tordconstnode(right).value >= 0) then
  1532. inserttypeconv(right,uinttype);
  1533. { when one of the operand is signed or the operation is subn then perform
  1534. the operation in a larger signed type, can't use rd/ld here because there
  1535. could be already typeconvs inserted.
  1536. This is compatible with the code below for other unsigned types (PFV) }
  1537. if is_signed(left.resultdef) or
  1538. is_signed(right.resultdef) or
  1539. (nodetype=subn) then
  1540. begin
  1541. if nodetype<>subn then
  1542. CGMessage(type_h_mixed_signed_unsigned);
  1543. { mark as internal in case added for a subn, so }
  1544. { ttypeconvnode.simplify can remove the larger }
  1545. { typecast again if semantically correct. Even }
  1546. { if we could detect that here already, we }
  1547. { mustn't do it here because that would change }
  1548. { overload choosing behaviour etc. The code in }
  1549. { ncnv.pas is run after that is already decided }
  1550. if (not is_signed(left.resultdef) and
  1551. not is_signed(right.resultdef)) or
  1552. (nodetype in [orn,xorn]) then
  1553. include(flags,nf_internal);
  1554. { get next larger signed int type }
  1555. nd:=get_common_intdef(torddef(sinttype),torddef(uinttype),false);
  1556. inserttypeconv(left,nd);
  1557. inserttypeconv(right,nd);
  1558. end
  1559. else
  1560. begin
  1561. if not is_nativeuint(left.resultdef) then
  1562. inserttypeconv(left,uinttype);
  1563. if not is_nativeuint(right.resultdef) then
  1564. inserttypeconv(right,uinttype);
  1565. end;
  1566. end
  1567. { generic ord conversion is sinttype }
  1568. else
  1569. begin
  1570. { When there is a signed type or there is a minus operation
  1571. we convert to signed int. Otherwise (both are unsigned) we keep
  1572. the result also unsigned. This is compatible with Delphi (PFV) }
  1573. if is_signed(ld) or
  1574. is_signed(rd) or
  1575. (nodetype=subn) then
  1576. begin
  1577. inserttypeconv(right,sinttype);
  1578. inserttypeconv(left,sinttype);
  1579. end
  1580. else
  1581. begin
  1582. inserttypeconv(right,uinttype);
  1583. inserttypeconv(left,uinttype);
  1584. end;
  1585. end;
  1586. end
  1587. { if both are floatdefs, conversion is already done before constant folding }
  1588. else if (ld.typ=floatdef) then
  1589. begin
  1590. if not(nodetype in [addn,subn,muln,slashn,equaln,unequaln,ltn,lten,gtn,gten]) then
  1591. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1592. end
  1593. { left side a setdef, must be before string processing,
  1594. else array constructor can be seen as array of char (PFV) }
  1595. else if (ld.typ=setdef) then
  1596. begin
  1597. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  1598. CGMessage(type_e_set_operation_unknown);
  1599. { right must either be a set or a set element }
  1600. if (rd.typ<>setdef) and
  1601. (rt<>setelementn) then
  1602. CGMessage(type_e_mismatch)
  1603. { Make operands the same setdef. If one's elementtype fits }
  1604. { entirely inside the other's, pick the one with the largest }
  1605. { range. Otherwise create a new setdef with a range which }
  1606. { can contain both. }
  1607. else if not(equal_defs(ld,rd)) then
  1608. begin
  1609. { note: ld cannot be an empty set with elementdef=nil in }
  1610. { case right is not a set, arrayconstructor_to_set takes }
  1611. { care of that }
  1612. { 1: rd is a set with an assigned elementdef, and ld is }
  1613. { either an empty set without elementdef or a set whose }
  1614. { elementdef fits in rd's elementdef -> convert to rd }
  1615. if ((rd.typ=setdef) and
  1616. assigned(tsetdef(rd).elementdef) and
  1617. (not assigned(tsetdef(ld).elementdef) or
  1618. is_in_limit(ld,rd))) then
  1619. inserttypeconv(left,rd)
  1620. { 2: rd is either an empty set without elementdef or a set }
  1621. { whose elementdef fits in ld's elementdef, or a set }
  1622. { element whose def fits in ld's elementdef -> convert }
  1623. { to ld. ld's elementdef can't be nil here, is caught }
  1624. { previous case and "note:" above }
  1625. else if ((rd.typ=setdef) and
  1626. (not assigned(tsetdef(rd).elementdef) or
  1627. is_in_limit(rd,ld))) or
  1628. ((rd.typ<>setdef) and
  1629. is_in_limit(rd,tsetdef(ld).elementdef)) then
  1630. if (rd.typ=setdef) then
  1631. inserttypeconv(right,ld)
  1632. else
  1633. inserttypeconv(right,tsetdef(ld).elementdef)
  1634. { 3: otherwise create setdef which encompasses both, taking }
  1635. { into account empty sets without elementdef }
  1636. else
  1637. begin
  1638. if assigned(tsetdef(ld).elementdef) then
  1639. begin
  1640. llow:=tsetdef(ld).setbase;
  1641. lhigh:=tsetdef(ld).setmax;
  1642. end;
  1643. if (rd.typ=setdef) then
  1644. if assigned(tsetdef(rd).elementdef) then
  1645. begin
  1646. rlow:=tsetdef(rd).setbase;
  1647. rhigh:=tsetdef(rd).setmax;
  1648. end
  1649. else
  1650. begin
  1651. { ld's elementdef must have been valid }
  1652. rlow:=llow;
  1653. rhigh:=lhigh;
  1654. end
  1655. else
  1656. getrange(rd,rlow,rhigh);
  1657. if not assigned(tsetdef(ld).elementdef) then
  1658. begin
  1659. llow:=rlow;
  1660. lhigh:=rhigh;
  1661. end;
  1662. nd:=csetdef.create(tsetdef(ld).elementdef,min(llow,rlow).svalue,max(lhigh,rhigh).svalue,true);
  1663. inserttypeconv(left,nd);
  1664. if (rd.typ=setdef) then
  1665. inserttypeconv(right,nd)
  1666. else
  1667. inserttypeconv(right,tsetdef(nd).elementdef);
  1668. end;
  1669. end;
  1670. end
  1671. { pointer comparision and subtraction }
  1672. else if (
  1673. (rd.typ=pointerdef) and (ld.typ=pointerdef)
  1674. ) or
  1675. { compare/add pchar to variable (not stringconst) char arrays
  1676. by addresses like BP/Delphi }
  1677. (
  1678. (nodetype in [equaln,unequaln,subn,addn]) and
  1679. (
  1680. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd) and (rt<>stringconstn)) or
  1681. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld) and (lt<>stringconstn))
  1682. )
  1683. ) then
  1684. begin
  1685. { convert char array to pointer }
  1686. if is_chararray(rd) then
  1687. begin
  1688. inserttypeconv(right,charpointertype);
  1689. rd:=right.resultdef;
  1690. end
  1691. else if is_chararray(ld) then
  1692. begin
  1693. inserttypeconv(left,charpointertype);
  1694. ld:=left.resultdef;
  1695. end;
  1696. case nodetype of
  1697. equaln,unequaln :
  1698. begin
  1699. if is_voidpointer(right.resultdef) then
  1700. inserttypeconv(right,left.resultdef)
  1701. else if is_voidpointer(left.resultdef) then
  1702. inserttypeconv(left,right.resultdef)
  1703. else if not(equal_defs(ld,rd)) then
  1704. IncompatibleTypes(ld,rd);
  1705. { now that the type checking is done, convert both to charpointer, }
  1706. { because methodpointers are 8 bytes even though only the first 4 }
  1707. { bytes must be compared. This can happen here if we are in }
  1708. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  1709. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  1710. { optimized away, since the result already was a voidpointer, so }
  1711. { use a charpointer instead (JM) }
  1712. {$if defined(jvm)}
  1713. inserttypeconv_internal(left,java_jlobject);
  1714. inserttypeconv_internal(right,java_jlobject);
  1715. {$elseif defined(i8086)}
  1716. if is_hugepointer(left.resultdef) then
  1717. inserttypeconv_internal(left,charhugepointertype)
  1718. else if is_farpointer(left.resultdef) then
  1719. inserttypeconv_internal(left,charfarpointertype)
  1720. else
  1721. inserttypeconv_internal(left,charnearpointertype);
  1722. if is_hugepointer(right.resultdef) then
  1723. inserttypeconv_internal(right,charhugepointertype)
  1724. else if is_farpointer(right.resultdef) then
  1725. inserttypeconv_internal(right,charfarpointertype)
  1726. else
  1727. inserttypeconv_internal(right,charnearpointertype);
  1728. {$else}
  1729. inserttypeconv_internal(left,charpointertype);
  1730. inserttypeconv_internal(right,charpointertype);
  1731. {$endif jvm}
  1732. end;
  1733. ltn,lten,gtn,gten:
  1734. begin
  1735. if (cs_extsyntax in current_settings.moduleswitches) or
  1736. (nf_internal in flags) then
  1737. begin
  1738. if is_voidpointer(right.resultdef) then
  1739. inserttypeconv(right,left.resultdef)
  1740. else if is_voidpointer(left.resultdef) then
  1741. inserttypeconv(left,right.resultdef)
  1742. else if not(equal_defs(ld,rd)) then
  1743. IncompatibleTypes(ld,rd);
  1744. end
  1745. else
  1746. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1747. end;
  1748. subn:
  1749. begin
  1750. if (cs_extsyntax in current_settings.moduleswitches) then
  1751. begin
  1752. if is_voidpointer(right.resultdef) then
  1753. begin
  1754. if is_big_untyped_addrnode(right) then
  1755. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1756. inserttypeconv(right,left.resultdef)
  1757. end
  1758. else if is_voidpointer(left.resultdef) then
  1759. inserttypeconv(left,right.resultdef)
  1760. else if not(equal_defs(ld,rd)) then
  1761. IncompatibleTypes(ld,rd);
  1762. end
  1763. else
  1764. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1765. if not(nf_has_pointerdiv in flags) and
  1766. (tpointerdef(rd).pointeddef.size>1) then
  1767. begin
  1768. hp:=getcopy;
  1769. include(hp.flags,nf_has_pointerdiv);
  1770. result:=cmoddivnode.create(divn,hp,
  1771. cordconstnode.create(tpointerdef(rd).pointeddef.size,tpointerdef(rd).pointer_subtraction_result_type,false));
  1772. end;
  1773. resultdef:=tpointerdef(rd).pointer_subtraction_result_type;
  1774. exit;
  1775. end;
  1776. else
  1777. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1778. end;
  1779. end
  1780. { is one of the operands a string?,
  1781. chararrays are also handled as strings (after conversion), also take
  1782. care of chararray+chararray and chararray+char.
  1783. Note: Must be done after pointerdef+pointerdef has been checked, else
  1784. pchar is converted to string }
  1785. else if (rd.typ=stringdef) or
  1786. (ld.typ=stringdef) or
  1787. { stringconstn's can be arraydefs }
  1788. (lt=stringconstn) or
  1789. (rt=stringconstn) or
  1790. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1791. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1792. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1793. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1794. begin
  1795. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1796. begin
  1797. { Is there a unicodestring? }
  1798. if is_unicodestring(rd) or is_unicodestring(ld) or
  1799. ((m_default_unicodestring in current_settings.modeswitches) and
  1800. (cs_refcountedstrings in current_settings.localswitches) and
  1801. (
  1802. is_pwidechar(rd) or is_widechararray(rd) or is_open_widechararray(rd) or (lt = stringconstn) or
  1803. is_pwidechar(ld) or is_widechararray(ld) or is_open_widechararray(ld) or (rt = stringconstn)
  1804. )
  1805. ) then
  1806. strtype:=st_unicodestring
  1807. else
  1808. { Is there a widestring? }
  1809. if is_widestring(rd) or is_widestring(ld) or
  1810. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1811. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1812. strtype:=st_widestring
  1813. else
  1814. if is_ansistring(rd) or is_ansistring(ld) or
  1815. ((cs_refcountedstrings in current_settings.localswitches) and
  1816. //todo: Move some of this to longstring's then they are implemented?
  1817. (
  1818. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or (lt = stringconstn) or
  1819. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld) or (rt = stringconstn)
  1820. )
  1821. ) then
  1822. strtype:=st_ansistring
  1823. else
  1824. if is_longstring(rd) or is_longstring(ld) then
  1825. strtype:=st_longstring
  1826. else
  1827. begin
  1828. { TODO: todo: add a warning/hint here if one converting a too large array}
  1829. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1830. Note: Delphi halts with error if "array [0..xx] of char"
  1831. is assigned to ShortString and string length is less
  1832. then array size }
  1833. strtype:= st_shortstring;
  1834. end;
  1835. // Now convert nodes to common string type
  1836. case strtype of
  1837. st_widestring :
  1838. begin
  1839. if not(is_widestring(rd)) then
  1840. inserttypeconv(right,cwidestringtype);
  1841. if not(is_widestring(ld)) then
  1842. inserttypeconv(left,cwidestringtype);
  1843. end;
  1844. st_unicodestring :
  1845. begin
  1846. if not(is_unicodestring(rd)) then
  1847. inserttypeconv(right,cunicodestringtype);
  1848. if not(is_unicodestring(ld)) then
  1849. inserttypeconv(left,cunicodestringtype);
  1850. end;
  1851. st_ansistring :
  1852. begin
  1853. { use same code page if possible (don't force same code
  1854. page in case both are ansistrings with code page <>
  1855. CP_NONE, since then data loss can occur: the ansistring
  1856. helpers will convert them at run time to an encoding
  1857. that can represent both encodings) }
  1858. if is_ansistring(ld) and
  1859. (tstringdef(ld).encoding<>0) and
  1860. (tstringdef(ld).encoding<>globals.CP_NONE) and
  1861. (not is_ansistring(rd) or
  1862. (tstringdef(rd).encoding=0) or
  1863. (tstringdef(rd).encoding=globals.CP_NONE)) then
  1864. inserttypeconv(right,ld)
  1865. else if is_ansistring(rd) and
  1866. (tstringdef(rd).encoding<>0) and
  1867. (tstringdef(rd).encoding<>globals.CP_NONE) and
  1868. (not is_ansistring(ld) or
  1869. (tstringdef(ld).encoding=0) or
  1870. (tstringdef(ld).encoding=globals.CP_NONE)) then
  1871. inserttypeconv(left,rd)
  1872. else
  1873. begin
  1874. if not is_ansistring(ld) then
  1875. inserttypeconv(left,getansistringdef);
  1876. if not is_ansistring(rd) then
  1877. inserttypeconv(right,getansistringdef);
  1878. end;
  1879. end;
  1880. st_longstring :
  1881. begin
  1882. if not(is_longstring(rd)) then
  1883. inserttypeconv(right,clongstringtype);
  1884. if not(is_longstring(ld)) then
  1885. inserttypeconv(left,clongstringtype);
  1886. end;
  1887. st_shortstring :
  1888. begin
  1889. if not(is_shortstring(ld)) then
  1890. inserttypeconv(left,cshortstringtype);
  1891. { don't convert char, that can be handled by the optimized node }
  1892. if not(is_shortstring(rd) or is_char(rd)) then
  1893. inserttypeconv(right,cshortstringtype);
  1894. end;
  1895. else
  1896. internalerror(2005101);
  1897. end;
  1898. end
  1899. else
  1900. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1901. end
  1902. { implicit pointer object type comparison }
  1903. else if is_implicit_pointer_object_type(rd) or is_implicit_pointer_object_type(ld) then
  1904. begin
  1905. if (nodetype in [equaln,unequaln]) then
  1906. begin
  1907. if is_implicit_pointer_object_type(rd) and is_implicit_pointer_object_type(ld) then
  1908. begin
  1909. if def_is_related(tobjectdef(rd),tobjectdef(ld)) then
  1910. inserttypeconv(right,left.resultdef)
  1911. else
  1912. inserttypeconv(left,right.resultdef);
  1913. end
  1914. else if is_implicit_pointer_object_type(rd) then
  1915. inserttypeconv(left,right.resultdef)
  1916. else
  1917. inserttypeconv(right,left.resultdef);
  1918. end
  1919. else
  1920. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1921. end
  1922. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  1923. begin
  1924. if (nodetype in [equaln,unequaln]) then
  1925. begin
  1926. if def_is_related(tobjectdef(tclassrefdef(rd).pointeddef),
  1927. tobjectdef(tclassrefdef(ld).pointeddef)) then
  1928. inserttypeconv(right,left.resultdef)
  1929. else
  1930. inserttypeconv(left,right.resultdef);
  1931. end
  1932. else
  1933. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1934. end
  1935. { allow comparison with nil pointer }
  1936. else if is_implicit_pointer_object_type(rd) or (rd.typ=classrefdef) then
  1937. begin
  1938. if (nodetype in [equaln,unequaln]) then
  1939. inserttypeconv(left,right.resultdef)
  1940. else
  1941. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1942. end
  1943. else if is_implicit_pointer_object_type(ld) or (ld.typ=classrefdef) then
  1944. begin
  1945. if (nodetype in [equaln,unequaln]) then
  1946. inserttypeconv(right,left.resultdef)
  1947. else
  1948. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1949. end
  1950. { support procvar=nil,procvar<>nil }
  1951. else if ((ld.typ=procvardef) and (rt=niln)) or
  1952. ((rd.typ=procvardef) and (lt=niln)) then
  1953. begin
  1954. if not(nodetype in [equaln,unequaln]) then
  1955. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1956. { find proc field in methodpointer record }
  1957. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1958. if not assigned(hsym) then
  1959. internalerror(200412043);
  1960. { For methodpointers compare only tmethodpointer.proc }
  1961. if (rd.typ=procvardef) and
  1962. (not tprocvardef(rd).is_addressonly) then
  1963. begin
  1964. right:=csubscriptnode.create(
  1965. hsym,
  1966. ctypeconvnode.create_internal(right,methodpointertype));
  1967. typecheckpass(right);
  1968. end;
  1969. if (ld.typ=procvardef) and
  1970. (not tprocvardef(ld).is_addressonly) then
  1971. begin
  1972. left:=csubscriptnode.create(
  1973. hsym,
  1974. ctypeconvnode.create_internal(left,methodpointertype));
  1975. typecheckpass(left);
  1976. end;
  1977. if lt=niln then
  1978. inserttypeconv_explicit(left,right.resultdef)
  1979. else
  1980. inserttypeconv_explicit(right,left.resultdef)
  1981. end
  1982. { support dynamicarray=nil,dynamicarray<>nil }
  1983. else if (is_dynamic_array(ld) and (rt=niln)) or
  1984. (is_dynamic_array(rd) and (lt=niln)) or
  1985. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1986. begin
  1987. if not(nodetype in [equaln,unequaln]) then
  1988. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1989. if lt=niln then
  1990. inserttypeconv_explicit(left,right.resultdef)
  1991. else
  1992. inserttypeconv_explicit(right,left.resultdef)
  1993. end
  1994. {$ifdef SUPPORT_MMX}
  1995. { mmx support, this must be before the zero based array
  1996. check }
  1997. else if (cs_mmx in current_settings.localswitches) and
  1998. is_mmx_able_array(ld) and
  1999. is_mmx_able_array(rd) and
  2000. equal_defs(ld,rd) then
  2001. begin
  2002. case nodetype of
  2003. addn,subn,xorn,orn,andn:
  2004. ;
  2005. { mul is a little bit restricted }
  2006. muln:
  2007. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  2008. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2009. else
  2010. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2011. end;
  2012. end
  2013. {$endif SUPPORT_MMX}
  2014. { vector support, this must be before the zero based array
  2015. check }
  2016. else if (cs_support_vectors in current_settings.globalswitches) and
  2017. is_vector(ld) and
  2018. is_vector(rd) and
  2019. equal_defs(ld,rd) then
  2020. begin
  2021. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  2022. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2023. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  2024. resultdef:=left.resultdef;
  2025. end
  2026. { this is a little bit dangerous, also the left type }
  2027. { pointer to should be checked! This broke the mmx support }
  2028. else if (rd.typ=pointerdef) or
  2029. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  2030. begin
  2031. if is_zero_based_array(rd) then
  2032. begin
  2033. resultdef:=cpointerdef.getreusable(tarraydef(rd).elementdef);
  2034. inserttypeconv(right,resultdef);
  2035. end
  2036. else
  2037. resultdef:=right.resultdef;
  2038. inserttypeconv(left,tpointerdef(right.resultdef).pointer_arithmetic_int_type);
  2039. if nodetype=addn then
  2040. begin
  2041. if (rt=niln) then
  2042. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,'NIL');
  2043. if not(cs_extsyntax in current_settings.moduleswitches) or
  2044. (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
  2045. not(cs_pointermath in current_settings.localswitches) and
  2046. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  2047. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2048. if (rd.typ=pointerdef) and
  2049. (tpointerdef(rd).pointeddef.size>1) then
  2050. begin
  2051. left:=caddnode.create(muln,left,
  2052. cordconstnode.create(tpointerdef(rd).pointeddef.size,tpointerdef(right.resultdef).pointer_arithmetic_int_type,true));
  2053. typecheckpass(left);
  2054. end;
  2055. end
  2056. else
  2057. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2058. end
  2059. else if (ld.typ=pointerdef) or
  2060. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  2061. begin
  2062. if is_zero_based_array(ld) then
  2063. begin
  2064. resultdef:=cpointerdef.getreusable(tarraydef(ld).elementdef);
  2065. inserttypeconv(left,resultdef);
  2066. end
  2067. else
  2068. resultdef:=left.resultdef;
  2069. inserttypeconv(right,tpointerdef(left.resultdef).pointer_arithmetic_int_type);
  2070. if nodetype in [addn,subn] then
  2071. begin
  2072. if (lt=niln) then
  2073. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),'NIL',rd.typename);
  2074. if not(cs_extsyntax in current_settings.moduleswitches) or
  2075. (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
  2076. not(cs_pointermath in current_settings.localswitches) and
  2077. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  2078. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2079. if (ld.typ=pointerdef) then
  2080. begin
  2081. if is_big_untyped_addrnode(left) then
  2082. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  2083. if (tpointerdef(ld).pointeddef.size>1) then
  2084. begin
  2085. right:=caddnode.create(muln,right,
  2086. cordconstnode.create(tpointerdef(ld).pointeddef.size,tpointerdef(left.resultdef).pointer_arithmetic_int_type,true));
  2087. typecheckpass(right);
  2088. end
  2089. end else
  2090. if is_zero_based_array(ld) and
  2091. (tarraydef(ld).elementdef.size>1) then
  2092. begin
  2093. right:=caddnode.create(muln,right,
  2094. cordconstnode.create(tarraydef(ld).elementdef.size,tpointerdef(left.resultdef).pointer_arithmetic_int_type,true));
  2095. typecheckpass(right);
  2096. end;
  2097. end
  2098. else
  2099. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2100. end
  2101. else if (rd.typ=procvardef) and
  2102. (ld.typ=procvardef) and
  2103. equal_defs(rd,ld) then
  2104. begin
  2105. if (nodetype in [equaln,unequaln]) then
  2106. begin
  2107. if tprocvardef(rd).is_addressonly then
  2108. begin
  2109. inserttypeconv_internal(right,voidcodepointertype);
  2110. inserttypeconv_internal(left,voidcodepointertype);
  2111. end
  2112. else
  2113. begin
  2114. { find proc field in methodpointer record }
  2115. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  2116. if not assigned(hsym) then
  2117. internalerror(200412043);
  2118. { Compare tmehodpointer(left).proc }
  2119. right:=csubscriptnode.create(
  2120. hsym,
  2121. ctypeconvnode.create_internal(right,methodpointertype));
  2122. typecheckpass(right);
  2123. left:=csubscriptnode.create(
  2124. hsym,
  2125. ctypeconvnode.create_internal(left,methodpointertype));
  2126. typecheckpass(left);
  2127. end;
  2128. end
  2129. else
  2130. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2131. end
  2132. { enums }
  2133. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  2134. begin
  2135. if allowenumop(nodetype) or (nf_internal in flags) then
  2136. inserttypeconv(right,left.resultdef)
  2137. else
  2138. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2139. end
  2140. { generic conversion, this is for error recovery }
  2141. else
  2142. begin
  2143. inserttypeconv(left,sinttype);
  2144. inserttypeconv(right,sinttype);
  2145. end;
  2146. if cmp_of_disjunct_ranges(res) and not(nf_internal in flags) then
  2147. begin
  2148. if res then
  2149. CGMessage(type_w_comparison_always_true)
  2150. else
  2151. CGMessage(type_w_comparison_always_false);
  2152. end;
  2153. { set resultdef if not already done }
  2154. if not assigned(resultdef) then
  2155. begin
  2156. case nodetype of
  2157. ltn,lten,gtn,gten,equaln,unequaln :
  2158. resultdef:=pasbool8type;
  2159. slashn :
  2160. resultdef:=resultrealdef;
  2161. addn:
  2162. begin
  2163. { for strings, return is always a 255 char string }
  2164. if is_shortstring(left.resultdef) then
  2165. resultdef:=cshortstringtype
  2166. else
  2167. { for ansistrings set resultdef to assignment left node
  2168. if it is an assignment and left node expects ansistring }
  2169. if is_ansistring(left.resultdef) and
  2170. assigned(aktassignmentnode) and
  2171. (aktassignmentnode.right=self) and
  2172. is_ansistring(aktassignmentnode.left.resultdef) then
  2173. resultdef:=aktassignmentnode.left.resultdef
  2174. else
  2175. resultdef:=left.resultdef;
  2176. end;
  2177. else
  2178. resultdef:=left.resultdef;
  2179. end;
  2180. end;
  2181. { when the result is currency we need some extra code for
  2182. multiplication and division. this should not be done when
  2183. the muln or slashn node is created internally }
  2184. if not(nf_is_currency in flags) and
  2185. is_currency(resultdef) then
  2186. begin
  2187. case nodetype of
  2188. slashn :
  2189. begin
  2190. { slashn will only work with floats }
  2191. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  2192. include(hp.flags,nf_is_currency);
  2193. result:=hp;
  2194. end;
  2195. muln :
  2196. begin
  2197. if s64currencytype.typ=floatdef then
  2198. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  2199. else
  2200. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  2201. include(hp.flags,nf_is_currency);
  2202. result:=hp
  2203. end;
  2204. end;
  2205. end;
  2206. if not codegenerror and
  2207. not assigned(result) then
  2208. result:=simplify(false);
  2209. end;
  2210. function taddnode.first_addstring: tnode;
  2211. const
  2212. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  2213. var
  2214. p: tnode;
  2215. newstatement : tstatementnode;
  2216. tempnode (*,tempnode2*) : ttempcreatenode;
  2217. cmpfuncname: string;
  2218. para: tcallparanode;
  2219. begin
  2220. result:=nil;
  2221. { when we get here, we are sure that both the left and the right }
  2222. { node are both strings of the same stringtype (JM) }
  2223. case nodetype of
  2224. addn:
  2225. begin
  2226. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  2227. begin
  2228. result:=right;
  2229. left.free;
  2230. left:=nil;
  2231. right:=nil;
  2232. exit;
  2233. end;
  2234. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  2235. begin
  2236. result:=left;
  2237. left:=nil;
  2238. right.free;
  2239. right:=nil;
  2240. exit;
  2241. end;
  2242. { create the call to the concat routine both strings as arguments }
  2243. if assigned(aktassignmentnode) and
  2244. (aktassignmentnode.right=self) and
  2245. (aktassignmentnode.left.resultdef=resultdef) and
  2246. valid_for_var(aktassignmentnode.left,false) then
  2247. begin
  2248. para:=ccallparanode.create(
  2249. right,
  2250. ccallparanode.create(
  2251. left,
  2252. ccallparanode.create(aktassignmentnode.left.getcopy,nil)
  2253. )
  2254. );
  2255. if is_ansistring(resultdef) then
  2256. para:=ccallparanode.create(
  2257. cordconstnode.create(
  2258. { don't use getparaencoding(), we have to know
  2259. when the result is rawbytestring }
  2260. tstringdef(resultdef).encoding,
  2261. u16inttype,
  2262. true
  2263. ),
  2264. para
  2265. );
  2266. result:=ccallnode.createintern(
  2267. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2268. para
  2269. );
  2270. include(aktassignmentnode.flags,nf_assign_done_in_right);
  2271. firstpass(result);
  2272. end
  2273. else
  2274. begin
  2275. result:=internalstatements(newstatement);
  2276. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2277. addstatement(newstatement,tempnode);
  2278. { initialize the temp, since it will be passed to a
  2279. var-parameter (and finalization, which is performed by the
  2280. ttempcreate node and which takes care of the initialization
  2281. on native targets, is a noop on managed VM targets) }
  2282. if (target_info.system in systems_managed_vm) and
  2283. is_managed_type(resultdef) then
  2284. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  2285. false,
  2286. ccallparanode.create(genintconstnode(0),
  2287. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  2288. para:=ccallparanode.create(
  2289. right,
  2290. ccallparanode.create(
  2291. left,
  2292. ccallparanode.create(ctemprefnode.create(tempnode),nil)
  2293. )
  2294. );
  2295. if is_ansistring(resultdef) then
  2296. para:=ccallparanode.create(
  2297. cordconstnode.create(
  2298. { don't use getparaencoding(), we have to know
  2299. when the result is rawbytestring }
  2300. tstringdef(resultdef).encoding,
  2301. u16inttype,
  2302. true
  2303. ),
  2304. para
  2305. );
  2306. addstatement(
  2307. newstatement,
  2308. ccallnode.createintern(
  2309. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2310. para
  2311. )
  2312. );
  2313. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2314. addstatement(newstatement,ctemprefnode.create(tempnode));
  2315. end;
  2316. { we reused the arguments }
  2317. left := nil;
  2318. right := nil;
  2319. end;
  2320. ltn,lten,gtn,gten,equaln,unequaln :
  2321. begin
  2322. { generate better code for comparison with empty string, we
  2323. only need to compare the length with 0 }
  2324. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  2325. { windows widestrings are too complicated to be handled optimized }
  2326. not(is_widestring(left.resultdef) and (target_info.system in systems_windows)) and
  2327. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  2328. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  2329. begin
  2330. { switch so that the constant is always on the right }
  2331. if left.nodetype = stringconstn then
  2332. begin
  2333. p := left;
  2334. left := right;
  2335. right := p;
  2336. nodetype:=swap_relation[nodetype];
  2337. end;
  2338. if is_shortstring(left.resultdef) or
  2339. (nodetype in [gtn,gten,ltn,lten]) or
  2340. (target_info.system in systems_managed_vm) then
  2341. { compare the length with 0 }
  2342. result := caddnode.create(nodetype,
  2343. cinlinenode.create(in_length_x,false,left),
  2344. cordconstnode.create(0,s8inttype,false))
  2345. else
  2346. begin
  2347. (*
  2348. if is_widestring(left.resultdef) and
  2349. (target_info.system in system_windows) then
  2350. begin
  2351. { windows like widestrings requires that we also check the length }
  2352. result:=internalstatements(newstatement);
  2353. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2354. tempnode2:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2355. addstatement(newstatement,tempnode);
  2356. addstatement(newstatement,tempnode2);
  2357. { poor man's cse }
  2358. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2359. ctypeconvnode.create_internal(left,voidpointertype))
  2360. );
  2361. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode2),
  2362. caddnode.create(orn,
  2363. caddnode.create(nodetype,
  2364. ctemprefnode.create(tempnode),
  2365. cpointerconstnode.create(0,voidpointertype)
  2366. ),
  2367. caddnode.create(nodetype,
  2368. ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),s32inttype),
  2369. cordconstnode.create(0,s32inttype,false)
  2370. )
  2371. )
  2372. ));
  2373. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2374. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode2));
  2375. addstatement(newstatement,ctemprefnode.create(tempnode2));
  2376. end
  2377. else
  2378. *)
  2379. begin
  2380. { compare the pointer with nil (for ansistrings etc), }
  2381. { faster than getting the length (JM) }
  2382. result:= caddnode.create(nodetype,
  2383. ctypeconvnode.create_internal(left,voidpointertype),
  2384. cpointerconstnode.create(0,voidpointertype));
  2385. end;
  2386. end;
  2387. { left is reused }
  2388. left := nil;
  2389. { right isn't }
  2390. right.free;
  2391. right := nil;
  2392. exit;
  2393. end;
  2394. { no string constant -> call compare routine }
  2395. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  2396. { for equality checks use optimized version }
  2397. if nodetype in [equaln,unequaln] then
  2398. cmpfuncname := cmpfuncname + '_equal';
  2399. result := ccallnode.createintern(cmpfuncname,
  2400. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2401. { and compare its result with 0 according to the original operator }
  2402. result := caddnode.create(nodetype,result,
  2403. cordconstnode.create(0,s8inttype,false));
  2404. left := nil;
  2405. right := nil;
  2406. end;
  2407. end;
  2408. end;
  2409. function taddnode.first_addset : tnode;
  2410. procedure call_varset_helper(const n : string);
  2411. var
  2412. newstatement : tstatementnode;
  2413. temp : ttempcreatenode;
  2414. begin
  2415. { add two var sets }
  2416. result:=internalstatements(newstatement);
  2417. { create temp for result }
  2418. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2419. addstatement(newstatement,temp);
  2420. addstatement(newstatement,ccallnode.createintern(n,
  2421. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2422. ccallparanode.create(ctemprefnode.create(temp),
  2423. ccallparanode.create(right,
  2424. ccallparanode.create(left,nil)))))
  2425. );
  2426. { remove reused parts from original node }
  2427. left:=nil;
  2428. right:=nil;
  2429. { the last statement should return the value as
  2430. location and type, this is done be referencing the
  2431. temp and converting it first from a persistent temp to
  2432. normal temp }
  2433. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2434. addstatement(newstatement,ctemprefnode.create(temp));
  2435. end;
  2436. var
  2437. procname: string[31];
  2438. tempn: tnode;
  2439. newstatement : tstatementnode;
  2440. temp : ttempcreatenode;
  2441. begin
  2442. result:=nil;
  2443. case nodetype of
  2444. equaln,unequaln,lten,gten:
  2445. begin
  2446. case nodetype of
  2447. equaln,unequaln:
  2448. procname := 'fpc_varset_comp_sets';
  2449. lten,gten:
  2450. begin
  2451. procname := 'fpc_varset_contains_sets';
  2452. { (left >= right) = (right <= left) }
  2453. if nodetype = gten then
  2454. begin
  2455. tempn := left;
  2456. left := right;
  2457. right := tempn;
  2458. end;
  2459. end;
  2460. else
  2461. internalerror(2013112911);
  2462. end;
  2463. result := ccallnode.createinternres(procname,
  2464. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2465. ccallparanode.create(right,
  2466. ccallparanode.create(left,nil))),resultdef);
  2467. { left and right are reused as parameters }
  2468. left := nil;
  2469. right := nil;
  2470. { for an unequaln, we have to negate the result of comp_sets }
  2471. if nodetype = unequaln then
  2472. result := cnotnode.create(result);
  2473. end;
  2474. addn:
  2475. begin
  2476. { optimize first loading of a set }
  2477. if (right.nodetype=setelementn) and
  2478. not(assigned(tsetelementnode(right).right)) and
  2479. is_emptyset(left) then
  2480. begin
  2481. result:=internalstatements(newstatement);
  2482. { create temp for result }
  2483. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2484. addstatement(newstatement,temp);
  2485. { adjust for set base }
  2486. tsetelementnode(right).left:=caddnode.create(subn,
  2487. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2488. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2489. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  2490. ccallparanode.create(ctemprefnode.create(temp),
  2491. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2492. ccallparanode.create(tsetelementnode(right).left,nil))))
  2493. );
  2494. { the last statement should return the value as
  2495. location and type, this is done be referencing the
  2496. temp and converting it first from a persistent temp to
  2497. normal temp }
  2498. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2499. addstatement(newstatement,ctemprefnode.create(temp));
  2500. tsetelementnode(right).left := nil;
  2501. end
  2502. else
  2503. begin
  2504. if right.nodetype=setelementn then
  2505. begin
  2506. result:=internalstatements(newstatement);
  2507. { create temp for result }
  2508. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2509. addstatement(newstatement,temp);
  2510. { adjust for set base }
  2511. tsetelementnode(right).left:=caddnode.create(subn,
  2512. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2513. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2514. { add a range or a single element? }
  2515. if assigned(tsetelementnode(right).right) then
  2516. begin
  2517. { adjust for set base }
  2518. tsetelementnode(right).right:=caddnode.create(subn,
  2519. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  2520. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2521. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  2522. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2523. ccallparanode.create(tsetelementnode(right).right,
  2524. ccallparanode.create(tsetelementnode(right).left,
  2525. ccallparanode.create(ctemprefnode.create(temp),
  2526. ccallparanode.create(left,nil))))))
  2527. );
  2528. end
  2529. else
  2530. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  2531. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2532. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2533. ccallparanode.create(ctemprefnode.create(temp),
  2534. ccallparanode.create(left,nil)))))
  2535. );
  2536. { remove reused parts from original node }
  2537. tsetelementnode(right).right:=nil;
  2538. tsetelementnode(right).left:=nil;
  2539. left:=nil;
  2540. { the last statement should return the value as
  2541. location and type, this is done be referencing the
  2542. temp and converting it first from a persistent temp to
  2543. normal temp }
  2544. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2545. addstatement(newstatement,ctemprefnode.create(temp));
  2546. end
  2547. else
  2548. call_varset_helper('fpc_varset_add_sets');
  2549. end
  2550. end;
  2551. subn:
  2552. call_varset_helper('fpc_varset_sub_sets');
  2553. symdifn:
  2554. call_varset_helper('fpc_varset_symdif_sets');
  2555. muln:
  2556. call_varset_helper('fpc_varset_mul_sets');
  2557. else
  2558. internalerror(200609241);
  2559. end;
  2560. end;
  2561. function taddnode.use_generic_mul32to64: boolean;
  2562. begin
  2563. result := true;
  2564. end;
  2565. function taddnode.use_generic_mul64bit: boolean;
  2566. begin
  2567. result := true;
  2568. end;
  2569. function taddnode.try_make_mul32to64: boolean;
  2570. function canbe32bitint(v: tconstexprint): boolean;
  2571. begin
  2572. result := ((v >= int64(low(longint))) and (v <= int64(high(longint)))) or
  2573. ((v >= qword(low(cardinal))) and (v <= qword(high(cardinal))))
  2574. end;
  2575. function is_32bitordconst(n: tnode): boolean;
  2576. begin
  2577. result := (n.nodetype = ordconstn) and
  2578. canbe32bitint(tordconstnode(n).value);
  2579. end;
  2580. function is_32to64typeconv(n: tnode): boolean;
  2581. begin
  2582. result := (n.nodetype = typeconvn) and
  2583. is_integer(ttypeconvnode(n).left.resultdef) and
  2584. not is_64bit(ttypeconvnode(n).left.resultdef);
  2585. end;
  2586. var
  2587. temp: tnode;
  2588. begin
  2589. result := false;
  2590. if is_32to64typeconv(left) and
  2591. (is_32bitordconst(right) or
  2592. is_32to64typeconv(right) and
  2593. ((is_signed(ttypeconvnode(left).left.resultdef) =
  2594. is_signed(ttypeconvnode(right).left.resultdef)) or
  2595. (is_signed(ttypeconvnode(left).left.resultdef) and
  2596. (torddef(ttypeconvnode(right).left.resultdef).ordtype in [u8bit,u16bit])))) then
  2597. begin
  2598. temp := ttypeconvnode(left).left;
  2599. ttypeconvnode(left).left := nil;
  2600. left.free;
  2601. left := temp;
  2602. if (right.nodetype = typeconvn) then
  2603. begin
  2604. temp := ttypeconvnode(right).left;
  2605. ttypeconvnode(right).left := nil;
  2606. right.free;
  2607. right := temp;
  2608. end;
  2609. if (is_signed(left.resultdef)) then
  2610. begin
  2611. inserttypeconv_internal(left,s32inttype);
  2612. inserttypeconv_internal(right,s32inttype);
  2613. end
  2614. else
  2615. begin
  2616. inserttypeconv_internal(left,u32inttype);
  2617. inserttypeconv_internal(right,u32inttype);
  2618. end;
  2619. firstpass(left);
  2620. firstpass(right);
  2621. result := true;
  2622. end;
  2623. end;
  2624. function taddnode.use_fma : boolean;
  2625. begin
  2626. result:=false;
  2627. end;
  2628. function taddnode.try_fma(ld,rd : tdef) : tnode;
  2629. var
  2630. inlinennr : Integer;
  2631. begin
  2632. result:=nil;
  2633. if (cs_opt_fastmath in current_settings.optimizerswitches) and
  2634. use_fma and
  2635. (nodetype in [addn,subn]) and
  2636. (rd.typ=floatdef) and (ld.typ=floatdef) and
  2637. (is_single(rd) or is_double(rd)) and
  2638. equal_defs(rd,ld) and
  2639. { transforming a*b+c into fma(a,b,c) makes only sense if c can be
  2640. calculated easily. Consider a*b+c*d which results in
  2641. fmul
  2642. fmul
  2643. fadd
  2644. and in
  2645. fmul
  2646. fma
  2647. when using the fma optimization. On a super scalar architecture, the first instruction
  2648. sequence requires clock_cycles(fmul)+clock_cycles(fadd) clock cycles because the fmuls can be executed in parallel.
  2649. The second sequence requires clock_cycles(fmul)+clock_cycles(fma) because the fma has to wait for the
  2650. result of the fmul. Since typically clock_cycles(fma)>clock_cycles(fadd) applies, the first sequence is better.
  2651. }
  2652. (((left.nodetype=muln) and (node_complexity(right)<3)) or
  2653. ((right.nodetype=muln) and (node_complexity(left)<3)) or
  2654. ((left.nodetype=inlinen) and
  2655. (tinlinenode(left).inlinenumber=in_sqr_real) and
  2656. (node_complexity(right)<3)) or
  2657. ((right.nodetype=inlinen) and
  2658. (tinlinenode(right).inlinenumber=in_sqr_real) and
  2659. (node_complexity(left)<3))
  2660. ) then
  2661. begin
  2662. case tfloatdef(ld).floattype of
  2663. s32real:
  2664. inlinennr:=in_fma_single;
  2665. s64real:
  2666. inlinennr:=in_fma_double;
  2667. s80real:
  2668. inlinennr:=in_fma_extended;
  2669. s128real:
  2670. inlinennr:=in_fma_float128;
  2671. else
  2672. internalerror(2014042601);
  2673. end;
  2674. if left.nodetype=muln then
  2675. begin
  2676. if nodetype=subn then
  2677. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(cunaryminusnode.create(right),
  2678. ccallparanode.create(taddnode(left).right,
  2679. ccallparanode.create(taddnode(left).left,nil
  2680. ))))
  2681. else
  2682. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(right,
  2683. ccallparanode.create(taddnode(left).right,
  2684. ccallparanode.create(taddnode(left).left,nil
  2685. ))));
  2686. right:=nil;
  2687. taddnode(left).right:=nil;
  2688. taddnode(left).left:=nil;
  2689. end
  2690. else if right.nodetype=muln then
  2691. begin
  2692. if nodetype=subn then
  2693. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  2694. ccallparanode.create(cunaryminusnode.create(taddnode(right).right),
  2695. ccallparanode.create(taddnode(right).left,nil
  2696. ))))
  2697. else
  2698. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  2699. ccallparanode.create(taddnode(right).right,
  2700. ccallparanode.create(taddnode(right).left,nil
  2701. ))));
  2702. left:=nil;
  2703. taddnode(right).right:=nil;
  2704. taddnode(right).left:=nil;
  2705. end
  2706. else if (left.nodetype=inlinen) and (tinlinenode(left).inlinenumber=in_sqr_real) then
  2707. begin
  2708. if nodetype=subn then
  2709. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(cunaryminusnode.create(right),
  2710. ccallparanode.create(tinlinenode(left).left.getcopy,
  2711. ccallparanode.create(tinlinenode(left).left.getcopy,nil
  2712. ))))
  2713. else
  2714. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(right,
  2715. ccallparanode.create(tinlinenode(left).left.getcopy,
  2716. ccallparanode.create(tinlinenode(left).left.getcopy,nil
  2717. ))));
  2718. right:=nil;
  2719. end
  2720. { we get here only if right is a sqr node }
  2721. else if (right.nodetype=inlinen) and (tinlinenode(right).inlinenumber=in_sqr_real) then
  2722. begin
  2723. if nodetype=subn then
  2724. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  2725. ccallparanode.create(cunaryminusnode.create(tinlinenode(right).left.getcopy),
  2726. ccallparanode.create(tinlinenode(right).left.getcopy,nil
  2727. ))))
  2728. else
  2729. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  2730. ccallparanode.create(tinlinenode(right).left.getcopy,
  2731. ccallparanode.create(tinlinenode(right).left.getcopy,nil
  2732. ))));
  2733. left:=nil;
  2734. end;
  2735. end;
  2736. end;
  2737. function taddnode.first_add64bitint: tnode;
  2738. var
  2739. procname: string[31];
  2740. temp: tnode;
  2741. power: longint;
  2742. begin
  2743. result := nil;
  2744. { create helper calls mul }
  2745. if nodetype <> muln then
  2746. exit;
  2747. { make sure that if there is a constant, that it's on the right }
  2748. if left.nodetype = ordconstn then
  2749. begin
  2750. temp := right;
  2751. right := left;
  2752. left := temp;
  2753. end;
  2754. { can we use a shift instead of a mul? }
  2755. if not (cs_check_overflow in current_settings.localswitches) and
  2756. (right.nodetype = ordconstn) and
  2757. ispowerof2(tordconstnode(right).value,power) then
  2758. begin
  2759. tordconstnode(right).value := power;
  2760. result := cshlshrnode.create(shln,left,right);
  2761. { left and right are reused }
  2762. left := nil;
  2763. right := nil;
  2764. { return firstpassed new node }
  2765. exit;
  2766. end;
  2767. if try_make_mul32to64 then
  2768. begin
  2769. { this uses the same criteria for signedness as the 32 to 64-bit mul
  2770. handling in the i386 code generator }
  2771. if is_signed(left.resultdef) and is_signed(right.resultdef) then
  2772. procname := 'fpc_mul_longint_to_int64'
  2773. else
  2774. procname := 'fpc_mul_dword_to_qword';
  2775. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  2776. result := ccallnode.createintern(procname,right);
  2777. left := nil;
  2778. right := nil;
  2779. end
  2780. else
  2781. begin
  2782. { can full 64-bit multiplication be handled inline? }
  2783. if not use_generic_mul64bit then
  2784. begin
  2785. { generic handling replaces this node with call to fpc_mul_int64,
  2786. whose result is int64 }
  2787. if is_currency(resultdef) then
  2788. resultdef:=s64inttype;
  2789. exit;
  2790. end;
  2791. { when currency is used set the result of the
  2792. parameters to s64bit, so they are not converted }
  2793. if is_currency(resultdef) then
  2794. begin
  2795. left.resultdef:=s64inttype;
  2796. right.resultdef:=s64inttype;
  2797. end;
  2798. { otherwise, create the parameters for the helper }
  2799. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  2800. left := nil;
  2801. { only qword needs the unsigned code, the
  2802. signed code is also used for currency }
  2803. if is_signed(resultdef) then
  2804. procname := 'fpc_mul_int64'
  2805. else
  2806. procname := 'fpc_mul_qword';
  2807. if cs_check_overflow in current_settings.localswitches then
  2808. procname := procname + '_checkoverflow';
  2809. result := ccallnode.createintern(procname,right);
  2810. right := nil;
  2811. end;
  2812. end;
  2813. function taddnode.first_addpointer: tnode;
  2814. begin
  2815. result:=nil;
  2816. expectloc:=LOC_REGISTER;
  2817. end;
  2818. function taddnode.first_cmppointer: tnode;
  2819. begin
  2820. result:=nil;
  2821. expectloc:=LOC_FLAGS;
  2822. end;
  2823. function taddnode.first_addfloat : tnode;
  2824. var
  2825. procname: string[31];
  2826. { do we need to reverse the result ? }
  2827. notnode : boolean;
  2828. fdef : tdef;
  2829. begin
  2830. result := nil;
  2831. notnode := false;
  2832. fdef := nil;
  2833. { In non-emulation mode, real opcodes are
  2834. emitted for floating point values.
  2835. }
  2836. if not ((cs_fp_emulation in current_settings.moduleswitches)
  2837. {$ifdef cpufpemu}
  2838. or (current_settings.fputype=fpu_soft)
  2839. {$endif cpufpemu}
  2840. ) then
  2841. exit;
  2842. if not(target_info.system in systems_wince) then
  2843. begin
  2844. case tfloatdef(left.resultdef).floattype of
  2845. s32real:
  2846. begin
  2847. fdef:=search_system_type('FLOAT32REC').typedef;
  2848. procname:='float32';
  2849. end;
  2850. s64real:
  2851. begin
  2852. fdef:=search_system_type('FLOAT64').typedef;
  2853. procname:='float64';
  2854. end;
  2855. {!!! not yet implemented
  2856. s128real:
  2857. }
  2858. else
  2859. internalerror(2005082601);
  2860. end;
  2861. case nodetype of
  2862. addn:
  2863. procname:=procname+'_add';
  2864. muln:
  2865. procname:=procname+'_mul';
  2866. subn:
  2867. procname:=procname+'_sub';
  2868. slashn:
  2869. procname:=procname+'_div';
  2870. ltn:
  2871. procname:=procname+'_lt';
  2872. lten:
  2873. procname:=procname+'_le';
  2874. gtn:
  2875. begin
  2876. procname:=procname+'_lt';
  2877. swapleftright;
  2878. end;
  2879. gten:
  2880. begin
  2881. procname:=procname+'_le';
  2882. swapleftright;
  2883. end;
  2884. equaln:
  2885. procname:=procname+'_eq';
  2886. unequaln:
  2887. begin
  2888. procname:=procname+'_eq';
  2889. notnode:=true;
  2890. end;
  2891. else
  2892. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2893. end;
  2894. end
  2895. else
  2896. begin
  2897. case nodetype of
  2898. addn:
  2899. procname:='add';
  2900. muln:
  2901. procname:='mul';
  2902. subn:
  2903. procname:='sub';
  2904. slashn:
  2905. procname:='div';
  2906. ltn:
  2907. procname:='lt';
  2908. lten:
  2909. procname:='le';
  2910. gtn:
  2911. procname:='gt';
  2912. gten:
  2913. procname:='ge';
  2914. equaln:
  2915. procname:='eq';
  2916. unequaln:
  2917. procname:='ne';
  2918. else
  2919. begin
  2920. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2921. exit;
  2922. end;
  2923. end;
  2924. case tfloatdef(left.resultdef).floattype of
  2925. s32real:
  2926. begin
  2927. procname:=procname+'s';
  2928. if nodetype in [addn,muln,subn,slashn] then
  2929. procname:=lower(procname);
  2930. end;
  2931. s64real:
  2932. procname:=procname+'d';
  2933. {!!! not yet implemented
  2934. s128real:
  2935. }
  2936. else
  2937. internalerror(2005082602);
  2938. end;
  2939. end;
  2940. { cast softfpu result? }
  2941. if not(target_info.system in systems_wince) then
  2942. begin
  2943. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2944. resultdef:=pasbool8type;
  2945. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  2946. ctypeconvnode.create_internal(right,fdef),
  2947. ccallparanode.create(
  2948. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  2949. end
  2950. else
  2951. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  2952. ccallparanode.create(left,nil)));
  2953. left:=nil;
  2954. right:=nil;
  2955. { do we need to reverse the result }
  2956. if notnode then
  2957. result:=cnotnode.create(result);
  2958. end;
  2959. function taddnode.pass_1 : tnode;
  2960. var
  2961. {$ifdef addstringopt}
  2962. hp : tnode;
  2963. {$endif addstringopt}
  2964. rd,ld : tdef;
  2965. i,i2 : longint;
  2966. lt,rt : tnodetype;
  2967. {$ifdef cpuneedsmulhelper}
  2968. procname : string[32];
  2969. {$endif cpuneedsmulhelper}
  2970. begin
  2971. result:=nil;
  2972. { Can we optimize multiple string additions into a single call?
  2973. This need to be done on a complete tree to detect the multiple
  2974. add nodes and is therefor done before the subtrees are processed }
  2975. if canbemultistringadd(self) then
  2976. begin
  2977. result:=genmultistringadd(self);
  2978. exit;
  2979. end;
  2980. { first do the two subtrees }
  2981. firstpass(left);
  2982. firstpass(right);
  2983. if codegenerror then
  2984. exit;
  2985. { load easier access variables }
  2986. rd:=right.resultdef;
  2987. ld:=left.resultdef;
  2988. rt:=right.nodetype;
  2989. lt:=left.nodetype;
  2990. { int/int gives real/real! }
  2991. if nodetype=slashn then
  2992. begin
  2993. {$ifdef cpufpemu}
  2994. result:=first_addfloat;
  2995. if assigned(result) then
  2996. exit;
  2997. {$endif cpufpemu}
  2998. expectloc:=LOC_FPUREGISTER;
  2999. end
  3000. { if both are orddefs then check sub types }
  3001. else if (ld.typ=orddef) and (rd.typ=orddef) then
  3002. begin
  3003. { optimize multiplacation by a power of 2 }
  3004. if not(cs_check_overflow in current_settings.localswitches) and
  3005. (nodetype = muln) and
  3006. (((left.nodetype = ordconstn) and
  3007. ispowerof2(tordconstnode(left).value,i)) or
  3008. ((right.nodetype = ordconstn) and
  3009. ispowerof2(tordconstnode(right).value,i2))) then
  3010. begin
  3011. if ((left.nodetype = ordconstn) and
  3012. ispowerof2(tordconstnode(left).value,i)) then
  3013. begin
  3014. tordconstnode(left).value := i;
  3015. result := cshlshrnode.create(shln,right,left);
  3016. end
  3017. else
  3018. begin
  3019. tordconstnode(right).value := i2;
  3020. result := cshlshrnode.create(shln,left,right);
  3021. end;
  3022. result.resultdef := resultdef;
  3023. left := nil;
  3024. right := nil;
  3025. exit;
  3026. end;
  3027. { 2 booleans ? }
  3028. if is_boolean(ld) and is_boolean(rd) then
  3029. begin
  3030. if (not(cs_full_boolean_eval in current_settings.localswitches) or
  3031. (nf_short_bool in flags)) and
  3032. (nodetype in [andn,orn]) then
  3033. expectloc:=LOC_JUMP
  3034. else
  3035. begin
  3036. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  3037. expectloc:=LOC_FLAGS
  3038. else
  3039. expectloc:=LOC_REGISTER;
  3040. end;
  3041. end
  3042. else
  3043. { Both are chars? only convert to shortstrings for addn }
  3044. if is_char(ld) then
  3045. begin
  3046. if nodetype=addn then
  3047. internalerror(200103291);
  3048. expectloc:=LOC_FLAGS;
  3049. end
  3050. else if (nodetype=muln) and
  3051. is_64bitint(resultdef) and
  3052. not use_generic_mul32to64 and
  3053. try_make_mul32to64 then
  3054. begin
  3055. { if the code generator can handle 32 to 64-bit muls,
  3056. we're done here }
  3057. end
  3058. {$ifndef cpu64bitalu}
  3059. { is there a 64 bit type ? }
  3060. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  3061. begin
  3062. result := first_add64bitint;
  3063. if assigned(result) then
  3064. exit;
  3065. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3066. expectloc:=LOC_REGISTER
  3067. else
  3068. expectloc:=LOC_JUMP;
  3069. end
  3070. {$endif cpu64bitalu}
  3071. { generic 32bit conversion }
  3072. else
  3073. begin
  3074. {$ifdef cpuneedsmulhelper}
  3075. if (nodetype=muln) and not(torddef(resultdef).ordtype in [u8bit,s8bit
  3076. {$if defined(cpu16bitalu) or defined(avr)},u16bit,s16bit{$endif}]) then
  3077. begin
  3078. result := nil;
  3079. case torddef(resultdef).ordtype of
  3080. s16bit:
  3081. procname := 'fpc_mul_integer';
  3082. u16bit:
  3083. procname := 'fpc_mul_word';
  3084. s32bit:
  3085. procname := 'fpc_mul_longint';
  3086. u32bit:
  3087. procname := 'fpc_mul_dword';
  3088. else
  3089. internalerror(2011022301);
  3090. end;
  3091. result := ccallnode.createintern(procname,
  3092. ccallparanode.create(cordconstnode.create(ord(cs_check_overflow in current_settings.localswitches),pasbool8type,false),
  3093. ccallparanode.create(right,
  3094. ccallparanode.create(left,nil))));
  3095. left := nil;
  3096. right := nil;
  3097. firstpass(result);
  3098. exit;
  3099. end;
  3100. {$endif cpuneedsmulhelper}
  3101. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3102. expectloc:=LOC_REGISTER
  3103. else if torddef(ld).size>sizeof(aint) then
  3104. expectloc:=LOC_JUMP
  3105. else
  3106. expectloc:=LOC_FLAGS;
  3107. end;
  3108. end
  3109. { left side a setdef, must be before string processing,
  3110. else array constructor can be seen as array of char (PFV) }
  3111. else if (ld.typ=setdef) then
  3112. begin
  3113. { small sets are handled inline by the compiler.
  3114. small set doesn't have support for adding ranges }
  3115. if is_smallset(ld) and
  3116. not(
  3117. (right.nodetype=setelementn) and
  3118. assigned(tsetelementnode(right).right)
  3119. ) then
  3120. begin
  3121. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  3122. expectloc:=LOC_FLAGS
  3123. else
  3124. expectloc:=LOC_REGISTER;
  3125. end
  3126. else
  3127. begin
  3128. result := first_addset;
  3129. if assigned(result) then
  3130. exit;
  3131. expectloc:=LOC_CREFERENCE;
  3132. end;
  3133. end
  3134. { compare pchar by addresses like BP/Delphi }
  3135. else if is_pchar(ld) then
  3136. begin
  3137. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3138. result:=first_addpointer
  3139. else
  3140. result:=first_cmppointer;
  3141. end
  3142. { is one of the operands a string }
  3143. else if (ld.typ=stringdef) then
  3144. begin
  3145. if is_widestring(ld) then
  3146. begin
  3147. { this is only for add, the comparisaion is handled later }
  3148. expectloc:=LOC_REGISTER;
  3149. end
  3150. else if is_unicodestring(ld) then
  3151. begin
  3152. { this is only for add, the comparisaion is handled later }
  3153. expectloc:=LOC_REGISTER;
  3154. end
  3155. else if is_ansistring(ld) then
  3156. begin
  3157. { this is only for add, the comparisaion is handled later }
  3158. expectloc:=LOC_REGISTER;
  3159. end
  3160. else if is_longstring(ld) then
  3161. begin
  3162. { this is only for add, the comparisaion is handled later }
  3163. expectloc:=LOC_REFERENCE;
  3164. end
  3165. else
  3166. begin
  3167. {$ifdef addstringopt}
  3168. { can create a call which isn't handled by callparatemp }
  3169. if canbeaddsstringcharoptnode(self) then
  3170. begin
  3171. hp := genaddsstringcharoptnode(self);
  3172. pass_1 := hp;
  3173. exit;
  3174. end
  3175. else
  3176. {$endif addstringopt}
  3177. begin
  3178. { Fix right to be shortstring }
  3179. if is_char(right.resultdef) then
  3180. begin
  3181. inserttypeconv(right,cshortstringtype);
  3182. firstpass(right);
  3183. end;
  3184. end;
  3185. {$ifdef addstringopt}
  3186. { can create a call which isn't handled by callparatemp }
  3187. if canbeaddsstringcsstringoptnode(self) then
  3188. begin
  3189. hp := genaddsstringcsstringoptnode(self);
  3190. pass_1 := hp;
  3191. exit;
  3192. end;
  3193. {$endif addstringopt}
  3194. end;
  3195. { otherwise, let addstring convert everything }
  3196. result := first_addstring;
  3197. exit;
  3198. end
  3199. { is one a real float ? }
  3200. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  3201. begin
  3202. {$ifdef cpufpemu}
  3203. result:=first_addfloat;
  3204. if assigned(result) then
  3205. exit;
  3206. {$endif cpufpemu}
  3207. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3208. expectloc:=LOC_FPUREGISTER
  3209. else
  3210. expectloc:=LOC_FLAGS;
  3211. result:=try_fma(ld,rd);
  3212. if assigned(result) then
  3213. exit;
  3214. end
  3215. { pointer comperation and subtraction }
  3216. else if (ld.typ=pointerdef) then
  3217. begin
  3218. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3219. result:=first_addpointer
  3220. else
  3221. result:=first_cmppointer;
  3222. end
  3223. else if is_implicit_pointer_object_type(ld) then
  3224. begin
  3225. expectloc:=LOC_FLAGS;
  3226. end
  3227. else if (ld.typ=classrefdef) then
  3228. begin
  3229. expectloc:=LOC_FLAGS;
  3230. end
  3231. { support procvar=nil,procvar<>nil }
  3232. else if ((ld.typ=procvardef) and (rt=niln)) or
  3233. ((rd.typ=procvardef) and (lt=niln)) then
  3234. begin
  3235. expectloc:=LOC_FLAGS;
  3236. end
  3237. {$ifdef SUPPORT_MMX}
  3238. { mmx support, this must be before the zero based array
  3239. check }
  3240. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  3241. is_mmx_able_array(rd) then
  3242. begin
  3243. expectloc:=LOC_MMXREGISTER;
  3244. end
  3245. {$endif SUPPORT_MMX}
  3246. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  3247. begin
  3248. result:=first_addpointer;
  3249. end
  3250. else if (rd.typ=procvardef) and
  3251. (ld.typ=procvardef) and
  3252. equal_defs(rd,ld) then
  3253. begin
  3254. expectloc:=LOC_FLAGS;
  3255. end
  3256. else if (ld.typ=enumdef) then
  3257. begin
  3258. expectloc:=LOC_FLAGS;
  3259. end
  3260. {$ifdef SUPPORT_MMX}
  3261. else if (cs_mmx in current_settings.localswitches) and
  3262. is_mmx_able_array(ld) and
  3263. is_mmx_able_array(rd) then
  3264. begin
  3265. expectloc:=LOC_MMXREGISTER;
  3266. end
  3267. {$endif SUPPORT_MMX}
  3268. { the general solution is to convert to 32 bit int }
  3269. else
  3270. begin
  3271. expectloc:=LOC_REGISTER;
  3272. end;
  3273. end;
  3274. {$ifdef state_tracking}
  3275. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  3276. var factval:Tnode;
  3277. begin
  3278. track_state_pass:=false;
  3279. if left.track_state_pass(exec_known) then
  3280. begin
  3281. track_state_pass:=true;
  3282. left.resultdef:=nil;
  3283. do_typecheckpass(left);
  3284. end;
  3285. factval:=aktstate.find_fact(left);
  3286. if factval<>nil then
  3287. begin
  3288. track_state_pass:=true;
  3289. left.destroy;
  3290. left:=factval.getcopy;
  3291. end;
  3292. if right.track_state_pass(exec_known) then
  3293. begin
  3294. track_state_pass:=true;
  3295. right.resultdef:=nil;
  3296. do_typecheckpass(right);
  3297. end;
  3298. factval:=aktstate.find_fact(right);
  3299. if factval<>nil then
  3300. begin
  3301. track_state_pass:=true;
  3302. right.destroy;
  3303. right:=factval.getcopy;
  3304. end;
  3305. end;
  3306. {$endif}
  3307. end.