ninl.pas 118 KB

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