nadd.pas 162 KB

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