ninl.pas 98 KB

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