ninl.pas 94 KB

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