ninl.pas 132 KB

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