<commit_before>//
//  Compiler.cpp
//  Addie
//
//  Created by <NAME> on 24/04/16.
//  Copyright © 2016 <NAME>, Starcounter AB.
//

#include "VM.hpp"
#include "Compiler.hpp"
#include <sstream>

using namespace Addie;
using namespace Addie::Internals;

/*
// Find all declared variables and their default values
Metaframe* CompileLetFrame( Isolate* isolate, Namespace* ns, Metaframe* mf, VALUE form ) {

    auto newMf = MALLOC_HEAP(Metaframe); // TODO! GC
    new (newMf) Metaframe();

    if (mf != NULL ) {
        // We nest all the frames for lexical scoping. Any variable not bound locally, will
        // be added as an implicit argument. These implicit arguments allows closures
        // to refer to variables in their parent scope.
        newMf->Parent = mf;
    }

    int cnt = form.Count();
    int varCount = 0;
    for (int t=0;t<cnt;t += 2) {
        varCount++;
        Symbol variableName = form.GetAt(t).SymbolId;
        std::cout << "Found r[" << varCount << "] as local variable " << form.GetAt(t).Print() << "=" << form.GetAt(t+1).Print() << "\n";
        //mf->LocalsAndArguments.push_back(Variable(variableName,form.GetAt(t+1)));
        newMf->Registers.push_back(variableName);
        uint8_t regno = newMf->Registers.size()-1;
        newMf->Bindings[variableName] = Binding(newMf,regno);
    }
    return newMf;
}

Metaframe* CompileFrames( Isolate* isolate, Namespace* ns, Metaframe* mf, VALUE form ) {
    
    
    if (form.Type == TList && form.ListStyle == QParenthesis) {
        VALUE fst = form.First();
        if (fst.IsSymbol()) {
            Symbol verb = fst.SymbolId;
            switch (verb) {
                case SymLetStar:
                    return CompileLetFrame( isolate, ns, mf, form.Rest().First() );
                    break;
            }
        }
        mf->Body = form;
    }
    return mf;
}

 */


byte* CompilePrototype( Isolate* isolate, byte* p, VALUE form ) {
    
    byte* start = p; //(byte*)isolate->NextOnConstant;
    //byte* p = (byte*)start;
    
    Instruction* code;
    Instruction* c;
    VALUE* registers;
    VALUE* r;
    int uninitatedRegisters;
    
    CompilationUnit* header = (CompilationUnit*)p;
    p += sizeof(CompilationUnit);
    
    registers = r = (VALUE*)p;
    
    std::cout << "\nCompiling (print (+ 10 20))\n";
    
    *((VALUE*)r++) = NIL();             // R0 retval
    *((VALUE*)r++) = SYMBOL(SymPlus);   // R1
    *((VALUE*)r++) = SYMBOL(SymPrint);  // R2
    *((VALUE*)r++) = INTEGER(10);       // R3
    *((VALUE*)r++) = INTEGER(20);       // R4
    
    // R5
    
    code = c = (Instruction*)r;
    
    //            *(c++) = Instruction(SET_REGISTER_WINDOW);     // CODE START 0=SymPlus 1=10 2=20
    //            uint64_t temp1 = (uint64_t)registers;
    
    //            *((VALUE*)c) = INTEGER(temp1);
    //            uint64_t temp2 = (uint64_t)(*((VALUE*)c)).Integer;
    
    //            if (temp1 != temp2 ) {
    //                std::cout << temp1 << "!=" << temp2 << "\n";
    //            }
    
    
    //            std::cout << (*((VALUE*)c)).Integer;
    //            c += sizeof(INTEGER)/sizeof(Instruction);
    *(c++) = OpCall(1,3,4);     // CODE START 0=SymPlus 1=10 2=20
    //*(c++) = OpMove(0,5);       // retval -> intermediate1
    *(c++) = Instruction(EXIT_WITH_CONTINUATION);
    *(c++) = OpCall(2,0);       // 3=Print 5=internadiate1
    *(c++) = Instruction(END);       // 3=Print 5=internadiate1
    
    //*(c++) = Instruction(JMP_IF_TRUE,(int32_t)-1);       // 3=Print 5=internadiate1
    uninitatedRegisters = 1;
    p = (byte*)(c-1);
    
    header->SizeOfInitializedRegisters = ((byte*)code) - ((byte*)registers);
    header->SizeOfRegisters = header->SizeOfInitializedRegisters + uninitatedRegisters * sizeof(VALUE);
    
    //isolate->NextOnConstant = (u64)p; // Mark the memory as used
    
    return p;
    
    //return header;
}



