ninl.pas 110 KB

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