src/Code.h
author David Anderson <dvander@alliedmods.net>
Sun Nov 18 15:00:08 2012 -0800 (2012-11-18)
changeset 214 e1a6deaa6ebe
parent 195 c1ba166f3fc4
child 230 fb3cd08bc7fb
permissions -rw-r--r--
Fix timeout handling in the test harness.
     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 
    48   public:
    49     static inline size_t BytesPerSafepoint(size_t stackDepth) {
    50         return (Align(stackDepth, 8) / 8) + sizeof(unsigned);
    51     }
    52 
    53   public:
    54     static Code *New(Zone *zone, Handle<ByteArray> bytecode, Handle<FixedArray> locals,
    55                      Handle<FixedArray> objects, Handle<FixedArray> strings, unsigned maxStackDepth,
    56                      Handle<ByteArray> safepoints, Handle<ByteArray> sourcemap);
    57 
    58     Module *module() {
    59         return module_;
    60     }
    61     bytecode *start() {
    62         return bytecode_->raw();
    63     }
    64     unsigned length() {
    65         return bytecode_->length();
    66     }
    67     Type *localType(size_t index) {
    68         return Type::cast(locals_->at(index));
    69     }
    70     unsigned nlocals() {
    71         return locals_ ? locals_->length() : 0;
    72     }
    73     unsigned maxStackDepth() {
    74         return maxStackDepth_;
    75     }
    76     Map *map(size_t index) {
    77         return Map::cast(objects_->at(index));
    78     }
    79     Object *object(size_t index) {
    80         return objects_->at(index);
    81     }
    82     ByteArray *safepoints() {
    83         return safepoints_;
    84     }
    85     FixedArray *strings() {
    86         return strings_;
    87     }
    88     uint8 *safepointAt(unsigned pcOffset);
    89 
    90     void setModule(Handle<Module> module) {
    91         module_ = module;
    92     }
    93 
    94     unsigned pcToOffset(bytecode *pc) {
    95         assert(pc >= start() && pc < start() + length());
    96         return unsigned(pc - start());
    97     }
    98 
    99     SourcePosition pcToSource(unsigned pc);
   100     unsigned lineNumber() {
   101         return lineNumber_;
   102     }
   103 
   104     // GC Descriptor.
   105     static inline size_t offsetOfModule() {
   106         return OFFSETOF(Code, module_);
   107     }
   108     static inline size_t offsetOfBytecode() {
   109         return OFFSETOF(Code, bytecode_);
   110     }
   111     static inline size_t offsetOfLocals() {
   112         return OFFSETOF(Code, locals_);
   113     }
   114     static inline size_t offsetOfObjects() {
   115         return OFFSETOF(Code, objects_);
   116     }
   117     static inline size_t offsetOfStrings() {
   118         return OFFSETOF(Code, strings_);
   119     }
   120     static inline size_t offsetOfSafepoints() {
   121         return OFFSETOF(Code, safepoints_);
   122     }
   123     static inline size_t offsetOfSourceMap() {
   124         return OFFSETOF(Code, sourcemap_);
   125     }
   126     // End GC Descriptor.
   127 
   128     static Code *cast(Object *obj) {
   129         assert(!obj || obj->is(MapKind_Code));
   130         return reinterpret_cast<Code *>(obj);
   131     }
   132 };
   133 
   134 }
   135 
   136 #endif // _include_jc_code_h_