byte* Compile2( Isolate* isolate, byte* p, VALUE form ) {

    //byte* p = (byte*)isolate->NextOnConstant;
    
    Instruction* code;
    Instruction* c;
    VALUE* registers;
    VALUE* r;
    int uninitatedRegisters;
    
    CompilationUnit* header = (CompilationUnit*)p;
    p += sizeof(CompilationUnit);
    
    registers = r = (VALUE*)p;
    
    if (form.Type == TList && form.ListStyle == QParenthesis) {
        // Function calling form (...)
        VALUE fst = form.First();
        if (fst.IsSymbol()) {
            r[0] = NIL();             // R0 retval
            r[1] = form.First();    // R1
            uninitatedRegisters = 0;
     //       r += mf->Bindings.size();
            code = c = (Instruction*)r;
            *(c++) = OpCall(1);
            *(c++) = Instruction(END);
            p = (byte*)c;
            goto end;
        } else {
            return CompilePrototype( isolate, p, form );
            throw std::runtime_error("Cannot compile");
        }
    }
    else {
    
        *((VALUE*)r++) = form;             // R0 retval
        uninitatedRegisters = 0;
    
        code = c = (Instruction*)r;
        *(c++) = Instruction(END);       // 3=Print 5=internadiate1
        p = (byte*)c;
    }
    
end:
    header->SizeOfInitializedRegisters = ((byte*)code) - ((byte*)registers);
    header->SizeOfRegisters = header->SizeOfInitializedRegisters + uninitatedRegisters * sizeof(VALUE);
    
    //isolate->ReportConstantWrite( (uintptr_t)p ); // Mark the memory as used
    
    return p;
}

byte* CompileConstant( Isolate* isolate, byte* p, VALUE form ) {
    
    //byte* p = (byte*)isolate->NextOnConstant;
    
    Instruction* code, *c;
    VALUE* registers, *r;
    
    CompilationUnit* header = (CompilationUnit*)p;
    p += sizeof(CompilationUnit);
    
    registers = r = (VALUE*)p;
    
        *((VALUE*)r++) = form;             // R0 retval
    
        code = c = (Instruction*)r;
        *(c++) = Instruction(END);       // 3=Print 5=intermediate1
        p = (byte*)c;
    
    header->SizeOfInitializedRegisters = ((byte*)code) - ((byte*)registers);
    header->SizeOfRegisters = header->SizeOfInitializedRegisters;
    
    //isolate->ReportConstantWrite( (uintptr_t)p ); // Mark the memory as used
    
    return p;
}

byte* CompileSymbol( Isolate* isolate, byte* bytecode, Metaframe* mf, VALUE symbol ) {
    throw std::runtime_error("Not Implemented");
}

byte* CompileFunctionCall( Isolate* isolate, byte* bytecode, Metaframe* mf, VALUE form ) {
    throw std::runtime_error("Not Implemented");
}

byte* CompileList( Isolate* isolate, byte* bytecode, Metaframe* mf, VALUE form ) {
    // TODO! We don't know that this list is a constant. Temporary code.
    return CompileConstant(isolate, bytecode, form);
}
<commit_msg>Compilation of constants returned wrong pointer<commit_after>//
//  Compiler.cpp
//  Addie
//
//  Created by <NAME> on 24/04/16.
//  Copyright © 2016 <NAME>, Starcounter AB.
//

