ninl.pas 121 KB

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