ninl.pas 161 KB

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