#include "VM.hpp"
#include "Compiler.hpp"
#include <sstream>

using namespace Addie;
using namespace Addie::Internals;

/*
// Find all declared variables and their default values
Metaframe* CompileLetFrame( Isolate* isolate, Namespace* ns, Metaframe* mf, VALUE form ) {

    auto newMf = MALLOC_HEAP(Metaframe); // TODO! GC
    new (newMf) Metaframe();

    if (mf != NULL ) {
        // We nest all the frames for lexical scoping. Any variable not bound locally, will
        // be added as an implicit argument. These implicit arguments allows closures
        // to refer to variables in their parent scope.
        newMf->Parent = mf;
    }

    int cnt = form.Count();
    int varCount = 0;
    for (int t=0;t<cnt;t += 2) {
        varCount++;
        Symbol variableName = form.GetAt(t).SymbolId;
        std::cout << "Found r[" << varCount << "] as local variable " << form.GetAt(t).Print() << "=" << form.GetAt(t+1).Print() << "\n";
        //mf->LocalsAndArguments.push_back(Variable(variableName,form.GetAt(t+1)));
        newMf->Registers.push_back(variableName);
        uint8_t regno = newMf->Registers.size()-1;
        newMf->Bindings[variableName] = Binding(newMf,regno);
    }
    return newMf;
}

Metaframe* CompileFrames( Isolate* isolate, Namespace* ns, Metaframe* mf, VALUE form ) {
    
    
    if (form.Type == TList && form.ListStyle == QParenthesis) {
        VALUE fst = form.First();
        if (fst.IsSymbol()) {
            Symbol verb = fst.SymbolId;
            switch (verb) {
                case SymLetStar:
                    return CompileLetFrame( isolate, ns, mf, form.Rest().First() );
                    break;
            }
        }
        mf->Body = form;
    }
    return mf;
}

 */


byte* CompilePrototype( Isolate* isolate, byte* p, VALUE form ) {
    
    byte* start = p; //(byte*)isolate->NextOnConstant;
    //byte* p = (byte*)start;
    
    Instruction* code;
    Instruction* c;
    VALUE* registers;
    VALUE* r;
    int uninitatedRegisters;
    
    CompilationUnit* header = (CompilationUnit*)p;
    p += sizeof(CompilationUnit);
    
    registers = r = (VALUE*)p;
    
    std::cout << "\nCompiling (print (+ 10 20))\n";
    
    *((VALUE*)r++) = NIL();             // R0 retval
    *((VALUE*)r++) = SYMBOL(SymPlus);   // R1
    *((VALUE*)r++) = SYMBOL(SymPrint);  // R2
    *((VALUE*)r++) = INTEGER(10);       // R3
    *((VALUE*)r++) = INTEGER(20);       // R4
    
    // R5
    
    code = c = (Instruction*)r;
    
    //            *(c++) = Instruction(SET_REGISTER_WINDOW);     // CODE START 0=SymPlus 1=10 2=20
    //            uint64_t temp1 = (uint64_t)registers;
    
    //            *((VALUE*)c) = INTEGER(temp1);
    //            uint64_t temp2 = (uint64_t)(*((VALUE*)c)).Integer;
    
    //            if (temp1 != temp2 ) {
    //                std::cout << temp1 << "!=" << temp2 << "\n";
    //            }
    
    
    //            std::cout << (*((VALUE*)c)).Integer;
    //            c += sizeof(INTEGER)/sizeof(Instruction);
    *(c++) = OpCall(1,3,4);     // CODE START 0=SymPlus 1=10 2=20
    //*(c++) = OpMove(0,5);       // retval -> intermediate1
    *(c++) = Instruction(EXIT_WITH_CONTINUATION);
    *(c++) = OpCall(2,0);       // 3=Print 5=internadiate1
    *(c++) = Instruction(END);       // 3=Print 5=internadiate1
    
    //*(c++) = Instruction(JMP_IF_TRUE,(int32_t)-1);       // 3=Print 5=internadiate1
    uninitatedRegisters = 1;
    p = (byte*)(c-1);
    
    header->SizeOfInitializedRegisters = ((byte*)code) - ((byte*)registers);
    header->SizeOfRegisters = header->SizeOfInitializedRegisters + uninitatedRegisters * sizeof(VALUE);
    
    //isolate->NextOnConstant = (u64)p; // Mark the memory as used
    
    return p;
    
    //return header;
}



