ninl.pas 240 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072
  1. {
  2. Copyright (c) 1998-2007 by Florian Klaempfl
  3. Type checking and register allocation for inline 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 ninl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,htypechk,symtype,compinnr;
  22. type
  23. TInlineNodeFlag = (
  24. inf_inlineconst
  25. );
  26. TInlineNodeFlags = set of TInlineNodeFlag;
  27. tinlinenode = class(tunarynode)
  28. inlinenodeflags : TInlineNodeFlags;
  29. inlinenumber : tinlinenumber;
  30. constructor create(number : tinlinenumber;is_const:boolean;l : tnode);virtual;
  31. constructor createintern(number : tinlinenumber;is_const:boolean;l : tnode);virtual;
  32. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  33. procedure ppuwrite(ppufile:tcompilerppufile);override;
  34. function dogetcopy : tnode;override;
  35. procedure printnodeinfo(var t : text);override;
  36. {$ifdef DEBUG_NODE_XML}
  37. procedure XMLPrintNodeInfo(var t : text);override;
  38. {$endif DEBUG_NODE_XML}
  39. function pass_1 : tnode;override;
  40. function pass_typecheck:tnode;override;
  41. function pass_typecheck_cpu:tnode;virtual;
  42. function simplify(forinline : boolean): tnode;override;
  43. function docompare(p: tnode): boolean; override;
  44. procedure mark_write;override;
  45. { returns a node tree where the inc/dec are replaced by add/sub }
  46. function getaddsub_for_incdec : tnode;
  47. { pack and unpack are changed into for-loops by the compiler }
  48. function first_pack_unpack: tnode; virtual;
  49. property parameters : tnode read left write left;
  50. function may_have_sideeffect_norecurse: boolean;
  51. protected
  52. { All the following routines currently
  53. call compilerprocs, unless they are
  54. overridden in which case, the code
  55. generator handles them.
  56. }
  57. function first_pi: tnode ; virtual;
  58. function first_arctan_real: tnode; virtual;
  59. function first_abs_real: tnode; virtual;
  60. function first_sqr_real: tnode; virtual;
  61. function first_sqrt_real: tnode; virtual;
  62. function first_ln_real: tnode; virtual;
  63. function first_cos_real: tnode; virtual;
  64. function first_sin_real: tnode; virtual;
  65. function first_exp_real: tnode; virtual;
  66. function first_frac_real: tnode; virtual;
  67. function first_round_real: tnode; virtual;
  68. function first_trunc_real: tnode; virtual;
  69. function first_int_real: tnode; virtual;
  70. function first_abs_long: tnode; virtual;
  71. function first_IncDec: tnode; virtual;
  72. function first_IncludeExclude: tnode; virtual;
  73. function first_get_frame: tnode; virtual;
  74. function first_setlength: tnode; virtual;
  75. function first_copy: tnode; virtual;
  76. { This one by default generates an internal error, because such
  77. nodes are not generated by the parser. It's however used internally
  78. by the JVM backend to create new dynamic arrays. }
  79. function first_new: tnode; virtual;
  80. function first_length: tnode; virtual;
  81. function first_high: tnode; virtual;
  82. function first_box: tnode; virtual; abstract;
  83. function first_unbox: tnode; virtual; abstract;
  84. function first_assigned: tnode; virtual;
  85. function first_assert: tnode; virtual;
  86. function first_popcnt: tnode; virtual;
  87. function first_bitscan: tnode; virtual;
  88. { override these for Seg() support }
  89. function typecheck_seg: tnode; virtual;
  90. function first_seg: tnode; virtual;
  91. function first_sar: tnode; virtual;
  92. function first_fma : tnode; virtual;
  93. function first_minmax: tnode; virtual;
  94. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  95. function first_ShiftRot_assign_64bitint: tnode; virtual;
  96. {$endif not cpu64bitalu and not cpuhighleveltarget}
  97. function first_AndOrXorShiftRot_assign: tnode; virtual;
  98. function first_NegNot_assign: tnode; virtual;
  99. function first_cpu : tnode; virtual;
  100. procedure CheckParameters(count : integer);
  101. private
  102. function handle_str: tnode;
  103. function handle_reset_rewrite_typed: tnode;
  104. function handle_text_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  105. function handle_typed_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  106. function handle_read_write: tnode;
  107. function handle_val: tnode;
  108. function handle_default: tnode;
  109. function handle_setlength: tnode;
  110. function handle_copy: tnode;
  111. function handle_box: tnode;
  112. function handle_unbox: tnode;
  113. function handle_insert:tnode;
  114. function handle_delete:tnode;
  115. function handle_concat:tnode;
  116. end;
  117. tinlinenodeclass = class of tinlinenode;
  118. var
  119. cinlinenode : tinlinenodeclass = tinlinenode;
  120. function geninlinenode(number : tinlinenumber;is_const:boolean;l : tnode) : tinlinenode;
  121. implementation
  122. uses
  123. verbose,globals,systems,constexp,
  124. globtype,cutils,cclasses,fmodule,
  125. symconst,symdef,symsym,symcpu,symtable,paramgr,defcmp,defutil,symbase,
  126. cpuinfo,cpubase,
  127. pass_1,ppu,
  128. ncal,ncon,ncnv,nadd,nld,nbas,nflw,nmem,nmat,nutils,ngenutil,
  129. nobjc,objcdef,
  130. cgbase,procinfo;
  131. function geninlinenode(number : tinlinenumber;is_const:boolean;l : tnode) : tinlinenode;
  132. begin
  133. geninlinenode:=cinlinenode.create(number,is_const,l);
  134. end;
  135. {*****************************************************************************
  136. TINLINENODE
  137. *****************************************************************************}
  138. constructor tinlinenode.create(number : tinlinenumber;is_const:boolean;l : tnode);
  139. begin
  140. inherited create(inlinen,l);
  141. if is_const then
  142. inlinenodeflags:=[inf_inlineconst]
  143. else
  144. inlinenodeflags:=[];
  145. inlinenumber:=number;
  146. end;
  147. constructor tinlinenode.createintern(number : tinlinenumber; is_const : boolean;
  148. l : tnode);
  149. begin
  150. create(number,is_const,l);
  151. inlinenodeflags:=[];
  152. include(flags,nf_internal);
  153. end;
  154. constructor tinlinenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  155. begin
  156. inherited ppuload(t,ppufile);
  157. ppufile.getset(tppuset1(inlinenodeflags));
  158. inlinenumber:=tinlinenumber(ppufile.getlongint);
  159. end;
  160. procedure tinlinenode.ppuwrite(ppufile:tcompilerppufile);
  161. begin
  162. inherited ppuwrite(ppufile);
  163. ppufile.putset(tppuset1(inlinenodeflags));
  164. ppufile.putlongint(longint(inlinenumber));
  165. end;
  166. function tinlinenode.dogetcopy : tnode;
  167. var
  168. n : tinlinenode;
  169. begin
  170. n:=tinlinenode(inherited dogetcopy);
  171. n.inlinenodeflags:=inlinenodeflags;
  172. n.inlinenumber:=inlinenumber;
  173. result:=n;
  174. end;
  175. procedure tinlinenode.printnodeinfo(var t : text);
  176. begin
  177. inherited;
  178. write(t,', inlinenumber = ',inlinenumber);
  179. end;
  180. {$ifdef DEBUG_NODE_XML}
  181. procedure TInlineNode.XMLPrintNodeInfo(var T: Text);
  182. var
  183. i: TInlineNodeFlag;
  184. First: Boolean;
  185. begin
  186. inherited XMLPrintNodeInfo(T);
  187. First := True;
  188. for i in inlinenodeflags do
  189. begin
  190. if First then
  191. begin
  192. Write(T, ' inlinenodeflags="', i);
  193. First := False;
  194. end
  195. else
  196. Write(T, ',', i)
  197. end;
  198. if not First then
  199. Write(T, '"');
  200. Write(T, ' inlinenumber="', inlinenumber, '"');
  201. end;
  202. {$endif DEBUG_NODE_XML}
  203. function get_str_int_func(def: tdef): string;
  204. var
  205. ordtype: tordtype;
  206. begin
  207. ordtype := torddef(def).ordtype;
  208. if not (ordtype in [scurrency,s64bit,u64bit,s32bit,u32bit,s16bit,u16bit,s8bit,u8bit]) then
  209. internalerror(2013032603);
  210. if is_oversizedord(def) then
  211. begin
  212. case ordtype of
  213. scurrency,
  214. s64bit: exit('int64');
  215. u64bit: exit('qword');
  216. s32bit: exit('longint');
  217. u32bit: exit('longword');
  218. s16bit: exit('smallint');
  219. u16bit: exit('word');
  220. else
  221. internalerror(2013032604);
  222. end;
  223. end
  224. else
  225. begin
  226. if is_nativeuint(def) then
  227. exit('uint')
  228. else
  229. exit('sint');
  230. end;
  231. internalerror(2013032605);
  232. end;
  233. function tinlinenode.handle_str : tnode;
  234. var
  235. lenpara,
  236. fracpara,
  237. newparas,
  238. tmppara,
  239. dest,
  240. source : tcallparanode;
  241. procname: string;
  242. is_real,is_enum : boolean;
  243. rt : aint;
  244. intrinsiccode: TInlineNumber;
  245. begin
  246. result := cerrornode.create;
  247. { get destination string }
  248. dest := tcallparanode(left);
  249. { get source para (number) }
  250. source := dest;
  251. while assigned(source.right) do
  252. source := tcallparanode(source.right);
  253. { destination parameter must be a normal (not a colon) parameter, this
  254. check is needed because str(v:len) also has 2 parameters }
  255. if (source=dest) or
  256. (cpf_is_colon_para in tcallparanode(dest).callparaflags) then
  257. begin
  258. CGMessage1(parser_e_wrong_parameter_size,'Str');
  259. exit;
  260. end;
  261. { in case we are in a generic definition, we cannot
  262. do all checks, the parameters might be type parameters,
  263. bailout as well in case of an error before }
  264. if (df_generic in current_procinfo.procdef.defoptions) or
  265. (dest.resultdef.typ=errordef) or
  266. (source.resultdef.typ=errordef) then
  267. begin
  268. result.Free;
  269. result:=nil;
  270. resultdef:=voidtype;
  271. exit;
  272. end;
  273. is_real:=(source.resultdef.typ = floatdef) or is_currency(source.resultdef);
  274. is_enum:=source.left.resultdef.typ=enumdef;
  275. if ((dest.left.resultdef.typ<>stringdef) and
  276. not(is_chararray(dest.left.resultdef))) or
  277. not(is_real or is_enum or
  278. (source.left.resultdef.typ=orddef)) then
  279. begin
  280. CGMessagePos(fileinfo,parser_e_illegal_expression);
  281. exit;
  282. end;
  283. { get len/frac parameters }
  284. lenpara := nil;
  285. fracpara := nil;
  286. if (cpf_is_colon_para in tcallparanode(dest.right).callparaflags) then
  287. begin
  288. lenpara := tcallparanode(dest.right);
  289. { we can let the callnode do the type checking of these parameters too, }
  290. { but then the error messages aren't as nice }
  291. if not is_integer(lenpara.resultdef) then
  292. begin
  293. CGMessagePos1(lenpara.fileinfo,
  294. type_e_integer_expr_expected,lenpara.resultdef.typename);
  295. exit;
  296. end;
  297. if (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  298. begin
  299. { parameters are in reverse order! }
  300. fracpara := lenpara;
  301. lenpara := tcallparanode(lenpara.right);
  302. if not is_real then
  303. begin
  304. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  305. exit
  306. end;
  307. if not is_integer(lenpara.resultdef) then
  308. begin
  309. CGMessagePos1(lenpara.fileinfo,
  310. type_e_integer_expr_expected,lenpara.resultdef.typename);
  311. exit;
  312. end;
  313. end;
  314. end;
  315. { generate the parameter list for the compilerproc }
  316. newparas := dest;
  317. { if we have a float parameter, insert the realtype, len and fracpara parameters }
  318. if is_real then
  319. begin
  320. { insert realtype parameter }
  321. if not is_currency(source.resultdef) then
  322. begin
  323. rt:=ord(tfloatdef(source.left.resultdef).floattype);
  324. newparas.right := ccallparanode.create(cordconstnode.create(
  325. rt,s32inttype,true),newparas.right);
  326. tmppara:=tcallparanode(newparas.right);
  327. end
  328. else
  329. tmppara:=newparas;
  330. { if necessary, insert a fraction parameter }
  331. if not assigned(fracpara) then
  332. begin
  333. tmppara.right := ccallparanode.create(
  334. cordconstnode.create(int64(-1),s32inttype,false),
  335. tmppara.right);
  336. fracpara := tcallparanode(tmppara.right);
  337. end;
  338. { if necessary, insert a length para }
  339. if not assigned(lenpara) then
  340. fracpara.right := ccallparanode.create(
  341. cordconstnode.create(int64(-32767),s32inttype,false),
  342. fracpara.right);
  343. end
  344. else if is_enum then
  345. begin
  346. {Insert a reference to the ord2string index.}
  347. newparas.right:=Ccallparanode.create(
  348. Caddrnode.create_internal(
  349. Crttinode.create(Tenumdef(source.left.resultdef),fullrtti,rdt_normal)
  350. ),
  351. newparas.right);
  352. {Insert a reference to the typinfo.}
  353. newparas.right:=Ccallparanode.create(
  354. Caddrnode.create_internal(
  355. Crttinode.create(Tenumdef(source.left.resultdef),fullrtti,rdt_ord2str)
  356. ),
  357. newparas.right);
  358. {Insert a type conversion from the enumeration to longint.}
  359. source.left:=Ctypeconvnode.create_internal(source.left,s32inttype);
  360. typecheckpass(source.left);
  361. { if necessary, insert a length para }
  362. if not assigned(lenpara) then
  363. Tcallparanode(Tcallparanode(newparas.right).right).right:=
  364. Ccallparanode.create(
  365. cordconstnode.create(int64(-1),s32inttype,false),
  366. Tcallparanode(Tcallparanode(newparas.right).right).right
  367. );
  368. end
  369. else
  370. { for a normal parameter, insert a only length parameter if one is missing }
  371. if not assigned(lenpara) then
  372. newparas.right := ccallparanode.create(cordconstnode.create(int64(-1),s32inttype,false),
  373. newparas.right);
  374. { remove the parameters from the original node so they won't get disposed, }
  375. { since they're reused }
  376. left := nil;
  377. intrinsiccode := Default(TInlineNumber);
  378. { create procedure name }
  379. if is_chararray(dest.resultdef) then
  380. procname:='fpc_chararray_'
  381. else
  382. procname := 'fpc_' + tstringdef(dest.resultdef).stringtypname+'_';
  383. if is_real then
  384. if is_currency(source.resultdef) then
  385. procname := procname + 'currency'
  386. else
  387. procname := procname + 'float'
  388. else if is_enum then
  389. procname:=procname+'enum'
  390. else
  391. case torddef(source.resultdef).ordtype of
  392. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64,
  393. bool8bit,bool16bit,bool32bit,bool64bit:
  394. procname := procname + 'bool';
  395. else
  396. begin
  397. intrinsiccode := in_str_x_string;
  398. procname := procname + get_str_int_func(source.resultdef);
  399. end;
  400. end;
  401. { for ansistrings insert the encoding argument }
  402. if is_ansistring(dest.resultdef) then
  403. newparas:=ccallparanode.create(cordconstnode.create(
  404. getparaencoding(dest.resultdef),u16inttype,true),newparas);
  405. { free the errornode we generated in the beginning }
  406. result.free;
  407. { create the call node, }
  408. result := ccallnode.createfromintrinsic(intrinsiccode,procname,newparas);
  409. end;
  410. function tinlinenode.handle_default: tnode;
  411. function getdefaultvarsym(def:tdef):tnode;
  412. var
  413. hashedid : thashedidstring;
  414. srsym : tsym;
  415. srsymtable : tsymtable;
  416. defaultname : tidstring;
  417. varspez : tvarspez;
  418. begin
  419. if not assigned(def) or
  420. not (def.typ in [arraydef,recorddef,variantdef,objectdef,procvardef]) or
  421. ((def.typ=objectdef) and not is_object(def)) then
  422. internalerror(201202101);
  423. { extra '$' prefix because on darwin the result of makemangledname
  424. is prefixed by '_' and hence adding a '$' at the start of the
  425. prefix passed to makemangledname doesn't help (the whole point of
  426. the copy() operation below is to ensure that the id does not start
  427. with a '$', because that is interpreted specially by the symtable
  428. routines -- that's also why we prefix with '$_', so it will still
  429. work if make_mangledname() would somehow return a name that already
  430. starts with '$' }
  431. defaultname:='$_'+make_mangledname('zero',def.owner,def.typesym.Name);
  432. { can't hardcode the position of the '$', e.g. on darwin an underscore
  433. is added }
  434. hashedid.id:=copy(defaultname,2,255);
  435. { in case of a previous error, current_procinfo might not be set
  436. so avoid a crash in this case }
  437. if assigned(current_procinfo) then
  438. begin
  439. { the default sym is always part of the current procedure/function }
  440. srsymtable:=current_module.localsymtable;
  441. srsym:=tsym(srsymtable.findwithhash(hashedid));
  442. if not assigned(srsym) then
  443. begin
  444. varspez:=vs_const;
  445. { if we have an initialize or finalize management operator then
  446. we may not declare this as const as the unit init-/finalization
  447. needs to be able to modify it }
  448. if (def.typ=recorddef) and (mop_initialize in trecordsymtable(trecorddef(def).symtable).managementoperators) then
  449. varspez:=vs_var;
  450. { no valid default variable found, so create it }
  451. srsym:=cstaticvarsym.create(defaultname,varspez,def,[]);
  452. { mark the staticvarsym as typedconst }
  453. if varspez=vs_const then
  454. include(tabstractvarsym(srsym).varoptions,vo_is_typed_const);
  455. include(tabstractvarsym(srsym).varoptions,vo_is_default_var);
  456. { The variable has a value assigned }
  457. tabstractvarsym(srsym).varstate:=vs_initialised;
  458. srsymtable.insertsym(srsym);
  459. cnodeutils.insertbssdata(tstaticvarsym(srsym));
  460. end;
  461. result:=cloadnode.create(srsym,srsymtable);
  462. end
  463. else
  464. result:=cerrornode.create;
  465. end;
  466. var
  467. def : tdef;
  468. begin
  469. if not assigned(left) or (left.nodetype<>typen) then
  470. internalerror(2012032102);
  471. def:=ttypenode(left).typedef;
  472. if assigned(current_procinfo) and
  473. (df_generic in current_procinfo.procdef.defoptions) then
  474. begin
  475. { don't allow as a default parameter value, because it may not be valid
  476. when specialising }
  477. if block_type<>bt_const then
  478. result:=cpointerconstnode.create(0,def)
  479. else
  480. result:=cerrornode.create;
  481. exit;
  482. end;
  483. result:=nil;
  484. case def.typ of
  485. enumdef,
  486. orddef:
  487. { don't do a rangecheck as Default will also return 0
  488. for the following types (Delphi compatible):
  489. TRange1 = -10..-5;
  490. TRange2 = 5..10;
  491. TEnum = (a:=5;b:=10); }
  492. result:=cordconstnode.create(0,def,false);
  493. classrefdef,
  494. pointerdef:
  495. result:=cpointerconstnode.create(0,def);
  496. procvardef:
  497. if tprocvardef(def).size<>sizeof(pint) then
  498. result:=getdefaultvarsym(def)
  499. else
  500. result:=cpointerconstnode.create(0,def);
  501. stringdef:
  502. result:=cstringconstnode.createstr('');
  503. floatdef:
  504. result:=crealconstnode.create(0,def);
  505. objectdef:
  506. begin
  507. if is_implicit_pointer_object_type(def) then
  508. result:=cpointerconstnode.create(0,def)
  509. else
  510. if is_object(def) then
  511. begin
  512. { Delphi does not recursively check whether
  513. an object contains unsupported types }
  514. if not (m_delphi in current_settings.modeswitches) and
  515. not is_valid_for_default(def) then
  516. Message(type_e_type_not_allowed_for_default);
  517. result:=getdefaultvarsym(def);
  518. end
  519. else
  520. Message(type_e_type_not_allowed_for_default);
  521. end;
  522. variantdef,
  523. recorddef:
  524. begin
  525. { Delphi does not recursively check whether a record
  526. contains unsupported types }
  527. if (def.typ=recorddef) and not (m_delphi in current_settings.modeswitches) and
  528. not is_valid_for_default(def) then
  529. Message(type_e_type_not_allowed_for_default);
  530. result:=getdefaultvarsym(def);
  531. end;
  532. setdef:
  533. begin
  534. result:=csetconstnode.create(nil,def);
  535. New(tsetconstnode(result).value_set);
  536. tsetconstnode(result).value_set^:=[];
  537. end;
  538. arraydef:
  539. begin
  540. { can other array types be parsed by single_type? }
  541. if ado_isdynamicarray in tarraydef(def).arrayoptions then
  542. result:=cpointerconstnode.create(0,def)
  543. else
  544. begin
  545. result:=getdefaultvarsym(def);
  546. end;
  547. end;
  548. undefineddef:
  549. begin
  550. if sp_generic_dummy in def.typesym.symoptions then
  551. begin
  552. { this matches the error messages that are printed
  553. in case of non-Delphi modes }
  554. Message(parser_e_no_generics_as_types);
  555. Message(type_e_type_id_expected);
  556. end
  557. else
  558. result:=cpointerconstnode.create(0,def);
  559. end;
  560. else
  561. Message(type_e_type_not_allowed_for_default);
  562. end;
  563. if not assigned(result) then
  564. result:=cerrornode.create;
  565. end;
  566. function tinlinenode.handle_reset_rewrite_typed: tnode;
  567. begin
  568. { since this is a "in_xxxx_typedfile" node, we can be sure we have }
  569. { a typed file as argument and we don't have to check it again (JM) }
  570. { add the recsize parameter }
  571. { iso mode extension with name? }
  572. if inlinenumber in [in_reset_typedfile_name,in_rewrite_typedfile_name] then
  573. begin
  574. left := ccallparanode.create(cordconstnode.create(
  575. tfiledef(tcallparanode(tcallparanode(left).nextpara).paravalue.resultdef).typedfiledef.size,s32inttype,true),left);
  576. end
  577. else
  578. begin
  579. { note: for some reason, the parameter of intern procedures with only one }
  580. { parameter is gets lifted out of its original tcallparanode (see round }
  581. { line 1306 of ncal.pas), so recreate a tcallparanode here (JM) }
  582. left := ccallparanode.create(cordconstnode.create(
  583. tfiledef(left.resultdef).typedfiledef.size,s32inttype,true),
  584. ccallparanode.create(left,nil));
  585. end;
  586. { create the correct call }
  587. if m_isolike_io in current_settings.modeswitches then
  588. begin
  589. case inlinenumber of
  590. in_reset_typedfile:
  591. result := ccallnode.createintern('fpc_reset_typed_iso',left);
  592. in_reset_typedfile_name:
  593. result := ccallnode.createintern('fpc_reset_typed_name_iso',left);
  594. in_rewrite_typedfile:
  595. result := ccallnode.createintern('fpc_rewrite_typed_iso',left);
  596. in_rewrite_typedfile_name:
  597. result := ccallnode.createintern('fpc_rewrite_typed_name_iso',left);
  598. else
  599. internalerror(2016101502);
  600. end;
  601. end
  602. else
  603. begin
  604. if inlinenumber=in_reset_typedfile then
  605. result := ccallnode.createintern('fpc_reset_typed',left)
  606. else
  607. result := ccallnode.createintern('fpc_rewrite_typed',left);
  608. end;
  609. { make sure left doesn't get disposed, since we use it in the new call }
  610. left := nil;
  611. end;
  612. procedure maybe_convert_to_string(var n: tnode);
  613. begin
  614. { stringconstnodes are arrays of char. It's much more }
  615. { efficient to write a constant string, so convert }
  616. { either to shortstring or ansistring depending on }
  617. { length }
  618. if (n.nodetype=stringconstn) then
  619. if is_chararray(n.resultdef) then
  620. if (tstringconstnode(n).len<=255) then
  621. inserttypeconv(n,cshortstringtype)
  622. else
  623. inserttypeconv(n,getansistringdef)
  624. else if is_widechararray(n.resultdef) then
  625. inserttypeconv(n,cunicodestringtype);
  626. end;
  627. procedure get_read_write_int_func(def: tdef; out func_suffix: string; out readfunctype: tdef);
  628. var
  629. ordtype: tordtype;
  630. begin
  631. ordtype := torddef(def).ordtype;
  632. if is_oversizedint(def) then
  633. begin
  634. case ordtype of
  635. s64bit:
  636. begin
  637. func_suffix := 'int64';
  638. readfunctype:=s64inttype;
  639. end;
  640. u64bit :
  641. begin
  642. func_suffix := 'qword';
  643. readfunctype:=u64inttype;
  644. end;
  645. s32bit:
  646. begin
  647. func_suffix := 'longint';
  648. readfunctype:=s32inttype;
  649. end;
  650. u32bit :
  651. begin
  652. func_suffix := 'longword';
  653. readfunctype:=u32inttype;
  654. end;
  655. s16bit:
  656. begin
  657. func_suffix := 'smallint';
  658. readfunctype:=s16inttype;
  659. end;
  660. u16bit :
  661. begin
  662. func_suffix := 'word';
  663. readfunctype:=u16inttype;
  664. end;
  665. else
  666. internalerror(2013032602);
  667. end;
  668. end
  669. else
  670. begin
  671. case ordtype of
  672. s64bit,
  673. s32bit,
  674. s16bit,
  675. s8bit:
  676. begin
  677. func_suffix := 'sint';
  678. readfunctype := sinttype;
  679. end;
  680. u64bit,
  681. u32bit,
  682. u16bit,
  683. u8bit:
  684. begin
  685. func_suffix := 'uint';
  686. readfunctype := uinttype;
  687. end;
  688. else
  689. internalerror(2013032601);
  690. end;
  691. end;
  692. end;
  693. function Tinlinenode.handle_text_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  694. {Read(ln)/write(ln) for text files.}
  695. const procprefixes:array[boolean] of string[15]=('fpc_write_text_','fpc_read_text_');
  696. var error_para,is_real,special_handling,found_error,do_read:boolean;
  697. p1:Tnode;
  698. nextpara,
  699. indexpara,
  700. lenpara,
  701. para,
  702. fracpara:Tcallparanode;
  703. temp:Ttempcreatenode;
  704. readfunctype:Tdef;
  705. name:string[63];
  706. func_suffix:string[8];
  707. begin
  708. para:=Tcallparanode(params);
  709. found_error:=false;
  710. do_read:=inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  711. name:='';
  712. while assigned(para) do
  713. begin
  714. { is this parameter faulty? }
  715. error_para:=false;
  716. { is this parameter a real? }
  717. is_real:=false;
  718. { type used for the read(), this is used to check
  719. whether a temp is needed for range checking }
  720. readfunctype:=nil;
  721. { can't read/write types }
  722. if (para.left.nodetype=typen) and not(is_typeparam(ttypenode(para.left).typedef)) then
  723. begin
  724. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  725. error_para := true;
  726. end;
  727. { support writeln(procvar) }
  728. if para.left.resultdef.typ=procvardef then
  729. begin
  730. p1:=ccallnode.create_procvar(nil,para.left);
  731. typecheckpass(p1);
  732. para.left:=p1;
  733. end;
  734. if inlinenumber in [in_write_x,in_writeln_x] then
  735. { prefer strings to chararrays }
  736. maybe_convert_to_string(para.left);
  737. if is_typeparam(para.left.resultdef) then
  738. error_para:=true
  739. else
  740. case para.left.resultdef.typ of
  741. stringdef :
  742. begin
  743. name:=procprefixes[do_read]+tstringdef(para.left.resultdef).stringtypname;
  744. if (m_isolike_io in current_settings.modeswitches) and (tstringdef(para.left.resultdef).stringtype<>st_shortstring) then
  745. begin
  746. CGMessagePos(para.fileinfo,type_e_cant_read_write_type_in_iso_mode);
  747. error_para := true;
  748. end;
  749. end;
  750. pointerdef :
  751. begin
  752. if (not is_pchar(para.left.resultdef)) or do_read then
  753. begin
  754. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  755. error_para := true;
  756. end
  757. else
  758. name:=procprefixes[do_read]+'pchar_as_pointer';
  759. end;
  760. floatdef :
  761. begin
  762. is_real:=true;
  763. if Tfloatdef(para.left.resultdef).floattype=s64currency then
  764. name := procprefixes[do_read]+'currency'
  765. else
  766. begin
  767. name := procprefixes[do_read]+'float';
  768. readfunctype:=pbestrealtype^;
  769. end;
  770. { iso pascal needs a different handler }
  771. if (m_isolike_io in current_settings.modeswitches) and do_read then
  772. name:=name+'_iso';
  773. end;
  774. enumdef:
  775. begin
  776. if m_isolike_io in current_settings.modeswitches then
  777. begin
  778. error_para := true;
  779. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  780. end;
  781. name:=procprefixes[do_read]+'enum';
  782. if do_read then
  783. { read is done with a var parameter so we need the correct
  784. size for that }
  785. case para.left.resultdef.size of
  786. 1:
  787. begin
  788. name:=name+'_shortint';
  789. readfunctype:=s8inttype;
  790. end;
  791. 2:
  792. begin
  793. name:=name+'_smallint';
  794. readfunctype:=s16inttype;
  795. end;
  796. 4:
  797. begin
  798. name:=name+'_longint';
  799. readfunctype:=s32inttype;
  800. end;
  801. else
  802. internalerror(2022082601);
  803. end
  804. else
  805. readfunctype:=s32inttype;
  806. end;
  807. orddef :
  808. begin
  809. case Torddef(para.left.resultdef).ordtype of
  810. s8bit,
  811. s16bit,
  812. s32bit,
  813. s64bit,
  814. u8bit,
  815. u16bit,
  816. u32bit,
  817. u64bit:
  818. begin
  819. get_read_write_int_func(para.left.resultdef,func_suffix,readfunctype);
  820. name := procprefixes[do_read]+func_suffix;
  821. if (m_isolike_io in current_settings.modeswitches) and do_read then
  822. name:=name+'_iso';
  823. end;
  824. uchar :
  825. begin
  826. name := procprefixes[do_read]+'char';
  827. { iso pascal needs a different handler }
  828. if (m_isolike_io in current_settings.modeswitches) and do_read then
  829. name:=name+'_iso';
  830. readfunctype:=cansichartype;
  831. end;
  832. uwidechar :
  833. begin
  834. name := procprefixes[do_read]+'widechar';
  835. readfunctype:=cwidechartype;
  836. end;
  837. scurrency:
  838. begin
  839. name := procprefixes[do_read]+'currency';
  840. { iso pascal needs a different handler }
  841. if (m_isolike_io in current_settings.modeswitches) and do_read then
  842. name:=name+'_iso';
  843. readfunctype:=s64currencytype;
  844. is_real:=true;
  845. end;
  846. pasbool1,
  847. pasbool8,
  848. pasbool16,
  849. pasbool32,
  850. pasbool64,
  851. bool8bit,
  852. bool16bit,
  853. bool32bit,
  854. bool64bit:
  855. if do_read then
  856. begin
  857. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  858. error_para := true;
  859. end
  860. else
  861. begin
  862. name := procprefixes[do_read]+'boolean';
  863. readfunctype:=pasbool1type;
  864. end
  865. else
  866. begin
  867. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  868. error_para := true;
  869. end;
  870. end;
  871. end;
  872. variantdef :
  873. begin
  874. name:=procprefixes[do_read]+'variant';
  875. include(current_module.moduleflags,mf_uses_variants);
  876. end;
  877. arraydef :
  878. begin
  879. if is_chararray(para.left.resultdef) then
  880. name := procprefixes[do_read]+'pchar_as_array'
  881. else
  882. begin
  883. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  884. error_para := true;
  885. end
  886. end;
  887. else
  888. begin
  889. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  890. error_para := true;
  891. end;
  892. end;
  893. { iso pascal needs a different handler }
  894. if (m_isolike_io in current_settings.modeswitches) and not(do_read) then
  895. name:=name+'_iso';
  896. { check for length/fractional colon para's }
  897. fracpara:=nil;
  898. lenpara:=nil;
  899. indexpara:=nil;
  900. if assigned(para.right) and
  901. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  902. begin
  903. lenpara := tcallparanode(para.right);
  904. if assigned(lenpara.right) and
  905. (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  906. fracpara:=tcallparanode(lenpara.right);
  907. end;
  908. { get the next parameter now already, because we're going }
  909. { to muck around with the pointers }
  910. if assigned(fracpara) then
  911. nextpara := tcallparanode(fracpara.right)
  912. else if assigned(lenpara) then
  913. nextpara := tcallparanode(lenpara.right)
  914. else
  915. nextpara := tcallparanode(para.right);
  916. { check if a fracpara is allowed }
  917. if assigned(fracpara) and not is_real then
  918. begin
  919. CGMessagePos(fracpara.fileinfo,parser_e_illegal_colon_qualifier);
  920. error_para := true;
  921. end
  922. else if assigned(lenpara) and do_read then
  923. begin
  924. { I think this is already filtered out by parsing, but I'm not sure (JM) }
  925. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  926. error_para := true;
  927. end;
  928. { adjust found_error }
  929. found_error := found_error or error_para;
  930. if not error_para then
  931. begin
  932. special_handling:=false;
  933. { create dummy frac/len para's if necessary }
  934. if not do_read then
  935. begin
  936. { difference in default value for floats and the rest :( }
  937. if not is_real then
  938. begin
  939. if not assigned(lenpara) then
  940. begin
  941. if m_isolike_io in current_settings.modeswitches then
  942. lenpara := ccallparanode.create(
  943. cordconstnode.create(-1,s32inttype,false),nil)
  944. else
  945. lenpara := ccallparanode.create(
  946. cordconstnode.create(0,s32inttype,false),nil);
  947. end
  948. else
  949. { make sure we don't pass the successive }
  950. { parameters too. We also already have a }
  951. { reference to the next parameter in }
  952. { nextpara }
  953. lenpara.right := nil;
  954. end
  955. else
  956. begin
  957. if not assigned(lenpara) then
  958. lenpara := ccallparanode.create(
  959. cordconstnode.create(int64(-32767),s32inttype,false),nil);
  960. { also create a default fracpara if necessary }
  961. if not assigned(fracpara) then
  962. fracpara := ccallparanode.create(
  963. cordconstnode.create(int64(-1),s32inttype,false),nil);
  964. { add it to the lenpara }
  965. lenpara.right := fracpara;
  966. if not is_currency(para.left.resultdef) then
  967. begin
  968. { and add the realtype para (this also removes the link }
  969. { to any parameters coming after it) }
  970. fracpara.right := ccallparanode.create(
  971. cordconstnode.create(ord(tfloatdef(para.left.resultdef).floattype),
  972. s32inttype,true),nil);
  973. end
  974. else
  975. fracpara.right:=nil;
  976. end;
  977. if para.left.resultdef.typ=enumdef then
  978. begin
  979. {To write(ln) an enum we need a some extra parameters.}
  980. {Insert a reference to the ord2string index.}
  981. indexpara:=Ccallparanode.create(
  982. Caddrnode.create_internal(
  983. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_normal)
  984. ),
  985. nil);
  986. {Insert a reference to the typinfo.}
  987. indexpara:=Ccallparanode.create(
  988. Caddrnode.create_internal(
  989. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_ord2str)
  990. ),
  991. indexpara);
  992. {Insert a type conversion to to convert the enum to longint.}
  993. para.left:=Ctypeconvnode.create_internal(para.left,s32inttype);
  994. typecheckpass(para.left);
  995. end;
  996. end
  997. else
  998. begin
  999. {To read(ln) an enum we need a an extra parameter.}
  1000. if para.left.resultdef.typ=enumdef then
  1001. begin
  1002. {Insert a reference to the string2ord index.}
  1003. indexpara:=Ccallparanode.create(Caddrnode.create_internal(
  1004. Crttinode.create(Tenumdef(para.left.resultdef),fullrtti,rdt_str2ord)
  1005. ),nil);
  1006. {Insert a type conversion to to convert the enum to longint.}
  1007. para.left:=Ctypeconvnode.create_internal(para.left,readfunctype);
  1008. typecheckpass(para.left);
  1009. end;
  1010. { special handling of reading small numbers, because the helpers }
  1011. { expect a longint/card/bestreal var parameter. Use a temp. can't }
  1012. { use functions because then the call to FPC_IOCHECK destroys }
  1013. { their result before we can store it }
  1014. if (readfunctype<>nil) and (para.left.resultdef<>readfunctype) then
  1015. special_handling:=true;
  1016. end;
  1017. if special_handling then
  1018. begin
  1019. { since we're not going to pass the parameter as var-parameter }
  1020. { to the read function, manually check whether the parameter }
  1021. { can be used as var-parameter (e.g., whether it isn't a }
  1022. { property) }
  1023. valid_for_var(para.left,true);
  1024. { create the parameter list: the temp ... }
  1025. temp := ctempcreatenode.create(readfunctype,readfunctype.size,tt_persistent,false);
  1026. addstatement(Tstatementnode(newstatement),temp);
  1027. { ... and the file }
  1028. p1 := ccallparanode.create(ctemprefnode.create(temp),
  1029. filepara.getcopy);
  1030. Tcallparanode(Tcallparanode(p1).right).right:=indexpara;
  1031. { create the call to the helper }
  1032. addstatement(Tstatementnode(newstatement),
  1033. ccallnode.createintern(name,tcallparanode(p1)));
  1034. { assign the result to the original var (this automatically }
  1035. { takes care of range checking) }
  1036. addstatement(Tstatementnode(newstatement),
  1037. cassignmentnode.create(para.left,
  1038. ctemprefnode.create(temp)));
  1039. { release the temp location }
  1040. addstatement(Tstatementnode(newstatement),ctempdeletenode.create(temp));
  1041. { statement of para is used }
  1042. para.left := nil;
  1043. { free the enclosing tcallparanode, but not the }
  1044. { parameters coming after it }
  1045. para.right := nil;
  1046. para.free;
  1047. end
  1048. else
  1049. { read of non s/u-8/16bit, or a write }
  1050. begin
  1051. { add the filepara to the current parameter }
  1052. para.right := filepara.getcopy;
  1053. {Add the lenpara and the indexpara(s) (fracpara and realtype are
  1054. already linked with the lenpara if necessary).}
  1055. if indexpara=nil then
  1056. Tcallparanode(para.right).right:=lenpara
  1057. else
  1058. begin
  1059. if lenpara=nil then
  1060. Tcallparanode(para.right).right:=indexpara
  1061. else
  1062. begin
  1063. Tcallparanode(para.right).right:=lenpara;
  1064. lenpara.right:=indexpara;
  1065. end;
  1066. { indexpara.right:=lenpara;}
  1067. end;
  1068. { in case of writing a chararray, add whether it's zero-based }
  1069. if para.left.resultdef.typ=arraydef then
  1070. para := ccallparanode.create(cordconstnode.create(
  1071. ord(tarraydef(para.left.resultdef).lowrange=0),pasbool1type,false),para)
  1072. else
  1073. { in case of reading an ansistring pass a codepage argument }
  1074. if do_read and is_ansistring(para.left.resultdef) then
  1075. para:=ccallparanode.create(cordconstnode.create(
  1076. getparaencoding(para.left.resultdef),u16inttype,true),para);
  1077. { create the call statement }
  1078. addstatement(Tstatementnode(newstatement),
  1079. ccallnode.createintern(name,para));
  1080. end
  1081. end
  1082. else
  1083. { error_para = true }
  1084. begin
  1085. { free the parameter, since it isn't referenced anywhere anymore }
  1086. para.right := nil;
  1087. para.free;
  1088. if assigned(lenpara) then
  1089. begin
  1090. lenpara.right := nil;
  1091. lenpara.free;
  1092. end;
  1093. if assigned(fracpara) then
  1094. begin
  1095. fracpara.right := nil;
  1096. fracpara.free;
  1097. end;
  1098. end;
  1099. { process next parameter }
  1100. para := nextpara;
  1101. end;
  1102. { if no error, add the write(ln)/read(ln) end calls }
  1103. if not found_error then
  1104. begin
  1105. case inlinenumber of
  1106. in_read_x,
  1107. in_readstr_x:
  1108. name:='fpc_read_end';
  1109. in_write_x,
  1110. in_writestr_x:
  1111. name:='fpc_write_end';
  1112. in_readln_x:
  1113. begin
  1114. name:='fpc_readln_end';
  1115. if m_isolike_io in current_settings.modeswitches then
  1116. name:=name+'_iso';
  1117. end;
  1118. in_writeln_x:
  1119. name:='fpc_writeln_end';
  1120. else
  1121. internalerror(2019050501);
  1122. end;
  1123. addstatement(Tstatementnode(newstatement),ccallnode.createintern(name,filepara.getcopy));
  1124. end;
  1125. handle_text_read_write:=found_error;
  1126. end;
  1127. function Tinlinenode.handle_typed_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  1128. {Read/write for typed files.}
  1129. const procprefixes:array[boolean,boolean] of string[19]=(('fpc_typed_write','fpc_typed_read'),
  1130. ('fpc_typed_write','fpc_typed_read_iso'));
  1131. procnamesdisplay:array[boolean,boolean] of string[8] = (('Write','Read'),('WriteStr','ReadStr'));
  1132. var found_error,do_read,is_rwstr:boolean;
  1133. para,nextpara:Tcallparanode;
  1134. p1:Tnode;
  1135. temp:Ttempcreatenode;
  1136. begin
  1137. found_error:=false;
  1138. para:=Tcallparanode(params);
  1139. do_read:=inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  1140. is_rwstr := inlinenumber in [in_readstr_x,in_writestr_x];
  1141. temp:=nil;
  1142. { add the typesize to the filepara }
  1143. if filepara.resultdef.typ=filedef then
  1144. filepara.right := ccallparanode.create(cordconstnode.create(
  1145. tfiledef(filepara.resultdef).typedfiledef.size,s32inttype,true),nil);
  1146. { check for "no parameters" (you need at least one extra para for typed files) }
  1147. if not assigned(para) then
  1148. begin
  1149. CGMessage1(parser_e_wrong_parameter_size,procnamesdisplay[is_rwstr,do_read]);
  1150. found_error := true;
  1151. end;
  1152. { process all parameters }
  1153. while assigned(para) do
  1154. begin
  1155. { check if valid parameter }
  1156. if para.left.nodetype=typen then
  1157. begin
  1158. CGMessagePos(para.left.fileinfo,type_e_cant_read_write_type);
  1159. found_error := true;
  1160. end;
  1161. { support writeln(procvar) }
  1162. if (para.left.resultdef.typ=procvardef) then
  1163. begin
  1164. p1:=ccallnode.create_procvar(nil,para.left);
  1165. typecheckpass(p1);
  1166. para.left:=p1;
  1167. end;
  1168. if filepara.resultdef.typ=filedef then
  1169. inserttypeconv(para.left,tfiledef(filepara.resultdef).typedfiledef);
  1170. if assigned(para.right) and
  1171. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  1172. begin
  1173. CGMessagePos(para.right.fileinfo,parser_e_illegal_colon_qualifier);
  1174. { skip all colon para's }
  1175. nextpara := tcallparanode(tcallparanode(para.right).right);
  1176. while assigned(nextpara) and (cpf_is_colon_para in nextpara.callparaflags) do
  1177. nextpara := tcallparanode(nextpara.right);
  1178. found_error := true;
  1179. end
  1180. else
  1181. { get next parameter }
  1182. nextpara := tcallparanode(para.right);
  1183. { When we have a call, we have a problem: you can't pass the }
  1184. { result of a call as a formal const parameter. Solution: }
  1185. { assign the result to a temp and pass this temp as parameter }
  1186. { This is not very efficient, but write(typedfile,x) is }
  1187. { already slow by itself anyway (no buffering) (JM) }
  1188. { Actually, thge same goes for every non-simple expression }
  1189. { (such as an addition, ...) -> put everything but load nodes }
  1190. { into temps (JM) }
  1191. { of course, this must only be allowed for writes!!! (JM) }
  1192. if not(do_read) and (para.left.nodetype <> loadn) then
  1193. begin
  1194. { create temp for result }
  1195. temp := ctempcreatenode.create(para.left.resultdef,
  1196. para.left.resultdef.size,tt_persistent,false);
  1197. addstatement(Tstatementnode(newstatement),temp);
  1198. { assign result to temp }
  1199. addstatement(Tstatementnode(newstatement),
  1200. cassignmentnode.create(ctemprefnode.create(temp),
  1201. para.left));
  1202. { replace (reused) paranode with temp }
  1203. para.left := ctemprefnode.create(temp);
  1204. end;
  1205. { add fileparameter }
  1206. para.right := filepara.getcopy;
  1207. { create call statment }
  1208. { since the parameters are in the correct order, we have to insert }
  1209. { the statements always at the end of the current block }
  1210. addstatement(Tstatementnode(newstatement),
  1211. Ccallnode.createintern(procprefixes[m_isolike_io in current_settings.modeswitches,do_read],para
  1212. ));
  1213. { if we used a temp, free it }
  1214. if para.left.nodetype = temprefn then
  1215. addstatement(Tstatementnode(newstatement),ctempdeletenode.create(temp));
  1216. { process next parameter }
  1217. para := nextpara;
  1218. end;
  1219. handle_typed_read_write:=found_error;
  1220. end;
  1221. function tinlinenode.handle_read_write: tnode;
  1222. var
  1223. filepara,
  1224. nextpara,
  1225. params : tcallparanode;
  1226. newstatement : tstatementnode;
  1227. newblock : tblocknode;
  1228. filetemp : Ttempcreatenode;
  1229. name : string[31];
  1230. textsym : ttypesym;
  1231. is_typed,
  1232. do_read,
  1233. is_rwstr,
  1234. found_error : boolean;
  1235. begin
  1236. filepara := nil;
  1237. is_typed := false;
  1238. filetemp := nil;
  1239. do_read := inlinenumber in [in_read_x,in_readln_x,in_readstr_x];
  1240. is_rwstr := inlinenumber in [in_readstr_x,in_writestr_x];
  1241. { if we fail, we can quickly exit this way. We must generate something }
  1242. { instead of the inline node, because firstpass will bomb with an }
  1243. { internalerror if it encounters a read/write }
  1244. result := cerrornode.create;
  1245. { reverse the parameters (needed to get the colon parameters in the }
  1246. { correct order when processing write(ln) }
  1247. reverseparameters(tcallparanode(left));
  1248. if is_rwstr then
  1249. begin
  1250. filepara := tcallparanode(left);
  1251. { needs at least two parameters: source/dest string + min. 1 value }
  1252. if not(assigned(filepara)) or
  1253. not(assigned(filepara.right)) then
  1254. begin
  1255. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,'ReadStr/WriteStr');
  1256. exit;
  1257. end
  1258. else if (filepara.resultdef.typ <> stringdef) then
  1259. begin
  1260. { convert chararray to string, or give an appropriate error message }
  1261. { (if you want to optimize to use shortstring, keep in mind that }
  1262. { readstr internally always uses ansistring, and to account for }
  1263. { chararrays with > 255 characters) }
  1264. inserttypeconv(filepara.left,getansistringdef);
  1265. filepara.resultdef:=filepara.left.resultdef;
  1266. if codegenerror then
  1267. exit;
  1268. end
  1269. end
  1270. else if assigned(left) then
  1271. begin
  1272. { check if we have a file parameter and if yes, what kind it is }
  1273. filepara := tcallparanode(left);
  1274. if (filepara.resultdef.typ=filedef) then
  1275. begin
  1276. if (tfiledef(filepara.resultdef).filetyp=ft_untyped) then
  1277. begin
  1278. CGMessagePos(fileinfo,type_e_no_read_write_for_untyped_file);
  1279. exit;
  1280. end
  1281. else
  1282. begin
  1283. if (tfiledef(filepara.resultdef).filetyp=ft_typed) then
  1284. begin
  1285. if (inlinenumber in [in_readln_x,in_writeln_x]) then
  1286. begin
  1287. CGMessagePos(fileinfo,type_e_no_readln_writeln_for_typed_file);
  1288. exit;
  1289. end;
  1290. is_typed := true;
  1291. end
  1292. end;
  1293. end
  1294. else
  1295. filepara := nil;
  1296. end;
  1297. if assigned(filepara) and
  1298. assigned(filepara.right) and
  1299. (cpf_is_colon_para in tcallparanode(filepara.right).callparaflags) then
  1300. begin
  1301. CGMessagePos(filepara.fileinfo,parser_e_illegal_colon_qualifier);
  1302. { for recovery we can simply continue, because the compiler will
  1303. simply treat the next parameters as normal parameters }
  1304. end;
  1305. { create a blocknode in which the successive write/read statements will be }
  1306. { put, since they belong together. Also create a dummy statement already to }
  1307. { make inserting of additional statements easier }
  1308. newblock:=internalstatements(newstatement);
  1309. if is_rwstr then
  1310. begin
  1311. { create a dummy temp text file that will be used to cache the
  1312. readstr/writestr state. Can't use a global variable in the system
  1313. unit because these can be nested (in case of parameters to
  1314. writestr that are function calls to functions that also call
  1315. readstr/writestr) }
  1316. textsym:=search_system_type('TEXT');
  1317. filetemp:=ctempcreatenode.create(textsym.typedef,textsym.typedef.size,tt_persistent,false);
  1318. addstatement(newstatement,filetemp);
  1319. if (do_read) then
  1320. name:='fpc_setupreadstr_'
  1321. else
  1322. name:='fpc_setupwritestr_';
  1323. name:=name+tstringdef(filepara.resultdef).stringtypname;
  1324. { the file para is a var parameter, but it is properly initialized,
  1325. so it should be actually an out parameter }
  1326. if not(do_read) then
  1327. set_varstate(filepara.left,vs_written,[]);
  1328. { remove the source/destination string parameter from the }
  1329. { parameter chain }
  1330. left:=filepara.right;
  1331. filepara.right:=ccallparanode.create(ctemprefnode.create(filetemp),nil);
  1332. { in case of a writestr() to an ansistring, also pass the string's
  1333. code page }
  1334. if not do_read and
  1335. is_ansistring(filepara.left.resultdef) then
  1336. filepara:=ccallparanode.create(genintconstnode(tstringdef(filepara.left.resultdef).encoding),filepara);
  1337. { pass the temp text file and the source/destination string to the
  1338. setup routine, which will store the string's address in the
  1339. textrec }
  1340. addstatement(newstatement,ccallnode.createintern(name,filepara));
  1341. filepara:=ccallparanode.create(ctemprefnode.create(filetemp),nil);
  1342. end
  1343. { if we don't have a filepara, create one containing the default }
  1344. else if not assigned(filepara) then
  1345. begin
  1346. { since the input/output variables are threadvars loading them into
  1347. a temp once is faster. Create a temp which will hold a pointer to the file }
  1348. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1349. addstatement(newstatement,filetemp);
  1350. { make sure the resultdef of the temp (and as such of the }
  1351. { temprefs coming after it) is set (necessary because the }
  1352. { temprefs will be part of the filepara, of which we need }
  1353. { the resultdef later on and temprefs can only be }
  1354. { typecheckpassed if the resultdef of the temp is known) }
  1355. typecheckpass(tnode(filetemp));
  1356. { assign the address of the file to the temp }
  1357. if do_read then
  1358. name := 'input'
  1359. else
  1360. name := 'output';
  1361. addstatement(newstatement,
  1362. cassignmentnode.create(ctemprefnode.create(filetemp),
  1363. ccallnode.createintern('fpc_get_'+name,nil)));
  1364. { create a new fileparameter as follows: file_type(temp^) }
  1365. { (so that we pass the value and not the address of the temp }
  1366. { to the read/write routine) }
  1367. textsym:=search_system_type('TEXT');
  1368. filepara := ccallparanode.create(ctypeconvnode.create_internal(
  1369. cderefnode.create(ctemprefnode.create(filetemp)),textsym.typedef),nil);
  1370. end
  1371. else
  1372. { remove filepara from the parameter chain }
  1373. begin
  1374. left := filepara.right;
  1375. filepara.right := nil;
  1376. { the file para is a var parameter, but it must be valid already }
  1377. set_varstate(filepara.left,vs_readwritten,[vsf_must_be_valid]);
  1378. { check if we should make a temp to store the result of a complex }
  1379. { expression (better heuristics, anyone?) (JM) }
  1380. if (filepara.left.nodetype <> loadn) then
  1381. begin
  1382. { create a temp which will hold a pointer to the file }
  1383. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1384. { add it to the statements }
  1385. addstatement(newstatement,filetemp);
  1386. { make sure the resultdef of the temp (and as such of the }
  1387. { temprefs coming after it) is set (necessary because the }
  1388. { temprefs will be part of the filepara, of which we need }
  1389. { the resultdef later on and temprefs can only be }
  1390. { typecheckpassed if the resultdef of the temp is known) }
  1391. typecheckpass(tnode(filetemp));
  1392. { assign the address of the file to the temp }
  1393. addstatement(newstatement,
  1394. cassignmentnode.create(ctemprefnode.create(filetemp),
  1395. caddrnode.create_internal(filepara.left)));
  1396. typecheckpass(newstatement.left);
  1397. { create a new fileparameter as follows: file_type(temp^) }
  1398. { (so that we pass the value and not the address of the temp }
  1399. { to the read/write routine) }
  1400. nextpara := ccallparanode.create(ctypeconvnode.create_internal(
  1401. cderefnode.create(ctemprefnode.create(filetemp)),filepara.left.resultdef),nil);
  1402. { replace the old file para with the new one }
  1403. filepara.left := nil;
  1404. filepara.free;
  1405. filepara := nextpara;
  1406. end;
  1407. end;
  1408. { the resultdef of the filepara must be set since it's }
  1409. { used below }
  1410. filepara.get_paratype;
  1411. { now, filepara is nowhere referenced anymore, so we can safely dispose it }
  1412. { if something goes wrong or at the end of the procedure }
  1413. { we're going to reuse the paranodes, so make sure they don't get freed }
  1414. { twice }
  1415. params:=Tcallparanode(left);
  1416. left := nil;
  1417. if is_typed then
  1418. found_error:=handle_typed_read_write(filepara,Ttertiarynode(params),tnode(newstatement))
  1419. else
  1420. found_error:=handle_text_read_write(filepara,Ttertiarynode(params),tnode(newstatement));
  1421. { free the file parameter (it's copied inside the handle_*_read_write methods) }
  1422. filepara.free;
  1423. { if we found an error, simply delete the generated blocknode }
  1424. if found_error then
  1425. begin
  1426. { ensure that the tempinfo is freed correctly by destroying a
  1427. delete node for it
  1428. Note: this might happen legitimately whe parsing a generic that
  1429. passes a undefined type to Write/Read }
  1430. if assigned(filetemp) then
  1431. ctempdeletenode.create(filetemp).free;
  1432. newblock.free
  1433. end
  1434. else
  1435. begin
  1436. { deallocate the temp for the file para if we used one }
  1437. if assigned(filetemp) then
  1438. addstatement(newstatement,ctempdeletenode.create(filetemp));
  1439. { otherwise return the newly generated block of instructions, }
  1440. { but first free the errornode we generated at the beginning }
  1441. result.free;
  1442. result := newblock
  1443. end;
  1444. end;
  1445. function get_val_int_func(def: tdef): string;
  1446. var
  1447. ordtype: tordtype;
  1448. begin
  1449. ordtype := torddef(def).ordtype;
  1450. if not (ordtype in [s64bit,u64bit,s32bit,u32bit,s16bit,u16bit,s8bit,u8bit]) then
  1451. internalerror(2020080101);
  1452. if is_oversizedint(def) then
  1453. begin
  1454. case ordtype of
  1455. s64bit: exit('int64');
  1456. u64bit: exit('qword');
  1457. s32bit: exit('longint');
  1458. u32bit: exit('longword');
  1459. s16bit: exit('smallint');
  1460. u16bit: exit('word');
  1461. else
  1462. internalerror(2013032606);
  1463. end;
  1464. end
  1465. else
  1466. begin
  1467. case ordtype of
  1468. s64bit,s32bit,s16bit,s8bit: exit('sint');
  1469. u64bit,u32bit,u16bit,u8bit: exit('uint');
  1470. else
  1471. internalerror(2013032607);
  1472. end;
  1473. end;
  1474. internalerror(2013032608);
  1475. end;
  1476. function tinlinenode.handle_val: tnode;
  1477. var
  1478. procname,
  1479. suffix : string[31];
  1480. sourcepara,
  1481. destpara,
  1482. codepara,
  1483. sizepara,
  1484. newparas : tcallparanode;
  1485. orgcode,tc : tnode;
  1486. newstatement : tstatementnode;
  1487. newblock : tblocknode;
  1488. tempcode : ttempcreatenode;
  1489. valsinttype : tdef;
  1490. begin
  1491. { for easy exiting if something goes wrong }
  1492. result := cerrornode.create;
  1493. { check the amount of parameters }
  1494. if not(assigned(left)) or
  1495. not(assigned(tcallparanode(left).right)) then
  1496. begin
  1497. CGMessage1(parser_e_wrong_parameter_size,'Val');
  1498. exit;
  1499. end;
  1500. suffix:='';
  1501. { in case we are in a generic definition, we cannot
  1502. do all checks, the parameters might be type parameters }
  1503. if df_generic in current_procinfo.procdef.defoptions then
  1504. begin
  1505. result.Free;
  1506. result:=nil;
  1507. resultdef:=voidtype;
  1508. exit;
  1509. end;
  1510. { retrieve the ValSInt type }
  1511. valsinttype:=search_system_type('VALSINT').typedef;
  1512. { reverse parameters for easier processing }
  1513. reverseparameters(tcallparanode(left));
  1514. { get the parameters }
  1515. tempcode := nil;
  1516. orgcode := nil;
  1517. sizepara := nil;
  1518. sourcepara := tcallparanode(left);
  1519. destpara := tcallparanode(sourcepara.right);
  1520. codepara := tcallparanode(destpara.right);
  1521. { check if codepara is valid }
  1522. if assigned(codepara) and
  1523. (
  1524. not is_integer(codepara.resultdef)
  1525. {$ifndef cpu64bitaddr}
  1526. or is_64bitint(codepara.resultdef)
  1527. {$endif not cpu64bitaddr}
  1528. ) then
  1529. begin
  1530. CGMessagePos1(codepara.fileinfo,type_e_integer_expr_expected,codepara.resultdef.typename);
  1531. exit;
  1532. end;
  1533. { check if dest para is valid }
  1534. if not is_integer(destpara.resultdef) and
  1535. not is_currency(destpara.resultdef) and
  1536. not(destpara.resultdef.typ in [floatdef,enumdef]) then
  1537. begin
  1538. CGMessagePos(destpara.fileinfo,type_e_integer_or_real_expr_expected);
  1539. exit;
  1540. end;
  1541. { we're going to reuse the exisiting para's, so make sure they }
  1542. { won't be disposed }
  1543. left := nil;
  1544. { create the blocknode which will hold the generated statements + }
  1545. { an initial dummy statement }
  1546. newblock:=internalstatements(newstatement);
  1547. { do we need a temp for code? Yes, if no code specified, or if }
  1548. { code is not a valsinttype sized parameter (we already checked }
  1549. { whether the code para, if specified, was an orddef) }
  1550. if not assigned(codepara) or
  1551. (codepara.resultdef.size<>valsinttype.size) then
  1552. begin
  1553. tempcode := ctempcreatenode.create(valsinttype,valsinttype.size,tt_persistent,false);
  1554. addstatement(newstatement,tempcode);
  1555. { set the resultdef of the temp (needed to be able to get }
  1556. { the resultdef of the tempref used in the new code para) }
  1557. typecheckpass(tnode(tempcode));
  1558. { create a temp codepara, but save the original code para to }
  1559. { assign the result to later on }
  1560. if assigned(codepara) then
  1561. begin
  1562. orgcode := codepara.left;
  1563. codepara.left := ctemprefnode.create(tempcode);
  1564. end
  1565. else
  1566. codepara := ccallparanode.create(ctemprefnode.create(tempcode),nil);
  1567. { we need its resultdef later on }
  1568. codepara.get_paratype;
  1569. end
  1570. else if (torddef(codepara.resultdef).ordtype <> torddef(valsinttype).ordtype) then
  1571. { because code is a var parameter, it must match types exactly }
  1572. { however, since it will return values >= 0, both signed and }
  1573. { and unsigned ints of the same size are fine. Since the formal }
  1574. { code para type is sinttype, insert a typecoversion to sint for }
  1575. { unsigned para's }
  1576. begin
  1577. codepara.left := ctypeconvnode.create_internal(codepara.left,valsinttype);
  1578. { make it explicit, oterwise you may get a nonsense range }
  1579. { check error if the cardinal already contained a value }
  1580. { > $7fffffff }
  1581. codepara.get_paratype;
  1582. end;
  1583. { create the procedure name }
  1584. procname := 'fpc_val_';
  1585. case destpara.resultdef.typ of
  1586. orddef:
  1587. begin
  1588. case torddef(destpara.resultdef).ordtype of
  1589. s8bit,s16bit,s32bit,s64bit,
  1590. u8bit,u16bit,u32bit,u64bit:
  1591. begin
  1592. suffix := get_val_int_func(destpara.resultdef) + '_';
  1593. { we also need a destsize para in the case of sint or uint }
  1594. if (suffix = 'sint_') or (suffix = 'uint_') then
  1595. sizepara := ccallparanode.create(cordconstnode.create
  1596. (destpara.resultdef.size,s32inttype,true),nil);
  1597. end;
  1598. scurrency: suffix := 'currency_';
  1599. else
  1600. internalerror(200304225);
  1601. end;
  1602. end;
  1603. floatdef:
  1604. suffix:='real_';
  1605. enumdef:
  1606. begin
  1607. suffix:='enum_';
  1608. sizepara:=Ccallparanode.create(Caddrnode.create_internal(
  1609. Crttinode.create(Tenumdef(destpara.resultdef),fullrtti,rdt_str2ord)
  1610. ),nil);
  1611. end;
  1612. else
  1613. internalerror(2019050515);
  1614. end;
  1615. procname := procname + suffix;
  1616. { play a trick to have tcallnode handle invalid source parameters: }
  1617. { the shortstring-longint val routine by default }
  1618. if (sourcepara.resultdef.typ = stringdef) then
  1619. procname := procname + tstringdef(sourcepara.resultdef).stringtypname
  1620. { zero-based arrays (of char) can be implicitely converted to ansistring, but don't do
  1621. so if not needed because the array is too short }
  1622. else if is_zero_based_array(sourcepara.resultdef) and (sourcepara.resultdef.size>255) then
  1623. procname := procname + 'ansistr'
  1624. else
  1625. procname := procname + 'shortstr';
  1626. { set up the correct parameters for the call: the code para... }
  1627. newparas := codepara;
  1628. { and the source para }
  1629. codepara.right := sourcepara;
  1630. { sizepara either contains nil if none is needed (which is ok, since }
  1631. { then the next statement severes any possible links with other paras }
  1632. { that sourcepara may have) or it contains the necessary size para and }
  1633. { its right field is nil }
  1634. sourcepara.right := sizepara;
  1635. { create the call and assign the result to dest (val helpers are functions).
  1636. Use a trick to prevent a type size mismatch warning to be generated by the
  1637. assignment node. First convert implicitly to the resultdef. This will insert
  1638. the range check. The Second conversion is done explicitly to hide the implicit conversion
  1639. for the assignment node and therefor preventing the warning (PFV)
  1640. The implicit conversion is avoided for enums because implicit conversion between
  1641. longint (which is what fpc_val_enum_shortstr returns) and enumerations is not
  1642. possible. (DM).
  1643. The implicit conversion is also avoided for COMP type if it is handled by FPU (x86)
  1644. to prevent warning about automatic type conversion. }
  1645. if (destpara.resultdef.typ=enumdef) or
  1646. ((destpara.resultdef.typ=floatdef) and (tfloatdef(destpara.resultdef).floattype=s64comp))
  1647. then
  1648. tc:=ccallnode.createfromintrinsic(in_val_x,procname,newparas)
  1649. else
  1650. tc:=ctypeconvnode.create(ccallnode.createfromintrinsic(in_val_x,procname,newparas),destpara.left.resultdef);
  1651. addstatement(newstatement,cassignmentnode.create(
  1652. destpara.left,ctypeconvnode.create_internal(tc,destpara.left.resultdef)));
  1653. { dispose of the enclosing paranode of the destination }
  1654. destpara.left := nil;
  1655. destpara.right := nil;
  1656. destpara.free;
  1657. { check if we used a temp for code and whether we have to store }
  1658. { it to the real code parameter }
  1659. if assigned(orgcode) then
  1660. addstatement(newstatement,cassignmentnode.create(
  1661. orgcode,
  1662. ctypeconvnode.create_internal(
  1663. ctemprefnode.create(tempcode),orgcode.resultdef)));
  1664. { release the temp if we allocated one }
  1665. if assigned(tempcode) then
  1666. addstatement(newstatement,ctempdeletenode.create(tempcode));
  1667. { free the errornode }
  1668. result.free;
  1669. { and return it }
  1670. result := newblock;
  1671. end;
  1672. function tinlinenode.handle_setlength: tnode;
  1673. var
  1674. def: tdef;
  1675. destppn,
  1676. paras: tnode;
  1677. newstatement: tstatementnode;
  1678. ppn: tcallparanode;
  1679. counter,
  1680. dims: longint;
  1681. isarray: boolean;
  1682. begin
  1683. { for easy exiting if something goes wrong }
  1684. result:=cerrornode.create;
  1685. resultdef:=voidtype;
  1686. paras:=left;
  1687. dims:=0;
  1688. if assigned(paras) then
  1689. begin
  1690. { check type of lengths }
  1691. ppn:=tcallparanode(paras);
  1692. while assigned(ppn.right) do
  1693. begin
  1694. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1695. inserttypeconv(ppn.left,sinttype);
  1696. inc(dims);
  1697. ppn:=tcallparanode(ppn.right);
  1698. end;
  1699. end
  1700. else
  1701. internalerror(2013112912);
  1702. if dims=0 then
  1703. begin
  1704. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  1705. exit;
  1706. end;
  1707. { last param must be var }
  1708. destppn:=ppn.left;
  1709. valid_for_var(destppn,true);
  1710. set_varstate(destppn,vs_written,[vsf_must_be_valid,vsf_use_hints,vsf_use_hint_for_string_result]);
  1711. { first param must be a string or dynamic array ...}
  1712. isarray:=is_dynamic_array(destppn.resultdef);
  1713. if not((destppn.resultdef.typ=stringdef) or
  1714. isarray) then
  1715. begin
  1716. { possibly generic involved? }
  1717. if df_generic in current_procinfo.procdef.defoptions then
  1718. result:=internalstatements(newstatement)
  1719. else
  1720. CGMessage(type_e_mismatch);
  1721. exit;
  1722. end;
  1723. { only dynamic arrays accept more dimensions }
  1724. if (dims>1) then
  1725. begin
  1726. if (not isarray) then
  1727. CGMessage(type_e_mismatch)
  1728. else
  1729. begin
  1730. { check if the amount of dimensions is valid }
  1731. def:=tarraydef(destppn.resultdef).elementdef;
  1732. counter:=dims;
  1733. while counter > 1 do
  1734. begin
  1735. if not(is_dynamic_array(def)) then
  1736. begin
  1737. CGMessage1(parser_e_wrong_parameter_size,'SetLength');
  1738. break;
  1739. end;
  1740. dec(counter);
  1741. def:=tarraydef(def).elementdef;
  1742. end;
  1743. end;
  1744. end;
  1745. result.free;
  1746. result:=nil;
  1747. end;
  1748. function tinlinenode.handle_copy: tnode;
  1749. procedure do_error(typemismatch:boolean;func:string;fi:tfileposinfo);
  1750. procedure write_dynarray_copy;
  1751. begin
  1752. MessagePos1(fileinfo,sym_e_param_list,'Copy(Dynamic Array;'+sizesinttype.typename+'=`<low>`;'+sizesinttype.typename+'=`<length>`);');
  1753. end;
  1754. begin
  1755. if typemismatch then
  1756. CGMessagePos(fi,type_e_mismatch)
  1757. else
  1758. CGMessagePos1(fi,parser_e_wrong_parameter_size,'Copy');
  1759. if func='' then
  1760. begin
  1761. write_system_parameter_lists('fpc_shortstr_copy');
  1762. write_system_parameter_lists('fpc_char_copy');
  1763. write_system_parameter_lists('fpc_unicodestr_copy');
  1764. if tf_winlikewidestring in target_info.flags then
  1765. write_system_parameter_lists('fpc_widestr_copy');
  1766. write_system_parameter_lists('fpc_ansistr_copy');
  1767. write_dynarray_copy;
  1768. end
  1769. else if func='fpc_dynarray_copy' then
  1770. write_dynarray_copy
  1771. else
  1772. write_system_parameter_lists(func);
  1773. end;
  1774. var
  1775. paras : tnode;
  1776. ppn : tcallparanode;
  1777. paradef : tdef;
  1778. counter : integer;
  1779. minargs,
  1780. maxargs : longint;
  1781. func : string;
  1782. begin
  1783. if not assigned(left) then
  1784. begin
  1785. do_error(false,'',fileinfo);
  1786. exit(cerrornode.create);
  1787. end;
  1788. result:=nil;
  1789. { determine copy function to use based on the first argument,
  1790. also count the number of arguments in this loop }
  1791. counter:=1;
  1792. paras:=left;
  1793. ppn:=tcallparanode(paras);
  1794. while assigned(ppn.right) do
  1795. begin
  1796. inc(counter);
  1797. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1798. ppn:=tcallparanode(ppn.right);
  1799. end;
  1800. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1801. paradef:=ppn.left.resultdef;
  1802. { the string variants all require 2 or 3 args, only the array one allows less }
  1803. minargs:=2;
  1804. maxargs:=3;
  1805. func:='';
  1806. if is_ansistring(paradef) then
  1807. begin
  1808. // set resultdef to argument def
  1809. resultdef:=paradef;
  1810. func:='fpc_ansistr_copy';
  1811. end
  1812. else if (is_chararray(paradef) and (paradef.size>255)) or
  1813. ((cs_refcountedstrings in current_settings.localswitches) and is_pchar(paradef)) then
  1814. begin
  1815. // set resultdef to ansistring type since result will be in ansistring codepage
  1816. resultdef:=getansistringdef;
  1817. func:='fpc_ansistr_copy';
  1818. end
  1819. else if is_widestring(paradef) then
  1820. begin
  1821. resultdef:=cwidestringtype;
  1822. func:='fpc_widestr_copy';
  1823. end
  1824. else if is_unicodestring(paradef) or
  1825. is_widechararray(paradef) or
  1826. is_pwidechar(paradef) then
  1827. begin
  1828. resultdef:=cunicodestringtype;
  1829. func:='fpc_unicodestr_copy';
  1830. end
  1831. else
  1832. if is_char(paradef) then
  1833. begin
  1834. resultdef:=cshortstringtype;
  1835. func:='fpc_char_copy';
  1836. end
  1837. else
  1838. if is_dynamic_array(paradef) then
  1839. begin
  1840. minargs:=1;
  1841. resultdef:=paradef;
  1842. func:='fpc_array_to_dynarray_copy';
  1843. end
  1844. else
  1845. if is_open_array(paradef) then
  1846. begin
  1847. minargs:=1;
  1848. resultdef:=carraydef.create(0,-1,tarraydef(paradef).rangedef);
  1849. tarraydef(resultdef).arrayoptions:=tarraydef(resultdef).arrayoptions+[ado_IsDynamicArray];
  1850. tarraydef(resultdef).elementdef:=tarraydef(paradef).elementdef;
  1851. func:='fpc_array_to_dynarray_copy';
  1852. end
  1853. else if counter in [2..3] then
  1854. begin
  1855. resultdef:=cshortstringtype;
  1856. func:='fpc_shortstr_copy';
  1857. end
  1858. else if counter<=maxargs then
  1859. begin
  1860. do_error(true,'',ppn.left.fileinfo);
  1861. exit(cerrornode.create);
  1862. end;
  1863. if (counter<minargs) or (counter>maxargs) then
  1864. begin
  1865. do_error(false,func,fileinfo);
  1866. exit(cerrornode.create);
  1867. end;
  1868. end;
  1869. {$maxfpuregisters 0}
  1870. function getpi : bestreal;
  1871. begin
  1872. {$ifdef x86}
  1873. { x86 has pi in hardware }
  1874. result:=pi;
  1875. {$else x86}
  1876. {$ifdef cpuextended}
  1877. result:=MathPiExtended.Value;
  1878. {$else cpuextended}
  1879. result:=MathPi.Value;
  1880. {$endif cpuextended}
  1881. {$endif x86}
  1882. end;
  1883. function tinlinenode.simplify(forinline : boolean): tnode;
  1884. function do_lowhigh(def:tdef) : tnode;
  1885. var
  1886. v : tconstexprint;
  1887. enum : tenumsym;
  1888. hp : tnode;
  1889. i : integer;
  1890. begin
  1891. case def.typ of
  1892. orddef:
  1893. begin
  1894. set_varstate(left,vs_read,[]);
  1895. if inlinenumber=in_low_x then
  1896. v:=torddef(def).low
  1897. else
  1898. v:=torddef(def).high;
  1899. hp:=cordconstnode.create(v,def,true);
  1900. typecheckpass(hp);
  1901. do_lowhigh:=hp;
  1902. end;
  1903. enumdef:
  1904. begin
  1905. set_varstate(left,vs_read,[]);
  1906. if inlinenumber=in_high_x then
  1907. v:=tenumdef(def).maxval
  1908. else
  1909. v:=tenumdef(def).minval;
  1910. enum:=nil;
  1911. for i := 0 to tenumdef(def).symtable.SymList.Count - 1 do
  1912. if tenumsym(tenumdef(def).symtable.SymList[i]).value=v then
  1913. begin
  1914. enum:=tenumsym(tenumdef(def).symtable.SymList[i]);
  1915. break;
  1916. end;
  1917. if not assigned(enum) then
  1918. internalerror(309993)
  1919. else
  1920. hp:=genenumnode(enum);
  1921. do_lowhigh:=hp;
  1922. end;
  1923. else
  1924. internalerror(87);
  1925. end;
  1926. end;
  1927. function getconstrealvalue : bestreal;
  1928. begin
  1929. case left.nodetype of
  1930. ordconstn:
  1931. getconstrealvalue:=tordconstnode(left).value;
  1932. realconstn:
  1933. getconstrealvalue:=trealconstnode(left).value_real;
  1934. else
  1935. internalerror(309992);
  1936. end;
  1937. end;
  1938. procedure setconstrealvalue(r : bestreal);
  1939. begin
  1940. result:=crealconstnode.create(r,pbestrealtype^);
  1941. end;
  1942. function handle_ln_const(r : bestreal) : tnode;
  1943. begin
  1944. if r<=0.0 then
  1945. if floating_point_range_check_error then
  1946. begin
  1947. result:=crealconstnode.create(0,pbestrealtype^);
  1948. CGMessage(type_e_wrong_math_argument)
  1949. end
  1950. else
  1951. begin
  1952. if r=0.0 then
  1953. result:=crealconstnode.create(MathNegInf.Value,pbestrealtype^)
  1954. else
  1955. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1956. end
  1957. else
  1958. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1959. end;
  1960. function handle_sqrt_const(r : bestreal) : tnode;
  1961. begin
  1962. if r<0.0 then
  1963. if floating_point_range_check_error then
  1964. begin
  1965. result:=crealconstnode.create(0,pbestrealtype^);
  1966. CGMessage(type_e_wrong_math_argument)
  1967. end
  1968. else
  1969. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1970. else
  1971. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1972. end;
  1973. function handle_const_sar : tnode;
  1974. var
  1975. vl,vl2 : TConstExprInt;
  1976. bits,shift: integer;
  1977. mask : qword;
  1978. def : tdef;
  1979. begin
  1980. result:=nil;
  1981. if (left.nodetype=ordconstn) or ((left.nodetype=callparan) and (tcallparanode(left).left.nodetype=ordconstn)) then
  1982. begin
  1983. if (left.nodetype=callparan) and
  1984. assigned(tcallparanode(left).right) then
  1985. begin
  1986. vl:=tordconstnode(tcallparanode(left).left).value;
  1987. if forinline then
  1988. case resultdef.size of
  1989. 1,2,4:
  1990. vl:=vl and byte($1f);
  1991. 8:
  1992. vl:=vl and byte($3f);
  1993. else
  1994. internalerror(2013122303);
  1995. end;
  1996. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  1997. begin
  1998. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  1999. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  2000. end
  2001. else if vl=0 then
  2002. begin
  2003. result:=tcallparanode(tcallparanode(left).right).left;
  2004. tcallparanode(tcallparanode(left).right).left:=nil;
  2005. exit;
  2006. end
  2007. else
  2008. exit;
  2009. end
  2010. else
  2011. begin
  2012. def:=left.resultdef;
  2013. vl:=1;
  2014. vl2:=tordconstnode(left).value;
  2015. end;
  2016. bits:=def.size*8;
  2017. shift:=vl.svalue and (bits-1);
  2018. case bits of
  2019. 8:
  2020. mask:=$ff;
  2021. 16:
  2022. mask:=$ffff;
  2023. 32:
  2024. mask:=$ffffffff;
  2025. 64:
  2026. mask:=qword($ffffffffffffffff);
  2027. else
  2028. mask:=qword(1 shl bits)-1;
  2029. end;
  2030. {$push}
  2031. {$r-,q-}
  2032. if shift=0 then
  2033. result:=cordconstnode.create(vl2.svalue,def,false)
  2034. else if vl2.svalue<0 then
  2035. result:=cordconstnode.create(((vl2.svalue shr shift) or (mask shl (bits-shift))) and mask,def,false)
  2036. else
  2037. result:=cordconstnode.create((vl2.svalue shr shift) and mask,def,false);
  2038. {$pop}
  2039. end
  2040. else if (left.nodetype=callparan) and assigned(tcallparanode(left).right) and
  2041. (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  2042. begin
  2043. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  2044. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  2045. { sar(0,x) is 0 }
  2046. { sar32(ffffffff,x) is ffffffff, etc. }
  2047. if ((vl2=0) or
  2048. ((resultdef.size=1) and (shortint(vl2.svalue)=-1)) or
  2049. ((resultdef.size=2) and (smallint(vl2.svalue)=-1)) or
  2050. ((resultdef.size=4) and (longint(vl2.svalue)=-1)) or
  2051. ((resultdef.size=8) and (int64(vl2.svalue)=-1))) and
  2052. ((cs_opt_level4 in current_settings.optimizerswitches) or
  2053. not might_have_sideeffects(tcallparanode(left).left)) then
  2054. begin
  2055. if vl2=0 then
  2056. result:=cordconstnode.create(0,resultdef,true)
  2057. else
  2058. result:=cordconstnode.create(-1,resultdef,true);
  2059. end;
  2060. end;
  2061. end;
  2062. function handle_const_rox : tnode;
  2063. var
  2064. vl,vl2 : TConstExprInt;
  2065. bits,shift: integer;
  2066. def : tdef;
  2067. begin
  2068. result:=nil;
  2069. if (left.nodetype=ordconstn) or ((left.nodetype=callparan) and (tcallparanode(left).left.nodetype=ordconstn)) then
  2070. begin
  2071. if (left.nodetype=callparan) and
  2072. assigned(tcallparanode(left).right) then
  2073. begin
  2074. vl:=tordconstnode(tcallparanode(left).left).value;
  2075. if forinline then
  2076. case resultdef.size of
  2077. { unlike shifts, for rotates, when masking out the higher bits
  2078. of the rotate count, we go all the way down to byte, because
  2079. it doesn't matter, it produces the same result, since it's a rotate }
  2080. 1:
  2081. vl:=vl and byte($07);
  2082. 2:
  2083. vl:=vl and byte($0f);
  2084. 4:
  2085. vl:=vl and byte($1f);
  2086. 8:
  2087. vl:=vl and byte($3f);
  2088. else
  2089. internalerror(2013122304);
  2090. end;
  2091. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  2092. begin
  2093. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  2094. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  2095. end
  2096. else if vl=0 then
  2097. begin
  2098. result:=tcallparanode(tcallparanode(left).right).left;
  2099. tcallparanode(tcallparanode(left).right).left:=nil;
  2100. exit;
  2101. end
  2102. else
  2103. exit;
  2104. end
  2105. else
  2106. begin
  2107. def:=left.resultdef;
  2108. vl:=1;
  2109. vl2:=tordconstnode(left).value;
  2110. end;
  2111. bits:=def.size*8;
  2112. shift:=vl.svalue and (bits-1);
  2113. {$push}
  2114. {$r-,q-}
  2115. if shift=0 then
  2116. result:=cordconstnode.create(vl2.svalue,def,false)
  2117. else
  2118. case inlinenumber of
  2119. in_ror_x,in_ror_x_y:
  2120. case def.size of
  2121. 1:
  2122. result:=cordconstnode.create(RorByte(Byte(vl2.svalue),shift),def,false);
  2123. 2:
  2124. result:=cordconstnode.create(RorWord(Word(vl2.svalue),shift),def,false);
  2125. 4:
  2126. result:=cordconstnode.create(RorDWord(DWord(vl2.svalue),shift),def,false);
  2127. 8:
  2128. result:=cordconstnode.create(RorQWord(QWord(vl2.svalue),shift),def,false);
  2129. else
  2130. internalerror(2011061903);
  2131. end;
  2132. in_rol_x,in_rol_x_y:
  2133. case def.size of
  2134. 1:
  2135. result:=cordconstnode.create(RolByte(Byte(vl2.svalue),shift),def,false);
  2136. 2:
  2137. result:=cordconstnode.create(RolWord(Word(vl2.svalue),shift),def,false);
  2138. 4:
  2139. result:=cordconstnode.create(RolDWord(DWord(vl2.svalue),shift),def,false);
  2140. 8:
  2141. result:=cordconstnode.create(RolQWord(QWord(vl2.svalue),shift),def,false);
  2142. else
  2143. internalerror(2011061902);
  2144. end;
  2145. else
  2146. internalerror(2011061901);
  2147. end;
  2148. {$pop}
  2149. end
  2150. else if (left.nodetype=callparan) and assigned(tcallparanode(left).right) and
  2151. (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  2152. begin
  2153. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  2154. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  2155. { rol/ror are unsigned operations, so cut off upper bits }
  2156. case resultdef.size of
  2157. 1:
  2158. vl2:=vl2 and byte($ff);
  2159. 2:
  2160. vl2:=vl2 and word($ffff);
  2161. 4:
  2162. vl2:=vl2 and dword($ffffffff);
  2163. 8:
  2164. vl2:=vl2 and qword($ffffffffffffffff);
  2165. else
  2166. internalerror(2017050101);
  2167. end;
  2168. { rol(0,x) and ror(0,x) are 0 }
  2169. { rol32(ffffffff,x) and ror32(ffffffff,x) are ffffffff, etc. }
  2170. if ((vl2=0) or
  2171. ((resultdef.size=1) and (vl2=$ff)) or
  2172. ((resultdef.size=2) and (vl2=$ffff)) or
  2173. ((resultdef.size=4) and (vl2=$ffffffff)) or
  2174. ((resultdef.size=8) and (vl2.uvalue=qword($ffffffffffffffff)))) and
  2175. ((cs_opt_level4 in current_settings.optimizerswitches) or
  2176. not might_have_sideeffects(tcallparanode(left).left)) then
  2177. result:=cordconstnode.create(vl2,resultdef,true);
  2178. end;
  2179. end;
  2180. function is_minmax_deterministic(var node: tordconstnode; const DoMax: Boolean; out res: Boolean): Boolean;
  2181. begin
  2182. Result := False;
  2183. if not is_integer(resultdef) then
  2184. InternalError(2024031501);
  2185. if (node.value <= torddef(resultdef).low) then
  2186. begin
  2187. res := not DoMax;
  2188. Exit(True);
  2189. end
  2190. else if (node.value >= torddef(resultdef).high) then
  2191. begin
  2192. res := DoMax;
  2193. Exit(True);
  2194. end
  2195. end;
  2196. var
  2197. hp,hp2 : tnode;
  2198. vl,vl2 : TConstExprInt;
  2199. vr : bestreal;
  2200. helperres : Boolean;
  2201. begin { simplify }
  2202. result:=nil;
  2203. { handle intern constant functions in separate case }
  2204. if inf_inlineconst in inlinenodeflags then
  2205. begin
  2206. { no parameters? }
  2207. if not assigned(left) then
  2208. internalerror(200501231)
  2209. else
  2210. begin
  2211. vl:=0;
  2212. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  2213. case left.nodetype of
  2214. realconstn :
  2215. begin
  2216. { Real functions are all handled with internproc below }
  2217. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename)
  2218. end;
  2219. ordconstn :
  2220. vl:=tordconstnode(left).value;
  2221. callparan :
  2222. begin
  2223. { both exists, else it was not generated }
  2224. vl:=tordconstnode(tcallparanode(left).left).value;
  2225. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  2226. end;
  2227. else
  2228. CGMessage(parser_e_illegal_expression);
  2229. end;
  2230. case inlinenumber of
  2231. in_const_abs :
  2232. if vl.signed then
  2233. hp:=create_simplified_ord_const(abs(vl.svalue),resultdef,forinline,false)
  2234. else
  2235. hp:=create_simplified_ord_const(vl.uvalue,resultdef,forinline,false);
  2236. in_const_sqr:
  2237. if vl.signed then
  2238. hp:=create_simplified_ord_const(sqr(vl.svalue),resultdef,forinline,false)
  2239. else
  2240. hp:=create_simplified_ord_const(sqr(vl.uvalue),resultdef,forinline,false);
  2241. in_const_odd :
  2242. hp:=cordconstnode.create(qword(odd(int64(vl))),pasbool1type,true);
  2243. in_const_swap_word :
  2244. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resultdef,true);
  2245. in_const_swap_long :
  2246. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resultdef,true);
  2247. in_const_swap_qword :
  2248. hp:=cordconstnode.create((vl and $ffffffff) shl 32+(vl shr 32),left.resultdef,true);
  2249. in_const_ptr:
  2250. begin
  2251. {Don't construct pointers from negative values.}
  2252. if (vl.signed and (vl.svalue<0)) or (vl2.signed and (vl2.svalue<0)) then
  2253. cgmessage(parser_e_range_check_error);
  2254. {$if defined(i8086)}
  2255. hp:=cpointerconstnode.create((vl2.uvalue shl 16)+vl.uvalue,voidfarpointertype);
  2256. {$elseif defined(i386)}
  2257. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidnearfspointertype);
  2258. {$else}
  2259. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidpointertype);
  2260. {$endif}
  2261. end;
  2262. in_const_eh_return_data_regno:
  2263. begin
  2264. vl:=eh_return_data_regno(vl.svalue);
  2265. if vl=-1 then
  2266. CGMessagePos(left.fileinfo,type_e_range_check_error_bounds);
  2267. hp:=genintconstnode(vl);
  2268. end;
  2269. else
  2270. internalerror(88);
  2271. end;
  2272. end;
  2273. if hp=nil then
  2274. hp:=cerrornode.create;
  2275. result:=hp;
  2276. end
  2277. else
  2278. begin
  2279. case inlinenumber of
  2280. in_lo_long,
  2281. in_hi_long,
  2282. in_lo_qword,
  2283. in_hi_qword,
  2284. in_lo_word,
  2285. in_hi_word :
  2286. begin
  2287. if left.nodetype=ordconstn then
  2288. begin
  2289. case inlinenumber of
  2290. in_lo_word :
  2291. result:=cordconstnode.create(tordconstnode(left).value and $ff,u8inttype,true);
  2292. in_hi_word :
  2293. result:=cordconstnode.create(tordconstnode(left).value shr 8,u8inttype,true);
  2294. in_lo_long :
  2295. result:=cordconstnode.create(tordconstnode(left).value and $ffff,u16inttype,true);
  2296. in_hi_long :
  2297. result:=cordconstnode.create(tordconstnode(left).value shr 16,u16inttype,true);
  2298. in_lo_qword :
  2299. result:=cordconstnode.create(tordconstnode(left).value and $ffffffff,u32inttype,true);
  2300. in_hi_qword :
  2301. result:=cordconstnode.create(tordconstnode(left).value shr 32,u32inttype,true);
  2302. else
  2303. internalerror(2019050514);
  2304. end;
  2305. end;
  2306. end;
  2307. in_ord_x:
  2308. begin
  2309. case left.resultdef.typ of
  2310. orddef :
  2311. begin
  2312. case torddef(left.resultdef).ordtype of
  2313. pasbool1,
  2314. pasbool8,
  2315. uchar:
  2316. begin
  2317. { change to byte() }
  2318. result:=ctypeconvnode.create_internal(left,u8inttype);
  2319. left:=nil;
  2320. end;
  2321. pasbool16,
  2322. uwidechar :
  2323. begin
  2324. { change to word() }
  2325. result:=ctypeconvnode.create_internal(left,u16inttype);
  2326. left:=nil;
  2327. end;
  2328. pasbool32 :
  2329. begin
  2330. { change to dword() }
  2331. result:=ctypeconvnode.create_internal(left,u32inttype);
  2332. left:=nil;
  2333. end;
  2334. pasbool64 :
  2335. begin
  2336. { change to qword() }
  2337. result:=ctypeconvnode.create_internal(left,u64inttype);
  2338. left:=nil;
  2339. end;
  2340. bool8bit:
  2341. begin
  2342. { change to shortint() }
  2343. result:=ctypeconvnode.create_internal(left,s8inttype);
  2344. left:=nil;
  2345. end;
  2346. bool16bit :
  2347. begin
  2348. { change to smallint() }
  2349. result:=ctypeconvnode.create_internal(left,s16inttype);
  2350. left:=nil;
  2351. end;
  2352. bool32bit :
  2353. begin
  2354. { change to longint() }
  2355. result:=ctypeconvnode.create_internal(left,s32inttype);
  2356. left:=nil;
  2357. end;
  2358. bool64bit :
  2359. begin
  2360. { change to int64() }
  2361. result:=ctypeconvnode.create_internal(left,s64inttype);
  2362. left:=nil;
  2363. end;
  2364. uvoid :
  2365. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2366. else
  2367. begin
  2368. { all other orddef need no transformation }
  2369. result:=left;
  2370. left:=nil;
  2371. end;
  2372. end;
  2373. end;
  2374. enumdef :
  2375. begin
  2376. result:=ctypeconvnode.create_internal(left,s32inttype);
  2377. left:=nil;
  2378. end;
  2379. undefineddef :
  2380. begin
  2381. { we just create a constant 0 here, that's marked as a
  2382. parameter }
  2383. result:=cordconstnode.create(0,s32inttype,false);
  2384. include(result.flags,nf_generic_para);
  2385. left:=nil;
  2386. end;
  2387. pointerdef :
  2388. begin
  2389. if m_mac in current_settings.modeswitches then
  2390. begin
  2391. result:=ctypeconvnode.create_internal(left,ptruinttype);
  2392. left:=nil;
  2393. end
  2394. end;
  2395. else
  2396. internalerror(2019050513);
  2397. end;
  2398. (*
  2399. if (left.nodetype=ordconstn) then
  2400. begin
  2401. result:=cordconstnode.create(
  2402. tordconstnode(left).value,sinttype,true);
  2403. end
  2404. else if (m_mac in current_settings.modeswitches) and
  2405. (left.ndoetype=pointerconstn) then
  2406. result:=cordconstnode.create(
  2407. tpointerconstnode(left).value,ptruinttype,true);
  2408. *)
  2409. end;
  2410. in_chr_byte:
  2411. begin
  2412. { convert to explicit char() }
  2413. result:=ctypeconvnode.create_internal(left,cansichartype);
  2414. left:=nil;
  2415. end;
  2416. in_length_x:
  2417. begin
  2418. case left.resultdef.typ of
  2419. stringdef :
  2420. begin
  2421. if (left.nodetype=stringconstn) then
  2422. begin
  2423. result:=cordconstnode.create(
  2424. tstringconstnode(left).len,sinttype,true);
  2425. end;
  2426. end;
  2427. orddef :
  2428. begin
  2429. { length of char is always one }
  2430. if is_char(left.resultdef) or
  2431. is_widechar(left.resultdef) then
  2432. begin
  2433. result:=cordconstnode.create(1,sinttype,false);
  2434. end
  2435. end;
  2436. arraydef :
  2437. begin
  2438. if (left.nodetype=stringconstn) then
  2439. begin
  2440. result:=cordconstnode.create(
  2441. tstringconstnode(left).len,sinttype,true);
  2442. end
  2443. else if not is_open_array(left.resultdef) and
  2444. not is_array_of_const(left.resultdef) and
  2445. not is_dynamic_array(left.resultdef) then
  2446. result:=genintconstnode(tarraydef(left.resultdef).highrange-
  2447. tarraydef(left.resultdef).lowrange+1,sizesinttype);
  2448. end;
  2449. else
  2450. ;
  2451. end;
  2452. end;
  2453. in_assigned_x:
  2454. begin
  2455. if is_constnode(tcallparanode(left).left) or
  2456. (tcallparanode(left).left.nodetype = pointerconstn) then
  2457. begin
  2458. { let an add node figure it out }
  2459. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  2460. tcallparanode(left).left := nil;
  2461. end;
  2462. end;
  2463. in_pred_x,
  2464. in_succ_x:
  2465. begin
  2466. case left.nodetype of
  2467. ordconstn:
  2468. begin
  2469. if inlinenumber=in_succ_x then
  2470. vl:=tordconstnode(left).value+1
  2471. else
  2472. vl:=tordconstnode(left).value-1;
  2473. if is_integer(left.resultdef) then
  2474. { the type of the original integer constant is irrelevant,
  2475. it should be automatically adapted to the new value
  2476. (except when inlining) }
  2477. result:=create_simplified_ord_const(vl,resultdef,forinline,cs_check_range in localswitches)
  2478. else
  2479. { check the range for enums, chars, booleans }
  2480. result:=cordconstnode.create(vl,left.resultdef,not(nf_internal in flags));
  2481. result.flags:=result.flags+(flags*[nf_internal]);
  2482. end;
  2483. addn,
  2484. subn:
  2485. begin
  2486. { fold succ/pred in child add/sub nodes with a constant if possible:
  2487. - no overflow/range checking
  2488. - equal types
  2489. }
  2490. if ([cs_check_overflow,cs_check_range]*current_settings.localswitches)=[] then
  2491. begin
  2492. if inlinenumber=in_succ_x then
  2493. vl:=1
  2494. else
  2495. vl:=-1;
  2496. if (taddnode(left).left.nodetype=ordconstn) and equal_defs(resultdef,taddnode(left).left.resultdef) then
  2497. begin
  2498. tordconstnode(taddnode(left).left).value:=tordconstnode(taddnode(left).left).value+vl;
  2499. result:=left;
  2500. left:=nil;
  2501. end
  2502. else if (taddnode(left).right.nodetype=ordconstn) and equal_defs(resultdef,taddnode(left).right.resultdef) then
  2503. begin
  2504. if left.nodetype=subn then
  2505. tordconstnode(taddnode(left).right).value:=tordconstnode(taddnode(left).right).value-vl
  2506. else
  2507. tordconstnode(taddnode(left).right).value:=tordconstnode(taddnode(left).right).value+vl;
  2508. result:=left;
  2509. left:=nil;
  2510. end;
  2511. end;
  2512. end;
  2513. else
  2514. ;
  2515. end;
  2516. end;
  2517. in_low_x,
  2518. in_high_x:
  2519. begin
  2520. case left.resultdef.typ of
  2521. orddef,
  2522. enumdef:
  2523. begin
  2524. result:=do_lowhigh(left.resultdef);
  2525. end;
  2526. setdef:
  2527. begin
  2528. result:=do_lowhigh(tsetdef(left.resultdef).elementdef);
  2529. end;
  2530. arraydef:
  2531. begin
  2532. if (inlinenumber=in_low_x) then
  2533. begin
  2534. result:=cordconstnode.create(int64(tarraydef(
  2535. left.resultdef).lowrange),tarraydef(left.resultdef).rangedef,true);
  2536. end
  2537. else if not is_open_array(left.resultdef) and
  2538. not is_array_of_const(left.resultdef) and
  2539. not is_dynamic_array(left.resultdef) then
  2540. result:=cordconstnode.create(int64(tarraydef(left.resultdef).highrange),
  2541. tarraydef(left.resultdef).rangedef,true);
  2542. end;
  2543. stringdef:
  2544. begin
  2545. if inlinenumber=in_low_x then
  2546. begin
  2547. if is_dynamicstring(left.resultdef) and
  2548. not(cs_zerobasedstrings in current_settings.localswitches) then
  2549. result:=cordconstnode.create(1,u8inttype,false)
  2550. else
  2551. result:=cordconstnode.create(0,u8inttype,false);
  2552. end
  2553. else if not is_dynamicstring(left.resultdef) then
  2554. result:=cordconstnode.create(tstringdef(left.resultdef).len,u8inttype,true)
  2555. end;
  2556. undefineddef:
  2557. begin
  2558. result:=cordconstnode.create(0,u8inttype,false);
  2559. end;
  2560. errordef:
  2561. ;
  2562. else
  2563. internalerror(2019050512);
  2564. end;
  2565. end;
  2566. in_exp_real :
  2567. begin
  2568. if left.nodetype in [ordconstn,realconstn] then
  2569. begin
  2570. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  2571. if (trealconstnode(result).value_real=MathInf.Value) and
  2572. floating_point_range_check_error then
  2573. begin
  2574. result:=crealconstnode.create(0,pbestrealtype^);
  2575. CGMessage(parser_e_range_check_error);
  2576. end;
  2577. end
  2578. end;
  2579. in_trunc_real :
  2580. begin
  2581. if left.nodetype in [ordconstn,realconstn] then
  2582. begin
  2583. vr:=getconstrealvalue;
  2584. if (vr>=9223372036854775807.99) or (vr<=-9223372036854775808.0) then
  2585. begin
  2586. message3(type_e_range_check_error_bounds,realtostr(vr),'-9223372036854775808.0','9223372036854775807.99..');
  2587. result:=cordconstnode.create(1,s64inttype,false)
  2588. end
  2589. else
  2590. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  2591. end
  2592. end;
  2593. in_round_real :
  2594. begin
  2595. { can't evaluate while inlining, may depend on fpu setting }
  2596. if (not forinline) and
  2597. (left.nodetype in [ordconstn,realconstn]) then
  2598. begin
  2599. vr:=getconstrealvalue;
  2600. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  2601. begin
  2602. message3(type_e_range_check_error_bounds,realtostr(vr),'-9223372036854775808.49..','9223372036854775807.49..');
  2603. result:=cordconstnode.create(1,s64inttype,false)
  2604. end
  2605. else
  2606. result:=cordconstnode.create(round(vr),s64inttype,true)
  2607. end
  2608. end;
  2609. in_frac_real :
  2610. begin
  2611. if left.nodetype in [ordconstn,realconstn] then
  2612. setconstrealvalue(frac(getconstrealvalue))
  2613. end;
  2614. in_int_real :
  2615. begin
  2616. if left.nodetype in [ordconstn,realconstn] then
  2617. setconstrealvalue(int(getconstrealvalue));
  2618. end;
  2619. in_pi_real :
  2620. begin
  2621. if block_type=bt_const then
  2622. setconstrealvalue(getpi)
  2623. end;
  2624. in_cos_real :
  2625. begin
  2626. if left.nodetype in [ordconstn,realconstn] then
  2627. setconstrealvalue(cos(getconstrealvalue))
  2628. end;
  2629. in_sin_real :
  2630. begin
  2631. if left.nodetype in [ordconstn,realconstn] then
  2632. setconstrealvalue(sin(getconstrealvalue))
  2633. end;
  2634. in_arctan_real :
  2635. begin
  2636. if left.nodetype in [ordconstn,realconstn] then
  2637. setconstrealvalue(arctan(getconstrealvalue))
  2638. end;
  2639. in_abs_real :
  2640. begin
  2641. if left.nodetype in [ordconstn,realconstn] then
  2642. setconstrealvalue(abs(getconstrealvalue))
  2643. end;
  2644. in_abs_long:
  2645. begin
  2646. if left.nodetype=ordconstn then
  2647. begin
  2648. if tordconstnode(left).value<0 then
  2649. result:=cordconstnode.create((-tordconstnode(left).value),resultdef,false)
  2650. else
  2651. result:=cordconstnode.create((tordconstnode(left).value),resultdef,false);
  2652. end
  2653. end;
  2654. in_sqr_real :
  2655. begin
  2656. if left.nodetype in [ordconstn,realconstn] then
  2657. setconstrealvalue(sqr(getconstrealvalue))
  2658. end;
  2659. in_sqrt_real :
  2660. begin
  2661. if left.nodetype in [ordconstn,realconstn] then
  2662. result:=handle_sqrt_const(getconstrealvalue);
  2663. end;
  2664. in_ln_real :
  2665. begin
  2666. if left.nodetype in [ordconstn,realconstn] then
  2667. result:=handle_ln_const(getconstrealvalue);
  2668. end;
  2669. in_assert_x_y :
  2670. begin
  2671. if not(cs_do_assertion in current_settings.localswitches) then
  2672. { we need a valid node, so insert a nothingn }
  2673. result:=cnothingnode.create;
  2674. end;
  2675. in_sar_x,
  2676. in_sar_x_y :
  2677. begin
  2678. result:=handle_const_sar;
  2679. end;
  2680. in_rol_x,
  2681. in_rol_x_y,
  2682. in_ror_x,
  2683. in_ror_x_y :
  2684. result:=handle_const_rox;
  2685. in_bsf_x:
  2686. begin
  2687. if left.nodetype=ordconstn then
  2688. begin
  2689. case left.resultdef.size of
  2690. 1:
  2691. result:=cordconstnode.create(BsfByte(Byte(tordconstnode(left).value.uvalue)),resultdef,false);
  2692. 2:
  2693. result:=cordconstnode.create(BsfWord(Word(tordconstnode(left).value.uvalue)),resultdef,false);
  2694. 4:
  2695. result:=cordconstnode.create(BsfDWord(DWord(tordconstnode(left).value.uvalue)),resultdef,false);
  2696. 8:
  2697. result:=cordconstnode.create(BsfQWord(QWord(tordconstnode(left).value.uvalue)),resultdef,false);
  2698. else
  2699. internalerror(2017042401);
  2700. end;
  2701. end;
  2702. end;
  2703. in_bsr_x :
  2704. begin
  2705. if left.nodetype=ordconstn then
  2706. begin
  2707. case left.resultdef.size of
  2708. 1:
  2709. result:=cordconstnode.create(BsrByte(Byte(tordconstnode(left).value.uvalue)),resultdef,false);
  2710. 2:
  2711. result:=cordconstnode.create(BsrWord(Word(tordconstnode(left).value.uvalue)),resultdef,false);
  2712. 4:
  2713. result:=cordconstnode.create(BsrDWord(DWord(tordconstnode(left).value.uvalue)),resultdef,false);
  2714. 8:
  2715. result:=cordconstnode.create(BsrQWord(QWord(tordconstnode(left).value.uvalue)),resultdef,false);
  2716. else
  2717. internalerror(2017042402);
  2718. end;
  2719. end;
  2720. end;
  2721. in_popcnt_x :
  2722. begin
  2723. if left.nodetype=ordconstn then
  2724. begin
  2725. result:=cordconstnode.create(PopCnt(tordconstnode(left).value),resultdef,false);
  2726. end;
  2727. end;
  2728. in_min_single,
  2729. in_min_double:
  2730. begin
  2731. { Check to see if the result is deterministic }
  2732. if left.nodetype=callparan then
  2733. begin
  2734. hp:=tcallparanode(tcallparanode(left).nextpara).paravalue;
  2735. hp2:=tcallparanode(left).paravalue;
  2736. if (hp.nodetype=realconstn) then
  2737. begin
  2738. if (trealconstnode(hp).value_real = MathQNaN.value) then
  2739. { If one of the inputs is NaN, the second parameter
  2740. is taken }
  2741. result:=hp2.getcopy()
  2742. else if (trealconstnode(hp).value_real = MathNegInf.value) then
  2743. { Nothing is less than than -oo }
  2744. result:=crealconstnode.create(MathNegInf.value,resultdef)
  2745. else if (trealconstnode(hp).value_real = MathInf.value) then
  2746. { Everything is less than +oo }
  2747. result:=hp2.getcopy()
  2748. else if (hp2.nodetype=realconstn) then
  2749. begin
  2750. { Both actual parameters are constants, so take
  2751. the smaller of the two right now }
  2752. if (trealconstnode(hp).value_real < trealconstnode(hp2).value_real) then
  2753. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef)
  2754. else
  2755. result:=crealconstnode.create(trealconstnode(hp2).value_real,resultdef);
  2756. end;
  2757. end
  2758. else if (hp2.nodetype=realconstn) then
  2759. begin
  2760. if (trealconstnode(hp2).value_real = MathQNaN.value) then
  2761. { If one of the inputs is NaN, the second parameter
  2762. is taken (even if it is NaN) }
  2763. result:=crealconstnode.create(MathQNaN.value,resultdef)
  2764. else if (trealconstnode(hp2).value_real = MathNegInf.value) then
  2765. { Nothing is less than than -oo }
  2766. result:=crealconstnode.create(MathNegInf.value,resultdef)
  2767. else if (trealconstnode(hp2).value_real = MathInf.value) then
  2768. { Everything is less than +oo }
  2769. result:=hp.getcopy();
  2770. end;
  2771. end;
  2772. end;
  2773. in_max_single,
  2774. in_max_double:
  2775. begin
  2776. { Check to see if the result is deterministic }
  2777. if left.nodetype=callparan then
  2778. begin
  2779. hp:=tcallparanode(tcallparanode(left).nextpara).paravalue;
  2780. hp2:=tcallparanode(left).paravalue;
  2781. if (hp.nodetype=realconstn) then
  2782. begin
  2783. if (trealconstnode(hp).value_real = MathQNaN.value) then
  2784. { If one of the inputs is NaN, the second parameter
  2785. is taken }
  2786. result:=hp2.getcopy()
  2787. else if (trealconstnode(hp).value_real = MathNegInf.value) then
  2788. { Everything is greater than than -oo }
  2789. result:=hp2.getcopy()
  2790. else if (trealconstnode(hp).value_real = MathInf.value) then
  2791. { Nothing is greater than +oo }
  2792. result:=crealconstnode.create(MathInf.value,resultdef)
  2793. else if (hp2.nodetype=realconstn) then
  2794. begin
  2795. { Both actual parameters are constants, so take
  2796. the larger of the two right now }
  2797. if (trealconstnode(hp).value_real > trealconstnode(hp2).value_real) then
  2798. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef)
  2799. else
  2800. result:=crealconstnode.create(trealconstnode(hp2).value_real,resultdef)
  2801. end;
  2802. end
  2803. else if (hp2.nodetype=realconstn) then
  2804. begin
  2805. if (trealconstnode(hp2).value_real = MathQNaN.value) then
  2806. { If one of the inputs is NaN, the second parameter
  2807. is taken (even if it is NaN) }
  2808. result:=crealconstnode.create(MathQNaN.value,resultdef)
  2809. else if (trealconstnode(hp2).value_real = MathNegInf.value) then
  2810. { Everything is greater than than -oo }
  2811. result:=hp.getcopy()
  2812. else if (trealconstnode(hp2).value_real = MathInf.value) then
  2813. { Nothing is greater than +oo }
  2814. result:=crealconstnode.create(MathInf.value,resultdef);
  2815. end;
  2816. end;
  2817. end;
  2818. in_min_longint,
  2819. in_min_int64:
  2820. begin
  2821. if left.nodetype=callparan then
  2822. begin
  2823. { Check to see if the result is deterministic }
  2824. hp:=tcallparanode(tcallparanode(left).nextpara).paravalue;
  2825. hp2:=tcallparanode(left).paravalue;
  2826. if (hp.nodetype=ordconstn) then
  2827. begin
  2828. if (hp2.nodetype=ordconstn) then
  2829. begin
  2830. { Both actual parameters are constants, so take
  2831. the smaller of the two right now }
  2832. if inlinenumber=in_min_longint then
  2833. result:=cordconstnode.create(min(LongInt(tordconstnode(hp).value.svalue),LongInt(tordconstnode(hp2).value.svalue)),resultdef,false)
  2834. else
  2835. result:=cordconstnode.create(min(tordconstnode(hp).value,tordconstnode(hp2).value),resultdef,false);
  2836. end;
  2837. if is_minmax_deterministic(tordconstnode(hp), False, helperres) then
  2838. begin
  2839. if helperres then
  2840. result:=cordconstnode.create(tordconstnode(hp).value,resultdef,false)
  2841. else
  2842. result:=hp2.getcopy();
  2843. end;
  2844. end
  2845. else if (hp2.nodetype=ordconstn) then
  2846. begin
  2847. if is_minmax_deterministic(tordconstnode(hp2), False, helperres) then
  2848. begin
  2849. if helperres then
  2850. result:=cordconstnode.create(tordconstnode(hp2).value,resultdef,false)
  2851. else
  2852. result:=hp.getcopy();
  2853. end;
  2854. end;
  2855. end;
  2856. end;
  2857. in_max_longint,
  2858. in_max_int64:
  2859. begin
  2860. if left.nodetype=callparan then
  2861. begin
  2862. { Check to see if the result is deterministic }
  2863. hp:=tcallparanode(left).paravalue;
  2864. hp2:=tcallparanode(tcallparanode(left).nextpara).paravalue;
  2865. if (hp.nodetype=ordconstn) then
  2866. begin
  2867. if (hp2.nodetype=ordconstn) then
  2868. begin
  2869. { Both actual parameters are constants, so take
  2870. the larger of the two right now }
  2871. if inlinenumber=in_max_longint then
  2872. result:=cordconstnode.create(max(LongInt(tordconstnode(hp).value.svalue),LongInt(tordconstnode(hp2).value.svalue)),resultdef,false)
  2873. else
  2874. result:=cordconstnode.create(max(tordconstnode(hp).value,tordconstnode(hp2).value),resultdef,false);
  2875. end;
  2876. if is_minmax_deterministic(tordconstnode(hp), True, helperres) then
  2877. begin
  2878. if helperres then
  2879. result:=cordconstnode.create(tordconstnode(hp).value,resultdef,false)
  2880. else
  2881. result:=hp2.getcopy();
  2882. end;
  2883. end
  2884. else if (hp2.nodetype=ordconstn) then
  2885. begin
  2886. if is_minmax_deterministic(tordconstnode(hp2), True, helperres) then
  2887. begin
  2888. if helperres then
  2889. result:=cordconstnode.create(tordconstnode(hp2).value,resultdef,false)
  2890. else
  2891. result:=hp.getcopy();
  2892. end;
  2893. end;
  2894. end;
  2895. end;
  2896. in_min_dword,
  2897. in_min_qword:
  2898. begin
  2899. if left.nodetype=callparan then
  2900. begin
  2901. { Check to see if the result is deterministic }
  2902. hp:=tcallparanode(tcallparanode(left).nextpara).paravalue;
  2903. hp2:=tcallparanode(left).paravalue;
  2904. if (hp.nodetype=ordconstn) then
  2905. begin
  2906. if (hp2.nodetype=ordconstn) then
  2907. begin
  2908. { Both actual parameters are constants, so take
  2909. the smaller of the two right now }
  2910. if inlinenumber=in_min_dword then
  2911. result:=cordconstnode.create(min(DWord(tordconstnode(hp).value.uvalue),DWord(tordconstnode(hp2).value.uvalue)),resultdef,false)
  2912. else
  2913. result:=cordconstnode.create(min(tordconstnode(hp).value,tordconstnode(hp2).value),resultdef,false);
  2914. end;
  2915. if is_minmax_deterministic(tordconstnode(hp), False, helperres) then
  2916. begin
  2917. if helperres then
  2918. result:=cordconstnode.create(tordconstnode(hp).value,resultdef,false)
  2919. else
  2920. result:=hp2.getcopy();
  2921. end;
  2922. end
  2923. else if (hp2.nodetype=ordconstn) then
  2924. begin
  2925. if is_minmax_deterministic(tordconstnode(hp2), False, helperres) then
  2926. begin
  2927. if helperres then
  2928. result:=cordconstnode.create(tordconstnode(hp2).value,resultdef,false)
  2929. else
  2930. result:=hp.getcopy();
  2931. end;
  2932. end;
  2933. end;
  2934. end;
  2935. in_max_dword,
  2936. in_max_qword:
  2937. begin
  2938. if left.nodetype=callparan then
  2939. begin
  2940. { Check to see if the result is deterministic }
  2941. hp:=tcallparanode(left).paravalue;
  2942. hp2:=tcallparanode(tcallparanode(left).nextpara).paravalue;
  2943. if (hp.nodetype=ordconstn) then
  2944. begin
  2945. if (hp2.nodetype=ordconstn) then
  2946. begin
  2947. { Both actual parameters are constants, so take
  2948. the larger of the two right now }
  2949. if inlinenumber=in_max_dword then
  2950. result:=cordconstnode.create(max(DWord(tordconstnode(hp).value.uvalue),DWord(tordconstnode(hp2).value.uvalue)),resultdef,false)
  2951. else
  2952. result:=cordconstnode.create(max(tordconstnode(hp).value,tordconstnode(hp2).value),resultdef,false);
  2953. end;
  2954. if is_minmax_deterministic(tordconstnode(hp), True, helperres) then
  2955. begin
  2956. if helperres then
  2957. result:=cordconstnode.create(tordconstnode(hp).value,resultdef,false)
  2958. else
  2959. result:=hp2.getcopy();
  2960. end;
  2961. end
  2962. else if (hp2.nodetype=ordconstn) then
  2963. begin
  2964. if is_minmax_deterministic(tordconstnode(hp2), True, helperres) then
  2965. begin
  2966. if helperres then
  2967. result:=cordconstnode.create(tordconstnode(hp2).value,resultdef,false)
  2968. else
  2969. result:=hp.getcopy();
  2970. end;
  2971. end;
  2972. end;
  2973. end;
  2974. else
  2975. ;
  2976. end;
  2977. end;
  2978. end;
  2979. function tinlinenode.pass_typecheck:tnode;
  2980. type
  2981. tfloattypeset = set of tfloattype;
  2982. function removefloatupcasts(var p: tnode; const floattypes: tfloattypeset): tdef;
  2983. var
  2984. hnode: tnode;
  2985. begin
  2986. { System unit declares internal functions like this:
  2987. function foo(x: valreal): valreal; [internproc: number];
  2988. Calls to such functions are initially processed by callnode,
  2989. which typechecks the arguments, possibly inserting conversion to valreal.
  2990. To handle smaller types without excess precision, we need to remove
  2991. these extra typecasts. }
  2992. if not(cs_excessprecision in current_settings.localswitches) and
  2993. (p.nodetype=typeconvn) and
  2994. (ttypeconvnode(p).left.resultdef.typ=floatdef) and
  2995. (p.flags*[nf_explicit,nf_internal]=[]) and
  2996. (tfloatdef(ttypeconvnode(p).left.resultdef).floattype in (floattypes*[s32real,s64real,s80real,sc80real,s128real])) then
  2997. begin
  2998. hnode:=ttypeconvnode(p).left;
  2999. ttypeconvnode(p).left:=nil;
  3000. p.free;
  3001. p:=hnode;
  3002. result:=p.resultdef;
  3003. end
  3004. else if (p.nodetype=typeconvn) and
  3005. (p.flags*[nf_explicit,nf_internal]=[]) and
  3006. (ttypeconvnode(p).left.resultdef.typ=floatdef) and
  3007. (tfloatdef(ttypeconvnode(p).left.resultdef).floattype in (floattypes*[s64currency,s64comp])) then
  3008. begin
  3009. hnode:=ttypeconvnode(p).left;
  3010. ttypeconvnode(p).left:=nil;
  3011. p.free;
  3012. p:=hnode;
  3013. if is_currency(p.resultdef) then
  3014. begin
  3015. if (nf_is_currency in p.flags) and
  3016. (p.nodetype=slashn) and
  3017. (taddnode(p).right.nodetype=realconstn) and
  3018. (trealconstnode(taddnode(p).right).value_real=10000.0) and
  3019. not(nf_is_currency in taddnode(p).left.flags) then
  3020. begin
  3021. hnode:=taddnode(p).left;
  3022. taddnode(p).left:=nil;
  3023. p.free;
  3024. p:=hnode;
  3025. end;
  3026. end;
  3027. result:=p.resultdef;
  3028. end
  3029. { in case the system helper was declared with overloads for different types,
  3030. keep those }
  3031. else if (p.resultdef.typ=floatdef) and
  3032. (tfloatdef(p.resultdef).floattype in (floattypes*[s32real,s64real,s80real,sc80real,s128real])) then
  3033. result:=p.resultdef
  3034. else
  3035. begin
  3036. { for variant parameters; the rest has been converted by the
  3037. call node already }
  3038. if not(p.nodetype in [ordconstn,realconstn]) then
  3039. inserttypeconv(P,pbestrealtype^);
  3040. result:=p.resultdef
  3041. end;
  3042. end;
  3043. procedure handle_pack_unpack;
  3044. var
  3045. source, target, index: tcallparanode;
  3046. unpackedarraydef, packedarraydef: tarraydef;
  3047. tempindex: TConstExprInt;
  3048. begin
  3049. resultdef:=voidtype;
  3050. unpackedarraydef := nil;
  3051. packedarraydef := nil;
  3052. source := tcallparanode(left);
  3053. if (inlinenumber = in_unpack_x_y_z) then
  3054. begin
  3055. target := tcallparanode(source.right);
  3056. index := tcallparanode(target.right);
  3057. { source must be a packed array }
  3058. if not is_packed_array(source.left.resultdef) then
  3059. CGMessagePos2(source.left.fileinfo,type_e_got_expected_packed_array,'1',source.left.resultdef.typename)
  3060. else
  3061. packedarraydef := tarraydef(source.left.resultdef);
  3062. { target can be any kind of array, as long as it's not packed }
  3063. if (target.left.resultdef.typ <> arraydef) or
  3064. is_packed_array(target.left.resultdef) then
  3065. CGMessagePos2(target.left.fileinfo,type_e_got_expected_unpacked_array,'2',target.left.resultdef.typename)
  3066. else
  3067. unpackedarraydef := tarraydef(target.left.resultdef);
  3068. end
  3069. else
  3070. begin
  3071. index := tcallparanode(source.right);
  3072. target := tcallparanode(index.right);
  3073. { source can be any kind of array, as long as it's not packed }
  3074. if (source.left.resultdef.typ <> arraydef) or
  3075. is_packed_array(source.left.resultdef) then
  3076. CGMessagePos2(source.left.fileinfo,type_e_got_expected_unpacked_array,'1',source.left.resultdef.typename)
  3077. else
  3078. unpackedarraydef := tarraydef(source.left.resultdef);
  3079. { target must be a packed array }
  3080. if not is_packed_array(target.left.resultdef) then
  3081. CGMessagePos2(target.left.fileinfo,type_e_got_expected_packed_array,'3',target.left.resultdef.typename)
  3082. else
  3083. packedarraydef := tarraydef(target.left.resultdef);
  3084. end;
  3085. if assigned(unpackedarraydef) then
  3086. begin
  3087. { index must be compatible with the unpacked array's indextype }
  3088. inserttypeconv(index.left,unpackedarraydef.rangedef);
  3089. { range check at compile time if possible }
  3090. if assigned(packedarraydef) and
  3091. (index.left.nodetype = ordconstn) and
  3092. not is_special_array(unpackedarraydef) then
  3093. begin
  3094. adaptrange(unpackedarraydef,tordconstnode(index.left).value,false,false,cs_check_range in current_settings.localswitches);
  3095. tempindex := tordconstnode(index.left).value + packedarraydef.highrange-packedarraydef.lowrange;
  3096. adaptrange(unpackedarraydef,tempindex,false,false,cs_check_range in current_settings.localswitches);
  3097. end;
  3098. end;
  3099. { source array is read and must be valid }
  3100. set_varstate(source.left,vs_read,[vsf_must_be_valid]);
  3101. { target array is written }
  3102. valid_for_assignment(target.left,true);
  3103. set_varstate(target.left,vs_written,[]);
  3104. { index in the unpacked array is read and must be valid }
  3105. set_varstate(index.left,vs_read,[vsf_must_be_valid]);
  3106. { if the size of the arrays is 0 (array of empty records), }
  3107. { do nothing }
  3108. if (source.resultdef.size = 0) then
  3109. result:=cnothingnode.create;
  3110. end;
  3111. function handle_objc_encode: tnode;
  3112. var
  3113. encodedtype: ansistring;
  3114. errordef: tdef;
  3115. begin
  3116. encodedtype:='';
  3117. if not objctryencodetype(left.resultdef,encodedtype,errordef) then
  3118. Message1(type_e_objc_type_unsupported,errordef.typename);
  3119. result:=cstringconstnode.createpchar(ansistring2pchar(encodedtype),length(encodedtype),nil);
  3120. end;
  3121. var
  3122. hightree,
  3123. hp : tnode;
  3124. temp_pnode: pnode;
  3125. begin
  3126. result:=nil;
  3127. { when handling writeln "left" contains no valid address }
  3128. if assigned(left) then
  3129. begin
  3130. if left.nodetype=callparan then
  3131. tcallparanode(left).get_paratype
  3132. else
  3133. typecheckpass(left);
  3134. end;
  3135. if not(inf_inlineconst in inlinenodeflags) then
  3136. begin
  3137. case inlinenumber of
  3138. in_lo_long,
  3139. in_hi_long,
  3140. in_lo_qword,
  3141. in_hi_qword,
  3142. in_lo_word,
  3143. in_hi_word :
  3144. begin
  3145. { give warning for incompatibility with tp and delphi }
  3146. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  3147. ((m_tp7 in current_settings.modeswitches) or
  3148. (m_delphi in current_settings.modeswitches)) then
  3149. CGMessage(type_w_maybe_wrong_hi_lo);
  3150. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3151. if not is_integer(left.resultdef) then
  3152. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  3153. case inlinenumber of
  3154. in_lo_word,
  3155. in_hi_word :
  3156. resultdef:=u8inttype;
  3157. in_lo_long,
  3158. in_hi_long :
  3159. resultdef:=u16inttype;
  3160. in_lo_qword,
  3161. in_hi_qword :
  3162. resultdef:=u32inttype;
  3163. else
  3164. ;
  3165. end;
  3166. end;
  3167. in_sizeof_x:
  3168. begin
  3169. { the constant evaluation of in_sizeof_x happens in pexpr where possible,
  3170. though for generics it can reach here as well }
  3171. set_varstate(left,vs_read,[]);
  3172. if (left.resultdef.typ<>undefineddef) and
  3173. assigned(current_procinfo) and
  3174. paramanager.push_high_param(vs_value,left.resultdef,current_procinfo.procdef.proccalloption) then
  3175. begin
  3176. { this should be an open array or array of const, both of
  3177. which can only be simple load nodes of parameters }
  3178. if left.nodetype<>loadn then
  3179. internalerror(2014120701);
  3180. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  3181. if assigned(hightree) then
  3182. begin
  3183. hp:=caddnode.create(addn,hightree,
  3184. cordconstnode.create(1,sizesinttype,false));
  3185. if (left.resultdef.typ=arraydef) then
  3186. if not is_packed_array(tarraydef(left.resultdef)) then
  3187. begin
  3188. if (tarraydef(left.resultdef).elesize<>1) then
  3189. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  3190. left.resultdef).elesize,sizesinttype,true));
  3191. end
  3192. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  3193. begin
  3194. { no packed open array support yet }
  3195. if (hp.nodetype <> ordconstn) then
  3196. internalerror(2006081511);
  3197. hp.free;
  3198. hp := cordconstnode.create(left.resultdef.size,sizesinttype,true);
  3199. {
  3200. hp:=
  3201. ctypeconvnode.create_explicit(sizesinttype,
  3202. cmoddivnode.create(divn,
  3203. caddnode.create(addn,
  3204. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  3205. left.resultdef).elepackedbitsize,s64inttype,true)),
  3206. cordconstnode.create(a,s64inttype,true)),
  3207. cordconstnode.create(8,s64inttype,true)),
  3208. sizesinttype);
  3209. }
  3210. end;
  3211. result:=hp;
  3212. end;
  3213. end
  3214. else
  3215. resultdef:=sizesinttype;
  3216. end;
  3217. in_typeof_x:
  3218. begin
  3219. if target_info.system in systems_managed_vm then
  3220. message(parser_e_feature_unsupported_for_vm);
  3221. typecheckpass(left);
  3222. set_varstate(left,vs_read,[]);
  3223. if (left.resultdef.typ=objectdef) and
  3224. not(oo_has_vmt in tobjectdef(left.resultdef).objectoptions) then
  3225. message(type_e_typeof_requires_vmt);
  3226. resultdef:=voidpointertype;
  3227. end;
  3228. in_ord_x:
  3229. begin
  3230. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3231. case left.resultdef.typ of
  3232. orddef,
  3233. enumdef,
  3234. undefineddef :
  3235. ;
  3236. pointerdef :
  3237. begin
  3238. if not(m_mac in current_settings.modeswitches) then
  3239. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  3240. end
  3241. else
  3242. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  3243. end;
  3244. end;
  3245. in_chr_byte:
  3246. begin
  3247. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3248. end;
  3249. in_length_x:
  3250. begin
  3251. if ((left.resultdef.typ=arraydef) and
  3252. (not is_special_array(left.resultdef) or
  3253. is_open_array(left.resultdef))) or
  3254. (left.resultdef.typ=orddef) then
  3255. set_varstate(left,vs_read,[])
  3256. else
  3257. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3258. case left.resultdef.typ of
  3259. variantdef:
  3260. begin
  3261. inserttypeconv(left,getansistringdef);
  3262. end;
  3263. stringdef :
  3264. begin
  3265. { we don't need string convertions here, }
  3266. { except if from widestring to ansistring }
  3267. { and vice versa (that can change the }
  3268. { length) }
  3269. if (left.nodetype=typeconvn) and
  3270. (ttypeconvnode(left).left.resultdef.typ=stringdef) and
  3271. not(is_wide_or_unicode_string(left.resultdef) xor
  3272. is_wide_or_unicode_string(ttypeconvnode(left).left.resultdef)) then
  3273. begin
  3274. hp:=ttypeconvnode(left).left;
  3275. ttypeconvnode(left).left:=nil;
  3276. left.free;
  3277. left:=hp;
  3278. end;
  3279. end;
  3280. orddef :
  3281. begin
  3282. { will be handled in simplify }
  3283. if not is_char(left.resultdef) and
  3284. not is_widechar(left.resultdef) then
  3285. CGMessage(type_e_mismatch);
  3286. end;
  3287. pointerdef :
  3288. begin
  3289. if is_pchar(left.resultdef) then
  3290. begin
  3291. hp := ccallparanode.create(left,nil);
  3292. result := ccallnode.createintern('fpc_pchar_length',hp);
  3293. { make sure the left node doesn't get disposed, since it's }
  3294. { reused in the new node (JM) }
  3295. left:=nil;
  3296. exit;
  3297. end
  3298. else if is_pwidechar(left.resultdef) then
  3299. begin
  3300. hp := ccallparanode.create(left,nil);
  3301. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  3302. { make sure the left node doesn't get disposed, since it's }
  3303. { reused in the new node (JM) }
  3304. left:=nil;
  3305. exit;
  3306. end
  3307. else
  3308. CGMessage(type_e_mismatch);
  3309. end;
  3310. arraydef :
  3311. begin
  3312. if is_open_array(left.resultdef) or
  3313. is_array_of_const(left.resultdef) then
  3314. begin
  3315. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  3316. if assigned(hightree) then
  3317. result:=caddnode.create(addn,hightree,
  3318. cordconstnode.create(1,sinttype,false));
  3319. exit;
  3320. end
  3321. { Length() for dynamic arrays is inlined }
  3322. else
  3323. begin
  3324. { will be handled in simplify }
  3325. end;
  3326. end;
  3327. undefineddef :
  3328. begin
  3329. if not (df_generic in current_procinfo.procdef.defoptions) then
  3330. CGMessage(type_e_mismatch);
  3331. { otherwise nothing }
  3332. end;
  3333. else
  3334. CGMessage(type_e_mismatch);
  3335. end;
  3336. { shortstring return an 8 bit value as the length
  3337. is the first byte of the string }
  3338. if is_shortstring(left.resultdef) then
  3339. resultdef:=u8inttype
  3340. else
  3341. resultdef:=sizesinttype;
  3342. end;
  3343. in_typeinfo_x:
  3344. begin
  3345. if target_info.system in systems_managed_vm then
  3346. message(parser_e_feature_unsupported_for_vm);
  3347. if (left.resultdef.typ=enumdef) and
  3348. (tenumdef(left.resultdef).has_jumps) and
  3349. (
  3350. (left.nodetype<>typen) or
  3351. not (sp_generic_para in ttypenode(left).typesym.symoptions)
  3352. ) then
  3353. CGMessage(type_e_no_type_info);
  3354. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3355. resultdef:=voidpointertype;
  3356. end;
  3357. in_gettypekind_x:
  3358. begin
  3359. if target_info.system in systems_managed_vm then
  3360. message(parser_e_feature_unsupported_for_vm);
  3361. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3362. resultdef:=typekindtype;
  3363. end;
  3364. in_ismanagedtype_x:
  3365. begin
  3366. if target_info.system in systems_managed_vm then
  3367. message(parser_e_feature_unsupported_for_vm);
  3368. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3369. resultdef:=pasbool1type;
  3370. end;
  3371. in_isconstvalue_x:
  3372. begin
  3373. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3374. resultdef:=pasbool1type;
  3375. end;
  3376. in_assigned_x:
  3377. begin
  3378. { the parser has already made sure the expression is valid }
  3379. { in case of a complex procvar, only check the "code" pointer }
  3380. if (tcallparanode(left).left.resultdef.typ=procvardef) and
  3381. not tprocvardef(tcallparanode(left).left.resultdef).is_addressonly then
  3382. begin
  3383. inserttypeconv_explicit(tcallparanode(left).left,search_system_type('TMETHOD').typedef);
  3384. tcallparanode(left).left:=csubscriptnode.create(tsym(tabstractrecorddef(tcallparanode(left).left.resultdef).symtable.find('CODE')),tcallparanode(left).left);
  3385. tcallparanode(left).get_paratype;
  3386. end;
  3387. { Postpone conversion into addnode until firstpass, so targets
  3388. may override first_assigned and insert specific code. }
  3389. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  3390. resultdef:=pasbool1type;
  3391. end;
  3392. in_ofs_x :
  3393. internalerror(2000101001);
  3394. in_seg_x :
  3395. begin
  3396. result := typecheck_seg;
  3397. end;
  3398. in_pred_x,
  3399. in_succ_x:
  3400. begin
  3401. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3402. resultdef:=left.resultdef;
  3403. if is_ordinal(resultdef) or is_typeparam(resultdef) then
  3404. begin
  3405. if (resultdef.typ=enumdef) and
  3406. (tenumdef(resultdef).has_jumps) and
  3407. not(m_delphi in current_settings.modeswitches) and
  3408. not(nf_internal in flags) then
  3409. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  3410. end
  3411. else
  3412. CGMessage(type_e_ordinal_expr_expected)
  3413. end;
  3414. in_copy_x:
  3415. result:=handle_copy;
  3416. in_initialize_x,
  3417. in_finalize_x:
  3418. begin
  3419. { inlined from pinline }
  3420. internalerror(200204231);
  3421. end;
  3422. in_setlength_x:
  3423. begin
  3424. result:=handle_setlength;
  3425. end;
  3426. in_inc_x,
  3427. in_dec_x:
  3428. begin
  3429. resultdef:=voidtype;
  3430. if not(df_generic in current_procinfo.procdef.defoptions) then
  3431. begin
  3432. if assigned(left) then
  3433. begin
  3434. { first param must be var }
  3435. valid_for_var(tcallparanode(left).left,true);
  3436. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  3437. if (left.resultdef.typ in [enumdef,pointerdef]) or
  3438. is_ordinal(left.resultdef) or
  3439. is_currency(left.resultdef) then
  3440. begin
  3441. { value of left gets changed -> must be unique }
  3442. set_unique(tcallparanode(left).left);
  3443. { two paras ? }
  3444. if assigned(tcallparanode(left).right) then
  3445. begin
  3446. if is_integer(tcallparanode(left).right.resultdef) then
  3447. begin
  3448. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  3449. { when range/overflow checking is on, we
  3450. convert this to a regular add, and for proper
  3451. checking we need the original type }
  3452. if ([cs_check_range,cs_check_overflow]*current_settings.localswitches=[]) then
  3453. if (tcallparanode(left).left.resultdef.typ=pointerdef) then
  3454. begin
  3455. { don't convert values added to pointers into the pointer types themselves,
  3456. because that will turn signed values into unsigned ones, which then
  3457. goes wrong when they have to be multiplied with the size of the elements
  3458. to which the pointer points in ncginl (mantis #17342) }
  3459. if is_signed(tcallparanode(tcallparanode(left).right).left.resultdef) then
  3460. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tpointerdef(tcallparanode(left).left.resultdef).pointer_arithmetic_int_type)
  3461. else
  3462. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tpointerdef(tcallparanode(left).left.resultdef).pointer_arithmetic_uint_type)
  3463. end
  3464. else if is_integer(tcallparanode(left).left.resultdef) then
  3465. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef)
  3466. else
  3467. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  3468. if assigned(tcallparanode(tcallparanode(left).right).right) then
  3469. { should be handled in the parser (JM) }
  3470. internalerror(2006020901);
  3471. end
  3472. else
  3473. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  3474. end;
  3475. end
  3476. { generic type parameter? }
  3477. else if is_typeparam(left.resultdef) then
  3478. begin
  3479. result:=cnothingnode.create;
  3480. exit;
  3481. end
  3482. else
  3483. begin
  3484. hp:=self;
  3485. if isunaryoverloaded(hp,[]) then
  3486. begin
  3487. { inc(rec) and dec(rec) assigns result value to argument }
  3488. result:=cassignmentnode.create(tcallparanode(left).left.getcopy,hp);
  3489. exit;
  3490. end
  3491. else
  3492. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  3493. end;
  3494. end
  3495. else
  3496. CGMessagePos(fileinfo,type_e_mismatch);
  3497. end;
  3498. end;
  3499. in_and_assign_x_y,
  3500. in_or_assign_x_y,
  3501. in_xor_assign_x_y,
  3502. in_sar_assign_x_y,
  3503. in_shl_assign_x_y,
  3504. in_shr_assign_x_y,
  3505. in_rol_assign_x_y,
  3506. in_ror_assign_x_y:
  3507. begin
  3508. resultdef:=voidtype;
  3509. if not(df_generic in current_procinfo.procdef.defoptions) then
  3510. begin
  3511. { first parameter must exist }
  3512. if not assigned(left) or (left.nodetype<>callparan) then
  3513. internalerror(2017032501);
  3514. { second parameter must exist }
  3515. if not assigned(tcallparanode(left).right) or (tcallparanode(left).right.nodetype<>callparan) then
  3516. internalerror(2017032502);
  3517. { third parameter must NOT exist }
  3518. if assigned(tcallparanode(tcallparanode(left).right).right) then
  3519. internalerror(2017032503);
  3520. valid_for_var(tcallparanode(tcallparanode(left).right).left,true);
  3521. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_readwritten,[vsf_must_be_valid]);
  3522. if is_integer(tcallparanode(left).right.resultdef) then
  3523. begin
  3524. { value of right gets changed -> must be unique }
  3525. set_unique(tcallparanode(tcallparanode(left).right).left);
  3526. if is_integer(left.resultdef) then
  3527. begin
  3528. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  3529. { these nodes shouldn't be created, when range checking is on }
  3530. if [cs_check_range,cs_check_overflow]*localswitches<>[] then
  3531. internalerror(2017032701);
  3532. if inlinenumber in [in_sar_assign_x_y,in_shl_assign_x_y,in_shr_assign_x_y,in_rol_assign_x_y,in_ror_assign_x_y] then
  3533. inserttypeconv(tcallparanode(left).left,sinttype)
  3534. else
  3535. inserttypeconv(tcallparanode(left).left,tcallparanode(tcallparanode(left).right).left.resultdef);
  3536. end
  3537. else
  3538. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  3539. end
  3540. { generic type parameter? }
  3541. else if is_typeparam(tcallparanode(left).right.resultdef) then
  3542. begin
  3543. result:=cnothingnode.create;
  3544. exit;
  3545. end
  3546. else
  3547. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  3548. end;
  3549. end;
  3550. in_neg_assign_x,
  3551. in_not_assign_x:
  3552. begin
  3553. resultdef:=voidtype;
  3554. if not(df_generic in current_procinfo.procdef.defoptions) then
  3555. begin
  3556. valid_for_var(left,true);
  3557. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  3558. if is_integer(left.resultdef) then
  3559. begin
  3560. { value of left gets changed -> must be unique }
  3561. set_unique(left);
  3562. { these nodes shouldn't be created, when range checking is on }
  3563. if [cs_check_range,cs_check_overflow]*current_settings.localswitches<>[] then
  3564. internalerror(2017040703);
  3565. end
  3566. { generic type parameter? }
  3567. else if is_typeparam(left.resultdef) then
  3568. begin
  3569. result:=cnothingnode.create;
  3570. exit;
  3571. end
  3572. else
  3573. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  3574. end;
  3575. end;
  3576. in_read_x,
  3577. in_readln_x,
  3578. in_readstr_x,
  3579. in_write_x,
  3580. in_writeln_x,
  3581. in_writestr_x :
  3582. begin
  3583. result := handle_read_write;
  3584. end;
  3585. in_settextbuf_file_x :
  3586. begin
  3587. if target_info.system in systems_managed_vm then
  3588. message(parser_e_feature_unsupported_for_vm);
  3589. resultdef:=voidtype;
  3590. { now we know the type of buffer }
  3591. hp:=ccallparanode.create(cordconstnode.create(
  3592. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  3593. result:=ccallnode.createintern('SETTEXTBUF',hp);
  3594. left:=nil;
  3595. end;
  3596. { the firstpass of the arg has been done in firstcalln ? }
  3597. in_reset_typedfile,
  3598. in_rewrite_typedfile,
  3599. in_reset_typedfile_name,
  3600. in_rewrite_typedfile_name :
  3601. begin
  3602. result := handle_reset_rewrite_typed;
  3603. end;
  3604. in_str_x_string :
  3605. begin
  3606. result:=handle_str;
  3607. end;
  3608. in_val_x :
  3609. begin
  3610. result:=handle_val;
  3611. end;
  3612. in_include_x_y,
  3613. in_exclude_x_y:
  3614. begin
  3615. resultdef:=voidtype;
  3616. { the parser already checks whether we have two (and exactly two) }
  3617. { parameters (JM) }
  3618. { first param must be var }
  3619. valid_for_var(tcallparanode(left).left,true);
  3620. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  3621. { check type }
  3622. if (left.resultdef.typ=setdef) then
  3623. begin
  3624. { insert a type conversion }
  3625. { to the type of the set elements }
  3626. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  3627. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  3628. tsetdef(left.resultdef).elementdef);
  3629. end
  3630. else if left.resultdef.typ<>undefineddef then
  3631. CGMessage(type_e_mismatch);
  3632. end;
  3633. in_pack_x_y_z,
  3634. in_unpack_x_y_z :
  3635. begin
  3636. handle_pack_unpack;
  3637. end;
  3638. in_slice_x:
  3639. begin
  3640. if target_info.system in systems_managed_vm then
  3641. message(parser_e_feature_unsupported_for_vm);
  3642. result:=nil;
  3643. resultdef:=tcallparanode(left).left.resultdef;
  3644. if (resultdef.typ <> arraydef) then
  3645. CGMessagePos(left.fileinfo,type_e_mismatch)
  3646. else if is_packed_array(resultdef) then
  3647. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  3648. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  3649. CGMessagePos1(tcallparanode(left).right.fileinfo,
  3650. type_e_integer_expr_expected,
  3651. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  3652. end;
  3653. in_new_x:
  3654. resultdef:=left.resultdef;
  3655. in_low_x,
  3656. in_high_x:
  3657. begin
  3658. case left.resultdef.typ of
  3659. undefineddef,
  3660. orddef,
  3661. enumdef,
  3662. setdef:
  3663. ;
  3664. arraydef:
  3665. begin
  3666. if (inlinenumber=in_low_x) then
  3667. set_varstate(left,vs_read,[])
  3668. else
  3669. begin
  3670. if is_open_array(left.resultdef) or
  3671. is_array_of_const(left.resultdef) then
  3672. begin
  3673. set_varstate(left,vs_read,[]);
  3674. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  3675. end
  3676. else
  3677. begin
  3678. set_varstate(left,vs_read,[]);
  3679. resultdef:=sizesinttype;
  3680. end;
  3681. end;
  3682. end;
  3683. stringdef:
  3684. begin
  3685. if inlinenumber=in_low_x then
  3686. begin
  3687. set_varstate(left,vs_read,[]);
  3688. end
  3689. else
  3690. begin
  3691. if is_open_string(left.resultdef) then
  3692. begin
  3693. set_varstate(left,vs_read,[]);
  3694. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  3695. end
  3696. else if is_dynamicstring(left.resultdef) then
  3697. begin
  3698. result:=cinlinenode.create(in_length_x,false,left);
  3699. if cs_zerobasedstrings in current_settings.localswitches then
  3700. result:=caddnode.create(subn,result,cordconstnode.create(1,sinttype,false));
  3701. { make sure the left node doesn't get disposed, since it's }
  3702. { reused in the new node (JM) }
  3703. left:=nil;
  3704. end
  3705. end;
  3706. end;
  3707. else
  3708. CGMessage(type_e_mismatch);
  3709. end;
  3710. end;
  3711. in_exp_real,
  3712. in_frac_real,
  3713. in_int_real,
  3714. in_cos_real,
  3715. in_sin_real,
  3716. in_arctan_real,
  3717. in_ln_real :
  3718. begin
  3719. { on the Z80, the double result is returned in a var param, because
  3720. it's too big to fit in registers. In that case we have 2 parameters
  3721. and left.nodetype is a callparan. }
  3722. if left.nodetype = callparan then
  3723. temp_pnode := @tcallparanode(left).left
  3724. else
  3725. temp_pnode := @left;
  3726. set_varstate(temp_pnode^,vs_read,[vsf_must_be_valid]);
  3727. { converting an int64 to double on platforms without }
  3728. { extended can cause precision loss }
  3729. if not(temp_pnode^.nodetype in [ordconstn,realconstn]) then
  3730. inserttypeconv(temp_pnode^,pbestrealtype^);
  3731. resultdef:=pbestrealtype^;
  3732. end;
  3733. in_trunc_real,
  3734. in_round_real :
  3735. begin
  3736. { on i8086, the int64 result is returned in a var param, because
  3737. it's too big to fit in a register or a pair of registers. In
  3738. that case we have 2 parameters and left.nodetype is a callparan. }
  3739. if left.nodetype=callparan then
  3740. temp_pnode:=@tcallparanode(left).left
  3741. else
  3742. temp_pnode:=@left;
  3743. set_varstate(temp_pnode^,vs_read,[vsf_must_be_valid]);
  3744. { on platforms where comp and currency are "type int64", this is
  3745. handled via inlined system helpers (-> no need for special
  3746. handling of s64currency/s64comp for them) }
  3747. if inlinenumber=in_trunc_real then
  3748. removefloatupcasts(temp_pnode^,[s32real,s64real,s80real,sc80real,s128real,s64currency,s64comp])
  3749. else
  3750. removefloatupcasts(temp_pnode^,[s32real,s64real,s80real,sc80real,s128real,s64comp]);
  3751. if (inlinenumber=in_trunc_real) and
  3752. is_currency(temp_pnode^.resultdef) then
  3753. begin
  3754. result:=cmoddivnode.create(divn,ctypeconvnode.create_internal(temp_pnode^.getcopy,s64inttype),genintconstnode(10000));
  3755. exit;
  3756. end
  3757. else if is_fpucomp(temp_pnode^.resultdef) then
  3758. begin
  3759. result:=ctypeconvnode.create_internal(temp_pnode^.getcopy,s64inttype);
  3760. exit;
  3761. end;
  3762. resultdef:=s64inttype;
  3763. end;
  3764. in_pi_real :
  3765. begin
  3766. resultdef:=pbestrealtype^;
  3767. end;
  3768. in_abs_long:
  3769. begin
  3770. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3771. resultdef:=left.resultdef;
  3772. end;
  3773. in_abs_real,
  3774. in_sqr_real,
  3775. in_sqrt_real :
  3776. begin
  3777. { on the Z80, the double result is returned in a var param, because
  3778. it's too big to fit in registers. In that case we have 2 parameters
  3779. and left.nodetype is a callparan. }
  3780. if left.nodetype = callparan then
  3781. temp_pnode := @tcallparanode(left).left
  3782. else
  3783. temp_pnode := @left;
  3784. set_varstate(temp_pnode^,vs_read,[vsf_must_be_valid]);
  3785. resultdef:=removefloatupcasts(temp_pnode^,[s32real,s64real,s80real,sc80real,s128real]);
  3786. end;
  3787. {$ifdef SUPPORT_MMX}
  3788. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  3789. begin
  3790. end;
  3791. {$endif SUPPORT_MMX}
  3792. in_aligned_x,
  3793. in_unaligned_x:
  3794. begin
  3795. resultdef:=left.resultdef;
  3796. end;
  3797. in_volatile_x:
  3798. begin
  3799. resultdef:=left.resultdef;
  3800. { volatile only makes sense if the value is in memory }
  3801. make_not_regable(left,[ra_addr_regable]);
  3802. end;
  3803. in_assert_x_y :
  3804. begin
  3805. resultdef:=voidtype;
  3806. if assigned(left) then
  3807. begin
  3808. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  3809. { check type }
  3810. if is_boolean(left.resultdef) or
  3811. (
  3812. (left.resultdef.typ=undefineddef) and
  3813. (df_generic in current_procinfo.procdef.defoptions)
  3814. ) then
  3815. begin
  3816. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  3817. { must always be a string }
  3818. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  3819. end
  3820. else
  3821. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  3822. end
  3823. else
  3824. CGMessage(type_e_mismatch);
  3825. if (cs_do_assertion in current_settings.localswitches) then
  3826. include(current_procinfo.flags,pi_do_call);
  3827. end;
  3828. in_prefetch_var:
  3829. resultdef:=voidtype;
  3830. in_get_frame,
  3831. in_get_caller_frame,
  3832. in_get_caller_addr:
  3833. begin
  3834. resultdef:=voidpointertype;
  3835. end;
  3836. in_rol_x,
  3837. in_ror_x,
  3838. in_sar_x:
  3839. begin
  3840. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3841. resultdef:=left.resultdef;
  3842. end;
  3843. in_rol_x_y,
  3844. in_ror_x_y,
  3845. in_sar_x_y:
  3846. begin
  3847. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  3848. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  3849. resultdef:=tcallparanode(tcallparanode(left).right).left.resultdef;
  3850. end;
  3851. in_bsf_x,
  3852. in_bsr_x:
  3853. begin
  3854. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3855. if not is_integer(left.resultdef) then
  3856. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  3857. if torddef(left.resultdef).ordtype in [u64bit, s64bit] then
  3858. resultdef:=u64inttype
  3859. else
  3860. resultdef:=u32inttype
  3861. end;
  3862. in_popcnt_x:
  3863. begin
  3864. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3865. if not is_integer(left.resultdef) then
  3866. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  3867. resultdef:=left.resultdef;
  3868. end;
  3869. in_objc_selector_x:
  3870. begin
  3871. result:=cobjcselectornode.create(left);
  3872. { reused }
  3873. left:=nil;
  3874. end;
  3875. in_objc_protocol_x:
  3876. begin
  3877. result:=cobjcprotocolnode.create(left);
  3878. { reused }
  3879. left:=nil;
  3880. end;
  3881. in_objc_encode_x:
  3882. begin
  3883. result:=handle_objc_encode;
  3884. end;
  3885. in_default_x:
  3886. begin
  3887. result:=handle_default;
  3888. end;
  3889. in_box_x:
  3890. begin
  3891. result:=handle_box;
  3892. end;
  3893. in_unbox_x_y:
  3894. begin
  3895. result:=handle_unbox;
  3896. end;
  3897. in_fma_single,
  3898. in_fma_double,
  3899. in_fma_extended,
  3900. in_fma_float128:
  3901. begin
  3902. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  3903. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  3904. set_varstate(tcallparanode(tcallparanode(tcallparanode(left).right).right).left,vs_read,[vsf_must_be_valid]);
  3905. resultdef:=tcallparanode(left).left.resultdef;
  3906. end;
  3907. in_max_longint,
  3908. in_max_dword,
  3909. in_min_longint,
  3910. in_min_dword,
  3911. in_max_int64,
  3912. in_max_qword,
  3913. in_min_int64,
  3914. in_min_qword,
  3915. in_max_single,
  3916. in_max_double,
  3917. in_min_single,
  3918. in_min_double:
  3919. begin
  3920. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  3921. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  3922. resultdef:=tcallparanode(left).left.resultdef;
  3923. end;
  3924. in_delete_x_y_z:
  3925. begin
  3926. result:=handle_delete;
  3927. end;
  3928. in_insert_x_y_z:
  3929. begin
  3930. result:=handle_insert;
  3931. end;
  3932. in_concat_x:
  3933. begin
  3934. result:=handle_concat;
  3935. end;
  3936. else
  3937. result:=pass_typecheck_cpu;
  3938. end;
  3939. end;
  3940. if not assigned(result) and not
  3941. codegenerror then
  3942. result:=simplify(false);
  3943. end;
  3944. function tinlinenode.pass_typecheck_cpu : tnode;
  3945. begin
  3946. Result:=nil;
  3947. internalerror(2017110102);
  3948. end;
  3949. function tinlinenode.pass_1 : tnode;
  3950. var
  3951. hp: tnode;
  3952. shiftconst: longint;
  3953. objdef: tobjectdef;
  3954. sym : tsym;
  3955. begin
  3956. result:=nil;
  3957. { if we handle writeln; left contains no valid address }
  3958. if assigned(left) then
  3959. begin
  3960. if left.nodetype=callparan then
  3961. tcallparanode(left).firstcallparan
  3962. else
  3963. firstpass(left);
  3964. end;
  3965. { intern const should already be handled }
  3966. if inf_inlineconst in inlinenodeflags then
  3967. internalerror(200104044);
  3968. case inlinenumber of
  3969. in_lo_qword,
  3970. in_hi_qword,
  3971. in_lo_long,
  3972. in_hi_long,
  3973. in_lo_word,
  3974. in_hi_word:
  3975. begin
  3976. shiftconst := 0;
  3977. case inlinenumber of
  3978. in_hi_qword:
  3979. shiftconst := 32;
  3980. in_hi_long:
  3981. shiftconst := 16;
  3982. in_hi_word:
  3983. shiftconst := 8;
  3984. else
  3985. ;
  3986. end;
  3987. if shiftconst <> 0 then
  3988. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  3989. cordconstnode.create(shiftconst,sinttype,false)),resultdef)
  3990. else
  3991. result := ctypeconvnode.create_internal(left,resultdef);
  3992. left := nil;
  3993. firstpass(result);
  3994. end;
  3995. in_sizeof_x,
  3996. in_typeof_x:
  3997. begin
  3998. expectloc:=LOC_REGISTER;
  3999. case left.resultdef.typ of
  4000. objectdef,classrefdef:
  4001. begin
  4002. if left.resultdef.typ=objectdef then
  4003. begin
  4004. result:=cloadvmtaddrnode.create(left);
  4005. objdef:=tobjectdef(left.resultdef);
  4006. end
  4007. else
  4008. begin
  4009. result:=left;
  4010. objdef:=tobjectdef(tclassrefdef(left.resultdef).pointeddef);
  4011. end;
  4012. left:=nil;
  4013. if inlinenumber=in_sizeof_x then
  4014. begin
  4015. inserttypeconv_explicit(result,cpointerdef.getreusable(objdef.vmt_def));
  4016. result:=cderefnode.create(result);
  4017. result:=genloadfield(result,'VINSTANCESIZE');
  4018. end
  4019. else
  4020. inserttypeconv_explicit(result,voidpointertype);
  4021. end;
  4022. undefineddef:
  4023. ;
  4024. else
  4025. internalerror(2015122702);
  4026. end;
  4027. end;
  4028. in_length_x:
  4029. begin
  4030. result:=first_length;
  4031. end;
  4032. in_typeinfo_x:
  4033. begin
  4034. if (left.resultdef.typ=enumdef) and
  4035. (tenumdef(left.resultdef).has_jumps) then
  4036. begin
  4037. if (left.nodetype=typen) and (sp_generic_para in ttypenode(left).typesym.symoptions) then
  4038. result:=cnilnode.create
  4039. else
  4040. internalerror(2021032601);
  4041. end
  4042. else
  4043. result:=caddrnode.create_internal(
  4044. crttinode.create(tstoreddef(left.resultdef),fullrtti,rdt_normal)
  4045. );
  4046. end;
  4047. in_gettypekind_x:
  4048. begin
  4049. sym:=tenumdef(typekindtype).int2enumsym(get_typekind(left.resultdef));
  4050. if not assigned(sym) then
  4051. internalerror(2017081101);
  4052. if sym.typ<>enumsym then
  4053. internalerror(2017081102);
  4054. result:=genenumnode(tenumsym(sym));
  4055. end;
  4056. in_ismanagedtype_x:
  4057. begin
  4058. if left.resultdef.needs_inittable then
  4059. result:=cordconstnode.create(1,resultdef,false)
  4060. else
  4061. result:=cordconstnode.create(0,resultdef,false);
  4062. end;
  4063. in_isconstvalue_x:
  4064. begin
  4065. if is_constnode(left) then
  4066. result:=cordconstnode.create(1,resultdef,false)
  4067. else
  4068. result:=cordconstnode.create(0,resultdef,false);
  4069. end;
  4070. in_assigned_x:
  4071. begin
  4072. result:=first_assigned;
  4073. end;
  4074. in_pred_x,
  4075. in_succ_x:
  4076. begin
  4077. expectloc:=LOC_REGISTER;
  4078. { in case of range/overflow checking, use a regular addnode
  4079. because it's too complex to handle correctly otherwise }
  4080. {$ifndef jvm}
  4081. { enums are class instances in the JVM -> always need conversion }
  4082. if (([cs_check_overflow,cs_check_range]*current_settings.localswitches)<>[]) and not(nf_internal in flags) then
  4083. {$endif}
  4084. begin
  4085. { create constant 1 }
  4086. hp:=cordconstnode.create(1,left.resultdef,false);
  4087. typecheckpass(hp);
  4088. if not is_integer(hp.resultdef) then
  4089. inserttypeconv_internal(hp,sinttype);
  4090. { avoid type errors from the addn/subn }
  4091. if not is_integer(left.resultdef) then
  4092. inserttypeconv_internal(left,sinttype);
  4093. { addition/substraction depending on succ/pred }
  4094. if inlinenumber=in_succ_x then
  4095. hp:=caddnode.create(addn,left,hp)
  4096. else
  4097. hp:=caddnode.create(subn,left,hp);
  4098. { the condition above is not tested for jvm, so we need to avoid overflow checks here
  4099. by setting nf_internal for the add/sub node as well }
  4100. if nf_internal in flags then
  4101. include(hp.flags,nf_internal);
  4102. { assign result of addition }
  4103. if not(is_integer(resultdef)) then
  4104. inserttypeconv(hp,corddef.create(
  4105. {$ifdef cpu64bitaddr}
  4106. s64bit,
  4107. {$else cpu64bitaddr}
  4108. s32bit,
  4109. {$endif cpu64bitaddr}
  4110. get_min_value(resultdef),
  4111. get_max_value(resultdef),
  4112. true))
  4113. else
  4114. inserttypeconv(hp,resultdef);
  4115. if nf_internal in flags then
  4116. include(hp.flags,nf_internal);
  4117. { avoid any possible errors/warnings }
  4118. inserttypeconv_internal(hp,resultdef);
  4119. { firstpass it }
  4120. firstpass(hp);
  4121. { left is reused }
  4122. left:=nil;
  4123. { return new node }
  4124. result:=hp;
  4125. end;
  4126. end;
  4127. in_setlength_x:
  4128. result:=first_setlength;
  4129. in_copy_x:
  4130. result:=first_copy;
  4131. in_initialize_x,
  4132. in_finalize_x:
  4133. begin
  4134. expectloc:=LOC_VOID;
  4135. end;
  4136. in_inc_x,
  4137. in_dec_x:
  4138. begin
  4139. result:=first_IncDec;
  4140. end;
  4141. in_and_assign_x_y,
  4142. in_or_assign_x_y,
  4143. in_xor_assign_x_y,
  4144. in_sar_assign_x_y,
  4145. in_shl_assign_x_y,
  4146. in_shr_assign_x_y,
  4147. in_rol_assign_x_y,
  4148. in_ror_assign_x_y:
  4149. begin
  4150. result:=first_AndOrXorShiftRot_assign;
  4151. end;
  4152. in_neg_assign_x,
  4153. in_not_assign_x:
  4154. begin
  4155. result:=first_NegNot_assign;
  4156. end;
  4157. in_include_x_y,
  4158. in_exclude_x_y:
  4159. begin
  4160. result:=first_IncludeExclude;
  4161. end;
  4162. in_pack_x_y_z,
  4163. in_unpack_x_y_z:
  4164. begin
  4165. result:=first_pack_unpack;
  4166. end;
  4167. in_exp_real:
  4168. begin
  4169. result:= first_exp_real;
  4170. end;
  4171. in_round_real:
  4172. begin
  4173. result:= first_round_real;
  4174. end;
  4175. in_trunc_real:
  4176. begin
  4177. result:= first_trunc_real;
  4178. end;
  4179. in_int_real:
  4180. begin
  4181. result:= first_int_real;
  4182. end;
  4183. in_frac_real:
  4184. begin
  4185. result:= first_frac_real;
  4186. end;
  4187. in_cos_real:
  4188. begin
  4189. result:= first_cos_real;
  4190. end;
  4191. in_sin_real:
  4192. begin
  4193. result := first_sin_real;
  4194. end;
  4195. in_arctan_real:
  4196. begin
  4197. result := first_arctan_real;
  4198. end;
  4199. in_pi_real :
  4200. begin
  4201. result := first_pi;
  4202. end;
  4203. in_abs_real:
  4204. begin
  4205. result := first_abs_real;
  4206. end;
  4207. in_abs_long:
  4208. begin
  4209. result := first_abs_long;
  4210. end;
  4211. in_sqr_real:
  4212. begin
  4213. result := first_sqr_real;
  4214. end;
  4215. in_sqrt_real:
  4216. begin
  4217. result := first_sqrt_real;
  4218. end;
  4219. in_ln_real:
  4220. begin
  4221. result := first_ln_real;
  4222. end;
  4223. {$ifdef SUPPORT_MMX}
  4224. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  4225. begin
  4226. end;
  4227. {$endif SUPPORT_MMX}
  4228. in_assert_x_y :
  4229. begin
  4230. result:=first_assert;
  4231. end;
  4232. in_low_x:
  4233. internalerror(200104047);
  4234. in_high_x:
  4235. begin
  4236. result:=first_high;
  4237. end;
  4238. in_slice_x:
  4239. { slice can be used only in calls for open array parameters, so it has to be converted appropriatly before
  4240. if we get here, the array could not be passed to an open array parameter so it is an error }
  4241. CGMessagePos(left.fileinfo,type_e_mismatch);
  4242. in_ord_x,
  4243. in_chr_byte:
  4244. begin
  4245. { should not happend as it's converted to typeconv }
  4246. internalerror(200104045);
  4247. end;
  4248. in_ofs_x :
  4249. internalerror(2000101002);
  4250. in_seg_x :
  4251. begin
  4252. result:=first_seg;
  4253. end;
  4254. in_settextbuf_file_x,
  4255. in_reset_typedfile,
  4256. in_rewrite_typedfile,
  4257. in_reset_typedfile_name,
  4258. in_rewrite_typedfile_name,
  4259. in_str_x_string,
  4260. in_val_x,
  4261. in_read_x,
  4262. in_readln_x,
  4263. in_write_x,
  4264. in_writeln_x :
  4265. begin
  4266. { should be handled by pass_typecheck }
  4267. internalerror(2001082302);
  4268. end;
  4269. in_get_frame:
  4270. begin
  4271. result:=first_get_frame;
  4272. end;
  4273. in_get_caller_frame:
  4274. begin
  4275. expectloc:=LOC_REGISTER;
  4276. end;
  4277. in_get_caller_addr:
  4278. begin
  4279. expectloc:=LOC_REGISTER;
  4280. end;
  4281. in_prefetch_var:
  4282. begin
  4283. expectloc:=LOC_VOID;
  4284. end;
  4285. in_aligned_x,
  4286. in_unaligned_x,
  4287. in_volatile_x:
  4288. begin
  4289. expectloc:=tcallparanode(left).left.expectloc;
  4290. end;
  4291. in_rol_x,
  4292. in_rol_x_y,
  4293. in_ror_x,
  4294. in_ror_x_y:
  4295. expectloc:=LOC_REGISTER;
  4296. in_bsf_x,
  4297. in_bsr_x:
  4298. result:=first_bitscan;
  4299. in_sar_x,
  4300. in_sar_x_y:
  4301. result:=first_sar;
  4302. in_popcnt_x:
  4303. result:=first_popcnt;
  4304. in_new_x:
  4305. result:=first_new;
  4306. in_box_x:
  4307. result:=first_box;
  4308. in_unbox_x_y:
  4309. result:=first_unbox;
  4310. in_fma_single,
  4311. in_fma_double,
  4312. in_fma_extended,
  4313. in_fma_float128:
  4314. result:=first_fma;
  4315. in_max_longint,
  4316. in_max_dword,
  4317. in_min_longint,
  4318. in_min_dword,
  4319. in_max_int64,
  4320. in_max_qword,
  4321. in_min_int64,
  4322. in_min_qword,
  4323. in_min_single,
  4324. in_min_double,
  4325. in_max_single,
  4326. in_max_double:
  4327. result:=first_minmax;
  4328. else
  4329. result:=first_cpu;
  4330. end;
  4331. end;
  4332. {$maxfpuregisters default}
  4333. function tinlinenode.docompare(p: tnode): boolean;
  4334. begin
  4335. docompare :=
  4336. inherited docompare(p) and
  4337. (inlinenumber = tinlinenode(p).inlinenumber);
  4338. end;
  4339. procedure tinlinenode.mark_write;
  4340. begin
  4341. case inlinenumber of
  4342. in_aligned_x, in_unaligned_x:
  4343. tcallparanode(left).left.mark_write;
  4344. else
  4345. inherited mark_write;
  4346. end;
  4347. end;
  4348. function tinlinenode.first_pi : tnode;
  4349. begin
  4350. result:=crealconstnode.create(getpi,pbestrealtype^);
  4351. end;
  4352. function tinlinenode.first_arctan_real : tnode;
  4353. var
  4354. temp_pnode: pnode;
  4355. begin
  4356. { create the call to the helper }
  4357. { on entry left node contains the parameter }
  4358. if left.nodetype = callparan then
  4359. temp_pnode := @tcallparanode(left).left
  4360. else
  4361. temp_pnode := @left;
  4362. result := ccallnode.createintern('fpc_arctan_real',
  4363. ccallparanode.create(temp_pnode^,nil));
  4364. temp_pnode^ := nil;
  4365. end;
  4366. function tinlinenode.first_abs_real : tnode;
  4367. var
  4368. callnode : tcallnode;
  4369. temp_pnode: pnode;
  4370. begin
  4371. { create the call to the helper }
  4372. { on entry left node contains the parameter }
  4373. if left.nodetype = callparan then
  4374. temp_pnode := @tcallparanode(left).left
  4375. else
  4376. temp_pnode := @left;
  4377. callnode:=ccallnode.createintern('fpc_abs_real',
  4378. ccallparanode.create(temp_pnode^,nil));
  4379. result := ctypeconvnode.create(callnode,resultdef);
  4380. include(callnode.callnodeflags,cnf_check_fpu_exceptions);
  4381. temp_pnode^ := nil;
  4382. end;
  4383. function tinlinenode.first_sqr_real : tnode;
  4384. var
  4385. callnode : tcallnode;
  4386. temp_pnode: pnode;
  4387. begin
  4388. {$ifndef cpufpemu}
  4389. { this procedure might be only used for cpus definining cpufpemu else
  4390. the optimizer might go into an endless loop when doing x*x -> changes }
  4391. internalerror(2011092401);
  4392. {$endif cpufpemu}
  4393. { create the call to the helper }
  4394. { on entry left node contains the parameter }
  4395. if left.nodetype = callparan then
  4396. temp_pnode := @tcallparanode(left).left
  4397. else
  4398. temp_pnode := @left;
  4399. callnode:=ccallnode.createintern('fpc_sqr_real',
  4400. ccallparanode.create(temp_pnode^,nil));
  4401. result := ctypeconvnode.create(callnode,resultdef);
  4402. include(callnode.callnodeflags,cnf_check_fpu_exceptions);
  4403. temp_pnode^ := nil;
  4404. end;
  4405. function tinlinenode.first_sqrt_real : tnode;
  4406. var
  4407. fdef: tdef;
  4408. procname: string[31];
  4409. callnode: tcallnode;
  4410. temp_pnode: pnode;
  4411. begin
  4412. if left.nodetype = callparan then
  4413. temp_pnode := @tcallparanode(left).left
  4414. else
  4415. temp_pnode := @left;
  4416. if ((cs_fp_emulation in current_settings.moduleswitches)
  4417. {$ifdef cpufpemu}
  4418. or (current_settings.fputype=fpu_soft)
  4419. {$endif cpufpemu}
  4420. ) and not (target_info.system in systems_wince) then
  4421. begin
  4422. case tfloatdef(temp_pnode^.resultdef).floattype of
  4423. s32real:
  4424. begin
  4425. fdef:=search_system_type('FLOAT32REC').typedef;
  4426. procname:='float32_sqrt';
  4427. end;
  4428. s64real:
  4429. begin
  4430. fdef:=search_system_type('FLOAT64').typedef;
  4431. procname:='float64_sqrt';
  4432. end;
  4433. {!!! not yet implemented
  4434. s128real:
  4435. }
  4436. else
  4437. internalerror(2014052101);
  4438. end;
  4439. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  4440. ctypeconvnode.create_internal(temp_pnode^,fdef),nil)),resultdef);
  4441. end
  4442. else
  4443. begin
  4444. { create the call to the helper }
  4445. { on entry left node contains the parameter }
  4446. callnode := ccallnode.createintern('fpc_sqrt_real',
  4447. ccallparanode.create(temp_pnode^,nil));
  4448. result := ctypeconvnode.create(callnode,resultdef);
  4449. include(callnode.callnodeflags,cnf_check_fpu_exceptions);
  4450. end;
  4451. temp_pnode^ := nil;
  4452. end;
  4453. function tinlinenode.first_ln_real : tnode;
  4454. var
  4455. temp_pnode: pnode;
  4456. begin
  4457. { create the call to the helper }
  4458. { on entry left node contains the parameter }
  4459. if left.nodetype = callparan then
  4460. temp_pnode := @tcallparanode(left).left
  4461. else
  4462. temp_pnode := @left;
  4463. result := ccallnode.createintern('fpc_ln_real',
  4464. ccallparanode.create(temp_pnode^,nil));
  4465. include(tcallnode(result).callnodeflags,cnf_check_fpu_exceptions);
  4466. temp_pnode^ := nil;
  4467. end;
  4468. function tinlinenode.first_cos_real : tnode;
  4469. var
  4470. temp_pnode: pnode;
  4471. begin
  4472. { create the call to the helper }
  4473. { on entry left node contains the parameter }
  4474. if left.nodetype = callparan then
  4475. temp_pnode := @tcallparanode(left).left
  4476. else
  4477. temp_pnode := @left;
  4478. result := ccallnode.createintern('fpc_cos_real',
  4479. ccallparanode.create(temp_pnode^,nil));
  4480. include(tcallnode(result).callnodeflags,cnf_check_fpu_exceptions);
  4481. temp_pnode^ := nil;
  4482. end;
  4483. function tinlinenode.first_sin_real : tnode;
  4484. var
  4485. temp_pnode: pnode;
  4486. begin
  4487. { create the call to the helper }
  4488. { on entry left node contains the parameter }
  4489. if left.nodetype = callparan then
  4490. temp_pnode := @tcallparanode(left).left
  4491. else
  4492. temp_pnode := @left;
  4493. result := ccallnode.createintern('fpc_sin_real',
  4494. ccallparanode.create(temp_pnode^,nil));
  4495. include(tcallnode(result).callnodeflags,cnf_check_fpu_exceptions);
  4496. temp_pnode^ := nil;
  4497. end;
  4498. function tinlinenode.first_exp_real : tnode;
  4499. var
  4500. temp_pnode: pnode;
  4501. begin
  4502. { create the call to the helper }
  4503. { on entry left node contains the parameter }
  4504. if left.nodetype = callparan then
  4505. temp_pnode := @tcallparanode(left).left
  4506. else
  4507. temp_pnode := @left;
  4508. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(temp_pnode^,nil));
  4509. include(tcallnode(result).callnodeflags,cnf_check_fpu_exceptions);
  4510. temp_pnode^ := nil;
  4511. end;
  4512. function tinlinenode.first_int_real : tnode;
  4513. var
  4514. temp_pnode: pnode;
  4515. begin
  4516. { create the call to the helper }
  4517. { on entry left node contains the parameter }
  4518. if left.nodetype = callparan then
  4519. temp_pnode := @tcallparanode(left).left
  4520. else
  4521. temp_pnode := @left;
  4522. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(temp_pnode^,nil));
  4523. include(tcallnode(result).callnodeflags,cnf_check_fpu_exceptions);
  4524. temp_pnode^ := nil;
  4525. end;
  4526. function tinlinenode.first_frac_real : tnode;
  4527. var
  4528. temp_pnode: pnode;
  4529. begin
  4530. { create the call to the helper }
  4531. { on entry left node contains the parameter }
  4532. if left.nodetype = callparan then
  4533. temp_pnode := @tcallparanode(left).left
  4534. else
  4535. temp_pnode := @left;
  4536. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(temp_pnode^,nil));
  4537. include(tcallnode(result).callnodeflags,cnf_check_fpu_exceptions);
  4538. temp_pnode^ := nil;
  4539. end;
  4540. function tinlinenode.first_round_real : tnode;
  4541. var
  4542. temp_pnode: pnode;
  4543. begin
  4544. { create the call to the helper }
  4545. { on entry left node contains the parameter }
  4546. if left.nodetype = callparan then
  4547. temp_pnode := @tcallparanode(left).left
  4548. else
  4549. temp_pnode := @left;
  4550. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(temp_pnode^,nil));
  4551. include(tcallnode(result).callnodeflags,cnf_check_fpu_exceptions);
  4552. temp_pnode^ := nil;
  4553. end;
  4554. function tinlinenode.first_trunc_real : tnode;
  4555. var
  4556. temp_pnode: pnode;
  4557. begin
  4558. { create the call to the helper }
  4559. { on entry left node contains the parameter }
  4560. if left.nodetype = callparan then
  4561. temp_pnode := @tcallparanode(left).left
  4562. else
  4563. temp_pnode := @left;
  4564. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(temp_pnode^,nil));
  4565. include(tcallnode(result).callnodeflags,cnf_check_fpu_exceptions);
  4566. temp_pnode^ := nil;
  4567. end;
  4568. function tinlinenode.first_abs_long : tnode;
  4569. begin
  4570. expectloc:=LOC_REGISTER;
  4571. result:=nil;
  4572. end;
  4573. function tinlinenode.getaddsub_for_incdec : tnode;
  4574. var
  4575. hp,hpp,resultnode : tnode;
  4576. tempnode: ttempcreatenode;
  4577. newstatement: tstatementnode;
  4578. newblock: tblocknode;
  4579. begin
  4580. newblock := internalstatements(newstatement);
  4581. { extra parameter? }
  4582. if assigned(tcallparanode(left).right) then
  4583. begin
  4584. { Yes, use for add node }
  4585. hpp := tcallparanode(tcallparanode(left).right).left;
  4586. tcallparanode(tcallparanode(left).right).left := nil;
  4587. if assigned(tcallparanode(tcallparanode(left).right).right) then
  4588. CGMessage(parser_e_illegal_expression);
  4589. end
  4590. else
  4591. begin
  4592. { no, create constant 1 }
  4593. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  4594. end;
  4595. typecheckpass(hpp);
  4596. { make sure we don't call functions part of the left node twice (and generally }
  4597. { optimize the code generation) }
  4598. { Storing address is not always an optimization: alignment of left is not known
  4599. at this point, so we must assume the worst and use an unaligned pointer.
  4600. This results in larger and slower code on alignment-sensitive targets.
  4601. Therefore the complexity condition below is questionable, maybe just filtering
  4602. out calls with "= NODE_COMPLEXITY_INF" is sufficient.
  4603. Value of 3 corresponds to subscript nodes, i.e. record field. }
  4604. if node_complexity(tcallparanode(left).left) > 3 then
  4605. begin
  4606. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  4607. addstatement(newstatement,tempnode);
  4608. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  4609. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  4610. hp := cderefnode.create(ctemprefnode.create(tempnode));
  4611. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  4612. end
  4613. else
  4614. begin
  4615. hp := tcallparanode(left).left.getcopy;
  4616. tempnode := nil;
  4617. end;
  4618. resultnode := hp.getcopy;
  4619. { Remove "modify" flag from what will be an assignment destination }
  4620. Exclude(resultnode.flags, nf_modify);
  4621. { avoid type errors from the addn/subn }
  4622. if not is_integer(resultnode.resultdef) then
  4623. begin
  4624. inserttypeconv_internal(hp,sinttype);
  4625. inserttypeconv_internal(hpp,sinttype);
  4626. end;
  4627. { addition/subtraction depending on inc/dec }
  4628. if inlinenumber = in_inc_x then
  4629. hpp := caddnode.create_internal(addn,hp,hpp)
  4630. else
  4631. hpp := caddnode.create_internal(subn,hp,hpp);
  4632. { assign result of addition }
  4633. { inherit internal flag }
  4634. if not(is_integer(resultnode.resultdef)) then
  4635. begin
  4636. if nf_internal in flags then
  4637. inserttypeconv_internal(hpp,corddef.create(
  4638. {$ifdef cpu64bitaddr}
  4639. s64bit,
  4640. {$else cpu64bitaddr}
  4641. s32bit,
  4642. {$endif cpu64bitaddr}
  4643. get_min_value(resultnode.resultdef),
  4644. get_max_value(resultnode.resultdef),
  4645. true))
  4646. else
  4647. inserttypeconv(hpp,corddef.create(
  4648. {$ifdef cpu64bitaddr}
  4649. s64bit,
  4650. {$else cpu64bitaddr}
  4651. s32bit,
  4652. {$endif cpu64bitaddr}
  4653. get_min_value(resultnode.resultdef),
  4654. get_max_value(resultnode.resultdef),
  4655. true))
  4656. end
  4657. else
  4658. begin
  4659. if nf_internal in flags then
  4660. inserttypeconv_internal(hpp,resultnode.resultdef)
  4661. else
  4662. inserttypeconv(hpp,resultnode.resultdef);
  4663. end;
  4664. { avoid any possible warnings }
  4665. inserttypeconv_internal(hpp,resultnode.resultdef);
  4666. { force pass 1, so copied trees get first pass'ed as well and flags like
  4667. nf_call_unique get set right }
  4668. node_reset_pass1_write(hpp);
  4669. do_typecheckpass(hpp);
  4670. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  4671. { firstpass it }
  4672. firstpass(tnode(newstatement.left));
  4673. { deallocate the temp }
  4674. if assigned(tempnode) then
  4675. addstatement(newstatement,ctempdeletenode.create(tempnode));
  4676. { firstpass it }
  4677. firstpass(tnode(newblock));
  4678. { return new node }
  4679. result := newblock;
  4680. end;
  4681. function tinlinenode.first_IncDec: tnode;
  4682. begin
  4683. expectloc:=LOC_VOID;
  4684. result:=nil;
  4685. { range/overflow checking doesn't work properly }
  4686. { with the inc/dec code that's generated (JM) }
  4687. if ((localswitches * [cs_check_overflow,cs_check_range] <> []) and
  4688. { No overflow check for pointer operations, because inc(pointer,-1) will always
  4689. trigger an overflow. For uint32 it works because then the operation is done
  4690. in 64bit. Range checking is not applicable to pointers either }
  4691. (tcallparanode(left).left.resultdef.typ<>pointerdef))
  4692. {$ifdef jvm}
  4693. { enums are class instances on the JVM -> special treatment }
  4694. or (tcallparanode(left).left.resultdef.typ=enumdef)
  4695. {$endif}
  4696. then
  4697. { convert to simple add (JM) }
  4698. result:=getaddsub_for_incdec
  4699. end;
  4700. function tinlinenode.first_IncludeExclude: tnode;
  4701. begin
  4702. result:=nil;
  4703. expectloc:=LOC_VOID;
  4704. end;
  4705. function tinlinenode.first_get_frame: tnode;
  4706. begin
  4707. include(current_procinfo.flags,pi_needs_stackframe);
  4708. include(current_procinfo.flags,pi_uses_get_frame);
  4709. expectloc:=LOC_CREGISTER;
  4710. result:=nil;
  4711. end;
  4712. function tinlinenode.first_setlength: tnode;
  4713. var
  4714. paras : tnode;
  4715. npara,
  4716. ppn : tcallparanode;
  4717. dims,
  4718. counter : integer;
  4719. isarray : boolean;
  4720. destppn : tnode;
  4721. newstatement : tstatementnode;
  4722. temp : ttempcreatenode;
  4723. newblock : tnode;
  4724. begin
  4725. paras:=left;
  4726. ppn:=tcallparanode(paras);
  4727. dims:=0;
  4728. while assigned(ppn.right) do
  4729. begin
  4730. inc(dims);
  4731. ppn:=tcallparanode(ppn.right);
  4732. end;
  4733. destppn:=ppn.left;
  4734. isarray:=is_dynamic_array(destppn.resultdef);
  4735. { first param must be a string or dynamic array ...}
  4736. if isarray then
  4737. begin
  4738. { SetLength(Arr, 0), Arr := nil, Arr := [] }
  4739. if (dims=1) and is_constintvalue(tcallparanode(paras).left, 0) then
  4740. begin
  4741. ppn.left:=nil; { unlink destppn }
  4742. result:=ccallparanode.create(ctypeconvnode.create_internal(destppn,voidpointertype),nil);
  4743. result:=ccallnode.createintern('fpc_dynarray_clear',
  4744. ccallparanode.create(caddrnode.create_internal(
  4745. crttinode.create(tstoreddef(destppn.resultdef),initrtti,rdt_normal)),
  4746. result));
  4747. exit;
  4748. end;
  4749. { create statements with call initialize the arguments and
  4750. call fpc_dynarr_setlength }
  4751. newblock:=internalstatements(newstatement);
  4752. { get temp for array of lengths }
  4753. temp:=ctempcreatenode.create(carraydef.getreusable(sinttype,dims),dims*sinttype.size,tt_persistent,false);
  4754. addstatement(newstatement,temp);
  4755. { load array of lengths }
  4756. ppn:=tcallparanode(paras);
  4757. counter:=dims-1;
  4758. while assigned(ppn.right) do
  4759. begin
  4760. addstatement(newstatement,cassignmentnode.create(
  4761. cvecnode.create(
  4762. ctemprefnode.create(temp),
  4763. genintconstnode(counter)
  4764. ),
  4765. ppn.left));
  4766. ppn.left:=nil;
  4767. dec(counter);
  4768. ppn:=tcallparanode(ppn.right);
  4769. end;
  4770. { destppn is also reused }
  4771. ppn.left:=nil;
  4772. { create call to fpc_dynarr_setlength }
  4773. npara:=ccallparanode.create(caddrnode.create_internal(
  4774. cvecnode.create(
  4775. ctemprefnode.create(temp),
  4776. genintconstnode(0)
  4777. )),
  4778. ccallparanode.create(cordconstnode.create
  4779. (dims,sinttype,true),
  4780. ccallparanode.create(caddrnode.create_internal
  4781. (crttinode.create(tstoreddef(destppn.resultdef),initrtti,rdt_normal)),
  4782. ccallparanode.create(ctypeconvnode.create_internal(destppn,voidpointertype),nil))));
  4783. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',npara));
  4784. addstatement(newstatement,ctempdeletenode.create(temp));
  4785. end
  4786. else if is_ansistring(destppn.resultdef) then
  4787. begin
  4788. newblock:=ccallnode.createintern(
  4789. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',
  4790. ccallparanode.create(
  4791. cordconstnode.create(getparaencoding(destppn.resultdef),u16inttype,true),
  4792. paras
  4793. )
  4794. );
  4795. { we reused the parameters, make sure we don't release them }
  4796. left:=nil;
  4797. end
  4798. else
  4799. begin
  4800. { we can reuse the supplied parameters }
  4801. newblock:=ccallnode.createintern(
  4802. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',paras);
  4803. { we reused the parameters, make sure we don't release them }
  4804. left:=nil;
  4805. end;
  4806. result:=newblock;
  4807. end;
  4808. function tinlinenode.first_copy: tnode;
  4809. var
  4810. lowppn,
  4811. countppn,
  4812. elesizeppn,
  4813. eletypeppn,
  4814. maxcountppn,
  4815. arrayppn,
  4816. rttippn,
  4817. npara,
  4818. paras : tnode;
  4819. ppn : tcallparanode;
  4820. paradef : tdef;
  4821. counter : integer;
  4822. begin
  4823. { determine copy function to use based on the first argument,
  4824. also count the number of arguments in this loop }
  4825. counter:=1;
  4826. paras:=left;
  4827. ppn:=tcallparanode(paras);
  4828. while assigned(ppn.right) do
  4829. begin
  4830. inc(counter);
  4831. ppn:=tcallparanode(ppn.right);
  4832. end;
  4833. paradef:=ppn.left.resultdef;
  4834. { fill up third parameter }
  4835. if counter=2 then
  4836. begin
  4837. paras:=ccallparanode.create(cordconstnode.create(torddef(sinttype).high,sinttype,false),paras);
  4838. counter:=3;
  4839. end;
  4840. if is_ansistring(resultdef) then
  4841. { keep the specific kind of ansistringdef as result }
  4842. result:=ccallnode.createinternres('fpc_ansistr_copy',paras,resultdef)
  4843. else if is_widestring(resultdef) then
  4844. result:=ccallnode.createintern('fpc_widestr_copy',paras)
  4845. else if is_unicodestring(resultdef) then
  4846. result:=ccallnode.createintern('fpc_unicodestr_copy',paras)
  4847. { can't check for resultdef = cansichartype, because resultdef=
  4848. cshortstringtype here }
  4849. else if is_char(paradef) then
  4850. result:=ccallnode.createintern('fpc_char_copy',paras)
  4851. else if is_dynamic_array(resultdef) then
  4852. begin
  4853. { create statements with call }
  4854. elesizeppn:=cordconstnode.create(tarraydef(paradef).elesize,sinttype,false);
  4855. if is_managed_type(tarraydef(paradef).elementdef) then
  4856. eletypeppn:=caddrnode.create_internal(
  4857. crttinode.create(tstoreddef(tarraydef(paradef).elementdef),initrtti,rdt_normal))
  4858. else
  4859. eletypeppn:=cordconstnode.create(0,voidpointertype,false);
  4860. maxcountppn:=geninlinenode(in_length_x,false,ppn.left.getcopy);
  4861. case counter of
  4862. 1:
  4863. begin
  4864. { copy the whole array using [0..high(sizeint)] range }
  4865. countppn:=cordconstnode.create(torddef(sinttype).high,sinttype,false);
  4866. lowppn:=cordconstnode.create(0,sinttype,false);
  4867. end;
  4868. 2:
  4869. begin
  4870. { copy the array using [low..high(sizeint)] range }
  4871. countppn:=cordconstnode.create(torddef(sinttype).high,sinttype,false);
  4872. lowppn:=tcallparanode(paras).left.getcopy;
  4873. end;
  4874. 3:
  4875. begin
  4876. countppn:=tcallparanode(paras).left.getcopy;
  4877. lowppn:=tcallparanode(tcallparanode(paras).right).left.getcopy;
  4878. end;
  4879. else
  4880. internalerror(2012100703);
  4881. end;
  4882. if is_open_array(paradef) then
  4883. begin
  4884. arrayppn:=caddrnode.create_internal(ppn.left);
  4885. end
  4886. else if is_dynamic_array(paradef) then
  4887. begin
  4888. arrayppn:=ctypeconvnode.create_internal(ppn.left,voidpointertype);
  4889. end
  4890. else
  4891. internalerror(2012100704);
  4892. rttippn:=caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal));
  4893. { create call to fpc_array_to_dynarray_copy }
  4894. npara:=ccallparanode.create(eletypeppn,
  4895. ccallparanode.create(elesizeppn,
  4896. ccallparanode.create(maxcountppn,
  4897. ccallparanode.create(countppn,
  4898. ccallparanode.create(lowppn,
  4899. ccallparanode.create(rttippn,
  4900. ccallparanode.create(arrayppn,nil)))))));
  4901. result:=ccallnode.createinternres('fpc_array_to_dynarray_copy',npara,resultdef);
  4902. ppn.left:=nil;
  4903. paras.free;
  4904. end
  4905. else
  4906. result:=ccallnode.createintern('fpc_shortstr_copy',paras);
  4907. { parameters are reused }
  4908. left:=nil;
  4909. end;
  4910. function tinlinenode.first_new: tnode;
  4911. var
  4912. newstatement : tstatementnode;
  4913. newblock : tblocknode;
  4914. temp : ttempcreatenode;
  4915. para : tcallparanode;
  4916. begin
  4917. { create statements with call to getmem+initialize }
  4918. newblock:=internalstatements(newstatement);
  4919. { create temp for result }
  4920. temp := ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  4921. addstatement(newstatement,temp);
  4922. { create call to fpc_getmem }
  4923. para := ccallparanode.create(cordconstnode.create
  4924. (tpointerdef(left.resultdef).pointeddef.size,s32inttype,true),nil);
  4925. addstatement(newstatement,cassignmentnode.create(
  4926. ctemprefnode.create(temp),
  4927. ccallnode.createintern('fpc_getmem',para)));
  4928. { create call to fpc_initialize }
  4929. if is_managed_type(tpointerdef(left.resultdef).pointeddef) then
  4930. begin
  4931. para := ccallparanode.create(caddrnode.create_internal(crttinode.create
  4932. (tstoreddef(tpointerdef(left.resultdef).pointeddef),initrtti,rdt_normal)),
  4933. ccallparanode.create(ctemprefnode.create
  4934. (temp),nil));
  4935. addstatement(newstatement,ccallnode.createintern('fpc_initialize',para));
  4936. end;
  4937. { the last statement should return the value as
  4938. location and type, this is done be referencing the
  4939. temp and converting it first from a persistent temp to
  4940. normal temp }
  4941. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  4942. addstatement(newstatement,ctemprefnode.create(temp));
  4943. result:=newblock;
  4944. end;
  4945. function tinlinenode.first_length: tnode;
  4946. begin
  4947. result:=nil;
  4948. if is_shortstring(left.resultdef) then
  4949. expectloc:=left.expectloc
  4950. else
  4951. begin
  4952. { ansi/wide string }
  4953. expectloc:=LOC_REGISTER;
  4954. end;
  4955. end;
  4956. function tinlinenode.first_high: tnode;
  4957. begin
  4958. result:=nil;
  4959. if not(is_dynamic_array(left.resultdef)) then
  4960. Internalerror(2019122802);
  4961. expectloc:=LOC_REGISTER;
  4962. end;
  4963. function tinlinenode.first_assigned: tnode;
  4964. begin
  4965. { Comparison must not call procvars, indicate that with nf_load_procvar flag }
  4966. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  4967. include(result.flags,nf_load_procvar);
  4968. tcallparanode(left).left:=nil;
  4969. end;
  4970. function tinlinenode.first_assert: tnode;
  4971. var
  4972. paras: tcallparanode;
  4973. begin
  4974. paras:=tcallparanode(tcallparanode(left).right);
  4975. paras:=ccallparanode.create(cstringconstnode.createstr(current_module.sourcefiles.get_file_name(current_filepos.fileindex)),paras);
  4976. paras:=ccallparanode.create(genintconstnode(fileinfo.line),paras);
  4977. {$ifdef SUPPORT_GET_FRAME}
  4978. paras:=ccallparanode.create(geninlinenode(in_get_frame,false,nil),paras);
  4979. {$else}
  4980. paras:=ccallparanode.create(ccallnode.createinternfromunit('SYSTEM','GET_FRAME',nil),paras);
  4981. {$endif}
  4982. result:=cifnode.create(cnotnode.create(tcallparanode(left).left),
  4983. ccallnode.createintern('fpc_assert',paras),nil);
  4984. include(result.flags,nf_internal);
  4985. tcallparanode(left).left:=nil;
  4986. tcallparanode(left).right:=nil;
  4987. end;
  4988. function tinlinenode.first_popcnt: tnode;
  4989. var
  4990. suffix : string;
  4991. begin
  4992. case torddef(left.resultdef).ordtype of
  4993. u8bit: suffix:='byte';
  4994. u16bit: suffix:='word';
  4995. u32bit: suffix:='dword';
  4996. u64bit: suffix:='qword';
  4997. else
  4998. internalerror(2012082601);
  4999. end;
  5000. result:=ccallnode.createintern('fpc_popcnt_'+suffix,ccallparanode.create(left,nil));
  5001. left:=nil;
  5002. end;
  5003. function tinlinenode.first_bitscan: tnode;
  5004. begin
  5005. result:=nil;
  5006. expectloc:=LOC_REGISTER;
  5007. end;
  5008. function tinlinenode.typecheck_seg: tnode;
  5009. begin
  5010. if target_info.system in systems_managed_vm then
  5011. message(parser_e_feature_unsupported_for_vm);
  5012. set_varstate(left,vs_read,[]);
  5013. result:=cordconstnode.create(0,s32inttype,false);
  5014. end;
  5015. function tinlinenode.first_seg: tnode;
  5016. begin
  5017. internalerror(200104046);
  5018. result:=nil;
  5019. end;
  5020. function tinlinenode.first_sar: tnode;
  5021. begin
  5022. result:=nil;
  5023. expectloc:=LOC_REGISTER;
  5024. {$if not defined(cpu64bitalu) and not defined(cpucg64shiftsupport)}
  5025. if is_64bitint(resultdef) then
  5026. begin
  5027. if (inlinenumber=in_sar_x) then
  5028. left:=ccallparanode.create(cordconstnode.create(1,u8inttype,false),
  5029. ccallparanode.create(left,nil));
  5030. result:=ccallnode.createintern('fpc_sarint64',left);
  5031. left:=nil;
  5032. end;
  5033. {$endif not defined(cpu64bitalu) and not defined(cpucg64shiftsupport)}
  5034. end;
  5035. function tinlinenode.handle_box: tnode;
  5036. begin
  5037. result:=nil;
  5038. if not assigned(left) or
  5039. assigned(tcallparanode(left).right) then
  5040. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalBox');
  5041. resultdef:=class_tobject;
  5042. end;
  5043. function tinlinenode.handle_unbox: tnode;
  5044. begin
  5045. result:=nil;
  5046. if not assigned(left) or
  5047. not assigned(tcallparanode(left).right) or
  5048. assigned(tcallparanode(tcallparanode(left).right).right) then
  5049. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalUnBox');
  5050. if tcallparanode(left).left.nodetype<>typen then
  5051. internalerror(2011071701);
  5052. ttypenode(tcallparanode(left).left).allowed:=true;
  5053. resultdef:=tcallparanode(left).left.resultdef;
  5054. end;
  5055. function tinlinenode.handle_insert: tnode;
  5056. procedure do_error;
  5057. begin
  5058. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,'Insert');
  5059. write_system_parameter_lists('fpc_shortstr_insert');
  5060. write_system_parameter_lists('fpc_shortstr_insert_char');
  5061. write_system_parameter_lists('fpc_unicodestr_insert');
  5062. if tf_winlikewidestring in target_info.flags then
  5063. write_system_parameter_lists('fpc_widestr_insert');
  5064. write_system_parameter_lists('fpc_ansistr_insert');
  5065. MessagePos1(fileinfo,sym_e_param_list,'Insert(Dynamic Array;var Dynamic Array;'+sinttype.typename+');');
  5066. MessagePos1(fileinfo,sym_e_param_list,'Insert(Element;var Dynamic Array;'+sinttype.typename+');');
  5067. end;
  5068. var
  5069. procname : String;
  5070. newn,
  5071. datan,
  5072. datacountn,
  5073. firstn,
  5074. secondn : tnode;
  5075. first,
  5076. second : tdef;
  5077. isconstr,
  5078. iscomparray,
  5079. iscompelem : boolean;
  5080. datatemp : ttempcreatenode;
  5081. insertblock : tblocknode;
  5082. insertstatement : tstatementnode;
  5083. begin
  5084. if not assigned(left) or
  5085. not assigned(tcallparanode(left).right) or
  5086. not assigned(tcallparanode(tcallparanode(left).right).right) or
  5087. assigned(tcallparanode(tcallparanode(tcallparanode(left).right).right).right) then
  5088. begin
  5089. do_error;
  5090. exit(cerrornode.create);
  5091. end;
  5092. { determine the correct function based on the second parameter }
  5093. firstn:=tcallparanode(tcallparanode(tcallparanode(left).right).right).left;
  5094. first:=firstn.resultdef;
  5095. secondn:=tcallparanode(tcallparanode(left).right).left;
  5096. second:=secondn.resultdef;
  5097. if is_shortstring(second) then
  5098. begin
  5099. if is_char(first) then
  5100. procname:='fpc_shortstr_insert_char'
  5101. else
  5102. procname:='fpc_shortstr_insert';
  5103. end
  5104. else if is_unicodestring(second) then
  5105. procname:='fpc_unicodestr_insert'
  5106. else if is_widestring(second) then
  5107. procname:='fpc_widestr_insert'
  5108. else if is_ansistring(second) then
  5109. procname:='fpc_ansistr_insert'
  5110. else if is_dynamic_array(second) then
  5111. begin
  5112. { The first parameter needs to be
  5113. a) a dynamic array of the same type
  5114. b) a single element of the same type
  5115. c) a static array of the same type (not Delphi compatible)
  5116. }
  5117. isconstr:=is_array_constructor(first);
  5118. iscomparray:=(first.typ=arraydef) and equal_defs(tarraydef(first).elementdef,tarraydef(second).elementdef);
  5119. iscompelem:=compare_defs(first,tarraydef(second).elementdef,niln)<>te_incompatible;
  5120. if not iscomparray
  5121. and not iscompelem
  5122. and not isconstr then
  5123. begin
  5124. CGMessagePos(fileinfo,type_e_array_required);
  5125. exit(cerrornode.create);
  5126. end;
  5127. insertblock:=internalstatements(insertstatement);
  5128. datatemp:=nil;
  5129. if iscomparray then
  5130. begin
  5131. datatemp:=ctempcreatenode.create_value(first,first.size,tt_normal,false,firstn);
  5132. addstatement(insertstatement,datatemp);
  5133. if is_dynamic_array(first) then
  5134. datan:=ctypeconvnode.create_internal(ctemprefnode.create(datatemp),voidpointertype)
  5135. else
  5136. datan:=caddrnode.create_internal(cvecnode.create(ctemprefnode.create(datatemp),cordconstnode.create(0,sizesinttype,false)));
  5137. datacountn:=cinlinenode.create(in_length_x,false,ctemprefnode.create(datatemp));
  5138. end
  5139. else if isconstr then
  5140. begin
  5141. inserttypeconv(firstn,second);
  5142. datatemp:=ctempcreatenode.create_value(second,second.size,tt_normal,false,firstn);
  5143. addstatement(insertstatement,datatemp);
  5144. datan:=ctypeconvnode.create_internal(ctemprefnode.create(datatemp),voidpointertype);
  5145. datacountn:=cinlinenode.create(in_length_x,false,ctemprefnode.create(datatemp));
  5146. end
  5147. else
  5148. begin
  5149. if is_const(firstn) then
  5150. begin
  5151. datatemp:=ctempcreatenode.create_value(tarraydef(second).elementdef,tarraydef(second).elementdef.size,tt_normal,false,firstn);
  5152. addstatement(insertstatement,datatemp);
  5153. datan:=caddrnode.create_internal(ctemprefnode.create(datatemp));
  5154. end
  5155. else
  5156. begin
  5157. datan:=firstn;
  5158. inserttypeconv(datan,tarraydef(second).elementdef);
  5159. datan:=caddrnode.create_internal(datan);
  5160. end;
  5161. datacountn:=cordconstnode.create(1,sizesinttype,false);
  5162. end;
  5163. procname:='fpc_dynarray_insert';
  5164. { recreate the parameters as array pointer, source, data, count, typeinfo }
  5165. newn:=ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(second),initrtti,rdt_normal)),
  5166. ccallparanode.create(datacountn,
  5167. ccallparanode.create(datan,
  5168. ccallparanode.create(tcallparanode(left).left,
  5169. ccallparanode.create(ctypeconvnode.create_internal(secondn,voidpointertype),nil)))));
  5170. addstatement(insertstatement,ccallnode.createintern(procname,newn));
  5171. if assigned(datatemp) then
  5172. addstatement(insertstatement,ctempdeletenode.create(datatemp));
  5173. tcallparanode(tcallparanode(tcallparanode(left).right).right).left:=nil; // insert idx
  5174. tcallparanode(tcallparanode(left).right).left:=nil; // dyn array
  5175. tcallparanode(left).left:=nil; // insert element/array
  5176. left.free;
  5177. left:=nil;
  5178. result:=insertblock;
  5179. exit; { ! }
  5180. end
  5181. else if second.typ=undefineddef then
  5182. { just pick one }
  5183. procname:='fpc_ansistr_insert'
  5184. else
  5185. begin
  5186. do_error;
  5187. exit(cerrornode.create);
  5188. end;
  5189. result:=ccallnode.createintern(procname,left);
  5190. left:=nil;
  5191. end;
  5192. function tinlinenode.handle_delete: tnode;
  5193. procedure do_error;
  5194. begin
  5195. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,'Delete');
  5196. write_system_parameter_lists('fpc_shortstr_delete');
  5197. write_system_parameter_lists('fpc_unicodestr_delete');
  5198. if tf_winlikewidestring in target_info.flags then
  5199. write_system_parameter_lists('fpc_widestr_delete');
  5200. write_system_parameter_lists('fpc_ansistr_delete');
  5201. MessagePos1(fileinfo,sym_e_param_list,'Delete(var Dynamic Array;'+sinttype.typename+';'+sinttype.typename+');');
  5202. end;
  5203. var
  5204. procname : String;
  5205. first : tdef;
  5206. firstn,
  5207. newn : tnode;
  5208. begin
  5209. if not assigned(left) or
  5210. not assigned(tcallparanode(left).right) or
  5211. not assigned(tcallparanode(tcallparanode(left).right).right) or
  5212. assigned(tcallparanode(tcallparanode(tcallparanode(left).right).right).right) then
  5213. begin
  5214. do_error;
  5215. exit(cerrornode.create);
  5216. end;
  5217. { determine the correct function based on the first parameter }
  5218. firstn:=tcallparanode(tcallparanode(tcallparanode(left).right).right).left;
  5219. first:=firstn.resultdef;
  5220. if is_shortstring(first) then
  5221. procname:='fpc_shortstr_delete'
  5222. else if is_unicodestring(first) then
  5223. procname:='fpc_unicodestr_delete'
  5224. else if is_widestring(first) then
  5225. procname:='fpc_widestr_delete'
  5226. else if is_ansistring(first) then
  5227. procname:='fpc_ansistr_delete'
  5228. else if is_dynamic_array(first) then
  5229. begin
  5230. procname:='fpc_dynarray_delete';
  5231. { recreate the parameters as array pointer, src, count, typeinfo }
  5232. newn:=ccallparanode.create(caddrnode.create_internal
  5233. (crttinode.create(tstoreddef(first),initrtti,rdt_normal)),
  5234. ccallparanode.create(tcallparanode(left).left,
  5235. ccallparanode.create(tcallparanode(tcallparanode(left).right).left,
  5236. ccallparanode.create(ctypeconvnode.create_internal(firstn,voidpointertype),nil))));
  5237. tcallparanode(tcallparanode(tcallparanode(left).right).right).left:=nil;
  5238. tcallparanode(tcallparanode(left).right).left:=nil;
  5239. tcallparanode(left).left:=nil;
  5240. left.free;
  5241. left:=newn;
  5242. end
  5243. else if first.typ=undefineddef then
  5244. { just pick one }
  5245. procname:='fpc_ansistr_delete'
  5246. else
  5247. begin
  5248. do_error;
  5249. exit(cerrornode.create);
  5250. end;
  5251. result:=ccallnode.createintern(procname,left);
  5252. left:=nil;
  5253. end;
  5254. function tinlinenode.handle_concat:tnode;
  5255. procedure do_error;
  5256. begin
  5257. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,'Concat');
  5258. MessagePos1(fileinfo,sym_e_param_list,'Concat(String[;String;...])');
  5259. MessagePos1(fileinfo,sym_e_param_list,'Concat(Dynamic Array[;Dynamic Array;...])');
  5260. end;
  5261. var
  5262. cpn : tcallparanode;
  5263. list : tfpobjectlist;
  5264. n,
  5265. arrn,
  5266. firstn : tnode;
  5267. i : longint;
  5268. arrconstr : tarrayconstructornode;
  5269. newstatement : tstatementnode;
  5270. tempnode : ttempcreatenode;
  5271. lastchanged : boolean;
  5272. begin
  5273. if not assigned(left) then
  5274. begin
  5275. do_error;
  5276. exit(cerrornode.create);
  5277. end;
  5278. result:=nil;
  5279. { the arguments are right to left, but we need to work on them from
  5280. left to right, so insert them in a list and process that from back
  5281. to front }
  5282. list:=tfpobjectlist.create(false);
  5283. { remember the last (aka first) dynamic array parameter (important
  5284. in case of array constructors) }
  5285. arrn:=nil;
  5286. cpn:=tcallparanode(left);
  5287. while assigned(cpn) do
  5288. begin
  5289. list.add(cpn.left);
  5290. if is_dynamic_array(cpn.left.resultdef) then
  5291. arrn:=cpn.left;
  5292. cpn.left:=nil;
  5293. cpn:=tcallparanode(cpn.right);
  5294. end;
  5295. if list.count=0 then
  5296. internalerror(2017100901);
  5297. firstn:=tnode(list.last);
  5298. if not assigned(firstn) then
  5299. internalerror(2017100902);
  5300. { are we dealing with strings or dynamic arrays? }
  5301. if is_dynamic_array(firstn.resultdef) or is_array_constructor(firstn.resultdef) then
  5302. begin
  5303. { try to combine all consecutive array constructors }
  5304. lastchanged:=false;
  5305. i:=0;
  5306. repeat
  5307. if lastchanged or is_array_constructor(tnode(list[i]).resultdef) then
  5308. begin
  5309. if (i<list.count-1) and is_array_constructor(tnode(list[i+1]).resultdef) then
  5310. begin
  5311. arrconstr:=tarrayconstructornode(list[i+1]);
  5312. while assigned(arrconstr.right) do
  5313. arrconstr:=tarrayconstructornode(arrconstr.right);
  5314. arrconstr.right:=tnode(list[i]);
  5315. list[i]:=list[i+1];
  5316. list.delete(i+1);
  5317. lastchanged:=true;
  5318. tnode(list[i]).resultdef:=nil;
  5319. { don't increase index! }
  5320. continue;
  5321. end;
  5322. if lastchanged then
  5323. begin
  5324. { we concatted all consecutive ones, so typecheck the new one again }
  5325. n:=tnode(list[i]);
  5326. typecheckpass(n);
  5327. list[i]:=n;
  5328. end;
  5329. lastchanged:=false;
  5330. end;
  5331. inc(i);
  5332. until i=list.count;
  5333. if list.count=1 then
  5334. begin
  5335. { no need to call the concat helper }
  5336. result:=firstn;
  5337. end
  5338. else
  5339. begin
  5340. { if we reach this point then the concat list didn't consist
  5341. solely of array constructors }
  5342. if not assigned(arrn) then
  5343. internalerror(2017101001);
  5344. result:=internalstatements(newstatement);
  5345. { generate the open array constructor for the source arrays
  5346. note: the order needs to be swapped again here! }
  5347. arrconstr:=nil;
  5348. for i:=0 to list.count-1 do
  5349. begin
  5350. n:=tnode(list[i]);
  5351. { first convert to the target type }
  5352. if not is_array_constructor(n.resultdef) then
  5353. inserttypeconv(n,arrn.resultdef);
  5354. { we need to ensure that we get a reference counted
  5355. assignement for the temp array }
  5356. tempnode:=ctempcreatenode.create(arrn.resultdef,arrn.resultdef.size,tt_persistent,true);
  5357. addstatement(newstatement,tempnode);
  5358. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),n));
  5359. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  5360. n:=ctemprefnode.create(tempnode);
  5361. { then to a plain pointer for the helper }
  5362. inserttypeconv_internal(n,voidpointertype);
  5363. arrconstr:=carrayconstructornode.create(n,arrconstr);
  5364. end;
  5365. Include(arrconstr.arrayconstructornodeflags,acnf_allow_array_constructor);
  5366. { based on the code from nopt.genmultistringadd() }
  5367. tempnode:=ctempcreatenode.create(arrn.resultdef,arrn.resultdef.size,tt_persistent,true);
  5368. addstatement(newstatement,tempnode);
  5369. { initialize the temp, since it will be passed to a
  5370. var-parameter (and finalization, which is performed by the
  5371. ttempcreate node and which takes care of the initialization
  5372. on native targets, is a noop on managed VM targets) }
  5373. if (target_info.system in systems_managed_vm) and
  5374. is_managed_type(arrn.resultdef) then
  5375. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  5376. false,
  5377. ccallparanode.create(genintconstnode(0),
  5378. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  5379. cpn:=ccallparanode.create(
  5380. arrconstr,
  5381. ccallparanode.create(
  5382. caddrnode.create_internal(crttinode.create(tstoreddef(arrn.resultdef),initrtti,rdt_normal)),
  5383. ccallparanode.create(ctypeconvnode.create_internal(ctemprefnode.create(tempnode),voidpointertype),nil))
  5384. );
  5385. addstatement(
  5386. newstatement,
  5387. ccallnode.createintern(
  5388. 'fpc_dynarray_concat_multi',
  5389. cpn
  5390. )
  5391. );
  5392. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  5393. addstatement(newstatement,ctemprefnode.create(tempnode));
  5394. end;
  5395. end
  5396. else
  5397. begin
  5398. { enforce strings }
  5399. for i:=list.count-1 downto 0 do
  5400. begin
  5401. if assigned(result) then
  5402. result:=caddnode.create(addn,result,tnode(list[i]))
  5403. else
  5404. begin
  5405. result:=tnode(list[i]);
  5406. { Force string type if it isn't yet }
  5407. if not(
  5408. (result.resultdef.typ=stringdef) or
  5409. is_chararray(result.resultdef) or
  5410. is_char(result.resultdef)
  5411. ) then
  5412. inserttypeconv(result,cshortstringtype);
  5413. end;
  5414. end;
  5415. end;
  5416. list.free;
  5417. end;
  5418. function tinlinenode.first_pack_unpack: tnode;
  5419. var
  5420. loopstatement : tstatementnode;
  5421. loop : tblocknode;
  5422. loopvar : ttempcreatenode;
  5423. tempnode,
  5424. source,
  5425. target,
  5426. index,
  5427. unpackednode,
  5428. packednode,
  5429. sourcevecindex,
  5430. targetvecindex,
  5431. loopbody : tnode;
  5432. temprangedef : tdef;
  5433. ulorange,
  5434. uhirange,
  5435. plorange,
  5436. phirange : TConstExprInt;
  5437. begin
  5438. { transform into a for loop which assigns the data of the (un)packed }
  5439. { array to the other one }
  5440. source := left;
  5441. if (inlinenumber = in_unpack_x_y_z) then
  5442. begin
  5443. target := tcallparanode(source).right;
  5444. index := tcallparanode(target).right;
  5445. packednode := tcallparanode(source).left;
  5446. unpackednode := tcallparanode(target).left;
  5447. end
  5448. else
  5449. begin
  5450. index := tcallparanode(source).right;
  5451. target := tcallparanode(index).right;
  5452. packednode := tcallparanode(target).left;
  5453. unpackednode := tcallparanode(source).left;
  5454. end;
  5455. source := tcallparanode(source).left;
  5456. target := tcallparanode(target).left;
  5457. index := tcallparanode(index).left;
  5458. loop := internalstatements(loopstatement);
  5459. loopvar := ctempcreatenode.create(
  5460. tarraydef(packednode.resultdef).rangedef,
  5461. tarraydef(packednode.resultdef).rangedef.size,
  5462. tt_persistent,true);
  5463. addstatement(loopstatement,loopvar);
  5464. { For range checking: we have to convert to an integer type (in case the index type }
  5465. { is an enum), add the index and loop variable together, convert the result }
  5466. { implicitly to an orddef with range equal to the rangedef to get range checking }
  5467. { and finally convert it explicitly back to the actual rangedef to avoid type }
  5468. { errors }
  5469. temprangedef:=nil;
  5470. getrange(unpackednode.resultdef,ulorange,uhirange);
  5471. getrange(packednode.resultdef,plorange,phirange);
  5472. { does not really need to be registered, but then we would have to
  5473. record it elsewhere so it still can be freed }
  5474. temprangedef:=corddef.create(torddef(sinttype).ordtype,ulorange,uhirange,true);
  5475. sourcevecindex := ctemprefnode.create(loopvar);
  5476. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  5477. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  5478. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  5479. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  5480. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  5481. if (inlinenumber = in_pack_x_y_z) then
  5482. begin
  5483. { swap source and target vec indices }
  5484. tempnode := sourcevecindex;
  5485. sourcevecindex := targetvecindex;
  5486. targetvecindex := tempnode;
  5487. end;
  5488. { create the assignment in the loop body }
  5489. loopbody :=
  5490. cassignmentnode.create(
  5491. cvecnode.create(target.getcopy,targetvecindex),
  5492. cvecnode.create(source.getcopy,sourcevecindex)
  5493. );
  5494. { create the actual for loop }
  5495. tempnode := cfornode.create(
  5496. ctemprefnode.create(loopvar),
  5497. cinlinenode.create(in_low_x,false,packednode.getcopy),
  5498. cinlinenode.create(in_high_x,false,packednode.getcopy),
  5499. loopbody,
  5500. false);
  5501. addstatement(loopstatement,tempnode);
  5502. { free the loop counter }
  5503. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  5504. result := loop;
  5505. end;
  5506. function tinlinenode.may_have_sideeffect_norecurse: boolean;
  5507. begin
  5508. result:=
  5509. (inlinenumber in [in_write_x,in_writeln_x,in_read_x,in_readln_x,in_str_x_string,
  5510. in_val_x,in_reset_x,in_rewrite_x,in_reset_typedfile,in_rewrite_typedfile,
  5511. in_reset_typedfile_name,in_rewrite_typedfile_name,in_settextbuf_file_x,
  5512. in_inc_x,in_dec_x,in_include_x_y,in_exclude_x_y,in_break,in_continue,in_setlength_x,
  5513. in_finalize_x,in_new_x,in_dispose_x,in_exit,in_copy_x,in_initialize_x,in_leave,in_cycle,
  5514. in_and_assign_x_y,in_or_assign_x_y,in_xor_assign_x_y,in_sar_assign_x_y,in_shl_assign_x_y,
  5515. in_shr_assign_x_y,in_rol_assign_x_y,in_ror_assign_x_y,in_neg_assign_x,in_not_assign_x]) or
  5516. ((inlinenumber = in_assert_x_y) and
  5517. (cs_do_assertion in localswitches));
  5518. end;
  5519. function tinlinenode.first_fma: tnode;
  5520. begin
  5521. CGMessage1(cg_e_function_not_support_by_selected_instruction_set,'FMA');
  5522. result:=nil;
  5523. end;
  5524. function tinlinenode.first_minmax: tnode;
  5525. begin
  5526. CGMessage1(cg_e_function_not_support_by_selected_instruction_set,'MIN/MAX');
  5527. result:=nil;
  5528. end;
  5529. //
  5530. //||||||| .merge-left.r31134
  5531. //
  5532. //{$ifdef ARM}
  5533. // {$i armtype.inc}
  5534. //{$endif ARM}
  5535. //=======
  5536. //
  5537. //{$ifdef x86}
  5538. // {$i x86type.inc}
  5539. //{$endif x86}
  5540. //{$ifdef ARM}
  5541. // {$i armtype.inc}
  5542. //{$endif ARM}
  5543. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  5544. function tinlinenode.first_ShiftRot_assign_64bitint: tnode;
  5545. var
  5546. procname: string[31];
  5547. begin
  5548. {$ifdef cpucg64shiftsupport}
  5549. if inlinenumber in [in_sar_assign_x_y,in_shl_assign_x_y,in_shr_assign_x_y] then
  5550. begin
  5551. result:=nil;
  5552. expectloc:=tcallparanode(tcallparanode(left).right).left.expectloc;
  5553. exit;
  5554. end;
  5555. {$endif cpucg64shiftsupport}
  5556. result := nil;
  5557. if is_signed(tcallparanode(left).right.resultdef) then
  5558. procname:='int64'
  5559. else
  5560. procname:='qword';
  5561. case inlinenumber of
  5562. in_sar_assign_x_y:
  5563. procname := 'fpc_sar_assign_'+procname;
  5564. in_shl_assign_x_y:
  5565. procname := 'fpc_shl_assign_'+procname;
  5566. in_shr_assign_x_y:
  5567. procname := 'fpc_shr_assign_'+procname;
  5568. in_rol_assign_x_y:
  5569. procname := 'fpc_rol_assign_'+procname;
  5570. in_ror_assign_x_y:
  5571. procname := 'fpc_ror_assign_'+procname;
  5572. else
  5573. internalerror(2017041301);
  5574. end;
  5575. result := ccallnode.createintern(procname,ccallparanode.create(tcallparanode(left).left,
  5576. ccallparanode.create(tcallparanode(tcallparanode(left).right).left,nil)));
  5577. tcallparanode(tcallparanode(left).right).left := nil;
  5578. tcallparanode(left).left := nil;
  5579. firstpass(result);
  5580. end;
  5581. {$endif not cpu64bitalu and nto cpuhighleveltarget}
  5582. function tinlinenode.first_AndOrXorShiftRot_assign: tnode;
  5583. begin
  5584. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  5585. { 64 bit ints have their own shift handling }
  5586. if is_64bit(tcallparanode(left).right.resultdef) and
  5587. (inlinenumber in [in_sar_assign_x_y,in_shl_assign_x_y,in_shr_assign_x_y,in_rol_assign_x_y,in_ror_assign_x_y]) then
  5588. result := first_ShiftRot_assign_64bitint
  5589. else
  5590. {$endif not cpu64bitalu and not cpuhighleveltarget}
  5591. begin
  5592. result:=nil;
  5593. expectloc:=tcallparanode(tcallparanode(left).right).left.expectloc;
  5594. end;
  5595. end;
  5596. function tinlinenode.first_NegNot_assign: tnode;
  5597. begin
  5598. result:=nil;
  5599. expectloc:=left.expectloc;
  5600. end;
  5601. function tinlinenode.first_cpu : tnode;
  5602. begin
  5603. Result:=nil;
  5604. internalerror(2017110101);
  5605. end;
  5606. procedure tinlinenode.CheckParameters(count: integer);
  5607. var
  5608. p: tnode;
  5609. begin
  5610. if count=1 then
  5611. begin
  5612. // Sometimes there are more callparanodes
  5613. if left is tcallparanode then
  5614. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid])
  5615. else
  5616. set_varstate(left,vs_read,[vsf_must_be_valid])
  5617. end
  5618. else
  5619. begin
  5620. p:=left;
  5621. while count>0 do
  5622. begin
  5623. set_varstate(tcallparanode(p).left,vs_read,[vsf_must_be_valid]);
  5624. p:=tcallparanode(p).right;
  5625. dec(count);
  5626. end;
  5627. end;
  5628. end;
  5629. end.