ninl.pas 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527
  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. { create the call statement }
  726. addstatement(newstatement,
  727. ccallnode.createintern(name,para));
  728. end
  729. end
  730. else
  731. { error_para = true }
  732. begin
  733. { free the parameter, since it isn't referenced anywhere anymore }
  734. para.right := nil;
  735. para.free;
  736. if assigned(lenpara) then
  737. begin
  738. lenpara.right := nil;
  739. lenpara.free;
  740. end;
  741. if assigned(fracpara) then
  742. begin
  743. fracpara.right := nil;
  744. fracpara.free;
  745. end;
  746. end;
  747. { process next parameter }
  748. para := nextpara;
  749. end;
  750. { if no error, add the write(ln)/read(ln) end calls }
  751. if not found_error then
  752. begin
  753. case inlinenumber of
  754. in_read_x:
  755. name:='fpc_read_end';
  756. in_write_x:
  757. name:='fpc_write_end';
  758. in_readln_x:
  759. name:='fpc_readln_end';
  760. in_writeln_x:
  761. name:='fpc_writeln_end';
  762. end;
  763. addstatement(newstatement,ccallnode.createintern(name,filepara));
  764. end;
  765. end;
  766. { if we found an error, simply delete the generated blocknode }
  767. if found_error then
  768. newblock.free
  769. else
  770. begin
  771. { deallocate the temp for the file para if we used one }
  772. if assigned(filetemp) then
  773. addstatement(newstatement,ctempdeletenode.create(filetemp));
  774. { otherwise return the newly generated block of instructions, }
  775. { but first free the errornode we generated at the beginning }
  776. result.free;
  777. result := newblock
  778. end;
  779. end;
  780. function tinlinenode.handle_val: tnode;
  781. var
  782. procname,
  783. suffix : string[31];
  784. sourcepara,
  785. destpara,
  786. codepara,
  787. sizepara,
  788. newparas : tcallparanode;
  789. orgcode : tnode;
  790. newstatement : tstatementnode;
  791. newblock : tblocknode;
  792. tempcode : ttempcreatenode;
  793. begin
  794. { for easy exiting if something goes wrong }
  795. result := cerrornode.create;
  796. { check the amount of parameters }
  797. if not(assigned(left)) or
  798. not(assigned(tcallparanode(left).right)) then
  799. begin
  800. CGMessage(parser_e_wrong_parameter_size);
  801. exit;
  802. end;
  803. { reverse parameters for easier processing }
  804. left := reverseparameters(tcallparanode(left));
  805. { get the parameters }
  806. tempcode := nil;
  807. orgcode := nil;
  808. sizepara := nil;
  809. sourcepara := tcallparanode(left);
  810. destpara := tcallparanode(sourcepara.right);
  811. codepara := tcallparanode(destpara.right);
  812. { check if codepara is valid }
  813. if assigned(codepara) and
  814. (
  815. (codepara.resulttype.def.deftype <> orddef)
  816. {$ifndef cpu64bit}
  817. or is_64bitint(codepara.resulttype.def)
  818. {$endif cpu64bit}
  819. ) then
  820. begin
  821. CGMessagePos1(codepara.fileinfo,type_e_integer_expr_expected,codepara.resulttype.def.typename);
  822. exit;
  823. end;
  824. { check if dest para is valid }
  825. if not(destpara.resulttype.def.deftype in [orddef,floatdef]) then
  826. begin
  827. CGMessagePos(destpara.fileinfo,type_e_integer_or_real_expr_expected);
  828. exit;
  829. end;
  830. { we're going to reuse the exisiting para's, so make sure they }
  831. { won't be disposed }
  832. left := nil;
  833. { create the blocknode which will hold the generated statements + }
  834. { an initial dummy statement }
  835. newblock:=internalstatements(newstatement);
  836. { do we need a temp for code? Yes, if no code specified, or if }
  837. { code is not a 32bit parameter (we already checked whether the }
  838. { the code para, if specified, was an orddef) }
  839. if not assigned(codepara) or
  840. (codepara.resulttype.def.size<>sinttype.def.size) then
  841. begin
  842. tempcode := ctempcreatenode.create(sinttype,sinttype.def.size,tt_persistent,false);
  843. addstatement(newstatement,tempcode);
  844. { set the resulttype of the temp (needed to be able to get }
  845. { the resulttype of the tempref used in the new code para) }
  846. resulttypepass(tnode(tempcode));
  847. { create a temp codepara, but save the original code para to }
  848. { assign the result to later on }
  849. if assigned(codepara) then
  850. begin
  851. orgcode := codepara.left;
  852. codepara.left := ctemprefnode.create(tempcode);
  853. end
  854. else
  855. codepara := ccallparanode.create(ctemprefnode.create(tempcode),nil);
  856. { we need its resulttype later on }
  857. codepara.get_paratype;
  858. end
  859. else if (torddef(codepara.resulttype.def).typ = torddef(sinttype.def).typ) then
  860. { because code is a var parameter, it must match types exactly }
  861. { however, since it will return values in [0..255], both longints }
  862. { and cardinals are fine. Since the formal code para type is }
  863. { longint, insert a typecoversion to longint for cardinal para's }
  864. begin
  865. codepara.left := ctypeconvnode.create_internal(codepara.left,sinttype);
  866. { make it explicit, oterwise you may get a nonsense range }
  867. { check error if the cardinal already contained a value }
  868. { > $7fffffff }
  869. codepara.get_paratype;
  870. end;
  871. { create the procedure name }
  872. procname := 'fpc_val_';
  873. case destpara.resulttype.def.deftype of
  874. orddef:
  875. begin
  876. case torddef(destpara.resulttype.def).typ of
  877. {$ifdef cpu64bit}
  878. scurrency,
  879. s64bit,
  880. {$endif cpu64bit}
  881. s8bit,
  882. s16bit,
  883. s32bit:
  884. begin
  885. suffix := 'sint_';
  886. { we also need a destsize para in this case }
  887. sizepara := ccallparanode.create(cordconstnode.create
  888. (destpara.resulttype.def.size,s32inttype,true),nil);
  889. end;
  890. {$ifdef cpu64bit}
  891. u64bit,
  892. {$endif cpu64bit}
  893. u8bit,
  894. u16bit,
  895. u32bit:
  896. suffix := 'uint_';
  897. {$ifndef cpu64bit}
  898. scurrency,
  899. s64bit: suffix := 'int64_';
  900. u64bit: suffix := 'qword_';
  901. {$endif cpu64bit}
  902. else
  903. internalerror(200304225);
  904. end;
  905. end;
  906. floatdef:
  907. begin
  908. suffix := 'real_';
  909. end;
  910. end;
  911. procname := procname + suffix;
  912. { play a trick to have tcallnode handle invalid source parameters: }
  913. { the shortstring-longint val routine by default }
  914. if (sourcepara.resulttype.def.deftype = stringdef) then
  915. procname := procname + tstringdef(sourcepara.resulttype.def).stringtypname
  916. else
  917. procname := procname + 'shortstr';
  918. { set up the correct parameters for the call: the code para... }
  919. newparas := codepara;
  920. { and the source para }
  921. codepara.right := sourcepara;
  922. { sizepara either contains nil if none is needed (which is ok, since }
  923. { then the next statement severes any possible links with other paras }
  924. { that sourcepara may have) or it contains the necessary size para and }
  925. { its right field is nil }
  926. sourcepara.right := sizepara;
  927. { create the call and assign the result to dest (val helpers are functions).
  928. Use a trick to prevent a type size mismatch warning to be generated by the
  929. assignment node. First convert implicitly to the resulttype. This will insert
  930. the range check. The Second conversion is done explicitly to hide the implicit conversion
  931. for the assignment node and therefor preventing the warning (PFV) }
  932. addstatement(newstatement,cassignmentnode.create(
  933. destpara.left,ctypeconvnode.create_internal(ctypeconvnode.create(ccallnode.createintern(procname,newparas),destpara.left.resulttype),destpara.left.resulttype)));
  934. { dispose of the enclosing paranode of the destination }
  935. destpara.left := nil;
  936. destpara.right := nil;
  937. destpara.free;
  938. { check if we used a temp for code and whether we have to store }
  939. { it to the real code parameter }
  940. if assigned(orgcode) then
  941. addstatement(newstatement,cassignmentnode.create(
  942. orgcode,
  943. ctemprefnode.create(tempcode)));
  944. { release the temp if we allocated one }
  945. if assigned(tempcode) then
  946. addstatement(newstatement,ctempdeletenode.create(tempcode));
  947. { free the errornode }
  948. result.free;
  949. { and return it }
  950. result := newblock;
  951. end;
  952. {$ifdef fpc}
  953. {$maxfpuregisters 0}
  954. {$endif fpc}
  955. function getpi : bestreal;
  956. begin
  957. {$ifdef x86}
  958. { x86 has pi in hardware }
  959. result:=pi;
  960. {$else x86}
  961. {$ifdef cpuextended}
  962. result:=extended(MathPiExtended);
  963. {$else cpuextended}
  964. result:=double(MathPi);
  965. {$endif cpuextended}
  966. {$endif x86}
  967. end;
  968. function tinlinenode.det_resulttype:tnode;
  969. function do_lowhigh(const t:ttype) : tnode;
  970. var
  971. v : tconstexprint;
  972. enum : tenumsym;
  973. hp : tnode;
  974. begin
  975. case t.def.deftype of
  976. orddef:
  977. begin
  978. if inlinenumber=in_low_x then
  979. v:=torddef(t.def).low
  980. else
  981. v:=torddef(t.def).high;
  982. { low/high of torddef are longints, so we need special }
  983. { handling for cardinal and 64bit types (JM) }
  984. { 1.0.x doesn't support int64($ffffffff) correct, it'll expand
  985. to -1 instead of staying $ffffffff. Therefor we use $ffff with
  986. shl twice (PFV) }
  987. case torddef(t.def).typ of
  988. s64bit,scurrency :
  989. begin
  990. if (inlinenumber=in_low_x) then
  991. v := int64($80000000) shl 32
  992. else
  993. v := (int64($7fffffff) shl 32) or int64($ffff) shl 16 or int64($ffff)
  994. end;
  995. u64bit :
  996. begin
  997. { we have to use a dirty trick for high(qword), }
  998. { because it's bigger than high(tconstexprint) (JM) }
  999. v := 0
  1000. end
  1001. else
  1002. begin
  1003. if not is_signed(t.def) then
  1004. v := cardinal(v);
  1005. end;
  1006. end;
  1007. hp:=cordconstnode.create(v,t,true);
  1008. resulttypepass(hp);
  1009. { fix high(qword) }
  1010. if (torddef(t.def).typ=u64bit) and
  1011. (inlinenumber = in_high_x) then
  1012. tordconstnode(hp).value := -1; { is the same as qword($ffffffffffffffff) }
  1013. do_lowhigh:=hp;
  1014. end;
  1015. enumdef:
  1016. begin
  1017. enum:=tenumsym(tenumdef(t.def).firstenum);
  1018. v:=tenumdef(t.def).maxval;
  1019. if inlinenumber=in_high_x then
  1020. while assigned(enum) and (enum.value <> v) do
  1021. enum:=enum.nextenum;
  1022. if not assigned(enum) then
  1023. internalerror(309993)
  1024. else
  1025. hp:=genenumnode(enum);
  1026. do_lowhigh:=hp;
  1027. end;
  1028. else
  1029. internalerror(87);
  1030. end;
  1031. end;
  1032. function getconstrealvalue : bestreal;
  1033. begin
  1034. case left.nodetype of
  1035. ordconstn:
  1036. getconstrealvalue:=tordconstnode(left).value;
  1037. realconstn:
  1038. getconstrealvalue:=trealconstnode(left).value_real;
  1039. else
  1040. internalerror(309992);
  1041. end;
  1042. end;
  1043. procedure setconstrealvalue(r : bestreal);
  1044. begin
  1045. result:=crealconstnode.create(r,pbestrealtype^);
  1046. end;
  1047. function handle_ln_const(r : bestreal) : tnode;
  1048. begin
  1049. if r<=0.0 then
  1050. if (cs_check_range in aktlocalswitches) or
  1051. (cs_check_overflow in aktlocalswitches) then
  1052. begin
  1053. result:=crealconstnode.create(0,pbestrealtype^);
  1054. CGMessage(type_e_wrong_math_argument)
  1055. end
  1056. else
  1057. begin
  1058. if r=0.0 then
  1059. result:=crealconstnode.create(double(MathQNaN),pbestrealtype^)
  1060. else
  1061. result:=crealconstnode.create(double(MathNegInf),pbestrealtype^)
  1062. end
  1063. else
  1064. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1065. end;
  1066. function handle_sqrt_const(r : bestreal) : tnode;
  1067. begin
  1068. if r<0.0 then
  1069. if (cs_check_range in aktlocalswitches) or
  1070. (cs_check_overflow in aktlocalswitches) then
  1071. begin
  1072. result:=crealconstnode.create(0,pbestrealtype^);
  1073. CGMessage(type_e_wrong_math_argument)
  1074. end
  1075. else
  1076. result:=crealconstnode.create(double(MathQNaN),pbestrealtype^)
  1077. else
  1078. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1079. end;
  1080. procedure setfloatresulttype;
  1081. begin
  1082. if (left.resulttype.def.deftype=floatdef) and
  1083. (tfloatdef(left.resulttype.def).typ in [s32real,s64real,s80real,s128real]) then
  1084. resulttype:=left.resulttype
  1085. else
  1086. begin
  1087. inserttypeconv(left,pbestrealtype^);
  1088. resulttype:=pbestrealtype^;
  1089. end;
  1090. end;
  1091. var
  1092. vl,vl2 : TConstExprInt;
  1093. vr : bestreal;
  1094. hightree,
  1095. hp : tnode;
  1096. srsym : tsym;
  1097. checkrange : boolean;
  1098. label
  1099. myexit;
  1100. begin
  1101. result:=nil;
  1102. { if we handle writeln; left contains no valid address }
  1103. if assigned(left) then
  1104. begin
  1105. if left.nodetype=callparan then
  1106. tcallparanode(left).get_paratype
  1107. else
  1108. resulttypepass(left);
  1109. end;
  1110. inc(parsing_para_level);
  1111. { handle intern constant functions in separate case }
  1112. if nf_inlineconst in flags then
  1113. begin
  1114. { no parameters? }
  1115. if not assigned(left) then
  1116. internalerror(200501231)
  1117. else
  1118. begin
  1119. vl:=0;
  1120. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1121. case left.nodetype of
  1122. realconstn :
  1123. begin
  1124. { Real functions are all handled with internproc below }
  1125. CGMessage1(type_e_integer_expr_expected,left.resulttype.def.typename)
  1126. end;
  1127. ordconstn :
  1128. vl:=tordconstnode(left).value;
  1129. callparan :
  1130. begin
  1131. { both exists, else it was not generated }
  1132. vl:=tordconstnode(tcallparanode(left).left).value;
  1133. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1134. end;
  1135. else
  1136. CGMessage(parser_e_illegal_expression);
  1137. end;
  1138. case inlinenumber of
  1139. in_const_abs :
  1140. hp:=genintconstnode(abs(vl));
  1141. in_const_sqr :
  1142. hp:=genintconstnode(sqr(vl));
  1143. in_const_odd :
  1144. hp:=cordconstnode.create(byte(odd(vl)),booltype,true);
  1145. in_const_swap_word :
  1146. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resulttype,true);
  1147. in_const_swap_long :
  1148. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resulttype,true);
  1149. in_const_swap_qword :
  1150. hp:=cordconstnode.create((vl and $ffff) shl 32+(vl shr 32),left.resulttype,true);
  1151. in_const_ptr :
  1152. hp:=cpointerconstnode.create((vl2 shl 4)+vl,voidfarpointertype);
  1153. else
  1154. internalerror(88);
  1155. end;
  1156. end;
  1157. if hp=nil then
  1158. hp:=cerrornode.create;
  1159. result:=hp;
  1160. goto myexit;
  1161. end
  1162. else
  1163. begin
  1164. case inlinenumber of
  1165. in_lo_long,
  1166. in_hi_long,
  1167. in_lo_qword,
  1168. in_hi_qword,
  1169. in_lo_word,
  1170. in_hi_word :
  1171. begin
  1172. { give warning for incompatibility with tp and delphi }
  1173. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  1174. ((m_tp7 in aktmodeswitches) or
  1175. (m_delphi in aktmodeswitches)) then
  1176. CGMessage(type_w_maybe_wrong_hi_lo);
  1177. { constant folding }
  1178. if left.nodetype=ordconstn then
  1179. begin
  1180. case inlinenumber of
  1181. in_lo_word :
  1182. hp:=cordconstnode.create(tordconstnode(left).value and $ff,left.resulttype,true);
  1183. in_hi_word :
  1184. hp:=cordconstnode.create(tordconstnode(left).value shr 8,left.resulttype,true);
  1185. in_lo_long :
  1186. hp:=cordconstnode.create(tordconstnode(left).value and $ffff,left.resulttype,true);
  1187. in_hi_long :
  1188. hp:=cordconstnode.create(tordconstnode(left).value shr 16,left.resulttype,true);
  1189. in_lo_qword :
  1190. hp:=cordconstnode.create(tordconstnode(left).value and $ffffffff,left.resulttype,true);
  1191. in_hi_qword :
  1192. hp:=cordconstnode.create(tordconstnode(left).value shr 32,left.resulttype,true);
  1193. end;
  1194. result:=hp;
  1195. goto myexit;
  1196. end;
  1197. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1198. if not is_integer(left.resulttype.def) then
  1199. CGMessage1(type_e_integer_expr_expected,left.resulttype.def.typename);
  1200. case inlinenumber of
  1201. in_lo_word,
  1202. in_hi_word :
  1203. resulttype:=u8inttype;
  1204. in_lo_long,
  1205. in_hi_long :
  1206. resulttype:=u16inttype;
  1207. in_lo_qword,
  1208. in_hi_qword :
  1209. resulttype:=u32inttype;
  1210. end;
  1211. end;
  1212. in_sizeof_x:
  1213. begin
  1214. set_varstate(left,vs_read,[]);
  1215. if paramanager.push_high_param(vs_value,left.resulttype.def,current_procinfo.procdef.proccalloption) then
  1216. begin
  1217. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1218. if assigned(hightree) then
  1219. begin
  1220. hp:=caddnode.create(addn,hightree,
  1221. cordconstnode.create(1,sinttype,false));
  1222. if (left.resulttype.def.deftype=arraydef) and
  1223. (tarraydef(left.resulttype.def).elesize<>1) then
  1224. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1225. left.resulttype.def).elesize,sinttype,true));
  1226. result:=hp;
  1227. end;
  1228. end
  1229. else
  1230. resulttype:=sinttype;
  1231. end;
  1232. in_typeof_x:
  1233. begin
  1234. set_varstate(left,vs_read,[]);
  1235. resulttype:=voidpointertype;
  1236. end;
  1237. in_ord_x:
  1238. begin
  1239. if (left.nodetype=ordconstn) then
  1240. begin
  1241. hp:=cordconstnode.create(
  1242. tordconstnode(left).value,sinttype,true);
  1243. result:=hp;
  1244. goto myexit;
  1245. end;
  1246. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1247. case left.resulttype.def.deftype of
  1248. orddef :
  1249. begin
  1250. case torddef(left.resulttype.def).typ of
  1251. bool8bit,
  1252. uchar:
  1253. begin
  1254. { change to byte() }
  1255. hp:=ctypeconvnode.create_internal(left,u8inttype);
  1256. left:=nil;
  1257. result:=hp;
  1258. end;
  1259. bool16bit,
  1260. uwidechar :
  1261. begin
  1262. { change to word() }
  1263. hp:=ctypeconvnode.create_internal(left,u16inttype);
  1264. left:=nil;
  1265. result:=hp;
  1266. end;
  1267. bool32bit :
  1268. begin
  1269. { change to dword() }
  1270. hp:=ctypeconvnode.create_internal(left,u32inttype);
  1271. left:=nil;
  1272. result:=hp;
  1273. end;
  1274. uvoid :
  1275. CGMessage1(type_e_ordinal_expr_expected,left.resulttype.def.typename);
  1276. else
  1277. begin
  1278. { all other orddef need no transformation }
  1279. hp:=left;
  1280. left:=nil;
  1281. result:=hp;
  1282. end;
  1283. end;
  1284. end;
  1285. enumdef :
  1286. begin
  1287. hp:=ctypeconvnode.create_internal(left,s32inttype);
  1288. left:=nil;
  1289. result:=hp;
  1290. end;
  1291. pointerdef :
  1292. begin
  1293. if m_mac in aktmodeswitches then
  1294. begin
  1295. hp:=ctypeconvnode.create_internal(left,ptrinttype);
  1296. left:=nil;
  1297. result:=hp;
  1298. end
  1299. else
  1300. CGMessage1(type_e_ordinal_expr_expected,left.resulttype.def.typename);
  1301. end
  1302. else
  1303. CGMessage1(type_e_ordinal_expr_expected,left.resulttype.def.typename);
  1304. end;
  1305. end;
  1306. in_chr_byte:
  1307. begin
  1308. { convert to explicit char() }
  1309. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1310. hp:=ctypeconvnode.create_internal(left,cchartype);
  1311. left:=nil;
  1312. result:=hp;
  1313. end;
  1314. in_length_x:
  1315. begin
  1316. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1317. case left.resulttype.def.deftype of
  1318. variantdef:
  1319. begin
  1320. inserttypeconv(left,cansistringtype);
  1321. end;
  1322. stringdef :
  1323. begin
  1324. { we don't need string convertions here }
  1325. if (left.nodetype=typeconvn) and
  1326. (ttypeconvnode(left).left.resulttype.def.deftype=stringdef) then
  1327. begin
  1328. hp:=ttypeconvnode(left).left;
  1329. ttypeconvnode(left).left:=nil;
  1330. left.free;
  1331. left:=hp;
  1332. end;
  1333. { evaluates length of constant strings direct }
  1334. if (left.nodetype=stringconstn) then
  1335. begin
  1336. hp:=cordconstnode.create(
  1337. tstringconstnode(left).len,s32inttype,true);
  1338. result:=hp;
  1339. goto myexit;
  1340. end;
  1341. end;
  1342. orddef :
  1343. begin
  1344. { length of char is one allways }
  1345. if is_char(left.resulttype.def) or
  1346. is_widechar(left.resulttype.def) then
  1347. begin
  1348. hp:=cordconstnode.create(1,s32inttype,false);
  1349. result:=hp;
  1350. goto myexit;
  1351. end
  1352. else
  1353. CGMessage(type_e_mismatch);
  1354. end;
  1355. pointerdef :
  1356. begin
  1357. if is_pchar(left.resulttype.def) then
  1358. begin
  1359. hp := ccallparanode.create(left,nil);
  1360. result := ccallnode.createintern('fpc_pchar_length',hp);
  1361. { make sure the left node doesn't get disposed, since it's }
  1362. { reused in the new node (JM) }
  1363. left:=nil;
  1364. goto myexit;
  1365. end
  1366. else if is_pwidechar(left.resulttype.def) then
  1367. begin
  1368. hp := ccallparanode.create(left,nil);
  1369. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  1370. { make sure the left node doesn't get disposed, since it's }
  1371. { reused in the new node (JM) }
  1372. left:=nil;
  1373. goto myexit;
  1374. end
  1375. else
  1376. CGMessage(type_e_mismatch);
  1377. end;
  1378. arraydef :
  1379. begin
  1380. if is_open_array(left.resulttype.def) or
  1381. is_array_of_const(left.resulttype.def) then
  1382. begin
  1383. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1384. if assigned(hightree) then
  1385. begin
  1386. hp:=caddnode.create(addn,hightree,
  1387. cordconstnode.create(1,s32inttype,false));
  1388. result:=hp;
  1389. end;
  1390. goto myexit;
  1391. end
  1392. else
  1393. if not is_dynamic_array(left.resulttype.def) then
  1394. begin
  1395. hp:=cordconstnode.create(tarraydef(left.resulttype.def).highrange-
  1396. tarraydef(left.resulttype.def).lowrange+1,
  1397. s32inttype,true);
  1398. result:=hp;
  1399. goto myexit;
  1400. end
  1401. else
  1402. begin
  1403. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1404. result := ccallnode.createintern('fpc_dynarray_length',hp);
  1405. { make sure the left node doesn't get disposed, since it's }
  1406. { reused in the new node (JM) }
  1407. left:=nil;
  1408. goto myexit;
  1409. end;
  1410. end;
  1411. else
  1412. CGMessage(type_e_mismatch);
  1413. end;
  1414. { shortstring return an 8 bit value as the length
  1415. is the first byte of the string }
  1416. if is_shortstring(left.resulttype.def) then
  1417. resulttype:=u8inttype
  1418. else
  1419. resulttype:=sinttype;
  1420. end;
  1421. in_typeinfo_x:
  1422. begin
  1423. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1424. resulttype:=voidpointertype;
  1425. end;
  1426. in_assigned_x:
  1427. begin
  1428. { the parser has already made sure the expression is valid }
  1429. { handle constant expressions }
  1430. if is_constnode(tcallparanode(left).left) or
  1431. (tcallparanode(left).left.nodetype = pointerconstn) then
  1432. begin
  1433. { let an add node figure it out }
  1434. result := caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  1435. tcallparanode(left).left := nil;
  1436. { free left, because otherwise some code at 'myexit' tries }
  1437. { to run get_paratype for it, which crashes since left.left }
  1438. { is now nil }
  1439. left.free;
  1440. left := nil;
  1441. goto myexit;
  1442. end;
  1443. { otherwise handle separately, because there could be a procvar, which }
  1444. { is 2*sizeof(pointer), while we must only check the first pointer }
  1445. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  1446. resulttype:=booltype;
  1447. end;
  1448. in_ofs_x :
  1449. internalerror(2000101001);
  1450. in_seg_x :
  1451. begin
  1452. set_varstate(left,vs_read,[]);
  1453. result:=cordconstnode.create(0,s32inttype,false);
  1454. goto myexit;
  1455. end;
  1456. in_pred_x,
  1457. in_succ_x:
  1458. begin
  1459. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1460. resulttype:=left.resulttype;
  1461. if not is_ordinal(resulttype.def) then
  1462. CGMessage(type_e_ordinal_expr_expected)
  1463. else
  1464. begin
  1465. if (resulttype.def.deftype=enumdef) and
  1466. (tenumdef(resulttype.def).has_jumps) and
  1467. not(m_delphi in aktmodeswitches) then
  1468. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  1469. end;
  1470. { only if the result is an enum do we do range checking }
  1471. if (resulttype.def.deftype=enumdef) then
  1472. checkrange := true
  1473. else
  1474. checkrange := false;
  1475. { do constant folding after check for jumps }
  1476. if left.nodetype=ordconstn then
  1477. begin
  1478. if inlinenumber=in_succ_x then
  1479. result:=cordconstnode.create(tordconstnode(left).value+1,left.resulttype,checkrange)
  1480. else
  1481. result:=cordconstnode.create(tordconstnode(left).value-1,left.resulttype,checkrange);
  1482. end;
  1483. end;
  1484. in_initialize_x,
  1485. in_finalize_x,
  1486. in_setlength_x:
  1487. begin
  1488. { inlined from pinline }
  1489. internalerror(200204231);
  1490. end;
  1491. in_inc_x,
  1492. in_dec_x:
  1493. begin
  1494. resulttype:=voidtype;
  1495. if assigned(left) then
  1496. begin
  1497. { first param must be var }
  1498. valid_for_var(tcallparanode(left).left,true);
  1499. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1500. if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  1501. is_ordinal(left.resulttype.def) or
  1502. is_currency(left.resulttype.def) then
  1503. begin
  1504. { value of left gets changed -> must be unique }
  1505. set_unique(tcallparanode(left).left);
  1506. { two paras ? }
  1507. if assigned(tcallparanode(left).right) then
  1508. begin
  1509. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1510. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resulttype);
  1511. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1512. CGMessage(parser_e_illegal_expression);
  1513. end;
  1514. end
  1515. else
  1516. CGMessage(type_e_ordinal_expr_expected);
  1517. end
  1518. else
  1519. CGMessage(type_e_mismatch);
  1520. end;
  1521. in_read_x,
  1522. in_readln_x,
  1523. in_write_x,
  1524. in_writeln_x :
  1525. begin
  1526. result := handle_read_write;
  1527. end;
  1528. in_settextbuf_file_x :
  1529. begin
  1530. resulttype:=voidtype;
  1531. { now we know the type of buffer }
  1532. srsym:=searchsymonlyin(systemunit,'SETTEXTBUF');
  1533. hp:=ccallparanode.create(cordconstnode.create(
  1534. tcallparanode(left).left.resulttype.def.size,s32inttype,true),left);
  1535. result:=ccallnode.create(hp,tprocsym(srsym),systemunit,nil,[]);
  1536. left:=nil;
  1537. end;
  1538. { the firstpass of the arg has been done in firstcalln ? }
  1539. in_reset_typedfile,
  1540. in_rewrite_typedfile :
  1541. begin
  1542. result := handle_reset_rewrite_typed;
  1543. end;
  1544. in_str_x_string :
  1545. begin
  1546. result := handle_str;
  1547. end;
  1548. in_val_x :
  1549. begin
  1550. result := handle_val;
  1551. end;
  1552. in_include_x_y,
  1553. in_exclude_x_y:
  1554. begin
  1555. resulttype:=voidtype;
  1556. { the parser already checks whether we have two (and exectly two) }
  1557. { parameters (JM) }
  1558. { first param must be var }
  1559. valid_for_var(tcallparanode(left).left,true);
  1560. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1561. { check type }
  1562. if (left.resulttype.def.deftype=setdef) then
  1563. begin
  1564. { insert a type conversion }
  1565. { to the type of the set elements }
  1566. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1567. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  1568. tsetdef(left.resulttype.def).elementtype);
  1569. end
  1570. else
  1571. CGMessage(type_e_mismatch);
  1572. end;
  1573. in_slice_x:
  1574. begin
  1575. result:=nil;
  1576. resulttype:=tcallparanode(tcallparanode(left).left).resulttype;
  1577. if not(resulttype.def.deftype=arraydef) then
  1578. CGMessage(type_e_mismatch);
  1579. end;
  1580. in_low_x,
  1581. in_high_x:
  1582. begin
  1583. case left.resulttype.def.deftype of
  1584. orddef,
  1585. enumdef:
  1586. begin
  1587. result:=do_lowhigh(left.resulttype);
  1588. end;
  1589. setdef:
  1590. begin
  1591. result:=do_lowhigh(tsetdef(left.resulttype.def).elementtype);
  1592. end;
  1593. arraydef:
  1594. begin
  1595. if inlinenumber=in_low_x then
  1596. begin
  1597. result:=cordconstnode.create(tarraydef(
  1598. left.resulttype.def).lowrange,tarraydef(left.resulttype.def).rangetype,true);
  1599. end
  1600. else
  1601. begin
  1602. if is_open_array(left.resulttype.def) or
  1603. is_array_of_const(left.resulttype.def) then
  1604. begin
  1605. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1606. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1607. end
  1608. else
  1609. if is_dynamic_array(left.resulttype.def) then
  1610. begin
  1611. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1612. { can't use inserttypeconv because we need }
  1613. { an explicit type conversion (JM) }
  1614. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1615. result := ccallnode.createintern('fpc_dynarray_high',hp);
  1616. { make sure the left node doesn't get disposed, since it's }
  1617. { reused in the new node (JM) }
  1618. left:=nil;
  1619. end
  1620. else
  1621. begin
  1622. result:=cordconstnode.create(tarraydef(
  1623. left.resulttype.def).highrange,tarraydef(left.resulttype.def).rangetype,true);
  1624. end;
  1625. end;
  1626. end;
  1627. stringdef:
  1628. begin
  1629. if inlinenumber=in_low_x then
  1630. begin
  1631. result:=cordconstnode.create(0,u8inttype,false);
  1632. end
  1633. else
  1634. begin
  1635. if is_open_string(left.resulttype.def) then
  1636. begin
  1637. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1638. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  1639. end
  1640. else
  1641. result:=cordconstnode.create(tstringdef(left.resulttype.def).len,u8inttype,true);
  1642. end;
  1643. end;
  1644. else
  1645. CGMessage(type_e_mismatch);
  1646. end;
  1647. end;
  1648. in_exp_real :
  1649. begin
  1650. if left.nodetype in [ordconstn,realconstn] then
  1651. begin
  1652. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  1653. if (trealconstnode(result).value_real=double(MathInf)) and
  1654. ((cs_check_range in aktlocalswitches) or
  1655. (cs_check_overflow in aktlocalswitches)) then
  1656. begin
  1657. result:=crealconstnode.create(0,pbestrealtype^);
  1658. CGMessage(parser_e_range_check_error);
  1659. end;
  1660. end
  1661. else
  1662. begin
  1663. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1664. inserttypeconv(left,pbestrealtype^);
  1665. resulttype:=pbestrealtype^;
  1666. end;
  1667. end;
  1668. in_trunc_real :
  1669. begin
  1670. if left.nodetype in [ordconstn,realconstn] then
  1671. begin
  1672. vr:=getconstrealvalue;
  1673. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1674. begin
  1675. CGMessage(parser_e_range_check_error);
  1676. result:=cordconstnode.create(1,s64inttype,false)
  1677. end
  1678. else
  1679. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  1680. end
  1681. else
  1682. begin
  1683. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1684. inserttypeconv(left,pbestrealtype^);
  1685. resulttype:=s64inttype;
  1686. end;
  1687. end;
  1688. in_round_real :
  1689. begin
  1690. if left.nodetype in [ordconstn,realconstn] then
  1691. begin
  1692. vr:=getconstrealvalue;
  1693. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1694. begin
  1695. CGMessage(parser_e_range_check_error);
  1696. result:=cordconstnode.create(1,s64inttype,false)
  1697. end
  1698. else
  1699. result:=cordconstnode.create(round(vr),s64inttype,true)
  1700. end
  1701. else
  1702. begin
  1703. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1704. inserttypeconv(left,pbestrealtype^);
  1705. resulttype:=s64inttype;
  1706. end;
  1707. end;
  1708. in_frac_real :
  1709. begin
  1710. if left.nodetype in [ordconstn,realconstn] then
  1711. setconstrealvalue(frac(getconstrealvalue))
  1712. else
  1713. begin
  1714. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1715. inserttypeconv(left,pbestrealtype^);
  1716. resulttype:=pbestrealtype^;
  1717. end;
  1718. end;
  1719. in_int_real :
  1720. begin
  1721. if left.nodetype in [ordconstn,realconstn] then
  1722. setconstrealvalue(int(getconstrealvalue))
  1723. else
  1724. begin
  1725. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1726. inserttypeconv(left,pbestrealtype^);
  1727. resulttype:=pbestrealtype^;
  1728. end;
  1729. end;
  1730. in_pi_real :
  1731. begin
  1732. if block_type=bt_const then
  1733. setconstrealvalue(getpi)
  1734. else
  1735. resulttype:=pbestrealtype^;
  1736. end;
  1737. in_cos_real :
  1738. begin
  1739. if left.nodetype in [ordconstn,realconstn] then
  1740. setconstrealvalue(cos(getconstrealvalue))
  1741. else
  1742. begin
  1743. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1744. inserttypeconv(left,pbestrealtype^);
  1745. resulttype:=pbestrealtype^;
  1746. end;
  1747. end;
  1748. in_sin_real :
  1749. begin
  1750. if left.nodetype in [ordconstn,realconstn] then
  1751. setconstrealvalue(sin(getconstrealvalue))
  1752. else
  1753. begin
  1754. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1755. inserttypeconv(left,pbestrealtype^);
  1756. resulttype:=pbestrealtype^;
  1757. end;
  1758. end;
  1759. in_arctan_real :
  1760. begin
  1761. if left.nodetype in [ordconstn,realconstn] then
  1762. setconstrealvalue(arctan(getconstrealvalue))
  1763. else
  1764. begin
  1765. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1766. inserttypeconv(left,pbestrealtype^);
  1767. resulttype:=pbestrealtype^;
  1768. end;
  1769. end;
  1770. in_abs_real :
  1771. begin
  1772. if left.nodetype in [ordconstn,realconstn] then
  1773. setconstrealvalue(abs(getconstrealvalue))
  1774. else
  1775. begin
  1776. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1777. inserttypeconv(left,pbestrealtype^);
  1778. resulttype:=pbestrealtype^;
  1779. end;
  1780. end;
  1781. in_sqr_real :
  1782. begin
  1783. if left.nodetype in [ordconstn,realconstn] then
  1784. setconstrealvalue(sqr(getconstrealvalue))
  1785. else
  1786. begin
  1787. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1788. setfloatresulttype;
  1789. end;
  1790. end;
  1791. in_sqrt_real :
  1792. begin
  1793. if left.nodetype in [ordconstn,realconstn] then
  1794. begin
  1795. vr:=getconstrealvalue;
  1796. if vr<0.0 then
  1797. result:=handle_sqrt_const(vr)
  1798. else
  1799. setconstrealvalue(sqrt(vr));
  1800. end
  1801. else
  1802. begin
  1803. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1804. setfloatresulttype;
  1805. end;
  1806. end;
  1807. in_ln_real :
  1808. begin
  1809. if left.nodetype in [ordconstn,realconstn] then
  1810. begin
  1811. vr:=getconstrealvalue;
  1812. if vr<=0.0 then
  1813. result:=handle_ln_const(vr)
  1814. else
  1815. setconstrealvalue(ln(vr));
  1816. end
  1817. else
  1818. begin
  1819. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1820. inserttypeconv(left,pbestrealtype^);
  1821. resulttype:=pbestrealtype^;
  1822. end;
  1823. end;
  1824. {$ifdef SUPPORT_MMX}
  1825. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  1826. begin
  1827. end;
  1828. {$endif SUPPORT_MMX}
  1829. in_prefetch_var:
  1830. begin
  1831. resulttype:=voidtype;
  1832. end;
  1833. in_assert_x_y :
  1834. begin
  1835. resulttype:=voidtype;
  1836. if assigned(left) then
  1837. begin
  1838. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  1839. { check type }
  1840. if is_boolean(left.resulttype.def) then
  1841. begin
  1842. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1843. { must always be a string }
  1844. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  1845. end
  1846. else
  1847. CGMessage1(type_e_boolean_expr_expected,left.resulttype.def.typename);
  1848. end
  1849. else
  1850. CGMessage(type_e_mismatch);
  1851. { We've checked the whole statement for correctness, now we
  1852. can remove it if assertions are off }
  1853. if not(cs_do_assertion in aktlocalswitches) then
  1854. begin
  1855. { we need a valid node, so insert a nothingn }
  1856. result:=cnothingnode.create;
  1857. end
  1858. else
  1859. include(current_procinfo.flags,pi_do_call);
  1860. end;
  1861. else
  1862. internalerror(8);
  1863. end;
  1864. end;
  1865. myexit:
  1866. { Run get_paratype again to update maybe inserted typeconvs }
  1867. if not codegenerror then
  1868. begin
  1869. if assigned(left) and
  1870. (left.nodetype=callparan) then
  1871. tcallparanode(left).get_paratype;
  1872. end;
  1873. dec(parsing_para_level);
  1874. end;
  1875. function tinlinenode.pass_1 : tnode;
  1876. var
  1877. hp,hpp : tnode;
  1878. shiftconst: longint;
  1879. tempnode: ttempcreatenode;
  1880. newstatement: tstatementnode;
  1881. newblock: tblocknode;
  1882. begin
  1883. result:=nil;
  1884. { if we handle writeln; left contains no valid address }
  1885. if assigned(left) then
  1886. begin
  1887. if left.nodetype=callparan then
  1888. tcallparanode(left).firstcallparan
  1889. else
  1890. firstpass(left);
  1891. left_max;
  1892. end;
  1893. inc(parsing_para_level);
  1894. { intern const should already be handled }
  1895. if nf_inlineconst in flags then
  1896. internalerror(200104044);
  1897. case inlinenumber of
  1898. in_lo_qword,
  1899. in_hi_qword,
  1900. in_lo_long,
  1901. in_hi_long,
  1902. in_lo_word,
  1903. in_hi_word:
  1904. begin
  1905. shiftconst := 0;
  1906. case inlinenumber of
  1907. in_hi_qword:
  1908. shiftconst := 32;
  1909. in_hi_long:
  1910. shiftconst := 16;
  1911. in_hi_word:
  1912. shiftconst := 8;
  1913. end;
  1914. if shiftconst <> 0 then
  1915. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  1916. cordconstnode.create(shiftconst,u32inttype,false)),resulttype)
  1917. else
  1918. result := ctypeconvnode.create_internal(left,resulttype);
  1919. left := nil;
  1920. firstpass(result);
  1921. end;
  1922. in_sizeof_x:
  1923. begin
  1924. if registersint<1 then
  1925. registersint:=1;
  1926. expectloc:=LOC_REGISTER;
  1927. end;
  1928. in_typeof_x:
  1929. begin
  1930. if registersint<1 then
  1931. registersint:=1;
  1932. expectloc:=LOC_REGISTER;
  1933. end;
  1934. in_length_x:
  1935. begin
  1936. if is_shortstring(left.resulttype.def) then
  1937. expectloc:=left.expectloc
  1938. else
  1939. begin
  1940. { ansi/wide string }
  1941. if registersint<1 then
  1942. registersint:=1;
  1943. expectloc:=LOC_REGISTER;
  1944. end;
  1945. end;
  1946. in_typeinfo_x:
  1947. begin
  1948. expectloc:=LOC_REGISTER;
  1949. registersint:=1;
  1950. end;
  1951. in_assigned_x:
  1952. begin
  1953. expectloc := LOC_JUMP;
  1954. registersint:=1;
  1955. end;
  1956. in_pred_x,
  1957. in_succ_x:
  1958. begin
  1959. if is_64bit(resulttype.def) then
  1960. begin
  1961. if (registersint<2) then
  1962. registersint:=2
  1963. end
  1964. else
  1965. begin
  1966. if (registersint<1) then
  1967. registersint:=1;
  1968. end;
  1969. expectloc:=LOC_REGISTER;
  1970. end;
  1971. in_setlength_x,
  1972. in_initialize_x,
  1973. in_finalize_x:
  1974. begin
  1975. expectloc:=LOC_VOID;
  1976. end;
  1977. in_inc_x,
  1978. in_dec_x:
  1979. begin
  1980. expectloc:=LOC_VOID;
  1981. { check type }
  1982. if
  1983. {$ifndef cpu64bit}
  1984. is_64bit(left.resulttype.def) or
  1985. {$endif cpu64bit}
  1986. { range/overflow checking doesn't work properly }
  1987. { with the inc/dec code that's generated (JM) }
  1988. (
  1989. (((left.resulttype.def.deftype = orddef) and
  1990. not(is_char(left.resulttype.def)) and
  1991. not(is_boolean(left.resulttype.def))) or
  1992. (left.resulttype.def.deftype = pointerdef)) and
  1993. (aktlocalswitches * [cs_check_overflow,cs_check_range] <> [])
  1994. ) then
  1995. { convert to simple add (JM) }
  1996. begin
  1997. newblock := internalstatements(newstatement);
  1998. { extra parameter? }
  1999. if assigned(tcallparanode(left).right) then
  2000. begin
  2001. { Yes, use for add node }
  2002. hpp := tcallparanode(tcallparanode(left).right).left;
  2003. tcallparanode(tcallparanode(left).right).left := nil;
  2004. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2005. CGMessage(parser_e_illegal_expression);
  2006. end
  2007. else
  2008. begin
  2009. { no, create constant 1 }
  2010. hpp := cordconstnode.create(1,tcallparanode(left).left.resulttype,false);
  2011. end;
  2012. resulttypepass(hpp);
  2013. {$ifndef cpu64bit}
  2014. if not((hpp.resulttype.def.deftype=orddef) and
  2015. (torddef(hpp.resulttype.def).typ<>u32bit)) then
  2016. {$endif cpu64bit}
  2017. inserttypeconv_internal(hpp,sinttype);
  2018. { No overflow check for pointer operations, because inc(pointer,-1) will always
  2019. trigger an overflow. For uint32 it works because then the operation is done
  2020. in 64bit }
  2021. if (tcallparanode(left).left.resulttype.def.deftype=pointerdef) then
  2022. exclude(aktlocalswitches,cs_check_overflow);
  2023. { make sure we don't call functions part of the left node twice (and generally }
  2024. { optimize the code generation) }
  2025. if node_complexity(tcallparanode(left).left) > 1 then
  2026. begin
  2027. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.def.size,tt_persistent,true);
  2028. addstatement(newstatement,tempnode);
  2029. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2030. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  2031. hp := cderefnode.create(ctemprefnode.create(tempnode));
  2032. inserttypeconv_internal(hp,tcallparanode(left).left.resulttype);
  2033. end
  2034. else
  2035. begin
  2036. hp := tcallparanode(left).left.getcopy;
  2037. tempnode := nil;
  2038. end;
  2039. { addition/substraction depending on inc/dec }
  2040. if inlinenumber = in_inc_x then
  2041. hpp := caddnode.create(addn,hp,hpp)
  2042. else
  2043. hpp := caddnode.create(subn,hp,hpp);
  2044. { assign result of addition }
  2045. inserttypeconv_internal(hpp,hp.resulttype);
  2046. addstatement(newstatement,cassignmentnode.create(hp.getcopy,hpp));
  2047. { deallocate the temp }
  2048. if assigned(tempnode) then
  2049. addstatement(newstatement,ctempdeletenode.create(tempnode));
  2050. { firstpass it }
  2051. firstpass(newblock);
  2052. { return new node }
  2053. result := newblock;
  2054. end
  2055. else if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  2056. is_ordinal(left.resulttype.def) then
  2057. begin
  2058. { two paras ? }
  2059. if assigned(tcallparanode(left).right) then
  2060. begin
  2061. { need we an additional register ? }
  2062. if not(is_constintnode(tcallparanode(tcallparanode(left).right).left)) and
  2063. (tcallparanode(tcallparanode(left).right).left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) and
  2064. (tcallparanode(tcallparanode(left).right).left.registersint<=1) then
  2065. inc(registersint);
  2066. { do we need an additional register to restore the first parameter? }
  2067. if tcallparanode(tcallparanode(left).right).left.registersint>=registersint then
  2068. inc(registersint);
  2069. end;
  2070. end;
  2071. end;
  2072. in_include_x_y,
  2073. in_exclude_x_y:
  2074. begin
  2075. expectloc:=LOC_VOID;
  2076. registersint:=left.registersint;
  2077. registersfpu:=left.registersfpu;
  2078. {$ifdef SUPPORT_MMX}
  2079. registersmmx:=left.registersmmx;
  2080. {$endif SUPPORT_MMX}
  2081. end;
  2082. in_exp_real:
  2083. begin
  2084. result:= first_exp_real;
  2085. end;
  2086. in_round_real:
  2087. begin
  2088. result:= first_round_real;
  2089. end;
  2090. in_trunc_real:
  2091. begin
  2092. result:= first_trunc_real;
  2093. end;
  2094. in_int_real:
  2095. begin
  2096. result:= first_int_real;
  2097. end;
  2098. in_frac_real:
  2099. begin
  2100. result:= first_frac_real;
  2101. end;
  2102. in_cos_real:
  2103. begin
  2104. result:= first_cos_real;
  2105. end;
  2106. in_sin_real:
  2107. begin
  2108. result := first_sin_real;
  2109. end;
  2110. in_arctan_real:
  2111. begin
  2112. result := first_arctan_real;
  2113. end;
  2114. in_pi_real :
  2115. begin
  2116. result := first_pi;
  2117. end;
  2118. in_abs_real:
  2119. begin
  2120. result := first_abs_real;
  2121. end;
  2122. in_sqr_real:
  2123. begin
  2124. result := first_sqr_real;
  2125. end;
  2126. in_sqrt_real:
  2127. begin
  2128. result := first_sqrt_real;
  2129. end;
  2130. in_ln_real:
  2131. begin
  2132. result := first_ln_real;
  2133. end;
  2134. {$ifdef SUPPORT_MMX}
  2135. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2136. begin
  2137. end;
  2138. {$endif SUPPORT_MMX}
  2139. in_assert_x_y :
  2140. begin
  2141. expectloc:=LOC_VOID;
  2142. registersint:=left.registersint;
  2143. registersfpu:=left.registersfpu;
  2144. {$ifdef SUPPORT_MMX}
  2145. registersmmx:=left.registersmmx;
  2146. {$endif SUPPORT_MMX}
  2147. end;
  2148. in_low_x,
  2149. in_high_x:
  2150. internalerror(200104047);
  2151. in_slice_x:
  2152. internalerror(2005101501);
  2153. in_ord_x,
  2154. in_chr_byte:
  2155. begin
  2156. { should not happend as it's converted to typeconv }
  2157. internalerror(200104045);
  2158. end;
  2159. in_ofs_x :
  2160. internalerror(2000101001);
  2161. in_seg_x :
  2162. internalerror(200104046);
  2163. in_settextbuf_file_x,
  2164. in_reset_typedfile,
  2165. in_rewrite_typedfile,
  2166. in_str_x_string,
  2167. in_val_x,
  2168. in_read_x,
  2169. in_readln_x,
  2170. in_write_x,
  2171. in_writeln_x :
  2172. begin
  2173. { should be handled by det_resulttype }
  2174. internalerror(200108234);
  2175. end;
  2176. in_prefetch_var:
  2177. begin
  2178. expectloc:=LOC_VOID;
  2179. end;
  2180. else
  2181. internalerror(8);
  2182. end;
  2183. dec(parsing_para_level);
  2184. end;
  2185. {$ifdef fpc}
  2186. {$maxfpuregisters default}
  2187. {$endif fpc}
  2188. function tinlinenode.docompare(p: tnode): boolean;
  2189. begin
  2190. docompare :=
  2191. inherited docompare(p) and
  2192. (inlinenumber = tinlinenode(p).inlinenumber);
  2193. end;
  2194. function tinlinenode.first_pi : tnode;
  2195. begin
  2196. result:=crealconstnode.create(getpi,pbestrealtype^);
  2197. end;
  2198. function tinlinenode.first_arctan_real : tnode;
  2199. begin
  2200. { create the call to the helper }
  2201. { on entry left node contains the parameter }
  2202. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  2203. ccallparanode.create(left,nil));
  2204. left := nil;
  2205. end;
  2206. function tinlinenode.first_abs_real : tnode;
  2207. begin
  2208. { create the call to the helper }
  2209. { on entry left node contains the parameter }
  2210. first_abs_real := ccallnode.createintern('fpc_abs_real',
  2211. ccallparanode.create(left,nil));
  2212. left := nil;
  2213. end;
  2214. function tinlinenode.first_sqr_real : tnode;
  2215. begin
  2216. { create the call to the helper }
  2217. { on entry left node contains the parameter }
  2218. first_sqr_real := ctypeconvnode.create_internal(ccallnode.createintern('fpc_sqr_real',
  2219. ccallparanode.create(left,nil)),resulttype);
  2220. left := nil;
  2221. end;
  2222. function tinlinenode.first_sqrt_real : tnode;
  2223. begin
  2224. { create the call to the helper }
  2225. { on entry left node contains the parameter }
  2226. first_sqrt_real := ctypeconvnode.create_internal(ccallnode.createintern('fpc_sqrt_real',
  2227. ccallparanode.create(left,nil)),resulttype);
  2228. left := nil;
  2229. end;
  2230. function tinlinenode.first_ln_real : tnode;
  2231. begin
  2232. { create the call to the helper }
  2233. { on entry left node contains the parameter }
  2234. first_ln_real := ccallnode.createintern('fpc_ln_real',
  2235. ccallparanode.create(left,nil));
  2236. left := nil;
  2237. end;
  2238. function tinlinenode.first_cos_real : tnode;
  2239. begin
  2240. { create the call to the helper }
  2241. { on entry left node contains the parameter }
  2242. first_cos_real := ccallnode.createintern('fpc_cos_real',
  2243. ccallparanode.create(left,nil));
  2244. left := nil;
  2245. end;
  2246. function tinlinenode.first_sin_real : tnode;
  2247. begin
  2248. { create the call to the helper }
  2249. { on entry left node contains the parameter }
  2250. first_sin_real := ccallnode.createintern('fpc_sin_real',
  2251. ccallparanode.create(left,nil));
  2252. left := nil;
  2253. end;
  2254. function tinlinenode.first_exp_real : tnode;
  2255. begin
  2256. { create the call to the helper }
  2257. { on entry left node contains the parameter }
  2258. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  2259. left := nil;
  2260. end;
  2261. function tinlinenode.first_int_real : tnode;
  2262. begin
  2263. { create the call to the helper }
  2264. { on entry left node contains the parameter }
  2265. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  2266. left := nil;
  2267. end;
  2268. function tinlinenode.first_frac_real : tnode;
  2269. begin
  2270. { create the call to the helper }
  2271. { on entry left node contains the parameter }
  2272. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  2273. left := nil;
  2274. end;
  2275. function tinlinenode.first_round_real : tnode;
  2276. begin
  2277. { create the call to the helper }
  2278. { on entry left node contains the parameter }
  2279. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  2280. left := nil;
  2281. end;
  2282. function tinlinenode.first_trunc_real : tnode;
  2283. begin
  2284. { create the call to the helper }
  2285. { on entry left node contains the parameter }
  2286. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  2287. left := nil;
  2288. end;
  2289. begin
  2290. cinlinenode:=tinlinenode;
  2291. end.