2
0

ninl.pas 225 KB

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