ninl.pas 210 KB

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