| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| |
| 3 |
|
| /* For versions of ExtUtils::ParseXS > 3.04_02, we need to |
| 4 |
|
| * explicitly enforce exporting of XSUBs since we want to |
| 5 |
|
| * refer to them using XS(). This isn't strictly necessary, |
| 6 |
|
| * but it's by far the simplest way to be backwards-compatible. |
| 7 |
|
| */ |
| 8 |
|
| /* #define PERL_EUPXS_ALWAYS_EXPORT */ |
| 9 |
|
| |
| 10 |
|
| #include "EXTERN.h" |
| 11 |
|
| #include "perl.h" |
| 12 |
|
| |
| 13 |
|
| #include "XSUB.h" |
| 14 |
|
| #include "ppport.h" |
| 15 |
|
| |
| 16 |
|
| #include <libtcc.h> |
| 17 |
|
| |
| 18 |
|
| /* functions to be called by core typemap reimplementations */ |
| 19 |
|
| #include "typemap_func.h" |
| 20 |
|
| |
| 21 |
|
| typedef void xstcc_symbol; |
| 22 |
|
| |
| 23 |
|
| typedef struct { |
| 24 |
|
| TCCState *tccstate; |
| 25 |
|
| CV *error_callback; |
| 26 |
|
| #ifdef PERL_IMPLICIT_CONTEXT |
| 27 |
|
| tTHX perl_thread_context; |
| 28 |
|
| #endif |
| 29 |
|
| } xstcc_state; |
| 30 |
|
| |
| 31 |
|
| void |
| 32 |
1 |
| xstcc_error_func(void *opaque, const char *msg) |
| 33 |
|
| { |
| 34 |
1 |
| xstcc_state *state = (xstcc_state *)opaque; |
| 35 |
|
| #ifdef PERL_IMPLICIT_CONTEXT |
| 36 |
|
| tTHX my_perl = state->perl_thread_context; |
| 37 |
|
| #endif |
| 38 |
|
| |
| 39 |
1 |
▶ | if (state->error_callback != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (state->error_callback != NULL) { |
1 |
0 |
1 |
|
| 40 |
1 |
| dSP; |
| 41 |
1 |
▶ | dXSTARG; |
Branch: 1 branch
| Branch | True | False | Total |
| dXSTARG; |
1 |
0 |
1 |
|
| 42 |
|
| |
| 43 |
1 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
1 |
1 |
|
| 44 |
1 |
▶ | XPUSHp(msg, strlen(msg)); |
Branch: 2 branches
| Branch | True | False | Total |
| XPUSHp(msg, strlen(msg)); |
0 |
1 |
1 |
| XPUSHp(msg, strlen(msg)); |
0 |
1 |
1 |
|
| 45 |
1 |
| PUTBACK; |
| 46 |
|
| |
| 47 |
1 |
| call_sv((SV *)state->error_callback, G_DISCARD); |
| 48 |
|
| } |
| 49 |
|
| else { |
| 50 |
|
| /* |
| 51 |
|
| * libtcc does not provide a way to determine errors and warnings. |
| 52 |
|
| */ |
| 53 |
0 |
▶ | if (strstr(msg, "error: ")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strstr(msg, "error: ")) { |
0 |
0 |
0 |
|
| 54 |
0 |
| croak("%s", msg); |
| 55 |
|
| } |
| 56 |
|
| else { |
| 57 |
0 |
| warn("%s", msg); |
| 58 |
|
| } |
| 59 |
|
| } |
| 60 |
1 |
| } |
| 61 |
|
| |
| 62 |
|
| MODULE = XS::TCC PACKAGE = XS::TCC |
| 63 |
|
| PROTOTYPES: DISABLE |
| 64 |
|
| |
| 65 |
|
| REQUIRE: 3.18 |
| 66 |
|
| |
| 67 |
|
| TYPEMAP: <<HERE |
| 68 |
|
| TCCState * O_OBJECT |
| 69 |
|
| xstcc_state * O_OBJECT |
| 70 |
|
| xstcc_symbol * O_OBJECT |
| 71 |
|
| |
| 72 |
|
| OUTPUT |
| 73 |
|
| O_OBJECT |
| 74 |
|
| sv_setref_pv( $arg, CLASS, (void*)$var ); |
| 75 |
|
| |
| 76 |
|
| INPUT |
| 77 |
|
| |
| 78 |
|
| O_OBJECT |
| 79 |
|
| if( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) ) |
| 80 |
|
| $var = ($type)SvIV((SV*)SvRV( $arg )); |
| 81 |
|
| else{ |
| 82 |
|
| warn( \"${Package}::$func_name() -- $var is not a blessed SV reference\" ); |
| 83 |
|
| XSRETURN_UNDEF; |
| 84 |
|
| } |
| 85 |
|
| HERE |
| 86 |
|
| |
| 87 |
|
| MODULE = XS::TCC PACKAGE = XS::TCC::TCCState |
| 88 |
|
| |
| 89 |
|
| xstcc_state * |
| 90 |
|
| new(const char *CLASS) |
| 91 |
|
| CODE: |
| 92 |
4 |
| Newx(RETVAL, 1, xstcc_state); |
| 93 |
4 |
| RETVAL->tccstate = tcc_new(); |
| 94 |
4 |
| RETVAL->error_callback = NULL; |
| 95 |
|
| #ifdef PERL_IMPLICIT_CONTEXT |
| 96 |
|
| RETVAL->perl_thread_context = aTHX; |
| 97 |
|
| #endif |
| 98 |
|
| /* for now, always set output type to memory by default */ |
| 99 |
4 |
| tcc_set_output_type(RETVAL->tccstate, TCC_OUTPUT_MEMORY); |
| 100 |
|
| /* croaks by default */ |
| 101 |
4 |
| tcc_set_error_func(RETVAL->tccstate, (void *)RETVAL, xstcc_error_func); |
| 102 |
|
| OUTPUT: RETVAL |
| 103 |
|
| |
| 104 |
|
| void |
| 105 |
|
| DESTROY(xstcc_state *self) |
| 106 |
|
| CODE: |
| 107 |
4 |
| tcc_delete(self->tccstate); |
| 108 |
4 |
| SvREFCNT_dec(self->error_callback); /* has a NULL check */ |
| 109 |
4 |
| Safefree(self); |
| 110 |
|
| |
| 111 |
|
| void |
| 112 |
|
| set_lib_path(xstcc_state *self, const char *path) |
| 113 |
|
| CODE: |
| 114 |
3 |
| tcc_set_lib_path(self->tccstate, path); |
| 115 |
|
| |
| 116 |
|
| void |
| 117 |
|
| set_error_callback(xstcc_state *self, CV *callback) |
| 118 |
|
| CODE: |
| 119 |
1 |
| SvREFCNT_inc(callback); |
| 120 |
1 |
| SvREFCNT_dec(self->error_callback); /* includes NULL check */ |
| 121 |
1 |
| self->error_callback = callback; |
| 122 |
|
| |
| 123 |
|
| int |
| 124 |
|
| set_options(xstcc_state *self, const char *opt) |
| 125 |
|
| CODE: |
| 126 |
2 |
| RETVAL = tcc_set_options(self->tccstate, opt); |
| 127 |
|
| OUTPUT: RETVAL |
| 128 |
|
| |
| 129 |
|
| int |
| 130 |
|
| add_sysinclude_path(xstcc_state *self, const char *path) |
| 131 |
|
| ALIAS: |
| 132 |
|
| add_include_path = 1 |
| 133 |
|
| CODE: |
| 134 |
6 |
▶ | if (ix == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ix == 0) |
6 |
0 |
6 |
|
| 135 |
6 |
| RETVAL = tcc_add_sysinclude_path(self->tccstate, path); |
| 136 |
|
| else |
| 137 |
0 |
| RETVAL = tcc_add_include_path(self->tccstate, path); |
| 138 |
|
| OUTPUT: RETVAL |
| 139 |
|
| |
| 140 |
|
| void |
| 141 |
|
| define_symbol(xstcc_state *self, const char *sym, const char *value) |
| 142 |
|
| CODE: |
| 143 |
0 |
| tcc_define_symbol(self->tccstate, sym, value); |
| 144 |
|
| |
| 145 |
|
| void |
| 146 |
|
| undefine_symbol(xstcc_state *self, const char *sym) |
| 147 |
|
| CODE: |
| 148 |
0 |
| tcc_undefine_symbol(self->tccstate, sym); |
| 149 |
|
| |
| 150 |
|
| int |
| 151 |
|
| add_file(xstcc_state *self, const char *filename) |
| 152 |
|
| CODE: |
| 153 |
0 |
| RETVAL = tcc_add_file(self->tccstate, filename); |
| 154 |
|
| OUTPUT: RETVAL |
| 155 |
|
| |
| 156 |
|
| int |
| 157 |
|
| compile_string(xstcc_state *self, SV *code) |
| 158 |
|
| PREINIT: |
| 159 |
|
| STRLEN len; |
| 160 |
|
| char *cstr; |
| 161 |
|
| CODE: |
| 162 |
4 |
| cstr = SvPV(code, len); |
| 163 |
4 |
| RETVAL = tcc_compile_string(self->tccstate, cstr); |
| 164 |
|
| OUTPUT: RETVAL |
| 165 |
|
| |
| 166 |
|
| int |
| 167 |
|
| set_output_type(xstcc_state *self, int output_type); |
| 168 |
|
| CODE: |
| 169 |
0 |
| RETVAL = tcc_set_output_type(self->tccstate, output_type); |
| 170 |
|
| OUTPUT: RETVAL |
| 171 |
|
| |
| 172 |
|
| int |
| 173 |
|
| add_library_path(xstcc_state *self, const char *pathname) |
| 174 |
|
| CODE: |
| 175 |
0 |
| RETVAL = tcc_add_library_path(self->tccstate, pathname); |
| 176 |
|
| OUTPUT: RETVAL |
| 177 |
|
| |
| 178 |
|
| int |
| 179 |
|
| add_library(xstcc_state *self, const char *libname) |
| 180 |
|
| CODE: |
| 181 |
0 |
| RETVAL = tcc_add_library(self->tccstate, libname); |
| 182 |
|
| OUTPUT: RETVAL |
| 183 |
|
| |
| 184 |
|
| int |
| 185 |
|
| add_symbol(xstcc_state *self, const char *name, const char *value) |
| 186 |
|
| CODE: |
| 187 |
0 |
| RETVAL = tcc_add_symbol(self->tccstate, name, value); |
| 188 |
|
| OUTPUT: RETVAL |
| 189 |
|
| |
| 190 |
|
| int |
| 191 |
|
| output_file(xstcc_state *self, const char *filename) |
| 192 |
|
| CODE: |
| 193 |
0 |
| RETVAL = tcc_output_file(self->tccstate, filename); |
| 194 |
|
| OUTPUT: RETVAL |
| 195 |
|
| |
| 196 |
|
| int |
| 197 |
|
| run(xstcc_state *self, int argc, AV *argv) |
| 198 |
|
| PREINIT: |
| 199 |
|
| unsigned int avlen, i; |
| 200 |
|
| SV **elem; |
| 201 |
|
| char **str_argv; |
| 202 |
|
| SV *argv_ctrl; |
| 203 |
|
| CODE: |
| 204 |
0 |
| avlen = av_len(argv)+1; |
| 205 |
0 |
| argv_ctrl = sv_2mortal(newSV(avlen * sizeof(char *))); |
| 206 |
0 |
| str_argv = (char **)argv_ctrl; |
| 207 |
0 |
▶ | for (i = 0; i < avlen; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < avlen; ++i) { |
0 |
0 |
0 |
|
| 208 |
0 |
| elem = av_fetch(argv, i, 0); |
| 209 |
0 |
| str_argv[i] = SvPV_nolen(*elem); |
| 210 |
|
| } |
| 211 |
0 |
| RETVAL = tcc_run(self->tccstate, argc, str_argv); |
| 212 |
|
| OUTPUT: RETVAL |
| 213 |
|
| |
| 214 |
|
| xstcc_symbol * |
| 215 |
|
| get_symbol(xstcc_state *self, const char *name) |
| 216 |
|
| PREINIT: |
| 217 |
2 |
| const char *CLASS = "XS::TCC::TCCSymbol"; |
| 218 |
|
| /* Note: Perl symbol objects must not live longer than TCCStates |
| 219 |
|
| * or they become invalid. */ |
| 220 |
|
| CODE: |
| 221 |
2 |
| RETVAL = tcc_get_symbol(self->tccstate, name); |
| 222 |
2 |
▶ | if (RETVAL == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (RETVAL == NULL) |
0 |
2 |
2 |
|
| 223 |
0 |
| croak("Symbol '%s' not found!", name); |
| 224 |
|
| OUTPUT: RETVAL |
| 225 |
|
| |
| 226 |
|
| int |
| 227 |
|
| relocate(xstcc_state *self) |
| 228 |
|
| PREINIT: |
| 229 |
|
| AV *outav; |
| 230 |
|
| SV *memsv; |
| 231 |
|
| size_t memsize; |
| 232 |
|
| CODE: |
| 233 |
2 |
| outav = get_av("XS::TCC::TCCState::_output_memory", GV_ADD); |
| 234 |
2 |
| memsize = tcc_relocate(self->tccstate, NULL); |
| 235 |
2 |
| memsv = newSV(memsize); |
| 236 |
2 |
| av_push(outav, memsv); |
| 237 |
2 |
| RETVAL = tcc_relocate(self->tccstate, SvPVX(memsv)); |
| 238 |
|
| OUTPUT: RETVAL |
| 239 |
|
| |
| 240 |
|
| MODULE = XS::TCC PACKAGE = XS::TCC::TCCSymbol |
| 241 |
|
| |
| 242 |
|
| CV * |
| 243 |
|
| as_xsub(xstcc_symbol *self) |
| 244 |
|
| PREINIT: |
| 245 |
|
| XSUBADDR_t sub; |
| 246 |
|
| CODE: |
| 247 |
1 |
| sub = (XSUBADDR_t)self; |
| 248 |
1 |
| RETVAL = newXS(NULL, sub, "anon"); |
| 249 |
1 |
| sv_2mortal((SV *)RETVAL); |
| 250 |
|
| OUTPUT: RETVAL |
| 251 |
|
| |
| 252 |
|
| MODULE = XS::TCC PACKAGE = XS::TCC |