ninl.pas 131 KB

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