ninl.pas 98 KB

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