nadd.pas 124 KB

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