2
0

ninl.pas 198 KB

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