bool Parser::ParseProgram(Program* prog) { if (!expectAndEat(spv::MagicNumber)) { return false; } prog->Version = getAndEat(); std::cout << "Version: " << prog->Version << std::endl; prog->GeneratorMagic = getAndEat(); std::cout << "Generator Magic: " << prog->GeneratorMagic << std::endl; prog->IDBound = getAndEat(); std::cout << "ID Bound: " << prog->IDBound << std::endl; prog->InstructionSchema = getAndEat(); std::cout << "Instruction Schema: " << prog->InstructionSchema << std::endl; std::cout << "=================================================" << std::endl; int instructionIndex = 0; prog->NextOp = readInstruction(); do { SOp op = prog->NextOp; std::cout << std::setw(3) << instructionIndex << ": " << writeOp(op); instructionIndex++; if (!end()) { prog->NextOp = readInstruction(); } else { prog->NextOp = SOp{ Op::OpNop, nullptr }; } LUTHandlerMethods[op.Op]((void*)op.Memory, prog); if (prog->InFunction && prog->CurrentFunction->InBlock) { addOp(prog, op); } } while (prog->NextOp.Op != Op::OpNop); return true; }
char* parser(){ initialiseInstructions(); std::ifstream infile("p.subleq"); std::string line; while (std::getline(infile, line)) { if(line.find(':') != std::string::npos)addLabel(line); readInstruction(line); //std::cout << line; } char* m = &program[0]; //char* m = new char[32]{15, 17, -1, 17, -1, -1, 16 ,1 ,-1, 16, 3, -1, 15, 15 ,0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 10, 0}; return m; }
void executeFunction(struct FunctionContext *func, struct RuntimeContext *ctx) { struct Instruction **opcodes = (struct Instruction**)malloc(sizeof(struct Instruction*) * func->opCount); for (int i = 0; i <= func->opCount; i++) { opcodes[i] = (struct Instruction*)malloc(sizeof(struct Instruction)); } for (int i = 0; i < func->opCount; i++) { if (!readInstruction(func->code, &func->localIp, opcodes[i])) break; } func->next = 0; while (executeInstruction(opcodes[func->next], func, ctx)) { } }
void readFunction(Function **table, FILE *in, FunctionFunctionPointer *linkingTable) { uint16_t vti = readUInt16(in); auto *function = static_cast<Function *>(malloc(sizeof(Function))); function->argumentCount = fgetc(in); DEBUG_LOG("*️⃣ Reading function with vti %d and takes %d argument(s)", vti, function->argumentCount); function->objectVariableRecordsCount = readUInt16(in); function->objectVariableRecords = new FunctionObjectVariableRecord[function->objectVariableRecordsCount]; for (unsigned int i = 0; i < function->objectVariableRecordsCount; i++) { function->objectVariableRecords[i].variableIndex = readUInt16(in); function->objectVariableRecords[i].condition = readUInt16(in); function->objectVariableRecords[i].type = static_cast<ObjectVariableType>(readUInt16(in)); function->objectVariableRecords[i].from = readInstruction(in); function->objectVariableRecords[i].to = readInstruction(in); } function->context = static_cast<ContextType>(fgetc(in)); DEBUG_LOG("Read %d object variable records", function->objectVariableRecordsCount); function->frameSize = readUInt16(in); uint16_t native = readUInt16(in); if (native != 0) { DEBUG_LOG("Function has native function"); function->handler = linkingTable[native]; } function->block.instructionCount = readEmojicodeChar(in); function->block.instructions = new EmojicodeInstruction[function->block.instructionCount]; #ifdef DEBUG int numberPrint = 0; #endif for (unsigned int i = 0; i < function->block.instructionCount; i++) { function->block.instructions[i] = readInstruction(in); #ifdef DEBUG if (numberPrint == 0) { printf("%4d: ", i); auto ins = static_cast<Instructions>(function->block.instructions[i]); pinsname(ins); printf(" "); numberPrint = inscount(ins); if (numberPrint == 0) { puts(""); } } else { printf("%d ", function->block.instructions[i]); if (--numberPrint == 0) { puts(""); } } #endif } DEBUG_LOG("Read block with %d coins and %d local variable(s)", function->block.instructionCount, function->frameSize); table[vti] = function; }
Function* readBytecode(FILE *in) { uint8_t version = fgetc(in); if (version != kByteCodeVersion) { error("The bytecode file (bcsv %d) is not compatible with this interpreter (bcsv %d).\n", version, kByteCodeVersion); } DEBUG_LOG("Bytecode version %d", version); const int classCount = readUInt16(in); classTable = new Class*[classCount]; DEBUG_LOG("%d class(es) on the whole", classCount); const int functionCount = readUInt16(in); functionTable = new Function*[functionCount]; DEBUG_LOG("%d function(s) on the whole", functionCount); for (int i = 0, l = fgetc(in); i < l; i++) { DEBUG_LOG("📦 Reading package %d of %d", i + 1, l); readPackage(in); } CL_STRING = classTable[0]; CL_LIST = classTable[1]; CL_DATA = classTable[2]; CL_DICTIONARY = classTable[3]; CL_CLOSURE = classTable[4]; DEBUG_LOG("✅ Read all packages"); uint16_t tableSize = readUInt16(in); protocolDispatchTableTable = new ProtocolDispatchTable[tableSize]; protocolDTTOffset = readUInt16(in); for (uint16_t count = readUInt16(in); count > 0; count--) { DEBUG_LOG("➡️ Still %d value type protocol tables to load", count); auto index = readUInt16(in); readProtocolTable(protocolDispatchTableTable[index - protocolDTTOffset], functionTable, in); } size_t n = readInstruction(in); boxObjectVariableRecordTable = new BoxObjectVariableRecords[n]; for (size_t i = 0; i < n; i++) { boxObjectVariableRecordTable[i].count = readUInt16(in); boxObjectVariableRecordTable[i].records = new ObjectVariableRecord[boxObjectVariableRecordTable[i].count]; for (size_t j = 0; j < boxObjectVariableRecordTable[i].count; j++) { boxObjectVariableRecordTable[i].records[j].variableIndex = readUInt16(in); boxObjectVariableRecordTable[i].records[j].condition = readUInt16(in); boxObjectVariableRecordTable[i].records[j].type = static_cast<ObjectVariableType>(readUInt16(in)); } } stringPoolCount = readUInt16(in); DEBUG_LOG("Reading string pool with %d strings", stringPoolCount); stringPool = new Object*[stringPoolCount]; for (int i = 0; i < stringPoolCount; i++) { Object *o = newObject(CL_STRING); auto *string = o->val<String>(); string->length = readUInt16(in); string->charactersObject = newArray(string->length * sizeof(EmojicodeChar)); for (int j = 0; j < string->length; j++) { string->characters()[j] = readEmojicodeChar(in); } stringPool[i] = o; } DEBUG_LOG("✅ Program ready for execution"); return functionTable[0]; }