ninl.pas 207 KB

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