ninl.pas 211 KB

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