2
0

nadd.pas 131 KB

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