nadd.pas 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869
  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) and
  744. not(might_have_sideeffects(left)) then
  745. begin
  746. result:=cinlinenode.create(in_sqr_real,false,left);
  747. left:=nil;
  748. exit;
  749. end;
  750. end;
  751. end;
  752. function taddnode.dogetcopy: tnode;
  753. var
  754. n: taddnode;
  755. begin
  756. n:=taddnode(inherited dogetcopy);
  757. n.resultrealdef:=resultrealdef;
  758. result:=n;
  759. end;
  760. function taddnode.docompare(p: tnode): boolean;
  761. begin
  762. result:=
  763. inherited docompare(p) and
  764. equal_defs(taddnode(p).resultrealdef,resultrealdef);
  765. end;
  766. function taddnode.pass_typecheck:tnode;
  767. begin
  768. { This function is small to keep the stack small for recursive of
  769. large + operations }
  770. typecheckpass(left);
  771. typecheckpass(right);
  772. result:=pass_typecheck_internal;
  773. end;
  774. function taddnode.pass_typecheck_internal:tnode;
  775. var
  776. hp : tnode;
  777. rd,ld,nd : tdef;
  778. hsym : tfieldvarsym;
  779. llow,lhigh,
  780. rlow,rhigh : tconstexprint;
  781. strtype : tstringtype;
  782. b : boolean;
  783. lt,rt : tnodetype;
  784. ot : tnodetype;
  785. {$ifdef state_tracking}
  786. factval : Tnode;
  787. change : boolean;
  788. {$endif}
  789. begin
  790. result:=nil;
  791. { avoid any problems with type parameters later on }
  792. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  793. begin
  794. resultdef:=cundefinedtype;
  795. exit;
  796. end;
  797. { both left and right need to be valid }
  798. set_varstate(left,vs_read,[vsf_must_be_valid]);
  799. set_varstate(right,vs_read,[vsf_must_be_valid]);
  800. if codegenerror then
  801. exit;
  802. { tp procvar support }
  803. maybe_call_procvar(left,true);
  804. maybe_call_procvar(right,true);
  805. { convert array constructors to sets, because there is no other operator
  806. possible for array constructors }
  807. if is_array_constructor(left.resultdef) then
  808. begin
  809. arrayconstructor_to_set(left);
  810. typecheckpass(left);
  811. end;
  812. if is_array_constructor(right.resultdef) then
  813. begin
  814. arrayconstructor_to_set(right);
  815. typecheckpass(right);
  816. end;
  817. { allow operator overloading }
  818. hp:=self;
  819. if isbinaryoverloaded(hp) then
  820. begin
  821. result:=hp;
  822. exit;
  823. end;
  824. { Stop checking when an error was found in the operator checking }
  825. if codegenerror then
  826. begin
  827. result:=cerrornode.create;
  828. exit;
  829. end;
  830. { Kylix allows enum+ordconstn in an enum type declaration, we need to do
  831. the conversion here before the constant folding }
  832. if (m_delphi in current_settings.modeswitches) and
  833. (blocktype in [bt_type,bt_const_type,bt_var_type]) then
  834. begin
  835. if (left.resultdef.typ=enumdef) and
  836. (right.resultdef.typ=orddef) then
  837. begin
  838. { insert explicit typecast to default signed int }
  839. left:=ctypeconvnode.create_internal(left,sinttype);
  840. typecheckpass(left);
  841. end
  842. else
  843. if (left.resultdef.typ=orddef) and
  844. (right.resultdef.typ=enumdef) then
  845. begin
  846. { insert explicit typecast to default signed int }
  847. right:=ctypeconvnode.create_internal(right,sinttype);
  848. typecheckpass(right);
  849. end;
  850. end;
  851. { is one a real float, then both need to be floats, this
  852. need to be done before the constant folding so constant
  853. operation on a float and int are also handled }
  854. {$ifdef x86}
  855. { use extended as default real type only when the x87 fpu is used }
  856. {$ifdef i386}
  857. if not(current_settings.fputype=fpu_x87) then
  858. resultrealdef:=s64floattype
  859. else
  860. resultrealdef:=pbestrealtype^;
  861. {$endif i386}
  862. {$ifdef x86_64}
  863. { x86-64 has no x87 only mode, so use always double as default }
  864. resultrealdef:=s64floattype;
  865. {$endif x86_6}
  866. {$else not x86}
  867. resultrealdef:=pbestrealtype^;
  868. {$endif not x86}
  869. if (right.resultdef.typ=floatdef) or (left.resultdef.typ=floatdef) then
  870. begin
  871. { when both floattypes are already equal then use that
  872. floattype for results }
  873. if (right.resultdef.typ=floatdef) and
  874. (left.resultdef.typ=floatdef) and
  875. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype) then
  876. resultrealdef:=left.resultdef
  877. { when there is a currency type then use currency, but
  878. only when currency is defined as float }
  879. else
  880. if (is_currency(right.resultdef) or
  881. is_currency(left.resultdef)) and
  882. ((s64currencytype.typ = floatdef) or
  883. (nodetype <> slashn)) then
  884. begin
  885. resultrealdef:=s64currencytype;
  886. inserttypeconv(right,resultrealdef);
  887. inserttypeconv(left,resultrealdef);
  888. end
  889. else
  890. begin
  891. resultrealdef:=getbestreal(left.resultdef,right.resultdef);
  892. inserttypeconv(right,resultrealdef);
  893. inserttypeconv(left,resultrealdef);
  894. end;
  895. end;
  896. { If both operands are constant and there is a unicodestring
  897. or unicodestring then convert everything to unicodestring }
  898. if is_constnode(right) and is_constnode(left) and
  899. (is_unicodestring(right.resultdef) or
  900. is_unicodestring(left.resultdef)) then
  901. begin
  902. inserttypeconv(right,cunicodestringtype);
  903. inserttypeconv(left,cunicodestringtype);
  904. end;
  905. { If both operands are constant and there is a widechar
  906. or widestring then convert everything to widestring. This
  907. allows constant folding like char+widechar }
  908. if is_constnode(right) and is_constnode(left) and
  909. (is_widestring(right.resultdef) or
  910. is_widestring(left.resultdef) or
  911. is_widechar(right.resultdef) or
  912. is_widechar(left.resultdef)) then
  913. begin
  914. inserttypeconv(right,cwidestringtype);
  915. inserttypeconv(left,cwidestringtype);
  916. end;
  917. { load easier access variables }
  918. rd:=right.resultdef;
  919. ld:=left.resultdef;
  920. rt:=right.nodetype;
  921. lt:=left.nodetype;
  922. { 4 character constant strings are compatible with orddef }
  923. { in macpas mode (become cardinals) }
  924. if (m_mac in current_settings.modeswitches) and
  925. { only allow for comparisons, additions etc are }
  926. { normally program errors }
  927. (nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) and
  928. (((lt=stringconstn) and
  929. (tstringconstnode(left).len=4) and
  930. (rd.typ=orddef)) or
  931. ((rt=stringconstn) and
  932. (tstringconstnode(right).len=4) and
  933. (ld.typ=orddef))) then
  934. begin
  935. if (rt=stringconstn) then
  936. begin
  937. inserttypeconv(right,u32inttype);
  938. rt:=right.nodetype;
  939. rd:=right.resultdef;
  940. end
  941. else
  942. begin
  943. inserttypeconv(left,u32inttype);
  944. lt:=left.nodetype;
  945. ld:=left.resultdef;
  946. end;
  947. end;
  948. { but an int/int gives real/real! }
  949. if (nodetype=slashn) and not(is_vector(left.resultdef)) and not(is_vector(right.resultdef)) then
  950. begin
  951. if is_currency(left.resultdef) and
  952. is_currency(right.resultdef) then
  953. { In case of currency, converting to float means dividing by 10000 }
  954. { However, since this is already a division, both divisions by }
  955. { 10000 are eliminated when we divide the results -> we can skip }
  956. { them. }
  957. if s64currencytype.typ = floatdef then
  958. begin
  959. { there's no s64comptype or so, how do we avoid the type conversion?
  960. left.resultdef := s64comptype;
  961. right.resultdef := s64comptype; }
  962. end
  963. else
  964. begin
  965. left.resultdef := s64inttype;
  966. right.resultdef := s64inttype;
  967. end;
  968. inserttypeconv(right,resultrealdef);
  969. inserttypeconv(left,resultrealdef);
  970. end
  971. { if both are orddefs then check sub types }
  972. else if (ld.typ=orddef) and (rd.typ=orddef) then
  973. begin
  974. { set for & and | operations in macpas mode: they only work on }
  975. { booleans, and always short circuit evaluation }
  976. if (nf_short_bool in flags) then
  977. begin
  978. if not is_boolean(ld) then
  979. begin
  980. inserttypeconv(left,pasbool8type);
  981. ld := left.resultdef;
  982. end;
  983. if not is_boolean(rd) then
  984. begin
  985. inserttypeconv(right,pasbool8type);
  986. rd := right.resultdef;
  987. end;
  988. end;
  989. { 2 booleans? Make them equal to the largest boolean }
  990. if (is_boolean(ld) and is_boolean(rd)) or
  991. (nf_short_bool in flags) then
  992. begin
  993. if (torddef(left.resultdef).size>torddef(right.resultdef).size) or
  994. (is_cbool(left.resultdef) and not is_cbool(right.resultdef)) then
  995. begin
  996. right:=ctypeconvnode.create_internal(right,left.resultdef);
  997. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  998. typecheckpass(right);
  999. end
  1000. else if (torddef(left.resultdef).size<torddef(right.resultdef).size) or
  1001. (not is_cbool(left.resultdef) and is_cbool(right.resultdef)) then
  1002. begin
  1003. left:=ctypeconvnode.create_internal(left,right.resultdef);
  1004. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  1005. typecheckpass(left);
  1006. end;
  1007. case nodetype of
  1008. xorn,
  1009. ltn,
  1010. lten,
  1011. gtn,
  1012. gten,
  1013. andn,
  1014. orn:
  1015. begin
  1016. end;
  1017. unequaln,
  1018. equaln:
  1019. begin
  1020. if not(cs_full_boolean_eval in current_settings.localswitches) or
  1021. (nf_short_bool in flags) then
  1022. begin
  1023. { Remove any compares with constants }
  1024. if (left.nodetype=ordconstn) then
  1025. begin
  1026. hp:=right;
  1027. b:=(tordconstnode(left).value<>0);
  1028. ot:=nodetype;
  1029. left.free;
  1030. left:=nil;
  1031. right:=nil;
  1032. if (not(b) and (ot=equaln)) or
  1033. (b and (ot=unequaln)) then
  1034. begin
  1035. hp:=cnotnode.create(hp);
  1036. end;
  1037. result:=hp;
  1038. exit;
  1039. end;
  1040. if (right.nodetype=ordconstn) then
  1041. begin
  1042. hp:=left;
  1043. b:=(tordconstnode(right).value<>0);
  1044. ot:=nodetype;
  1045. right.free;
  1046. right:=nil;
  1047. left:=nil;
  1048. if (not(b) and (ot=equaln)) or
  1049. (b and (ot=unequaln)) then
  1050. begin
  1051. hp:=cnotnode.create(hp);
  1052. end;
  1053. result:=hp;
  1054. exit;
  1055. end;
  1056. end;
  1057. end;
  1058. else
  1059. begin
  1060. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1061. result:=cnothingnode.create;
  1062. exit;
  1063. end;
  1064. end;
  1065. end
  1066. { Both are chars? }
  1067. else if is_char(rd) and is_char(ld) then
  1068. begin
  1069. if nodetype=addn then
  1070. begin
  1071. resultdef:=cshortstringtype;
  1072. if not(is_constcharnode(left) and is_constcharnode(right)) then
  1073. begin
  1074. inserttypeconv(left,cshortstringtype);
  1075. {$ifdef addstringopt}
  1076. hp := genaddsstringcharoptnode(self);
  1077. result := hp;
  1078. exit;
  1079. {$endif addstringopt}
  1080. end
  1081. end
  1082. else if not(nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) then
  1083. begin
  1084. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1085. result:=cnothingnode.create;
  1086. exit;
  1087. end;
  1088. end
  1089. { There is a widechar? }
  1090. else if is_widechar(rd) or is_widechar(ld) then
  1091. begin
  1092. { widechar+widechar gives widestring }
  1093. if nodetype=addn then
  1094. begin
  1095. inserttypeconv(left,cwidestringtype);
  1096. if (torddef(rd).ordtype<>uwidechar) then
  1097. inserttypeconv(right,cwidechartype);
  1098. resultdef:=cwidestringtype;
  1099. end
  1100. else
  1101. begin
  1102. if (torddef(ld).ordtype<>uwidechar) then
  1103. inserttypeconv(left,cwidechartype);
  1104. if (torddef(rd).ordtype<>uwidechar) then
  1105. inserttypeconv(right,cwidechartype);
  1106. end;
  1107. end
  1108. { is there a currency type ? }
  1109. else if ((torddef(rd).ordtype=scurrency) or (torddef(ld).ordtype=scurrency)) then
  1110. begin
  1111. if (torddef(ld).ordtype<>scurrency) then
  1112. inserttypeconv(left,s64currencytype);
  1113. if (torddef(rd).ordtype<>scurrency) then
  1114. inserttypeconv(right,s64currencytype);
  1115. end
  1116. { "and" does't care about the sign of integers }
  1117. { "xor", "or" and compares don't need extension to native int }
  1118. { size either as long as both values are signed or unsigned }
  1119. { "xor" and "or" also don't care about the sign if the values }
  1120. { occupy an entire register }
  1121. { don't do it if either type is 64 bit, since in that case we }
  1122. { can't safely find a "common" type }
  1123. else if is_integer(ld) and is_integer(rd) and
  1124. not is_64bitint(ld) and not is_64bitint(rd) and
  1125. ((nodetype=andn) or
  1126. ((nodetype in [orn,xorn,equaln,unequaln,gtn,gten,ltn,lten]) and
  1127. not(is_signed(ld) xor is_signed(rd)))) then
  1128. begin
  1129. if (rd.size>ld.size) or
  1130. { Delphi-compatible: prefer unsigned type for "and" with equal size }
  1131. ((rd.size=ld.size) and
  1132. not is_signed(rd)) then
  1133. begin
  1134. if (rd.size=ld.size) and
  1135. is_signed(ld) then
  1136. inserttypeconv_internal(left,rd)
  1137. else
  1138. begin
  1139. { not to left right.resultdef, because that may
  1140. cause a range error if left and right's def don't
  1141. completely overlap }
  1142. nd:=get_common_intdef(torddef(ld),torddef(rd),true);
  1143. inserttypeconv(left,nd);
  1144. inserttypeconv(right,nd);
  1145. end;
  1146. end
  1147. else
  1148. begin
  1149. if (rd.size=ld.size) and
  1150. is_signed(rd) then
  1151. inserttypeconv_internal(right,ld)
  1152. else
  1153. begin
  1154. nd:=get_common_intdef(torddef(ld),torddef(rd),true);
  1155. inserttypeconv(left,nd);
  1156. inserttypeconv(right,nd);
  1157. end;
  1158. end
  1159. end
  1160. { is there a signed 64 bit type ? }
  1161. else if ((torddef(rd).ordtype=s64bit) or (torddef(ld).ordtype=s64bit)) then
  1162. begin
  1163. if (torddef(ld).ordtype<>s64bit) then
  1164. inserttypeconv(left,s64inttype);
  1165. if (torddef(rd).ordtype<>s64bit) then
  1166. inserttypeconv(right,s64inttype);
  1167. end
  1168. { is there a unsigned 64 bit type ? }
  1169. else if ((torddef(rd).ordtype=u64bit) or (torddef(ld).ordtype=u64bit)) then
  1170. begin
  1171. if (torddef(ld).ordtype<>u64bit) then
  1172. inserttypeconv(left,u64inttype);
  1173. if (torddef(rd).ordtype<>u64bit) then
  1174. inserttypeconv(right,u64inttype);
  1175. end
  1176. { 64 bit cpus do calculations always in 64 bit }
  1177. {$ifndef cpu64bitaddr}
  1178. { is there a cardinal? }
  1179. else if ((torddef(rd).ordtype=u32bit) or (torddef(ld).ordtype=u32bit)) then
  1180. begin
  1181. { convert positive constants to u32bit }
  1182. if (torddef(ld).ordtype<>u32bit) and
  1183. is_constintnode(left) and
  1184. (tordconstnode(left).value >= 0) then
  1185. inserttypeconv(left,u32inttype);
  1186. if (torddef(rd).ordtype<>u32bit) and
  1187. is_constintnode(right) and
  1188. (tordconstnode(right).value >= 0) then
  1189. inserttypeconv(right,u32inttype);
  1190. { when one of the operand is signed or the operation is subn then perform
  1191. the operation in 64bit, can't use rd/ld here because there
  1192. could be already typeconvs inserted.
  1193. This is compatible with the code below for other unsigned types (PFV) }
  1194. if is_signed(left.resultdef) or
  1195. is_signed(right.resultdef) or
  1196. (nodetype=subn) then
  1197. begin
  1198. if nodetype<>subn then
  1199. CGMessage(type_h_mixed_signed_unsigned);
  1200. { mark as internal in case added for a subn, so }
  1201. { ttypeconvnode.simplify can remove the 64 bit }
  1202. { typecast again if semantically correct. Even }
  1203. { if we could detect that here already, we }
  1204. { mustn't do it here because that would change }
  1205. { overload choosing behaviour etc. The code in }
  1206. { ncnv.pas is run after that is already decided }
  1207. if (not is_signed(left.resultdef) and
  1208. not is_signed(right.resultdef)) or
  1209. (nodetype in [orn,xorn]) then
  1210. include(flags,nf_internal);
  1211. inserttypeconv(left,s64inttype);
  1212. inserttypeconv(right,s64inttype);
  1213. end
  1214. else
  1215. begin
  1216. if (torddef(left.resultdef).ordtype<>u32bit) then
  1217. inserttypeconv(left,u32inttype);
  1218. if (torddef(right.resultdef).ordtype<>u32bit) then
  1219. inserttypeconv(right,u32inttype);
  1220. end;
  1221. end
  1222. {$endif cpu64bitaddr}
  1223. { generic ord conversion is sinttype }
  1224. else
  1225. begin
  1226. { if the left or right value is smaller than the normal
  1227. type sinttype and is unsigned, and the other value
  1228. is a constant < 0, the result will always be false/true
  1229. for equal / unequal nodes.
  1230. }
  1231. if (
  1232. { left : unsigned ordinal var, right : < 0 constant }
  1233. (
  1234. ((is_signed(ld)=false) and (is_constintnode(left) =false)) and
  1235. ((is_constintnode(right)) and (tordconstnode(right).value < 0))
  1236. ) or
  1237. { right : unsigned ordinal var, left : < 0 constant }
  1238. (
  1239. ((is_signed(rd)=false) and (is_constintnode(right) =false)) and
  1240. ((is_constintnode(left)) and (tordconstnode(left).value < 0))
  1241. )
  1242. ) then
  1243. begin
  1244. if nodetype = equaln then
  1245. CGMessage(type_w_signed_unsigned_always_false)
  1246. else
  1247. if nodetype = unequaln then
  1248. CGMessage(type_w_signed_unsigned_always_true)
  1249. else
  1250. if (is_constintnode(left) and (nodetype in [ltn,lten])) or
  1251. (is_constintnode(right) and (nodetype in [gtn,gten])) then
  1252. CGMessage(type_w_signed_unsigned_always_true)
  1253. else
  1254. if (is_constintnode(right) and (nodetype in [ltn,lten])) or
  1255. (is_constintnode(left) and (nodetype in [gtn,gten])) then
  1256. CGMessage(type_w_signed_unsigned_always_false);
  1257. end;
  1258. { When there is a signed type or there is a minus operation
  1259. we convert to signed int. Otherwise (both are unsigned) we keep
  1260. the result also unsigned. This is compatible with Delphi (PFV) }
  1261. if is_signed(ld) or
  1262. is_signed(rd) or
  1263. (nodetype=subn) then
  1264. begin
  1265. {$ifdef cpunodefaultint}
  1266. { for small cpus we use the smallest common type }
  1267. nd:=get_common_intdef(torddef(ld),torddef(rd),false);
  1268. inserttypeconv(right,nd);
  1269. inserttypeconv(left,nd);
  1270. {$else cpunodefaultint}
  1271. inserttypeconv(right,sinttype);
  1272. inserttypeconv(left,sinttype);
  1273. {$endif cpunodefaultint}
  1274. end
  1275. else
  1276. begin
  1277. inserttypeconv(right,uinttype);
  1278. inserttypeconv(left,uinttype);
  1279. end;
  1280. end;
  1281. end
  1282. { if both are floatdefs, conversion is already done before constant folding }
  1283. else if (ld.typ=floatdef) then
  1284. begin
  1285. if not(nodetype in [addn,subn,muln,slashn,equaln,unequaln,ltn,lten,gtn,gten]) then
  1286. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1287. end
  1288. { left side a setdef, must be before string processing,
  1289. else array constructor can be seen as array of char (PFV) }
  1290. else if (ld.typ=setdef) then
  1291. begin
  1292. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  1293. CGMessage(type_e_set_operation_unknown);
  1294. { right must either be a set or a set element }
  1295. if (rd.typ<>setdef) and
  1296. (rt<>setelementn) then
  1297. CGMessage(type_e_mismatch)
  1298. { Make operands the same setdef. If one's elementtype fits }
  1299. { entirely inside the other's, pick the one with the largest }
  1300. { range. Otherwise create a new setdef with a range which }
  1301. { can contain both. }
  1302. else if not(equal_defs(ld,rd)) then
  1303. begin
  1304. { note: ld cannot be an empty set with elementdef=nil in }
  1305. { case right is not a set, arrayconstructor_to_set takes }
  1306. { care of that }
  1307. { 1: rd is a set with an assigned elementdef, and ld is }
  1308. { either an empty set without elementdef or a set whose }
  1309. { elementdef fits in rd's elementdef -> convert to rd }
  1310. if ((rd.typ=setdef) and
  1311. assigned(tsetdef(rd).elementdef) and
  1312. (not assigned(tsetdef(ld).elementdef) or
  1313. is_in_limit(ld,rd))) then
  1314. inserttypeconv(left,rd)
  1315. { 2: rd is either an empty set without elementdef or a set }
  1316. { whose elementdef fits in ld's elementdef, or a set }
  1317. { element whose def fits in ld's elementdef -> convert }
  1318. { to ld. ld's elementdef can't be nil here, is caught }
  1319. { previous case and "note:" above }
  1320. else if ((rd.typ=setdef) and
  1321. (not assigned(tsetdef(rd).elementdef) or
  1322. is_in_limit(rd,ld))) or
  1323. ((rd.typ<>setdef) and
  1324. is_in_limit(rd,tsetdef(ld).elementdef)) then
  1325. if (rd.typ=setdef) then
  1326. inserttypeconv(right,ld)
  1327. else
  1328. inserttypeconv(right,tsetdef(ld).elementdef)
  1329. { 3: otherwise create setdef which encompasses both, taking }
  1330. { into account empty sets without elementdef }
  1331. else
  1332. begin
  1333. if assigned(tsetdef(ld).elementdef) then
  1334. begin
  1335. llow:=tsetdef(ld).setbase;
  1336. lhigh:=tsetdef(ld).setmax;
  1337. end;
  1338. if (rd.typ=setdef) then
  1339. if assigned(tsetdef(rd).elementdef) then
  1340. begin
  1341. rlow:=tsetdef(rd).setbase;
  1342. rhigh:=tsetdef(rd).setmax;
  1343. end
  1344. else
  1345. begin
  1346. { ld's elementdef must have been valid }
  1347. rlow:=llow;
  1348. rhigh:=lhigh;
  1349. end
  1350. else
  1351. getrange(rd,rlow,rhigh);
  1352. if not assigned(tsetdef(ld).elementdef) then
  1353. begin
  1354. llow:=rlow;
  1355. lhigh:=rhigh;
  1356. end;
  1357. nd:=tsetdef.create(tsetdef(ld).elementdef,min(llow,rlow),max(lhigh,rhigh));
  1358. inserttypeconv(left,nd);
  1359. if (rd.typ=setdef) then
  1360. inserttypeconv(right,nd)
  1361. else
  1362. inserttypeconv(right,tsetdef(nd).elementdef);
  1363. end;
  1364. end;
  1365. end
  1366. { pointer comparision and subtraction }
  1367. else if (
  1368. (rd.typ=pointerdef) and (ld.typ=pointerdef)
  1369. ) or
  1370. { compare/add pchar to variable (not stringconst) char arrays
  1371. by addresses like BP/Delphi }
  1372. (
  1373. (nodetype in [equaln,unequaln,subn,addn]) and
  1374. (
  1375. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd) and (rt<>stringconstn)) or
  1376. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld) and (lt<>stringconstn))
  1377. )
  1378. ) then
  1379. begin
  1380. { convert char array to pointer }
  1381. if is_chararray(rd) then
  1382. begin
  1383. inserttypeconv(right,charpointertype);
  1384. rd:=right.resultdef;
  1385. end
  1386. else if is_chararray(ld) then
  1387. begin
  1388. inserttypeconv(left,charpointertype);
  1389. ld:=left.resultdef;
  1390. end;
  1391. case nodetype of
  1392. equaln,unequaln :
  1393. begin
  1394. if is_voidpointer(right.resultdef) then
  1395. inserttypeconv(right,left.resultdef)
  1396. else if is_voidpointer(left.resultdef) then
  1397. inserttypeconv(left,right.resultdef)
  1398. else if not(equal_defs(ld,rd)) then
  1399. IncompatibleTypes(ld,rd);
  1400. { now that the type checking is done, convert both to charpointer, }
  1401. { because methodpointers are 8 bytes even though only the first 4 }
  1402. { bytes must be compared. This can happen here if we are in }
  1403. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  1404. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  1405. { optimized away, since the result already was a voidpointer, so }
  1406. { use a charpointer instead (JM) }
  1407. inserttypeconv_internal(left,charpointertype);
  1408. inserttypeconv_internal(right,charpointertype);
  1409. end;
  1410. ltn,lten,gtn,gten:
  1411. begin
  1412. if (cs_extsyntax in current_settings.moduleswitches) then
  1413. begin
  1414. if is_voidpointer(right.resultdef) then
  1415. inserttypeconv(right,left.resultdef)
  1416. else if is_voidpointer(left.resultdef) then
  1417. inserttypeconv(left,right.resultdef)
  1418. else if not(equal_defs(ld,rd)) then
  1419. IncompatibleTypes(ld,rd);
  1420. end
  1421. else
  1422. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1423. end;
  1424. subn:
  1425. begin
  1426. if (cs_extsyntax in current_settings.moduleswitches) then
  1427. begin
  1428. if is_voidpointer(right.resultdef) then
  1429. begin
  1430. if is_big_untyped_addrnode(right) then
  1431. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1432. inserttypeconv(right,left.resultdef)
  1433. end
  1434. else if is_voidpointer(left.resultdef) then
  1435. inserttypeconv(left,right.resultdef)
  1436. else if not(equal_defs(ld,rd)) then
  1437. IncompatibleTypes(ld,rd);
  1438. end
  1439. else
  1440. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1441. if not(nf_has_pointerdiv in flags) and
  1442. (tpointerdef(rd).pointeddef.size>1) then
  1443. begin
  1444. hp:=getcopy;
  1445. include(hp.flags,nf_has_pointerdiv);
  1446. result:=cmoddivnode.create(divn,hp,cordconstnode.create(tpointerdef(rd).pointeddef.size,sinttype,false));
  1447. end;
  1448. resultdef:=sinttype;
  1449. exit;
  1450. end;
  1451. else
  1452. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1453. end;
  1454. end
  1455. { is one of the operands a string?,
  1456. chararrays are also handled as strings (after conversion), also take
  1457. care of chararray+chararray and chararray+char.
  1458. Note: Must be done after pointerdef+pointerdef has been checked, else
  1459. pchar is converted to string }
  1460. else if (rd.typ=stringdef) or
  1461. (ld.typ=stringdef) or
  1462. { stringconstn's can be arraydefs }
  1463. (lt=stringconstn) or
  1464. (rt=stringconstn) or
  1465. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1466. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1467. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1468. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1469. begin
  1470. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1471. begin
  1472. { Is there a unicodestring? }
  1473. if is_unicodestring(rd) or is_unicodestring(ld) then
  1474. strtype:=st_unicodestring
  1475. else
  1476. { Is there a widestring? }
  1477. if is_widestring(rd) or is_widestring(ld) or
  1478. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1479. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1480. strtype:=st_widestring
  1481. else
  1482. if is_ansistring(rd) or is_ansistring(ld) or
  1483. ((cs_ansistrings in current_settings.localswitches) and
  1484. //todo: Move some of this to longstring's then they are implemented?
  1485. (
  1486. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or (lt = stringconstn) or
  1487. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld) or (rt = stringconstn)
  1488. )
  1489. ) then
  1490. strtype:=st_ansistring
  1491. else
  1492. if is_longstring(rd) or is_longstring(ld) then
  1493. strtype:=st_longstring
  1494. else
  1495. begin
  1496. { TODO: todo: add a warning/hint here if one converting a too large array}
  1497. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1498. Note: Delphi halts with error if "array [0..xx] of char"
  1499. is assigned to ShortString and string length is less
  1500. then array size }
  1501. strtype:= st_shortstring;
  1502. end;
  1503. // Now convert nodes to common string type
  1504. case strtype of
  1505. st_widestring :
  1506. begin
  1507. if not(is_widestring(rd)) then
  1508. inserttypeconv(right,cwidestringtype);
  1509. if not(is_widestring(ld)) then
  1510. inserttypeconv(left,cwidestringtype);
  1511. end;
  1512. st_unicodestring :
  1513. begin
  1514. if not(is_unicodestring(rd)) then
  1515. inserttypeconv(right,cunicodestringtype);
  1516. if not(is_unicodestring(ld)) then
  1517. inserttypeconv(left,cunicodestringtype);
  1518. end;
  1519. st_ansistring :
  1520. begin
  1521. if not(is_ansistring(rd)) then
  1522. inserttypeconv(right,cansistringtype);
  1523. if not(is_ansistring(ld)) then
  1524. inserttypeconv(left,cansistringtype);
  1525. end;
  1526. st_longstring :
  1527. begin
  1528. if not(is_longstring(rd)) then
  1529. inserttypeconv(right,clongstringtype);
  1530. if not(is_longstring(ld)) then
  1531. inserttypeconv(left,clongstringtype);
  1532. end;
  1533. st_shortstring :
  1534. begin
  1535. if not(is_shortstring(ld)) then
  1536. inserttypeconv(left,cshortstringtype);
  1537. { don't convert char, that can be handled by the optimized node }
  1538. if not(is_shortstring(rd) or is_char(rd)) then
  1539. inserttypeconv(right,cshortstringtype);
  1540. end;
  1541. else
  1542. internalerror(2005101);
  1543. end;
  1544. end
  1545. else
  1546. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1547. end
  1548. { implicit pointer object type comparison }
  1549. else if is_implicit_pointer_object_type(rd) or is_implicit_pointer_object_type(ld) then
  1550. begin
  1551. if (nodetype in [equaln,unequaln]) then
  1552. begin
  1553. if is_implicit_pointer_object_type(rd) and is_implicit_pointer_object_type(ld) then
  1554. begin
  1555. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  1556. inserttypeconv(right,left.resultdef)
  1557. else
  1558. inserttypeconv(left,right.resultdef);
  1559. end
  1560. else if is_implicit_pointer_object_type(rd) then
  1561. inserttypeconv(left,right.resultdef)
  1562. else
  1563. inserttypeconv(right,left.resultdef);
  1564. end
  1565. else
  1566. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1567. end
  1568. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  1569. begin
  1570. if (nodetype in [equaln,unequaln]) then
  1571. begin
  1572. if tobjectdef(tclassrefdef(rd).pointeddef).is_related(
  1573. tobjectdef(tclassrefdef(ld).pointeddef)) then
  1574. inserttypeconv(right,left.resultdef)
  1575. else
  1576. inserttypeconv(left,right.resultdef);
  1577. end
  1578. else
  1579. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1580. end
  1581. { allow comparison with nil pointer }
  1582. else if is_implicit_pointer_object_type(rd) or (rd.typ=classrefdef) then
  1583. begin
  1584. if (nodetype in [equaln,unequaln]) then
  1585. inserttypeconv(left,right.resultdef)
  1586. else
  1587. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1588. end
  1589. else if is_implicit_pointer_object_type(ld) or (ld.typ=classrefdef) then
  1590. begin
  1591. if (nodetype in [equaln,unequaln]) then
  1592. inserttypeconv(right,left.resultdef)
  1593. else
  1594. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1595. end
  1596. { support procvar=nil,procvar<>nil }
  1597. else if ((ld.typ=procvardef) and (rt=niln)) or
  1598. ((rd.typ=procvardef) and (lt=niln)) then
  1599. begin
  1600. if not(nodetype in [equaln,unequaln]) then
  1601. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1602. { find proc field in methodpointer record }
  1603. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1604. if not assigned(hsym) then
  1605. internalerror(200412043);
  1606. { For methodpointers compare only tmethodpointer.proc }
  1607. if (rd.typ=procvardef) and
  1608. (not tprocvardef(rd).is_addressonly) then
  1609. begin
  1610. right:=csubscriptnode.create(
  1611. hsym,
  1612. ctypeconvnode.create_internal(right,methodpointertype));
  1613. typecheckpass(right);
  1614. end;
  1615. if (ld.typ=procvardef) and
  1616. (not tprocvardef(ld).is_addressonly) then
  1617. begin
  1618. left:=csubscriptnode.create(
  1619. hsym,
  1620. ctypeconvnode.create_internal(left,methodpointertype));
  1621. typecheckpass(left);
  1622. end;
  1623. end
  1624. { support dynamicarray=nil,dynamicarray<>nil }
  1625. else if (is_dynamic_array(ld) and (rt=niln)) or
  1626. (is_dynamic_array(rd) and (lt=niln)) or
  1627. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1628. begin
  1629. if not(nodetype in [equaln,unequaln]) then
  1630. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1631. end
  1632. {$ifdef SUPPORT_MMX}
  1633. { mmx support, this must be before the zero based array
  1634. check }
  1635. else if (cs_mmx in current_settings.localswitches) and
  1636. is_mmx_able_array(ld) and
  1637. is_mmx_able_array(rd) and
  1638. equal_defs(ld,rd) then
  1639. begin
  1640. case nodetype of
  1641. addn,subn,xorn,orn,andn:
  1642. ;
  1643. { mul is a little bit restricted }
  1644. muln:
  1645. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1646. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1647. else
  1648. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1649. end;
  1650. end
  1651. {$endif SUPPORT_MMX}
  1652. { vector support, this must be before the zero based array
  1653. check }
  1654. else if (cs_support_vectors in current_settings.globalswitches) and
  1655. is_vector(ld) and
  1656. is_vector(rd) and
  1657. equal_defs(ld,rd) then
  1658. begin
  1659. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  1660. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1661. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  1662. resultdef:=left.resultdef;
  1663. end
  1664. { this is a little bit dangerous, also the left type }
  1665. { pointer to should be checked! This broke the mmx support }
  1666. else if (rd.typ=pointerdef) or
  1667. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  1668. begin
  1669. if is_zero_based_array(rd) then
  1670. begin
  1671. resultdef:=tpointerdef.create(tarraydef(rd).elementdef);
  1672. inserttypeconv(right,resultdef);
  1673. end
  1674. else
  1675. resultdef:=right.resultdef;
  1676. inserttypeconv(left,sinttype);
  1677. if nodetype=addn then
  1678. begin
  1679. if not(cs_extsyntax in current_settings.moduleswitches) or
  1680. (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
  1681. not(cs_pointermath in current_settings.localswitches) and
  1682. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  1683. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1684. if (rd.typ=pointerdef) and
  1685. (tpointerdef(rd).pointeddef.size>1) then
  1686. begin
  1687. left:=caddnode.create(muln,left,
  1688. cordconstnode.create(tpointerdef(rd).pointeddef.size,sinttype,true));
  1689. typecheckpass(left);
  1690. end;
  1691. end
  1692. else
  1693. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1694. end
  1695. else if (ld.typ=pointerdef) or
  1696. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  1697. begin
  1698. if is_zero_based_array(ld) then
  1699. begin
  1700. resultdef:=tpointerdef.create(tarraydef(ld).elementdef);
  1701. inserttypeconv(left,resultdef);
  1702. end
  1703. else
  1704. resultdef:=left.resultdef;
  1705. inserttypeconv(right,sinttype);
  1706. if nodetype in [addn,subn] then
  1707. begin
  1708. if (lt=niln) then
  1709. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),'NIL',rd.typename);
  1710. if not(cs_extsyntax in current_settings.moduleswitches) or
  1711. (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
  1712. not(cs_pointermath in current_settings.localswitches) and
  1713. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  1714. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1715. if (ld.typ=pointerdef) then
  1716. begin
  1717. if is_big_untyped_addrnode(left) then
  1718. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1719. if (tpointerdef(ld).pointeddef.size>1) then
  1720. begin
  1721. right:=caddnode.create(muln,right,
  1722. cordconstnode.create(tpointerdef(ld).pointeddef.size,sinttype,true));
  1723. typecheckpass(right);
  1724. end
  1725. end else
  1726. if is_zero_based_array(ld) and
  1727. (tarraydef(ld).elementdef.size>1) then
  1728. begin
  1729. right:=caddnode.create(muln,right,
  1730. cordconstnode.create(tarraydef(ld).elementdef.size,sinttype,true));
  1731. typecheckpass(right);
  1732. end;
  1733. end
  1734. else
  1735. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1736. end
  1737. else if (rd.typ=procvardef) and
  1738. (ld.typ=procvardef) and
  1739. equal_defs(rd,ld) then
  1740. begin
  1741. if (nodetype in [equaln,unequaln]) then
  1742. begin
  1743. if tprocvardef(rd).is_addressonly then
  1744. begin
  1745. inserttypeconv_internal(right,voidpointertype);
  1746. inserttypeconv_internal(left,voidpointertype);
  1747. end
  1748. else
  1749. begin
  1750. { find proc field in methodpointer record }
  1751. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1752. if not assigned(hsym) then
  1753. internalerror(200412043);
  1754. { Compare tmehodpointer(left).proc }
  1755. right:=csubscriptnode.create(
  1756. hsym,
  1757. ctypeconvnode.create_internal(right,methodpointertype));
  1758. typecheckpass(right);
  1759. left:=csubscriptnode.create(
  1760. hsym,
  1761. ctypeconvnode.create_internal(left,methodpointertype));
  1762. typecheckpass(left);
  1763. end;
  1764. end
  1765. else
  1766. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1767. end
  1768. { enums }
  1769. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  1770. begin
  1771. if allowenumop(nodetype) then
  1772. inserttypeconv(right,left.resultdef)
  1773. else
  1774. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1775. end
  1776. { generic conversion, this is for error recovery }
  1777. else
  1778. begin
  1779. inserttypeconv(left,sinttype);
  1780. inserttypeconv(right,sinttype);
  1781. end;
  1782. { set resultdef if not already done }
  1783. if not assigned(resultdef) then
  1784. begin
  1785. case nodetype of
  1786. ltn,lten,gtn,gten,equaln,unequaln :
  1787. resultdef:=pasbool8type;
  1788. slashn :
  1789. resultdef:=resultrealdef;
  1790. addn:
  1791. begin
  1792. { for strings, return is always a 255 char string }
  1793. if is_shortstring(left.resultdef) then
  1794. resultdef:=cshortstringtype
  1795. else
  1796. resultdef:=left.resultdef;
  1797. end;
  1798. else
  1799. resultdef:=left.resultdef;
  1800. end;
  1801. end;
  1802. { when the result is currency we need some extra code for
  1803. multiplication and division. this should not be done when
  1804. the muln or slashn node is created internally }
  1805. if not(nf_is_currency in flags) and
  1806. is_currency(resultdef) then
  1807. begin
  1808. case nodetype of
  1809. slashn :
  1810. begin
  1811. { slashn will only work with floats }
  1812. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  1813. include(hp.flags,nf_is_currency);
  1814. result:=hp;
  1815. end;
  1816. muln :
  1817. begin
  1818. if s64currencytype.typ=floatdef then
  1819. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  1820. else
  1821. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  1822. include(hp.flags,nf_is_currency);
  1823. result:=hp
  1824. end;
  1825. end;
  1826. end;
  1827. if not codegenerror and
  1828. not assigned(result) then
  1829. result:=simplify(false);
  1830. end;
  1831. function taddnode.first_addstring: tnode;
  1832. const
  1833. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  1834. var
  1835. p: tnode;
  1836. newstatement : tstatementnode;
  1837. tempnode (*,tempnode2*) : ttempcreatenode;
  1838. cmpfuncname: string;
  1839. begin
  1840. { when we get here, we are sure that both the left and the right }
  1841. { node are both strings of the same stringtype (JM) }
  1842. case nodetype of
  1843. addn:
  1844. begin
  1845. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  1846. begin
  1847. result:=right;
  1848. left.free;
  1849. left:=nil;
  1850. right:=nil;
  1851. exit;
  1852. end;
  1853. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  1854. begin
  1855. result:=left;
  1856. left:=nil;
  1857. right.free;
  1858. right:=nil;
  1859. exit;
  1860. end;
  1861. { create the call to the concat routine both strings as arguments }
  1862. if assigned(aktassignmentnode) and
  1863. (aktassignmentnode.right=self) and
  1864. (aktassignmentnode.left.resultdef=resultdef) and
  1865. valid_for_var(aktassignmentnode.left,false) then
  1866. begin
  1867. result:=ccallnode.createintern('fpc_'+
  1868. tstringdef(resultdef).stringtypname+'_concat',
  1869. ccallparanode.create(right,
  1870. ccallparanode.create(left,
  1871. ccallparanode.create(aktassignmentnode.left.getcopy,nil))));
  1872. include(aktassignmentnode.flags,nf_assign_done_in_right);
  1873. firstpass(result);
  1874. end
  1875. else
  1876. begin
  1877. result:=internalstatements(newstatement);
  1878. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1879. addstatement(newstatement,tempnode);
  1880. addstatement(newstatement,ccallnode.createintern('fpc_'+
  1881. tstringdef(resultdef).stringtypname+'_concat',
  1882. ccallparanode.create(right,
  1883. ccallparanode.create(left,
  1884. ccallparanode.create(ctemprefnode.create(tempnode),nil)))));
  1885. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1886. addstatement(newstatement,ctemprefnode.create(tempnode));
  1887. end;
  1888. { we reused the arguments }
  1889. left := nil;
  1890. right := nil;
  1891. end;
  1892. ltn,lten,gtn,gten,equaln,unequaln :
  1893. begin
  1894. { generate better code for comparison with empty string, we
  1895. only need to compare the length with 0 }
  1896. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1897. { windows widestrings are too complicated to be handled optimized }
  1898. not(is_widestring(left.resultdef) and (target_info.system in systems_windows)) and
  1899. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  1900. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  1901. begin
  1902. { switch so that the constant is always on the right }
  1903. if left.nodetype = stringconstn then
  1904. begin
  1905. p := left;
  1906. left := right;
  1907. right := p;
  1908. nodetype:=swap_relation[nodetype];
  1909. end;
  1910. if is_shortstring(left.resultdef) or
  1911. (nodetype in [gtn,gten,ltn,lten]) then
  1912. { compare the length with 0 }
  1913. result := caddnode.create(nodetype,
  1914. cinlinenode.create(in_length_x,false,left),
  1915. cordconstnode.create(0,s32inttype,false))
  1916. else
  1917. begin
  1918. (*
  1919. if is_widestring(left.resultdef) and
  1920. (target_info.system in system_windows) then
  1921. begin
  1922. { windows like widestrings requires that we also check the length }
  1923. result:=internalstatements(newstatement);
  1924. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1925. tempnode2:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1926. addstatement(newstatement,tempnode);
  1927. addstatement(newstatement,tempnode2);
  1928. { poor man's cse }
  1929. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  1930. ctypeconvnode.create_internal(left,voidpointertype))
  1931. );
  1932. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode2),
  1933. caddnode.create(orn,
  1934. caddnode.create(nodetype,
  1935. ctemprefnode.create(tempnode),
  1936. cpointerconstnode.create(0,voidpointertype)
  1937. ),
  1938. caddnode.create(nodetype,
  1939. ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),s32inttype),
  1940. cordconstnode.create(0,s32inttype,false)
  1941. )
  1942. )
  1943. ));
  1944. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1945. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode2));
  1946. addstatement(newstatement,ctemprefnode.create(tempnode2));
  1947. end
  1948. else
  1949. *)
  1950. begin
  1951. { compare the pointer with nil (for ansistrings etc), }
  1952. { faster than getting the length (JM) }
  1953. result:= caddnode.create(nodetype,
  1954. ctypeconvnode.create_internal(left,voidpointertype),
  1955. cpointerconstnode.create(0,voidpointertype));
  1956. end;
  1957. end;
  1958. { left is reused }
  1959. left := nil;
  1960. { right isn't }
  1961. right.free;
  1962. right := nil;
  1963. exit;
  1964. end;
  1965. { no string constant -> call compare routine }
  1966. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  1967. { for equality checks use optimized version }
  1968. if nodetype in [equaln,unequaln] then
  1969. cmpfuncname := cmpfuncname + '_equal';
  1970. result := ccallnode.createintern(cmpfuncname,
  1971. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1972. { and compare its result with 0 according to the original operator }
  1973. result := caddnode.create(nodetype,result,
  1974. cordconstnode.create(0,s32inttype,false));
  1975. left := nil;
  1976. right := nil;
  1977. end;
  1978. end;
  1979. end;
  1980. function taddnode.first_addset : tnode;
  1981. procedure call_varset_helper(const n : string);
  1982. var
  1983. newstatement : tstatementnode;
  1984. temp : ttempcreatenode;
  1985. begin
  1986. { add two var sets }
  1987. result:=internalstatements(newstatement);
  1988. { create temp for result }
  1989. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1990. addstatement(newstatement,temp);
  1991. addstatement(newstatement,ccallnode.createintern(n,
  1992. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1993. ccallparanode.create(ctemprefnode.create(temp),
  1994. ccallparanode.create(right,
  1995. ccallparanode.create(left,nil)))))
  1996. );
  1997. { remove reused parts from original node }
  1998. left:=nil;
  1999. right:=nil;
  2000. { the last statement should return the value as
  2001. location and type, this is done be referencing the
  2002. temp and converting it first from a persistent temp to
  2003. normal temp }
  2004. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2005. addstatement(newstatement,ctemprefnode.create(temp));
  2006. end;
  2007. var
  2008. procname: string[31];
  2009. tempn: tnode;
  2010. newstatement : tstatementnode;
  2011. temp : ttempcreatenode;
  2012. begin
  2013. result:=nil;
  2014. case nodetype of
  2015. equaln,unequaln,lten,gten:
  2016. begin
  2017. case nodetype of
  2018. equaln,unequaln:
  2019. procname := 'fpc_varset_comp_sets';
  2020. lten,gten:
  2021. begin
  2022. procname := 'fpc_varset_contains_sets';
  2023. { (left >= right) = (right <= left) }
  2024. if nodetype = gten then
  2025. begin
  2026. tempn := left;
  2027. left := right;
  2028. right := tempn;
  2029. end;
  2030. end;
  2031. end;
  2032. result := ccallnode.createinternres(procname,
  2033. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2034. ccallparanode.create(right,
  2035. ccallparanode.create(left,nil))),resultdef);
  2036. { left and right are reused as parameters }
  2037. left := nil;
  2038. right := nil;
  2039. { for an unequaln, we have to negate the result of comp_sets }
  2040. if nodetype = unequaln then
  2041. result := cnotnode.create(result);
  2042. end;
  2043. addn:
  2044. begin
  2045. { optimize first loading of a set }
  2046. if (right.nodetype=setelementn) and
  2047. not(assigned(tsetelementnode(right).right)) and
  2048. is_emptyset(left) then
  2049. begin
  2050. result:=internalstatements(newstatement);
  2051. { create temp for result }
  2052. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2053. addstatement(newstatement,temp);
  2054. { adjust for set base }
  2055. tsetelementnode(right).left:=caddnode.create(subn,
  2056. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2057. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2058. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  2059. ccallparanode.create(ctemprefnode.create(temp),
  2060. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2061. ccallparanode.create(tsetelementnode(right).left,nil))))
  2062. );
  2063. { the last statement should return the value as
  2064. location and type, this is done be referencing the
  2065. temp and converting it first from a persistent temp to
  2066. normal temp }
  2067. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2068. addstatement(newstatement,ctemprefnode.create(temp));
  2069. tsetelementnode(right).left := nil;
  2070. end
  2071. else
  2072. begin
  2073. if right.nodetype=setelementn then
  2074. begin
  2075. result:=internalstatements(newstatement);
  2076. { create temp for result }
  2077. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2078. addstatement(newstatement,temp);
  2079. { adjust for set base }
  2080. tsetelementnode(right).left:=caddnode.create(subn,
  2081. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2082. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2083. { add a range or a single element? }
  2084. if assigned(tsetelementnode(right).right) then
  2085. begin
  2086. { adjust for set base }
  2087. tsetelementnode(right).right:=caddnode.create(subn,
  2088. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  2089. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2090. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  2091. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2092. ccallparanode.create(tsetelementnode(right).right,
  2093. ccallparanode.create(tsetelementnode(right).left,
  2094. ccallparanode.create(ctemprefnode.create(temp),
  2095. ccallparanode.create(left,nil))))))
  2096. );
  2097. end
  2098. else
  2099. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  2100. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2101. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2102. ccallparanode.create(ctemprefnode.create(temp),
  2103. ccallparanode.create(left,nil)))))
  2104. );
  2105. { remove reused parts from original node }
  2106. tsetelementnode(right).right:=nil;
  2107. tsetelementnode(right).left:=nil;
  2108. left:=nil;
  2109. { the last statement should return the value as
  2110. location and type, this is done be referencing the
  2111. temp and converting it first from a persistent temp to
  2112. normal temp }
  2113. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2114. addstatement(newstatement,ctemprefnode.create(temp));
  2115. end
  2116. else
  2117. call_varset_helper('fpc_varset_add_sets');
  2118. end
  2119. end;
  2120. subn:
  2121. call_varset_helper('fpc_varset_sub_sets');
  2122. symdifn:
  2123. call_varset_helper('fpc_varset_symdif_sets');
  2124. muln:
  2125. call_varset_helper('fpc_varset_mul_sets');
  2126. else
  2127. internalerror(200609241);
  2128. end;
  2129. end;
  2130. function taddnode.use_generic_mul32to64: boolean;
  2131. begin
  2132. result := true;
  2133. end;
  2134. function taddnode.try_make_mul32to64: boolean;
  2135. function canbe32bitint(v: tconstexprint): boolean;
  2136. begin
  2137. result := ((v >= int64(low(longint))) and (v <= int64(high(longint)))) or
  2138. ((v >= qword(low(cardinal))) and (v <= qword(high(cardinal))))
  2139. end;
  2140. var
  2141. temp: tnode;
  2142. begin
  2143. result := false;
  2144. if ((left.nodetype = typeconvn) and
  2145. is_integer(ttypeconvnode(left).left.resultdef) and
  2146. (not(torddef(ttypeconvnode(left).left.resultdef).ordtype in [u64bit,s64bit])) and
  2147. (((right.nodetype = ordconstn) and canbe32bitint(tordconstnode(right).value)) or
  2148. ((right.nodetype = typeconvn) and
  2149. is_integer(ttypeconvnode(right).left.resultdef) and
  2150. not(torddef(ttypeconvnode(right).left.resultdef).ordtype in [u64bit,s64bit])) and
  2151. ((is_signed(ttypeconvnode(left).left.resultdef) =
  2152. is_signed(ttypeconvnode(right).left.resultdef)) or
  2153. (is_signed(ttypeconvnode(left).left.resultdef) and
  2154. (torddef(ttypeconvnode(right).left.resultdef).ordtype in [u8bit,u16bit]))))) then
  2155. begin
  2156. temp := ttypeconvnode(left).left;
  2157. ttypeconvnode(left).left := nil;
  2158. left.free;
  2159. left := temp;
  2160. if (right.nodetype = typeconvn) then
  2161. begin
  2162. temp := ttypeconvnode(right).left;
  2163. ttypeconvnode(right).left := nil;
  2164. right.free;
  2165. right := temp;
  2166. end;
  2167. if (is_signed(left.resultdef)) then
  2168. begin
  2169. inserttypeconv(left,s32inttype);
  2170. inserttypeconv(right,s32inttype);
  2171. end
  2172. else
  2173. begin
  2174. inserttypeconv(left,u32inttype);
  2175. inserttypeconv(right,u32inttype);
  2176. end;
  2177. firstpass(left);
  2178. firstpass(right);
  2179. result := true;
  2180. end;
  2181. end;
  2182. function taddnode.first_add64bitint: tnode;
  2183. var
  2184. procname: string[31];
  2185. temp: tnode;
  2186. power: longint;
  2187. begin
  2188. result := nil;
  2189. { create helper calls mul }
  2190. if nodetype <> muln then
  2191. exit;
  2192. { make sure that if there is a constant, that it's on the right }
  2193. if left.nodetype = ordconstn then
  2194. begin
  2195. temp := right;
  2196. right := left;
  2197. left := temp;
  2198. end;
  2199. { can we use a shift instead of a mul? }
  2200. if not (cs_check_overflow in current_settings.localswitches) and
  2201. (right.nodetype = ordconstn) and
  2202. ispowerof2(tordconstnode(right).value,power) then
  2203. begin
  2204. tordconstnode(right).value := power;
  2205. result := cshlshrnode.create(shln,left,right);
  2206. { left and right are reused }
  2207. left := nil;
  2208. right := nil;
  2209. { return firstpassed new node }
  2210. exit;
  2211. end;
  2212. if not(use_generic_mul32to64) and
  2213. try_make_mul32to64 then
  2214. exit;
  2215. { when currency is used set the result of the
  2216. parameters to s64bit, so they are not converted }
  2217. if is_currency(resultdef) then
  2218. begin
  2219. left.resultdef:=s64inttype;
  2220. right.resultdef:=s64inttype;
  2221. end;
  2222. { otherwise, create the parameters for the helper }
  2223. right := ccallparanode.create(
  2224. cordconstnode.create(ord(cs_check_overflow in current_settings.localswitches),pasbool8type,true),
  2225. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2226. left := nil;
  2227. { only qword needs the unsigned code, the
  2228. signed code is also used for currency }
  2229. if is_signed(resultdef) then
  2230. procname := 'fpc_mul_int64'
  2231. else
  2232. procname := 'fpc_mul_qword';
  2233. result := ccallnode.createintern(procname,right);
  2234. right := nil;
  2235. end;
  2236. function taddnode.first_addfloat : tnode;
  2237. var
  2238. procname: string[31];
  2239. { do we need to reverse the result ? }
  2240. notnode : boolean;
  2241. fdef : tdef;
  2242. begin
  2243. result := nil;
  2244. notnode := false;
  2245. { In non-emulation mode, real opcodes are
  2246. emitted for floating point values.
  2247. }
  2248. if not (cs_fp_emulation in current_settings.moduleswitches) then
  2249. exit;
  2250. if not(target_info.system in systems_wince) then
  2251. begin
  2252. case tfloatdef(left.resultdef).floattype of
  2253. s32real:
  2254. begin
  2255. fdef:=search_system_type('FLOAT32REC').typedef;
  2256. procname:='float32';
  2257. end;
  2258. s64real:
  2259. begin
  2260. fdef:=search_system_type('FLOAT64').typedef;
  2261. procname:='float64';
  2262. end;
  2263. {!!! not yet implemented
  2264. s128real:
  2265. }
  2266. else
  2267. internalerror(2005082601);
  2268. end;
  2269. case nodetype of
  2270. addn:
  2271. procname:=procname+'_add';
  2272. muln:
  2273. procname:=procname+'_mul';
  2274. subn:
  2275. procname:=procname+'_sub';
  2276. slashn:
  2277. procname:=procname+'_div';
  2278. ltn:
  2279. procname:=procname+'_lt';
  2280. lten:
  2281. procname:=procname+'_le';
  2282. gtn:
  2283. begin
  2284. procname:=procname+'_le';
  2285. notnode:=true;
  2286. end;
  2287. gten:
  2288. begin
  2289. procname:=procname+'_lt';
  2290. notnode:=true;
  2291. end;
  2292. equaln:
  2293. procname:=procname+'_eq';
  2294. unequaln:
  2295. begin
  2296. procname:=procname+'_eq';
  2297. notnode:=true;
  2298. end;
  2299. else
  2300. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2301. end;
  2302. end
  2303. else
  2304. begin
  2305. case nodetype of
  2306. addn:
  2307. procname:='ADD';
  2308. muln:
  2309. procname:='MUL';
  2310. subn:
  2311. procname:='SUB';
  2312. slashn:
  2313. procname:='DIV';
  2314. ltn:
  2315. procname:='LT';
  2316. lten:
  2317. procname:='LE';
  2318. gtn:
  2319. procname:='GT';
  2320. gten:
  2321. procname:='GE';
  2322. equaln:
  2323. procname:='EQ';
  2324. unequaln:
  2325. procname:='NE';
  2326. else
  2327. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2328. end;
  2329. case tfloatdef(left.resultdef).floattype of
  2330. s32real:
  2331. begin
  2332. procname:=procname+'S';
  2333. if nodetype in [addn,muln,subn,slashn] then
  2334. procname:=lower(procname);
  2335. end;
  2336. s64real:
  2337. procname:=procname+'D';
  2338. {!!! not yet implemented
  2339. s128real:
  2340. }
  2341. else
  2342. internalerror(2005082602);
  2343. end;
  2344. end;
  2345. { cast softfpu result? }
  2346. if not(target_info.system in systems_wince) then
  2347. begin
  2348. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2349. resultdef:=pasbool8type;
  2350. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  2351. ctypeconvnode.create_internal(right,fdef),
  2352. ccallparanode.create(
  2353. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  2354. end
  2355. else
  2356. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  2357. ccallparanode.create(left,nil)));
  2358. left:=nil;
  2359. right:=nil;
  2360. { do we need to reverse the result }
  2361. if notnode then
  2362. result:=cnotnode.create(result);
  2363. end;
  2364. function taddnode.pass_1 : tnode;
  2365. var
  2366. {$ifdef addstringopt}
  2367. hp : tnode;
  2368. {$endif addstringopt}
  2369. rd,ld : tdef;
  2370. i : longint;
  2371. lt,rt : tnodetype;
  2372. {$ifdef cpuneedsmulhelper}
  2373. procname : string[32];
  2374. {$endif cpuneedsmulhelper}
  2375. begin
  2376. result:=nil;
  2377. { Can we optimize multiple string additions into a single call?
  2378. This need to be done on a complete tree to detect the multiple
  2379. add nodes and is therefor done before the subtrees are processed }
  2380. if canbemultistringadd(self) then
  2381. begin
  2382. result := genmultistringadd(self);
  2383. exit;
  2384. end;
  2385. { first do the two subtrees }
  2386. firstpass(left);
  2387. firstpass(right);
  2388. if codegenerror then
  2389. exit;
  2390. { load easier access variables }
  2391. rd:=right.resultdef;
  2392. ld:=left.resultdef;
  2393. rt:=right.nodetype;
  2394. lt:=left.nodetype;
  2395. { int/int gives real/real! }
  2396. if nodetype=slashn then
  2397. begin
  2398. {$ifdef cpufpemu}
  2399. if (current_settings.fputype=fpu_soft) or (cs_fp_emulation in current_settings.moduleswitches) then
  2400. begin
  2401. result:=first_addfloat;
  2402. if assigned(result) then
  2403. exit;
  2404. end;
  2405. {$endif cpufpemu}
  2406. expectloc:=LOC_FPUREGISTER;
  2407. end
  2408. { if both are orddefs then check sub types }
  2409. else if (ld.typ=orddef) and (rd.typ=orddef) then
  2410. begin
  2411. { optimize multiplacation by a power of 2 }
  2412. if not(cs_check_overflow in current_settings.localswitches) and
  2413. (nodetype = muln) and
  2414. (((left.nodetype = ordconstn) and
  2415. ispowerof2(tordconstnode(left).value,i)) or
  2416. ((right.nodetype = ordconstn) and
  2417. ispowerof2(tordconstnode(right).value,i))) then
  2418. begin
  2419. if left.nodetype = ordconstn then
  2420. begin
  2421. tordconstnode(left).value := i;
  2422. result := cshlshrnode.create(shln,right,left);
  2423. end
  2424. else
  2425. begin
  2426. tordconstnode(right).value := i;
  2427. result := cshlshrnode.create(shln,left,right);
  2428. end;
  2429. left := nil;
  2430. right := nil;
  2431. exit;
  2432. end;
  2433. { 2 booleans ? }
  2434. if is_boolean(ld) and is_boolean(rd) then
  2435. begin
  2436. if (not(cs_full_boolean_eval in current_settings.localswitches) or
  2437. (nf_short_bool in flags)) and
  2438. (nodetype in [andn,orn]) then
  2439. expectloc:=LOC_JUMP
  2440. else
  2441. begin
  2442. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2443. expectloc:=LOC_FLAGS
  2444. else
  2445. expectloc:=LOC_REGISTER;
  2446. end;
  2447. end
  2448. else
  2449. { Both are chars? only convert to shortstrings for addn }
  2450. if is_char(ld) then
  2451. begin
  2452. if nodetype=addn then
  2453. internalerror(200103291);
  2454. expectloc:=LOC_FLAGS;
  2455. end
  2456. {$ifndef cpu64bitaddr}
  2457. { is there a 64 bit type ? }
  2458. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  2459. begin
  2460. result := first_add64bitint;
  2461. if assigned(result) then
  2462. exit;
  2463. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2464. expectloc:=LOC_REGISTER
  2465. else
  2466. expectloc:=LOC_JUMP;
  2467. end
  2468. {$endif cpu64bitaddr}
  2469. {$ifndef cpuneedsmulhelper}
  2470. { is there a cardinal? }
  2471. else if (torddef(ld).ordtype=u32bit) then
  2472. begin
  2473. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2474. expectloc:=LOC_REGISTER
  2475. else
  2476. expectloc:=LOC_FLAGS;
  2477. end
  2478. {$endif cpuneedsmulhelper}
  2479. { generic s32bit conversion }
  2480. else
  2481. begin
  2482. {$ifdef cpuneedsmulhelper}
  2483. if (nodetype=muln) and not(torddef(resultdef).ordtype in [u8bit,s8bit]) then
  2484. begin
  2485. result := nil;
  2486. case torddef(resultdef).ordtype of
  2487. s16bit:
  2488. procname := 'fpc_mul_integer';
  2489. u16bit:
  2490. procname := 'fpc_mul_word';
  2491. s32bit:
  2492. procname := 'fpc_mul_longint';
  2493. u32bit:
  2494. procname := 'fpc_mul_dword';
  2495. else
  2496. internalerror(2011022301);
  2497. end;
  2498. result := ccallnode.createintern(procname,
  2499. ccallparanode.create(cordconstnode.create(0,pasbool8type,false),
  2500. ccallparanode.create(right,
  2501. ccallparanode.create(left,nil))));
  2502. left := nil;
  2503. right := nil;
  2504. firstpass(result);
  2505. exit;
  2506. end;
  2507. {$endif cpuneedsmulhelper}
  2508. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2509. expectloc:=LOC_REGISTER
  2510. else
  2511. expectloc:=LOC_FLAGS;
  2512. end;
  2513. end
  2514. { left side a setdef, must be before string processing,
  2515. else array constructor can be seen as array of char (PFV) }
  2516. else if (ld.typ=setdef) then
  2517. begin
  2518. { small sets are handled inline by the compiler.
  2519. small set doesn't have support for adding ranges }
  2520. if is_smallset(ld) and
  2521. not(
  2522. (right.nodetype=setelementn) and
  2523. assigned(tsetelementnode(right).right)
  2524. ) then
  2525. begin
  2526. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2527. expectloc:=LOC_FLAGS
  2528. else
  2529. expectloc:=LOC_REGISTER;
  2530. end
  2531. else
  2532. begin
  2533. result := first_addset;
  2534. if assigned(result) then
  2535. exit;
  2536. expectloc:=LOC_CREFERENCE;
  2537. end;
  2538. end
  2539. { compare pchar by addresses like BP/Delphi }
  2540. else if is_pchar(ld) then
  2541. begin
  2542. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2543. expectloc:=LOC_REGISTER
  2544. else
  2545. expectloc:=LOC_FLAGS;
  2546. end
  2547. { is one of the operands a string }
  2548. else if (ld.typ=stringdef) then
  2549. begin
  2550. if is_widestring(ld) then
  2551. begin
  2552. { this is only for add, the comparisaion is handled later }
  2553. expectloc:=LOC_REGISTER;
  2554. end
  2555. else if is_unicodestring(ld) then
  2556. begin
  2557. { this is only for add, the comparisaion is handled later }
  2558. expectloc:=LOC_REGISTER;
  2559. end
  2560. else if is_ansistring(ld) then
  2561. begin
  2562. { this is only for add, the comparisaion is handled later }
  2563. expectloc:=LOC_REGISTER;
  2564. end
  2565. else if is_longstring(ld) then
  2566. begin
  2567. { this is only for add, the comparisaion is handled later }
  2568. expectloc:=LOC_REFERENCE;
  2569. end
  2570. else
  2571. begin
  2572. {$ifdef addstringopt}
  2573. { can create a call which isn't handled by callparatemp }
  2574. if canbeaddsstringcharoptnode(self) then
  2575. begin
  2576. hp := genaddsstringcharoptnode(self);
  2577. pass_1 := hp;
  2578. exit;
  2579. end
  2580. else
  2581. {$endif addstringopt}
  2582. begin
  2583. { Fix right to be shortstring }
  2584. if is_char(right.resultdef) then
  2585. begin
  2586. inserttypeconv(right,cshortstringtype);
  2587. firstpass(right);
  2588. end;
  2589. end;
  2590. {$ifdef addstringopt}
  2591. { can create a call which isn't handled by callparatemp }
  2592. if canbeaddsstringcsstringoptnode(self) then
  2593. begin
  2594. hp := genaddsstringcsstringoptnode(self);
  2595. pass_1 := hp;
  2596. exit;
  2597. end;
  2598. {$endif addstringopt}
  2599. end;
  2600. { otherwise, let addstring convert everything }
  2601. result := first_addstring;
  2602. exit;
  2603. end
  2604. { is one a real float ? }
  2605. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  2606. begin
  2607. {$ifdef cpufpemu}
  2608. if (current_settings.fputype=fpu_soft) or (cs_fp_emulation in current_settings.moduleswitches) then
  2609. begin
  2610. result:=first_addfloat;
  2611. if assigned(result) then
  2612. exit;
  2613. end;
  2614. {$endif cpufpemu}
  2615. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2616. expectloc:=LOC_FPUREGISTER
  2617. else
  2618. expectloc:=LOC_FLAGS;
  2619. end
  2620. { pointer comperation and subtraction }
  2621. else if (ld.typ=pointerdef) then
  2622. begin
  2623. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2624. expectloc:=LOC_REGISTER
  2625. else
  2626. expectloc:=LOC_FLAGS;
  2627. end
  2628. else if is_implicit_pointer_object_type(ld) then
  2629. begin
  2630. expectloc:=LOC_FLAGS;
  2631. end
  2632. else if (ld.typ=classrefdef) then
  2633. begin
  2634. expectloc:=LOC_FLAGS;
  2635. end
  2636. { support procvar=nil,procvar<>nil }
  2637. else if ((ld.typ=procvardef) and (rt=niln)) or
  2638. ((rd.typ=procvardef) and (lt=niln)) then
  2639. begin
  2640. expectloc:=LOC_FLAGS;
  2641. end
  2642. {$ifdef SUPPORT_MMX}
  2643. { mmx support, this must be before the zero based array
  2644. check }
  2645. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  2646. is_mmx_able_array(rd) then
  2647. begin
  2648. expectloc:=LOC_MMXREGISTER;
  2649. end
  2650. {$endif SUPPORT_MMX}
  2651. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  2652. begin
  2653. expectloc:=LOC_REGISTER;
  2654. end
  2655. else if (rd.typ=procvardef) and
  2656. (ld.typ=procvardef) and
  2657. equal_defs(rd,ld) then
  2658. begin
  2659. expectloc:=LOC_FLAGS;
  2660. end
  2661. else if (ld.typ=enumdef) then
  2662. begin
  2663. expectloc:=LOC_FLAGS;
  2664. end
  2665. {$ifdef SUPPORT_MMX}
  2666. else if (cs_mmx in current_settings.localswitches) and
  2667. is_mmx_able_array(ld) and
  2668. is_mmx_able_array(rd) then
  2669. begin
  2670. expectloc:=LOC_MMXREGISTER;
  2671. end
  2672. {$endif SUPPORT_MMX}
  2673. { the general solution is to convert to 32 bit int }
  2674. else
  2675. begin
  2676. expectloc:=LOC_REGISTER;
  2677. end;
  2678. end;
  2679. {$ifdef state_tracking}
  2680. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2681. var factval:Tnode;
  2682. begin
  2683. track_state_pass:=false;
  2684. if left.track_state_pass(exec_known) then
  2685. begin
  2686. track_state_pass:=true;
  2687. left.resultdef:=nil;
  2688. do_typecheckpass(left);
  2689. end;
  2690. factval:=aktstate.find_fact(left);
  2691. if factval<>nil then
  2692. begin
  2693. track_state_pass:=true;
  2694. left.destroy;
  2695. left:=factval.getcopy;
  2696. end;
  2697. if right.track_state_pass(exec_known) then
  2698. begin
  2699. track_state_pass:=true;
  2700. right.resultdef:=nil;
  2701. do_typecheckpass(right);
  2702. end;
  2703. factval:=aktstate.find_fact(right);
  2704. if factval<>nil then
  2705. begin
  2706. track_state_pass:=true;
  2707. right.destroy;
  2708. right:=factval.getcopy;
  2709. end;
  2710. end;
  2711. {$endif}
  2712. end.