ninl.pas 115 KB

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