ninl.pas 206 KB

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