ninl.pas 113 KB

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