2
0

nadd.pas 128 KB

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