ninl.pas 174 KB

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