ninl.pas 96 KB

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