ninl.pas 158 KB

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