ninl.pas 97 KB

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