ninl.pas 95 KB

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