ninl.pas 117 KB

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