CC.pm 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681
  1. # CC.pm
  2. #
  3. # Copyright (c) 1996, 1997, 1998 Malcolm Beattie
  4. # Copyright (c) 2009, 2010, 2011 Reini Urban
  5. # Copyright (c) 2010 Heinz Knutzen
  6. # Copyright (c) 2012 cPanel Inc
  7. #
  8. # You may distribute under the terms of either the GNU General Public
  9. # License or the Artistic License, as specified in the README file.
  10. =head1 NAME
  11. B::CC - Perl compiler's optimized C translation backend
  12. =head1 SYNOPSIS
  13. perl -MO=CC[,OPTIONS] foo.pl
  14. =head1 DESCRIPTION
  15. This compiler backend takes Perl source and generates C source code
  16. corresponding to the flow of your program with unrolled ops and optimised
  17. stack handling and lexicals variable types. In other words, this backend is
  18. somewhat a "real" compiler in the sense that many people think about
  19. compilers. Note however that, currently, it is a very poor compiler in that
  20. although it generates (mostly, or at least sometimes) correct code, it
  21. performs relatively few optimisations. This will change as the compiler and
  22. the types develops. The result is that running an executable compiled with
  23. this backend may start up more quickly than running the original Perl program
  24. (a feature shared by the B<C> compiler backend--see L<B::C>) and may also
  25. execute slightly faster. This is by no means a good optimising compiler--yet.
  26. =head1 OPTIONS
  27. If there are any non-option arguments, they are taken to be
  28. names of objects to be saved (probably doesn't work properly yet).
  29. Without extra arguments, it saves the main program.
  30. =over 4
  31. =item B<-ofilename>
  32. Output to filename instead of STDOUT
  33. =item B<-c>
  34. Check and abort.
  35. Compiles and prints only warnings, but does not emit C code.
  36. =item B<-v>
  37. Verbose compilation (prints a few compilation stages).
  38. =item B<-->
  39. Force end of options
  40. =item B<-uPackname>
  41. Force apparently unused subs from package Packname to be compiled.
  42. This allows programs to use eval "foo()" even when sub foo is never
  43. seen to be used at compile time. The down side is that any subs which
  44. really are never used also have code generated. This option is
  45. necessary, for example, if you have a signal handler foo which you
  46. initialise with C<$SIG{BAR} = "foo">. A better fix, though, is just
  47. to change it to C<$SIG{BAR} = \&foo>. You can have multiple B<-u>
  48. options. The compiler tries to figure out which packages may possibly
  49. have subs in which need compiling but the current version doesn't do
  50. it very well. In particular, it is confused by nested packages (i.e.
  51. of the form C<A::B>) where package C<A> does not contain any subs.
  52. =item B<-UPackname> "unuse" skip Package
  53. Ignore all subs from Package to be compiled.
  54. Certain packages might not be needed at run-time, even if the pessimistic
  55. walker detects it.
  56. =item B<-mModulename>
  57. Instead of generating source for a runnable executable, generate
  58. source for an XSUB module. The boot_Modulename function (which
  59. DynaLoader can look for) does the appropriate initialisation and runs
  60. the main part of the Perl source that is being compiled.
  61. =item B<-nInitname>
  62. Provide a different init name for additional objects added via cmdline.
  63. =item B<-strict>
  64. With a DEBUGGING perl compile-time errors for range and flip without
  65. compile-time context are only warnings.
  66. With C<-strict> these warnings are fatal, otherwise only run-time errors occur.
  67. =item B<-On>
  68. Optimisation level (n = 0, 1, 2). B<-O> means B<-O1>.
  69. The following L<B::C> optimisations are applied automatically:
  70. optimize_warn_sv save_data_fh av-init2|av_init save_sig destruct
  71. pv_copy_on_grow
  72. B<-O1> sets B<-ffreetmps-each-bblock>.
  73. B<-O2> adds B<-ffreetmps-each-loop>, C<-faelem> and B<-fno-destruct> from L<B::C>.
  74. The following options must be set explicitly:
  75. B<-fno-taint> or B<-fomit-taint>,
  76. B<-fslow-signals>,
  77. B<-no-autovivify>,
  78. B<-fno-magic>.
  79. =item B<-f>C<OPTIM>
  80. Force optimisations on or off one at a time.
  81. Unknown optimizations are passed down to L<B::C>.
  82. =item B<-ffreetmps-each-bblock>
  83. Delays FREETMPS from the end of each statement to the end of the each
  84. basic block.
  85. Enabled with B<-O1>.
  86. =item B<-ffreetmps-each-loop>
  87. Delays FREETMPS from the end of each statement to the end of the group
  88. of basic blocks forming a loop. At most one of the freetmps-each-*
  89. options can be used.
  90. Enabled with B<-O2>.
  91. =item B<-faelem>
  92. Enable array element access optimizations, allowing unchecked
  93. fast access under certain circumstances.
  94. Enabled with B<-O2> and not-threaded perls only.
  95. =item B<-fno-inline-ops>
  96. Do not inline calls to certain small pp ops.
  97. Most of the inlinable ops were already inlined.
  98. Turns off inlining for some new ops.
  99. AUTOMATICALLY inlined:
  100. pp_null pp_stub pp_unstack pp_and pp_andassign pp_or pp_orassign pp_cond_expr
  101. pp_padsv pp_const pp_nextstate pp_dbstate pp_rv2gv pp_sort pp_gv pp_gvsv
  102. pp_aelemfast pp_ncmp pp_add pp_subtract pp_multiply pp_divide pp_modulo
  103. pp_left_shift pp_right_shift pp_i_add pp_i_subtract pp_i_multiply pp_i_divide
  104. pp_i_modulo pp_eq pp_ne pp_lt pp_gt pp_le pp_ge pp_i_eq pp_i_ne pp_i_lt
  105. pp_i_gt pp_i_le pp_i_ge pp_scmp pp_slt pp_sgt pp_sle pp_sge pp_seq pp_sne
  106. pp_sassign pp_preinc pp_pushmark pp_list pp_entersub pp_formline pp_goto
  107. pp_enterwrite pp_leavesub pp_leavewrite pp_entergiven pp_leavegiven
  108. pp_entereval pp_dofile pp_require pp_entertry pp_leavetry pp_grepstart
  109. pp_mapstart pp_grepwhile pp_mapwhile pp_return pp_range pp_flip pp_flop
  110. pp_enterloop pp_enteriter pp_leaveloop pp_next pp_redo pp_last pp_subst
  111. pp_substcont
  112. DONE with -finline-ops:
  113. pp_enter pp_reset pp_regcreset pp_stringify
  114. TODO with -finline-ops:
  115. pp_anoncode pp_wantarray pp_srefgen pp_refgen pp_ref pp_trans pp_schop pp_chop
  116. pp_schomp pp_chomp pp_not pp_sprintf pp_anonlist pp_shift pp_once pp_lock
  117. pp_rcatline pp_close pp_time pp_alarm pp_av2arylen: no lvalue, pp_length: no
  118. magic
  119. =item B<-fomit-taint>
  120. Omits generating code for handling perl's tainting mechanism.
  121. =item B<-fslow-signals>
  122. Add PERL_ASYNC_CHECK after every op as in the old Perl runloop before 5.13.
  123. perl "Safe signals" check the state of incoming signals after every op.
  124. See L<http://perldoc.perl.org/perlipc.html#Deferred-Signals-(Safe-Signals)>
  125. We trade safety for more speed and delay the execution of non-IO signals
  126. (IO signals are already handled in PerlIO) from after every single Perl op
  127. to the same ops as used in 5.14.
  128. Only with -fslow-signals we get the old slow and safe behaviour.
  129. =item B<-fno-name-magic>
  130. With the default C<-fname-magic> we infer the SCALAR type for specially named
  131. locals vars and most ops use C vars then, not the perl vars.
  132. Arithmetic and comparison is inlined. Scalar magic is bypassed.
  133. With C<-fno-name-magic> do not infer a local variable type from its name:
  134. B<_i> suffix for int, B<_d> for double/num, B<_ir> for register int
  135. See the experimental C<-ftype-attr> type attributes.
  136. Currently supported are B<int> and B<num> only. See </load_pad>.
  137. =item B<-ftype-attr> (DOES NOT WORK YET)
  138. Experimentally support B<type attributes> for B<int> and B<num>,
  139. SCALAR only so far.
  140. For most ops new C vars are used then, not the fat perl vars.
  141. Very awkward to use until the basic type classes are supported from
  142. within core or use types.
  143. Enabled with B<-O2>. See L<TYPES> and </load_pad>.
  144. =item B<-fno-autovivify>
  145. Do not vivify array and soon also hash elements when accessing them.
  146. Beware: Vivified elements default to undef, unvivified elements are
  147. invalid.
  148. This is the same as the pragma "no autovivification" and allows
  149. very fast array accesses, 4-6 times faster, without the overhead of
  150. L<autovivification>.
  151. =item B<-fno-magic>
  152. Assume certain data being optimized is never tied or is holding other magic.
  153. This mainly holds for arrays being optimized, but in the future hashes also.
  154. =item B<-D>
  155. Debug options (concatenated or separate flags like C<perl -D>).
  156. Verbose debugging options are crucial, because the interactive
  157. debugger L<Od> adds a lot of ballast to the resulting code.
  158. =item B<-Dr>
  159. Writes debugging output to STDERR just as it's about to write to the
  160. program's runtime (otherwise writes debugging info as comments in
  161. its C output).
  162. =item B<-DO>
  163. Outputs each OP as it's compiled
  164. =item B<-Ds>
  165. Outputs the contents of the shadow stack at each OP
  166. =item B<-Dp>
  167. Outputs the contents of the shadow pad of lexicals as it's loaded for
  168. each sub or the main program.
  169. =item B<-Dq>
  170. Outputs the name of each fake PP function in the queue as it's about
  171. to process it.
  172. =item B<-Dl>
  173. Output the filename and line number of each original line of Perl
  174. code as it's processed (C<pp_nextstate>).
  175. =item B<-Dt>
  176. Outputs timing information of compilation stages.
  177. =item B<-DF>
  178. Add Flags info to the code.
  179. =back
  180. =head1 NOTABLE FUNCTIONS
  181. =cut
  182. package B::CC;
  183. our $VERSION = '1.13';
  184. # Start registering the L<types> namespaces.
  185. $main::int::B_CC = $main::num::B_CC = $main::str::B_CC = $main::double::B_CC = $main::string::B_CC = $VERSION;
  186. use Config;
  187. use strict;
  188. #use 5.008;
  189. use B qw(main_start main_root class comppadlist peekop svref_2object
  190. timing_info init_av end_av sv_undef
  191. OPf_WANT_VOID OPf_WANT_SCALAR OPf_WANT_LIST OPf_WANT
  192. OPf_MOD OPf_STACKED OPf_SPECIAL OPpLVAL_DEFER OPpLVAL_INTRO
  193. OPpASSIGN_BACKWARDS OPpLVAL_INTRO OPpDEREF_AV OPpDEREF_HV
  194. OPpDEREF OPpFLIP_LINENUM G_VOID G_SCALAR G_ARRAY);
  195. #CXt_NULL CXt_SUB CXt_EVAL CXt_SUBST CXt_BLOCK
  196. use B::C qw(save_unused_subs objsym init_sections mark_unused mark_skip
  197. output_all output_boilerplate output_main output_main_rest fixup_ppaddr save_sig
  198. svop_or_padop_pv inc_cleanup);
  199. use B::Bblock qw(find_leaders);
  200. use B::Stackobj qw(:types :flags);
  201. use B::C::Flags;
  202. # use attributes qw(get reftype);
  203. @B::OP::ISA = qw(B); # support -Do
  204. @B::LISTOP::ISA = qw(B::BINOP B); # support -Do
  205. push @B::OP::ISA, 'B::NULLOP' if exists $main::B::{'NULLOP'};
  206. # These should probably be elsewhere
  207. # Flags for $op->flags
  208. my $module; # module name (when compiled with -m)
  209. my %done; # hash keyed by $$op of leaders of basic blocks
  210. # which have already been done.
  211. my $leaders; # ref to hash of basic block leaders. Keys are $$op
  212. # addresses, values are the $op objects themselves.
  213. my @bblock_todo; # list of leaders of basic blocks that need visiting
  214. # sometime.
  215. my @cc_todo; # list of tuples defining what PP code needs to be
  216. # saved (e.g. CV, main or PMOP repl code). Each tuple
  217. # is [$name, $root, $start, @padlist]. PMOP repl code
  218. # tuples inherit padlist.
  219. my %cc_pp_sub; # hashed names of pp_sub functions already saved
  220. my @stack; # shadows perl's stack when contents are known.
  221. # Values are objects derived from class B::Stackobj
  222. my @pad; # Lexicals in current pad as Stackobj-derived objects
  223. my @padlist; # Copy of current padlist so PMOP repl code can find it
  224. my @cxstack; # Shadows the (compile-time) cxstack for next,last,redo
  225. # This covers only a small part of the perl cxstack
  226. my $labels; # hashref to array of op labels
  227. my %constobj; # OP_CONST constants as Stackobj-derived objects
  228. # keyed by $$sv.
  229. my $need_freetmps = 0; # We may postpone FREETMPS to the end of each basic
  230. # block or even to the end of each loop of blocks,
  231. # depending on optimisation options.
  232. my $know_op = 0; # Set when C variable op already holds the right op
  233. # (from an immediately preceding DOOP(ppname)).
  234. my $errors = 0; # Number of errors encountered
  235. my $op_count = 0; # for B::compile_stats on verbose
  236. my %no_stack; # PP names which don't need save pp restore stack
  237. my %skip_stack; # PP names which don't need write_back_stack (empty)
  238. my %skip_lexicals; # PP names which don't need write_back_lexicals
  239. my %skip_invalidate; # PP names which don't need invalidate_lexicals
  240. my %ignore_op; # ops which do nothing except returning op_next
  241. my %need_curcop; # ops which need PL_curcop
  242. my $package_pv; # sv->pv of previous op for method_named
  243. my %lexstate; # state of padsvs at the start of a bblock
  244. my ( $verbose, $check );
  245. my ( $entertry_defined, $vivify_ref_defined );
  246. my ( $init_name, %debug, $strict );
  247. # Optimisation options. On the command line, use hyphens instead of
  248. # underscores for compatibility with gcc-style options. We use
  249. # underscores here because they are OK in (strict) barewords.
  250. # Disable with -fno-
  251. my ( $freetmps_each_bblock, $freetmps_each_loop, $inline_ops, $opt_taint, $opt_omit_taint,
  252. $opt_slow_signals, $opt_name_magic, $opt_type_attr, $opt_autovivify, $opt_magic,
  253. $opt_aelem, %c_optimise );
  254. $inline_ops = 1 unless $^O eq 'MSWin32'; # Win32 cannot link to unexported pp_op() XXX
  255. $opt_name_magic = 1;
  256. my %optimise = (
  257. freetmps_each_bblock => \$freetmps_each_bblock, # -O1
  258. freetmps_each_loop => \$freetmps_each_loop, # -O2
  259. aelem => \$opt_aelem, # -O2
  260. inline_ops => \$inline_ops, # not on Win32
  261. omit_taint => \$opt_omit_taint,
  262. taint => \$opt_taint,
  263. slow_signals => \$opt_slow_signals,
  264. name_magic => \$opt_name_magic,
  265. type_attr => \$opt_type_attr,
  266. autovivify => \$opt_autovivify,
  267. magic => \$opt_magic,
  268. );
  269. my %async_signals = map { $_ => 1 } # 5.14 ops which do PERL_ASYNC_CHECK
  270. qw(wait waitpid nextstate and cond_expr unstack or subst dorassign);
  271. $async_signals{$_} = 1 for # more 5.16 ops which do PERL_ASYNC_CHECK
  272. qw(substcont next redo goto leavewhen);
  273. # perl patchlevel to generate code for (defaults to current patchlevel)
  274. my $patchlevel = int( 0.5 + 1000 * ( $] - 5 ) ); # XXX unused?
  275. my $MULTI = $Config{usemultiplicity};
  276. my $ITHREADS = $Config{useithreads};
  277. my $PERL510 = ( $] >= 5.009005 );
  278. my $PERL512 = ( $] >= 5.011 );
  279. my $SVt_PVLV = $PERL510 ? 10 : 9;
  280. my $SVt_PVAV = $PERL510 ? 11 : 10;
  281. # use sub qw(CXt_LOOP_PLAIN CXt_LOOP);
  282. BEGIN {
  283. if ($PERL512) {
  284. sub CXt_LOOP_PLAIN {5} # CXt_LOOP_FOR CXt_LOOP_LAZYSV CXt_LOOP_LAZYIV
  285. } else {
  286. sub CXt_LOOP {3}
  287. }
  288. sub CxTYPE_no_LOOP {
  289. $PERL512
  290. ? ( $_[0]->{type} < 4 or $_[0]->{type} > 7 )
  291. : $_[0]->{type} != 3
  292. }
  293. if ($] < 5.008) {
  294. eval "sub SVs_RMG {0x8000};";
  295. } else {
  296. B->import('SVs_RMG');
  297. }
  298. if ($] <= 5.010) {
  299. eval "sub PMf_ONCE() {0xff}; # unused";
  300. } elsif ($] >= 5.018) { # PMf_ONCE not exported
  301. eval q[sub PMf_ONCE(){ 0x10000 }];
  302. } elsif ($] >= 5.014) {
  303. eval q[sub PMf_ONCE(){ 0x8000 }];
  304. } elsif ($] >= 5.012) {
  305. eval q[sub PMf_ONCE(){ 0x0080 }];
  306. } else { # 5.10. not used with <= 5.8
  307. eval q[sub PMf_ONCE(){ 0x0002 }];
  308. }
  309. }
  310. # Could rewrite push_runtime() and output_runtime() to use a
  311. # temporary file if memory is at a premium.
  312. my $ppname; # name of current fake PP function
  313. my $runtime_list_ref;
  314. my $declare_ref; # Hash ref keyed by C variable type of declarations.
  315. my @pp_list; # list of [$ppname, $runtime_list_ref, $declare_ref]
  316. # tuples to be written out.
  317. my ( $init, $decl );
  318. sub init_hash {
  319. map { $_ => 1 } @_;
  320. }
  321. #
  322. # Initialise the hashes for the default PP functions where we can avoid
  323. # either stack save/restore,write_back_stack, write_back_lexicals or invalidate_lexicals.
  324. # XXX We should really take some of this info from Opcodes (was: CORE opcode.pl)
  325. #
  326. # no args and no return value = Opcodes::argnum 0
  327. %no_stack = init_hash qw(pp_unstack pp_break pp_continue);
  328. # pp_enter pp_leave, use/change global stack.
  329. #skip write_back_stack (no args)
  330. %skip_stack = init_hash qw(pp_enter pp_leave pp_nextstate pp_dbstate);
  331. # which ops do not read pad vars
  332. %skip_lexicals = init_hash qw(pp_enter pp_enterloop pp_leave pp_nextstate pp_dbstate);
  333. # which ops no not write to pad vars
  334. %skip_invalidate = init_hash qw(pp_enter pp_enterloop pp_leave pp_nextstate pp_dbstate
  335. pp_return pp_leavesub pp_list pp_pushmark
  336. pp_anonlist
  337. );
  338. %need_curcop = init_hash qw(pp_rv2gv pp_bless pp_repeat pp_sort pp_caller
  339. pp_reset pp_rv2cv pp_entereval pp_require pp_dofile
  340. pp_entertry pp_enterloop pp_enteriter pp_entersub pp_entergiven
  341. pp_enter pp_method);
  342. %ignore_op = init_hash qw(pp_scalar pp_regcmaybe pp_lineseq pp_scope pp_null);
  343. { # block necessary for caller to work
  344. my $caller = caller;
  345. if ( $caller eq 'O' ) {
  346. require XSLoader;
  347. XSLoader::load('B::C'); # for r-magic only
  348. }
  349. }
  350. sub debug {
  351. if ( $debug{runtime} ) {
  352. # TODO: fix COP to callers line number
  353. warn(@_) if $verbose;
  354. }
  355. else {
  356. my @tmp = @_;
  357. runtime( map { chomp; "/* $_ */" } @tmp );
  358. }
  359. }
  360. sub declare {
  361. my ( $type, $var ) = @_;
  362. push( @{ $declare_ref->{$type} }, $var );
  363. }
  364. sub push_runtime {
  365. push( @$runtime_list_ref, @_ );
  366. warn join( "\n", @_ ) . "\n" if $debug{runtime};
  367. }
  368. sub save_runtime {
  369. push( @pp_list, [ $ppname, $runtime_list_ref, $declare_ref ] );
  370. }
  371. sub output_runtime {
  372. my $ppdata;
  373. print qq(\n#include "cc_runtime.h"\n);
  374. # CC coverage: 12, 32
  375. # Perls >=5.8.9 have a broken PP_ENTERTRY. See PERL_FLEXIBLE_EXCEPTIONS in cop.h
  376. # Fixed in CORE with 5.11.4
  377. print'
  378. #undef PP_ENTERTRY
  379. #define PP_ENTERTRY(label) \
  380. STMT_START { \
  381. dJMPENV; \
  382. int ret; \
  383. JMPENV_PUSH(ret); \
  384. switch (ret) { \
  385. case 1: JMPENV_POP; JMPENV_JUMP(1);\
  386. case 2: JMPENV_POP; JMPENV_JUMP(2);\
  387. case 3: JMPENV_POP; SPAGAIN; goto label;\
  388. } \
  389. } STMT_END'
  390. if $entertry_defined and $] < 5.011004;
  391. # XXX need to find out when PERL_FLEXIBLE_EXCEPTIONS were actually active.
  392. # 5.6.2 not, 5.8.9 not. coverage 32
  393. # test 12. Used by entereval + dofile
  394. if ($PERL510 or $MULTI) {
  395. # Threads error Bug#55302: too few arguments to function
  396. # CALLRUNOPS()=>CALLRUNOPS(aTHX)
  397. # fixed with 5.11.4
  398. print '
  399. #undef PP_EVAL
  400. #define PP_EVAL(ppaddr, nxt) do { \
  401. dJMPENV; \
  402. int ret; \
  403. PUTBACK; \
  404. JMPENV_PUSH(ret); \
  405. switch (ret) { \
  406. case 0: \
  407. PL_op = ppaddr(aTHX); \\';
  408. if ($PERL510) {
  409. # pp_leaveeval sets: retop = cx->blk_eval.retop
  410. print '
  411. cxstack[cxstack_ix].blk_eval.retop = Nullop; \\';
  412. } else {
  413. # up to 5.8 pp_entereval did set the retstack to next.
  414. # nullify that so that we can now exec the rest of this bblock.
  415. # (nextstate .. leaveeval)
  416. print '
  417. PL_retstack[PL_retstack_ix - 1] = Nullop; \\';
  418. }
  419. print '
  420. if (PL_op != nxt) CALLRUNOPS(aTHX); \
  421. JMPENV_POP; \
  422. break; \
  423. case 1: JMPENV_POP; JMPENV_JUMP(1); \
  424. case 2: JMPENV_POP; JMPENV_JUMP(2); \
  425. case 3: \
  426. JMPENV_POP; \
  427. if (PL_restartop && PL_restartop != nxt) \
  428. JMPENV_JUMP(3); \
  429. } \
  430. PL_op = nxt; \
  431. SPAGAIN; \
  432. } while (0)
  433. ';
  434. }
  435. # Perl_vivify_ref not exported on MSWin32
  436. # coverage: 18
  437. if ($PERL510 and $^O eq 'MSWin32') {
  438. # CC coverage: 18, 29
  439. print << '__EOV' if $vivify_ref_defined;
  440. /* Code to take a scalar and ready it to hold a reference */
  441. # ifndef SVt_RV
  442. # define SVt_RV SVt_IV
  443. # endif
  444. # define prepare_SV_for_RV(sv) \
  445. STMT_START { \
  446. if (SvTYPE(sv) < SVt_RV) \
  447. sv_upgrade(sv, SVt_RV); \
  448. else if (SvPVX_const(sv)) { \
  449. SvPV_free(sv); \
  450. SvLEN_set(sv, 0); \
  451. SvCUR_set(sv, 0); \
  452. } \
  453. } STMT_END
  454. #if (PERL_VERSION > 15) || ((PERL_VERSION == 15) && (PERL_SUBVERSION >= 2))
  455. SV*
  456. #else
  457. void
  458. #endif
  459. Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
  460. {
  461. SvGETMAGIC(sv);
  462. if (!SvOK(sv)) {
  463. if (SvREADONLY(sv))
  464. Perl_croak(aTHX_ "%s", PL_no_modify);
  465. prepare_SV_for_RV(sv);
  466. switch (to_what) {
  467. case OPpDEREF_SV:
  468. SvRV_set(sv, newSV(0));
  469. break;
  470. case OPpDEREF_AV:
  471. SvRV_set(sv, newAV());
  472. break;
  473. case OPpDEREF_HV:
  474. SvRV_set(sv, newHV());
  475. break;
  476. }
  477. SvROK_on(sv);
  478. SvSETMAGIC(sv);
  479. }
  480. }
  481. __EOV
  482. }
  483. print '
  484. OP *Perl_pp_aelem_nolval(pTHXx);
  485. #ifndef SVfARG
  486. # define SVfARG(x) (void *)x
  487. #endif
  488. #ifndef MUTABLE_AV
  489. # define MUTABLE_AV(av) av
  490. #endif
  491. PP(pp_aelem_nolval)
  492. {
  493. dSP;
  494. SV** svp;
  495. SV* const elemsv = POPs;
  496. IV elem = SvIV(elemsv);
  497. AV *const av = MUTABLE_AV(POPs);
  498. SV *sv;
  499. #if PERL_VERSION > 6
  500. if (SvROK(elemsv) && !SvGAMAGIC(elemsv) && ckWARN(WARN_MISC))
  501. Perl_warner(aTHX_ packWARN(WARN_MISC),
  502. "Use of reference \"%"SVf"\" as array index",
  503. SVfARG(elemsv));
  504. #endif
  505. if (SvTYPE(av) != SVt_PVAV) RETPUSHUNDEF;
  506. svp = av_fetch(av, elem, 0);
  507. sv = (svp ? *svp : &PL_sv_undef);
  508. if (SvRMAGICAL(av) && SvGMAGICAL(sv)) mg_get(sv);
  509. PUSHs(sv);
  510. RETURN;
  511. }
  512. ' if 0;
  513. foreach $ppdata (@pp_list) {
  514. my ( $name, $runtime, $declare ) = @$ppdata;
  515. print "\nstatic\nCCPP($name)\n{\n";
  516. my ( $type, $varlist, $line );
  517. while ( ( $type, $varlist ) = each %$declare ) {
  518. print "\t$type ", join( ", ", @$varlist ), ";\n";
  519. }
  520. foreach $line (@$runtime) {
  521. print $line, "\n";
  522. }
  523. print "}\n";
  524. }
  525. }
  526. sub runtime {
  527. my $line;
  528. foreach $line (@_) {
  529. push_runtime("\t$line");
  530. }
  531. }
  532. sub init_pp {
  533. $ppname = shift;
  534. $runtime_list_ref = [];
  535. $declare_ref = {};
  536. runtime("dSP;");
  537. declare( "I32", "oldsave" );
  538. map { declare( "SV", "*$_" ) } qw(sv src dst left right);
  539. declare( "MAGIC", "*mg" );
  540. $decl->add( "#undef cxinc", "#define cxinc() Perl_cxinc(aTHX)")
  541. if $] < 5.011001 and $inline_ops;
  542. declare( "PERL_CONTEXT", "*cx" );
  543. declare( "I32", "gimme");
  544. $decl->add("static OP * $ppname (pTHX);");
  545. debug "init_pp: $ppname\n" if $debug{queue};
  546. }
  547. # Initialise runtime_callback function for Stackobj class
  548. BEGIN { B::Stackobj::set_callback( \&runtime ) }
  549. =head2 cc_queue
  550. Creates a new ccpp optree.
  551. Initialised by saveoptree_callback in L<B::C>, replaces B::C::walk_and_save_optree.
  552. Called by every C<CV::save> if ROOT.
  553. B<blocksort> also creates its block closure with cc_queue.
  554. =cut
  555. # coverage: test 18, 28 (fixed with B-C-1.30 r971)
  556. sub cc_queue {
  557. my ( $name, $root, $start, @pl ) = @_;
  558. debug "cc_queue: name $name, root $root, start $start, padlist (@pl)\n"
  559. if $debug{queue};
  560. if ( $name eq "*ignore*" or $name =~ /^pp_sub_.*(FETCH|MODIFY)_SCALAR_ATTRIBUTES$/) {
  561. $name = '';
  562. } else {
  563. push( @cc_todo, [ $name, $root, $start, ( @pl ? @pl : @padlist ) ] );
  564. }
  565. my $fakeop_next = 0;
  566. if ($name =~ /^pp_sub_IO_.*DESTROY$/) {
  567. $fakeop_next = $start->next->save;
  568. }
  569. my $fakeop = B::FAKEOP->new( "next" => $fakeop_next, sibling => 0, ppaddr => $name,
  570. targ=>0, type=>0, flags=>0, private=>0);
  571. $start = $fakeop->save;
  572. debug "cc_queue: name $name returns $start\n" if $debug{queue};
  573. return $start;
  574. }
  575. BEGIN { B::C::set_callback( \&cc_queue ) }
  576. sub valid_int { $_[0]->{flags} & VALID_INT }
  577. sub valid_double { $_[0]->{flags} & VALID_NUM }
  578. sub valid_numeric { $_[0]->{flags} & ( VALID_INT | VALID_NUM ) }
  579. sub valid_str { $_[0]->{flags} & VALID_STR }
  580. sub valid_sv { $_[0]->{flags} & VALID_SV }
  581. sub top_int { @stack ? $stack[-1]->as_int : "TOPi" }
  582. sub top_double { @stack ? $stack[-1]->as_double : "TOPn" }
  583. sub top_numeric { @stack ? $stack[-1]->as_numeric : "TOPn" }
  584. sub top_sv { @stack ? $stack[-1]->as_sv : "TOPs" }
  585. sub top_str { @stack ? $stack[-1]->as_str : "TOPs" }
  586. sub top_bool { @stack ? $stack[-1]->as_bool : "SvTRUE(TOPs)" }
  587. sub pop_int { @stack ? ( pop @stack )->as_int : "POPi" }
  588. sub pop_double { @stack ? ( pop @stack )->as_double : "POPn" }
  589. sub pop_numeric { @stack ? ( pop @stack )->as_numeric : "POPn" }
  590. sub pop_str { @stack ? ( pop @stack )->as_str : "POPs" }
  591. sub pop_sv { @stack ? ( pop @stack )->as_sv : "POPs" }
  592. sub pop_bool {
  593. if (@stack) {
  594. return ( ( pop @stack )->as_bool );
  595. }
  596. else {
  597. # Careful: POPs has an auto-decrement and SvTRUE evaluates
  598. # its argument more than once.
  599. runtime("sv = POPs;");
  600. return "SvTRUE(sv)";
  601. }
  602. }
  603. sub write_back_lexicals {
  604. my $avoid = shift || 0;
  605. debug "write_back_lexicals($avoid) called from @{[(caller(1))[3]]}\n"
  606. if $debug{shadow};
  607. my $lex;
  608. foreach $lex (@pad) {
  609. next unless ref($lex);
  610. $lex->write_back unless $lex->{flags} & $avoid;
  611. }
  612. }
  613. =head1 save_or_restore_lexical_state
  614. The compiler tracks state of lexical variables in @pad to generate optimised
  615. code. But multiple execution paths lead to the entry point of a basic block.
  616. The state of the first execution path is saved and all other execution
  617. paths are restored to the state of the first one.
  618. Missing flags are regenerated by loading values.
  619. Added flags must are removed; otherwise the compiler would be too optimistic,
  620. hence generating code which doesn't match state of the other execution paths.
  621. =cut
  622. sub save_or_restore_lexical_state {
  623. my $bblock = shift;
  624. unless ( exists $lexstate{$bblock} ) {
  625. foreach my $lex (@pad) {
  626. next unless ref($lex);
  627. ${ $lexstate{$bblock} }{ $lex->{iv} } = $lex->{flags};
  628. }
  629. }
  630. else {
  631. foreach my $lex (@pad) {
  632. next unless ref($lex);
  633. my $old_flags = ${ $lexstate{$bblock} }{ $lex->{iv} };
  634. next if ( $old_flags eq $lex->{flags} );
  635. my $changed = $old_flags ^ $lex->{flags};
  636. if ( $changed & VALID_SV ) {
  637. ( $old_flags & VALID_SV ) ? $lex->write_back : $lex->invalidate;
  638. }
  639. if ( $changed & VALID_NUM ) {
  640. ( $old_flags & VALID_NUM ) ? $lex->load_double : $lex->invalidate_double;
  641. }
  642. if ( $changed & VALID_INT ) {
  643. ( $old_flags & VALID_INT ) ? $lex->load_int : $lex->invalidate_int;
  644. }
  645. if ( $changed & VALID_STR ) {
  646. ( $old_flags & VALID_STR ) ? $lex->load_str : $lex->invalidate_str;
  647. }
  648. }
  649. }
  650. }
  651. sub write_back_stack {
  652. debug "write_back_stack() ".scalar(@stack)." called from @{[(caller(1))[3]]}\n"
  653. if $debug{shadow};
  654. return unless @stack;
  655. runtime( sprintf( "EXTEND(sp, %d);", scalar(@stack) ) );
  656. foreach my $obj (@stack) {
  657. runtime( sprintf( "PUSHs((SV*)%s);", $obj->as_sv ) );
  658. }
  659. @stack = ();
  660. }
  661. sub invalidate_lexicals {
  662. my $avoid = shift || 0;
  663. debug "invalidate_lexicals($avoid) called from @{[(caller(1))[3]]}\n"
  664. if $debug{shadow};
  665. my $lex;
  666. foreach $lex (@pad) {
  667. next unless ref($lex);
  668. $lex->invalidate unless $lex->{flags} & $avoid;
  669. }
  670. }
  671. sub reload_lexicals {
  672. my $lex;
  673. foreach $lex (@pad) {
  674. next unless ref($lex);
  675. my $type = $lex->{type};
  676. if ( $type == T_INT ) {
  677. $lex->as_int;
  678. }
  679. elsif ( $type == T_NUM ) {
  680. $lex->as_double;
  681. }
  682. elsif ( $type == T_STR ) {
  683. $lex->as_str;
  684. }
  685. else {
  686. $lex->as_sv;
  687. }
  688. }
  689. }
  690. {
  691. package B::Pseudoreg;
  692. #
  693. # This class allocates pseudo-registers (OK, so they're C variables).
  694. #
  695. my %alloc; # Keyed by variable name. A value of 1 means the
  696. # variable has been declared. A value of 2 means
  697. # it's in use.
  698. sub new_scope { %alloc = () }
  699. sub new ($$$) {
  700. my ( $class, $type, $prefix ) = @_;
  701. my ( $ptr, $i, $varname, $status, $obj );
  702. $prefix =~ s/^(\**)//;
  703. $ptr = $1;
  704. $i = 0;
  705. do {
  706. $varname = "$prefix$i";
  707. $status = exists $alloc{$varname} ? $alloc{$varname} : 0;
  708. } while $status == 2;
  709. if ( $status != 1 ) {
  710. # Not declared yet
  711. B::CC::declare( $type, "$ptr$varname" );
  712. $alloc{$varname} = 2; # declared and in use
  713. }
  714. $obj = bless \$varname, $class;
  715. return $obj;
  716. }
  717. sub DESTROY {
  718. my $obj = shift;
  719. $alloc{$$obj} = 1; # no longer in use but still declared
  720. }
  721. }
  722. {
  723. package B::Shadow;
  724. #
  725. # This class gives a standard API for a perl object to shadow a
  726. # C variable and only generate reloads/write-backs when necessary.
  727. #
  728. # Use $obj->load($foo) instead of runtime("shadowed_c_var = foo").
  729. # Use $obj->write_back whenever shadowed_c_var needs to be up to date.
  730. # Use $obj->invalidate whenever an unknown function may have
  731. # set shadow itself.
  732. sub new {
  733. my ( $class, $write_back ) = @_;
  734. # Object fields are perl shadow variable, validity flag
  735. # (for *C* variable) and callback sub for write_back
  736. # (passed perl shadow variable as argument).
  737. bless [ undef, 1, $write_back ], $class;
  738. }
  739. sub load {
  740. my ( $obj, $newval ) = @_;
  741. $obj->[1] = 0; # C variable no longer valid
  742. $obj->[0] = $newval;
  743. }
  744. sub value {
  745. return $_[0]->[0];
  746. }
  747. sub write_back {
  748. my $obj = shift;
  749. if ( !( $obj->[1] ) ) {
  750. $obj->[1] = 1; # C variable will now be valid
  751. &{ $obj->[2] }( $obj->[0] );
  752. }
  753. }
  754. sub invalidate { $_[0]->[1] = 0 } # force C variable to be invalid
  755. }
  756. my $curcop = B::Shadow->new(
  757. sub {
  758. my $op = shift;
  759. my $opsym = $op->save;
  760. runtime("PL_curcop = (COP*)$opsym;");
  761. }
  762. );
  763. #
  764. # Context stack shadowing. Mimics stuff in pp_ctl.c, cop.h and so on.
  765. #
  766. sub dopoptoloop {
  767. my $cxix = $#cxstack;
  768. while ( $cxix >= 0 && CxTYPE_no_LOOP( $cxstack[$cxix] ) ) {
  769. $cxix--;
  770. }
  771. debug "dopoptoloop: returning $cxix" if $debug{cxstack};
  772. return $cxix;
  773. }
  774. sub dopoptolabel {
  775. my $label = shift;
  776. my $cxix = $#cxstack;
  777. while (
  778. $cxix >= 0
  779. && ( CxTYPE_no_LOOP( $cxstack[$cxix] )
  780. || $cxstack[$cxix]->{label} ne $label )
  781. )
  782. {
  783. $cxix--;
  784. }
  785. debug "dopoptolabel: returning $cxix\n" if $debug{cxstack};
  786. if ($cxix < 0 and $debug{cxstack}) {
  787. for my $cx (0 .. $#cxstack) {
  788. debug "$cx: ",$cxstack[$cx]->{label},"\n";
  789. }
  790. for my $op (keys %{$labels->{label}}) {
  791. debug $labels->{label}->{$op},"\n";
  792. }
  793. }
  794. return $cxix;
  795. }
  796. sub push_label {
  797. my $op = shift;
  798. my $type = shift;
  799. push @{$labels->{$type}}, ( $op );
  800. }
  801. sub pop_label {
  802. my $type = shift;
  803. my $op = pop @{$labels->{$type}};
  804. write_label ($op); # avoids duplicate labels
  805. }
  806. sub error {
  807. my $format = shift;
  808. my $file = $curcop->[0]->file;
  809. my $line = $curcop->[0]->line;
  810. $errors++;
  811. if (@_) {
  812. warn sprintf( "ERROR at %s:%d: $format\n", $file, $line, @_ );
  813. }
  814. else {
  815. warn sprintf( "ERROR at %s:%d: %s\n", $file, $line, $format );
  816. }
  817. }
  818. # run-time eval is too late for attrs being checked by perlcore. BEGIN does not help.
  819. # use types is the right approach. But until types is fixed we use this hack.
  820. # Note that we also need a new CHECK_SCALAR_ATTRIBUTES hook, starting with v5.18.
  821. sub init_type_attrs {
  822. eval q[
  823. our $valid_attr = '^(int|num|str|double|string|unsigned|register|temporary|ro|readonly|const)$';
  824. sub MODIFY_SCALAR_ATTRIBUTES {
  825. my $pkg = shift;
  826. my $v = shift;
  827. my $attr = $B::CC::valid_attr;
  828. $attr =~ s/\b$pkg\b//;
  829. if (my @bad = grep !/$attr/, @_) {
  830. return @bad;
  831. } else {
  832. no strict 'refs';
  833. push @{"$pkg\::$v\::attributes"}, @_; # create a magic glob
  834. return ();
  835. }
  836. }
  837. sub FETCH_SCALAR_ATTRIBUTES {
  838. my ($pkg, $v) = @_;
  839. no strict 'refs';
  840. return @{"$pkg\::$v\::attributes"};
  841. }
  842. # pollute our callers namespace for attributes to be accepted with -MB::CC
  843. *main::MODIFY_SCALAR_ATTRIBUTES = \&B::CC::MODIFY_SCALAR_ATTRIBUTES;
  844. *main::FETCH_SCALAR_ATTRIBUTES = \&B::CC::FETCH_SCALAR_ATTRIBUTES;
  845. # my int $i : register : ro;
  846. *int::MODIFY_SCALAR_ATTRIBUTES = \&B::CC::MODIFY_SCALAR_ATTRIBUTES;
  847. *int::FETCH_SCALAR_ATTRIBUTES = \&B::CC::FETCH_SCALAR_ATTRIBUTES;
  848. # my double $d : ro;
  849. *num::MODIFY_SCALAR_ATTRIBUTES = \&B::CC::MODIFY_SCALAR_ATTRIBUTES;
  850. *num::FETCH_SCALAR_ATTRIBUTES = \&B::CC::FETCH_SCALAR_ATTRIBUTES;
  851. *str::MODIFY_SCALAR_ATTRIBUTES = \&B::CC::MODIFY_SCALAR_ATTRIBUTES;
  852. *str::FETCH_SCALAR_ATTRIBUTES = \&B::CC::FETCH_SCALAR_ATTRIBUTES;
  853. # deprecated:
  854. *double::MODIFY_SCALAR_ATTRIBUTES = \&B::CC::MODIFY_SCALAR_ATTRIBUTES;
  855. *double::FETCH_SCALAR_ATTRIBUTES = \&B::CC::FETCH_SCALAR_ATTRIBUTES;
  856. *string::MODIFY_SCALAR_ATTRIBUTES = \&B::CC::MODIFY_SCALAR_ATTRIBUTES;
  857. *string::FETCH_SCALAR_ATTRIBUTES = \&B::CC::FETCH_SCALAR_ATTRIBUTES;
  858. ];
  859. }
  860. =head2 load_pad
  861. Load pad takes (the elements of) a PADLIST as arguments and loads up @pad
  862. with Stackobj-derived objects which represent those lexicals.
  863. If/when perl itself can generate type information C<(my int $foo; my $foo : int)> then we'll
  864. take advantage of that here. Until then, we'll use the L<-fname-magic/-fno-name-magic>
  865. hack to tell the compiler when we want a lexical to be a particular type or to be a register.
  866. =cut
  867. sub load_pad {
  868. my ( $namelistav, $valuelistav ) = @_;
  869. @padlist = @_;
  870. my @namelist = $namelistav->ARRAY;
  871. my @valuelist = $valuelistav->ARRAY;
  872. my $ix;
  873. @pad = ();
  874. debug "load_pad: $#namelist names, $#valuelist values\n" if $debug{pad};
  875. # Temporary lexicals don't get named so it's possible for @valuelist
  876. # to be strictly longer than @namelist. We count $ix up to the end of
  877. # @valuelist but index into @namelist for the name. Any temporaries which
  878. # run off the end of @namelist will make $namesv undefined and we treat
  879. # that the same as having an explicit SPECIAL sv_undef object in @namelist.
  880. # [XXX If/when @_ becomes a lexical, we must start at 0 here.]
  881. for ( $ix = 1 ; $ix < @valuelist ; $ix++ ) {
  882. my $namesv = $namelist[$ix];
  883. my $type = T_UNKNOWN;
  884. my $flags = 0;
  885. my $name = "tmp";
  886. my $class = class($namesv);
  887. if ( !defined($namesv) || $class eq "SPECIAL" ) {
  888. # temporaries have &PL_sv_undef instead of a PVNV for a name
  889. $flags = VALID_SV | TEMPORARY | REGISTER;
  890. }
  891. else {
  892. my ($nametry) = $namesv->PV =~ /^\$(.+)$/ if $namesv->PV;
  893. $name = $nametry if $nametry;
  894. # my int $i; my num $d; compiled code only, unless the source provides the int and num packages.
  895. # With Ctypes it is easier. my c_int $i; defines an external Ctypes int, which can be efficiently
  896. # compiled in Perl also.
  897. # XXX Better use attributes, like my $i:int; my $d:num; which works un-compiled also.
  898. if (ref($namesv) eq 'B::PVMG' and ref($namesv->SvSTASH) eq 'B::HV') { # my int
  899. $class = $namesv->SvSTASH->NAME;
  900. if ($class eq 'int') {
  901. $type = T_INT;
  902. $flags = VALID_SV | VALID_INT;
  903. }
  904. elsif ($class eq 'num' or $class eq 'double') { # my num
  905. $type = T_NUM;
  906. $flags = VALID_SV | VALID_NUM;
  907. }
  908. elsif ($class eq 'str' or $class eq 'string') { # my str
  909. $type = T_STR;
  910. $flags = VALID_SV | VALID_STR;
  911. }
  912. #elsif ($class eq 'c_int') { # use Ctypes;
  913. # $type = T_INT;
  914. # $flags = VALID_SV | VALID_INT;
  915. #}
  916. #elsif ($class eq 'c_double') {
  917. # $type = T_NUM;
  918. # $flags = VALID_SV | VALID_NUM;
  919. #}
  920. # TODO: MooseX::Types
  921. }
  922. # Valid scalar type attributes:
  923. # int num str ro readonly const unsigned
  924. # Note: PVMG from above also.
  925. # Typed arrays and hashes later.
  926. if (0 and $class =~ /^(I|P|S|N)V/
  927. and $opt_type_attr
  928. and UNIVERSAL::can($class,"CHECK_SCALAR_ATTRIBUTES")) # with 5.18
  929. {
  930. require attributes;
  931. #my $svtype = uc reftype ($namesv);
  932. # test 105
  933. my @attr = attributes::get(\$namesv); # how to get em from B? see optimize
  934. warn "\$$name attrs: ".@attr if $verbose or $debug{pad};
  935. #my $valid_types = ${"$class\::valid_attr"}; # They ARE valid, parser checked already.
  936. }
  937. # XXX We should try Devel::TypeCheck for type inference also
  938. # magic names: my $i_ir, my $d_d. without -fno-name-magic cmdline option only
  939. if ( $type == T_UNKNOWN and $opt_name_magic and $name =~ /^(.*)_([dis])(r?)$/ ) {
  940. $name = $1;
  941. if ( $2 eq "i" ) {
  942. $type = T_INT;
  943. $flags = VALID_SV | VALID_INT;
  944. }
  945. elsif ( $2 eq "d" ) {
  946. $type = T_NUM;
  947. $flags = VALID_SV | VALID_NUM;
  948. }
  949. elsif ( $2 eq "s" ) {
  950. $type = T_STR;
  951. $flags = VALID_SV | VALID_STR;
  952. }
  953. $flags |= REGISTER if $3;
  954. }
  955. }
  956. $name = "${ix}_$name";
  957. $pad[$ix] =
  958. B::Stackobj::Padsv->new( $type, $flags, $ix, "i$name", "d$name" );
  959. debug sprintf( "PL_curpad[$ix] = %s\n", $pad[$ix]->peek ) if $debug{pad};
  960. }
  961. }
  962. sub declare_pad {
  963. my $ix;
  964. for ( $ix = 1 ; $ix <= $#pad ; $ix++ ) {
  965. my $type = $pad[$ix]->{type};
  966. declare( "IV",
  967. $type == T_INT ? sprintf( "%s=0", $pad[$ix]->{iv} ) : $pad[$ix]->{iv} )
  968. if $pad[$ix]->save_int;
  969. declare( "NV",
  970. $type == T_NUM
  971. ? sprintf( "%s = 0", $pad[$ix]->{nv} )
  972. : $pad[$ix]->{nv} )
  973. if $pad[$ix]->save_double;
  974. declare( "PV",
  975. $type == T_STR
  976. ? sprintf( "%s = 0", $pad[$ix]->{sv} )
  977. : $pad[$ix]->{sv} )
  978. if $pad[$ix]->save_str;
  979. }
  980. }
  981. #
  982. # Debugging stuff
  983. #
  984. sub peek_stack {
  985. sprintf "stack = %s\n", join( " ", map( $_->minipeek, @stack ) );
  986. }
  987. #
  988. # OP stuff
  989. #
  990. =head2 label
  991. We not only mark named labels in C as such - with prefix "label_".
  992. We also have to mark each known (back jumps) and yet unknown branch targets
  993. (forward jumps) for compile-time generated branch points, with the "lab_"
  994. prefix.
  995. =cut
  996. sub label {
  997. my $op = shift;
  998. # Preserve original label name for "real" labels
  999. if ($op->can("label") and $op->label) {
  1000. # cc should error on duplicate named labels
  1001. return sprintf( "label_%s_%x", $op->label, $$op);
  1002. } else {
  1003. return sprintf( "lab_%x", $$op );
  1004. }
  1005. }
  1006. sub write_label {
  1007. my $op = shift;
  1008. $op->save if $$op;
  1009. # debug sprintf("lab_%x:?\n", $$op) if $debug{cxstack};
  1010. unless ($labels->{label}->{$$op}) {
  1011. my $l = label($op);
  1012. # named label but op not yet known?
  1013. if ( $op->can("label") and $op->label ) {
  1014. $l = "label_".$op->label;
  1015. # only print first such label. test 21
  1016. push_runtime(sprintf( " %s:", $l))
  1017. unless $labels->{label}->{$l};
  1018. $labels->{label}->{$l} = $$op;
  1019. }
  1020. if ($verbose) {
  1021. push_runtime(sprintf( " %s:\t/* %s */", label($op), $op->name ));
  1022. } else {
  1023. push_runtime(sprintf( " %s:", label($op) ));
  1024. }
  1025. # avoid printing duplicate jump labels
  1026. $labels->{label}->{$$op} = $l;
  1027. if ($op->can("label") and $op->label ) {
  1028. push(@cxstack, {
  1029. type => 0,
  1030. op => $op,
  1031. nextop => ((ref($op) eq 'B::LOOP') && $op->nextop) ? $op->nextop : $op,
  1032. redoop => ((ref($op) eq 'B::LOOP') && $op->redoop) ? $op->redoop : $op,
  1033. lastop => ((ref($op) eq 'B::LOOP') && $op->lastop) ? $op->lastop : $op,
  1034. 'label' => $op->can("label") && $op->label ? $op->label : $l
  1035. });
  1036. }
  1037. }
  1038. }
  1039. sub loadop {
  1040. my $op = shift;
  1041. my $opsym = $op->save;
  1042. $op_count++; # for statistics
  1043. runtime("PL_op = $opsym;") unless $know_op;
  1044. return $opsym;
  1045. }
  1046. sub doop {
  1047. my $op = shift;
  1048. my $ppaddr = $op->ppaddr;
  1049. my $sym = loadop($op);
  1050. my $ppname = "pp_" . $op->name;
  1051. if ($inline_ops) {
  1052. # inlining direct calls is safe, just CALLRUNOPS for macros not
  1053. $ppaddr = "Perl_".$ppname;
  1054. $no_stack{$ppname}
  1055. ? runtime("PL_op = $ppaddr(aTHX);")
  1056. : runtime("PUTBACK; PL_op = $ppaddr(aTHX); SPAGAIN;");
  1057. } else {
  1058. $no_stack{$ppname}
  1059. ? runtime("PL_op = $ppaddr(aTHX);")
  1060. : runtime("DOOP($ppaddr);");
  1061. }
  1062. $know_op = 1;
  1063. return $sym;
  1064. }
  1065. sub gimme {
  1066. my $op = shift;
  1067. my $want = $op->flags & OPf_WANT;
  1068. return ( $want == OPf_WANT_VOID ? G_VOID :
  1069. $want == OPf_WANT_SCALAR ? G_SCALAR :
  1070. $want == OPf_WANT_LIST ? G_ARRAY :
  1071. undef );
  1072. }
  1073. #
  1074. # Code generation for PP code
  1075. #
  1076. # coverage: 18,19,25,...
  1077. sub pp_null {
  1078. my $op = shift;
  1079. $B::C::nullop_count++;
  1080. return $op->next;
  1081. }
  1082. # coverage: 102
  1083. sub pp_stub {
  1084. my $op = shift;
  1085. my $gimme = gimme($op);
  1086. if ( not defined $gimme ) {
  1087. write_back_stack();
  1088. runtime("if (block_gimme() == G_SCALAR)",
  1089. "\tXPUSHs(&PL_sv_undef);");
  1090. } elsif ( $gimme == G_SCALAR ) {
  1091. my $obj = B::Stackobj::Const->new(sv_undef);
  1092. push( @stack, $obj );
  1093. }
  1094. return $op->next;
  1095. }
  1096. # coverage: 2,21,28,30
  1097. sub pp_unstack {
  1098. my $op = shift;
  1099. @stack = ();
  1100. runtime("PP_UNSTACK;");
  1101. return $op->next;
  1102. }
  1103. # coverage: 2,21,27,28,30
  1104. sub pp_and {
  1105. my $op = shift;
  1106. my $next = $op->next;
  1107. reload_lexicals();
  1108. unshift( @bblock_todo, $next );
  1109. if ( @stack >= 1 ) {
  1110. my $obj = pop @stack;
  1111. my $bool = $obj->as_bool;
  1112. write_back_stack();
  1113. save_or_restore_lexical_state($$next);
  1114. if ($bool =~ /POPs/) {
  1115. runtime("sv = $bool;",
  1116. sprintf("if (!sv) { PUSHs(sv); goto %s;}", label($next)));
  1117. } else {
  1118. runtime(sprintf(
  1119. "if (!$bool) { PUSHs((SV*)%s); goto %s;}", $obj->as_sv, label($next)
  1120. ));
  1121. }
  1122. }
  1123. else {
  1124. save_or_restore_lexical_state($$next);
  1125. runtime( sprintf( "if (!%s) goto %s;", top_bool(), label($next) ),
  1126. "*sp--;" );
  1127. }
  1128. return $op->other;
  1129. }
  1130. # Nearly identical to pp_and, but leaves stack unchanged.
  1131. sub pp_andassign {
  1132. my $op = shift;
  1133. my $next = $op->next;
  1134. reload_lexicals();
  1135. unshift( @bblock_todo, $next );
  1136. if ( @stack >= 1 ) {
  1137. my $obj = pop @stack;
  1138. my $bool = $obj->as_bool;
  1139. write_back_stack();
  1140. save_or_restore_lexical_state($$next);
  1141. if ($bool =~ /POPs/) {
  1142. runtime("sv = $bool;",
  1143. sprintf("PUSHs((SV*)%s); if (!$bool) { goto %s;}",
  1144. $obj->as_sv, label($next)));
  1145. } else {
  1146. runtime(
  1147. sprintf("PUSHs((SV*)%s); if (!$bool) { goto %s;}",
  1148. $obj->as_sv, label($next)));
  1149. }
  1150. }
  1151. else {
  1152. save_or_restore_lexical_state($$next);
  1153. runtime( sprintf( "if (!%s) goto %s;", top_bool(), label($next) ) );
  1154. }
  1155. return $op->other;
  1156. }
  1157. # coverage: 28
  1158. sub pp_or {
  1159. my $op = shift;
  1160. my $next = $op->next;
  1161. reload_lexicals();
  1162. unshift( @bblock_todo, $next );
  1163. if ( @stack >= 1 ) {
  1164. my $obj = pop @stack;
  1165. my $bool = $obj->as_bool;
  1166. write_back_stack();
  1167. save_or_restore_lexical_state($$next);
  1168. if ($bool =~ /POPs/) {
  1169. runtime("sv = $bool;",
  1170. sprintf("if (sv) { PUSHs(sv); goto %s;}", label($next)));
  1171. } else {
  1172. runtime(
  1173. sprintf("if ($bool) { PUSHs((SV*)%s); goto %s; }", $obj->as_sv, label($next)));
  1174. }
  1175. }
  1176. else {
  1177. save_or_restore_lexical_state($$next);
  1178. runtime( sprintf( "if (%s) goto %s;", top_bool(), label($next) ),
  1179. "*sp--;" );
  1180. }
  1181. return $op->other;
  1182. }
  1183. # Nearly identical to pp_or, but leaves stack unchanged.
  1184. sub pp_orassign {
  1185. my $op = shift;
  1186. my $next = $op->next;
  1187. reload_lexicals();
  1188. unshift( @bblock_todo, $next );
  1189. if ( @stack >= 1 ) {
  1190. my $obj = pop @stack;
  1191. my $bool = $obj->as_bool;
  1192. write_back_stack();
  1193. save_or_restore_lexical_state($$next);
  1194. runtime(
  1195. sprintf(
  1196. "PUSHs((SV*)%s); if ($bool) { goto %s; }", $obj->as_sv, label($next)
  1197. )
  1198. );
  1199. }
  1200. else {
  1201. save_or_restore_lexical_state($$next);
  1202. runtime( sprintf( "if (%s) goto %s;", top_bool(), label($next) ) );
  1203. }
  1204. return $op->other;
  1205. }
  1206. # coverage: issue 45 (1,2)
  1207. # in CORE aliased to pp_defined
  1208. # default dor is okay issue 45 (3,4)
  1209. sub pp_dorassign {
  1210. my $op = shift;
  1211. my $next = $op->next;
  1212. reload_lexicals();
  1213. unshift( @bblock_todo, $next );
  1214. my $sv = pop @stack;
  1215. write_back_stack();
  1216. save_or_restore_lexical_state($$next);
  1217. runtime( sprintf( "PUSHs(%s); if (%s && SvANY(%s)) goto %s;\t/* dorassign */",
  1218. $sv->as_sv, $sv->as_sv, $sv->as_sv, label($next)) ) if $sv;
  1219. return $op->other;
  1220. }
  1221. # coverage: 102
  1222. sub pp_cond_expr {
  1223. my $op = shift;
  1224. my $false = $op->next;
  1225. unshift( @bblock_todo, $false );
  1226. reload_lexicals();
  1227. my $bool = pop_bool();
  1228. write_back_stack();
  1229. save_or_restore_lexical_state($$false);
  1230. runtime( sprintf( "if (!$bool) goto %s;\t/* cond_expr */", label($false) ) );
  1231. return $op->other;
  1232. }
  1233. # coverage: 9,10,12,17,18,22,28,32
  1234. sub pp_padsv {
  1235. my $op = shift;
  1236. my $ix = $op->targ;
  1237. push( @stack, $pad[$ix] ) if $pad[$ix];
  1238. if ( $op->flags & OPf_MOD ) {
  1239. my $private = $op->private;
  1240. if ( $private & OPpLVAL_INTRO ) {
  1241. # coverage: 9,10,12,17,18,19,20,22,27,28,31,32
  1242. runtime("SAVECLEARSV(PL_curpad[$ix]);");
  1243. }
  1244. elsif ( $private & OPpDEREF ) {
  1245. # coverage: 18
  1246. if ($] >= 5.015002) {
  1247. runtime(sprintf( "PL_curpad[%d] = Perl_vivify_ref(aTHX_ PL_curpad[%d], %d);",
  1248. $ix, $ix, $private & OPpDEREF ));
  1249. } else {
  1250. runtime(sprintf( "Perl_vivify_ref(aTHX_ PL_curpad[%d], %d);",
  1251. $ix, $private & OPpDEREF ));
  1252. }
  1253. $vivify_ref_defined++;
  1254. $pad[$ix]->invalidate;
  1255. }
  1256. }
  1257. return $op->next;
  1258. }
  1259. # coverage: 1-5,7-14,18-23,25,27-32
  1260. sub pp_const {
  1261. my $op = shift;
  1262. my $sv = $op->sv;
  1263. my $obj;
  1264. # constant could be in the pad (under useithreads)
  1265. if ($$sv) {
  1266. $obj = $constobj{$$sv};
  1267. if ( !defined($obj) ) {
  1268. $obj = $constobj{$$sv} = B::Stackobj::Const->new($sv);
  1269. }
  1270. }
  1271. else {
  1272. $obj = $pad[ $op->targ ];
  1273. }
  1274. # XXX looks like method_named has only const as prev op
  1275. if ($op->next
  1276. and $op->next->can('name')
  1277. and $op->next->name eq 'method_named'
  1278. ) {
  1279. $package_pv = svop_or_padop_pv($op);
  1280. debug "save package_pv \"$package_pv\" for method_name\n" if $debug{op};
  1281. }
  1282. push( @stack, $obj );
  1283. return $op->next;
  1284. }
  1285. # coverage: 1-39, fails in 33
  1286. sub pp_nextstate {
  1287. my $op = shift;
  1288. if ($labels->{'nextstate'}->[-1] and $labels->{'nextstate'}->[-1] == $op) {
  1289. debug sprintf("pop_label nextstate: cxstack label %s\n", $curcop->[0]->label) if $debug{cxstack};
  1290. pop_label 'nextstate';
  1291. } else {
  1292. write_label($op);
  1293. }
  1294. $curcop->load($op);
  1295. loadop($op);
  1296. @stack = ();
  1297. debug( sprintf( "%s:%d\n", $op->file, $op->line ) ) if $debug{lineno};
  1298. debug( sprintf( "CopLABEL %s\n", $op->label ) ) if $op->label and $debug{cxstack};
  1299. runtime("TAINT_NOT;") if $opt_taint; # TODO Not always needed (resets PL_taint = 0)
  1300. runtime("sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;"); # TODO reset sp not needed always
  1301. if ( $freetmps_each_bblock || $freetmps_each_loop ) {
  1302. $need_freetmps = 1;
  1303. }
  1304. else {
  1305. runtime("FREETMPS;"); # TODO Not always needed
  1306. }
  1307. return $op->next;
  1308. }
  1309. # Like pp_nextstate, but used instead when the debugger is active.
  1310. sub pp_dbstate { pp_nextstate(@_) }
  1311. #default_pp will handle this:
  1312. #sub pp_bless { $curcop->write_back; default_pp(@_) }
  1313. #sub pp_repeat { $curcop->write_back; default_pp(@_) }
  1314. # The following subs need $curcop->write_back if we decide to support arybase:
  1315. # pp_pos, pp_substr, pp_index, pp_rindex, pp_aslice, pp_lslice, pp_splice
  1316. #sub pp_caller { $curcop->write_back; default_pp(@_) }
  1317. # coverage: ny
  1318. sub bad_pp_reset {
  1319. if ($inline_ops) {
  1320. my $op = shift;
  1321. warn "inlining reset\n" if $debug{op};
  1322. $curcop->write_back if $curcop;
  1323. runtime '{ /* pp_reset */';
  1324. runtime ' const char * const tmps = (MAXARG < 1) ? (const char *)"" : POPpconstx;';
  1325. runtime ' sv_reset(tmps, CopSTASH(PL_curcop));}';
  1326. runtime 'PUSHs(&PL_sv_yes);';
  1327. return $op->next;
  1328. } else {
  1329. default_pp(@_);
  1330. }
  1331. }
  1332. # coverage: 20
  1333. sub pp_regcreset {
  1334. if ($inline_ops) {
  1335. my $op = shift;
  1336. warn "inlining regcreset\n" if $debug{op};
  1337. $curcop->write_back if $curcop;
  1338. runtime 'PL_reginterp_cnt = 0; /* pp_regcreset */';
  1339. runtime 'TAINT_NOT;' if $opt_taint;
  1340. return $op->next;
  1341. } else {
  1342. default_pp(@_);
  1343. }
  1344. }
  1345. # coverage: 103
  1346. sub pp_stringify {
  1347. if ($inline_ops and $] >= 5.008) {
  1348. my $op = shift;
  1349. warn "inlining stringify\n" if $debug{op};
  1350. my $sv = top_sv();
  1351. my $ix = $op->targ;
  1352. my $targ = $pad[$ix];
  1353. runtime "sv_copypv(PL_curpad[$ix], $sv);\t/* pp_stringify */";
  1354. $stack[-1] = $targ if @stack;
  1355. return $op->next;
  1356. } else {
  1357. default_pp(@_);
  1358. }
  1359. }
  1360. # coverage: 9,10,27
  1361. sub bad_pp_anoncode {
  1362. if ($inline_ops) {
  1363. my $op = shift;
  1364. warn "inlining anoncode\n" if $debug{op};
  1365. my $ix = $op->targ;
  1366. my $ppname = "pp_" . $op->name;
  1367. write_back_lexicals() unless $skip_lexicals{$ppname};
  1368. write_back_stack() unless $skip_stack{$ppname};
  1369. # XXX finish me. this works only with >= 5.10
  1370. runtime '{ /* pp_anoncode */',
  1371. ' CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));',
  1372. ' if (CvCLONE(cv))',
  1373. ' cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(Perl_cv_clone(aTHX_ cv))));',
  1374. ' EXTEND(SP,1);',
  1375. ' PUSHs(MUTABLE_SV(cv));',
  1376. '}';
  1377. invalidate_lexicals() unless $skip_invalidate{$ppname};
  1378. return $op->next;
  1379. } else {
  1380. default_pp(@_);
  1381. }
  1382. }
  1383. # coverage: 35
  1384. # XXX TODO get prev op. For now saved in pp_const.
  1385. sub pp_method_named {
  1386. my ( $op ) = @_;
  1387. my $name = svop_or_padop_pv($op);
  1388. # The pkg PV is at [PL_stack_base+TOPMARK+1], the previous op->sv->PV.
  1389. my $stash = $package_pv ? $package_pv."::" : "main::";
  1390. $name = $stash . $name;
  1391. debug "save method_name \"$name\"\n" if $debug{op};
  1392. svref_2object( \&{$name} )->save;
  1393. default_pp(@_);
  1394. }
  1395. # inconsequence: gvs are not passed around on the stack
  1396. # coverage: 26,103
  1397. sub bad_pp_srefgen {
  1398. if ($inline_ops) {
  1399. my $op = shift;
  1400. warn "inlining srefgen\n" if $debug{op};
  1401. #my $ppname = "pp_" . $op->name;
  1402. #$curcop->write_back;
  1403. #write_back_lexicals() unless $skip_lexicals{$ppname};
  1404. #write_back_stack() unless $skip_stack{$ppname};
  1405. my $svobj = $stack[-1]->as_sv;
  1406. my $sv = pop_sv();
  1407. # XXX fix me
  1408. runtime "{ /* pp_srefgen */
  1409. SV* rv;
  1410. SV* sv = $sv;";
  1411. # sv = POPs
  1412. #B::svref_2object(\$sv);
  1413. if (($svobj->flags & 0xff) == $SVt_PVLV
  1414. and B::PVLV::LvTYPE($svobj) eq ord('y'))
  1415. {
  1416. runtime 'if (LvTARGLEN(sv))
  1417. vivify_defelem(sv);
  1418. if (!(sv = LvTARG(sv)))
  1419. sv = &PL_sv_undef;
  1420. else
  1421. SvREFCNT_inc_void_NN(sv);';
  1422. }
  1423. elsif (($svobj->flags & 0xff) == $SVt_PVAV) {
  1424. runtime 'if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
  1425. av_reify(MUTABLE_AV(sv));
  1426. SvTEMP_off(sv);
  1427. SvREFCNT_inc_void_NN(sv);';
  1428. }
  1429. #elsif ($sv->SvPADTMP && !IS_PADGV(sv)) {
  1430. # runtime 'sv = newSVsv(sv);';
  1431. #}
  1432. else {
  1433. runtime 'SvTEMP_off(sv);
  1434. SvREFCNT_inc_void_NN(sv);';
  1435. }
  1436. runtime 'rv = sv_newmortal();
  1437. sv_upgrade(rv, SVt_IV);
  1438. SvRV_set(rv, sv);
  1439. SvROK_on(rv);
  1440. PUSHBACK;
  1441. }';
  1442. return $op->next;
  1443. } else {
  1444. default_pp(@_);
  1445. }
  1446. }
  1447. # coverage: 9,10,27
  1448. #sub pp_refgen
  1449. # coverage: 28, 14
  1450. sub pp_rv2gv {
  1451. my $op = shift;
  1452. $curcop->write_back if $curcop;
  1453. my $ppname = "pp_" . $op->name;
  1454. write_back_lexicals() unless $skip_lexicals{$ppname};
  1455. write_back_stack() unless $skip_stack{$ppname};
  1456. my $sym = doop($op);
  1457. if ( $op->private & OPpDEREF ) {
  1458. $init->add( sprintf("((UNOP *)$sym)->op_first = $sym;") );
  1459. $init->add( sprintf( "((UNOP *)$sym)->op_type = %d;", $op->first->type ) );
  1460. }
  1461. return $op->next;
  1462. }
  1463. # coverage: 18,19,25
  1464. sub pp_sort {
  1465. my $op = shift;
  1466. #my $ppname = $op->ppaddr;
  1467. if ( $op->flags & OPf_SPECIAL && $op->flags & OPf_STACKED ) {
  1468. # blocksort is awful. E.g. we need to the leading NULL op, invalidates -fcop
  1469. # Ugly surgery required. sort expects as block: pushmark rv2gv leave => enter
  1470. # pp_sort() OP *kid = cLISTOP->op_first->op_sibling;/* skip over pushmark 4 to null */
  1471. # kid = cUNOPx(kid)->op_first; /* pass rv2gv (null'ed) */
  1472. # kid = cUNOPx(kid)->op_first; /* pass leave */
  1473. #
  1474. #3 <0> pushmark s ->4
  1475. #8 <@> sort lKS* ->9
  1476. #4 <0> pushmark s ->5
  1477. #- <1> null sK/1 ->5
  1478. #- <1> ex-leave sKP ->-
  1479. #- <0> enter s ->-
  1480. # some code doing cmp or ncmp
  1481. # Example with 3 const args: print sort { bla; $b <=> $a } 1,4,3
  1482. #5 <$> const[IV 1] s ->6
  1483. #6 <$> const[IV 4] s ->7
  1484. #7 <$> const[IV 3] s ->8 => sort
  1485. #
  1486. my $root = $op->first->sibling->first; #leave or null
  1487. my $start = $root->first; #enter
  1488. warn "blocksort: root=",$root->name,", start=",$start->name,"\n" if $debug{op};
  1489. my $pushmark = $op->first->save; #pushmark sibling to null
  1490. $op->first->sibling->save; #null->first to leave
  1491. $root->save; #ex-leave
  1492. my $sym = $start->save; #enter
  1493. my $fakeop = cc_queue( "pp_sort" . sprintf("%x",abs($$op)), $root, $start );
  1494. $init->add( sprintf( "(%s)->op_next = %s;", $sym, $fakeop ) );
  1495. }
  1496. $curcop->write_back;
  1497. write_back_lexicals();
  1498. write_back_stack();
  1499. doop($op);
  1500. return $op->next;
  1501. }
  1502. # coverage: 2-4,6,7,13,15,21,24,26,27,30,31
  1503. sub pp_gv {
  1504. my $op = shift;
  1505. my $gvsym;
  1506. if ($ITHREADS) {
  1507. $gvsym = $pad[ $op->padix ]->as_sv;
  1508. #push @stack, ($pad[$op->padix]);
  1509. }
  1510. else {
  1511. $gvsym = $op->gv->save;
  1512. # XXX
  1513. #my $obj = new B::Stackobj::Const($op->gv);
  1514. #push( @stack, $obj );
  1515. }
  1516. write_back_stack();
  1517. runtime("XPUSHs((SV*)$gvsym);");
  1518. return $op->next;
  1519. }
  1520. # coverage: 2,3,4,9,11,14,20,21,23,28
  1521. sub pp_gvsv {
  1522. my $op = shift;
  1523. my $gvsym;
  1524. if ($ITHREADS) {
  1525. #debug(sprintf("OP name=%s, class=%s\n",$op->name,class($op))) if $debug{pad};
  1526. debug( sprintf( "GVSV->padix = %d\n", $op->padix ) ) if $debug{pad};
  1527. $gvsym = $pad[ $op->padix ]->as_sv;
  1528. debug( sprintf( "GVSV->private = 0x%x\n", $op->private ) ) if $debug{pad};
  1529. }
  1530. else {
  1531. $gvsym = $op->gv->save;
  1532. }
  1533. # Expects GV*, not SV* PL_curpad
  1534. $gvsym = "(GV*)$gvsym" if $gvsym =~ /PL_curpad/;
  1535. write_back_stack();
  1536. if ( $op->private & OPpLVAL_INTRO ) {
  1537. runtime("XPUSHs(save_scalar($gvsym));");
  1538. #my $obj = new B::Stackobj::Const($op->gv);
  1539. #push( @stack, $obj );
  1540. }
  1541. else {
  1542. $PERL510
  1543. ? runtime("XPUSHs(GvSVn($gvsym));")
  1544. : runtime("XPUSHs(GvSV($gvsym));");
  1545. }
  1546. return $op->next;
  1547. }
  1548. # Check for faster fetch calls. Returns 0 if the fast 'no' is in effect.
  1549. sub autovivification {
  1550. if (!$opt_autovivify) {
  1551. return 0;
  1552. } elsif ($INC{'autovivification.pm'}) {
  1553. return _autovivification($curcop->[0]);
  1554. } else {
  1555. return 1;
  1556. }
  1557. }
  1558. # coverage: 16, issue44
  1559. sub pp_aelemfast {
  1560. my $op = shift;
  1561. my ($av, $rmg);
  1562. if ($op->flags & OPf_SPECIAL) {
  1563. my $sv = $pad[ $op->targ ]->as_sv;
  1564. my @c = comppadlist->ARRAY;
  1565. my @p = $c[1]->ARRAY;
  1566. my $lex = $p[ $op->targ ];
  1567. $rmg = ($lex and ref $lex eq 'B::AV' and ($lex->MAGICAL & SVs_RMG or !$lex->ARRAY)) ? 1 : 0;
  1568. # MUTABLE_AV is only needed to catch compiler const loss
  1569. # $av = $] > 5.01000 ? "MUTABLE_AV($sv)" : $sv;
  1570. $av = "(AV*)$sv";
  1571. } else {
  1572. my $gvsym;
  1573. if ($ITHREADS) { #padop XXX if it's only a OP, no PADOP? t/CORE/op/ref.t test 36
  1574. if ($op->can('padix')) {
  1575. #warn "padix\n";
  1576. $gvsym = $pad[ $op->padix ]->as_sv;
  1577. my @c = comppadlist->ARRAY; # XXX curpad, not comppad!!
  1578. my @p = $c[1]->ARRAY;
  1579. my $lex = $p[ $op->padix ];
  1580. $rmg = ($lex and ref $lex eq 'B::AV' and ($lex->MAGICAL & SVs_RMG or !$lex->ARRAY)) ? 1 : 0;
  1581. } else {
  1582. $gvsym = 'PL_incgv'; # XXX passes, but need to investigate why. cc test 43 5.10.1
  1583. #write_back_stack();
  1584. #runtime("PUSHs(&PL_sv_undef);");
  1585. #return $op->next;
  1586. }
  1587. }
  1588. else { #svop
  1589. my $gv = $op->gv;
  1590. $gvsym = $gv->save;
  1591. my $gvav = $gv->AV; # test 16, tied gvav
  1592. $rmg = $] < 5.007 ? 0 : ($gvav and ($gvav->MAGICAL & SVs_RMG or !$gvav->ARRAY)) ? 1 : 0;
  1593. }
  1594. $av = "GvAV($gvsym)";
  1595. }
  1596. my $ix = $op->private;
  1597. my $lval = $op->flags & OPf_MOD;
  1598. my $vivify = !$rmg ? autovivification() : 1; # no need to call if $rmg
  1599. debug "aelemfast: vivify=$vivify, rmg=$rmg, lval=$lval, -fautovivify=$opt_autovivify -faelem=$opt_aelem\n" if $debug{pad};
  1600. return _aelem($op, $av, $ix, $lval, $rmg, $vivify);
  1601. }
  1602. sub _aelem {
  1603. my ($op, $av, $ix, $lval, $rmg, $vivify) = @_;
  1604. if ($opt_aelem and !$rmg and !$vivify and $ix >= 0) {
  1605. push @stack, B::Stackobj::Aelem->new($av, $ix, $lval);
  1606. } else {
  1607. write_back_stack();
  1608. runtime(
  1609. "{ AV* av = (AV*)$av;",
  1610. " SV** const svp = av_fetch(av, $ix, $lval);",
  1611. " SV *sv = (svp ? *svp : &PL_sv_undef);",
  1612. (!$lval and $rmg) ? " if (SvRMAGICAL(av) && SvGMAGICAL(sv)) mg_get(sv);" : "",
  1613. " PUSHs(sv);",
  1614. "}"
  1615. );
  1616. }
  1617. return $op->next;
  1618. }
  1619. # coverage: ?
  1620. sub pp_aelem {
  1621. my $op = shift;
  1622. my ($ix, $av);
  1623. my $lval = ($op->flags & OPf_MOD or $op->private & (OPpLVAL_DEFER || OPpLVAL_INTRO)) ? 1 : 0;
  1624. my $vivify = autovivification();
  1625. my $rmg = $opt_magic; # use -fno-magic for the av (2nd stack arg)
  1626. if (@stack >= 1) { # at least ix
  1627. $ix = pop_int(); # TODO: substract CopARYBASE from ix
  1628. if (@stack >= 1) {
  1629. my $avobj = $stack[-1]->as_obj;
  1630. $rmg = ($avobj and $avobj->MAGICAL & SVs_RMG) ? 1 : 0;
  1631. }
  1632. $av = pop_sv();
  1633. debug "aelem: vivify = $vivify, rmg = $rmg, lval = $lval\n" if $debug{pad};
  1634. return _aelem($op, $av, $ix, $lval, $rmg, $vivify);
  1635. } else {
  1636. if ($lval or $rmg) { # always
  1637. return default_pp($op);
  1638. } else {
  1639. $ix = pop_int(); # TODO: substract CopARYBASE from ix
  1640. $av = pop_sv();
  1641. debug "aelem: vivify = $vivify, rmg = $rmg, lval = $lval\n" if $debug{pad};
  1642. return _aelem($op, $av, $ix, $lval, $rmg, $vivify);
  1643. }
  1644. }
  1645. }
  1646. # coverage: ?
  1647. sub int_binop {
  1648. my ( $op, $operator, $unsigned ) = @_;
  1649. if ( $op->flags & OPf_STACKED ) {
  1650. my $right = pop_int();
  1651. if ( @stack >= 1 ) {
  1652. my $left = top_int();
  1653. $stack[-1]->set_int( &$operator( $left, $right ), $unsigned );
  1654. }
  1655. else {
  1656. my $sv_setxv = $unsigned ? 'sv_setuv' : 'sv_setiv';
  1657. runtime( sprintf( "$sv_setxv(TOPs, %s);", &$operator( "TOPi", $right ) ) );
  1658. }
  1659. }
  1660. else {
  1661. my $targ = $pad[ $op->targ ];
  1662. my $right = B::Pseudoreg->new( "IV", "riv" );
  1663. my $left = B::Pseudoreg->new( "IV", "liv" );
  1664. runtime( sprintf( "$$right = %s; $$left = %s;", pop_int(), pop_int ) );
  1665. $targ->set_int( &$operator( $$left, $$right ), $unsigned );
  1666. push( @stack, $targ );
  1667. }
  1668. return $op->next;
  1669. }
  1670. sub INTS_CLOSED () { 0x1 }
  1671. sub INT_RESULT () { 0x2 }
  1672. sub NUMERIC_RESULT () { 0x4 }
  1673. # coverage: 101
  1674. sub numeric_binop {
  1675. my ( $op, $operator, $flags ) = @_;
  1676. my $force_int = 0;
  1677. $flags = 0 unless $flags;
  1678. $force_int ||= ( $flags & INT_RESULT );
  1679. $force_int ||=
  1680. ( $flags & INTS_CLOSED
  1681. && @stack >= 2
  1682. && valid_int( $stack[-2] )
  1683. && valid_int( $stack[-1] ) );
  1684. if ( $op->flags & OPf_STACKED ) {
  1685. runtime(sprintf("/* %s */", $op->name)) if $verbose;
  1686. my $right = pop_numeric();
  1687. if ( @stack >= 1 ) {
  1688. my $left = top_numeric();
  1689. if ($force_int) {
  1690. $stack[-1]->set_int( &$operator( $left, $right ) );
  1691. }
  1692. else {
  1693. $stack[-1]->set_numeric( &$operator( $left, $right ) );
  1694. }
  1695. }
  1696. else {
  1697. if ($force_int) {
  1698. my $rightruntime = B::Pseudoreg->new( "IV", "riv" );
  1699. runtime( sprintf( "$$rightruntime = %s;", $right ) );
  1700. runtime(
  1701. sprintf(
  1702. "sv_setiv(TOPs, %s);", &$operator( "TOPi", $$rightruntime )
  1703. )
  1704. );
  1705. }
  1706. else {
  1707. my $rightruntime = B::Pseudoreg->new( "NV", "rnv" );
  1708. runtime( sprintf( "$$rightruntime = %s;\t/* %s */", $right, $op->name ) );
  1709. runtime(
  1710. sprintf(
  1711. "sv_setnv(TOPs, %s);", &$operator( "TOPn", $$rightruntime )
  1712. )
  1713. );
  1714. }
  1715. }
  1716. }
  1717. else {
  1718. my $targ = $pad[ $op->targ ];
  1719. $force_int ||= ( $targ->{type} == T_INT );
  1720. if ($force_int) {
  1721. my $right = B::Pseudoreg->new( "IV", "riv" );
  1722. my $left = B::Pseudoreg->new( "IV", "liv" );
  1723. runtime(
  1724. sprintf( "$$right = %s;", pop_numeric()),
  1725. sprintf( "$$left = %s;\t/* %s */", pop_numeric(), pop_numeric(), $op->name ) );
  1726. $targ->set_int( &$operator( $$left, $$right ) );
  1727. }
  1728. else {
  1729. my $right = B::Pseudoreg->new( "NV", "rnv" );
  1730. my $left = B::Pseudoreg->new( "NV", "lnv" );
  1731. runtime(
  1732. sprintf( "$$right = %s;", pop_numeric()),
  1733. sprintf( "$$left = %s;\t/* %s */", pop_numeric(), $op->name ) );
  1734. $targ->set_numeric( &$operator( $$left, $$right ) );
  1735. }
  1736. push( @stack, $targ );
  1737. }
  1738. return $op->next;
  1739. }
  1740. sub numeric_unop {
  1741. my ( $op, $operator, $flags ) = @_;
  1742. my $force_int = 0;
  1743. $force_int ||= ( $flags & INT_RESULT );
  1744. $force_int ||=
  1745. ( $flags & INTS_CLOSED
  1746. && @stack >= 1
  1747. && valid_int( $stack[-1] ) );
  1748. my $targ = $pad[ $op->targ ];
  1749. $force_int ||= ( $targ->{type} == T_INT );
  1750. if ($force_int) {
  1751. my $arg = B::Pseudoreg->new( "IV", "liv" );
  1752. runtime(sprintf( "$$arg = %s;\t/* %s */",
  1753. pop_numeric, $op->name ) );
  1754. # XXX set targ?
  1755. $targ->set_int( &$operator( $$arg ) );
  1756. }
  1757. else {
  1758. my $arg = B::Pseudoreg->new( "NV", "lnv" );
  1759. runtime(sprintf( "$$arg = %s;\t/* %s */",
  1760. pop_numeric, $op->name ) );
  1761. # XXX set targ?
  1762. $targ->set_numeric( &$operator( $$arg ) );
  1763. }
  1764. push( @stack, $targ );
  1765. return $op->next;
  1766. }
  1767. # coverage: 18
  1768. sub pp_ncmp {
  1769. my ($op) = @_;
  1770. if ( $op->flags & OPf_STACKED ) {
  1771. my $right = pop_numeric();
  1772. if ( @stack >= 1 ) {
  1773. my $left = top_numeric();
  1774. runtime sprintf( "if (%s > %s){\t/* %s */", $left, $right, $op->name );
  1775. $stack[-1]->set_int(1);
  1776. $stack[-1]->write_back();
  1777. runtime sprintf( "}else if (%s < %s ) {", $left, $right );
  1778. $stack[-1]->set_int(-1);
  1779. $stack[-1]->write_back();
  1780. runtime sprintf( "}else if (%s == %s) {", $left, $right );
  1781. $stack[-1]->set_int(0);
  1782. $stack[-1]->write_back();
  1783. runtime sprintf("}else {");
  1784. $stack[-1]->set_sv("&PL_sv_undef");
  1785. runtime "}";
  1786. }
  1787. else {
  1788. my $rightruntime = B::Pseudoreg->new( "NV", "rnv" );
  1789. runtime( sprintf( "$$rightruntime = %s;\t/* %s */", $right, $op->name ) );
  1790. runtime sprintf( qq/if ("TOPn" > %s){/, $rightruntime );
  1791. runtime sprintf(" sv_setiv(TOPs,1);");
  1792. runtime sprintf( qq/}else if ( "TOPn" < %s ) {/, $$rightruntime );
  1793. runtime sprintf(" sv_setiv(TOPs,-1);");
  1794. runtime sprintf( qq/} else if ("TOPn" == %s) {/, $$rightruntime );
  1795. runtime sprintf(" sv_setiv(TOPs,0);");
  1796. runtime sprintf(qq/}else {/);
  1797. runtime sprintf(" sv_setiv(TOPs,&PL_sv_undef;");
  1798. runtime "}";
  1799. }
  1800. }
  1801. else {
  1802. my $targ = $pad[ $op->targ ];
  1803. my $right = B::Pseudoreg->new( "NV", "rnv" );
  1804. my $left = B::Pseudoreg->new( "NV", "lnv" );
  1805. runtime(
  1806. sprintf( "$$right = %s; $$left = %s;\t/* %s */",
  1807. pop_numeric(), pop_numeric, $op->name ) );
  1808. runtime sprintf( "if (%s > %s){ /*targ*/", $$left, $$right );
  1809. $targ->set_int(1);
  1810. $targ->write_back();
  1811. runtime sprintf( "}else if (%s < %s ) {", $$left, $$right );
  1812. $targ->set_int(-1);
  1813. $targ->write_back();
  1814. runtime sprintf( "}else if (%s == %s) {", $$left, $$right );
  1815. $targ->set_int(0);
  1816. $targ->write_back();
  1817. runtime sprintf("}else {");
  1818. $targ->set_sv("&PL_sv_undef");
  1819. runtime "}";
  1820. push( @stack, $targ );
  1821. }
  1822. #runtime "return NULL;";
  1823. return $op->next;
  1824. }
  1825. # coverage: ?
  1826. sub sv_binop {
  1827. my ( $op, $operator, $flags ) = @_;
  1828. if ( $op->flags & OPf_STACKED ) {
  1829. my $right = pop_sv();
  1830. if ( @stack >= 1 ) {
  1831. my $left = top_sv();
  1832. if ( $flags & INT_RESULT ) {
  1833. $stack[-1]->set_int( &$operator( $left, $right ) );
  1834. }
  1835. elsif ( $flags & NUMERIC_RESULT ) {
  1836. $stack[-1]->set_numeric( &$operator( $left, $right ) );
  1837. }
  1838. else {
  1839. # XXX Does this work?
  1840. runtime(
  1841. sprintf( "sv_setsv($left, %s);\t/* %s */",
  1842. &$operator( $left, $right ), $op->name ) );
  1843. $stack[-1]->invalidate;
  1844. }
  1845. }
  1846. else {
  1847. my $f;
  1848. if ( $flags & INT_RESULT ) {
  1849. $f = "sv_setiv";
  1850. }
  1851. elsif ( $flags & NUMERIC_RESULT ) {
  1852. $f = "sv_setnv";
  1853. }
  1854. else {
  1855. $f = "sv_setsv";
  1856. }
  1857. runtime( sprintf( "%s(TOPs, %s);\t/* %s */",
  1858. $f, &$operator( "TOPs", $right ), $op->name ) );
  1859. }
  1860. }
  1861. else {
  1862. my $targ = $pad[ $op->targ ];
  1863. runtime( sprintf( "right = %s; left = %s;\t/* %s */",
  1864. pop_sv(), pop_sv, $op->name ) );
  1865. if ( $flags & INT_RESULT ) {
  1866. $targ->set_int( &$operator( "left", "right" ) );
  1867. }
  1868. elsif ( $flags & NUMERIC_RESULT ) {
  1869. $targ->set_numeric( &$operator( "left", "right" ) );
  1870. }
  1871. else {
  1872. # XXX Does this work?
  1873. runtime(sprintf("sv_setsv(%s, %s);",
  1874. $targ->as_sv, &$operator( "left", "right" ) ));
  1875. $targ->invalidate;
  1876. }
  1877. push( @stack, $targ );
  1878. }
  1879. return $op->next;
  1880. }
  1881. # coverage: ?
  1882. sub bool_int_binop {
  1883. my ( $op, $operator ) = @_;
  1884. my $right = B::Pseudoreg->new( "IV", "riv" );
  1885. my $left = B::Pseudoreg->new( "IV", "liv" );
  1886. runtime( sprintf( "$$right = %s; $$left = %s;\t/* %s */",
  1887. pop_int(), pop_int(), $op->name ) );
  1888. my $bool = B::Stackobj::Bool->new( B::Pseudoreg->new( "int", "b" ) );
  1889. $bool->set_int( &$operator( $$left, $$right ) );
  1890. push( @stack, $bool );
  1891. return $op->next;
  1892. }
  1893. # coverage: ?
  1894. sub bool_numeric_binop {
  1895. my ( $op, $operator ) = @_;
  1896. my $right = B::Pseudoreg->new( "NV", "rnv" );
  1897. my $left = B::Pseudoreg->new( "NV", "lnv" );
  1898. runtime(
  1899. sprintf( "$$right = %s; $$left = %s;\t/* %s */",
  1900. pop_numeric(), pop_numeric(), $op->name ) );
  1901. my $bool = B::Stackobj::Bool->new( B::Pseudoreg->new( "int", "b" ) );
  1902. $bool->set_numeric( &$operator( $$left, $$right ) );
  1903. push( @stack, $bool );
  1904. return $op->next;
  1905. }
  1906. # coverage: ?
  1907. sub bool_sv_binop {
  1908. my ( $op, $operator ) = @_;
  1909. runtime( sprintf( "right = %s; left = %s;\t/* %s */",
  1910. pop_sv(), pop_sv(), $op->name ) );
  1911. my $bool = B::Stackobj::Bool->new( B::Pseudoreg->new( "int", "b" ) );
  1912. $bool->set_numeric( &$operator( "left", "right" ) );
  1913. push( @stack, $bool );
  1914. return $op->next;
  1915. }
  1916. # coverage: ?
  1917. sub infix_op {
  1918. my $opname = shift;
  1919. return sub { "$_[0] $opname $_[1]" }
  1920. }
  1921. # coverage: ?
  1922. sub prefix_op {
  1923. my $opname = shift;
  1924. return sub { sprintf( "%s(%s)", $opname, join( ", ", @_ ) ) }
  1925. }
  1926. BEGIN {
  1927. my $plus_op = infix_op("+");
  1928. my $minus_op = infix_op("-");
  1929. my $multiply_op = infix_op("*");
  1930. my $divide_op = infix_op("/");
  1931. my $modulo_op = infix_op("%");
  1932. my $lshift_op = infix_op("<<");
  1933. my $rshift_op = infix_op(">>");
  1934. my $scmp_op = prefix_op("sv_cmp");
  1935. my $seq_op = prefix_op("sv_eq");
  1936. my $sne_op = prefix_op("!sv_eq");
  1937. my $slt_op = sub { "sv_cmp($_[0], $_[1]) < 0" };
  1938. my $sgt_op = sub { "sv_cmp($_[0], $_[1]) > 0" };
  1939. my $sle_op = sub { "sv_cmp($_[0], $_[1]) <= 0" };
  1940. my $sge_op = sub { "sv_cmp($_[0], $_[1]) >= 0" };
  1941. my $eq_op = infix_op("==");
  1942. my $ne_op = infix_op("!=");
  1943. my $lt_op = infix_op("<");
  1944. my $gt_op = infix_op(">");
  1945. my $le_op = infix_op("<=");
  1946. my $ge_op = infix_op(">=");
  1947. #
  1948. # XXX The standard perl PP code has extra handling for
  1949. # some special case arguments of these operators.
  1950. #
  1951. sub pp_add { numeric_binop( $_[0], $plus_op ) }
  1952. sub pp_subtract { numeric_binop( $_[0], $minus_op ) }
  1953. sub pp_multiply { numeric_binop( $_[0], $multiply_op ) }
  1954. sub pp_divide { numeric_binop( $_[0], $divide_op ) }
  1955. sub pp_modulo { int_binop( $_[0], $modulo_op ) } # differs from perl's
  1956. # http://perldoc.perl.org/perlop.html#Shift-Operators:
  1957. # If use integer is in force then signed C integers are used,
  1958. # else unsigned C integers are used.
  1959. sub pp_left_shift { int_binop( $_[0], $lshift_op, VALID_UNSIGNED ) }
  1960. sub pp_right_shift { int_binop( $_[0], $rshift_op, VALID_UNSIGNED ) }
  1961. sub pp_i_add { int_binop( $_[0], $plus_op ) }
  1962. sub pp_i_subtract { int_binop( $_[0], $minus_op ) }
  1963. sub pp_i_multiply { int_binop( $_[0], $multiply_op ) }
  1964. sub pp_i_divide { int_binop( $_[0], $divide_op ) }
  1965. sub pp_i_modulo { int_binop( $_[0], $modulo_op ) }
  1966. sub pp_eq { bool_numeric_binop( $_[0], $eq_op ) }
  1967. sub pp_ne { bool_numeric_binop( $_[0], $ne_op ) }
  1968. # coverage: 21
  1969. sub pp_lt { bool_numeric_binop( $_[0], $lt_op ) }
  1970. # coverage: 28
  1971. sub pp_gt { bool_numeric_binop( $_[0], $gt_op ) }
  1972. sub pp_le { bool_numeric_binop( $_[0], $le_op ) }
  1973. sub pp_ge { bool_numeric_binop( $_[0], $ge_op ) }
  1974. sub pp_i_eq { bool_int_binop( $_[0], $eq_op ) }
  1975. sub pp_i_ne { bool_int_binop( $_[0], $ne_op ) }
  1976. sub pp_i_lt { bool_int_binop( $_[0], $lt_op ) }
  1977. sub pp_i_gt { bool_int_binop( $_[0], $gt_op ) }
  1978. sub pp_i_le { bool_int_binop( $_[0], $le_op ) }
  1979. sub pp_i_ge { bool_int_binop( $_[0], $ge_op ) }
  1980. sub pp_scmp { sv_binop( $_[0], $scmp_op, INT_RESULT ) }
  1981. sub pp_slt { bool_sv_binop( $_[0], $slt_op ) }
  1982. sub pp_sgt { bool_sv_binop( $_[0], $sgt_op ) }
  1983. sub pp_sle { bool_sv_binop( $_[0], $sle_op ) }
  1984. sub pp_sge { bool_sv_binop( $_[0], $sge_op ) }
  1985. sub pp_seq { bool_sv_binop( $_[0], $seq_op ) }
  1986. sub pp_sne { bool_sv_binop( $_[0], $sne_op ) }
  1987. # sub pp_sin { numeric_unop( $_[0], prefix_op("Perl_sin"), NUMERIC_RESULT ) }
  1988. # sub pp_cos { numeric_unop( $_[0], prefix_op("Perl_cos"), NUMERIC_RESULT ) }
  1989. # sub pp_exp { numeric_unop( $_[0], prefix_op("Perl_exp"), NUMERIC_RESULT ) }
  1990. # sub pp_abs { numeric_unop( $_[0], prefix_op("abs") ) }
  1991. # sub pp_negate { numeric_unop( $_[0], sub { "- $_[0]" }; ) }
  1992. # pow has special perl logic
  1993. ## sub pp_pow { numeric_binop( $_[0], prefix_op("Perl_pow"), NUMERIC_RESULT ) }
  1994. #XXX log and sqrt need to check negative args
  1995. # sub pp_sqrt { numeric_unop( $_[0], prefix_op("Perl_sqrt"), NUMERIC_RESULT ) }
  1996. # sub pp_log { numeric_unop( $_[0], prefix_op("Perl_log"), NUMERIC_RESULT ) }
  1997. # sub pp_atan2 { numeric_binop( $_[0], prefix_op("Perl_atan2"), NUMERIC_RESULT ) }
  1998. }
  1999. # coverage: 3,4,9,10,11,12,17,18,20,21,23
  2000. sub pp_sassign {
  2001. my $op = shift;
  2002. my $backwards = $op->private & OPpASSIGN_BACKWARDS;
  2003. debug( sprintf( "sassign->private=0x%x\n", $op->private ) ) if $debug{op};
  2004. my ( $dst, $src );
  2005. runtime("/* pp_sassign */") if $verbose;
  2006. if ( @stack >= 2 ) {
  2007. $dst = pop @stack;
  2008. $src = pop @stack;
  2009. ( $src, $dst ) = ( $dst, $src ) if $backwards;
  2010. my $type = $src->{type};
  2011. if ( $type == T_INT ) {
  2012. $dst->set_int( $src->as_int, $src->{flags} & VALID_UNSIGNED );
  2013. }
  2014. elsif ( $type == T_NUM ) {
  2015. $dst->set_numeric( $src->as_numeric );
  2016. }
  2017. else {
  2018. $dst->set_sv( $src->as_sv );
  2019. }
  2020. push( @stack, $dst );
  2021. }
  2022. elsif ( @stack == 1 ) {
  2023. if ($backwards) {
  2024. my $src = pop @stack;
  2025. my $type = $src->{type};
  2026. runtime("if (PL_tainting && PL_tainted) TAINT_NOT;") if $opt_taint;
  2027. if ( $type == T_INT ) {
  2028. if ( $src->{flags} & VALID_UNSIGNED ) {
  2029. runtime sprintf( "sv_setuv(TOPs, %s);", $src->as_int );
  2030. }
  2031. else {
  2032. runtime sprintf( "sv_setiv(TOPs, %s);", $src->as_int );
  2033. }
  2034. }
  2035. elsif ( $type == T_NUM ) {
  2036. runtime sprintf( "sv_setnv(TOPs, %s);", $src->as_double );
  2037. }
  2038. else {
  2039. runtime sprintf( "sv_setsv(TOPs, %s);", $src->as_sv );
  2040. }
  2041. runtime("SvSETMAGIC(TOPs);") if $opt_magic;
  2042. }
  2043. else {
  2044. my $dst = $stack[-1];
  2045. my $type = $dst->{type};
  2046. runtime("sv = POPs;");
  2047. runtime("MAYBE_TAINT_SASSIGN_SRC(sv);") if $opt_taint;
  2048. if ( $type == T_INT ) {
  2049. $dst->set_int("SvIV(sv)");
  2050. }
  2051. elsif ( $type == T_NUM ) {
  2052. $dst->set_double("SvNV(sv)");
  2053. }
  2054. else {
  2055. $opt_magic
  2056. ? runtime("SvSetMagicSV($dst->{sv}, sv);")
  2057. : runtime("SvSetSV($dst->{sv}, sv);");
  2058. $dst->invalidate;
  2059. }
  2060. }
  2061. }
  2062. else {
  2063. # empty perl stack, both at run-time
  2064. if ($backwards) {
  2065. runtime("src = POPs; dst = TOPs;");
  2066. }
  2067. else {
  2068. runtime("dst = POPs; src = TOPs;");
  2069. }
  2070. runtime(
  2071. $opt_taint ? "MAYBE_TAINT_SASSIGN_SRC(src);" : "",
  2072. "SvSetSV(dst, src);",
  2073. $opt_magic ? "SvSETMAGIC(dst);" : "",
  2074. "SETs(dst);"
  2075. );
  2076. }
  2077. return $op->next;
  2078. }
  2079. # coverage: ny
  2080. sub pp_preinc {
  2081. my $op = shift;
  2082. if ( @stack >= 1 ) {
  2083. my $obj = $stack[-1];
  2084. my $type = $obj->{type};
  2085. if ( $type == T_INT || $type == T_NUM ) {
  2086. $obj->set_int( $obj->as_int . " + 1" );
  2087. }
  2088. else {
  2089. runtime sprintf( "PP_PREINC(%s);", $obj->as_sv );
  2090. $obj->invalidate();
  2091. }
  2092. }
  2093. else {
  2094. runtime sprintf("PP_PREINC(TOPs);");
  2095. }
  2096. return $op->next;
  2097. }
  2098. # coverage: 1-32,35
  2099. sub pp_pushmark {
  2100. my $op = shift;
  2101. # runtime(sprintf("/* %s */", $op->name)) if $verbose;
  2102. write_back_stack();
  2103. runtime("PUSHMARK(sp);");
  2104. return $op->next;
  2105. }
  2106. # coverage: 28
  2107. sub pp_list {
  2108. my $op = shift;
  2109. runtime(sprintf("/* %s */", $op->name)) if $verbose;
  2110. write_back_stack();
  2111. my $gimme = gimme($op);
  2112. if ( not defined $gimme ) {
  2113. runtime("PP_LIST(block_gimme());");
  2114. } elsif ( $gimme == G_ARRAY ) { # sic
  2115. runtime("POPMARK;"); # need this even though not a "full" pp_list
  2116. }
  2117. else {
  2118. runtime("PP_LIST($gimme);");
  2119. }
  2120. return $op->next;
  2121. }
  2122. # coverage: 6,8,9,10,24,26,27,31,35
  2123. sub pp_entersub {
  2124. my $op = shift;
  2125. runtime(sprintf("/* %s */", $op->name)) if $verbose;
  2126. $curcop->write_back if $curcop;
  2127. write_back_lexicals( REGISTER | TEMPORARY );
  2128. write_back_stack();
  2129. my $sym = doop($op);
  2130. $op->next->save if ${$op->next};
  2131. $op->first->save if ${$op->first} and $op->first->type;
  2132. # sometimes needs an additional check
  2133. my $ck_next = ${$op->next} ? "PL_op != ($sym)->op_next && " : "";
  2134. runtime("while ($ck_next PL_op != (OP*)0 ){",
  2135. "\tPL_op = (*PL_op->op_ppaddr)(aTHX);",
  2136. "\tSPAGAIN;}");
  2137. $know_op = 0;
  2138. invalidate_lexicals( REGISTER | TEMPORARY );
  2139. # B::C::check_entersub($op);
  2140. return $op->next;
  2141. }
  2142. # coverage: 16,26,35,51,72,73
  2143. sub pp_bless {
  2144. my $op = shift;
  2145. $curcop->write_back if $curcop;
  2146. # B::C::check_bless($op);
  2147. default_pp($op);
  2148. }
  2149. # coverage: ny
  2150. sub pp_formline {
  2151. my $op = shift;
  2152. my $ppname = "pp_" . $op->name;
  2153. runtime(sprintf("/* %s */", $ppname)) if $verbose;
  2154. write_back_lexicals() unless $skip_lexicals{$ppname};
  2155. write_back_stack() unless $skip_stack{$ppname};
  2156. my $sym = doop($op);
  2157. # See comment in pp_grepwhile to see why!
  2158. $init->add("((LISTOP*)$sym)->op_first = $sym;");
  2159. runtime("if (PL_op == ((LISTOP*)($sym))->op_first) {");
  2160. save_or_restore_lexical_state( ${ $op->first } );
  2161. runtime( sprintf( "goto %s;", label( $op->first ) ),
  2162. "}");
  2163. return $op->next;
  2164. }
  2165. # coverage: 2,17,21,28,30
  2166. sub pp_goto {
  2167. my $op = shift;
  2168. my $ppname = "pp_" . $op->name;
  2169. runtime(sprintf("/* %s */", $ppname)) if $verbose;
  2170. write_back_lexicals() unless $skip_lexicals{$ppname};
  2171. write_back_stack() unless $skip_stack{$ppname};
  2172. my $sym = doop($op);
  2173. runtime("if (PL_op != ($sym)->op_next && PL_op != (OP*)0){return PL_op;}");
  2174. invalidate_lexicals() unless $skip_invalidate{$ppname};
  2175. return $op->next;
  2176. }
  2177. # coverage: 1-39, c_argv.t 2
  2178. sub pp_enter {
  2179. # XXX fails with simple c_argv.t 2. no cxix. Disabled for now
  2180. if (0 and $inline_ops) {
  2181. my $op = shift;
  2182. runtime(sprintf("/* %s */", $op->name)) if $verbose;
  2183. warn "inlining enter\n" if $debug{op};
  2184. $curcop->write_back if $curcop;
  2185. if (!($op->flags & OPf_WANT)) {
  2186. my $cxix = $#cxstack;
  2187. if ( $cxix >= 0 ) {
  2188. if ( $op->flags & OPf_SPECIAL ) {
  2189. runtime "gimme = block_gimme();";
  2190. } else {
  2191. runtime "gimme = cxstack[cxstack_ix].blk_gimme;";
  2192. }
  2193. } else {
  2194. runtime "gimme = G_SCALAR;";
  2195. }
  2196. } else {
  2197. runtime "gimme = OP_GIMME(PL_op, -1);";
  2198. }
  2199. runtime($] >= 5.011001 and $] < 5.011004
  2200. ? 'ENTER_with_name("block");' : 'ENTER;',
  2201. "SAVETMPS;",
  2202. "PUSHBLOCK(cx, CXt_BLOCK, SP);");
  2203. return $op->next;
  2204. } else {
  2205. return default_pp(@_);
  2206. }
  2207. }
  2208. # coverage: ny
  2209. sub pp_enterwrite { pp_entersub(@_) }
  2210. # coverage: 6,8,9,10,24,26,27,31
  2211. sub pp_leavesub {
  2212. my $op = shift;
  2213. my $ppname = "pp_" . $op->name;
  2214. write_back_lexicals() unless $skip_lexicals{$ppname};
  2215. write_back_stack() unless $skip_stack{$ppname};
  2216. runtime("if (PL_curstackinfo->si_type == PERLSI_SORT){",
  2217. "\tPUTBACK;return 0;",
  2218. "}");
  2219. doop($op);
  2220. return $op->next;
  2221. }
  2222. # coverage: ny
  2223. sub pp_leavewrite {
  2224. my $op = shift;
  2225. write_back_lexicals( REGISTER | TEMPORARY );
  2226. write_back_stack();
  2227. my $sym = doop($op);
  2228. # XXX Is this the right way to distinguish between it returning
  2229. # CvSTART(cv) (via doform) and pop_return()?
  2230. #runtime("if (PL_op) PL_op = (*PL_op->op_ppaddr)(aTHX);");
  2231. runtime("SPAGAIN;");
  2232. $know_op = 0;
  2233. invalidate_lexicals( REGISTER | TEMPORARY );
  2234. return $op->next;
  2235. }
  2236. # coverage: ny
  2237. sub pp_entergiven { pp_enterwrite(@_) }
  2238. # coverage: ny
  2239. sub pp_leavegiven { pp_leavewrite(@_) }
  2240. sub doeval {
  2241. my $op = shift;
  2242. $curcop->write_back;
  2243. write_back_lexicals( REGISTER | TEMPORARY );
  2244. write_back_stack();
  2245. my $sym = loadop($op);
  2246. my $ppaddr = $op->ppaddr;
  2247. runtime("PP_EVAL($ppaddr, ($sym)->op_next);");
  2248. $know_op = 1;
  2249. invalidate_lexicals( REGISTER | TEMPORARY );
  2250. return $op->next;
  2251. }
  2252. # coverage: 12
  2253. sub pp_entereval { doeval(@_) }
  2254. # coverage: ny
  2255. sub pp_dofile { doeval(@_) }
  2256. # coverage: 28
  2257. #pp_require is protected by pp_entertry, so no protection for it.
  2258. sub pp_require {
  2259. my $op = shift;
  2260. $curcop->write_back;
  2261. write_back_lexicals( REGISTER | TEMPORARY );
  2262. write_back_stack();
  2263. my $sym = doop($op);
  2264. # sometimes needs an additional check
  2265. my $ck_next = ${$op->next} ? "PL_op != ($sym)->op_next && " : "";
  2266. runtime("while ($ck_next PL_op != (OP*)0 ) {", #(test 28).
  2267. " PL_op = (*PL_op->op_ppaddr)(aTHX);",
  2268. " SPAGAIN;",
  2269. "}");
  2270. $know_op = 1;
  2271. invalidate_lexicals( REGISTER | TEMPORARY );
  2272. # B::C::check_require($op); # mark package
  2273. return $op->next;
  2274. }
  2275. # coverage: 32
  2276. sub pp_entertry {
  2277. my $op = shift;
  2278. $curcop->write_back;
  2279. write_back_lexicals( REGISTER | TEMPORARY );
  2280. write_back_stack();
  2281. my $sym = doop($op);
  2282. $entertry_defined = 1;
  2283. my $next = $op->next; # broken in 5.12, fixed in B::C by upgrading BASEOP
  2284. # jump past leavetry
  2285. $next = $op->other->next if $op->can("other"); # before 5.11.4 and after 5.13.8
  2286. my $l = label( $next );
  2287. debug "ENTERTRY label=$l (".ref($op).") ->".$next->name."(".ref($next).")\n";
  2288. runtime(sprintf( "PP_ENTERTRY(%s);", $l));
  2289. if ($next->isa('B::COP')) {
  2290. push_label($next, 'nextstate');
  2291. } else {
  2292. push_label($op->other, 'leavetry') if $op->can("other");
  2293. }
  2294. invalidate_lexicals( REGISTER | TEMPORARY );
  2295. return $op->next;
  2296. }
  2297. # coverage: 32
  2298. sub pp_leavetry {
  2299. my $op = shift;
  2300. pop_label 'leavetry' if $labels->{'leavetry'}->[-1] and $labels->{'leavetry'}->[-1] == $op;
  2301. default_pp($op);
  2302. runtime("PP_LEAVETRY;");
  2303. write_label($op->next);
  2304. return $op->next;
  2305. }
  2306. # coverage: ny
  2307. sub pp_grepstart {
  2308. my $op = shift;
  2309. if ( $need_freetmps && $freetmps_each_loop ) {
  2310. runtime("FREETMPS;"); # otherwise the grepwhile loop messes things up
  2311. $need_freetmps = 0;
  2312. }
  2313. write_back_stack();
  2314. my $sym = doop($op);
  2315. my $next = $op->next;
  2316. $next->save;
  2317. my $nexttonext = $next->next;
  2318. $nexttonext->save;
  2319. save_or_restore_lexical_state($$nexttonext);
  2320. runtime(
  2321. sprintf( "if (PL_op == (($sym)->op_next)->op_next) goto %s;",
  2322. label($nexttonext) )
  2323. );
  2324. return $op->next->other;
  2325. }
  2326. # coverage: ny
  2327. sub pp_mapstart {
  2328. my $op = shift;
  2329. if ( $need_freetmps && $freetmps_each_loop ) {
  2330. runtime("FREETMPS;"); # otherwise the mapwhile loop messes things up
  2331. $need_freetmps = 0;
  2332. }
  2333. write_back_stack();
  2334. # pp_mapstart can return either op_next->op_next or op_next->op_other and
  2335. # we need to be able to distinguish the two at runtime.
  2336. my $sym = doop($op);
  2337. my $next = $op->next;
  2338. $next->save;
  2339. my $nexttonext = $next->next;
  2340. $nexttonext->save;
  2341. save_or_restore_lexical_state($$nexttonext);
  2342. runtime(
  2343. sprintf( "if (PL_op == (($sym)->op_next)->op_next) goto %s;",
  2344. label($nexttonext) )
  2345. );
  2346. return $op->next->other;
  2347. }
  2348. # coverage: ny
  2349. sub pp_grepwhile {
  2350. my $op = shift;
  2351. my $next = $op->next;
  2352. unshift( @bblock_todo, $next );
  2353. write_back_lexicals();
  2354. write_back_stack();
  2355. my $sym = doop($op);
  2356. # pp_grepwhile can return either op_next or op_other and we need to
  2357. # be able to distinguish the two at runtime. Since it's possible for
  2358. # both ops to be "inlined", the fields could both be zero. To get
  2359. # around that, we hack op_next to be our own op (purely because we
  2360. # know it's a non-NULL pointer and can't be the same as op_other).
  2361. $init->add("((LOGOP*)$sym)->op_next = $sym;");
  2362. save_or_restore_lexical_state($$next);
  2363. runtime( sprintf( "if (PL_op == ($sym)->op_next) goto %s;", label($next) ) );
  2364. $know_op = 0;
  2365. return $op->other;
  2366. }
  2367. # coverage: ny
  2368. sub pp_mapwhile { pp_grepwhile(@_) }
  2369. # coverage: 24
  2370. sub pp_return {
  2371. my $op = shift;
  2372. write_back_lexicals( REGISTER | TEMPORARY );
  2373. write_back_stack();
  2374. doop($op);
  2375. runtime( "PUTBACK;", "return PL_op;" );
  2376. $know_op = 0;
  2377. return $op->next;
  2378. }
  2379. sub nyi {
  2380. my $op = shift;
  2381. warn sprintf( "Warning: %s not yet implemented properly\n", $op->ppaddr );
  2382. return default_pp($op);
  2383. }
  2384. # coverage: 17
  2385. sub pp_range {
  2386. my $op = shift;
  2387. my $flags = $op->flags;
  2388. if ( !( $flags & OPf_WANT ) ) {
  2389. if ($strict) {
  2390. error("context of range unknown at compile-time\n");
  2391. } else {
  2392. warn("Warning: context of range unknown at compile-time\n");
  2393. runtime('warn("context of range unknown at compile-time");');
  2394. }
  2395. return default_pp($op);
  2396. }
  2397. write_back_lexicals();
  2398. write_back_stack();
  2399. unless ( ( $flags & OPf_WANT ) == OPf_WANT_LIST ) {
  2400. # We need to save our UNOP structure since pp_flop uses
  2401. # it to find and adjust out targ. We don't need it ourselves.
  2402. $op->save;
  2403. save_or_restore_lexical_state( ${ $op->other } );
  2404. runtime sprintf( "if (SvTRUE(PL_curpad[%d])) goto %s;",
  2405. $op->targ, label( $op->other ) );
  2406. unshift( @bblock_todo, $op->other );
  2407. }
  2408. return $op->next;
  2409. }
  2410. # coverage: 17, 30
  2411. sub pp_flip {
  2412. my $op = shift;
  2413. my $flags = $op->flags;
  2414. if ( !( $flags & OPf_WANT ) ) {
  2415. if ($strict) {
  2416. error("context of flip unknown at compile-time\n");
  2417. } else {
  2418. warn("Warning: context of flip unknown at compile-time\n");
  2419. runtime('warn("context of flip unknown at compile-time");');
  2420. }
  2421. return default_pp($op);
  2422. }
  2423. if ( ( $flags & OPf_WANT ) == OPf_WANT_LIST ) {
  2424. return $op->first->other;
  2425. }
  2426. write_back_lexicals();
  2427. write_back_stack();
  2428. # We need to save our UNOP structure since pp_flop uses
  2429. # it to find and adjust out targ. We don't need it ourselves.
  2430. $op->save;
  2431. my $ix = $op->targ;
  2432. my $rangeix = $op->first->targ;
  2433. runtime(
  2434. ( $op->private & OPpFLIP_LINENUM )
  2435. ? "if (PL_last_in_gv && SvIV(TOPs) == IoLINES(GvIOp(PL_last_in_gv))) {"
  2436. : "if (SvTRUE(TOPs)) {"
  2437. );
  2438. runtime("\tsv_setiv(PL_curpad[$rangeix], 1);");
  2439. if ( $op->flags & OPf_SPECIAL ) {
  2440. runtime("sv_setiv(PL_curpad[$ix], 1);");
  2441. }
  2442. else {
  2443. save_or_restore_lexical_state( ${ $op->first->other } );
  2444. runtime( "\tsv_setiv(PL_curpad[$ix], 0);",
  2445. "\tsp--;", sprintf( "\tgoto %s;", label( $op->first->other ) ) );
  2446. }
  2447. runtime( "}", qq{sv_setpv(PL_curpad[$ix], "");}, "SETs(PL_curpad[$ix]);" );
  2448. $know_op = 0;
  2449. return $op->next;
  2450. }
  2451. # coverage: 17
  2452. sub pp_flop {
  2453. my $op = shift;
  2454. default_pp($op);
  2455. $know_op = 0;
  2456. return $op->next;
  2457. }
  2458. sub enterloop {
  2459. my $op = shift;
  2460. my $nextop = $op->nextop;
  2461. my $lastop = $op->lastop;
  2462. my $redoop = $op->redoop;
  2463. $curcop->write_back if $curcop;
  2464. debug "enterloop: pushing on cxstack\n" if $debug{cxstack};
  2465. push(
  2466. @cxstack,
  2467. {
  2468. type => $PERL512 ? CXt_LOOP_PLAIN : CXt_LOOP,
  2469. op => $op,
  2470. "label" => $curcop->[0]->label,
  2471. nextop => $nextop,
  2472. lastop => $lastop,
  2473. redoop => $redoop
  2474. }
  2475. );
  2476. debug sprintf("enterloop: cxstack label %s\n", $curcop->[0]->label) if $debug{cxstack};
  2477. $nextop->save;
  2478. $lastop->save;
  2479. $redoop->save;
  2480. # We need to compile the corresponding pp_leaveloop even if it's
  2481. # never executed. This is needed to get @cxstack right.
  2482. # Use case: while(1) { .. }
  2483. unshift @bblock_todo, ($lastop);
  2484. if (0 and $inline_ops and $op->name eq 'enterloop') {
  2485. warn "inlining enterloop\n" if $debug{op};
  2486. # XXX = GIMME_V fails on freebsd7 5.8.8 (28)
  2487. # = block_gimme() fails on the rest, but passes on freebsd7
  2488. runtime "gimme = GIMME_V;"; # XXX
  2489. if ($PERL512) {
  2490. runtime('ENTER_with_name("loop1");',
  2491. 'SAVETMPS;',
  2492. 'ENTER_with_name("loop2");',
  2493. 'PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);',
  2494. 'PUSHLOOP_PLAIN(cx, SP);');
  2495. } else {
  2496. runtime('ENTER;',
  2497. 'SAVETMPS;',
  2498. 'ENTER;',
  2499. 'PUSHBLOCK(cx, CXt_LOOP, SP);',
  2500. 'PUSHLOOP(cx, 0, SP);');
  2501. }
  2502. return $op->next;
  2503. } else {
  2504. return default_pp($op);
  2505. }
  2506. }
  2507. # coverage: 6,21,28,30
  2508. sub pp_enterloop { enterloop(@_) }
  2509. # coverage: 2
  2510. sub pp_enteriter { enterloop(@_) }
  2511. # coverage: 6,21,28,30
  2512. sub pp_leaveloop {
  2513. my $op = shift;
  2514. if ( !@cxstack ) {
  2515. die "panic: leaveloop, no cxstack";
  2516. }
  2517. debug "leaveloop: popping from cxstack\n" if $debug{cxstack};
  2518. pop(@cxstack);
  2519. return default_pp($op);
  2520. }
  2521. # coverage: ?
  2522. sub pp_next {
  2523. my $op = shift;
  2524. my $cxix;
  2525. if ( $op->flags & OPf_SPECIAL ) {
  2526. $cxix = dopoptoloop();
  2527. if ( $cxix < 0 ) {
  2528. warn "Warning: \"next\" used outside loop\n";
  2529. return default_pp($op); # no optimization
  2530. }
  2531. }
  2532. else {
  2533. my $label = $op->pv;
  2534. if ($label) {
  2535. $cxix = dopoptolabel( $label );
  2536. if ( $cxix < 0 ) {
  2537. # coverage: t/testcc 21
  2538. warn(sprintf("Warning: Label not found at compile time for \"next %s\"\n", $label ));
  2539. $labels->{nlabel}->{$label} = $$op;
  2540. return $op->next;
  2541. }
  2542. }
  2543. # Add support to leave non-loop blocks.
  2544. if ( CxTYPE_no_LOOP( $cxstack[$cxix] ) ) {
  2545. if (!$cxstack[$cxix]->{'nextop'} or !$cxstack[$cxix]->{'label'}) {
  2546. error("Use of \"next\" for non-loop and non-label blocks not yet implemented\n");
  2547. }
  2548. }
  2549. }
  2550. default_pp($op);
  2551. my $nextop = $cxstack[$cxix]->{nextop};
  2552. if ($nextop) {
  2553. push( @bblock_todo, $nextop );
  2554. save_or_restore_lexical_state($$nextop);
  2555. runtime( sprintf( "goto %s;", label($nextop) ) );
  2556. }
  2557. return $op->next;
  2558. }
  2559. # coverage: ?
  2560. sub pp_redo {
  2561. my $op = shift;
  2562. my $cxix;
  2563. if ( $op->flags & OPf_SPECIAL ) {
  2564. $cxix = dopoptoloop();
  2565. if ( $cxix < 0 ) {
  2566. #warn("Warning: \"redo\" used outside loop\n");
  2567. return default_pp($op); # no optimization
  2568. }
  2569. }
  2570. else {
  2571. my $label = $op->pv;
  2572. if ($label) {
  2573. $cxix = dopoptolabel( $label );
  2574. if ( $cxix < 0 ) {
  2575. warn(sprintf("Warning: Label not found at compile time for \"redo %s\"\n", $label ));
  2576. $labels->{nlabel}->{$label} = $$op;
  2577. return $op->next;
  2578. }
  2579. }
  2580. # Add support to leave non-loop blocks.
  2581. if ( CxTYPE_no_LOOP( $cxstack[$cxix] ) ) {
  2582. if (!$cxstack[$cxix]->{'redoop'} or !$cxstack[$cxix]->{'label'}) {
  2583. error("Use of \"redo\" for non-loop and non-label blocks not yet implemented\n");
  2584. }
  2585. }
  2586. }
  2587. default_pp($op);
  2588. my $redoop = $cxstack[$cxix]->{redoop};
  2589. if ($redoop) {
  2590. push( @bblock_todo, $redoop );
  2591. save_or_restore_lexical_state($$redoop);
  2592. runtime( sprintf( "goto %s;", label($redoop) ) );
  2593. }
  2594. return $op->next;
  2595. }
  2596. # coverage: issue36, cc_last.t
  2597. sub pp_last {
  2598. my $op = shift;
  2599. my $cxix;
  2600. if ( $op->flags & OPf_SPECIAL ) {
  2601. $cxix = dopoptoloop();
  2602. if ( $cxix < 0 ) {
  2603. #warn("Warning: \"last\" used outside loop\n");
  2604. return default_pp($op); # no optimization
  2605. }
  2606. }
  2607. elsif (ref($op) eq 'B::PVOP') { # !OPf_STACKED
  2608. my $label = $op->pv;
  2609. if ($label) {
  2610. $cxix = dopoptolabel( $label );
  2611. if ( $cxix < 0 ) {
  2612. # coverage: cc_last.t 2 (ok) 4 (ok)
  2613. warn( sprintf("Warning: Label not found at compile time for \"last %s\"\n", $label ));
  2614. # last does not jump into the future, by name without $$op
  2615. # instead it should jump to the block afterwards
  2616. $labels->{nlabel}->{$label} = $$op;
  2617. return $op->next;
  2618. }
  2619. }
  2620. # Add support to leave non-loop blocks. label fixed with 1.11
  2621. if ( CxTYPE_no_LOOP( $cxstack[$cxix] ) ) {
  2622. if (!$cxstack[$cxix]->{'lastop'} or !$cxstack[$cxix]->{'label'}) {
  2623. error("Use of \"last\" for non-loop and non-label blocks not yet implemented\n");
  2624. }
  2625. }
  2626. }
  2627. default_pp($op);
  2628. if ($cxstack[$cxix]->{lastop} and $cxstack[$cxix]->{lastop}->next) {
  2629. my $lastop = $cxstack[$cxix]->{lastop}->next;
  2630. push( @bblock_todo, $lastop );
  2631. save_or_restore_lexical_state($$lastop);
  2632. runtime( sprintf( "goto %s;", label($lastop) ) );
  2633. }
  2634. return $op->next;
  2635. }
  2636. # coverage: 3,4
  2637. sub pp_subst {
  2638. my $op = shift;
  2639. write_back_lexicals();
  2640. write_back_stack();
  2641. my $sym = doop($op);
  2642. my $replroot = $op->pmreplroot;
  2643. if ($$replroot) {
  2644. save_or_restore_lexical_state($$replroot);
  2645. runtime sprintf(
  2646. "if (PL_op == ((PMOP*)(%s))%s) goto %s;",
  2647. $sym, $PERL510 ? "->op_pmreplrootu.op_pmreplroot" : "->op_pmreplroot",
  2648. label($replroot)
  2649. );
  2650. $op->pmreplstart->save;
  2651. push( @bblock_todo, $replroot );
  2652. }
  2653. invalidate_lexicals();
  2654. return $op->next;
  2655. }
  2656. # coverage: 3
  2657. sub pp_substcont {
  2658. my $op = shift;
  2659. write_back_lexicals();
  2660. write_back_stack();
  2661. doop($op);
  2662. my $pmop = $op->other;
  2663. #warn sprintf( "substcont: op = %s, pmop = %s\n", peekop($op), peekop($pmop) ) if $verbose;
  2664. # my $pmopsym = objsym($pmop);
  2665. my $pmopsym = $pmop->save; # XXX can this recurse?
  2666. # warn "pmopsym = $pmopsym\n" if $verbose;
  2667. save_or_restore_lexical_state( ${ $pmop->pmreplstart } );
  2668. runtime sprintf(
  2669. "if (PL_op == ((PMOP*)(%s))%s) goto %s;",
  2670. $pmopsym,
  2671. $PERL510 ? "->op_pmstashstartu.op_pmreplstart" : "->op_pmreplstart",
  2672. label( $pmop->pmreplstart )
  2673. );
  2674. push( @bblock_todo, $pmop->pmreplstart );
  2675. invalidate_lexicals();
  2676. return $pmop->next;
  2677. }
  2678. # coverage: issue24
  2679. # resolve the DBM library at compile-time, not at run-time
  2680. sub pp_dbmopen {
  2681. my $op = shift;
  2682. require AnyDBM_File;
  2683. my $dbm = $AnyDBM_File::ISA[0];
  2684. svref_2object( \&{"$dbm\::bootstrap"} )->save;
  2685. return default_pp($op);
  2686. }
  2687. sub default_pp {
  2688. my $op = shift;
  2689. my $ppname = "pp_" . $op->name;
  2690. # runtime(sprintf("/* %s */", $ppname)) if $verbose;
  2691. if ( $curcop and $need_curcop{$ppname} ) {
  2692. $curcop->write_back;
  2693. }
  2694. write_back_lexicals() unless $skip_lexicals{$ppname};
  2695. write_back_stack() unless $skip_stack{$ppname};
  2696. doop($op);
  2697. # XXX If the only way that ops can write to a TEMPORARY lexical is
  2698. # when it's named in $op->targ then we could call
  2699. # invalidate_lexicals(TEMPORARY) and avoid having to write back all
  2700. # the temporaries. For now, we'll play it safe and write back the lot.
  2701. invalidate_lexicals() unless $skip_invalidate{$ppname};
  2702. return $op->next;
  2703. }
  2704. sub compile_op {
  2705. my $op = shift;
  2706. my $ppname = "pp_" . $op->name;
  2707. if ( exists $ignore_op{$ppname} ) {
  2708. return $op->next;
  2709. }
  2710. debug peek_stack() if $debug{stack};
  2711. if ( $debug{op} ) {
  2712. debug sprintf( "%s [%s]\n",
  2713. peekop($op), $op->flags & OPf_STACKED ? "OPf_STACKED" : $op->targ );
  2714. }
  2715. no strict 'refs';
  2716. if ( defined(&$ppname) ) {
  2717. $know_op = 0;
  2718. return &$ppname($op);
  2719. }
  2720. else {
  2721. return default_pp($op);
  2722. }
  2723. }
  2724. sub compile_bblock {
  2725. my $op = shift;
  2726. warn "compile_bblock: ", peekop($op), "\n" if $debug{bblock};
  2727. save_or_restore_lexical_state($$op);
  2728. write_label($op);
  2729. $know_op = 0;
  2730. do {
  2731. $op = compile_op($op);
  2732. if ($] < 5.013 and ($opt_slow_signals or ($$op and $async_signals{$op->name}))) {
  2733. runtime("PERL_ASYNC_CHECK();");
  2734. }
  2735. } while ( defined($op) && $$op && !exists( $leaders->{$$op} ) );
  2736. write_back_stack(); # boo hoo: big loss
  2737. reload_lexicals();
  2738. return $op;
  2739. }
  2740. sub cc {
  2741. my ( $name, $root, $start, @padlist ) = @_;
  2742. my $op;
  2743. if ( $done{$$start} ) {
  2744. warn "repeat=>" . ref($start) . " $name,\n" if $verbose;
  2745. $decl->add( sprintf( "#define $name %s", $done{$$start} ) );
  2746. return;
  2747. }
  2748. warn "cc $name\n" if $verbose;
  2749. init_pp($name);
  2750. load_pad(@padlist);
  2751. %lexstate = ();
  2752. B::Pseudoreg->new_scope;
  2753. @cxstack = ();
  2754. if ( $debug{timings} ) {
  2755. warn sprintf( "Basic block analysis at %s\n", timing_info );
  2756. }
  2757. $leaders = find_leaders( $root, $start );
  2758. my @leaders = keys %$leaders;
  2759. if ( $#leaders > -1 ) {
  2760. # Don't add basic blocks of dead code.
  2761. # It would produce errors when processing $cxstack.
  2762. # @bblock_todo = ( values %$leaders );
  2763. # Instead, save $root (pp_leavesub) separately,
  2764. # because it will not get compiled if located in dead code.
  2765. $root->save;
  2766. unshift @bblock_todo, ($start) if $$start;
  2767. }
  2768. else {
  2769. runtime("return PL_op?PL_op->op_next:0;");
  2770. }
  2771. if ( $debug{timings} ) {
  2772. warn sprintf( "Compilation at %s\n", timing_info );
  2773. }
  2774. while (@bblock_todo) {
  2775. $op = shift @bblock_todo;
  2776. warn sprintf( "Considering basic block %s\n", peekop($op) ) if $debug{bblock};
  2777. next if !defined($op) || !$$op || $done{$$op};
  2778. warn "...compiling it\n" if $debug{bblock};
  2779. do {
  2780. $done{$$op} = $name;
  2781. $op = compile_bblock($op);
  2782. if ( $need_freetmps && $freetmps_each_bblock ) {
  2783. runtime("FREETMPS;");
  2784. $need_freetmps = 0;
  2785. }
  2786. } while defined($op) && $$op && !$done{$$op};
  2787. if ( $need_freetmps && $freetmps_each_loop ) {
  2788. runtime("FREETMPS;");
  2789. $need_freetmps = 0;
  2790. }
  2791. if ( !$$op ) {
  2792. runtime( "PUTBACK;",
  2793. "return NULL;" );
  2794. }
  2795. elsif ( $done{$$op} ) {
  2796. save_or_restore_lexical_state($$op);
  2797. runtime( sprintf( "goto %s;", label($op) ) );
  2798. }
  2799. }
  2800. if ( $debug{timings} ) {
  2801. warn sprintf( "Saving runtime at %s\n", timing_info );
  2802. }
  2803. declare_pad(@padlist);
  2804. save_runtime();
  2805. }
  2806. sub cc_recurse {
  2807. my ($ccinfo);
  2808. my $start = cc_queue(@_) if @_;
  2809. while ( $ccinfo = shift @cc_todo ) {
  2810. if ($DB::deep and $ccinfo->[0] =~ /^pp_sub_(DB|Term__ReadLine)_/) {
  2811. warn "cc $ccinfo->[0] skipped (debugging)\n" if $verbose;
  2812. debug "cc(ccinfo): @$ccinfo skipped (debugging)\n" if $debug{queue};
  2813. }
  2814. elsif ($cc_pp_sub{$ccinfo->[0]}) { # skip duplicates
  2815. warn "cc $ccinfo->[0] already defined\n" if $verbose;
  2816. debug "cc(ccinfo): @$ccinfo already defined\n" if $debug{queue};
  2817. } else {
  2818. debug "cc(ccinfo): @$ccinfo\n" if $debug{queue};
  2819. cc(@$ccinfo);
  2820. $cc_pp_sub{$ccinfo->[0]}++;
  2821. }
  2822. }
  2823. return $start;
  2824. }
  2825. sub cc_obj {
  2826. my ( $name, $cvref ) = @_;
  2827. my $cv = svref_2object($cvref);
  2828. my @padlist = $cv->PADLIST->ARRAY;
  2829. my $curpad_sym = $padlist[1]->save;
  2830. cc_recurse( $name, $cv->ROOT, $cv->START, @padlist );
  2831. }
  2832. sub cc_main {
  2833. my @comppadlist = comppadlist->ARRAY;
  2834. my $curpad_nam = $comppadlist[0]->save;
  2835. my $curpad_sym = $comppadlist[1]->save;
  2836. my $init_av = init_av->save;
  2837. my $start = cc_recurse( "pp_main", main_root, main_start, @comppadlist );
  2838. # Do save_unused_subs before saving inc_hv
  2839. B::C::module($module) if $module;
  2840. save_unused_subs();
  2841. my $warner = $SIG{__WARN__};
  2842. save_sig($warner);
  2843. my($inc_hv, $inc_av, $end_av);
  2844. if ( !defined($module) ) {
  2845. # forbid run-time extends of curpad syms, names and INC
  2846. warn "save context:\n" if $verbose;
  2847. $init->add("/* save context */");
  2848. $init->add('/* %INC */');
  2849. inc_cleanup();
  2850. my $inc_gv = svref_2object( \*main::INC );
  2851. $inc_hv = $inc_gv->HV->save('main::INC');
  2852. $init->add( sprintf( "GvHV(%s) = s\\_%x;",
  2853. $inc_gv->save('main::INC'), $inc_gv->HV ) );
  2854. local ($B::C::const_strings);
  2855. $B::C::const_strings = 1 if $B::C::ro_inc;
  2856. $inc_hv = $inc_gv->HV->save('main::INC');
  2857. $inc_av = $inc_gv->AV->save('main::INC');
  2858. }
  2859. {
  2860. # >=5.10 needs to defer nullifying of all vars in END, not only new ones.
  2861. local ($B::C::const_strings);
  2862. $B::C::in_endav = 1;
  2863. $end_av = end_av->save;
  2864. }
  2865. cc_recurse();
  2866. return if $errors or $check;
  2867. if ( !defined($module) ) {
  2868. # XXX TODO push BEGIN/END blocks to modules code.
  2869. $init->add(
  2870. sprintf( "PL_main_root = s\\_%x;", ${ main_root() } ),
  2871. "PL_main_start = $start;",
  2872. "PL_curpad = AvARRAY($curpad_sym);",
  2873. "PL_comppad = $curpad_sym;");
  2874. if ($] < 5.017005) {
  2875. $init->add(
  2876. "av_store((AV*)CvPADLIST(PL_main_cv), 0, SvREFCNT_inc($curpad_nam)); /* namepad */",
  2877. "av_store((AV*)CvPADLIST(PL_main_cv), 1, SvREFCNT_inc($curpad_sym)); /* curpad */");
  2878. } else {
  2879. $init->add(
  2880. "PadlistARRAY(CvPADLIST(PL_main_cv))[0] = (PAD*)SvREFCNT_inc($curpad_nam); /* namepad */",
  2881. "PadlistARRAY(CvPADLIST(PL_main_cv))[1] = (PAD*)SvREFCNT_inc($curpad_sym); /* curpad */");
  2882. }
  2883. $init->add(
  2884. "GvHV(PL_incgv) = $inc_hv;",
  2885. "GvAV(PL_incgv) = $inc_av;",
  2886. "PL_initav = (AV*)$init_av;",
  2887. "PL_endav = (AV*)$end_av;");
  2888. if ($] < 5.017) {
  2889. my $amagic_generate = B::amagic_generation;
  2890. $init->add("PL_amagic_generation = $amagic_generate;");
  2891. };
  2892. }
  2893. seek( STDOUT, 0, 0 ); #prevent print statements from BEGIN{} into the output
  2894. fixup_ppaddr();
  2895. output_boilerplate();
  2896. print "\n";
  2897. output_all("perl_init");
  2898. output_runtime();
  2899. print "\n";
  2900. output_main_rest();
  2901. if ( defined($module) ) {
  2902. my $cmodule = $module ||= 'main';
  2903. $cmodule =~ s/::/__/g;
  2904. print <<"EOT";
  2905. #include "XSUB.h"
  2906. XS(boot_$cmodule)
  2907. {
  2908. dXSARGS;
  2909. perl_init();
  2910. ENTER;
  2911. SAVETMPS;
  2912. SAVEVPTR(PL_curpad);
  2913. SAVEVPTR(PL_op);
  2914. PL_curpad = AvARRAY($curpad_sym);
  2915. PL_op = $start;
  2916. pp_main(aTHX);
  2917. FREETMPS;
  2918. LEAVE;
  2919. ST(0) = &PL_sv_yes;
  2920. XSRETURN(1);
  2921. }
  2922. EOT
  2923. } else {
  2924. output_main();
  2925. }
  2926. if ( $debug{timings} ) {
  2927. warn sprintf( "Done at %s\n", timing_info );
  2928. }
  2929. }
  2930. sub compile_stats {
  2931. my $s = "Total number of OPs processed: $op_count\n";
  2932. $s .= "Total number of unresolved symbols: $B::C::unresolved_count\n"
  2933. if $B::C::unresolved_count;
  2934. return $s;
  2935. }
  2936. # Accessible via use B::CC '-ftype-attr'; in user code, or -MB::CC=-O2 on the cmdline
  2937. sub import {
  2938. my @options = @_;
  2939. # Allow debugging in CHECK blocks without Od
  2940. $DB::single = 1 if defined &DB::DB;
  2941. my ( $option, $opt, $arg );
  2942. # init with -O0
  2943. foreach my $ref ( values %optimise ) {
  2944. $$ref = 0;
  2945. }
  2946. $B::C::fold = 0 if $] >= 5.013009; # utf8::Cased tables
  2947. $B::C::warnings = 0 if $] >= 5.013005; # Carp warnings categories and B
  2948. $opt_taint = 1;
  2949. $opt_magic = 1; # only makes sense with -fno-magic
  2950. $opt_autovivify = 1; # only makes sense with -fno-autovivify
  2951. OPTION:
  2952. while ( $option = shift @options ) {
  2953. if ( $option =~ /^-(.)(.*)/ ) {
  2954. $opt = $1;
  2955. $arg = $2;
  2956. }
  2957. else {
  2958. unshift @options, $option;
  2959. last OPTION;
  2960. }
  2961. if ( $opt eq "-" && $arg eq "-" ) {
  2962. shift @options;
  2963. last OPTION;
  2964. }
  2965. elsif ( $opt eq "o" ) {
  2966. $arg ||= shift @options;
  2967. open( STDOUT, ">$arg" ) or return "open '>$arg': $!\n";
  2968. }
  2969. elsif ( $opt eq "c" ) {
  2970. $check = 1;
  2971. $B::C::check = 1;
  2972. }
  2973. elsif ( $opt eq "v" ) {
  2974. $verbose = 1;
  2975. B::C::verbose(1); # crashed in C _save_common_middle(B::FAKEOP)
  2976. }
  2977. elsif ( $opt eq "u" ) {
  2978. $arg ||= shift @options;
  2979. eval "require $arg;";
  2980. mark_unused( $arg, 1 );
  2981. }
  2982. elsif ( $opt eq "U" ) {
  2983. $arg ||= shift @options;
  2984. mark_skip( $arg );
  2985. }
  2986. elsif ( $opt eq "strict" ) {
  2987. $arg ||= shift @options;
  2988. $strict++;
  2989. }
  2990. elsif ( $opt eq "f" ) {
  2991. $arg ||= shift @options;
  2992. my $value = $arg !~ s/^no-//;
  2993. $arg =~ s/-/_/g;
  2994. my $ref = $optimise{$arg};
  2995. if ( defined($ref) ) {
  2996. $$ref = $value;
  2997. }
  2998. else {
  2999. # Pass down to B::C
  3000. my $ref = $B::C::option_map{$arg};
  3001. if ( defined($ref) ) {
  3002. $$ref = $value;
  3003. $c_optimise{$ref}++;
  3004. }
  3005. else {
  3006. warn qq(Warning: ignoring unknown optimisation "$arg"\n);
  3007. }
  3008. }
  3009. }
  3010. elsif ( $opt eq "O" ) {
  3011. $arg = 1 if $arg eq "";
  3012. foreach my $ref ( values %optimise ) {
  3013. $$ref = 0;
  3014. }
  3015. $B::C::destruct = 0 unless $] < 5.008; # fast_destruct
  3016. if ($arg >= 2) {
  3017. $freetmps_each_loop = 1;
  3018. if (!$ITHREADS) {
  3019. #warn qq(Warning: ignoring -faelem with threaded perl\n);
  3020. $opt_aelem = 1; # unstable, test: 68 pp_padhv targ assert
  3021. }
  3022. }
  3023. if ( $arg >= 1 ) {
  3024. $opt_type_attr = 1;
  3025. $freetmps_each_bblock = 1 unless $freetmps_each_loop;
  3026. }
  3027. }
  3028. elsif ( $opt eq "n" ) {
  3029. $arg ||= shift @options;
  3030. $init_name = $arg;
  3031. }
  3032. elsif ( $opt eq "m" ) {
  3033. $module = $arg;
  3034. mark_unused( $arg, undef );
  3035. }
  3036. elsif ( $opt eq "p" ) {
  3037. $arg ||= shift @options;
  3038. $patchlevel = $arg;
  3039. }
  3040. elsif ( $opt eq "D" ) {
  3041. $arg ||= shift @options;
  3042. $verbose++;
  3043. $arg = 'oOscprSqlt' if $arg eq 'full';
  3044. foreach $arg ( split( //, $arg ) ) {
  3045. if ( $arg eq "o" ) {
  3046. B->debug(1);
  3047. }
  3048. elsif ( $arg eq "O" ) {
  3049. $debug{op}++;
  3050. }
  3051. elsif ( $arg eq "s" ) {
  3052. $debug{stack}++;
  3053. }
  3054. elsif ( $arg eq "c" ) {
  3055. $debug{cxstack}++;
  3056. }
  3057. elsif ( $arg eq "p" ) {
  3058. $debug{pad}++;
  3059. }
  3060. elsif ( $arg eq "r" ) {
  3061. $debug{runtime}++;
  3062. }
  3063. elsif ( $arg eq "S" ) {
  3064. $debug{shadow}++;
  3065. }
  3066. elsif ( $arg eq "q" ) {
  3067. $debug{queue}++;
  3068. }
  3069. elsif ( $arg eq "l" ) {
  3070. $debug{lineno}++;
  3071. }
  3072. elsif ( $arg eq "t" ) {
  3073. $debug{timings}++;
  3074. }
  3075. elsif ( $arg eq "b" ) {
  3076. $debug{bblock}++;
  3077. }
  3078. elsif ( $arg eq "F" and eval "require B::Flags;" ) {
  3079. $debug{flags}++;
  3080. $B::C::debug{flags}++;
  3081. }
  3082. elsif ( exists $B::C::debug_map{$arg} ) {
  3083. $B::C::debug{ $B::C::debug_map{$arg} }++;
  3084. }
  3085. else {
  3086. warn qq(Warning: ignoring unknown -D option "$arg"\n);
  3087. }
  3088. }
  3089. }
  3090. }
  3091. $strict++ if !$strict and $Config{ccflags} !~ m/-DDEBUGGING/;
  3092. if ($opt_omit_taint) {
  3093. $opt_taint = 0;
  3094. warn "Warning: -fomit_taint is deprecated. Use -fno-taint instead.\n";
  3095. }
  3096. # rgs didn't want opcodes to be added to Opcode. So I had to add it to a
  3097. # seperate Opcodes package.
  3098. eval { require Opcodes; };
  3099. if (!$@ and $Opcodes::VERSION) {
  3100. my $MAXO = Opcodes::opcodes();
  3101. for (0..$MAXO-1) {
  3102. no strict 'refs';
  3103. my $ppname = "pp_".Opcodes::opname($_);
  3104. # opflags n: no args, no return values. don't need save/restore stack
  3105. # But pp_enter, pp_leave use/change global stack.
  3106. next if $ppname eq 'pp_enter' || $ppname eq 'pp_leave';
  3107. $no_stack{$ppname} = 1
  3108. if Opcodes::opflags($_) & 512;
  3109. # XXX More Opcodes options to be added later
  3110. }
  3111. }
  3112. #if ($debug{op}) {
  3113. # warn "no_stack: ",join(" ",sort keys %no_stack),"\n";
  3114. #}
  3115. mark_skip('B::C', 'B::C::Flags', 'B::CC', 'B::Asmdata', 'B::FAKEOP',
  3116. 'B::Section', 'B::Pseudoreg', 'B::Shadow', 'O', 'Opcodes',
  3117. 'B::Stackobj', 'B::Stackobj::Bool', 'B::Stackobj::Padsv',
  3118. 'B::Stackobj::Const', 'B::Stackobj::Aelem', 'B::Bblock');
  3119. mark_skip('DB', 'Term::ReadLine') if defined &DB::DB;
  3120. # Set some B::C optimizations.
  3121. # optimize_ppaddr is not needed with B::CC as CC does it even better.
  3122. for (qw(optimize_warn_sv save_data_fh av_init save_sig destruct const_strings)) {
  3123. no strict 'refs';
  3124. ${"B::C::$_"} = 1 unless $c_optimise{$_};
  3125. }
  3126. $B::C::stash = 0 unless $c_optimise{stash};
  3127. if (!$B::C::Flags::have_independent_comalloc) {
  3128. $B::C::av_init = 1 unless $c_optimise{av_init};
  3129. $B::C::av_init2 = 0 unless $c_optimise{av_init2};
  3130. } else {
  3131. $B::C::av_init = 0 unless $c_optimise{av_init};
  3132. $B::C::av_init2 = 1 unless $c_optimise{av_init2};
  3133. }
  3134. init_type_attrs() if $opt_type_attr; # but too late for -MB::CC=-O2 on import. attrs are checked before
  3135. @options;
  3136. }
  3137. # -MO=CC entry point
  3138. sub compile {
  3139. my @options = @_;
  3140. @options = import(@options);
  3141. init_sections();
  3142. $init = B::Section->get("init");
  3143. $decl = B::Section->get("decl");
  3144. # just some subs or main?
  3145. if (@options) {
  3146. return sub {
  3147. my ( $objname, $ppname );
  3148. foreach $objname (@options) {
  3149. $objname = "main::$objname" unless $objname =~ /::/;
  3150. ( $ppname = $objname ) =~ s/^.*?:://;
  3151. eval "cc_obj(qq(pp_sub_$ppname), \\&$objname)";
  3152. die "cc_obj(qq(pp_sub_$ppname, \\&$objname) failed: $@" if $@;
  3153. return if $errors;
  3154. }
  3155. my $warner = $SIG{__WARN__};
  3156. save_sig($warner);
  3157. fixup_ppaddr();
  3158. return if $check;
  3159. output_boilerplate();
  3160. print "\n";
  3161. output_all( $init_name || "init_module" );
  3162. output_runtime();
  3163. output_main_rest();
  3164. }
  3165. }
  3166. else {
  3167. return sub { cc_main() };
  3168. }
  3169. }
  3170. 1;
  3171. __END__
  3172. =head1 EXAMPLES
  3173. perl -MO=CC,-O2,-ofoo.c foo.pl
  3174. perl cc_harness -o foo foo.c
  3175. Note that C<cc_harness> lives in the C<B> subdirectory of your perl
  3176. library directory. The utility called C<perlcc> may also be used to
  3177. help make use of this compiler.
  3178. # create a shared XS module
  3179. perl -MO=CC,-mFoo,-oFoo.c Foo.pm
  3180. perl cc_harness -shared -c -o Foo.so Foo.c
  3181. # side-effects just for the types and attributes
  3182. perl -MB::CC -e'my int $i:unsigned; ...'
  3183. =head1 TYPES
  3184. Implemented type classes are B<int> and B<num>.
  3185. Planned is B<str> also.
  3186. Implemented are only SCALAR types yet.
  3187. Typed arrays and hashes and perfect hashes need L<coretypes>, L<types> and
  3188. proper C<const> support first.
  3189. Deprecated are inferred types via the names of locals, with '_i', '_d' suffix
  3190. and an optional 'r' suffix for register allocation.
  3191. C<my ($i_i, $j_ir, $num_d);>
  3192. Planned type attributes are B<int>, B<num>, B<str>, B<unsigned>, B<ro> / B<const>.
  3193. The attributes are perl attributes, and C<int|num|str> are either
  3194. compiler classes or hints for more allowed types.
  3195. C<my int $i :num;> declares a NV with SVf_IOK. Same as C<my $i:int:double;>
  3196. C<my int $i;> declares an IV. Same as C<my $i:int;>
  3197. C<my int $i :str;> declares a PVIV. Same as C<my $i:int:string;>
  3198. C<my int @array :unsigned = (0..4);> will be used as c var in faster arithmetic and cmp.
  3199. With :const or :ro even more.
  3200. C<my str %hash :const
  3201. = (foo => 'foo', bar => 'bar');> declare string values,
  3202. generate as read-only perfect hash.
  3203. B<:unsigned> is valid for int only and declares an UV.
  3204. B<:register> denotes optionally a short and hot life-time.
  3205. B<:temporary> are usually generated internally, nameless lexicals.
  3206. They are more aggressivly destroyed and ignored.
  3207. B<:ro> or B<:const> throw a compile-time error on write access and may optimize
  3208. the internal structure of the variable. We don't need to write back the variable
  3209. to perl (lexical write_back).
  3210. STATUS
  3211. OK (classes only):
  3212. my int $i;
  3213. my num $d;
  3214. NOT YET OK (attributes):
  3215. my int $i :register;
  3216. my $i :int;
  3217. my $const :int:const;
  3218. my $uv :int:unsigned;
  3219. ISSUES
  3220. This does not work with pure perl, unless you C<use B::CC> or C<use types> or
  3221. implement the classes and attribute type stubs in your code,
  3222. C<sub Mypkg::MODIFY_SCALAR_ATTRIBUTES {}> and C<sub Mypkg::FETCH_SCALAR_ATTRIBUTES {}>.
  3223. (TODO: empty should be enough to be detected by the compiler.)
  3224. Compiled code pulls in the magic MODIFY_SCALAR_ATTRIBUTES and FETCH_SCALAR_ATTRIBUTES
  3225. functions, even if they are used at compile time only.
  3226. Using attributes adds an import block to your code.
  3227. Up until 5.20 only B<our> variable attributes are checked at compile-time,
  3228. B<my> variables attributes at run-time only, which is too late for the compiler.
  3229. Perl attributes need to be fixed for types hints by adding C<CHECK_SCALAR_ATTRIBUTES>.
  3230. FUTURE
  3231. We should be able to support types on ARRAY and HASH.
  3232. For arrays also sizes to omit bounds-checking.
  3233. my int @array; # array of ints, faster magic-less access esp. in inlined arithmetic and cmp.
  3234. my str @array : const = qw(foo bar); # compile-time error on write. no lexical write_back
  3235. my int $hash = {"1" => 1, "2" => 2}; # int values, type-checked on write my
  3236. str %hash1 : const = (foo => 'bar'); # string keys only => maybe gperf
  3237. # compile-time error on write
  3238. Typed hash keys are always strings, as array keys are always int. Only the values are typed.
  3239. We should be also able to add type attributes for functions and methods,
  3240. i.e. for argument and return types. See L<types> and
  3241. L<http://blogs.perl.org/users/rurban/2011/02/use-types.html>
  3242. =head1 BUGS
  3243. Plenty. Current status: experimental.
  3244. =head1 DIFFERENCES
  3245. These aren't really bugs but they are constructs which are heavily
  3246. tied to perl's compile-and-go implementation and with which this
  3247. compiler backend cannot cope.
  3248. =head2 Loops
  3249. Standard perl calculates the target of "next", "last", and "redo"
  3250. at run-time. The compiler calculates the targets at compile-time.
  3251. For example, the program
  3252. sub skip_on_odd { next NUMBER if $_[0] % 2 }
  3253. NUMBER: for ($i = 0; $i < 5; $i++) {
  3254. skip_on_odd($i);
  3255. print $i;
  3256. }
  3257. produces the output
  3258. 024
  3259. with standard perl but calculates with the compiler the
  3260. goto label_NUMBER wrong, producing 01234.
  3261. =head2 Context of ".."
  3262. The context (scalar or array) of the ".." operator determines whether
  3263. it behaves as a range or a flip/flop. Standard perl delays until
  3264. runtime the decision of which context it is in but the compiler needs
  3265. to know the context at compile-time. For example,
  3266. @a = (4,6,1,0,0,1);
  3267. sub range { (shift @a)..(shift @a) }
  3268. print range();
  3269. while (@a) { print scalar(range()) }
  3270. generates the output
  3271. 456123E0
  3272. with standard Perl but gives a run-time warning with compiled Perl.
  3273. If the option B<-strict> is used it gives a compile-time error.
  3274. =head2 Arithmetic
  3275. Compiled Perl programs use native C arithmetic much more frequently
  3276. than standard perl. Operations on large numbers or on boundary
  3277. cases may produce different behaviour.
  3278. In doubt B::CC code behaves more like with C<use integer>.
  3279. =head2 Deprecated features
  3280. Features of standard perl such as C<$[> which have been deprecated
  3281. in standard perl since Perl5 was released have not been implemented
  3282. in the optimizing compiler.
  3283. =head1 AUTHORS
  3284. Malcolm Beattie C<MICB at cpan.org> I<(1996-1998, retired)>,
  3285. Vishal Bhatia <vishal at deja.com> I(1999),
  3286. Gurusamy Sarathy <gsar at cpan.org> I(1998-2001),
  3287. Reini Urban C<perl-compiler at googlegroups.com> I(2008-now),
  3288. Heinz Knutzen C<heinz.knutzen at gmx.de> I(2010)
  3289. Will Braswell C<wbraswell at hush.com> I(2012)
  3290. =cut
  3291. # Local Variables:
  3292. # mode: cperl
  3293. # cperl-indent-level: 2
  3294. # fill-column: 78
  3295. # End:
  3296. # vim: expandtab shiftwidth=2: