ninl.pas 113 KB

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