ninl.pas 253 KB

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