ninl.pas 95 KB

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