ninl.pas 94 KB

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