ninl.pas 223 KB

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