ninl.pas 205 KB

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