ninl.pas 96 KB

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