jitcraft/Code.h
author David Anderson <dvander@alliedmods.net>
Sat Jun 30 22:31:14 2012 -0700 (2012-06-30)
changeset 14 0784ac5cc347
parent 13 627b6b94e774
child 21 af5d8e985502
permissions -rw-r--r--
Added support for non-array references.
     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     Module *module_;
    38     Unbarriered<ByteArray> bytecode_;
    39     Unbarriered<FixedArray> locals_;
    40     Unbarriered<FixedArray> args_;
    41     Unbarriered<FixedArray> maps_;
    42     unsigned maxStackDepth_;
    43 
    44   public:
    45     // :TODO: use handles
    46     static Code *New(Zone *zone, ByteArray *bytecode, FixedArray *locals, FixedArray *args,
    47                      FixedArray *maps, unsigned maxStackDepth, Module *module);
    48 
    49     Module *module() {
    50         return module_;
    51     }
    52     bytecode *start() {
    53         return bytecode_->raw();
    54     }
    55     unsigned length() {
    56         return bytecode_->length();
    57     }
    58     Type *localType(size_t index) {
    59         return Type::cast(locals_->at(index));
    60     }
    61     Type *argType(size_t index) {
    62         return Type::cast(args_->at(index));
    63     }
    64     unsigned nlocals() {
    65         return locals_->length();
    66     }
    67     unsigned nargs() {
    68         return args_->length();
    69     }
    70     unsigned maxStackDepth() {
    71         return maxStackDepth_;
    72     }
    73     Map *map(size_t index) {
    74         return Map::cast(maps_->at(index));
    75     }
    76 
    77     static Code *cast(Object *obj) {
    78         assert(!obj || obj->is(MapKind_Code));
    79         return reinterpret_cast<Code *>(obj);
    80     }
    81 };
    82 
    83 }
    84 
    85 #endif // _include_jc_code_h_