nadd.pas 134 KB

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