ninl.pas 222 KB

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