ninl.pas 92 KB

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