src/compiler/BytecodeEmitter.h
author David Anderson <dvander@alliedmods.net>
Sat Dec 22 19:48:43 2012 -0800 (2012-12-22)
changeset 232 2b8512340180
parent 230 fb3cd08bc7fb
child 234 26e3035cdffc
permissions -rw-r--r--
Re-introduce definite return analysis.
     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 "../Opcodes.h"
    25 #include "../Handles.h"
    26 #include "../CompactBuffer.h"
    27 #include "../Array.h"
    28 #include "../HashTable.h"
    29 #include "../VirtualObjectVisitor.h"
    30 
    31 namespace ke {
    32 
    33 // An overarching note on overflows: Though we should use CheckedInt, we rely
    34 // on a source-length cap covering all possible overflow checks. As long as
    35 // the source length is capped to INT_MAX, the number of AST nodes will be
    36 // strictly beneath that cap, and so to will most things generated by the
    37 // AST. For example, local and argument counts, number of functions, fields
    38 // in a struct, et cetera. Not everything can be covered by this cap: the
    39 // stack depth, number of bytecodes for example. In these cases we explicitly
    40 // test for overflow of INT_MAX and in other cases we assert.
    41 //
    42 // In the future, we should of course use a checked int wrapper.
    43 
    44 class Type;
    45 class Map;
    46 class ArrayMap;
    47 class Symbol;
    48 class VariableSymbol;
    49 
    50 struct OopRecord
    51 {
    52     unsigned depth;
    53     unsigned offset;
    54     Vector<bool> slots;
    55 
    56     OopRecord(unsigned length, unsigned offset)
    57       : depth(length),
    58         offset(offset)
    59     {
    60     }
    61 };
    62 
    63 class StringTable
    64 {
    65     struct Entry
    66     {
    67         String *string;
    68         unsigned index;
    69 
    70         Entry()
    71           : string(NULL),
    72             index(0)
    73         {
    74         }
    75 
    76         Entry(String *string, unsigned index)
    77           : string(string),
    78             index(index)
    79         {
    80         }
    81     };
    82 
    83     struct Policy
    84     {
    85         typedef Entry Payload;
    86 
    87         static uint32 hash(String *string) {
    88             return HashPointer(string);
    89         }
    90         
    91         static bool matches(String *key, Entry &e) {
    92             return key == e.string;
    93         }
    94     };
    95 
    96     typedef HashTable<Policy, ZoneAllocatorPolicy> Set;
    97 
    98   public:
    99     StringTable();
   100     bool initialize();
   101     void accept(VirtualObjectVisitor *visitor);
   102 
   103     bool add(Handle<String> name, unsigned *indexp);
   104     size_t stringCount() const {
   105         return strings_.length();
   106     }
   107     String *stringAt(size_t i) const {
   108         return strings_[i].string;
   109     }
   110 
   111   private:
   112     Set set_;
   113     Vector<Entry> strings_;
   114     bool initialized_;
   115 };
   116 
   117 // The bytecode emitter assists in building Code objects, by emitting opcodes
   118 // and tracking stack information. Since the SemA creates temporary pool/root
   119 // scopes, we cannot use ScopedRoot or PoolObjects. Instead, BCE gets a
   120 // special marking function.
   121 class BytecodeEmitter : public AutoRooted
   122 {
   123   private:
   124     PoolAllocator &pool_;
   125 
   126     bytecode *buffer_;
   127     bytecode *buffer_pos_;
   128     bytecode *buffer_end_;
   129     bytecode inline_buffer_[512];
   130     bool oom_;
   131     unsigned maxStackDepth_;
   132     Vector<bool> oopMap_;
   133     Vector<OopRecord *> oopRecords_;
   134     CompactBufferWriter sourcemap_;
   135     unsigned lastCodePosition_;
   136     unsigned firstLine_;
   137     StringTable strings_;
   138     Vector<Symbol *> locals_;
   139     Vector<Object *> objects_;
   140     unsigned loopDepth_;
   141     unsigned maxLoopDepth_;
   142 
   143   private:
   144     // The maximum size of a code object must be such that the distance between
   145     // two instructions fits into a signed, 31-bit integer. This ensures that
   146     // all code-bounded buffers (like the number of object literals or
   147     // safepoints) does not need to be bounds checked. It also means we can add
   148     // any two offsets and be guaranteed that they don't overflow.
   149     static const size_t MAX_LENGTH = INT_MAX / 2;
   150 
   151   private:
   152     bool growBuffer(size_t bytes);
   153 
   154     // Checking the return value is not necessary, since the inline buffer is
   155     // used as a ballast.
   156     bool ensure(size_t bytes) {
   157         if (buffer_pos_ + bytes > buffer_end_)
   158             return growBuffer(bytes);
   159         return true;
   160     }
   161 
   162     void writeUint8(unsigned value) {
   163         assert(value <= UCHAR_MAX);
   164         *buffer_pos_++ = value;
   165     }
   166     void writeFloat(float value) {
   167         *(float *)buffer_pos_ = value;
   168         buffer_pos_ += sizeof(float);
   169     }
   170     void writeInt32(int value) {
   171         *(int *)buffer_pos_ = value;
   172         buffer_pos_ += sizeof(int);
   173     }
   174     void writeUint32(unsigned value) {
   175         *(unsigned *)buffer_pos_ = value;
   176         buffer_pos_ += sizeof(unsigned);
   177     }
   178     void writeOp(Opcode op) {
   179         if (oom_)
   180             return;
   181 
   182         writeUint8(op);
   183         for (int i = 0; i < OpcodeInfo[op].nuses; i++)
   184             oopMap_.pop();
   185         for (int i = 0; i < OpcodeInfo[op].ndefs; i++)
   186             oopMap_.append(false);
   187         if (oopMap_.length() > maxStackDepth_)
   188             maxStackDepth_ = oopMap_.length();
   189     }
   190     void writeObject(Object *obj) {
   191         writeUint32(objects_.length());
   192         ScopedRoot<Object> root(obj);
   193         if (!objects_.append(root))
   194             oom_ = true;
   195     }
   196 
   197     int position() const {
   198         assert(buffer_pos_ - buffer_ < INT_MAX / 2);
   199         return int(buffer_pos_ - buffer_);
   200     }
   201 
   202   public:
   203     BytecodeEmitter(PoolAllocator &pool);
   204     ~BytecodeEmitter();
   205 
   206     void accept(VirtualObjectVisitor *visitor);
   207 
   208     bytecode *buffer() const {
   209         return buffer_;
   210     }
   211     unsigned length() const {
   212         return unsigned(buffer_pos_ - buffer_);
   213     }
   214     bool oom() const {
   215         return oom_ || sourcemap_.oom();
   216     }
   217     unsigned stackDepth() const {
   218         return oopMap_.length();
   219     }
   220     unsigned maxStackDepth() const {
   221         return maxStackDepth_;
   222     }
   223 
   224     void drop(unsigned amt) {
   225         assert(amt <= stackDepth());
   226         for (unsigned i = 0; i < amt; i++)
   227             oopMap_.pop();
   228     }
   229 
   230     void note_position(const SourcePosition &pos);
   231     void setFirstLine(unsigned line) {
   232         assert(!sourcemap_.length());
   233         firstLine_ = line;
   234     }
   235 
   236   public:
   237     // A label represents a local control-flow target in the bytecode. A label
   238     // may be in three states:
   239     //
   240     // (1) Empty - the label is not used yet.
   241     // (2) Pending - the label has incoming jumps, but the actual location of
   242     //               is not yet known. The list of jumps is threaded through
   243     //               the four bytes reserved for each emitted jump target. In
   244     //               this case, the offset field is the location of the most
   245     //               recently emitted incoming jump.
   246     // (3) Bound   - the label's position has been pinned.
   247     //
   248     // A jump can be emitted to a label in any state. Binding a label
   249     // immediately backpatches all pending incoming jumps. A label must be
   250     // bound if it is used.
   251     class Label
   252     {
   253         int offset_ : 31;
   254         bool bound_ : 1;
   255 
   256         void setOffset(int offset) {
   257             offset_ = offset;
   258             assert(offset_ == offset);
   259         }
   260 
   261       public:
   262         static const int INVALID_OFFSET = -1;
   263 
   264       public:
   265         Label()
   266           : offset_(INVALID_OFFSET),
   267             bound_(false)
   268         {
   269         }
   270         ~Label()
   271         {
   272             assert(bound_ || offset_ == INVALID_OFFSET);
   273         }
   274         
   275         bool bound() const {
   276             return bound_;
   277         }
   278         int offset() const {
   279             return offset_;
   280         }
   281         void bind(int offset) {
   282             assert(!bound_);
   283             setOffset(offset);
   284             bound_ = true;
   285         }
   286         int link(int offset) {
   287             assert(!bound_);
   288             int old = offset_;
   289             setOffset(offset);
   290             return old;
   291         }
   292     };
   293 
   294   private:
   295     int patchJumpAt(int at, int to) {
   296         assert(at >= 0 && at < position());
   297         int old = *(int *)(buffer_ + at + 1);
   298         *(int *)(buffer_ + at + 1) = (to - at);
   299         return old;
   300     }
   301 
   302     void singleByteOp(Opcode op) {
   303         assert(OpcodeInfo[op].length == 1);
   304         ensure(OpcodeInfo[op].length);
   305         writeOp(op);
   306     }
   307 
   308     // Mark a stack slot as having an object pointer.
   309     void markOop(unsigned depth, bool oop = true) {
   310         if (oom_)
   311             return;
   312         oopMap_[oopMap_.length() - depth] = oop;
   313     }
   314     bool getOop(unsigned depth) {
   315         if (oom_)
   316             return false;
   317         return oopMap_[oopMap_.length() - depth];
   318     }
   319 
   320     // Mark the current bytecode position as either re-entrant (meaning, it
   321     // can leave the interpreter or call to another function), or causing GC.
   322     // This will record which stack slots have objects and associate that
   323     // information with the current pc.
   324     void recordOops();
   325 
   326   public:
   327     void int_(int value) {
   328         ensure(OP_INT);
   329         writeOp(OP_INT);
   330         writeInt32(value);
   331     }
   332     void return_() {
   333         ensure(OP_RETURN);
   334         writeOp(OP_RETURN);
   335     }
   336     void getlocal(VariableSymbol *var);
   337     void setlocal(VariableSymbol *var);
   338     void getarg(VariableSymbol *var);
   339     void setarg(VariableSymbol *var);
   340 
   341     void j(Opcode op, Label *label) {
   342         // Since jumps can skip around the stream, do not try to do anything
   343         // in a potentially truncated state.
   344         if (oom())
   345             return;
   346 
   347         assert(op == OP_JT || op == OP_JF || op == OP_JUMP ||
   348                op == OP_CONTINUE || op == OP_BREAK);
   349         ensure(OP_JUMP_LENGTH);
   350 
   351         if (label->bound()) {
   352             // We can immediately emit this jump.
   353             int distance = label->offset() - position();
   354             writeOp(op);
   355             writeInt32(distance);
   356         } else {
   357             // We can't emit this jump yet. Add a placeholder.
   358             int old = label->link(position());
   359             writeOp(op);
   360             writeInt32(old);
   361         }
   362     }
   363     void bind(Label *label) {
   364         if (oom())
   365             return;
   366 
   367         assert(!label->bound());
   368 
   369         int offset = label->offset();
   370         while (offset != Label::INVALID_OFFSET)
   371             offset = patchJumpAt(offset, position());
   372 
   373         label->bind(position());
   374     }
   375     void jump(Label *label) {
   376         j(OP_JUMP, label);
   377     }
   378 
   379     void binary(Opcode op) {
   380         singleByteOp(op);
   381     }
   382     void unary(Opcode op) {
   383         singleByteOp(op);
   384     }
   385 
   386     void pop() {
   387         singleByteOp(OP_POP);
   388     }
   389     void stop() {
   390         singleByteOp(OP_STOP);
   391     }
   392     void call(Handle<FunctionType> fun, unsigned argc) {
   393         recordOops();
   394 
   395         if (fun->isNative()) {
   396             ensure(OP_CALLNATIVE_LENGTH);
   397             writeOp(OP_CALLNATIVE);
   398             writeUint32(argc);
   399             drop(argc + 1);
   400         } else {
   401             ensure(OP_CALL_LENGTH);
   402             writeOp(OP_CALL);
   403             drop(argc + 1);
   404 
   405             if (fun->returnType()->isVoid())
   406                 drop(1);
   407             else if (fun->returnType()->isTraceable())
   408                 markOop(1);
   409         }
   410     }
   411 
   412     void getelem(Type *type) {
   413         singleByteOp(OP_GETELEM);
   414 
   415         if (type->isTraceable())
   416             markOop(1);
   417     }
   418     void setelem(Type *type) {
   419         singleByteOp(OP_SETELEM);
   420     }
   421 
   422     void getfield(unsigned index, Type *type) {
   423         ensure(OP_GETFIELD_LENGTH);
   424         writeOp(OP_GETFIELD);
   425         writeUint32(index);
   426 
   427         if (type->isTraceable())
   428             markOop(1);
   429     }
   430     void setfield(unsigned index, Type *type) {
   431         ensure(OP_SETFIELD_LENGTH);
   432         writeOp(OP_SETFIELD);
   433         writeUint32(index);
   434 
   435         if (type->isTraceable())
   436             markOop(1);
   437     }
   438 
   439     int enterloop() {
   440         int offset = position();
   441 
   442         ensure(OP_ENTERLOOP);
   443         writeOp(OP_ENTERLOOP);
   444         writeUint8(0);
   445 
   446         loopDepth_++;
   447         if (loopDepth_ > maxLoopDepth_)
   448             maxLoopDepth_ = loopDepth_;
   449 
   450         return offset;
   451     }
   452     void leaveloop() {
   453         singleByteOp(OP_LEAVELOOP);
   454 
   455         assert(loopDepth_);
   456         loopDepth_--;
   457     }
   458     void setLoopUsesLifoStack(int pos) {
   459         assert(pos >= 0 && pos < position());
   460         assert(*(buffer() + pos) == OP_ENTERLOOP);
   461         *(buffer() + pos + 1) = 1;
   462     }
   463 
   464     void float_(float value) {
   465         ensure(OP_FLOAT_LENGTH);
   466         writeOp(OP_FLOAT);
   467         writeFloat(value);
   468     }
   469     void cvt_i2f() {
   470         singleByteOp(OP_CVT_I2F);
   471     }
   472     void newarray(Opcode op, Handle<ArrayMap> map, Heap::Tenure tenure) {
   473         assert(op == OP_NEWFIXED ||
   474                op == OP_NEWEMPTY ||
   475                op == OP_NEWSIZED);
   476 
   477         recordOops();
   478         ensure(OpcodeInfo[op].length);
   479         writeOp(op);
   480         writeUint8(tenure);
   481         writeObject(map);
   482 
   483         if (op == OP_NEWSIZED)
   484             drop(map->type()->levels());
   485 
   486         markOop(1);
   487     }
   488     void newstruct(Handle<Map> map, Heap::Tenure tenure) {
   489         recordOops();
   490         ensure(OP_NEWSTRUCT_LENGTH);
   491         writeOp(OP_NEWSTRUCT);
   492         writeUint8(tenure);
   493         writeObject(map);
   494         markOop(1);
   495     }
   496     void savelifo(unsigned slot) {
   497         ensure(OP_SAVELIFO_LENGTH);
   498         writeOp(OP_SAVELIFO);
   499         writeUint32(slot);
   500     }
   501     void unwindlifo(unsigned slot) {
   502         ensure(OP_UNWINDLIFO_LENGTH);
   503         writeOp(OP_UNWINDLIFO);
   504         writeUint32(slot);
   505     }
   506 
   507     void newref(VariableSymbol *var);
   508 
   509     void slotref_lifo() {
   510         recordOops();
   511         singleByteOp(OP_SLOTREF_LIFO);
   512         markOop(1);
   513     }
   514     void getref(Type *type) {
   515         if (type->pod() == PrimitiveType_Int32) {
   516             singleByteOp(OP_GETREF_I);
   517         } else {
   518             assert(type->pod() == PrimitiveType_Float);
   519             singleByteOp(OP_GETREF_F);
   520         }
   521     }
   522     void setref(Type *type) {
   523         if (type->pod() == PrimitiveType_Int32) {
   524             singleByteOp(OP_SETREF_I);
   525         } else {
   526             assert(type->pod() == PrimitiveType_Float);
   527             singleByteOp(OP_SETREF_F);
   528         }
   529     }
   530     void object(Handle<Object> obj) {
   531         ensure(OP_OBJECT_LENGTH);
   532         writeOp(OP_OBJECT);
   533         writeObject(obj);
   534         markOop(1);
   535     }
   536     void copyarray() {
   537         singleByteOp(OP_COPYARRAY);
   538         markOop(1);
   539     }
   540     void copystruct() {
   541         singleByteOp(OP_COPYSTRUCT);
   542         markOop(1);
   543     }
   544     void sizeof_() {
   545         singleByteOp(OP_SIZEOF);
   546     }
   547 
   548     void dup() {
   549         bool oop = getOop(1);
   550         singleByteOp(OP_DUP);
   551         markOop(1, oop);
   552     }
   553     void dup2() {
   554         bool oop2 = getOop(2);
   555         bool oop1 = getOop(1);
   556 
   557         singleByteOp(OP_DUP2);
   558 
   559         markOop(2, oop2);
   560         markOop(1, oop1);
   561     }
   562     void swap() {
   563         bool oop2 = getOop(2);
   564         bool oop1 = getOop(1);
   565 
   566         singleByteOp(OP_SWAP);
   567 
   568         markOop(2, oop1);
   569         markOop(1, oop2);
   570     }
   571     void roll3() {
   572         bool oop3 = getOop(3);
   573         bool oop2 = getOop(2);
   574         bool oop1 = getOop(1);
   575 
   576         singleByteOp(OP_ROLL3);
   577 
   578         markOop(3, oop2);
   579         markOop(2, oop1);
   580         markOop(1, oop3);
   581     }
   582     void pick2() {
   583         bool oop2 = getOop(2);
   584         bool oop1 = getOop(1);
   585 
   586         singleByteOp(OP_PICK2);
   587 
   588         markOop(3, oop2);
   589         markOop(2, oop1);
   590         markOop(1, oop2);
   591     }
   592     void pick3() {
   593         bool oop3 = getOop(3);
   594         bool oop2 = getOop(2);
   595         bool oop1 = getOop(1);
   596 
   597         singleByteOp(OP_PICK3);
   598 
   599         markOop(4, oop3);
   600         markOop(3, oop2);
   601         markOop(2, oop1);
   602         markOop(1, oop3);
   603     }
   604 
   605     void returnvoid() {
   606         singleByteOp(OP_RETURNVOID);
   607     }
   608     void bitnot() {
   609         singleByteOp(OP_BITNOT);
   610     }
   611     void not_() {
   612         singleByteOp(OP_NOT);
   613     }
   614     void not_f() {
   615         singleByteOp(OP_NOT_F);
   616     }
   617     void neg() {
   618         singleByteOp(OP_NEG);
   619     }
   620     void neg_f() {
   621         singleByteOp(OP_NEG_F);
   622     }
   623     void bits_i2f() {
   624         singleByteOp(OP_BITS_I2F);
   625     }
   626     void bits_f2i() {
   627         singleByteOp(OP_BITS_F2I);
   628     }
   629     void true_() {
   630         singleByteOp(OP_TRUE);
   631     }
   632     void false_() {
   633         singleByteOp(OP_FALSE);
   634     }
   635     
   636     unsigned addString(Handle<String> name) {
   637         unsigned index;
   638         if (!strings_.add(name, &index)) {
   639             oom_ |= true;
   640             return 0;
   641         }
   642         return index;
   643     }
   644 
   645     void getglobal(Symbol *sym);
   646     void setglobal(Symbol *sym);
   647 
   648     void public_() {
   649         recordOops();
   650         singleByteOp(OP_PUBLIC);
   651     }
   652 
   653     void tableswitch(int low, int high, Label *def, Label **labels) {
   654         unsigned tablesize = TableSwitchEntries(low, high);
   655         assert(tablesize <= MAX_TABLESWITCH_ENTRIES);
   656 
   657         ensure(OP_TABLESWITCH_LENGTH);
   658         writeOp(OP_TABLESWITCH);
   659         writeInt32(low);
   660         writeInt32(high);
   661         jump(def);
   662 
   663         // When labels are patched, we assume each position is a jump. Rather
   664         // than hack position offsets to make labels work without actual
   665         // incoming jumps, we emit a jump inline.
   666         for (unsigned i = 0; i < tablesize; i++)
   667             jump(labels[i]);
   668     }
   669 
   670     bool allocate(VariableSymbol *sym);
   671 
   672     Code *finish();
   673 
   674     // Helpers.
   675 #if 0
   676     void get(Variable *var) {
   677         if (var->location() == Variable::STACK) {
   678             getlocal(var);
   679         } else {
   680             assert(var->location() == Variable::PARAMETER);
   681             getarg(var);
   682         }
   683     }
   684     void set(Variable *var) {
   685         assert(!var->type()->isReference());
   686         if (var->location() == Variable::STACK) {
   687             setlocal(var);
   688         } else {
   689             assert(var->location() == Variable::PARAMETER);
   690             setarg(var);
   691         }
   692     }
   693 #endif
   694 };
   695 
   696 }
   697 
   698 #endif // _include_bytecode_emitter_h_