ninl.pas 158 KB

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