nadd.pas 107 KB

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