ninl.pas 167 KB

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