ninl.pas 97 KB

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