ninl.pas 184 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738
  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,cpuinfo,symtype;
  22. {$i compinnr.inc}
  23. type
  24. tinlinenode = class(tunarynode)
  25. inlinenumber : byte;
  26. constructor create(number : byte;is_const:boolean;l : tnode);virtual;
  27. constructor createintern(number : byte;is_const:boolean;l : tnode);virtual;
  28. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  29. procedure ppuwrite(ppufile:tcompilerppufile);override;
  30. function dogetcopy : tnode;override;
  31. procedure printnodeinfo(var t : text);override;
  32. function pass_1 : tnode;override;
  33. function pass_typecheck:tnode;override;
  34. function simplify(forinline : boolean): tnode;override;
  35. function docompare(p: tnode): boolean; override;
  36. { returns a node tree where the inc/dec are replaced by add/sub }
  37. function getaddsub_for_incdec : tnode;
  38. { pack and unpack are changed into for-loops by the compiler }
  39. function first_pack_unpack: tnode; virtual;
  40. property parameters : tnode read left write left;
  41. protected
  42. { All the following routines currently
  43. call compilerprocs, unless they are
  44. overridden in which case, the code
  45. generator handles them.
  46. }
  47. function first_pi: tnode ; virtual;
  48. function first_arctan_real: tnode; virtual;
  49. function first_abs_real: tnode; virtual;
  50. function first_sqr_real: tnode; virtual;
  51. function first_sqrt_real: tnode; virtual;
  52. function first_ln_real: tnode; virtual;
  53. function first_cos_real: tnode; virtual;
  54. function first_sin_real: tnode; virtual;
  55. function first_exp_real: tnode; virtual;
  56. function first_frac_real: tnode; virtual;
  57. function first_round_real: tnode; virtual;
  58. function first_trunc_real: tnode; virtual;
  59. function first_int_real: tnode; virtual;
  60. function first_abs_long: tnode; virtual;
  61. function first_IncDec: tnode; virtual;
  62. function first_IncludeExclude: tnode; virtual;
  63. function first_get_frame: tnode; virtual;
  64. function first_setlength: tnode; virtual;
  65. function first_copy: tnode; virtual;
  66. { This one by default generates an internal error, because such
  67. nodes are not generated by the parser. It's however used internally
  68. by the JVM backend to create new dynamic arrays. }
  69. function first_new: tnode; virtual;
  70. function first_length: tnode; virtual;
  71. function first_box: tnode; virtual; abstract;
  72. function first_unbox: tnode; virtual; abstract;
  73. function first_assigned: tnode; virtual;
  74. function first_assert: tnode; virtual;
  75. function first_popcnt: tnode; virtual;
  76. { override these for Seg() support }
  77. function typecheck_seg: tnode; virtual;
  78. function first_seg: tnode; virtual;
  79. function first_sar: tnode; virtual;
  80. function first_fma : tnode; virtual;
  81. {$ifndef cpu64bitalu}
  82. function first_ShiftRot_assign_64bitint: tnode; virtual;
  83. {$endif not cpu64bitalu}
  84. function first_AndOrXorShiftRot_assign: tnode; virtual;
  85. function first_NegNot_assign: tnode; virtual;
  86. private
  87. function handle_str: tnode;
  88. function handle_reset_rewrite_typed: tnode;
  89. function handle_text_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  90. function handle_typed_read_write(filepara,params:Ttertiarynode;var newstatement:Tnode):boolean;
  91. function handle_read_write: tnode;
  92. function handle_val: tnode;
  93. function handle_default: tnode;
  94. function handle_setlength: tnode;
  95. function handle_copy: tnode;
  96. function handle_box: tnode;
  97. function handle_unbox: tnode;
  98. function handle_insert:tnode;
  99. function handle_delete:tnode;
  100. end;
  101. tinlinenodeclass = class of tinlinenode;
  102. var
  103. cinlinenode : tinlinenodeclass = tinlinenode;
  104. function geninlinenode(number : byte;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. pass_1,
  111. ncal,ncon,ncnv,nadd,nld,nbas,nflw,nmem,nmat,nutils,
  112. nobjc,objcdef,
  113. cgbase,procinfo
  114. ;
  115. function geninlinenode(number : byte;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 : byte;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 : byte; 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:=ppufile.getbyte;
  139. end;
  140. procedure tinlinenode.ppuwrite(ppufile:tcompilerppufile);
  141. begin
  142. inherited ppuwrite(ppufile);
  143. ppufile.putbyte(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. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  1790. begin
  1791. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  1792. vl:=tordconstnode(tcallparanode(left).left).value;
  1793. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1794. end
  1795. else
  1796. exit;
  1797. end
  1798. else
  1799. begin
  1800. def:=left.resultdef;
  1801. vl:=1;
  1802. vl2:=tordconstnode(left).value;
  1803. end;
  1804. bits:=def.size*8;
  1805. shift:=vl.svalue and (bits-1);
  1806. case bits of
  1807. 8:
  1808. mask:=$ff;
  1809. 16:
  1810. mask:=$ffff;
  1811. 32:
  1812. mask:=$ffffffff;
  1813. 64:
  1814. mask:=qword($ffffffffffffffff);
  1815. else
  1816. mask:=qword(1 shl bits)-1;
  1817. end;
  1818. {$push}
  1819. {$r-,q-}
  1820. if shift=0 then
  1821. result:=cordconstnode.create(vl2.svalue,def,false)
  1822. else if vl2.svalue<0 then
  1823. result:=cordconstnode.create(((vl2.svalue shr shift) or (mask shl (bits-shift))) and mask,def,false)
  1824. else
  1825. result:=cordconstnode.create((vl2.svalue shr shift) and mask,def,false);
  1826. {$pop}
  1827. end
  1828. else
  1829. end;
  1830. function handle_const_rox : tnode;
  1831. var
  1832. vl,vl2 : TConstExprInt;
  1833. bits,shift: integer;
  1834. def : tdef;
  1835. begin
  1836. result:=nil;
  1837. if (left.nodetype=ordconstn) or ((left.nodetype=callparan) and (tcallparanode(left).left.nodetype=ordconstn)) then
  1838. begin
  1839. if (left.nodetype=callparan) and
  1840. assigned(tcallparanode(left).right) then
  1841. begin
  1842. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  1843. begin
  1844. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  1845. vl:=tordconstnode(tcallparanode(left).left).value;
  1846. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1847. end
  1848. else
  1849. exit;
  1850. end
  1851. else
  1852. begin
  1853. def:=left.resultdef;
  1854. vl:=1;
  1855. vl2:=tordconstnode(left).value;
  1856. end;
  1857. bits:=def.size*8;
  1858. shift:=vl.svalue and (bits-1);
  1859. {$push}
  1860. {$r-,q-}
  1861. if shift=0 then
  1862. result:=cordconstnode.create(vl2.svalue,def,false)
  1863. else
  1864. case inlinenumber of
  1865. in_ror_x,in_ror_x_y:
  1866. case def.size of
  1867. 1:
  1868. result:=cordconstnode.create(RorByte(Byte(vl2.svalue),shift),def,false);
  1869. 2:
  1870. result:=cordconstnode.create(RorWord(Word(vl2.svalue),shift),def,false);
  1871. 4:
  1872. result:=cordconstnode.create(RorDWord(DWord(vl2.svalue),shift),def,false);
  1873. 8:
  1874. result:=cordconstnode.create(RorQWord(QWord(vl2.svalue),shift),def,false);
  1875. else
  1876. internalerror(2011061903);
  1877. end;
  1878. in_rol_x,in_rol_x_y:
  1879. case def.size of
  1880. 1:
  1881. result:=cordconstnode.create(RolByte(Byte(vl2.svalue),shift),def,false);
  1882. 2:
  1883. result:=cordconstnode.create(RolWord(Word(vl2.svalue),shift),def,false);
  1884. 4:
  1885. result:=cordconstnode.create(RolDWord(DWord(vl2.svalue),shift),def,false);
  1886. 8:
  1887. result:=cordconstnode.create(RolQWord(QWord(vl2.svalue),shift),def,false);
  1888. else
  1889. internalerror(2011061902);
  1890. end;
  1891. else
  1892. internalerror(2011061901);
  1893. end;
  1894. end;
  1895. end;
  1896. var
  1897. hp : tnode;
  1898. vl,vl2 : TConstExprInt;
  1899. vr : bestreal;
  1900. begin { simplify }
  1901. result:=nil;
  1902. { handle intern constant functions in separate case }
  1903. if nf_inlineconst in flags then
  1904. begin
  1905. { no parameters? }
  1906. if not assigned(left) then
  1907. internalerror(200501231)
  1908. else
  1909. begin
  1910. vl:=0;
  1911. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1912. case left.nodetype of
  1913. realconstn :
  1914. begin
  1915. { Real functions are all handled with internproc below }
  1916. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename)
  1917. end;
  1918. ordconstn :
  1919. vl:=tordconstnode(left).value;
  1920. callparan :
  1921. begin
  1922. { both exists, else it was not generated }
  1923. vl:=tordconstnode(tcallparanode(left).left).value;
  1924. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1925. end;
  1926. else
  1927. CGMessage(parser_e_illegal_expression);
  1928. end;
  1929. case inlinenumber of
  1930. in_const_abs :
  1931. if vl.signed then
  1932. hp:=create_simplified_ord_const(abs(vl.svalue),resultdef,forinline)
  1933. else
  1934. hp:=create_simplified_ord_const(vl.uvalue,resultdef,forinline);
  1935. in_const_sqr:
  1936. if vl.signed then
  1937. hp:=create_simplified_ord_const(sqr(vl.svalue),resultdef,forinline)
  1938. else
  1939. hp:=create_simplified_ord_const(sqr(vl.uvalue),resultdef,forinline);
  1940. in_const_odd :
  1941. hp:=cordconstnode.create(qword(odd(int64(vl))),pasbool8type,true);
  1942. in_const_swap_word :
  1943. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resultdef,true);
  1944. in_const_swap_long :
  1945. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resultdef,true);
  1946. in_const_swap_qword :
  1947. hp:=cordconstnode.create((vl and $ffffffff) shl 32+(vl shr 32),left.resultdef,true);
  1948. in_const_ptr:
  1949. begin
  1950. {Don't construct pointers from negative values.}
  1951. if (vl.signed and (vl.svalue<0)) or (vl2.signed and (vl2.svalue<0)) then
  1952. cgmessage(parser_e_range_check_error);
  1953. {$if defined(i8086)}
  1954. hp:=cpointerconstnode.create((vl2.uvalue shl 16)+vl.uvalue,voidfarpointertype);
  1955. {$elseif defined(i386)}
  1956. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidnearfspointertype);
  1957. {$else}
  1958. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidpointertype);
  1959. {$endif}
  1960. end
  1961. else
  1962. internalerror(88);
  1963. end;
  1964. end;
  1965. if hp=nil then
  1966. hp:=cerrornode.create;
  1967. result:=hp;
  1968. end
  1969. else
  1970. begin
  1971. case inlinenumber of
  1972. in_lo_long,
  1973. in_hi_long,
  1974. in_lo_qword,
  1975. in_hi_qword,
  1976. in_lo_word,
  1977. in_hi_word :
  1978. begin
  1979. if left.nodetype=ordconstn then
  1980. begin
  1981. case inlinenumber of
  1982. in_lo_word :
  1983. result:=cordconstnode.create(tordconstnode(left).value and $ff,u8inttype,true);
  1984. in_hi_word :
  1985. result:=cordconstnode.create(tordconstnode(left).value shr 8,u8inttype,true);
  1986. in_lo_long :
  1987. result:=cordconstnode.create(tordconstnode(left).value and $ffff,u16inttype,true);
  1988. in_hi_long :
  1989. result:=cordconstnode.create(tordconstnode(left).value shr 16,u16inttype,true);
  1990. in_lo_qword :
  1991. result:=cordconstnode.create(tordconstnode(left).value and $ffffffff,u32inttype,true);
  1992. in_hi_qword :
  1993. result:=cordconstnode.create(tordconstnode(left).value shr 32,u32inttype,true);
  1994. end;
  1995. end;
  1996. end;
  1997. in_ord_x:
  1998. begin
  1999. case left.resultdef.typ of
  2000. orddef :
  2001. begin
  2002. case torddef(left.resultdef).ordtype of
  2003. pasbool8,
  2004. uchar:
  2005. begin
  2006. { change to byte() }
  2007. result:=ctypeconvnode.create_internal(left,u8inttype);
  2008. left:=nil;
  2009. end;
  2010. pasbool16,
  2011. uwidechar :
  2012. begin
  2013. { change to word() }
  2014. result:=ctypeconvnode.create_internal(left,u16inttype);
  2015. left:=nil;
  2016. end;
  2017. pasbool32 :
  2018. begin
  2019. { change to dword() }
  2020. result:=ctypeconvnode.create_internal(left,u32inttype);
  2021. left:=nil;
  2022. end;
  2023. pasbool64 :
  2024. begin
  2025. { change to qword() }
  2026. result:=ctypeconvnode.create_internal(left,u64inttype);
  2027. left:=nil;
  2028. end;
  2029. bool8bit:
  2030. begin
  2031. { change to shortint() }
  2032. result:=ctypeconvnode.create_internal(left,s8inttype);
  2033. left:=nil;
  2034. end;
  2035. bool16bit :
  2036. begin
  2037. { change to smallint() }
  2038. result:=ctypeconvnode.create_internal(left,s16inttype);
  2039. left:=nil;
  2040. end;
  2041. bool32bit :
  2042. begin
  2043. { change to longint() }
  2044. result:=ctypeconvnode.create_internal(left,s32inttype);
  2045. left:=nil;
  2046. end;
  2047. bool64bit :
  2048. begin
  2049. { change to int64() }
  2050. result:=ctypeconvnode.create_internal(left,s64inttype);
  2051. left:=nil;
  2052. end;
  2053. uvoid :
  2054. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2055. else
  2056. begin
  2057. { all other orddef need no transformation }
  2058. result:=left;
  2059. left:=nil;
  2060. end;
  2061. end;
  2062. end;
  2063. enumdef :
  2064. begin
  2065. result:=ctypeconvnode.create_internal(left,s32inttype);
  2066. left:=nil;
  2067. end;
  2068. pointerdef :
  2069. begin
  2070. if m_mac in current_settings.modeswitches then
  2071. begin
  2072. result:=ctypeconvnode.create_internal(left,ptruinttype);
  2073. left:=nil;
  2074. end
  2075. end;
  2076. end;
  2077. (*
  2078. if (left.nodetype=ordconstn) then
  2079. begin
  2080. result:=cordconstnode.create(
  2081. tordconstnode(left).value,sinttype,true);
  2082. end
  2083. else if (m_mac in current_settings.modeswitches) and
  2084. (left.ndoetype=pointerconstn) then
  2085. result:=cordconstnode.create(
  2086. tpointerconstnode(left).value,ptruinttype,true);
  2087. *)
  2088. end;
  2089. in_chr_byte:
  2090. begin
  2091. { convert to explicit char() }
  2092. result:=ctypeconvnode.create_internal(left,cansichartype);
  2093. left:=nil;
  2094. end;
  2095. in_length_x:
  2096. begin
  2097. case left.resultdef.typ of
  2098. stringdef :
  2099. begin
  2100. if (left.nodetype=stringconstn) then
  2101. begin
  2102. result:=cordconstnode.create(
  2103. tstringconstnode(left).len,sinttype,true);
  2104. end;
  2105. end;
  2106. orddef :
  2107. begin
  2108. { length of char is always one }
  2109. if is_char(left.resultdef) or
  2110. is_widechar(left.resultdef) then
  2111. begin
  2112. result:=cordconstnode.create(1,sinttype,false);
  2113. end
  2114. end;
  2115. arraydef :
  2116. begin
  2117. if (left.nodetype=stringconstn) then
  2118. begin
  2119. result:=cordconstnode.create(
  2120. tstringconstnode(left).len,sinttype,true);
  2121. end
  2122. else if not is_open_array(left.resultdef) and
  2123. not is_array_of_const(left.resultdef) and
  2124. not is_dynamic_array(left.resultdef) then
  2125. result:=cordconstnode.create(tarraydef(left.resultdef).highrange-
  2126. tarraydef(left.resultdef).lowrange+1,
  2127. sinttype,true);
  2128. end;
  2129. end;
  2130. end;
  2131. in_assigned_x:
  2132. begin
  2133. if is_constnode(tcallparanode(left).left) or
  2134. (tcallparanode(left).left.nodetype = pointerconstn) then
  2135. begin
  2136. { let an add node figure it out }
  2137. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  2138. tcallparanode(left).left := nil;
  2139. end;
  2140. end;
  2141. in_pred_x,
  2142. in_succ_x:
  2143. begin
  2144. case left.nodetype of
  2145. ordconstn:
  2146. begin
  2147. if inlinenumber=in_succ_x then
  2148. vl:=tordconstnode(left).value+1
  2149. else
  2150. vl:=tordconstnode(left).value-1;
  2151. if is_integer(left.resultdef) then
  2152. { the type of the original integer constant is irrelevant,
  2153. it should be automatically adapted to the new value
  2154. (except when inlining) }
  2155. result:=create_simplified_ord_const(vl,resultdef,forinline)
  2156. else
  2157. { check the range for enums, chars, booleans }
  2158. result:=cordconstnode.create(vl,left.resultdef,not(nf_internal in flags))
  2159. end;
  2160. addn,
  2161. subn:
  2162. begin
  2163. { fold succ/pred in child add/sub nodes with a constant if possible:
  2164. - no overflow/range checking
  2165. - equal types
  2166. }
  2167. if ([cs_check_overflow,cs_check_range]*current_settings.localswitches)=[] then
  2168. begin
  2169. if inlinenumber=in_succ_x then
  2170. vl:=1
  2171. else
  2172. vl:=-1;
  2173. if (taddnode(left).left.nodetype=ordconstn) and equal_defs(resultdef,taddnode(left).left.resultdef) then
  2174. begin
  2175. tordconstnode(taddnode(left).left).value:=tordconstnode(taddnode(left).left).value+vl;
  2176. result:=left;
  2177. left:=nil;
  2178. end
  2179. else if (taddnode(left).right.nodetype=ordconstn) and equal_defs(resultdef,taddnode(left).right.resultdef) then
  2180. begin
  2181. if left.nodetype=subn then
  2182. tordconstnode(taddnode(left).right).value:=tordconstnode(taddnode(left).right).value-vl
  2183. else
  2184. tordconstnode(taddnode(left).right).value:=tordconstnode(taddnode(left).right).value+vl;
  2185. result:=left;
  2186. left:=nil;
  2187. end;
  2188. end;
  2189. end;
  2190. end;
  2191. end;
  2192. in_low_x,
  2193. in_high_x:
  2194. begin
  2195. case left.resultdef.typ of
  2196. orddef,
  2197. enumdef:
  2198. begin
  2199. result:=do_lowhigh(left.resultdef);
  2200. end;
  2201. setdef:
  2202. begin
  2203. result:=do_lowhigh(tsetdef(left.resultdef).elementdef);
  2204. end;
  2205. arraydef:
  2206. begin
  2207. if (inlinenumber=in_low_x) then
  2208. begin
  2209. result:=cordconstnode.create(int64(tarraydef(
  2210. left.resultdef).lowrange),tarraydef(left.resultdef).rangedef,true);
  2211. end
  2212. else if not is_open_array(left.resultdef) and
  2213. not is_array_of_const(left.resultdef) and
  2214. not is_dynamic_array(left.resultdef) then
  2215. result:=cordconstnode.create(int64(tarraydef(left.resultdef).highrange),
  2216. tarraydef(left.resultdef).rangedef,true);
  2217. end;
  2218. stringdef:
  2219. begin
  2220. if inlinenumber=in_low_x then
  2221. begin
  2222. if is_dynamicstring(left.resultdef) and
  2223. not(cs_zerobasedstrings in current_settings.localswitches) then
  2224. result:=cordconstnode.create(1,u8inttype,false)
  2225. else
  2226. result:=cordconstnode.create(0,u8inttype,false);
  2227. end
  2228. else if not is_dynamicstring(left.resultdef) then
  2229. result:=cordconstnode.create(tstringdef(left.resultdef).len,u8inttype,true)
  2230. end;
  2231. undefineddef:
  2232. begin
  2233. result:=cordconstnode.create(0,u8inttype,false);
  2234. end;
  2235. end;
  2236. end;
  2237. in_exp_real :
  2238. begin
  2239. if left.nodetype in [ordconstn,realconstn] then
  2240. begin
  2241. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  2242. if (trealconstnode(result).value_real=MathInf.Value) and
  2243. floating_point_range_check_error then
  2244. begin
  2245. result:=crealconstnode.create(0,pbestrealtype^);
  2246. CGMessage(parser_e_range_check_error);
  2247. end;
  2248. end
  2249. end;
  2250. in_trunc_real :
  2251. begin
  2252. if left.nodetype in [ordconstn,realconstn] then
  2253. begin
  2254. vr:=getconstrealvalue;
  2255. if (vr>=9223372036854775807.99) or (vr<=-9223372036854775808.0) then
  2256. begin
  2257. message3(type_e_range_check_error_bounds,realtostr(vr),'-9223372036854775808.0','9223372036854775807.99..');
  2258. result:=cordconstnode.create(1,s64inttype,false)
  2259. end
  2260. else
  2261. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  2262. end
  2263. end;
  2264. in_round_real :
  2265. begin
  2266. { can't evaluate while inlining, may depend on fpu setting }
  2267. if (not forinline) and
  2268. (left.nodetype in [ordconstn,realconstn]) then
  2269. begin
  2270. vr:=getconstrealvalue;
  2271. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  2272. begin
  2273. message3(type_e_range_check_error_bounds,realtostr(vr),'-9223372036854775808.49..','9223372036854775807.49..');
  2274. result:=cordconstnode.create(1,s64inttype,false)
  2275. end
  2276. else
  2277. result:=cordconstnode.create(round(vr),s64inttype,true)
  2278. end
  2279. end;
  2280. in_frac_real :
  2281. begin
  2282. if left.nodetype in [ordconstn,realconstn] then
  2283. setconstrealvalue(frac(getconstrealvalue))
  2284. end;
  2285. in_int_real :
  2286. begin
  2287. if left.nodetype in [ordconstn,realconstn] then
  2288. setconstrealvalue(int(getconstrealvalue));
  2289. end;
  2290. in_pi_real :
  2291. begin
  2292. if block_type=bt_const then
  2293. setconstrealvalue(getpi)
  2294. end;
  2295. in_cos_real :
  2296. begin
  2297. if left.nodetype in [ordconstn,realconstn] then
  2298. setconstrealvalue(cos(getconstrealvalue))
  2299. end;
  2300. in_sin_real :
  2301. begin
  2302. if left.nodetype in [ordconstn,realconstn] then
  2303. setconstrealvalue(sin(getconstrealvalue))
  2304. end;
  2305. in_arctan_real :
  2306. begin
  2307. if left.nodetype in [ordconstn,realconstn] then
  2308. setconstrealvalue(arctan(getconstrealvalue))
  2309. end;
  2310. in_abs_real :
  2311. begin
  2312. if left.nodetype in [ordconstn,realconstn] then
  2313. setconstrealvalue(abs(getconstrealvalue))
  2314. end;
  2315. in_abs_long:
  2316. begin
  2317. if left.nodetype=ordconstn then
  2318. begin
  2319. if tordconstnode(left).value<0 then
  2320. result:=cordconstnode.create((-tordconstnode(left).value),resultdef,false)
  2321. else
  2322. result:=cordconstnode.create((tordconstnode(left).value),resultdef,false);
  2323. end
  2324. end;
  2325. in_sqr_real :
  2326. begin
  2327. if left.nodetype in [ordconstn,realconstn] then
  2328. setconstrealvalue(sqr(getconstrealvalue))
  2329. end;
  2330. in_sqrt_real :
  2331. begin
  2332. if left.nodetype in [ordconstn,realconstn] then
  2333. result:=handle_sqrt_const(getconstrealvalue);
  2334. end;
  2335. in_ln_real :
  2336. begin
  2337. if left.nodetype in [ordconstn,realconstn] then
  2338. result:=handle_ln_const(getconstrealvalue);
  2339. end;
  2340. in_assert_x_y :
  2341. begin
  2342. if not(cs_do_assertion in current_settings.localswitches) then
  2343. { we need a valid node, so insert a nothingn }
  2344. result:=cnothingnode.create;
  2345. end;
  2346. in_sar_x,
  2347. in_sar_x_y :
  2348. begin
  2349. result:=handle_const_sar;
  2350. end;
  2351. in_rol_x,
  2352. in_rol_x_y,
  2353. in_ror_x,
  2354. in_ror_x_y :
  2355. result:=handle_const_rox;
  2356. end;
  2357. end;
  2358. end;
  2359. function tinlinenode.pass_typecheck:tnode;
  2360. procedure setfloatresultdef;
  2361. var
  2362. hnode: tnode;
  2363. begin
  2364. { System unit declares internal functions like this:
  2365. function foo(x: valreal): valreal; [internproc: number];
  2366. Calls to such functions are initially processed by callnode,
  2367. which typechecks the arguments, possibly inserting conversion to valreal.
  2368. To handle smaller types without excess precision, we need to remove
  2369. these extra typecasts. }
  2370. if (left.nodetype=typeconvn) and
  2371. (ttypeconvnode(left).left.resultdef.typ=floatdef) and
  2372. (left.flags*[nf_explicit,nf_internal]=[]) and
  2373. (tfloatdef(ttypeconvnode(left).left.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real]) then
  2374. begin
  2375. hnode:=ttypeconvnode(left).left;
  2376. ttypeconvnode(left).left:=nil;
  2377. left.free;
  2378. left:=hnode;
  2379. resultdef:=left.resultdef;
  2380. end
  2381. else if (left.resultdef.typ=floatdef) and
  2382. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real]) then
  2383. resultdef:=left.resultdef
  2384. else
  2385. begin
  2386. if (left.nodetype <> ordconstn) then
  2387. inserttypeconv(left,pbestrealtype^);
  2388. resultdef:=pbestrealtype^;
  2389. end;
  2390. end;
  2391. procedure handle_pack_unpack;
  2392. var
  2393. source, target, index: tcallparanode;
  2394. unpackedarraydef, packedarraydef: tarraydef;
  2395. tempindex: TConstExprInt;
  2396. begin
  2397. resultdef:=voidtype;
  2398. unpackedarraydef := nil;
  2399. packedarraydef := nil;
  2400. source := tcallparanode(left);
  2401. if (inlinenumber = in_unpack_x_y_z) then
  2402. begin
  2403. target := tcallparanode(source.right);
  2404. index := tcallparanode(target.right);
  2405. { source must be a packed array }
  2406. if not is_packed_array(source.left.resultdef) then
  2407. CGMessagePos2(source.left.fileinfo,type_e_got_expected_packed_array,'1',source.left.resultdef.typename)
  2408. else
  2409. packedarraydef := tarraydef(source.left.resultdef);
  2410. { target can be any kind of array, as long as it's not packed }
  2411. if (target.left.resultdef.typ <> arraydef) or
  2412. is_packed_array(target.left.resultdef) then
  2413. CGMessagePos2(target.left.fileinfo,type_e_got_expected_unpacked_array,'2',target.left.resultdef.typename)
  2414. else
  2415. unpackedarraydef := tarraydef(target.left.resultdef);
  2416. end
  2417. else
  2418. begin
  2419. index := tcallparanode(source.right);
  2420. target := tcallparanode(index.right);
  2421. { source can be any kind of array, as long as it's not packed }
  2422. if (source.left.resultdef.typ <> arraydef) or
  2423. is_packed_array(source.left.resultdef) then
  2424. CGMessagePos2(source.left.fileinfo,type_e_got_expected_unpacked_array,'1',source.left.resultdef.typename)
  2425. else
  2426. unpackedarraydef := tarraydef(source.left.resultdef);
  2427. { target must be a packed array }
  2428. if not is_packed_array(target.left.resultdef) then
  2429. CGMessagePos2(target.left.fileinfo,type_e_got_expected_packed_array,'3',target.left.resultdef.typename)
  2430. else
  2431. packedarraydef := tarraydef(target.left.resultdef);
  2432. end;
  2433. if assigned(unpackedarraydef) then
  2434. begin
  2435. { index must be compatible with the unpacked array's indextype }
  2436. inserttypeconv(index.left,unpackedarraydef.rangedef);
  2437. { range check at compile time if possible }
  2438. if assigned(packedarraydef) and
  2439. (index.left.nodetype = ordconstn) and
  2440. not is_special_array(unpackedarraydef) then
  2441. begin
  2442. testrange(unpackedarraydef,tordconstnode(index.left).value,false,false);
  2443. tempindex := tordconstnode(index.left).value + packedarraydef.highrange-packedarraydef.lowrange;
  2444. testrange(unpackedarraydef,tempindex,false,false);
  2445. end;
  2446. end;
  2447. { source array is read and must be valid }
  2448. set_varstate(source.left,vs_read,[vsf_must_be_valid]);
  2449. { target array is written }
  2450. valid_for_assignment(target.left,true);
  2451. set_varstate(target.left,vs_written,[]);
  2452. { index in the unpacked array is read and must be valid }
  2453. set_varstate(index.left,vs_read,[vsf_must_be_valid]);
  2454. { if the size of the arrays is 0 (array of empty records), }
  2455. { do nothing }
  2456. if (source.resultdef.size = 0) then
  2457. result:=cnothingnode.create;
  2458. end;
  2459. function handle_objc_encode: tnode;
  2460. var
  2461. encodedtype: ansistring;
  2462. errordef: tdef;
  2463. begin
  2464. encodedtype:='';
  2465. if not objctryencodetype(left.resultdef,encodedtype,errordef) then
  2466. Message1(type_e_objc_type_unsupported,errordef.typename);
  2467. result:=cstringconstnode.createpchar(ansistring2pchar(encodedtype),length(encodedtype),nil);
  2468. end;
  2469. var
  2470. hightree,
  2471. hp : tnode;
  2472. temp_pnode: pnode;
  2473. begin
  2474. result:=nil;
  2475. { when handling writeln "left" contains no valid address }
  2476. if assigned(left) then
  2477. begin
  2478. if left.nodetype=callparan then
  2479. tcallparanode(left).get_paratype
  2480. else
  2481. typecheckpass(left);
  2482. end;
  2483. if not(nf_inlineconst in flags) then
  2484. begin
  2485. case inlinenumber of
  2486. in_lo_long,
  2487. in_hi_long,
  2488. in_lo_qword,
  2489. in_hi_qword,
  2490. in_lo_word,
  2491. in_hi_word :
  2492. begin
  2493. { give warning for incompatibility with tp and delphi }
  2494. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  2495. ((m_tp7 in current_settings.modeswitches) or
  2496. (m_delphi in current_settings.modeswitches)) then
  2497. CGMessage(type_w_maybe_wrong_hi_lo);
  2498. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2499. if not is_integer(left.resultdef) then
  2500. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2501. case inlinenumber of
  2502. in_lo_word,
  2503. in_hi_word :
  2504. resultdef:=u8inttype;
  2505. in_lo_long,
  2506. in_hi_long :
  2507. resultdef:=u16inttype;
  2508. in_lo_qword,
  2509. in_hi_qword :
  2510. resultdef:=u32inttype;
  2511. end;
  2512. end;
  2513. in_sizeof_x:
  2514. begin
  2515. { the constant evaluation of in_sizeof_x happens in pexpr where possible }
  2516. set_varstate(left,vs_read,[]);
  2517. if (left.resultdef.typ<>undefineddef) and
  2518. paramanager.push_high_param(vs_value,left.resultdef,current_procinfo.procdef.proccalloption) then
  2519. begin
  2520. { this should be an open array or array of const, both of
  2521. which can only be simple load nodes of parameters }
  2522. if left.nodetype<>loadn then
  2523. internalerror(2014120701);
  2524. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2525. if assigned(hightree) then
  2526. begin
  2527. hp:=caddnode.create(addn,hightree,
  2528. cordconstnode.create(1,sinttype,false));
  2529. if (left.resultdef.typ=arraydef) then
  2530. if not is_packed_array(tarraydef(left.resultdef)) then
  2531. begin
  2532. if (tarraydef(left.resultdef).elesize<>1) then
  2533. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  2534. left.resultdef).elesize,sinttype,true));
  2535. end
  2536. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  2537. begin
  2538. { no packed open array support yet }
  2539. if (hp.nodetype <> ordconstn) then
  2540. internalerror(2006081511);
  2541. hp.free;
  2542. hp := cordconstnode.create(left.resultdef.size,sinttype,true);
  2543. {
  2544. hp:=
  2545. ctypeconvnode.create_explicit(sinttype,
  2546. cmoddivnode.create(divn,
  2547. caddnode.create(addn,
  2548. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  2549. left.resultdef).elepackedbitsize,s64inttype,true)),
  2550. cordconstnode.create(a,s64inttype,true)),
  2551. cordconstnode.create(8,s64inttype,true)),
  2552. sinttype);
  2553. }
  2554. end;
  2555. result:=hp;
  2556. end;
  2557. end
  2558. else
  2559. resultdef:=sinttype;
  2560. end;
  2561. in_typeof_x:
  2562. begin
  2563. if target_info.system in systems_managed_vm then
  2564. message(parser_e_feature_unsupported_for_vm);
  2565. typecheckpass(left);
  2566. set_varstate(left,vs_read,[]);
  2567. if (left.resultdef.typ=objectdef) and
  2568. not(oo_has_vmt in tobjectdef(left.resultdef).objectoptions) then
  2569. message(type_e_typeof_requires_vmt);
  2570. resultdef:=voidpointertype;
  2571. end;
  2572. in_ord_x:
  2573. begin
  2574. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2575. case left.resultdef.typ of
  2576. orddef,
  2577. enumdef :
  2578. ;
  2579. pointerdef :
  2580. begin
  2581. if not(m_mac in current_settings.modeswitches) then
  2582. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2583. end
  2584. else
  2585. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2586. end;
  2587. end;
  2588. in_chr_byte:
  2589. begin
  2590. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2591. end;
  2592. in_length_x:
  2593. begin
  2594. if ((left.resultdef.typ=arraydef) and
  2595. (not is_special_array(left.resultdef) or
  2596. is_open_array(left.resultdef))) or
  2597. (left.resultdef.typ=orddef) then
  2598. set_varstate(left,vs_read,[])
  2599. else
  2600. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2601. case left.resultdef.typ of
  2602. variantdef:
  2603. begin
  2604. inserttypeconv(left,getansistringdef);
  2605. end;
  2606. stringdef :
  2607. begin
  2608. { we don't need string convertions here, }
  2609. { except if from widestring to ansistring }
  2610. { and vice versa (that can change the }
  2611. { length) }
  2612. if (left.nodetype=typeconvn) and
  2613. (ttypeconvnode(left).left.resultdef.typ=stringdef) and
  2614. not(is_wide_or_unicode_string(left.resultdef) xor
  2615. is_wide_or_unicode_string(ttypeconvnode(left).left.resultdef)) then
  2616. begin
  2617. hp:=ttypeconvnode(left).left;
  2618. ttypeconvnode(left).left:=nil;
  2619. left.free;
  2620. left:=hp;
  2621. end;
  2622. end;
  2623. orddef :
  2624. begin
  2625. { will be handled in simplify }
  2626. if not is_char(left.resultdef) and
  2627. not is_widechar(left.resultdef) then
  2628. CGMessage(type_e_mismatch);
  2629. end;
  2630. pointerdef :
  2631. begin
  2632. if is_pchar(left.resultdef) then
  2633. begin
  2634. hp := ccallparanode.create(left,nil);
  2635. result := ccallnode.createintern('fpc_pchar_length',hp);
  2636. { make sure the left node doesn't get disposed, since it's }
  2637. { reused in the new node (JM) }
  2638. left:=nil;
  2639. exit;
  2640. end
  2641. else if is_pwidechar(left.resultdef) then
  2642. begin
  2643. hp := ccallparanode.create(left,nil);
  2644. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  2645. { make sure the left node doesn't get disposed, since it's }
  2646. { reused in the new node (JM) }
  2647. left:=nil;
  2648. exit;
  2649. end
  2650. else
  2651. CGMessage(type_e_mismatch);
  2652. end;
  2653. arraydef :
  2654. begin
  2655. if is_open_array(left.resultdef) or
  2656. is_array_of_const(left.resultdef) then
  2657. begin
  2658. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2659. if assigned(hightree) then
  2660. result:=caddnode.create(addn,hightree,
  2661. cordconstnode.create(1,sinttype,false));
  2662. exit;
  2663. end
  2664. { Length() for dynamic arrays is inlined }
  2665. else
  2666. begin
  2667. { will be handled in simplify }
  2668. end;
  2669. end;
  2670. undefineddef :
  2671. begin
  2672. if not (df_generic in current_procinfo.procdef.defoptions) then
  2673. CGMessage(type_e_mismatch);
  2674. { otherwise nothing }
  2675. end;
  2676. else
  2677. CGMessage(type_e_mismatch);
  2678. end;
  2679. { shortstring return an 8 bit value as the length
  2680. is the first byte of the string }
  2681. if is_shortstring(left.resultdef) then
  2682. resultdef:=u8inttype
  2683. else
  2684. resultdef:=ossinttype;
  2685. end;
  2686. in_typeinfo_x:
  2687. begin
  2688. if target_info.system in systems_managed_vm then
  2689. message(parser_e_feature_unsupported_for_vm);
  2690. if (left.resultdef.typ=enumdef) and
  2691. (tenumdef(left.resultdef).has_jumps) then
  2692. CGMessage(type_e_no_type_info);
  2693. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2694. resultdef:=voidpointertype;
  2695. end;
  2696. in_assigned_x:
  2697. begin
  2698. { the parser has already made sure the expression is valid }
  2699. { in case of a complex procvar, only check the "code" pointer }
  2700. if (tcallparanode(left).left.resultdef.typ=procvardef) and
  2701. not tprocvardef(tcallparanode(left).left.resultdef).is_addressonly then
  2702. begin
  2703. inserttypeconv_explicit(tcallparanode(left).left,search_system_type('TMETHOD').typedef);
  2704. tcallparanode(left).left:=csubscriptnode.create(tsym(tabstractrecorddef(tcallparanode(left).left.resultdef).symtable.find('CODE')),tcallparanode(left).left);
  2705. tcallparanode(left).get_paratype;
  2706. end;
  2707. { Postpone conversion into addnode until firstpass, so targets
  2708. may override first_assigned and insert specific code. }
  2709. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2710. resultdef:=pasbool8type;
  2711. end;
  2712. in_ofs_x :
  2713. internalerror(2000101001);
  2714. in_seg_x :
  2715. begin
  2716. result := typecheck_seg;
  2717. end;
  2718. in_pred_x,
  2719. in_succ_x:
  2720. begin
  2721. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2722. resultdef:=left.resultdef;
  2723. if is_ordinal(resultdef) or is_typeparam(resultdef) then
  2724. begin
  2725. if (resultdef.typ=enumdef) and
  2726. (tenumdef(resultdef).has_jumps) and
  2727. not(m_delphi in current_settings.modeswitches) and
  2728. not(nf_internal in flags) then
  2729. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  2730. end
  2731. else
  2732. CGMessage(type_e_ordinal_expr_expected)
  2733. end;
  2734. in_copy_x:
  2735. result:=handle_copy;
  2736. in_initialize_x,
  2737. in_finalize_x:
  2738. begin
  2739. { inlined from pinline }
  2740. internalerror(200204231);
  2741. end;
  2742. in_setlength_x:
  2743. begin
  2744. result:=handle_setlength;
  2745. end;
  2746. in_inc_x,
  2747. in_dec_x:
  2748. begin
  2749. resultdef:=voidtype;
  2750. if not(df_generic in current_procinfo.procdef.defoptions) then
  2751. begin
  2752. if assigned(left) then
  2753. begin
  2754. { first param must be var }
  2755. valid_for_var(tcallparanode(left).left,true);
  2756. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2757. if (left.resultdef.typ in [enumdef,pointerdef]) or
  2758. is_ordinal(left.resultdef) or
  2759. is_currency(left.resultdef) then
  2760. begin
  2761. { value of left gets changed -> must be unique }
  2762. set_unique(tcallparanode(left).left);
  2763. { two paras ? }
  2764. if assigned(tcallparanode(left).right) then
  2765. begin
  2766. if is_integer(tcallparanode(left).right.resultdef) then
  2767. begin
  2768. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2769. { when range/overflow checking is on, we
  2770. convert this to a regular add, and for proper
  2771. checking we need the original type }
  2772. if ([cs_check_range,cs_check_overflow]*current_settings.localswitches=[]) then
  2773. if (tcallparanode(left).left.resultdef.typ=pointerdef) then
  2774. begin
  2775. { don't convert values added to pointers into the pointer types themselves,
  2776. because that will turn signed values into unsigned ones, which then
  2777. goes wrong when they have to be multiplied with the size of the elements
  2778. to which the pointer points in ncginl (mantis #17342) }
  2779. if is_signed(tcallparanode(tcallparanode(left).right).left.resultdef) then
  2780. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tpointerdef(tcallparanode(left).left.resultdef).pointer_arithmetic_int_type)
  2781. else
  2782. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tpointerdef(tcallparanode(left).left.resultdef).pointer_arithmetic_uint_type)
  2783. end
  2784. else if is_integer(tcallparanode(left).left.resultdef) then
  2785. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef)
  2786. else
  2787. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  2788. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2789. { should be handled in the parser (JM) }
  2790. internalerror(2006020901);
  2791. end
  2792. else
  2793. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  2794. end;
  2795. end
  2796. { generic type parameter? }
  2797. else if is_typeparam(left.resultdef) then
  2798. begin
  2799. result:=cnothingnode.create;
  2800. exit;
  2801. end
  2802. else
  2803. begin
  2804. hp:=self;
  2805. if isunaryoverloaded(hp) then
  2806. begin
  2807. { inc(rec) and dec(rec) assigns result value to argument }
  2808. result:=cassignmentnode.create(tcallparanode(left).left.getcopy,hp);
  2809. exit;
  2810. end
  2811. else
  2812. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  2813. end;
  2814. end
  2815. else
  2816. CGMessagePos(fileinfo,type_e_mismatch);
  2817. end;
  2818. end;
  2819. in_and_assign_x_y,
  2820. in_or_assign_x_y,
  2821. in_xor_assign_x_y,
  2822. in_sar_assign_x_y,
  2823. in_shl_assign_x_y,
  2824. in_shr_assign_x_y,
  2825. in_rol_assign_x_y,
  2826. in_ror_assign_x_y:
  2827. begin
  2828. resultdef:=voidtype;
  2829. if not(df_generic in current_procinfo.procdef.defoptions) then
  2830. begin
  2831. { first parameter must exist }
  2832. if not assigned(left) or (left.nodetype<>callparan) then
  2833. internalerror(2017032501);
  2834. { second parameter must exist }
  2835. if not assigned(tcallparanode(left).right) or (tcallparanode(left).right.nodetype<>callparan) then
  2836. internalerror(2017032502);
  2837. { third parameter must NOT exist }
  2838. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2839. internalerror(2017032503);
  2840. valid_for_var(tcallparanode(tcallparanode(left).right).left,true);
  2841. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_readwritten,[vsf_must_be_valid]);
  2842. if is_integer(tcallparanode(left).right.resultdef) then
  2843. begin
  2844. { value of right gets changed -> must be unique }
  2845. set_unique(tcallparanode(tcallparanode(left).right).left);
  2846. if is_integer(left.resultdef) then
  2847. begin
  2848. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2849. { these nodes shouldn't be created, when range checking is on }
  2850. if [cs_check_range,cs_check_overflow]*current_settings.localswitches<>[] then
  2851. internalerror(2017032701);
  2852. 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
  2853. inserttypeconv(tcallparanode(left).left,sinttype)
  2854. else
  2855. inserttypeconv(tcallparanode(left).left,tcallparanode(tcallparanode(left).right).left.resultdef);
  2856. end
  2857. else
  2858. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  2859. end
  2860. { generic type parameter? }
  2861. else if is_typeparam(tcallparanode(left).right.resultdef) then
  2862. begin
  2863. result:=cnothingnode.create;
  2864. exit;
  2865. end
  2866. else
  2867. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  2868. end;
  2869. end;
  2870. in_neg_assign_x,
  2871. in_not_assign_x:
  2872. begin
  2873. resultdef:=voidtype;
  2874. if not(df_generic in current_procinfo.procdef.defoptions) then
  2875. begin
  2876. valid_for_var(left,true);
  2877. set_varstate(left,vs_readwritten,[vsf_must_be_valid]);
  2878. if is_integer(left.resultdef) then
  2879. begin
  2880. { value of left gets changed -> must be unique }
  2881. set_unique(left);
  2882. { these nodes shouldn't be created, when range checking is on }
  2883. if [cs_check_range,cs_check_overflow]*current_settings.localswitches<>[] then
  2884. internalerror(2017040703);
  2885. end
  2886. { generic type parameter? }
  2887. else if is_typeparam(left.resultdef) then
  2888. begin
  2889. result:=cnothingnode.create;
  2890. exit;
  2891. end
  2892. else
  2893. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  2894. end;
  2895. end;
  2896. in_read_x,
  2897. in_readln_x,
  2898. in_readstr_x,
  2899. in_write_x,
  2900. in_writeln_x,
  2901. in_writestr_x :
  2902. begin
  2903. result := handle_read_write;
  2904. end;
  2905. in_settextbuf_file_x :
  2906. begin
  2907. if target_info.system in systems_managed_vm then
  2908. message(parser_e_feature_unsupported_for_vm);
  2909. resultdef:=voidtype;
  2910. { now we know the type of buffer }
  2911. hp:=ccallparanode.create(cordconstnode.create(
  2912. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  2913. result:=ccallnode.createintern('SETTEXTBUF',hp);
  2914. left:=nil;
  2915. end;
  2916. { the firstpass of the arg has been done in firstcalln ? }
  2917. in_reset_typedfile,
  2918. in_rewrite_typedfile,
  2919. in_reset_typedfile_name,
  2920. in_rewrite_typedfile_name :
  2921. begin
  2922. result := handle_reset_rewrite_typed;
  2923. end;
  2924. in_str_x_string :
  2925. begin
  2926. result:=handle_str;
  2927. end;
  2928. in_val_x :
  2929. begin
  2930. result:=handle_val;
  2931. end;
  2932. in_include_x_y,
  2933. in_exclude_x_y:
  2934. begin
  2935. resultdef:=voidtype;
  2936. { the parser already checks whether we have two (and exactly two) }
  2937. { parameters (JM) }
  2938. { first param must be var }
  2939. valid_for_var(tcallparanode(left).left,true);
  2940. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2941. { check type }
  2942. if (left.resultdef.typ=setdef) then
  2943. begin
  2944. { insert a type conversion }
  2945. { to the type of the set elements }
  2946. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2947. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  2948. tsetdef(left.resultdef).elementdef);
  2949. end
  2950. else
  2951. CGMessage(type_e_mismatch);
  2952. end;
  2953. in_pack_x_y_z,
  2954. in_unpack_x_y_z :
  2955. begin
  2956. handle_pack_unpack;
  2957. end;
  2958. in_slice_x:
  2959. begin
  2960. if target_info.system in systems_managed_vm then
  2961. message(parser_e_feature_unsupported_for_vm);
  2962. result:=nil;
  2963. resultdef:=tcallparanode(left).left.resultdef;
  2964. if (resultdef.typ <> arraydef) then
  2965. CGMessagePos(left.fileinfo,type_e_mismatch)
  2966. else if is_packed_array(resultdef) then
  2967. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  2968. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  2969. CGMessagePos1(tcallparanode(left).right.fileinfo,
  2970. type_e_integer_expr_expected,
  2971. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  2972. end;
  2973. in_new_x:
  2974. resultdef:=left.resultdef;
  2975. in_low_x,
  2976. in_high_x:
  2977. begin
  2978. case left.resultdef.typ of
  2979. undefineddef,
  2980. orddef,
  2981. enumdef,
  2982. setdef:
  2983. ;
  2984. arraydef:
  2985. begin
  2986. if (inlinenumber=in_low_x) then
  2987. set_varstate(left,vs_read,[])
  2988. else
  2989. begin
  2990. if is_open_array(left.resultdef) or
  2991. is_array_of_const(left.resultdef) then
  2992. begin
  2993. set_varstate(left,vs_read,[]);
  2994. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2995. end
  2996. else
  2997. if is_dynamic_array(left.resultdef) then
  2998. begin
  2999. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3000. { can't use inserttypeconv because we need }
  3001. { an explicit type conversion (JM) }
  3002. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  3003. result := ccallnode.createintern('fpc_dynarray_high',hp);
  3004. { make sure the left node doesn't get disposed, since it's }
  3005. { reused in the new node (JM) }
  3006. left:=nil;
  3007. end
  3008. else
  3009. begin
  3010. set_varstate(left,vs_read,[]);
  3011. end;
  3012. end;
  3013. end;
  3014. stringdef:
  3015. begin
  3016. if inlinenumber=in_low_x then
  3017. begin
  3018. set_varstate(left,vs_read,[]);
  3019. end
  3020. else
  3021. begin
  3022. if is_open_string(left.resultdef) then
  3023. begin
  3024. set_varstate(left,vs_read,[]);
  3025. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  3026. end
  3027. else if is_dynamicstring(left.resultdef) then
  3028. begin
  3029. result:=cinlinenode.create(in_length_x,false,left);
  3030. if cs_zerobasedstrings in current_settings.localswitches then
  3031. result:=caddnode.create(subn,result,cordconstnode.create(1,sinttype,false));
  3032. { make sure the left node doesn't get disposed, since it's }
  3033. { reused in the new node (JM) }
  3034. left:=nil;
  3035. end
  3036. end;
  3037. end;
  3038. else
  3039. CGMessage(type_e_mismatch);
  3040. end;
  3041. end;
  3042. in_exp_real,
  3043. in_frac_real,
  3044. in_int_real,
  3045. in_cos_real,
  3046. in_sin_real,
  3047. in_arctan_real,
  3048. in_ln_real :
  3049. begin
  3050. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3051. { converting an int64 to double on platforms without }
  3052. { extended can cause precision loss }
  3053. if not(left.nodetype in [ordconstn,realconstn]) then
  3054. inserttypeconv(left,pbestrealtype^);
  3055. resultdef:=pbestrealtype^;
  3056. end;
  3057. in_trunc_real,
  3058. in_round_real :
  3059. begin
  3060. { on i8086, the int64 result is returned in a var param, because
  3061. it's too big to fit in a register or a pair of registers. In
  3062. that case we have 2 parameters and left.nodetype is a callparan. }
  3063. if left.nodetype = callparan then
  3064. temp_pnode := @tcallparanode(left).left
  3065. else
  3066. temp_pnode := @left;
  3067. set_varstate(temp_pnode^,vs_read,[vsf_must_be_valid]);
  3068. { for direct float rounding, no best real type cast should be necessary }
  3069. if not((temp_pnode^.resultdef.typ=floatdef) and
  3070. (tfloatdef(temp_pnode^.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real])) and
  3071. { converting an int64 to double on platforms without }
  3072. { extended can cause precision loss }
  3073. not(temp_pnode^.nodetype in [ordconstn,realconstn]) then
  3074. inserttypeconv(temp_pnode^,pbestrealtype^);
  3075. resultdef:=s64inttype;
  3076. end;
  3077. in_pi_real :
  3078. begin
  3079. resultdef:=pbestrealtype^;
  3080. end;
  3081. in_abs_long:
  3082. begin
  3083. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3084. resultdef:=left.resultdef;
  3085. end;
  3086. in_abs_real,
  3087. in_sqr_real,
  3088. in_sqrt_real :
  3089. begin
  3090. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3091. setfloatresultdef;
  3092. end;
  3093. {$ifdef SUPPORT_MMX}
  3094. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  3095. begin
  3096. end;
  3097. {$endif SUPPORT_MMX}
  3098. in_aligned_x,
  3099. in_unaligned_x:
  3100. begin
  3101. resultdef:=left.resultdef;
  3102. end;
  3103. in_assert_x_y :
  3104. begin
  3105. resultdef:=voidtype;
  3106. if assigned(left) then
  3107. begin
  3108. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  3109. { check type }
  3110. if is_boolean(left.resultdef) or
  3111. (
  3112. (left.resultdef.typ=undefineddef) and
  3113. (df_generic in current_procinfo.procdef.defoptions)
  3114. ) then
  3115. begin
  3116. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  3117. { must always be a string }
  3118. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  3119. end
  3120. else
  3121. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  3122. end
  3123. else
  3124. CGMessage(type_e_mismatch);
  3125. if (cs_do_assertion in current_settings.localswitches) then
  3126. include(current_procinfo.flags,pi_do_call);
  3127. end;
  3128. in_prefetch_var:
  3129. resultdef:=voidtype;
  3130. in_get_frame,
  3131. in_get_caller_frame,
  3132. in_get_caller_addr:
  3133. begin
  3134. resultdef:=voidpointertype;
  3135. end;
  3136. in_rol_x,
  3137. in_ror_x,
  3138. in_sar_x:
  3139. begin
  3140. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3141. resultdef:=left.resultdef;
  3142. end;
  3143. in_rol_x_y,
  3144. in_ror_x_y,
  3145. in_sar_x_y:
  3146. begin
  3147. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  3148. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  3149. resultdef:=tcallparanode(tcallparanode(left).right).left.resultdef;
  3150. end;
  3151. in_bsf_x,
  3152. in_bsr_x:
  3153. begin
  3154. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3155. if not is_integer(left.resultdef) then
  3156. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  3157. if torddef(left.resultdef).ordtype in [u64bit, s64bit] then
  3158. resultdef:=u64inttype
  3159. else
  3160. resultdef:=u32inttype
  3161. end;
  3162. in_popcnt_x:
  3163. begin
  3164. set_varstate(left,vs_read,[vsf_must_be_valid]);
  3165. if not is_integer(left.resultdef) then
  3166. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  3167. resultdef:=left.resultdef;
  3168. end;
  3169. in_objc_selector_x:
  3170. begin
  3171. result:=cobjcselectornode.create(left);
  3172. { reused }
  3173. left:=nil;
  3174. end;
  3175. in_objc_protocol_x:
  3176. begin
  3177. result:=cobjcprotocolnode.create(left);
  3178. { reused }
  3179. left:=nil;
  3180. end;
  3181. in_objc_encode_x:
  3182. begin
  3183. result:=handle_objc_encode;
  3184. end;
  3185. in_default_x:
  3186. begin
  3187. result:=handle_default;
  3188. end;
  3189. in_box_x:
  3190. begin
  3191. result:=handle_box;
  3192. end;
  3193. in_unbox_x_y:
  3194. begin
  3195. result:=handle_unbox;
  3196. end;
  3197. in_fma_single,
  3198. in_fma_double,
  3199. in_fma_extended,
  3200. in_fma_float128:
  3201. begin
  3202. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  3203. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  3204. set_varstate(tcallparanode(tcallparanode(tcallparanode(left).right).right).left,vs_read,[vsf_must_be_valid]);
  3205. resultdef:=tcallparanode(left).left.resultdef;
  3206. end;
  3207. in_delete_x_y_z:
  3208. begin
  3209. result:=handle_delete;
  3210. end;
  3211. in_insert_x_y_z:
  3212. begin
  3213. result:=handle_insert;
  3214. end;
  3215. else
  3216. internalerror(8);
  3217. end;
  3218. end;
  3219. if not assigned(result) and not
  3220. codegenerror then
  3221. result:=simplify(false);
  3222. end;
  3223. function tinlinenode.pass_1 : tnode;
  3224. var
  3225. hp: tnode;
  3226. shiftconst: longint;
  3227. objdef: tobjectdef;
  3228. begin
  3229. result:=nil;
  3230. { if we handle writeln; left contains no valid address }
  3231. if assigned(left) then
  3232. begin
  3233. if left.nodetype=callparan then
  3234. tcallparanode(left).firstcallparan
  3235. else
  3236. firstpass(left);
  3237. end;
  3238. { intern const should already be handled }
  3239. if nf_inlineconst in flags then
  3240. internalerror(200104044);
  3241. case inlinenumber of
  3242. in_lo_qword,
  3243. in_hi_qword,
  3244. in_lo_long,
  3245. in_hi_long,
  3246. in_lo_word,
  3247. in_hi_word:
  3248. begin
  3249. shiftconst := 0;
  3250. case inlinenumber of
  3251. in_hi_qword:
  3252. shiftconst := 32;
  3253. in_hi_long:
  3254. shiftconst := 16;
  3255. in_hi_word:
  3256. shiftconst := 8;
  3257. end;
  3258. if shiftconst <> 0 then
  3259. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  3260. cordconstnode.create(shiftconst,sinttype,false)),resultdef)
  3261. else
  3262. result := ctypeconvnode.create_internal(left,resultdef);
  3263. left := nil;
  3264. firstpass(result);
  3265. end;
  3266. in_sizeof_x,
  3267. in_typeof_x:
  3268. begin
  3269. expectloc:=LOC_REGISTER;
  3270. case left.resultdef.typ of
  3271. objectdef,classrefdef:
  3272. begin
  3273. if left.resultdef.typ=objectdef then
  3274. begin
  3275. result:=cloadvmtaddrnode.create(left);
  3276. objdef:=tobjectdef(left.resultdef);
  3277. end
  3278. else
  3279. begin
  3280. result:=left;
  3281. objdef:=tobjectdef(tclassrefdef(left.resultdef).pointeddef);
  3282. end;
  3283. left:=nil;
  3284. if inlinenumber=in_sizeof_x then
  3285. begin
  3286. inserttypeconv_explicit(result,cpointerdef.getreusable(objdef.vmt_def));
  3287. result:=cderefnode.create(result);
  3288. result:=genloadfield(result,'VINSTANCESIZE');
  3289. end
  3290. else
  3291. inserttypeconv_explicit(result,voidpointertype);
  3292. end;
  3293. undefineddef:
  3294. ;
  3295. else
  3296. internalerror(2015122702);
  3297. end;
  3298. end;
  3299. in_length_x:
  3300. begin
  3301. result:=first_length;
  3302. end;
  3303. in_typeinfo_x:
  3304. begin
  3305. result:=caddrnode.create_internal(
  3306. crttinode.create(tstoreddef(left.resultdef),fullrtti,rdt_normal)
  3307. );
  3308. end;
  3309. in_assigned_x:
  3310. begin
  3311. result:=first_assigned;
  3312. end;
  3313. in_pred_x,
  3314. in_succ_x:
  3315. begin
  3316. expectloc:=LOC_REGISTER;
  3317. { in case of range/overflow checking, use a regular addnode
  3318. because it's too complex to handle correctly otherwise }
  3319. {$ifndef jvm}
  3320. { enums are class instances in the JVM -> always need conversion }
  3321. if (([cs_check_overflow,cs_check_range]*current_settings.localswitches)<>[]) and not(nf_internal in flags) then
  3322. {$endif}
  3323. begin
  3324. { create constant 1 }
  3325. hp:=cordconstnode.create(1,left.resultdef,false);
  3326. typecheckpass(hp);
  3327. if not is_integer(hp.resultdef) then
  3328. inserttypeconv_internal(hp,sinttype);
  3329. { avoid type errors from the addn/subn }
  3330. if not is_integer(left.resultdef) then
  3331. inserttypeconv_internal(left,sinttype);
  3332. { addition/substraction depending on succ/pred }
  3333. if inlinenumber=in_succ_x then
  3334. hp:=caddnode.create(addn,left,hp)
  3335. else
  3336. hp:=caddnode.create(subn,left,hp);
  3337. { the condition above is not tested for jvm, so we need to avoid overflow checks here
  3338. by setting nf_internal for the add/sub node as well }
  3339. if nf_internal in flags then
  3340. include(hp.flags,nf_internal);
  3341. { assign result of addition }
  3342. if not(is_integer(resultdef)) then
  3343. inserttypeconv(hp,corddef.create(
  3344. {$ifdef cpu64bitaddr}
  3345. s64bit,
  3346. {$else cpu64bitaddr}
  3347. s32bit,
  3348. {$endif cpu64bitaddr}
  3349. get_min_value(resultdef),
  3350. get_max_value(resultdef),
  3351. true))
  3352. else
  3353. inserttypeconv(hp,resultdef);
  3354. if nf_internal in flags then
  3355. include(hp.flags,nf_internal);
  3356. { avoid any possible errors/warnings }
  3357. inserttypeconv_internal(hp,resultdef);
  3358. { firstpass it }
  3359. firstpass(hp);
  3360. { left is reused }
  3361. left:=nil;
  3362. { return new node }
  3363. result:=hp;
  3364. end;
  3365. end;
  3366. in_setlength_x:
  3367. result:=first_setlength;
  3368. in_copy_x:
  3369. result:=first_copy;
  3370. in_initialize_x,
  3371. in_finalize_x:
  3372. begin
  3373. expectloc:=LOC_VOID;
  3374. end;
  3375. in_inc_x,
  3376. in_dec_x:
  3377. begin
  3378. result:=first_IncDec;
  3379. end;
  3380. in_and_assign_x_y,
  3381. in_or_assign_x_y,
  3382. in_xor_assign_x_y,
  3383. in_sar_assign_x_y,
  3384. in_shl_assign_x_y,
  3385. in_shr_assign_x_y,
  3386. in_rol_assign_x_y,
  3387. in_ror_assign_x_y:
  3388. begin
  3389. result:=first_AndOrXorShiftRot_assign;
  3390. end;
  3391. in_neg_assign_x,
  3392. in_not_assign_x:
  3393. begin
  3394. result:=first_NegNot_assign;
  3395. end;
  3396. in_include_x_y,
  3397. in_exclude_x_y:
  3398. begin
  3399. result:=first_IncludeExclude;
  3400. end;
  3401. in_pack_x_y_z,
  3402. in_unpack_x_y_z:
  3403. begin
  3404. result:=first_pack_unpack;
  3405. end;
  3406. in_exp_real:
  3407. begin
  3408. result:= first_exp_real;
  3409. end;
  3410. in_round_real:
  3411. begin
  3412. result:= first_round_real;
  3413. end;
  3414. in_trunc_real:
  3415. begin
  3416. result:= first_trunc_real;
  3417. end;
  3418. in_int_real:
  3419. begin
  3420. result:= first_int_real;
  3421. end;
  3422. in_frac_real:
  3423. begin
  3424. result:= first_frac_real;
  3425. end;
  3426. in_cos_real:
  3427. begin
  3428. result:= first_cos_real;
  3429. end;
  3430. in_sin_real:
  3431. begin
  3432. result := first_sin_real;
  3433. end;
  3434. in_arctan_real:
  3435. begin
  3436. result := first_arctan_real;
  3437. end;
  3438. in_pi_real :
  3439. begin
  3440. result := first_pi;
  3441. end;
  3442. in_abs_real:
  3443. begin
  3444. result := first_abs_real;
  3445. end;
  3446. in_abs_long:
  3447. begin
  3448. result := first_abs_long;
  3449. end;
  3450. in_sqr_real:
  3451. begin
  3452. result := first_sqr_real;
  3453. end;
  3454. in_sqrt_real:
  3455. begin
  3456. result := first_sqrt_real;
  3457. end;
  3458. in_ln_real:
  3459. begin
  3460. result := first_ln_real;
  3461. end;
  3462. {$ifdef SUPPORT_MMX}
  3463. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  3464. begin
  3465. end;
  3466. {$endif SUPPORT_MMX}
  3467. in_assert_x_y :
  3468. begin
  3469. result:=first_assert;
  3470. end;
  3471. in_low_x,
  3472. in_high_x:
  3473. internalerror(200104047);
  3474. in_slice_x:
  3475. internalerror(2005101501);
  3476. in_ord_x,
  3477. in_chr_byte:
  3478. begin
  3479. { should not happend as it's converted to typeconv }
  3480. internalerror(200104045);
  3481. end;
  3482. in_ofs_x :
  3483. internalerror(2000101001);
  3484. in_seg_x :
  3485. begin
  3486. result:=first_seg;
  3487. end;
  3488. in_settextbuf_file_x,
  3489. in_reset_typedfile,
  3490. in_rewrite_typedfile,
  3491. in_reset_typedfile_name,
  3492. in_rewrite_typedfile_name,
  3493. in_str_x_string,
  3494. in_val_x,
  3495. in_read_x,
  3496. in_readln_x,
  3497. in_write_x,
  3498. in_writeln_x :
  3499. begin
  3500. { should be handled by pass_typecheck }
  3501. internalerror(200108234);
  3502. end;
  3503. in_get_frame:
  3504. begin
  3505. result:=first_get_frame;
  3506. end;
  3507. in_get_caller_frame:
  3508. begin
  3509. expectloc:=LOC_REGISTER;
  3510. end;
  3511. in_get_caller_addr:
  3512. begin
  3513. expectloc:=LOC_REGISTER;
  3514. end;
  3515. in_prefetch_var:
  3516. begin
  3517. expectloc:=LOC_VOID;
  3518. end;
  3519. in_aligned_x,
  3520. in_unaligned_x:
  3521. begin
  3522. expectloc:=tcallparanode(left).left.expectloc;
  3523. end;
  3524. in_rol_x,
  3525. in_rol_x_y,
  3526. in_ror_x,
  3527. in_ror_x_y,
  3528. in_bsf_x,
  3529. in_bsr_x:
  3530. expectloc:=LOC_REGISTER;
  3531. in_sar_x,
  3532. in_sar_x_y:
  3533. result:=first_sar;
  3534. in_popcnt_x:
  3535. result:=first_popcnt;
  3536. in_new_x:
  3537. result:=first_new;
  3538. in_box_x:
  3539. result:=first_box;
  3540. in_unbox_x_y:
  3541. result:=first_unbox;
  3542. in_fma_single,
  3543. in_fma_double,
  3544. in_fma_extended,
  3545. in_fma_float128:
  3546. result:=first_fma;
  3547. else
  3548. internalerror(89);
  3549. end;
  3550. end;
  3551. {$maxfpuregisters default}
  3552. function tinlinenode.docompare(p: tnode): boolean;
  3553. begin
  3554. docompare :=
  3555. inherited docompare(p) and
  3556. (inlinenumber = tinlinenode(p).inlinenumber);
  3557. end;
  3558. function tinlinenode.first_pi : tnode;
  3559. begin
  3560. result:=crealconstnode.create(getpi,pbestrealtype^);
  3561. end;
  3562. function tinlinenode.first_arctan_real : tnode;
  3563. begin
  3564. { create the call to the helper }
  3565. { on entry left node contains the parameter }
  3566. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  3567. ccallparanode.create(left,nil));
  3568. left := nil;
  3569. end;
  3570. function tinlinenode.first_abs_real : tnode;
  3571. begin
  3572. { create the call to the helper }
  3573. { on entry left node contains the parameter }
  3574. first_abs_real := ctypeconvnode.create(ccallnode.createintern('fpc_abs_real',
  3575. ccallparanode.create(left,nil)),resultdef);
  3576. left := nil;
  3577. end;
  3578. function tinlinenode.first_sqr_real : tnode;
  3579. begin
  3580. {$ifndef cpufpemu}
  3581. { this procedure might be only used for cpus definining cpufpemu else
  3582. the optimizer might go into an endless loop when doing x*x -> changes }
  3583. internalerror(2011092401);
  3584. {$endif cpufpemu}
  3585. { create the call to the helper }
  3586. { on entry left node contains the parameter }
  3587. first_sqr_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqr_real',
  3588. ccallparanode.create(left,nil)),resultdef);
  3589. left := nil;
  3590. end;
  3591. function tinlinenode.first_sqrt_real : tnode;
  3592. var
  3593. fdef: tdef;
  3594. procname: string[31];
  3595. begin
  3596. if ((cs_fp_emulation in current_settings.moduleswitches)
  3597. {$ifdef cpufpemu}
  3598. or (current_settings.fputype=fpu_soft)
  3599. {$endif cpufpemu}
  3600. ) and not (target_info.system in systems_wince) then
  3601. begin
  3602. case tfloatdef(left.resultdef).floattype of
  3603. s32real:
  3604. begin
  3605. fdef:=search_system_type('FLOAT32REC').typedef;
  3606. procname:='float32_sqrt';
  3607. end;
  3608. s64real:
  3609. begin
  3610. fdef:=search_system_type('FLOAT64').typedef;
  3611. procname:='float64_sqrt';
  3612. end;
  3613. {!!! not yet implemented
  3614. s128real:
  3615. }
  3616. else
  3617. internalerror(2014052101);
  3618. end;
  3619. first_sqrt_real:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  3620. ctypeconvnode.create_internal(left,fdef),nil)),resultdef);
  3621. end
  3622. else
  3623. begin
  3624. { create the call to the helper }
  3625. { on entry left node contains the parameter }
  3626. first_sqrt_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqrt_real',
  3627. ccallparanode.create(left,nil)),resultdef);
  3628. end;
  3629. left := nil;
  3630. end;
  3631. function tinlinenode.first_ln_real : tnode;
  3632. begin
  3633. { create the call to the helper }
  3634. { on entry left node contains the parameter }
  3635. first_ln_real := ccallnode.createintern('fpc_ln_real',
  3636. ccallparanode.create(left,nil));
  3637. left := nil;
  3638. end;
  3639. function tinlinenode.first_cos_real : tnode;
  3640. begin
  3641. { create the call to the helper }
  3642. { on entry left node contains the parameter }
  3643. first_cos_real := ccallnode.createintern('fpc_cos_real',
  3644. ccallparanode.create(left,nil));
  3645. left := nil;
  3646. end;
  3647. function tinlinenode.first_sin_real : tnode;
  3648. begin
  3649. { create the call to the helper }
  3650. { on entry left node contains the parameter }
  3651. first_sin_real := ccallnode.createintern('fpc_sin_real',
  3652. ccallparanode.create(left,nil));
  3653. left := nil;
  3654. end;
  3655. function tinlinenode.first_exp_real : tnode;
  3656. begin
  3657. { create the call to the helper }
  3658. { on entry left node contains the parameter }
  3659. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  3660. left := nil;
  3661. end;
  3662. function tinlinenode.first_int_real : tnode;
  3663. begin
  3664. { create the call to the helper }
  3665. { on entry left node contains the parameter }
  3666. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  3667. left := nil;
  3668. end;
  3669. function tinlinenode.first_frac_real : tnode;
  3670. begin
  3671. { create the call to the helper }
  3672. { on entry left node contains the parameter }
  3673. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  3674. left := nil;
  3675. end;
  3676. function tinlinenode.first_round_real : tnode;
  3677. begin
  3678. { create the call to the helper }
  3679. { on entry left node contains the parameter }
  3680. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  3681. left := nil;
  3682. end;
  3683. function tinlinenode.first_trunc_real : tnode;
  3684. begin
  3685. { create the call to the helper }
  3686. { on entry left node contains the parameter }
  3687. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  3688. left := nil;
  3689. end;
  3690. function tinlinenode.first_abs_long : tnode;
  3691. begin
  3692. expectloc:=LOC_REGISTER;
  3693. result:=nil;
  3694. end;
  3695. function tinlinenode.getaddsub_for_incdec : tnode;
  3696. var
  3697. hp,hpp,resultnode : tnode;
  3698. tempnode: ttempcreatenode;
  3699. newstatement: tstatementnode;
  3700. newblock: tblocknode;
  3701. begin
  3702. newblock := internalstatements(newstatement);
  3703. { extra parameter? }
  3704. if assigned(tcallparanode(left).right) then
  3705. begin
  3706. { Yes, use for add node }
  3707. hpp := tcallparanode(tcallparanode(left).right).left;
  3708. tcallparanode(tcallparanode(left).right).left := nil;
  3709. if assigned(tcallparanode(tcallparanode(left).right).right) then
  3710. CGMessage(parser_e_illegal_expression);
  3711. end
  3712. else
  3713. begin
  3714. { no, create constant 1 }
  3715. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  3716. end;
  3717. typecheckpass(hpp);
  3718. { make sure we don't call functions part of the left node twice (and generally }
  3719. { optimize the code generation) }
  3720. { Storing address is not always an optimization: alignment of left is not known
  3721. at this point, so we must assume the worst and use an unaligned pointer.
  3722. This results in larger and slower code on alignment-sensitive targets.
  3723. Therefore the complexity condition below is questionable, maybe just filtering
  3724. out calls with "= NODE_COMPLEXITY_INF" is sufficient.
  3725. Value of 3 corresponds to subscript nodes, i.e. record field. }
  3726. if node_complexity(tcallparanode(left).left) > 3 then
  3727. begin
  3728. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  3729. addstatement(newstatement,tempnode);
  3730. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  3731. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  3732. hp := cderefnode.create(ctemprefnode.create(tempnode));
  3733. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  3734. end
  3735. else
  3736. begin
  3737. hp := tcallparanode(left).left.getcopy;
  3738. tempnode := nil;
  3739. end;
  3740. resultnode := hp.getcopy;
  3741. { avoid type errors from the addn/subn }
  3742. if not is_integer(resultnode.resultdef) then
  3743. begin
  3744. inserttypeconv_internal(hp,sinttype);
  3745. inserttypeconv_internal(hpp,sinttype);
  3746. end;
  3747. { addition/substraction depending on inc/dec }
  3748. if inlinenumber = in_inc_x then
  3749. hpp := caddnode.create_internal(addn,hp,hpp)
  3750. else
  3751. hpp := caddnode.create_internal(subn,hp,hpp);
  3752. { assign result of addition }
  3753. { inherit internal flag }
  3754. if not(is_integer(resultnode.resultdef)) then
  3755. begin
  3756. if nf_internal in flags then
  3757. inserttypeconv_internal(hpp,corddef.create(
  3758. {$ifdef cpu64bitaddr}
  3759. s64bit,
  3760. {$else cpu64bitaddr}
  3761. s32bit,
  3762. {$endif cpu64bitaddr}
  3763. get_min_value(resultnode.resultdef),
  3764. get_max_value(resultnode.resultdef),
  3765. true))
  3766. else
  3767. inserttypeconv(hpp,corddef.create(
  3768. {$ifdef cpu64bitaddr}
  3769. s64bit,
  3770. {$else cpu64bitaddr}
  3771. s32bit,
  3772. {$endif cpu64bitaddr}
  3773. get_min_value(resultnode.resultdef),
  3774. get_max_value(resultnode.resultdef),
  3775. true))
  3776. end
  3777. else
  3778. begin
  3779. if nf_internal in flags then
  3780. inserttypeconv_internal(hpp,resultnode.resultdef)
  3781. else
  3782. inserttypeconv(hpp,resultnode.resultdef);
  3783. end;
  3784. { avoid any possible warnings }
  3785. inserttypeconv_internal(hpp,resultnode.resultdef);
  3786. { get varstates right }
  3787. node_reset_flags(hpp,[nf_pass1_done,nf_modify,nf_write]);
  3788. do_typecheckpass(hpp);
  3789. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  3790. { force pass 1, so copied tries get first pass'ed as well and flags like nf_write, nf_call_unique
  3791. get set right }
  3792. node_reset_flags(newstatement.statement,[nf_pass1_done]);
  3793. { firstpass it }
  3794. firstpass(tnode(newstatement.left));
  3795. { deallocate the temp }
  3796. if assigned(tempnode) then
  3797. addstatement(newstatement,ctempdeletenode.create(tempnode));
  3798. { firstpass it }
  3799. firstpass(tnode(newblock));
  3800. { return new node }
  3801. result := newblock;
  3802. end;
  3803. function tinlinenode.first_IncDec: tnode;
  3804. begin
  3805. expectloc:=LOC_VOID;
  3806. result:=nil;
  3807. { range/overflow checking doesn't work properly }
  3808. { with the inc/dec code that's generated (JM) }
  3809. if ((current_settings.localswitches * [cs_check_overflow,cs_check_range] <> []) and
  3810. { No overflow check for pointer operations, because inc(pointer,-1) will always
  3811. trigger an overflow. For uint32 it works because then the operation is done
  3812. in 64bit. Range checking is not applicable to pointers either }
  3813. (tcallparanode(left).left.resultdef.typ<>pointerdef))
  3814. {$ifdef jvm}
  3815. { enums are class instances on the JVM -> special treatment }
  3816. or (tcallparanode(left).left.resultdef.typ=enumdef)
  3817. {$endif}
  3818. then
  3819. { convert to simple add (JM) }
  3820. result:=getaddsub_for_incdec
  3821. end;
  3822. function tinlinenode.first_IncludeExclude: tnode;
  3823. begin
  3824. result:=nil;
  3825. expectloc:=LOC_VOID;
  3826. end;
  3827. function tinlinenode.first_get_frame: tnode;
  3828. begin
  3829. include(current_procinfo.flags,pi_needs_stackframe);
  3830. expectloc:=LOC_CREGISTER;
  3831. result:=nil;
  3832. end;
  3833. function tinlinenode.first_setlength: tnode;
  3834. var
  3835. paras : tnode;
  3836. npara,
  3837. ppn : tcallparanode;
  3838. dims,
  3839. counter : integer;
  3840. isarray : boolean;
  3841. destppn : tnode;
  3842. newstatement : tstatementnode;
  3843. temp : ttempcreatenode;
  3844. newblock : tnode;
  3845. begin
  3846. paras:=left;
  3847. ppn:=tcallparanode(paras);
  3848. dims:=0;
  3849. while assigned(ppn.right) do
  3850. begin
  3851. inc(dims);
  3852. ppn:=tcallparanode(ppn.right);
  3853. end;
  3854. destppn:=ppn.left;
  3855. isarray:=is_dynamic_array(destppn.resultdef);
  3856. { first param must be a string or dynamic array ...}
  3857. if isarray then
  3858. begin
  3859. { create statements with call initialize the arguments and
  3860. call fpc_dynarr_setlength }
  3861. newblock:=internalstatements(newstatement);
  3862. { get temp for array of lengths }
  3863. temp:=ctempcreatenode.create(carraydef.getreusable(sinttype,dims),dims*sinttype.size,tt_persistent,false);
  3864. addstatement(newstatement,temp);
  3865. { load array of lengths }
  3866. ppn:=tcallparanode(paras);
  3867. counter:=dims-1;
  3868. while assigned(ppn.right) do
  3869. begin
  3870. addstatement(newstatement,cassignmentnode.create(
  3871. cvecnode.create(
  3872. ctemprefnode.create(temp),
  3873. genintconstnode(counter)
  3874. ),
  3875. ppn.left));
  3876. ppn.left:=nil;
  3877. dec(counter);
  3878. ppn:=tcallparanode(ppn.right);
  3879. end;
  3880. { destppn is also reused }
  3881. ppn.left:=nil;
  3882. { create call to fpc_dynarr_setlength }
  3883. npara:=ccallparanode.create(caddrnode.create_internal(
  3884. cvecnode.create(
  3885. ctemprefnode.create(temp),
  3886. genintconstnode(0)
  3887. )),
  3888. ccallparanode.create(cordconstnode.create
  3889. (dims,sinttype,true),
  3890. ccallparanode.create(caddrnode.create_internal
  3891. (crttinode.create(tstoreddef(destppn.resultdef),initrtti,rdt_normal)),
  3892. ccallparanode.create(ctypeconvnode.create_internal(destppn,voidpointertype),nil))));
  3893. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',npara));
  3894. addstatement(newstatement,ctempdeletenode.create(temp));
  3895. end
  3896. else if is_ansistring(destppn.resultdef) then
  3897. begin
  3898. newblock:=ccallnode.createintern(
  3899. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',
  3900. ccallparanode.create(
  3901. cordconstnode.create(getparaencoding(destppn.resultdef),u16inttype,true),
  3902. paras
  3903. )
  3904. );
  3905. { we reused the parameters, make sure we don't release them }
  3906. left:=nil;
  3907. end
  3908. else
  3909. begin
  3910. { we can reuse the supplied parameters }
  3911. newblock:=ccallnode.createintern(
  3912. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',paras);
  3913. { we reused the parameters, make sure we don't release them }
  3914. left:=nil;
  3915. end;
  3916. result:=newblock;
  3917. end;
  3918. function tinlinenode.first_copy: tnode;
  3919. var
  3920. lowppn,
  3921. highppn,
  3922. npara,
  3923. paras : tnode;
  3924. ppn : tcallparanode;
  3925. paradef : tdef;
  3926. counter : integer;
  3927. begin
  3928. { determine copy function to use based on the first argument,
  3929. also count the number of arguments in this loop }
  3930. counter:=1;
  3931. paras:=left;
  3932. ppn:=tcallparanode(paras);
  3933. while assigned(ppn.right) do
  3934. begin
  3935. inc(counter);
  3936. ppn:=tcallparanode(ppn.right);
  3937. end;
  3938. paradef:=ppn.left.resultdef;
  3939. { fill up third parameter }
  3940. if counter=2 then
  3941. begin
  3942. paras:=ccallparanode.create(cordconstnode.create(torddef(sinttype).high,sinttype,false),paras);
  3943. counter:=3;
  3944. end;
  3945. if is_ansistring(resultdef) then
  3946. { keep the specific kind of ansistringdef as result }
  3947. result:=ccallnode.createinternres('fpc_ansistr_copy',paras,resultdef)
  3948. else if is_widestring(resultdef) then
  3949. result:=ccallnode.createintern('fpc_widestr_copy',paras)
  3950. else if is_unicodestring(resultdef) then
  3951. result:=ccallnode.createintern('fpc_unicodestr_copy',paras)
  3952. { can't check for resultdef = cansichartype, because resultdef=
  3953. cshortstringtype here }
  3954. else if is_char(paradef) then
  3955. result:=ccallnode.createintern('fpc_char_copy',paras)
  3956. else if is_dynamic_array(resultdef) then
  3957. begin
  3958. { create statements with call }
  3959. case counter of
  3960. 1:
  3961. begin
  3962. { copy the whole array using [0..high(sizeint)] range }
  3963. highppn:=cordconstnode.create(torddef(sinttype).high,sinttype,false);
  3964. lowppn:=cordconstnode.create(0,sinttype,false);
  3965. end;
  3966. 3:
  3967. begin
  3968. highppn:=tcallparanode(paras).left.getcopy;
  3969. lowppn:=tcallparanode(tcallparanode(paras).right).left.getcopy;
  3970. end;
  3971. else
  3972. internalerror(2012100701);
  3973. end;
  3974. { create call to fpc_dynarray_copy }
  3975. npara:=ccallparanode.create(highppn,
  3976. ccallparanode.create(lowppn,
  3977. ccallparanode.create(caddrnode.create_internal
  3978. (crttinode.create(tstoreddef(paradef),initrtti,rdt_normal)),
  3979. ccallparanode.create
  3980. (ctypeconvnode.create_internal(ppn.left,voidpointertype),nil))));
  3981. result:=ccallnode.createinternres('fpc_dynarray_copy',npara,paradef);
  3982. ppn.left:=nil;
  3983. paras.free;
  3984. end
  3985. else
  3986. result:=ccallnode.createintern('fpc_shortstr_copy',paras);
  3987. { parameters are reused }
  3988. left:=nil;
  3989. end;
  3990. function tinlinenode.first_new: tnode;
  3991. var
  3992. newstatement : tstatementnode;
  3993. newblock : tblocknode;
  3994. temp : ttempcreatenode;
  3995. para : tcallparanode;
  3996. begin
  3997. { create statements with call to getmem+initialize }
  3998. newblock:=internalstatements(newstatement);
  3999. { create temp for result }
  4000. temp := ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  4001. addstatement(newstatement,temp);
  4002. { create call to fpc_getmem }
  4003. para := ccallparanode.create(cordconstnode.create
  4004. (tpointerdef(left.resultdef).pointeddef.size,s32inttype,true),nil);
  4005. addstatement(newstatement,cassignmentnode.create(
  4006. ctemprefnode.create(temp),
  4007. ccallnode.createintern('fpc_getmem',para)));
  4008. { create call to fpc_initialize }
  4009. if is_managed_type(tpointerdef(left.resultdef).pointeddef) then
  4010. begin
  4011. para := ccallparanode.create(caddrnode.create_internal(crttinode.create
  4012. (tstoreddef(tpointerdef(left.resultdef).pointeddef),initrtti,rdt_normal)),
  4013. ccallparanode.create(ctemprefnode.create
  4014. (temp),nil));
  4015. addstatement(newstatement,ccallnode.createintern('fpc_initialize',para));
  4016. end;
  4017. { the last statement should return the value as
  4018. location and type, this is done be referencing the
  4019. temp and converting it first from a persistent temp to
  4020. normal temp }
  4021. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  4022. addstatement(newstatement,ctemprefnode.create(temp));
  4023. result:=newblock;
  4024. end;
  4025. function tinlinenode.first_length: tnode;
  4026. begin
  4027. result:=nil;
  4028. if is_shortstring(left.resultdef) then
  4029. expectloc:=left.expectloc
  4030. else
  4031. begin
  4032. { ansi/wide string }
  4033. expectloc:=LOC_REGISTER;
  4034. end;
  4035. end;
  4036. function tinlinenode.first_assigned: tnode;
  4037. begin
  4038. { Comparison must not call procvars, indicate that with nf_load_procvar flag }
  4039. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  4040. include(result.flags,nf_load_procvar);
  4041. tcallparanode(left).left:=nil;
  4042. end;
  4043. function tinlinenode.first_assert: tnode;
  4044. var
  4045. paras: tcallparanode;
  4046. begin
  4047. paras:=tcallparanode(tcallparanode(left).right);
  4048. paras:=ccallparanode.create(cstringconstnode.createstr(current_module.sourcefiles.get_file_name(current_filepos.fileindex)),paras);
  4049. paras:=ccallparanode.create(genintconstnode(fileinfo.line),paras);
  4050. {$ifdef SUPPORT_GET_FRAME}
  4051. paras:=ccallparanode.create(geninlinenode(in_get_frame,false,nil),paras);
  4052. {$else}
  4053. paras:=ccallparanode.create(ccallnode.createinternfromunit('SYSTEM','GET_FRAME',nil),paras);
  4054. {$endif}
  4055. result:=cifnode.create(cnotnode.create(tcallparanode(left).left),
  4056. ccallnode.createintern('fpc_assert',paras),nil);
  4057. include(result.flags,nf_internal);
  4058. tcallparanode(left).left:=nil;
  4059. tcallparanode(left).right:=nil;
  4060. end;
  4061. function tinlinenode.first_popcnt: tnode;
  4062. var
  4063. suffix : string;
  4064. begin
  4065. case torddef(left.resultdef).ordtype of
  4066. u8bit: suffix:='byte';
  4067. u16bit: suffix:='word';
  4068. u32bit: suffix:='dword';
  4069. u64bit: suffix:='qword';
  4070. else
  4071. internalerror(2012082601);
  4072. end;
  4073. result:=ccallnode.createintern('fpc_popcnt_'+suffix,ccallparanode.create(left,nil));
  4074. left:=nil;
  4075. end;
  4076. function tinlinenode.typecheck_seg: tnode;
  4077. begin
  4078. if target_info.system in systems_managed_vm then
  4079. message(parser_e_feature_unsupported_for_vm);
  4080. set_varstate(left,vs_read,[]);
  4081. result:=cordconstnode.create(0,s32inttype,false);
  4082. end;
  4083. function tinlinenode.first_seg: tnode;
  4084. begin
  4085. internalerror(200104046);
  4086. result:=nil;
  4087. end;
  4088. function tinlinenode.first_sar: tnode;
  4089. begin
  4090. result:=nil;
  4091. expectloc:=LOC_REGISTER;
  4092. {$ifndef cpu64bitalu}
  4093. if is_64bitint(resultdef) then
  4094. begin
  4095. if (inlinenumber=in_sar_x) then
  4096. left:=ccallparanode.create(cordconstnode.create(1,u8inttype,false),
  4097. ccallparanode.create(left,nil));
  4098. result:=ccallnode.createintern('fpc_sarint64',left);
  4099. left:=nil;
  4100. end;
  4101. {$endif cpu64bitalu}
  4102. end;
  4103. function tinlinenode.handle_box: tnode;
  4104. begin
  4105. result:=nil;
  4106. if not assigned(left) or
  4107. assigned(tcallparanode(left).right) then
  4108. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalBox');
  4109. resultdef:=class_tobject;
  4110. end;
  4111. function tinlinenode.handle_unbox: tnode;
  4112. begin
  4113. result:=nil;
  4114. if not assigned(left) or
  4115. not assigned(tcallparanode(left).right) or
  4116. assigned(tcallparanode(tcallparanode(left).right).right) then
  4117. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalUnBox');
  4118. if tcallparanode(left).left.nodetype<>typen then
  4119. internalerror(2011071701);
  4120. ttypenode(tcallparanode(left).left).allowed:=true;
  4121. resultdef:=tcallparanode(left).left.resultdef;
  4122. end;
  4123. function tinlinenode.handle_insert: tnode;
  4124. var
  4125. procname : String;
  4126. first,
  4127. second : tdef;
  4128. begin
  4129. { determine the correct function based on the second parameter }
  4130. first:=tcallparanode(tcallparanode(tcallparanode(left).right).right).left.resultdef;
  4131. second:=tcallparanode(tcallparanode(left).right).left.resultdef;
  4132. if is_shortstring(second) then
  4133. begin
  4134. if is_char(first) then
  4135. procname:='fpc_shortstr_insert_char'
  4136. else
  4137. procname:='fpc_shortstr_insert';
  4138. end
  4139. else if is_unicodestring(second) then
  4140. procname:='fpc_unicodestr_insert'
  4141. else if is_widestring(second) then
  4142. procname:='fpc_widestr_insert'
  4143. else if is_ansistring(second) then
  4144. procname:='fpc_ansistr_insert'
  4145. else if second.typ=undefineddef then
  4146. { just pick one }
  4147. procname:='fpc_ansistr_insert'
  4148. else
  4149. begin
  4150. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,'Insert');
  4151. write_system_parameter_lists('fpc_shortstr_insert');
  4152. write_system_parameter_lists('fpc_shortstr_insert_char');
  4153. write_system_parameter_lists('fpc_unicodestr_insert');
  4154. if tf_winlikewidestring in target_info.flags then
  4155. write_system_parameter_lists('fpc_widestr_insert');
  4156. write_system_parameter_lists('fpc_ansistr_insert');
  4157. exit(cerrornode.create);
  4158. end;
  4159. result:=ccallnode.createintern(procname,left);
  4160. left:=nil;
  4161. end;
  4162. function tinlinenode.handle_delete: tnode;
  4163. var
  4164. procname : String;
  4165. first : tdef;
  4166. firstn,
  4167. newn : tnode;
  4168. begin
  4169. { determine the correct function based on the first parameter }
  4170. firstn:=tcallparanode(tcallparanode(tcallparanode(left).right).right).left;
  4171. first:=firstn.resultdef;
  4172. if is_shortstring(first) then
  4173. procname:='fpc_shortstr_delete'
  4174. else if is_unicodestring(first) then
  4175. procname:='fpc_unicodestr_delete'
  4176. else if is_widestring(first) then
  4177. procname:='fpc_widestr_delete'
  4178. else if is_ansistring(first) then
  4179. procname:='fpc_ansistr_delete'
  4180. else if is_dynamic_array(first) then
  4181. begin
  4182. procname:='fpc_dynarray_delete';
  4183. { recreate the parameters as array pointer, src, count, typeinfo }
  4184. newn:=ccallparanode.create(caddrnode.create_internal
  4185. (crttinode.create(tstoreddef(first),initrtti,rdt_normal)),
  4186. ccallparanode.create(tcallparanode(left).left,
  4187. ccallparanode.create(tcallparanode(tcallparanode(left).right).left,
  4188. ccallparanode.create(ctypeconvnode.create_internal(firstn,voidpointertype),nil))));
  4189. tcallparanode(tcallparanode(tcallparanode(left).right).right).left:=nil;
  4190. tcallparanode(tcallparanode(left).right).left:=nil;
  4191. tcallparanode(left).left:=nil;
  4192. left.free;
  4193. left:=newn;
  4194. end
  4195. else if first.typ=undefineddef then
  4196. { just pick one }
  4197. procname:='fpc_ansistr_delete'
  4198. else
  4199. begin
  4200. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,'Delete');
  4201. write_system_parameter_lists('fpc_shortstr_delete');
  4202. write_system_parameter_lists('fpc_unicodestr_delete');
  4203. if tf_winlikewidestring in target_info.flags then
  4204. write_system_parameter_lists('fpc_widestr_delete');
  4205. write_system_parameter_lists('fpc_ansistr_delete');
  4206. MessagePos1(fileinfo,sym_e_param_list,'Delete(var Dynamic Array;'+sinttype.typename+';'+sinttype.typename+');');
  4207. exit(cerrornode.create);
  4208. end;
  4209. result:=ccallnode.createintern(procname,left);
  4210. left:=nil;
  4211. end;
  4212. function tinlinenode.first_pack_unpack: tnode;
  4213. var
  4214. loopstatement : tstatementnode;
  4215. loop : tblocknode;
  4216. loopvar : ttempcreatenode;
  4217. tempnode,
  4218. source,
  4219. target,
  4220. index,
  4221. unpackednode,
  4222. packednode,
  4223. sourcevecindex,
  4224. targetvecindex,
  4225. loopbody : tnode;
  4226. temprangedef : tdef;
  4227. ulorange,
  4228. uhirange,
  4229. plorange,
  4230. phirange : TConstExprInt;
  4231. begin
  4232. { transform into a for loop which assigns the data of the (un)packed }
  4233. { array to the other one }
  4234. source := left;
  4235. if (inlinenumber = in_unpack_x_y_z) then
  4236. begin
  4237. target := tcallparanode(source).right;
  4238. index := tcallparanode(target).right;
  4239. packednode := tcallparanode(source).left;
  4240. unpackednode := tcallparanode(target).left;
  4241. end
  4242. else
  4243. begin
  4244. index := tcallparanode(source).right;
  4245. target := tcallparanode(index).right;
  4246. packednode := tcallparanode(target).left;
  4247. unpackednode := tcallparanode(source).left;
  4248. end;
  4249. source := tcallparanode(source).left;
  4250. target := tcallparanode(target).left;
  4251. index := tcallparanode(index).left;
  4252. loop := internalstatements(loopstatement);
  4253. loopvar := ctempcreatenode.create(
  4254. tarraydef(packednode.resultdef).rangedef,
  4255. tarraydef(packednode.resultdef).rangedef.size,
  4256. tt_persistent,true);
  4257. addstatement(loopstatement,loopvar);
  4258. { For range checking: we have to convert to an integer type (in case the index type }
  4259. { is an enum), add the index and loop variable together, convert the result }
  4260. { implicitly to an orddef with range equal to the rangedef to get range checking }
  4261. { and finally convert it explicitly back to the actual rangedef to avoid type }
  4262. { errors }
  4263. temprangedef:=nil;
  4264. getrange(unpackednode.resultdef,ulorange,uhirange);
  4265. getrange(packednode.resultdef,plorange,phirange);
  4266. { does not really need to be registered, but then we would have to
  4267. record it elsewhere so it still can be freed }
  4268. temprangedef:=corddef.create(torddef(sinttype).ordtype,ulorange,uhirange,true);
  4269. sourcevecindex := ctemprefnode.create(loopvar);
  4270. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  4271. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  4272. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  4273. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  4274. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  4275. if (inlinenumber = in_pack_x_y_z) then
  4276. begin
  4277. { swap source and target vec indices }
  4278. tempnode := sourcevecindex;
  4279. sourcevecindex := targetvecindex;
  4280. targetvecindex := tempnode;
  4281. end;
  4282. { create the assignment in the loop body }
  4283. loopbody :=
  4284. cassignmentnode.create(
  4285. cvecnode.create(target.getcopy,targetvecindex),
  4286. cvecnode.create(source.getcopy,sourcevecindex)
  4287. );
  4288. { create the actual for loop }
  4289. tempnode := cfornode.create(
  4290. ctemprefnode.create(loopvar),
  4291. cinlinenode.create(in_low_x,false,packednode.getcopy),
  4292. cinlinenode.create(in_high_x,false,packednode.getcopy),
  4293. loopbody,
  4294. false);
  4295. addstatement(loopstatement,tempnode);
  4296. { free the loop counter }
  4297. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  4298. result := loop;
  4299. end;
  4300. function tinlinenode.first_fma: tnode;
  4301. begin
  4302. CGMessage1(cg_e_function_not_support_by_selected_instruction_set,'FMA');
  4303. result:=nil;
  4304. end;
  4305. {$ifndef cpu64bitalu}
  4306. function tinlinenode.first_ShiftRot_assign_64bitint: tnode;
  4307. var
  4308. procname: string[31];
  4309. begin
  4310. {$ifdef cpucg64shiftsupport}
  4311. if inlinenumber in [in_sar_assign_x_y,in_shl_assign_x_y,in_shr_assign_x_y] then
  4312. begin
  4313. result:=nil;
  4314. expectloc:=tcallparanode(tcallparanode(left).right).left.expectloc;
  4315. exit;
  4316. end;
  4317. {$endif cpucg64shiftsupport}
  4318. result := nil;
  4319. if is_signed(tcallparanode(left).right.resultdef) then
  4320. procname:='int64'
  4321. else
  4322. procname:='qword';
  4323. case inlinenumber of
  4324. in_sar_assign_x_y:
  4325. procname := 'fpc_sar_assign_'+procname;
  4326. in_shl_assign_x_y:
  4327. procname := 'fpc_shl_assign_'+procname;
  4328. in_shr_assign_x_y:
  4329. procname := 'fpc_shr_assign_'+procname;
  4330. in_rol_assign_x_y:
  4331. procname := 'fpc_rol_assign_'+procname;
  4332. in_ror_assign_x_y:
  4333. procname := 'fpc_ror_assign_'+procname;
  4334. else
  4335. internalerror(2017041301);
  4336. end;
  4337. result := ccallnode.createintern(procname,ccallparanode.create(tcallparanode(left).left,
  4338. ccallparanode.create(tcallparanode(tcallparanode(left).right).left,nil)));
  4339. tcallparanode(tcallparanode(left).right).left := nil;
  4340. tcallparanode(left).left := nil;
  4341. firstpass(result);
  4342. end;
  4343. {$endif not cpu64bitalu}
  4344. function tinlinenode.first_AndOrXorShiftRot_assign: tnode;
  4345. begin
  4346. {$ifndef cpu64bitalu}
  4347. { 64 bit ints have their own shift handling }
  4348. if is_64bit(tcallparanode(left).right.resultdef) and
  4349. (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
  4350. result := first_ShiftRot_assign_64bitint
  4351. else
  4352. {$endif not cpu64bitalu}
  4353. begin
  4354. result:=nil;
  4355. expectloc:=tcallparanode(tcallparanode(left).right).left.expectloc;
  4356. end;
  4357. end;
  4358. function tinlinenode.first_NegNot_assign: tnode;
  4359. begin
  4360. result:=nil;
  4361. expectloc:=left.expectloc;
  4362. end;
  4363. end.