ninl.pas 109 KB

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