ninl.pas 112 KB

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