ninl.pas 110 KB

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