2
0

ninl.pas 153 KB

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