nadd.pas 156 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856
  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. function maybe_convert_to_insert:tnode;
  1142. function element_count(arrconstr: tarrayconstructornode):asizeint;
  1143. begin
  1144. result:=0;
  1145. while assigned(arrconstr) do
  1146. begin
  1147. if arrconstr.nodetype=arrayconstructorrangen then
  1148. internalerror(2018052501);
  1149. inc(result);
  1150. arrconstr:=tarrayconstructornode(tarrayconstructornode(arrconstr).right);
  1151. end;
  1152. end;
  1153. var
  1154. elem : tnode;
  1155. para : tcallparanode;
  1156. isarrconstrl,
  1157. isarrconstrr : boolean;
  1158. index : asizeint;
  1159. begin
  1160. result:=nil;
  1161. isarrconstrl:=left.nodetype=arrayconstructorn;
  1162. isarrconstrr:=right.nodetype=arrayconstructorn;
  1163. if not assigned(aktassignmentnode) or
  1164. (aktassignmentnode.right<>self) or
  1165. not(
  1166. isarrconstrl or
  1167. isarrconstrr
  1168. ) or
  1169. not(
  1170. left.isequal(aktassignmentnode.left) or
  1171. right.isequal(aktassignmentnode.left)
  1172. ) or
  1173. not valid_for_var(aktassignmentnode.left,false) or
  1174. (isarrconstrl and (element_count(tarrayconstructornode(left))>1)) or
  1175. (isarrconstrr and (element_count(tarrayconstructornode(right))>1)) then
  1176. exit;
  1177. if isarrconstrl then
  1178. begin
  1179. index:=0;
  1180. elem:=tarrayconstructornode(left).left;
  1181. tarrayconstructornode(left).left:=nil;
  1182. end
  1183. else
  1184. begin
  1185. index:=high(asizeint);
  1186. elem:=tarrayconstructornode(right).left;
  1187. tarrayconstructornode(right).left:=nil;
  1188. end;
  1189. { we use the fact that insert() caps the index to avoid a copy }
  1190. para:=ccallparanode.create(
  1191. cordconstnode.create(index,sizesinttype,false),
  1192. ccallparanode.create(
  1193. aktassignmentnode.left.getcopy,
  1194. ccallparanode.create(
  1195. elem,nil)));
  1196. result:=cinlinenode.create(in_insert_x_y_z,false,para);
  1197. include(aktassignmentnode.flags,nf_assign_done_in_right);
  1198. end;
  1199. begin
  1200. result:=nil;
  1201. rlow:=0;
  1202. llow:=0;
  1203. rhigh:=0;
  1204. lhigh:=0;
  1205. { avoid any problems with type parameters later on }
  1206. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  1207. begin
  1208. resultdef:=cundefinedtype;
  1209. exit;
  1210. end;
  1211. { both left and right need to be valid }
  1212. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1213. set_varstate(right,vs_read,[vsf_must_be_valid]);
  1214. if codegenerror then
  1215. exit;
  1216. { tp procvar support. Omit for converted assigned() nodes }
  1217. if not (nf_load_procvar in flags) then
  1218. begin
  1219. maybe_call_procvar(left,true);
  1220. maybe_call_procvar(right,true);
  1221. end
  1222. else
  1223. if not (nodetype in [equaln,unequaln]) then
  1224. InternalError(2013091601);
  1225. { convert array constructors to sets, because there is no other operator
  1226. possible for array constructors }
  1227. if not(is_dynamic_array(right.resultdef)) and is_array_constructor(left.resultdef) then
  1228. begin
  1229. arrayconstructor_to_set(left);
  1230. typecheckpass(left);
  1231. end;
  1232. if not(is_dynamic_array(left.resultdef)) and is_array_constructor(right.resultdef) then
  1233. begin
  1234. arrayconstructor_to_set(right);
  1235. typecheckpass(right);
  1236. end;
  1237. { allow operator overloading }
  1238. hp:=self;
  1239. if isbinaryoverloaded(hp,[]) then
  1240. begin
  1241. result:=hp;
  1242. exit;
  1243. end;
  1244. { Stop checking when an error was found in the operator checking }
  1245. if codegenerror then
  1246. begin
  1247. result:=cerrornode.create;
  1248. exit;
  1249. end;
  1250. { Kylix allows enum+ordconstn in an enum type declaration, we need to do
  1251. the conversion here before the constant folding }
  1252. if (m_delphi in current_settings.modeswitches) and
  1253. (blocktype in [bt_type,bt_const_type,bt_var_type]) then
  1254. begin
  1255. if (left.resultdef.typ=enumdef) and
  1256. (right.resultdef.typ=orddef) then
  1257. begin
  1258. { insert explicit typecast to default signed int }
  1259. left:=ctypeconvnode.create_internal(left,sinttype);
  1260. typecheckpass(left);
  1261. end
  1262. else
  1263. if (left.resultdef.typ=orddef) and
  1264. (right.resultdef.typ=enumdef) then
  1265. begin
  1266. { insert explicit typecast to default signed int }
  1267. right:=ctypeconvnode.create_internal(right,sinttype);
  1268. typecheckpass(right);
  1269. end;
  1270. end;
  1271. { is one a real float, then both need to be floats, this
  1272. need to be done before the constant folding so constant
  1273. operation on a float and int are also handled }
  1274. {$ifdef x86}
  1275. { use extended as default real type only when the x87 fpu is used }
  1276. {$if defined(i386) or defined(i8086)}
  1277. if not(current_settings.fputype=fpu_x87) then
  1278. resultrealdef:=s64floattype
  1279. else
  1280. resultrealdef:=pbestrealtype^;
  1281. {$endif i386 or i8086}
  1282. {$ifdef x86_64}
  1283. { x86-64 has no x87 only mode, so use always double as default }
  1284. resultrealdef:=s64floattype;
  1285. {$endif x86_6}
  1286. {$else not x86}
  1287. resultrealdef:=pbestrealtype^;
  1288. {$endif not x86}
  1289. if (right.resultdef.typ=floatdef) or (left.resultdef.typ=floatdef) then
  1290. begin
  1291. { when both floattypes are already equal then use that
  1292. floattype for results }
  1293. if (right.resultdef.typ=floatdef) and
  1294. (left.resultdef.typ=floatdef) and
  1295. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype) then
  1296. resultrealdef:=left.resultdef
  1297. { when there is a currency type then use currency, but
  1298. only when currency is defined as float }
  1299. else
  1300. if (is_currency(right.resultdef) or
  1301. is_currency(left.resultdef)) and
  1302. ((s64currencytype.typ = floatdef) or
  1303. (nodetype <> slashn)) then
  1304. begin
  1305. resultrealdef:=s64currencytype;
  1306. inserttypeconv(right,resultrealdef);
  1307. inserttypeconv(left,resultrealdef);
  1308. end
  1309. else
  1310. begin
  1311. resultrealdef:=getbestreal(left.resultdef,right.resultdef);
  1312. inserttypeconv(right,resultrealdef);
  1313. inserttypeconv(left,resultrealdef);
  1314. end;
  1315. end;
  1316. { If both operands are constant and there is a unicodestring
  1317. or unicodestring then convert everything to unicodestring }
  1318. if is_constnode(right) and is_constnode(left) and
  1319. (is_unicodestring(right.resultdef) or
  1320. is_unicodestring(left.resultdef)) then
  1321. begin
  1322. inserttypeconv(right,cunicodestringtype);
  1323. inserttypeconv(left,cunicodestringtype);
  1324. end;
  1325. { If both operands are constant and there is a widechar
  1326. or widestring then convert everything to widestring. This
  1327. allows constant folding like char+widechar }
  1328. if is_constnode(right) and is_constnode(left) and
  1329. (is_widestring(right.resultdef) or
  1330. is_widestring(left.resultdef) or
  1331. is_widechar(right.resultdef) or
  1332. is_widechar(left.resultdef)) then
  1333. begin
  1334. inserttypeconv(right,cwidestringtype);
  1335. inserttypeconv(left,cwidestringtype);
  1336. end;
  1337. { load easier access variables }
  1338. rd:=right.resultdef;
  1339. ld:=left.resultdef;
  1340. rt:=right.nodetype;
  1341. lt:=left.nodetype;
  1342. { 4 character constant strings are compatible with orddef }
  1343. { in macpas mode (become cardinals) }
  1344. if (m_mac in current_settings.modeswitches) and
  1345. { only allow for comparisons, additions etc are }
  1346. { normally program errors }
  1347. (nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) and
  1348. (((lt=stringconstn) and
  1349. (tstringconstnode(left).len=4) and
  1350. (rd.typ=orddef)) or
  1351. ((rt=stringconstn) and
  1352. (tstringconstnode(right).len=4) and
  1353. (ld.typ=orddef))) then
  1354. begin
  1355. if (rt=stringconstn) then
  1356. begin
  1357. inserttypeconv(right,u32inttype);
  1358. rt:=right.nodetype;
  1359. rd:=right.resultdef;
  1360. end
  1361. else
  1362. begin
  1363. inserttypeconv(left,u32inttype);
  1364. lt:=left.nodetype;
  1365. ld:=left.resultdef;
  1366. end;
  1367. end;
  1368. { but an int/int gives real/real! }
  1369. if (nodetype=slashn) and not(is_vector(left.resultdef)) and not(is_vector(right.resultdef)) then
  1370. begin
  1371. if is_currency(left.resultdef) and
  1372. is_currency(right.resultdef) then
  1373. { In case of currency, converting to float means dividing by 10000 }
  1374. { However, since this is already a division, both divisions by }
  1375. { 10000 are eliminated when we divide the results -> we can skip }
  1376. { them. }
  1377. if s64currencytype.typ = floatdef then
  1378. begin
  1379. { there's no s64comptype or so, how do we avoid the type conversion?
  1380. left.resultdef := s64comptype;
  1381. right.resultdef := s64comptype; }
  1382. end
  1383. else
  1384. begin
  1385. left.resultdef := s64inttype;
  1386. right.resultdef := s64inttype;
  1387. end;
  1388. inserttypeconv(right,resultrealdef);
  1389. inserttypeconv(left,resultrealdef);
  1390. end
  1391. { if both are orddefs then check sub types }
  1392. else if (ld.typ=orddef) and (rd.typ=orddef) then
  1393. begin
  1394. { set for & and | operations in macpas mode: they only work on }
  1395. { booleans, and always short circuit evaluation }
  1396. if (nf_short_bool in flags) then
  1397. begin
  1398. if not is_boolean(ld) then
  1399. begin
  1400. inserttypeconv(left,pasbool8type);
  1401. ld := left.resultdef;
  1402. end;
  1403. if not is_boolean(rd) then
  1404. begin
  1405. inserttypeconv(right,pasbool8type);
  1406. rd := right.resultdef;
  1407. end;
  1408. end;
  1409. { 2 booleans? }
  1410. if (is_boolean(ld) and is_boolean(rd)) then
  1411. begin
  1412. case nodetype of
  1413. xorn,
  1414. andn,
  1415. orn:
  1416. begin
  1417. { Make sides equal to the largest boolean }
  1418. if (torddef(left.resultdef).size>torddef(right.resultdef).size) or
  1419. (is_cbool(left.resultdef) and not is_cbool(right.resultdef)) then
  1420. begin
  1421. right:=ctypeconvnode.create_internal(right,left.resultdef);
  1422. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  1423. typecheckpass(right);
  1424. end
  1425. else if (torddef(left.resultdef).size<torddef(right.resultdef).size) or
  1426. (not is_cbool(left.resultdef) and is_cbool(right.resultdef)) then
  1427. begin
  1428. left:=ctypeconvnode.create_internal(left,right.resultdef);
  1429. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  1430. typecheckpass(left);
  1431. end;
  1432. end;
  1433. ltn,
  1434. lten,
  1435. gtn,
  1436. gten:
  1437. begin
  1438. { convert both to pasbool to perform the comparison (so
  1439. that longbool(4) = longbool(2), since both represent
  1440. "true" }
  1441. inserttypeconv(left,pasbool8type);
  1442. inserttypeconv(right,pasbool8type);
  1443. end;
  1444. unequaln,
  1445. equaln:
  1446. begin
  1447. if not(cs_full_boolean_eval in current_settings.localswitches) or
  1448. (nf_short_bool in flags) then
  1449. begin
  1450. { Remove any compares with constants }
  1451. if (left.nodetype=ordconstn) then
  1452. begin
  1453. hp:=right;
  1454. b:=(tordconstnode(left).value<>0);
  1455. ot:=nodetype;
  1456. left.free;
  1457. left:=nil;
  1458. right:=nil;
  1459. if (not(b) and (ot=equaln)) or
  1460. (b and (ot=unequaln)) then
  1461. begin
  1462. hp:=cnotnode.create(hp);
  1463. end;
  1464. result:=hp;
  1465. exit;
  1466. end;
  1467. if (right.nodetype=ordconstn) then
  1468. begin
  1469. hp:=left;
  1470. b:=(tordconstnode(right).value<>0);
  1471. ot:=nodetype;
  1472. right.free;
  1473. right:=nil;
  1474. left:=nil;
  1475. if (not(b) and (ot=equaln)) or
  1476. (b and (ot=unequaln)) then
  1477. begin
  1478. hp:=cnotnode.create(hp);
  1479. end;
  1480. result:=hp;
  1481. exit;
  1482. end;
  1483. end;
  1484. { Delphi-compatibility: convert both to pasbool to
  1485. perform the equality comparison }
  1486. inserttypeconv(left,pasbool8type);
  1487. inserttypeconv(right,pasbool8type);
  1488. end;
  1489. else
  1490. begin
  1491. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1492. result:=cnothingnode.create;
  1493. exit;
  1494. end;
  1495. end;
  1496. end
  1497. { Both are chars? }
  1498. else if is_char(rd) and is_char(ld) then
  1499. begin
  1500. if nodetype=addn then
  1501. begin
  1502. resultdef:=cshortstringtype;
  1503. if not(is_constcharnode(left) and is_constcharnode(right)) then
  1504. begin
  1505. inserttypeconv(left,cshortstringtype);
  1506. {$ifdef addstringopt}
  1507. hp := genaddsstringcharoptnode(self);
  1508. result := hp;
  1509. exit;
  1510. {$endif addstringopt}
  1511. end
  1512. end
  1513. else if not(nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) then
  1514. begin
  1515. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1516. result:=cnothingnode.create;
  1517. exit;
  1518. end;
  1519. end
  1520. { There is a widechar? }
  1521. else if is_widechar(rd) or is_widechar(ld) then
  1522. begin
  1523. { widechar+widechar gives unicodestring }
  1524. if nodetype=addn then
  1525. begin
  1526. inserttypeconv(left,cunicodestringtype);
  1527. if (torddef(rd).ordtype<>uwidechar) then
  1528. inserttypeconv(right,cwidechartype);
  1529. resultdef:=cunicodestringtype;
  1530. end
  1531. else
  1532. begin
  1533. if (torddef(ld).ordtype<>uwidechar) then
  1534. inserttypeconv(left,cwidechartype);
  1535. if (torddef(rd).ordtype<>uwidechar) then
  1536. inserttypeconv(right,cwidechartype);
  1537. end;
  1538. end
  1539. { is there a currency type ? }
  1540. else if ((torddef(rd).ordtype=scurrency) or (torddef(ld).ordtype=scurrency)) then
  1541. begin
  1542. if (torddef(ld).ordtype<>scurrency) then
  1543. inserttypeconv(left,s64currencytype);
  1544. if (torddef(rd).ordtype<>scurrency) then
  1545. inserttypeconv(right,s64currencytype);
  1546. end
  1547. { leave some constant integer expressions alone in case the
  1548. resultdef of the integer types doesn't influence the outcome,
  1549. because the forced type conversions below can otherwise result
  1550. in unexpected results (such as high(qword)<high(int64) returning
  1551. true because high(qword) gets converted to int64) }
  1552. else if is_integer(ld) and is_integer(rd) and
  1553. (lt=ordconstn) and (rt=ordconstn) and
  1554. (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) then
  1555. begin
  1556. end
  1557. { "and" does't care about the sign of integers }
  1558. { "xor", "or" and compares don't need extension to native int }
  1559. { size either as long as both values are signed or unsigned }
  1560. { "xor" and "or" also don't care about the sign if the values }
  1561. { occupy an entire register }
  1562. { don't do it if either type is 64 bit (except for "and"), }
  1563. { since in that case we can't safely find a "common" type }
  1564. else if is_integer(ld) and is_integer(rd) and
  1565. ((nodetype=andn) or
  1566. ((nodetype in [orn,xorn,equaln,unequaln,gtn,gten,ltn,lten]) and
  1567. not is_64bitint(ld) and not is_64bitint(rd) and
  1568. (is_signed(ld)=is_signed(rd)))) then
  1569. begin
  1570. { Delphi-compatible: prefer unsigned type for "and", when the
  1571. unsigned type is bigger than the signed one, and also bigger
  1572. than min(native_int, 32-bit) }
  1573. if (is_oversizedint(rd) or is_nativeint(rd) or is_32bitint(rd)) and
  1574. (rd.size>=ld.size) and
  1575. not is_signed(rd) and is_signed(ld) then
  1576. inserttypeconv_internal(left,rd)
  1577. else if (is_oversizedint(ld) or is_nativeint(ld) or is_32bitint(ld)) and
  1578. (ld.size>=rd.size) and
  1579. not is_signed(ld) and is_signed(rd) then
  1580. inserttypeconv_internal(right,ld)
  1581. else
  1582. begin
  1583. { not to left right.resultdef, because that may
  1584. cause a range error if left and right's def don't
  1585. completely overlap }
  1586. nd:=get_common_intdef(torddef(ld),torddef(rd),true);
  1587. inserttypeconv(left,nd);
  1588. inserttypeconv(right,nd);
  1589. end;
  1590. end
  1591. { don't extend (sign-mismatched) comparisons if either side is a constant
  1592. whose value is within range of opposite side }
  1593. else if is_integer(ld) and is_integer(rd) and
  1594. (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1595. (is_signed(ld)<>is_signed(rd)) and
  1596. (
  1597. ((lt=ordconstn) and maybe_cast_ordconst(left,rd)) or
  1598. ((rt=ordconstn) and maybe_cast_ordconst(right,ld))
  1599. ) then
  1600. begin
  1601. { done here }
  1602. end
  1603. { is there a signed 64 bit type ? }
  1604. else if ((torddef(rd).ordtype=s64bit) or (torddef(ld).ordtype=s64bit)) then
  1605. begin
  1606. if (torddef(ld).ordtype<>s64bit) then
  1607. inserttypeconv(left,s64inttype);
  1608. if (torddef(rd).ordtype<>s64bit) then
  1609. inserttypeconv(right,s64inttype);
  1610. end
  1611. { is there a unsigned 64 bit type ? }
  1612. else if ((torddef(rd).ordtype=u64bit) or (torddef(ld).ordtype=u64bit)) then
  1613. begin
  1614. if (torddef(ld).ordtype<>u64bit) then
  1615. inserttypeconv(left,u64inttype);
  1616. if (torddef(rd).ordtype<>u64bit) then
  1617. inserttypeconv(right,u64inttype);
  1618. end
  1619. { is there a larger int? }
  1620. else if is_oversizedint(rd) or is_oversizedint(ld) then
  1621. begin
  1622. nd:=get_common_intdef(torddef(ld),torddef(rd),false);
  1623. inserttypeconv(right,nd);
  1624. inserttypeconv(left,nd);
  1625. end
  1626. { is there a native unsigned int? }
  1627. else if is_nativeuint(rd) or is_nativeuint(ld) then
  1628. begin
  1629. { convert positive constants to uinttype }
  1630. if (not is_nativeuint(ld)) and
  1631. is_constintnode(left) and
  1632. (tordconstnode(left).value >= 0) then
  1633. inserttypeconv(left,uinttype);
  1634. if (not is_nativeuint(rd)) and
  1635. is_constintnode(right) and
  1636. (tordconstnode(right).value >= 0) then
  1637. inserttypeconv(right,uinttype);
  1638. { when one of the operand is signed or the operation is subn then perform
  1639. the operation in a larger signed type, can't use rd/ld here because there
  1640. could be already typeconvs inserted.
  1641. This is compatible with the code below for other unsigned types (PFV) }
  1642. if is_signed(left.resultdef) or
  1643. is_signed(right.resultdef) or
  1644. (nodetype=subn) then
  1645. begin
  1646. if nodetype<>subn then
  1647. CGMessage(type_h_mixed_signed_unsigned);
  1648. { mark as internal in case added for a subn, so }
  1649. { ttypeconvnode.simplify can remove the larger }
  1650. { typecast again if semantically correct. Even }
  1651. { if we could detect that here already, we }
  1652. { mustn't do it here because that would change }
  1653. { overload choosing behaviour etc. The code in }
  1654. { ncnv.pas is run after that is already decided }
  1655. if (not is_signed(left.resultdef) and
  1656. not is_signed(right.resultdef)) or
  1657. (nodetype in [orn,xorn]) then
  1658. include(flags,nf_internal);
  1659. { get next larger signed int type }
  1660. nd:=get_common_intdef(torddef(sinttype),torddef(uinttype),false);
  1661. inserttypeconv(left,nd);
  1662. inserttypeconv(right,nd);
  1663. end
  1664. else
  1665. begin
  1666. if not is_nativeuint(left.resultdef) then
  1667. inserttypeconv(left,uinttype);
  1668. if not is_nativeuint(right.resultdef) then
  1669. inserttypeconv(right,uinttype);
  1670. end;
  1671. end
  1672. { generic ord conversion is sinttype }
  1673. else
  1674. begin
  1675. { When there is a signed type or there is a minus operation
  1676. we convert to signed int. Otherwise (both are unsigned) we keep
  1677. the result also unsigned. This is compatible with Delphi (PFV) }
  1678. if is_signed(ld) or
  1679. is_signed(rd) or
  1680. (nodetype=subn) then
  1681. begin
  1682. inserttypeconv(right,sinttype);
  1683. inserttypeconv(left,sinttype);
  1684. end
  1685. else
  1686. begin
  1687. inserttypeconv(right,uinttype);
  1688. inserttypeconv(left,uinttype);
  1689. end;
  1690. end;
  1691. end
  1692. { if both are floatdefs, conversion is already done before constant folding }
  1693. else if (ld.typ=floatdef) then
  1694. begin
  1695. if not(nodetype in [addn,subn,muln,slashn,equaln,unequaln,ltn,lten,gtn,gten]) then
  1696. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1697. end
  1698. { left side a setdef, must be before string processing,
  1699. else array constructor can be seen as array of char (PFV) }
  1700. else if (ld.typ=setdef) then
  1701. begin
  1702. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  1703. CGMessage(type_e_set_operation_unknown);
  1704. { right must either be a set or a set element }
  1705. if (rd.typ<>setdef) and
  1706. (rt<>setelementn) then
  1707. CGMessage(type_e_mismatch)
  1708. { Make operands the same setdef. If one's elementtype fits }
  1709. { entirely inside the other's, pick the one with the largest }
  1710. { range. Otherwise create a new setdef with a range which }
  1711. { can contain both. }
  1712. else if not(equal_defs(ld,rd)) then
  1713. begin
  1714. { note: ld cannot be an empty set with elementdef=nil in }
  1715. { case right is not a set, arrayconstructor_to_set takes }
  1716. { care of that }
  1717. { 1: rd is a set with an assigned elementdef, and ld is }
  1718. { either an empty set without elementdef or a set whose }
  1719. { elementdef fits in rd's elementdef -> convert to rd }
  1720. if ((rd.typ=setdef) and
  1721. assigned(tsetdef(rd).elementdef) and
  1722. (not assigned(tsetdef(ld).elementdef) or
  1723. is_in_limit(ld,rd))) then
  1724. inserttypeconv(left,rd)
  1725. { 2: rd is either an empty set without elementdef or a set }
  1726. { whose elementdef fits in ld's elementdef, or a set }
  1727. { element whose def fits in ld's elementdef -> convert }
  1728. { to ld. ld's elementdef can't be nil here, is caught }
  1729. { previous case and "note:" above }
  1730. else if ((rd.typ=setdef) and
  1731. (not assigned(tsetdef(rd).elementdef) or
  1732. is_in_limit(rd,ld))) or
  1733. ((rd.typ<>setdef) and
  1734. is_in_limit(rd,tsetdef(ld).elementdef)) then
  1735. if (rd.typ=setdef) then
  1736. inserttypeconv(right,ld)
  1737. else
  1738. inserttypeconv(right,tsetdef(ld).elementdef)
  1739. { 3: otherwise create setdef which encompasses both, taking }
  1740. { into account empty sets without elementdef }
  1741. else
  1742. begin
  1743. if assigned(tsetdef(ld).elementdef) then
  1744. begin
  1745. llow:=tsetdef(ld).setbase;
  1746. lhigh:=tsetdef(ld).setmax;
  1747. end;
  1748. if (rd.typ=setdef) then
  1749. if assigned(tsetdef(rd).elementdef) then
  1750. begin
  1751. rlow:=tsetdef(rd).setbase;
  1752. rhigh:=tsetdef(rd).setmax;
  1753. end
  1754. else
  1755. begin
  1756. { ld's elementdef must have been valid }
  1757. rlow:=llow;
  1758. rhigh:=lhigh;
  1759. end
  1760. else
  1761. getrange(rd,rlow,rhigh);
  1762. if not assigned(tsetdef(ld).elementdef) then
  1763. begin
  1764. llow:=rlow;
  1765. lhigh:=rhigh;
  1766. end;
  1767. nd:=csetdef.create(tsetdef(ld).elementdef,min(llow,rlow).svalue,max(lhigh,rhigh).svalue,true);
  1768. inserttypeconv(left,nd);
  1769. if (rd.typ=setdef) then
  1770. inserttypeconv(right,nd)
  1771. else
  1772. inserttypeconv(right,tsetdef(nd).elementdef);
  1773. end;
  1774. end;
  1775. end
  1776. { pointer comparision and subtraction }
  1777. else if (
  1778. (rd.typ=pointerdef) and (ld.typ=pointerdef)
  1779. ) or
  1780. { compare/add pchar to variable (not stringconst) char arrays
  1781. by addresses like BP/Delphi }
  1782. (
  1783. (nodetype in [equaln,unequaln,subn,addn]) and
  1784. (
  1785. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd) and (rt<>stringconstn)) or
  1786. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld) and (lt<>stringconstn))
  1787. )
  1788. ) then
  1789. begin
  1790. { convert char array to pointer }
  1791. if is_chararray(rd) then
  1792. begin
  1793. inserttypeconv(right,charpointertype);
  1794. rd:=right.resultdef;
  1795. end
  1796. else if is_chararray(ld) then
  1797. begin
  1798. inserttypeconv(left,charpointertype);
  1799. ld:=left.resultdef;
  1800. end;
  1801. case nodetype of
  1802. equaln,unequaln :
  1803. begin
  1804. if is_voidpointer(right.resultdef) then
  1805. inserttypeconv(right,left.resultdef)
  1806. else if is_voidpointer(left.resultdef) then
  1807. inserttypeconv(left,right.resultdef)
  1808. else if not(equal_defs(ld,rd)) then
  1809. IncompatibleTypes(ld,rd);
  1810. { now that the type checking is done, convert both to charpointer, }
  1811. { because methodpointers are 8 bytes even though only the first 4 }
  1812. { bytes must be compared. This can happen here if we are in }
  1813. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  1814. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  1815. { optimized away, since the result already was a voidpointer, so }
  1816. { use a charpointer instead (JM) }
  1817. {$if defined(jvm)}
  1818. inserttypeconv_internal(left,java_jlobject);
  1819. inserttypeconv_internal(right,java_jlobject);
  1820. {$elseif defined(i8086)}
  1821. if is_hugepointer(left.resultdef) then
  1822. inserttypeconv_internal(left,charhugepointertype)
  1823. else if is_farpointer(left.resultdef) then
  1824. inserttypeconv_internal(left,charfarpointertype)
  1825. else
  1826. inserttypeconv_internal(left,charnearpointertype);
  1827. if is_hugepointer(right.resultdef) then
  1828. inserttypeconv_internal(right,charhugepointertype)
  1829. else if is_farpointer(right.resultdef) then
  1830. inserttypeconv_internal(right,charfarpointertype)
  1831. else
  1832. inserttypeconv_internal(right,charnearpointertype);
  1833. {$else}
  1834. inserttypeconv_internal(left,charpointertype);
  1835. inserttypeconv_internal(right,charpointertype);
  1836. {$endif jvm}
  1837. end;
  1838. ltn,lten,gtn,gten:
  1839. begin
  1840. if (cs_extsyntax in current_settings.moduleswitches) or
  1841. (nf_internal in flags) then
  1842. begin
  1843. if is_voidpointer(right.resultdef) then
  1844. inserttypeconv(right,left.resultdef)
  1845. else if is_voidpointer(left.resultdef) then
  1846. inserttypeconv(left,right.resultdef)
  1847. else if not(equal_defs(ld,rd)) then
  1848. IncompatibleTypes(ld,rd);
  1849. end
  1850. else
  1851. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1852. end;
  1853. subn:
  1854. begin
  1855. if (cs_extsyntax in current_settings.moduleswitches) then
  1856. begin
  1857. if is_voidpointer(right.resultdef) then
  1858. begin
  1859. if is_big_untyped_addrnode(right) then
  1860. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1861. inserttypeconv(right,left.resultdef)
  1862. end
  1863. else if is_voidpointer(left.resultdef) then
  1864. inserttypeconv(left,right.resultdef)
  1865. else if not(equal_defs(ld,rd)) then
  1866. IncompatibleTypes(ld,rd);
  1867. end
  1868. else
  1869. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1870. if not(nf_has_pointerdiv in flags) and
  1871. (tpointerdef(rd).pointeddef.size>1) then
  1872. begin
  1873. hp:=getcopy;
  1874. include(hp.flags,nf_has_pointerdiv);
  1875. result:=cmoddivnode.create(divn,hp,
  1876. cordconstnode.create(tpointerdef(rd).pointeddef.size,tpointerdef(rd).pointer_subtraction_result_type,false));
  1877. end;
  1878. resultdef:=tpointerdef(rd).pointer_subtraction_result_type;
  1879. exit;
  1880. end;
  1881. else
  1882. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1883. end;
  1884. end
  1885. { is one of the operands a string?,
  1886. chararrays are also handled as strings (after conversion), also take
  1887. care of chararray+chararray and chararray+char.
  1888. Note: Must be done after pointerdef+pointerdef has been checked, else
  1889. pchar is converted to string }
  1890. else if (rd.typ=stringdef) or
  1891. (ld.typ=stringdef) or
  1892. { stringconstn's can be arraydefs }
  1893. (lt=stringconstn) or
  1894. (rt=stringconstn) or
  1895. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1896. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1897. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1898. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1899. begin
  1900. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1901. begin
  1902. { Is there a unicodestring? }
  1903. if is_unicodestring(rd) or is_unicodestring(ld) or
  1904. ((m_default_unicodestring in current_settings.modeswitches) and
  1905. (cs_refcountedstrings in current_settings.localswitches) and
  1906. (
  1907. is_pwidechar(rd) or is_widechararray(rd) or is_open_widechararray(rd) or (lt = stringconstn) or
  1908. is_pwidechar(ld) or is_widechararray(ld) or is_open_widechararray(ld) or (rt = stringconstn)
  1909. )
  1910. ) then
  1911. strtype:=st_unicodestring
  1912. else
  1913. { Is there a widestring? }
  1914. if is_widestring(rd) or is_widestring(ld) or
  1915. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1916. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1917. strtype:=st_widestring
  1918. else
  1919. if is_ansistring(rd) or is_ansistring(ld) or
  1920. ((cs_refcountedstrings in current_settings.localswitches) and
  1921. //todo: Move some of this to longstring's then they are implemented?
  1922. (
  1923. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or (lt = stringconstn) or
  1924. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld) or (rt = stringconstn)
  1925. )
  1926. ) then
  1927. strtype:=st_ansistring
  1928. else
  1929. if is_longstring(rd) or is_longstring(ld) then
  1930. strtype:=st_longstring
  1931. else
  1932. begin
  1933. { TODO: todo: add a warning/hint here if one converting a too large array}
  1934. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1935. Note: Delphi halts with error if "array [0..xx] of char"
  1936. is assigned to ShortString and string length is less
  1937. then array size }
  1938. strtype:= st_shortstring;
  1939. end;
  1940. // Now convert nodes to common string type
  1941. case strtype of
  1942. st_widestring :
  1943. begin
  1944. if not(is_widestring(rd)) then
  1945. inserttypeconv(right,cwidestringtype);
  1946. if not(is_widestring(ld)) then
  1947. inserttypeconv(left,cwidestringtype);
  1948. end;
  1949. st_unicodestring :
  1950. begin
  1951. if not(is_unicodestring(rd)) then
  1952. inserttypeconv(right,cunicodestringtype);
  1953. if not(is_unicodestring(ld)) then
  1954. inserttypeconv(left,cunicodestringtype);
  1955. end;
  1956. st_ansistring :
  1957. begin
  1958. { use same code page if possible (don't force same code
  1959. page in case both are ansistrings with code page <>
  1960. CP_NONE, since then data loss can occur: the ansistring
  1961. helpers will convert them at run time to an encoding
  1962. that can represent both encodings) }
  1963. if is_ansistring(ld) and
  1964. (tstringdef(ld).encoding<>0) and
  1965. (tstringdef(ld).encoding<>globals.CP_NONE) and
  1966. (not is_ansistring(rd) or
  1967. (tstringdef(rd).encoding=0) or
  1968. (tstringdef(rd).encoding=globals.CP_NONE)) then
  1969. inserttypeconv(right,ld)
  1970. else if is_ansistring(rd) and
  1971. (tstringdef(rd).encoding<>0) and
  1972. (tstringdef(rd).encoding<>globals.CP_NONE) and
  1973. (not is_ansistring(ld) or
  1974. (tstringdef(ld).encoding=0) or
  1975. (tstringdef(ld).encoding=globals.CP_NONE)) then
  1976. inserttypeconv(left,rd)
  1977. else
  1978. begin
  1979. if not is_ansistring(ld) then
  1980. inserttypeconv(left,getansistringdef);
  1981. if not is_ansistring(rd) then
  1982. inserttypeconv(right,getansistringdef);
  1983. end;
  1984. end;
  1985. st_longstring :
  1986. begin
  1987. if not(is_longstring(rd)) then
  1988. inserttypeconv(right,clongstringtype);
  1989. if not(is_longstring(ld)) then
  1990. inserttypeconv(left,clongstringtype);
  1991. end;
  1992. st_shortstring :
  1993. begin
  1994. if not(is_shortstring(ld)) then
  1995. inserttypeconv(left,cshortstringtype);
  1996. { don't convert char, that can be handled by the optimized node }
  1997. if not(is_shortstring(rd) or is_char(rd)) then
  1998. inserttypeconv(right,cshortstringtype);
  1999. end;
  2000. else
  2001. internalerror(2005101);
  2002. end;
  2003. end
  2004. else
  2005. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2006. end
  2007. { implicit pointer object type comparison }
  2008. else if is_implicit_pointer_object_type(rd) or is_implicit_pointer_object_type(ld) then
  2009. begin
  2010. if (nodetype in [equaln,unequaln]) then
  2011. begin
  2012. if is_implicit_pointer_object_type(rd) and is_implicit_pointer_object_type(ld) then
  2013. begin
  2014. if def_is_related(tobjectdef(rd),tobjectdef(ld)) then
  2015. inserttypeconv(right,left.resultdef)
  2016. else
  2017. inserttypeconv(left,right.resultdef);
  2018. end
  2019. else if is_implicit_pointer_object_type(rd) then
  2020. inserttypeconv(left,right.resultdef)
  2021. else
  2022. inserttypeconv(right,left.resultdef);
  2023. end
  2024. else
  2025. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2026. end
  2027. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  2028. begin
  2029. if (nodetype in [equaln,unequaln]) then
  2030. begin
  2031. if def_is_related(tobjectdef(tclassrefdef(rd).pointeddef),
  2032. tobjectdef(tclassrefdef(ld).pointeddef)) then
  2033. inserttypeconv(right,left.resultdef)
  2034. else
  2035. inserttypeconv(left,right.resultdef);
  2036. end
  2037. else
  2038. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2039. end
  2040. { allow comparison with nil pointer }
  2041. else if is_implicit_pointer_object_type(rd) or (rd.typ=classrefdef) then
  2042. begin
  2043. if (nodetype in [equaln,unequaln]) then
  2044. inserttypeconv(left,right.resultdef)
  2045. else
  2046. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2047. end
  2048. else if is_implicit_pointer_object_type(ld) or (ld.typ=classrefdef) then
  2049. begin
  2050. if (nodetype in [equaln,unequaln]) then
  2051. inserttypeconv(right,left.resultdef)
  2052. else
  2053. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2054. end
  2055. { support procvar=nil,procvar<>nil }
  2056. else if ((ld.typ=procvardef) and (rt=niln)) or
  2057. ((rd.typ=procvardef) and (lt=niln)) then
  2058. begin
  2059. if not(nodetype in [equaln,unequaln]) then
  2060. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2061. { find proc field in methodpointer record }
  2062. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  2063. if not assigned(hsym) then
  2064. internalerror(200412043);
  2065. { For methodpointers compare only tmethodpointer.proc }
  2066. if (rd.typ=procvardef) and
  2067. (not tprocvardef(rd).is_addressonly) then
  2068. begin
  2069. right:=csubscriptnode.create(
  2070. hsym,
  2071. ctypeconvnode.create_internal(right,methodpointertype));
  2072. typecheckpass(right);
  2073. end;
  2074. if (ld.typ=procvardef) and
  2075. (not tprocvardef(ld).is_addressonly) then
  2076. begin
  2077. left:=csubscriptnode.create(
  2078. hsym,
  2079. ctypeconvnode.create_internal(left,methodpointertype));
  2080. typecheckpass(left);
  2081. end;
  2082. if lt=niln then
  2083. inserttypeconv_explicit(left,right.resultdef)
  2084. else
  2085. inserttypeconv_explicit(right,left.resultdef)
  2086. end
  2087. { <dyn. array>+<dyn. array> ? }
  2088. else if (nodetype=addn) and (is_dynamic_array(ld) or is_dynamic_array(rd)) then
  2089. begin
  2090. result:=maybe_convert_to_insert;
  2091. if assigned(result) then
  2092. exit;
  2093. if not(is_dynamic_array(ld)) then
  2094. inserttypeconv(left,rd);
  2095. if not(is_dynamic_array(rd)) then
  2096. inserttypeconv(right,ld);
  2097. end
  2098. { support dynamicarray=nil,dynamicarray<>nil }
  2099. else if (is_dynamic_array(ld) and (rt=niln)) or
  2100. (is_dynamic_array(rd) and (lt=niln)) or
  2101. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  2102. begin
  2103. if not(nodetype in [equaln,unequaln]) then
  2104. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2105. if lt=niln then
  2106. inserttypeconv_explicit(left,right.resultdef)
  2107. else
  2108. inserttypeconv_explicit(right,left.resultdef)
  2109. end
  2110. {$ifdef SUPPORT_MMX}
  2111. { mmx support, this must be before the zero based array
  2112. check }
  2113. else if (cs_mmx in current_settings.localswitches) and
  2114. is_mmx_able_array(ld) and
  2115. is_mmx_able_array(rd) and
  2116. equal_defs(ld,rd) then
  2117. begin
  2118. case nodetype of
  2119. addn,subn,xorn,orn,andn:
  2120. ;
  2121. { mul is a little bit restricted }
  2122. muln:
  2123. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  2124. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2125. else
  2126. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2127. end;
  2128. end
  2129. {$endif SUPPORT_MMX}
  2130. { vector support, this must be before the zero based array
  2131. check }
  2132. else if (cs_support_vectors in current_settings.globalswitches) and
  2133. is_vector(ld) and
  2134. is_vector(rd) and
  2135. equal_defs(ld,rd) then
  2136. begin
  2137. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  2138. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2139. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  2140. resultdef:=left.resultdef;
  2141. end
  2142. { this is a little bit dangerous, also the left type }
  2143. { pointer to should be checked! This broke the mmx support }
  2144. else if (rd.typ=pointerdef) or
  2145. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  2146. begin
  2147. if is_zero_based_array(rd) then
  2148. begin
  2149. resultdef:=cpointerdef.getreusable(tarraydef(rd).elementdef);
  2150. inserttypeconv(right,resultdef);
  2151. end
  2152. else
  2153. resultdef:=right.resultdef;
  2154. inserttypeconv(left,tpointerdef(right.resultdef).pointer_arithmetic_int_type);
  2155. if nodetype=addn then
  2156. begin
  2157. if (rt=niln) then
  2158. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,'NIL');
  2159. if not(cs_extsyntax in current_settings.moduleswitches) or
  2160. (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
  2161. not(cs_pointermath in current_settings.localswitches) and
  2162. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  2163. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2164. if (rd.typ=pointerdef) and
  2165. (tpointerdef(rd).pointeddef.size>1) then
  2166. begin
  2167. left:=caddnode.create(muln,left,
  2168. cordconstnode.create(tpointerdef(rd).pointeddef.size,tpointerdef(right.resultdef).pointer_arithmetic_int_type,true));
  2169. typecheckpass(left);
  2170. end;
  2171. end
  2172. else
  2173. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2174. end
  2175. else if (ld.typ=pointerdef) or
  2176. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  2177. begin
  2178. if is_zero_based_array(ld) then
  2179. begin
  2180. resultdef:=cpointerdef.getreusable(tarraydef(ld).elementdef);
  2181. inserttypeconv(left,resultdef);
  2182. end
  2183. else
  2184. resultdef:=left.resultdef;
  2185. inserttypeconv(right,tpointerdef(left.resultdef).pointer_arithmetic_int_type);
  2186. if nodetype in [addn,subn] then
  2187. begin
  2188. if (lt=niln) then
  2189. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),'NIL',rd.typename);
  2190. if not(cs_extsyntax in current_settings.moduleswitches) or
  2191. (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
  2192. not(cs_pointermath in current_settings.localswitches) and
  2193. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  2194. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2195. if (ld.typ=pointerdef) then
  2196. begin
  2197. if is_big_untyped_addrnode(left) then
  2198. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  2199. if (tpointerdef(ld).pointeddef.size>1) then
  2200. begin
  2201. right:=caddnode.create(muln,right,
  2202. cordconstnode.create(tpointerdef(ld).pointeddef.size,tpointerdef(left.resultdef).pointer_arithmetic_int_type,true));
  2203. typecheckpass(right);
  2204. end
  2205. end else
  2206. if is_zero_based_array(ld) and
  2207. (tarraydef(ld).elementdef.size>1) then
  2208. begin
  2209. right:=caddnode.create(muln,right,
  2210. cordconstnode.create(tarraydef(ld).elementdef.size,tpointerdef(left.resultdef).pointer_arithmetic_int_type,true));
  2211. typecheckpass(right);
  2212. end;
  2213. end
  2214. else
  2215. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2216. end
  2217. else if (rd.typ=procvardef) and
  2218. (ld.typ=procvardef) and
  2219. equal_defs(rd,ld) then
  2220. begin
  2221. if (nodetype in [equaln,unequaln]) then
  2222. begin
  2223. if tprocvardef(rd).is_addressonly then
  2224. begin
  2225. inserttypeconv_internal(right,voidcodepointertype);
  2226. inserttypeconv_internal(left,voidcodepointertype);
  2227. end
  2228. else
  2229. begin
  2230. { find proc field in methodpointer record }
  2231. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  2232. if not assigned(hsym) then
  2233. internalerror(200412043);
  2234. { Compare tmehodpointer(left).proc }
  2235. right:=csubscriptnode.create(
  2236. hsym,
  2237. ctypeconvnode.create_internal(right,methodpointertype));
  2238. typecheckpass(right);
  2239. left:=csubscriptnode.create(
  2240. hsym,
  2241. ctypeconvnode.create_internal(left,methodpointertype));
  2242. typecheckpass(left);
  2243. end;
  2244. end
  2245. else
  2246. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2247. end
  2248. { enums }
  2249. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  2250. begin
  2251. if allowenumop(nodetype) or (nf_internal in flags) then
  2252. inserttypeconv(right,left.resultdef)
  2253. else
  2254. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2255. end
  2256. { generic conversion, this is for error recovery }
  2257. else
  2258. begin
  2259. inserttypeconv(left,sinttype);
  2260. inserttypeconv(right,sinttype);
  2261. end;
  2262. if cmp_of_disjunct_ranges(res) and not(nf_internal in flags) then
  2263. begin
  2264. if res then
  2265. CGMessage(type_w_comparison_always_true)
  2266. else
  2267. CGMessage(type_w_comparison_always_false);
  2268. end;
  2269. { set resultdef if not already done }
  2270. if not assigned(resultdef) then
  2271. begin
  2272. case nodetype of
  2273. ltn,lten,gtn,gten,equaln,unequaln :
  2274. resultdef:=pasbool8type;
  2275. slashn :
  2276. resultdef:=resultrealdef;
  2277. addn:
  2278. begin
  2279. { for strings, return is always a 255 char string }
  2280. if is_shortstring(left.resultdef) then
  2281. resultdef:=cshortstringtype
  2282. else
  2283. { for ansistrings set resultdef to assignment left node
  2284. if it is an assignment and left node expects ansistring }
  2285. if is_ansistring(left.resultdef) and
  2286. assigned(aktassignmentnode) and
  2287. (aktassignmentnode.right=self) and
  2288. is_ansistring(aktassignmentnode.left.resultdef) then
  2289. resultdef:=aktassignmentnode.left.resultdef
  2290. else
  2291. resultdef:=left.resultdef;
  2292. end;
  2293. else
  2294. resultdef:=left.resultdef;
  2295. end;
  2296. end;
  2297. { when the result is currency we need some extra code for
  2298. multiplication and division. this should not be done when
  2299. the muln or slashn node is created internally }
  2300. if not(nf_is_currency in flags) and
  2301. is_currency(resultdef) then
  2302. begin
  2303. case nodetype of
  2304. slashn :
  2305. begin
  2306. { slashn will only work with floats }
  2307. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  2308. include(hp.flags,nf_is_currency);
  2309. result:=hp;
  2310. end;
  2311. muln :
  2312. begin
  2313. hp:=nil;
  2314. if s64currencytype.typ=floatdef then
  2315. begin
  2316. {$ifndef VER3_0}
  2317. { if left is a currency integer constant, we can get rid of the factor 10000 }
  2318. { int64(...) causes a cast on currency, so it is the currency value multiplied by 10000 }
  2319. if (left.nodetype=realconstn) and (is_currency(left.resultdef)) and ((int64(trealconstnode(left).value_currency) mod 10000)=0) then
  2320. begin
  2321. { trealconstnode expects that value_real and value_currency contain valid values }
  2322. trealconstnode(left).value_currency:=trealconstnode(left).value_currency {$ifdef FPC_CURRENCY_IS_INT64}div{$else}/{$endif} 10000;
  2323. trealconstnode(left).value_real:=trealconstnode(left).value_real/10000;
  2324. end
  2325. { or if right is an integer constant, we can get rid of its factor 10000 }
  2326. else if (right.nodetype=realconstn) and (is_currency(right.resultdef)) and ((int64(trealconstnode(right).value_currency) mod 10000)=0) then
  2327. begin
  2328. { trealconstnode expects that value and value_currency contain valid values }
  2329. trealconstnode(right).value_currency:=trealconstnode(right).value_currency {$ifdef FPC_CURRENCY_IS_INT64}div{$else}/{$endif} 10000;
  2330. trealconstnode(right).value_real:=trealconstnode(right).value_real/10000;
  2331. end
  2332. else
  2333. {$endif VER3_0}
  2334. begin
  2335. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype));
  2336. include(hp.flags,nf_is_currency);
  2337. end;
  2338. end
  2339. else
  2340. begin
  2341. {$ifndef VER3_0}
  2342. { if left is a currency integer constant, we can get rid of the factor 10000 }
  2343. if (left.nodetype=ordconstn) and (is_currency(left.resultdef)) and ((tordconstnode(left).value mod 10000)=0) then
  2344. tordconstnode(left).value:=tordconstnode(left).value div 10000
  2345. { or if right is an integer constant, we can get rid of its factor 10000 }
  2346. else if (right.nodetype=ordconstn) and (is_currency(right.resultdef)) and ((tordconstnode(right).value mod 10000)=0) then
  2347. tordconstnode(right).value:=tordconstnode(right).value div 10000
  2348. else
  2349. {$endif VER3_0}
  2350. begin
  2351. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  2352. include(hp.flags,nf_is_currency);
  2353. end
  2354. end;
  2355. result:=hp
  2356. end;
  2357. end;
  2358. end;
  2359. if not codegenerror and
  2360. not assigned(result) then
  2361. result:=simplify(false);
  2362. end;
  2363. function taddnode.first_addstring: tnode;
  2364. const
  2365. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  2366. var
  2367. p: tnode;
  2368. newstatement : tstatementnode;
  2369. tempnode (*,tempnode2*) : ttempcreatenode;
  2370. cmpfuncname: string;
  2371. para: tcallparanode;
  2372. begin
  2373. result:=nil;
  2374. { when we get here, we are sure that both the left and the right }
  2375. { node are both strings of the same stringtype (JM) }
  2376. case nodetype of
  2377. addn:
  2378. begin
  2379. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  2380. begin
  2381. result:=right;
  2382. left.free;
  2383. left:=nil;
  2384. right:=nil;
  2385. exit;
  2386. end;
  2387. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  2388. begin
  2389. result:=left;
  2390. left:=nil;
  2391. right.free;
  2392. right:=nil;
  2393. exit;
  2394. end;
  2395. { create the call to the concat routine both strings as arguments }
  2396. if assigned(aktassignmentnode) and
  2397. (aktassignmentnode.right=self) and
  2398. (aktassignmentnode.left.resultdef=resultdef) and
  2399. valid_for_var(aktassignmentnode.left,false) then
  2400. begin
  2401. para:=ccallparanode.create(
  2402. right,
  2403. ccallparanode.create(
  2404. left,
  2405. ccallparanode.create(aktassignmentnode.left.getcopy,nil)
  2406. )
  2407. );
  2408. if is_ansistring(resultdef) then
  2409. para:=ccallparanode.create(
  2410. cordconstnode.create(
  2411. { don't use getparaencoding(), we have to know
  2412. when the result is rawbytestring }
  2413. tstringdef(resultdef).encoding,
  2414. u16inttype,
  2415. true
  2416. ),
  2417. para
  2418. );
  2419. result:=ccallnode.createintern(
  2420. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2421. para
  2422. );
  2423. include(aktassignmentnode.flags,nf_assign_done_in_right);
  2424. firstpass(result);
  2425. end
  2426. else
  2427. begin
  2428. result:=internalstatements(newstatement);
  2429. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2430. addstatement(newstatement,tempnode);
  2431. { initialize the temp, since it will be passed to a
  2432. var-parameter (and finalization, which is performed by the
  2433. ttempcreate node and which takes care of the initialization
  2434. on native targets, is a noop on managed VM targets) }
  2435. if (target_info.system in systems_managed_vm) and
  2436. is_managed_type(resultdef) then
  2437. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  2438. false,
  2439. ccallparanode.create(genintconstnode(0),
  2440. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  2441. para:=ccallparanode.create(
  2442. right,
  2443. ccallparanode.create(
  2444. left,
  2445. ccallparanode.create(ctemprefnode.create(tempnode),nil)
  2446. )
  2447. );
  2448. if is_ansistring(resultdef) then
  2449. para:=ccallparanode.create(
  2450. cordconstnode.create(
  2451. { don't use getparaencoding(), we have to know
  2452. when the result is rawbytestring }
  2453. tstringdef(resultdef).encoding,
  2454. u16inttype,
  2455. true
  2456. ),
  2457. para
  2458. );
  2459. addstatement(
  2460. newstatement,
  2461. ccallnode.createintern(
  2462. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2463. para
  2464. )
  2465. );
  2466. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2467. addstatement(newstatement,ctemprefnode.create(tempnode));
  2468. end;
  2469. { we reused the arguments }
  2470. left := nil;
  2471. right := nil;
  2472. end;
  2473. ltn,lten,gtn,gten,equaln,unequaln :
  2474. begin
  2475. { generate better code for comparison with empty string, we
  2476. only need to compare the length with 0 }
  2477. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  2478. { windows widestrings are too complicated to be handled optimized }
  2479. not(is_widestring(left.resultdef) and (target_info.system in systems_windows)) and
  2480. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  2481. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  2482. begin
  2483. { switch so that the constant is always on the right }
  2484. if left.nodetype = stringconstn then
  2485. begin
  2486. p := left;
  2487. left := right;
  2488. right := p;
  2489. nodetype:=swap_relation[nodetype];
  2490. end;
  2491. if is_shortstring(left.resultdef) or
  2492. (nodetype in [gtn,gten,ltn,lten]) or
  2493. (target_info.system in systems_managed_vm) then
  2494. { compare the length with 0 }
  2495. result := caddnode.create(nodetype,
  2496. cinlinenode.create(in_length_x,false,left),
  2497. cordconstnode.create(0,s8inttype,false))
  2498. else
  2499. begin
  2500. (*
  2501. if is_widestring(left.resultdef) and
  2502. (target_info.system in system_windows) then
  2503. begin
  2504. { windows like widestrings requires that we also check the length }
  2505. result:=internalstatements(newstatement);
  2506. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2507. tempnode2:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2508. addstatement(newstatement,tempnode);
  2509. addstatement(newstatement,tempnode2);
  2510. { poor man's cse }
  2511. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2512. ctypeconvnode.create_internal(left,voidpointertype))
  2513. );
  2514. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode2),
  2515. caddnode.create(orn,
  2516. caddnode.create(nodetype,
  2517. ctemprefnode.create(tempnode),
  2518. cpointerconstnode.create(0,voidpointertype)
  2519. ),
  2520. caddnode.create(nodetype,
  2521. ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),s32inttype),
  2522. cordconstnode.create(0,s32inttype,false)
  2523. )
  2524. )
  2525. ));
  2526. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2527. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode2));
  2528. addstatement(newstatement,ctemprefnode.create(tempnode2));
  2529. end
  2530. else
  2531. *)
  2532. begin
  2533. { compare the pointer with nil (for ansistrings etc), }
  2534. { faster than getting the length (JM) }
  2535. result:= caddnode.create(nodetype,
  2536. ctypeconvnode.create_internal(left,voidpointertype),
  2537. cpointerconstnode.create(0,voidpointertype));
  2538. end;
  2539. end;
  2540. { left is reused }
  2541. left := nil;
  2542. { right isn't }
  2543. right.free;
  2544. right := nil;
  2545. exit;
  2546. end;
  2547. { no string constant -> call compare routine }
  2548. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  2549. { for equality checks use optimized version }
  2550. if nodetype in [equaln,unequaln] then
  2551. cmpfuncname := cmpfuncname + '_equal';
  2552. result := ccallnode.createintern(cmpfuncname,
  2553. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2554. { and compare its result with 0 according to the original operator }
  2555. result := caddnode.create(nodetype,result,
  2556. cordconstnode.create(0,s8inttype,false));
  2557. left := nil;
  2558. right := nil;
  2559. end;
  2560. end;
  2561. end;
  2562. function taddnode.first_addset : tnode;
  2563. procedure call_varset_helper(const n : string);
  2564. var
  2565. newstatement : tstatementnode;
  2566. temp : ttempcreatenode;
  2567. begin
  2568. { add two var sets }
  2569. result:=internalstatements(newstatement);
  2570. { create temp for result }
  2571. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2572. addstatement(newstatement,temp);
  2573. addstatement(newstatement,ccallnode.createintern(n,
  2574. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2575. ccallparanode.create(ctemprefnode.create(temp),
  2576. ccallparanode.create(right,
  2577. ccallparanode.create(left,nil)))))
  2578. );
  2579. { remove reused parts from original node }
  2580. left:=nil;
  2581. right:=nil;
  2582. { the last statement should return the value as
  2583. location and type, this is done be referencing the
  2584. temp and converting it first from a persistent temp to
  2585. normal temp }
  2586. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2587. addstatement(newstatement,ctemprefnode.create(temp));
  2588. end;
  2589. var
  2590. procname: string[31];
  2591. tempn: tnode;
  2592. newstatement : tstatementnode;
  2593. temp : ttempcreatenode;
  2594. begin
  2595. result:=nil;
  2596. case nodetype of
  2597. equaln,unequaln,lten,gten:
  2598. begin
  2599. case nodetype of
  2600. equaln,unequaln:
  2601. procname := 'fpc_varset_comp_sets';
  2602. lten,gten:
  2603. begin
  2604. procname := 'fpc_varset_contains_sets';
  2605. { (left >= right) = (right <= left) }
  2606. if nodetype = gten then
  2607. begin
  2608. tempn := left;
  2609. left := right;
  2610. right := tempn;
  2611. end;
  2612. end;
  2613. else
  2614. internalerror(2013112911);
  2615. end;
  2616. result := ccallnode.createinternres(procname,
  2617. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2618. ccallparanode.create(right,
  2619. ccallparanode.create(left,nil))),resultdef);
  2620. { left and right are reused as parameters }
  2621. left := nil;
  2622. right := nil;
  2623. { for an unequaln, we have to negate the result of comp_sets }
  2624. if nodetype = unequaln then
  2625. result := cnotnode.create(result);
  2626. end;
  2627. addn:
  2628. begin
  2629. { optimize first loading of a set }
  2630. if (right.nodetype=setelementn) and
  2631. not(assigned(tsetelementnode(right).right)) and
  2632. is_emptyset(left) then
  2633. begin
  2634. result:=internalstatements(newstatement);
  2635. { create temp for result }
  2636. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2637. addstatement(newstatement,temp);
  2638. { adjust for set base }
  2639. tsetelementnode(right).left:=caddnode.create(subn,
  2640. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2641. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2642. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  2643. ccallparanode.create(ctemprefnode.create(temp),
  2644. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2645. ccallparanode.create(tsetelementnode(right).left,nil))))
  2646. );
  2647. { the last statement should return the value as
  2648. location and type, this is done be referencing the
  2649. temp and converting it first from a persistent temp to
  2650. normal temp }
  2651. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2652. addstatement(newstatement,ctemprefnode.create(temp));
  2653. tsetelementnode(right).left := nil;
  2654. end
  2655. else
  2656. begin
  2657. if right.nodetype=setelementn then
  2658. begin
  2659. result:=internalstatements(newstatement);
  2660. { create temp for result }
  2661. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2662. addstatement(newstatement,temp);
  2663. { adjust for set base }
  2664. tsetelementnode(right).left:=caddnode.create(subn,
  2665. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2666. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2667. { add a range or a single element? }
  2668. if assigned(tsetelementnode(right).right) then
  2669. begin
  2670. { adjust for set base }
  2671. tsetelementnode(right).right:=caddnode.create(subn,
  2672. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  2673. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2674. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  2675. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2676. ccallparanode.create(tsetelementnode(right).right,
  2677. ccallparanode.create(tsetelementnode(right).left,
  2678. ccallparanode.create(ctemprefnode.create(temp),
  2679. ccallparanode.create(left,nil))))))
  2680. );
  2681. end
  2682. else
  2683. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  2684. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2685. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2686. ccallparanode.create(ctemprefnode.create(temp),
  2687. ccallparanode.create(left,nil)))))
  2688. );
  2689. { remove reused parts from original node }
  2690. tsetelementnode(right).right:=nil;
  2691. tsetelementnode(right).left:=nil;
  2692. left:=nil;
  2693. { the last statement should return the value as
  2694. location and type, this is done be referencing the
  2695. temp and converting it first from a persistent temp to
  2696. normal temp }
  2697. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2698. addstatement(newstatement,ctemprefnode.create(temp));
  2699. end
  2700. else
  2701. call_varset_helper('fpc_varset_add_sets');
  2702. end
  2703. end;
  2704. subn:
  2705. call_varset_helper('fpc_varset_sub_sets');
  2706. symdifn:
  2707. call_varset_helper('fpc_varset_symdif_sets');
  2708. muln:
  2709. call_varset_helper('fpc_varset_mul_sets');
  2710. else
  2711. internalerror(200609241);
  2712. end;
  2713. end;
  2714. function taddnode.first_adddynarray : tnode;
  2715. var
  2716. p: tnode;
  2717. newstatement : tstatementnode;
  2718. tempnode (*,tempnode2*) : ttempcreatenode;
  2719. cmpfuncname: string;
  2720. para: tcallparanode;
  2721. begin
  2722. result:=nil;
  2723. { when we get here, we are sure that both the left and the right }
  2724. { node are both strings of the same stringtype (JM) }
  2725. case nodetype of
  2726. addn:
  2727. begin
  2728. if (left.nodetype=arrayconstructorn) and (tarrayconstructornode(left).isempty) then
  2729. begin
  2730. result:=right;
  2731. left.free;
  2732. left:=nil;
  2733. right:=nil;
  2734. exit;
  2735. end;
  2736. if (right.nodetype=arrayconstructorn) and (tarrayconstructornode(right).isempty) then
  2737. begin
  2738. result:=left;
  2739. left:=nil;
  2740. right.free;
  2741. right:=nil;
  2742. exit;
  2743. end;
  2744. { create the call to the concat routine both strings as arguments }
  2745. if assigned(aktassignmentnode) and
  2746. (aktassignmentnode.right=self) and
  2747. (aktassignmentnode.left.resultdef=resultdef) and
  2748. valid_for_var(aktassignmentnode.left,false) then
  2749. begin
  2750. para:=ccallparanode.create(
  2751. ctypeconvnode.create_internal(right,voidcodepointertype),
  2752. ccallparanode.create(
  2753. ctypeconvnode.create_internal(left,voidcodepointertype),
  2754. ccallparanode.create(
  2755. caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  2756. ccallparanode.create(
  2757. ctypeconvnode.create_internal(aktassignmentnode.left.getcopy,voidcodepointertype),nil)
  2758. )));
  2759. result:=ccallnode.createintern(
  2760. 'fpc_dynarray_concat',
  2761. para
  2762. );
  2763. include(aktassignmentnode.flags,nf_assign_done_in_right);
  2764. firstpass(result);
  2765. end
  2766. else
  2767. begin
  2768. result:=internalstatements(newstatement);
  2769. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2770. addstatement(newstatement,tempnode);
  2771. { initialize the temp, since it will be passed to a
  2772. var-parameter (and finalization, which is performed by the
  2773. ttempcreate node and which takes care of the initialization
  2774. on native targets, is a noop on managed VM targets) }
  2775. if (target_info.system in systems_managed_vm) and
  2776. is_managed_type(resultdef) then
  2777. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  2778. false,
  2779. ccallparanode.create(genintconstnode(0),
  2780. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  2781. para:=ccallparanode.create(
  2782. ctypeconvnode.create_internal(right,voidcodepointertype),
  2783. ccallparanode.create(
  2784. ctypeconvnode.create_internal(left,voidcodepointertype),
  2785. ccallparanode.create(
  2786. caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  2787. ccallparanode.create(
  2788. ctypeconvnode.create_internal(ctemprefnode.create(tempnode),voidcodepointertype),nil)
  2789. )));
  2790. addstatement(
  2791. newstatement,
  2792. ccallnode.createintern(
  2793. 'fpc_dynarray_concat',
  2794. para
  2795. )
  2796. );
  2797. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2798. addstatement(newstatement,ctemprefnode.create(tempnode));
  2799. end;
  2800. { we reused the arguments }
  2801. left := nil;
  2802. right := nil;
  2803. end;
  2804. unequaln,equaln:
  2805. { nothing to do }
  2806. ;
  2807. else
  2808. Internalerror(2018030301);
  2809. end;
  2810. end;
  2811. function taddnode.use_generic_mul32to64: boolean;
  2812. begin
  2813. result := true;
  2814. end;
  2815. function taddnode.use_generic_mul64bit: boolean;
  2816. begin
  2817. result := true;
  2818. end;
  2819. function taddnode.try_make_mul32to64: boolean;
  2820. function canbe32bitint(v: tconstexprint; out canbesignedconst, canbeunsignedconst: boolean): boolean;
  2821. begin
  2822. result := ((v >= int64(low(longint))) and (v <= int64(high(longint)))) or
  2823. ((v >= qword(low(cardinal))) and (v <= qword(high(cardinal))));
  2824. canbesignedconst:=v<=int64(high(longint));
  2825. canbeunsignedconst:=v>=0;
  2826. end;
  2827. function is_32bitordconst(n: tnode; out canbesignedconst, canbeunsignedconst: boolean): boolean;
  2828. begin
  2829. canbesignedconst:=false;
  2830. canbeunsignedconst:=false;
  2831. result := (n.nodetype = ordconstn) and
  2832. canbe32bitint(tordconstnode(n).value, canbesignedconst, canbeunsignedconst);
  2833. end;
  2834. function is_32to64typeconv(n: tnode): boolean;
  2835. begin
  2836. result := (n.nodetype = typeconvn) and
  2837. is_integer(ttypeconvnode(n).left.resultdef) and
  2838. not is_64bit(ttypeconvnode(n).left.resultdef);
  2839. end;
  2840. var
  2841. temp: tnode;
  2842. leftoriginallysigned,
  2843. canbesignedconst, canbeunsignedconst: boolean;
  2844. begin
  2845. result := false;
  2846. if is_32to64typeconv(left) then
  2847. begin
  2848. leftoriginallysigned:=is_signed(ttypeconvnode(left).left.resultdef);
  2849. if ((is_32bitordconst(right,canbesignedconst, canbeunsignedconst) and
  2850. ((leftoriginallysigned and canbesignedconst) or
  2851. (not leftoriginallysigned and canbeunsignedconst))) or
  2852. (is_32to64typeconv(right) and
  2853. ((leftoriginallysigned =
  2854. is_signed(ttypeconvnode(right).left.resultdef)) or
  2855. (leftoriginallysigned and
  2856. (torddef(ttypeconvnode(right).left.resultdef).ordtype in [u8bit,u16bit]))))) then
  2857. begin
  2858. temp := ttypeconvnode(left).left;
  2859. ttypeconvnode(left).left := nil;
  2860. left.free;
  2861. left := temp;
  2862. if (right.nodetype = typeconvn) then
  2863. begin
  2864. temp := ttypeconvnode(right).left;
  2865. ttypeconvnode(right).left := nil;
  2866. right.free;
  2867. right := temp;
  2868. end;
  2869. if (is_signed(left.resultdef)) then
  2870. begin
  2871. inserttypeconv_internal(left,s32inttype);
  2872. inserttypeconv_internal(right,s32inttype);
  2873. end
  2874. else
  2875. begin
  2876. inserttypeconv_internal(left,u32inttype);
  2877. inserttypeconv_internal(right,u32inttype);
  2878. end;
  2879. firstpass(left);
  2880. firstpass(right);
  2881. result := true;
  2882. end;
  2883. end;
  2884. end;
  2885. function taddnode.use_fma : boolean;
  2886. begin
  2887. result:=false;
  2888. end;
  2889. function taddnode.try_fma(ld,rd : tdef) : tnode;
  2890. var
  2891. inlinennr : tinlinenumber;
  2892. begin
  2893. result:=nil;
  2894. if (cs_opt_fastmath in current_settings.optimizerswitches) and
  2895. use_fma and
  2896. (nodetype in [addn,subn]) and
  2897. (rd.typ=floatdef) and (ld.typ=floatdef) and
  2898. (is_single(rd) or is_double(rd)) and
  2899. equal_defs(rd,ld) and
  2900. { transforming a*b+c into fma(a,b,c) makes only sense if c can be
  2901. calculated easily. Consider a*b+c*d which results in
  2902. fmul
  2903. fmul
  2904. fadd
  2905. and in
  2906. fmul
  2907. fma
  2908. when using the fma optimization. On a super scalar architecture, the first instruction
  2909. sequence requires clock_cycles(fmul)+clock_cycles(fadd) clock cycles because the fmuls can be executed in parallel.
  2910. The second sequence requires clock_cycles(fmul)+clock_cycles(fma) because the fma has to wait for the
  2911. result of the fmul. Since typically clock_cycles(fma)>clock_cycles(fadd) applies, the first sequence is better.
  2912. }
  2913. (((left.nodetype=muln) and (node_complexity(right)<3)) or
  2914. ((right.nodetype=muln) and (node_complexity(left)<3)) or
  2915. ((left.nodetype=inlinen) and
  2916. (tinlinenode(left).inlinenumber=in_sqr_real) and
  2917. (node_complexity(right)<3)) or
  2918. ((right.nodetype=inlinen) and
  2919. (tinlinenode(right).inlinenumber=in_sqr_real) and
  2920. (node_complexity(left)<3))
  2921. ) then
  2922. begin
  2923. case tfloatdef(ld).floattype of
  2924. s32real:
  2925. inlinennr:=in_fma_single;
  2926. s64real:
  2927. inlinennr:=in_fma_double;
  2928. s80real:
  2929. inlinennr:=in_fma_extended;
  2930. s128real:
  2931. inlinennr:=in_fma_float128;
  2932. else
  2933. internalerror(2014042601);
  2934. end;
  2935. if left.nodetype=muln then
  2936. begin
  2937. if nodetype=subn then
  2938. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(cunaryminusnode.create(right),
  2939. ccallparanode.create(taddnode(left).right,
  2940. ccallparanode.create(taddnode(left).left,nil
  2941. ))))
  2942. else
  2943. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(right,
  2944. ccallparanode.create(taddnode(left).right,
  2945. ccallparanode.create(taddnode(left).left,nil
  2946. ))));
  2947. right:=nil;
  2948. taddnode(left).right:=nil;
  2949. taddnode(left).left:=nil;
  2950. end
  2951. else if right.nodetype=muln then
  2952. begin
  2953. if nodetype=subn then
  2954. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  2955. ccallparanode.create(cunaryminusnode.create(taddnode(right).right),
  2956. ccallparanode.create(taddnode(right).left,nil
  2957. ))))
  2958. else
  2959. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  2960. ccallparanode.create(taddnode(right).right,
  2961. ccallparanode.create(taddnode(right).left,nil
  2962. ))));
  2963. left:=nil;
  2964. taddnode(right).right:=nil;
  2965. taddnode(right).left:=nil;
  2966. end
  2967. else if (left.nodetype=inlinen) and (tinlinenode(left).inlinenumber=in_sqr_real) then
  2968. begin
  2969. if nodetype=subn then
  2970. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(cunaryminusnode.create(right),
  2971. ccallparanode.create(tinlinenode(left).left.getcopy,
  2972. ccallparanode.create(tinlinenode(left).left.getcopy,nil
  2973. ))))
  2974. else
  2975. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(right,
  2976. ccallparanode.create(tinlinenode(left).left.getcopy,
  2977. ccallparanode.create(tinlinenode(left).left.getcopy,nil
  2978. ))));
  2979. right:=nil;
  2980. end
  2981. { we get here only if right is a sqr node }
  2982. else if (right.nodetype=inlinen) and (tinlinenode(right).inlinenumber=in_sqr_real) then
  2983. begin
  2984. if nodetype=subn then
  2985. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  2986. ccallparanode.create(cunaryminusnode.create(tinlinenode(right).left.getcopy),
  2987. ccallparanode.create(tinlinenode(right).left.getcopy,nil
  2988. ))))
  2989. else
  2990. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  2991. ccallparanode.create(tinlinenode(right).left.getcopy,
  2992. ccallparanode.create(tinlinenode(right).left.getcopy,nil
  2993. ))));
  2994. left:=nil;
  2995. end;
  2996. end;
  2997. end;
  2998. function taddnode.first_add64bitint: tnode;
  2999. var
  3000. procname: string[31];
  3001. temp: tnode;
  3002. power: longint;
  3003. begin
  3004. result := nil;
  3005. { create helper calls mul }
  3006. if nodetype <> muln then
  3007. exit;
  3008. { make sure that if there is a constant, that it's on the right }
  3009. if left.nodetype = ordconstn then
  3010. begin
  3011. temp := right;
  3012. right := left;
  3013. left := temp;
  3014. end;
  3015. { can we use a shift instead of a mul? }
  3016. if not (cs_check_overflow in current_settings.localswitches) and
  3017. (right.nodetype = ordconstn) and
  3018. ispowerof2(tordconstnode(right).value,power) then
  3019. begin
  3020. tordconstnode(right).value := power;
  3021. result := cshlshrnode.create(shln,left,right);
  3022. { left and right are reused }
  3023. left := nil;
  3024. right := nil;
  3025. { return firstpassed new node }
  3026. exit;
  3027. end;
  3028. if try_make_mul32to64 then
  3029. begin
  3030. { this uses the same criteria for signedness as the 32 to 64-bit mul
  3031. handling in the i386 code generator }
  3032. if is_signed(left.resultdef) and is_signed(right.resultdef) then
  3033. procname := 'fpc_mul_longint_to_int64'
  3034. else
  3035. procname := 'fpc_mul_dword_to_qword';
  3036. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  3037. result := ccallnode.createintern(procname,right);
  3038. left := nil;
  3039. right := nil;
  3040. end
  3041. else
  3042. begin
  3043. { can full 64-bit multiplication be handled inline? }
  3044. if not use_generic_mul64bit then
  3045. begin
  3046. { generic handling replaces this node with call to fpc_mul_int64,
  3047. whose result is int64 }
  3048. if is_currency(resultdef) then
  3049. resultdef:=s64inttype;
  3050. exit;
  3051. end;
  3052. { when currency is used set the result of the
  3053. parameters to s64bit, so they are not converted }
  3054. if is_currency(resultdef) then
  3055. begin
  3056. left.resultdef:=s64inttype;
  3057. right.resultdef:=s64inttype;
  3058. end;
  3059. { otherwise, create the parameters for the helper }
  3060. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  3061. left := nil;
  3062. { only qword needs the unsigned code, the
  3063. signed code is also used for currency }
  3064. if is_signed(resultdef) then
  3065. procname := 'fpc_mul_int64'
  3066. else
  3067. procname := 'fpc_mul_qword';
  3068. if cs_check_overflow in current_settings.localswitches then
  3069. procname := procname + '_checkoverflow';
  3070. result := ccallnode.createintern(procname,right);
  3071. right := nil;
  3072. end;
  3073. end;
  3074. function taddnode.first_addpointer: tnode;
  3075. begin
  3076. result:=nil;
  3077. expectloc:=LOC_REGISTER;
  3078. end;
  3079. function taddnode.first_cmppointer: tnode;
  3080. begin
  3081. result:=nil;
  3082. expectloc:=LOC_FLAGS;
  3083. end;
  3084. function taddnode.first_addfloat : tnode;
  3085. var
  3086. procname: string[31];
  3087. { do we need to reverse the result ? }
  3088. notnode : boolean;
  3089. fdef : tdef;
  3090. begin
  3091. result := nil;
  3092. notnode := false;
  3093. fdef := nil;
  3094. { In non-emulation mode, real opcodes are
  3095. emitted for floating point values.
  3096. }
  3097. if not ((cs_fp_emulation in current_settings.moduleswitches)
  3098. {$ifdef cpufpemu}
  3099. or (current_settings.fputype=fpu_soft)
  3100. {$endif cpufpemu}
  3101. ) then
  3102. exit;
  3103. if not(target_info.system in systems_wince) then
  3104. begin
  3105. case tfloatdef(left.resultdef).floattype of
  3106. s32real:
  3107. begin
  3108. fdef:=search_system_type('FLOAT32REC').typedef;
  3109. procname:='float32';
  3110. end;
  3111. s64real:
  3112. begin
  3113. fdef:=search_system_type('FLOAT64').typedef;
  3114. procname:='float64';
  3115. end;
  3116. {!!! not yet implemented
  3117. s128real:
  3118. }
  3119. else
  3120. internalerror(2005082601);
  3121. end;
  3122. case nodetype of
  3123. addn:
  3124. procname:=procname+'_add';
  3125. muln:
  3126. procname:=procname+'_mul';
  3127. subn:
  3128. procname:=procname+'_sub';
  3129. slashn:
  3130. procname:=procname+'_div';
  3131. ltn:
  3132. procname:=procname+'_lt';
  3133. lten:
  3134. procname:=procname+'_le';
  3135. gtn:
  3136. begin
  3137. procname:=procname+'_lt';
  3138. swapleftright;
  3139. end;
  3140. gten:
  3141. begin
  3142. procname:=procname+'_le';
  3143. swapleftright;
  3144. end;
  3145. equaln:
  3146. procname:=procname+'_eq';
  3147. unequaln:
  3148. begin
  3149. procname:=procname+'_eq';
  3150. notnode:=true;
  3151. end;
  3152. else
  3153. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  3154. end;
  3155. end
  3156. else
  3157. begin
  3158. case nodetype of
  3159. addn:
  3160. procname:='add';
  3161. muln:
  3162. procname:='mul';
  3163. subn:
  3164. procname:='sub';
  3165. slashn:
  3166. procname:='div';
  3167. ltn:
  3168. procname:='lt';
  3169. lten:
  3170. procname:='le';
  3171. gtn:
  3172. procname:='gt';
  3173. gten:
  3174. procname:='ge';
  3175. equaln:
  3176. procname:='eq';
  3177. unequaln:
  3178. procname:='ne';
  3179. else
  3180. begin
  3181. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  3182. exit;
  3183. end;
  3184. end;
  3185. case tfloatdef(left.resultdef).floattype of
  3186. s32real:
  3187. begin
  3188. procname:=procname+'s';
  3189. if nodetype in [addn,muln,subn,slashn] then
  3190. procname:=lower(procname);
  3191. end;
  3192. s64real:
  3193. procname:=procname+'d';
  3194. {!!! not yet implemented
  3195. s128real:
  3196. }
  3197. else
  3198. internalerror(2005082602);
  3199. end;
  3200. end;
  3201. { cast softfpu result? }
  3202. if not(target_info.system in systems_wince) then
  3203. begin
  3204. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  3205. resultdef:=pasbool8type;
  3206. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  3207. ctypeconvnode.create_internal(right,fdef),
  3208. ccallparanode.create(
  3209. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  3210. end
  3211. else
  3212. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  3213. ccallparanode.create(left,nil)));
  3214. left:=nil;
  3215. right:=nil;
  3216. { do we need to reverse the result }
  3217. if notnode then
  3218. result:=cnotnode.create(result);
  3219. end;
  3220. {$ifdef cpuneedsmulhelper}
  3221. function taddnode.use_mul_helper: boolean;
  3222. begin
  3223. result:=(nodetype=muln) and
  3224. not(torddef(resultdef).ordtype in [u8bit,s8bit
  3225. {$if defined(cpu16bitalu) or defined(avr)},u16bit,s16bit{$endif}]);
  3226. end;
  3227. {$endif cpuneedsmulhelper}
  3228. function taddnode.pass_1 : tnode;
  3229. function isconstsetfewelements(p : tnode) : boolean;
  3230. begin
  3231. result:=(p.nodetype=setconstn) and (tsetconstnode(p).elements<=4);
  3232. end;
  3233. var
  3234. {$ifdef addstringopt}
  3235. hp : tnode;
  3236. {$endif addstringopt}
  3237. rd,ld : tdef;
  3238. i,i2 : longint;
  3239. lt,rt : tnodetype;
  3240. {$ifdef cpuneedsmulhelper}
  3241. procname : string[32];
  3242. {$endif cpuneedsmulhelper}
  3243. tempn,varsetnode: tnode;
  3244. mulnode : taddnode;
  3245. constsetnode : tsetconstnode;
  3246. trycreateinnodes : Boolean;
  3247. begin
  3248. result:=nil;
  3249. { Can we optimize multiple string additions into a single call?
  3250. This need to be done on a complete tree to detect the multiple
  3251. add nodes and is therefor done before the subtrees are processed }
  3252. if canbemultistringadd(self) then
  3253. begin
  3254. result:=genmultistringadd(self);
  3255. exit;
  3256. end;
  3257. { Can we optimize multiple dyn. array additions into a single call?
  3258. This need to be done on a complete tree to detect the multiple
  3259. add nodes and is therefor done before the subtrees are processed }
  3260. if canbemultidynarrayadd(self) then
  3261. begin
  3262. result:=genmultidynarrayadd(self);
  3263. exit;
  3264. end;
  3265. { typical set tests like (s*[const. set])<>/=[] can be converted into an or'ed chain of in tests
  3266. for var sets if const. set contains only a few elements }
  3267. 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
  3268. begin
  3269. trycreateinnodes:=false;
  3270. mulnode:=nil;
  3271. if (is_emptyset(right) and (left.nodetype=muln) and
  3272. (isconstsetfewelements(taddnode(left).right) or isconstsetfewelements(taddnode(left).left))) then
  3273. begin
  3274. trycreateinnodes:=true;
  3275. mulnode:=taddnode(left);
  3276. end
  3277. else if (is_emptyset(left) and (right.nodetype=muln) and
  3278. (isconstsetfewelements(taddnode(right).right) or isconstsetfewelements(taddnode(right).left))) then
  3279. begin
  3280. trycreateinnodes:=true;
  3281. mulnode:=taddnode(right);
  3282. end;
  3283. if trycreateinnodes then
  3284. begin
  3285. constsetnode:=nil;
  3286. varsetnode:=nil;
  3287. if isconstsetfewelements(mulnode.right) then
  3288. begin
  3289. constsetnode:=tsetconstnode(mulnode.right);
  3290. varsetnode:=mulnode.left;
  3291. end
  3292. else
  3293. begin
  3294. constsetnode:=tsetconstnode(mulnode.left);
  3295. varsetnode:=mulnode.right;
  3296. end;
  3297. { the node is copied so it might have no side effects, if the complexity is too, cse should fix it, so
  3298. do not check complexity }
  3299. if not(might_have_sideeffects(varsetnode)) then
  3300. begin
  3301. result:=nil;
  3302. for i:=low(tconstset) to high(tconstset) do
  3303. if i in constsetnode.value_set^ then
  3304. begin
  3305. tempn:=cinnode.create(cordconstnode.create(i,tsetdef(constsetnode.resultdef).elementdef,false),varsetnode.getcopy);
  3306. if assigned(result) then
  3307. result:=caddnode.create_internal(orn,result,tempn)
  3308. else
  3309. result:=tempn;
  3310. end;
  3311. if nodetype=equaln then
  3312. result:=cnotnode.create(result);
  3313. exit;
  3314. end;
  3315. end;
  3316. end;
  3317. { first do the two subtrees }
  3318. firstpass(left);
  3319. firstpass(right);
  3320. if codegenerror then
  3321. exit;
  3322. { load easier access variables }
  3323. rd:=right.resultdef;
  3324. ld:=left.resultdef;
  3325. rt:=right.nodetype;
  3326. lt:=left.nodetype;
  3327. { int/int gives real/real! }
  3328. if nodetype=slashn then
  3329. begin
  3330. {$ifdef cpufpemu}
  3331. result:=first_addfloat;
  3332. if assigned(result) then
  3333. exit;
  3334. {$endif cpufpemu}
  3335. expectloc:=LOC_FPUREGISTER;
  3336. end
  3337. { if both are orddefs then check sub types }
  3338. else if (ld.typ=orddef) and (rd.typ=orddef) then
  3339. begin
  3340. { optimize multiplacation by a power of 2 }
  3341. if not(cs_check_overflow in current_settings.localswitches) and
  3342. (nodetype = muln) and
  3343. (((left.nodetype = ordconstn) and
  3344. ispowerof2(tordconstnode(left).value,i)) or
  3345. ((right.nodetype = ordconstn) and
  3346. ispowerof2(tordconstnode(right).value,i2))) then
  3347. begin
  3348. if ((left.nodetype = ordconstn) and
  3349. ispowerof2(tordconstnode(left).value,i)) then
  3350. begin
  3351. tordconstnode(left).value := i;
  3352. result := cshlshrnode.create(shln,right,left);
  3353. end
  3354. else
  3355. begin
  3356. tordconstnode(right).value := i2;
  3357. result := cshlshrnode.create(shln,left,right);
  3358. end;
  3359. result.resultdef := resultdef;
  3360. left := nil;
  3361. right := nil;
  3362. exit;
  3363. end;
  3364. { 2 booleans ? }
  3365. if is_boolean(ld) and is_boolean(rd) then
  3366. begin
  3367. if (not(cs_full_boolean_eval in current_settings.localswitches) or
  3368. (nf_short_bool in flags)) and
  3369. (nodetype in [andn,orn]) then
  3370. expectloc:=LOC_JUMP
  3371. else
  3372. begin
  3373. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  3374. expectloc:=LOC_FLAGS
  3375. else
  3376. expectloc:=LOC_REGISTER;
  3377. end;
  3378. end
  3379. else
  3380. { Both are chars? only convert to shortstrings for addn }
  3381. if is_char(ld) then
  3382. begin
  3383. if nodetype=addn then
  3384. internalerror(200103291);
  3385. expectloc:=LOC_FLAGS;
  3386. end
  3387. else if (nodetype=muln) and
  3388. is_64bitint(resultdef) and
  3389. not use_generic_mul32to64 and
  3390. try_make_mul32to64 then
  3391. begin
  3392. { if the code generator can handle 32 to 64-bit muls,
  3393. we're done here }
  3394. end
  3395. {$ifndef cpu64bitalu}
  3396. { is there a 64 bit type ? }
  3397. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  3398. begin
  3399. result := first_add64bitint;
  3400. if assigned(result) then
  3401. exit;
  3402. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3403. expectloc:=LOC_REGISTER
  3404. else
  3405. expectloc:=LOC_JUMP;
  3406. end
  3407. {$endif cpu64bitalu}
  3408. { generic 32bit conversion }
  3409. else
  3410. begin
  3411. {$ifdef cpuneedsmulhelper}
  3412. if use_mul_helper then
  3413. begin
  3414. result := nil;
  3415. case torddef(resultdef).ordtype of
  3416. s16bit:
  3417. procname := 'fpc_mul_integer';
  3418. u16bit:
  3419. procname := 'fpc_mul_word';
  3420. s32bit:
  3421. procname := 'fpc_mul_longint';
  3422. u32bit:
  3423. procname := 'fpc_mul_dword';
  3424. else
  3425. internalerror(2011022301);
  3426. end;
  3427. if cs_check_overflow in current_settings.localswitches then
  3428. procname:=procname+'_checkoverflow';
  3429. result := ccallnode.createintern(procname,
  3430. ccallparanode.create(right,
  3431. ccallparanode.create(left,nil)));
  3432. left := nil;
  3433. right := nil;
  3434. firstpass(result);
  3435. exit;
  3436. end;
  3437. {$endif cpuneedsmulhelper}
  3438. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3439. expectloc:=LOC_REGISTER
  3440. else if torddef(ld).size>sizeof(aint) then
  3441. expectloc:=LOC_JUMP
  3442. else
  3443. expectloc:=LOC_FLAGS;
  3444. end;
  3445. end
  3446. { left side a setdef, must be before string processing,
  3447. else array constructor can be seen as array of char (PFV) }
  3448. else if (ld.typ=setdef) then
  3449. begin
  3450. { small sets are handled inline by the compiler.
  3451. small set doesn't have support for adding ranges }
  3452. if is_smallset(ld) and
  3453. not(
  3454. (right.nodetype=setelementn) and
  3455. assigned(tsetelementnode(right).right)
  3456. ) then
  3457. begin
  3458. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  3459. expectloc:=LOC_FLAGS
  3460. else
  3461. expectloc:=LOC_REGISTER;
  3462. end
  3463. else
  3464. begin
  3465. result := first_addset;
  3466. if assigned(result) then
  3467. exit;
  3468. expectloc:=LOC_CREFERENCE;
  3469. end;
  3470. end
  3471. { compare pchar by addresses like BP/Delphi }
  3472. else if is_pchar(ld) then
  3473. begin
  3474. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3475. result:=first_addpointer
  3476. else
  3477. result:=first_cmppointer;
  3478. end
  3479. { is one of the operands a string }
  3480. else if (ld.typ=stringdef) then
  3481. begin
  3482. if is_widestring(ld) then
  3483. begin
  3484. { this is only for add, the comparisaion is handled later }
  3485. expectloc:=LOC_REGISTER;
  3486. end
  3487. else if is_unicodestring(ld) then
  3488. begin
  3489. { this is only for add, the comparisaion is handled later }
  3490. expectloc:=LOC_REGISTER;
  3491. end
  3492. else if is_ansistring(ld) then
  3493. begin
  3494. { this is only for add, the comparisaion is handled later }
  3495. expectloc:=LOC_REGISTER;
  3496. end
  3497. else if is_longstring(ld) then
  3498. begin
  3499. { this is only for add, the comparisaion is handled later }
  3500. expectloc:=LOC_REFERENCE;
  3501. end
  3502. else
  3503. begin
  3504. {$ifdef addstringopt}
  3505. { can create a call which isn't handled by callparatemp }
  3506. if canbeaddsstringcharoptnode(self) then
  3507. begin
  3508. hp := genaddsstringcharoptnode(self);
  3509. pass_1 := hp;
  3510. exit;
  3511. end
  3512. else
  3513. {$endif addstringopt}
  3514. begin
  3515. { Fix right to be shortstring }
  3516. if is_char(right.resultdef) then
  3517. begin
  3518. inserttypeconv(right,cshortstringtype);
  3519. firstpass(right);
  3520. end;
  3521. end;
  3522. {$ifdef addstringopt}
  3523. { can create a call which isn't handled by callparatemp }
  3524. if canbeaddsstringcsstringoptnode(self) then
  3525. begin
  3526. hp := genaddsstringcsstringoptnode(self);
  3527. pass_1 := hp;
  3528. exit;
  3529. end;
  3530. {$endif addstringopt}
  3531. end;
  3532. { otherwise, let addstring convert everything }
  3533. result := first_addstring;
  3534. exit;
  3535. end
  3536. { is one a real float ? }
  3537. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  3538. begin
  3539. {$ifdef cpufpemu}
  3540. result:=first_addfloat;
  3541. if assigned(result) then
  3542. exit;
  3543. {$endif cpufpemu}
  3544. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3545. expectloc:=LOC_FPUREGISTER
  3546. else
  3547. expectloc:=LOC_FLAGS;
  3548. result:=try_fma(ld,rd);
  3549. if assigned(result) then
  3550. exit;
  3551. end
  3552. { pointer comperation and subtraction }
  3553. else if (ld.typ=pointerdef) then
  3554. begin
  3555. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3556. result:=first_addpointer
  3557. else
  3558. result:=first_cmppointer;
  3559. end
  3560. else if is_implicit_pointer_object_type(ld) then
  3561. begin
  3562. expectloc:=LOC_FLAGS;
  3563. end
  3564. else if (ld.typ=classrefdef) then
  3565. begin
  3566. expectloc:=LOC_FLAGS;
  3567. end
  3568. { support procvar=nil,procvar<>nil }
  3569. else if ((ld.typ=procvardef) and (rt=niln)) or
  3570. ((rd.typ=procvardef) and (lt=niln)) then
  3571. begin
  3572. expectloc:=LOC_FLAGS;
  3573. end
  3574. {$ifdef SUPPORT_MMX}
  3575. { mmx support, this must be before the zero based array
  3576. check }
  3577. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  3578. is_mmx_able_array(rd) then
  3579. begin
  3580. expectloc:=LOC_MMXREGISTER;
  3581. end
  3582. {$endif SUPPORT_MMX}
  3583. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  3584. begin
  3585. result:=first_addpointer;
  3586. end
  3587. else if (rd.typ=procvardef) and
  3588. (ld.typ=procvardef) and
  3589. equal_defs(rd,ld) then
  3590. begin
  3591. expectloc:=LOC_FLAGS;
  3592. end
  3593. else if (ld.typ=enumdef) then
  3594. begin
  3595. expectloc:=LOC_FLAGS;
  3596. end
  3597. {$ifdef SUPPORT_MMX}
  3598. else if (cs_mmx in current_settings.localswitches) and
  3599. is_mmx_able_array(ld) and
  3600. is_mmx_able_array(rd) then
  3601. begin
  3602. expectloc:=LOC_MMXREGISTER;
  3603. end
  3604. {$endif SUPPORT_MMX}
  3605. else if is_dynamic_array(ld) or is_dynamic_array(rd) then
  3606. begin
  3607. result:=first_adddynarray;
  3608. exit;
  3609. end
  3610. { the general solution is to convert to 32 bit int }
  3611. else
  3612. begin
  3613. expectloc:=LOC_REGISTER;
  3614. end;
  3615. end;
  3616. {$ifdef state_tracking}
  3617. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  3618. var factval:Tnode;
  3619. begin
  3620. track_state_pass:=false;
  3621. if left.track_state_pass(exec_known) then
  3622. begin
  3623. track_state_pass:=true;
  3624. left.resultdef:=nil;
  3625. do_typecheckpass(left);
  3626. end;
  3627. factval:=aktstate.find_fact(left);
  3628. if factval<>nil then
  3629. begin
  3630. track_state_pass:=true;
  3631. left.destroy;
  3632. left:=factval.getcopy;
  3633. end;
  3634. if right.track_state_pass(exec_known) then
  3635. begin
  3636. track_state_pass:=true;
  3637. right.resultdef:=nil;
  3638. do_typecheckpass(right);
  3639. end;
  3640. factval:=aktstate.find_fact(right);
  3641. if factval<>nil then
  3642. begin
  3643. track_state_pass:=true;
  3644. right.destroy;
  3645. right:=factval.getcopy;
  3646. end;
  3647. end;
  3648. {$endif}
  3649. end.