ninl.pas 100 KB

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