ninl.pas 96 KB

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