ninl.pas 206 KB

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