ninl.pas 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544
  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_readwritten,[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)) or do_read 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. { in case of writing a chararray, add whether it's }
  726. { zero-based }
  727. if not(do_read) and
  728. (para.left.resulttype.def.deftype = arraydef) then
  729. para := ccallparanode.create(cordconstnode.create(
  730. ord(tarraydef(para.left.resulttype.def).lowrange=0),booltype,false),para);
  731. { create the call statement }
  732. addstatement(newstatement,
  733. ccallnode.createintern(name,para));
  734. end
  735. end
  736. else
  737. { error_para = true }
  738. begin
  739. { free the parameter, since it isn't referenced anywhere anymore }
  740. para.right := nil;
  741. para.free;
  742. if assigned(lenpara) then
  743. begin
  744. lenpara.right := nil;
  745. lenpara.free;
  746. end;
  747. if assigned(fracpara) then
  748. begin
  749. fracpara.right := nil;
  750. fracpara.free;
  751. end;
  752. end;
  753. { process next parameter }
  754. para := nextpara;
  755. end;
  756. { if no error, add the write(ln)/read(ln) end calls }
  757. if not found_error then
  758. begin
  759. case inlinenumber of
  760. in_read_x:
  761. name:='fpc_read_end';
  762. in_write_x:
  763. name:='fpc_write_end';
  764. in_readln_x:
  765. name:='fpc_readln_end';
  766. in_writeln_x:
  767. name:='fpc_writeln_end';
  768. end;
  769. addstatement(newstatement,ccallnode.createintern(name,filepara));
  770. end;
  771. end;
  772. { if we found an error, simply delete the generated blocknode }
  773. if found_error then
  774. newblock.free
  775. else
  776. begin
  777. { deallocate the temp for the file para if we used one }
  778. if assigned(filetemp) then
  779. addstatement(newstatement,ctempdeletenode.create(filetemp));
  780. { otherwise return the newly generated block of instructions, }
  781. { but first free the errornode we generated at the beginning }
  782. result.free;
  783. result := newblock
  784. end;
  785. end;
  786. function tinlinenode.handle_val: tnode;
  787. var
  788. procname,
  789. suffix : string[31];
  790. sourcepara,
  791. destpara,
  792. codepara,
  793. sizepara,
  794. newparas : tcallparanode;
  795. orgcode : tnode;
  796. newstatement : tstatementnode;
  797. newblock : tblocknode;
  798. tempcode : ttempcreatenode;
  799. begin
  800. { for easy exiting if something goes wrong }
  801. result := cerrornode.create;
  802. { check the amount of parameters }
  803. if not(assigned(left)) or
  804. not(assigned(tcallparanode(left).right)) then
  805. begin
  806. CGMessage(parser_e_wrong_parameter_size);
  807. exit;
  808. end;
  809. { reverse parameters for easier processing }
  810. left := reverseparameters(tcallparanode(left));
  811. { get the parameters }
  812. tempcode := nil;
  813. orgcode := nil;
  814. sizepara := nil;
  815. sourcepara := tcallparanode(left);
  816. destpara := tcallparanode(sourcepara.right);
  817. codepara := tcallparanode(destpara.right);
  818. { check if codepara is valid }
  819. if assigned(codepara) and
  820. (
  821. (codepara.resulttype.def.deftype <> orddef)
  822. {$ifndef cpu64bit}
  823. or is_64bitint(codepara.resulttype.def)
  824. {$endif cpu64bit}
  825. ) then
  826. begin
  827. CGMessagePos1(codepara.fileinfo,type_e_integer_expr_expected,codepara.resulttype.def.typename);
  828. exit;
  829. end;
  830. { check if dest para is valid }
  831. if not(destpara.resulttype.def.deftype in [orddef,floatdef]) then
  832. begin
  833. CGMessagePos(destpara.fileinfo,type_e_integer_or_real_expr_expected);
  834. exit;
  835. end;
  836. { we're going to reuse the exisiting para's, so make sure they }
  837. { won't be disposed }
  838. left := nil;
  839. { create the blocknode which will hold the generated statements + }
  840. { an initial dummy statement }
  841. newblock:=internalstatements(newstatement);
  842. { do we need a temp for code? Yes, if no code specified, or if }
  843. { code is not a 32bit parameter (we already checked whether the }
  844. { the code para, if specified, was an orddef) }
  845. if not assigned(codepara) or
  846. (codepara.resulttype.def.size<>sinttype.def.size) then
  847. begin
  848. tempcode := ctempcreatenode.create(sinttype,sinttype.def.size,tt_persistent,false);
  849. addstatement(newstatement,tempcode);
  850. { set the resulttype of the temp (needed to be able to get }
  851. { the resulttype of the tempref used in the new code para) }
  852. resulttypepass(tnode(tempcode));
  853. { create a temp codepara, but save the original code para to }
  854. { assign the result to later on }
  855. if assigned(codepara) then
  856. begin
  857. orgcode := codepara.left;
  858. codepara.left := ctemprefnode.create(tempcode);
  859. end
  860. else
  861. codepara := ccallparanode.create(ctemprefnode.create(tempcode),nil);
  862. { we need its resulttype later on }
  863. codepara.get_paratype;
  864. end
  865. else if (torddef(codepara.resulttype.def).typ = torddef(sinttype.def).typ) then
  866. { because code is a var parameter, it must match types exactly }
  867. { however, since it will return values in [0..255], both longints }
  868. { and cardinals are fine. Since the formal code para type is }
  869. { longint, insert a typecoversion to longint for cardinal para's }
  870. begin
  871. codepara.left := ctypeconvnode.create_internal(codepara.left,sinttype);
  872. { make it explicit, oterwise you may get a nonsense range }
  873. { check error if the cardinal already contained a value }
  874. { > $7fffffff }
  875. codepara.get_paratype;
  876. end;
  877. { create the procedure name }
  878. procname := 'fpc_val_';
  879. case destpara.resulttype.def.deftype of
  880. orddef:
  881. begin
  882. case torddef(destpara.resulttype.def).typ of
  883. {$ifdef cpu64bit}
  884. scurrency,
  885. s64bit,
  886. {$endif cpu64bit}
  887. s8bit,
  888. s16bit,
  889. s32bit:
  890. begin
  891. suffix := 'sint_';
  892. { we also need a destsize para in this case }
  893. sizepara := ccallparanode.create(cordconstnode.create
  894. (destpara.resulttype.def.size,s32inttype,true),nil);
  895. end;
  896. {$ifdef cpu64bit}
  897. u64bit,
  898. {$endif cpu64bit}
  899. u8bit,
  900. u16bit,
  901. u32bit:
  902. suffix := 'uint_';
  903. {$ifndef cpu64bit}
  904. scurrency,
  905. s64bit: suffix := 'int64_';
  906. u64bit: suffix := 'qword_';
  907. {$endif cpu64bit}
  908. else
  909. internalerror(200304225);
  910. end;
  911. end;
  912. floatdef:
  913. begin
  914. suffix := 'real_';
  915. end;
  916. end;
  917. procname := procname + suffix;
  918. { play a trick to have tcallnode handle invalid source parameters: }
  919. { the shortstring-longint val routine by default }
  920. if (sourcepara.resulttype.def.deftype = stringdef) then
  921. procname := procname + tstringdef(sourcepara.resulttype.def).stringtypname
  922. else
  923. procname := procname + 'shortstr';
  924. { set up the correct parameters for the call: the code para... }
  925. newparas := codepara;
  926. { and the source para }
  927. codepara.right := sourcepara;
  928. { sizepara either contains nil if none is needed (which is ok, since }
  929. { then the next statement severes any possible links with other paras }
  930. { that sourcepara may have) or it contains the necessary size para and }
  931. { its right field is nil }
  932. sourcepara.right := sizepara;
  933. { create the call and assign the result to dest (val helpers are functions).
  934. Use a trick to prevent a type size mismatch warning to be generated by the
  935. assignment node. First convert implicitly to the resulttype. This will insert
  936. the range check. The Second conversion is done explicitly to hide the implicit conversion
  937. for the assignment node and therefor preventing the warning (PFV) }
  938. addstatement(newstatement,cassignmentnode.create(
  939. destpara.left,ctypeconvnode.create_internal(ctypeconvnode.create(ccallnode.createintern(procname,newparas),destpara.left.resulttype),destpara.left.resulttype)));
  940. { dispose of the enclosing paranode of the destination }
  941. destpara.left := nil;
  942. destpara.right := nil;
  943. destpara.free;
  944. { check if we used a temp for code and whether we have to store }
  945. { it to the real code parameter }
  946. if assigned(orgcode) then
  947. addstatement(newstatement,cassignmentnode.create(
  948. orgcode,
  949. ctypeconvnode.create_internal(
  950. ctemprefnode.create(tempcode),orgcode.resulttype)));
  951. { release the temp if we allocated one }
  952. if assigned(tempcode) then
  953. addstatement(newstatement,ctempdeletenode.create(tempcode));
  954. { free the errornode }
  955. result.free;
  956. { and return it }
  957. result := newblock;
  958. end;
  959. {$ifdef fpc}
  960. {$maxfpuregisters 0}
  961. {$endif fpc}
  962. function getpi : bestreal;
  963. begin
  964. {$ifdef x86}
  965. { x86 has pi in hardware }
  966. result:=pi;
  967. {$else x86}
  968. {$ifdef cpuextended}
  969. result:=extended(MathPiExtended);
  970. {$else cpuextended}
  971. result:=double(MathPi);
  972. {$endif cpuextended}
  973. {$endif x86}
  974. end;
  975. function tinlinenode.det_resulttype:tnode;
  976. function do_lowhigh(const t:ttype) : tnode;
  977. var
  978. v : tconstexprint;
  979. enum : tenumsym;
  980. hp : tnode;
  981. begin
  982. case t.def.deftype of
  983. orddef:
  984. begin
  985. if inlinenumber=in_low_x then
  986. v:=torddef(t.def).low
  987. else
  988. v:=torddef(t.def).high;
  989. { low/high of torddef are longints, so we need special }
  990. { handling for cardinal and 64bit types (JM) }
  991. { 1.0.x doesn't support int64($ffffffff) correct, it'll expand
  992. to -1 instead of staying $ffffffff. Therefor we use $ffff with
  993. shl twice (PFV) }
  994. case torddef(t.def).typ of
  995. s64bit,scurrency :
  996. begin
  997. if (inlinenumber=in_low_x) then
  998. v := int64($80000000) shl 32
  999. else
  1000. v := (int64($7fffffff) shl 32) or int64($ffff) shl 16 or int64($ffff)
  1001. end;
  1002. u64bit :
  1003. begin
  1004. { we have to use a dirty trick for high(qword), }
  1005. { because it's bigger than high(tconstexprint) (JM) }
  1006. v := 0
  1007. end
  1008. else
  1009. begin
  1010. if not is_signed(t.def) then
  1011. v := cardinal(v);
  1012. end;
  1013. end;
  1014. hp:=cordconstnode.create(v,t,true);
  1015. resulttypepass(hp);
  1016. { fix high(qword) }
  1017. if (torddef(t.def).typ=u64bit) and
  1018. (inlinenumber = in_high_x) then
  1019. tordconstnode(hp).value := -1; { is the same as qword($ffffffffffffffff) }
  1020. do_lowhigh:=hp;
  1021. end;
  1022. enumdef:
  1023. begin
  1024. enum:=tenumsym(tenumdef(t.def).firstenum);
  1025. v:=tenumdef(t.def).maxval;
  1026. if inlinenumber=in_high_x then
  1027. while assigned(enum) and (enum.value <> v) do
  1028. enum:=enum.nextenum;
  1029. if not assigned(enum) then
  1030. internalerror(309993)
  1031. else
  1032. hp:=genenumnode(enum);
  1033. do_lowhigh:=hp;
  1034. end;
  1035. else
  1036. internalerror(87);
  1037. end;
  1038. end;
  1039. function getconstrealvalue : bestreal;
  1040. begin
  1041. case left.nodetype of
  1042. ordconstn:
  1043. getconstrealvalue:=tordconstnode(left).value;
  1044. realconstn:
  1045. getconstrealvalue:=trealconstnode(left).value_real;
  1046. else
  1047. internalerror(309992);
  1048. end;
  1049. end;
  1050. procedure setconstrealvalue(r : bestreal);
  1051. begin
  1052. result:=crealconstnode.create(r,pbestrealtype^);
  1053. end;
  1054. function handle_ln_const(r : bestreal) : tnode;
  1055. begin
  1056. if r<=0.0 then
  1057. if (cs_check_range in aktlocalswitches) or
  1058. (cs_check_overflow in aktlocalswitches) then
  1059. begin
  1060. result:=crealconstnode.create(0,pbestrealtype^);
  1061. CGMessage(type_e_wrong_math_argument)
  1062. end
  1063. else
  1064. begin
  1065. if r=0.0 then
  1066. result:=crealconstnode.create(double(MathQNaN),pbestrealtype^)
  1067. else
  1068. result:=crealconstnode.create(double(MathNegInf),pbestrealtype^)
  1069. end
  1070. else
  1071. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1072. end;
  1073. function handle_sqrt_const(r : bestreal) : tnode;
  1074. begin
  1075. if r<0.0 then
  1076. if (cs_check_range in aktlocalswitches) or
  1077. (cs_check_overflow in aktlocalswitches) then
  1078. begin
  1079. result:=crealconstnode.create(0,pbestrealtype^);
  1080. CGMessage(type_e_wrong_math_argument)
  1081. end
  1082. else
  1083. result:=crealconstnode.create(double(MathQNaN),pbestrealtype^)
  1084. else
  1085. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1086. end;
  1087. procedure setfloatresulttype;
  1088. begin
  1089. if (left.resulttype.def.deftype=floatdef) and
  1090. (tfloatdef(left.resulttype.def).typ in [s32real,s64real,s80real,s128real]) then
  1091. resulttype:=left.resulttype
  1092. else
  1093. begin
  1094. inserttypeconv(left,pbestrealtype^);
  1095. resulttype:=pbestrealtype^;
  1096. end;
  1097. end;
  1098. var
  1099. vl,vl2 : TConstExprInt;
  1100. vr : bestreal;
  1101. hightree,
  1102. hp : tnode;
  1103. srsym : tsym;
  1104. checkrange : boolean;
  1105. label
  1106. myexit;
  1107. begin
  1108. result:=nil;
  1109. { if we handle writeln; left contains no valid address }
  1110. if assigned(left) then
  1111. begin
  1112. if left.nodetype=callparan then
  1113. tcallparanode(left).get_paratype
  1114. else
  1115. resulttypepass(left);
  1116. end;
  1117. inc(parsing_para_level);
  1118. { handle intern constant functions in separate case }
  1119. if nf_inlineconst in flags then
  1120. begin
  1121. { no parameters? }
  1122. if not assigned(left) then
  1123. internalerror(200501231)
  1124. else
  1125. begin
  1126. vl:=0;
  1127. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1128. case left.nodetype of
  1129. realconstn :
  1130. begin
  1131. { Real functions are all handled with internproc below }
  1132. CGMessage1(type_e_integer_expr_expected,left.resulttype.def.typename)
  1133. end;
  1134. ordconstn :
  1135. vl:=tordconstnode(left).value;
  1136. callparan :
  1137. begin
  1138. { both exists, else it was not generated }
  1139. vl:=tordconstnode(tcallparanode(left).left).value;
  1140. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1141. end;
  1142. else
  1143. CGMessage(parser_e_illegal_expression);
  1144. end;
  1145. case inlinenumber of
  1146. in_const_abs :
  1147. hp:=genintconstnode(abs(vl));
  1148. in_const_sqr :
  1149. hp:=genintconstnode(sqr(vl));
  1150. in_const_odd :
  1151. hp:=cordconstnode.create(byte(odd(vl)),booltype,true);
  1152. in_const_swap_word :
  1153. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resulttype,true);
  1154. in_const_swap_long :
  1155. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resulttype,true);
  1156. in_const_swap_qword :
  1157. hp:=cordconstnode.create((vl and $ffff) shl 32+(vl shr 32),left.resulttype,true);
  1158. in_const_ptr :
  1159. hp:=cpointerconstnode.create((vl2 shl 4)+vl,voidfarpointertype);
  1160. else
  1161. internalerror(88);
  1162. end;
  1163. end;
  1164. if hp=nil then
  1165. hp:=cerrornode.create;
  1166. result:=hp;
  1167. goto myexit;
  1168. end
  1169. else
  1170. begin
  1171. case inlinenumber of
  1172. in_lo_long,
  1173. in_hi_long,
  1174. in_lo_qword,
  1175. in_hi_qword,
  1176. in_lo_word,
  1177. in_hi_word :
  1178. begin
  1179. { give warning for incompatibility with tp and delphi }
  1180. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  1181. ((m_tp7 in aktmodeswitches) or
  1182. (m_delphi in aktmodeswitches)) then
  1183. CGMessage(type_w_maybe_wrong_hi_lo);
  1184. { constant folding }
  1185. if left.nodetype=ordconstn then
  1186. begin
  1187. case inlinenumber of
  1188. in_lo_word :
  1189. hp:=cordconstnode.create(tordconstnode(left).value and $ff,left.resulttype,true);
  1190. in_hi_word :
  1191. hp:=cordconstnode.create(tordconstnode(left).value shr 8,left.resulttype,true);
  1192. in_lo_long :
  1193. hp:=cordconstnode.create(tordconstnode(left).value and $ffff,left.resulttype,true);
  1194. in_hi_long :
  1195. hp:=cordconstnode.create(tordconstnode(left).value shr 16,left.resulttype,true);
  1196. in_lo_qword :
  1197. hp:=cordconstnode.create(tordconstnode(left).value and $ffffffff,left.resulttype,true);
  1198. in_hi_qword :
  1199. hp:=cordconstnode.create(tordconstnode(left).value shr 32,left.resulttype,true);
  1200. end;
  1201. result:=hp;
  1202. goto myexit;
  1203. end;
  1204. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1205. if not is_integer(left.resulttype.def) then
  1206. CGMessage1(type_e_integer_expr_expected,left.resulttype.def.typename);
  1207. case inlinenumber of
  1208. in_lo_word,
  1209. in_hi_word :
  1210. resulttype:=u8inttype;
  1211. in_lo_long,
  1212. in_hi_long :
  1213. resulttype:=u16inttype;
  1214. in_lo_qword,
  1215. in_hi_qword :
  1216. resulttype:=u32inttype;
  1217. end;
  1218. end;
  1219. in_sizeof_x:
  1220. begin
  1221. set_varstate(left,vs_read,[]);
  1222. if paramanager.push_high_param(vs_value,left.resulttype.def,current_procinfo.procdef.proccalloption) then
  1223. begin
  1224. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1225. if assigned(hightree) then
  1226. begin
  1227. hp:=caddnode.create(addn,hightree,
  1228. cordconstnode.create(1,sinttype,false));
  1229. if (left.resulttype.def.deftype=arraydef) and
  1230. (tarraydef(left.resulttype.def).elesize<>1) then
  1231. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1232. left.resulttype.def).elesize,sinttype,true));
  1233. result:=hp;
  1234. end;
  1235. end
  1236. else
  1237. resulttype:=sinttype;
  1238. end;
  1239. in_typeof_x:
  1240. begin
  1241. set_varstate(left,vs_read,[]);
  1242. resulttype:=voidpointertype;
  1243. end;
  1244. in_ord_x:
  1245. begin
  1246. if (left.nodetype=ordconstn) then
  1247. begin
  1248. hp:=cordconstnode.create(
  1249. tordconstnode(left).value,sinttype,true);
  1250. result:=hp;
  1251. goto myexit;
  1252. end;
  1253. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1254. case left.resulttype.def.deftype of
  1255. orddef :
  1256. begin
  1257. case torddef(left.resulttype.def).typ of
  1258. bool8bit,
  1259. uchar:
  1260. begin
  1261. { change to byte() }
  1262. hp:=ctypeconvnode.create_internal(left,u8inttype);
  1263. left:=nil;
  1264. result:=hp;
  1265. end;
  1266. bool16bit,
  1267. uwidechar :
  1268. begin
  1269. { change to word() }
  1270. hp:=ctypeconvnode.create_internal(left,u16inttype);
  1271. left:=nil;
  1272. result:=hp;
  1273. end;
  1274. bool32bit :
  1275. begin
  1276. { change to dword() }
  1277. hp:=ctypeconvnode.create_internal(left,u32inttype);
  1278. left:=nil;
  1279. result:=hp;
  1280. end;
  1281. uvoid :
  1282. CGMessage1(type_e_ordinal_expr_expected,left.resulttype.def.typename);
  1283. else
  1284. begin
  1285. { all other orddef need no transformation }
  1286. hp:=left;
  1287. left:=nil;
  1288. result:=hp;
  1289. end;
  1290. end;
  1291. end;
  1292. enumdef :
  1293. begin
  1294. hp:=ctypeconvnode.create_internal(left,s32inttype);
  1295. left:=nil;
  1296. result:=hp;
  1297. end;
  1298. pointerdef :
  1299. begin
  1300. if m_mac in aktmodeswitches then
  1301. begin
  1302. hp:=ctypeconvnode.create_internal(left,ptrinttype);
  1303. left:=nil;
  1304. result:=hp;
  1305. end
  1306. else
  1307. CGMessage1(type_e_ordinal_expr_expected,left.resulttype.def.typename);
  1308. end
  1309. else
  1310. CGMessage1(type_e_ordinal_expr_expected,left.resulttype.def.typename);
  1311. end;
  1312. end;
  1313. in_chr_byte:
  1314. begin
  1315. { convert to explicit char() }
  1316. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1317. hp:=ctypeconvnode.create_internal(left,cchartype);
  1318. left:=nil;
  1319. result:=hp;
  1320. end;
  1321. in_length_x:
  1322. begin
  1323. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1324. case left.resulttype.def.deftype of
  1325. variantdef:
  1326. begin
  1327. inserttypeconv(left,cansistringtype);
  1328. end;
  1329. stringdef :
  1330. begin
  1331. { we don't need string convertions here }
  1332. if (left.nodetype=typeconvn) and
  1333. (ttypeconvnode(left).left.resulttype.def.deftype=stringdef) then
  1334. begin
  1335. hp:=ttypeconvnode(left).left;
  1336. ttypeconvnode(left).left:=nil;
  1337. left.free;
  1338. left:=hp;
  1339. end;
  1340. { evaluates length of constant strings direct }
  1341. if (left.nodetype=stringconstn) then
  1342. begin
  1343. hp:=cordconstnode.create(
  1344. tstringconstnode(left).len,s32inttype,true);
  1345. result:=hp;
  1346. goto myexit;
  1347. end;
  1348. end;
  1349. orddef :
  1350. begin
  1351. { length of char is one allways }
  1352. if is_char(left.resulttype.def) or
  1353. is_widechar(left.resulttype.def) then
  1354. begin
  1355. hp:=cordconstnode.create(1,s32inttype,false);
  1356. result:=hp;
  1357. goto myexit;
  1358. end
  1359. else
  1360. CGMessage(type_e_mismatch);
  1361. end;
  1362. pointerdef :
  1363. begin
  1364. if is_pchar(left.resulttype.def) then
  1365. begin
  1366. hp := ccallparanode.create(left,nil);
  1367. result := ccallnode.createintern('fpc_pchar_length',hp);
  1368. { make sure the left node doesn't get disposed, since it's }
  1369. { reused in the new node (JM) }
  1370. left:=nil;
  1371. goto myexit;
  1372. end
  1373. else if is_pwidechar(left.resulttype.def) then
  1374. begin
  1375. hp := ccallparanode.create(left,nil);
  1376. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  1377. { make sure the left node doesn't get disposed, since it's }
  1378. { reused in the new node (JM) }
  1379. left:=nil;
  1380. goto myexit;
  1381. end
  1382. else
  1383. CGMessage(type_e_mismatch);
  1384. end;
  1385. arraydef :
  1386. begin
  1387. if is_open_array(left.resulttype.def) or
  1388. is_array_of_const(left.resulttype.def) then
  1389. begin
  1390. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1391. if assigned(hightree) then
  1392. begin
  1393. hp:=caddnode.create(addn,hightree,
  1394. cordconstnode.create(1,s32inttype,false));
  1395. result:=hp;
  1396. end;
  1397. goto myexit;
  1398. end
  1399. else
  1400. if not is_dynamic_array(left.resulttype.def) then
  1401. begin
  1402. hp:=cordconstnode.create(tarraydef(left.resulttype.def).highrange-
  1403. tarraydef(left.resulttype.def).lowrange+1,
  1404. s32inttype,true);
  1405. result:=hp;
  1406. goto myexit;
  1407. end
  1408. else
  1409. begin
  1410. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1411. result := ccallnode.createintern('fpc_dynarray_length',hp);
  1412. { make sure the left node doesn't get disposed, since it's }
  1413. { reused in the new node (JM) }
  1414. left:=nil;
  1415. goto myexit;
  1416. end;
  1417. end;
  1418. else
  1419. CGMessage(type_e_mismatch);
  1420. end;
  1421. { shortstring return an 8 bit value as the length
  1422. is the first byte of the string }
  1423. if is_shortstring(left.resulttype.def) then
  1424. resulttype:=u8inttype
  1425. else
  1426. resulttype:=sinttype;
  1427. end;
  1428. in_typeinfo_x:
  1429. begin
  1430. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1431. resulttype:=voidpointertype;
  1432. end;
  1433. in_assigned_x:
  1434. begin
  1435. { the parser has already made sure the expression is valid }
  1436. { handle constant expressions }
  1437. if is_constnode(tcallparanode(left).left) or
  1438. (tcallparanode(left).left.nodetype = pointerconstn) then
  1439. begin
  1440. { let an add node figure it out }
  1441. result := caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  1442. tcallparanode(left).left := nil;
  1443. { free left, because otherwise some code at 'myexit' tries }
  1444. { to run get_paratype for it, which crashes since left.left }
  1445. { is now nil }
  1446. left.free;
  1447. left := nil;
  1448. goto myexit;
  1449. end;
  1450. { otherwise handle separately, because there could be a procvar, which }
  1451. { is 2*sizeof(pointer), while we must only check the first pointer }
  1452. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  1453. resulttype:=booltype;
  1454. end;
  1455. in_ofs_x :
  1456. internalerror(2000101001);
  1457. in_seg_x :
  1458. begin
  1459. set_varstate(left,vs_read,[]);
  1460. result:=cordconstnode.create(0,s32inttype,false);
  1461. goto myexit;
  1462. end;
  1463. in_pred_x,
  1464. in_succ_x:
  1465. begin
  1466. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1467. resulttype:=left.resulttype;
  1468. if not is_ordinal(resulttype.def) then
  1469. CGMessage(type_e_ordinal_expr_expected)
  1470. else
  1471. begin
  1472. if (resulttype.def.deftype=enumdef) and
  1473. (tenumdef(resulttype.def).has_jumps) and
  1474. not(m_delphi in aktmodeswitches) then
  1475. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  1476. end;
  1477. { only if the result is an enum do we do range checking }
  1478. if (resulttype.def.deftype=enumdef) then
  1479. checkrange := true
  1480. else
  1481. checkrange := false;
  1482. { do constant folding after check for jumps }
  1483. if left.nodetype=ordconstn then
  1484. begin
  1485. if inlinenumber=in_succ_x then
  1486. result:=cordconstnode.create(tordconstnode(left).value+1,left.resulttype,checkrange)
  1487. else
  1488. result:=cordconstnode.create(tordconstnode(left).value-1,left.resulttype,checkrange);
  1489. end;
  1490. end;
  1491. in_initialize_x,
  1492. in_finalize_x,
  1493. in_setlength_x:
  1494. begin
  1495. { inlined from pinline }
  1496. internalerror(200204231);
  1497. end;
  1498. in_inc_x,
  1499. in_dec_x:
  1500. begin
  1501. resulttype:=voidtype;
  1502. if assigned(left) then
  1503. begin
  1504. { first param must be var }
  1505. valid_for_var(tcallparanode(left).left,true);
  1506. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1507. if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  1508. is_ordinal(left.resulttype.def) or
  1509. is_currency(left.resulttype.def) then
  1510. begin
  1511. { value of left gets changed -> must be unique }
  1512. set_unique(tcallparanode(left).left);
  1513. { two paras ? }
  1514. if assigned(tcallparanode(left).right) then
  1515. begin
  1516. if is_integer(tcallparanode(left).right.resulttype.def) then
  1517. begin
  1518. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1519. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resulttype);
  1520. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1521. { should be handled in the parser (JM) }
  1522. internalerror(2006020901);
  1523. end
  1524. else
  1525. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  1526. end;
  1527. end
  1528. else
  1529. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  1530. end
  1531. else
  1532. CGMessagePos(fileinfo,type_e_mismatch);
  1533. end;
  1534. in_read_x,
  1535. in_readln_x,
  1536. in_write_x,
  1537. in_writeln_x :
  1538. begin
  1539. result := handle_read_write;
  1540. end;
  1541. in_settextbuf_file_x :
  1542. begin
  1543. resulttype:=voidtype;
  1544. { now we know the type of buffer }
  1545. srsym:=searchsymonlyin(systemunit,'SETTEXTBUF');
  1546. hp:=ccallparanode.create(cordconstnode.create(
  1547. tcallparanode(left).left.resulttype.def.size,s32inttype,true),left);
  1548. result:=ccallnode.create(hp,tprocsym(srsym),systemunit,nil,[]);
  1549. left:=nil;
  1550. end;
  1551. { the firstpass of the arg has been done in firstcalln ? }
  1552. in_reset_typedfile,
  1553. in_rewrite_typedfile :
  1554. begin
  1555. result := handle_reset_rewrite_typed;
  1556. end;
  1557. in_str_x_string :
  1558. begin
  1559. result := handle_str;
  1560. end;
  1561. in_val_x :
  1562. begin
  1563. result := handle_val;
  1564. end;
  1565. in_include_x_y,
  1566. in_exclude_x_y:
  1567. begin
  1568. resulttype:=voidtype;
  1569. { the parser already checks whether we have two (and exectly two) }
  1570. { parameters (JM) }
  1571. { first param must be var }
  1572. valid_for_var(tcallparanode(left).left,true);
  1573. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1574. { check type }
  1575. if (left.resulttype.def.deftype=setdef) then
  1576. begin
  1577. { insert a type conversion }
  1578. { to the type of the set elements }
  1579. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1580. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  1581. tsetdef(left.resulttype.def).elementtype);
  1582. end
  1583. else
  1584. CGMessage(type_e_mismatch);
  1585. end;
  1586. in_slice_x:
  1587. begin
  1588. result:=nil;
  1589. resulttype:=tcallparanode(left).left.resulttype;
  1590. if (resulttype.def.deftype <> arraydef) then
  1591. CGMessagePos(left.fileinfo,type_e_mismatch);
  1592. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resulttype.def)) then
  1593. CGMessagePos1(tcallparanode(left).right.fileinfo,
  1594. type_e_integer_expr_expected,
  1595. tcallparanode(tcallparanode(left).right).left.resulttype.def.typename);
  1596. end;
  1597. in_low_x,
  1598. in_high_x:
  1599. begin
  1600. case left.resulttype.def.deftype of
  1601. orddef,
  1602. enumdef:
  1603. begin
  1604. result:=do_lowhigh(left.resulttype);
  1605. end;
  1606. setdef:
  1607. begin
  1608. result:=do_lowhigh(tsetdef(left.resulttype.def).elementtype);
  1609. end;
  1610. arraydef:
  1611. begin
  1612. if inlinenumber=in_low_x then
  1613. begin
  1614. result:=cordconstnode.create(tarraydef(
  1615. left.resulttype.def).lowrange,tarraydef(left.resulttype.def).rangetype,true);
  1616. end
  1617. else
  1618. begin
  1619. if is_open_array(left.resulttype.def) or
  1620. is_array_of_const(left.resulttype.def) then
  1621. begin
  1622. set_varstate(left,vs_read,[]);
  1623. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1624. end
  1625. else
  1626. if is_dynamic_array(left.resulttype.def) then
  1627. begin
  1628. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1629. { can't use inserttypeconv because we need }
  1630. { an explicit type conversion (JM) }
  1631. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1632. result := ccallnode.createintern('fpc_dynarray_high',hp);
  1633. { make sure the left node doesn't get disposed, since it's }
  1634. { reused in the new node (JM) }
  1635. left:=nil;
  1636. end
  1637. else
  1638. begin
  1639. result:=cordconstnode.create(tarraydef(
  1640. left.resulttype.def).highrange,tarraydef(left.resulttype.def).rangetype,true);
  1641. end;
  1642. end;
  1643. end;
  1644. stringdef:
  1645. begin
  1646. if inlinenumber=in_low_x then
  1647. begin
  1648. result:=cordconstnode.create(0,u8inttype,false);
  1649. end
  1650. else
  1651. begin
  1652. if is_open_string(left.resulttype.def) then
  1653. begin
  1654. set_varstate(left,vs_read,[]);
  1655. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  1656. end
  1657. else
  1658. result:=cordconstnode.create(tstringdef(left.resulttype.def).len,u8inttype,true);
  1659. end;
  1660. end;
  1661. else
  1662. CGMessage(type_e_mismatch);
  1663. end;
  1664. end;
  1665. in_exp_real :
  1666. begin
  1667. if left.nodetype in [ordconstn,realconstn] then
  1668. begin
  1669. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  1670. if (trealconstnode(result).value_real=double(MathInf)) and
  1671. ((cs_check_range in aktlocalswitches) or
  1672. (cs_check_overflow in aktlocalswitches)) then
  1673. begin
  1674. result:=crealconstnode.create(0,pbestrealtype^);
  1675. CGMessage(parser_e_range_check_error);
  1676. end;
  1677. end
  1678. else
  1679. begin
  1680. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1681. inserttypeconv(left,pbestrealtype^);
  1682. resulttype:=pbestrealtype^;
  1683. end;
  1684. end;
  1685. in_trunc_real :
  1686. begin
  1687. if left.nodetype in [ordconstn,realconstn] then
  1688. begin
  1689. vr:=getconstrealvalue;
  1690. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1691. begin
  1692. CGMessage(parser_e_range_check_error);
  1693. result:=cordconstnode.create(1,s64inttype,false)
  1694. end
  1695. else
  1696. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  1697. end
  1698. else
  1699. begin
  1700. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1701. inserttypeconv(left,pbestrealtype^);
  1702. resulttype:=s64inttype;
  1703. end;
  1704. end;
  1705. in_round_real :
  1706. begin
  1707. if left.nodetype in [ordconstn,realconstn] then
  1708. begin
  1709. vr:=getconstrealvalue;
  1710. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1711. begin
  1712. CGMessage(parser_e_range_check_error);
  1713. result:=cordconstnode.create(1,s64inttype,false)
  1714. end
  1715. else
  1716. result:=cordconstnode.create(round(vr),s64inttype,true)
  1717. end
  1718. else
  1719. begin
  1720. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1721. inserttypeconv(left,pbestrealtype^);
  1722. resulttype:=s64inttype;
  1723. end;
  1724. end;
  1725. in_frac_real :
  1726. begin
  1727. if left.nodetype in [ordconstn,realconstn] then
  1728. setconstrealvalue(frac(getconstrealvalue))
  1729. else
  1730. begin
  1731. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1732. inserttypeconv(left,pbestrealtype^);
  1733. resulttype:=pbestrealtype^;
  1734. end;
  1735. end;
  1736. in_int_real :
  1737. begin
  1738. if left.nodetype in [ordconstn,realconstn] then
  1739. setconstrealvalue(int(getconstrealvalue))
  1740. else
  1741. begin
  1742. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1743. inserttypeconv(left,pbestrealtype^);
  1744. resulttype:=pbestrealtype^;
  1745. end;
  1746. end;
  1747. in_pi_real :
  1748. begin
  1749. if block_type=bt_const then
  1750. setconstrealvalue(getpi)
  1751. else
  1752. resulttype:=pbestrealtype^;
  1753. end;
  1754. in_cos_real :
  1755. begin
  1756. if left.nodetype in [ordconstn,realconstn] then
  1757. setconstrealvalue(cos(getconstrealvalue))
  1758. else
  1759. begin
  1760. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1761. inserttypeconv(left,pbestrealtype^);
  1762. resulttype:=pbestrealtype^;
  1763. end;
  1764. end;
  1765. in_sin_real :
  1766. begin
  1767. if left.nodetype in [ordconstn,realconstn] then
  1768. setconstrealvalue(sin(getconstrealvalue))
  1769. else
  1770. begin
  1771. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1772. inserttypeconv(left,pbestrealtype^);
  1773. resulttype:=pbestrealtype^;
  1774. end;
  1775. end;
  1776. in_arctan_real :
  1777. begin
  1778. if left.nodetype in [ordconstn,realconstn] then
  1779. setconstrealvalue(arctan(getconstrealvalue))
  1780. else
  1781. begin
  1782. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1783. inserttypeconv(left,pbestrealtype^);
  1784. resulttype:=pbestrealtype^;
  1785. end;
  1786. end;
  1787. in_abs_real :
  1788. begin
  1789. if left.nodetype in [ordconstn,realconstn] then
  1790. setconstrealvalue(abs(getconstrealvalue))
  1791. else
  1792. begin
  1793. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1794. inserttypeconv(left,pbestrealtype^);
  1795. resulttype:=pbestrealtype^;
  1796. end;
  1797. end;
  1798. in_sqr_real :
  1799. begin
  1800. if left.nodetype in [ordconstn,realconstn] then
  1801. setconstrealvalue(sqr(getconstrealvalue))
  1802. else
  1803. begin
  1804. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1805. setfloatresulttype;
  1806. end;
  1807. end;
  1808. in_sqrt_real :
  1809. begin
  1810. if left.nodetype in [ordconstn,realconstn] then
  1811. begin
  1812. vr:=getconstrealvalue;
  1813. if vr<0.0 then
  1814. result:=handle_sqrt_const(vr)
  1815. else
  1816. setconstrealvalue(sqrt(vr));
  1817. end
  1818. else
  1819. begin
  1820. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1821. setfloatresulttype;
  1822. end;
  1823. end;
  1824. in_ln_real :
  1825. begin
  1826. if left.nodetype in [ordconstn,realconstn] then
  1827. begin
  1828. vr:=getconstrealvalue;
  1829. if vr<=0.0 then
  1830. result:=handle_ln_const(vr)
  1831. else
  1832. setconstrealvalue(ln(vr));
  1833. end
  1834. else
  1835. begin
  1836. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1837. inserttypeconv(left,pbestrealtype^);
  1838. resulttype:=pbestrealtype^;
  1839. end;
  1840. end;
  1841. {$ifdef SUPPORT_MMX}
  1842. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  1843. begin
  1844. end;
  1845. {$endif SUPPORT_MMX}
  1846. in_prefetch_var:
  1847. begin
  1848. resulttype:=voidtype;
  1849. end;
  1850. in_assert_x_y :
  1851. begin
  1852. resulttype:=voidtype;
  1853. if assigned(left) then
  1854. begin
  1855. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  1856. { check type }
  1857. if is_boolean(left.resulttype.def) then
  1858. begin
  1859. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1860. { must always be a string }
  1861. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  1862. end
  1863. else
  1864. CGMessage1(type_e_boolean_expr_expected,left.resulttype.def.typename);
  1865. end
  1866. else
  1867. CGMessage(type_e_mismatch);
  1868. { We've checked the whole statement for correctness, now we
  1869. can remove it if assertions are off }
  1870. if not(cs_do_assertion in aktlocalswitches) then
  1871. begin
  1872. { we need a valid node, so insert a nothingn }
  1873. result:=cnothingnode.create;
  1874. end
  1875. else
  1876. include(current_procinfo.flags,pi_do_call);
  1877. end;
  1878. else
  1879. internalerror(8);
  1880. end;
  1881. end;
  1882. myexit:
  1883. { Run get_paratype again to update maybe inserted typeconvs }
  1884. if not codegenerror then
  1885. begin
  1886. if assigned(left) and
  1887. (left.nodetype=callparan) then
  1888. tcallparanode(left).get_paratype;
  1889. end;
  1890. dec(parsing_para_level);
  1891. end;
  1892. function tinlinenode.pass_1 : tnode;
  1893. var
  1894. hp,hpp : tnode;
  1895. shiftconst: longint;
  1896. tempnode: ttempcreatenode;
  1897. newstatement: tstatementnode;
  1898. newblock: tblocknode;
  1899. begin
  1900. result:=nil;
  1901. { if we handle writeln; left contains no valid address }
  1902. if assigned(left) then
  1903. begin
  1904. if left.nodetype=callparan then
  1905. tcallparanode(left).firstcallparan
  1906. else
  1907. firstpass(left);
  1908. left_max;
  1909. end;
  1910. inc(parsing_para_level);
  1911. { intern const should already be handled }
  1912. if nf_inlineconst in flags then
  1913. internalerror(200104044);
  1914. case inlinenumber of
  1915. in_lo_qword,
  1916. in_hi_qword,
  1917. in_lo_long,
  1918. in_hi_long,
  1919. in_lo_word,
  1920. in_hi_word:
  1921. begin
  1922. shiftconst := 0;
  1923. case inlinenumber of
  1924. in_hi_qword:
  1925. shiftconst := 32;
  1926. in_hi_long:
  1927. shiftconst := 16;
  1928. in_hi_word:
  1929. shiftconst := 8;
  1930. end;
  1931. if shiftconst <> 0 then
  1932. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  1933. cordconstnode.create(shiftconst,u32inttype,false)),resulttype)
  1934. else
  1935. result := ctypeconvnode.create_internal(left,resulttype);
  1936. left := nil;
  1937. firstpass(result);
  1938. end;
  1939. in_sizeof_x:
  1940. begin
  1941. if registersint<1 then
  1942. registersint:=1;
  1943. expectloc:=LOC_REGISTER;
  1944. end;
  1945. in_typeof_x:
  1946. begin
  1947. if registersint<1 then
  1948. registersint:=1;
  1949. expectloc:=LOC_REGISTER;
  1950. end;
  1951. in_length_x:
  1952. begin
  1953. if is_shortstring(left.resulttype.def) then
  1954. expectloc:=left.expectloc
  1955. else
  1956. begin
  1957. { ansi/wide string }
  1958. if registersint<1 then
  1959. registersint:=1;
  1960. expectloc:=LOC_REGISTER;
  1961. end;
  1962. end;
  1963. in_typeinfo_x:
  1964. begin
  1965. expectloc:=LOC_REGISTER;
  1966. registersint:=1;
  1967. end;
  1968. in_assigned_x:
  1969. begin
  1970. expectloc := LOC_JUMP;
  1971. registersint:=1;
  1972. end;
  1973. in_pred_x,
  1974. in_succ_x:
  1975. begin
  1976. if is_64bit(resulttype.def) then
  1977. begin
  1978. if (registersint<2) then
  1979. registersint:=2
  1980. end
  1981. else
  1982. begin
  1983. if (registersint<1) then
  1984. registersint:=1;
  1985. end;
  1986. expectloc:=LOC_REGISTER;
  1987. end;
  1988. in_setlength_x,
  1989. in_initialize_x,
  1990. in_finalize_x:
  1991. begin
  1992. expectloc:=LOC_VOID;
  1993. end;
  1994. in_inc_x,
  1995. in_dec_x:
  1996. begin
  1997. expectloc:=LOC_VOID;
  1998. { check type }
  1999. if
  2000. {$ifndef cpu64bit}
  2001. is_64bit(left.resulttype.def) or
  2002. {$endif cpu64bit}
  2003. { range/overflow checking doesn't work properly }
  2004. { with the inc/dec code that's generated (JM) }
  2005. (
  2006. (((left.resulttype.def.deftype = orddef) and
  2007. not(is_char(left.resulttype.def)) and
  2008. not(is_boolean(left.resulttype.def))) or
  2009. (left.resulttype.def.deftype = pointerdef)) and
  2010. (aktlocalswitches * [cs_check_overflow,cs_check_range] <> [])
  2011. ) then
  2012. { convert to simple add (JM) }
  2013. begin
  2014. newblock := internalstatements(newstatement);
  2015. { extra parameter? }
  2016. if assigned(tcallparanode(left).right) then
  2017. begin
  2018. { Yes, use for add node }
  2019. hpp := tcallparanode(tcallparanode(left).right).left;
  2020. tcallparanode(tcallparanode(left).right).left := nil;
  2021. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2022. CGMessage(parser_e_illegal_expression);
  2023. end
  2024. else
  2025. begin
  2026. { no, create constant 1 }
  2027. hpp := cordconstnode.create(1,tcallparanode(left).left.resulttype,false);
  2028. end;
  2029. resulttypepass(hpp);
  2030. {$ifndef cpu64bit}
  2031. if not((hpp.resulttype.def.deftype=orddef) and
  2032. (torddef(hpp.resulttype.def).typ<>u32bit)) then
  2033. {$endif cpu64bit}
  2034. inserttypeconv_internal(hpp,sinttype);
  2035. { No overflow check for pointer operations, because inc(pointer,-1) will always
  2036. trigger an overflow. For uint32 it works because then the operation is done
  2037. in 64bit }
  2038. if (tcallparanode(left).left.resulttype.def.deftype=pointerdef) then
  2039. exclude(aktlocalswitches,cs_check_overflow);
  2040. { make sure we don't call functions part of the left node twice (and generally }
  2041. { optimize the code generation) }
  2042. if node_complexity(tcallparanode(left).left) > 1 then
  2043. begin
  2044. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.def.size,tt_persistent,true);
  2045. addstatement(newstatement,tempnode);
  2046. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2047. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  2048. hp := cderefnode.create(ctemprefnode.create(tempnode));
  2049. inserttypeconv_internal(hp,tcallparanode(left).left.resulttype);
  2050. end
  2051. else
  2052. begin
  2053. hp := tcallparanode(left).left.getcopy;
  2054. tempnode := nil;
  2055. end;
  2056. { addition/substraction depending on inc/dec }
  2057. if inlinenumber = in_inc_x then
  2058. hpp := caddnode.create(addn,hp,hpp)
  2059. else
  2060. hpp := caddnode.create(subn,hp,hpp);
  2061. { assign result of addition }
  2062. inserttypeconv_internal(hpp,hp.resulttype);
  2063. addstatement(newstatement,cassignmentnode.create(hp.getcopy,hpp));
  2064. { deallocate the temp }
  2065. if assigned(tempnode) then
  2066. addstatement(newstatement,ctempdeletenode.create(tempnode));
  2067. { firstpass it }
  2068. firstpass(newblock);
  2069. { return new node }
  2070. result := newblock;
  2071. end
  2072. else if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  2073. is_ordinal(left.resulttype.def) then
  2074. begin
  2075. { two paras ? }
  2076. if assigned(tcallparanode(left).right) then
  2077. begin
  2078. { need we an additional register ? }
  2079. if not(is_constintnode(tcallparanode(tcallparanode(left).right).left)) and
  2080. (tcallparanode(tcallparanode(left).right).left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) and
  2081. (tcallparanode(tcallparanode(left).right).left.registersint<=1) then
  2082. inc(registersint);
  2083. { do we need an additional register to restore the first parameter? }
  2084. if tcallparanode(tcallparanode(left).right).left.registersint>=registersint then
  2085. inc(registersint);
  2086. end;
  2087. end;
  2088. end;
  2089. in_include_x_y,
  2090. in_exclude_x_y:
  2091. begin
  2092. expectloc:=LOC_VOID;
  2093. registersint:=left.registersint;
  2094. registersfpu:=left.registersfpu;
  2095. {$ifdef SUPPORT_MMX}
  2096. registersmmx:=left.registersmmx;
  2097. {$endif SUPPORT_MMX}
  2098. end;
  2099. in_exp_real:
  2100. begin
  2101. result:= first_exp_real;
  2102. end;
  2103. in_round_real:
  2104. begin
  2105. result:= first_round_real;
  2106. end;
  2107. in_trunc_real:
  2108. begin
  2109. result:= first_trunc_real;
  2110. end;
  2111. in_int_real:
  2112. begin
  2113. result:= first_int_real;
  2114. end;
  2115. in_frac_real:
  2116. begin
  2117. result:= first_frac_real;
  2118. end;
  2119. in_cos_real:
  2120. begin
  2121. result:= first_cos_real;
  2122. end;
  2123. in_sin_real:
  2124. begin
  2125. result := first_sin_real;
  2126. end;
  2127. in_arctan_real:
  2128. begin
  2129. result := first_arctan_real;
  2130. end;
  2131. in_pi_real :
  2132. begin
  2133. result := first_pi;
  2134. end;
  2135. in_abs_real:
  2136. begin
  2137. result := first_abs_real;
  2138. end;
  2139. in_sqr_real:
  2140. begin
  2141. result := first_sqr_real;
  2142. end;
  2143. in_sqrt_real:
  2144. begin
  2145. result := first_sqrt_real;
  2146. end;
  2147. in_ln_real:
  2148. begin
  2149. result := first_ln_real;
  2150. end;
  2151. {$ifdef SUPPORT_MMX}
  2152. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2153. begin
  2154. end;
  2155. {$endif SUPPORT_MMX}
  2156. in_assert_x_y :
  2157. begin
  2158. expectloc:=LOC_VOID;
  2159. registersint:=left.registersint;
  2160. registersfpu:=left.registersfpu;
  2161. {$ifdef SUPPORT_MMX}
  2162. registersmmx:=left.registersmmx;
  2163. {$endif SUPPORT_MMX}
  2164. end;
  2165. in_low_x,
  2166. in_high_x:
  2167. internalerror(200104047);
  2168. in_slice_x:
  2169. internalerror(2005101501);
  2170. in_ord_x,
  2171. in_chr_byte:
  2172. begin
  2173. { should not happend as it's converted to typeconv }
  2174. internalerror(200104045);
  2175. end;
  2176. in_ofs_x :
  2177. internalerror(2000101001);
  2178. in_seg_x :
  2179. internalerror(200104046);
  2180. in_settextbuf_file_x,
  2181. in_reset_typedfile,
  2182. in_rewrite_typedfile,
  2183. in_str_x_string,
  2184. in_val_x,
  2185. in_read_x,
  2186. in_readln_x,
  2187. in_write_x,
  2188. in_writeln_x :
  2189. begin
  2190. { should be handled by det_resulttype }
  2191. internalerror(200108234);
  2192. end;
  2193. in_prefetch_var:
  2194. begin
  2195. expectloc:=LOC_VOID;
  2196. end;
  2197. else
  2198. internalerror(8);
  2199. end;
  2200. dec(parsing_para_level);
  2201. end;
  2202. {$ifdef fpc}
  2203. {$maxfpuregisters default}
  2204. {$endif fpc}
  2205. function tinlinenode.docompare(p: tnode): boolean;
  2206. begin
  2207. docompare :=
  2208. inherited docompare(p) and
  2209. (inlinenumber = tinlinenode(p).inlinenumber);
  2210. end;
  2211. function tinlinenode.first_pi : tnode;
  2212. begin
  2213. result:=crealconstnode.create(getpi,pbestrealtype^);
  2214. end;
  2215. function tinlinenode.first_arctan_real : tnode;
  2216. begin
  2217. { create the call to the helper }
  2218. { on entry left node contains the parameter }
  2219. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  2220. ccallparanode.create(left,nil));
  2221. left := nil;
  2222. end;
  2223. function tinlinenode.first_abs_real : tnode;
  2224. begin
  2225. { create the call to the helper }
  2226. { on entry left node contains the parameter }
  2227. first_abs_real := ccallnode.createintern('fpc_abs_real',
  2228. ccallparanode.create(left,nil));
  2229. left := nil;
  2230. end;
  2231. function tinlinenode.first_sqr_real : tnode;
  2232. begin
  2233. { create the call to the helper }
  2234. { on entry left node contains the parameter }
  2235. first_sqr_real := ctypeconvnode.create_internal(ccallnode.createintern('fpc_sqr_real',
  2236. ccallparanode.create(left,nil)),resulttype);
  2237. left := nil;
  2238. end;
  2239. function tinlinenode.first_sqrt_real : tnode;
  2240. begin
  2241. { create the call to the helper }
  2242. { on entry left node contains the parameter }
  2243. first_sqrt_real := ctypeconvnode.create_internal(ccallnode.createintern('fpc_sqrt_real',
  2244. ccallparanode.create(left,nil)),resulttype);
  2245. left := nil;
  2246. end;
  2247. function tinlinenode.first_ln_real : tnode;
  2248. begin
  2249. { create the call to the helper }
  2250. { on entry left node contains the parameter }
  2251. first_ln_real := ccallnode.createintern('fpc_ln_real',
  2252. ccallparanode.create(left,nil));
  2253. left := nil;
  2254. end;
  2255. function tinlinenode.first_cos_real : tnode;
  2256. begin
  2257. { create the call to the helper }
  2258. { on entry left node contains the parameter }
  2259. first_cos_real := ccallnode.createintern('fpc_cos_real',
  2260. ccallparanode.create(left,nil));
  2261. left := nil;
  2262. end;
  2263. function tinlinenode.first_sin_real : tnode;
  2264. begin
  2265. { create the call to the helper }
  2266. { on entry left node contains the parameter }
  2267. first_sin_real := ccallnode.createintern('fpc_sin_real',
  2268. ccallparanode.create(left,nil));
  2269. left := nil;
  2270. end;
  2271. function tinlinenode.first_exp_real : tnode;
  2272. begin
  2273. { create the call to the helper }
  2274. { on entry left node contains the parameter }
  2275. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  2276. left := nil;
  2277. end;
  2278. function tinlinenode.first_int_real : tnode;
  2279. begin
  2280. { create the call to the helper }
  2281. { on entry left node contains the parameter }
  2282. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  2283. left := nil;
  2284. end;
  2285. function tinlinenode.first_frac_real : tnode;
  2286. begin
  2287. { create the call to the helper }
  2288. { on entry left node contains the parameter }
  2289. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  2290. left := nil;
  2291. end;
  2292. function tinlinenode.first_round_real : tnode;
  2293. begin
  2294. { create the call to the helper }
  2295. { on entry left node contains the parameter }
  2296. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  2297. left := nil;
  2298. end;
  2299. function tinlinenode.first_trunc_real : tnode;
  2300. begin
  2301. { create the call to the helper }
  2302. { on entry left node contains the parameter }
  2303. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  2304. left := nil;
  2305. end;
  2306. begin
  2307. cinlinenode:=tinlinenode;
  2308. end.