ninl.pas 97 KB

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