nadd.pas 127 KB

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