ninl.pas 209 KB

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