ninl.pas 160 KB

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