ninl.pas 187 KB

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