<commit_before>/*
 Copyright (c) 2012, The Saffire Group
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
     * Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
     * Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
     * Neither the name of the <organization> nor the
       names of its contributors may be used to endorse or promote products
       derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/stat.h>
#include <string.h>
#include "compiler/bytecode.h"
#include "compiler/ast.h"
#include "general/dll.h"
#include "general/smm.h"
#include "version.h"


/**
 * Check if a constant already is present inside the bytecode structure
 */
static int _bytecode_constant_exists(t_bytecode *bc, int type, void *value) {
    t_dll_element *e = DLL_HEAD(bc->constant_dll);
    long l;

    while (e) {
        t_bytecode_constant *constant = e->data;
        if (constant->hdr.type == type) {
            switch (constant->hdr.type) {
                case BYTECODE_CONST_NULL :
                    return 1;
                    break;
                case BYTECODE_CONST_STRING :
                    if (strcmp(constant->data.s, value) == 0) return 1;
                    break;
                case BYTECODE_CONST_NUMERICAL :
                    printf("Checking %d against %d\n", constant->data.l, (long)value);
                    if (constant->data.l == (long)value) return 1;
                    break;
                case BYTECODE_CONST_BOOLEAN :
                    if (constant->data.l == (long)value) return 1;
                    break;
                case BYTECODE_CONST_REGEX :
                    break;
            }
        }
        e = DLL_NEXT(e);
    }

    return 0;
}

/**
 * Parse the AST and add all constants
 */
static void _bytecode_parse_constants(t_ast_element *p, t_bytecode *bc) {
    t_bytecode_constant *constant = NULL;
    if (!p) return;

    switch (p->type) {
        case typeString :
            printf("STRING: '%s'\n", p->string.value);
            if (! _bytecode_constant_exists(bc, BYTECODE_CONST_STRING, p->string.value)) {
                constant = (t_bytecode_constant *)smm_malloc(sizeof(t_bytecode_constant));
                constant->hdr.type = BYTECODE_CONST_STRING;
                constant->hdr.length = strlen(p->string.value);
                constant->data.s = smm_strdup(p->string.value);
                dll_append(bc->constant_dll, constant);
            }
            break;
        case typeNumerical :
            if (! _bytecode_constant_exists(bc, BYTECODE_CONST_NUMERICAL, (void *)p->numerical.value)) {
                constant = (t_bytecode_constant *)smm_malloc(sizeof(t_bytecode_constant));
                constant->hdr.type = BYTECODE_CONST_NUMERICAL;
                constant->hdr.length = sizeof(long);
                constant->data.l = p->numerical.value;
                dll_append(bc->constant_dll, constant);
            }
            break;
        case typeIdentifier :
            if (p->string.value[0] == '$') break;
            printf("IDENT: '%s'\n", p->string.value);
            if (! _bytecode_constant_exists(bc, BYTECODE_CONST_STRING, p->identifier.name)) {
                constant = (t_bytecode_constant *)smm_malloc(sizeof(t_bytecode_constant));
                constant->hdr.type = BYTECODE_CONST_STRING;
                constant->hdr.length = strlen(p->identifier.name);
                constant->data.s = smm_strdup(p->identifier.name);
                dll_append(bc->constant_dll, constant);
            }
            break;
        case typeNull :
            if (! _bytecode_constant_exists(bc, BYTECODE_CONST_NULL, NULL)) {
                constant = (t_bytecode_constant *)smm_malloc(sizeof(t_bytecode_constant));
                constant->hdr.type = BYTECODE_CONST_NULL;
                constant->hdr.length = 0;
                constant->data.l = 0;
                dll_append(bc->constant_dll, constant);
            }
            break;
        case typeOpr :
            // Plot all the operands
            for (int i=0; i!=p->opr.nops; i++) {
                _bytecode_parse_constants(p->opr.ops[i], bc);
            }
            break;
        case typeInterface :
            _bytecode_parse_constants(p->interface.implements, bc);
            _bytecode_parse_constants(p->interface.body, bc);
            break;
        case typeClass :
            _bytecode_parse_constants(p->class.extends, bc);
            _bytecode_parse_constants(p->class.implements, bc);
            _bytecode_parse_constants(p->class.body, bc);
            break;
        case typeMethod:
            _bytecode_parse_constants(p->method.arguments, bc);
            _bytecode_parse_constants(p->method.body, bc);
            break;
        default :
            printf("Unknown type!");
            exit(1);
            break;
    }
}


/**
 * Returns the length of byte from a (filled) bytecode structure.
 */
