ninl.pas 99 KB

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