ninl.pas 128 KB

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