jitcraft/Code.h
author David Anderson <dvander@alliedmods.net>
Mon Jul 09 22:25:52 2012 -0700 (2012-07-09)
changeset 67 b765a6383849
parent 59 b7d61ddb3c8d
child 70 1aec996cde2d
permissions -rw-r--r--
Implemented const for references and arrays.
     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<ByteArray> safepoints_;
    43     unsigned maxStackDepth_;
    44 
    45   public:
    46     static inline size_t BytesPerSafepoint(size_t stackDepth) {
    47         return (Align(stackDepth, 8) / 8) + sizeof(unsigned);
    48     }
    49 
    50   public:
    51     static Code *New(Zone *zone, Handle<ByteArray> bytecode, Handle<FixedArray> locals,
    52                      Handle<FixedArray> objects, unsigned maxStackDepth, Handle<Module> module,
    53                      Handle<ByteArray> safepoints);
    54 
    55     Module *module() {
    56         return module_;
    57     }
    58     bytecode *start() {
    59         return bytecode_->raw();
    60     }
    61     unsigned length() {
    62         return bytecode_->length();
    63     }
    64     Type *localType(size_t index) {
    65         return Type::cast(locals_->at(index));
    66     }
    67     unsigned nlocals() {
    68         return locals_->length();
    69     }
    70     unsigned maxStackDepth() {
    71         return maxStackDepth_;
    72     }
    73     Map *map(size_t index) {
    74         return Map::cast(objects_->at(index));
    75     }
    76     Object *object(size_t index) {
    77         return objects_->at(index);
    78     }
    79     ByteArray *safepoints() {
    80         return safepoints_;
    81     }
    82     uint8 *safepointAt(unsigned pcOffset);
    83 
    84     unsigned pcToOffset(bytecode *pc) {
    85         assert(pc >= start() && pc < start() + length());
    86         return pc - start();
    87     }
    88 
    89     // GC Descriptor.
    90     static inline size_t offsetOfModule() {
    91         return offsetof(Code, module_);
    92     }
    93     static inline size_t offsetOfBytecode() {
    94         return offsetof(Code, bytecode_);
    95     }
    96     static inline size_t offsetOfLocals() {
    97         return offsetof(Code, locals_);
    98     }
    99     static inline size_t offsetOfObjects() {
   100         return offsetof(Code, objects_);
   101     }
   102     static inline size_t offsetOfSafepoints() {
   103         return offsetof(Code, safepoints_);
   104     }
   105     // End GC Descriptor.
   106 
   107     static Code *cast(Object *obj) {
   108         assert(!obj || obj->is(MapKind_Code));
   109         return reinterpret_cast<Code *>(obj);
   110     }
   111 };
   112 
   113 }
   114 
   115 #endif // _include_jc_code_h_