ninl.pas 158 KB

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