ninl.pas 114 KB

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