ninl.pas 97 KB

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