jitcraft/BytecodeEmitter.h
author David Anderson <dvander@alliedmods.net>
Tue Jul 03 00:36:48 2012 -0700 (2012-07-03)
changeset 33 95baf120f7d4
parent 23 8a31742781c1
child 46 735503e448a0
permissions -rw-r--r--
Handle-ize BytecodeEmitter.
     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_bytecode_emitter_h_
    20 #define _include_bytecode_emitter_h_
    21 
    22 #include <assert.h>
    23 #include <limits.h>
    24 #include <vector>
    25 #include "Opcodes.h"
    26 #include "NameBinding.h"
    27 #include "Handles.h"
    28 
    29 namespace jc {
    30 
    31 class Type;
    32 class Map;
    33 
    34 typedef std::vector<ScopedRoot<Object> *> ObjectList;
    35 
    36 class BytecodeEmitter
    37 {
    38     bytecode *buffer_;
    39     bytecode *buffer_pos_;
    40     bytecode *buffer_end_;
    41     bytecode inline_buffer_[512];
    42     bool oom_;
    43     unsigned maxStackDepth_;
    44     unsigned stackDepth_;
    45     ObjectList objects_;
    46 
    47   private:
    48     static const size_t MAX_LENGTH = INT_MAX / 2;
    49 
    50   private:
    51     bool growBuffer(size_t bytes);
    52 
    53     // Checking the return value is not necessary, since the inline buffer is
    54     // used as a ballast.
    55     bool ensure(size_t bytes) {
    56         if (buffer_pos_ + bytes > buffer_end_)
    57             return growBuffer(bytes);
    58         return true;
    59     }
    60 
    61     void writeUint8(unsigned value) {
    62         assert(value >= 0 && value <= UCHAR_MAX);
    63         *buffer_pos_++ = value;
    64     }
    65     void writeFloat(float value) {
    66         *(float *)buffer_pos_ = value;
    67         buffer_pos_ += sizeof(float);
    68     }
    69     void writeInt32(int value) {
    70         *(int *)buffer_pos_ = value;
    71         buffer_pos_ += sizeof(int);
    72     }
    73     void writeUint32(unsigned value) {
    74         *(unsigned *)buffer_pos_ = value;
    75         buffer_pos_ += sizeof(unsigned);
    76     }
    77     void writeOp(Opcode op) {
    78         writeUint8(op);
    79         stackDepth_ += OpcodeInfo[op].ndefs;
    80         if (OpcodeInfo[op].nuses != -1)
    81             stackDepth_ -= OpcodeInfo[op].nuses;
    82         if (stackDepth_ > maxStackDepth_)
    83             maxStackDepth_ = stackDepth_;
    84     }
    85     void writeObject(Object *obj) {
    86         writeUint32(objects_.size());
    87         ScopedRoot<Object> *root = new ScopedRoot<Object>(obj);
    88         if (!root)
    89             oom_ = true;
    90         objects_.push_back(root);
    91     }
    92 
    93     int position() const {
    94         assert(buffer_pos_ - buffer_ < INT_MAX / 2);
    95         return int(buffer_pos_ - buffer_);
    96     }
    97 
    98   public:
    99     BytecodeEmitter();
   100 
   101     bytecode *buffer() const {
   102         return buffer_;
   103     }
   104     unsigned length() const {
   105         return unsigned(buffer_pos_ - buffer_);
   106     }
   107     bool oom() const {
   108         return oom_;
   109     }
   110     unsigned stackDepth() const {
   111         return stackDepth_;
   112     }
   113     unsigned maxStackDepth() const {
   114         return maxStackDepth_;
   115     }
   116     const ObjectList *objects() const {
   117         return &objects_;
   118     }
   119 
   120   public:
   121     // A label represents a local control-flow target in the bytecode. A label
   122     // may be in three states:
   123     //
   124     // (1) Empty - the label is not used yet.
   125     // (2) Pending - the label has incoming jumps, but the actual location of
   126     //               is not yet known. The list of jumps is threaded through
   127     //               the four bytes reserved for each emitted jump target. In
   128     //               this case, the offset field is the location of the most
   129     //               recently emitted incoming jump.
   130     // (3) Bound   - the label's position has been pinned.
   131     //
   132     // A jump can be emitted to a label in any state. Binding a label
   133     // immediately backpatches all pending incoming jumps. A label must be
   134     // bound if it is used.
   135     class Label
   136     {
   137         int offset_ : 31;
   138         bool bound_ : 1;
   139 
   140         void setOffset(int offset) {
   141             offset_ = offset;
   142             assert(offset_ == offset);
   143         }
   144 
   145       public:
   146         static const int INVALID_OFFSET = -1;
   147 
   148       public:
   149         Label()
   150           : offset_(INVALID_OFFSET),
   151             bound_(false)
   152         {
   153         }
   154         ~Label()
   155         {
   156             assert(bound_ || offset_ == INVALID_OFFSET);
   157         }
   158         
   159         bool bound() const {
   160             return bound_;
   161         }
   162         int offset() const {
   163             return offset_;
   164         }
   165         void bind(int offset) {
   166             assert(!bound_);
   167             setOffset(offset);
   168             bound_ = true;
   169         }
   170         int link(int offset) {
   171             assert(!bound_);
   172             int old = offset_;
   173             setOffset(offset);
   174             return old;
   175         }
   176     };
   177 
   178   private:
   179     int patchJumpAt(int at, int to) {
   180         assert(at >= 0 && at < position());
   181         int old = *(int *)(buffer_ + at + 1);
   182         *(int *)(buffer_ + at + 1) = (to - at);
   183         return old;
   184     }
   185 
   186     void singleByteOp(Opcode op) {
   187         assert(OpcodeInfo[op].length == 1);
   188         ensure(OpcodeInfo[op].length);
   189         writeOp(op);
   190     }
   191 
   192   public:
   193     void int_(int value) {
   194         ensure(OP_INT);
   195         writeOp(OP_INT);
   196         writeInt32(value);
   197     }
   198     void return_() {
   199         ensure(OP_RETURN);
   200         writeOp(OP_RETURN);
   201     }
   202     void getlocal(unsigned local) {
   203         ensure(OP_GETLOCAL_LENGTH);
   204         writeOp(OP_GETLOCAL);
   205         writeUint32(local);
   206     }
   207     void setlocal(unsigned local) {
   208         ensure(OP_SETLOCAL_LENGTH);
   209         writeOp(OP_SETLOCAL);
   210         writeUint32(local);
   211     }
   212     void getarg(unsigned slot) {
   213         ensure(OP_GETARG_LENGTH);
   214         writeOp(OP_GETARG);
   215         writeUint32(slot);
   216     }
   217     void setarg(unsigned slot) {
   218         ensure(OP_SETARG_LENGTH);
   219         writeOp(OP_SETARG);
   220         writeUint32(slot);
   221     }
   222 
   223     void j(Opcode op, Label *label) {
   224         // Since jumps can skip around the stream, do not try to do anything
   225         // in a potentially truncated state.
   226         if (oom())
   227             return;
   228 
   229         assert(op >= OP_JL && op <= OP_JUMP);
   230         ensure(OP_JUMP_LENGTH);
   231 
   232         if (label->bound()) {
   233             // We can immediately emit this jump.
   234             int distance = label->offset() - position();
   235             writeOp(op);
   236             writeInt32(distance);
   237         } else {
   238             // We can't emit this jump yet. Add a placeholder.
   239             int old = label->link(position());
   240             writeOp(op);
   241             writeInt32(old);
   242         }
   243     }
   244     void bind(Label *label) {
   245         if (oom())
   246             return;
   247 
   248         assert(!label->bound());
   249 
   250         int offset = label->offset();
   251         while (offset != Label::INVALID_OFFSET)
   252             offset = patchJumpAt(offset, position());
   253 
   254         label->bind(position());
   255     }
   256     void jump(Label *label) {
   257         j(OP_JUMP, label);
   258     }
   259 
   260     void binary(Opcode op) {
   261         singleByteOp(op);
   262     }
   263 
   264     void pop() {
   265         singleByteOp(OP_POP);
   266     }
   267     void stop() {
   268         singleByteOp(OP_STOP);
   269     }
   270     void callglobal(unsigned nargs, unsigned index) {
   271         ensure(OP_CALLGLOBAL_LENGTH);
   272         writeOp(OP_CALLGLOBAL);
   273         writeUint32(nargs);
   274         writeUint32(index);
   275 
   276         stackDepth_ -= nargs;
   277     }
   278     void getarray(PrimitiveType type) {
   279         switch (type) {
   280           case PrimitiveType_Float:
   281             singleByteOp(OP_GETARRAY_F);
   282             break;
   283           case PrimitiveType_Int32:
   284             singleByteOp(OP_GETARRAY_I);
   285             break;
   286           default:
   287             assert(type == PrimitiveType_Char);
   288             singleByteOp(OP_GETARRAY_C);
   289             break;
   290         }
   291     }
   292     void setarray(PrimitiveType type) {
   293         switch (type) {
   294           case PrimitiveType_Float:
   295             singleByteOp(OP_SETARRAY_F);
   296             break;
   297           case PrimitiveType_Int32:
   298             singleByteOp(OP_SETARRAY_I);
   299             break;
   300           default:
   301             assert(type == PrimitiveType_Char);
   302             singleByteOp(OP_SETARRAY_C);
   303             break;
   304         }
   305     }
   306     void getarray_o() {
   307         singleByteOp(OP_GETARRAY_O);
   308     }
   309     void bounds() {
   310         singleByteOp(OP_BOUNDS);
   311     }
   312     void float_(float value) {
   313         ensure(OP_FLOAT_LENGTH);
   314         writeOp(OP_FLOAT);
   315         writeFloat(value);
   316     }
   317     void cvt_i2f() {
   318         singleByteOp(OP_CVT_I2F);
   319     }
   320     void newarray_lifo(Handle<Map> map) {
   321         ensure(OP_NEWARRAY_LIFO_LENGTH);
   322         writeOp(OP_NEWARRAY_LIFO);
   323         writeObject(map);
   324     }
   325     void savelifo(unsigned slot) {
   326         ensure(OP_SAVELIFO_LENGTH);
   327         writeOp(OP_SAVELIFO);
   328         writeUint32(slot);
   329     }
   330     void unwindlifo(unsigned slot) {
   331         ensure(OP_UNWINDLIFO_LENGTH);
   332         writeOp(OP_UNWINDLIFO);
   333         writeUint32(slot);
   334     }
   335     void newref(Variable *var) {
   336         if (var->location() == Variable::STACK) {
   337             ensure(OP_VARREF_LIFO_LENGTH);
   338             writeOp(OP_VARREF_LIFO);
   339         } else {
   340             assert(var->location() == Variable::PARAMETER);
   341             ensure(OP_ARGREF_LIFO_LENGTH);
   342             writeOp(OP_ARGREF_LIFO);
   343         }
   344         writeUint32(var->slot());
   345     }
   346     void get(Variable *var) {
   347         if (var->location() == Variable::STACK) {
   348             getlocal(var->slot());
   349         } else {
   350             assert(var->location() == Variable::PARAMETER);
   351             getarg(var->slot());
   352         }
   353     }
   354     void set(Variable *var) {
   355         assert(!var->type()->isReference());
   356         if (var->location() == Variable::STACK) {
   357             setlocal(var->slot());
   358         } else {
   359             assert(var->location() == Variable::PARAMETER);
   360             setarg(var->slot());
   361         }
   362     }
   363     void slotref_lifo() {
   364         singleByteOp(OP_SLOTREF_LIFO);
   365     }
   366     void arrayref_lifo() {
   367         singleByteOp(OP_ARRAYREF_LIFO);
   368     }
   369     void getref(PrimitiveType type) {
   370         if (type == PrimitiveType_Int32) {
   371             singleByteOp(OP_GETREF_I);
   372         } else {
   373             assert(type == PrimitiveType_Float);
   374             singleByteOp(OP_GETREF_F);
   375         }
   376     }
   377     void setref(PrimitiveType type) {
   378         if (type == PrimitiveType_Int32) {
   379             singleByteOp(OP_SETREF_I);
   380         } else {
   381             assert(type == PrimitiveType_Float);
   382             singleByteOp(OP_SETREF_F);
   383         }
   384     }
   385     void object(Handle<Object> obj) {
   386         ensure(OP_OBJECT_LENGTH);
   387         writeOp(OP_OBJECT);
   388         writeObject(obj);
   389     }
   390     void arraycopy() {
   391         singleByteOp(OP_ARRAYCOPY);
   392     }
   393     void dup() {
   394         singleByteOp(OP_DUP);
   395     }
   396     void dup2() {
   397         singleByteOp(OP_DUP2);
   398     }
   399     void swap() {
   400         singleByteOp(OP_SWAP);
   401     }
   402     void roll3() {
   403         singleByteOp(OP_ROLL3);
   404     }
   405     void pick2() {
   406         singleByteOp(OP_PICK2);
   407     }
   408     void pick3() {
   409         singleByteOp(OP_PICK3);
   410     }
   411 };
   412 
   413 }
   414 
   415 #endif // _include_bytecode_emitter_h_