nadd.pas 137 KB

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