ninl.pas 93 KB

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