ninl.pas 173 KB

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