ninl.pas 158 KB

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