ninl.pas 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541
  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. ctypeconvnode.create_internal(
  949. ctemprefnode.create(tempcode),orgcode.resulttype)));
  950. { release the temp if we allocated one }
  951. if assigned(tempcode) then
  952. addstatement(newstatement,ctempdeletenode.create(tempcode));
  953. { free the errornode }
  954. result.free;
  955. { and return it }
  956. result := newblock;
  957. end;
  958. {$ifdef fpc}
  959. {$maxfpuregisters 0}
  960. {$endif fpc}
  961. function getpi : bestreal;
  962. begin
  963. {$ifdef x86}
  964. { x86 has pi in hardware }
  965. result:=pi;
  966. {$else x86}
  967. {$ifdef cpuextended}
  968. result:=MathPiExtended.Value;
  969. {$else cpuextended}
  970. result:=MathPi.Value;
  971. {$endif cpuextended}
  972. {$endif x86}
  973. end;
  974. function tinlinenode.det_resulttype:tnode;
  975. function do_lowhigh(const t:ttype) : tnode;
  976. var
  977. v : tconstexprint;
  978. enum : tenumsym;
  979. hp : tnode;
  980. begin
  981. case t.def.deftype of
  982. orddef:
  983. begin
  984. if inlinenumber=in_low_x then
  985. v:=torddef(t.def).low
  986. else
  987. v:=torddef(t.def).high;
  988. { low/high of torddef are longints, so we need special }
  989. { handling for cardinal and 64bit types (JM) }
  990. { 1.0.x doesn't support int64($ffffffff) correct, it'll expand
  991. to -1 instead of staying $ffffffff. Therefor we use $ffff with
  992. shl twice (PFV) }
  993. case torddef(t.def).typ of
  994. s64bit,scurrency :
  995. begin
  996. if (inlinenumber=in_low_x) then
  997. v := int64($80000000) shl 32
  998. else
  999. v := (int64($7fffffff) shl 32) or int64($ffff) shl 16 or int64($ffff)
  1000. end;
  1001. u64bit :
  1002. begin
  1003. { we have to use a dirty trick for high(qword), }
  1004. { because it's bigger than high(tconstexprint) (JM) }
  1005. v := 0
  1006. end
  1007. else
  1008. begin
  1009. if not is_signed(t.def) then
  1010. v := cardinal(v);
  1011. end;
  1012. end;
  1013. hp:=cordconstnode.create(v,t,true);
  1014. resulttypepass(hp);
  1015. { fix high(qword) }
  1016. if (torddef(t.def).typ=u64bit) and
  1017. (inlinenumber = in_high_x) then
  1018. tordconstnode(hp).value := -1; { is the same as qword($ffffffffffffffff) }
  1019. do_lowhigh:=hp;
  1020. end;
  1021. enumdef:
  1022. begin
  1023. enum:=tenumsym(tenumdef(t.def).firstenum);
  1024. v:=tenumdef(t.def).maxval;
  1025. if inlinenumber=in_high_x then
  1026. while assigned(enum) and (enum.value <> v) do
  1027. enum:=enum.nextenum;
  1028. if not assigned(enum) then
  1029. internalerror(309993)
  1030. else
  1031. hp:=genenumnode(enum);
  1032. do_lowhigh:=hp;
  1033. end;
  1034. else
  1035. internalerror(87);
  1036. end;
  1037. end;
  1038. function getconstrealvalue : bestreal;
  1039. begin
  1040. case left.nodetype of
  1041. ordconstn:
  1042. getconstrealvalue:=tordconstnode(left).value;
  1043. realconstn:
  1044. getconstrealvalue:=trealconstnode(left).value_real;
  1045. else
  1046. internalerror(309992);
  1047. end;
  1048. end;
  1049. procedure setconstrealvalue(r : bestreal);
  1050. begin
  1051. result:=crealconstnode.create(r,pbestrealtype^);
  1052. end;
  1053. function handle_ln_const(r : bestreal) : tnode;
  1054. begin
  1055. if r<=0.0 then
  1056. if (cs_check_range in aktlocalswitches) or
  1057. (cs_check_overflow in aktlocalswitches) then
  1058. begin
  1059. result:=crealconstnode.create(0,pbestrealtype^);
  1060. CGMessage(type_e_wrong_math_argument)
  1061. end
  1062. else
  1063. begin
  1064. if r=0.0 then
  1065. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1066. else
  1067. result:=crealconstnode.create(MathNegInf.Value,pbestrealtype^)
  1068. end
  1069. else
  1070. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1071. end;
  1072. function handle_sqrt_const(r : bestreal) : tnode;
  1073. begin
  1074. if r<0.0 then
  1075. if (cs_check_range in aktlocalswitches) or
  1076. (cs_check_overflow in aktlocalswitches) then
  1077. begin
  1078. result:=crealconstnode.create(0,pbestrealtype^);
  1079. CGMessage(type_e_wrong_math_argument)
  1080. end
  1081. else
  1082. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1083. else
  1084. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1085. end;
  1086. procedure setfloatresulttype;
  1087. begin
  1088. if (left.resulttype.def.deftype=floatdef) and
  1089. (tfloatdef(left.resulttype.def).typ in [s32real,s64real,s80real,s128real]) then
  1090. resulttype:=left.resulttype
  1091. else
  1092. begin
  1093. inserttypeconv(left,pbestrealtype^);
  1094. resulttype:=pbestrealtype^;
  1095. end;
  1096. end;
  1097. var
  1098. vl,vl2 : TConstExprInt;
  1099. vr : bestreal;
  1100. hightree,
  1101. hp : tnode;
  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. if is_integer(tcallparanode(left).right.resulttype.def) then
  1515. begin
  1516. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1517. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resulttype);
  1518. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1519. { should be handled in the parser (JM) }
  1520. internalerror(2006020901);
  1521. end
  1522. else
  1523. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  1524. end;
  1525. end
  1526. else
  1527. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  1528. end
  1529. else
  1530. CGMessagePos(fileinfo,type_e_mismatch);
  1531. end;
  1532. in_read_x,
  1533. in_readln_x,
  1534. in_write_x,
  1535. in_writeln_x :
  1536. begin
  1537. result := handle_read_write;
  1538. end;
  1539. in_settextbuf_file_x :
  1540. begin
  1541. resulttype:=voidtype;
  1542. { now we know the type of buffer }
  1543. hp:=ccallparanode.create(cordconstnode.create(
  1544. tcallparanode(left).left.resulttype.def.size,s32inttype,true),left);
  1545. result:=ccallnode.createintern('SETTEXTBUF',hp);
  1546. left:=nil;
  1547. end;
  1548. { the firstpass of the arg has been done in firstcalln ? }
  1549. in_reset_typedfile,
  1550. in_rewrite_typedfile :
  1551. begin
  1552. result := handle_reset_rewrite_typed;
  1553. end;
  1554. in_str_x_string :
  1555. begin
  1556. result := handle_str;
  1557. end;
  1558. in_val_x :
  1559. begin
  1560. result := handle_val;
  1561. end;
  1562. in_include_x_y,
  1563. in_exclude_x_y:
  1564. begin
  1565. resulttype:=voidtype;
  1566. { the parser already checks whether we have two (and exectly two) }
  1567. { parameters (JM) }
  1568. { first param must be var }
  1569. valid_for_var(tcallparanode(left).left,true);
  1570. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1571. { check type }
  1572. if (left.resulttype.def.deftype=setdef) then
  1573. begin
  1574. { insert a type conversion }
  1575. { to the type of the set elements }
  1576. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1577. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  1578. tsetdef(left.resulttype.def).elementtype);
  1579. end
  1580. else
  1581. CGMessage(type_e_mismatch);
  1582. end;
  1583. in_slice_x:
  1584. begin
  1585. result:=nil;
  1586. resulttype:=tcallparanode(left).left.resulttype;
  1587. if (resulttype.def.deftype <> arraydef) then
  1588. CGMessagePos(left.fileinfo,type_e_mismatch);
  1589. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resulttype.def)) then
  1590. CGMessagePos1(tcallparanode(left).right.fileinfo,
  1591. type_e_integer_expr_expected,
  1592. tcallparanode(tcallparanode(left).right).left.resulttype.def.typename);
  1593. end;
  1594. in_low_x,
  1595. in_high_x:
  1596. begin
  1597. case left.resulttype.def.deftype of
  1598. orddef,
  1599. enumdef:
  1600. begin
  1601. result:=do_lowhigh(left.resulttype);
  1602. end;
  1603. setdef:
  1604. begin
  1605. result:=do_lowhigh(tsetdef(left.resulttype.def).elementtype);
  1606. end;
  1607. arraydef:
  1608. begin
  1609. if inlinenumber=in_low_x then
  1610. begin
  1611. result:=cordconstnode.create(tarraydef(
  1612. left.resulttype.def).lowrange,tarraydef(left.resulttype.def).rangetype,true);
  1613. end
  1614. else
  1615. begin
  1616. if is_open_array(left.resulttype.def) or
  1617. is_array_of_const(left.resulttype.def) then
  1618. begin
  1619. set_varstate(left,vs_read,[]);
  1620. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1621. end
  1622. else
  1623. if is_dynamic_array(left.resulttype.def) then
  1624. begin
  1625. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1626. { can't use inserttypeconv because we need }
  1627. { an explicit type conversion (JM) }
  1628. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1629. result := ccallnode.createintern('fpc_dynarray_high',hp);
  1630. { make sure the left node doesn't get disposed, since it's }
  1631. { reused in the new node (JM) }
  1632. left:=nil;
  1633. end
  1634. else
  1635. begin
  1636. result:=cordconstnode.create(tarraydef(
  1637. left.resulttype.def).highrange,tarraydef(left.resulttype.def).rangetype,true);
  1638. end;
  1639. end;
  1640. end;
  1641. stringdef:
  1642. begin
  1643. if inlinenumber=in_low_x then
  1644. begin
  1645. result:=cordconstnode.create(0,u8inttype,false);
  1646. end
  1647. else
  1648. begin
  1649. if is_open_string(left.resulttype.def) then
  1650. begin
  1651. set_varstate(left,vs_read,[]);
  1652. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  1653. end
  1654. else
  1655. result:=cordconstnode.create(tstringdef(left.resulttype.def).len,u8inttype,true);
  1656. end;
  1657. end;
  1658. else
  1659. CGMessage(type_e_mismatch);
  1660. end;
  1661. end;
  1662. in_exp_real :
  1663. begin
  1664. if left.nodetype in [ordconstn,realconstn] then
  1665. begin
  1666. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  1667. if (trealconstnode(result).value_real=MathInf.Value) and
  1668. ((cs_check_range in aktlocalswitches) or
  1669. (cs_check_overflow in aktlocalswitches)) then
  1670. begin
  1671. result:=crealconstnode.create(0,pbestrealtype^);
  1672. CGMessage(parser_e_range_check_error);
  1673. end;
  1674. end
  1675. else
  1676. begin
  1677. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1678. inserttypeconv(left,pbestrealtype^);
  1679. resulttype:=pbestrealtype^;
  1680. end;
  1681. end;
  1682. in_trunc_real :
  1683. begin
  1684. if left.nodetype in [ordconstn,realconstn] then
  1685. begin
  1686. vr:=getconstrealvalue;
  1687. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1688. begin
  1689. CGMessage(parser_e_range_check_error);
  1690. result:=cordconstnode.create(1,s64inttype,false)
  1691. end
  1692. else
  1693. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  1694. end
  1695. else
  1696. begin
  1697. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1698. inserttypeconv(left,pbestrealtype^);
  1699. resulttype:=s64inttype;
  1700. end;
  1701. end;
  1702. in_round_real :
  1703. begin
  1704. if left.nodetype in [ordconstn,realconstn] then
  1705. begin
  1706. vr:=getconstrealvalue;
  1707. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1708. begin
  1709. CGMessage(parser_e_range_check_error);
  1710. result:=cordconstnode.create(1,s64inttype,false)
  1711. end
  1712. else
  1713. result:=cordconstnode.create(round(vr),s64inttype,true)
  1714. end
  1715. else
  1716. begin
  1717. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1718. inserttypeconv(left,pbestrealtype^);
  1719. resulttype:=s64inttype;
  1720. end;
  1721. end;
  1722. in_frac_real :
  1723. begin
  1724. if left.nodetype in [ordconstn,realconstn] then
  1725. setconstrealvalue(frac(getconstrealvalue))
  1726. else
  1727. begin
  1728. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1729. inserttypeconv(left,pbestrealtype^);
  1730. resulttype:=pbestrealtype^;
  1731. end;
  1732. end;
  1733. in_int_real :
  1734. begin
  1735. if left.nodetype in [ordconstn,realconstn] then
  1736. setconstrealvalue(int(getconstrealvalue))
  1737. else
  1738. begin
  1739. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1740. inserttypeconv(left,pbestrealtype^);
  1741. resulttype:=pbestrealtype^;
  1742. end;
  1743. end;
  1744. in_pi_real :
  1745. begin
  1746. if block_type=bt_const then
  1747. setconstrealvalue(getpi)
  1748. else
  1749. resulttype:=pbestrealtype^;
  1750. end;
  1751. in_cos_real :
  1752. begin
  1753. if left.nodetype in [ordconstn,realconstn] then
  1754. setconstrealvalue(cos(getconstrealvalue))
  1755. else
  1756. begin
  1757. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1758. inserttypeconv(left,pbestrealtype^);
  1759. resulttype:=pbestrealtype^;
  1760. end;
  1761. end;
  1762. in_sin_real :
  1763. begin
  1764. if left.nodetype in [ordconstn,realconstn] then
  1765. setconstrealvalue(sin(getconstrealvalue))
  1766. else
  1767. begin
  1768. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1769. inserttypeconv(left,pbestrealtype^);
  1770. resulttype:=pbestrealtype^;
  1771. end;
  1772. end;
  1773. in_arctan_real :
  1774. begin
  1775. if left.nodetype in [ordconstn,realconstn] then
  1776. setconstrealvalue(arctan(getconstrealvalue))
  1777. else
  1778. begin
  1779. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1780. inserttypeconv(left,pbestrealtype^);
  1781. resulttype:=pbestrealtype^;
  1782. end;
  1783. end;
  1784. in_abs_real :
  1785. begin
  1786. if left.nodetype in [ordconstn,realconstn] then
  1787. setconstrealvalue(abs(getconstrealvalue))
  1788. else
  1789. begin
  1790. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1791. inserttypeconv(left,pbestrealtype^);
  1792. resulttype:=pbestrealtype^;
  1793. end;
  1794. end;
  1795. in_sqr_real :
  1796. begin
  1797. if left.nodetype in [ordconstn,realconstn] then
  1798. setconstrealvalue(sqr(getconstrealvalue))
  1799. else
  1800. begin
  1801. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1802. setfloatresulttype;
  1803. end;
  1804. end;
  1805. in_sqrt_real :
  1806. begin
  1807. if left.nodetype in [ordconstn,realconstn] then
  1808. begin
  1809. vr:=getconstrealvalue;
  1810. if vr<0.0 then
  1811. result:=handle_sqrt_const(vr)
  1812. else
  1813. setconstrealvalue(sqrt(vr));
  1814. end
  1815. else
  1816. begin
  1817. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1818. setfloatresulttype;
  1819. end;
  1820. end;
  1821. in_ln_real :
  1822. begin
  1823. if left.nodetype in [ordconstn,realconstn] then
  1824. begin
  1825. vr:=getconstrealvalue;
  1826. if vr<=0.0 then
  1827. result:=handle_ln_const(vr)
  1828. else
  1829. setconstrealvalue(ln(vr));
  1830. end
  1831. else
  1832. begin
  1833. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1834. inserttypeconv(left,pbestrealtype^);
  1835. resulttype:=pbestrealtype^;
  1836. end;
  1837. end;
  1838. {$ifdef SUPPORT_MMX}
  1839. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  1840. begin
  1841. end;
  1842. {$endif SUPPORT_MMX}
  1843. in_prefetch_var:
  1844. begin
  1845. resulttype:=voidtype;
  1846. end;
  1847. in_assert_x_y :
  1848. begin
  1849. resulttype:=voidtype;
  1850. if assigned(left) then
  1851. begin
  1852. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  1853. { check type }
  1854. if is_boolean(left.resulttype.def) then
  1855. begin
  1856. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1857. { must always be a string }
  1858. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  1859. end
  1860. else
  1861. CGMessage1(type_e_boolean_expr_expected,left.resulttype.def.typename);
  1862. end
  1863. else
  1864. CGMessage(type_e_mismatch);
  1865. { We've checked the whole statement for correctness, now we
  1866. can remove it if assertions are off }
  1867. if not(cs_do_assertion in aktlocalswitches) then
  1868. begin
  1869. { we need a valid node, so insert a nothingn }
  1870. result:=cnothingnode.create;
  1871. end
  1872. else
  1873. include(current_procinfo.flags,pi_do_call);
  1874. end;
  1875. else
  1876. internalerror(8);
  1877. end;
  1878. end;
  1879. myexit:
  1880. { Run get_paratype again to update maybe inserted typeconvs }
  1881. if not codegenerror then
  1882. begin
  1883. if assigned(left) and
  1884. (left.nodetype=callparan) then
  1885. tcallparanode(left).get_paratype;
  1886. end;
  1887. dec(parsing_para_level);
  1888. end;
  1889. function tinlinenode.pass_1 : tnode;
  1890. var
  1891. hp,hpp : tnode;
  1892. shiftconst: longint;
  1893. tempnode: ttempcreatenode;
  1894. newstatement: tstatementnode;
  1895. newblock: tblocknode;
  1896. begin
  1897. result:=nil;
  1898. { if we handle writeln; left contains no valid address }
  1899. if assigned(left) then
  1900. begin
  1901. if left.nodetype=callparan then
  1902. tcallparanode(left).firstcallparan
  1903. else
  1904. firstpass(left);
  1905. left_max;
  1906. end;
  1907. inc(parsing_para_level);
  1908. { intern const should already be handled }
  1909. if nf_inlineconst in flags then
  1910. internalerror(200104044);
  1911. case inlinenumber of
  1912. in_lo_qword,
  1913. in_hi_qword,
  1914. in_lo_long,
  1915. in_hi_long,
  1916. in_lo_word,
  1917. in_hi_word:
  1918. begin
  1919. shiftconst := 0;
  1920. case inlinenumber of
  1921. in_hi_qword:
  1922. shiftconst := 32;
  1923. in_hi_long:
  1924. shiftconst := 16;
  1925. in_hi_word:
  1926. shiftconst := 8;
  1927. end;
  1928. if shiftconst <> 0 then
  1929. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  1930. cordconstnode.create(shiftconst,u32inttype,false)),resulttype)
  1931. else
  1932. result := ctypeconvnode.create_internal(left,resulttype);
  1933. left := nil;
  1934. firstpass(result);
  1935. end;
  1936. in_sizeof_x:
  1937. begin
  1938. if registersint<1 then
  1939. registersint:=1;
  1940. expectloc:=LOC_REGISTER;
  1941. end;
  1942. in_typeof_x:
  1943. begin
  1944. if registersint<1 then
  1945. registersint:=1;
  1946. expectloc:=LOC_REGISTER;
  1947. end;
  1948. in_length_x:
  1949. begin
  1950. if is_shortstring(left.resulttype.def) then
  1951. expectloc:=left.expectloc
  1952. else
  1953. begin
  1954. { ansi/wide string }
  1955. if registersint<1 then
  1956. registersint:=1;
  1957. expectloc:=LOC_REGISTER;
  1958. end;
  1959. end;
  1960. in_typeinfo_x:
  1961. begin
  1962. expectloc:=LOC_REGISTER;
  1963. registersint:=1;
  1964. end;
  1965. in_assigned_x:
  1966. begin
  1967. expectloc := LOC_JUMP;
  1968. registersint:=1;
  1969. end;
  1970. in_pred_x,
  1971. in_succ_x:
  1972. begin
  1973. if is_64bit(resulttype.def) then
  1974. begin
  1975. if (registersint<2) then
  1976. registersint:=2
  1977. end
  1978. else
  1979. begin
  1980. if (registersint<1) then
  1981. registersint:=1;
  1982. end;
  1983. expectloc:=LOC_REGISTER;
  1984. end;
  1985. in_setlength_x,
  1986. in_initialize_x,
  1987. in_finalize_x:
  1988. begin
  1989. expectloc:=LOC_VOID;
  1990. end;
  1991. in_inc_x,
  1992. in_dec_x:
  1993. begin
  1994. expectloc:=LOC_VOID;
  1995. { check type }
  1996. if
  1997. {$ifndef cpu64bit}
  1998. is_64bit(left.resulttype.def) or
  1999. {$endif cpu64bit}
  2000. { range/overflow checking doesn't work properly }
  2001. { with the inc/dec code that's generated (JM) }
  2002. (
  2003. (((left.resulttype.def.deftype = orddef) and
  2004. not(is_char(left.resulttype.def)) and
  2005. not(is_boolean(left.resulttype.def))) or
  2006. (left.resulttype.def.deftype = pointerdef)) and
  2007. (aktlocalswitches * [cs_check_overflow,cs_check_range] <> [])
  2008. ) then
  2009. { convert to simple add (JM) }
  2010. begin
  2011. newblock := internalstatements(newstatement);
  2012. { extra parameter? }
  2013. if assigned(tcallparanode(left).right) then
  2014. begin
  2015. { Yes, use for add node }
  2016. hpp := tcallparanode(tcallparanode(left).right).left;
  2017. tcallparanode(tcallparanode(left).right).left := nil;
  2018. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2019. CGMessage(parser_e_illegal_expression);
  2020. end
  2021. else
  2022. begin
  2023. { no, create constant 1 }
  2024. hpp := cordconstnode.create(1,tcallparanode(left).left.resulttype,false);
  2025. end;
  2026. resulttypepass(hpp);
  2027. {$ifndef cpu64bit}
  2028. if not((hpp.resulttype.def.deftype=orddef) and
  2029. (torddef(hpp.resulttype.def).typ<>u32bit)) then
  2030. {$endif cpu64bit}
  2031. inserttypeconv_internal(hpp,sinttype);
  2032. { No overflow check for pointer operations, because inc(pointer,-1) will always
  2033. trigger an overflow. For uint32 it works because then the operation is done
  2034. in 64bit }
  2035. if (tcallparanode(left).left.resulttype.def.deftype=pointerdef) then
  2036. exclude(aktlocalswitches,cs_check_overflow);
  2037. { make sure we don't call functions part of the left node twice (and generally }
  2038. { optimize the code generation) }
  2039. if node_complexity(tcallparanode(left).left) > 1 then
  2040. begin
  2041. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.def.size,tt_persistent,true);
  2042. addstatement(newstatement,tempnode);
  2043. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2044. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  2045. hp := cderefnode.create(ctemprefnode.create(tempnode));
  2046. inserttypeconv_internal(hp,tcallparanode(left).left.resulttype);
  2047. end
  2048. else
  2049. begin
  2050. hp := tcallparanode(left).left.getcopy;
  2051. tempnode := nil;
  2052. end;
  2053. { addition/substraction depending on inc/dec }
  2054. if inlinenumber = in_inc_x then
  2055. hpp := caddnode.create(addn,hp,hpp)
  2056. else
  2057. hpp := caddnode.create(subn,hp,hpp);
  2058. { assign result of addition }
  2059. inserttypeconv_internal(hpp,hp.resulttype);
  2060. addstatement(newstatement,cassignmentnode.create(hp.getcopy,hpp));
  2061. { deallocate the temp }
  2062. if assigned(tempnode) then
  2063. addstatement(newstatement,ctempdeletenode.create(tempnode));
  2064. { firstpass it }
  2065. firstpass(newblock);
  2066. { return new node }
  2067. result := newblock;
  2068. end
  2069. else if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  2070. is_ordinal(left.resulttype.def) then
  2071. begin
  2072. { two paras ? }
  2073. if assigned(tcallparanode(left).right) then
  2074. begin
  2075. { need we an additional register ? }
  2076. if not(is_constintnode(tcallparanode(tcallparanode(left).right).left)) and
  2077. (tcallparanode(tcallparanode(left).right).left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) and
  2078. (tcallparanode(tcallparanode(left).right).left.registersint<=1) then
  2079. inc(registersint);
  2080. { do we need an additional register to restore the first parameter? }
  2081. if tcallparanode(tcallparanode(left).right).left.registersint>=registersint then
  2082. inc(registersint);
  2083. end;
  2084. end;
  2085. end;
  2086. in_include_x_y,
  2087. in_exclude_x_y:
  2088. begin
  2089. expectloc:=LOC_VOID;
  2090. registersint:=left.registersint;
  2091. registersfpu:=left.registersfpu;
  2092. {$ifdef SUPPORT_MMX}
  2093. registersmmx:=left.registersmmx;
  2094. {$endif SUPPORT_MMX}
  2095. end;
  2096. in_exp_real:
  2097. begin
  2098. result:= first_exp_real;
  2099. end;
  2100. in_round_real:
  2101. begin
  2102. result:= first_round_real;
  2103. end;
  2104. in_trunc_real:
  2105. begin
  2106. result:= first_trunc_real;
  2107. end;
  2108. in_int_real:
  2109. begin
  2110. result:= first_int_real;
  2111. end;
  2112. in_frac_real:
  2113. begin
  2114. result:= first_frac_real;
  2115. end;
  2116. in_cos_real:
  2117. begin
  2118. result:= first_cos_real;
  2119. end;
  2120. in_sin_real:
  2121. begin
  2122. result := first_sin_real;
  2123. end;
  2124. in_arctan_real:
  2125. begin
  2126. result := first_arctan_real;
  2127. end;
  2128. in_pi_real :
  2129. begin
  2130. result := first_pi;
  2131. end;
  2132. in_abs_real:
  2133. begin
  2134. result := first_abs_real;
  2135. end;
  2136. in_sqr_real:
  2137. begin
  2138. result := first_sqr_real;
  2139. end;
  2140. in_sqrt_real:
  2141. begin
  2142. result := first_sqrt_real;
  2143. end;
  2144. in_ln_real:
  2145. begin
  2146. result := first_ln_real;
  2147. end;
  2148. {$ifdef SUPPORT_MMX}
  2149. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2150. begin
  2151. end;
  2152. {$endif SUPPORT_MMX}
  2153. in_assert_x_y :
  2154. begin
  2155. expectloc:=LOC_VOID;
  2156. registersint:=left.registersint;
  2157. registersfpu:=left.registersfpu;
  2158. {$ifdef SUPPORT_MMX}
  2159. registersmmx:=left.registersmmx;
  2160. {$endif SUPPORT_MMX}
  2161. end;
  2162. in_low_x,
  2163. in_high_x:
  2164. internalerror(200104047);
  2165. in_slice_x:
  2166. internalerror(2005101501);
  2167. in_ord_x,
  2168. in_chr_byte:
  2169. begin
  2170. { should not happend as it's converted to typeconv }
  2171. internalerror(200104045);
  2172. end;
  2173. in_ofs_x :
  2174. internalerror(2000101001);
  2175. in_seg_x :
  2176. internalerror(200104046);
  2177. in_settextbuf_file_x,
  2178. in_reset_typedfile,
  2179. in_rewrite_typedfile,
  2180. in_str_x_string,
  2181. in_val_x,
  2182. in_read_x,
  2183. in_readln_x,
  2184. in_write_x,
  2185. in_writeln_x :
  2186. begin
  2187. { should be handled by det_resulttype }
  2188. internalerror(200108234);
  2189. end;
  2190. in_prefetch_var:
  2191. begin
  2192. expectloc:=LOC_VOID;
  2193. end;
  2194. else
  2195. internalerror(8);
  2196. end;
  2197. dec(parsing_para_level);
  2198. end;
  2199. {$ifdef fpc}
  2200. {$maxfpuregisters default}
  2201. {$endif fpc}
  2202. function tinlinenode.docompare(p: tnode): boolean;
  2203. begin
  2204. docompare :=
  2205. inherited docompare(p) and
  2206. (inlinenumber = tinlinenode(p).inlinenumber);
  2207. end;
  2208. function tinlinenode.first_pi : tnode;
  2209. begin
  2210. result:=crealconstnode.create(getpi,pbestrealtype^);
  2211. end;
  2212. function tinlinenode.first_arctan_real : tnode;
  2213. begin
  2214. { create the call to the helper }
  2215. { on entry left node contains the parameter }
  2216. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  2217. ccallparanode.create(left,nil));
  2218. left := nil;
  2219. end;
  2220. function tinlinenode.first_abs_real : tnode;
  2221. begin
  2222. { create the call to the helper }
  2223. { on entry left node contains the parameter }
  2224. first_abs_real := ccallnode.createintern('fpc_abs_real',
  2225. ccallparanode.create(left,nil));
  2226. left := nil;
  2227. end;
  2228. function tinlinenode.first_sqr_real : tnode;
  2229. begin
  2230. { create the call to the helper }
  2231. { on entry left node contains the parameter }
  2232. first_sqr_real := ctypeconvnode.create_internal(ccallnode.createintern('fpc_sqr_real',
  2233. ccallparanode.create(left,nil)),resulttype);
  2234. left := nil;
  2235. end;
  2236. function tinlinenode.first_sqrt_real : tnode;
  2237. begin
  2238. { create the call to the helper }
  2239. { on entry left node contains the parameter }
  2240. first_sqrt_real := ctypeconvnode.create_internal(ccallnode.createintern('fpc_sqrt_real',
  2241. ccallparanode.create(left,nil)),resulttype);
  2242. left := nil;
  2243. end;
  2244. function tinlinenode.first_ln_real : tnode;
  2245. begin
  2246. { create the call to the helper }
  2247. { on entry left node contains the parameter }
  2248. first_ln_real := ccallnode.createintern('fpc_ln_real',
  2249. ccallparanode.create(left,nil));
  2250. left := nil;
  2251. end;
  2252. function tinlinenode.first_cos_real : tnode;
  2253. begin
  2254. { create the call to the helper }
  2255. { on entry left node contains the parameter }
  2256. first_cos_real := ccallnode.createintern('fpc_cos_real',
  2257. ccallparanode.create(left,nil));
  2258. left := nil;
  2259. end;
  2260. function tinlinenode.first_sin_real : tnode;
  2261. begin
  2262. { create the call to the helper }
  2263. { on entry left node contains the parameter }
  2264. first_sin_real := ccallnode.createintern('fpc_sin_real',
  2265. ccallparanode.create(left,nil));
  2266. left := nil;
  2267. end;
  2268. function tinlinenode.first_exp_real : tnode;
  2269. begin
  2270. { create the call to the helper }
  2271. { on entry left node contains the parameter }
  2272. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  2273. left := nil;
  2274. end;
  2275. function tinlinenode.first_int_real : tnode;
  2276. begin
  2277. { create the call to the helper }
  2278. { on entry left node contains the parameter }
  2279. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  2280. left := nil;
  2281. end;
  2282. function tinlinenode.first_frac_real : tnode;
  2283. begin
  2284. { create the call to the helper }
  2285. { on entry left node contains the parameter }
  2286. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  2287. left := nil;
  2288. end;
  2289. function tinlinenode.first_round_real : tnode;
  2290. begin
  2291. { create the call to the helper }
  2292. { on entry left node contains the parameter }
  2293. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  2294. left := nil;
  2295. end;
  2296. function tinlinenode.first_trunc_real : tnode;
  2297. begin
  2298. { create the call to the helper }
  2299. { on entry left node contains the parameter }
  2300. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  2301. left := nil;
  2302. end;
  2303. begin
  2304. cinlinenode:=tinlinenode;
  2305. end.