jitcraft/Code.h
author David Anderson <dvander@alliedmods.net>
Sat Jun 30 19:38:53 2012 -0700 (2012-06-30)
changeset 12 75e385f68b7e
parent 0 c455ebb9a5dc
child 13 627b6b94e774
permissions -rw-r--r--
Refactored Array, Code to be Objects. Removed Object tagging.
     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 class Code : public Object
    33 {
    34     OBJECT_FIELD(ByteArray *, bytecode);
    35     OBJECT_FIELD(FixedArray *, locals);
    36     OBJECT_FIELD(FixedArray *, args);
    37     OBJECT_FIELD(uint32, maxStackDepth);
    38     OBJECT_FIELD(VoidPtr, module);
    39     OBJECT_FIELD(FixedArray *, maps);
    40 
    41   private:
    42     void init(ByteArray *bytecode, FixedArray *locals, FixedArray *args,
    43               FixedArray *maps, unsigned maxStackDepth, Module *module);
    44 
    45     ByteArray *code() {
    46         return ByteArray::cast(READ_FIELD_OBJ(bytecode));
    47     }
    48     FixedArray *locals() {
    49         return FixedArray::cast(READ_FIELD_OBJ(locals));
    50     }
    51     FixedArray *args() {
    52         return FixedArray::cast(READ_FIELD_OBJ(args));
    53     }
    54     FixedArray *maps() {
    55         return FixedArray::cast(READ_FIELD_OBJ(maps));
    56     }
    57 
    58   public:
    59     // :TODO: use handles
    60     static Code *New(Zone *zone, ByteArray *bytecode, FixedArray *locals, FixedArray *args,
    61                      FixedArray *maps, unsigned maxStackDepth, Module *module);
    62 
    63     Module *module() {
    64         return (Module *)READ_FIELD(module);
    65     }
    66     bytecode *start() {
    67         return code()->raw();
    68     }
    69     unsigned length() {
    70         return code()->length();
    71     }
    72     Type *localType(size_t index) {
    73         assert(index < nlocals());
    74         return Type::cast(locals()->at(index));
    75     }
    76     Type *argType(size_t index) {
    77         assert(index < nlocals());
    78         return Type::cast(args()->at(index));
    79     }
    80     unsigned nlocals() {
    81         return locals()->length();
    82     }
    83     unsigned nargs() {
    84         return args()->length();
    85     }
    86     unsigned maxStackDepth() {
    87         return READ_FIELD(maxStackDepth);
    88     }
    89     Map *map(size_t index) {
    90         return Map::cast(maps()->at(index));
    91     }
    92 
    93     static Code *cast(Object *obj) {
    94         assert(obj->is(MapKind_Code));
    95         return reinterpret_cast<Code *>(obj);
    96     }
    97 };
    98 
    99 }
   100 
   101 #endif // _include_jc_code_h_