ninl.pas 222 KB

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