ninl.pas 137 KB

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