2
0

nadd.pas 109 KB

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