ninl.pas 115 KB

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