ninl.pas 95 KB

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