ninl.pas 208 KB

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