Bytecode: move CodeLoad class out of the preprocessor

silicon
Sébastien Villemot 2023-02-21 16:02:21 -05:00
parent 845eac99d3
commit 24b2976ac2
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
3 changed files with 344 additions and 4 deletions

View File

@ -25,13 +25,355 @@
#include <map>
#include <optional>
#define BYTECODE_MEX
#include "Bytecode.hh"
#include "ErrorHandling.hh"
#include "BasicSymbolTable.hh"
#include <dynmex.h>
using instructions_list_t = vector<BytecodeInstruction *>;
using it_code_type = instructions_list_t::const_iterator;
class CodeLoad
{
private:
char *code;
int nb_blocks;
vector<size_t> begin_block;
public:
int
get_block_number() const
{
return nb_blocks;
}
size_t
get_begin_block(int block) const
{
return begin_block[block];
}
void *
get_current_code() const
{
return code;
}
instructions_list_t
get_op_code(const filesystem::path &codfile)
{
instructions_list_t tags_liste;
ifstream CompiledCode;
streamoff Code_Size;
CompiledCode.open(codfile, ios::in | ios::binary| ios::ate);
if (!CompiledCode.is_open())
return tags_liste;
Code_Size = CompiledCode.tellg();
CompiledCode.seekg(ios::beg);
code = static_cast<char *>(mxMalloc(Code_Size));
CompiledCode.seekg(0);
CompiledCode.read(reinterpret_cast<char *>(code), Code_Size);
CompiledCode.close();
nb_blocks = 0;
bool done = false;
int instruction = 0;
while (!done)
{
BytecodeInstruction *instr {reinterpret_cast<BytecodeInstruction *>(code)};
switch (*reinterpret_cast<Tags *>(code))
{
case Tags::FLDZ:
# ifdef DEBUGL
mexPrintf("FLDZ = %d size = %d\n", Tags::FLDZ, sizeof(FLDZ_));
# endif
tags_liste.push_back(instr);
code += sizeof(FLDZ_);
break;
case Tags::FEND:
# ifdef DEBUGL
mexPrintf("FEND\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FEND_);
done = true;
break;
case Tags::FENDBLOCK:
# ifdef DEBUGL
mexPrintf("FENDBLOCK\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FENDBLOCK_);
break;
case Tags::FENDEQU:
# ifdef DEBUGL
mexPrintf("FENDEQU\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FENDEQU_);
break;
case Tags::FDIMT:
# ifdef DEBUGL
mexPrintf("FDIMT = %d size = %d\n", Tags::FDIMT, sizeof(FDIMT_));
# endif
tags_liste.push_back(instr);
code += sizeof(FDIMT_);
break;
case Tags::FDIMST:
# ifdef DEBUGL
mexPrintf("FDIMST\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FDIMST_);
break;
case Tags::FNUMEXPR:
# ifdef DEBUGL
mexPrintf("FNUMEXPR\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FNUMEXPR_);
break;
case Tags::FLDC:
# ifdef DEBUGL
mexPrintf("FLDC\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FLDC_);
break;
case Tags::FLDU:
# ifdef DEBUGL
mexPrintf("FLDU\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FLDU_);
break;
case Tags::FLDSU:
# ifdef DEBUGL
mexPrintf("FLDSU\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FLDSU_);
break;
case Tags::FLDR:
# ifdef DEBUGL
mexPrintf("FLDR\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FLDR_);
break;
case Tags::FLDT:
# ifdef DEBUGL
mexPrintf("FLDT\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FLDT_);
break;
case Tags::FLDST:
# ifdef DEBUGL
mexPrintf("FLDST\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FLDST_);
break;
case Tags::FSTPT:
# ifdef DEBUGL
mexPrintf("FSTPT = %d size = %d\n", Tags::FSTPT, sizeof(FSTPT_));
# endif
tags_liste.push_back(instr);
code += sizeof(FSTPT_);
break;
case Tags::FSTPST:
# ifdef DEBUGL
mexPrintf("FSTPST\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FSTPST_);
break;
case Tags::FSTPR:
# ifdef DEBUGL
mexPrintf("FSTPR\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FSTPR_);
break;
case Tags::FSTPU:
# ifdef DEBUGL
mexPrintf("FSTPU\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FSTPU_);
break;
case Tags::FSTPSU:
# ifdef DEBUGL
mexPrintf("FSTPSU\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FSTPSU_);
break;
case Tags::FSTPG:
# ifdef DEBUGL
mexPrintf("FSTPG\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FSTPG_);
break;
case Tags::FSTPG2:
# ifdef DEBUGL
mexPrintf("FSTPG2\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FSTPG2_);
break;
case Tags::FSTPG3:
# ifdef DEBUGL
mexPrintf("FSTPG3\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FSTPG3_);
break;
case Tags::FUNARY:
# ifdef DEBUGL
mexPrintf("FUNARY\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FUNARY_);
break;
case Tags::FBINARY:
# ifdef DEBUGL
mexPrintf("FBINARY\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FBINARY_);
break;
case Tags::FTRINARY:
# ifdef DEBUGL
mexPrintf("FTRINARY\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FTRINARY_);
break;
case Tags::FLDVS:
# ifdef DEBUGL
mexPrintf("FLDVS\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FLDVS_);
break;
case Tags::FLDSV:
# ifdef DEBUGL
mexPrintf("FLDSV\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FLDSV_);
break;
case Tags::FSTPSV:
# ifdef DEBUGL
mexPrintf("FSTPSV\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FSTPSV_);
break;
case Tags::FLDV:
# ifdef DEBUGL
mexPrintf("FLDV\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FLDV_);
break;
case Tags::FSTPV:
# ifdef DEBUGL
mexPrintf("FSTPV\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FSTPV_);
break;
case Tags::FBEGINBLOCK:
# ifdef DEBUGL
mexPrintf("FBEGINBLOCK\n");
# endif
{
auto *fbegin_block = new FBEGINBLOCK_{code};
begin_block.push_back(tags_liste.size());
tags_liste.push_back(fbegin_block);
nb_blocks++;
}
break;
case Tags::FJMPIFEVAL:
# ifdef DEBUGL
mexPrintf("FJMPIFEVAL\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FJMPIFEVAL_);
break;
case Tags::FJMP:
# ifdef DEBUGL
mexPrintf("FJMP\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FJMP_);
break;
case Tags::FCALL:
{
# ifdef DEBUGL
mexPrintf("FCALL\n");
# endif
auto *fcall = new FCALL_{code};
tags_liste.push_back(fcall);
# ifdef DEBUGL
mexPrintf("FCALL finish\n"); mexEvalString("drawnow;");
mexPrintf("-- *code=%d\n", *code); mexEvalString("drawnow;");
# endif
}
break;
case Tags::FLDTEF:
# ifdef DEBUGL
mexPrintf("FLDTEF\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FLDTEF_);
break;
case Tags::FSTPTEF:
# ifdef DEBUGL
mexPrintf("FSTPTEF\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FSTPTEF_);
break;
case Tags::FLDTEFD:
# ifdef DEBUGL
mexPrintf("FLDTEFD\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FLDTEFD_);
break;
case Tags::FSTPTEFD:
# ifdef DEBUGL
mexPrintf("FSTPTEFD\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FSTPTEFD_);
break;
case Tags::FLDTEFDD:
# ifdef DEBUGL
mexPrintf("FLDTEFDD\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FLDTEFDD_);
break;
case Tags::FSTPTEFDD:
# ifdef DEBUGL
mexPrintf("FSTPTEFDD\n");
# endif
tags_liste.push_back(instr);
code += sizeof(FSTPTEFDD_);
break;
default:
mexPrintf("Unknown Tag value=%d code=%x\n", *code, code);
done = true;
}
instruction++;
}
return tags_liste;
}
};
class Evaluate
{
private:

View File

@ -28,8 +28,6 @@
#include "ErrorHandling.hh"
#include "SparseMatrix.hh"
#define BYTECODE_MEX
#include "Bytecode.hh"
using namespace std;

@ -1 +1 @@
Subproject commit 114d8eadfb3dee24ab65f291253076c4d0ea83c3
Subproject commit 008a80910e63e0a06143c7b6c9ac3110e67ee12a