ninl.pas 216 KB

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