ninl.pas 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533
  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. if not searchsystype('TEXT',textsym) then
  347. internalerror(200108313);
  348. filepara := ccallparanode.create(ctypeconvnode.create_internal(
  349. cderefnode.create(ctemprefnode.create(filetemp)),textsym.restype),nil);
  350. end
  351. else
  352. { remove filepara from the parameter chain }
  353. begin
  354. left := filepara.right;
  355. filepara.right := nil;
  356. { the file para is a var parameter, but it must be valid already }
  357. set_varstate(filepara.left,vs_readwritten,[vsf_must_be_valid]);
  358. { check if we should make a temp to store the result of a complex }
  359. { expression (better heuristics, anyone?) (JM) }
  360. if (filepara.left.nodetype <> loadn) then
  361. begin
  362. { create a temp which will hold a pointer to the file }
  363. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.def.size,tt_persistent,true);
  364. { add it to the statements }
  365. addstatement(newstatement,filetemp);
  366. { make sure the resulttype of the temp (and as such of the }
  367. { temprefs coming after it) is set (necessary because the }
  368. { temprefs will be part of the filepara, of which we need }
  369. { the resulttype later on and temprefs can only be }
  370. { resulttypepassed if the resulttype of the temp is known) }
  371. resulttypepass(tnode(filetemp));
  372. { assign the address of the file to the temp }
  373. addstatement(newstatement,
  374. cassignmentnode.create(ctemprefnode.create(filetemp),
  375. caddrnode.create_internal(filepara.left)));
  376. resulttypepass(newstatement.left);
  377. { create a new fileparameter as follows: file_type(temp^) }
  378. { (so that we pass the value and not the address of the temp }
  379. { to the read/write routine) }
  380. nextpara := ccallparanode.create(ctypeconvnode.create_internal(
  381. cderefnode.create(ctemprefnode.create(filetemp)),filepara.left.resulttype),nil);
  382. { replace the old file para with the new one }
  383. filepara.left := nil;
  384. filepara.free;
  385. filepara := nextpara;
  386. end;
  387. end;
  388. { the resulttype of the filepara must be set since it's }
  389. { used below }
  390. filepara.get_paratype;
  391. { now, filepara is nowhere referenced anymore, so we can safely dispose it }
  392. { if something goes wrong or at the end of the procedure }
  393. { choose the correct procedure prefix }
  394. procprefix := 'fpc_'+procnames[is_typed,do_read];
  395. { we're going to reuse the paranodes, so make sure they don't get freed }
  396. { twice }
  397. para := tcallparanode(left);
  398. left := nil;
  399. { no errors found yet... }
  400. found_error := false;
  401. if is_typed then
  402. begin
  403. { add the typesize to the filepara }
  404. if filepara.resulttype.def.deftype=filedef then
  405. filepara.right := ccallparanode.create(cordconstnode.create(
  406. tfiledef(filepara.resulttype.def).typedfiletype.def.size,s32inttype,true),nil);
  407. { check for "no parameters" (you need at least one extra para for typed files) }
  408. if not assigned(para) then
  409. begin
  410. CGMessage(parser_e_wrong_parameter_size);
  411. found_error := true;
  412. end;
  413. { process all parameters }
  414. while assigned(para) do
  415. begin
  416. { check if valid parameter }
  417. if para.left.nodetype=typen then
  418. begin
  419. CGMessagePos(para.left.fileinfo,type_e_cant_read_write_type);
  420. found_error := true;
  421. end;
  422. { support writeln(procvar) }
  423. if (para.left.resulttype.def.deftype=procvardef) then
  424. begin
  425. p1:=ccallnode.create_procvar(nil,para.left);
  426. resulttypepass(p1);
  427. para.left:=p1;
  428. end;
  429. if filepara.resulttype.def.deftype=filedef then
  430. inserttypeconv(para.left,tfiledef(filepara.resulttype.def).typedfiletype);
  431. if assigned(para.right) and
  432. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  433. begin
  434. CGMessagePos(para.right.fileinfo,parser_e_illegal_colon_qualifier);
  435. { skip all colon para's }
  436. nextpara := tcallparanode(tcallparanode(para.right).right);
  437. while assigned(nextpara) and
  438. (cpf_is_colon_para in nextpara.callparaflags) do
  439. nextpara := tcallparanode(nextpara.right);
  440. found_error := true;
  441. end
  442. else
  443. { get next parameter }
  444. nextpara := tcallparanode(para.right);
  445. { When we have a call, we have a problem: you can't pass the }
  446. { result of a call as a formal const parameter. Solution: }
  447. { assign the result to a temp and pass this temp as parameter }
  448. { This is not very efficient, but write(typedfile,x) is }
  449. { already slow by itself anyway (no buffering) (JM) }
  450. { Actually, thge same goes for every non-simple expression }
  451. { (such as an addition, ...) -> put everything but load nodes }
  452. { into temps (JM) }
  453. { of course, this must only be allowed for writes!!! (JM) }
  454. if not(do_read) and
  455. (para.left.nodetype <> loadn) then
  456. begin
  457. { create temp for result }
  458. temp := ctempcreatenode.create(para.left.resulttype,
  459. para.left.resulttype.def.size,tt_persistent,false);
  460. addstatement(newstatement,temp);
  461. { assign result to temp }
  462. addstatement(newstatement,
  463. cassignmentnode.create(ctemprefnode.create(temp),
  464. para.left));
  465. { replace (reused) paranode with temp }
  466. para.left := ctemprefnode.create(temp);
  467. end;
  468. { add fileparameter }
  469. para.right := filepara.getcopy;
  470. { create call statment }
  471. { since the parameters are in the correct order, we have to insert }
  472. { the statements always at the end of the current block }
  473. addstatement(newstatement,ccallnode.createintern(procprefix,para));
  474. { if we used a temp, free it }
  475. if para.left.nodetype = temprefn then
  476. addstatement(newstatement,ctempdeletenode.create(temp));
  477. { process next parameter }
  478. para := nextpara;
  479. end;
  480. { free the file parameter }
  481. filepara.free;
  482. end
  483. else
  484. { text read/write }
  485. begin
  486. while assigned(para) do
  487. begin
  488. { is this parameter faulty? }
  489. error_para := false;
  490. { is this parameter a real? }
  491. is_real:=false;
  492. { type used for the read(), this is used to check
  493. whether a temp is needed for range checking }
  494. readfunctype.reset;
  495. { can't read/write types }
  496. if para.left.nodetype=typen then
  497. begin
  498. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  499. error_para := true;
  500. end;
  501. { support writeln(procvar) }
  502. if (para.left.resulttype.def.deftype=procvardef) then
  503. begin
  504. p1:=ccallnode.create_procvar(nil,para.left);
  505. resulttypepass(p1);
  506. para.left:=p1;
  507. end;
  508. { Currency will be written using the bestreal }
  509. if is_currency(para.left.resulttype.def) then
  510. inserttypeconv(para.left,pbestrealtype^);
  511. case para.left.resulttype.def.deftype of
  512. stringdef :
  513. begin
  514. name := procprefix+tstringdef(para.left.resulttype.def).stringtypname;
  515. end;
  516. pointerdef :
  517. begin
  518. if (not is_pchar(para.left.resulttype.def)) or do_read then
  519. begin
  520. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  521. error_para := true;
  522. end
  523. else
  524. name := procprefix+'pchar_as_pointer';
  525. end;
  526. floatdef :
  527. begin
  528. is_real:=true;
  529. name := procprefix+'float';
  530. readfunctype:=pbestrealtype^;
  531. end;
  532. orddef :
  533. begin
  534. case torddef(para.left.resulttype.def).typ of
  535. {$ifdef cpu64bit}
  536. s64bit,
  537. {$endif cpu64bit}
  538. s8bit,
  539. s16bit,
  540. s32bit :
  541. begin
  542. name := procprefix+'sint';
  543. readfunctype:=sinttype;
  544. end;
  545. {$ifdef cpu64bit}
  546. u64bit,
  547. {$endif cpu64bit}
  548. u8bit,
  549. u16bit,
  550. u32bit :
  551. begin
  552. name := procprefix+'uint';
  553. readfunctype:=uinttype;
  554. end;
  555. uchar :
  556. begin
  557. name := procprefix+'char';
  558. readfunctype:=cchartype;
  559. end;
  560. uwidechar :
  561. begin
  562. name := procprefix+'widechar';
  563. readfunctype:=cwidechartype;
  564. end;
  565. {$ifndef cpu64bit}
  566. s64bit :
  567. begin
  568. name := procprefix+'int64';
  569. readfunctype:=s64inttype;
  570. end;
  571. u64bit :
  572. begin
  573. name := procprefix+'qword';
  574. readfunctype:=u64inttype;
  575. end;
  576. {$endif cpu64bit}
  577. bool8bit,
  578. bool16bit,
  579. bool32bit :
  580. begin
  581. if do_read then
  582. begin
  583. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  584. error_para := true;
  585. end
  586. else
  587. begin
  588. name := procprefix+'boolean';
  589. readfunctype:=booltype;
  590. end;
  591. end
  592. else
  593. begin
  594. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  595. error_para := true;
  596. end;
  597. end;
  598. end;
  599. variantdef :
  600. name:=procprefix+'variant';
  601. arraydef :
  602. begin
  603. if is_chararray(para.left.resulttype.def) then
  604. name := procprefix+'pchar_as_array'
  605. else
  606. begin
  607. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  608. error_para := true;
  609. end
  610. end
  611. else
  612. begin
  613. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  614. error_para := true;
  615. end
  616. end;
  617. { check for length/fractional colon para's }
  618. fracpara := nil;
  619. lenpara := nil;
  620. if assigned(para.right) and
  621. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  622. begin
  623. lenpara := tcallparanode(para.right);
  624. if assigned(lenpara.right) and
  625. (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  626. fracpara:=tcallparanode(lenpara.right);
  627. end;
  628. { get the next parameter now already, because we're going }
  629. { to muck around with the pointers }
  630. if assigned(fracpara) then
  631. nextpara := tcallparanode(fracpara.right)
  632. else if assigned(lenpara) then
  633. nextpara := tcallparanode(lenpara.right)
  634. else
  635. nextpara := tcallparanode(para.right);
  636. { check if a fracpara is allowed }
  637. if assigned(fracpara) and not is_real then
  638. begin
  639. CGMessagePos(fracpara.fileinfo,parser_e_illegal_colon_qualifier);
  640. error_para := true;
  641. end
  642. else if assigned(lenpara) and do_read then
  643. begin
  644. { I think this is already filtered out by parsing, but I'm not sure (JM) }
  645. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  646. error_para := true;
  647. end;
  648. { adjust found_error }
  649. found_error := found_error or error_para;
  650. if not error_para then
  651. begin
  652. { create dummy frac/len para's if necessary }
  653. if not do_read then
  654. begin
  655. { difference in default value for floats and the rest :( }
  656. if not is_real then
  657. begin
  658. if not assigned(lenpara) then
  659. lenpara := ccallparanode.create(
  660. cordconstnode.create(0,sinttype,false),nil)
  661. else
  662. { make sure we don't pass the successive }
  663. { parameters too. We also already have a }
  664. { reference to the next parameter in }
  665. { nextpara }
  666. lenpara.right := nil;
  667. end
  668. else
  669. begin
  670. if not assigned(lenpara) then
  671. lenpara := ccallparanode.create(
  672. cordconstnode.create(-32767,sinttype,false),nil);
  673. { also create a default fracpara if necessary }
  674. if not assigned(fracpara) then
  675. fracpara := ccallparanode.create(
  676. cordconstnode.create(-1,sinttype,false),nil);
  677. { add it to the lenpara }
  678. lenpara.right := fracpara;
  679. { and add the realtype para (this also removes the link }
  680. { to any parameters coming after it) }
  681. fracpara.right := ccallparanode.create(
  682. cordconstnode.create(ord(tfloatdef(para.left.resulttype.def).typ),
  683. sinttype,true),nil);
  684. end;
  685. end;
  686. { special handling of reading small numbers, because the helpers }
  687. { expect a longint/card/bestreal var parameter. Use a temp. can't }
  688. { use functions because then the call to FPC_IOCHECK destroys }
  689. { their result before we can store it }
  690. if do_read and
  691. assigned(readfunctype.def) and
  692. (para.left.resulttype.def<>readfunctype.def) then
  693. begin
  694. { create the parameter list: the temp ... }
  695. temp := ctempcreatenode.create(readfunctype,readfunctype.def.size,tt_persistent,false);
  696. addstatement(newstatement,temp);
  697. { ... and the file }
  698. p1 := ccallparanode.create(ctemprefnode.create(temp),
  699. filepara.getcopy);
  700. { create the call to the helper }
  701. addstatement(newstatement,
  702. ccallnode.createintern(name,tcallparanode(p1)));
  703. { assign the result to the original var (this automatically }
  704. { takes care of range checking) }
  705. addstatement(newstatement,
  706. cassignmentnode.create(para.left,
  707. ctemprefnode.create(temp)));
  708. { release the temp location }
  709. addstatement(newstatement,ctempdeletenode.create(temp));
  710. { statement of para is used }
  711. para.left := nil;
  712. { free the enclosing tcallparanode, but not the }
  713. { parameters coming after it }
  714. para.right := nil;
  715. para.free;
  716. end
  717. else
  718. { read of non s/u-8/16bit, or a write }
  719. begin
  720. { add the filepara to the current parameter }
  721. para.right := filepara.getcopy;
  722. { add the lenpara (fracpara and realtype are already linked }
  723. { with it if necessary) }
  724. tcallparanode(para.right).right := lenpara;
  725. { in case of writing a chararray, add whether it's }
  726. { zero-based }
  727. if not(do_read) and
  728. (para.left.resulttype.def.deftype = arraydef) then
  729. para := ccallparanode.create(cordconstnode.create(
  730. ord(tarraydef(para.left.resulttype.def).lowrange=0),booltype,false),para);
  731. { create the call statement }
  732. addstatement(newstatement,
  733. ccallnode.createintern(name,para));
  734. end
  735. end
  736. else
  737. { error_para = true }
  738. begin
  739. { free the parameter, since it isn't referenced anywhere anymore }
  740. para.right := nil;
  741. para.free;
  742. if assigned(lenpara) then
  743. begin
  744. lenpara.right := nil;
  745. lenpara.free;
  746. end;
  747. if assigned(fracpara) then
  748. begin
  749. fracpara.right := nil;
  750. fracpara.free;
  751. end;
  752. end;
  753. { process next parameter }
  754. para := nextpara;
  755. end;
  756. { if no error, add the write(ln)/read(ln) end calls }
  757. if not found_error then
  758. begin
  759. case inlinenumber of
  760. in_read_x:
  761. name:='fpc_read_end';
  762. in_write_x:
  763. name:='fpc_write_end';
  764. in_readln_x:
  765. name:='fpc_readln_end';
  766. in_writeln_x:
  767. name:='fpc_writeln_end';
  768. end;
  769. addstatement(newstatement,ccallnode.createintern(name,filepara));
  770. end;
  771. end;
  772. { if we found an error, simply delete the generated blocknode }
  773. if found_error then
  774. newblock.free
  775. else
  776. begin
  777. { deallocate the temp for the file para if we used one }
  778. if assigned(filetemp) then
  779. addstatement(newstatement,ctempdeletenode.create(filetemp));
  780. { otherwise return the newly generated block of instructions, }
  781. { but first free the errornode we generated at the beginning }
  782. result.free;
  783. result := newblock
  784. end;
  785. end;
  786. function tinlinenode.handle_val: tnode;
  787. var
  788. procname,
  789. suffix : string[31];
  790. sourcepara,
  791. destpara,
  792. codepara,
  793. sizepara,
  794. newparas : tcallparanode;
  795. orgcode : tnode;
  796. newstatement : tstatementnode;
  797. newblock : tblocknode;
  798. tempcode : ttempcreatenode;
  799. begin
  800. { for easy exiting if something goes wrong }
  801. result := cerrornode.create;
  802. { check the amount of parameters }
  803. if not(assigned(left)) or
  804. not(assigned(tcallparanode(left).right)) then
  805. begin
  806. CGMessage(parser_e_wrong_parameter_size);
  807. exit;
  808. end;
  809. { reverse parameters for easier processing }
  810. left := reverseparameters(tcallparanode(left));
  811. { get the parameters }
  812. tempcode := nil;
  813. orgcode := nil;
  814. sizepara := nil;
  815. sourcepara := tcallparanode(left);
  816. destpara := tcallparanode(sourcepara.right);
  817. codepara := tcallparanode(destpara.right);
  818. { check if codepara is valid }
  819. if assigned(codepara) and
  820. (
  821. (codepara.resulttype.def.deftype <> orddef)
  822. {$ifndef cpu64bit}
  823. or is_64bitint(codepara.resulttype.def)
  824. {$endif cpu64bit}
  825. ) then
  826. begin
  827. CGMessagePos1(codepara.fileinfo,type_e_integer_expr_expected,codepara.resulttype.def.typename);
  828. exit;
  829. end;
  830. { check if dest para is valid }
  831. if not(destpara.resulttype.def.deftype in [orddef,floatdef]) then
  832. begin
  833. CGMessagePos(destpara.fileinfo,type_e_integer_or_real_expr_expected);
  834. exit;
  835. end;
  836. { we're going to reuse the exisiting para's, so make sure they }
  837. { won't be disposed }
  838. left := nil;
  839. { create the blocknode which will hold the generated statements + }
  840. { an initial dummy statement }
  841. newblock:=internalstatements(newstatement);
  842. { do we need a temp for code? Yes, if no code specified, or if }
  843. { code is not a 32bit parameter (we already checked whether the }
  844. { the code para, if specified, was an orddef) }
  845. if not assigned(codepara) or
  846. (codepara.resulttype.def.size<>sinttype.def.size) then
  847. begin
  848. tempcode := ctempcreatenode.create(sinttype,sinttype.def.size,tt_persistent,false);
  849. addstatement(newstatement,tempcode);
  850. { set the resulttype of the temp (needed to be able to get }
  851. { the resulttype of the tempref used in the new code para) }
  852. resulttypepass(tnode(tempcode));
  853. { create a temp codepara, but save the original code para to }
  854. { assign the result to later on }
  855. if assigned(codepara) then
  856. begin
  857. orgcode := codepara.left;
  858. codepara.left := ctemprefnode.create(tempcode);
  859. end
  860. else
  861. codepara := ccallparanode.create(ctemprefnode.create(tempcode),nil);
  862. { we need its resulttype later on }
  863. codepara.get_paratype;
  864. end
  865. else if (torddef(codepara.resulttype.def).typ = torddef(sinttype.def).typ) then
  866. { because code is a var parameter, it must match types exactly }
  867. { however, since it will return values in [0..255], both longints }
  868. { and cardinals are fine. Since the formal code para type is }
  869. { longint, insert a typecoversion to longint for cardinal para's }
  870. begin
  871. codepara.left := ctypeconvnode.create_internal(codepara.left,sinttype);
  872. { make it explicit, oterwise you may get a nonsense range }
  873. { check error if the cardinal already contained a value }
  874. { > $7fffffff }
  875. codepara.get_paratype;
  876. end;
  877. { create the procedure name }
  878. procname := 'fpc_val_';
  879. case destpara.resulttype.def.deftype of
  880. orddef:
  881. begin
  882. case torddef(destpara.resulttype.def).typ of
  883. {$ifdef cpu64bit}
  884. scurrency,
  885. s64bit,
  886. {$endif cpu64bit}
  887. s8bit,
  888. s16bit,
  889. s32bit:
  890. begin
  891. suffix := 'sint_';
  892. { we also need a destsize para in this case }
  893. sizepara := ccallparanode.create(cordconstnode.create
  894. (destpara.resulttype.def.size,s32inttype,true),nil);
  895. end;
  896. {$ifdef cpu64bit}
  897. u64bit,
  898. {$endif cpu64bit}
  899. u8bit,
  900. u16bit,
  901. u32bit:
  902. suffix := 'uint_';
  903. {$ifndef cpu64bit}
  904. scurrency,
  905. s64bit: suffix := 'int64_';
  906. u64bit: suffix := 'qword_';
  907. {$endif cpu64bit}
  908. else
  909. internalerror(200304225);
  910. end;
  911. end;
  912. floatdef:
  913. begin
  914. suffix := 'real_';
  915. end;
  916. end;
  917. procname := procname + suffix;
  918. { play a trick to have tcallnode handle invalid source parameters: }
  919. { the shortstring-longint val routine by default }
  920. if (sourcepara.resulttype.def.deftype = stringdef) then
  921. procname := procname + tstringdef(sourcepara.resulttype.def).stringtypname
  922. else
  923. procname := procname + 'shortstr';
  924. { set up the correct parameters for the call: the code para... }
  925. newparas := codepara;
  926. { and the source para }
  927. codepara.right := sourcepara;
  928. { sizepara either contains nil if none is needed (which is ok, since }
  929. { then the next statement severes any possible links with other paras }
  930. { that sourcepara may have) or it contains the necessary size para and }
  931. { its right field is nil }
  932. sourcepara.right := sizepara;
  933. { create the call and assign the result to dest (val helpers are functions).
  934. Use a trick to prevent a type size mismatch warning to be generated by the
  935. assignment node. First convert implicitly to the resulttype. This will insert
  936. the range check. The Second conversion is done explicitly to hide the implicit conversion
  937. for the assignment node and therefor preventing the warning (PFV) }
  938. addstatement(newstatement,cassignmentnode.create(
  939. destpara.left,ctypeconvnode.create_internal(ctypeconvnode.create(ccallnode.createintern(procname,newparas),destpara.left.resulttype),destpara.left.resulttype)));
  940. { dispose of the enclosing paranode of the destination }
  941. destpara.left := nil;
  942. destpara.right := nil;
  943. destpara.free;
  944. { check if we used a temp for code and whether we have to store }
  945. { it to the real code parameter }
  946. if assigned(orgcode) then
  947. addstatement(newstatement,cassignmentnode.create(
  948. orgcode,
  949. ctemprefnode.create(tempcode)));
  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. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1516. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resulttype);
  1517. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1518. CGMessage(parser_e_illegal_expression);
  1519. end;
  1520. end
  1521. else
  1522. CGMessage(type_e_ordinal_expr_expected);
  1523. end
  1524. else
  1525. CGMessage(type_e_mismatch);
  1526. end;
  1527. in_read_x,
  1528. in_readln_x,
  1529. in_write_x,
  1530. in_writeln_x :
  1531. begin
  1532. result := handle_read_write;
  1533. end;
  1534. in_settextbuf_file_x :
  1535. begin
  1536. resulttype:=voidtype;
  1537. { now we know the type of buffer }
  1538. srsym:=searchsymonlyin(systemunit,'SETTEXTBUF');
  1539. hp:=ccallparanode.create(cordconstnode.create(
  1540. tcallparanode(left).left.resulttype.def.size,s32inttype,true),left);
  1541. result:=ccallnode.create(hp,tprocsym(srsym),systemunit,nil,[]);
  1542. left:=nil;
  1543. end;
  1544. { the firstpass of the arg has been done in firstcalln ? }
  1545. in_reset_typedfile,
  1546. in_rewrite_typedfile :
  1547. begin
  1548. result := handle_reset_rewrite_typed;
  1549. end;
  1550. in_str_x_string :
  1551. begin
  1552. result := handle_str;
  1553. end;
  1554. in_val_x :
  1555. begin
  1556. result := handle_val;
  1557. end;
  1558. in_include_x_y,
  1559. in_exclude_x_y:
  1560. begin
  1561. resulttype:=voidtype;
  1562. { the parser already checks whether we have two (and exectly two) }
  1563. { parameters (JM) }
  1564. { first param must be var }
  1565. valid_for_var(tcallparanode(left).left,true);
  1566. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1567. { check type }
  1568. if (left.resulttype.def.deftype=setdef) then
  1569. begin
  1570. { insert a type conversion }
  1571. { to the type of the set elements }
  1572. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1573. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  1574. tsetdef(left.resulttype.def).elementtype);
  1575. end
  1576. else
  1577. CGMessage(type_e_mismatch);
  1578. end;
  1579. in_slice_x:
  1580. begin
  1581. result:=nil;
  1582. resulttype:=tcallparanode(tcallparanode(left).left).resulttype;
  1583. if not(resulttype.def.deftype=arraydef) then
  1584. CGMessage(type_e_mismatch);
  1585. end;
  1586. in_low_x,
  1587. in_high_x:
  1588. begin
  1589. case left.resulttype.def.deftype of
  1590. orddef,
  1591. enumdef:
  1592. begin
  1593. result:=do_lowhigh(left.resulttype);
  1594. end;
  1595. setdef:
  1596. begin
  1597. result:=do_lowhigh(tsetdef(left.resulttype.def).elementtype);
  1598. end;
  1599. arraydef:
  1600. begin
  1601. if inlinenumber=in_low_x then
  1602. begin
  1603. result:=cordconstnode.create(tarraydef(
  1604. left.resulttype.def).lowrange,tarraydef(left.resulttype.def).rangetype,true);
  1605. end
  1606. else
  1607. begin
  1608. if is_open_array(left.resulttype.def) or
  1609. is_array_of_const(left.resulttype.def) then
  1610. begin
  1611. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1612. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1613. end
  1614. else
  1615. if is_dynamic_array(left.resulttype.def) then
  1616. begin
  1617. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1618. { can't use inserttypeconv because we need }
  1619. { an explicit type conversion (JM) }
  1620. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1621. result := ccallnode.createintern('fpc_dynarray_high',hp);
  1622. { make sure the left node doesn't get disposed, since it's }
  1623. { reused in the new node (JM) }
  1624. left:=nil;
  1625. end
  1626. else
  1627. begin
  1628. result:=cordconstnode.create(tarraydef(
  1629. left.resulttype.def).highrange,tarraydef(left.resulttype.def).rangetype,true);
  1630. end;
  1631. end;
  1632. end;
  1633. stringdef:
  1634. begin
  1635. if inlinenumber=in_low_x then
  1636. begin
  1637. result:=cordconstnode.create(0,u8inttype,false);
  1638. end
  1639. else
  1640. begin
  1641. if is_open_string(left.resulttype.def) then
  1642. begin
  1643. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1644. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  1645. end
  1646. else
  1647. result:=cordconstnode.create(tstringdef(left.resulttype.def).len,u8inttype,true);
  1648. end;
  1649. end;
  1650. else
  1651. CGMessage(type_e_mismatch);
  1652. end;
  1653. end;
  1654. in_exp_real :
  1655. begin
  1656. if left.nodetype in [ordconstn,realconstn] then
  1657. begin
  1658. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  1659. if (trealconstnode(result).value_real=double(MathInf)) and
  1660. ((cs_check_range in aktlocalswitches) or
  1661. (cs_check_overflow in aktlocalswitches)) then
  1662. begin
  1663. result:=crealconstnode.create(0,pbestrealtype^);
  1664. CGMessage(parser_e_range_check_error);
  1665. end;
  1666. end
  1667. else
  1668. begin
  1669. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1670. inserttypeconv(left,pbestrealtype^);
  1671. resulttype:=pbestrealtype^;
  1672. end;
  1673. end;
  1674. in_trunc_real :
  1675. begin
  1676. if left.nodetype in [ordconstn,realconstn] then
  1677. begin
  1678. vr:=getconstrealvalue;
  1679. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1680. begin
  1681. CGMessage(parser_e_range_check_error);
  1682. result:=cordconstnode.create(1,s64inttype,false)
  1683. end
  1684. else
  1685. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  1686. end
  1687. else
  1688. begin
  1689. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1690. inserttypeconv(left,pbestrealtype^);
  1691. resulttype:=s64inttype;
  1692. end;
  1693. end;
  1694. in_round_real :
  1695. begin
  1696. if left.nodetype in [ordconstn,realconstn] then
  1697. begin
  1698. vr:=getconstrealvalue;
  1699. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1700. begin
  1701. CGMessage(parser_e_range_check_error);
  1702. result:=cordconstnode.create(1,s64inttype,false)
  1703. end
  1704. else
  1705. result:=cordconstnode.create(round(vr),s64inttype,true)
  1706. end
  1707. else
  1708. begin
  1709. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1710. inserttypeconv(left,pbestrealtype^);
  1711. resulttype:=s64inttype;
  1712. end;
  1713. end;
  1714. in_frac_real :
  1715. begin
  1716. if left.nodetype in [ordconstn,realconstn] then
  1717. setconstrealvalue(frac(getconstrealvalue))
  1718. else
  1719. begin
  1720. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1721. inserttypeconv(left,pbestrealtype^);
  1722. resulttype:=pbestrealtype^;
  1723. end;
  1724. end;
  1725. in_int_real :
  1726. begin
  1727. if left.nodetype in [ordconstn,realconstn] then
  1728. setconstrealvalue(int(getconstrealvalue))
  1729. else
  1730. begin
  1731. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1732. inserttypeconv(left,pbestrealtype^);
  1733. resulttype:=pbestrealtype^;
  1734. end;
  1735. end;
  1736. in_pi_real :
  1737. begin
  1738. if block_type=bt_const then
  1739. setconstrealvalue(getpi)
  1740. else
  1741. resulttype:=pbestrealtype^;
  1742. end;
  1743. in_cos_real :
  1744. begin
  1745. if left.nodetype in [ordconstn,realconstn] then
  1746. setconstrealvalue(cos(getconstrealvalue))
  1747. else
  1748. begin
  1749. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1750. inserttypeconv(left,pbestrealtype^);
  1751. resulttype:=pbestrealtype^;
  1752. end;
  1753. end;
  1754. in_sin_real :
  1755. begin
  1756. if left.nodetype in [ordconstn,realconstn] then
  1757. setconstrealvalue(sin(getconstrealvalue))
  1758. else
  1759. begin
  1760. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1761. inserttypeconv(left,pbestrealtype^);
  1762. resulttype:=pbestrealtype^;
  1763. end;
  1764. end;
  1765. in_arctan_real :
  1766. begin
  1767. if left.nodetype in [ordconstn,realconstn] then
  1768. setconstrealvalue(arctan(getconstrealvalue))
  1769. else
  1770. begin
  1771. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1772. inserttypeconv(left,pbestrealtype^);
  1773. resulttype:=pbestrealtype^;
  1774. end;
  1775. end;
  1776. in_abs_real :
  1777. begin
  1778. if left.nodetype in [ordconstn,realconstn] then
  1779. setconstrealvalue(abs(getconstrealvalue))
  1780. else
  1781. begin
  1782. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1783. inserttypeconv(left,pbestrealtype^);
  1784. resulttype:=pbestrealtype^;
  1785. end;
  1786. end;
  1787. in_sqr_real :
  1788. begin
  1789. if left.nodetype in [ordconstn,realconstn] then
  1790. setconstrealvalue(sqr(getconstrealvalue))
  1791. else
  1792. begin
  1793. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1794. setfloatresulttype;
  1795. end;
  1796. end;
  1797. in_sqrt_real :
  1798. begin
  1799. if left.nodetype in [ordconstn,realconstn] then
  1800. begin
  1801. vr:=getconstrealvalue;
  1802. if vr<0.0 then
  1803. result:=handle_sqrt_const(vr)
  1804. else
  1805. setconstrealvalue(sqrt(vr));
  1806. end
  1807. else
  1808. begin
  1809. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1810. setfloatresulttype;
  1811. end;
  1812. end;
  1813. in_ln_real :
  1814. begin
  1815. if left.nodetype in [ordconstn,realconstn] then
  1816. begin
  1817. vr:=getconstrealvalue;
  1818. if vr<=0.0 then
  1819. result:=handle_ln_const(vr)
  1820. else
  1821. setconstrealvalue(ln(vr));
  1822. end
  1823. else
  1824. begin
  1825. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1826. inserttypeconv(left,pbestrealtype^);
  1827. resulttype:=pbestrealtype^;
  1828. end;
  1829. end;
  1830. {$ifdef SUPPORT_MMX}
  1831. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  1832. begin
  1833. end;
  1834. {$endif SUPPORT_MMX}
  1835. in_prefetch_var:
  1836. begin
  1837. resulttype:=voidtype;
  1838. end;
  1839. in_assert_x_y :
  1840. begin
  1841. resulttype:=voidtype;
  1842. if assigned(left) then
  1843. begin
  1844. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  1845. { check type }
  1846. if is_boolean(left.resulttype.def) then
  1847. begin
  1848. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1849. { must always be a string }
  1850. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  1851. end
  1852. else
  1853. CGMessage1(type_e_boolean_expr_expected,left.resulttype.def.typename);
  1854. end
  1855. else
  1856. CGMessage(type_e_mismatch);
  1857. { We've checked the whole statement for correctness, now we
  1858. can remove it if assertions are off }
  1859. if not(cs_do_assertion in aktlocalswitches) then
  1860. begin
  1861. { we need a valid node, so insert a nothingn }
  1862. result:=cnothingnode.create;
  1863. end
  1864. else
  1865. include(current_procinfo.flags,pi_do_call);
  1866. end;
  1867. else
  1868. internalerror(8);
  1869. end;
  1870. end;
  1871. myexit:
  1872. { Run get_paratype again to update maybe inserted typeconvs }
  1873. if not codegenerror then
  1874. begin
  1875. if assigned(left) and
  1876. (left.nodetype=callparan) then
  1877. tcallparanode(left).get_paratype;
  1878. end;
  1879. dec(parsing_para_level);
  1880. end;
  1881. function tinlinenode.pass_1 : tnode;
  1882. var
  1883. hp,hpp : tnode;
  1884. shiftconst: longint;
  1885. tempnode: ttempcreatenode;
  1886. newstatement: tstatementnode;
  1887. newblock: tblocknode;
  1888. begin
  1889. result:=nil;
  1890. { if we handle writeln; left contains no valid address }
  1891. if assigned(left) then
  1892. begin
  1893. if left.nodetype=callparan then
  1894. tcallparanode(left).firstcallparan
  1895. else
  1896. firstpass(left);
  1897. left_max;
  1898. end;
  1899. inc(parsing_para_level);
  1900. { intern const should already be handled }
  1901. if nf_inlineconst in flags then
  1902. internalerror(200104044);
  1903. case inlinenumber of
  1904. in_lo_qword,
  1905. in_hi_qword,
  1906. in_lo_long,
  1907. in_hi_long,
  1908. in_lo_word,
  1909. in_hi_word:
  1910. begin
  1911. shiftconst := 0;
  1912. case inlinenumber of
  1913. in_hi_qword:
  1914. shiftconst := 32;
  1915. in_hi_long:
  1916. shiftconst := 16;
  1917. in_hi_word:
  1918. shiftconst := 8;
  1919. end;
  1920. if shiftconst <> 0 then
  1921. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  1922. cordconstnode.create(shiftconst,u32inttype,false)),resulttype)
  1923. else
  1924. result := ctypeconvnode.create_internal(left,resulttype);
  1925. left := nil;
  1926. firstpass(result);
  1927. end;
  1928. in_sizeof_x:
  1929. begin
  1930. if registersint<1 then
  1931. registersint:=1;
  1932. expectloc:=LOC_REGISTER;
  1933. end;
  1934. in_typeof_x:
  1935. begin
  1936. if registersint<1 then
  1937. registersint:=1;
  1938. expectloc:=LOC_REGISTER;
  1939. end;
  1940. in_length_x:
  1941. begin
  1942. if is_shortstring(left.resulttype.def) then
  1943. expectloc:=left.expectloc
  1944. else
  1945. begin
  1946. { ansi/wide string }
  1947. if registersint<1 then
  1948. registersint:=1;
  1949. expectloc:=LOC_REGISTER;
  1950. end;
  1951. end;
  1952. in_typeinfo_x:
  1953. begin
  1954. expectloc:=LOC_REGISTER;
  1955. registersint:=1;
  1956. end;
  1957. in_assigned_x:
  1958. begin
  1959. expectloc := LOC_JUMP;
  1960. registersint:=1;
  1961. end;
  1962. in_pred_x,
  1963. in_succ_x:
  1964. begin
  1965. if is_64bit(resulttype.def) then
  1966. begin
  1967. if (registersint<2) then
  1968. registersint:=2
  1969. end
  1970. else
  1971. begin
  1972. if (registersint<1) then
  1973. registersint:=1;
  1974. end;
  1975. expectloc:=LOC_REGISTER;
  1976. end;
  1977. in_setlength_x,
  1978. in_initialize_x,
  1979. in_finalize_x:
  1980. begin
  1981. expectloc:=LOC_VOID;
  1982. end;
  1983. in_inc_x,
  1984. in_dec_x:
  1985. begin
  1986. expectloc:=LOC_VOID;
  1987. { check type }
  1988. if
  1989. {$ifndef cpu64bit}
  1990. is_64bit(left.resulttype.def) or
  1991. {$endif cpu64bit}
  1992. { range/overflow checking doesn't work properly }
  1993. { with the inc/dec code that's generated (JM) }
  1994. (
  1995. (((left.resulttype.def.deftype = orddef) and
  1996. not(is_char(left.resulttype.def)) and
  1997. not(is_boolean(left.resulttype.def))) or
  1998. (left.resulttype.def.deftype = pointerdef)) and
  1999. (aktlocalswitches * [cs_check_overflow,cs_check_range] <> [])
  2000. ) then
  2001. { convert to simple add (JM) }
  2002. begin
  2003. newblock := internalstatements(newstatement);
  2004. { extra parameter? }
  2005. if assigned(tcallparanode(left).right) then
  2006. begin
  2007. { Yes, use for add node }
  2008. hpp := tcallparanode(tcallparanode(left).right).left;
  2009. tcallparanode(tcallparanode(left).right).left := nil;
  2010. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2011. CGMessage(parser_e_illegal_expression);
  2012. end
  2013. else
  2014. begin
  2015. { no, create constant 1 }
  2016. hpp := cordconstnode.create(1,tcallparanode(left).left.resulttype,false);
  2017. end;
  2018. resulttypepass(hpp);
  2019. {$ifndef cpu64bit}
  2020. if not((hpp.resulttype.def.deftype=orddef) and
  2021. (torddef(hpp.resulttype.def).typ<>u32bit)) then
  2022. {$endif cpu64bit}
  2023. inserttypeconv_internal(hpp,sinttype);
  2024. { No overflow check for pointer operations, because inc(pointer,-1) will always
  2025. trigger an overflow. For uint32 it works because then the operation is done
  2026. in 64bit }
  2027. if (tcallparanode(left).left.resulttype.def.deftype=pointerdef) then
  2028. exclude(aktlocalswitches,cs_check_overflow);
  2029. { make sure we don't call functions part of the left node twice (and generally }
  2030. { optimize the code generation) }
  2031. if node_complexity(tcallparanode(left).left) > 1 then
  2032. begin
  2033. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.def.size,tt_persistent,true);
  2034. addstatement(newstatement,tempnode);
  2035. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2036. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  2037. hp := cderefnode.create(ctemprefnode.create(tempnode));
  2038. inserttypeconv_internal(hp,tcallparanode(left).left.resulttype);
  2039. end
  2040. else
  2041. begin
  2042. hp := tcallparanode(left).left.getcopy;
  2043. tempnode := nil;
  2044. end;
  2045. { addition/substraction depending on inc/dec }
  2046. if inlinenumber = in_inc_x then
  2047. hpp := caddnode.create(addn,hp,hpp)
  2048. else
  2049. hpp := caddnode.create(subn,hp,hpp);
  2050. { assign result of addition }
  2051. inserttypeconv_internal(hpp,hp.resulttype);
  2052. addstatement(newstatement,cassignmentnode.create(hp.getcopy,hpp));
  2053. { deallocate the temp }
  2054. if assigned(tempnode) then
  2055. addstatement(newstatement,ctempdeletenode.create(tempnode));
  2056. { firstpass it }
  2057. firstpass(newblock);
  2058. { return new node }
  2059. result := newblock;
  2060. end
  2061. else if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  2062. is_ordinal(left.resulttype.def) then
  2063. begin
  2064. { two paras ? }
  2065. if assigned(tcallparanode(left).right) then
  2066. begin
  2067. { need we an additional register ? }
  2068. if not(is_constintnode(tcallparanode(tcallparanode(left).right).left)) and
  2069. (tcallparanode(tcallparanode(left).right).left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) and
  2070. (tcallparanode(tcallparanode(left).right).left.registersint<=1) then
  2071. inc(registersint);
  2072. { do we need an additional register to restore the first parameter? }
  2073. if tcallparanode(tcallparanode(left).right).left.registersint>=registersint then
  2074. inc(registersint);
  2075. end;
  2076. end;
  2077. end;
  2078. in_include_x_y,
  2079. in_exclude_x_y:
  2080. begin
  2081. expectloc:=LOC_VOID;
  2082. registersint:=left.registersint;
  2083. registersfpu:=left.registersfpu;
  2084. {$ifdef SUPPORT_MMX}
  2085. registersmmx:=left.registersmmx;
  2086. {$endif SUPPORT_MMX}
  2087. end;
  2088. in_exp_real:
  2089. begin
  2090. result:= first_exp_real;
  2091. end;
  2092. in_round_real:
  2093. begin
  2094. result:= first_round_real;
  2095. end;
  2096. in_trunc_real:
  2097. begin
  2098. result:= first_trunc_real;
  2099. end;
  2100. in_int_real:
  2101. begin
  2102. result:= first_int_real;
  2103. end;
  2104. in_frac_real:
  2105. begin
  2106. result:= first_frac_real;
  2107. end;
  2108. in_cos_real:
  2109. begin
  2110. result:= first_cos_real;
  2111. end;
  2112. in_sin_real:
  2113. begin
  2114. result := first_sin_real;
  2115. end;
  2116. in_arctan_real:
  2117. begin
  2118. result := first_arctan_real;
  2119. end;
  2120. in_pi_real :
  2121. begin
  2122. result := first_pi;
  2123. end;
  2124. in_abs_real:
  2125. begin
  2126. result := first_abs_real;
  2127. end;
  2128. in_sqr_real:
  2129. begin
  2130. result := first_sqr_real;
  2131. end;
  2132. in_sqrt_real:
  2133. begin
  2134. result := first_sqrt_real;
  2135. end;
  2136. in_ln_real:
  2137. begin
  2138. result := first_ln_real;
  2139. end;
  2140. {$ifdef SUPPORT_MMX}
  2141. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2142. begin
  2143. end;
  2144. {$endif SUPPORT_MMX}
  2145. in_assert_x_y :
  2146. begin
  2147. expectloc:=LOC_VOID;
  2148. registersint:=left.registersint;
  2149. registersfpu:=left.registersfpu;
  2150. {$ifdef SUPPORT_MMX}
  2151. registersmmx:=left.registersmmx;
  2152. {$endif SUPPORT_MMX}
  2153. end;
  2154. in_low_x,
  2155. in_high_x:
  2156. internalerror(200104047);
  2157. in_slice_x:
  2158. internalerror(2005101501);
  2159. in_ord_x,
  2160. in_chr_byte:
  2161. begin
  2162. { should not happend as it's converted to typeconv }
  2163. internalerror(200104045);
  2164. end;
  2165. in_ofs_x :
  2166. internalerror(2000101001);
  2167. in_seg_x :
  2168. internalerror(200104046);
  2169. in_settextbuf_file_x,
  2170. in_reset_typedfile,
  2171. in_rewrite_typedfile,
  2172. in_str_x_string,
  2173. in_val_x,
  2174. in_read_x,
  2175. in_readln_x,
  2176. in_write_x,
  2177. in_writeln_x :
  2178. begin
  2179. { should be handled by det_resulttype }
  2180. internalerror(200108234);
  2181. end;
  2182. in_prefetch_var:
  2183. begin
  2184. expectloc:=LOC_VOID;
  2185. end;
  2186. else
  2187. internalerror(8);
  2188. end;
  2189. dec(parsing_para_level);
  2190. end;
  2191. {$ifdef fpc}
  2192. {$maxfpuregisters default}
  2193. {$endif fpc}
  2194. function tinlinenode.docompare(p: tnode): boolean;
  2195. begin
  2196. docompare :=
  2197. inherited docompare(p) and
  2198. (inlinenumber = tinlinenode(p).inlinenumber);
  2199. end;
  2200. function tinlinenode.first_pi : tnode;
  2201. begin
  2202. result:=crealconstnode.create(getpi,pbestrealtype^);
  2203. end;
  2204. function tinlinenode.first_arctan_real : tnode;
  2205. begin
  2206. { create the call to the helper }
  2207. { on entry left node contains the parameter }
  2208. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  2209. ccallparanode.create(left,nil));
  2210. left := nil;
  2211. end;
  2212. function tinlinenode.first_abs_real : tnode;
  2213. begin
  2214. { create the call to the helper }
  2215. { on entry left node contains the parameter }
  2216. first_abs_real := ccallnode.createintern('fpc_abs_real',
  2217. ccallparanode.create(left,nil));
  2218. left := nil;
  2219. end;
  2220. function tinlinenode.first_sqr_real : tnode;
  2221. begin
  2222. { create the call to the helper }
  2223. { on entry left node contains the parameter }
  2224. first_sqr_real := ctypeconvnode.create_internal(ccallnode.createintern('fpc_sqr_real',
  2225. ccallparanode.create(left,nil)),resulttype);
  2226. left := nil;
  2227. end;
  2228. function tinlinenode.first_sqrt_real : tnode;
  2229. begin
  2230. { create the call to the helper }
  2231. { on entry left node contains the parameter }
  2232. first_sqrt_real := ctypeconvnode.create_internal(ccallnode.createintern('fpc_sqrt_real',
  2233. ccallparanode.create(left,nil)),resulttype);
  2234. left := nil;
  2235. end;
  2236. function tinlinenode.first_ln_real : tnode;
  2237. begin
  2238. { create the call to the helper }
  2239. { on entry left node contains the parameter }
  2240. first_ln_real := ccallnode.createintern('fpc_ln_real',
  2241. ccallparanode.create(left,nil));
  2242. left := nil;
  2243. end;
  2244. function tinlinenode.first_cos_real : tnode;
  2245. begin
  2246. { create the call to the helper }
  2247. { on entry left node contains the parameter }
  2248. first_cos_real := ccallnode.createintern('fpc_cos_real',
  2249. ccallparanode.create(left,nil));
  2250. left := nil;
  2251. end;
  2252. function tinlinenode.first_sin_real : tnode;
  2253. begin
  2254. { create the call to the helper }
  2255. { on entry left node contains the parameter }
  2256. first_sin_real := ccallnode.createintern('fpc_sin_real',
  2257. ccallparanode.create(left,nil));
  2258. left := nil;
  2259. end;
  2260. function tinlinenode.first_exp_real : tnode;
  2261. begin
  2262. { create the call to the helper }
  2263. { on entry left node contains the parameter }
  2264. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  2265. left := nil;
  2266. end;
  2267. function tinlinenode.first_int_real : tnode;
  2268. begin
  2269. { create the call to the helper }
  2270. { on entry left node contains the parameter }
  2271. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  2272. left := nil;
  2273. end;
  2274. function tinlinenode.first_frac_real : tnode;
  2275. begin
  2276. { create the call to the helper }
  2277. { on entry left node contains the parameter }
  2278. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  2279. left := nil;
  2280. end;
  2281. function tinlinenode.first_round_real : tnode;
  2282. begin
  2283. { create the call to the helper }
  2284. { on entry left node contains the parameter }
  2285. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  2286. left := nil;
  2287. end;
  2288. function tinlinenode.first_trunc_real : tnode;
  2289. begin
  2290. { create the call to the helper }
  2291. { on entry left node contains the parameter }
  2292. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  2293. left := nil;
  2294. end;
  2295. begin
  2296. cinlinenode:=tinlinenode;
  2297. end.