ninl.pas 100 KB

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