ninl.pas 208 KB

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