ninl.pas 240 KB

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