nadd.pas 162 KB

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