nadd.pas 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196
  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,cordconstnode.create(tpointerdef(rd).pointeddef.size,sinttype,false));
  1622. end;
  1623. resultdef:=sinttype;
  1624. exit;
  1625. end;
  1626. else
  1627. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1628. end;
  1629. end
  1630. { is one of the operands a string?,
  1631. chararrays are also handled as strings (after conversion), also take
  1632. care of chararray+chararray and chararray+char.
  1633. Note: Must be done after pointerdef+pointerdef has been checked, else
  1634. pchar is converted to string }
  1635. else if (rd.typ=stringdef) or
  1636. (ld.typ=stringdef) or
  1637. { stringconstn's can be arraydefs }
  1638. (lt=stringconstn) or
  1639. (rt=stringconstn) or
  1640. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1641. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1642. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1643. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1644. begin
  1645. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1646. begin
  1647. { Is there a unicodestring? }
  1648. if is_unicodestring(rd) or is_unicodestring(ld) or
  1649. ((m_default_unicodestring in current_settings.modeswitches) and
  1650. (cs_refcountedstrings in current_settings.localswitches) and
  1651. (
  1652. is_pwidechar(rd) or is_widechararray(rd) or is_open_widechararray(rd) or (lt = stringconstn) or
  1653. is_pwidechar(ld) or is_widechararray(ld) or is_open_widechararray(ld) or (rt = stringconstn)
  1654. )
  1655. ) then
  1656. strtype:=st_unicodestring
  1657. else
  1658. { Is there a widestring? }
  1659. if is_widestring(rd) or is_widestring(ld) or
  1660. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1661. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1662. strtype:=st_widestring
  1663. else
  1664. if is_ansistring(rd) or is_ansistring(ld) or
  1665. ((cs_refcountedstrings in current_settings.localswitches) and
  1666. //todo: Move some of this to longstring's then they are implemented?
  1667. (
  1668. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or (lt = stringconstn) or
  1669. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld) or (rt = stringconstn)
  1670. )
  1671. ) then
  1672. strtype:=st_ansistring
  1673. else
  1674. if is_longstring(rd) or is_longstring(ld) then
  1675. strtype:=st_longstring
  1676. else
  1677. begin
  1678. { TODO: todo: add a warning/hint here if one converting a too large array}
  1679. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1680. Note: Delphi halts with error if "array [0..xx] of char"
  1681. is assigned to ShortString and string length is less
  1682. then array size }
  1683. strtype:= st_shortstring;
  1684. end;
  1685. // Now convert nodes to common string type
  1686. case strtype of
  1687. st_widestring :
  1688. begin
  1689. if not(is_widestring(rd)) then
  1690. inserttypeconv(right,cwidestringtype);
  1691. if not(is_widestring(ld)) then
  1692. inserttypeconv(left,cwidestringtype);
  1693. end;
  1694. st_unicodestring :
  1695. begin
  1696. if not(is_unicodestring(rd)) then
  1697. inserttypeconv(right,cunicodestringtype);
  1698. if not(is_unicodestring(ld)) then
  1699. inserttypeconv(left,cunicodestringtype);
  1700. end;
  1701. st_ansistring :
  1702. begin
  1703. { use same code page if possible (don't force same code
  1704. page in case both are ansistrings with code page <>
  1705. CP_NONE, since then data loss can occur (the ansistring
  1706. helpers will convert them at run time to an encoding
  1707. that can represent both encodings) }
  1708. if is_ansistring(ld) and
  1709. (tstringdef(ld).encoding<>0) and
  1710. (tstringdef(ld).encoding<>globals.CP_NONE) and
  1711. (not is_ansistring(rd) or
  1712. (tstringdef(rd).encoding=0) or
  1713. (tstringdef(rd).encoding=globals.CP_NONE)) then
  1714. inserttypeconv(right,ld)
  1715. else if is_ansistring(rd) and
  1716. (tstringdef(rd).encoding<>0) and
  1717. (tstringdef(rd).encoding<>globals.CP_NONE) and
  1718. (not is_ansistring(ld) or
  1719. (tstringdef(ld).encoding=0) or
  1720. (tstringdef(ld).encoding=globals.CP_NONE)) then
  1721. inserttypeconv(left,rd)
  1722. else
  1723. begin
  1724. if not is_ansistring(ld) then
  1725. inserttypeconv(left,getansistringdef);
  1726. if not is_ansistring(rd) then
  1727. inserttypeconv(right,getansistringdef);
  1728. end;
  1729. end;
  1730. st_longstring :
  1731. begin
  1732. if not(is_longstring(rd)) then
  1733. inserttypeconv(right,clongstringtype);
  1734. if not(is_longstring(ld)) then
  1735. inserttypeconv(left,clongstringtype);
  1736. end;
  1737. st_shortstring :
  1738. begin
  1739. if not(is_shortstring(ld)) then
  1740. inserttypeconv(left,cshortstringtype);
  1741. { don't convert char, that can be handled by the optimized node }
  1742. if not(is_shortstring(rd) or is_char(rd)) then
  1743. inserttypeconv(right,cshortstringtype);
  1744. end;
  1745. else
  1746. internalerror(2005101);
  1747. end;
  1748. end
  1749. else
  1750. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1751. end
  1752. { implicit pointer object type comparison }
  1753. else if is_implicit_pointer_object_type(rd) or is_implicit_pointer_object_type(ld) then
  1754. begin
  1755. if (nodetype in [equaln,unequaln]) then
  1756. begin
  1757. if is_implicit_pointer_object_type(rd) and is_implicit_pointer_object_type(ld) then
  1758. begin
  1759. if def_is_related(tobjectdef(rd),tobjectdef(ld)) then
  1760. inserttypeconv(right,left.resultdef)
  1761. else
  1762. inserttypeconv(left,right.resultdef);
  1763. end
  1764. else if is_implicit_pointer_object_type(rd) then
  1765. inserttypeconv(left,right.resultdef)
  1766. else
  1767. inserttypeconv(right,left.resultdef);
  1768. end
  1769. else
  1770. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1771. end
  1772. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  1773. begin
  1774. if (nodetype in [equaln,unequaln]) then
  1775. begin
  1776. if def_is_related(tobjectdef(tclassrefdef(rd).pointeddef),
  1777. tobjectdef(tclassrefdef(ld).pointeddef)) then
  1778. inserttypeconv(right,left.resultdef)
  1779. else
  1780. inserttypeconv(left,right.resultdef);
  1781. end
  1782. else
  1783. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1784. end
  1785. { allow comparison with nil pointer }
  1786. else if is_implicit_pointer_object_type(rd) or (rd.typ=classrefdef) then
  1787. begin
  1788. if (nodetype in [equaln,unequaln]) then
  1789. inserttypeconv(left,right.resultdef)
  1790. else
  1791. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1792. end
  1793. else if is_implicit_pointer_object_type(ld) or (ld.typ=classrefdef) then
  1794. begin
  1795. if (nodetype in [equaln,unequaln]) then
  1796. inserttypeconv(right,left.resultdef)
  1797. else
  1798. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1799. end
  1800. { support procvar=nil,procvar<>nil }
  1801. else if ((ld.typ=procvardef) and (rt=niln)) or
  1802. ((rd.typ=procvardef) and (lt=niln)) then
  1803. begin
  1804. if not(nodetype in [equaln,unequaln]) then
  1805. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1806. { find proc field in methodpointer record }
  1807. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1808. if not assigned(hsym) then
  1809. internalerror(200412043);
  1810. { For methodpointers compare only tmethodpointer.proc }
  1811. if (rd.typ=procvardef) and
  1812. (not tprocvardef(rd).is_addressonly) then
  1813. begin
  1814. right:=csubscriptnode.create(
  1815. hsym,
  1816. ctypeconvnode.create_internal(right,methodpointertype));
  1817. typecheckpass(right);
  1818. end;
  1819. if (ld.typ=procvardef) and
  1820. (not tprocvardef(ld).is_addressonly) then
  1821. begin
  1822. left:=csubscriptnode.create(
  1823. hsym,
  1824. ctypeconvnode.create_internal(left,methodpointertype));
  1825. typecheckpass(left);
  1826. end;
  1827. end
  1828. { support dynamicarray=nil,dynamicarray<>nil }
  1829. else if (is_dynamic_array(ld) and (rt=niln)) or
  1830. (is_dynamic_array(rd) and (lt=niln)) or
  1831. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1832. begin
  1833. if not(nodetype in [equaln,unequaln]) then
  1834. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1835. end
  1836. {$ifdef SUPPORT_MMX}
  1837. { mmx support, this must be before the zero based array
  1838. check }
  1839. else if (cs_mmx in current_settings.localswitches) and
  1840. is_mmx_able_array(ld) and
  1841. is_mmx_able_array(rd) and
  1842. equal_defs(ld,rd) then
  1843. begin
  1844. case nodetype of
  1845. addn,subn,xorn,orn,andn:
  1846. ;
  1847. { mul is a little bit restricted }
  1848. muln:
  1849. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1850. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1851. else
  1852. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1853. end;
  1854. end
  1855. {$endif SUPPORT_MMX}
  1856. { vector support, this must be before the zero based array
  1857. check }
  1858. else if (cs_support_vectors in current_settings.globalswitches) and
  1859. is_vector(ld) and
  1860. is_vector(rd) and
  1861. equal_defs(ld,rd) then
  1862. begin
  1863. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  1864. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1865. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  1866. resultdef:=left.resultdef;
  1867. end
  1868. { this is a little bit dangerous, also the left type }
  1869. { pointer to should be checked! This broke the mmx support }
  1870. else if (rd.typ=pointerdef) or
  1871. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  1872. begin
  1873. if is_zero_based_array(rd) then
  1874. begin
  1875. resultdef:=getpointerdef(tarraydef(rd).elementdef);
  1876. inserttypeconv(right,resultdef);
  1877. end
  1878. else
  1879. resultdef:=right.resultdef;
  1880. inserttypeconv(left,get_int_type_for_pointer_arithmetic(rd));
  1881. if nodetype=addn then
  1882. begin
  1883. if not(cs_extsyntax in current_settings.moduleswitches) or
  1884. (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
  1885. not(cs_pointermath in current_settings.localswitches) and
  1886. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  1887. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1888. if (rd.typ=pointerdef) and
  1889. (tpointerdef(rd).pointeddef.size>1) then
  1890. begin
  1891. left:=caddnode.create(muln,left,
  1892. cordconstnode.create(tpointerdef(rd).pointeddef.size,get_int_type_for_pointer_arithmetic(rd),true));
  1893. typecheckpass(left);
  1894. end;
  1895. end
  1896. else
  1897. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1898. end
  1899. else if (ld.typ=pointerdef) or
  1900. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  1901. begin
  1902. if is_zero_based_array(ld) then
  1903. begin
  1904. resultdef:=getpointerdef(tarraydef(ld).elementdef);
  1905. inserttypeconv(left,resultdef);
  1906. end
  1907. else
  1908. resultdef:=left.resultdef;
  1909. inserttypeconv(right,get_int_type_for_pointer_arithmetic(ld));
  1910. if nodetype in [addn,subn] then
  1911. begin
  1912. if (lt=niln) then
  1913. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),'NIL',rd.typename);
  1914. if not(cs_extsyntax in current_settings.moduleswitches) or
  1915. (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
  1916. not(cs_pointermath in current_settings.localswitches) and
  1917. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  1918. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1919. if (ld.typ=pointerdef) then
  1920. begin
  1921. if is_big_untyped_addrnode(left) then
  1922. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1923. if (tpointerdef(ld).pointeddef.size>1) then
  1924. begin
  1925. right:=caddnode.create(muln,right,
  1926. cordconstnode.create(tpointerdef(ld).pointeddef.size,get_int_type_for_pointer_arithmetic(ld),true));
  1927. typecheckpass(right);
  1928. end
  1929. end else
  1930. if is_zero_based_array(ld) and
  1931. (tarraydef(ld).elementdef.size>1) then
  1932. begin
  1933. right:=caddnode.create(muln,right,
  1934. cordconstnode.create(tarraydef(ld).elementdef.size,get_int_type_for_pointer_arithmetic(ld),true));
  1935. typecheckpass(right);
  1936. end;
  1937. end
  1938. else
  1939. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1940. end
  1941. else if (rd.typ=procvardef) and
  1942. (ld.typ=procvardef) and
  1943. equal_defs(rd,ld) then
  1944. begin
  1945. if (nodetype in [equaln,unequaln]) then
  1946. begin
  1947. if tprocvardef(rd).is_addressonly then
  1948. begin
  1949. inserttypeconv_internal(right,voidpointertype);
  1950. inserttypeconv_internal(left,voidpointertype);
  1951. end
  1952. else
  1953. begin
  1954. { find proc field in methodpointer record }
  1955. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1956. if not assigned(hsym) then
  1957. internalerror(200412043);
  1958. { Compare tmehodpointer(left).proc }
  1959. right:=csubscriptnode.create(
  1960. hsym,
  1961. ctypeconvnode.create_internal(right,methodpointertype));
  1962. typecheckpass(right);
  1963. left:=csubscriptnode.create(
  1964. hsym,
  1965. ctypeconvnode.create_internal(left,methodpointertype));
  1966. typecheckpass(left);
  1967. end;
  1968. end
  1969. else
  1970. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1971. end
  1972. { enums }
  1973. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  1974. begin
  1975. if allowenumop(nodetype) then
  1976. inserttypeconv(right,left.resultdef)
  1977. else
  1978. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1979. end
  1980. { generic conversion, this is for error recovery }
  1981. else
  1982. begin
  1983. inserttypeconv(left,sinttype);
  1984. inserttypeconv(right,sinttype);
  1985. end;
  1986. if cmp_of_disjunct_ranges(res) then
  1987. begin
  1988. if res then
  1989. CGMessage(type_w_comparison_always_true)
  1990. else
  1991. CGMessage(type_w_comparison_always_false);
  1992. end;
  1993. { set resultdef if not already done }
  1994. if not assigned(resultdef) then
  1995. begin
  1996. case nodetype of
  1997. ltn,lten,gtn,gten,equaln,unequaln :
  1998. resultdef:=pasbool8type;
  1999. slashn :
  2000. resultdef:=resultrealdef;
  2001. addn:
  2002. begin
  2003. { for strings, return is always a 255 char string }
  2004. if is_shortstring(left.resultdef) then
  2005. resultdef:=cshortstringtype
  2006. else
  2007. { for ansistrings set resultdef to assignment left node
  2008. if it is an assignment and left node expects ansistring }
  2009. if is_ansistring(left.resultdef) and
  2010. assigned(aktassignmentnode) and
  2011. (aktassignmentnode.right=self) and
  2012. is_ansistring(aktassignmentnode.left.resultdef) then
  2013. resultdef:=aktassignmentnode.left.resultdef
  2014. else
  2015. resultdef:=left.resultdef;
  2016. end;
  2017. else
  2018. resultdef:=left.resultdef;
  2019. end;
  2020. end;
  2021. { when the result is currency we need some extra code for
  2022. multiplication and division. this should not be done when
  2023. the muln or slashn node is created internally }
  2024. if not(nf_is_currency in flags) and
  2025. is_currency(resultdef) then
  2026. begin
  2027. case nodetype of
  2028. slashn :
  2029. begin
  2030. { slashn will only work with floats }
  2031. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  2032. include(hp.flags,nf_is_currency);
  2033. result:=hp;
  2034. end;
  2035. muln :
  2036. begin
  2037. if s64currencytype.typ=floatdef then
  2038. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  2039. else
  2040. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  2041. include(hp.flags,nf_is_currency);
  2042. result:=hp
  2043. end;
  2044. end;
  2045. end;
  2046. if not codegenerror and
  2047. not assigned(result) then
  2048. result:=simplify(false);
  2049. end;
  2050. function taddnode.first_addstring: tnode;
  2051. const
  2052. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  2053. var
  2054. p: tnode;
  2055. newstatement : tstatementnode;
  2056. tempnode (*,tempnode2*) : ttempcreatenode;
  2057. cmpfuncname: string;
  2058. para: tcallparanode;
  2059. begin
  2060. result:=nil;
  2061. { when we get here, we are sure that both the left and the right }
  2062. { node are both strings of the same stringtype (JM) }
  2063. case nodetype of
  2064. addn:
  2065. begin
  2066. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  2067. begin
  2068. result:=right;
  2069. left.free;
  2070. left:=nil;
  2071. right:=nil;
  2072. exit;
  2073. end;
  2074. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  2075. begin
  2076. result:=left;
  2077. left:=nil;
  2078. right.free;
  2079. right:=nil;
  2080. exit;
  2081. end;
  2082. { create the call to the concat routine both strings as arguments }
  2083. if assigned(aktassignmentnode) and
  2084. (aktassignmentnode.right=self) and
  2085. (aktassignmentnode.left.resultdef=resultdef) and
  2086. valid_for_var(aktassignmentnode.left,false) then
  2087. begin
  2088. para:=ccallparanode.create(
  2089. right,
  2090. ccallparanode.create(
  2091. left,
  2092. ccallparanode.create(aktassignmentnode.left.getcopy,nil)
  2093. )
  2094. );
  2095. if is_ansistring(resultdef) then
  2096. para:=ccallparanode.create(
  2097. cordconstnode.create(
  2098. { don't use getparaencoding(), we have to know
  2099. when the result is rawbytestring }
  2100. tstringdef(resultdef).encoding,
  2101. u16inttype,
  2102. true
  2103. ),
  2104. para
  2105. );
  2106. result:=ccallnode.createintern(
  2107. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2108. para
  2109. );
  2110. include(aktassignmentnode.flags,nf_assign_done_in_right);
  2111. firstpass(result);
  2112. end
  2113. else
  2114. begin
  2115. result:=internalstatements(newstatement);
  2116. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2117. addstatement(newstatement,tempnode);
  2118. { initialize the temp, since it will be passed to a
  2119. var-parameter (and finalization, which is performed by the
  2120. ttempcreate node and which takes care of the initialization
  2121. on native targets, is a noop on managed VM targets) }
  2122. if (target_info.system in systems_managed_vm) and
  2123. is_managed_type(resultdef) then
  2124. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  2125. false,
  2126. ccallparanode.create(genintconstnode(0),
  2127. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  2128. para:=ccallparanode.create(
  2129. right,
  2130. ccallparanode.create(
  2131. left,
  2132. ccallparanode.create(ctemprefnode.create(tempnode),nil)
  2133. )
  2134. );
  2135. if is_ansistring(resultdef) then
  2136. para:=ccallparanode.create(
  2137. cordconstnode.create(
  2138. { don't use getparaencoding(), we have to know
  2139. when the result is rawbytestring }
  2140. tstringdef(resultdef).encoding,
  2141. u16inttype,
  2142. true
  2143. ),
  2144. para
  2145. );
  2146. addstatement(
  2147. newstatement,
  2148. ccallnode.createintern(
  2149. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2150. para
  2151. )
  2152. );
  2153. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2154. addstatement(newstatement,ctemprefnode.create(tempnode));
  2155. end;
  2156. { we reused the arguments }
  2157. left := nil;
  2158. right := nil;
  2159. end;
  2160. ltn,lten,gtn,gten,equaln,unequaln :
  2161. begin
  2162. { generate better code for comparison with empty string, we
  2163. only need to compare the length with 0 }
  2164. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  2165. { windows widestrings are too complicated to be handled optimized }
  2166. not(is_widestring(left.resultdef) and (target_info.system in systems_windows)) and
  2167. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  2168. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  2169. begin
  2170. { switch so that the constant is always on the right }
  2171. if left.nodetype = stringconstn then
  2172. begin
  2173. p := left;
  2174. left := right;
  2175. right := p;
  2176. nodetype:=swap_relation[nodetype];
  2177. end;
  2178. if is_shortstring(left.resultdef) or
  2179. (nodetype in [gtn,gten,ltn,lten]) or
  2180. (target_info.system in systems_managed_vm) then
  2181. { compare the length with 0 }
  2182. result := caddnode.create(nodetype,
  2183. cinlinenode.create(in_length_x,false,left),
  2184. cordconstnode.create(0,s32inttype,false))
  2185. else
  2186. begin
  2187. (*
  2188. if is_widestring(left.resultdef) and
  2189. (target_info.system in system_windows) then
  2190. begin
  2191. { windows like widestrings requires that we also check the length }
  2192. result:=internalstatements(newstatement);
  2193. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2194. tempnode2:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2195. addstatement(newstatement,tempnode);
  2196. addstatement(newstatement,tempnode2);
  2197. { poor man's cse }
  2198. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2199. ctypeconvnode.create_internal(left,voidpointertype))
  2200. );
  2201. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode2),
  2202. caddnode.create(orn,
  2203. caddnode.create(nodetype,
  2204. ctemprefnode.create(tempnode),
  2205. cpointerconstnode.create(0,voidpointertype)
  2206. ),
  2207. caddnode.create(nodetype,
  2208. ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),s32inttype),
  2209. cordconstnode.create(0,s32inttype,false)
  2210. )
  2211. )
  2212. ));
  2213. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2214. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode2));
  2215. addstatement(newstatement,ctemprefnode.create(tempnode2));
  2216. end
  2217. else
  2218. *)
  2219. begin
  2220. { compare the pointer with nil (for ansistrings etc), }
  2221. { faster than getting the length (JM) }
  2222. result:= caddnode.create(nodetype,
  2223. ctypeconvnode.create_internal(left,voidpointertype),
  2224. cpointerconstnode.create(0,voidpointertype));
  2225. end;
  2226. end;
  2227. { left is reused }
  2228. left := nil;
  2229. { right isn't }
  2230. right.free;
  2231. right := nil;
  2232. exit;
  2233. end;
  2234. { no string constant -> call compare routine }
  2235. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  2236. { for equality checks use optimized version }
  2237. if nodetype in [equaln,unequaln] then
  2238. cmpfuncname := cmpfuncname + '_equal';
  2239. result := ccallnode.createintern(cmpfuncname,
  2240. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2241. { and compare its result with 0 according to the original operator }
  2242. result := caddnode.create(nodetype,result,
  2243. cordconstnode.create(0,s32inttype,false));
  2244. left := nil;
  2245. right := nil;
  2246. end;
  2247. end;
  2248. end;
  2249. function taddnode.first_addset : tnode;
  2250. procedure call_varset_helper(const n : string);
  2251. var
  2252. newstatement : tstatementnode;
  2253. temp : ttempcreatenode;
  2254. begin
  2255. { add two var sets }
  2256. result:=internalstatements(newstatement);
  2257. { create temp for result }
  2258. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2259. addstatement(newstatement,temp);
  2260. addstatement(newstatement,ccallnode.createintern(n,
  2261. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2262. ccallparanode.create(ctemprefnode.create(temp),
  2263. ccallparanode.create(right,
  2264. ccallparanode.create(left,nil)))))
  2265. );
  2266. { remove reused parts from original node }
  2267. left:=nil;
  2268. right:=nil;
  2269. { the last statement should return the value as
  2270. location and type, this is done be referencing the
  2271. temp and converting it first from a persistent temp to
  2272. normal temp }
  2273. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2274. addstatement(newstatement,ctemprefnode.create(temp));
  2275. end;
  2276. var
  2277. procname: string[31];
  2278. tempn: tnode;
  2279. newstatement : tstatementnode;
  2280. temp : ttempcreatenode;
  2281. begin
  2282. result:=nil;
  2283. case nodetype of
  2284. equaln,unequaln,lten,gten:
  2285. begin
  2286. case nodetype of
  2287. equaln,unequaln:
  2288. procname := 'fpc_varset_comp_sets';
  2289. lten,gten:
  2290. begin
  2291. procname := 'fpc_varset_contains_sets';
  2292. { (left >= right) = (right <= left) }
  2293. if nodetype = gten then
  2294. begin
  2295. tempn := left;
  2296. left := right;
  2297. right := tempn;
  2298. end;
  2299. end;
  2300. else
  2301. internalerror(2013112911);
  2302. end;
  2303. result := ccallnode.createinternres(procname,
  2304. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2305. ccallparanode.create(right,
  2306. ccallparanode.create(left,nil))),resultdef);
  2307. { left and right are reused as parameters }
  2308. left := nil;
  2309. right := nil;
  2310. { for an unequaln, we have to negate the result of comp_sets }
  2311. if nodetype = unequaln then
  2312. result := cnotnode.create(result);
  2313. end;
  2314. addn:
  2315. begin
  2316. { optimize first loading of a set }
  2317. if (right.nodetype=setelementn) and
  2318. not(assigned(tsetelementnode(right).right)) and
  2319. is_emptyset(left) then
  2320. begin
  2321. result:=internalstatements(newstatement);
  2322. { create temp for result }
  2323. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2324. addstatement(newstatement,temp);
  2325. { adjust for set base }
  2326. tsetelementnode(right).left:=caddnode.create(subn,
  2327. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2328. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2329. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  2330. ccallparanode.create(ctemprefnode.create(temp),
  2331. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2332. ccallparanode.create(tsetelementnode(right).left,nil))))
  2333. );
  2334. { the last statement should return the value as
  2335. location and type, this is done be referencing the
  2336. temp and converting it first from a persistent temp to
  2337. normal temp }
  2338. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2339. addstatement(newstatement,ctemprefnode.create(temp));
  2340. tsetelementnode(right).left := nil;
  2341. end
  2342. else
  2343. begin
  2344. if right.nodetype=setelementn then
  2345. begin
  2346. result:=internalstatements(newstatement);
  2347. { create temp for result }
  2348. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2349. addstatement(newstatement,temp);
  2350. { adjust for set base }
  2351. tsetelementnode(right).left:=caddnode.create(subn,
  2352. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2353. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2354. { add a range or a single element? }
  2355. if assigned(tsetelementnode(right).right) then
  2356. begin
  2357. { adjust for set base }
  2358. tsetelementnode(right).right:=caddnode.create(subn,
  2359. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  2360. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2361. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  2362. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2363. ccallparanode.create(tsetelementnode(right).right,
  2364. ccallparanode.create(tsetelementnode(right).left,
  2365. ccallparanode.create(ctemprefnode.create(temp),
  2366. ccallparanode.create(left,nil))))))
  2367. );
  2368. end
  2369. else
  2370. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  2371. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2372. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2373. ccallparanode.create(ctemprefnode.create(temp),
  2374. ccallparanode.create(left,nil)))))
  2375. );
  2376. { remove reused parts from original node }
  2377. tsetelementnode(right).right:=nil;
  2378. tsetelementnode(right).left:=nil;
  2379. left:=nil;
  2380. { the last statement should return the value as
  2381. location and type, this is done be referencing the
  2382. temp and converting it first from a persistent temp to
  2383. normal temp }
  2384. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2385. addstatement(newstatement,ctemprefnode.create(temp));
  2386. end
  2387. else
  2388. call_varset_helper('fpc_varset_add_sets');
  2389. end
  2390. end;
  2391. subn:
  2392. call_varset_helper('fpc_varset_sub_sets');
  2393. symdifn:
  2394. call_varset_helper('fpc_varset_symdif_sets');
  2395. muln:
  2396. call_varset_helper('fpc_varset_mul_sets');
  2397. else
  2398. internalerror(200609241);
  2399. end;
  2400. end;
  2401. function taddnode.use_generic_mul32to64: boolean;
  2402. begin
  2403. result := true;
  2404. end;
  2405. function taddnode.use_generic_mul64bit: boolean;
  2406. begin
  2407. result := true;
  2408. end;
  2409. function taddnode.try_make_mul32to64: boolean;
  2410. function canbe32bitint(v: tconstexprint): boolean;
  2411. begin
  2412. result := ((v >= int64(low(longint))) and (v <= int64(high(longint)))) or
  2413. ((v >= qword(low(cardinal))) and (v <= qword(high(cardinal))))
  2414. end;
  2415. function is_32bitordconst(n: tnode): boolean;
  2416. begin
  2417. result := (n.nodetype = ordconstn) and
  2418. canbe32bitint(tordconstnode(n).value);
  2419. end;
  2420. function is_32to64typeconv(n: tnode): boolean;
  2421. begin
  2422. result := (n.nodetype = typeconvn) and
  2423. is_integer(ttypeconvnode(n).left.resultdef) and
  2424. not is_64bit(ttypeconvnode(n).left.resultdef);
  2425. end;
  2426. var
  2427. temp: tnode;
  2428. begin
  2429. result := false;
  2430. if is_32to64typeconv(left) and
  2431. (is_32bitordconst(right) or
  2432. is_32to64typeconv(right) and
  2433. ((is_signed(ttypeconvnode(left).left.resultdef) =
  2434. is_signed(ttypeconvnode(right).left.resultdef)) or
  2435. (is_signed(ttypeconvnode(left).left.resultdef) and
  2436. (torddef(ttypeconvnode(right).left.resultdef).ordtype in [u8bit,u16bit])))) then
  2437. begin
  2438. temp := ttypeconvnode(left).left;
  2439. ttypeconvnode(left).left := nil;
  2440. left.free;
  2441. left := temp;
  2442. if (right.nodetype = typeconvn) then
  2443. begin
  2444. temp := ttypeconvnode(right).left;
  2445. ttypeconvnode(right).left := nil;
  2446. right.free;
  2447. right := temp;
  2448. end;
  2449. if (is_signed(left.resultdef)) then
  2450. begin
  2451. inserttypeconv_internal(left,s32inttype);
  2452. inserttypeconv_internal(right,s32inttype);
  2453. end
  2454. else
  2455. begin
  2456. inserttypeconv_internal(left,u32inttype);
  2457. inserttypeconv_internal(right,u32inttype);
  2458. end;
  2459. firstpass(left);
  2460. firstpass(right);
  2461. result := true;
  2462. end;
  2463. end;
  2464. function taddnode.first_add64bitint: tnode;
  2465. var
  2466. procname: string[31];
  2467. temp: tnode;
  2468. power: longint;
  2469. begin
  2470. result := nil;
  2471. { create helper calls mul }
  2472. if nodetype <> muln then
  2473. exit;
  2474. { make sure that if there is a constant, that it's on the right }
  2475. if left.nodetype = ordconstn then
  2476. begin
  2477. temp := right;
  2478. right := left;
  2479. left := temp;
  2480. end;
  2481. { can we use a shift instead of a mul? }
  2482. if not (cs_check_overflow in current_settings.localswitches) and
  2483. (right.nodetype = ordconstn) and
  2484. ispowerof2(tordconstnode(right).value,power) then
  2485. begin
  2486. tordconstnode(right).value := power;
  2487. result := cshlshrnode.create(shln,left,right);
  2488. { left and right are reused }
  2489. left := nil;
  2490. right := nil;
  2491. { return firstpassed new node }
  2492. exit;
  2493. end;
  2494. if try_make_mul32to64 then
  2495. begin
  2496. { if the code generator can handle 32 to 64-bit muls, we're done here }
  2497. if not use_generic_mul32to64 then
  2498. exit;
  2499. { this uses the same criteria for signedness as the 32 to 64-bit mul
  2500. handling in the i386 code generator }
  2501. if is_signed(left.resultdef) and is_signed(right.resultdef) then
  2502. procname := 'fpc_mul_longint_to_int64'
  2503. else
  2504. procname := 'fpc_mul_dword_to_qword';
  2505. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  2506. result := ccallnode.createintern(procname,right);
  2507. left := nil;
  2508. right := nil;
  2509. end
  2510. else
  2511. begin
  2512. { can full 64-bit multiplication be handled inline? }
  2513. if not use_generic_mul64bit then
  2514. begin
  2515. { generic handling replaces this node with call to fpc_mul_int64,
  2516. whose result is int64 }
  2517. if is_currency(resultdef) then
  2518. resultdef:=s64inttype;
  2519. exit;
  2520. end;
  2521. { when currency is used set the result of the
  2522. parameters to s64bit, so they are not converted }
  2523. if is_currency(resultdef) then
  2524. begin
  2525. left.resultdef:=s64inttype;
  2526. right.resultdef:=s64inttype;
  2527. end;
  2528. { otherwise, create the parameters for the helper }
  2529. right := ccallparanode.create(
  2530. cordconstnode.create(ord(cs_check_overflow in current_settings.localswitches),pasbool8type,true),
  2531. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2532. left := nil;
  2533. { only qword needs the unsigned code, the
  2534. signed code is also used for currency }
  2535. if is_signed(resultdef) then
  2536. procname := 'fpc_mul_int64'
  2537. else
  2538. procname := 'fpc_mul_qword';
  2539. result := ccallnode.createintern(procname,right);
  2540. right := nil;
  2541. end;
  2542. end;
  2543. function taddnode.first_addpointer: tnode;
  2544. begin
  2545. result:=nil;
  2546. expectloc:=LOC_REGISTER;
  2547. end;
  2548. function taddnode.first_cmppointer: tnode;
  2549. begin
  2550. result:=nil;
  2551. expectloc:=LOC_FLAGS;
  2552. end;
  2553. function taddnode.first_addfloat : tnode;
  2554. var
  2555. procname: string[31];
  2556. { do we need to reverse the result ? }
  2557. notnode : boolean;
  2558. fdef : tdef;
  2559. begin
  2560. result := nil;
  2561. notnode := false;
  2562. fdef := nil;
  2563. { In non-emulation mode, real opcodes are
  2564. emitted for floating point values.
  2565. }
  2566. if not ((cs_fp_emulation in current_settings.moduleswitches)
  2567. {$ifdef cpufpemu}
  2568. or (current_settings.fputype=fpu_soft)
  2569. {$endif cpufpemu}
  2570. ) then
  2571. exit;
  2572. if not(target_info.system in systems_wince) then
  2573. begin
  2574. case tfloatdef(left.resultdef).floattype of
  2575. s32real:
  2576. begin
  2577. fdef:=search_system_type('FLOAT32REC').typedef;
  2578. procname:='float32';
  2579. end;
  2580. s64real:
  2581. begin
  2582. fdef:=search_system_type('FLOAT64').typedef;
  2583. procname:='float64';
  2584. end;
  2585. {!!! not yet implemented
  2586. s128real:
  2587. }
  2588. else
  2589. internalerror(2005082601);
  2590. end;
  2591. case nodetype of
  2592. addn:
  2593. procname:=procname+'_add';
  2594. muln:
  2595. procname:=procname+'_mul';
  2596. subn:
  2597. procname:=procname+'_sub';
  2598. slashn:
  2599. procname:=procname+'_div';
  2600. ltn:
  2601. procname:=procname+'_lt';
  2602. lten:
  2603. procname:=procname+'_le';
  2604. gtn:
  2605. begin
  2606. procname:=procname+'_lt';
  2607. swapleftright;
  2608. end;
  2609. gten:
  2610. begin
  2611. procname:=procname+'_le';
  2612. swapleftright;
  2613. end;
  2614. equaln:
  2615. procname:=procname+'_eq';
  2616. unequaln:
  2617. begin
  2618. procname:=procname+'_eq';
  2619. notnode:=true;
  2620. end;
  2621. else
  2622. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2623. end;
  2624. end
  2625. else
  2626. begin
  2627. case nodetype of
  2628. addn:
  2629. procname:='ADD';
  2630. muln:
  2631. procname:='MUL';
  2632. subn:
  2633. procname:='SUB';
  2634. slashn:
  2635. procname:='DIV';
  2636. ltn:
  2637. procname:='LT';
  2638. lten:
  2639. procname:='LE';
  2640. gtn:
  2641. procname:='GT';
  2642. gten:
  2643. procname:='GE';
  2644. equaln:
  2645. procname:='EQ';
  2646. unequaln:
  2647. procname:='NE';
  2648. else
  2649. begin
  2650. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2651. exit;
  2652. end;
  2653. end;
  2654. case tfloatdef(left.resultdef).floattype of
  2655. s32real:
  2656. begin
  2657. procname:=procname+'S';
  2658. if nodetype in [addn,muln,subn,slashn] then
  2659. procname:=lower(procname);
  2660. end;
  2661. s64real:
  2662. procname:=procname+'D';
  2663. {!!! not yet implemented
  2664. s128real:
  2665. }
  2666. else
  2667. internalerror(2005082602);
  2668. end;
  2669. end;
  2670. { cast softfpu result? }
  2671. if not(target_info.system in systems_wince) then
  2672. begin
  2673. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2674. resultdef:=pasbool8type;
  2675. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  2676. ctypeconvnode.create_internal(right,fdef),
  2677. ccallparanode.create(
  2678. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  2679. end
  2680. else
  2681. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  2682. ccallparanode.create(left,nil)));
  2683. left:=nil;
  2684. right:=nil;
  2685. { do we need to reverse the result }
  2686. if notnode then
  2687. result:=cnotnode.create(result);
  2688. end;
  2689. function taddnode.pass_1 : tnode;
  2690. var
  2691. {$ifdef addstringopt}
  2692. hp : tnode;
  2693. {$endif addstringopt}
  2694. rd,ld : tdef;
  2695. i,i2 : longint;
  2696. lt,rt : tnodetype;
  2697. {$ifdef cpuneedsmulhelper}
  2698. procname : string[32];
  2699. {$endif cpuneedsmulhelper}
  2700. begin
  2701. result:=nil;
  2702. { Can we optimize multiple string additions into a single call?
  2703. This need to be done on a complete tree to detect the multiple
  2704. add nodes and is therefor done before the subtrees are processed }
  2705. if canbemultistringadd(self) then
  2706. begin
  2707. result:=genmultistringadd(self);
  2708. exit;
  2709. end;
  2710. { first do the two subtrees }
  2711. firstpass(left);
  2712. firstpass(right);
  2713. if codegenerror then
  2714. exit;
  2715. { load easier access variables }
  2716. rd:=right.resultdef;
  2717. ld:=left.resultdef;
  2718. rt:=right.nodetype;
  2719. lt:=left.nodetype;
  2720. { int/int gives real/real! }
  2721. if nodetype=slashn then
  2722. begin
  2723. {$ifdef cpufpemu}
  2724. result:=first_addfloat;
  2725. if assigned(result) then
  2726. exit;
  2727. {$endif cpufpemu}
  2728. expectloc:=LOC_FPUREGISTER;
  2729. end
  2730. { if both are orddefs then check sub types }
  2731. else if (ld.typ=orddef) and (rd.typ=orddef) then
  2732. begin
  2733. { optimize multiplacation by a power of 2 }
  2734. if not(cs_check_overflow in current_settings.localswitches) and
  2735. (nodetype = muln) and
  2736. (((left.nodetype = ordconstn) and
  2737. ispowerof2(tordconstnode(left).value,i)) or
  2738. ((right.nodetype = ordconstn) and
  2739. ispowerof2(tordconstnode(right).value,i2))) then
  2740. begin
  2741. if ((left.nodetype = ordconstn) and
  2742. ispowerof2(tordconstnode(left).value,i)) then
  2743. begin
  2744. tordconstnode(left).value := i;
  2745. result := cshlshrnode.create(shln,right,left);
  2746. end
  2747. else
  2748. begin
  2749. tordconstnode(right).value := i2;
  2750. result := cshlshrnode.create(shln,left,right);
  2751. end;
  2752. result.resultdef := resultdef;
  2753. left := nil;
  2754. right := nil;
  2755. exit;
  2756. end;
  2757. { 2 booleans ? }
  2758. if is_boolean(ld) and is_boolean(rd) then
  2759. begin
  2760. if (not(cs_full_boolean_eval in current_settings.localswitches) or
  2761. (nf_short_bool in flags)) and
  2762. (nodetype in [andn,orn]) then
  2763. expectloc:=LOC_JUMP
  2764. else
  2765. begin
  2766. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2767. expectloc:=LOC_FLAGS
  2768. else
  2769. expectloc:=LOC_REGISTER;
  2770. end;
  2771. end
  2772. else
  2773. { Both are chars? only convert to shortstrings for addn }
  2774. if is_char(ld) then
  2775. begin
  2776. if nodetype=addn then
  2777. internalerror(200103291);
  2778. expectloc:=LOC_FLAGS;
  2779. end
  2780. {$ifndef cpu64bitalu}
  2781. { is there a 64 bit type ? }
  2782. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  2783. begin
  2784. result := first_add64bitint;
  2785. if assigned(result) then
  2786. exit;
  2787. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2788. expectloc:=LOC_REGISTER
  2789. else
  2790. expectloc:=LOC_JUMP;
  2791. end
  2792. {$endif cpu64bitalu}
  2793. { generic 32bit conversion }
  2794. else
  2795. begin
  2796. {$ifdef cpuneedsmulhelper}
  2797. if (nodetype=muln) and not(torddef(resultdef).ordtype in [u8bit,s8bit{$ifdef cpu16bitalu},u16bit,s16bit{$endif}]) then
  2798. begin
  2799. result := nil;
  2800. case torddef(resultdef).ordtype of
  2801. s16bit:
  2802. procname := 'fpc_mul_integer';
  2803. u16bit:
  2804. procname := 'fpc_mul_word';
  2805. s32bit:
  2806. procname := 'fpc_mul_longint';
  2807. u32bit:
  2808. procname := 'fpc_mul_dword';
  2809. else
  2810. internalerror(2011022301);
  2811. end;
  2812. result := ccallnode.createintern(procname,
  2813. ccallparanode.create(cordconstnode.create(ord(cs_check_overflow in current_settings.localswitches),pasbool8type,false),
  2814. ccallparanode.create(right,
  2815. ccallparanode.create(left,nil))));
  2816. left := nil;
  2817. right := nil;
  2818. firstpass(result);
  2819. exit;
  2820. end;
  2821. {$endif cpuneedsmulhelper}
  2822. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2823. expectloc:=LOC_REGISTER
  2824. else if torddef(ld).size>sizeof(aint) then
  2825. expectloc:=LOC_JUMP
  2826. else
  2827. expectloc:=LOC_FLAGS;
  2828. end;
  2829. end
  2830. { left side a setdef, must be before string processing,
  2831. else array constructor can be seen as array of char (PFV) }
  2832. else if (ld.typ=setdef) then
  2833. begin
  2834. { small sets are handled inline by the compiler.
  2835. small set doesn't have support for adding ranges }
  2836. if is_smallset(ld) and
  2837. not(
  2838. (right.nodetype=setelementn) and
  2839. assigned(tsetelementnode(right).right)
  2840. ) then
  2841. begin
  2842. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2843. expectloc:=LOC_FLAGS
  2844. else
  2845. expectloc:=LOC_REGISTER;
  2846. end
  2847. else
  2848. begin
  2849. result := first_addset;
  2850. if assigned(result) then
  2851. exit;
  2852. expectloc:=LOC_CREFERENCE;
  2853. end;
  2854. end
  2855. { compare pchar by addresses like BP/Delphi }
  2856. else if is_pchar(ld) then
  2857. begin
  2858. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2859. expectloc:=LOC_REGISTER
  2860. else
  2861. expectloc:=LOC_FLAGS;
  2862. end
  2863. { is one of the operands a string }
  2864. else if (ld.typ=stringdef) then
  2865. begin
  2866. if is_widestring(ld) then
  2867. begin
  2868. { this is only for add, the comparisaion is handled later }
  2869. expectloc:=LOC_REGISTER;
  2870. end
  2871. else if is_unicodestring(ld) then
  2872. begin
  2873. { this is only for add, the comparisaion is handled later }
  2874. expectloc:=LOC_REGISTER;
  2875. end
  2876. else if is_ansistring(ld) then
  2877. begin
  2878. { this is only for add, the comparisaion is handled later }
  2879. expectloc:=LOC_REGISTER;
  2880. end
  2881. else if is_longstring(ld) then
  2882. begin
  2883. { this is only for add, the comparisaion is handled later }
  2884. expectloc:=LOC_REFERENCE;
  2885. end
  2886. else
  2887. begin
  2888. {$ifdef addstringopt}
  2889. { can create a call which isn't handled by callparatemp }
  2890. if canbeaddsstringcharoptnode(self) then
  2891. begin
  2892. hp := genaddsstringcharoptnode(self);
  2893. pass_1 := hp;
  2894. exit;
  2895. end
  2896. else
  2897. {$endif addstringopt}
  2898. begin
  2899. { Fix right to be shortstring }
  2900. if is_char(right.resultdef) then
  2901. begin
  2902. inserttypeconv(right,cshortstringtype);
  2903. firstpass(right);
  2904. end;
  2905. end;
  2906. {$ifdef addstringopt}
  2907. { can create a call which isn't handled by callparatemp }
  2908. if canbeaddsstringcsstringoptnode(self) then
  2909. begin
  2910. hp := genaddsstringcsstringoptnode(self);
  2911. pass_1 := hp;
  2912. exit;
  2913. end;
  2914. {$endif addstringopt}
  2915. end;
  2916. { otherwise, let addstring convert everything }
  2917. result := first_addstring;
  2918. exit;
  2919. end
  2920. { is one a real float ? }
  2921. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  2922. begin
  2923. {$ifdef cpufpemu}
  2924. result:=first_addfloat;
  2925. if assigned(result) then
  2926. exit;
  2927. {$endif cpufpemu}
  2928. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2929. expectloc:=LOC_FPUREGISTER
  2930. else
  2931. expectloc:=LOC_FLAGS;
  2932. end
  2933. { pointer comperation and subtraction }
  2934. else if (ld.typ=pointerdef) then
  2935. begin
  2936. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2937. result:=first_addpointer
  2938. else
  2939. result:=first_cmppointer;
  2940. end
  2941. else if is_implicit_pointer_object_type(ld) then
  2942. begin
  2943. expectloc:=LOC_FLAGS;
  2944. end
  2945. else if (ld.typ=classrefdef) then
  2946. begin
  2947. expectloc:=LOC_FLAGS;
  2948. end
  2949. { support procvar=nil,procvar<>nil }
  2950. else if ((ld.typ=procvardef) and (rt=niln)) or
  2951. ((rd.typ=procvardef) and (lt=niln)) then
  2952. begin
  2953. expectloc:=LOC_FLAGS;
  2954. end
  2955. {$ifdef SUPPORT_MMX}
  2956. { mmx support, this must be before the zero based array
  2957. check }
  2958. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  2959. is_mmx_able_array(rd) then
  2960. begin
  2961. expectloc:=LOC_MMXREGISTER;
  2962. end
  2963. {$endif SUPPORT_MMX}
  2964. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  2965. begin
  2966. result:=first_addpointer;
  2967. end
  2968. else if (rd.typ=procvardef) and
  2969. (ld.typ=procvardef) and
  2970. equal_defs(rd,ld) then
  2971. begin
  2972. expectloc:=LOC_FLAGS;
  2973. end
  2974. else if (ld.typ=enumdef) then
  2975. begin
  2976. expectloc:=LOC_FLAGS;
  2977. end
  2978. {$ifdef SUPPORT_MMX}
  2979. else if (cs_mmx in current_settings.localswitches) and
  2980. is_mmx_able_array(ld) and
  2981. is_mmx_able_array(rd) then
  2982. begin
  2983. expectloc:=LOC_MMXREGISTER;
  2984. end
  2985. {$endif SUPPORT_MMX}
  2986. { the general solution is to convert to 32 bit int }
  2987. else
  2988. begin
  2989. expectloc:=LOC_REGISTER;
  2990. end;
  2991. end;
  2992. {$ifdef state_tracking}
  2993. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2994. var factval:Tnode;
  2995. begin
  2996. track_state_pass:=false;
  2997. if left.track_state_pass(exec_known) then
  2998. begin
  2999. track_state_pass:=true;
  3000. left.resultdef:=nil;
  3001. do_typecheckpass(left);
  3002. end;
  3003. factval:=aktstate.find_fact(left);
  3004. if factval<>nil then
  3005. begin
  3006. track_state_pass:=true;
  3007. left.destroy;
  3008. left:=factval.getcopy;
  3009. end;
  3010. if right.track_state_pass(exec_known) then
  3011. begin
  3012. track_state_pass:=true;
  3013. right.resultdef:=nil;
  3014. do_typecheckpass(right);
  3015. end;
  3016. factval:=aktstate.find_fact(right);
  3017. if factval<>nil then
  3018. begin
  3019. track_state_pass:=true;
  3020. right.destroy;
  3021. right:=factval.getcopy;
  3022. end;
  3023. end;
  3024. {$endif}
  3025. end.