ninl.pas 95 KB

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