home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / js / src / jsopcode.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.5 KB  |  183 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef jsopcode_h___
  20. #define jsopcode_h___
  21. /*
  22.  * JS bytecode definitions.
  23.  */
  24. #include <stddef.h>
  25. #include "jsprvtd.h"
  26. #include "jspubtd.h"
  27.  
  28. PR_BEGIN_EXTERN_C
  29.  
  30. /*
  31.  * JS operation bytecodes.
  32.  */
  33. typedef enum JSOp {
  34. #define OPDEF(op,val,name,token,length,nuses,ndefs,prec,format) \
  35.     op = val,
  36. #include "jsopcode.def"
  37. #undef OPDEF
  38.     JSOP_LIMIT
  39. } JSOp;
  40.  
  41. /*
  42.  * JS bytecode formats.
  43.  */
  44. #define JOF_BYTE          0       /* single bytecode, no immediates */
  45. #define JOF_JUMP          1       /* signed 16-bit jump offset immediate */
  46. #define JOF_CONST         2       /* unsigned 16-bit constant pool index */
  47. #define JOF_UINT16        3       /* unsigned 16-bit immediate operand */
  48. #define JOF_TABLESWITCH   4       /* table switch */
  49. #define JOF_LOOKUPSWITCH  5       /* lookup switch */
  50. #define JOF_QARG          6       /* quickened get/set function argument ops */
  51. #define JOF_QVAR          7       /* quickened get/set local variable ops */
  52. #define JOF_TYPEMASK      0x000f  /* mask for above immediate types */
  53. #define JOF_NAME          0x0010  /* name operation */
  54. #define JOF_PROP          0x0020  /* obj.prop operation */
  55. #define JOF_ELEM          0x0030  /* obj[index] operation */
  56. #define JOF_MODEMASK      0x0030  /* mask for above addressing modes */
  57. #define JOF_SET           0x0040  /* set (i.e., assignment) operation */
  58. #define JOF_DEL           0x0080  /* delete operation */
  59. #define JOF_DEC           0x0100  /* decrement (--, not ++) opcode */
  60. #define JOF_INC           0x0200  /* increment (++, not --) opcode */
  61. #define JOF_INCDEC        0x0300  /* increment or decrement opcode */
  62. #define JOF_POST          0x0400  /* postorder increment or decrement */
  63. #define JOF_IMPORT        0x0800  /* import property op */
  64.  
  65. /*
  66.  * Immediate operand getters, setters, and bounds.
  67.  */
  68. #define JUMP_OFFSET_HI(off)     ((jsbytecode)((off) >> 8))
  69. #define JUMP_OFFSET_LO(off)     ((jsbytecode)(off))
  70. #define GET_JUMP_OFFSET(pc)     ((int16)(((pc)[1] << 8) | (pc)[2]))
  71. #define SET_JUMP_OFFSET(pc,off) ((pc)[1] = JUMP_OFFSET_HI(off),               \
  72.                  (pc)[2] = JUMP_OFFSET_LO(off))
  73. #define JUMP_OFFSET_MIN         ((int16)0x8000)
  74. #define JUMP_OFFSET_MAX         ((int16)0x7fff)
  75.  
  76. #define ATOM_INDEX_HI(index)    ((jsbytecode)((index) >> 8))
  77. #define ATOM_INDEX_LO(index)    ((jsbytecode)(index))
  78. #define GET_ATOM_INDEX(pc)      (((pc)[1] << 8) | (pc)[2])
  79. #define SET_ATOM_INDEX(pc,ndx)  ((pc)[1] = ATOM_INDEX_HI(ndx),                \
  80.                  (pc)[2] = ATOM_INDEX_LO(ndx))
  81. #define GET_ATOM(cx,script,pc)  js_GetAtom((cx), &(script)->atomMap,          \
  82.                        GET_ATOM_INDEX(pc))
  83. #define ATOM_INDEX_LIMIT_LOG2   16
  84. #define ATOM_INDEX_LIMIT        ((uint32)1 << ATOM_INDEX_LIMIT_LOG2)
  85.  
  86. #define ARGC_HI(argc)           ((jsbytecode)((argc) >> 8))
  87. #define ARGC_LO(argc)           ((jsbytecode)(argc))
  88. #define GET_ARGC(pc)            (((pc)[1] << 8) | (pc)[2])
  89. #define ARGC_LIMIT              ((uint32)1 << 16)
  90.  
  91. /* Synonyms for quick JOF_QARG and JOF_QVAR bytecodes. */
  92. #define GET_ARGNO(pc)           GET_ARGC(pc)
  93. #define SET_ARGNO(pc,argno)     SET_JUMP_OFFSET(pc,argno)
  94. #define GET_VARNO(pc)           GET_ARGC(pc)
  95. #define SET_VARNO(pc,varno)     SET_JUMP_OFFSET(pc,varno)
  96.  
  97. struct JSCodeSpec {
  98.     const char          *name;          /* JS bytecode name */
  99.     const char          *token;         /* JS source literal or null */
  100.     int8                length;         /* length including opcode byte */
  101.     int8                nuses;          /* arity, -1 if variadic */
  102.     int8                ndefs;          /* number of stack results */
  103.     uint8               prec;           /* operator precedence */
  104.     uint32              format;         /* immediate operand format */
  105. };
  106.  
  107. extern char             js_new_str[];
  108. extern char             js_delete_str[];
  109. extern char             js_typeof_str[];
  110. extern char             js_void_str[];
  111. extern char             js_null_str[];
  112. extern char             js_this_str[];
  113. extern char             js_false_str[];
  114. extern char             js_true_str[];
  115. extern JSCodeSpec       js_CodeSpec[];
  116. extern uintN            js_NumCodeSpecs;
  117. extern jschar           js_EscapeMap[];
  118.  
  119. /*
  120.  * Return a GC'ed string containing the chars in str, with any non-printing
  121.  * chars or quotes (' or " as specified by the quote argument) escaped.
  122.  */
  123. extern JSString *
  124. js_EscapeString(JSContext *cx, JSString *str, jschar quote);
  125.  
  126. /*
  127.  * JSPrinter operations, for printf style message formatting.  The return
  128.  * value from js_GetPrinterOutput() is the printer's cumulative output, in
  129.  * a GC'ed string.
  130.  */
  131. extern JSPrinter *
  132. js_NewPrinter(JSContext *cx, const char *name, uintN indent);
  133.  
  134. extern void
  135. js_DestroyPrinter(JSPrinter *jp);
  136.  
  137. extern JSString *
  138. js_GetPrinterOutput(JSPrinter *jp);
  139.  
  140. extern int
  141. js_printf(JSPrinter *jp, char *format, ...);
  142.  
  143. extern JSBool
  144. js_puts(JSPrinter *jp, char *s);
  145.  
  146. #ifdef DEBUG
  147. /*
  148.  * Disassemblers, for debugging only.
  149.  */
  150. #include <stdio.h>
  151.  
  152. extern void
  153. js_Disassemble(JSContext *cx, JSScript *script, JSBool lines, FILE *fp);
  154.  
  155. extern uintN
  156. js_Disassemble1(JSContext *cx, JSScript *script, jsbytecode *pc, uintN loc,
  157.         JSBool lines, FILE *fp);
  158. #endif /* DEBUG */
  159.  
  160. /*
  161.  * Decompilers, for script, function, and expression pretty-printing.
  162.  */
  163. extern JSBool
  164. js_DecompileCode(JSPrinter *jp, JSScript *script, jsbytecode *pc, uintN len);
  165.  
  166. extern JSBool
  167. js_DecompileScript(JSPrinter *jp, JSScript *script);
  168.  
  169. extern JSBool
  170. js_DecompileFunction(JSPrinter *jp, JSFunction *fun, JSBool newlines);
  171.  
  172. /*
  173.  * Find the source expression that resulted in v, and return a new string
  174.  * containing it.  Fall back on v's string conversion if cx lacks sufficient
  175.  * information to tell what source resulted in v.
  176.  */
  177. extern JSString *
  178. js_ValueToSource(JSContext *cx, jsval v);
  179.  
  180. PR_END_EXTERN_C
  181.  
  182. #endif /* jsopcode_h___ */
  183.