nadd.pas 136 KB

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