Utils.xs

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 97 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« OP.xs ↑ Summary blib/lib/B/Utils/OP.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "BUtils.h"
5 #include "ppport.h"
6  
7 /* After 5.10, the CxLVAL macro was added. */
8 #ifndef CxLVAL
9 # define CxLVAL(cx) cx->blk_sub.lval
10 #endif
11  
12 #define MY_CXT_KEY "B::Utils::_guts" XS_VERSION
13  
14 typedef struct {
15 int x_walkoptree_debug; /* Flag for walkoptree debug hook */
16 SV * x_specialsv_list[7];
17 } my_cxt_t;
18  
19 START_MY_CXT
20  
21 /* Stolen from B.xs */
22  
23 /* XXX: This really need to be properly exported by the B module,
24 possibly with custom .h retrieved by some extutil module for
25 building, and we shall be able to simply reuse the public symbol
26 from loaded module in run time.
27  
28 Beware of name clashes with CORE when loading the .so into the global namespace.
29 It fails with strict linkers, HP-UX, OpenBSD and probably on darwin without -flat-namespace.
30 "Can't make loaded symbols global on this platform while loading blib/arch/auto/B/Utils/Utils.sl
31 at /opt/perl_32/lib/5.8.8/PA-RISC1.1-thread-multi/DynaLoader.pm line 230."
32 */
33  
34 #ifdef PERL_OBJECT
35 #undef PL_op_name
36 #undef PL_opargs
37 #undef PL_op_desc
38 #define PL_op_name (get_op_names())
39 #define PL_opargs (get_opargs())
40 #define PL_op_desc (get_op_descs())
41 #endif
42  
43 /* duplicated from B.xs. */
44 static char *BUtils_svclassnames[] = {
45 "B::NULL",
46 "B::IV",
47 "B::NV",
48 "B::RV",
49 "B::PV",
50 "B::PVIV",
51 "B::PVNV",
52 "B::PVMG",
53 "B::BM",
54 #if PERL_VERSION >= 9
55 "B::GV",
56 #endif
57 "B::PVLV",
58 "B::AV",
59 "B::HV",
60 "B::CV",
61 #if PERL_VERSION <= 8
62 "B::GV",
63 #endif
64 "B::FM",
65 "B::IO",
66 };
67  
68 typedef enum {
69 OPc_NULL, /* 0 */
70 OPc_BASEOP, /* 1 */
71 OPc_UNOP, /* 2 */
72 OPc_BINOP, /* 3 */
73 OPc_LOGOP, /* 4 */
74 OPc_LISTOP, /* 5 */
75 OPc_PMOP, /* 6 */
76 OPc_SVOP, /* 7 */
77 OPc_PADOP, /* 8 */
78 OPc_PVOP, /* 9 */
79 OPc_LOOP, /* 10 */
80 OPc_COP /* 11 */
81 } BUtils_opclass;
82  
83 static char *BUtils_opclassnames[] = {
84 "B::NULL",
85 "B::OP",
86 "B::UNOP",
87 "B::BINOP",
88 "B::LOGOP",
89 "B::LISTOP",
90 "B::PMOP",
91 "B::SVOP",
92 "B::PADOP",
93 "B::PVOP",
94 "B::LOOP",
95 "B::COP"
96 };
97  
98 static BUtils_opclass
99 0 BUtils_cc_opclass(pTHX_ const OP *o)
100 {
101 0 ▶ if (!o)
Branch: 1 branch
BranchTrueFalseTotal
if (!o) 0 0 0
102 0 return OPc_NULL;
103  
104 0 ▶ if (o->op_type == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (o->op_type == 0) 0 0 0
105 0 ▶ return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
Branch: 1 branch
BranchTrueFalseTotal
return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP; 0 0 0
106  
107 0 ▶ if (o->op_type == OP_SASSIGN)
Branch: 1 branch
BranchTrueFalseTotal
if (o->op_type == OP_SASSIGN) 0 0 0
108 0 ▶ return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
Branch: 1 branch
BranchTrueFalseTotal
return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP); 0 0 0
109  
110 #ifdef USE_ITHREADS
111 if (o->op_type == OP_GV || o->op_type == OP_GVSV ||
112 o->op_type == OP_AELEMFAST || o->op_type == OP_RCATLINE)
113 return OPc_PADOP;
114 #endif
115  
116 0 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
117 0 case OA_BASEOP:
118 0 return OPc_BASEOP;
119  
120 0 case OA_UNOP:
121 0 return OPc_UNOP;
122  
123 0 case OA_BINOP:
124 0 return OPc_BINOP;
125  
126 0 case OA_LOGOP:
127 0 return OPc_LOGOP;
128  
129 0 case OA_LISTOP:
130 0 return OPc_LISTOP;
131  
132 0 case OA_PMOP:
133 0 return OPc_PMOP;
134  
135 0 case OA_SVOP:
136 0 return OPc_SVOP;
137  
138 0 case OA_PADOP:
139 0 return OPc_PADOP;
140  
141 0 case OA_PVOP_OR_SVOP:
142 /*
143 * Character translations (tr///) are usually a PVOP, keeping a
144 * pointer to a table of shorts used to look up translations.
145 * Under utf8, however, a simple table isn't practical; instead,
146 * the OP is an SVOP, and the SV is a reference to a swash
147 * (i.e., an RV pointing to an HV).
148 */
149 0 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF))
150 0 ▶ ? OPc_SVOP : OPc_PVOP;
Branch: 1 branch
BranchTrueFalseTotal
? OPc_SVOP : OPc_PVOP; 0 0 0
151  
152 0 case OA_LOOP:
153 0 return OPc_LOOP;
154  
155 0 case OA_COP:
156 0 return OPc_COP;
157  
158 0 case OA_BASEOP_OR_UNOP:
159 /*
160 * UNI(OP_foo) in toke.c returns token UNI or FUNC1 depending on
161 * whether parens were seen. perly.y uses OPf_SPECIAL to
162 * signal whether a BASEOP had empty parens or none.
163 * Some other UNOPs are created later, though, so the best
164 * test is OPf_KIDS, which is set in newUNOP.
165 */
166 0 ▶ return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
Branch: 1 branch
BranchTrueFalseTotal
return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP; 0 0 0
167  
168 0 case OA_FILESTATOP:
169 /*
170 * The file stat OPs are created via UNI(OP_foo) in toke.c but use
171 * the OPf_REF flag to distinguish between OP types instead of the
172 * usual OPf_SPECIAL flag. As usual, if OPf_KIDS is set, then we
173 * return OPc_UNOP so that walkoptree can find our children. If
174 * OPf_KIDS is not set then we check OPf_REF. Without OPf_REF set
175 * (no argument to the operator) it's an OP; with OPf_REF set it's
176 * an SVOP (and op_sv is the GV for the filehandle argument).
177 */
178 0 ▶ return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
Branch: 1 branch
BranchTrueFalseTotal
return ((o->op_flags & OPf_KIDS) ? OPc_UNOP : 0 0 0
179 #ifdef USE_ITHREADS
180 (o->op_flags & OPf_REF) ? OPc_PADOP : OPc_BASEOP);
181 #else
182 0 ▶ (o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP);
Branch: 1 branch
BranchTrueFalseTotal
(o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP); 0 0 0
183 #endif
184 0 case OA_LOOPEXOP:
185 /*
186 * next, last, redo, dump and goto use OPf_SPECIAL to indicate that a
187 * label was omitted (in which case it's a BASEOP) or else a term was
188 * seen. In this last case, all except goto are definitely PVOP but
189 * goto is either a PVOP (with an ordinary constant label), an UNOP
190 * with OPf_STACKED (with a non-constant non-sub) or an UNOP for
191 * OP_REFGEN (with goto &sub) in which case OPf_STACKED also seems to
192 * get set.
193 */
194 0 ▶ if (o->op_flags & OPf_STACKED)
Branch: 1 branch
BranchTrueFalseTotal
if (o->op_flags & OPf_STACKED) 0 0 0
195 0 return OPc_UNOP;
196 0 ▶ else if (o->op_flags & OPf_SPECIAL)
Branch: 1 branch
BranchTrueFalseTotal
else if (o->op_flags & OPf_SPECIAL) 0 0 0
197 0 return OPc_BASEOP;
198 else
199 0 return OPc_PVOP;
200 }
201 0 warn("can't determine class of operator %s, assuming BASEOP\n",
202 PL_op_name[o->op_type]);
203 0 return OPc_BASEOP;
204 }
205  
206 char *
207 0 BUtils_cc_opclassname(pTHX_ const OP *o)
208 {
209 0 return BUtils_opclassnames[BUtils_cc_opclass(aTHX_ o)];
210 }
211  
212 I32
213 0 BUtils_op_name_to_num(SV *name)
214 {
215 dTHX;
216 char const *s;
217 0 char *wanted = SvPV_nolen(name);
218 0 int i =0;
219 0 int topop = OP_max;
220  
221 #ifdef PERL_CUSTOM_OPS
222 topop--;
223 #endif
224  
225 0 ▶ if (SvIOK(name) && SvIV(name) >= 0 && SvIV(name) < topop)
Branch: 3 branches
BranchTrueFalseTotal
if (SvIOK(name) && SvIV(name) >= 0 && SvIV(name) < topop) 0 0 0
if (SvIOK(name) && SvIV(name) >= 0 && SvIV(name) < topop) 0 0 0
if (SvIOK(name) && SvIV(name) >= 0 && SvIV(name) < topop) 0 0 0
226 0 return SvIV(name);
227  
228 0 ▶ for (s = PL_op_name[i]; s; s = PL_op_name[++i]) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = PL_op_name[i]; s; s = PL_op_name[++i]) { 0 0 0
229 0 ▶ if (strEQ(s, wanted))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(s, wanted)) 0 0 0
230 0 return i;
231 }
232 #ifdef PERL_CUSTOM_OPS
233 if (PL_custom_op_names) {
234 HE* ent;
235 SV* value;
236 /* This is sort of a hv_exists, backwards */
237 (void)hv_iterinit(PL_custom_op_names);
238 while ((ent = hv_iternext(PL_custom_op_names))) {
239 if (strEQ(SvPV_nolen(hv_iterval(PL_custom_op_names,ent)),wanted))
240 return OP_CUSTOM;
241 }
242 }
243 #endif
244  
245 0 croak("No such op \"%s\"", SvPV_nolen(name));
246  
247 return -1;
248 }
249  
250 SV *
251 0 BUtils_make_sv_object(pTHX_ SV *arg, SV *sv)
252 {
253 0 char *type = 0;
254 IV iv;
255 dMY_CXT;
256  
257 0 ▶ if (!type) {
Branch: 1 branch
BranchTrueFalseTotal
if (!type) { 0 0 0
258 0 type = BUtils_svclassnames[SvTYPE(sv)];
259 0 iv = PTR2IV(sv);
260 }
261 0 sv_setiv(newSVrv(arg, type), iv);
262 0 return arg;
263 }
264  
265 MODULE = B::Utils PACKAGE = B::Utils
266 PROTOTYPES: DISABLE
« OP.xs ↑ Summary blib/lib/B/Utils/OP.pm »