src/Code.h
author David Anderson <dvander@alliedmods.net>
Sat Jan 12 16:35:26 2013 -0800 (2013-01-12)
changeset 260 05de34f712fe
parent 230 fb3cd08bc7fb
child 263 ba85a47ee414
permissions -rw-r--r--
Add support for mutually recursive imports.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #ifndef _include_jc_code_h_
    20 #define _include_jc_code_h_
    21 
    22 #include "Opcodes.h"
    23 #include "Barriers.h"
    24 #include "Heap.h"
    25 #include "Types.h"
    26 #include "FixedArray.h"
    27 
    28 namespace ke {
    29 
    30 class Module;
    31 class Type;
    32 
    33 // A code object contains the bytecode compiled from a function (or global
    34 // script), as well as all information needed to interpret or JIT-compile
    35 // the bytecode.
    36 class Code : public Object
    37 {
    38     Barriered<Module> module_;
    39     Barriered<ByteArray> bytecode_;
    40     Barriered<FixedArray> locals_;
    41     Barriered<FixedArray> objects_;
    42     Barriered<FixedArray> strings_;
    43     Barriered<ByteArray> safepoints_;
    44     Barriered<ByteArray> sourcemap_;
    45     unsigned maxStackDepth_;
    46     unsigned lineNumber_;
    47     unsigned maxLoopDepth_;
    48 
    49   public:
    50     static inline size_t BytesPerSafepoint(size_t stackDepth) {
    51         return (Align(stackDepth, 8) / 8) + sizeof(unsigned);
    52     }
    53 
    54   public:
    55     static Code *New(Zone *zone, Handle<ByteArray> bytecode, Handle<FixedArray> locals,
    56                      Handle<FixedArray> objects, Handle<FixedArray> strings, unsigned maxStackDepth,
    57                      unsigned maxLoopDepth, Handle<ByteArray> safepoints, Handle<ByteArray> sourcemap);
    58 
    59     Module *module() {
    60         return module_;
    61     }
    62     bytecode *start() {
    63         return bytecode_->raw();
    64     }
    65     unsigned length() {
    66         return bytecode_->length();
    67     }
    68     Type *localType(size_t index) {
    69         return Type::cast(locals_->at(index));
    70     }
    71     unsigned nlocals() {
    72         return locals_ ? locals_->length() : 0;
    73     }
    74     unsigned maxStackDepth() {
    75         return maxStackDepth_;
    76     }
    77     unsigned maxLoopDepth() {
    78         return maxLoopDepth_;
    79     }
    80     Map *map(size_t index) {
    81         return Map::cast(objects_->at(index));
    82     }
    83     Object *object(size_t index) {
    84         return objects_->at(index);
    85     }
    86     ByteArray *safepoints() {
    87         return safepoints_;
    88     }
    89     FixedArray *strings() {
    90         return strings_;
    91     }
    92     uint8 *safepointAt(unsigned pcOffset);
    93 
    94     void setModule(Handle<Module> module) {
    95         module_ = module;
    96     }
    97 
    98     unsigned pcToOffset(bytecode *pc) {
    99         assert(pc >= start() && pc < start() + length());
   100         return unsigned(pc - start());
   101     }
   102 
   103     SourcePosition pcToSource(unsigned pc);
   104     unsigned lineNumber() {
   105         return lineNumber_;
   106     }
   107 
   108     // GC Descriptor.
   109     static inline size_t offsetOfModule() {
   110         return OFFSETOF(Code, module_);
   111     }
   112     static inline size_t offsetOfBytecode() {
   113         return OFFSETOF(Code, bytecode_);
   114     }
   115     static inline size_t offsetOfLocals() {
   116         return OFFSETOF(Code, locals_);
   117     }
   118     static inline size_t offsetOfObjects() {
   119         return OFFSETOF(Code, objects_);
   120     }
   121     static inline size_t offsetOfStrings() {
   122         return OFFSETOF(Code, strings_);
   123     }
   124     static inline size_t offsetOfSafepoints() {
   125         return OFFSETOF(Code, safepoints_);
   126     }
   127     static inline size_t offsetOfSourceMap() {
   128         return OFFSETOF(Code, sourcemap_);
   129     }
   130     // End GC Descriptor.
   131 
   132     static Code *cast(Object *obj) {
   133         assert(!obj || obj->is(MapKind_Code));
   134         return reinterpret_cast<Code *>(obj);
   135     }
   136 };
   137 
   138 }
   139 
   140 #endif // _include_jc_code_h_