nadd.pas 109 KB

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