nadd.pas 154 KB

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