ninl.pas 90 KB

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