ninl.pas 125 KB

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