2
0

ninl.pas 108 KB

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