| 1 |
/* |
| 2 |
* code.c - compiled code builder/handler |
| 3 |
* |
| 4 |
* Copyright (c) 2005-2024 Shiro Kawai <[email protected]> |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the authors nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this |
| 19 |
* software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#define LIBGAUCHE_BODY |
| 35 |
#include "gauche.h" |
| 36 |
#include "gauche/priv/configP.h" |
| 37 |
#include "gauche/code.h" |
| 38 |
#include "gauche/vminsn.h" |
| 39 |
#include "gauche/priv/codeP.h" |
| 40 |
#include "gauche/priv/mmapP.h" |
| 41 |
#include "gauche/priv/identifierP.h" |
| 42 |
#include "gauche/priv/builtin-syms.h" |
| 43 |
|
| 44 |
/*=============================================================== |
| 45 |
* NVM related stuff |
| 46 |
*/ |
| 47 |
|
| 48 |
/* Debug information: |
| 49 |
* |
| 50 |
* debug info is kept as an assoc-list with insn offset |
| 51 |
* as a key. |
| 52 |
*/ |
| 53 |
|
| 54 |
ScmObj Scm_CompiledCodeFullName(ScmCompiledCode *cc) |
| 55 |
{ |
| 56 |
if (SCM_COMPILED_CODE_P(cc->parent) |
| 57 |
&& !SCM_EQ(SCM_COMPILED_CODE(cc->parent)->name, SCM_SYM_TOPLEVEL)) { |
| 58 |
ScmObj h = SCM_NIL, t = SCM_NIL; |
| 59 |
for (;;) { |
| 60 |
SCM_APPEND1(h, t, cc->name); |
| 61 |
if (!SCM_COMPILED_CODE_P(cc->parent)) break; |
| 62 |
cc = SCM_COMPILED_CODE(cc->parent); |
| 63 |
if (SCM_EQ(cc->name, SCM_SYM_TOPLEVEL)) break; |
| 64 |
} |
| 65 |
return Scm_ReverseX(h); |
| 66 |
} else { |
| 67 |
return cc->name; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
static ScmCompiledCode *make_compiled_code(void) |
| 72 |
{ |
| 73 |
ScmCompiledCode *cc = SCM_NEW(ScmCompiledCode); |
| 74 |
SCM_SET_CLASS(cc, SCM_CLASS_COMPILED_CODE); |
| 75 |
cc->code = NULL; |
| 76 |
cc->constants = NULL; |
| 77 |
cc->maxstack = -1; |
| 78 |
cc->debugInfo = SCM_NIL; |
| 79 |
cc->signatureInfo = SCM_FALSE; |
| 80 |
cc->name = SCM_FALSE; |
| 81 |
cc->parent = SCM_FALSE; |
| 82 |
cc->builder = NULL; |
| 83 |
return cc; |
| 84 |
} |
| 85 |
|
| 86 |
/* Copy the source compiled-code into the destination. This is used in |
| 87 |
the external optimizer to 'edit' code vector---such routines can |
| 88 |
create a new compiled-code, then copy it to the original so that |
| 89 |
the identity of compiled-code is kept. */ |
| 90 |
void Scm_CompiledCodeCopyX(ScmCompiledCode *dest, |
| 91 |
const ScmCompiledCode *src) |
| 92 |
{ |
| 93 |
SCM_ASSERT(dest->builder == NULL); |
| 94 |
SCM_ASSERT(src->builder == NULL); |
| 95 |
|
| 96 |
memcpy(dest, src, sizeof(ScmCompiledCode)); |
| 97 |
} |
| 98 |
|
| 99 |
/*---------------------------------------------------------------------- |
| 100 |
* An API to execute statically compiled toplevel code. *PROVISIONAL* |
| 101 |
*/ |
| 102 |
static ScmSubrProc execute_toplevels; |
| 103 |
|
| 104 |
void Scm_VMExecuteToplevels(ScmCompiledCode *cs[]) |
| 105 |
{ |
| 106 |
ScmObj proc = Scm_MakeSubr(execute_toplevels, cs, 0, 0, SCM_FALSE); |
| 107 |
Scm_ApplyRec(proc, SCM_NIL); |
| 108 |
} |
| 109 |
|
| 110 |
static ScmObj execute_toplevels_cc(ScmObj result SCM_UNUSED, void **data) |
| 111 |
{ |
| 112 |
ScmCompiledCode **cs = (ScmCompiledCode **)data[0]; |
| 113 |
if (cs[0] == NULL) return SCM_UNDEFINED; |
| 114 |
data[0] = cs+1; |
| 115 |
ScmVM *vm = Scm_VM(); |
| 116 |
Scm_VMPushCC(execute_toplevels_cc, data, 1); |
| 117 |
vm->base = cs[0]; |
| 118 |
vm->pc = vm->base->code; |
| 119 |
return SCM_UNDEFINED; |
| 120 |
} |
| 121 |
|
| 122 |
static ScmObj execute_toplevels(ScmObj *args SCM_UNUSED, |
| 123 |
int nargs SCM_UNUSED, |
| 124 |
void *cv) |
| 125 |
{ |
| 126 |
Scm_VMPushCC(execute_toplevels_cc, &cv, 1); |
| 127 |
return SCM_UNDEFINED; |
| 128 |
} |
| 129 |
|
| 130 |
/*---------------------------------------------------------------------- |
| 131 |
* Disassembler |
| 132 |
*/ |
| 133 |
static ScmObj check_lifted_closure(ScmWord *p, ScmObj lifted); |
| 134 |
static void print_header(const char *prefix, ScmObj name, ScmCompiledCode *cc); |
| 135 |
|
| 136 |
void Scm_CompiledCodeDump(ScmCompiledCode *cc) |
| 137 |
{ |
| 138 |
ScmObj closures = SCM_NIL, lifted = SCM_NIL, shown_lifted = SCM_NIL; |
| 139 |
int clonum = 0, more = FALSE; |
| 140 |
|
| 141 |
print_header("main_code", SCM_MAKE_STR(""), cc); |
| 142 |
do { |
| 143 |
ScmWord *p = cc->code; |
| 144 |
Scm_Printf(SCM_CUROUT, "signatureInfo: %S\n", cc->signatureInfo); |
| 145 |
for (int i=0; i < cc->codeSize; i++) { |
| 146 |
ScmWord insn = p[i]; |
| 147 |
ScmPort *out = SCM_PORT(Scm_MakeOutputStringPort(TRUE)); |
| 148 |
ScmObj info = Scm_Assq(SCM_MAKE_INT(i), Scm_CodeDebugInfo(cc)); |
| 149 |
u_int code = SCM_VM_INSN_CODE(insn); |
| 150 |
const char *insn_name = Scm_VMInsnName(code); |
| 151 |
|
| 152 |
switch (Scm_VMInsnNumParams(code)) { |
| 153 |
case 0: |
| 154 |
Scm_Printf(out, " %4d %s ", i, insn_name); |
| 155 |
break; |
| 156 |
case 1: |
| 157 |
Scm_Printf(out, " %4d %s(%d) ", i, insn_name, |
| 158 |
SCM_VM_INSN_ARG(insn)); |
| 159 |
break; |
| 160 |
case 2: |
| 161 |
Scm_Printf(out, " %4d %s(%d,%d) ", i, insn_name, |
| 162 |
SCM_VM_INSN_ARG0(insn),SCM_VM_INSN_ARG1(insn)); |
| 163 |
break; |
| 164 |
} |
| 165 |
switch (Scm_VMInsnOperandType(code)) { |
| 166 |
case SCM_VM_OPERAND_LABEL: |
| 167 |
Scm_Printf(out, "%d", (ScmWord*)p[i+1] - cc->code); |
| 168 |
i++; |
| 169 |
break; |
| 170 |
case SCM_VM_OPERAND_OBJ: |
| 171 |
/* Check if we're referring to a lifted closure. */ |
| 172 |
lifted = check_lifted_closure(p+i, lifted); |
| 173 |
Scm_Printf(out, "%S", p[i+1]); |
| 174 |
i++; |
| 175 |
break; |
| 176 |
case SCM_VM_OPERAND_OBJ_LABEL: |
| 177 |
Scm_Printf(out, "%S, %d", p[i+1], (ScmWord*)p[i+2] - cc->code); |
| 178 |
i += 2; |
| 179 |
break; |
| 180 |
case SCM_VM_OPERAND_OBJ_NATIVE: |
| 181 |
Scm_Printf(out, "%S, %p", p[i+1], (void*)p[i+2]); |
| 182 |
i += 2; |
| 183 |
break; |
| 184 |
case SCM_VM_OPERAND_CODE: |
| 185 |
Scm_Printf(out, "#<lambda %d>", clonum); |
| 186 |
closures = Scm_Acons(SCM_OBJ(p[i+1]), SCM_MAKE_INT(clonum), |
| 187 |
closures); |
| 188 |
clonum++; |
| 189 |
i++; |
| 190 |
break; |
| 191 |
case SCM_VM_OPERAND_CODES: { |
| 192 |
Scm_Printf(out, "("); |
| 193 |
ScmObj cp; |
| 194 |
SCM_FOR_EACH(cp, SCM_OBJ(p[i+1])) { |
| 195 |
if (SCM_COMPILED_CODE_P(SCM_CAR(cp))) { |
| 196 |
closures = Scm_Acons(SCM_CAR(cp), |
| 197 |
SCM_MAKE_INT(clonum), |
| 198 |
closures); |
| 199 |
Scm_Printf(out, "#<lambda %d>", clonum); |
| 200 |
clonum++; |
| 201 |
} |
| 202 |
} |
| 203 |
Scm_Printf(out, ")"); |
| 204 |
i++; |
| 205 |
break; |
| 206 |
} |
| 207 |
default: |
| 208 |
/*nothing*/; |
| 209 |
} |
| 210 |
|
| 211 |
/* Show info */ |
| 212 |
ScmObj s = Scm_GetOutputStringUnsafe(out, 0); |
| 213 |
if (!SCM_PAIRP(info)) { |
| 214 |
Scm_Puts(SCM_STRING(s), SCM_CUROUT); |
| 215 |
Scm_Putc('\n', SCM_CUROUT); |
| 216 |
} else { |
| 217 |
int len = SCM_STRING_BODY_SIZE(SCM_STRING_BODY(s)); |
| 218 |
ScmObj srcinfo = Scm_Assq(SCM_SYM_SOURCE_INFO, info); |
| 219 |
ScmObj bindinfo = Scm_Assq(SCM_SYM_BIND_INFO, info); |
| 220 |
Scm_Puts(SCM_STRING(s), SCM_CUROUT); |
| 221 |
Scm_Flush(SCM_CUROUT); |
| 222 |
for (; len<32; len++) { |
| 223 |
Scm_Putc(' ', SCM_CUROUT); |
| 224 |
} |
| 225 |
if (SCM_FALSEP(srcinfo)) { |
| 226 |
Scm_Printf(SCM_CUROUT, "; lambda %40.1S\n", |
| 227 |
SCM_CDR(bindinfo)); |
| 228 |
} else { |
| 229 |
Scm_Printf(SCM_CUROUT, "; %40.1S\n", |
| 230 |
Scm_UnwrapSyntax2(SCM_CDR(srcinfo), FALSE)); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
more = FALSE; |
| 235 |
if (!SCM_NULLP(closures)) { |
| 236 |
cc = SCM_COMPILED_CODE(SCM_CAAR(closures)); |
| 237 |
print_header("closure:", SCM_CDAR(closures), cc); |
| 238 |
closures = SCM_CDR(closures); |
| 239 |
more = TRUE; |
| 240 |
} else if (!SCM_NULLP(lifted)) { |
| 241 |
while (!SCM_NULLP(lifted)) { |
| 242 |
if (SCM_FALSEP(Scm_Memq(SCM_CAAR(lifted), shown_lifted))) { |
| 243 |
cc = SCM_COMPILED_CODE(SCM_CAAR(lifted)); |
| 244 |
print_header("lifted:", SCM_CDAR(lifted), cc); |
| 245 |
shown_lifted = Scm_Cons(SCM_CAAR(lifted), shown_lifted); |
| 246 |
lifted = SCM_CDR(lifted); |
| 247 |
more = TRUE; |
| 248 |
break; |
| 249 |
} else { |
| 250 |
lifted = SCM_CDR(lifted); |
| 251 |
continue; |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
} while (more); |
| 256 |
} |
| 257 |
|
| 258 |
static void print_header(const char *prefix, ScmObj name, ScmCompiledCode *cc) |
| 259 |
{ |
| 260 |
Scm_Printf(SCM_CUROUT, "=== %s%A (name=%S, cc=%p, codevec=%p, size=%d, const=%d stack=%d):\n", |
| 261 |
prefix, name, cc->name, cc, cc->code, |
| 262 |
cc->codeSize, cc->constantSize, cc->maxstack); |
| 263 |
} |
| 264 |
|
| 265 |
/* The compiler may have lifted an internal closure to a global procedure. |
| 266 |
We can tell so if the opcode is GREF_x, and the operand is an identifier, |
| 267 |
whose name is an uninterned symbol and it is globally bound to a procedure. |
| 268 |
|
| 269 |
If we indeed have a lifted closure, we chain the closure's code and |
| 270 |
the identifier into the lifted list, returns the updated list. |
| 271 |
Otherwise, we return lifted list as is. |
| 272 |
*/ |
| 273 |
static ScmObj check_lifted_closure(ScmWord *p, ScmObj lifted) |
| 274 |
{ |
| 275 |
ScmWord code = SCM_VM_INSN_CODE(p[0]); |
| 276 |
static ScmWord gref_insns[] = { |
| 277 |
SCM_VM_GREF, |
| 278 |
SCM_VM_GREF_PUSH, |
| 279 |
SCM_VM_GREF_CALL, |
| 280 |
SCM_VM_GREF_TAIL_CALL, |
| 281 |
SCM_VM_PUSH_GREF, |
| 282 |
SCM_VM_PUSH_GREF_CALL, |
| 283 |
SCM_VM_PUSH_GREF_TAIL_CALL |
| 284 |
}; |
| 285 |
|
| 286 |
if (!SCM_IDENTIFIERP(p[1])) return lifted; |
| 287 |
ScmIdentifier *id = Scm_OutermostIdentifier(SCM_IDENTIFIER(p[1])); |
| 288 |
if (SCM_SYMBOL_INTERNED(id->name)) return lifted; |
| 289 |
|
| 290 |
for (u_int i=0; i < sizeof(gref_insns)/sizeof(ScmWord); i++) { |
| 291 |
if (code == gref_insns[i]) { |
| 292 |
ScmObj g = Scm_GlobalVariableRef(id->module, SCM_SYMBOL(id->name), |
| 293 |
SCM_BINDING_STAY_IN_MODULE); |
| 294 |
if (SCM_CLOSUREP(g)) { |
| 295 |
if (SCM_FALSEP(Scm_Assq(SCM_CLOSURE(g)->code, lifted))) { |
| 296 |
return Scm_Acons(SCM_CLOSURE(g)->code, |
| 297 |
SCM_OBJ(id->name), |
| 298 |
lifted); |
| 299 |
} else { |
| 300 |
return lifted; |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
return lifted; |
| 306 |
} |
| 307 |
|
| 308 |
/*------------------------------------------------------------------ |
| 309 |
* Builder - used by the new compiler |
| 310 |
*/ |
| 311 |
|
| 312 |
#define CC_BUILDER_CHUNK_BITS 5 |
| 313 |
#define CC_BUILDER_CHUNK_SIZE (1L<<CC_BUILDER_CHUNK_BITS) |
| 314 |
#define CC_BUILDER_CHUNK_MASK (CC_BUILDER_CHUNK_SIZE-1) |
| 315 |
|
| 316 |
typedef struct cc_builder_chunk { |
| 317 |
struct cc_builder_chunk *prev; |
| 318 |
ScmWord code[CC_BUILDER_CHUNK_SIZE]; |
| 319 |
} cc_builder_chunk; |
| 320 |
|
| 321 |
/* To perform instruction combination, the builder buffers one insn/operand. |
| 322 |
* currentInsn == SCM_WORD(-1) indicates there's no buffered insn. |
| 323 |
*/ |
| 324 |
typedef struct cc_builder_rec { |
| 325 |
cc_builder_chunk *chunks; |
| 326 |
int numChunks; |
| 327 |
ScmObj constants; /* list of constants */ |
| 328 |
int currentIndex; |
| 329 |
ScmWord currentInsn; /* buffer for instruction combining. |
| 330 |
this can be a special value either |
| 331 |
CC_BUILDER_BUFFER_EMPTY or |
| 332 |
CC_BUILDER_BUFFER_TRANS. see below. */ |
| 333 |
int prevOpcode; /* previous saved insn opcode */ |
| 334 |
int currentOpcode; /* saved insn opcode */ |
| 335 |
int currentArg0; /* ditto */ |
| 336 |
int currentArg1; /* ditto */ |
| 337 |
ScmObj currentOperand; /* ditto */ |
| 338 |
ScmObj currentInfo; /* ditto */ |
| 339 |
int currentState; /* index to the current state of |
| 340 |
combiner STN */ |
| 341 |
ScmObj labelDefs; /* alist of (name . offset) */ |
| 342 |
ScmObj labelRefs; /* alist of (name . offset-to-fill) */ |
| 343 |
int labelCount; /* counter to generate unique labels */ |
| 344 |
ScmObj debugInfo; /* alist of (offset (source-info obj)) */ |
| 345 |
} cc_builder; |
| 346 |
|
| 347 |
/* Indicates that there's no pending instruction. */ |
| 348 |
#define CC_BUILDER_BUFFER_EMPTY SCM_WORD(-1) |
| 349 |
|
| 350 |
/* Indicates that the instruction combiner is in the transitional |
| 351 |
state. In ordinary circumstances this state will be resolved |
| 352 |
as the code generation goes. However, if the instruction combination |
| 353 |
is "cut off", for example by emitting a jump destination label, we |
| 354 |
have to complete the instruction. It is done by seeking for |
| 355 |
the default arc of the current state. */ |
| 356 |
#define CC_BUILDER_BUFFER_TRANS SCM_WORD(-2) |
| 357 |
|
| 358 |
/* Some internal stuff */ |
| 359 |
|
| 360 |
#define CC_BUILDER_GET(b, cc) \ |
| 361 |
do { \ |
| 362 |
if (cc->builder == NULL) { \ |
| 363 |
Scm_Error("[internal error] CompiledCode is already frozen"); \ |
| 364 |
} \ |
| 365 |
(b) = (cc_builder*)cc->builder; \ |
| 366 |
} while (0) |
| 367 |
|
| 368 |
static cc_builder *make_cc_builder(void) |
| 369 |
{ |
| 370 |
cc_builder *b = SCM_NEW(cc_builder); |
| 371 |
b->chunks = NULL; |
| 372 |
b->numChunks = 0; |
| 373 |
b->constants = SCM_NIL; |
| 374 |
b->currentIndex = 0; |
| 375 |
b->currentInsn = CC_BUILDER_BUFFER_EMPTY; |
| 376 |
b->currentOpcode = b->prevOpcode = -1; |
| 377 |
b->currentOperand = b->currentInfo = SCM_FALSE; |
| 378 |
b->currentState = -1; |
| 379 |
b->labelDefs = b->labelRefs = SCM_NIL; |
| 380 |
b->labelCount = 0; |
| 381 |
b->debugInfo = SCM_NIL; |
| 382 |
return b; |
| 383 |
} |
| 384 |
|
| 385 |
static void cc_builder_add_word(cc_builder *b, ScmWord w) |
| 386 |
{ |
| 387 |
int ni = b->currentIndex & CC_BUILDER_CHUNK_MASK; |
| 388 |
if (ni == 0) { |
| 389 |
cc_builder_chunk *newchunk = SCM_NEW(cc_builder_chunk); |
| 390 |
newchunk->prev = b->chunks; |
| 391 |
b->chunks = newchunk; |
| 392 |
b->numChunks++; |
| 393 |
} |
| 394 |
b->chunks->code[ni] = w; |
| 395 |
b->currentIndex++; |
| 396 |
} |
| 397 |
|
| 398 |
static void cc_builder_add_constant(cc_builder *b, ScmObj obj) |
| 399 |
{ |
| 400 |
if (!SCM_PTRP(obj)) return; |
| 401 |
if (!SCM_FALSEP(Scm_Memq(obj, b->constants))) return; |
| 402 |
b->constants = Scm_Cons(obj, b->constants); |
| 403 |
} |
| 404 |
|
| 405 |
static void cc_builder_add_info(cc_builder *b) |
| 406 |
{ |
| 407 |
if (SCM_FALSEP(b->currentInfo)) return; |
| 408 |
b->debugInfo = Scm_Acons(SCM_MAKE_INT(b->currentIndex), |
| 409 |
SCM_LIST1(Scm_Cons(SCM_SYM_SOURCE_INFO, |
| 410 |
b->currentInfo)), |
| 411 |
b->debugInfo); |
| 412 |
b->currentInfo = SCM_FALSE; |
| 413 |
} |
| 414 |
|
| 415 |
/* Returns label offset of the given label, if the label is already defined. |
| 416 |
Otherwise, returns -1. */ |
| 417 |
static int cc_builder_label_def(cc_builder *b, ScmObj label) |
| 418 |
{ |
| 419 |
ScmObj p = Scm_Assq(label, b->labelDefs); |
| 420 |
if (SCM_PAIRP(p)) { |
| 421 |
return SCM_INT_VALUE(SCM_CDR(p)); |
| 422 |
} else { |
| 423 |
return -1; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
static void finish_transition(cc_builder *b); |
| 428 |
|
| 429 |
/* Flush the currentInsn buffer. */ |
| 430 |
static void cc_builder_flush(cc_builder *b) |
| 431 |
{ |
| 432 |
if ((b)->currentInsn == CC_BUILDER_BUFFER_EMPTY) return; |
| 433 |
if ((b)->currentInsn == CC_BUILDER_BUFFER_TRANS) { |
| 434 |
finish_transition(b); |
| 435 |
} |
| 436 |
cc_builder_add_info(b); |
| 437 |
cc_builder_add_word(b, b->currentInsn); |
| 438 |
|
| 439 |
u_int code = SCM_VM_INSN_CODE(b->currentInsn); |
| 440 |
switch (Scm_VMInsnOperandType(code)) { |
| 441 |
case SCM_VM_OPERAND_LABEL: |
| 442 |
/* We just push the label reference into labelRefs, and emit a |
| 443 |
dummy address for the time being. (we can't emit the actual |
| 444 |
number even if we're referring to the label that has already |
| 445 |
appeared, since the number should be calculated after the |
| 446 |
code vector is allocated.) */ |
| 447 |
b->labelRefs = Scm_Acons(b->currentOperand, |
| 448 |
SCM_MAKE_INT(b->currentIndex), |
| 449 |
b->labelRefs); |
| 450 |
cc_builder_add_word(b, SCM_WORD(0)); /* dummy */ |
| 451 |
break; |
| 452 |
case SCM_VM_OPERAND_OBJ:; |
| 453 |
case SCM_VM_OPERAND_CODES: |
| 454 |
cc_builder_add_word(b, SCM_WORD(b->currentOperand)); |
| 455 |
cc_builder_add_constant(b, b->currentOperand); |
| 456 |
break; |
| 457 |
case SCM_VM_OPERAND_OBJ_LABEL: |
| 458 |
/* operand would be given as a list of (OBJ LABEL). */ |
| 459 |
SCM_ASSERT(SCM_PAIRP(b->currentOperand) |
| 460 |
&& SCM_PAIRP(SCM_CDR(b->currentOperand))); |
| 461 |
cc_builder_add_word(b, SCM_WORD(SCM_CAR(b->currentOperand))); |
| 462 |
cc_builder_add_constant(b, SCM_CAR(b->currentOperand)); |
| 463 |
b->labelRefs = Scm_Acons(SCM_CADR(b->currentOperand), |
| 464 |
SCM_MAKE_INT(b->currentIndex), |
| 465 |
b->labelRefs); |
| 466 |
cc_builder_add_word(b, SCM_WORD(0)); /* dummy */ |
| 467 |
break; |
| 468 |
case SCM_VM_OPERAND_OBJ_NATIVE: |
| 469 |
/* operand would be given as a list of (MEM-REGION OFFSET). */ |
| 470 |
SCM_ASSERT(SCM_PAIRP(b->currentOperand) |
| 471 |
&& SCM_MEMORY_REGION_P(SCM_CAR(b->currentOperand)) |
| 472 |
&& SCM_PAIRP(SCM_CDR(b->currentOperand)) |
| 473 |
&& SCM_INTP(SCM_CADR(b->currentOperand))); |
| 474 |
ScmMemoryRegion *mem = SCM_MEMORY_REGION(SCM_CAR(b->currentOperand)); |
| 475 |
cc_builder_add_word(b, SCM_WORD(mem)); |
| 476 |
cc_builder_add_constant(b, SCM_OBJ(mem)); |
| 477 |
u_long offset = Scm_GetIntegerU(SCM_CADR(b->currentOperand)); |
| 478 |
cc_builder_add_word(b, SCM_WORD(mem->ptr + offset)); |
| 479 |
break; |
| 480 |
case SCM_VM_OPERAND_CODE: |
| 481 |
if (!SCM_COMPILED_CODE_P(b->currentOperand)) goto badoperand; |
| 482 |
cc_builder_add_word(b, SCM_WORD(b->currentOperand)); |
| 483 |
cc_builder_add_constant(b, b->currentOperand); |
| 484 |
default: |
| 485 |
break; |
| 486 |
} |
| 487 |
b->currentInsn = CC_BUILDER_BUFFER_EMPTY; |
| 488 |
b->currentState = -1; |
| 489 |
b->currentOpcode = -1; |
| 490 |
return; |
| 491 |
badoperand: |
| 492 |
b->currentInsn = CC_BUILDER_BUFFER_EMPTY; |
| 493 |
b->currentState = -1; |
| 494 |
Scm_Error("[internal error] bad operand: %S", b->currentOperand); |
| 495 |
return; |
| 496 |
} |
| 497 |
|
| 498 |
/* a peephole optimization; rewrite jump destination for cascaded jump |
| 499 |
* |
| 500 |
* - if the destination of JUMP-like insn (including conditional jump |
| 501 |
* and PRE-CALL) is another JUMP, rewrite the destination. |
| 502 |
* - if the destination of BF is another BF (this pattern appears frequently, |
| 503 |
* e.g. 'or' is used in the test clause of 'cond'), rewrite the destination. |
| 504 |
*/ |
| 505 |
static void cc_builder_jumpopt(ScmCompiledCode *cc) |
| 506 |
{ |
| 507 |
ScmWord *cp = cc->code; |
| 508 |
|
| 509 |
for (u_int i=0; i<(u_int)cc->codeSize; i++) { |
| 510 |
u_int code = SCM_VM_INSN_CODE(*cp); cp++; |
| 511 |
switch (Scm_VMInsnOperandType(code)) { |
| 512 |
case SCM_VM_OPERAND_OBJ:; |
| 513 |
case SCM_VM_OPERAND_CODE:; |
| 514 |
case SCM_VM_OPERAND_CODES:; |
| 515 |
i++; cp++; |
| 516 |
break; |
| 517 |
case SCM_VM_OPERAND_OBJ_LABEL:; |
| 518 |
case SCM_VM_OPERAND_OBJ_NATIVE: |
| 519 |
i++; cp++; |
| 520 |
/*FALLTHROUGH*/ |
| 521 |
case SCM_VM_OPERAND_LABEL: { |
| 522 |
ScmWord *target = (ScmWord*)*cp; |
| 523 |
while (SCM_VM_INSN_CODE(*target) == SCM_VM_JUMP |
| 524 |
|| (code == SCM_VM_BF |
| 525 |
&& SCM_VM_INSN_CODE(*target) == SCM_VM_BF)) { |
| 526 |
target = (ScmWord*)target[1]; |
| 527 |
} |
| 528 |
if (target != (ScmWord*)*cp) { |
| 529 |
*cp = SCM_WORD(target); |
| 530 |
} |
| 531 |
i++; cp++; |
| 532 |
break; |
| 533 |
} |
| 534 |
default: |
| 535 |
break; |
| 536 |
} |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
/* Creates and returns a new empty compiled-code object for building |
| 541 |
new code chunk. */ |
| 542 |
ScmObj Scm_MakeCompiledCodeBuilder(int reqargs, int optargs, |
| 543 |
ScmObj name, ScmObj parent, ScmObj intForm) |
| 544 |
{ |
| 545 |
ScmCompiledCode *cc = make_compiled_code(); |
| 546 |
cc->builder = make_cc_builder(); |
| 547 |
cc->requiredArgs = reqargs; |
| 548 |
cc->optionalArgs = optargs; |
| 549 |
cc->name = name; |
| 550 |
cc->parent = parent; |
| 551 |
cc->intermediateForm = intForm; |
| 552 |
return SCM_OBJ(cc); |
| 553 |
} |
| 554 |
|
| 555 |
/* Returns a label identifier (integer) unique to this code block */ |
| 556 |
ScmObj Scm_CompiledCodeNewLabel(ScmCompiledCode *cc) |
| 557 |
{ |
| 558 |
cc_builder *b; |
| 559 |
CC_BUILDER_GET(b, cc); |
| 560 |
ScmObj label = SCM_MAKE_INT(b->labelCount); |
| 561 |
b->labelCount++; |
| 562 |
return label; |
| 563 |
} |
| 564 |
|
| 565 |
/* Set label to the current instruction position. */ |
| 566 |
void Scm_CompiledCodeSetLabel(ScmCompiledCode *cc, ScmObj label) |
| 567 |
{ |
| 568 |
cc_builder *b; |
| 569 |
|
| 570 |
CC_BUILDER_GET(b, cc); |
| 571 |
|
| 572 |
/* Flush buffered insn first. */ |
| 573 |
cc_builder_flush(b); |
| 574 |
|
| 575 |
/* NB: should check duplicate labels */ |
| 576 |
b->labelDefs = Scm_Acons(label, SCM_MAKE_INT(b->currentIndex), |
| 577 |
b->labelDefs); |
| 578 |
} |
| 579 |
|
| 580 |
/* Push arbitrary debug-info into the builder. |
| 581 |
<debug-info> : (<insn-offset> <item> ...) |
| 582 |
<insn-offset> : <integer> or 'definition |
| 583 |
Currently supported item: |
| 584 |
<item> : (<source-info> . source) |
| 585 |
*/ |
| 586 |
void Scm_CompiledCodePushInfo(ScmCompiledCode *cc, ScmObj info) |
| 587 |
{ |
| 588 |
cc_builder *b; |
| 589 |
CC_BUILDER_GET(b, cc); |
| 590 |
b->debugInfo = Scm_Cons(info, b->debugInfo); |
| 591 |
} |
| 592 |
|
| 593 |
/* Pack the code accumulated in the builder into a code vector. |
| 594 |
Perform label resolution and jump optimization. */ |
| 595 |
void Scm_CompiledCodeFinishBuilder(ScmCompiledCode *cc, int maxstack) |
| 596 |
{ |
| 597 |
cc_builder *b; |
| 598 |
CC_BUILDER_GET(b, cc); |
| 599 |
cc_builder_flush(b); |
| 600 |
cc->code = SCM_NEW_ATOMIC2(ScmWord *, b->currentIndex * sizeof(ScmWord)); |
| 601 |
cc->codeSize = b->currentIndex; |
| 602 |
|
| 603 |
/* reverse chunks, leaving the first chunk in bcprev. */ |
| 604 |
cc_builder_chunk *bc, *bcprev = NULL; |
| 605 |
for (bc = b->chunks; bc;) { |
| 606 |
cc_builder_chunk *next = bc->prev; |
| 607 |
bc->prev = bcprev; |
| 608 |
bcprev = bc; |
| 609 |
bc = next; |
| 610 |
} |
| 611 |
|
| 612 |
/* pack words */ |
| 613 |
bc = bcprev; |
| 614 |
for (int i=0, j=0; i<b->currentIndex; i++, j++) { |
| 615 |
if (j >= CC_BUILDER_CHUNK_SIZE) { |
| 616 |
bc = bc->prev; |
| 617 |
j = 0; |
| 618 |
} |
| 619 |
cc->code[i] = bc->code[j]; |
| 620 |
} |
| 621 |
|
| 622 |
/* pack constants */ |
| 623 |
int numConstants = Scm_Length(b->constants); |
| 624 |
if (numConstants > 0) { |
| 625 |
cc->constants = SCM_NEW_ARRAY(ScmObj, numConstants); |
| 626 |
ScmObj cp = b->constants; |
| 627 |
for (int i=0; i<numConstants; i++, cp=SCM_CDR(cp)) { |
| 628 |
cc->constants[i] = SCM_CAR(cp); |
| 629 |
} |
| 630 |
} |
| 631 |
cc->constantSize = numConstants; |
| 632 |
|
| 633 |
/* resolve labels */ |
| 634 |
ScmObj cp; |
| 635 |
SCM_FOR_EACH(cp, b->labelRefs) { |
| 636 |
int destAddr = cc_builder_label_def(b, SCM_CAAR(cp)); |
| 637 |
int operandAddr; |
| 638 |
if (destAddr < 0) { |
| 639 |
Scm_Error("[internal error] undefined label in compiled code: %S", |
| 640 |
SCM_CAAR(cp)); |
| 641 |
} |
| 642 |
operandAddr = SCM_INT_VALUE(SCM_CDAR(cp)); |
| 643 |
SCM_ASSERT(operandAddr >= 0 && operandAddr < cc->codeSize); |
| 644 |
cc->code[operandAddr] = SCM_WORD(cc->code + destAddr); |
| 645 |
} |
| 646 |
|
| 647 |
/* jump destination optimization */ |
| 648 |
cc_builder_jumpopt(cc); |
| 649 |
|
| 650 |
/* record debug info */ |
| 651 |
if (!SCM_VM_COMPILER_FLAG_IS_SET(Scm_VM(), SCM_COMPILE_NOSOURCE)) { |
| 652 |
cc->debugInfo = b->debugInfo; |
| 653 |
} |
| 654 |
|
| 655 |
/* set max stack depth */ |
| 656 |
cc->maxstack = maxstack; |
| 657 |
|
| 658 |
/* make sure this code is 'fixed'---no more building */ |
| 659 |
cc->builder = NULL; |
| 660 |
} |
| 661 |
|
| 662 |
/*---------------------------------------------------------------- |
| 663 |
* Emitting instruction and operand, performing instruction combination |
| 664 |
*/ |
| 665 |
|
| 666 |
/* This is originally implemented in Scheme, but moved here for efficiency, |
| 667 |
* since this routine is the most frequently called one during compilation. |
| 668 |
*/ |
| 669 |
|
| 670 |
/* The state transition table */ |
| 671 |
struct stn_arc { |
| 672 |
int input; /* input insn, or -1 for wildcard */ |
| 673 |
int action; /* EMIT, KEEP, NEXT */ |
| 674 |
int next; /* emitting insn / next state */ |
| 675 |
}; |
| 676 |
|
| 677 |
/* State transition actions */ |
| 678 |
enum { |
| 679 |
NEXT, |
| 680 |
EMIT, |
| 681 |
KEEP |
| 682 |
}; |
| 683 |
|
| 684 |
/* Include STN generated from vminsn.scm */ |
| 685 |
static struct stn_arc stn[] = { |
| 686 |
#define STATE_TABLE |
| 687 |
#include "vminsn.c" |
| 688 |
#undef STATE_TABLE |
| 689 |
}; |
| 690 |
|
| 691 |
/* Save the args/operand if necessary */ |
| 692 |
static inline void save_params(cc_builder *b, int code, |
| 693 |
int arg0, int arg1, ScmObj operand, |
| 694 |
ScmObj info) |
| 695 |
{ |
| 696 |
b->prevOpcode = b->currentOpcode; |
| 697 |
b->currentOpcode = code; |
| 698 |
switch (Scm_VMInsnNumParams(code)) { |
| 699 |
case 2: b->currentArg1 = arg1; |
| 700 |
/* FALLTHROUGH */ |
| 701 |
case 1: b->currentArg0 = arg0; |
| 702 |
/* FALLTHROUGH */ |
| 703 |
case 0:; |
| 704 |
} |
| 705 |
if (Scm_VMInsnOperandType(code) != SCM_VM_OPERAND_NONE) { |
| 706 |
b->currentOperand = operand; |
| 707 |
} |
| 708 |
if (!SCM_FALSEP(info)) { |
| 709 |
b->currentInfo = info; |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
static int vm_insn_flags(u_int code); |
| 714 |
|
| 715 |
/* Fill the current insn word */ |
| 716 |
static inline void fill_current_insn(cc_builder *b, int code) |
| 717 |
{ |
| 718 |
/* A special handling of fold-lref insn. |
| 719 |
Fold-lref insn is a combined insn LREF-XXXX(depth,offset). What's |
| 720 |
special about it is that we 'fold' specialized LREF insn |
| 721 |
(e.g. LREF10) into generic LREF. |
| 722 |
*/ |
| 723 |
#define SET_LREF_ARGS(dep, off) \ |
| 724 |
b->currentArg0 = (dep); b->currentArg1 = (off); break |
| 725 |
if (vm_insn_flags(code) & SCM_VM_INSN_FOLD_LREF) { |
| 726 |
switch (b->prevOpcode) { |
| 727 |
case SCM_VM_LREF0: SET_LREF_ARGS(0, 0); |
| 728 |
case SCM_VM_LREF1: SET_LREF_ARGS(0, 1); |
| 729 |
case SCM_VM_LREF2: SET_LREF_ARGS(0, 2); |
| 730 |
case SCM_VM_LREF3: SET_LREF_ARGS(0, 3); |
| 731 |
case SCM_VM_LREF10: SET_LREF_ARGS(1, 0); |
| 732 |
case SCM_VM_LREF11: SET_LREF_ARGS(1, 1); |
| 733 |
case SCM_VM_LREF12: SET_LREF_ARGS(1, 2); |
| 734 |
case SCM_VM_LREF20: SET_LREF_ARGS(2, 0); |
| 735 |
case SCM_VM_LREF21: SET_LREF_ARGS(2, 1); |
| 736 |
case SCM_VM_LREF30: SET_LREF_ARGS(3, 0); |
| 737 |
case SCM_VM_LREF: /* args are already set */ break; |
| 738 |
default: Scm_Error("[internal] Compiler internal error: FOLD_LREF insn needs to be combined with LREF*, but prevOpcode = %d", b->prevOpcode); |
| 739 |
} |
| 740 |
} |
| 741 |
#undef SET_LREF_ARGS |
| 742 |
|
| 743 |
/* Compose insn word */ |
| 744 |
switch (Scm_VMInsnNumParams(code)) { |
| 745 |
case 0: b->currentInsn = SCM_VM_INSN(code); break; |
| 746 |
case 1: b->currentInsn = SCM_VM_INSN1(code, b->currentArg0); break; |
| 747 |
case 2: b->currentInsn = SCM_VM_INSN2(code, |
| 748 |
b->currentArg0, |
| 749 |
b->currentArg1); break; |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
/* Called by cc_builder_flush to finish the current transition forcibly. |
| 754 |
We look for the default arc (-1) of the current state and use that |
| 755 |
insn to represent the current state. We can assume parameters and |
| 756 |
operands are set properly in the cc_builder. */ |
| 757 |
static void finish_transition(cc_builder *b) |
| 758 |
{ |
| 759 |
int i = b->currentState; |
| 760 |
SCM_ASSERT(i >= 0 && i < (int)(sizeof(stn)/sizeof(struct stn_arc[1]))); |
| 761 |
for (;; i++) { |
| 762 |
if (stn[i].input < 0) { |
| 763 |
fill_current_insn(b, stn[i].next); |
| 764 |
break; |
| 765 |
} |
| 766 |
SCM_ASSERT(i < (int)(sizeof(stn)/sizeof(struct stn_arc[1]))); |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
void Scm_CompiledCodeEmit(ScmCompiledCode *cc, |
| 771 |
int code, /* instruction code number */ |
| 772 |
int arg0, /* instruction code parameter 0 */ |
| 773 |
int arg1, /* instruction code parameter 1 */ |
| 774 |
ScmObj operand, |
| 775 |
ScmObj info) /* debug info */ |
| 776 |
{ |
| 777 |
cc_builder *b; |
| 778 |
struct stn_arc *arc; |
| 779 |
CC_BUILDER_GET(b, cc); |
| 780 |
|
| 781 |
if (SCM_VM_COMPILER_FLAG_IS_SET(Scm_VM(), SCM_COMPILE_NOCOMBINE)) { |
| 782 |
save_params(b, code, arg0, arg1, operand, info); |
| 783 |
fill_current_insn(b, code); |
| 784 |
cc_builder_flush(b); |
| 785 |
return; |
| 786 |
} |
| 787 |
|
| 788 |
restart: |
| 789 |
/* Some insn needs special treatment. */ |
| 790 |
if (code == SCM_VM_LREF) { |
| 791 |
static const int lrefs[4][4] = { |
| 792 |
{ SCM_VM_LREF0, SCM_VM_LREF1, SCM_VM_LREF2, SCM_VM_LREF3 }, |
| 793 |
{ SCM_VM_LREF10, SCM_VM_LREF11, SCM_VM_LREF12, -1 }, |
| 794 |
{ SCM_VM_LREF20, SCM_VM_LREF21, -1, -1 }, |
| 795 |
{ SCM_VM_LREF30, -1, -1, -1 } |
| 796 |
}; |
| 797 |
if (arg0 < 4 && arg1 < 4) { |
| 798 |
int insn = lrefs[arg0][arg1]; |
| 799 |
if (insn >= 0) code = insn; |
| 800 |
} |
| 801 |
} else if (code == SCM_VM_CONST) { |
| 802 |
if (SCM_NULLP(operand)) { |
| 803 |
code = SCM_VM_CONSTN; |
| 804 |
} else if (SCM_FALSEP(operand)) { |
| 805 |
code = SCM_VM_CONSTF; |
| 806 |
} else if (SCM_UNDEFINEDP(operand)) { |
| 807 |
code = SCM_VM_CONSTU; |
| 808 |
} else if (SCM_INTP(operand)) { |
| 809 |
long v = SCM_INT_VALUE(operand); |
| 810 |
if (SCM_VM_INSN_ARG_FITS(v)) { |
| 811 |
code = SCM_VM_CONSTI; |
| 812 |
arg0 = v; |
| 813 |
} |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
/* Look up the state */ |
| 818 |
if (b->currentState < 0) { |
| 819 |
arc = stn + code; |
| 820 |
} else { |
| 821 |
int i = b->currentState; |
| 822 |
for (;;i++) { |
| 823 |
if (stn[i].input == code || stn[i].input == -1) { |
| 824 |
arc = stn + i; |
| 825 |
break; |
| 826 |
} |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
switch (arc->action) { |
| 831 |
case EMIT: |
| 832 |
save_params(b, code, arg0, arg1, operand, info); |
| 833 |
fill_current_insn(b, arc->next); |
| 834 |
cc_builder_flush(b); |
| 835 |
b->currentState = -1; |
| 836 |
break; |
| 837 |
case KEEP: |
| 838 |
fill_current_insn(b, arc->next); |
| 839 |
cc_builder_flush(b); |
| 840 |
b->currentState = -1; |
| 841 |
goto restart; |
| 842 |
case NEXT: |
| 843 |
save_params(b, code, arg0, arg1, operand, info); |
| 844 |
b->currentState = arc->next; |
| 845 |
b->currentInsn = CC_BUILDER_BUFFER_TRANS; |
| 846 |
break; |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
/*---------------------------------------------------------------- |
| 851 |
* CompiledCode - introspection |
| 852 |
*/ |
| 853 |
|
| 854 |
/* Converts the code vector into a list. |
| 855 |
Instruction -> (<insn-symbol> [<arg0> <arg1>]) |
| 856 |
Obj/Code operand -> as is |
| 857 |
Addr operand -> integer offset from the beginning of the code */ |
| 858 |
ScmObj Scm_CompiledCodeToList(ScmCompiledCode *cc) |
| 859 |
{ |
| 860 |
ScmObj h = SCM_NIL, t = SCM_NIL; |
| 861 |
|
| 862 |
for (u_int i=0; i<(u_int)cc->codeSize; i++) { |
| 863 |
ScmWord insn = cc->code[i]; |
| 864 |
u_int code = SCM_VM_INSN_CODE(insn); |
| 865 |
const char *name = Scm_VMInsnName(code); |
| 866 |
|
| 867 |
switch (Scm_VMInsnNumParams(code)) { |
| 868 |
case 0: |
| 869 |
SCM_APPEND1(h, t, SCM_LIST1(SCM_INTERN(name))); |
| 870 |
break; |
| 871 |
case 1: |
| 872 |
SCM_APPEND1(h, t, SCM_LIST2(SCM_INTERN(name), |
| 873 |
SCM_MAKE_INT(SCM_VM_INSN_ARG(insn)))); |
| 874 |
break; |
| 875 |
case 2: |
| 876 |
SCM_APPEND1(h, t, SCM_LIST3(SCM_INTERN(name), |
| 877 |
SCM_MAKE_INT(SCM_VM_INSN_ARG0(insn)), |
| 878 |
SCM_MAKE_INT(SCM_VM_INSN_ARG1(insn)))); |
| 879 |
break; |
| 880 |
} |
| 881 |
|
| 882 |
switch (Scm_VMInsnOperandType(code)) { |
| 883 |
case SCM_VM_OPERAND_OBJ:; |
| 884 |
case SCM_VM_OPERAND_CODE:; |
| 885 |
case SCM_VM_OPERAND_CODES:; |
| 886 |
SCM_APPEND1(h, t, SCM_OBJ(cc->code[++i])); |
| 887 |
break; |
| 888 |
case SCM_VM_OPERAND_LABEL: { |
| 889 |
u_int off = (u_int)((ScmWord*)cc->code[++i] - cc->code); |
| 890 |
SCM_APPEND1(h, t, SCM_MAKE_INT(off)); |
| 891 |
break; |
| 892 |
} |
| 893 |
case SCM_VM_OPERAND_OBJ_LABEL: { |
| 894 |
u_int off = (u_int)((ScmWord*)cc->code[i+2] - cc->code); |
| 895 |
SCM_APPEND(h, t, SCM_LIST2(SCM_OBJ(cc->code[i+1]), |
| 896 |
SCM_MAKE_INT(off))); |
| 897 |
i += 2; |
| 898 |
break; |
| 899 |
} |
| 900 |
case SCM_VM_OPERAND_OBJ_NATIVE: { |
| 901 |
ScmObj addr = Scm_IntptrToInteger((intptr_t)cc->code[i+2]); |
| 902 |
SCM_APPEND(h, t, SCM_LIST2(SCM_OBJ(cc->code[i+1]), addr)); |
| 903 |
i += 2; |
| 904 |
break; |
| 905 |
} |
| 906 |
} |
| 907 |
} |
| 908 |
return h; |
| 909 |
} |
| 910 |
|
| 911 |
/*=========================================================== |
| 912 |
* VM Instruction introspection |
| 913 |
*/ |
| 914 |
|
| 915 |
static struct { |
| 916 |
const char *name; /* name */ |
| 917 |
int nparams; /* # of parameters */ |
| 918 |
int operandType; /* operand type */ |
| 919 |
int flags; /* flags */ |
| 920 |
} insn_table[] = { |
| 921 |
#define DEFINSN(sym, nam, np, type, flags) \ |
| 922 |
[sym] = { nam, np, SCM_CPP_CAT(SCM_VM_OPERAND_, type), flags }, |
| 923 |
#include "vminsn.c" |
| 924 |
#undef DEFINSN |
| 925 |
}; |
| 926 |
|
| 927 |
#define CHECK_CODE(code) \ |
| 928 |
do { \ |
| 929 |
if (code >= SCM_VM_NUM_INSNS) { \ |
| 930 |
Scm_Error("invalid VM instruction code: %d", code); \ |
| 931 |
} \ |
| 932 |
} while (0) |
| 933 |
|
| 934 |
const char *Scm_VMInsnName(u_int code) |
| 935 |
{ |
| 936 |
CHECK_CODE(code); |
| 937 |
return insn_table[code].name; |
| 938 |
} |
| 939 |
|
| 940 |
int Scm_VMInsnNumParams(u_int code) |
| 941 |
{ |
| 942 |
CHECK_CODE(code); |
| 943 |
return insn_table[code].nparams; |
| 944 |
} |
| 945 |
|
| 946 |
int vm_insn_flags(u_int code) /* private for the time being */ |
| 947 |
{ |
| 948 |
CHECK_CODE(code); |
| 949 |
return insn_table[code].flags; |
| 950 |
} |
| 951 |
|
| 952 |
int Scm_VMInsnOperandType(u_int code) |
| 953 |
{ |
| 954 |
CHECK_CODE(code); |
| 955 |
return insn_table[code].operandType; |
| 956 |
} |
| 957 |
|
| 958 |
int Scm_VMInsnNameToCode(ScmObj name) |
| 959 |
{ |
| 960 |
if (SCM_SYMBOLP(name)) name = SCM_OBJ(SCM_SYMBOL_NAME(name)); |
| 961 |
else if (!SCM_STRINGP(name)) { |
| 962 |
Scm_Error("vm-insn-name->code: requires a symbol or a string, but got %S", name); |
| 963 |
} |
| 964 |
const char *n = Scm_GetStringConst(SCM_STRING(name)); |
| 965 |
for (int i=0; i<SCM_VM_NUM_INSNS; i++) { |
| 966 |
if (strcmp(insn_table[i].name, n) == 0) { |
| 967 |
return i; |
| 968 |
} |
| 969 |
} |
| 970 |
Scm_Error("vm-insn-name->code: no such instruction: %A", name); |
| 971 |
return -1; /* dummy */ |
| 972 |
} |
| 973 |
|
| 974 |
/* (kind of) inversion of VMInsnInspect. */ |
| 975 |
ScmWord Scm_VMInsnBuild(ScmObj obj) |
| 976 |
{ |
| 977 |
int len = Scm_Length(obj); |
| 978 |
|
| 979 |
if (len < 1 || len > 3 || !SCM_SYMBOLP(SCM_CAR(obj))) goto badspec; |
| 980 |
int code = Scm_VMInsnNameToCode(SCM_CAR(obj)); |
| 981 |
|
| 982 |
switch (Scm_VMInsnNumParams(code)) { |
| 983 |
case 0: |
| 984 |
if (len != 1) { |
| 985 |
Scm_Error("VM instruction %S takes no parameters, but got %S", |
| 986 |
SCM_CAR(obj), obj); |
| 987 |
} |
| 988 |
return SCM_VM_INSN(code); |
| 989 |
case 1: { |
| 990 |
if (len != 2) { |
| 991 |
Scm_Error("VM instruction %S takes one parameter, but got %S", |
| 992 |
SCM_CAR(obj), obj); |
| 993 |
} |
| 994 |
if (!SCM_INTP(SCM_CADR(obj))) goto badspec; |
| 995 |
int arg0 = SCM_INT_VALUE(SCM_CADR(obj)); |
| 996 |
return SCM_VM_INSN1(code, arg0); |
| 997 |
} |
| 998 |
case 2: { |
| 999 |
if (len != 3) { |
| 1000 |
Scm_Error("VM instruction %S takes two parameters, but got %S", |
| 1001 |
SCM_CAR(obj), obj); |
| 1002 |
} |
| 1003 |
if (!SCM_INTP(SCM_CADR(obj))) goto badspec; |
| 1004 |
if (!SCM_INTP(SCM_CAR(SCM_CDDR(obj)))) goto badspec; |
| 1005 |
int arg0 = SCM_INT_VALUE(SCM_CADR(obj)); |
| 1006 |
int arg1 = SCM_INT_VALUE(SCM_CAR(SCM_CDDR(obj))); |
| 1007 |
return SCM_VM_INSN2(code, arg0, arg1); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
/*FALLTHROUGH*/ |
| 1011 |
badspec: |
| 1012 |
Scm_Error("Bad VM insn spec: %S", obj); |
| 1013 |
return 0; /* dummy */ |
| 1014 |
} |
| 1015 |
|
| 1016 |
/*=========================================================== |
| 1017 |
* Packed debug info |
| 1018 |
*/ |
| 1019 |
|
| 1020 |
ScmObj Scm_MakePackedDebugInfo(ScmUVector *packedVector, |
| 1021 |
ScmObj (*getConstVector)(void)) |
| 1022 |
{ |
| 1023 |
ScmPackedDebugInfo *z = SCM_NEW(ScmPackedDebugInfo); |
| 1024 |
SCM_SET_CLASS(z, SCM_CLASS_PACKED_DEBUG_INFO); |
| 1025 |
z->codeSize = SCM_UVECTOR_SIZE(packedVector); |
| 1026 |
z->codeVector = SCM_U8VECTOR_ELEMENTS(packedVector); |
| 1027 |
z->constVector = SCM_FALSE; |
| 1028 |
z->getConstVector = getConstVector; |
| 1029 |
z->decoded = SCM_FALSE; |
| 1030 |
return SCM_OBJ(z); |
| 1031 |
} |
| 1032 |
|
| 1033 |
/* |
| 1034 |
* The decoder is implemented in C, since this can be called during examining |
| 1035 |
* VM state, thus the decoder should not change VM state. |
| 1036 |
* See lib/gauche/vm/debug-info.scm for the details of packed debug info. |
| 1037 |
*/ |
| 1038 |
typedef struct debug_info_decoder { |
| 1039 |
const uint8_t *packed; |
| 1040 |
const ScmObj *consts; |
| 1041 |
int pos; /* bit position (excluding version octet) */ |
| 1042 |
int len; |
| 1043 |
ScmHashCore table; /* bit position -> pair */ |
| 1044 |
} debug_info_decoder; |
| 1045 |
|
| 1046 |
#define DEBUG_INFO_VERSION 0 /* must match the encoder */ |
| 1047 |
|
| 1048 |
static ScmObj decode_list(debug_info_decoder *decoder); |
| 1049 |
|
| 1050 |
static void init_decoder(debug_info_decoder *decoder, |
| 1051 |
const uint8_t *packed, |
| 1052 |
int packed_len, |
| 1053 |
const ScmVector *const_vector) |
| 1054 |
{ |
| 1055 |
decoder->packed = packed; |
| 1056 |
decoder->len = packed_len; |
| 1057 |
decoder->consts = SCM_VECTOR_ELEMENTS(const_vector); |
| 1058 |
decoder->pos = 0; |
| 1059 |
Scm_HashCoreInitSimple(&decoder->table, SCM_HASH_WORD, 0, NULL); |
| 1060 |
|
| 1061 |
SCM_ASSERT(decoder->len > 0); |
| 1062 |
if (packed[0] != DEBUG_INFO_VERSION) { |
| 1063 |
Scm_Panic("Invalid debug-info"); |
| 1064 |
} |
| 1065 |
decoder->packed++; |
| 1066 |
decoder->len--; |
| 1067 |
} |
| 1068 |
|
| 1069 |
static int next_bit(debug_info_decoder *decoder) |
| 1070 |
{ |
| 1071 |
int bytepos = decoder->pos >> 3; |
| 1072 |
int bitpos = decoder->pos & 0x07; |
| 1073 |
if (bytepos >= decoder->len) return -1; |
| 1074 |
decoder->pos++; |
| 1075 |
return (decoder->packed[bytepos] & (1<<(7-bitpos)))? 1 : 0; |
| 1076 |
} |
| 1077 |
|
| 1078 |
static u_long next_nbits(debug_info_decoder *decoder, int nbits) |
| 1079 |
{ |
| 1080 |
u_long r = 0; |
| 1081 |
for (; nbits > 0; nbits--) { |
| 1082 |
int b = next_bit(decoder); |
| 1083 |
SCM_ASSERT(b >= 0); |
| 1084 |
r = (r << 1) + b; |
| 1085 |
} |
| 1086 |
return r; |
| 1087 |
} |
| 1088 |
|
| 1089 |
static u_long peek_nbits(debug_info_decoder *decoder, int nbits) |
| 1090 |
{ |
| 1091 |
int oldpos = decoder->pos; |
| 1092 |
u_long r = 0; |
| 1093 |
for (; nbits > 0; nbits--) { |
| 1094 |
int b = next_bit(decoder); |
| 1095 |
if (b < 0) return (u_long)-1; |
| 1096 |
r = (r << 1) + b; |
| 1097 |
} |
| 1098 |
decoder->pos = oldpos; |
| 1099 |
return r; |
| 1100 |
} |
| 1101 |
|
| 1102 |
static void skip_nbits(debug_info_decoder *decoder, int nbits) |
| 1103 |
{ |
| 1104 |
decoder->pos += nbits; |
| 1105 |
} |
| 1106 |
|
| 1107 |
static void save_pair(debug_info_decoder *decoder, ScmObj pair) |
| 1108 |
{ |
| 1109 |
ScmDictEntry *e = Scm_HashCoreSearch(&decoder->table, |
| 1110 |
decoder->pos, |
| 1111 |
SCM_DICT_CREATE); |
| 1112 |
(void)SCM_DICT_SET_VALUE(e, pair); |
| 1113 |
} |
| 1114 |
|
| 1115 |
static int decode_index(debug_info_decoder *decoder, int leading_bits) |
| 1116 |
{ |
| 1117 |
_Bool cont; |
| 1118 |
u_long r = 0; |
| 1119 |
int nbits = leading_bits; |
| 1120 |
do { |
| 1121 |
cont = next_bit(decoder); |
| 1122 |
r = (r << nbits) + next_nbits(decoder, nbits); |
| 1123 |
nbits = 5; /* following bits comes in 5 */ |
| 1124 |
} while (cont); |
| 1125 |
return r; |
| 1126 |
} |
| 1127 |
|
| 1128 |
static ScmObj decode_integer(debug_info_decoder *decoder, int leading_bits) |
| 1129 |
{ |
| 1130 |
_Bool cont; |
| 1131 |
ScmObj v = SCM_MAKE_INT(0); |
| 1132 |
int nbits = leading_bits; |
| 1133 |
do { |
| 1134 |
cont = next_bit(decoder); |
| 1135 |
u_long r = next_nbits(decoder, nbits); |
| 1136 |
v = Scm_LogIor(Scm_Ash(v, nbits), SCM_MAKE_INT(r)); |
| 1137 |
nbits = 5; /* following bits comes in 5 */ |
| 1138 |
} while (cont); |
| 1139 |
return v; |
| 1140 |
} |
| 1141 |
|
| 1142 |
static ScmObj decode_item(debug_info_decoder *decoder) |
| 1143 |
{ |
| 1144 |
if (!next_bit(decoder)) { |
| 1145 |
if (!next_bit(decoder)) { |
| 1146 |
if (!next_bit(decoder)) { |
| 1147 |
/* 000 */ |
| 1148 |
return decode_list(decoder); |
| 1149 |
} else { |
| 1150 |
/* 001 */ |
| 1151 |
Scm_Error("Stray end-of-list marker"); |
| 1152 |
} |
| 1153 |
} else { |
| 1154 |
if (!next_bit(decoder)) { |
| 1155 |
if (!next_bit(decoder)) { |
| 1156 |
/* 0100 */ |
| 1157 |
Scm_Error("Stray dot-list marker"); |
| 1158 |
} else { |
| 1159 |
/* 0101 */ |
| 1160 |
return SCM_NIL; |
| 1161 |
} |
| 1162 |
} else { |
| 1163 |
/* 011, predefined value |
| 1164 |
The following must be in sync with *predefined-values* |
| 1165 |
in debug-info.scm. It can be generated by |
| 1166 |
gosh -mgauche.vm.debug-info gauche/vm/debug-info |
| 1167 |
*/ |
| 1168 |
switch (next_nbits(decoder, 5)) |
| 1169 |
{ |
| 1170 |
case 0: return SCM_SYM_SOURCE_INFO; |
| 1171 |
case 1: return SCM_SYM_QUOTE; |
| 1172 |
case 2: return SCM_SYM_DEFINITION; |
| 1173 |
case 3: return SCM_SYM_LAMBDA; |
| 1174 |
case 4: return SCM_SYM_LET; |
| 1175 |
case 5: return SCM_SYM_IF; |
| 1176 |
case 6: return SCM_SYM_DEFINE; |
| 1177 |
case 7: return SCM_SYM_ERROR; |
| 1178 |
case 8: return SCM_SYM_CAR; |
| 1179 |
case 9: return SCM_SYM_ELSE; |
| 1180 |
case 10: return SCM_SYM_CDR; |
| 1181 |
case 11: return SCM_SYM_NULLP; |
| 1182 |
case 12: return SCM_SYM_COND; |
| 1183 |
case 13: return SCM_SYM_LET1; |
| 1184 |
case 14: return SCM_FALSE; |
| 1185 |
case 15: return SCM_SYM_UNLESS; |
| 1186 |
case 16: return SCM_SYM_AND; |
| 1187 |
case 17: return SCM_SYM_LOOP; |
| 1188 |
case 18: return SCM_SYM_CARET; |
| 1189 |
case 19: return SCM_SYM_CONS; |
| 1190 |
case 20: return SCM_SYM_PLUS; |
| 1191 |
case 21: return SCM_SYM_APPLY; |
| 1192 |
case 22: return SCM_SYM_OR; |
| 1193 |
case 23: return SCM_SYM_CADR; |
| 1194 |
case 24: return SCM_SYM_WHEN; |
| 1195 |
case 25: return SCM_SYM_NOT; |
| 1196 |
case 26: return SCM_SYM_BEGIN; |
| 1197 |
case 27: return SCM_SYM_EQP; |
| 1198 |
case 28: return SCM_SYM_CDDR; |
| 1199 |
case 29: return SCM_SYM_LIST; |
| 1200 |
case 30: return SCM_TRUE; |
| 1201 |
case 31: return SCM_SYM_WITH_MODULE; |
| 1202 |
} |
| 1203 |
} |
| 1204 |
} |
| 1205 |
} else { |
| 1206 |
if (!next_bit(decoder)) { |
| 1207 |
/* 10 */ |
| 1208 |
u_long index = decode_index(decoder, 7); |
| 1209 |
return decoder->consts[index]; |
| 1210 |
} else { |
| 1211 |
if (!next_bit(decoder)) { |
| 1212 |
/* 110 */ |
| 1213 |
u_long ind = decode_index(decoder, 10); |
| 1214 |
ScmDictEntry *e = |
| 1215 |
Scm_HashCoreSearch(&decoder->table, ind, SCM_DICT_GET); |
| 1216 |
return SCM_OBJ(SCM_DICT_VALUE(e)); |
| 1217 |
} else { |
| 1218 |
/* 111 */ |
| 1219 |
return decode_integer(decoder, 7); |
| 1220 |
} |
| 1221 |
} |
| 1222 |
} |
| 1223 |
return SCM_UNDEFINED; /* dummy */ |
| 1224 |
} |
| 1225 |
|
| 1226 |
static ScmObj decode_list(debug_info_decoder *decoder) |
| 1227 |
{ |
| 1228 |
if (peek_nbits(decoder, 3) == 1) { /* EOL */ |
| 1229 |
skip_nbits(decoder, 3); |
| 1230 |
return SCM_NIL; |
| 1231 |
} |
| 1232 |
if (peek_nbits(decoder, 4) == 4) { /* dot */ |
| 1233 |
skip_nbits(decoder, 4); |
| 1234 |
return decode_item(decoder); |
| 1235 |
} |
| 1236 |
else { |
| 1237 |
ScmObj pair = Scm_Cons(SCM_FALSE, SCM_FALSE); |
| 1238 |
save_pair(decoder, pair); |
| 1239 |
SCM_SET_CAR(pair, decode_item(decoder)); |
| 1240 |
SCM_SET_CDR(pair, decode_list(decoder)); |
| 1241 |
return pair; |
| 1242 |
} |
| 1243 |
} |
| 1244 |
|
| 1245 |
ScmObj Scm_DecodePackedDebugInfo(const uint8_t *codeVector, |
| 1246 |
ScmSmallInt codeSize, |
| 1247 |
ScmVector *constVector) |
| 1248 |
{ |
| 1249 |
debug_info_decoder decoder; |
| 1250 |
init_decoder(&decoder, codeVector, codeSize, constVector); |
| 1251 |
return decode_item(&decoder); |
| 1252 |
} |
| 1253 |
|
| 1254 |
ScmObj Scm_CodeDebugInfo(ScmCompiledCode *cc) |
| 1255 |
{ |
| 1256 |
if (SCM_PACKED_DEBUG_INFO_P(cc->debugInfo)) { |
| 1257 |
ScmPackedDebugInfo *di = SCM_PACKED_DEBUG_INFO(cc->debugInfo); |
| 1258 |
if (SCM_FALSEP(di->constVector)) { |
| 1259 |
/* This is idempotent. No need to mutex. */ |
| 1260 |
di->constVector = di->getConstVector(); |
| 1261 |
} |
| 1262 |
cc->debugInfo = Scm_DecodePackedDebugInfo(di->codeVector, |
| 1263 |
di->codeSize, |
| 1264 |
SCM_VECTOR(di->constVector)); |
| 1265 |
} |
| 1266 |
return cc->debugInfo; |
| 1267 |
} |