ninl.pas 96 KB

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