ninl.pas 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559
  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 tinlinenode.det_resulttype:tnode;
  950. function do_lowhigh(const t:ttype) : tnode;
  951. var
  952. v : tconstexprint;
  953. enum : tenumsym;
  954. hp : tnode;
  955. begin
  956. case t.def.deftype of
  957. orddef:
  958. begin
  959. if inlinenumber=in_low_x then
  960. v:=torddef(t.def).low
  961. else
  962. v:=torddef(t.def).high;
  963. { low/high of torddef are longints, so we need special }
  964. { handling for cardinal and 64bit types (JM) }
  965. { 1.0.x doesn't support int64($ffffffff) correct, it'll expand
  966. to -1 instead of staying $ffffffff. Therefor we use $ffff with
  967. shl twice (PFV) }
  968. case torddef(t.def).typ of
  969. s64bit,scurrency :
  970. begin
  971. if (inlinenumber=in_low_x) then
  972. v := int64($80000000) shl 32
  973. else
  974. v := (int64($7fffffff) shl 32) or int64($ffff) shl 16 or int64($ffff)
  975. end;
  976. u64bit :
  977. begin
  978. { we have to use a dirty trick for high(qword), }
  979. { because it's bigger than high(tconstexprint) (JM) }
  980. v := 0
  981. end
  982. else
  983. begin
  984. if not is_signed(t.def) then
  985. v := cardinal(v);
  986. end;
  987. end;
  988. hp:=cordconstnode.create(v,t,true);
  989. resulttypepass(hp);
  990. { fix high(qword) }
  991. if (torddef(t.def).typ=u64bit) and
  992. (inlinenumber = in_high_x) then
  993. tordconstnode(hp).value := -1; { is the same as qword($ffffffffffffffff) }
  994. do_lowhigh:=hp;
  995. end;
  996. enumdef:
  997. begin
  998. enum:=tenumsym(tenumdef(t.def).firstenum);
  999. v:=tenumdef(t.def).maxval;
  1000. if inlinenumber=in_high_x then
  1001. while assigned(enum) and (enum.value <> v) do
  1002. enum:=enum.nextenum;
  1003. if not assigned(enum) then
  1004. internalerror(309993)
  1005. else
  1006. hp:=genenumnode(enum);
  1007. do_lowhigh:=hp;
  1008. end;
  1009. else
  1010. internalerror(87);
  1011. end;
  1012. end;
  1013. function getconstrealvalue : bestreal;
  1014. begin
  1015. case left.nodetype of
  1016. ordconstn:
  1017. getconstrealvalue:=tordconstnode(left).value;
  1018. realconstn:
  1019. getconstrealvalue:=trealconstnode(left).value_real;
  1020. else
  1021. internalerror(309992);
  1022. end;
  1023. end;
  1024. procedure setconstrealvalue(r : bestreal);
  1025. begin
  1026. result:=crealconstnode.create(r,pbestrealtype^);
  1027. end;
  1028. function handle_ln_const(r : bestreal) : tnode;
  1029. begin
  1030. if r<=0.0 then
  1031. if (cs_check_range in aktlocalswitches) or
  1032. (cs_check_overflow in aktlocalswitches) then
  1033. begin
  1034. result:=crealconstnode.create(0,pbestrealtype^);
  1035. CGMessage(type_e_wrong_math_argument)
  1036. end
  1037. else
  1038. begin
  1039. if r=0.0 then
  1040. result:=crealconstnode.create(double(MathQNaN),pbestrealtype^)
  1041. else
  1042. result:=crealconstnode.create(double(MathNegInf),pbestrealtype^)
  1043. end
  1044. else
  1045. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1046. end;
  1047. function handle_sqrt_const(r : bestreal) : tnode;
  1048. begin
  1049. if r<0.0 then
  1050. if (cs_check_range in aktlocalswitches) or
  1051. (cs_check_overflow in aktlocalswitches) then
  1052. begin
  1053. result:=crealconstnode.create(0,pbestrealtype^);
  1054. CGMessage(type_e_wrong_math_argument)
  1055. end
  1056. else
  1057. result:=crealconstnode.create(double(MathQNaN),pbestrealtype^)
  1058. else
  1059. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1060. end;
  1061. var
  1062. vl,vl2 : TConstExprInt;
  1063. vr : bestreal;
  1064. hightree,
  1065. hp : tnode;
  1066. srsym : tsym;
  1067. checkrange : boolean;
  1068. label
  1069. myexit;
  1070. begin
  1071. result:=nil;
  1072. { if we handle writeln; left contains no valid address }
  1073. if assigned(left) then
  1074. begin
  1075. if left.nodetype=callparan then
  1076. tcallparanode(left).get_paratype
  1077. else
  1078. resulttypepass(left);
  1079. end;
  1080. inc(parsing_para_level);
  1081. { handle intern constant functions in separate case }
  1082. if nf_inlineconst in flags then
  1083. begin
  1084. { no parameters? }
  1085. if not assigned(left) then
  1086. internalerror(200501231)
  1087. else
  1088. begin
  1089. vl:=0;
  1090. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1091. case left.nodetype of
  1092. realconstn :
  1093. begin
  1094. { Real functions are all handled with internproc below }
  1095. CGMessage1(type_e_integer_expr_expected,left.resulttype.def.typename)
  1096. end;
  1097. ordconstn :
  1098. vl:=tordconstnode(left).value;
  1099. callparan :
  1100. begin
  1101. { both exists, else it was not generated }
  1102. vl:=tordconstnode(tcallparanode(left).left).value;
  1103. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1104. end;
  1105. else
  1106. CGMessage(parser_e_illegal_expression);
  1107. end;
  1108. case inlinenumber of
  1109. in_const_abs :
  1110. hp:=genintconstnode(abs(vl));
  1111. in_const_sqr :
  1112. hp:=genintconstnode(sqr(vl));
  1113. in_const_odd :
  1114. hp:=cordconstnode.create(byte(odd(vl)),booltype,true);
  1115. in_const_swap_word :
  1116. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resulttype,true);
  1117. in_const_swap_long :
  1118. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resulttype,true);
  1119. in_const_swap_qword :
  1120. hp:=cordconstnode.create((vl and $ffff) shl 32+(vl shr 32),left.resulttype,true);
  1121. in_const_ptr :
  1122. hp:=cpointerconstnode.create((vl2 shl 4)+vl,voidfarpointertype);
  1123. else
  1124. internalerror(88);
  1125. end;
  1126. end;
  1127. if hp=nil then
  1128. hp:=terrornode.create;
  1129. result:=hp;
  1130. goto myexit;
  1131. end
  1132. else
  1133. begin
  1134. case inlinenumber of
  1135. in_lo_long,
  1136. in_hi_long,
  1137. in_lo_qword,
  1138. in_hi_qword,
  1139. in_lo_word,
  1140. in_hi_word :
  1141. begin
  1142. { give warning for incompatibility with tp and delphi }
  1143. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  1144. ((m_tp7 in aktmodeswitches) or
  1145. (m_delphi in aktmodeswitches)) then
  1146. CGMessage(type_w_maybe_wrong_hi_lo);
  1147. { constant folding }
  1148. if left.nodetype=ordconstn then
  1149. begin
  1150. case inlinenumber of
  1151. in_lo_word :
  1152. hp:=cordconstnode.create(tordconstnode(left).value and $ff,left.resulttype,true);
  1153. in_hi_word :
  1154. hp:=cordconstnode.create(tordconstnode(left).value shr 8,left.resulttype,true);
  1155. in_lo_long :
  1156. hp:=cordconstnode.create(tordconstnode(left).value and $ffff,left.resulttype,true);
  1157. in_hi_long :
  1158. hp:=cordconstnode.create(tordconstnode(left).value shr 16,left.resulttype,true);
  1159. in_lo_qword :
  1160. hp:=cordconstnode.create(tordconstnode(left).value and $ffffffff,left.resulttype,true);
  1161. in_hi_qword :
  1162. hp:=cordconstnode.create(tordconstnode(left).value shr 32,left.resulttype,true);
  1163. end;
  1164. result:=hp;
  1165. goto myexit;
  1166. end;
  1167. set_varstate(left,vs_used,true);
  1168. if not is_integer(left.resulttype.def) then
  1169. CGMessage1(type_e_integer_expr_expected,left.resulttype.def.typename);
  1170. case inlinenumber of
  1171. in_lo_word,
  1172. in_hi_word :
  1173. resulttype:=u8inttype;
  1174. in_lo_long,
  1175. in_hi_long :
  1176. resulttype:=u16inttype;
  1177. in_lo_qword,
  1178. in_hi_qword :
  1179. resulttype:=u32inttype;
  1180. end;
  1181. end;
  1182. in_sizeof_x:
  1183. begin
  1184. set_varstate(left,vs_used,false);
  1185. if paramanager.push_high_param(vs_value,left.resulttype.def,current_procinfo.procdef.proccalloption) then
  1186. begin
  1187. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1188. if assigned(hightree) then
  1189. begin
  1190. hp:=caddnode.create(addn,hightree,
  1191. cordconstnode.create(1,sinttype,false));
  1192. if (left.resulttype.def.deftype=arraydef) and
  1193. (tarraydef(left.resulttype.def).elesize<>1) then
  1194. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1195. left.resulttype.def).elesize,sinttype,true));
  1196. result:=hp;
  1197. end;
  1198. end
  1199. else
  1200. resulttype:=sinttype;
  1201. end;
  1202. in_typeof_x:
  1203. begin
  1204. set_varstate(left,vs_used,false);
  1205. resulttype:=voidpointertype;
  1206. end;
  1207. in_ord_x:
  1208. begin
  1209. if (left.nodetype=ordconstn) then
  1210. begin
  1211. hp:=cordconstnode.create(
  1212. tordconstnode(left).value,sinttype,true);
  1213. result:=hp;
  1214. goto myexit;
  1215. end;
  1216. set_varstate(left,vs_used,true);
  1217. case left.resulttype.def.deftype of
  1218. orddef :
  1219. begin
  1220. case torddef(left.resulttype.def).typ of
  1221. bool8bit,
  1222. uchar:
  1223. begin
  1224. { change to byte() }
  1225. hp:=ctypeconvnode.create_internal(left,u8inttype);
  1226. left:=nil;
  1227. result:=hp;
  1228. end;
  1229. bool16bit,
  1230. uwidechar :
  1231. begin
  1232. { change to word() }
  1233. hp:=ctypeconvnode.create_internal(left,u16inttype);
  1234. left:=nil;
  1235. result:=hp;
  1236. end;
  1237. bool32bit :
  1238. begin
  1239. { change to dword() }
  1240. hp:=ctypeconvnode.create_internal(left,u32inttype);
  1241. left:=nil;
  1242. result:=hp;
  1243. end;
  1244. uvoid :
  1245. CGMessage1(type_e_ordinal_expr_expected,left.resulttype.def.typename);
  1246. else
  1247. begin
  1248. { all other orddef need no transformation }
  1249. hp:=left;
  1250. left:=nil;
  1251. result:=hp;
  1252. end;
  1253. end;
  1254. end;
  1255. enumdef :
  1256. begin
  1257. hp:=ctypeconvnode.create_internal(left,s32inttype);
  1258. left:=nil;
  1259. result:=hp;
  1260. end;
  1261. pointerdef :
  1262. begin
  1263. if m_mac in aktmodeswitches then
  1264. begin
  1265. hp:=ctypeconvnode.create_internal(left,ptrinttype);
  1266. left:=nil;
  1267. result:=hp;
  1268. end
  1269. else
  1270. CGMessage1(type_e_ordinal_expr_expected,left.resulttype.def.typename);
  1271. end
  1272. else
  1273. CGMessage1(type_e_ordinal_expr_expected,left.resulttype.def.typename);
  1274. end;
  1275. end;
  1276. in_chr_byte:
  1277. begin
  1278. { convert to explicit char() }
  1279. set_varstate(left,vs_used,true);
  1280. hp:=ctypeconvnode.create_internal(left,cchartype);
  1281. left:=nil;
  1282. result:=hp;
  1283. end;
  1284. in_length_x:
  1285. begin
  1286. set_varstate(left,vs_used,true);
  1287. case left.resulttype.def.deftype of
  1288. stringdef :
  1289. begin
  1290. { we don't need string convertions here }
  1291. if (left.nodetype=typeconvn) and
  1292. (ttypeconvnode(left).left.resulttype.def.deftype=stringdef) then
  1293. begin
  1294. hp:=ttypeconvnode(left).left;
  1295. ttypeconvnode(left).left:=nil;
  1296. left.free;
  1297. left:=hp;
  1298. end;
  1299. { evaluates length of constant strings direct }
  1300. if (left.nodetype=stringconstn) then
  1301. begin
  1302. hp:=cordconstnode.create(
  1303. tstringconstnode(left).len,s32inttype,true);
  1304. result:=hp;
  1305. goto myexit;
  1306. end;
  1307. end;
  1308. orddef :
  1309. begin
  1310. { length of char is one allways }
  1311. if is_char(left.resulttype.def) or
  1312. is_widechar(left.resulttype.def) then
  1313. begin
  1314. hp:=cordconstnode.create(1,s32inttype,false);
  1315. result:=hp;
  1316. goto myexit;
  1317. end
  1318. else
  1319. CGMessage(type_e_mismatch);
  1320. end;
  1321. pointerdef :
  1322. begin
  1323. if is_pchar(left.resulttype.def) then
  1324. begin
  1325. hp := ccallparanode.create(left,nil);
  1326. result := ccallnode.createintern('fpc_pchar_length',hp);
  1327. { make sure the left node doesn't get disposed, since it's }
  1328. { reused in the new node (JM) }
  1329. left:=nil;
  1330. goto myexit;
  1331. end
  1332. else if is_pwidechar(left.resulttype.def) then
  1333. begin
  1334. hp := ccallparanode.create(left,nil);
  1335. result := ccallnode.createintern('fpc_pwidechar_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
  1342. CGMessage(type_e_mismatch);
  1343. end;
  1344. arraydef :
  1345. begin
  1346. if is_open_array(left.resulttype.def) or
  1347. is_array_of_const(left.resulttype.def) then
  1348. begin
  1349. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1350. if assigned(hightree) then
  1351. begin
  1352. hp:=caddnode.create(addn,hightree,
  1353. cordconstnode.create(1,s32inttype,false));
  1354. result:=hp;
  1355. end;
  1356. goto myexit;
  1357. end
  1358. else
  1359. if not is_dynamic_array(left.resulttype.def) then
  1360. begin
  1361. hp:=cordconstnode.create(tarraydef(left.resulttype.def).highrange-
  1362. tarraydef(left.resulttype.def).lowrange+1,
  1363. s32inttype,true);
  1364. result:=hp;
  1365. goto myexit;
  1366. end
  1367. else
  1368. begin
  1369. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1370. result := ccallnode.createintern('fpc_dynarray_length',hp);
  1371. { make sure the left node doesn't get disposed, since it's }
  1372. { reused in the new node (JM) }
  1373. left:=nil;
  1374. goto myexit;
  1375. end;
  1376. end;
  1377. else
  1378. CGMessage(type_e_mismatch);
  1379. end;
  1380. { shortstring return an 8 bit value as the length
  1381. is the first byte of the string }
  1382. if is_shortstring(left.resulttype.def) then
  1383. resulttype:=u8inttype
  1384. else
  1385. resulttype:=sinttype;
  1386. end;
  1387. in_typeinfo_x:
  1388. begin
  1389. set_varstate(left,vs_used,true);
  1390. resulttype:=voidpointertype;
  1391. end;
  1392. in_assigned_x:
  1393. begin
  1394. { the parser has already made sure the expression is valid }
  1395. { handle constant expressions }
  1396. if is_constnode(tcallparanode(left).left) or
  1397. (tcallparanode(left).left.nodetype = pointerconstn) then
  1398. begin
  1399. { let an add node figure it out }
  1400. result := caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  1401. tcallparanode(left).left := nil;
  1402. { free left, because otherwise some code at 'myexit' tries }
  1403. { to run get_paratype for it, which crashes since left.left }
  1404. { is now nil }
  1405. left.free;
  1406. left := nil;
  1407. goto myexit;
  1408. end;
  1409. { otherwise handle separately, because there could be a procvar, which }
  1410. { is 2*sizeof(pointer), while we must only check the first pointer }
  1411. set_varstate(tcallparanode(left).left,vs_used,true);
  1412. resulttype:=booltype;
  1413. end;
  1414. in_ofs_x :
  1415. internalerror(2000101001);
  1416. in_seg_x :
  1417. begin
  1418. set_varstate(left,vs_used,false);
  1419. result:=cordconstnode.create(0,s32inttype,false);
  1420. goto myexit;
  1421. end;
  1422. in_pred_x,
  1423. in_succ_x:
  1424. begin
  1425. set_varstate(left,vs_used,true);
  1426. resulttype:=left.resulttype;
  1427. if not is_ordinal(resulttype.def) then
  1428. CGMessage(type_e_ordinal_expr_expected)
  1429. else
  1430. begin
  1431. if (resulttype.def.deftype=enumdef) and
  1432. (tenumdef(resulttype.def).has_jumps) then
  1433. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  1434. end;
  1435. { only if the result is an enum do we do range checking }
  1436. if (resulttype.def.deftype=enumdef) then
  1437. checkrange := true
  1438. else
  1439. checkrange := false;
  1440. { do constant folding after check for jumps }
  1441. if left.nodetype=ordconstn then
  1442. begin
  1443. if inlinenumber=in_succ_x then
  1444. result:=cordconstnode.create(tordconstnode(left).value+1,left.resulttype,checkrange)
  1445. else
  1446. result:=cordconstnode.create(tordconstnode(left).value-1,left.resulttype,checkrange);
  1447. end;
  1448. end;
  1449. in_initialize_x,
  1450. in_finalize_x,
  1451. in_setlength_x:
  1452. begin
  1453. { inlined from pinline }
  1454. internalerror(200204231);
  1455. end;
  1456. in_inc_x,
  1457. in_dec_x:
  1458. begin
  1459. resulttype:=voidtype;
  1460. if assigned(left) then
  1461. begin
  1462. { first param must be var }
  1463. valid_for_var(tcallparanode(left).left);
  1464. set_varstate(tcallparanode(left).left,vs_used,true);
  1465. if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  1466. is_ordinal(left.resulttype.def) or
  1467. is_currency(left.resulttype.def) then
  1468. begin
  1469. { value of left gets changed -> must be unique }
  1470. set_unique(tcallparanode(left).left);
  1471. { two paras ? }
  1472. if assigned(tcallparanode(left).right) then
  1473. begin
  1474. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_used,true);
  1475. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resulttype);
  1476. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1477. CGMessage(parser_e_illegal_expression);
  1478. end;
  1479. end
  1480. else
  1481. CGMessage(type_e_ordinal_expr_expected);
  1482. end
  1483. else
  1484. CGMessage(type_e_mismatch);
  1485. end;
  1486. in_read_x,
  1487. in_readln_x,
  1488. in_write_x,
  1489. in_writeln_x :
  1490. begin
  1491. result := handle_read_write;
  1492. end;
  1493. in_settextbuf_file_x :
  1494. begin
  1495. resulttype:=voidtype;
  1496. { now we know the type of buffer }
  1497. srsym:=searchsymonlyin(systemunit,'SETTEXTBUF');
  1498. hp:=ccallparanode.create(cordconstnode.create(
  1499. tcallparanode(left).left.resulttype.def.size,s32inttype,true),left);
  1500. result:=ccallnode.create(hp,tprocsym(srsym),systemunit,nil,[]);
  1501. left:=nil;
  1502. end;
  1503. { the firstpass of the arg has been done in firstcalln ? }
  1504. in_reset_typedfile,
  1505. in_rewrite_typedfile :
  1506. begin
  1507. result := handle_reset_rewrite_typed;
  1508. end;
  1509. in_str_x_string :
  1510. begin
  1511. result := handle_str;
  1512. end;
  1513. in_val_x :
  1514. begin
  1515. result := handle_val;
  1516. end;
  1517. in_include_x_y,
  1518. in_exclude_x_y:
  1519. begin
  1520. resulttype:=voidtype;
  1521. { the parser already checks whether we have two (and exectly two) }
  1522. { parameters (JM) }
  1523. { first param must be var }
  1524. valid_for_var(tcallparanode(left).left);
  1525. set_varstate(tcallparanode(left).left,vs_used,true);
  1526. { check type }
  1527. if (left.resulttype.def.deftype=setdef) then
  1528. begin
  1529. { insert a type conversion }
  1530. { to the type of the set elements }
  1531. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_used,true);
  1532. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  1533. tsetdef(left.resulttype.def).elementtype);
  1534. end
  1535. else
  1536. CGMessage(type_e_mismatch);
  1537. end;
  1538. in_low_x,
  1539. in_high_x:
  1540. begin
  1541. set_varstate(left,vs_used,false);
  1542. case left.resulttype.def.deftype of
  1543. orddef,
  1544. enumdef:
  1545. begin
  1546. result:=do_lowhigh(left.resulttype);
  1547. end;
  1548. setdef:
  1549. begin
  1550. result:=do_lowhigh(tsetdef(left.resulttype.def).elementtype);
  1551. end;
  1552. arraydef:
  1553. begin
  1554. if inlinenumber=in_low_x then
  1555. begin
  1556. result:=cordconstnode.create(tarraydef(
  1557. left.resulttype.def).lowrange,tarraydef(left.resulttype.def).rangetype,true);
  1558. end
  1559. else
  1560. begin
  1561. if is_open_array(left.resulttype.def) or
  1562. is_array_of_const(left.resulttype.def) then
  1563. begin
  1564. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1565. end
  1566. else
  1567. if is_dynamic_array(left.resulttype.def) then
  1568. begin
  1569. { can't use inserttypeconv because we need }
  1570. { an explicit type conversion (JM) }
  1571. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1572. result := ccallnode.createintern('fpc_dynarray_high',hp);
  1573. { make sure the left node doesn't get disposed, since it's }
  1574. { reused in the new node (JM) }
  1575. left:=nil;
  1576. end
  1577. else
  1578. begin
  1579. result:=cordconstnode.create(tarraydef(
  1580. left.resulttype.def).highrange,tarraydef(left.resulttype.def).rangetype,true);
  1581. end;
  1582. end;
  1583. end;
  1584. stringdef:
  1585. begin
  1586. if inlinenumber=in_low_x then
  1587. begin
  1588. result:=cordconstnode.create(0,u8inttype,false);
  1589. end
  1590. else
  1591. begin
  1592. if is_open_string(left.resulttype.def) then
  1593. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  1594. else
  1595. result:=cordconstnode.create(tstringdef(left.resulttype.def).len,u8inttype,true);
  1596. end;
  1597. end;
  1598. else
  1599. CGMessage(type_e_mismatch);
  1600. end;
  1601. end;
  1602. in_exp_real :
  1603. begin
  1604. if left.nodetype in [ordconstn,realconstn] then
  1605. begin
  1606. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  1607. if (trealconstnode(result).value_real=double(MathInf)) and
  1608. ((cs_check_range in aktlocalswitches) or
  1609. (cs_check_overflow in aktlocalswitches)) then
  1610. begin
  1611. result:=crealconstnode.create(0,pbestrealtype^);
  1612. CGMessage(parser_e_range_check_error);
  1613. end;
  1614. end
  1615. else
  1616. begin
  1617. set_varstate(left,vs_used,true);
  1618. inserttypeconv(left,pbestrealtype^);
  1619. resulttype:=pbestrealtype^;
  1620. end;
  1621. end;
  1622. in_trunc_real :
  1623. begin
  1624. if left.nodetype in [ordconstn,realconstn] then
  1625. begin
  1626. vr:=getconstrealvalue;
  1627. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1628. begin
  1629. CGMessage(parser_e_range_check_error);
  1630. result:=cordconstnode.create(1,s64inttype,false)
  1631. end
  1632. else
  1633. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  1634. end
  1635. else
  1636. begin
  1637. set_varstate(left,vs_used,true);
  1638. inserttypeconv(left,pbestrealtype^);
  1639. resulttype:=s64inttype;
  1640. end;
  1641. end;
  1642. in_round_real :
  1643. begin
  1644. if left.nodetype in [ordconstn,realconstn] then
  1645. begin
  1646. vr:=getconstrealvalue;
  1647. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1648. begin
  1649. CGMessage(parser_e_range_check_error);
  1650. result:=cordconstnode.create(1,s64inttype,false)
  1651. end
  1652. else
  1653. result:=cordconstnode.create(round(vr),s64inttype,true)
  1654. end
  1655. else
  1656. begin
  1657. set_varstate(left,vs_used,true);
  1658. inserttypeconv(left,pbestrealtype^);
  1659. resulttype:=s64inttype;
  1660. end;
  1661. end;
  1662. in_frac_real :
  1663. begin
  1664. if left.nodetype in [ordconstn,realconstn] then
  1665. setconstrealvalue(frac(getconstrealvalue))
  1666. else
  1667. begin
  1668. set_varstate(left,vs_used,true);
  1669. inserttypeconv(left,pbestrealtype^);
  1670. resulttype:=pbestrealtype^;
  1671. end;
  1672. end;
  1673. in_int_real :
  1674. begin
  1675. if left.nodetype in [ordconstn,realconstn] then
  1676. setconstrealvalue(int(getconstrealvalue))
  1677. else
  1678. begin
  1679. set_varstate(left,vs_used,true);
  1680. inserttypeconv(left,pbestrealtype^);
  1681. resulttype:=pbestrealtype^;
  1682. end;
  1683. end;
  1684. in_pi_real :
  1685. begin
  1686. if block_type=bt_const then
  1687. setconstrealvalue(pi)
  1688. else
  1689. resulttype:=pbestrealtype^;
  1690. end;
  1691. in_cos_real :
  1692. begin
  1693. if left.nodetype in [ordconstn,realconstn] then
  1694. setconstrealvalue(cos(getconstrealvalue))
  1695. else
  1696. begin
  1697. set_varstate(left,vs_used,true);
  1698. inserttypeconv(left,pbestrealtype^);
  1699. resulttype:=pbestrealtype^;
  1700. end;
  1701. end;
  1702. in_sin_real :
  1703. begin
  1704. if left.nodetype in [ordconstn,realconstn] then
  1705. setconstrealvalue(sin(getconstrealvalue))
  1706. else
  1707. begin
  1708. set_varstate(left,vs_used,true);
  1709. inserttypeconv(left,pbestrealtype^);
  1710. resulttype:=pbestrealtype^;
  1711. end;
  1712. end;
  1713. in_arctan_real :
  1714. begin
  1715. if left.nodetype in [ordconstn,realconstn] then
  1716. setconstrealvalue(arctan(getconstrealvalue))
  1717. else
  1718. begin
  1719. set_varstate(left,vs_used,true);
  1720. inserttypeconv(left,pbestrealtype^);
  1721. resulttype:=pbestrealtype^;
  1722. end;
  1723. end;
  1724. in_abs_real :
  1725. begin
  1726. if left.nodetype in [ordconstn,realconstn] then
  1727. setconstrealvalue(abs(getconstrealvalue))
  1728. else
  1729. begin
  1730. set_varstate(left,vs_used,true);
  1731. inserttypeconv(left,pbestrealtype^);
  1732. resulttype:=pbestrealtype^;
  1733. end;
  1734. end;
  1735. in_sqr_real :
  1736. begin
  1737. if left.nodetype in [ordconstn,realconstn] then
  1738. setconstrealvalue(sqr(getconstrealvalue))
  1739. else
  1740. begin
  1741. set_varstate(left,vs_used,true);
  1742. inserttypeconv(left,pbestrealtype^);
  1743. resulttype:=pbestrealtype^;
  1744. end;
  1745. end;
  1746. in_sqrt_real :
  1747. begin
  1748. if left.nodetype in [ordconstn,realconstn] then
  1749. begin
  1750. vr:=getconstrealvalue;
  1751. if vr<0.0 then
  1752. result:=handle_sqrt_const(vr)
  1753. else
  1754. setconstrealvalue(sqrt(vr));
  1755. end
  1756. else
  1757. begin
  1758. set_varstate(left,vs_used,true);
  1759. inserttypeconv(left,pbestrealtype^);
  1760. resulttype:=pbestrealtype^;
  1761. end;
  1762. end;
  1763. in_ln_real :
  1764. begin
  1765. if left.nodetype in [ordconstn,realconstn] then
  1766. begin
  1767. vr:=getconstrealvalue;
  1768. if vr<=0.0 then
  1769. result:=handle_ln_const(vr)
  1770. else
  1771. setconstrealvalue(ln(vr));
  1772. end
  1773. else
  1774. begin
  1775. set_varstate(left,vs_used,true);
  1776. inserttypeconv(left,pbestrealtype^);
  1777. resulttype:=pbestrealtype^;
  1778. end;
  1779. end;
  1780. {$ifdef SUPPORT_MMX}
  1781. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  1782. begin
  1783. end;
  1784. {$endif SUPPORT_MMX}
  1785. in_prefetch_var:
  1786. begin
  1787. resulttype:=voidtype;
  1788. end;
  1789. in_assert_x_y :
  1790. begin
  1791. resulttype:=voidtype;
  1792. if assigned(left) then
  1793. begin
  1794. set_varstate(tcallparanode(left).left,vs_used,true);
  1795. { check type }
  1796. if is_boolean(left.resulttype.def) then
  1797. begin
  1798. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_used,true);
  1799. { must always be a string }
  1800. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  1801. end
  1802. else
  1803. CGMessage1(type_e_boolean_expr_expected,left.resulttype.def.typename);
  1804. end
  1805. else
  1806. CGMessage(type_e_mismatch);
  1807. { We've checked the whole statement for correctness, now we
  1808. can remove it if assertions are off }
  1809. if not(cs_do_assertion in aktlocalswitches) then
  1810. begin
  1811. { we need a valid node, so insert a nothingn }
  1812. result:=cnothingnode.create;
  1813. end
  1814. else
  1815. include(current_procinfo.flags,pi_do_call);
  1816. end;
  1817. else
  1818. internalerror(8);
  1819. end;
  1820. end;
  1821. myexit:
  1822. { Run get_paratype again to update maybe inserted typeconvs }
  1823. if not codegenerror then
  1824. begin
  1825. if assigned(left) and
  1826. (left.nodetype=callparan) then
  1827. tcallparanode(left).get_paratype;
  1828. end;
  1829. dec(parsing_para_level);
  1830. end;
  1831. function tinlinenode.pass_1 : tnode;
  1832. var
  1833. hp,hpp : tnode;
  1834. shiftconst: longint;
  1835. tempnode: ttempcreatenode;
  1836. newstatement: tstatementnode;
  1837. newblock: tblocknode;
  1838. begin
  1839. result:=nil;
  1840. { if we handle writeln; left contains no valid address }
  1841. if assigned(left) then
  1842. begin
  1843. if left.nodetype=callparan then
  1844. tcallparanode(left).firstcallparan
  1845. else
  1846. firstpass(left);
  1847. left_max;
  1848. end;
  1849. inc(parsing_para_level);
  1850. { intern const should already be handled }
  1851. if nf_inlineconst in flags then
  1852. internalerror(200104044);
  1853. case inlinenumber of
  1854. in_lo_qword,
  1855. in_hi_qword,
  1856. in_lo_long,
  1857. in_hi_long,
  1858. in_lo_word,
  1859. in_hi_word:
  1860. begin
  1861. shiftconst := 0;
  1862. case inlinenumber of
  1863. in_hi_qword:
  1864. shiftconst := 32;
  1865. in_hi_long:
  1866. shiftconst := 16;
  1867. in_hi_word:
  1868. shiftconst := 8;
  1869. end;
  1870. if shiftconst <> 0 then
  1871. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  1872. cordconstnode.create(shiftconst,u32inttype,false)),resulttype)
  1873. else
  1874. result := ctypeconvnode.create_internal(left,resulttype);
  1875. left := nil;
  1876. firstpass(result);
  1877. end;
  1878. in_sizeof_x:
  1879. begin
  1880. if registersint<1 then
  1881. registersint:=1;
  1882. expectloc:=LOC_REGISTER;
  1883. end;
  1884. in_typeof_x:
  1885. begin
  1886. if registersint<1 then
  1887. registersint:=1;
  1888. expectloc:=LOC_REGISTER;
  1889. end;
  1890. in_length_x:
  1891. begin
  1892. if is_shortstring(left.resulttype.def) then
  1893. expectloc:=left.expectloc
  1894. else
  1895. begin
  1896. { ansi/wide string }
  1897. if registersint<1 then
  1898. registersint:=1;
  1899. expectloc:=LOC_REGISTER;
  1900. end;
  1901. end;
  1902. in_typeinfo_x:
  1903. begin
  1904. expectloc:=LOC_REGISTER;
  1905. registersint:=1;
  1906. end;
  1907. in_assigned_x:
  1908. begin
  1909. expectloc := LOC_JUMP;
  1910. registersint:=1;
  1911. end;
  1912. in_pred_x,
  1913. in_succ_x:
  1914. begin
  1915. if is_64bit(resulttype.def) then
  1916. begin
  1917. if (registersint<2) then
  1918. registersint:=2
  1919. end
  1920. else
  1921. begin
  1922. if (registersint<1) then
  1923. registersint:=1;
  1924. end;
  1925. expectloc:=LOC_REGISTER;
  1926. end;
  1927. in_setlength_x,
  1928. in_initialize_x,
  1929. in_finalize_x:
  1930. begin
  1931. expectloc:=LOC_VOID;
  1932. end;
  1933. in_inc_x,
  1934. in_dec_x:
  1935. begin
  1936. expectloc:=LOC_VOID;
  1937. { check type }
  1938. if
  1939. {$ifndef cpu64bit}
  1940. is_64bit(left.resulttype.def) or
  1941. {$endif cpu64bit}
  1942. { range/overflow checking doesn't work properly }
  1943. { with the inc/dec code that's generated (JM) }
  1944. (
  1945. (left.resulttype.def.deftype = orddef) and
  1946. not(is_char(left.resulttype.def)) and
  1947. not(is_boolean(left.resulttype.def)) and
  1948. (aktlocalswitches * [cs_check_overflow,cs_check_range] <> [])
  1949. ) then
  1950. { convert to simple add (JM) }
  1951. begin
  1952. newblock := internalstatements(newstatement);
  1953. { extra parameter? }
  1954. if assigned(tcallparanode(left).right) then
  1955. begin
  1956. { Yes, use for add node }
  1957. hpp := tcallparanode(tcallparanode(left).right).left;
  1958. tcallparanode(tcallparanode(left).right).left := nil;
  1959. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1960. CGMessage(parser_e_illegal_expression);
  1961. end
  1962. else
  1963. { no, create constant 1 }
  1964. hpp := cordconstnode.create(1,tcallparanode(left).left.resulttype,false);
  1965. { make sure we don't call functions part of the left node twice (and generally }
  1966. { optimize the code generation) }
  1967. if node_complexity(tcallparanode(left).left) > 1 then
  1968. begin
  1969. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.def.size,tt_persistent,true);
  1970. addstatement(newstatement,tempnode);
  1971. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  1972. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  1973. hp := cderefnode.create(ctemprefnode.create(tempnode));
  1974. inserttypeconv_internal(hp,tcallparanode(left).left.resulttype);
  1975. end
  1976. else
  1977. begin
  1978. hp := tcallparanode(left).left.getcopy;
  1979. tempnode := nil;
  1980. end;
  1981. { addition/substraction depending on inc/dec }
  1982. if inlinenumber = in_inc_x then
  1983. hpp := caddnode.create(addn,hp,hpp)
  1984. else
  1985. hpp := caddnode.create(subn,hp,hpp);
  1986. { assign result of addition }
  1987. addstatement(newstatement,cassignmentnode.create(hp.getcopy,hpp));
  1988. { deallocate the temp }
  1989. if assigned(tempnode) then
  1990. addstatement(newstatement,ctempdeletenode.create(tempnode));
  1991. { firstpass it }
  1992. firstpass(newblock);
  1993. { return new node }
  1994. result := newblock;
  1995. end
  1996. else if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  1997. is_ordinal(left.resulttype.def) then
  1998. begin
  1999. { two paras ? }
  2000. if assigned(tcallparanode(left).right) then
  2001. begin
  2002. { need we an additional register ? }
  2003. if not(is_constintnode(tcallparanode(tcallparanode(left).right).left)) and
  2004. (tcallparanode(tcallparanode(left).right).left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) and
  2005. (tcallparanode(tcallparanode(left).right).left.registersint<=1) then
  2006. inc(registersint);
  2007. { do we need an additional register to restore the first parameter? }
  2008. if tcallparanode(tcallparanode(left).right).left.registersint>=registersint then
  2009. inc(registersint);
  2010. end;
  2011. end;
  2012. end;
  2013. in_include_x_y,
  2014. in_exclude_x_y:
  2015. begin
  2016. expectloc:=LOC_VOID;
  2017. registersint:=left.registersint;
  2018. registersfpu:=left.registersfpu;
  2019. {$ifdef SUPPORT_MMX}
  2020. registersmmx:=left.registersmmx;
  2021. {$endif SUPPORT_MMX}
  2022. end;
  2023. in_exp_real:
  2024. begin
  2025. result:= first_exp_real;
  2026. end;
  2027. in_round_real:
  2028. begin
  2029. result:= first_round_real;
  2030. end;
  2031. in_trunc_real:
  2032. begin
  2033. result:= first_trunc_real;
  2034. end;
  2035. in_int_real:
  2036. begin
  2037. result:= first_int_real;
  2038. end;
  2039. in_frac_real:
  2040. begin
  2041. result:= first_frac_real;
  2042. end;
  2043. in_cos_real:
  2044. begin
  2045. result:= first_cos_real;
  2046. end;
  2047. in_sin_real:
  2048. begin
  2049. result := first_sin_real;
  2050. end;
  2051. in_arctan_real:
  2052. begin
  2053. result := first_arctan_real;
  2054. end;
  2055. in_pi_real :
  2056. begin
  2057. result := first_pi;
  2058. end;
  2059. in_abs_real:
  2060. begin
  2061. result := first_abs_real;
  2062. end;
  2063. in_sqr_real:
  2064. begin
  2065. result := first_sqr_real;
  2066. end;
  2067. in_sqrt_real:
  2068. begin
  2069. result := first_sqrt_real;
  2070. end;
  2071. in_ln_real:
  2072. begin
  2073. result := first_ln_real;
  2074. end;
  2075. {$ifdef SUPPORT_MMX}
  2076. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2077. begin
  2078. end;
  2079. {$endif SUPPORT_MMX}
  2080. in_assert_x_y :
  2081. begin
  2082. expectloc:=LOC_VOID;
  2083. registersint:=left.registersint;
  2084. registersfpu:=left.registersfpu;
  2085. {$ifdef SUPPORT_MMX}
  2086. registersmmx:=left.registersmmx;
  2087. {$endif SUPPORT_MMX}
  2088. end;
  2089. in_low_x,
  2090. in_high_x:
  2091. internalerror(200104047);
  2092. in_ord_x,
  2093. in_chr_byte:
  2094. begin
  2095. { should not happend as it's converted to typeconv }
  2096. internalerror(200104045);
  2097. end;
  2098. in_ofs_x :
  2099. internalerror(2000101001);
  2100. in_seg_x :
  2101. internalerror(200104046);
  2102. in_settextbuf_file_x,
  2103. in_reset_typedfile,
  2104. in_rewrite_typedfile,
  2105. in_str_x_string,
  2106. in_val_x,
  2107. in_read_x,
  2108. in_readln_x,
  2109. in_write_x,
  2110. in_writeln_x :
  2111. begin
  2112. { should be handled by det_resulttype }
  2113. internalerror(200108234);
  2114. end;
  2115. in_prefetch_var:
  2116. begin
  2117. expectloc:=LOC_VOID;
  2118. end;
  2119. else
  2120. internalerror(8);
  2121. end;
  2122. dec(parsing_para_level);
  2123. end;
  2124. {$ifdef fpc}
  2125. {$maxfpuregisters default}
  2126. {$endif fpc}
  2127. function tinlinenode.docompare(p: tnode): boolean;
  2128. begin
  2129. docompare :=
  2130. inherited docompare(p) and
  2131. (inlinenumber = tinlinenode(p).inlinenumber);
  2132. end;
  2133. function tinlinenode.first_pi : tnode;
  2134. begin
  2135. {$ifdef x86}
  2136. { x86 has pi in hardware }
  2137. result:=crealconstnode.create(pi,pbestrealtype^);
  2138. {$else x86}
  2139. {$ifdef cpuextended}
  2140. result:=crealconstnode.create(extended(MathPiExtended),pbestrealtype^);
  2141. {$else cpuextended}
  2142. result:=crealconstnode.create(double(MathPi),pbestrealtype^);
  2143. {$endif cpuextended}
  2144. {$endif x86}
  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.157 2005-01-23 21:09:12 florian
  2243. + added pi bit pattern to the compiler, so pi should always be correct
  2244. Revision 1.156 2004/12/05 12:28:11 peter
  2245. * procvar handling for tp procvar mode fixed
  2246. * proc to procvar moved from addrnode to typeconvnode
  2247. * inlininginfo is now allocated only for inline routines that
  2248. can be inlined, introduced a new flag po_has_inlining_info
  2249. Revision 1.155 2004/11/26 22:33:00 peter
  2250. * fixed read temp for result
  2251. Revision 1.154 2004/11/21 21:27:31 peter
  2252. * add checks for filedef type to prevent crashes
  2253. Revision 1.153 2004/11/21 17:54:59 peter
  2254. * ttempcreatenode.create_reg merged into .create with parameter
  2255. whether a register is allowed
  2256. * funcret_paraloc renamed to funcretloc
  2257. Revision 1.152 2004/11/21 15:35:23 peter
  2258. * float routines all use internproc and compilerproc helpers
  2259. Revision 1.151 2004/11/09 23:10:22 peter
  2260. * use helper call to retrieve address of input/output to reduce
  2261. code that is generated in the main program for loading the
  2262. threadvar
  2263. Revision 1.150 2004/11/08 22:09:59 peter
  2264. * tvarsym splitted
  2265. Revision 1.149 2004/11/02 12:55:16 peter
  2266. * nf_internal flag for internal inserted typeconvs. This will
  2267. supress the generation of warning/hints
  2268. Revision 1.148 2004/11/01 18:26:52 florian
  2269. - removed unnecessary printnode
  2270. Revision 1.147 2004/11/01 17:41:28 florian
  2271. * fixed arm compilation with cgutils
  2272. * ...
  2273. Revision 1.146 2004/10/31 21:45:03 peter
  2274. * generic tlocation
  2275. * move tlocation to cgutils
  2276. Revision 1.145 2004/09/16 16:32:27 peter
  2277. * another fix for reading of subranges
  2278. Revision 1.144 2004/09/13 20:32:06 peter
  2279. * fix for read(subranges) with subrange typ already being sinttype
  2280. Revision 1.143 2004/08/25 15:56:35 peter
  2281. * fix sqr() and abs() constant range check errors
  2282. Revision 1.142 2004/08/08 16:00:56 florian
  2283. * constant floating point assignments etc. are now overflow checked
  2284. if Q+ or R+ is turned on
  2285. Revision 1.141 2004/07/15 19:55:39 jonas
  2286. + (incomplete) node_complexity function to assess the complexity of a
  2287. tree
  2288. + support for inlining value and const parameters at the node level
  2289. (all procedures without local variables and without formal parameters
  2290. can now be inlined at the node level)
  2291. Revision 1.140 2004/07/14 21:40:52 olle
  2292. + added Ord(pointer) for macpas
  2293. Revision 1.139 2004/07/14 14:38:35 jonas
  2294. * fix for web bug 3210
  2295. Revision 1.138 2004/06/20 08:55:29 florian
  2296. * logs truncated
  2297. Revision 1.137 2004/06/18 15:16:46 peter
  2298. * remove obsolete cardinal() typecasts
  2299. Revision 1.136 2004/06/16 20:07:08 florian
  2300. * dwarf branch merged
  2301. Revision 1.135 2004/05/28 21:15:20 peter
  2302. * inc(x,y) makes y always of type x to prevent 64bit operations
  2303. when x is a u32bit and y is signed
  2304. Revision 1.134 2004/05/23 18:28:41 peter
  2305. * methodpointer is loaded into a temp when it was a calln
  2306. Revision 1.133.2.9 2004/05/03 16:49:00 peter
  2307. * sizeof fixed
  2308. }