ninl.pas 222 KB

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