ninl.pas 166 KB

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