ninl.pas 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818
  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. { zero-based arrays (of char) can be implicitely converted to ansistring }
  942. else if is_zero_based_array(sourcepara.resultdef) then
  943. procname := procname + 'ansistr'
  944. else
  945. procname := procname + 'shortstr';
  946. { set up the correct parameters for the call: the code para... }
  947. newparas := codepara;
  948. { and the source para }
  949. codepara.right := sourcepara;
  950. { sizepara either contains nil if none is needed (which is ok, since }
  951. { then the next statement severes any possible links with other paras }
  952. { that sourcepara may have) or it contains the necessary size para and }
  953. { its right field is nil }
  954. sourcepara.right := sizepara;
  955. { create the call and assign the result to dest (val helpers are functions).
  956. Use a trick to prevent a type size mismatch warning to be generated by the
  957. assignment node. First convert implicitly to the resultdef. This will insert
  958. the range check. The Second conversion is done explicitly to hide the implicit conversion
  959. for the assignment node and therefor preventing the warning (PFV) }
  960. addstatement(newstatement,cassignmentnode.create(
  961. destpara.left,ctypeconvnode.create_internal(ctypeconvnode.create(ccallnode.createintern(procname,newparas),destpara.left.resultdef),destpara.left.resultdef)));
  962. { dispose of the enclosing paranode of the destination }
  963. destpara.left := nil;
  964. destpara.right := nil;
  965. destpara.free;
  966. { check if we used a temp for code and whether we have to store }
  967. { it to the real code parameter }
  968. if assigned(orgcode) then
  969. addstatement(newstatement,cassignmentnode.create(
  970. orgcode,
  971. ctypeconvnode.create_internal(
  972. ctemprefnode.create(tempcode),orgcode.resultdef)));
  973. { release the temp if we allocated one }
  974. if assigned(tempcode) then
  975. addstatement(newstatement,ctempdeletenode.create(tempcode));
  976. { free the errornode }
  977. result.free;
  978. { and return it }
  979. result := newblock;
  980. end;
  981. {$maxfpuregisters 0}
  982. function getpi : bestreal;
  983. begin
  984. {$ifdef x86}
  985. { x86 has pi in hardware }
  986. result:=pi;
  987. {$else x86}
  988. {$ifdef cpuextended}
  989. result:=MathPiExtended.Value;
  990. {$else cpuextended}
  991. result:=MathPi.Value;
  992. {$endif cpuextended}
  993. {$endif x86}
  994. end;
  995. function tinlinenode.pass_typecheck:tnode;
  996. function do_lowhigh(def:tdef) : tnode;
  997. var
  998. v : tconstexprint;
  999. enum : tenumsym;
  1000. hp : tnode;
  1001. begin
  1002. case def.typ of
  1003. orddef:
  1004. begin
  1005. set_varstate(left,vs_read,[]);
  1006. if inlinenumber=in_low_x then
  1007. v:=torddef(def).low
  1008. else
  1009. v:=torddef(def).high;
  1010. { low/high of torddef are longints, so we need special }
  1011. { handling for cardinal and 64bit types (JM) }
  1012. { 1.0.x doesn't support int64($ffffffff) correct, it'll expand
  1013. to -1 instead of staying $ffffffff. Therefor we use $ffff with
  1014. shl twice (PFV) }
  1015. case torddef(def).ordtype of
  1016. s64bit,scurrency :
  1017. begin
  1018. if (inlinenumber=in_low_x) then
  1019. v := int64($80000000) shl 32
  1020. else
  1021. v := (int64($7fffffff) shl 32) or int64($ffff) shl 16 or int64($ffff)
  1022. end;
  1023. u64bit :
  1024. begin
  1025. { we have to use a dirty trick for high(qword), }
  1026. { because it's bigger than high(tconstexprint) (JM) }
  1027. v := 0
  1028. end
  1029. else
  1030. begin
  1031. if not is_signed(def) then
  1032. v := cardinal(v);
  1033. end;
  1034. end;
  1035. hp:=cordconstnode.create(v,def,true);
  1036. typecheckpass(hp);
  1037. { fix high(qword) }
  1038. if (torddef(def).ordtype=u64bit) and
  1039. (inlinenumber = in_high_x) then
  1040. tordconstnode(hp).value := -1; { is the same as qword($ffffffffffffffff) }
  1041. do_lowhigh:=hp;
  1042. end;
  1043. enumdef:
  1044. begin
  1045. set_varstate(left,vs_read,[]);
  1046. enum:=tenumsym(tenumdef(def).firstenum);
  1047. v:=tenumdef(def).maxval;
  1048. if inlinenumber=in_high_x then
  1049. while assigned(enum) and (enum.value <> v) do
  1050. enum:=enum.nextenum;
  1051. if not assigned(enum) then
  1052. internalerror(309993)
  1053. else
  1054. hp:=genenumnode(enum);
  1055. do_lowhigh:=hp;
  1056. end;
  1057. else
  1058. internalerror(87);
  1059. end;
  1060. end;
  1061. function getconstrealvalue : bestreal;
  1062. begin
  1063. case left.nodetype of
  1064. ordconstn:
  1065. getconstrealvalue:=tordconstnode(left).value;
  1066. realconstn:
  1067. getconstrealvalue:=trealconstnode(left).value_real;
  1068. else
  1069. internalerror(309992);
  1070. end;
  1071. end;
  1072. procedure setconstrealvalue(r : bestreal);
  1073. begin
  1074. result:=crealconstnode.create(r,pbestrealtype^);
  1075. end;
  1076. function handle_ln_const(r : bestreal) : tnode;
  1077. begin
  1078. if r<=0.0 then
  1079. if (cs_check_range in current_settings.localswitches) or
  1080. (cs_check_overflow in current_settings.localswitches) then
  1081. begin
  1082. result:=crealconstnode.create(0,pbestrealtype^);
  1083. CGMessage(type_e_wrong_math_argument)
  1084. end
  1085. else
  1086. begin
  1087. if r=0.0 then
  1088. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1089. else
  1090. result:=crealconstnode.create(MathNegInf.Value,pbestrealtype^)
  1091. end
  1092. else
  1093. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1094. end;
  1095. function handle_sqrt_const(r : bestreal) : tnode;
  1096. begin
  1097. if r<0.0 then
  1098. if (cs_check_range in current_settings.localswitches) or
  1099. (cs_check_overflow in current_settings.localswitches) then
  1100. begin
  1101. result:=crealconstnode.create(0,pbestrealtype^);
  1102. CGMessage(type_e_wrong_math_argument)
  1103. end
  1104. else
  1105. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1106. else
  1107. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1108. end;
  1109. procedure setfloatresultdef;
  1110. begin
  1111. if (left.resultdef.typ=floatdef) and
  1112. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,s128real]) then
  1113. resultdef:=left.resultdef
  1114. else
  1115. begin
  1116. inserttypeconv(left,pbestrealtype^);
  1117. resultdef:=pbestrealtype^;
  1118. end;
  1119. end;
  1120. procedure handle_pack_unpack;
  1121. var
  1122. source, target, index: tcallparanode;
  1123. unpackedarraydef, packedarraydef: tarraydef;
  1124. tempindex: TConstExprInt;
  1125. begin
  1126. resultdef:=voidtype;
  1127. unpackedarraydef := nil;
  1128. packedarraydef := nil;
  1129. source := tcallparanode(left);
  1130. if (inlinenumber = in_unpack_x_y_z) then
  1131. begin
  1132. target := tcallparanode(source.right);
  1133. index := tcallparanode(target.right);
  1134. { source must be a packed array }
  1135. if not is_packed_array(source.left.resultdef) then
  1136. CGMessagePos2(source.left.fileinfo,type_e_got_expected_packed_array,'1',source.left.resultdef.GetTypeName)
  1137. else
  1138. packedarraydef := tarraydef(source.left.resultdef);
  1139. { target can be any kind of array, as long as it's not packed }
  1140. if (target.left.resultdef.typ <> arraydef) or
  1141. is_packed_array(target.left.resultdef) then
  1142. CGMessagePos2(target.left.fileinfo,type_e_got_expected_unpacked_array,'2',target.left.resultdef.GetTypeName)
  1143. else
  1144. unpackedarraydef := tarraydef(target.left.resultdef);
  1145. end
  1146. else
  1147. begin
  1148. index := tcallparanode(source.right);
  1149. target := tcallparanode(index.right);
  1150. { source can be any kind of array, as long as it's not packed }
  1151. if (source.left.resultdef.typ <> arraydef) or
  1152. is_packed_array(source.left.resultdef) then
  1153. CGMessagePos2(source.left.fileinfo,type_e_got_expected_unpacked_array,'1',source.left.resultdef.GetTypeName)
  1154. else
  1155. unpackedarraydef := tarraydef(source.left.resultdef);
  1156. { target must be a packed array }
  1157. if not is_packed_array(target.left.resultdef) then
  1158. CGMessagePos2(target.left.fileinfo,type_e_got_expected_packed_array,'3',target.left.resultdef.GetTypeName)
  1159. else
  1160. packedarraydef := tarraydef(target.left.resultdef);
  1161. end;
  1162. if assigned(unpackedarraydef) then
  1163. begin
  1164. { index must be compatible with the unpacked array's indextype }
  1165. inserttypeconv(index.left,unpackedarraydef.rangedef);
  1166. { range check at compile time if possible }
  1167. if assigned(packedarraydef) and
  1168. (index.left.nodetype = ordconstn) and
  1169. not is_special_array(unpackedarraydef) then
  1170. begin
  1171. testrange(unpackedarraydef,tordconstnode(index.left).value,false);
  1172. tempindex := tordconstnode(index.left).value + packedarraydef.highrange-packedarraydef.lowrange;
  1173. testrange(unpackedarraydef,tempindex,false);
  1174. end;
  1175. end;
  1176. { source array is read and must be valid }
  1177. set_varstate(source.left,vs_read,[vsf_must_be_valid]);
  1178. { target array is written }
  1179. valid_for_assignment(target.left,true);
  1180. set_varstate(target.left,vs_written,[]);
  1181. { index in the unpacked array is read and must be valid }
  1182. set_varstate(index.left,vs_read,[vsf_must_be_valid]);
  1183. end;
  1184. var
  1185. vl,vl2 : TConstExprInt;
  1186. vr : bestreal;
  1187. hightree,
  1188. hp : tnode;
  1189. checkrange : boolean;
  1190. label
  1191. myexit;
  1192. begin
  1193. result:=nil;
  1194. { if we handle writeln; left contains no valid address }
  1195. if assigned(left) then
  1196. begin
  1197. if left.nodetype=callparan then
  1198. tcallparanode(left).get_paratype
  1199. else
  1200. typecheckpass(left);
  1201. end;
  1202. inc(parsing_para_level);
  1203. { handle intern constant functions in separate case }
  1204. if nf_inlineconst in flags then
  1205. begin
  1206. { no parameters? }
  1207. if not assigned(left) then
  1208. internalerror(200501231)
  1209. else
  1210. begin
  1211. vl:=0;
  1212. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1213. case left.nodetype of
  1214. realconstn :
  1215. begin
  1216. { Real functions are all handled with internproc below }
  1217. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename)
  1218. end;
  1219. ordconstn :
  1220. vl:=tordconstnode(left).value;
  1221. callparan :
  1222. begin
  1223. { both exists, else it was not generated }
  1224. vl:=tordconstnode(tcallparanode(left).left).value;
  1225. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1226. end;
  1227. else
  1228. CGMessage(parser_e_illegal_expression);
  1229. end;
  1230. case inlinenumber of
  1231. in_const_abs :
  1232. hp:=genintconstnode(abs(vl));
  1233. in_const_sqr :
  1234. hp:=genintconstnode(sqr(vl));
  1235. in_const_odd :
  1236. hp:=cordconstnode.create(byte(odd(vl)),booltype,true);
  1237. in_const_swap_word :
  1238. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resultdef,true);
  1239. in_const_swap_long :
  1240. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resultdef,true);
  1241. in_const_swap_qword :
  1242. hp:=cordconstnode.create((vl and $ffff) shl 32+(vl shr 32),left.resultdef,true);
  1243. in_const_ptr :
  1244. hp:=cpointerconstnode.create((vl2 shl 4)+vl,voidfarpointertype);
  1245. else
  1246. internalerror(88);
  1247. end;
  1248. end;
  1249. if hp=nil then
  1250. hp:=cerrornode.create;
  1251. result:=hp;
  1252. goto myexit;
  1253. end
  1254. else
  1255. begin
  1256. case inlinenumber of
  1257. in_lo_long,
  1258. in_hi_long,
  1259. in_lo_qword,
  1260. in_hi_qword,
  1261. in_lo_word,
  1262. in_hi_word :
  1263. begin
  1264. { give warning for incompatibility with tp and delphi }
  1265. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  1266. ((m_tp7 in current_settings.modeswitches) or
  1267. (m_delphi in current_settings.modeswitches)) then
  1268. CGMessage(type_w_maybe_wrong_hi_lo);
  1269. { constant folding }
  1270. if left.nodetype=ordconstn then
  1271. begin
  1272. case inlinenumber of
  1273. in_lo_word :
  1274. hp:=cordconstnode.create(tordconstnode(left).value and $ff,left.resultdef,true);
  1275. in_hi_word :
  1276. hp:=cordconstnode.create(tordconstnode(left).value shr 8,left.resultdef,true);
  1277. in_lo_long :
  1278. hp:=cordconstnode.create(tordconstnode(left).value and $ffff,left.resultdef,true);
  1279. in_hi_long :
  1280. hp:=cordconstnode.create(tordconstnode(left).value shr 16,left.resultdef,true);
  1281. in_lo_qword :
  1282. hp:=cordconstnode.create(tordconstnode(left).value and $ffffffff,left.resultdef,true);
  1283. in_hi_qword :
  1284. hp:=cordconstnode.create(tordconstnode(left).value shr 32,left.resultdef,true);
  1285. end;
  1286. result:=hp;
  1287. goto myexit;
  1288. end;
  1289. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1290. if not is_integer(left.resultdef) then
  1291. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  1292. case inlinenumber of
  1293. in_lo_word,
  1294. in_hi_word :
  1295. resultdef:=u8inttype;
  1296. in_lo_long,
  1297. in_hi_long :
  1298. resultdef:=u16inttype;
  1299. in_lo_qword,
  1300. in_hi_qword :
  1301. resultdef:=u32inttype;
  1302. end;
  1303. end;
  1304. in_sizeof_x:
  1305. begin
  1306. set_varstate(left,vs_read,[]);
  1307. if paramanager.push_high_param(vs_value,left.resultdef,current_procinfo.procdef.proccalloption) then
  1308. begin
  1309. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1310. if assigned(hightree) then
  1311. begin
  1312. hp:=caddnode.create(addn,hightree,
  1313. cordconstnode.create(1,sinttype,false));
  1314. if (left.resultdef.typ=arraydef) then
  1315. if not is_packed_array(tarraydef(left.resultdef)) then
  1316. begin
  1317. if (tarraydef(left.resultdef).elesize<>1) then
  1318. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1319. left.resultdef).elesize,sinttype,true));
  1320. end
  1321. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  1322. begin
  1323. { no packed open array support yet }
  1324. if (hp.nodetype <> ordconstn) then
  1325. internalerror(2006081511);
  1326. hp.free;
  1327. hp := cordconstnode.create(left.resultdef.size,sinttype,true);
  1328. {
  1329. hp:=
  1330. ctypeconvnode.create_explicit(sinttype,
  1331. cmoddivnode.create(divn,
  1332. caddnode.create(addn,
  1333. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1334. left.resultdef).elepackedbitsize,s64inttype,true)),
  1335. cordconstnode.create(a,s64inttype,true)),
  1336. cordconstnode.create(8,s64inttype,true)),
  1337. sinttype);
  1338. }
  1339. end;
  1340. result:=hp;
  1341. end;
  1342. end
  1343. else
  1344. resultdef:=sinttype;
  1345. end;
  1346. in_typeof_x:
  1347. begin
  1348. set_varstate(left,vs_read,[]);
  1349. resultdef:=voidpointertype;
  1350. end;
  1351. in_ord_x:
  1352. begin
  1353. if (left.nodetype=ordconstn) then
  1354. begin
  1355. hp:=cordconstnode.create(
  1356. tordconstnode(left).value,sinttype,true);
  1357. result:=hp;
  1358. goto myexit;
  1359. end;
  1360. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1361. case left.resultdef.typ of
  1362. orddef :
  1363. begin
  1364. case torddef(left.resultdef).ordtype of
  1365. bool8bit,
  1366. uchar:
  1367. begin
  1368. { change to byte() }
  1369. hp:=ctypeconvnode.create_internal(left,u8inttype);
  1370. left:=nil;
  1371. result:=hp;
  1372. end;
  1373. bool16bit,
  1374. uwidechar :
  1375. begin
  1376. { change to word() }
  1377. hp:=ctypeconvnode.create_internal(left,u16inttype);
  1378. left:=nil;
  1379. result:=hp;
  1380. end;
  1381. bool32bit :
  1382. begin
  1383. { change to dword() }
  1384. hp:=ctypeconvnode.create_internal(left,u32inttype);
  1385. left:=nil;
  1386. result:=hp;
  1387. end;
  1388. bool64bit :
  1389. begin
  1390. { change to qword() }
  1391. hp:=ctypeconvnode.create_internal(left,u64inttype);
  1392. left:=nil;
  1393. result:=hp;
  1394. end;
  1395. uvoid :
  1396. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1397. else
  1398. begin
  1399. { all other orddef need no transformation }
  1400. hp:=left;
  1401. left:=nil;
  1402. result:=hp;
  1403. end;
  1404. end;
  1405. end;
  1406. enumdef :
  1407. begin
  1408. hp:=ctypeconvnode.create_internal(left,s32inttype);
  1409. left:=nil;
  1410. result:=hp;
  1411. end;
  1412. pointerdef :
  1413. begin
  1414. if m_mac in current_settings.modeswitches then
  1415. begin
  1416. hp:=ctypeconvnode.create_internal(left,ptruinttype);
  1417. left:=nil;
  1418. result:=hp;
  1419. end
  1420. else
  1421. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1422. end
  1423. else
  1424. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1425. end;
  1426. end;
  1427. in_chr_byte:
  1428. begin
  1429. { convert to explicit char() }
  1430. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1431. hp:=ctypeconvnode.create_internal(left,cchartype);
  1432. left:=nil;
  1433. result:=hp;
  1434. end;
  1435. in_length_x:
  1436. begin
  1437. if not(is_special_array(left.resultdef)) or
  1438. (left.resultdef.typ=orddef) then
  1439. set_varstate(left,vs_read,[])
  1440. else
  1441. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1442. case left.resultdef.typ of
  1443. variantdef:
  1444. begin
  1445. inserttypeconv(left,cansistringtype);
  1446. end;
  1447. stringdef :
  1448. begin
  1449. { we don't need string convertions here }
  1450. if (left.nodetype=typeconvn) and
  1451. (ttypeconvnode(left).left.resultdef.typ=stringdef) then
  1452. begin
  1453. hp:=ttypeconvnode(left).left;
  1454. ttypeconvnode(left).left:=nil;
  1455. left.free;
  1456. left:=hp;
  1457. end;
  1458. { evaluates length of constant strings direct }
  1459. if (left.nodetype=stringconstn) then
  1460. begin
  1461. hp:=cordconstnode.create(
  1462. tstringconstnode(left).len,s32inttype,true);
  1463. result:=hp;
  1464. goto myexit;
  1465. end;
  1466. end;
  1467. orddef :
  1468. begin
  1469. { length of char is one allways }
  1470. if is_char(left.resultdef) or
  1471. is_widechar(left.resultdef) then
  1472. begin
  1473. hp:=cordconstnode.create(1,s32inttype,false);
  1474. result:=hp;
  1475. goto myexit;
  1476. end
  1477. else
  1478. CGMessage(type_e_mismatch);
  1479. end;
  1480. pointerdef :
  1481. begin
  1482. if is_pchar(left.resultdef) then
  1483. begin
  1484. hp := ccallparanode.create(left,nil);
  1485. result := ccallnode.createintern('fpc_pchar_length',hp);
  1486. { make sure the left node doesn't get disposed, since it's }
  1487. { reused in the new node (JM) }
  1488. left:=nil;
  1489. goto myexit;
  1490. end
  1491. else if is_pwidechar(left.resultdef) then
  1492. begin
  1493. hp := ccallparanode.create(left,nil);
  1494. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  1495. { make sure the left node doesn't get disposed, since it's }
  1496. { reused in the new node (JM) }
  1497. left:=nil;
  1498. goto myexit;
  1499. end
  1500. else
  1501. CGMessage(type_e_mismatch);
  1502. end;
  1503. arraydef :
  1504. begin
  1505. if is_open_array(left.resultdef) or
  1506. is_array_of_const(left.resultdef) then
  1507. begin
  1508. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1509. if assigned(hightree) then
  1510. begin
  1511. hp:=caddnode.create(addn,hightree,
  1512. cordconstnode.create(1,s32inttype,false));
  1513. result:=hp;
  1514. end;
  1515. goto myexit;
  1516. end
  1517. else
  1518. if not is_dynamic_array(left.resultdef) then
  1519. begin
  1520. hp:=cordconstnode.create(tarraydef(left.resultdef).highrange-
  1521. tarraydef(left.resultdef).lowrange+1,
  1522. s32inttype,true);
  1523. result:=hp;
  1524. goto myexit;
  1525. end
  1526. else
  1527. begin
  1528. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1529. result := ccallnode.createintern('fpc_dynarray_length',hp);
  1530. { make sure the left node doesn't get disposed, since it's }
  1531. { reused in the new node (JM) }
  1532. left:=nil;
  1533. goto myexit;
  1534. end;
  1535. end;
  1536. else
  1537. CGMessage(type_e_mismatch);
  1538. end;
  1539. { shortstring return an 8 bit value as the length
  1540. is the first byte of the string }
  1541. if is_shortstring(left.resultdef) then
  1542. resultdef:=u8inttype
  1543. else
  1544. resultdef:=sinttype;
  1545. end;
  1546. in_typeinfo_x:
  1547. begin
  1548. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1549. resultdef:=voidpointertype;
  1550. end;
  1551. in_assigned_x:
  1552. begin
  1553. { the parser has already made sure the expression is valid }
  1554. { handle constant expressions }
  1555. if is_constnode(tcallparanode(left).left) or
  1556. (tcallparanode(left).left.nodetype = pointerconstn) then
  1557. begin
  1558. { let an add node figure it out }
  1559. result := caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  1560. tcallparanode(left).left := nil;
  1561. { free left, because otherwise some code at 'myexit' tries }
  1562. { to run get_paratype for it, which crashes since left.left }
  1563. { is now nil }
  1564. left.free;
  1565. left := nil;
  1566. goto myexit;
  1567. end;
  1568. { otherwise handle separately, because there could be a procvar, which }
  1569. { is 2*sizeof(pointer), while we must only check the first pointer }
  1570. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  1571. resultdef:=booltype;
  1572. end;
  1573. in_ofs_x :
  1574. internalerror(2000101001);
  1575. in_seg_x :
  1576. begin
  1577. set_varstate(left,vs_read,[]);
  1578. result:=cordconstnode.create(0,s32inttype,false);
  1579. goto myexit;
  1580. end;
  1581. in_pred_x,
  1582. in_succ_x:
  1583. begin
  1584. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1585. resultdef:=left.resultdef;
  1586. if not is_ordinal(resultdef) then
  1587. CGMessage(type_e_ordinal_expr_expected)
  1588. else
  1589. begin
  1590. if (resultdef.typ=enumdef) and
  1591. (tenumdef(resultdef).has_jumps) and
  1592. not(m_delphi in current_settings.modeswitches) then
  1593. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  1594. end;
  1595. { only if the result is an enum do we do range checking }
  1596. if (resultdef.typ=enumdef) then
  1597. checkrange := true
  1598. else
  1599. checkrange := false;
  1600. { do constant folding after check for jumps }
  1601. if left.nodetype=ordconstn then
  1602. begin
  1603. if inlinenumber=in_succ_x then
  1604. result:=cordconstnode.create(tordconstnode(left).value+1,left.resultdef,checkrange)
  1605. else
  1606. result:=cordconstnode.create(tordconstnode(left).value-1,left.resultdef,checkrange);
  1607. end;
  1608. end;
  1609. in_initialize_x,
  1610. in_finalize_x,
  1611. in_setlength_x:
  1612. begin
  1613. { inlined from pinline }
  1614. internalerror(200204231);
  1615. end;
  1616. in_inc_x,
  1617. in_dec_x:
  1618. begin
  1619. resultdef:=voidtype;
  1620. if assigned(left) then
  1621. begin
  1622. { first param must be var }
  1623. valid_for_var(tcallparanode(left).left,true);
  1624. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1625. if (left.resultdef.typ in [enumdef,pointerdef]) or
  1626. is_ordinal(left.resultdef) or
  1627. is_currency(left.resultdef) then
  1628. begin
  1629. { value of left gets changed -> must be unique }
  1630. set_unique(tcallparanode(left).left);
  1631. { two paras ? }
  1632. if assigned(tcallparanode(left).right) then
  1633. begin
  1634. if is_integer(tcallparanode(left).right.resultdef) then
  1635. begin
  1636. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1637. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  1638. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1639. { should be handled in the parser (JM) }
  1640. internalerror(2006020901);
  1641. end
  1642. else
  1643. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  1644. end;
  1645. end
  1646. else
  1647. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  1648. end
  1649. else
  1650. CGMessagePos(fileinfo,type_e_mismatch);
  1651. end;
  1652. in_read_x,
  1653. in_readln_x,
  1654. in_write_x,
  1655. in_writeln_x :
  1656. begin
  1657. result := handle_read_write;
  1658. end;
  1659. in_settextbuf_file_x :
  1660. begin
  1661. resultdef:=voidtype;
  1662. { now we know the type of buffer }
  1663. hp:=ccallparanode.create(cordconstnode.create(
  1664. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  1665. result:=ccallnode.createintern('SETTEXTBUF',hp);
  1666. left:=nil;
  1667. end;
  1668. { the firstpass of the arg has been done in firstcalln ? }
  1669. in_reset_typedfile,
  1670. in_rewrite_typedfile :
  1671. begin
  1672. result := handle_reset_rewrite_typed;
  1673. end;
  1674. in_str_x_string :
  1675. begin
  1676. result := handle_str;
  1677. end;
  1678. in_val_x :
  1679. begin
  1680. result := handle_val;
  1681. end;
  1682. in_include_x_y,
  1683. in_exclude_x_y:
  1684. begin
  1685. resultdef:=voidtype;
  1686. { the parser already checks whether we have two (and exactly two) }
  1687. { parameters (JM) }
  1688. { first param must be var }
  1689. valid_for_var(tcallparanode(left).left,true);
  1690. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1691. { check type }
  1692. if (left.resultdef.typ=setdef) then
  1693. begin
  1694. { insert a type conversion }
  1695. { to the type of the set elements }
  1696. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1697. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  1698. tsetdef(left.resultdef).elementdef);
  1699. end
  1700. else
  1701. CGMessage(type_e_mismatch);
  1702. end;
  1703. in_pack_x_y_z,
  1704. in_unpack_x_y_z :
  1705. begin
  1706. handle_pack_unpack;
  1707. end;
  1708. in_slice_x:
  1709. begin
  1710. result:=nil;
  1711. resultdef:=tcallparanode(left).left.resultdef;
  1712. if (resultdef.typ <> arraydef) then
  1713. CGMessagePos(left.fileinfo,type_e_mismatch)
  1714. else if is_packed_array(resultdef) then
  1715. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  1716. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  1717. CGMessagePos1(tcallparanode(left).right.fileinfo,
  1718. type_e_integer_expr_expected,
  1719. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  1720. end;
  1721. in_low_x,
  1722. in_high_x:
  1723. begin
  1724. case left.resultdef.typ of
  1725. orddef,
  1726. enumdef:
  1727. begin
  1728. result:=do_lowhigh(left.resultdef);
  1729. end;
  1730. setdef:
  1731. begin
  1732. result:=do_lowhigh(tsetdef(left.resultdef).elementdef);
  1733. end;
  1734. arraydef:
  1735. begin
  1736. if inlinenumber=in_low_x then
  1737. begin
  1738. set_varstate(left,vs_read,[]);
  1739. result:=cordconstnode.create(tarraydef(
  1740. left.resultdef).lowrange,tarraydef(left.resultdef).rangedef,true);
  1741. end
  1742. else
  1743. begin
  1744. if is_open_array(left.resultdef) or
  1745. is_array_of_const(left.resultdef) then
  1746. begin
  1747. set_varstate(left,vs_read,[]);
  1748. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1749. end
  1750. else
  1751. if is_dynamic_array(left.resultdef) then
  1752. begin
  1753. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1754. { can't use inserttypeconv because we need }
  1755. { an explicit type conversion (JM) }
  1756. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1757. result := ccallnode.createintern('fpc_dynarray_high',hp);
  1758. { make sure the left node doesn't get disposed, since it's }
  1759. { reused in the new node (JM) }
  1760. left:=nil;
  1761. end
  1762. else
  1763. begin
  1764. set_varstate(left,vs_read,[]);
  1765. result:=cordconstnode.create(tarraydef(
  1766. left.resultdef).highrange,tarraydef(left.resultdef).rangedef,true);
  1767. end;
  1768. end;
  1769. end;
  1770. stringdef:
  1771. begin
  1772. if inlinenumber=in_low_x then
  1773. begin
  1774. result:=cordconstnode.create(0,u8inttype,false);
  1775. end
  1776. else
  1777. begin
  1778. if is_open_string(left.resultdef) then
  1779. begin
  1780. set_varstate(left,vs_read,[]);
  1781. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  1782. end
  1783. else
  1784. result:=cordconstnode.create(tstringdef(left.resultdef).len,u8inttype,true);
  1785. end;
  1786. end;
  1787. else
  1788. CGMessage(type_e_mismatch);
  1789. end;
  1790. end;
  1791. in_exp_real :
  1792. begin
  1793. if left.nodetype in [ordconstn,realconstn] then
  1794. begin
  1795. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  1796. if (trealconstnode(result).value_real=MathInf.Value) and
  1797. ((cs_check_range in current_settings.localswitches) or
  1798. (cs_check_overflow in current_settings.localswitches)) then
  1799. begin
  1800. result:=crealconstnode.create(0,pbestrealtype^);
  1801. CGMessage(parser_e_range_check_error);
  1802. end;
  1803. end
  1804. else
  1805. begin
  1806. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1807. inserttypeconv(left,pbestrealtype^);
  1808. resultdef:=pbestrealtype^;
  1809. end;
  1810. end;
  1811. in_trunc_real :
  1812. begin
  1813. if left.nodetype in [ordconstn,realconstn] then
  1814. begin
  1815. vr:=getconstrealvalue;
  1816. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1817. begin
  1818. CGMessage(parser_e_range_check_error);
  1819. result:=cordconstnode.create(1,s64inttype,false)
  1820. end
  1821. else
  1822. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  1823. end
  1824. else
  1825. begin
  1826. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1827. inserttypeconv(left,pbestrealtype^);
  1828. resultdef:=s64inttype;
  1829. end;
  1830. end;
  1831. in_round_real :
  1832. begin
  1833. if left.nodetype in [ordconstn,realconstn] then
  1834. begin
  1835. vr:=getconstrealvalue;
  1836. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1837. begin
  1838. CGMessage(parser_e_range_check_error);
  1839. result:=cordconstnode.create(1,s64inttype,false)
  1840. end
  1841. else
  1842. result:=cordconstnode.create(round(vr),s64inttype,true)
  1843. end
  1844. else
  1845. begin
  1846. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1847. inserttypeconv(left,pbestrealtype^);
  1848. resultdef:=s64inttype;
  1849. end;
  1850. end;
  1851. in_frac_real :
  1852. begin
  1853. if left.nodetype in [ordconstn,realconstn] then
  1854. setconstrealvalue(frac(getconstrealvalue))
  1855. else
  1856. begin
  1857. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1858. inserttypeconv(left,pbestrealtype^);
  1859. resultdef:=pbestrealtype^;
  1860. end;
  1861. end;
  1862. in_int_real :
  1863. begin
  1864. if left.nodetype in [ordconstn,realconstn] then
  1865. setconstrealvalue(int(getconstrealvalue))
  1866. else
  1867. begin
  1868. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1869. inserttypeconv(left,pbestrealtype^);
  1870. resultdef:=pbestrealtype^;
  1871. end;
  1872. end;
  1873. in_pi_real :
  1874. begin
  1875. if block_type=bt_const then
  1876. setconstrealvalue(getpi)
  1877. else
  1878. resultdef:=pbestrealtype^;
  1879. end;
  1880. in_cos_real :
  1881. begin
  1882. if left.nodetype in [ordconstn,realconstn] then
  1883. setconstrealvalue(cos(getconstrealvalue))
  1884. else
  1885. begin
  1886. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1887. inserttypeconv(left,pbestrealtype^);
  1888. resultdef:=pbestrealtype^;
  1889. end;
  1890. end;
  1891. in_sin_real :
  1892. begin
  1893. if left.nodetype in [ordconstn,realconstn] then
  1894. setconstrealvalue(sin(getconstrealvalue))
  1895. else
  1896. begin
  1897. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1898. inserttypeconv(left,pbestrealtype^);
  1899. resultdef:=pbestrealtype^;
  1900. end;
  1901. end;
  1902. in_arctan_real :
  1903. begin
  1904. if left.nodetype in [ordconstn,realconstn] then
  1905. setconstrealvalue(arctan(getconstrealvalue))
  1906. else
  1907. begin
  1908. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1909. inserttypeconv(left,pbestrealtype^);
  1910. resultdef:=pbestrealtype^;
  1911. end;
  1912. end;
  1913. in_abs_real :
  1914. begin
  1915. if left.nodetype in [ordconstn,realconstn] then
  1916. setconstrealvalue(abs(getconstrealvalue))
  1917. else
  1918. begin
  1919. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1920. inserttypeconv(left,pbestrealtype^);
  1921. resultdef:=pbestrealtype^;
  1922. end;
  1923. end;
  1924. in_sqr_real :
  1925. begin
  1926. if left.nodetype in [ordconstn,realconstn] then
  1927. setconstrealvalue(sqr(getconstrealvalue))
  1928. else
  1929. begin
  1930. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1931. setfloatresultdef;
  1932. end;
  1933. end;
  1934. in_sqrt_real :
  1935. begin
  1936. if left.nodetype in [ordconstn,realconstn] then
  1937. begin
  1938. vr:=getconstrealvalue;
  1939. if vr<0.0 then
  1940. result:=handle_sqrt_const(vr)
  1941. else
  1942. setconstrealvalue(sqrt(vr));
  1943. end
  1944. else
  1945. begin
  1946. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1947. setfloatresultdef;
  1948. end;
  1949. end;
  1950. in_ln_real :
  1951. begin
  1952. if left.nodetype in [ordconstn,realconstn] then
  1953. begin
  1954. vr:=getconstrealvalue;
  1955. if vr<=0.0 then
  1956. result:=handle_ln_const(vr)
  1957. else
  1958. setconstrealvalue(ln(vr));
  1959. end
  1960. else
  1961. begin
  1962. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1963. inserttypeconv(left,pbestrealtype^);
  1964. resultdef:=pbestrealtype^;
  1965. end;
  1966. end;
  1967. {$ifdef SUPPORT_MMX}
  1968. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  1969. begin
  1970. end;
  1971. {$endif SUPPORT_MMX}
  1972. in_prefetch_var:
  1973. begin
  1974. resultdef:=voidtype;
  1975. end;
  1976. {$ifdef SUPPORT_UNALIGNED}
  1977. in_unaligned_x:
  1978. begin
  1979. resultdef:=left.resultdef;
  1980. end;
  1981. {$endif SUPPORT_UNALIGNED}
  1982. in_assert_x_y :
  1983. begin
  1984. resultdef:=voidtype;
  1985. if assigned(left) then
  1986. begin
  1987. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  1988. { check type }
  1989. if is_boolean(left.resultdef) then
  1990. begin
  1991. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1992. { must always be a string }
  1993. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  1994. end
  1995. else
  1996. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  1997. end
  1998. else
  1999. CGMessage(type_e_mismatch);
  2000. { We've checked the whole statement for correctness, now we
  2001. can remove it if assertions are off }
  2002. if not(cs_do_assertion in current_settings.localswitches) then
  2003. begin
  2004. { we need a valid node, so insert a nothingn }
  2005. result:=cnothingnode.create;
  2006. end
  2007. else
  2008. include(current_procinfo.flags,pi_do_call);
  2009. end;
  2010. in_get_frame,
  2011. in_get_caller_frame,
  2012. in_get_caller_addr:
  2013. begin
  2014. resultdef:=voidpointertype;
  2015. end;
  2016. else
  2017. internalerror(8);
  2018. end;
  2019. end;
  2020. myexit:
  2021. { Run get_paratype again to update maybe inserted typeconvs }
  2022. if not codegenerror then
  2023. begin
  2024. if assigned(left) and
  2025. (left.nodetype=callparan) then
  2026. tcallparanode(left).get_paratype;
  2027. end;
  2028. dec(parsing_para_level);
  2029. end;
  2030. function tinlinenode.pass_1 : tnode;
  2031. var
  2032. hp,hpp,resultnode : tnode;
  2033. shiftconst: longint;
  2034. tempnode: ttempcreatenode;
  2035. newstatement: tstatementnode;
  2036. newblock: tblocknode;
  2037. begin
  2038. result:=nil;
  2039. { if we handle writeln; left contains no valid address }
  2040. if assigned(left) then
  2041. begin
  2042. if left.nodetype=callparan then
  2043. tcallparanode(left).firstcallparan
  2044. else
  2045. firstpass(left);
  2046. left_max;
  2047. end;
  2048. inc(parsing_para_level);
  2049. { intern const should already be handled }
  2050. if nf_inlineconst in flags then
  2051. internalerror(200104044);
  2052. case inlinenumber of
  2053. in_lo_qword,
  2054. in_hi_qword,
  2055. in_lo_long,
  2056. in_hi_long,
  2057. in_lo_word,
  2058. in_hi_word:
  2059. begin
  2060. shiftconst := 0;
  2061. case inlinenumber of
  2062. in_hi_qword:
  2063. shiftconst := 32;
  2064. in_hi_long:
  2065. shiftconst := 16;
  2066. in_hi_word:
  2067. shiftconst := 8;
  2068. end;
  2069. if shiftconst <> 0 then
  2070. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  2071. cordconstnode.create(shiftconst,u32inttype,false)),resultdef)
  2072. else
  2073. result := ctypeconvnode.create_internal(left,resultdef);
  2074. left := nil;
  2075. firstpass(result);
  2076. end;
  2077. in_sizeof_x:
  2078. begin
  2079. if registersint<1 then
  2080. registersint:=1;
  2081. expectloc:=LOC_REGISTER;
  2082. end;
  2083. in_typeof_x:
  2084. begin
  2085. if registersint<1 then
  2086. registersint:=1;
  2087. expectloc:=LOC_REGISTER;
  2088. end;
  2089. in_length_x:
  2090. begin
  2091. if is_shortstring(left.resultdef) then
  2092. expectloc:=left.expectloc
  2093. else
  2094. begin
  2095. { ansi/wide string }
  2096. if registersint<1 then
  2097. registersint:=1;
  2098. expectloc:=LOC_REGISTER;
  2099. end;
  2100. end;
  2101. in_typeinfo_x:
  2102. begin
  2103. expectloc:=LOC_REGISTER;
  2104. registersint:=1;
  2105. end;
  2106. in_assigned_x:
  2107. begin
  2108. expectloc := LOC_JUMP;
  2109. registersint:=1;
  2110. end;
  2111. in_pred_x,
  2112. in_succ_x:
  2113. begin
  2114. if is_64bit(resultdef) then
  2115. begin
  2116. if (registersint<2) then
  2117. registersint:=2
  2118. end
  2119. else
  2120. begin
  2121. if (registersint<1) then
  2122. registersint:=1;
  2123. end;
  2124. expectloc:=LOC_REGISTER;
  2125. end;
  2126. in_setlength_x,
  2127. in_initialize_x,
  2128. in_finalize_x:
  2129. begin
  2130. expectloc:=LOC_VOID;
  2131. end;
  2132. in_inc_x,
  2133. in_dec_x:
  2134. begin
  2135. expectloc:=LOC_VOID;
  2136. { range/overflow checking doesn't work properly }
  2137. { with the inc/dec code that's generated (JM) }
  2138. if (current_settings.localswitches * [cs_check_overflow,cs_check_range] <> []) and
  2139. { No overflow check for pointer operations, because inc(pointer,-1) will always
  2140. trigger an overflow. For uint32 it works because then the operation is done
  2141. in 64bit. Range checking is not applicable to pointers either }
  2142. (tcallparanode(left).left.resultdef.typ<>pointerdef) then
  2143. { convert to simple add (JM) }
  2144. begin
  2145. newblock := internalstatements(newstatement);
  2146. { extra parameter? }
  2147. if assigned(tcallparanode(left).right) then
  2148. begin
  2149. { Yes, use for add node }
  2150. hpp := tcallparanode(tcallparanode(left).right).left;
  2151. tcallparanode(tcallparanode(left).right).left := nil;
  2152. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2153. CGMessage(parser_e_illegal_expression);
  2154. end
  2155. else
  2156. begin
  2157. { no, create constant 1 }
  2158. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  2159. end;
  2160. typecheckpass(hpp);
  2161. if not((hpp.resultdef.typ=orddef) and
  2162. {$ifndef cpu64bit}
  2163. (torddef(hpp.resultdef).ordtype<>u32bit)) then
  2164. {$else not cpu64bit}
  2165. (torddef(hpp.resultdef).ordtype<>u64bit)) then
  2166. {$endif not cpu64bit}
  2167. inserttypeconv_internal(hpp,sinttype);
  2168. { make sure we don't call functions part of the left node twice (and generally }
  2169. { optimize the code generation) }
  2170. if node_complexity(tcallparanode(left).left) > 1 then
  2171. begin
  2172. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2173. addstatement(newstatement,tempnode);
  2174. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2175. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  2176. hp := cderefnode.create(ctemprefnode.create(tempnode));
  2177. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  2178. end
  2179. else
  2180. begin
  2181. hp := tcallparanode(left).left.getcopy;
  2182. tempnode := nil;
  2183. end;
  2184. resultnode := hp.getcopy;
  2185. { avoid type errors from the addn/subn }
  2186. if not is_integer(resultnode.resultdef) then
  2187. begin
  2188. inserttypeconv_internal(hp,sinttype);
  2189. inserttypeconv_internal(hpp,sinttype);
  2190. end;
  2191. { addition/substraction depending on inc/dec }
  2192. if inlinenumber = in_inc_x then
  2193. hpp := caddnode.create(addn,hp,hpp)
  2194. else
  2195. hpp := caddnode.create(subn,hp,hpp);
  2196. { assign result of addition }
  2197. if not(is_integer(resultnode.resultdef)) then
  2198. inserttypeconv(hpp,torddef.create(
  2199. {$ifdef cpu64bit}
  2200. s64bit,
  2201. {$else cpu64bit}
  2202. s32bit,
  2203. {$endif cpu64bit}
  2204. get_min_value(resultnode.resultdef),
  2205. get_max_value(resultnode.resultdef)))
  2206. else
  2207. inserttypeconv(hpp,resultnode.resultdef);
  2208. { avoid any possible warnings }
  2209. inserttypeconv_internal(hpp,resultnode.resultdef);
  2210. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  2211. { deallocate the temp }
  2212. if assigned(tempnode) then
  2213. addstatement(newstatement,ctempdeletenode.create(tempnode));
  2214. { firstpass it }
  2215. firstpass(newblock);
  2216. { return new node }
  2217. result := newblock;
  2218. end
  2219. else if (left.resultdef.typ in [enumdef,pointerdef]) or
  2220. is_ordinal(left.resultdef) then
  2221. begin
  2222. { two paras ? }
  2223. if assigned(tcallparanode(left).right) then
  2224. begin
  2225. { need we an additional register ? }
  2226. if not(is_constintnode(tcallparanode(tcallparanode(left).right).left)) and
  2227. (tcallparanode(tcallparanode(left).right).left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) and
  2228. (tcallparanode(tcallparanode(left).right).left.registersint<=1) then
  2229. inc(registersint);
  2230. { do we need an additional register to restore the first parameter? }
  2231. if tcallparanode(tcallparanode(left).right).left.registersint>=registersint then
  2232. inc(registersint);
  2233. end;
  2234. end;
  2235. end;
  2236. in_include_x_y,
  2237. in_exclude_x_y:
  2238. begin
  2239. expectloc:=LOC_VOID;
  2240. registersint:=left.registersint;
  2241. registersfpu:=left.registersfpu;
  2242. {$ifdef SUPPORT_MMX}
  2243. registersmmx:=left.registersmmx;
  2244. {$endif SUPPORT_MMX}
  2245. end;
  2246. in_pack_x_y_z,
  2247. in_unpack_x_y_z:
  2248. begin
  2249. result:=first_pack_unpack;
  2250. end;
  2251. in_exp_real:
  2252. begin
  2253. result:= first_exp_real;
  2254. end;
  2255. in_round_real:
  2256. begin
  2257. result:= first_round_real;
  2258. end;
  2259. in_trunc_real:
  2260. begin
  2261. result:= first_trunc_real;
  2262. end;
  2263. in_int_real:
  2264. begin
  2265. result:= first_int_real;
  2266. end;
  2267. in_frac_real:
  2268. begin
  2269. result:= first_frac_real;
  2270. end;
  2271. in_cos_real:
  2272. begin
  2273. result:= first_cos_real;
  2274. end;
  2275. in_sin_real:
  2276. begin
  2277. result := first_sin_real;
  2278. end;
  2279. in_arctan_real:
  2280. begin
  2281. result := first_arctan_real;
  2282. end;
  2283. in_pi_real :
  2284. begin
  2285. result := first_pi;
  2286. end;
  2287. in_abs_real:
  2288. begin
  2289. result := first_abs_real;
  2290. end;
  2291. in_sqr_real:
  2292. begin
  2293. result := first_sqr_real;
  2294. end;
  2295. in_sqrt_real:
  2296. begin
  2297. result := first_sqrt_real;
  2298. end;
  2299. in_ln_real:
  2300. begin
  2301. result := first_ln_real;
  2302. end;
  2303. {$ifdef SUPPORT_MMX}
  2304. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2305. begin
  2306. end;
  2307. {$endif SUPPORT_MMX}
  2308. in_assert_x_y :
  2309. begin
  2310. expectloc:=LOC_VOID;
  2311. registersint:=left.registersint;
  2312. registersfpu:=left.registersfpu;
  2313. {$ifdef SUPPORT_MMX}
  2314. registersmmx:=left.registersmmx;
  2315. {$endif SUPPORT_MMX}
  2316. end;
  2317. in_low_x,
  2318. in_high_x:
  2319. internalerror(200104047);
  2320. in_slice_x:
  2321. internalerror(2005101501);
  2322. in_ord_x,
  2323. in_chr_byte:
  2324. begin
  2325. { should not happend as it's converted to typeconv }
  2326. internalerror(200104045);
  2327. end;
  2328. in_ofs_x :
  2329. internalerror(2000101001);
  2330. in_seg_x :
  2331. internalerror(200104046);
  2332. in_settextbuf_file_x,
  2333. in_reset_typedfile,
  2334. in_rewrite_typedfile,
  2335. in_str_x_string,
  2336. in_val_x,
  2337. in_read_x,
  2338. in_readln_x,
  2339. in_write_x,
  2340. in_writeln_x :
  2341. begin
  2342. { should be handled by pass_typecheck }
  2343. internalerror(200108234);
  2344. end;
  2345. in_get_frame:
  2346. begin
  2347. include(current_procinfo.flags,pi_needs_stackframe);
  2348. expectloc:=LOC_CREGISTER;
  2349. end;
  2350. in_get_caller_frame:
  2351. begin
  2352. expectloc:=LOC_REGISTER;
  2353. registersint:=1;
  2354. end;
  2355. in_get_caller_addr:
  2356. begin
  2357. expectloc:=LOC_REGISTER;
  2358. registersint:=1;
  2359. end;
  2360. in_prefetch_var:
  2361. begin
  2362. expectloc:=LOC_VOID;
  2363. end;
  2364. {$ifdef SUPPORT_UNALIGNED}
  2365. in_unaligned_x:
  2366. begin
  2367. expectloc:=tcallparanode(left).left.expectloc;
  2368. end;
  2369. {$endif SUPPORT_UNALIGNED}
  2370. else
  2371. internalerror(89);
  2372. end;
  2373. dec(parsing_para_level);
  2374. end;
  2375. {$maxfpuregisters default}
  2376. function tinlinenode.docompare(p: tnode): boolean;
  2377. begin
  2378. docompare :=
  2379. inherited docompare(p) and
  2380. (inlinenumber = tinlinenode(p).inlinenumber);
  2381. end;
  2382. function tinlinenode.first_pi : tnode;
  2383. begin
  2384. result:=crealconstnode.create(getpi,pbestrealtype^);
  2385. end;
  2386. function tinlinenode.first_arctan_real : tnode;
  2387. begin
  2388. { create the call to the helper }
  2389. { on entry left node contains the parameter }
  2390. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  2391. ccallparanode.create(left,nil));
  2392. left := nil;
  2393. end;
  2394. function tinlinenode.first_abs_real : tnode;
  2395. begin
  2396. { create the call to the helper }
  2397. { on entry left node contains the parameter }
  2398. first_abs_real := ccallnode.createintern('fpc_abs_real',
  2399. ccallparanode.create(left,nil));
  2400. left := nil;
  2401. end;
  2402. function tinlinenode.first_sqr_real : tnode;
  2403. begin
  2404. { create the call to the helper }
  2405. { on entry left node contains the parameter }
  2406. first_sqr_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqr_real',
  2407. ccallparanode.create(left,nil)),resultdef);
  2408. left := nil;
  2409. end;
  2410. function tinlinenode.first_sqrt_real : tnode;
  2411. begin
  2412. { create the call to the helper }
  2413. { on entry left node contains the parameter }
  2414. first_sqrt_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqrt_real',
  2415. ccallparanode.create(left,nil)),resultdef);
  2416. left := nil;
  2417. end;
  2418. function tinlinenode.first_ln_real : tnode;
  2419. begin
  2420. { create the call to the helper }
  2421. { on entry left node contains the parameter }
  2422. first_ln_real := ccallnode.createintern('fpc_ln_real',
  2423. ccallparanode.create(left,nil));
  2424. left := nil;
  2425. end;
  2426. function tinlinenode.first_cos_real : tnode;
  2427. begin
  2428. { create the call to the helper }
  2429. { on entry left node contains the parameter }
  2430. first_cos_real := ccallnode.createintern('fpc_cos_real',
  2431. ccallparanode.create(left,nil));
  2432. left := nil;
  2433. end;
  2434. function tinlinenode.first_sin_real : tnode;
  2435. begin
  2436. { create the call to the helper }
  2437. { on entry left node contains the parameter }
  2438. first_sin_real := ccallnode.createintern('fpc_sin_real',
  2439. ccallparanode.create(left,nil));
  2440. left := nil;
  2441. end;
  2442. function tinlinenode.first_exp_real : tnode;
  2443. begin
  2444. { create the call to the helper }
  2445. { on entry left node contains the parameter }
  2446. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  2447. left := nil;
  2448. end;
  2449. function tinlinenode.first_int_real : tnode;
  2450. begin
  2451. { create the call to the helper }
  2452. { on entry left node contains the parameter }
  2453. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  2454. left := nil;
  2455. end;
  2456. function tinlinenode.first_frac_real : tnode;
  2457. begin
  2458. { create the call to the helper }
  2459. { on entry left node contains the parameter }
  2460. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  2461. left := nil;
  2462. end;
  2463. function tinlinenode.first_round_real : tnode;
  2464. begin
  2465. { create the call to the helper }
  2466. { on entry left node contains the parameter }
  2467. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  2468. left := nil;
  2469. end;
  2470. function tinlinenode.first_trunc_real : tnode;
  2471. begin
  2472. { create the call to the helper }
  2473. { on entry left node contains the parameter }
  2474. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  2475. left := nil;
  2476. end;
  2477. function tinlinenode.first_pack_unpack: tnode;
  2478. var
  2479. loopstatement : tstatementnode;
  2480. loop : tblocknode;
  2481. loopvar : ttempcreatenode;
  2482. tempnode,
  2483. source,
  2484. target,
  2485. index,
  2486. unpackednode,
  2487. packednode,
  2488. sourcevecindex,
  2489. targetvecindex,
  2490. loopbody : tnode;
  2491. temprangedef : tdef;
  2492. ulorange,
  2493. uhirange,
  2494. plorange,
  2495. phirange : TConstExprInt;
  2496. begin
  2497. { transform into a for loop which assigns the data of the (un)packed }
  2498. { array to the other one }
  2499. source := left;
  2500. if (inlinenumber = in_unpack_x_y_z) then
  2501. begin
  2502. target := tcallparanode(source).right;
  2503. index := tcallparanode(target).right;
  2504. packednode := tcallparanode(source).left;
  2505. unpackednode := tcallparanode(target).left;
  2506. end
  2507. else
  2508. begin
  2509. index := tcallparanode(source).right;
  2510. target := tcallparanode(index).right;
  2511. packednode := tcallparanode(target).left;
  2512. unpackednode := tcallparanode(source).left;
  2513. end;
  2514. source := tcallparanode(source).left;
  2515. target := tcallparanode(target).left;
  2516. index := tcallparanode(index).left;
  2517. loop := internalstatements(loopstatement);
  2518. loopvar := ctempcreatenode.create(
  2519. tarraydef(packednode.resultdef).rangedef,
  2520. tarraydef(packednode.resultdef).rangedef.size,
  2521. tt_persistent,true);
  2522. addstatement(loopstatement,loopvar);
  2523. { For range checking: we have to convert to an integer type (in case the index type }
  2524. { is an enum), add the index and loop variable together, convert the result }
  2525. { implicitly to an orddef with range equal to the rangedef to get range checking }
  2526. { and finally convert it explicitly back to the actual rangedef to avoid type }
  2527. { errors }
  2528. temprangedef:=nil;
  2529. getrange(unpackednode.resultdef,ulorange,uhirange);
  2530. getrange(packednode.resultdef,plorange,phirange);
  2531. temprangedef:=torddef.create(torddef(sinttype).ordtype,ulorange,uhirange);
  2532. sourcevecindex := ctemprefnode.create(loopvar);
  2533. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  2534. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  2535. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  2536. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  2537. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  2538. if (inlinenumber = in_pack_x_y_z) then
  2539. begin
  2540. { swap source and target vec indices }
  2541. tempnode := sourcevecindex;
  2542. sourcevecindex := targetvecindex;
  2543. targetvecindex := tempnode;
  2544. end;
  2545. { create the assignment in the loop body }
  2546. loopbody :=
  2547. cassignmentnode.create(
  2548. cvecnode.create(target.getcopy,targetvecindex),
  2549. cvecnode.create(source.getcopy,sourcevecindex)
  2550. );
  2551. { create the actual for loop }
  2552. tempnode := cfornode.create(
  2553. ctemprefnode.create(loopvar),
  2554. cinlinenode.create(in_low_x,false,packednode.getcopy),
  2555. cinlinenode.create(in_high_x,false,packednode.getcopy),
  2556. loopbody,
  2557. false);
  2558. addstatement(loopstatement,tempnode);
  2559. { free the loop counter }
  2560. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  2561. result := loop;
  2562. end;
  2563. begin
  2564. cinlinenode:=tinlinenode;
  2565. end.