ninl.pas 156 KB

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