ninl.pas 113 KB

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