ninl.pas 99 KB

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