src/Code.h
author David Anderson <dvander@alliedmods.net>
Sat Aug 11 12:11:39 2012 -0700 (2012-08-11)
changeset 170 5e4570966ece
parent 91 5f4402180204
child 195 c1ba166f3fc4
permissions -rw-r--r--
Parser support for public variables.
     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 #include "Token.h"
    28 
    29 namespace ke {
    30 
    31 class Module;
    32 class Type;
    33 
    34 // A code object contains the bytecode compiled from a function (or global
    35 // script), as well as all information needed to interpret or JIT-compile
    36 // the bytecode.
    37 class Code : public Object
    38 {
    39     Barriered<Module> module_;
    40     Barriered<ByteArray> bytecode_;
    41     Barriered<FixedArray> locals_;
    42     Barriered<FixedArray> objects_;
    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, unsigned maxStackDepth, Handle<Module> module,
    56                      Handle<ByteArray> safepoints, Handle<ByteArray> sourcemap,
    57                      unsigned lineNumber);
    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     Map *map(size_t index) {
    78         return Map::cast(objects_->at(index));
    79     }
    80     Object *object(size_t index) {
    81         return objects_->at(index);
    82     }
    83     ByteArray *safepoints() {
    84         return safepoints_;
    85     }
    86     uint8 *safepointAt(unsigned pcOffset);
    87 
    88     unsigned pcToOffset(bytecode *pc) {
    89         assert(pc >= start() && pc < start() + length());
    90         return unsigned(pc - start());
    91     }
    92 
    93     SourcePosition pcToSource(unsigned pc);
    94     unsigned lineNumber() {
    95         return lineNumber_;
    96     }
    97 
    98     // GC Descriptor.
    99     static inline size_t offsetOfModule() {
   100         return OFFSETOF(Code, module_);
   101     }
   102     static inline size_t offsetOfBytecode() {
   103         return OFFSETOF(Code, bytecode_);
   104     }
   105     static inline size_t offsetOfLocals() {
   106         return OFFSETOF(Code, locals_);
   107     }
   108     static inline size_t offsetOfObjects() {
   109         return OFFSETOF(Code, objects_);
   110     }
   111     static inline size_t offsetOfSafepoints() {
   112         return OFFSETOF(Code, safepoints_);
   113     }
   114     static inline size_t offsetOfSourceMap() {
   115         return OFFSETOF(Code, sourcemap_);
   116     }
   117     // End GC Descriptor.
   118 
   119     static Code *cast(Object *obj) {
   120         assert(!obj || obj->is(MapKind_Code));
   121         return reinterpret_cast<Code *>(obj);
   122     }
   123 };
   124 
   125 }
   126 
   127 #endif // _include_jc_code_h_