nadd.pas 127 KB

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