ninl.pas 205 KB

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