ninl.pas 98 KB

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