ninl.pas 95 KB

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