ninl.pas 144 KB

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