SpiderScript - Moved header to directory, ready to remove from tree
[tpg/acess2.git] / Usermode / Libraries / libspiderscript.so_src / bytecode_gen.c
index 54835ae..aad172d 100644 (file)
@@ -35,6 +35,7 @@ tBC_Op *Bytecode_int_AllocateOp(int Operation, int ExtraBytes)
        ret->Operation = Operation;
        ret->bUseInteger = 0;
        ret->bUseString = (ExtraBytes > 0);
+       ret->CacheEnt = NULL;
 
        return ret;
 }
@@ -135,11 +136,49 @@ int Bytecode_int_Serialize(const tBC_Function *Function, void *Output, int *Labe
                }
                len += 4;
        }
-       
+
+       void _put_index(uint32_t value)
+       {
+               if( !Output && !value ) {
+                       len += 5;
+                       return ;
+               }
+               if( value < 0x8000 ) {
+                       _put_byte(value >> 8);
+                       _put_byte(value & 0xFF);
+               }
+               else if( value < 0x400000 ) {
+                       _put_byte( (value >> 16) | 0x80 );
+                       _put_byte(value >> 8);
+                       _put_byte(value & 0xFF);
+               }
+               else {
+                       _put_byte( 0xC0 );
+                       _put_byte(value >> 24);
+                       _put_byte(value >> 16);
+                       _put_byte(value >> 8 );
+                       _put_byte(value & 0xFF);
+               }
+       }       
+
        void _put_qword(uint64_t value)
        {
-               _put_dword(value & 0xFFFFFFFF);
-               _put_dword(value >> 32);
+               if( value < 0x80 ) {    // 7 bits into 1 byte
+                       _put_byte(value);
+               }
+               else if( !(value >> (8+6)) ) {  // 14 bits packed into 2 bytes
+                       _put_byte( 0x80 | ((value >> 8) & 0x3F) );
+                       _put_byte( value & 0xFF );
+               }
+               else if( !(value >> (32+5)) ) { // 37 bits into 5 bytes
+                       _put_byte( 0xC0 | ((value >> 32) & 0x1F) );
+                       _put_dword(value & 0xFFFFFFFF);
+               }
+               else {
+                       _put_byte( 0xE0 );      // 64 (actually 68) bits into 9 bytes
+                       _put_dword(value & 0xFFFFFFFF);
+                       _put_dword(value >> 32);
+               }
        }
 
        void _put_double(double value)
@@ -159,7 +198,7 @@ int Bytecode_int_Serialize(const tBC_Function *Function, void *Output, int *Labe
                }
        
                // TODO: Relocations    
-               _put_dword(strIdx);
+               _put_index(strIdx);
        }
 
        for( op = Function->Operations; op; op = op->Next, idx ++ )
@@ -184,7 +223,7 @@ int Bytecode_int_Serialize(const tBC_Function *Function, void *Output, int *Labe
                case BC_OP_JUMPIF:
                case BC_OP_JUMPIFNOT:
                        // TODO: Relocations?
-                       _put_dword( LabelOffsets[op->Content.StringInt.Integer] );
+                       _put_index( LabelOffsets[op->Content.StringInt.Integer] );
                        break;
                // Special case for inline values
                case BC_OP_LOADINT:
@@ -201,7 +240,7 @@ int Bytecode_int_Serialize(const tBC_Function *Function, void *Output, int *Labe
                        if( op->bUseString )
                                _put_string(op->Content.StringInt.String, strlen(op->Content.StringInt.String));
                        if( op->bUseInteger )
-                               _put_dword(op->Content.StringInt.Integer);
+                               _put_index(op->Content.StringInt.Integer);
                        break;
                }
        }
@@ -221,8 +260,9 @@ char *Bytecode_SerialiseFunction(const tBC_Function *Function, int *Length, tStr
        len = Bytecode_int_Serialize(Function, NULL, label_offsets, Strings);
 
        code = malloc(len);
-       
-       Bytecode_int_Serialize(Function, code, label_offsets, Strings);
+
+       // Update length to the correct length (may decrease due to encoding)   
+       len = Bytecode_int_Serialize(Function, code, label_offsets, Strings);
 
        free(label_offsets);
 
@@ -373,12 +413,18 @@ void Bytecode_AppendConstString(tBC_Function *Handle, const void *Data, size_t L
        op->Content.StringInt.String[Length] = 0;
        Bytecode_int_AppendOp(Handle, op);
 }
+void Bytecode_AppendConstNull(tBC_Function *Handle)
+       DEF_BC_NONE(BC_OP_LOADNULL)
 
 // --- Indexing / Scoping
 void Bytecode_AppendElement(tBC_Function *Handle, const char *Name)
        DEF_BC_STR(BC_OP_ELEMENT, Name)
+void Bytecode_AppendSetElement(tBC_Function *Handle, const char *Name)
+       DEF_BC_STR(BC_OP_SETELEMENT, Name)
 void Bytecode_AppendIndex(tBC_Function *Handle)
        DEF_BC_NONE(BC_OP_INDEX)
+void Bytecode_AppendSetIndex(tBC_Function *Handle)
+       DEF_BC_NONE(BC_OP_SETINDEX);
 
 void Bytecode_AppendCreateObj(tBC_Function *Handle, const char *Name, int ArgumentCount)
        DEF_BC_STRINT(BC_OP_CREATEOBJ, Name, ArgumentCount)
@@ -395,6 +441,8 @@ void Bytecode_AppendCast(tBC_Function *Handle, int Type)
        DEF_BC_INT(BC_OP_CAST, Type)
 void Bytecode_AppendDuplicate(tBC_Function *Handle)
        DEF_BC_NONE(BC_OP_DUPSTACK);
+void Bytecode_AppendDelete(tBC_Function *Handle)
+       DEF_BC_NONE(BC_OP_DELSTACK);
 
 // Does some bookeeping to allocate variable slots at compile time
 void Bytecode_AppendEnterContext(tBC_Function *Handle)
@@ -414,7 +462,7 @@ void Bytecode_AppendLeaveContext(tBC_Function *Handle)
        Handle->CurContextDepth --;
        Handle->VariableCount = i;
 
-       DEF_BC_NONE(BC_OP_LEAVECONTEXT)
+       DEF_BC_NONE(BC_OP_LEAVECONTEXT);
 }
 //void Bytecode_AppendImportNamespace(tBC_Function *Handle, const char *Name);
 //     DEF_BC_STRINT(BC_OP_IMPORTNS, Name, 0)

UCC git Repository :: git.ucc.asn.au