nadd.pas 114 KB

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