byte* Compile2( Isolate* isolate, byte* p, VALUE form ) {

    //byte* p = (byte*)isolate->NextOnConstant;
    
    Instruction* code;
    Instruction* c;
    VALUE* registers;
    VALUE* r;
    int uninitatedRegisters;
    
    CompilationUnit* header = (CompilationUnit*)p;
    p += sizeof(CompilationUnit);
    
    registers = r = (VALUE*)p;
    
    if (form.Type == TList && form.ListStyle == QParenthesis) {
        // Function calling form (...)
        VALUE fst = form.First();
        if (fst.IsSymbol()) {
            r[0] = NIL();             // R0 retval
            r[1] = form.First();    // R1
            uninitatedRegisters = 0;
     //       r += mf->Bindings.size();
            code = c = (Instruction*)r;
            *(c++) = OpCall(1);
            *(c++) = Instruction(END);
            p = (byte*)c;
            goto end;
        } else {
            return CompilePrototype( isolate, p, form );
            throw std::runtime_error("Cannot compile");
        }
    }
    else {
    
        *((VALUE*)r++) = form;             // R0 retval
        uninitatedRegisters = 0;
    
        code = c = (Instruction*)r;
        *(c++) = Instruction(END);       // 3=Print 5=internadiate1
        p = (byte*)c;
    }
    
end:
    header->SizeOfInitializedRegisters = ((byte*)code) - ((byte*)registers);
    header->SizeOfRegisters = header->SizeOfInitializedRegisters + uninitatedRegisters * sizeof(VALUE);
    
    //isolate->ReportConstantWrite( (uintptr_t)p ); // Mark the memory as used
    
    return p;
}

byte* CompileConstant( Isolate* isolate, byte* p, VALUE form ) {
    
    //byte* p = (byte*)isolate->NextOnConstant;
    
    Instruction* code, *c;
    VALUE* registers, *r;
    
    CompilationUnit* header = (CompilationUnit*)p;
    p += sizeof(CompilationUnit);
    
    registers = r = (VALUE*)p;
    
        *((VALUE*)r++) = form;             // R0 retval
    
        code = c = (Instruction*)r;
        *(c++) = Instruction(END);       // 3=Print 5=intermediate1
        p = (byte*)(c-1);
    
    header->SizeOfInitializedRegisters = ((byte*)code) - ((byte*)registers);
    header->SizeOfRegisters = header->SizeOfInitializedRegisters;
    
    //isolate->ReportConstantWrite( (uintptr_t)p ); // Mark the memory as used
    
    return p;
}

byte* CompileSymbol( Isolate* isolate, byte* bytecode, Metaframe* mf, VALUE symbol ) {
    throw std::runtime_error("Not Implemented");
}

byte* CompileFunctionCall( Isolate* isolate, byte* bytecode, Metaframe* mf, VALUE form ) {
    throw std::runtime_error("Not Implemented");
}

byte* CompileList( Isolate* isolate, byte* bytecode, Metaframe* mf, VALUE form ) {
    // TODO! We don't know that this list is a constant. Temporary code.
    return CompileConstant(isolate, bytecode, form);
}

{'fraction_non_alphanumeric': 0.15605427974947808, 'fraction_numerical': 0.014986579182821353, 'mean_word_length': 2.1731724627395317, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 55, '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': '5773417', 'n_tokens_mistral': 4524, 'n_tokens_neox': 4353, 'n_words': 1009}