ninl.pas 153 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973
  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. paras : tnode;
  1445. ppn : tcallparanode;
  1446. paradef : tdef;
  1447. counter : integer;
  1448. begin
  1449. result:=nil;
  1450. { determine copy function to use based on the first argument,
  1451. also count the number of arguments in this loop }
  1452. counter:=1;
  1453. paras:=left;
  1454. ppn:=tcallparanode(paras);
  1455. while assigned(ppn.right) do
  1456. begin
  1457. inc(counter);
  1458. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1459. ppn:=tcallparanode(ppn.right);
  1460. end;
  1461. set_varstate(ppn.left,vs_read,[vsf_must_be_valid]);
  1462. paradef:=ppn.left.resultdef;
  1463. if is_ansistring(paradef) then
  1464. // set resultdef to argument def
  1465. resultdef:=paradef
  1466. else if (is_chararray(paradef) and (paradef.size>255)) or
  1467. ((cs_refcountedstrings in current_settings.localswitches) and is_pchar(paradef)) then
  1468. // set resultdef to ansistring type since result will be in ansistring codepage
  1469. resultdef:=getansistringdef
  1470. else
  1471. if is_widestring(paradef) then
  1472. resultdef:=cwidestringtype
  1473. else
  1474. if is_unicodestring(paradef) or
  1475. is_widechararray(paradef) or
  1476. is_pwidechar(paradef) then
  1477. resultdef:=cunicodestringtype
  1478. else
  1479. if is_char(paradef) then
  1480. resultdef:=cshortstringtype
  1481. else
  1482. if is_dynamic_array(paradef) then
  1483. begin
  1484. { Only allow 1 or 3 arguments }
  1485. if not(counter in [1..3]) then
  1486. begin
  1487. CGMessage1(parser_e_wrong_parameter_size,'Copy');
  1488. exit;
  1489. end;
  1490. resultdef:=paradef;
  1491. end
  1492. else
  1493. begin
  1494. { generic fallback that will give an error if a wrong
  1495. type is passed }
  1496. if (counter=3) then
  1497. resultdef:=cshortstringtype
  1498. else
  1499. CGMessagePos(ppn.left.fileinfo,type_e_mismatch);
  1500. end;
  1501. end;
  1502. {$maxfpuregisters 0}
  1503. function getpi : bestreal;
  1504. begin
  1505. {$ifdef x86}
  1506. { x86 has pi in hardware }
  1507. result:=pi;
  1508. {$else x86}
  1509. {$ifdef cpuextended}
  1510. result:=MathPiExtended.Value;
  1511. {$else cpuextended}
  1512. result:=MathPi.Value;
  1513. {$endif cpuextended}
  1514. {$endif x86}
  1515. end;
  1516. function tinlinenode.simplify(forinline : boolean): tnode;
  1517. function do_lowhigh(def:tdef) : tnode;
  1518. var
  1519. v : tconstexprint;
  1520. enum : tenumsym;
  1521. hp : tnode;
  1522. i : integer;
  1523. begin
  1524. case def.typ of
  1525. orddef:
  1526. begin
  1527. set_varstate(left,vs_read,[]);
  1528. if inlinenumber=in_low_x then
  1529. v:=torddef(def).low
  1530. else
  1531. v:=torddef(def).high;
  1532. hp:=cordconstnode.create(v,def,true);
  1533. typecheckpass(hp);
  1534. do_lowhigh:=hp;
  1535. end;
  1536. enumdef:
  1537. begin
  1538. set_varstate(left,vs_read,[]);
  1539. if inlinenumber=in_high_x then
  1540. v:=tenumdef(def).maxval
  1541. else
  1542. v:=tenumdef(def).minval;
  1543. enum:=nil;
  1544. for i := 0 to tenumdef(def).symtable.SymList.Count - 1 do
  1545. if tenumsym(tenumdef(def).symtable.SymList[i]).value=v then
  1546. begin
  1547. enum:=tenumsym(tenumdef(def).symtable.SymList[i]);
  1548. break;
  1549. end;
  1550. if not assigned(enum) then
  1551. internalerror(309993)
  1552. else
  1553. hp:=genenumnode(enum);
  1554. do_lowhigh:=hp;
  1555. end;
  1556. else
  1557. internalerror(87);
  1558. end;
  1559. end;
  1560. function getconstrealvalue : bestreal;
  1561. begin
  1562. case left.nodetype of
  1563. ordconstn:
  1564. getconstrealvalue:=tordconstnode(left).value;
  1565. realconstn:
  1566. getconstrealvalue:=trealconstnode(left).value_real;
  1567. else
  1568. internalerror(309992);
  1569. end;
  1570. end;
  1571. procedure setconstrealvalue(r : bestreal);
  1572. begin
  1573. result:=crealconstnode.create(r,pbestrealtype^);
  1574. end;
  1575. function handle_ln_const(r : bestreal) : tnode;
  1576. begin
  1577. if r<=0.0 then
  1578. if floating_point_range_check_error then
  1579. begin
  1580. result:=crealconstnode.create(0,pbestrealtype^);
  1581. CGMessage(type_e_wrong_math_argument)
  1582. end
  1583. else
  1584. begin
  1585. if r=0.0 then
  1586. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1587. else
  1588. result:=crealconstnode.create(MathNegInf.Value,pbestrealtype^)
  1589. end
  1590. else
  1591. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1592. end;
  1593. function handle_sqrt_const(r : bestreal) : tnode;
  1594. begin
  1595. if r<0.0 then
  1596. if floating_point_range_check_error then
  1597. begin
  1598. result:=crealconstnode.create(0,pbestrealtype^);
  1599. CGMessage(type_e_wrong_math_argument)
  1600. end
  1601. else
  1602. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1603. else
  1604. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1605. end;
  1606. function handle_const_sar : tnode;
  1607. var
  1608. vl,vl2 : TConstExprInt;
  1609. bits,shift: integer;
  1610. mask : qword;
  1611. def : tdef;
  1612. begin
  1613. result:=nil;
  1614. if (left.nodetype=ordconstn) or ((left.nodetype=callparan) and (tcallparanode(left).left.nodetype=ordconstn)) then
  1615. begin
  1616. if (left.nodetype=callparan) and
  1617. assigned(tcallparanode(left).right) then
  1618. begin
  1619. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  1620. begin
  1621. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  1622. vl:=tordconstnode(tcallparanode(left).left).value;
  1623. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1624. end
  1625. else
  1626. exit;
  1627. end
  1628. else
  1629. begin
  1630. def:=left.resultdef;
  1631. vl:=1;
  1632. vl2:=tordconstnode(left).value;
  1633. end;
  1634. bits:=def.size*8;
  1635. shift:=vl.svalue and (bits-1);
  1636. case bits of
  1637. 8:
  1638. mask:=$ff;
  1639. 16:
  1640. mask:=$ffff;
  1641. 32:
  1642. mask:=$ffffffff;
  1643. 64:
  1644. mask:=qword($ffffffffffffffff);
  1645. else
  1646. mask:=qword(1 shl bits)-1;
  1647. end;
  1648. {$push}
  1649. {$r-,q-}
  1650. if shift=0 then
  1651. result:=cordconstnode.create(vl2.svalue,def,false)
  1652. else if vl2.svalue<0 then
  1653. result:=cordconstnode.create(((vl2.svalue shr shift) or (mask shl (bits-shift))) and mask,def,false)
  1654. else
  1655. result:=cordconstnode.create((vl2.svalue shr shift) and mask,def,false);
  1656. {$pop}
  1657. end
  1658. else
  1659. end;
  1660. function handle_const_rox : tnode;
  1661. var
  1662. vl,vl2 : TConstExprInt;
  1663. bits,shift: integer;
  1664. def : tdef;
  1665. begin
  1666. result:=nil;
  1667. if (left.nodetype=ordconstn) or ((left.nodetype=callparan) and (tcallparanode(left).left.nodetype=ordconstn)) then
  1668. begin
  1669. if (left.nodetype=callparan) and
  1670. assigned(tcallparanode(left).right) then
  1671. begin
  1672. if (tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn) then
  1673. begin
  1674. def:=tcallparanode(tcallparanode(left).right).left.resultdef;
  1675. vl:=tordconstnode(tcallparanode(left).left).value;
  1676. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1677. end
  1678. else
  1679. exit;
  1680. end
  1681. else
  1682. begin
  1683. def:=left.resultdef;
  1684. vl:=1;
  1685. vl2:=tordconstnode(left).value;
  1686. end;
  1687. bits:=def.size*8;
  1688. shift:=vl.svalue and (bits-1);
  1689. {$push}
  1690. {$r-,q-}
  1691. if shift=0 then
  1692. result:=cordconstnode.create(vl2.svalue,def,false)
  1693. else
  1694. case inlinenumber of
  1695. in_ror_x,in_ror_x_y:
  1696. case def.size of
  1697. 1:
  1698. result:=cordconstnode.create(RorByte(Byte(vl2.svalue),shift),def,false);
  1699. 2:
  1700. result:=cordconstnode.create(RorWord(Word(vl2.svalue),shift),def,false);
  1701. 4:
  1702. result:=cordconstnode.create(RorDWord(DWord(vl2.svalue),shift),def,false);
  1703. 8:
  1704. result:=cordconstnode.create(RorQWord(QWord(vl2.svalue),shift),def,false);
  1705. else
  1706. internalerror(2011061903);
  1707. end;
  1708. in_rol_x,in_rol_x_y:
  1709. case def.size of
  1710. 1:
  1711. result:=cordconstnode.create(RolByte(Byte(vl2.svalue),shift),def,false);
  1712. 2:
  1713. result:=cordconstnode.create(RolWord(Word(vl2.svalue),shift),def,false);
  1714. 4:
  1715. result:=cordconstnode.create(RolDWord(DWord(vl2.svalue),shift),def,false);
  1716. 8:
  1717. result:=cordconstnode.create(RolQWord(QWord(vl2.svalue),shift),def,false);
  1718. else
  1719. internalerror(2011061902);
  1720. end;
  1721. else
  1722. internalerror(2011061901);
  1723. end;
  1724. end;
  1725. end;
  1726. var
  1727. hp : tnode;
  1728. vl,vl2 : TConstExprInt;
  1729. vr : bestreal;
  1730. begin { simplify }
  1731. result:=nil;
  1732. { handle intern constant functions in separate case }
  1733. if nf_inlineconst in flags then
  1734. begin
  1735. { no parameters? }
  1736. if not assigned(left) then
  1737. internalerror(200501231)
  1738. else
  1739. begin
  1740. vl:=0;
  1741. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1742. case left.nodetype of
  1743. realconstn :
  1744. begin
  1745. { Real functions are all handled with internproc below }
  1746. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename)
  1747. end;
  1748. ordconstn :
  1749. vl:=tordconstnode(left).value;
  1750. callparan :
  1751. begin
  1752. { both exists, else it was not generated }
  1753. vl:=tordconstnode(tcallparanode(left).left).value;
  1754. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1755. end;
  1756. else
  1757. CGMessage(parser_e_illegal_expression);
  1758. end;
  1759. case inlinenumber of
  1760. in_const_abs :
  1761. if vl.signed then
  1762. hp:=create_simplified_ord_const(abs(vl.svalue),resultdef,forinline)
  1763. else
  1764. hp:=create_simplified_ord_const(vl.uvalue,resultdef,forinline);
  1765. in_const_sqr:
  1766. if vl.signed then
  1767. hp:=create_simplified_ord_const(sqr(vl.svalue),resultdef,forinline)
  1768. else
  1769. hp:=create_simplified_ord_const(sqr(vl.uvalue),resultdef,forinline);
  1770. in_const_odd :
  1771. hp:=cordconstnode.create(qword(odd(int64(vl))),pasbool8type,true);
  1772. in_const_swap_word :
  1773. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resultdef,true);
  1774. in_const_swap_long :
  1775. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resultdef,true);
  1776. in_const_swap_qword :
  1777. hp:=cordconstnode.create((vl and $ffff) shl 32+(vl shr 32),left.resultdef,true);
  1778. in_const_ptr:
  1779. begin
  1780. {Don't construct pointers from negative values.}
  1781. if (vl.signed and (vl.svalue<0)) or (vl2.signed and (vl2.svalue<0)) then
  1782. cgmessage(parser_e_range_check_error);
  1783. hp:=cpointerconstnode.create((vl2.uvalue shl 4)+vl.uvalue,voidfarpointertype);
  1784. end
  1785. else
  1786. internalerror(88);
  1787. end;
  1788. end;
  1789. if hp=nil then
  1790. hp:=cerrornode.create;
  1791. result:=hp;
  1792. end
  1793. else
  1794. begin
  1795. case inlinenumber of
  1796. in_lo_long,
  1797. in_hi_long,
  1798. in_lo_qword,
  1799. in_hi_qword,
  1800. in_lo_word,
  1801. in_hi_word :
  1802. begin
  1803. if left.nodetype=ordconstn then
  1804. begin
  1805. case inlinenumber of
  1806. in_lo_word :
  1807. result:=cordconstnode.create(tordconstnode(left).value and $ff,u8inttype,true);
  1808. in_hi_word :
  1809. result:=cordconstnode.create(tordconstnode(left).value shr 8,u8inttype,true);
  1810. in_lo_long :
  1811. result:=cordconstnode.create(tordconstnode(left).value and $ffff,u16inttype,true);
  1812. in_hi_long :
  1813. result:=cordconstnode.create(tordconstnode(left).value shr 16,u16inttype,true);
  1814. in_lo_qword :
  1815. result:=cordconstnode.create(tordconstnode(left).value and $ffffffff,u32inttype,true);
  1816. in_hi_qword :
  1817. result:=cordconstnode.create(tordconstnode(left).value shr 32,u32inttype,true);
  1818. end;
  1819. end;
  1820. end;
  1821. in_ord_x:
  1822. begin
  1823. case left.resultdef.typ of
  1824. orddef :
  1825. begin
  1826. case torddef(left.resultdef).ordtype of
  1827. pasbool8,
  1828. uchar:
  1829. begin
  1830. { change to byte() }
  1831. result:=ctypeconvnode.create_internal(left,u8inttype);
  1832. left:=nil;
  1833. end;
  1834. pasbool16,
  1835. uwidechar :
  1836. begin
  1837. { change to word() }
  1838. result:=ctypeconvnode.create_internal(left,u16inttype);
  1839. left:=nil;
  1840. end;
  1841. pasbool32 :
  1842. begin
  1843. { change to dword() }
  1844. result:=ctypeconvnode.create_internal(left,u32inttype);
  1845. left:=nil;
  1846. end;
  1847. pasbool64 :
  1848. begin
  1849. { change to qword() }
  1850. result:=ctypeconvnode.create_internal(left,u64inttype);
  1851. left:=nil;
  1852. end;
  1853. bool8bit:
  1854. begin
  1855. { change to shortint() }
  1856. result:=ctypeconvnode.create_internal(left,s8inttype);
  1857. left:=nil;
  1858. end;
  1859. bool16bit :
  1860. begin
  1861. { change to smallint() }
  1862. result:=ctypeconvnode.create_internal(left,s16inttype);
  1863. left:=nil;
  1864. end;
  1865. bool32bit :
  1866. begin
  1867. { change to longint() }
  1868. result:=ctypeconvnode.create_internal(left,s32inttype);
  1869. left:=nil;
  1870. end;
  1871. bool64bit :
  1872. begin
  1873. { change to int64() }
  1874. result:=ctypeconvnode.create_internal(left,s64inttype);
  1875. left:=nil;
  1876. end;
  1877. uvoid :
  1878. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1879. else
  1880. begin
  1881. { all other orddef need no transformation }
  1882. result:=left;
  1883. left:=nil;
  1884. end;
  1885. end;
  1886. end;
  1887. enumdef :
  1888. begin
  1889. result:=ctypeconvnode.create_internal(left,s32inttype);
  1890. left:=nil;
  1891. end;
  1892. pointerdef :
  1893. begin
  1894. if m_mac in current_settings.modeswitches then
  1895. begin
  1896. result:=ctypeconvnode.create_internal(left,ptruinttype);
  1897. left:=nil;
  1898. end
  1899. end;
  1900. end;
  1901. (*
  1902. if (left.nodetype=ordconstn) then
  1903. begin
  1904. result:=cordconstnode.create(
  1905. tordconstnode(left).value,sinttype,true);
  1906. end
  1907. else if (m_mac in current_settings.modeswitches) and
  1908. (left.ndoetype=pointerconstn) then
  1909. result:=cordconstnode.create(
  1910. tpointerconstnode(left).value,ptruinttype,true);
  1911. *)
  1912. end;
  1913. in_chr_byte:
  1914. begin
  1915. { convert to explicit char() }
  1916. result:=ctypeconvnode.create_internal(left,cansichartype);
  1917. left:=nil;
  1918. end;
  1919. in_length_x:
  1920. begin
  1921. case left.resultdef.typ of
  1922. stringdef :
  1923. begin
  1924. if (left.nodetype=stringconstn) then
  1925. begin
  1926. result:=cordconstnode.create(
  1927. tstringconstnode(left).len,sinttype,true);
  1928. end;
  1929. end;
  1930. orddef :
  1931. begin
  1932. { length of char is always one }
  1933. if is_char(left.resultdef) or
  1934. is_widechar(left.resultdef) then
  1935. begin
  1936. result:=cordconstnode.create(1,sinttype,false);
  1937. end
  1938. end;
  1939. arraydef :
  1940. begin
  1941. if (left.nodetype=stringconstn) then
  1942. begin
  1943. result:=cordconstnode.create(
  1944. tstringconstnode(left).len,sinttype,true);
  1945. end
  1946. else if not is_open_array(left.resultdef) and
  1947. not is_array_of_const(left.resultdef) and
  1948. not is_dynamic_array(left.resultdef) then
  1949. result:=cordconstnode.create(tarraydef(left.resultdef).highrange-
  1950. tarraydef(left.resultdef).lowrange+1,
  1951. sinttype,true);
  1952. end;
  1953. end;
  1954. end;
  1955. in_assigned_x:
  1956. begin
  1957. if is_constnode(tcallparanode(left).left) or
  1958. (tcallparanode(left).left.nodetype = pointerconstn) then
  1959. begin
  1960. { let an add node figure it out }
  1961. result:=caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  1962. tcallparanode(left).left := nil;
  1963. end;
  1964. end;
  1965. in_pred_x,
  1966. in_succ_x:
  1967. begin
  1968. if (left.nodetype=ordconstn) then
  1969. begin
  1970. if (inlinenumber=in_succ_x) then
  1971. vl:=tordconstnode(left).value+1
  1972. else
  1973. vl:=tordconstnode(left).value-1;
  1974. if is_integer(left.resultdef) then
  1975. { the type of the original integer constant is irrelevant,
  1976. it should be automatically adapted to the new value
  1977. (except when inlining) }
  1978. result:=create_simplified_ord_const(vl,resultdef,forinline)
  1979. else
  1980. { check the range for enums, chars, booleans }
  1981. result:=cordconstnode.create(vl,left.resultdef,true)
  1982. end
  1983. end;
  1984. in_low_x,
  1985. in_high_x:
  1986. begin
  1987. case left.resultdef.typ of
  1988. orddef,
  1989. enumdef:
  1990. begin
  1991. result:=do_lowhigh(left.resultdef);
  1992. end;
  1993. setdef:
  1994. begin
  1995. result:=do_lowhigh(tsetdef(left.resultdef).elementdef);
  1996. end;
  1997. arraydef:
  1998. begin
  1999. if (inlinenumber=in_low_x) then
  2000. begin
  2001. result:=cordconstnode.create(int64(tarraydef(
  2002. left.resultdef).lowrange),tarraydef(left.resultdef).rangedef,true);
  2003. end
  2004. else if not is_open_array(left.resultdef) and
  2005. not is_array_of_const(left.resultdef) and
  2006. not is_dynamic_array(left.resultdef) then
  2007. result:=cordconstnode.create(int64(tarraydef(left.resultdef).highrange),
  2008. tarraydef(left.resultdef).rangedef,true);
  2009. end;
  2010. stringdef:
  2011. begin
  2012. if inlinenumber=in_low_x then
  2013. begin
  2014. result:=cordconstnode.create(0,u8inttype,false);
  2015. end
  2016. else if not is_ansistring(left.resultdef) and
  2017. not is_wide_or_unicode_string(left.resultdef) then
  2018. result:=cordconstnode.create(tstringdef(left.resultdef).len,u8inttype,true)
  2019. end;
  2020. end;
  2021. end;
  2022. in_exp_real :
  2023. begin
  2024. if left.nodetype in [ordconstn,realconstn] then
  2025. begin
  2026. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  2027. if (trealconstnode(result).value_real=MathInf.Value) and
  2028. floating_point_range_check_error then
  2029. begin
  2030. result:=crealconstnode.create(0,pbestrealtype^);
  2031. CGMessage(parser_e_range_check_error);
  2032. end;
  2033. end
  2034. end;
  2035. in_trunc_real :
  2036. begin
  2037. if left.nodetype in [ordconstn,realconstn] then
  2038. begin
  2039. vr:=getconstrealvalue;
  2040. if (vr>=9223372036854775807.99) or (vr<=-9223372036854775808.0) then
  2041. begin
  2042. message3(type_e_range_check_error_bounds,realtostr(vr),'-9223372036854775808.0','9223372036854775807.99..');
  2043. result:=cordconstnode.create(1,s64inttype,false)
  2044. end
  2045. else
  2046. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  2047. end
  2048. end;
  2049. in_round_real :
  2050. begin
  2051. { can't evaluate while inlining, may depend on fpu setting }
  2052. if (not forinline) and
  2053. (left.nodetype in [ordconstn,realconstn]) then
  2054. begin
  2055. vr:=getconstrealvalue;
  2056. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  2057. begin
  2058. message3(type_e_range_check_error_bounds,realtostr(vr),'-9223372036854775808.49..','9223372036854775807.49..');
  2059. result:=cordconstnode.create(1,s64inttype,false)
  2060. end
  2061. else
  2062. result:=cordconstnode.create(round(vr),s64inttype,true)
  2063. end
  2064. end;
  2065. in_frac_real :
  2066. begin
  2067. if left.nodetype in [ordconstn,realconstn] then
  2068. setconstrealvalue(frac(getconstrealvalue))
  2069. end;
  2070. in_int_real :
  2071. begin
  2072. if left.nodetype in [ordconstn,realconstn] then
  2073. setconstrealvalue(int(getconstrealvalue));
  2074. end;
  2075. in_pi_real :
  2076. begin
  2077. if block_type=bt_const then
  2078. setconstrealvalue(getpi)
  2079. end;
  2080. in_cos_real :
  2081. begin
  2082. if left.nodetype in [ordconstn,realconstn] then
  2083. setconstrealvalue(cos(getconstrealvalue))
  2084. end;
  2085. in_sin_real :
  2086. begin
  2087. if left.nodetype in [ordconstn,realconstn] then
  2088. setconstrealvalue(sin(getconstrealvalue))
  2089. end;
  2090. in_arctan_real :
  2091. begin
  2092. if left.nodetype in [ordconstn,realconstn] then
  2093. setconstrealvalue(arctan(getconstrealvalue))
  2094. end;
  2095. in_abs_real :
  2096. begin
  2097. if left.nodetype in [ordconstn,realconstn] then
  2098. setconstrealvalue(abs(getconstrealvalue))
  2099. end;
  2100. in_abs_long:
  2101. begin
  2102. if left.nodetype=ordconstn then
  2103. begin
  2104. if tordconstnode(left).value<0 then
  2105. result:=cordconstnode.create((-tordconstnode(left).value),s32inttype,false)
  2106. else
  2107. result:=cordconstnode.create((tordconstnode(left).value),s32inttype,false);
  2108. end
  2109. end;
  2110. in_sqr_real :
  2111. begin
  2112. if left.nodetype in [ordconstn,realconstn] then
  2113. setconstrealvalue(sqr(getconstrealvalue))
  2114. end;
  2115. in_sqrt_real :
  2116. begin
  2117. if left.nodetype in [ordconstn,realconstn] then
  2118. begin
  2119. vr:=getconstrealvalue;
  2120. if vr<0.0 then
  2121. result:=handle_sqrt_const(vr)
  2122. else
  2123. setconstrealvalue(sqrt(vr));
  2124. end
  2125. end;
  2126. in_ln_real :
  2127. begin
  2128. if left.nodetype in [ordconstn,realconstn] then
  2129. begin
  2130. vr:=getconstrealvalue;
  2131. if vr<=0.0 then
  2132. result:=handle_ln_const(vr)
  2133. else
  2134. setconstrealvalue(ln(vr));
  2135. end
  2136. end;
  2137. in_assert_x_y :
  2138. begin
  2139. if not(cs_do_assertion in current_settings.localswitches) then
  2140. { we need a valid node, so insert a nothingn }
  2141. result:=cnothingnode.create;
  2142. end;
  2143. in_sar_x,
  2144. in_sar_x_y :
  2145. begin
  2146. result:=handle_const_sar;
  2147. end;
  2148. in_rol_x,
  2149. in_rol_x_y,
  2150. in_ror_x,
  2151. in_ror_x_y :
  2152. result:=handle_const_rox;
  2153. end;
  2154. end;
  2155. end;
  2156. function tinlinenode.pass_typecheck:tnode;
  2157. procedure setfloatresultdef;
  2158. begin
  2159. if (left.resultdef.typ=floatdef) and
  2160. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real]) then
  2161. resultdef:=left.resultdef
  2162. else
  2163. begin
  2164. if (left.nodetype <> ordconstn) then
  2165. inserttypeconv(left,pbestrealtype^);
  2166. resultdef:=pbestrealtype^;
  2167. end;
  2168. end;
  2169. procedure handle_pack_unpack;
  2170. var
  2171. source, target, index: tcallparanode;
  2172. unpackedarraydef, packedarraydef: tarraydef;
  2173. tempindex: TConstExprInt;
  2174. begin
  2175. resultdef:=voidtype;
  2176. unpackedarraydef := nil;
  2177. packedarraydef := nil;
  2178. source := tcallparanode(left);
  2179. if (inlinenumber = in_unpack_x_y_z) then
  2180. begin
  2181. target := tcallparanode(source.right);
  2182. index := tcallparanode(target.right);
  2183. { source must be a packed array }
  2184. if not is_packed_array(source.left.resultdef) then
  2185. CGMessagePos2(source.left.fileinfo,type_e_got_expected_packed_array,'1',source.left.resultdef.typename)
  2186. else
  2187. packedarraydef := tarraydef(source.left.resultdef);
  2188. { target can be any kind of array, as long as it's not packed }
  2189. if (target.left.resultdef.typ <> arraydef) or
  2190. is_packed_array(target.left.resultdef) then
  2191. CGMessagePos2(target.left.fileinfo,type_e_got_expected_unpacked_array,'2',target.left.resultdef.typename)
  2192. else
  2193. unpackedarraydef := tarraydef(target.left.resultdef);
  2194. end
  2195. else
  2196. begin
  2197. index := tcallparanode(source.right);
  2198. target := tcallparanode(index.right);
  2199. { source can be any kind of array, as long as it's not packed }
  2200. if (source.left.resultdef.typ <> arraydef) or
  2201. is_packed_array(source.left.resultdef) then
  2202. CGMessagePos2(source.left.fileinfo,type_e_got_expected_unpacked_array,'1',source.left.resultdef.typename)
  2203. else
  2204. unpackedarraydef := tarraydef(source.left.resultdef);
  2205. { target must be a packed array }
  2206. if not is_packed_array(target.left.resultdef) then
  2207. CGMessagePos2(target.left.fileinfo,type_e_got_expected_packed_array,'3',target.left.resultdef.typename)
  2208. else
  2209. packedarraydef := tarraydef(target.left.resultdef);
  2210. end;
  2211. if assigned(unpackedarraydef) then
  2212. begin
  2213. { index must be compatible with the unpacked array's indextype }
  2214. inserttypeconv(index.left,unpackedarraydef.rangedef);
  2215. { range check at compile time if possible }
  2216. if assigned(packedarraydef) and
  2217. (index.left.nodetype = ordconstn) and
  2218. not is_special_array(unpackedarraydef) then
  2219. begin
  2220. testrange(unpackedarraydef,tordconstnode(index.left).value,false,false);
  2221. tempindex := tordconstnode(index.left).value + packedarraydef.highrange-packedarraydef.lowrange;
  2222. testrange(unpackedarraydef,tempindex,false,false);
  2223. end;
  2224. end;
  2225. { source array is read and must be valid }
  2226. set_varstate(source.left,vs_read,[vsf_must_be_valid]);
  2227. { target array is written }
  2228. valid_for_assignment(target.left,true);
  2229. set_varstate(target.left,vs_written,[]);
  2230. { index in the unpacked array is read and must be valid }
  2231. set_varstate(index.left,vs_read,[vsf_must_be_valid]);
  2232. { if the size of the arrays is 0 (array of empty records), }
  2233. { do nothing }
  2234. if (source.resultdef.size = 0) then
  2235. result:=cnothingnode.create;
  2236. end;
  2237. function handle_objc_encode: tnode;
  2238. var
  2239. encodedtype: ansistring;
  2240. errordef: tdef;
  2241. begin
  2242. encodedtype:='';
  2243. if not objctryencodetype(left.resultdef,encodedtype,errordef) then
  2244. Message1(type_e_objc_type_unsupported,errordef.typename);
  2245. result:=cstringconstnode.createpchar(ansistring2pchar(encodedtype),length(encodedtype));
  2246. end;
  2247. var
  2248. hightree,
  2249. hp : tnode;
  2250. begin
  2251. result:=nil;
  2252. { when handling writeln "left" contains no valid address }
  2253. if assigned(left) then
  2254. begin
  2255. if left.nodetype=callparan then
  2256. tcallparanode(left).get_paratype
  2257. else
  2258. typecheckpass(left);
  2259. end;
  2260. if not(nf_inlineconst in flags) then
  2261. begin
  2262. case inlinenumber of
  2263. in_lo_long,
  2264. in_hi_long,
  2265. in_lo_qword,
  2266. in_hi_qword,
  2267. in_lo_word,
  2268. in_hi_word :
  2269. begin
  2270. { give warning for incompatibility with tp and delphi }
  2271. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  2272. ((m_tp7 in current_settings.modeswitches) or
  2273. (m_delphi in current_settings.modeswitches)) then
  2274. CGMessage(type_w_maybe_wrong_hi_lo);
  2275. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2276. if not is_integer(left.resultdef) then
  2277. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2278. case inlinenumber of
  2279. in_lo_word,
  2280. in_hi_word :
  2281. resultdef:=u8inttype;
  2282. in_lo_long,
  2283. in_hi_long :
  2284. resultdef:=u16inttype;
  2285. in_lo_qword,
  2286. in_hi_qword :
  2287. resultdef:=u32inttype;
  2288. end;
  2289. end;
  2290. in_sizeof_x:
  2291. begin
  2292. { the constant evaluation of in_sizeof_x happens in pexpr where possible }
  2293. set_varstate(left,vs_read,[]);
  2294. if (left.resultdef.typ<>undefineddef) and
  2295. paramanager.push_high_param(vs_value,left.resultdef,current_procinfo.procdef.proccalloption) then
  2296. begin
  2297. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2298. if assigned(hightree) then
  2299. begin
  2300. hp:=caddnode.create(addn,hightree,
  2301. cordconstnode.create(1,sinttype,false));
  2302. if (left.resultdef.typ=arraydef) then
  2303. if not is_packed_array(tarraydef(left.resultdef)) then
  2304. begin
  2305. if (tarraydef(left.resultdef).elesize<>1) then
  2306. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  2307. left.resultdef).elesize,sinttype,true));
  2308. end
  2309. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  2310. begin
  2311. { no packed open array support yet }
  2312. if (hp.nodetype <> ordconstn) then
  2313. internalerror(2006081511);
  2314. hp.free;
  2315. hp := cordconstnode.create(left.resultdef.size,sinttype,true);
  2316. {
  2317. hp:=
  2318. ctypeconvnode.create_explicit(sinttype,
  2319. cmoddivnode.create(divn,
  2320. caddnode.create(addn,
  2321. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  2322. left.resultdef).elepackedbitsize,s64inttype,true)),
  2323. cordconstnode.create(a,s64inttype,true)),
  2324. cordconstnode.create(8,s64inttype,true)),
  2325. sinttype);
  2326. }
  2327. end;
  2328. result:=hp;
  2329. end;
  2330. end
  2331. else
  2332. resultdef:=sinttype;
  2333. end;
  2334. in_typeof_x:
  2335. begin
  2336. if target_info.system in systems_managed_vm then
  2337. message(parser_e_feature_unsupported_for_vm);
  2338. typecheckpass(left);
  2339. set_varstate(left,vs_read,[]);
  2340. if (left.resultdef.typ=objectdef) and
  2341. not(oo_has_vmt in tobjectdef(left.resultdef).objectoptions) then
  2342. message(type_e_typeof_requires_vmt);
  2343. resultdef:=voidpointertype;
  2344. end;
  2345. in_ord_x:
  2346. begin
  2347. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2348. case left.resultdef.typ of
  2349. orddef,
  2350. enumdef :
  2351. ;
  2352. pointerdef :
  2353. begin
  2354. if not(m_mac in current_settings.modeswitches) then
  2355. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2356. end
  2357. else
  2358. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  2359. end;
  2360. end;
  2361. in_chr_byte:
  2362. begin
  2363. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2364. end;
  2365. in_length_x:
  2366. begin
  2367. if ((left.resultdef.typ=arraydef) and
  2368. (not is_special_array(left.resultdef) or
  2369. is_open_array(left.resultdef))) or
  2370. (left.resultdef.typ=orddef) then
  2371. set_varstate(left,vs_read,[])
  2372. else
  2373. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2374. case left.resultdef.typ of
  2375. variantdef:
  2376. begin
  2377. inserttypeconv(left,getansistringdef);
  2378. end;
  2379. stringdef :
  2380. begin
  2381. { we don't need string convertions here, }
  2382. { except if from widestring to ansistring }
  2383. { and vice versa (that can change the }
  2384. { length) }
  2385. if (left.nodetype=typeconvn) and
  2386. (ttypeconvnode(left).left.resultdef.typ=stringdef) and
  2387. not(is_wide_or_unicode_string(left.resultdef) xor
  2388. is_wide_or_unicode_string(ttypeconvnode(left).left.resultdef)) then
  2389. begin
  2390. hp:=ttypeconvnode(left).left;
  2391. ttypeconvnode(left).left:=nil;
  2392. left.free;
  2393. left:=hp;
  2394. end;
  2395. end;
  2396. orddef :
  2397. begin
  2398. { will be handled in simplify }
  2399. if not is_char(left.resultdef) and
  2400. not is_widechar(left.resultdef) then
  2401. CGMessage(type_e_mismatch);
  2402. end;
  2403. pointerdef :
  2404. begin
  2405. if is_pchar(left.resultdef) then
  2406. begin
  2407. hp := ccallparanode.create(left,nil);
  2408. result := ccallnode.createintern('fpc_pchar_length',hp);
  2409. { make sure the left node doesn't get disposed, since it's }
  2410. { reused in the new node (JM) }
  2411. left:=nil;
  2412. exit;
  2413. end
  2414. else if is_pwidechar(left.resultdef) then
  2415. begin
  2416. hp := ccallparanode.create(left,nil);
  2417. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  2418. { make sure the left node doesn't get disposed, since it's }
  2419. { reused in the new node (JM) }
  2420. left:=nil;
  2421. exit;
  2422. end
  2423. else
  2424. CGMessage(type_e_mismatch);
  2425. end;
  2426. arraydef :
  2427. begin
  2428. if is_open_array(left.resultdef) or
  2429. is_array_of_const(left.resultdef) then
  2430. begin
  2431. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2432. if assigned(hightree) then
  2433. result:=caddnode.create(addn,hightree,
  2434. cordconstnode.create(1,sinttype,false));
  2435. exit;
  2436. end
  2437. else if is_dynamic_array(left.resultdef) then
  2438. begin
  2439. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  2440. result := ccallnode.createintern('fpc_dynarray_length',hp);
  2441. { make sure the left node doesn't get disposed, since it's }
  2442. { reused in the new node (JM) }
  2443. left:=nil;
  2444. exit;
  2445. end
  2446. else
  2447. begin
  2448. { will be handled in simplify }
  2449. end;
  2450. end
  2451. else
  2452. CGMessage(type_e_mismatch);
  2453. end;
  2454. { shortstring return an 8 bit value as the length
  2455. is the first byte of the string }
  2456. if is_shortstring(left.resultdef) then
  2457. resultdef:=u8inttype
  2458. else
  2459. resultdef:=sinttype;
  2460. end;
  2461. in_typeinfo_x:
  2462. begin
  2463. if target_info.system in systems_managed_vm then
  2464. message(parser_e_feature_unsupported_for_vm);
  2465. if (left.resultdef.typ=enumdef) and
  2466. (tenumdef(left.resultdef).has_jumps) then
  2467. CGMessage(type_e_no_type_info);
  2468. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2469. resultdef:=voidpointertype;
  2470. end;
  2471. in_assigned_x:
  2472. begin
  2473. { the parser has already made sure the expression is valid }
  2474. { in case of a complex procvar, only check the "code" pointer }
  2475. if (tcallparanode(left).left.resultdef.typ=procvardef) and
  2476. not tprocvardef(tcallparanode(left).left.resultdef).is_addressonly then
  2477. begin
  2478. inserttypeconv_explicit(tcallparanode(left).left,search_system_type('TMETHOD').typedef);
  2479. tcallparanode(left).left:=csubscriptnode.create(tsym(tabstractrecorddef(tcallparanode(left).left.resultdef).symtable.find('CODE')),tcallparanode(left).left);
  2480. tcallparanode(left).get_paratype;
  2481. end;
  2482. { converting to an add node is tricky because of differences
  2483. in procvar handling between FPC and Delphi handling, so
  2484. handle specially }
  2485. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2486. resultdef:=pasbool8type;
  2487. end;
  2488. in_ofs_x :
  2489. internalerror(2000101001);
  2490. in_seg_x :
  2491. begin
  2492. if target_info.system in systems_managed_vm then
  2493. message(parser_e_feature_unsupported_for_vm);
  2494. set_varstate(left,vs_read,[]);
  2495. result:=cordconstnode.create(0,s32inttype,false);
  2496. end;
  2497. in_pred_x,
  2498. in_succ_x:
  2499. begin
  2500. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2501. resultdef:=left.resultdef;
  2502. if not is_ordinal(resultdef) then
  2503. CGMessage(type_e_ordinal_expr_expected)
  2504. else
  2505. begin
  2506. if (resultdef.typ=enumdef) and
  2507. (tenumdef(resultdef).has_jumps) and
  2508. not(m_delphi in current_settings.modeswitches) then
  2509. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  2510. end;
  2511. end;
  2512. in_copy_x:
  2513. result:=handle_copy;
  2514. in_initialize_x,
  2515. in_finalize_x:
  2516. begin
  2517. { inlined from pinline }
  2518. internalerror(200204231);
  2519. end;
  2520. in_setlength_x:
  2521. begin
  2522. result:=handle_setlength;
  2523. end;
  2524. in_inc_x,
  2525. in_dec_x:
  2526. begin
  2527. resultdef:=voidtype;
  2528. if assigned(left) then
  2529. begin
  2530. { first param must be var }
  2531. valid_for_var(tcallparanode(left).left,true);
  2532. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2533. if (left.resultdef.typ in [enumdef,pointerdef]) or
  2534. is_ordinal(left.resultdef) or
  2535. is_currency(left.resultdef) then
  2536. begin
  2537. { value of left gets changed -> must be unique }
  2538. set_unique(tcallparanode(left).left);
  2539. { two paras ? }
  2540. if assigned(tcallparanode(left).right) then
  2541. begin
  2542. if is_integer(tcallparanode(left).right.resultdef) then
  2543. begin
  2544. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2545. { when range/overflow checking is on, we
  2546. convert this to a regular add, and for proper
  2547. checking we need the original type }
  2548. if ([cs_check_range,cs_check_overflow]*current_settings.localswitches=[]) then
  2549. if (tcallparanode(left).left.resultdef.typ=pointerdef) then
  2550. begin
  2551. { don't convert values added to pointers into the pointer types themselves,
  2552. because that will turn signed values into unsigned ones, which then
  2553. goes wrong when they have to be multiplied with the size of the elements
  2554. to which the pointer points in ncginl (mantis #17342) }
  2555. if is_signed(tcallparanode(tcallparanode(left).right).left.resultdef) then
  2556. inserttypeconv(tcallparanode(tcallparanode(left).right).left,ptrsinttype)
  2557. else
  2558. inserttypeconv(tcallparanode(tcallparanode(left).right).left,ptruinttype)
  2559. end
  2560. else if is_integer(tcallparanode(left).left.resultdef) then
  2561. inserttypeconv(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef)
  2562. else
  2563. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  2564. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2565. { should be handled in the parser (JM) }
  2566. internalerror(2006020901);
  2567. end
  2568. else
  2569. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  2570. end;
  2571. end
  2572. else
  2573. begin
  2574. hp:=self;
  2575. if isunaryoverloaded(hp) then
  2576. begin
  2577. { inc(rec) and dec(rec) assigns result value to argument }
  2578. result:=cassignmentnode.create(tcallparanode(left).left.getcopy,hp);
  2579. exit;
  2580. end
  2581. else
  2582. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  2583. end;
  2584. end
  2585. else
  2586. CGMessagePos(fileinfo,type_e_mismatch);
  2587. end;
  2588. in_read_x,
  2589. in_readln_x,
  2590. in_readstr_x,
  2591. in_write_x,
  2592. in_writeln_x,
  2593. in_writestr_x :
  2594. begin
  2595. result := handle_read_write;
  2596. end;
  2597. in_settextbuf_file_x :
  2598. begin
  2599. if target_info.system in systems_managed_vm then
  2600. message(parser_e_feature_unsupported_for_vm);
  2601. resultdef:=voidtype;
  2602. { now we know the type of buffer }
  2603. hp:=ccallparanode.create(cordconstnode.create(
  2604. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  2605. result:=ccallnode.createintern('SETTEXTBUF',hp);
  2606. left:=nil;
  2607. end;
  2608. { the firstpass of the arg has been done in firstcalln ? }
  2609. in_reset_typedfile,
  2610. in_rewrite_typedfile :
  2611. begin
  2612. result := handle_reset_rewrite_typed;
  2613. end;
  2614. in_str_x_string :
  2615. begin
  2616. result:=handle_str;
  2617. end;
  2618. in_val_x :
  2619. begin
  2620. result:=handle_val;
  2621. end;
  2622. in_include_x_y,
  2623. in_exclude_x_y:
  2624. begin
  2625. resultdef:=voidtype;
  2626. { the parser already checks whether we have two (and exactly two) }
  2627. { parameters (JM) }
  2628. { first param must be var }
  2629. valid_for_var(tcallparanode(left).left,true);
  2630. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  2631. { check type }
  2632. if (left.resultdef.typ=setdef) then
  2633. begin
  2634. { insert a type conversion }
  2635. { to the type of the set elements }
  2636. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2637. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  2638. tsetdef(left.resultdef).elementdef);
  2639. end
  2640. else
  2641. CGMessage(type_e_mismatch);
  2642. end;
  2643. in_pack_x_y_z,
  2644. in_unpack_x_y_z :
  2645. begin
  2646. handle_pack_unpack;
  2647. end;
  2648. in_slice_x:
  2649. begin
  2650. if target_info.system in systems_managed_vm then
  2651. message(parser_e_feature_unsupported_for_vm);
  2652. result:=nil;
  2653. resultdef:=tcallparanode(left).left.resultdef;
  2654. if (resultdef.typ <> arraydef) then
  2655. CGMessagePos(left.fileinfo,type_e_mismatch)
  2656. else if is_packed_array(resultdef) then
  2657. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  2658. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  2659. CGMessagePos1(tcallparanode(left).right.fileinfo,
  2660. type_e_integer_expr_expected,
  2661. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  2662. end;
  2663. in_low_x,
  2664. in_high_x:
  2665. begin
  2666. case left.resultdef.typ of
  2667. orddef,
  2668. enumdef,
  2669. setdef:
  2670. ;
  2671. arraydef:
  2672. begin
  2673. if (inlinenumber=in_low_x) then
  2674. set_varstate(left,vs_read,[])
  2675. else
  2676. begin
  2677. if is_open_array(left.resultdef) or
  2678. is_array_of_const(left.resultdef) then
  2679. begin
  2680. set_varstate(left,vs_read,[]);
  2681. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  2682. end
  2683. else
  2684. if is_dynamic_array(left.resultdef) then
  2685. begin
  2686. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2687. { can't use inserttypeconv because we need }
  2688. { an explicit type conversion (JM) }
  2689. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  2690. result := ccallnode.createintern('fpc_dynarray_high',hp);
  2691. { make sure the left node doesn't get disposed, since it's }
  2692. { reused in the new node (JM) }
  2693. left:=nil;
  2694. end
  2695. else
  2696. begin
  2697. set_varstate(left,vs_read,[]);
  2698. end;
  2699. end;
  2700. end;
  2701. stringdef:
  2702. begin
  2703. if inlinenumber=in_low_x then
  2704. begin
  2705. set_varstate(left,vs_read,[]);
  2706. end
  2707. else
  2708. begin
  2709. if is_open_string(left.resultdef) then
  2710. begin
  2711. set_varstate(left,vs_read,[]);
  2712. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  2713. end
  2714. else if is_ansistring(left.resultdef) or
  2715. is_wide_or_unicode_string(left.resultdef) then
  2716. CGMessage(type_e_mismatch)
  2717. end;
  2718. end;
  2719. else
  2720. CGMessage(type_e_mismatch);
  2721. end;
  2722. end;
  2723. in_exp_real,
  2724. in_frac_real,
  2725. in_int_real,
  2726. in_cos_real,
  2727. in_sin_real,
  2728. in_arctan_real,
  2729. in_abs_real,
  2730. in_ln_real :
  2731. begin
  2732. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2733. { converting an int64 to double on platforms without }
  2734. { extended can cause precision loss }
  2735. if not(left.nodetype in [ordconstn,realconstn]) then
  2736. inserttypeconv(left,pbestrealtype^);
  2737. resultdef:=pbestrealtype^;
  2738. end;
  2739. in_trunc_real,
  2740. in_round_real :
  2741. begin
  2742. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2743. { for direct float rounding, no best real type cast should be necessary }
  2744. if not((left.resultdef.typ=floatdef) and
  2745. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,sc80real,s128real])) and
  2746. { converting an int64 to double on platforms without }
  2747. { extended can cause precision loss }
  2748. not(left.nodetype in [ordconstn,realconstn]) then
  2749. inserttypeconv(left,pbestrealtype^);
  2750. resultdef:=s64inttype;
  2751. end;
  2752. in_pi_real :
  2753. begin
  2754. resultdef:=pbestrealtype^;
  2755. end;
  2756. in_abs_long:
  2757. begin
  2758. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2759. inserttypeconv(left,s32inttype);
  2760. resultdef:=s32inttype;
  2761. end;
  2762. in_sqr_real,
  2763. in_sqrt_real :
  2764. begin
  2765. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2766. setfloatresultdef;
  2767. end;
  2768. {$ifdef SUPPORT_MMX}
  2769. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2770. begin
  2771. end;
  2772. {$endif SUPPORT_MMX}
  2773. in_unaligned_x:
  2774. begin
  2775. resultdef:=left.resultdef;
  2776. end;
  2777. in_assert_x_y :
  2778. begin
  2779. resultdef:=voidtype;
  2780. if assigned(left) then
  2781. begin
  2782. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2783. { check type }
  2784. if is_boolean(left.resultdef) then
  2785. begin
  2786. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2787. { must always be a string }
  2788. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  2789. end
  2790. else
  2791. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  2792. end
  2793. else
  2794. CGMessage(type_e_mismatch);
  2795. if (cs_do_assertion in current_settings.localswitches) then
  2796. include(current_procinfo.flags,pi_do_call);
  2797. end;
  2798. in_prefetch_var:
  2799. resultdef:=voidtype;
  2800. in_get_frame,
  2801. in_get_caller_frame,
  2802. in_get_caller_addr:
  2803. begin
  2804. resultdef:=voidpointertype;
  2805. end;
  2806. in_rol_x,
  2807. in_ror_x,
  2808. in_sar_x:
  2809. begin
  2810. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2811. resultdef:=left.resultdef;
  2812. end;
  2813. in_rol_x_y,
  2814. in_ror_x_y,
  2815. in_sar_x_y:
  2816. begin
  2817. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  2818. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  2819. resultdef:=tcallparanode(tcallparanode(left).right).left.resultdef;
  2820. end;
  2821. in_bsf_x,
  2822. in_bsr_x:
  2823. begin
  2824. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2825. if not is_integer(left.resultdef) then
  2826. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2827. if torddef(left.resultdef).ordtype in [u64bit, s64bit] then
  2828. resultdef:=u64inttype
  2829. else
  2830. resultdef:=u32inttype
  2831. end;
  2832. in_popcnt_x:
  2833. begin
  2834. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2835. if not is_integer(left.resultdef) then
  2836. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  2837. resultdef:=left.resultdef;
  2838. end;
  2839. in_objc_selector_x:
  2840. begin
  2841. result:=cobjcselectornode.create(left);
  2842. { reused }
  2843. left:=nil;
  2844. end;
  2845. in_objc_protocol_x:
  2846. begin
  2847. result:=cobjcprotocolnode.create(left);
  2848. { reused }
  2849. left:=nil;
  2850. end;
  2851. in_objc_encode_x:
  2852. begin
  2853. result:=handle_objc_encode;
  2854. end;
  2855. in_default_x:
  2856. begin
  2857. result:=handle_default;
  2858. end;
  2859. in_box_x:
  2860. begin
  2861. result:=handle_box;
  2862. end;
  2863. in_unbox_x_y:
  2864. begin
  2865. result:=handle_unbox;
  2866. end;
  2867. else
  2868. internalerror(8);
  2869. end;
  2870. end;
  2871. if not assigned(result) and not
  2872. codegenerror then
  2873. result:=simplify(false);
  2874. end;
  2875. function tinlinenode.pass_1 : tnode;
  2876. var
  2877. hp,hpp,resultnode : tnode;
  2878. shiftconst: longint;
  2879. tempnode: ttempcreatenode;
  2880. newstatement: tstatementnode;
  2881. newblock: tblocknode;
  2882. begin
  2883. result:=nil;
  2884. { if we handle writeln; left contains no valid address }
  2885. if assigned(left) then
  2886. begin
  2887. if left.nodetype=callparan then
  2888. tcallparanode(left).firstcallparan
  2889. else
  2890. firstpass(left);
  2891. end;
  2892. { intern const should already be handled }
  2893. if nf_inlineconst in flags then
  2894. internalerror(200104044);
  2895. case inlinenumber of
  2896. in_lo_qword,
  2897. in_hi_qword,
  2898. in_lo_long,
  2899. in_hi_long,
  2900. in_lo_word,
  2901. in_hi_word:
  2902. begin
  2903. shiftconst := 0;
  2904. case inlinenumber of
  2905. in_hi_qword:
  2906. shiftconst := 32;
  2907. in_hi_long:
  2908. shiftconst := 16;
  2909. in_hi_word:
  2910. shiftconst := 8;
  2911. end;
  2912. if shiftconst <> 0 then
  2913. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  2914. cordconstnode.create(shiftconst,u32inttype,false)),resultdef)
  2915. else
  2916. result := ctypeconvnode.create_internal(left,resultdef);
  2917. left := nil;
  2918. firstpass(result);
  2919. end;
  2920. in_sizeof_x:
  2921. begin
  2922. expectloc:=LOC_REGISTER;
  2923. end;
  2924. in_typeof_x:
  2925. begin
  2926. expectloc:=LOC_REGISTER;
  2927. end;
  2928. in_length_x:
  2929. begin
  2930. result:=first_length;
  2931. end;
  2932. in_typeinfo_x:
  2933. begin
  2934. expectloc:=LOC_REGISTER;
  2935. end;
  2936. in_assigned_x:
  2937. begin
  2938. result:=first_assigned;
  2939. end;
  2940. in_pred_x,
  2941. in_succ_x:
  2942. begin
  2943. expectloc:=LOC_REGISTER;
  2944. { in case of range/overflow checking, use a regular addnode
  2945. because it's too complex to handle correctly otherwise }
  2946. {$ifndef jvm}
  2947. { enums are class instances in the JVM -> always need conversion }
  2948. if ([cs_check_overflow,cs_check_range]*current_settings.localswitches)<>[] then
  2949. {$endif}
  2950. begin
  2951. { create constant 1 }
  2952. hp:=cordconstnode.create(1,left.resultdef,false);
  2953. typecheckpass(hp);
  2954. if not is_integer(hp.resultdef) then
  2955. inserttypeconv_internal(hp,sinttype);
  2956. { avoid type errors from the addn/subn }
  2957. if not is_integer(left.resultdef) then
  2958. inserttypeconv_internal(left,sinttype);
  2959. { addition/substraction depending on succ/pred }
  2960. if inlinenumber=in_succ_x then
  2961. hp:=caddnode.create(addn,left,hp)
  2962. else
  2963. hp:=caddnode.create(subn,left,hp);
  2964. { assign result of addition }
  2965. if not(is_integer(resultdef)) then
  2966. inserttypeconv(hp,torddef.create(
  2967. {$ifdef cpu64bitaddr}
  2968. s64bit,
  2969. {$else cpu64bitaddr}
  2970. s32bit,
  2971. {$endif cpu64bitaddr}
  2972. get_min_value(resultdef),
  2973. get_max_value(resultdef)))
  2974. else
  2975. inserttypeconv(hp,resultdef);
  2976. { avoid any possible errors/warnings }
  2977. inserttypeconv_internal(hp,resultdef);
  2978. { firstpass it }
  2979. firstpass(hp);
  2980. { left is reused }
  2981. left:=nil;
  2982. { return new node }
  2983. result:=hp;
  2984. end;
  2985. end;
  2986. in_setlength_x:
  2987. result:=first_setlength;
  2988. in_copy_x:
  2989. result:=first_copy;
  2990. in_initialize_x,
  2991. in_finalize_x:
  2992. begin
  2993. expectloc:=LOC_VOID;
  2994. end;
  2995. in_inc_x,
  2996. in_dec_x:
  2997. begin
  2998. expectloc:=LOC_VOID;
  2999. { range/overflow checking doesn't work properly }
  3000. { with the inc/dec code that's generated (JM) }
  3001. if ((current_settings.localswitches * [cs_check_overflow,cs_check_range] <> []) and
  3002. { No overflow check for pointer operations, because inc(pointer,-1) will always
  3003. trigger an overflow. For uint32 it works because then the operation is done
  3004. in 64bit. Range checking is not applicable to pointers either }
  3005. (tcallparanode(left).left.resultdef.typ<>pointerdef))
  3006. {$ifdef jvm}
  3007. { enums are class instances on the JVM -> special treatment }
  3008. or (tcallparanode(left).left.resultdef.typ=enumdef)
  3009. {$endif}
  3010. then
  3011. { convert to simple add (JM) }
  3012. begin
  3013. newblock := internalstatements(newstatement);
  3014. { extra parameter? }
  3015. if assigned(tcallparanode(left).right) then
  3016. begin
  3017. { Yes, use for add node }
  3018. hpp := tcallparanode(tcallparanode(left).right).left;
  3019. tcallparanode(tcallparanode(left).right).left := nil;
  3020. if assigned(tcallparanode(tcallparanode(left).right).right) then
  3021. CGMessage(parser_e_illegal_expression);
  3022. end
  3023. else
  3024. begin
  3025. { no, create constant 1 }
  3026. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  3027. end;
  3028. typecheckpass(hpp);
  3029. { make sure we don't call functions part of the left node twice (and generally }
  3030. { optimize the code generation) }
  3031. if node_complexity(tcallparanode(left).left) > 1 then
  3032. begin
  3033. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  3034. addstatement(newstatement,tempnode);
  3035. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  3036. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  3037. hp := cderefnode.create(ctemprefnode.create(tempnode));
  3038. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  3039. end
  3040. else
  3041. begin
  3042. hp := tcallparanode(left).left.getcopy;
  3043. tempnode := nil;
  3044. end;
  3045. resultnode := hp.getcopy;
  3046. { avoid type errors from the addn/subn }
  3047. if not is_integer(resultnode.resultdef) then
  3048. begin
  3049. inserttypeconv_internal(hp,sinttype);
  3050. inserttypeconv_internal(hpp,sinttype);
  3051. end;
  3052. { addition/substraction depending on inc/dec }
  3053. if inlinenumber = in_inc_x then
  3054. hpp := caddnode.create(addn,hp,hpp)
  3055. else
  3056. hpp := caddnode.create(subn,hp,hpp);
  3057. { assign result of addition }
  3058. if not(is_integer(resultnode.resultdef)) then
  3059. inserttypeconv(hpp,torddef.create(
  3060. {$ifdef cpu64bitaddr}
  3061. s64bit,
  3062. {$else cpu64bitaddr}
  3063. s32bit,
  3064. {$endif cpu64bitaddr}
  3065. get_min_value(resultnode.resultdef),
  3066. get_max_value(resultnode.resultdef)))
  3067. else
  3068. inserttypeconv(hpp,resultnode.resultdef);
  3069. { avoid any possible warnings }
  3070. inserttypeconv_internal(hpp,resultnode.resultdef);
  3071. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  3072. { deallocate the temp }
  3073. if assigned(tempnode) then
  3074. addstatement(newstatement,ctempdeletenode.create(tempnode));
  3075. { firstpass it }
  3076. firstpass(tnode(newblock));
  3077. { return new node }
  3078. result := newblock;
  3079. end;
  3080. end;
  3081. in_include_x_y,
  3082. in_exclude_x_y:
  3083. begin
  3084. result:=first_IncludeExclude;
  3085. end;
  3086. in_pack_x_y_z,
  3087. in_unpack_x_y_z:
  3088. begin
  3089. result:=first_pack_unpack;
  3090. end;
  3091. in_exp_real:
  3092. begin
  3093. result:= first_exp_real;
  3094. end;
  3095. in_round_real:
  3096. begin
  3097. result:= first_round_real;
  3098. end;
  3099. in_trunc_real:
  3100. begin
  3101. result:= first_trunc_real;
  3102. end;
  3103. in_int_real:
  3104. begin
  3105. result:= first_int_real;
  3106. end;
  3107. in_frac_real:
  3108. begin
  3109. result:= first_frac_real;
  3110. end;
  3111. in_cos_real:
  3112. begin
  3113. result:= first_cos_real;
  3114. end;
  3115. in_sin_real:
  3116. begin
  3117. result := first_sin_real;
  3118. end;
  3119. in_arctan_real:
  3120. begin
  3121. result := first_arctan_real;
  3122. end;
  3123. in_pi_real :
  3124. begin
  3125. result := first_pi;
  3126. end;
  3127. in_abs_real:
  3128. begin
  3129. result := first_abs_real;
  3130. end;
  3131. in_abs_long:
  3132. begin
  3133. result := first_abs_long;
  3134. end;
  3135. in_sqr_real:
  3136. begin
  3137. result := first_sqr_real;
  3138. end;
  3139. in_sqrt_real:
  3140. begin
  3141. result := first_sqrt_real;
  3142. end;
  3143. in_ln_real:
  3144. begin
  3145. result := first_ln_real;
  3146. end;
  3147. {$ifdef SUPPORT_MMX}
  3148. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  3149. begin
  3150. end;
  3151. {$endif SUPPORT_MMX}
  3152. in_assert_x_y :
  3153. begin
  3154. result:=first_assert;
  3155. end;
  3156. in_low_x,
  3157. in_high_x:
  3158. internalerror(200104047);
  3159. in_slice_x:
  3160. internalerror(2005101501);
  3161. in_ord_x,
  3162. in_chr_byte:
  3163. begin
  3164. { should not happend as it's converted to typeconv }
  3165. internalerror(200104045);
  3166. end;
  3167. in_ofs_x :
  3168. internalerror(2000101001);
  3169. in_seg_x :
  3170. internalerror(200104046);
  3171. in_settextbuf_file_x,
  3172. in_reset_typedfile,
  3173. in_rewrite_typedfile,
  3174. in_str_x_string,
  3175. in_val_x,
  3176. in_read_x,
  3177. in_readln_x,
  3178. in_write_x,
  3179. in_writeln_x :
  3180. begin
  3181. { should be handled by pass_typecheck }
  3182. internalerror(200108234);
  3183. end;
  3184. in_get_frame:
  3185. begin
  3186. result:=first_get_frame;
  3187. end;
  3188. in_get_caller_frame:
  3189. begin
  3190. expectloc:=LOC_REGISTER;
  3191. end;
  3192. in_get_caller_addr:
  3193. begin
  3194. expectloc:=LOC_REGISTER;
  3195. end;
  3196. in_prefetch_var:
  3197. begin
  3198. expectloc:=LOC_VOID;
  3199. end;
  3200. in_unaligned_x:
  3201. begin
  3202. expectloc:=tcallparanode(left).left.expectloc;
  3203. end;
  3204. in_rol_x,
  3205. in_rol_x_y,
  3206. in_ror_x,
  3207. in_ror_x_y,
  3208. in_sar_x,
  3209. in_sar_x_y,
  3210. in_bsf_x,
  3211. in_bsr_x:
  3212. expectloc:=LOC_REGISTER;
  3213. in_popcnt_x:
  3214. result:=first_popcnt;
  3215. in_new_x:
  3216. result:=first_new;
  3217. in_box_x:
  3218. result:=first_box;
  3219. in_unbox_x_y:
  3220. result:=first_unbox;
  3221. else
  3222. internalerror(89);
  3223. end;
  3224. end;
  3225. {$maxfpuregisters default}
  3226. function tinlinenode.docompare(p: tnode): boolean;
  3227. begin
  3228. docompare :=
  3229. inherited docompare(p) and
  3230. (inlinenumber = tinlinenode(p).inlinenumber);
  3231. end;
  3232. function tinlinenode.first_pi : tnode;
  3233. begin
  3234. result:=crealconstnode.create(getpi,pbestrealtype^);
  3235. end;
  3236. function tinlinenode.first_arctan_real : tnode;
  3237. begin
  3238. { create the call to the helper }
  3239. { on entry left node contains the parameter }
  3240. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  3241. ccallparanode.create(left,nil));
  3242. left := nil;
  3243. end;
  3244. function tinlinenode.first_abs_real : tnode;
  3245. begin
  3246. { create the call to the helper }
  3247. { on entry left node contains the parameter }
  3248. first_abs_real := ccallnode.createintern('fpc_abs_real',
  3249. ccallparanode.create(left,nil));
  3250. left := nil;
  3251. end;
  3252. function tinlinenode.first_sqr_real : tnode;
  3253. begin
  3254. {$ifndef cpufpemu}
  3255. { this procedure might be only used for cpus definining cpufpemu else
  3256. the optimizer might go into an endless loop when doing x*x -> changes }
  3257. internalerror(2011092401);
  3258. {$endif cpufpemu}
  3259. { create the call to the helper }
  3260. { on entry left node contains the parameter }
  3261. first_sqr_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqr_real',
  3262. ccallparanode.create(left,nil)),resultdef);
  3263. left := nil;
  3264. end;
  3265. function tinlinenode.first_sqrt_real : tnode;
  3266. begin
  3267. { create the call to the helper }
  3268. { on entry left node contains the parameter }
  3269. first_sqrt_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqrt_real',
  3270. ccallparanode.create(left,nil)),resultdef);
  3271. left := nil;
  3272. end;
  3273. function tinlinenode.first_ln_real : tnode;
  3274. begin
  3275. { create the call to the helper }
  3276. { on entry left node contains the parameter }
  3277. first_ln_real := ccallnode.createintern('fpc_ln_real',
  3278. ccallparanode.create(left,nil));
  3279. left := nil;
  3280. end;
  3281. function tinlinenode.first_cos_real : tnode;
  3282. begin
  3283. { create the call to the helper }
  3284. { on entry left node contains the parameter }
  3285. first_cos_real := ccallnode.createintern('fpc_cos_real',
  3286. ccallparanode.create(left,nil));
  3287. left := nil;
  3288. end;
  3289. function tinlinenode.first_sin_real : tnode;
  3290. begin
  3291. { create the call to the helper }
  3292. { on entry left node contains the parameter }
  3293. first_sin_real := ccallnode.createintern('fpc_sin_real',
  3294. ccallparanode.create(left,nil));
  3295. left := nil;
  3296. end;
  3297. function tinlinenode.first_exp_real : tnode;
  3298. begin
  3299. { create the call to the helper }
  3300. { on entry left node contains the parameter }
  3301. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  3302. left := nil;
  3303. end;
  3304. function tinlinenode.first_int_real : tnode;
  3305. begin
  3306. { create the call to the helper }
  3307. { on entry left node contains the parameter }
  3308. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  3309. left := nil;
  3310. end;
  3311. function tinlinenode.first_frac_real : tnode;
  3312. begin
  3313. { create the call to the helper }
  3314. { on entry left node contains the parameter }
  3315. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  3316. left := nil;
  3317. end;
  3318. function tinlinenode.first_round_real : tnode;
  3319. begin
  3320. { create the call to the helper }
  3321. { on entry left node contains the parameter }
  3322. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  3323. left := nil;
  3324. end;
  3325. function tinlinenode.first_trunc_real : tnode;
  3326. begin
  3327. { create the call to the helper }
  3328. { on entry left node contains the parameter }
  3329. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  3330. left := nil;
  3331. end;
  3332. function tinlinenode.first_abs_long : tnode;
  3333. begin
  3334. expectloc:=LOC_REGISTER;
  3335. result:=nil;
  3336. end;
  3337. function tinlinenode.first_IncludeExclude: tnode;
  3338. begin
  3339. result:=nil;
  3340. expectloc:=LOC_VOID;
  3341. end;
  3342. function tinlinenode.first_get_frame: tnode;
  3343. begin
  3344. include(current_procinfo.flags,pi_needs_stackframe);
  3345. expectloc:=LOC_CREGISTER;
  3346. result:=nil;
  3347. end;
  3348. function tinlinenode.first_setlength: tnode;
  3349. var
  3350. paras : tnode;
  3351. npara,
  3352. ppn : tcallparanode;
  3353. dims,
  3354. counter : integer;
  3355. isarray : boolean;
  3356. destppn : tnode;
  3357. newstatement : tstatementnode;
  3358. temp : ttempcreatenode;
  3359. newblock : tnode;
  3360. begin
  3361. paras:=left;
  3362. ppn:=tcallparanode(paras);
  3363. dims:=0;
  3364. while assigned(ppn.right) do
  3365. begin
  3366. inc(dims);
  3367. ppn:=tcallparanode(ppn.right);
  3368. end;
  3369. destppn:=ppn.left;
  3370. isarray:=is_dynamic_array(destppn.resultdef);
  3371. { first param must be a string or dynamic array ...}
  3372. if isarray then
  3373. begin
  3374. { create statements with call initialize the arguments and
  3375. call fpc_dynarr_setlength }
  3376. newblock:=internalstatements(newstatement);
  3377. { get temp for array of lengths }
  3378. temp:=ctempcreatenode.create(sinttype,dims*sinttype.size,tt_persistent,false);
  3379. addstatement(newstatement,temp);
  3380. { load array of lengths }
  3381. ppn:=tcallparanode(paras);
  3382. counter:=dims-1;
  3383. while assigned(ppn.right) do
  3384. begin
  3385. addstatement(newstatement,cassignmentnode.create(
  3386. ctemprefnode.create_offset(temp,counter*sinttype.size),
  3387. ppn.left));
  3388. ppn.left:=nil;
  3389. dec(counter);
  3390. ppn:=tcallparanode(ppn.right);
  3391. end;
  3392. { destppn is also reused }
  3393. ppn.left:=nil;
  3394. { create call to fpc_dynarr_setlength }
  3395. npara:=ccallparanode.create(caddrnode.create_internal
  3396. (ctemprefnode.create(temp)),
  3397. ccallparanode.create(cordconstnode.create
  3398. (dims,sinttype,true),
  3399. ccallparanode.create(caddrnode.create_internal
  3400. (crttinode.create(tstoreddef(destppn.resultdef),initrtti,rdt_normal)),
  3401. ccallparanode.create(ctypeconvnode.create_internal(destppn,voidpointertype),nil))));
  3402. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',npara));
  3403. addstatement(newstatement,ctempdeletenode.create(temp));
  3404. end
  3405. else if is_ansistring(destppn.resultdef) then
  3406. begin
  3407. newblock:=ccallnode.createintern(
  3408. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',
  3409. ccallparanode.create(
  3410. cordconstnode.create(getparaencoding(destppn.resultdef),u16inttype,true),
  3411. paras
  3412. )
  3413. );
  3414. { we reused the parameters, make sure we don't release them }
  3415. left:=nil;
  3416. end
  3417. else
  3418. begin
  3419. { we can reuse the supplied parameters }
  3420. newblock:=ccallnode.createintern(
  3421. 'fpc_'+tstringdef(destppn.resultdef).stringtypname+'_setlength',paras);
  3422. { we reused the parameters, make sure we don't release them }
  3423. left:=nil;
  3424. end;
  3425. result:=newblock;
  3426. end;
  3427. function tinlinenode.first_copy: tnode;
  3428. var
  3429. lowppn,
  3430. highppn,
  3431. npara,
  3432. paras : tnode;
  3433. ppn : tcallparanode;
  3434. paradef : tdef;
  3435. counter : integer;
  3436. begin
  3437. { determine copy function to use based on the first argument,
  3438. also count the number of arguments in this loop }
  3439. counter:=1;
  3440. paras:=left;
  3441. ppn:=tcallparanode(paras);
  3442. while assigned(ppn.right) do
  3443. begin
  3444. inc(counter);
  3445. ppn:=tcallparanode(ppn.right);
  3446. end;
  3447. paradef:=ppn.left.resultdef;
  3448. { fill up third parameter }
  3449. if counter=2 then
  3450. begin
  3451. paras:=ccallparanode.create(cordconstnode.create(torddef(sinttype).high,sinttype,false),paras);
  3452. counter:=3;
  3453. end;
  3454. if is_ansistring(resultdef) then
  3455. { keep the specific kind of ansistringdef as result }
  3456. result:=ccallnode.createinternres('fpc_ansistr_copy',paras,resultdef)
  3457. else if is_widestring(resultdef) then
  3458. result:=ccallnode.createintern('fpc_widestr_copy',paras)
  3459. else if is_unicodestring(resultdef) then
  3460. result:=ccallnode.createintern('fpc_unicodestr_copy',paras)
  3461. { can't check for resultdef = cansichartype, because resultdef=
  3462. cshortstringtype here }
  3463. else if is_char(paradef) then
  3464. result:=ccallnode.createintern('fpc_char_copy',paras)
  3465. else if is_dynamic_array(resultdef) then
  3466. begin
  3467. { create statements with call }
  3468. case counter of
  3469. 1:
  3470. begin
  3471. { copy the whole array using [0..high(sizeint)] range }
  3472. highppn:=cordconstnode.create(torddef(sinttype).high,sinttype,false);
  3473. lowppn:=cordconstnode.create(0,sinttype,false);
  3474. end;
  3475. 3:
  3476. begin
  3477. highppn:=tcallparanode(paras).left.getcopy;
  3478. lowppn:=tcallparanode(tcallparanode(paras).right).left.getcopy;
  3479. end;
  3480. else
  3481. internalerror(2012100701);
  3482. end;
  3483. { create call to fpc_dynarray_copy }
  3484. npara:=ccallparanode.create(highppn,
  3485. ccallparanode.create(lowppn,
  3486. ccallparanode.create(caddrnode.create_internal
  3487. (crttinode.create(tstoreddef(paradef),initrtti,rdt_normal)),
  3488. ccallparanode.create
  3489. (ctypeconvnode.create_internal(ppn.left,voidpointertype),nil))));
  3490. result:=ccallnode.createinternres('fpc_dynarray_copy',npara,paradef);
  3491. ppn.left:=nil;
  3492. paras.free;
  3493. end
  3494. else
  3495. result:=ccallnode.createintern('fpc_shortstr_copy',paras);
  3496. { parameters are reused }
  3497. left:=nil;
  3498. end;
  3499. function tinlinenode.first_new: tnode;
  3500. begin
  3501. internalerror(2011012201);
  3502. result:=nil;
  3503. end;
  3504. function tinlinenode.first_length: tnode;
  3505. begin
  3506. result:=nil;
  3507. if is_shortstring(left.resultdef) then
  3508. expectloc:=left.expectloc
  3509. else
  3510. begin
  3511. { ansi/wide string }
  3512. expectloc:=LOC_REGISTER;
  3513. end;
  3514. end;
  3515. function tinlinenode.first_assigned: tnode;
  3516. begin
  3517. result:=nil;
  3518. expectloc := LOC_JUMP;
  3519. end;
  3520. function tinlinenode.first_assert: tnode;
  3521. var
  3522. paras: tcallparanode;
  3523. begin
  3524. paras:=tcallparanode(tcallparanode(left).right);
  3525. paras:=ccallparanode.create(cstringconstnode.createstr(current_module.sourcefiles.get_file_name(current_filepos.fileindex)),paras);
  3526. paras:=ccallparanode.create(genintconstnode(fileinfo.line),paras);
  3527. {$if defined(x86) or defined(arm) or defined(jvm)}
  3528. paras:=ccallparanode.create(geninlinenode(in_get_frame,false,nil),paras);
  3529. {$else}
  3530. paras:=ccallparanode.create(ccallnode.createinternfromunit('SYSTEM','GET_FRAME',nil),paras);
  3531. {$endif}
  3532. result:=cifnode.create(cnotnode.create(tcallparanode(left).left),
  3533. ccallnode.createintern('fpc_assert',paras),nil);
  3534. tcallparanode(left).left:=nil;
  3535. tcallparanode(left).right:=nil;
  3536. end;
  3537. function tinlinenode.first_popcnt: tnode;
  3538. var
  3539. suffix : string;
  3540. begin
  3541. case torddef(left.resultdef).ordtype of
  3542. u8bit: suffix:='byte';
  3543. u16bit: suffix:='word';
  3544. u32bit: suffix:='dword';
  3545. u64bit: suffix:='qword';
  3546. else
  3547. internalerror(2012082601);
  3548. end;
  3549. result:=ccallnode.createintern('fpc_popcnt_'+suffix,ccallparanode.create(left,nil));
  3550. left:=nil;
  3551. end;
  3552. function tinlinenode.handle_box: tnode;
  3553. begin
  3554. result:=nil;
  3555. if not assigned(left) or
  3556. assigned(tcallparanode(left).right) then
  3557. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalBox');
  3558. resultdef:=class_tobject;
  3559. end;
  3560. function tinlinenode.handle_unbox: tnode;
  3561. begin
  3562. result:=nil;
  3563. if not assigned(left) or
  3564. not assigned(tcallparanode(left).right) or
  3565. assigned(tcallparanode(tcallparanode(left).right).right) then
  3566. CGMessage1(parser_e_wrong_parameter_size,'FpcInternalUnBox');
  3567. if tcallparanode(left).left.nodetype<>typen then
  3568. internalerror(2011071701);
  3569. ttypenode(tcallparanode(left).left).allowed:=true;
  3570. resultdef:=tcallparanode(left).left.resultdef;
  3571. end;
  3572. function tinlinenode.first_pack_unpack: tnode;
  3573. var
  3574. loopstatement : tstatementnode;
  3575. loop : tblocknode;
  3576. loopvar : ttempcreatenode;
  3577. tempnode,
  3578. source,
  3579. target,
  3580. index,
  3581. unpackednode,
  3582. packednode,
  3583. sourcevecindex,
  3584. targetvecindex,
  3585. loopbody : tnode;
  3586. temprangedef : tdef;
  3587. ulorange,
  3588. uhirange,
  3589. plorange,
  3590. phirange : TConstExprInt;
  3591. begin
  3592. { transform into a for loop which assigns the data of the (un)packed }
  3593. { array to the other one }
  3594. source := left;
  3595. if (inlinenumber = in_unpack_x_y_z) then
  3596. begin
  3597. target := tcallparanode(source).right;
  3598. index := tcallparanode(target).right;
  3599. packednode := tcallparanode(source).left;
  3600. unpackednode := tcallparanode(target).left;
  3601. end
  3602. else
  3603. begin
  3604. index := tcallparanode(source).right;
  3605. target := tcallparanode(index).right;
  3606. packednode := tcallparanode(target).left;
  3607. unpackednode := tcallparanode(source).left;
  3608. end;
  3609. source := tcallparanode(source).left;
  3610. target := tcallparanode(target).left;
  3611. index := tcallparanode(index).left;
  3612. loop := internalstatements(loopstatement);
  3613. loopvar := ctempcreatenode.create(
  3614. tarraydef(packednode.resultdef).rangedef,
  3615. tarraydef(packednode.resultdef).rangedef.size,
  3616. tt_persistent,true);
  3617. addstatement(loopstatement,loopvar);
  3618. { For range checking: we have to convert to an integer type (in case the index type }
  3619. { is an enum), add the index and loop variable together, convert the result }
  3620. { implicitly to an orddef with range equal to the rangedef to get range checking }
  3621. { and finally convert it explicitly back to the actual rangedef to avoid type }
  3622. { errors }
  3623. temprangedef:=nil;
  3624. getrange(unpackednode.resultdef,ulorange,uhirange);
  3625. getrange(packednode.resultdef,plorange,phirange);
  3626. temprangedef:=torddef.create(torddef(sinttype).ordtype,ulorange,uhirange);
  3627. sourcevecindex := ctemprefnode.create(loopvar);
  3628. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  3629. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  3630. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  3631. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  3632. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  3633. if (inlinenumber = in_pack_x_y_z) then
  3634. begin
  3635. { swap source and target vec indices }
  3636. tempnode := sourcevecindex;
  3637. sourcevecindex := targetvecindex;
  3638. targetvecindex := tempnode;
  3639. end;
  3640. { create the assignment in the loop body }
  3641. loopbody :=
  3642. cassignmentnode.create(
  3643. cvecnode.create(target.getcopy,targetvecindex),
  3644. cvecnode.create(source.getcopy,sourcevecindex)
  3645. );
  3646. { create the actual for loop }
  3647. tempnode := cfornode.create(
  3648. ctemprefnode.create(loopvar),
  3649. cinlinenode.create(in_low_x,false,packednode.getcopy),
  3650. cinlinenode.create(in_high_x,false,packednode.getcopy),
  3651. loopbody,
  3652. false);
  3653. addstatement(loopstatement,tempnode);
  3654. { free the loop counter }
  3655. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  3656. result := loop;
  3657. end;
  3658. end.