ninl.pas 117 KB

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