ninl.pas 97 KB

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