ninl.pas 144 KB

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