nadd.pas 114 KB

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