jitcraft/Code.h
author David Anderson <dvander@alliedmods.net>
Tue Jul 03 08:30:09 2012 -0700 (2012-07-03)
changeset 44 c920f9328716
parent 34 9aafaf3797e6
child 47 050939c6181e
permissions -rw-r--r--
Added GC descriptors (lol, these are not descriptors).
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     6  * JITCraft is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * JITCraft. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #ifndef _include_jc_code_h_
    19 #define _include_jc_code_h_
    20 
    21 #include "Opcodes.h"
    22 #include "Barriers.h"
    23 #include "Heap.h"
    24 #include "Types.h"
    25 #include "FixedArray.h"
    26 
    27 namespace jc {
    28 
    29 class Module;
    30 class Type;
    31 
    32 // A code object contains the bytecode compiled from a function (or global
    33 // script), as well as all information needed to interpret or JIT-compile
    34 // the bytecode.
    35 class Code : public Object
    36 {
    37     Barriered<Module> module_;
    38     Barriered<ByteArray> bytecode_;
    39     Barriered<FixedArray> locals_;
    40     Barriered<FixedArray> objects_;
    41     unsigned maxStackDepth_;
    42 
    43   public:
    44     static Code *New(Zone *zone, Handle<ByteArray> bytecode, Handle<FixedArray> locals,
    45                      Handle<FixedArray> objects, unsigned maxStackDepth, Handle<Module> module);
    46 
    47     Module *module() {
    48         return module_;
    49     }
    50     bytecode *start() {
    51         return bytecode_->raw();
    52     }
    53     unsigned length() {
    54         return bytecode_->length();
    55     }
    56     Type *localType(size_t index) {
    57         return Type::cast(locals_->at(index));
    58     }
    59     unsigned nlocals() {
    60         return locals_->length();
    61     }
    62     unsigned maxStackDepth() {
    63         return maxStackDepth_;
    64     }
    65     Map *map(size_t index) {
    66         return Map::cast(objects_->at(index));
    67     }
    68     Object *object(size_t index) {
    69         return objects_->at(index);
    70     }
    71 
    72     // GC Descriptor.
    73     static inline size_t offsetOfModule() {
    74         return offsetof(Code, module_);
    75     }
    76     static inline size_t offsetOfBytecode() {
    77         return offsetof(Code, bytecode_);
    78     }
    79     static inline size_t offsetOfLocals() {
    80         return offsetof(Code, locals_);
    81     }
    82     static inline size_t offsetOfObjects() {
    83         return offsetof(Code, objects_);
    84     }
    85     // End GC Descriptor.
    86 
    87     static Code *cast(Object *obj) {
    88         assert(!obj || obj->is(MapKind_Code));
    89         return reinterpret_cast<Code *>(obj);
    90     }
    91 };
    92 
    93 }
    94 
    95 #endif // _include_jc_code_h_