ninl.pas 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Type checking and register allocation for inline nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ninl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,htypechk,cpuinfo,symtype;
  22. {$i compinnr.inc}
  23. type
  24. tinlinenode = class(tunarynode)
  25. inlinenumber : byte;
  26. constructor create(number : byte;is_const:boolean;l : tnode);virtual;
  27. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  28. procedure ppuwrite(ppufile:tcompilerppufile);override;
  29. function dogetcopy : tnode;override;
  30. function pass_1 : tnode;override;
  31. function pass_typecheck:tnode;override;
  32. function docompare(p: tnode): boolean; override;
  33. { pack and unpack are changed into for-loops by the compiler }
  34. function first_pack_unpack: tnode; virtual;
  35. { All the following routines currently
  36. call compilerprocs, unless they are
  37. overriden in which case, the code
  38. generator handles them.
  39. }
  40. function first_pi: tnode ; virtual;
  41. function first_arctan_real: tnode; virtual;
  42. function first_abs_real: tnode; virtual;
  43. function first_sqr_real: tnode; virtual;
  44. function first_sqrt_real: tnode; virtual;
  45. function first_ln_real: tnode; virtual;
  46. function first_cos_real: tnode; virtual;
  47. function first_sin_real: tnode; virtual;
  48. function first_exp_real: tnode; virtual;
  49. function first_frac_real: tnode; virtual;
  50. function first_round_real: tnode; virtual;
  51. function first_trunc_real: tnode; virtual;
  52. function first_int_real: tnode; virtual;
  53. private
  54. function handle_str: tnode;
  55. function handle_reset_rewrite_typed: tnode;
  56. function handle_read_write: tnode;
  57. function handle_val: tnode;
  58. end;
  59. tinlinenodeclass = class of tinlinenode;
  60. var
  61. cinlinenode : tinlinenodeclass;
  62. function geninlinenode(number : byte;is_const:boolean;l : tnode) : tinlinenode;
  63. implementation
  64. uses
  65. verbose,globals,systems,
  66. globtype, cutils,
  67. symconst,symdef,symsym,symtable,paramgr,defutil,
  68. pass_1,
  69. ncal,ncon,ncnv,nadd,nld,nbas,nflw,nmem,nmat,nutils,
  70. cgbase,procinfo
  71. ;
  72. function geninlinenode(number : byte;is_const:boolean;l : tnode) : tinlinenode;
  73. begin
  74. geninlinenode:=cinlinenode.create(number,is_const,l);
  75. end;
  76. {*****************************************************************************
  77. TINLINENODE
  78. *****************************************************************************}
  79. constructor tinlinenode.create(number : byte;is_const:boolean;l : tnode);
  80. begin
  81. inherited create(inlinen,l);
  82. if is_const then
  83. include(flags,nf_inlineconst);
  84. inlinenumber:=number;
  85. end;
  86. constructor tinlinenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  87. begin
  88. inherited ppuload(t,ppufile);
  89. inlinenumber:=ppufile.getbyte;
  90. end;
  91. procedure tinlinenode.ppuwrite(ppufile:tcompilerppufile);
  92. begin
  93. inherited ppuwrite(ppufile);
  94. ppufile.putbyte(inlinenumber);
  95. end;
  96. function tinlinenode.dogetcopy : tnode;
  97. var
  98. n : tinlinenode;
  99. begin
  100. n:=tinlinenode(inherited dogetcopy);
  101. n.inlinenumber:=inlinenumber;
  102. result:=n;
  103. end;
  104. function tinlinenode.handle_str : tnode;
  105. var
  106. lenpara,
  107. fracpara,
  108. newparas,
  109. dest,
  110. source : tcallparanode;
  111. procname: string;
  112. is_real : boolean;
  113. rt : aint;
  114. begin
  115. result := cerrornode.create;
  116. { make sure we got at least two parameters (if we got only one, }
  117. { this parameter may not be encapsulated in a callparan) }
  118. if not assigned(left) or
  119. (left.nodetype <> callparan) then
  120. begin
  121. CGMessage1(parser_e_wrong_parameter_size,'Str');
  122. exit;
  123. end;
  124. { get destination string }
  125. dest := tcallparanode(left);
  126. { get source para (number) }
  127. source := dest;
  128. while assigned(source.right) do
  129. source := tcallparanode(source.right);
  130. { destination parameter must be a normal (not a colon) parameter, this
  131. check is needed because str(v:len) also has 2 parameters }
  132. if (source=dest) or
  133. (cpf_is_colon_para in tcallparanode(dest).callparaflags) then
  134. begin
  135. CGMessage1(parser_e_wrong_parameter_size,'Str');
  136. exit;
  137. end;
  138. is_real := (source.resultdef.typ = floatdef) or is_currency(source.resultdef);
  139. if ((dest.left.resultdef.typ<>stringdef) and
  140. not(is_chararray(dest.left.resultdef))) or
  141. not(is_real or
  142. (source.left.resultdef.typ = orddef)) then
  143. begin
  144. CGMessagePos(fileinfo,parser_e_illegal_expression);
  145. exit;
  146. end;
  147. { get len/frac parameters }
  148. lenpara := nil;
  149. fracpara := nil;
  150. if (cpf_is_colon_para in tcallparanode(dest.right).callparaflags) then
  151. begin
  152. lenpara := tcallparanode(dest.right);
  153. { we can let the callnode do the type checking of these parameters too, }
  154. { but then the error messages aren't as nice }
  155. if not is_integer(lenpara.resultdef) then
  156. begin
  157. CGMessagePos1(lenpara.fileinfo,
  158. type_e_integer_expr_expected,lenpara.resultdef.typename);
  159. exit;
  160. end;
  161. if (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  162. begin
  163. { parameters are in reverse order! }
  164. fracpara := lenpara;
  165. lenpara := tcallparanode(lenpara.right);
  166. if not is_real then
  167. begin
  168. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  169. exit
  170. end;
  171. if not is_integer(lenpara.resultdef) then
  172. begin
  173. CGMessagePos1(lenpara.fileinfo,
  174. type_e_integer_expr_expected,lenpara.resultdef.typename);
  175. exit;
  176. end;
  177. end;
  178. end;
  179. { generate the parameter list for the compilerproc }
  180. newparas := dest;
  181. { if we have a float parameter, insert the realtype, len and fracpara parameters }
  182. if is_real then
  183. begin
  184. { insert realtype parameter }
  185. if source.resultdef.typ = floatdef then
  186. rt:=ord(tfloatdef(source.left.resultdef).floattype)
  187. else
  188. rt:=ord(tfloatdef(pbestrealtype^).floattype);
  189. newparas.right := ccallparanode.create(cordconstnode.create(
  190. rt,s32inttype,true),newparas.right);
  191. { if necessary, insert a fraction parameter }
  192. if not assigned(fracpara) then
  193. begin
  194. tcallparanode(newparas.right).right := ccallparanode.create(
  195. cordconstnode.create(-1,s32inttype,false),
  196. tcallparanode(newparas.right).right);
  197. fracpara := tcallparanode(tcallparanode(newparas.right).right);
  198. end;
  199. { if necessary, insert a length para }
  200. if not assigned(lenpara) then
  201. fracpara.right := ccallparanode.create(
  202. cordconstnode.create(-32767,s32inttype,false),
  203. fracpara.right);
  204. end
  205. else
  206. { for a normal parameter, insert a only length parameter if one is missing }
  207. if not assigned(lenpara) then
  208. newparas.right := ccallparanode.create(cordconstnode.create(-1,s32inttype,false),
  209. newparas.right);
  210. { remove the parameters from the original node so they won't get disposed, }
  211. { since they're reused }
  212. left := nil;
  213. { create procedure name }
  214. if is_chararray(dest.resultdef) then
  215. procname:='fpc_chararray_'
  216. else
  217. procname := 'fpc_' + tstringdef(dest.resultdef).stringtypname+'_';
  218. if is_real then
  219. procname := procname + 'float'
  220. else
  221. case torddef(source.resultdef).ordtype of
  222. {$ifdef cpu64bit}
  223. u64bit:
  224. procname := procname + 'uint';
  225. {$else}
  226. u32bit:
  227. procname := procname + 'uint';
  228. u64bit:
  229. procname := procname + 'qword';
  230. scurrency,
  231. s64bit:
  232. procname := procname + 'int64';
  233. {$endif}
  234. else
  235. procname := procname + 'sint';
  236. end;
  237. { free the errornode we generated in the beginning }
  238. result.free;
  239. { create the call node, }
  240. result := ccallnode.createintern(procname,newparas);
  241. end;
  242. function tinlinenode.handle_reset_rewrite_typed: tnode;
  243. begin
  244. { since this is a "in_xxxx_typedfile" node, we can be sure we have }
  245. { a typed file as argument and we don't have to check it again (JM) }
  246. { add the recsize parameter }
  247. { note: for some reason, the parameter of intern procedures with only one }
  248. { parameter is gets lifted out of its original tcallparanode (see round }
  249. { line 1306 of ncal.pas), so recreate a tcallparanode here (JM) }
  250. left := ccallparanode.create(cordconstnode.create(
  251. tfiledef(left.resultdef).typedfiledef.size,s32inttype,true),
  252. ccallparanode.create(left,nil));
  253. { create the correct call }
  254. if inlinenumber=in_reset_typedfile then
  255. result := ccallnode.createintern('fpc_reset_typed',left)
  256. else
  257. result := ccallnode.createintern('fpc_rewrite_typed',left);
  258. { make sure left doesn't get disposed, since we use it in the new call }
  259. left := nil;
  260. end;
  261. function tinlinenode.handle_read_write: tnode;
  262. const
  263. procnames: array[boolean,boolean] of string[11] =
  264. (('write_text_','read_text_'),('typed_write','typed_read'));
  265. procnamesdisplay: array[boolean] of string[5] =
  266. ('Write','Read');
  267. var
  268. filepara,
  269. lenpara,
  270. fracpara,
  271. nextpara,
  272. para : tcallparanode;
  273. newstatement : tstatementnode;
  274. newblock : tblocknode;
  275. p1 : tnode;
  276. filetemp,
  277. temp : ttempcreatenode;
  278. procprefix,
  279. name : string[31];
  280. textsym : ttypesym;
  281. readfunctype : tdef;
  282. is_typed,
  283. do_read,
  284. is_real,
  285. error_para,
  286. found_error : boolean;
  287. begin
  288. filepara := nil;
  289. is_typed := false;
  290. filetemp := nil;
  291. do_read := inlinenumber in [in_read_x,in_readln_x];
  292. { if we fail, we can quickly exit this way. We must generate something }
  293. { instead of the inline node, because firstpass will bomb with an }
  294. { internalerror if it encounters a read/write }
  295. result := cerrornode.create;
  296. { reverse the parameters (needed to get the colon parameters in the }
  297. { correct order when processing write(ln) }
  298. left := reverseparameters(tcallparanode(left));
  299. if assigned(left) then
  300. begin
  301. { check if we have a file parameter and if yes, what kind it is }
  302. filepara := tcallparanode(left);
  303. if (filepara.resultdef.typ=filedef) then
  304. begin
  305. if (tfiledef(filepara.resultdef).filetyp=ft_untyped) then
  306. begin
  307. CGMessagePos(fileinfo,type_e_no_read_write_for_untyped_file);
  308. exit;
  309. end
  310. else
  311. begin
  312. if (tfiledef(filepara.resultdef).filetyp=ft_typed) then
  313. begin
  314. if (inlinenumber in [in_readln_x,in_writeln_x]) then
  315. begin
  316. CGMessagePos(fileinfo,type_e_no_readln_writeln_for_typed_file);
  317. exit;
  318. end;
  319. is_typed := true;
  320. end
  321. end;
  322. end
  323. else
  324. filepara := nil;
  325. end;
  326. { create a blocknode in which the successive write/read statements will be }
  327. { put, since they belong together. Also create a dummy statement already to }
  328. { make inserting of additional statements easier }
  329. newblock:=internalstatements(newstatement);
  330. { if we don't have a filepara, create one containing the default }
  331. if not assigned(filepara) then
  332. begin
  333. { since the input/output variables are threadvars loading them into
  334. a temp once is faster. Create a temp which will hold a pointer to the file }
  335. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  336. addstatement(newstatement,filetemp);
  337. { make sure the resultdef of the temp (and as such of the }
  338. { temprefs coming after it) is set (necessary because the }
  339. { temprefs will be part of the filepara, of which we need }
  340. { the resultdef later on and temprefs can only be }
  341. { typecheckpassed if the resultdef of the temp is known) }
  342. typecheckpass(tnode(filetemp));
  343. { assign the address of the file to the temp }
  344. if do_read then
  345. name := 'input'
  346. else
  347. name := 'output';
  348. addstatement(newstatement,
  349. cassignmentnode.create(ctemprefnode.create(filetemp),
  350. ccallnode.createintern('fpc_get_'+name,nil)));
  351. { create a new fileparameter as follows: file_type(temp^) }
  352. { (so that we pass the value and not the address of the temp }
  353. { to the read/write routine) }
  354. textsym:=search_system_type('TEXT');
  355. filepara := ccallparanode.create(ctypeconvnode.create_internal(
  356. cderefnode.create(ctemprefnode.create(filetemp)),textsym.typedef),nil);
  357. end
  358. else
  359. { remove filepara from the parameter chain }
  360. begin
  361. left := filepara.right;
  362. filepara.right := nil;
  363. { the file para is a var parameter, but it must be valid already }
  364. set_varstate(filepara.left,vs_readwritten,[vsf_must_be_valid]);
  365. { check if we should make a temp to store the result of a complex }
  366. { expression (better heuristics, anyone?) (JM) }
  367. if (filepara.left.nodetype <> loadn) then
  368. begin
  369. { create a temp which will hold a pointer to the file }
  370. filetemp := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  371. { add it to the statements }
  372. addstatement(newstatement,filetemp);
  373. { make sure the resultdef of the temp (and as such of the }
  374. { temprefs coming after it) is set (necessary because the }
  375. { temprefs will be part of the filepara, of which we need }
  376. { the resultdef later on and temprefs can only be }
  377. { typecheckpassed if the resultdef of the temp is known) }
  378. typecheckpass(tnode(filetemp));
  379. { assign the address of the file to the temp }
  380. addstatement(newstatement,
  381. cassignmentnode.create(ctemprefnode.create(filetemp),
  382. caddrnode.create_internal(filepara.left)));
  383. typecheckpass(newstatement.left);
  384. { create a new fileparameter as follows: file_type(temp^) }
  385. { (so that we pass the value and not the address of the temp }
  386. { to the read/write routine) }
  387. nextpara := ccallparanode.create(ctypeconvnode.create_internal(
  388. cderefnode.create(ctemprefnode.create(filetemp)),filepara.left.resultdef),nil);
  389. { replace the old file para with the new one }
  390. filepara.left := nil;
  391. filepara.free;
  392. filepara := nextpara;
  393. end;
  394. end;
  395. { the resultdef of the filepara must be set since it's }
  396. { used below }
  397. filepara.get_paratype;
  398. { now, filepara is nowhere referenced anymore, so we can safely dispose it }
  399. { if something goes wrong or at the end of the procedure }
  400. { choose the correct procedure prefix }
  401. procprefix := 'fpc_'+procnames[is_typed,do_read];
  402. { we're going to reuse the paranodes, so make sure they don't get freed }
  403. { twice }
  404. para := tcallparanode(left);
  405. left := nil;
  406. { no errors found yet... }
  407. found_error := false;
  408. if is_typed then
  409. begin
  410. { add the typesize to the filepara }
  411. if filepara.resultdef.typ=filedef then
  412. filepara.right := ccallparanode.create(cordconstnode.create(
  413. tfiledef(filepara.resultdef).typedfiledef.size,s32inttype,true),nil);
  414. { check for "no parameters" (you need at least one extra para for typed files) }
  415. if not assigned(para) then
  416. begin
  417. CGMessage1(parser_e_wrong_parameter_size,procnamesdisplay[do_read]);
  418. found_error := true;
  419. end;
  420. { process all parameters }
  421. while assigned(para) do
  422. begin
  423. { check if valid parameter }
  424. if para.left.nodetype=typen then
  425. begin
  426. CGMessagePos(para.left.fileinfo,type_e_cant_read_write_type);
  427. found_error := true;
  428. end;
  429. { support writeln(procvar) }
  430. if (para.left.resultdef.typ=procvardef) then
  431. begin
  432. p1:=ccallnode.create_procvar(nil,para.left);
  433. typecheckpass(p1);
  434. para.left:=p1;
  435. end;
  436. if filepara.resultdef.typ=filedef then
  437. inserttypeconv(para.left,tfiledef(filepara.resultdef).typedfiledef);
  438. if assigned(para.right) and
  439. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  440. begin
  441. CGMessagePos(para.right.fileinfo,parser_e_illegal_colon_qualifier);
  442. { skip all colon para's }
  443. nextpara := tcallparanode(tcallparanode(para.right).right);
  444. while assigned(nextpara) and
  445. (cpf_is_colon_para in nextpara.callparaflags) do
  446. nextpara := tcallparanode(nextpara.right);
  447. found_error := true;
  448. end
  449. else
  450. { get next parameter }
  451. nextpara := tcallparanode(para.right);
  452. { When we have a call, we have a problem: you can't pass the }
  453. { result of a call as a formal const parameter. Solution: }
  454. { assign the result to a temp and pass this temp as parameter }
  455. { This is not very efficient, but write(typedfile,x) is }
  456. { already slow by itself anyway (no buffering) (JM) }
  457. { Actually, thge same goes for every non-simple expression }
  458. { (such as an addition, ...) -> put everything but load nodes }
  459. { into temps (JM) }
  460. { of course, this must only be allowed for writes!!! (JM) }
  461. if not(do_read) and
  462. (para.left.nodetype <> loadn) then
  463. begin
  464. { create temp for result }
  465. temp := ctempcreatenode.create(para.left.resultdef,
  466. para.left.resultdef.size,tt_persistent,false);
  467. addstatement(newstatement,temp);
  468. { assign result to temp }
  469. addstatement(newstatement,
  470. cassignmentnode.create(ctemprefnode.create(temp),
  471. para.left));
  472. { replace (reused) paranode with temp }
  473. para.left := ctemprefnode.create(temp);
  474. end;
  475. { add fileparameter }
  476. para.right := filepara.getcopy;
  477. { create call statment }
  478. { since the parameters are in the correct order, we have to insert }
  479. { the statements always at the end of the current block }
  480. addstatement(newstatement,ccallnode.createintern(procprefix,para));
  481. { if we used a temp, free it }
  482. if para.left.nodetype = temprefn then
  483. addstatement(newstatement,ctempdeletenode.create(temp));
  484. { process next parameter }
  485. para := nextpara;
  486. end;
  487. { free the file parameter }
  488. filepara.free;
  489. end
  490. else
  491. { text read/write }
  492. begin
  493. while assigned(para) do
  494. begin
  495. { is this parameter faulty? }
  496. error_para := false;
  497. { is this parameter a real? }
  498. is_real:=false;
  499. { type used for the read(), this is used to check
  500. whether a temp is needed for range checking }
  501. readfunctype:=nil;
  502. { can't read/write types }
  503. if para.left.nodetype=typen then
  504. begin
  505. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  506. error_para := true;
  507. end;
  508. { support writeln(procvar) }
  509. if (para.left.resultdef.typ=procvardef) then
  510. begin
  511. p1:=ccallnode.create_procvar(nil,para.left);
  512. typecheckpass(p1);
  513. para.left:=p1;
  514. end;
  515. { Currency will be written using the bestreal }
  516. if is_currency(para.left.resultdef) then
  517. inserttypeconv(para.left,pbestrealtype^);
  518. case para.left.resultdef.typ of
  519. stringdef :
  520. begin
  521. name := procprefix+tstringdef(para.left.resultdef).stringtypname;
  522. end;
  523. pointerdef :
  524. begin
  525. if (not is_pchar(para.left.resultdef)) or do_read then
  526. begin
  527. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  528. error_para := true;
  529. end
  530. else
  531. name := procprefix+'pchar_as_pointer';
  532. end;
  533. floatdef :
  534. begin
  535. is_real:=true;
  536. name := procprefix+'float';
  537. readfunctype:=pbestrealtype^;
  538. end;
  539. orddef :
  540. begin
  541. case torddef(para.left.resultdef).ordtype of
  542. {$ifdef cpu64bit}
  543. s64bit,
  544. {$endif cpu64bit}
  545. s8bit,
  546. s16bit,
  547. s32bit :
  548. begin
  549. name := procprefix+'sint';
  550. readfunctype:=sinttype;
  551. end;
  552. {$ifdef cpu64bit}
  553. u64bit,
  554. {$endif cpu64bit}
  555. u8bit,
  556. u16bit,
  557. u32bit :
  558. begin
  559. name := procprefix+'uint';
  560. readfunctype:=uinttype;
  561. end;
  562. uchar :
  563. begin
  564. name := procprefix+'char';
  565. readfunctype:=cchartype;
  566. end;
  567. uwidechar :
  568. begin
  569. name := procprefix+'widechar';
  570. readfunctype:=cwidechartype;
  571. end;
  572. {$ifndef cpu64bit}
  573. s64bit :
  574. begin
  575. name := procprefix+'int64';
  576. readfunctype:=s64inttype;
  577. end;
  578. u64bit :
  579. begin
  580. name := procprefix+'qword';
  581. readfunctype:=u64inttype;
  582. end;
  583. {$endif cpu64bit}
  584. bool8bit,
  585. bool16bit,
  586. bool32bit,
  587. bool64bit:
  588. begin
  589. if do_read then
  590. begin
  591. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  592. error_para := true;
  593. end
  594. else
  595. begin
  596. name := procprefix+'boolean';
  597. readfunctype:=booltype;
  598. end;
  599. end
  600. else
  601. begin
  602. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  603. error_para := true;
  604. end;
  605. end;
  606. end;
  607. variantdef :
  608. name:=procprefix+'variant';
  609. arraydef :
  610. begin
  611. if is_chararray(para.left.resultdef) then
  612. name := procprefix+'pchar_as_array'
  613. else
  614. begin
  615. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  616. error_para := true;
  617. end
  618. end
  619. else
  620. begin
  621. CGMessagePos(para.fileinfo,type_e_cant_read_write_type);
  622. error_para := true;
  623. end
  624. end;
  625. { check for length/fractional colon para's }
  626. fracpara := nil;
  627. lenpara := nil;
  628. if assigned(para.right) and
  629. (cpf_is_colon_para in tcallparanode(para.right).callparaflags) then
  630. begin
  631. lenpara := tcallparanode(para.right);
  632. if assigned(lenpara.right) and
  633. (cpf_is_colon_para in tcallparanode(lenpara.right).callparaflags) then
  634. fracpara:=tcallparanode(lenpara.right);
  635. end;
  636. { get the next parameter now already, because we're going }
  637. { to muck around with the pointers }
  638. if assigned(fracpara) then
  639. nextpara := tcallparanode(fracpara.right)
  640. else if assigned(lenpara) then
  641. nextpara := tcallparanode(lenpara.right)
  642. else
  643. nextpara := tcallparanode(para.right);
  644. { check if a fracpara is allowed }
  645. if assigned(fracpara) and not is_real then
  646. begin
  647. CGMessagePos(fracpara.fileinfo,parser_e_illegal_colon_qualifier);
  648. error_para := true;
  649. end
  650. else if assigned(lenpara) and do_read then
  651. begin
  652. { I think this is already filtered out by parsing, but I'm not sure (JM) }
  653. CGMessagePos(lenpara.fileinfo,parser_e_illegal_colon_qualifier);
  654. error_para := true;
  655. end;
  656. { adjust found_error }
  657. found_error := found_error or error_para;
  658. if not error_para then
  659. begin
  660. { create dummy frac/len para's if necessary }
  661. if not do_read then
  662. begin
  663. { difference in default value for floats and the rest :( }
  664. if not is_real then
  665. begin
  666. if not assigned(lenpara) then
  667. lenpara := ccallparanode.create(
  668. cordconstnode.create(0,s32inttype,false),nil)
  669. else
  670. { make sure we don't pass the successive }
  671. { parameters too. We also already have a }
  672. { reference to the next parameter in }
  673. { nextpara }
  674. lenpara.right := nil;
  675. end
  676. else
  677. begin
  678. if not assigned(lenpara) then
  679. lenpara := ccallparanode.create(
  680. cordconstnode.create(-32767,s32inttype,false),nil);
  681. { also create a default fracpara if necessary }
  682. if not assigned(fracpara) then
  683. fracpara := ccallparanode.create(
  684. cordconstnode.create(-1,s32inttype,false),nil);
  685. { add it to the lenpara }
  686. lenpara.right := fracpara;
  687. { and add the realtype para (this also removes the link }
  688. { to any parameters coming after it) }
  689. fracpara.right := ccallparanode.create(
  690. cordconstnode.create(ord(tfloatdef(para.left.resultdef).floattype),
  691. s32inttype,true),nil);
  692. end;
  693. end;
  694. { special handling of reading small numbers, because the helpers }
  695. { expect a longint/card/bestreal var parameter. Use a temp. can't }
  696. { use functions because then the call to FPC_IOCHECK destroys }
  697. { their result before we can store it }
  698. if do_read and
  699. assigned(readfunctype) and
  700. (para.left.resultdef<>readfunctype) then
  701. begin
  702. { create the parameter list: the temp ... }
  703. temp := ctempcreatenode.create(readfunctype,readfunctype.size,tt_persistent,false);
  704. addstatement(newstatement,temp);
  705. { ... and the file }
  706. p1 := ccallparanode.create(ctemprefnode.create(temp),
  707. filepara.getcopy);
  708. { create the call to the helper }
  709. addstatement(newstatement,
  710. ccallnode.createintern(name,tcallparanode(p1)));
  711. { assign the result to the original var (this automatically }
  712. { takes care of range checking) }
  713. addstatement(newstatement,
  714. cassignmentnode.create(para.left,
  715. ctemprefnode.create(temp)));
  716. { release the temp location }
  717. addstatement(newstatement,ctempdeletenode.create(temp));
  718. { statement of para is used }
  719. para.left := nil;
  720. { free the enclosing tcallparanode, but not the }
  721. { parameters coming after it }
  722. para.right := nil;
  723. para.free;
  724. end
  725. else
  726. { read of non s/u-8/16bit, or a write }
  727. begin
  728. { add the filepara to the current parameter }
  729. para.right := filepara.getcopy;
  730. { add the lenpara (fracpara and realtype are already linked }
  731. { with it if necessary) }
  732. tcallparanode(para.right).right := lenpara;
  733. { in case of writing a chararray, add whether it's }
  734. { zero-based }
  735. if (para.left.resultdef.typ = arraydef) then
  736. para := ccallparanode.create(cordconstnode.create(
  737. ord(tarraydef(para.left.resultdef).lowrange=0),booltype,false),para);
  738. { create the call statement }
  739. addstatement(newstatement,
  740. ccallnode.createintern(name,para));
  741. end
  742. end
  743. else
  744. { error_para = true }
  745. begin
  746. { free the parameter, since it isn't referenced anywhere anymore }
  747. para.right := nil;
  748. para.free;
  749. if assigned(lenpara) then
  750. begin
  751. lenpara.right := nil;
  752. lenpara.free;
  753. end;
  754. if assigned(fracpara) then
  755. begin
  756. fracpara.right := nil;
  757. fracpara.free;
  758. end;
  759. end;
  760. { process next parameter }
  761. para := nextpara;
  762. end;
  763. { if no error, add the write(ln)/read(ln) end calls }
  764. if not found_error then
  765. begin
  766. case inlinenumber of
  767. in_read_x:
  768. name:='fpc_read_end';
  769. in_write_x:
  770. name:='fpc_write_end';
  771. in_readln_x:
  772. name:='fpc_readln_end';
  773. in_writeln_x:
  774. name:='fpc_writeln_end';
  775. end;
  776. addstatement(newstatement,ccallnode.createintern(name,filepara));
  777. end;
  778. end;
  779. { if we found an error, simply delete the generated blocknode }
  780. if found_error then
  781. newblock.free
  782. else
  783. begin
  784. { deallocate the temp for the file para if we used one }
  785. if assigned(filetemp) then
  786. addstatement(newstatement,ctempdeletenode.create(filetemp));
  787. { otherwise return the newly generated block of instructions, }
  788. { but first free the errornode we generated at the beginning }
  789. result.free;
  790. result := newblock
  791. end;
  792. end;
  793. function tinlinenode.handle_val: tnode;
  794. var
  795. procname,
  796. suffix : string[31];
  797. sourcepara,
  798. destpara,
  799. codepara,
  800. sizepara,
  801. newparas : tcallparanode;
  802. orgcode : tnode;
  803. newstatement : tstatementnode;
  804. newblock : tblocknode;
  805. tempcode : ttempcreatenode;
  806. begin
  807. { for easy exiting if something goes wrong }
  808. result := cerrornode.create;
  809. { check the amount of parameters }
  810. if not(assigned(left)) or
  811. not(assigned(tcallparanode(left).right)) then
  812. begin
  813. CGMessage1(parser_e_wrong_parameter_size,'Val');
  814. exit;
  815. end;
  816. { reverse parameters for easier processing }
  817. left := reverseparameters(tcallparanode(left));
  818. { get the parameters }
  819. tempcode := nil;
  820. orgcode := nil;
  821. sizepara := nil;
  822. sourcepara := tcallparanode(left);
  823. destpara := tcallparanode(sourcepara.right);
  824. codepara := tcallparanode(destpara.right);
  825. { check if codepara is valid }
  826. if assigned(codepara) and
  827. (
  828. (codepara.resultdef.typ <> orddef)
  829. {$ifndef cpu64bit}
  830. or is_64bitint(codepara.resultdef)
  831. {$endif cpu64bit}
  832. ) then
  833. begin
  834. CGMessagePos1(codepara.fileinfo,type_e_integer_expr_expected,codepara.resultdef.typename);
  835. exit;
  836. end;
  837. { check if dest para is valid }
  838. if not(destpara.resultdef.typ in [orddef,floatdef]) then
  839. begin
  840. CGMessagePos(destpara.fileinfo,type_e_integer_or_real_expr_expected);
  841. exit;
  842. end;
  843. { we're going to reuse the exisiting para's, so make sure they }
  844. { won't be disposed }
  845. left := nil;
  846. { create the blocknode which will hold the generated statements + }
  847. { an initial dummy statement }
  848. newblock:=internalstatements(newstatement);
  849. { do we need a temp for code? Yes, if no code specified, or if }
  850. { code is not a 32bit parameter (we already checked whether the }
  851. { the code para, if specified, was an orddef) }
  852. if not assigned(codepara) or
  853. (codepara.resultdef.size<>sinttype.size) then
  854. begin
  855. tempcode := ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  856. addstatement(newstatement,tempcode);
  857. { set the resultdef of the temp (needed to be able to get }
  858. { the resultdef of the tempref used in the new code para) }
  859. typecheckpass(tnode(tempcode));
  860. { create a temp codepara, but save the original code para to }
  861. { assign the result to later on }
  862. if assigned(codepara) then
  863. begin
  864. orgcode := codepara.left;
  865. codepara.left := ctemprefnode.create(tempcode);
  866. end
  867. else
  868. codepara := ccallparanode.create(ctemprefnode.create(tempcode),nil);
  869. { we need its resultdef later on }
  870. codepara.get_paratype;
  871. end
  872. else if (torddef(codepara.resultdef).ordtype = torddef(sinttype).ordtype) then
  873. { because code is a var parameter, it must match types exactly }
  874. { however, since it will return values in [0..255], both longints }
  875. { and cardinals are fine. Since the formal code para type is }
  876. { longint, insert a typecoversion to longint for cardinal para's }
  877. begin
  878. codepara.left := ctypeconvnode.create_internal(codepara.left,sinttype);
  879. { make it explicit, oterwise you may get a nonsense range }
  880. { check error if the cardinal already contained a value }
  881. { > $7fffffff }
  882. codepara.get_paratype;
  883. end;
  884. { create the procedure name }
  885. procname := 'fpc_val_';
  886. case destpara.resultdef.typ of
  887. orddef:
  888. begin
  889. case torddef(destpara.resultdef).ordtype of
  890. {$ifdef cpu64bit}
  891. scurrency,
  892. s64bit,
  893. {$endif cpu64bit}
  894. s8bit,
  895. s16bit,
  896. s32bit:
  897. begin
  898. suffix := 'sint_';
  899. { we also need a destsize para in this case }
  900. sizepara := ccallparanode.create(cordconstnode.create
  901. (destpara.resultdef.size,s32inttype,true),nil);
  902. end;
  903. {$ifdef cpu64bit}
  904. u64bit,
  905. {$endif cpu64bit}
  906. u8bit,
  907. u16bit,
  908. u32bit:
  909. suffix := 'uint_';
  910. {$ifndef cpu64bit}
  911. scurrency,
  912. s64bit: suffix := 'int64_';
  913. u64bit: suffix := 'qword_';
  914. {$endif cpu64bit}
  915. else
  916. internalerror(200304225);
  917. end;
  918. end;
  919. floatdef:
  920. begin
  921. suffix := 'real_';
  922. end;
  923. end;
  924. procname := procname + suffix;
  925. { play a trick to have tcallnode handle invalid source parameters: }
  926. { the shortstring-longint val routine by default }
  927. if (sourcepara.resultdef.typ = stringdef) then
  928. procname := procname + tstringdef(sourcepara.resultdef).stringtypname
  929. else
  930. procname := procname + 'shortstr';
  931. { set up the correct parameters for the call: the code para... }
  932. newparas := codepara;
  933. { and the source para }
  934. codepara.right := sourcepara;
  935. { sizepara either contains nil if none is needed (which is ok, since }
  936. { then the next statement severes any possible links with other paras }
  937. { that sourcepara may have) or it contains the necessary size para and }
  938. { its right field is nil }
  939. sourcepara.right := sizepara;
  940. { create the call and assign the result to dest (val helpers are functions).
  941. Use a trick to prevent a type size mismatch warning to be generated by the
  942. assignment node. First convert implicitly to the resultdef. This will insert
  943. the range check. The Second conversion is done explicitly to hide the implicit conversion
  944. for the assignment node and therefor preventing the warning (PFV) }
  945. addstatement(newstatement,cassignmentnode.create(
  946. destpara.left,ctypeconvnode.create_internal(ctypeconvnode.create(ccallnode.createintern(procname,newparas),destpara.left.resultdef),destpara.left.resultdef)));
  947. { dispose of the enclosing paranode of the destination }
  948. destpara.left := nil;
  949. destpara.right := nil;
  950. destpara.free;
  951. { check if we used a temp for code and whether we have to store }
  952. { it to the real code parameter }
  953. if assigned(orgcode) then
  954. addstatement(newstatement,cassignmentnode.create(
  955. orgcode,
  956. ctypeconvnode.create_internal(
  957. ctemprefnode.create(tempcode),orgcode.resultdef)));
  958. { release the temp if we allocated one }
  959. if assigned(tempcode) then
  960. addstatement(newstatement,ctempdeletenode.create(tempcode));
  961. { free the errornode }
  962. result.free;
  963. { and return it }
  964. result := newblock;
  965. end;
  966. {$maxfpuregisters 0}
  967. function getpi : bestreal;
  968. begin
  969. {$ifdef x86}
  970. { x86 has pi in hardware }
  971. result:=pi;
  972. {$else x86}
  973. {$ifdef cpuextended}
  974. result:=MathPiExtended.Value;
  975. {$else cpuextended}
  976. result:=MathPi.Value;
  977. {$endif cpuextended}
  978. {$endif x86}
  979. end;
  980. function tinlinenode.pass_typecheck:tnode;
  981. function do_lowhigh(def:tdef) : tnode;
  982. var
  983. v : tconstexprint;
  984. enum : tenumsym;
  985. hp : tnode;
  986. begin
  987. case def.typ of
  988. orddef:
  989. begin
  990. if inlinenumber=in_low_x then
  991. v:=torddef(def).low
  992. else
  993. v:=torddef(def).high;
  994. { low/high of torddef are longints, so we need special }
  995. { handling for cardinal and 64bit types (JM) }
  996. { 1.0.x doesn't support int64($ffffffff) correct, it'll expand
  997. to -1 instead of staying $ffffffff. Therefor we use $ffff with
  998. shl twice (PFV) }
  999. case torddef(def).ordtype of
  1000. s64bit,scurrency :
  1001. begin
  1002. if (inlinenumber=in_low_x) then
  1003. v := int64($80000000) shl 32
  1004. else
  1005. v := (int64($7fffffff) shl 32) or int64($ffff) shl 16 or int64($ffff)
  1006. end;
  1007. u64bit :
  1008. begin
  1009. { we have to use a dirty trick for high(qword), }
  1010. { because it's bigger than high(tconstexprint) (JM) }
  1011. v := 0
  1012. end
  1013. else
  1014. begin
  1015. if not is_signed(def) then
  1016. v := cardinal(v);
  1017. end;
  1018. end;
  1019. hp:=cordconstnode.create(v,def,true);
  1020. typecheckpass(hp);
  1021. { fix high(qword) }
  1022. if (torddef(def).ordtype=u64bit) and
  1023. (inlinenumber = in_high_x) then
  1024. tordconstnode(hp).value := -1; { is the same as qword($ffffffffffffffff) }
  1025. do_lowhigh:=hp;
  1026. end;
  1027. enumdef:
  1028. begin
  1029. enum:=tenumsym(tenumdef(def).firstenum);
  1030. v:=tenumdef(def).maxval;
  1031. if inlinenumber=in_high_x then
  1032. while assigned(enum) and (enum.value <> v) do
  1033. enum:=enum.nextenum;
  1034. if not assigned(enum) then
  1035. internalerror(309993)
  1036. else
  1037. hp:=genenumnode(enum);
  1038. do_lowhigh:=hp;
  1039. end;
  1040. else
  1041. internalerror(87);
  1042. end;
  1043. end;
  1044. function getconstrealvalue : bestreal;
  1045. begin
  1046. case left.nodetype of
  1047. ordconstn:
  1048. getconstrealvalue:=tordconstnode(left).value;
  1049. realconstn:
  1050. getconstrealvalue:=trealconstnode(left).value_real;
  1051. else
  1052. internalerror(309992);
  1053. end;
  1054. end;
  1055. procedure setconstrealvalue(r : bestreal);
  1056. begin
  1057. result:=crealconstnode.create(r,pbestrealtype^);
  1058. end;
  1059. function handle_ln_const(r : bestreal) : tnode;
  1060. begin
  1061. if r<=0.0 then
  1062. if (cs_check_range in current_settings.localswitches) or
  1063. (cs_check_overflow in current_settings.localswitches) then
  1064. begin
  1065. result:=crealconstnode.create(0,pbestrealtype^);
  1066. CGMessage(type_e_wrong_math_argument)
  1067. end
  1068. else
  1069. begin
  1070. if r=0.0 then
  1071. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1072. else
  1073. result:=crealconstnode.create(MathNegInf.Value,pbestrealtype^)
  1074. end
  1075. else
  1076. result:=crealconstnode.create(ln(r),pbestrealtype^)
  1077. end;
  1078. function handle_sqrt_const(r : bestreal) : tnode;
  1079. begin
  1080. if r<0.0 then
  1081. if (cs_check_range in current_settings.localswitches) or
  1082. (cs_check_overflow in current_settings.localswitches) then
  1083. begin
  1084. result:=crealconstnode.create(0,pbestrealtype^);
  1085. CGMessage(type_e_wrong_math_argument)
  1086. end
  1087. else
  1088. result:=crealconstnode.create(MathQNaN.Value,pbestrealtype^)
  1089. else
  1090. result:=crealconstnode.create(sqrt(r),pbestrealtype^)
  1091. end;
  1092. procedure setfloatresultdef;
  1093. begin
  1094. if (left.resultdef.typ=floatdef) and
  1095. (tfloatdef(left.resultdef).floattype in [s32real,s64real,s80real,s128real]) then
  1096. resultdef:=left.resultdef
  1097. else
  1098. begin
  1099. inserttypeconv(left,pbestrealtype^);
  1100. resultdef:=pbestrealtype^;
  1101. end;
  1102. end;
  1103. procedure handle_pack_unpack;
  1104. var
  1105. source, target, index: tcallparanode;
  1106. unpackedarraydef, packedarraydef: tarraydef;
  1107. tempindex: TConstExprInt;
  1108. begin
  1109. resultdef:=voidtype;
  1110. unpackedarraydef := nil;
  1111. packedarraydef := nil;
  1112. source := tcallparanode(left);
  1113. if (inlinenumber = in_unpack_x_y_z) then
  1114. begin
  1115. target := tcallparanode(source.right);
  1116. index := tcallparanode(target.right);
  1117. { source must be a packed array }
  1118. if not is_packed_array(source.left.resultdef) then
  1119. CGMessagePos2(source.left.fileinfo,type_e_got_expected_packed_array,'1',source.left.resultdef.GetTypeName)
  1120. else
  1121. packedarraydef := tarraydef(source.left.resultdef);
  1122. { target can be any kind of array, as long as it's not packed }
  1123. if (target.left.resultdef.typ <> arraydef) or
  1124. is_packed_array(target.left.resultdef) then
  1125. CGMessagePos2(target.left.fileinfo,type_e_got_expected_unpacked_array,'2',target.left.resultdef.GetTypeName)
  1126. else
  1127. unpackedarraydef := tarraydef(target.left.resultdef);
  1128. end
  1129. else
  1130. begin
  1131. index := tcallparanode(source.right);
  1132. target := tcallparanode(index.right);
  1133. { source can be any kind of array, as long as it's not packed }
  1134. if (source.left.resultdef.typ <> arraydef) or
  1135. is_packed_array(source.left.resultdef) then
  1136. CGMessagePos2(source.left.fileinfo,type_e_got_expected_unpacked_array,'1',source.left.resultdef.GetTypeName)
  1137. else
  1138. unpackedarraydef := tarraydef(source.left.resultdef);
  1139. { target must be a packed array }
  1140. if not is_packed_array(target.left.resultdef) then
  1141. CGMessagePos2(target.left.fileinfo,type_e_got_expected_packed_array,'3',target.left.resultdef.GetTypeName)
  1142. else
  1143. packedarraydef := tarraydef(target.left.resultdef);
  1144. end;
  1145. if assigned(unpackedarraydef) then
  1146. begin
  1147. { index must be compatible with the unpacked array's indextype }
  1148. inserttypeconv(index.left,unpackedarraydef.rangedef);
  1149. { range check at compile time if possible }
  1150. if assigned(packedarraydef) and
  1151. (index.left.nodetype = ordconstn) and
  1152. not is_special_array(unpackedarraydef) then
  1153. begin
  1154. testrange(unpackedarraydef,tordconstnode(index.left).value,false);
  1155. tempindex := tordconstnode(index.left).value + packedarraydef.highrange-packedarraydef.lowrange;
  1156. testrange(unpackedarraydef,tempindex,false);
  1157. end;
  1158. end;
  1159. { source array is read and must be valid }
  1160. set_varstate(source.left,vs_read,[vsf_must_be_valid]);
  1161. { target array is written }
  1162. valid_for_assignment(target.left,true);
  1163. set_varstate(target.left,vs_written,[]);
  1164. { index in the unpacked array is read and must be valid }
  1165. set_varstate(index.left,vs_read,[vsf_must_be_valid]);
  1166. end;
  1167. var
  1168. vl,vl2 : TConstExprInt;
  1169. vr : bestreal;
  1170. hightree,
  1171. hp : tnode;
  1172. checkrange : boolean;
  1173. label
  1174. myexit;
  1175. begin
  1176. result:=nil;
  1177. { if we handle writeln; left contains no valid address }
  1178. if assigned(left) then
  1179. begin
  1180. if left.nodetype=callparan then
  1181. tcallparanode(left).get_paratype
  1182. else
  1183. typecheckpass(left);
  1184. end;
  1185. inc(parsing_para_level);
  1186. { handle intern constant functions in separate case }
  1187. if nf_inlineconst in flags then
  1188. begin
  1189. { no parameters? }
  1190. if not assigned(left) then
  1191. internalerror(200501231)
  1192. else
  1193. begin
  1194. vl:=0;
  1195. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  1196. case left.nodetype of
  1197. realconstn :
  1198. begin
  1199. { Real functions are all handled with internproc below }
  1200. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename)
  1201. end;
  1202. ordconstn :
  1203. vl:=tordconstnode(left).value;
  1204. callparan :
  1205. begin
  1206. { both exists, else it was not generated }
  1207. vl:=tordconstnode(tcallparanode(left).left).value;
  1208. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  1209. end;
  1210. else
  1211. CGMessage(parser_e_illegal_expression);
  1212. end;
  1213. case inlinenumber of
  1214. in_const_abs :
  1215. hp:=genintconstnode(abs(vl));
  1216. in_const_sqr :
  1217. hp:=genintconstnode(sqr(vl));
  1218. in_const_odd :
  1219. hp:=cordconstnode.create(byte(odd(vl)),booltype,true);
  1220. in_const_swap_word :
  1221. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resultdef,true);
  1222. in_const_swap_long :
  1223. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resultdef,true);
  1224. in_const_swap_qword :
  1225. hp:=cordconstnode.create((vl and $ffff) shl 32+(vl shr 32),left.resultdef,true);
  1226. in_const_ptr :
  1227. hp:=cpointerconstnode.create((vl2 shl 4)+vl,voidfarpointertype);
  1228. else
  1229. internalerror(88);
  1230. end;
  1231. end;
  1232. if hp=nil then
  1233. hp:=cerrornode.create;
  1234. result:=hp;
  1235. goto myexit;
  1236. end
  1237. else
  1238. begin
  1239. case inlinenumber of
  1240. in_lo_long,
  1241. in_hi_long,
  1242. in_lo_qword,
  1243. in_hi_qword,
  1244. in_lo_word,
  1245. in_hi_word :
  1246. begin
  1247. { give warning for incompatibility with tp and delphi }
  1248. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  1249. ((m_tp7 in current_settings.modeswitches) or
  1250. (m_delphi in current_settings.modeswitches)) then
  1251. CGMessage(type_w_maybe_wrong_hi_lo);
  1252. { constant folding }
  1253. if left.nodetype=ordconstn then
  1254. begin
  1255. case inlinenumber of
  1256. in_lo_word :
  1257. hp:=cordconstnode.create(tordconstnode(left).value and $ff,left.resultdef,true);
  1258. in_hi_word :
  1259. hp:=cordconstnode.create(tordconstnode(left).value shr 8,left.resultdef,true);
  1260. in_lo_long :
  1261. hp:=cordconstnode.create(tordconstnode(left).value and $ffff,left.resultdef,true);
  1262. in_hi_long :
  1263. hp:=cordconstnode.create(tordconstnode(left).value shr 16,left.resultdef,true);
  1264. in_lo_qword :
  1265. hp:=cordconstnode.create(tordconstnode(left).value and $ffffffff,left.resultdef,true);
  1266. in_hi_qword :
  1267. hp:=cordconstnode.create(tordconstnode(left).value shr 32,left.resultdef,true);
  1268. end;
  1269. result:=hp;
  1270. goto myexit;
  1271. end;
  1272. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1273. if not is_integer(left.resultdef) then
  1274. CGMessage1(type_e_integer_expr_expected,left.resultdef.typename);
  1275. case inlinenumber of
  1276. in_lo_word,
  1277. in_hi_word :
  1278. resultdef:=u8inttype;
  1279. in_lo_long,
  1280. in_hi_long :
  1281. resultdef:=u16inttype;
  1282. in_lo_qword,
  1283. in_hi_qword :
  1284. resultdef:=u32inttype;
  1285. end;
  1286. end;
  1287. in_sizeof_x:
  1288. begin
  1289. set_varstate(left,vs_read,[]);
  1290. if paramanager.push_high_param(vs_value,left.resultdef,current_procinfo.procdef.proccalloption) then
  1291. begin
  1292. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1293. if assigned(hightree) then
  1294. begin
  1295. hp:=caddnode.create(addn,hightree,
  1296. cordconstnode.create(1,sinttype,false));
  1297. if (left.resultdef.typ=arraydef) then
  1298. if not is_packed_array(tarraydef(left.resultdef)) then
  1299. begin
  1300. if (tarraydef(left.resultdef).elesize<>1) then
  1301. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1302. left.resultdef).elesize,sinttype,true));
  1303. end
  1304. else if (tarraydef(left.resultdef).elepackedbitsize <> 8) then
  1305. begin
  1306. { no packed open array support yet }
  1307. if (hp.nodetype <> ordconstn) then
  1308. internalerror(2006081511);
  1309. hp.free;
  1310. hp := cordconstnode.create(left.resultdef.size,sinttype,true);
  1311. {
  1312. hp:=
  1313. ctypeconvnode.create_explicit(sinttype,
  1314. cmoddivnode.create(divn,
  1315. caddnode.create(addn,
  1316. caddnode.create(muln,hp,cordconstnode.create(tarraydef(
  1317. left.resultdef).elepackedbitsize,s64inttype,true)),
  1318. cordconstnode.create(a,s64inttype,true)),
  1319. cordconstnode.create(8,s64inttype,true)),
  1320. sinttype);
  1321. }
  1322. end;
  1323. result:=hp;
  1324. end;
  1325. end
  1326. else
  1327. resultdef:=sinttype;
  1328. end;
  1329. in_typeof_x:
  1330. begin
  1331. set_varstate(left,vs_read,[]);
  1332. resultdef:=voidpointertype;
  1333. end;
  1334. in_ord_x:
  1335. begin
  1336. if (left.nodetype=ordconstn) then
  1337. begin
  1338. hp:=cordconstnode.create(
  1339. tordconstnode(left).value,sinttype,true);
  1340. result:=hp;
  1341. goto myexit;
  1342. end;
  1343. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1344. case left.resultdef.typ of
  1345. orddef :
  1346. begin
  1347. case torddef(left.resultdef).ordtype of
  1348. bool8bit,
  1349. uchar:
  1350. begin
  1351. { change to byte() }
  1352. hp:=ctypeconvnode.create_internal(left,u8inttype);
  1353. left:=nil;
  1354. result:=hp;
  1355. end;
  1356. bool16bit,
  1357. uwidechar :
  1358. begin
  1359. { change to word() }
  1360. hp:=ctypeconvnode.create_internal(left,u16inttype);
  1361. left:=nil;
  1362. result:=hp;
  1363. end;
  1364. bool32bit :
  1365. begin
  1366. { change to dword() }
  1367. hp:=ctypeconvnode.create_internal(left,u32inttype);
  1368. left:=nil;
  1369. result:=hp;
  1370. end;
  1371. bool64bit :
  1372. begin
  1373. { change to qword() }
  1374. hp:=ctypeconvnode.create_internal(left,u64inttype);
  1375. left:=nil;
  1376. result:=hp;
  1377. end;
  1378. uvoid :
  1379. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1380. else
  1381. begin
  1382. { all other orddef need no transformation }
  1383. hp:=left;
  1384. left:=nil;
  1385. result:=hp;
  1386. end;
  1387. end;
  1388. end;
  1389. enumdef :
  1390. begin
  1391. hp:=ctypeconvnode.create_internal(left,s32inttype);
  1392. left:=nil;
  1393. result:=hp;
  1394. end;
  1395. pointerdef :
  1396. begin
  1397. if m_mac in current_settings.modeswitches then
  1398. begin
  1399. hp:=ctypeconvnode.create_internal(left,ptrinttype);
  1400. left:=nil;
  1401. result:=hp;
  1402. end
  1403. else
  1404. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1405. end
  1406. else
  1407. CGMessage1(type_e_ordinal_expr_expected,left.resultdef.typename);
  1408. end;
  1409. end;
  1410. in_chr_byte:
  1411. begin
  1412. { convert to explicit char() }
  1413. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1414. hp:=ctypeconvnode.create_internal(left,cchartype);
  1415. left:=nil;
  1416. result:=hp;
  1417. end;
  1418. in_length_x:
  1419. begin
  1420. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1421. case left.resultdef.typ of
  1422. variantdef:
  1423. begin
  1424. inserttypeconv(left,cansistringtype);
  1425. end;
  1426. stringdef :
  1427. begin
  1428. { we don't need string convertions here }
  1429. if (left.nodetype=typeconvn) and
  1430. (ttypeconvnode(left).left.resultdef.typ=stringdef) then
  1431. begin
  1432. hp:=ttypeconvnode(left).left;
  1433. ttypeconvnode(left).left:=nil;
  1434. left.free;
  1435. left:=hp;
  1436. end;
  1437. { evaluates length of constant strings direct }
  1438. if (left.nodetype=stringconstn) then
  1439. begin
  1440. hp:=cordconstnode.create(
  1441. tstringconstnode(left).len,s32inttype,true);
  1442. result:=hp;
  1443. goto myexit;
  1444. end;
  1445. end;
  1446. orddef :
  1447. begin
  1448. { length of char is one allways }
  1449. if is_char(left.resultdef) or
  1450. is_widechar(left.resultdef) then
  1451. begin
  1452. hp:=cordconstnode.create(1,s32inttype,false);
  1453. result:=hp;
  1454. goto myexit;
  1455. end
  1456. else
  1457. CGMessage(type_e_mismatch);
  1458. end;
  1459. pointerdef :
  1460. begin
  1461. if is_pchar(left.resultdef) then
  1462. begin
  1463. hp := ccallparanode.create(left,nil);
  1464. result := ccallnode.createintern('fpc_pchar_length',hp);
  1465. { make sure the left node doesn't get disposed, since it's }
  1466. { reused in the new node (JM) }
  1467. left:=nil;
  1468. goto myexit;
  1469. end
  1470. else if is_pwidechar(left.resultdef) then
  1471. begin
  1472. hp := ccallparanode.create(left,nil);
  1473. result := ccallnode.createintern('fpc_pwidechar_length',hp);
  1474. { make sure the left node doesn't get disposed, since it's }
  1475. { reused in the new node (JM) }
  1476. left:=nil;
  1477. goto myexit;
  1478. end
  1479. else
  1480. CGMessage(type_e_mismatch);
  1481. end;
  1482. arraydef :
  1483. begin
  1484. if is_open_array(left.resultdef) or
  1485. is_array_of_const(left.resultdef) then
  1486. begin
  1487. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1488. if assigned(hightree) then
  1489. begin
  1490. hp:=caddnode.create(addn,hightree,
  1491. cordconstnode.create(1,s32inttype,false));
  1492. result:=hp;
  1493. end;
  1494. goto myexit;
  1495. end
  1496. else
  1497. if not is_dynamic_array(left.resultdef) then
  1498. begin
  1499. hp:=cordconstnode.create(tarraydef(left.resultdef).highrange-
  1500. tarraydef(left.resultdef).lowrange+1,
  1501. s32inttype,true);
  1502. result:=hp;
  1503. goto myexit;
  1504. end
  1505. else
  1506. begin
  1507. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1508. result := ccallnode.createintern('fpc_dynarray_length',hp);
  1509. { make sure the left node doesn't get disposed, since it's }
  1510. { reused in the new node (JM) }
  1511. left:=nil;
  1512. goto myexit;
  1513. end;
  1514. end;
  1515. else
  1516. CGMessage(type_e_mismatch);
  1517. end;
  1518. { shortstring return an 8 bit value as the length
  1519. is the first byte of the string }
  1520. if is_shortstring(left.resultdef) then
  1521. resultdef:=u8inttype
  1522. else
  1523. resultdef:=sinttype;
  1524. end;
  1525. in_typeinfo_x:
  1526. begin
  1527. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1528. resultdef:=voidpointertype;
  1529. end;
  1530. in_assigned_x:
  1531. begin
  1532. { the parser has already made sure the expression is valid }
  1533. { handle constant expressions }
  1534. if is_constnode(tcallparanode(left).left) or
  1535. (tcallparanode(left).left.nodetype = pointerconstn) then
  1536. begin
  1537. { let an add node figure it out }
  1538. result := caddnode.create(unequaln,tcallparanode(left).left,cnilnode.create);
  1539. tcallparanode(left).left := nil;
  1540. { free left, because otherwise some code at 'myexit' tries }
  1541. { to run get_paratype for it, which crashes since left.left }
  1542. { is now nil }
  1543. left.free;
  1544. left := nil;
  1545. goto myexit;
  1546. end;
  1547. { otherwise handle separately, because there could be a procvar, which }
  1548. { is 2*sizeof(pointer), while we must only check the first pointer }
  1549. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  1550. resultdef:=booltype;
  1551. end;
  1552. in_ofs_x :
  1553. internalerror(2000101001);
  1554. in_seg_x :
  1555. begin
  1556. set_varstate(left,vs_read,[]);
  1557. result:=cordconstnode.create(0,s32inttype,false);
  1558. goto myexit;
  1559. end;
  1560. in_pred_x,
  1561. in_succ_x:
  1562. begin
  1563. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1564. resultdef:=left.resultdef;
  1565. if not is_ordinal(resultdef) then
  1566. CGMessage(type_e_ordinal_expr_expected)
  1567. else
  1568. begin
  1569. if (resultdef.typ=enumdef) and
  1570. (tenumdef(resultdef).has_jumps) and
  1571. not(m_delphi in current_settings.modeswitches) then
  1572. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  1573. end;
  1574. { only if the result is an enum do we do range checking }
  1575. if (resultdef.typ=enumdef) then
  1576. checkrange := true
  1577. else
  1578. checkrange := false;
  1579. { do constant folding after check for jumps }
  1580. if left.nodetype=ordconstn then
  1581. begin
  1582. if inlinenumber=in_succ_x then
  1583. result:=cordconstnode.create(tordconstnode(left).value+1,left.resultdef,checkrange)
  1584. else
  1585. result:=cordconstnode.create(tordconstnode(left).value-1,left.resultdef,checkrange);
  1586. end;
  1587. end;
  1588. in_initialize_x,
  1589. in_finalize_x,
  1590. in_setlength_x:
  1591. begin
  1592. { inlined from pinline }
  1593. internalerror(200204231);
  1594. end;
  1595. in_inc_x,
  1596. in_dec_x:
  1597. begin
  1598. resultdef:=voidtype;
  1599. if assigned(left) then
  1600. begin
  1601. { first param must be var }
  1602. valid_for_var(tcallparanode(left).left,true);
  1603. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1604. if (left.resultdef.typ in [enumdef,pointerdef]) or
  1605. is_ordinal(left.resultdef) or
  1606. is_currency(left.resultdef) then
  1607. begin
  1608. { value of left gets changed -> must be unique }
  1609. set_unique(tcallparanode(left).left);
  1610. { two paras ? }
  1611. if assigned(tcallparanode(left).right) then
  1612. begin
  1613. if is_integer(tcallparanode(left).right.resultdef) then
  1614. begin
  1615. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1616. inserttypeconv_internal(tcallparanode(tcallparanode(left).right).left,tcallparanode(left).left.resultdef);
  1617. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1618. { should be handled in the parser (JM) }
  1619. internalerror(2006020901);
  1620. end
  1621. else
  1622. CGMessagePos(tcallparanode(left).right.fileinfo,type_e_ordinal_expr_expected);
  1623. end;
  1624. end
  1625. else
  1626. CGMessagePos(left.fileinfo,type_e_ordinal_expr_expected);
  1627. end
  1628. else
  1629. CGMessagePos(fileinfo,type_e_mismatch);
  1630. end;
  1631. in_read_x,
  1632. in_readln_x,
  1633. in_write_x,
  1634. in_writeln_x :
  1635. begin
  1636. result := handle_read_write;
  1637. end;
  1638. in_settextbuf_file_x :
  1639. begin
  1640. resultdef:=voidtype;
  1641. { now we know the type of buffer }
  1642. hp:=ccallparanode.create(cordconstnode.create(
  1643. tcallparanode(left).left.resultdef.size,s32inttype,true),left);
  1644. result:=ccallnode.createintern('SETTEXTBUF',hp);
  1645. left:=nil;
  1646. end;
  1647. { the firstpass of the arg has been done in firstcalln ? }
  1648. in_reset_typedfile,
  1649. in_rewrite_typedfile :
  1650. begin
  1651. result := handle_reset_rewrite_typed;
  1652. end;
  1653. in_str_x_string :
  1654. begin
  1655. result := handle_str;
  1656. end;
  1657. in_val_x :
  1658. begin
  1659. result := handle_val;
  1660. end;
  1661. in_include_x_y,
  1662. in_exclude_x_y:
  1663. begin
  1664. resultdef:=voidtype;
  1665. { the parser already checks whether we have two (and exactly two) }
  1666. { parameters (JM) }
  1667. { first param must be var }
  1668. valid_for_var(tcallparanode(left).left,true);
  1669. set_varstate(tcallparanode(left).left,vs_readwritten,[vsf_must_be_valid]);
  1670. { check type }
  1671. if (left.resultdef.typ=setdef) then
  1672. begin
  1673. { insert a type conversion }
  1674. { to the type of the set elements }
  1675. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1676. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  1677. tsetdef(left.resultdef).elementdef);
  1678. end
  1679. else
  1680. CGMessage(type_e_mismatch);
  1681. end;
  1682. in_pack_x_y_z,
  1683. in_unpack_x_y_z :
  1684. begin
  1685. handle_pack_unpack;
  1686. end;
  1687. in_slice_x:
  1688. begin
  1689. result:=nil;
  1690. resultdef:=tcallparanode(left).left.resultdef;
  1691. if (resultdef.typ <> arraydef) then
  1692. CGMessagePos(left.fileinfo,type_e_mismatch)
  1693. else if is_packed_array(resultdef) then
  1694. CGMessagePos2(left.fileinfo,type_e_got_expected_unpacked_array,'1',resultdef.typename);
  1695. if not(is_integer(tcallparanode(tcallparanode(left).right).left.resultdef)) then
  1696. CGMessagePos1(tcallparanode(left).right.fileinfo,
  1697. type_e_integer_expr_expected,
  1698. tcallparanode(tcallparanode(left).right).left.resultdef.typename);
  1699. end;
  1700. in_low_x,
  1701. in_high_x:
  1702. begin
  1703. case left.resultdef.typ of
  1704. orddef,
  1705. enumdef:
  1706. begin
  1707. result:=do_lowhigh(left.resultdef);
  1708. end;
  1709. setdef:
  1710. begin
  1711. result:=do_lowhigh(tsetdef(left.resultdef).elementdef);
  1712. end;
  1713. arraydef:
  1714. begin
  1715. if inlinenumber=in_low_x then
  1716. begin
  1717. result:=cordconstnode.create(tarraydef(
  1718. left.resultdef).lowrange,tarraydef(left.resultdef).rangedef,true);
  1719. end
  1720. else
  1721. begin
  1722. if is_open_array(left.resultdef) or
  1723. is_array_of_const(left.resultdef) then
  1724. begin
  1725. set_varstate(left,vs_read,[]);
  1726. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  1727. end
  1728. else
  1729. if is_dynamic_array(left.resultdef) then
  1730. begin
  1731. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1732. { can't use inserttypeconv because we need }
  1733. { an explicit type conversion (JM) }
  1734. hp := ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil);
  1735. result := ccallnode.createintern('fpc_dynarray_high',hp);
  1736. { make sure the left node doesn't get disposed, since it's }
  1737. { reused in the new node (JM) }
  1738. left:=nil;
  1739. end
  1740. else
  1741. begin
  1742. result:=cordconstnode.create(tarraydef(
  1743. left.resultdef).highrange,tarraydef(left.resultdef).rangedef,true);
  1744. end;
  1745. end;
  1746. end;
  1747. stringdef:
  1748. begin
  1749. if inlinenumber=in_low_x then
  1750. begin
  1751. result:=cordconstnode.create(0,u8inttype,false);
  1752. end
  1753. else
  1754. begin
  1755. if is_open_string(left.resultdef) then
  1756. begin
  1757. set_varstate(left,vs_read,[]);
  1758. result:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry))
  1759. end
  1760. else
  1761. result:=cordconstnode.create(tstringdef(left.resultdef).len,u8inttype,true);
  1762. end;
  1763. end;
  1764. else
  1765. CGMessage(type_e_mismatch);
  1766. end;
  1767. end;
  1768. in_exp_real :
  1769. begin
  1770. if left.nodetype in [ordconstn,realconstn] then
  1771. begin
  1772. result:=crealconstnode.create(exp(getconstrealvalue),pbestrealtype^);
  1773. if (trealconstnode(result).value_real=MathInf.Value) and
  1774. ((cs_check_range in current_settings.localswitches) or
  1775. (cs_check_overflow in current_settings.localswitches)) then
  1776. begin
  1777. result:=crealconstnode.create(0,pbestrealtype^);
  1778. CGMessage(parser_e_range_check_error);
  1779. end;
  1780. end
  1781. else
  1782. begin
  1783. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1784. inserttypeconv(left,pbestrealtype^);
  1785. resultdef:=pbestrealtype^;
  1786. end;
  1787. end;
  1788. in_trunc_real :
  1789. begin
  1790. if left.nodetype in [ordconstn,realconstn] then
  1791. begin
  1792. vr:=getconstrealvalue;
  1793. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1794. begin
  1795. CGMessage(parser_e_range_check_error);
  1796. result:=cordconstnode.create(1,s64inttype,false)
  1797. end
  1798. else
  1799. result:=cordconstnode.create(trunc(vr),s64inttype,true)
  1800. end
  1801. else
  1802. begin
  1803. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1804. inserttypeconv(left,pbestrealtype^);
  1805. resultdef:=s64inttype;
  1806. end;
  1807. end;
  1808. in_round_real :
  1809. begin
  1810. if left.nodetype in [ordconstn,realconstn] then
  1811. begin
  1812. vr:=getconstrealvalue;
  1813. if (vr>=9223372036854775807.5) or (vr<=-9223372036854775808.5) then
  1814. begin
  1815. CGMessage(parser_e_range_check_error);
  1816. result:=cordconstnode.create(1,s64inttype,false)
  1817. end
  1818. else
  1819. result:=cordconstnode.create(round(vr),s64inttype,true)
  1820. end
  1821. else
  1822. begin
  1823. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1824. inserttypeconv(left,pbestrealtype^);
  1825. resultdef:=s64inttype;
  1826. end;
  1827. end;
  1828. in_frac_real :
  1829. begin
  1830. if left.nodetype in [ordconstn,realconstn] then
  1831. setconstrealvalue(frac(getconstrealvalue))
  1832. else
  1833. begin
  1834. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1835. inserttypeconv(left,pbestrealtype^);
  1836. resultdef:=pbestrealtype^;
  1837. end;
  1838. end;
  1839. in_int_real :
  1840. begin
  1841. if left.nodetype in [ordconstn,realconstn] then
  1842. setconstrealvalue(int(getconstrealvalue))
  1843. else
  1844. begin
  1845. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1846. inserttypeconv(left,pbestrealtype^);
  1847. resultdef:=pbestrealtype^;
  1848. end;
  1849. end;
  1850. in_pi_real :
  1851. begin
  1852. if block_type=bt_const then
  1853. setconstrealvalue(getpi)
  1854. else
  1855. resultdef:=pbestrealtype^;
  1856. end;
  1857. in_cos_real :
  1858. begin
  1859. if left.nodetype in [ordconstn,realconstn] then
  1860. setconstrealvalue(cos(getconstrealvalue))
  1861. else
  1862. begin
  1863. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1864. inserttypeconv(left,pbestrealtype^);
  1865. resultdef:=pbestrealtype^;
  1866. end;
  1867. end;
  1868. in_sin_real :
  1869. begin
  1870. if left.nodetype in [ordconstn,realconstn] then
  1871. setconstrealvalue(sin(getconstrealvalue))
  1872. else
  1873. begin
  1874. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1875. inserttypeconv(left,pbestrealtype^);
  1876. resultdef:=pbestrealtype^;
  1877. end;
  1878. end;
  1879. in_arctan_real :
  1880. begin
  1881. if left.nodetype in [ordconstn,realconstn] then
  1882. setconstrealvalue(arctan(getconstrealvalue))
  1883. else
  1884. begin
  1885. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1886. inserttypeconv(left,pbestrealtype^);
  1887. resultdef:=pbestrealtype^;
  1888. end;
  1889. end;
  1890. in_abs_real :
  1891. begin
  1892. if left.nodetype in [ordconstn,realconstn] then
  1893. setconstrealvalue(abs(getconstrealvalue))
  1894. else
  1895. begin
  1896. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1897. inserttypeconv(left,pbestrealtype^);
  1898. resultdef:=pbestrealtype^;
  1899. end;
  1900. end;
  1901. in_sqr_real :
  1902. begin
  1903. if left.nodetype in [ordconstn,realconstn] then
  1904. setconstrealvalue(sqr(getconstrealvalue))
  1905. else
  1906. begin
  1907. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1908. setfloatresultdef;
  1909. end;
  1910. end;
  1911. in_sqrt_real :
  1912. begin
  1913. if left.nodetype in [ordconstn,realconstn] then
  1914. begin
  1915. vr:=getconstrealvalue;
  1916. if vr<0.0 then
  1917. result:=handle_sqrt_const(vr)
  1918. else
  1919. setconstrealvalue(sqrt(vr));
  1920. end
  1921. else
  1922. begin
  1923. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1924. setfloatresultdef;
  1925. end;
  1926. end;
  1927. in_ln_real :
  1928. begin
  1929. if left.nodetype in [ordconstn,realconstn] then
  1930. begin
  1931. vr:=getconstrealvalue;
  1932. if vr<=0.0 then
  1933. result:=handle_ln_const(vr)
  1934. else
  1935. setconstrealvalue(ln(vr));
  1936. end
  1937. else
  1938. begin
  1939. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1940. inserttypeconv(left,pbestrealtype^);
  1941. resultdef:=pbestrealtype^;
  1942. end;
  1943. end;
  1944. {$ifdef SUPPORT_MMX}
  1945. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  1946. begin
  1947. end;
  1948. {$endif SUPPORT_MMX}
  1949. in_prefetch_var:
  1950. begin
  1951. resultdef:=voidtype;
  1952. end;
  1953. {$ifdef SUPPORT_UNALIGNED}
  1954. in_unaligned_x:
  1955. begin
  1956. resultdef:=left.resultdef;
  1957. end;
  1958. {$endif SUPPORT_UNALIGNED}
  1959. in_assert_x_y :
  1960. begin
  1961. resultdef:=voidtype;
  1962. if assigned(left) then
  1963. begin
  1964. set_varstate(tcallparanode(left).left,vs_read,[vsf_must_be_valid]);
  1965. { check type }
  1966. if is_boolean(left.resultdef) then
  1967. begin
  1968. set_varstate(tcallparanode(tcallparanode(left).right).left,vs_read,[vsf_must_be_valid]);
  1969. { must always be a string }
  1970. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  1971. end
  1972. else
  1973. CGMessage1(type_e_boolean_expr_expected,left.resultdef.typename);
  1974. end
  1975. else
  1976. CGMessage(type_e_mismatch);
  1977. { We've checked the whole statement for correctness, now we
  1978. can remove it if assertions are off }
  1979. if not(cs_do_assertion in current_settings.localswitches) then
  1980. begin
  1981. { we need a valid node, so insert a nothingn }
  1982. result:=cnothingnode.create;
  1983. end
  1984. else
  1985. include(current_procinfo.flags,pi_do_call);
  1986. end;
  1987. in_get_frame,
  1988. in_get_caller_frame,
  1989. in_get_caller_addr:
  1990. begin
  1991. resultdef:=voidpointertype;
  1992. end;
  1993. else
  1994. internalerror(8);
  1995. end;
  1996. end;
  1997. myexit:
  1998. { Run get_paratype again to update maybe inserted typeconvs }
  1999. if not codegenerror then
  2000. begin
  2001. if assigned(left) and
  2002. (left.nodetype=callparan) then
  2003. tcallparanode(left).get_paratype;
  2004. end;
  2005. dec(parsing_para_level);
  2006. end;
  2007. function tinlinenode.pass_1 : tnode;
  2008. var
  2009. hp,hpp,resultnode : tnode;
  2010. shiftconst: longint;
  2011. tempnode: ttempcreatenode;
  2012. newstatement: tstatementnode;
  2013. newblock: tblocknode;
  2014. begin
  2015. result:=nil;
  2016. { if we handle writeln; left contains no valid address }
  2017. if assigned(left) then
  2018. begin
  2019. if left.nodetype=callparan then
  2020. tcallparanode(left).firstcallparan
  2021. else
  2022. firstpass(left);
  2023. left_max;
  2024. end;
  2025. inc(parsing_para_level);
  2026. { intern const should already be handled }
  2027. if nf_inlineconst in flags then
  2028. internalerror(200104044);
  2029. case inlinenumber of
  2030. in_lo_qword,
  2031. in_hi_qword,
  2032. in_lo_long,
  2033. in_hi_long,
  2034. in_lo_word,
  2035. in_hi_word:
  2036. begin
  2037. shiftconst := 0;
  2038. case inlinenumber of
  2039. in_hi_qword:
  2040. shiftconst := 32;
  2041. in_hi_long:
  2042. shiftconst := 16;
  2043. in_hi_word:
  2044. shiftconst := 8;
  2045. end;
  2046. if shiftconst <> 0 then
  2047. result := ctypeconvnode.create_internal(cshlshrnode.create(shrn,left,
  2048. cordconstnode.create(shiftconst,u32inttype,false)),resultdef)
  2049. else
  2050. result := ctypeconvnode.create_internal(left,resultdef);
  2051. left := nil;
  2052. firstpass(result);
  2053. end;
  2054. in_sizeof_x:
  2055. begin
  2056. if registersint<1 then
  2057. registersint:=1;
  2058. expectloc:=LOC_REGISTER;
  2059. end;
  2060. in_typeof_x:
  2061. begin
  2062. if registersint<1 then
  2063. registersint:=1;
  2064. expectloc:=LOC_REGISTER;
  2065. end;
  2066. in_length_x:
  2067. begin
  2068. if is_shortstring(left.resultdef) then
  2069. expectloc:=left.expectloc
  2070. else
  2071. begin
  2072. { ansi/wide string }
  2073. if registersint<1 then
  2074. registersint:=1;
  2075. expectloc:=LOC_REGISTER;
  2076. end;
  2077. end;
  2078. in_typeinfo_x:
  2079. begin
  2080. expectloc:=LOC_REGISTER;
  2081. registersint:=1;
  2082. end;
  2083. in_assigned_x:
  2084. begin
  2085. expectloc := LOC_JUMP;
  2086. registersint:=1;
  2087. end;
  2088. in_pred_x,
  2089. in_succ_x:
  2090. begin
  2091. if is_64bit(resultdef) then
  2092. begin
  2093. if (registersint<2) then
  2094. registersint:=2
  2095. end
  2096. else
  2097. begin
  2098. if (registersint<1) then
  2099. registersint:=1;
  2100. end;
  2101. expectloc:=LOC_REGISTER;
  2102. end;
  2103. in_setlength_x,
  2104. in_initialize_x,
  2105. in_finalize_x:
  2106. begin
  2107. expectloc:=LOC_VOID;
  2108. end;
  2109. in_inc_x,
  2110. in_dec_x:
  2111. begin
  2112. expectloc:=LOC_VOID;
  2113. { range/overflow checking doesn't work properly }
  2114. { with the inc/dec code that's generated (JM) }
  2115. if (current_settings.localswitches * [cs_check_overflow,cs_check_range] <> []) and
  2116. { No overflow check for pointer operations, because inc(pointer,-1) will always
  2117. trigger an overflow. For uint32 it works because then the operation is done
  2118. in 64bit. Range checking is not applicable to pointers either }
  2119. (tcallparanode(left).left.resultdef.typ<>pointerdef) then
  2120. { convert to simple add (JM) }
  2121. begin
  2122. newblock := internalstatements(newstatement);
  2123. { extra parameter? }
  2124. if assigned(tcallparanode(left).right) then
  2125. begin
  2126. { Yes, use for add node }
  2127. hpp := tcallparanode(tcallparanode(left).right).left;
  2128. tcallparanode(tcallparanode(left).right).left := nil;
  2129. if assigned(tcallparanode(tcallparanode(left).right).right) then
  2130. CGMessage(parser_e_illegal_expression);
  2131. end
  2132. else
  2133. begin
  2134. { no, create constant 1 }
  2135. hpp := cordconstnode.create(1,tcallparanode(left).left.resultdef,false);
  2136. end;
  2137. typecheckpass(hpp);
  2138. if not((hpp.resultdef.typ=orddef) and
  2139. {$ifndef cpu64bit}
  2140. (torddef(hpp.resultdef).ordtype<>u32bit)) then
  2141. {$else not cpu64bit}
  2142. (torddef(hpp.resultdef).ordtype<>u64bit)) then
  2143. {$endif not cpu64bit}
  2144. inserttypeconv_internal(hpp,sinttype);
  2145. { make sure we don't call functions part of the left node twice (and generally }
  2146. { optimize the code generation) }
  2147. if node_complexity(tcallparanode(left).left) > 1 then
  2148. begin
  2149. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2150. addstatement(newstatement,tempnode);
  2151. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2152. caddrnode.create_internal(tcallparanode(left).left.getcopy)));
  2153. hp := cderefnode.create(ctemprefnode.create(tempnode));
  2154. inserttypeconv_internal(hp,tcallparanode(left).left.resultdef);
  2155. end
  2156. else
  2157. begin
  2158. hp := tcallparanode(left).left.getcopy;
  2159. tempnode := nil;
  2160. end;
  2161. resultnode := hp.getcopy;
  2162. { avoid type errors from the addn/subn }
  2163. if not is_integer(resultnode.resultdef) then
  2164. begin
  2165. inserttypeconv_internal(hp,sinttype);
  2166. inserttypeconv_internal(hpp,sinttype);
  2167. end;
  2168. { addition/substraction depending on inc/dec }
  2169. if inlinenumber = in_inc_x then
  2170. hpp := caddnode.create(addn,hp,hpp)
  2171. else
  2172. hpp := caddnode.create(subn,hp,hpp);
  2173. { assign result of addition }
  2174. if not(is_integer(resultnode.resultdef)) then
  2175. inserttypeconv(hpp,torddef.create(
  2176. {$ifdef cpu64bit}
  2177. s64bit,
  2178. {$else cpu64bit}
  2179. s32bit,
  2180. {$endif cpu64bit}
  2181. get_min_value(resultnode.resultdef),
  2182. get_max_value(resultnode.resultdef)))
  2183. else
  2184. inserttypeconv(hpp,resultnode.resultdef);
  2185. { avoid any possible warnings }
  2186. inserttypeconv_internal(hpp,resultnode.resultdef);
  2187. addstatement(newstatement,cassignmentnode.create(resultnode,hpp));
  2188. { deallocate the temp }
  2189. if assigned(tempnode) then
  2190. addstatement(newstatement,ctempdeletenode.create(tempnode));
  2191. { firstpass it }
  2192. firstpass(newblock);
  2193. { return new node }
  2194. result := newblock;
  2195. end
  2196. else if (left.resultdef.typ in [enumdef,pointerdef]) or
  2197. is_ordinal(left.resultdef) then
  2198. begin
  2199. { two paras ? }
  2200. if assigned(tcallparanode(left).right) then
  2201. begin
  2202. { need we an additional register ? }
  2203. if not(is_constintnode(tcallparanode(tcallparanode(left).right).left)) and
  2204. (tcallparanode(tcallparanode(left).right).left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) and
  2205. (tcallparanode(tcallparanode(left).right).left.registersint<=1) then
  2206. inc(registersint);
  2207. { do we need an additional register to restore the first parameter? }
  2208. if tcallparanode(tcallparanode(left).right).left.registersint>=registersint then
  2209. inc(registersint);
  2210. end;
  2211. end;
  2212. end;
  2213. in_include_x_y,
  2214. in_exclude_x_y:
  2215. begin
  2216. expectloc:=LOC_VOID;
  2217. registersint:=left.registersint;
  2218. registersfpu:=left.registersfpu;
  2219. {$ifdef SUPPORT_MMX}
  2220. registersmmx:=left.registersmmx;
  2221. {$endif SUPPORT_MMX}
  2222. end;
  2223. in_pack_x_y_z,
  2224. in_unpack_x_y_z:
  2225. begin
  2226. result:=first_pack_unpack;
  2227. end;
  2228. in_exp_real:
  2229. begin
  2230. result:= first_exp_real;
  2231. end;
  2232. in_round_real:
  2233. begin
  2234. result:= first_round_real;
  2235. end;
  2236. in_trunc_real:
  2237. begin
  2238. result:= first_trunc_real;
  2239. end;
  2240. in_int_real:
  2241. begin
  2242. result:= first_int_real;
  2243. end;
  2244. in_frac_real:
  2245. begin
  2246. result:= first_frac_real;
  2247. end;
  2248. in_cos_real:
  2249. begin
  2250. result:= first_cos_real;
  2251. end;
  2252. in_sin_real:
  2253. begin
  2254. result := first_sin_real;
  2255. end;
  2256. in_arctan_real:
  2257. begin
  2258. result := first_arctan_real;
  2259. end;
  2260. in_pi_real :
  2261. begin
  2262. result := first_pi;
  2263. end;
  2264. in_abs_real:
  2265. begin
  2266. result := first_abs_real;
  2267. end;
  2268. in_sqr_real:
  2269. begin
  2270. result := first_sqr_real;
  2271. end;
  2272. in_sqrt_real:
  2273. begin
  2274. result := first_sqrt_real;
  2275. end;
  2276. in_ln_real:
  2277. begin
  2278. result := first_ln_real;
  2279. end;
  2280. {$ifdef SUPPORT_MMX}
  2281. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  2282. begin
  2283. end;
  2284. {$endif SUPPORT_MMX}
  2285. in_assert_x_y :
  2286. begin
  2287. expectloc:=LOC_VOID;
  2288. registersint:=left.registersint;
  2289. registersfpu:=left.registersfpu;
  2290. {$ifdef SUPPORT_MMX}
  2291. registersmmx:=left.registersmmx;
  2292. {$endif SUPPORT_MMX}
  2293. end;
  2294. in_low_x,
  2295. in_high_x:
  2296. internalerror(200104047);
  2297. in_slice_x:
  2298. internalerror(2005101501);
  2299. in_ord_x,
  2300. in_chr_byte:
  2301. begin
  2302. { should not happend as it's converted to typeconv }
  2303. internalerror(200104045);
  2304. end;
  2305. in_ofs_x :
  2306. internalerror(2000101001);
  2307. in_seg_x :
  2308. internalerror(200104046);
  2309. in_settextbuf_file_x,
  2310. in_reset_typedfile,
  2311. in_rewrite_typedfile,
  2312. in_str_x_string,
  2313. in_val_x,
  2314. in_read_x,
  2315. in_readln_x,
  2316. in_write_x,
  2317. in_writeln_x :
  2318. begin
  2319. { should be handled by pass_typecheck }
  2320. internalerror(200108234);
  2321. end;
  2322. in_get_frame:
  2323. begin
  2324. include(current_procinfo.flags,pi_needs_stackframe);
  2325. expectloc:=LOC_CREGISTER;
  2326. end;
  2327. in_get_caller_frame:
  2328. begin
  2329. expectloc:=LOC_REGISTER;
  2330. registersint:=1;
  2331. end;
  2332. in_get_caller_addr:
  2333. begin
  2334. expectloc:=LOC_REGISTER;
  2335. registersint:=1;
  2336. end;
  2337. in_prefetch_var:
  2338. begin
  2339. expectloc:=LOC_VOID;
  2340. end;
  2341. {$ifdef SUPPORT_UNALIGNED}
  2342. in_unaligned_x:
  2343. begin
  2344. expectloc:=tcallparanode(left).left.expectloc;
  2345. end;
  2346. {$endif SUPPORT_UNALIGNED}
  2347. else
  2348. internalerror(89);
  2349. end;
  2350. dec(parsing_para_level);
  2351. end;
  2352. {$maxfpuregisters default}
  2353. function tinlinenode.docompare(p: tnode): boolean;
  2354. begin
  2355. docompare :=
  2356. inherited docompare(p) and
  2357. (inlinenumber = tinlinenode(p).inlinenumber);
  2358. end;
  2359. function tinlinenode.first_pi : tnode;
  2360. begin
  2361. result:=crealconstnode.create(getpi,pbestrealtype^);
  2362. end;
  2363. function tinlinenode.first_arctan_real : tnode;
  2364. begin
  2365. { create the call to the helper }
  2366. { on entry left node contains the parameter }
  2367. first_arctan_real := ccallnode.createintern('fpc_arctan_real',
  2368. ccallparanode.create(left,nil));
  2369. left := nil;
  2370. end;
  2371. function tinlinenode.first_abs_real : tnode;
  2372. begin
  2373. { create the call to the helper }
  2374. { on entry left node contains the parameter }
  2375. first_abs_real := ccallnode.createintern('fpc_abs_real',
  2376. ccallparanode.create(left,nil));
  2377. left := nil;
  2378. end;
  2379. function tinlinenode.first_sqr_real : tnode;
  2380. begin
  2381. { create the call to the helper }
  2382. { on entry left node contains the parameter }
  2383. first_sqr_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqr_real',
  2384. ccallparanode.create(left,nil)),resultdef);
  2385. left := nil;
  2386. end;
  2387. function tinlinenode.first_sqrt_real : tnode;
  2388. begin
  2389. { create the call to the helper }
  2390. { on entry left node contains the parameter }
  2391. first_sqrt_real := ctypeconvnode.create(ccallnode.createintern('fpc_sqrt_real',
  2392. ccallparanode.create(left,nil)),resultdef);
  2393. left := nil;
  2394. end;
  2395. function tinlinenode.first_ln_real : tnode;
  2396. begin
  2397. { create the call to the helper }
  2398. { on entry left node contains the parameter }
  2399. first_ln_real := ccallnode.createintern('fpc_ln_real',
  2400. ccallparanode.create(left,nil));
  2401. left := nil;
  2402. end;
  2403. function tinlinenode.first_cos_real : tnode;
  2404. begin
  2405. { create the call to the helper }
  2406. { on entry left node contains the parameter }
  2407. first_cos_real := ccallnode.createintern('fpc_cos_real',
  2408. ccallparanode.create(left,nil));
  2409. left := nil;
  2410. end;
  2411. function tinlinenode.first_sin_real : tnode;
  2412. begin
  2413. { create the call to the helper }
  2414. { on entry left node contains the parameter }
  2415. first_sin_real := ccallnode.createintern('fpc_sin_real',
  2416. ccallparanode.create(left,nil));
  2417. left := nil;
  2418. end;
  2419. function tinlinenode.first_exp_real : tnode;
  2420. begin
  2421. { create the call to the helper }
  2422. { on entry left node contains the parameter }
  2423. result := ccallnode.createintern('fpc_exp_real',ccallparanode.create(left,nil));
  2424. left := nil;
  2425. end;
  2426. function tinlinenode.first_int_real : tnode;
  2427. begin
  2428. { create the call to the helper }
  2429. { on entry left node contains the parameter }
  2430. result := ccallnode.createintern('fpc_int_real',ccallparanode.create(left,nil));
  2431. left := nil;
  2432. end;
  2433. function tinlinenode.first_frac_real : tnode;
  2434. begin
  2435. { create the call to the helper }
  2436. { on entry left node contains the parameter }
  2437. result := ccallnode.createintern('fpc_frac_real',ccallparanode.create(left,nil));
  2438. left := nil;
  2439. end;
  2440. function tinlinenode.first_round_real : tnode;
  2441. begin
  2442. { create the call to the helper }
  2443. { on entry left node contains the parameter }
  2444. result := ccallnode.createintern('fpc_round_real',ccallparanode.create(left,nil));
  2445. left := nil;
  2446. end;
  2447. function tinlinenode.first_trunc_real : tnode;
  2448. begin
  2449. { create the call to the helper }
  2450. { on entry left node contains the parameter }
  2451. result := ccallnode.createintern('fpc_trunc_real',ccallparanode.create(left,nil));
  2452. left := nil;
  2453. end;
  2454. function tinlinenode.first_pack_unpack: tnode;
  2455. var
  2456. loopstatement : tstatementnode;
  2457. loop : tblocknode;
  2458. loopvar : ttempcreatenode;
  2459. tempnode,
  2460. source,
  2461. target,
  2462. index,
  2463. unpackednode,
  2464. packednode,
  2465. sourcevecindex,
  2466. targetvecindex,
  2467. loopbody : tnode;
  2468. temprangedef : tdef;
  2469. ulorange,
  2470. uhirange,
  2471. plorange,
  2472. phirange : TConstExprInt;
  2473. begin
  2474. { transform into a for loop which assigns the data of the (un)packed }
  2475. { array to the other one }
  2476. source := left;
  2477. if (inlinenumber = in_unpack_x_y_z) then
  2478. begin
  2479. target := tcallparanode(source).right;
  2480. index := tcallparanode(target).right;
  2481. packednode := tcallparanode(source).left;
  2482. unpackednode := tcallparanode(target).left;
  2483. end
  2484. else
  2485. begin
  2486. index := tcallparanode(source).right;
  2487. target := tcallparanode(index).right;
  2488. packednode := tcallparanode(target).left;
  2489. unpackednode := tcallparanode(source).left;
  2490. end;
  2491. source := tcallparanode(source).left;
  2492. target := tcallparanode(target).left;
  2493. index := tcallparanode(index).left;
  2494. loop := internalstatements(loopstatement);
  2495. loopvar := ctempcreatenode.create(
  2496. tarraydef(packednode.resultdef).rangedef,
  2497. tarraydef(packednode.resultdef).rangedef.size,
  2498. tt_persistent,true);
  2499. addstatement(loopstatement,loopvar);
  2500. { For range checking: we have to convert to an integer type (in case the index type }
  2501. { is an enum), add the index and loop variable together, convert the result }
  2502. { implicitly to an orddef with range equal to the rangedef to get range checking }
  2503. { and finally convert it explicitly back to the actual rangedef to avoid type }
  2504. { errors }
  2505. temprangedef:=nil;
  2506. getrange(unpackednode.resultdef,ulorange,uhirange);
  2507. getrange(packednode.resultdef,plorange,phirange);
  2508. temprangedef:=torddef.create(torddef(sinttype).ordtype,ulorange,uhirange);
  2509. sourcevecindex := ctemprefnode.create(loopvar);
  2510. targetvecindex := ctypeconvnode.create_internal(index.getcopy,sinttype);
  2511. targetvecindex := caddnode.create(subn,targetvecindex,cordconstnode.create(plorange,sinttype,true));
  2512. targetvecindex := caddnode.create(addn,targetvecindex,ctemprefnode.create(loopvar));
  2513. targetvecindex := ctypeconvnode.create(targetvecindex,temprangedef);
  2514. targetvecindex := ctypeconvnode.create_explicit(targetvecindex,tarraydef(unpackednode.resultdef).rangedef);
  2515. if (inlinenumber = in_pack_x_y_z) then
  2516. begin
  2517. { swap source and target vec indices }
  2518. tempnode := sourcevecindex;
  2519. sourcevecindex := targetvecindex;
  2520. targetvecindex := tempnode;
  2521. end;
  2522. { create the assignment in the loop body }
  2523. loopbody :=
  2524. cassignmentnode.create(
  2525. cvecnode.create(target.getcopy,targetvecindex),
  2526. cvecnode.create(source.getcopy,sourcevecindex)
  2527. );
  2528. { create the actual for loop }
  2529. tempnode := cfornode.create(
  2530. ctemprefnode.create(loopvar),
  2531. cinlinenode.create(in_low_x,false,packednode.getcopy),
  2532. cinlinenode.create(in_high_x,false,packednode.getcopy),
  2533. loopbody,
  2534. false);
  2535. addstatement(loopstatement,tempnode);
  2536. { free the loop counter }
  2537. addstatement(loopstatement,ctempdeletenode.create(loopvar));
  2538. result := loop;
  2539. end;
  2540. begin
  2541. cinlinenode:=tinlinenode;
  2542. end.