| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* disassembler.d - bytecodes disassembler utilities |
| 6 |
* |
| 7 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 8 |
* |
| 9 |
* See file 'LICENSE' for the copyright details. |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <ecl/ecl.h> |
| 14 |
#include <ecl/ecl-inl.h> |
| 15 |
#include <ecl/bytecodes.h> |
| 16 |
#include <ecl/internal.h> |
| 17 |
|
| 18 |
static cl_opcode *disassemble(cl_object bytecodes, cl_opcode *vector); |
| 19 |
|
| 20 |
static cl_opcode *base = NULL; |
| 21 |
|
| 22 |
static void |
| 23 |
print_noarg(const char *s) { |
| 24 |
ecl_princ_str(s, ECL_NIL); |
| 25 |
} |
| 26 |
|
| 27 |
static void |
| 28 |
print_oparg(const char *s, cl_fixnum n) { |
| 29 |
ecl_princ_str(s, ECL_NIL); |
| 30 |
ecl_princ(ecl_make_fixnum(n), ECL_NIL); |
| 31 |
} |
| 32 |
|
| 33 |
static void |
| 34 |
print_arg(const char *s, cl_object x) { |
| 35 |
ecl_princ_str(s, ECL_NIL); |
| 36 |
ecl_princ(x, ECL_NIL); |
| 37 |
} |
| 38 |
|
| 39 |
static void |
| 40 |
print_oparg_arg(const char *s, cl_fixnum n, cl_object x) { |
| 41 |
ecl_princ_str(s, ECL_NIL); |
| 42 |
ecl_princ(ecl_make_fixnum(n), ECL_NIL); |
| 43 |
ecl_princ_str(",", ECL_NIL); |
| 44 |
ecl_princ(x, ECL_NIL); |
| 45 |
} |
| 46 |
|
| 47 |
#define GET_DATA(r,v,data) { \ |
| 48 |
cl_oparg ndx; \ |
| 49 |
GET_OPARG(ndx, v); \ |
| 50 |
r = data[ndx]; \ |
| 51 |
} |
| 52 |
|
| 53 |
static void |
| 54 |
disassemble_lambda(cl_object bytecodes) { |
| 55 |
const cl_env_ptr env = ecl_process_env(); |
| 56 |
cl_opcode *vector; |
| 57 |
|
| 58 |
ecl_bds_bind(env, @'*print-pretty*', ECL_NIL); |
| 59 |
|
| 60 |
/* Print required arguments */ |
| 61 |
cl_print(1,bytecodes->bytecodes.data); |
| 62 |
|
| 63 |
/* Name of LAMBDA */ |
| 64 |
print_arg("\nName:\t\t", bytecodes->bytecodes.name); |
| 65 |
if (bytecodes->bytecodes.name == OBJNULL || |
| 66 |
bytecodes->bytecodes.name == @'si::bytecodes') { |
| 67 |
print_noarg("\nEvaluated form:"); |
| 68 |
goto NO_ARGS; |
| 69 |
} |
| 70 |
|
| 71 |
NO_ARGS: |
| 72 |
base = vector = (cl_opcode *)bytecodes->bytecodes.code; |
| 73 |
disassemble(bytecodes, vector); |
| 74 |
|
| 75 |
ecl_bds_unwind1(env); |
| 76 |
} |
| 77 |
|
| 78 |
/* -------------------- DISASSEMBLER CORE -------------------- */ |
| 79 |
|
| 80 |
/* OP_FLET nfun{arg}, fun1{object} |
| 81 |
... |
| 82 |
|
| 83 |
Executes the enclosed code in a lexical enviroment extended with |
| 84 |
the functions "fun1" ... "funn". |
| 85 |
*/ |
| 86 |
static cl_opcode * |
| 87 |
disassemble_flet(cl_object bytecodes, cl_opcode *vector) { |
| 88 |
cl_index nfun; |
| 89 |
cl_object *data = bytecodes->bytecodes.data->vector.self.t; |
| 90 |
GET_OPARG(nfun, vector); |
| 91 |
print_noarg("FLET"); |
| 92 |
while (nfun--) { |
| 93 |
cl_object fun; |
| 94 |
GET_DATA(fun, vector, data); |
| 95 |
print_arg("\n\tFLET\t", fun->bytecodes.name); |
| 96 |
} |
| 97 |
return vector; |
| 98 |
} |
| 99 |
|
| 100 |
/* OP_LABELS nfun{arg}, fun1{object} |
| 101 |
... |
| 102 |
|
| 103 |
Executes the enclosed code in a lexical enviroment extended with |
| 104 |
the functions "fun1" ... "funn". |
| 105 |
*/ |
| 106 |
static cl_opcode * |
| 107 |
disassemble_labels(cl_object bytecodes, cl_opcode *vector) { |
| 108 |
cl_index nfun; |
| 109 |
cl_object *data = bytecodes->bytecodes.data->vector.self.t; |
| 110 |
GET_OPARG(nfun, vector); |
| 111 |
print_noarg("LABELS"); |
| 112 |
while (nfun--) { |
| 113 |
cl_object fun; |
| 114 |
GET_DATA(fun, vector, data); |
| 115 |
print_arg("\n\tLABELS\t", fun->bytecodes.name); |
| 116 |
} |
| 117 |
return vector; |
| 118 |
} |
| 119 |
|
| 120 |
/* OP_PROGV bindings{list} |
| 121 |
... |
| 122 |
OP_EXIT |
| 123 |
Execute the code enclosed with the special variables in BINDINGS |
| 124 |
set to the values in the list which was passed in VALUES(0). |
| 125 |
*/ |
| 126 |
static cl_opcode * |
| 127 |
disassemble_progv(cl_object bytecodes, cl_opcode *vector) { |
| 128 |
print_noarg("PROGV"); |
| 129 |
vector = disassemble(bytecodes, vector); |
| 130 |
print_noarg("\t\t; progv"); |
| 131 |
return vector; |
| 132 |
} |
| 133 |
|
| 134 |
/* OP_TAGBODY n{arg} |
| 135 |
label1 |
| 136 |
... |
| 137 |
labeln |
| 138 |
label1: |
| 139 |
... |
| 140 |
labeln: |
| 141 |
... |
| 142 |
OP_EXIT |
| 143 |
|
| 144 |
High level construct for the TAGBODY form. |
| 145 |
*/ |
| 146 |
static cl_opcode * |
| 147 |
disassemble_tagbody(cl_object bytecodes, cl_opcode *vector) { |
| 148 |
cl_index i, ntags; |
| 149 |
cl_opcode *destination; |
| 150 |
GET_OPARG(ntags, vector); |
| 151 |
print_noarg("TAGBODY"); |
| 152 |
for (i=0; i<ntags; i++) { |
| 153 |
GET_LABEL(destination, vector); |
| 154 |
ecl_princ_str("\n\tTAG\t", ECL_NIL); |
| 155 |
ecl_princ(ecl_make_fixnum(i), ECL_NIL); |
| 156 |
ecl_princ_str(" @@ ", ECL_NIL); |
| 157 |
ecl_princ(ecl_make_fixnum(destination - base), ECL_NIL); |
| 158 |
} |
| 159 |
vector = disassemble(bytecodes, vector); |
| 160 |
print_noarg("\t\t; tagbody"); |
| 161 |
|
| 162 |
return vector; |
| 163 |
} |
| 164 |
|
| 165 |
static cl_opcode * |
| 166 |
disassemble(cl_object bytecodes, cl_opcode *vector) { |
| 167 |
const char *string; |
| 168 |
cl_object o; |
| 169 |
cl_fixnum n, m; |
| 170 |
cl_object line_format; |
| 171 |
cl_object *data = bytecodes->bytecodes.data->vector.self.t; |
| 172 |
cl_object line_no; |
| 173 |
|
| 174 |
if (cl_fboundp(@'si::formatter-aux') != ECL_NIL) |
| 175 |
line_format = @"~%~4d\t"; |
| 176 |
else |
| 177 |
line_format = ECL_NIL; |
| 178 |
BEGIN: |
| 179 |
if (1) { |
| 180 |
line_no = ecl_make_fixnum(vector-base); |
| 181 |
} else { |
| 182 |
line_no = @'*'; |
| 183 |
} |
| 184 |
if (line_format != ECL_NIL) { |
| 185 |
cl_format(3, ECL_T, line_format, line_no); |
| 186 |
} else { |
| 187 |
ecl_princ_char('\n', ECL_NIL); |
| 188 |
ecl_princ(line_no, ECL_NIL); |
| 189 |
ecl_princ_char('\t', ECL_NIL); |
| 190 |
} |
| 191 |
switch (GET_OPCODE(vector)) { |
| 192 |
|
| 193 |
/* OP_NOP |
| 194 |
Sets VALUES(0) = NIL and NVALUES = 1 |
| 195 |
*/ |
| 196 |
case OP_NOP: string = "NOP"; goto NOARG; |
| 197 |
|
| 198 |
case OP_INT: string = "QUOTE\t"; |
| 199 |
GET_OPARG(n, vector); |
| 200 |
goto OPARG; |
| 201 |
|
| 202 |
case OP_PINT: string = "PUSH\t"; |
| 203 |
GET_OPARG(n, vector); |
| 204 |
goto OPARG; |
| 205 |
|
| 206 |
/* OP_QUOTE |
| 207 |
Sets VALUES(0) to an immediate value. |
| 208 |
*/ |
| 209 |
case OP_QUOTE: string = "QUOTE\t"; |
| 210 |
GET_DATA(o, vector, data); |
| 211 |
goto ARG; |
| 212 |
|
| 213 |
/* OP_CSET n{arg} |
| 214 |
Replace constant with a computed value |
| 215 |
*/ |
| 216 |
case OP_CSET: string = "CSET\t"; |
| 217 |
GET_OPARG(n, vector); |
| 218 |
goto OPARG; |
| 219 |
|
| 220 |
/* OP_VAR n{arg} |
| 221 |
Sets reg0 to the value of the n-th local. |
| 222 |
*/ |
| 223 |
case OP_VAR: string = "VAR\t"; |
| 224 |
GET_OPARG(n, vector); |
| 225 |
goto OPARG; |
| 226 |
|
| 227 |
/* OP_VARC n{arg} |
| 228 |
Sets reg0 to the value of the n-th lexical. |
| 229 |
*/ |
| 230 |
case OP_VARC: string = "VARC\t"; |
| 231 |
GET_OPARG(n, vector); |
| 232 |
goto OPARG; |
| 233 |
|
| 234 |
/* OP_VARS var{symbol} |
| 235 |
Sets reg0 to the value of the symbol VAR. |
| 236 |
VAR should be either a special variable or a constant. |
| 237 |
*/ |
| 238 |
case OP_VARS: string = "VARS\t"; |
| 239 |
GET_DATA(o, vector, data); |
| 240 |
goto ARG; |
| 241 |
|
| 242 |
/* OP_PUSH |
| 243 |
Pushes the object in VALUES(0). |
| 244 |
*/ |
| 245 |
case OP_PUSH: string = "PUSH\tVALUES(0)"; |
| 246 |
goto NOARG; |
| 247 |
|
| 248 |
case OP_VALUEREG0: string = "SET\tVALUES(0),REG0"; |
| 249 |
goto NOARG; |
| 250 |
|
| 251 |
/* OP_PUSHV n{arg} |
| 252 |
Pushes the value of the n-th local onto the stack. |
| 253 |
*/ |
| 254 |
case OP_PUSHV: string = "PUSHV\t"; |
| 255 |
GET_OPARG(n, vector); |
| 256 |
goto OPARG; |
| 257 |
|
| 258 |
/* OP_PUSHV n{arg} |
| 259 |
Pushes the value of the n-th lexical onto the stack. |
| 260 |
*/ |
| 261 |
case OP_PUSHVC: string = "PUSHVC\t"; |
| 262 |
GET_OPARG(n, vector); |
| 263 |
goto OPARG; |
| 264 |
|
| 265 |
/* OP_PUSHVS var{symbol} |
| 266 |
Pushes the value of the symbol VAR onto the stack. |
| 267 |
VAR should be either a special variable or a constant. |
| 268 |
*/ |
| 269 |
case OP_PUSHVS: string = "PUSHVS\t"; |
| 270 |
GET_DATA(o, vector, data); |
| 271 |
goto ARG; |
| 272 |
|
| 273 |
/* OP_PUSHQ value{object} |
| 274 |
Pushes "value" onto the stack. |
| 275 |
*/ |
| 276 |
case OP_PUSHQ: string = "PUSH\t'"; |
| 277 |
GET_DATA(o, vector, data); |
| 278 |
goto ARG; |
| 279 |
|
| 280 |
/* OP_PUSHVALUES |
| 281 |
Pushes the values output by the last form, plus the number |
| 282 |
of values. |
| 283 |
*/ |
| 284 |
case OP_PUSHVALUES: string = "PUSH\tVALUES"; |
| 285 |
goto NOARG; |
| 286 |
/* OP_PUSHMOREVALUES |
| 287 |
Adds more values to the ones pushed by OP_PUSHVALUES. |
| 288 |
*/ |
| 289 |
case OP_PUSHMOREVALUES: string = "PUSH\tMORE VALUES"; |
| 290 |
goto NOARG; |
| 291 |
/* OP_POP |
| 292 |
Pops a single value pushed by a OP_PUSH[V[S]] operator. |
| 293 |
*/ |
| 294 |
case OP_POP: string = "POP"; |
| 295 |
goto NOARG; |
| 296 |
/* OP_POP1 |
| 297 |
Pops a single value pushed by a OP_PUSH[V[S]] operator. |
| 298 |
*/ |
| 299 |
case OP_POP1: string = "POP1"; |
| 300 |
goto NOARG; |
| 301 |
/* OP_POPVALUES |
| 302 |
Pops all values pushed by a OP_PUSHVALUES operator. |
| 303 |
*/ |
| 304 |
case OP_POPVALUES: string = "POP\tVALUES"; |
| 305 |
goto NOARG; |
| 306 |
|
| 307 |
case OP_BLOCK: string = "BLOCK\t"; |
| 308 |
GET_DATA(o, vector, data); |
| 309 |
goto ARG; |
| 310 |
case OP_CATCH: string = "CATCH\tREG0"; |
| 311 |
goto NOARG; |
| 312 |
case OP_DO: string = "BLOCK\t"; |
| 313 |
o = ECL_NIL; |
| 314 |
goto ARG; |
| 315 |
case OP_FRAME: string = "FRAME\t"; |
| 316 |
goto JMP; |
| 317 |
|
| 318 |
/* OP_CALL n{arg} |
| 319 |
Calls the function in VALUES(0) with N arguments which |
| 320 |
have been deposited in the stack. The output values |
| 321 |
are left in VALUES(...) |
| 322 |
*/ |
| 323 |
case OP_CALL: string = "CALL\t"; |
| 324 |
GET_OPARG(n, vector); |
| 325 |
goto OPARG; |
| 326 |
|
| 327 |
/* OP_CALLG n{arg}, name{arg} |
| 328 |
Calls the function NAME with N arguments which have been |
| 329 |
deposited in the stack. The output values are left in VALUES. |
| 330 |
*/ |
| 331 |
case OP_CALLG: string = "CALLG\t"; |
| 332 |
GET_OPARG(n, vector); |
| 333 |
GET_DATA(o, vector, data); |
| 334 |
goto OPARG_ARG; |
| 335 |
|
| 336 |
/* OP_FCALL n{arg} |
| 337 |
Calls the function in the stack with N arguments which |
| 338 |
have been also deposited in the stack. The output values |
| 339 |
are left in VALUES(...) |
| 340 |
*/ |
| 341 |
case OP_STEPCALL: |
| 342 |
case OP_FCALL: string = "FCALL\t"; |
| 343 |
GET_OPARG(n, vector); |
| 344 |
goto OPARG; |
| 345 |
|
| 346 |
/* OP_MCALL |
| 347 |
Similar to FCALL, but gets the number of arguments from |
| 348 |
the stack (They all have been deposited by OP_PUSHVALUES) |
| 349 |
*/ |
| 350 |
case OP_MCALL: string = "MCALL"; |
| 351 |
goto NOARG; |
| 352 |
|
| 353 |
/* OP_POPREQ |
| 354 |
Extracts next required argument. |
| 355 |
*/ |
| 356 |
case OP_POPREQ: string = "POP\tREQ"; |
| 357 |
goto NOARG; |
| 358 |
/* OP_NOMORE |
| 359 |
Ensure there are no more arguments. |
| 360 |
*/ |
| 361 |
case OP_NOMORE: string = "NOMORE"; |
| 362 |
goto NOARG; |
| 363 |
/* OP_POPOPT |
| 364 |
Extracts next optional argument. |
| 365 |
*/ |
| 366 |
case OP_POPOPT: string = "POP\tOPT"; |
| 367 |
goto NOARG; |
| 368 |
/* OP_POPREST |
| 369 |
Extracts list of remaining arguments. |
| 370 |
*/ |
| 371 |
case OP_POPREST: string = "POP\tREST"; |
| 372 |
goto NOARG; |
| 373 |
/* OP_PUSHKEYS |
| 374 |
Parses the keyword arguments |
| 375 |
*/ |
| 376 |
case OP_PUSHKEYS: string = "PUSH\tKEYS "; |
| 377 |
GET_DATA(o, vector, data); |
| 378 |
goto ARG; |
| 379 |
|
| 380 |
/* OP_EXIT |
| 381 |
Marks the end of a high level construct |
| 382 |
*/ |
| 383 |
case OP_EXIT: print_noarg("EXIT"); |
| 384 |
return vector; |
| 385 |
/* OP_EXIT_FRAME |
| 386 |
Marks the end of a high level construct (BLOCK, CATCH...) |
| 387 |
*/ |
| 388 |
case OP_EXIT_FRAME: string = "EXIT\tFRAME"; |
| 389 |
goto NOARG; |
| 390 |
/* OP_EXIT_TAGBODY |
| 391 |
Marks the end of a high level construct (TAGBODY) |
| 392 |
*/ |
| 393 |
case OP_EXIT_TAGBODY: print_noarg("EXIT\tTAGBODY"); |
| 394 |
return vector; |
| 395 |
|
| 396 |
case OP_FLET: vector = disassemble_flet(bytecodes, vector); |
| 397 |
break; |
| 398 |
case OP_LABELS: vector = disassemble_labels(bytecodes, vector); |
| 399 |
break; |
| 400 |
|
| 401 |
/* OP_LFUNCTION index{fixnum} |
| 402 |
Extracts nth function defined in the local environment. |
| 403 |
*/ |
| 404 |
case OP_LFUNCTION: string = "LOCFUNC\t"; |
| 405 |
GET_OPARG(n, vector); |
| 406 |
goto OPARG; |
| 407 |
|
| 408 |
/* OP_CFUNCTION index{fixnum} |
| 409 |
Extracts nth function defined in the lexical environment. |
| 410 |
*/ |
| 411 |
case OP_CFUNCTION: string = "LEXFUNC\t"; |
| 412 |
GET_OPARG(n, vector); |
| 413 |
goto OPARG; |
| 414 |
|
| 415 |
/* OP_FUNCTION name{symbol} |
| 416 |
Extracts the function associated to a symbol. The function |
| 417 |
is defined in the global environment. |
| 418 |
*/ |
| 419 |
case OP_FUNCTION: string = "SYMFUNC\t"; |
| 420 |
GET_DATA(o, vector, data); |
| 421 |
goto ARG; |
| 422 |
|
| 423 |
/* OP_CLOSE name{arg} |
| 424 |
Extracts the function associated to a symbol. The function |
| 425 |
may be defined in the global environment or in the local |
| 426 |
environment. This last value takes precedence. |
| 427 |
*/ |
| 428 |
case OP_CLOSE: string = "CLOSE\t"; |
| 429 |
GET_DATA(o, vector, data); |
| 430 |
goto ARG; |
| 431 |
|
| 432 |
/* OP_GO n{arg}, tag-ndx{arg} |
| 433 |
OP_GO_CFB n{lex}, tag-ndx{arg} |
| 434 |
|
| 435 |
Jumps to the tag which is defined for the tagbody |
| 436 |
frame registered at the n-th position in the lexical |
| 437 |
environment. TAG-NDX is the number of tag in the list. |
| 438 |
*/ |
| 439 |
case OP_GO: string = "GO\t"; |
| 440 |
GET_OPARG(n, vector); |
| 441 |
GET_OPARG(m, vector); |
| 442 |
o = ecl_make_fixnum(m); |
| 443 |
goto OPARG_ARG; |
| 444 |
|
| 445 |
case OP_GO_CFB: string = "GO_CFB\t"; |
| 446 |
GET_OPARG(n, vector); |
| 447 |
GET_OPARG(m, vector); |
| 448 |
o = ecl_make_fixnum(m); |
| 449 |
goto OPARG_ARG; |
| 450 |
|
| 451 |
/* OP_RETURN n{arg} |
| 452 |
OP_RETURN_CFB n{lex} |
| 453 |
|
| 454 |
Returns from the block whose record in the lexical environment |
| 455 |
occuppies the n-th position. |
| 456 |
*/ |
| 457 |
case OP_RETURN: string = "RETFROM\t"; |
| 458 |
GET_OPARG(n, vector); |
| 459 |
goto OPARG; |
| 460 |
|
| 461 |
case OP_RETURN_CFB: string = "RETFROM_CFB\t"; |
| 462 |
GET_OPARG(n, vector); |
| 463 |
goto OPARG; |
| 464 |
|
| 465 |
/* OP_THROW |
| 466 |
Jumps to an enclosing CATCH form whose tag matches the one |
| 467 |
of the THROW. The tag is taken from the stack, while the |
| 468 |
output values are left in VALUES(...). |
| 469 |
*/ |
| 470 |
case OP_THROW: string = "THROW"; |
| 471 |
goto NOARG; |
| 472 |
|
| 473 |
/* OP_JMP label{arg} |
| 474 |
OP_JNIL label{arg} |
| 475 |
OP_JT label{arg} |
| 476 |
OP_JEQ label{arg}, value{object} |
| 477 |
OP_JNEQ label{arg}, value{object} |
| 478 |
Direct or conditional jumps. The conditional jumps are made |
| 479 |
comparing with the value of VALUES(0). |
| 480 |
*/ |
| 481 |
case OP_JMP: string = "JMP\t"; |
| 482 |
goto JMP; |
| 483 |
case OP_JNIL: string = "JNIL\t"; |
| 484 |
goto JMP; |
| 485 |
case OP_JT: string = "JT\t"; |
| 486 |
JMP: { GET_OPARG(m, vector); |
| 487 |
n = vector + m - OPARG_SIZE - base; |
| 488 |
goto OPARG; |
| 489 |
} |
| 490 |
case OP_JEQL: string = "JEQL\t"; |
| 491 |
goto JEQL; |
| 492 |
case OP_JNEQL: string = "JNEQL\t"; |
| 493 |
JEQL: { GET_DATA(o, vector, data); |
| 494 |
GET_OPARG(m, vector); |
| 495 |
n = vector + m - OPARG_SIZE - base; |
| 496 |
goto OPARG_ARG; |
| 497 |
} |
| 498 |
case OP_NOT: string = "NOT"; |
| 499 |
goto NOARG; |
| 500 |
|
| 501 |
/* OP_UNBIND n{arg} |
| 502 |
Undo "n" bindings of lexical variables. |
| 503 |
*/ |
| 504 |
case OP_UNBIND: string = "UNBIND\t"; |
| 505 |
GET_OPARG(n, vector); |
| 506 |
goto OPARG; |
| 507 |
/* OP_UNBINDS n{arg} |
| 508 |
Undo "n" bindings of special variables. |
| 509 |
*/ |
| 510 |
case OP_UNBINDS: string = "UNBINDS\t"; |
| 511 |
GET_OPARG(n, vector); |
| 512 |
goto OPARG; |
| 513 |
/* OP_BIND name{symbol} |
| 514 |
OP_PBIND name{symbol} |
| 515 |
OP_BINDS name{symbol} |
| 516 |
OP_PBINDS name{symbol} |
| 517 |
Binds a lexical or special variable to the either the |
| 518 |
value of VALUES(0), to the first value of the stack, or |
| 519 |
to the n-th value of VALUES(...). |
| 520 |
*/ |
| 521 |
case OP_BIND: string = "BIND\t"; |
| 522 |
GET_DATA(o, vector, data); |
| 523 |
goto ARG; |
| 524 |
case OP_PBIND: string = "PBIND\t"; |
| 525 |
GET_DATA(o, vector, data); |
| 526 |
goto ARG; |
| 527 |
case OP_VBIND: string = "VBIND\t"; |
| 528 |
GET_OPARG(n, vector); |
| 529 |
GET_DATA(o, vector, data); |
| 530 |
goto OPARG_ARG; |
| 531 |
case OP_BINDS: string = "BINDS\t"; |
| 532 |
GET_DATA(o, vector, data); |
| 533 |
goto ARG; |
| 534 |
case OP_PBINDS: string = "PBINDS\t"; |
| 535 |
GET_DATA(o, vector, data); |
| 536 |
goto ARG; |
| 537 |
case OP_VBINDS: string = "VBINDS\t"; |
| 538 |
GET_OPARG(n, vector); |
| 539 |
GET_DATA(o, vector, data); |
| 540 |
goto OPARG_ARG; |
| 541 |
/* OP_SETQ n{arg} |
| 542 |
OP_SETQC n{arg} |
| 543 |
OP_SETQS n{arg} |
| 544 |
|
| 545 |
OP_PSETQ n{arg} |
| 546 |
OP_PSETQC n{arg} |
| 547 |
OP_PSETQS n{arg} |
| 548 |
|
| 549 |
OP_VSETQ n{arg}, nvalue{arg} |
| 550 |
OP_VSETQC n{arg}, nvalue{arg} |
| 551 |
OP_VSETQS n{arg}, nvalue{arg} |
| 552 |
|
| 553 |
Sets either the n-th variable to either the value in REG0 (OP_SETQ[CS]) |
| 554 |
or to the first value on the stack (OP_PSETQ[CS]), or to a given value |
| 555 |
from the multiple values array (OP_VSETQ[CS]). Note NVALUE > 0 strictly. |
| 556 |
*/ |
| 557 |
case OP_SETQ: string = "SETQ\t"; |
| 558 |
GET_OPARG(n, vector); |
| 559 |
goto OPARG; |
| 560 |
case OP_SETQC: string = "SETQC\t"; |
| 561 |
GET_OPARG(n, vector); |
| 562 |
goto OPARG; |
| 563 |
case OP_SETQS: string = "SETQS\t"; |
| 564 |
GET_DATA(o, vector, data); |
| 565 |
goto ARG; |
| 566 |
|
| 567 |
case OP_PSETQ: string = "PSETQ\t"; |
| 568 |
GET_OPARG(n, vector); |
| 569 |
goto OPARG; |
| 570 |
case OP_PSETQC: string = "PSETQC\t"; |
| 571 |
GET_DATA(o, vector, data); |
| 572 |
goto ARG; |
| 573 |
case OP_PSETQS: string = "PSETQS\t"; |
| 574 |
GET_DATA(o, vector, data); |
| 575 |
goto ARG; |
| 576 |
|
| 577 |
case OP_VSETQ: string = "VSETQ\t"; |
| 578 |
GET_OPARG(m, vector); |
| 579 |
o = ecl_make_fixnum(m); |
| 580 |
GET_OPARG(n, vector); |
| 581 |
goto OPARG_ARG; |
| 582 |
case OP_VSETQC: string = "VSETQC\t"; |
| 583 |
GET_OPARG(m, vector); |
| 584 |
o = ecl_make_fixnum(m); |
| 585 |
GET_OPARG(n, vector); |
| 586 |
goto OPARG_ARG; |
| 587 |
case OP_VSETQS: string = "VSETQS\t"; |
| 588 |
GET_DATA(o, vector, data); |
| 589 |
GET_OPARG(n, vector); |
| 590 |
goto OPARG_ARG; |
| 591 |
|
| 592 |
case OP_PROGV: vector = disassemble_progv(bytecodes, vector); |
| 593 |
break; |
| 594 |
case OP_EXIT_PROGV: print_noarg("PROGV\tEXIT"); |
| 595 |
return vector; |
| 596 |
|
| 597 |
/* OP_VALUES n{arg} |
| 598 |
Pop N values from the stack and store them in VALUES(...) |
| 599 |
*/ |
| 600 |
case OP_VALUES: string = "VALUES\t"; |
| 601 |
GET_OPARG(n, vector); |
| 602 |
goto OPARG; |
| 603 |
/* OP_NTHVAL |
| 604 |
Set VALUES(0) to the N-th value of the VALUES(...) list. |
| 605 |
The index N-th is extracted from the top of the stack. |
| 606 |
*/ |
| 607 |
case OP_NTHVAL: string = "NTHVAL\t"; |
| 608 |
goto NOARG; |
| 609 |
case OP_TAGBODY: vector = disassemble_tagbody(bytecodes, vector); |
| 610 |
break; |
| 611 |
case OP_PROTECT: string = "PROTECT\t"; |
| 612 |
goto JMP; |
| 613 |
case OP_PROTECT_NORMAL: string = "PROTECT\tNORMAL"; |
| 614 |
goto NOARG; |
| 615 |
case OP_PROTECT_EXIT: string = "PROTECT\tEXIT"; |
| 616 |
goto NOARG; |
| 617 |
case OP_NIL: string = "QUOTE\tNIL"; |
| 618 |
goto NOARG; |
| 619 |
case OP_PUSHNIL: string = "PUSH\t'NIL"; |
| 620 |
goto NOARG; |
| 621 |
case OP_STEPIN: string = "STEP\tIN,"; |
| 622 |
GET_DATA(o, vector, data); |
| 623 |
goto ARG; |
| 624 |
case OP_STEPOUT: string = "STEP\tOUT"; |
| 625 |
goto NOARG; |
| 626 |
|
| 627 |
case OP_CONS: string = "CONS"; goto NOARG; |
| 628 |
case OP_ENDP: string = "ENDP\tREG0"; goto NOARG; |
| 629 |
case OP_CAR: string = "CAR\tREG0"; goto NOARG; |
| 630 |
case OP_CDR: string = "CDR\tREG0"; goto NOARG; |
| 631 |
case OP_LIST: string = "LIST\t"; |
| 632 |
GET_OPARG(n, vector); |
| 633 |
goto OPARG; |
| 634 |
case OP_LISTA: string = "LIST*\t"; |
| 635 |
GET_OPARG(n, vector); |
| 636 |
goto OPARG; |
| 637 |
case OP_CONS_CAR: string = "CONS-CAR\tREG0"; goto NOARG; |
| 638 |
case OP_CONS_CDR: string = "CONS-CDR\tREG0"; goto NOARG; |
| 639 |
|
| 640 |
case OP_CALLG1: string = "CALLG1\t"; |
| 641 |
GET_DATA(o, vector, data); |
| 642 |
goto ARG; |
| 643 |
case OP_CALLG2: string = "CALLG2\t"; |
| 644 |
GET_DATA(o, vector, data); |
| 645 |
goto ARG; |
| 646 |
|
| 647 |
default: |
| 648 |
FEerror("Unknown code ~S", 1, ecl_make_fixnum(*(vector-1))); |
| 649 |
return vector; |
| 650 |
NOARG: print_noarg(string); |
| 651 |
break; |
| 652 |
ARG: print_noarg(string); |
| 653 |
@prin1(1, o); |
| 654 |
break; |
| 655 |
OPARG: print_oparg(string, n); |
| 656 |
break; |
| 657 |
OPARG_ARG: print_oparg_arg(string, n, o); |
| 658 |
break; |
| 659 |
} |
| 660 |
goto BEGIN; |
| 661 |
} |
| 662 |
|
| 663 |
cl_object |
| 664 |
si_bc_disassemble(cl_object v) |
| 665 |
{ |
| 666 |
if (ecl_t_of(v) == t_bclosure) { |
| 667 |
v = v->bclosure.code; |
| 668 |
} |
| 669 |
if (ecl_t_of(v) == t_bytecodes) { |
| 670 |
disassemble_lambda(v); |
| 671 |
@(return v); |
| 672 |
} |
| 673 |
@(return ECL_NIL); |
| 674 |
} |
| 675 |
|
| 676 |
cl_object |
| 677 |
si_bc_split(cl_object b) |
| 678 |
{ |
| 679 |
cl_object vector, data, name, lex = ECL_NIL; |
| 680 |
|
| 681 |
if (ecl_t_of(b) == t_bclosure) { |
| 682 |
b = b->bclosure.code; |
| 683 |
lex = b->bclosure.lex; |
| 684 |
} |
| 685 |
if (ecl_t_of(b) != t_bytecodes) { |
| 686 |
vector = ECL_NIL; |
| 687 |
data = ECL_NIL; |
| 688 |
name = ECL_NIL; |
| 689 |
} else { |
| 690 |
vector = ecl_alloc_simple_vector(b->bytecodes.code_size * |
| 691 |
sizeof(cl_opcode), ecl_aet_b8); |
| 692 |
vector->vector.self.b8 = (uint8_t*)b->bytecodes.code; |
| 693 |
data = cl_copy_seq(b->bytecodes.data); |
| 694 |
name = b->bytecodes.name; |
| 695 |
} |
| 696 |
@(return lex vector data name); |
| 697 |
} |
| 698 |
|
| 699 |
cl_object |
| 700 |
si_bc_join(cl_object lex, cl_object code, cl_object data, cl_object name) |
| 701 |
{ |
| 702 |
cl_object output; |
| 703 |
if (lex != ECL_NIL) { |
| 704 |
output = ecl_alloc_object(t_bclosure); |
| 705 |
output->bclosure.code = si_bc_join(ECL_NIL, code, data, name); |
| 706 |
output->bclosure.lex = lex; |
| 707 |
output->bclosure.entry = _ecl_bclosure_dispatch_vararg; |
| 708 |
} else { |
| 709 |
/* Ensure minimal sanity of data */ |
| 710 |
unlikely_if (!ECL_VECTORP(code) || |
| 711 |
(code->vector.elttype != ecl_aet_b8)) { |
| 712 |
FEwrong_type_nth_arg(@[si::bc-join], |
| 713 |
0, code, |
| 714 |
cl_list(2, |
| 715 |
@'simple-array', |
| 716 |
@'ext::byte8')); |
| 717 |
} |
| 718 |
unlikely_if (!ECL_VECTORP(code) || |
| 719 |
(data->vector.elttype != ecl_aet_object)) { |
| 720 |
FEwrong_type_nth_arg(@[si::bc-join], |
| 721 |
0, code, |
| 722 |
cl_list(2, |
| 723 |
@'simple-array', |
| 724 |
ECL_T)); |
| 725 |
} |
| 726 |
/* Duplicate the vectors and steal their data pointers */ |
| 727 |
code = cl_copy_seq(code); |
| 728 |
data = cl_copy_seq(data); |
| 729 |
output = ecl_alloc_object(t_bytecodes); |
| 730 |
output->bytecodes.name = ECL_NIL; |
| 731 |
output->bytecodes.definition = ECL_NIL; |
| 732 |
output->bytecodes.entry = _ecl_bytecodes_dispatch_vararg; |
| 733 |
output->bytecodes.code_size = code->vector.fillp / sizeof(cl_opcode); |
| 734 |
output->bytecodes.code = (void*)code->vector.self.b8; |
| 735 |
output->bytecodes.data = data; |
| 736 |
output->bytecodes.file = ECL_NIL; |
| 737 |
output->bytecodes.file_position = ECL_NIL; |
| 738 |
} |
| 739 |
@(return output); |
| 740 |
} |