📄 asm.js
字号:
/*
* JavaScript Assembler.
* Copyright (c) 1998 New Generation Software (NGS) Oy
*
* Author: Markku Rossi <mtr@ngs.fi>
*/
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA
*/
/*
* $Source: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/kjs/jsc/asm.js,v $
* $Id: asm.js 21681 2006-04-21 15:00:24Z peterw $
*/
/* Byte-code file definitions. */
JSC$BC_MAGIC = 0xc0014a53;
JSC$BC_SECT_CODE = 0;
JSC$BC_SECT_CONSTANTS = 1;
JSC$BC_SECT_SYMTAB = 2;
JSC$BC_SECT_DEBUG = 3;
JSC$CONST_INT = 3;
JSC$CONST_STRING = 4;
JSC$CONST_FLOAT = 5;
JSC$CONST_SYMBOL = 10;
JSC$CONST_REGEXP = 11;
JSC$CONST_NAN = 13;
JSC$CONST_REGEXP_FLAG_G = 0x01;
JSC$CONST_REGEXP_FLAG_I = 0x02;
JSC$DEBUG_FILENAME = 1;
JSC$DEBUG_LINENUMBER = 2;
/* Opcode definitions. */
JSC$OP_HALT = 0;
JSC$OP_DONE = 1;
JSC$OP_NOP = 2;
JSC$OP_DUP = 3;
JSC$OP_POP = 4;
JSC$OP_POP_N = 5;
JSC$OP_APOP = 6;
JSC$OP_SWAP = 7;
JSC$OP_ROLL = 8;
JSC$OP_CONST = 9;
JSC$OP_CONST_NULL = 10;
JSC$OP_CONST_TRUE = 11;
JSC$OP_CONST_FALSE = 12;
JSC$OP_CONST_UNDEFINED = 13;
JSC$OP_CONST_I0 = 14;
JSC$OP_CONST_I1 = 15;
JSC$OP_CONST_I2 = 16;
JSC$OP_CONST_I3 = 17;
JSC$OP_CONST_I = 18;
JSC$OP_LOAD_GLOBAL = 19;
JSC$OP_STORE_GLOBAL = 20;
JSC$OP_LOAD_ARG = 21;
JSC$OP_STORE_ARG = 22;
JSC$OP_LOAD_LOCAL = 23;
JSC$OP_STORE_LOCAL = 24;
JSC$OP_LOAD_PROPERTY = 25;
JSC$OP_STORE_PROPERTY = 26;
JSC$OP_LOAD_ARRAY = 27;
JSC$OP_STORE_ARRAY = 28;
JSC$OP_NTH = 29;
JSC$OP_CMP_EQ = 30;
JSC$OP_CMP_NE = 31;
JSC$OP_CMP_LT = 32;
JSC$OP_CMP_GT = 33;
JSC$OP_CMP_LE = 34;
JSC$OP_CMP_GE = 35;
JSC$OP_CMP_SEQ = 36;
JSC$OP_CMP_SNE = 37;
JSC$OP_SUB = 38;
JSC$OP_ADD = 39;
JSC$OP_MUL = 40;
JSC$OP_DIV = 41;
JSC$OP_MOD = 42;
JSC$OP_NEG = 43;
JSC$OP_AND = 44;
JSC$OP_NOT = 45;
JSC$OP_OR = 46;
JSC$OP_XOR = 47;
JSC$OP_SHIFT_LEFT = 48;
JSC$OP_SHIFT_RIGHT = 49;
JSC$OP_SHIFT_RRIGHT = 50;
JSC$OP_IFFALSE = 51;
JSC$OP_IFTRUE = 52;
JSC$OP_CALL_METHOD = 53;
JSC$OP_JMP = 54;
JSC$OP_JSR = 55;
JSC$OP_RETURN = 56;
JSC$OP_TYPEOF = 57;
JSC$OP_NEW = 58;
JSC$OP_DELETE_PROPERTY = 59;
JSC$OP_DELETE_ARRAY = 60;
JSC$OP_LOCALS = 61;
JSC$OP_MIN_ARGS = 62;
JSC$OP_LOAD_NTH_ARG = 63;
JSC$OP_WITH_PUSH = 64;
JSC$OP_WITH_POP = 65;
JSC$OP_TRY_PUSH = 66;
JSC$OP_TRY_POP = 67;
JSC$OP_THROW = 68;
/* Type aware operands. */
JSC$OP_IFFALSE_B = 69;
JSC$OP_IFTRUE_B = 70;
JSC$OP_ADD_1_I = 71;
JSC$OP_ADD_2_I = 72;
JSC$OP_LOAD_GLOBAL_W = 73;
JSC$OP_JSR_W = 74;
/* Internal values. */
JSC$ASM_SYMBOL = 1000;
JSC$ASM_LABEL = 1001;
/*
* General helpers.
*/
/* Generate byte-code for operands with Int8 value. */
function JSC$ASM_bytecode_int8 ()
{
return String.pack ("C", this.value);
}
/* Generate byte-code for operands with Int16 value. */
function JSC$ASM_bytecode_int16 ()
{
return String.pack ("n", this.value);
}
/* Generate byte-code for operands with Int32 value. */
function JSC$ASM_bytecode_int32 ()
{
return String.pack ("N", this.value);
}
/* Generate byte-code for operands with Symbol value. */
function JSC$ASM_bytecode_symbol ()
{
var cid = JSC$asm_genconstant (String.pack ("C", JSC$CONST_SYMBOL)
+ this.value + String.pack ("C", 0));
return String.pack ("N", cid);
}
/* Generate byte-code for local jump operands. */
function JSC$ASM_bytecode_local_jump ()
{
var delta = this.value.offset - (this.offset + this.size);
return String.pack ("N", delta);
}
/*
* Assembler operands.
*/
/* Symbol. */
function JSC$ASM_symbol (ln, value)
{
this.type = JSC$ASM_SYMBOL;
this.linenum = ln;
this.value = value;
this.size = 0;
this.print = JSC$ASM_symbol_print;
this.link = JSC$asm_link;
}
function JSC$ASM_symbol_print (stream)
{
stream.write ("\n" + this.value + ":\n");
}
/* Label */
function JSC$ASM_label ()
{
this.type = JSC$ASM_LABEL;
this.linenum = 0;
this.size = 0;
this.value = JSC$asm_label_count++;
this.referenced = false;
this.next = null;
this.print = JSC$ASM_label_print;
this.link = JSC$asm_link;
this.format = JSC$ASM_label_format;
}
function JSC$ASM_label_print (stream)
{
stream.write (this.format () + ":\n");
}
function JSC$ASM_label_format ()
{
return ".L" + this.value.toString ();
}
/* halt */
function JSC$ASM_halt (ln)
{
this.type = JSC$OP_HALT;
this.linenum = ln;
this.size = 1;
this.print = JSC$ASM_halt_print;
this.link = JSC$asm_link;
}
function JSC$ASM_halt_print (stream)
{
stream.write ("\thalt\n");
}
/* done */
function JSC$ASM_done (ln)
{
this.type = JSC$OP_DONE;
this.linenum = ln;
this.size = 1;
this.print = JSC$ASM_done_print;
this.link = JSC$asm_link;
}
function JSC$ASM_done_print (stream)
{
stream.write ("\tdone\n");
}
/* nop */
function JSC$ASM_nop (ln)
{
this.type = JSC$OP_NOP;
this.linenum = ln;
this.size = 1;
this.print = JSC$ASM_nop_print;
this.link = JSC$asm_link;
}
function JSC$ASM_nop_print (stream)
{
stream.write ("\tnop\n");
}
/* dup */
function JSC$ASM_dup (ln)
{
this.type = JSC$OP_DUP;
this.linenum = ln;
this.stack_delta = 1;
this.size = 1;
this.print = JSC$ASM_dup_print;
this.link = JSC$asm_link;
}
function JSC$ASM_dup_print (stream)
{
stream.write ("\tdup\n");
}
/* pop */
function JSC$ASM_pop (ln)
{
this.type = JSC$OP_POP;
this.linenum = ln;
this.stack_delta = -1;
this.size = 1;
this.print = JSC$ASM_pop_print;
this.link = JSC$asm_link;
}
function JSC$ASM_pop_print (stream)
{
stream.write ("\tpop\n");
}
/* pop_n */
function JSC$ASM_pop_n (ln, value)
{
this.type = JSC$OP_POP_N;
this.linenum = ln;
this.value = value;
this.stack_delta = -value;
this.size = 2;
this.print = JSC$ASM_pop_n_print;
this.link = JSC$asm_link;
this.bytecode = JSC$ASM_bytecode_int8;
}
function JSC$ASM_pop_n_print (stream)
{
stream.write ("\tpop_n\t\t" + this.value.toString () + "\n");
}
/* apop */
function JSC$ASM_apop (ln, value)
{
this.type = JSC$OP_APOP;
this.linenum = ln;
this.value = value;
this.stack_delta = -value;
this.size = 2;
this.print = JSC$ASM_apop_print;
this.link = JSC$asm_link;
this.bytecode = JSC$ASM_bytecode_int8;
}
function JSC$ASM_apop_print (stream)
{
stream.write ("\tapop\t\t" + this.value.toString () + "\n");
}
/* swap */
function JSC$ASM_swap (ln)
{
this.type = JSC$OP_SWAP;
this.linenum = ln;
this.size = 1;
this.print = JSC$ASM_swap_print;
this.link = JSC$asm_link;
}
function JSC$ASM_swap_print (stream)
{
stream.write ("\tswap\n");
}
/* roll */
function JSC$ASM_roll (ln, value)
{
this.type = JSC$OP_ROLL;
this.linenum = ln;
this.value = value;
this.size = 2;
this.print = JSC$ASM_roll_print;
this.link = JSC$asm_link;
this.bytecode = JSC$ASM_bytecode_int8;
}
function JSC$ASM_roll_print (stream)
{
stream.write ("\troll\t\t" + this.value.toString () + "\n");
}
/* const */
function JSC$ASM_const (ln, value)
{
this.type = JSC$OP_CONST;
this.linenum = ln;
this.value = value;
this.stack_delta = 1;
this.size = 5;
this.print = JSC$ASM_const_print;
this.link = JSC$asm_link;
this.bytecode = JSC$ASM_const_bytecode;
}
function JSC$ASM_const_print (stream)
{
if (typeof this.value == "number")
stream.write ("\tconst\t\t" + this.value.toString () + "\n");
else if (typeof this.value == "string"
|| typeof this.value == "#builtin")
{
var i, c;
var ender, src;
var stringp = (typeof this.value == "string");
if (stringp)
{
ender = "\"";
src = this.value;
}
else
{
ender = "/";
src = this.value.source;
}
stream.write ("\tconst\t\t" + ender);
for (i = 0; i < src.length; i++)
{
c = src.charCodeAt (i);
if (c == ender[0] || c == #'\\')
stream.write ("\\" + src.charAt (i));
else if (c == #'\n')
stream.write ("\\n");
else if (c == #'\r')
stream.write ("\\r");
else if (c == #'\t')
stream.write ("\\t");
else if (c == #'\f')
stream.write ("\\f");
else
stream.write (src.charAt (i));
}
stream.write (ender);
if (!stringp)
{
if (this.value.global)
stream.write ("g");
if (this.value.ignoreCase)
stream.write ("i");
}
stream.write ("\n");
}
}
function JSC$ASM_const_bytecode ()
{
var cid;
if (typeof this.value == "number")
{
if (isInt (this.value))
cid = JSC$asm_genconstant (String.pack ("CN", JSC$CONST_INT,
this.value));
else if (isFloat (this.value))
cid = JSC$asm_genconstant (String.pack ("Cd", JSC$CONST_FLOAT,
this.value));
else
cid = JSC$asm_genconstant (String.pack ("C", JSC$CONST_NAN));
}
else if (typeof this.value == "string")
cid = JSC$asm_genconstant (String.pack ("CN", JSC$CONST_STRING,
this.value.length)
+ this.value);
else if (typeof this.value == "#builtin")
{
/* Regular expression. */
var flags = 0;
if (this.value.global)
flags |= JSC$CONST_REGEXP_FLAG_G;
if (this.value.ignoreCase)
flags |= JSC$CONST_REGEXP_FLAG_I;
cid = JSC$asm_genconstant (String.pack ("CCN", JSC$CONST_REGEXP, flags,
this.value.source.length)
+ this.value.source);
}
else
error ("ASM_const_bytecode(): unknown type: " + typeof this.value);
return String.pack ("N", cid);
}
/* const_null */
function JSC$ASM_const_null (ln)
{
this.type = JSC$OP_CONST_NULL;
this.linenum = ln;
this.stack_delta = 1;
this.size = 1;
this.print = JSC$ASM_const_null_print;
this.link = JSC$asm_link;
}
function JSC$ASM_const_null_print (stream)
{
stream.write ("\tconst_null\n");
}
/* const_true */
function JSC$ASM_const_true (ln)
{
this.type = JSC$OP_CONST_TRUE;
this.linenum = ln;
this.stack_delta = 1;
this.size = 1;
this.print = JSC$ASM_const_true_print;
this.link = JSC$asm_link;
}
function JSC$ASM_const_true_print (stream)
{
stream.write ("\tconst_true\n");
}
/* const_false */
function JSC$ASM_const_false (ln)
{
this.type = JSC$OP_CONST_FALSE;
this.linenum = ln;
this.stack_delta = 1;
this.size = 1;
this.print = JSC$ASM_const_false_print;
this.link = JSC$asm_link;
}
function JSC$ASM_const_false_print (stream)
{
stream.write ("\tconst_false\n");
}
/* const_undefined */
function JSC$ASM_const_undefined (ln)
{
this.type = JSC$OP_CONST_UNDEFINED;
this.linenum = ln;
this.stack_delta = 1;
this.size = 1;
this.print = JSC$ASM_const_undefined_print;
this.link = JSC$asm_link;
}
function JSC$ASM_const_undefined_print (stream)
{
stream.write ("\tconst_undefined\n");
}
/* const_i0 */
function JSC$ASM_const_i0 (ln)
{
this.type = JSC$OP_CONST_I0;
this.linenum = ln;
this.stack_delta = 1;
this.size = 1;
this.print = JSC$ASM_const_i0_print;
this.link = JSC$asm_link;
}
function JSC$ASM_const_i0_print (stream)
{
stream.write ("\tconst_i0\n");
}
/* const_i1 */
function JSC$ASM_const_i1 (ln)
{
this.type = JSC$OP_CONST_I1;
this.linenum = ln;
this.stack_delta = 1;
this.size = 1;
this.print = JSC$ASM_const_i1_print;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -