ninl.pas 95 KB

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