ninl.pas 115 KB

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