static long bytecode_calculate_length(t_bytecode *bc) {
    t_dll_element *e;
    long len = sizeof(t_bytecode_header);

    // Calculate and add constant lengths
    if (bc->constant_dll) {
        e = DLL_HEAD(bc->constant_dll);
        for (int i=0; i!=bc->constant_dll->size; i++) {
            t_bytecode_constant *constant = e->data;
            len += sizeof(t_bytecode_constant_header) + constant->hdr.length;
            e = DLL_NEXT(e);
        }
    }

    // Calculate and add class lengths
    if (bc->class_dll) {
        e = DLL_HEAD(bc->class_dll);
        for (int i=0; i!=bc->class_dll->size; i++) {
            t_bytecode_constant *constant = e->data;
            len += sizeof(t_bytecode_constant_header) + constant->hdr.length;<commit_msg>Removed temp code<commit_after>/*
 Copyright (c) 2012, The Saffire Group
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
     * Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
     * Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
     * Neither the name of the <organization> nor the
       names of its contributors may be used to endorse or promote products
       derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/stat.h>
#include <string.h>
#include "compiler/bytecode.h"
#include "compiler/ast.h"
#include "general/dll.h"
#include "general/smm.h"
#include "version.h"


/**
 * Check if a constant already is present inside the bytecode structure
 */
static int _bytecode_constant_exists(t_bytecode *bc, int type, void *value) {
    t_dll_element *e = DLL_HEAD(bc->constant_dll);
    long l;

    while (e) {
        t_bytecode_constant *constant = e->data;
        if (constant->hdr.type == type) {
            switch (constant->hdr.type) {
                case BYTECODE_CONST_NULL :
                    return 1;
                    break;
                case BYTECODE_CONST_STRING :
                    if (strcmp(constant->data.s, value) == 0) return 1;
                    break;
                case BYTECODE_CONST_NUMERICAL :
                    printf("Checking %d against %d\n", constant->data.l, (long)value);
                    if (constant->data.l == (long)value) return 1;
                    break;
                case BYTECODE_CONST_BOOLEAN :
                    if (constant->data.l == (long)value) return 1;
                    break;
                case BYTECODE_CONST_REGEX :
                    break;
            }
        }
        e = DLL_NEXT(e);
    }

    return 0;
}

/**
 * Parse the AST and add all constants
 */
static void _bytecode_parse_constants(t_ast_element *p, t_bytecode *bc) {
    t_bytecode_constant *constant = NULL;
    if (!p) return;

    switch (p->type) {
//        case typeAstString :
//            printf("STRING: '%s'\n", p->string.value);
//            if (! _bytecode_constant_exists(bc, BYTECODE_CONST_STRING, p->string.value)) {
//                constant = (t_bytecode_constant *)smm_malloc(sizeof(t_bytecode_constant));
//                constant->hdr.type = BYTECODE_CONST_STRING;
//                constant->hdr.length = strlen(p->string.value);
//                constant->data.s = smm_strdup(p->string.value);
//                dll_append(bc->constant_dll, constant);
//            }
//            break;
//        case typeAstNumerical :
//            if (! _bytecode_constant_exists(bc, BYTECODE_CONST_NUMERICAL, (void *)p->numerical.value)) {
//                constant = (t_bytecode_constant *)smm_malloc(sizeof(t_bytecode_constant));
//                constant->hdr.type = BYTECODE_CONST_NUMERICAL;
//                constant->hdr.length = sizeof(long);
//                constant->data.l = p->numerical.value;
//                dll_append(bc->constant_dll, constant);
//            }
//            break;
//        case typeAstIdentifier :
//            if (p->string.value[0] == '$') break;
//            printf("IDENT: '%s'\n", p->string.value);
//            if (! _bytecode_constant_exists(bc, BYTECODE_CONST_STRING, p->identifier.name)) {
//                constant = (t_bytecode_constant *)smm_malloc(sizeof(t_bytecode_constant));
//                constant->hdr.type = BYTECODE_CONST_STRING;
//                constant->hdr.length = strlen(p->identifier.name);
//                constant->data.s = smm_strdup(p->identifier.name);
//                dll_append(bc->constant_dll, constant);
//            }
//            break;
//        case typeNull :
//            if (! _bytecode_constant_exists(bc, BYTECODE_CONST_NULL, NULL)) {
//                constant = (t_bytecode_constant *)smm_malloc(sizeof(t_bytecode_constant));
//                constant->hdr.type = BYTECODE_CONST_NULL;
//                constant->hdr.length = 0;
//                constant->data.l = 0;
//                dll_append(bc->constant_dll, constant);
//            }
//            break;
//        case typeOpr :
//            // Plot all the operands
//            for (int i=0; i!=p->opr.nops; i++) {
//                _bytecode_parse_constants(p->opr.ops[i], bc);
//            }
//            break;
//        case typeInterface :
//            _bytecode_parse_constants(p->interface.implements, bc);
//            _bytecode_parse_constants(p->interface.body, bc);
//            break;
//        case typeClass :
//            _bytecode_parse_constants(p->class.extends, bc);
//            _bytecode_parse_constants(p->class.implements, bc);
//            _bytecode_parse_constants(p->class.body, bc);
//            break;
//        case typeMethod:
//            _bytecode_parse_constants(p->method.arguments, bc);
//            _bytecode_parse_constants(p->method.body, bc);
//            break;
        default :
            printf("Unknown type!");
            exit(1);
            break;
    }
}


/**
 * Returns the length of byte from a (filled) bytecode structure.
 */
static long bytecode_calculate_length(t_bytecode *bc) {
    t_dll_element *e;
    long len = sizeof(t_bytecode_header);

    // Calculate and add constant lengths
    if (bc->constant_dll) {
        e = DLL_HEAD(bc->constant_dll);
        for (int i=0; i!=bc->constant_dll->size; i++) {
            t_bytecode_constant *constant = e->data;
            len += sizeof(t_bytecode_constant_header) + constant->hdr.length;
            e = DLL_NEXT(e);
        }
    }

    // Calculate and add class lengths
    if (bc->class_dll) {
        e = DLL_HEAD(bc->class_dll);
        for (int i=0; i!=bc->class_dll->size; i++) {
            t_bytecode_constant *constant = e->data;
            len += sizeof(t_bytecode_constant_header) + constant->hdr.length;
{'fraction_non_alphanumeric': 0.10428602509246501, 'fraction_numerical': 0.0024657335557328303, 'mean_word_length': 2.15922107674685, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2724234', 'n_tokens_mistral': 4036, 'n_tokens_neox': 3839, 'n_words': 1037}