nadd.pas 126 KB

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