ninl.pas 181 KB

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