/************************************************************************* * loadLabel * Load (and validate) a label operand. *************************************************************************/ void loadLabel(uINSTRUCTION *instr, int opNum) { int lab; if (codeFlag == valid) { processWhitespace(); switch (yytok) { case tLabel: sscanf(yytext, "%*[Ll]%d", &lab); instr->Operand[opNum].Mode = mLabel; setOperandValue(&(instr->Operand[opNum]), lab); validateLabel(lab); break; case tEOLN: case tEOF : strcpy(loadMessage, "MISSING LABEL OPERAND"); codeFlag = invalidForm; break; default: sprintf(loadMessage, "FOUND %s, EXPECTING LABEL OPERAND", yytext); codeFlag = invalidForm; } } }
void FreeFormatReader::parse(string line) { autalities::dos2unix(line); smatch sm; if (regex_match(line, sm, FREEFORMAT_REGEX0)) { label = sm[2]; operation = sm[3]; } else if (regex_match(line, sm, FREEFORMAT_REGEX1)) { label = sm[2]; operation = sm[3]; operand = sm[4]; autalities::removeTrailingSpaces(operand); operand = autalities::removeLeadingSpaces(operand); } else { valid = false; addToErrorMessage("can not match line with any valid format"); } OpTable *opTable = new OpTable(); DirectivseTable *dir = new DirectivseTable(); label = autalities::tolow(label); if (opTable->hasOperation(label) || dir->contains(label)) { if (operand.empty()) { operand = operation; } else if (!operation.empty()) { operand = operation + " " + operand; } operation = label; label = ""; } validateLabel(); validateOperation(); args = OperandValidator::getOperands(operand); cout << "\"" << label << "\", \"" << operation << "\", \"" << operand << "\"" << endl; }
/************************************************************************* * uLoad * "Parse" a uCode source program (using flex to scan it). *************************************************************************/ int uLoad(uINSTRUCTION code[], int label[]) { uINSTRUCTION *currInstr; int codeLine = 0; int i, lab; do { currInstr = &(code[codeLine]); clearInstruction(currInstr); codeFlag = valid; loadOpcode(currInstr); if (codeFlag == valid) { switch (yytok) { case tHLT: break; case tRD: case tRDF: case tRDS: loadTarget(currInstr, 0); break; case tWRT: case tWRTLN: loadSource(currInstr, 0); break; case tWRTS: case tWRTLNS: break; case tMOV : case tNEG : case tNEGF : case tCASTI: case tCASTF: loadSource(currInstr, 0); loadTarget(currInstr, 1); break; case tADD : case tSUB : case tMUL : case tDIV : case tMOD : case tADDF : case tSUBF : case tMULF : case tDIVF : loadSource(currInstr, 0); loadSource(currInstr, 1); loadTarget(currInstr, 2); break; case tPUSH: loadSource(currInstr, 0); break; case tPOP : loadTarget(currInstr, 0); break; case tNEGS: case tADDS: case tSUBS: case tMULS: case tDIVS: case tMODS: case tNEGSF: case tADDSF: case tSUBSF: case tMULSF: case tDIVSF: case tCASTSI: case tCASTSF: break; /* Label instruction must be handled at load time */ case tLAB : { sscanf(yytext, "%*[Ll]%d:", &lab); currInstr->Operand[0].Mode = mLabel; setOperandValue(&(currInstr->Operand[0]), lab); validateLabel(lab); if (codeFlag == valid) { if (label[lab] != undefinedLabel) { sprintf(loadMessage, "L%d REDEFINED", lab); codeFlag = badLabel; } else { label[lab] = codeLine; } } break; } case tANDS: case tORS: case tNOTS: break; case tCMPEQS: case tCMPGES: case tCMPGTS: case tCMPLES: case tCMPLTS: case tCMPNES: case tCMPEQSF: case tCMPGESF: case tCMPGTSF: case tCMPLESF: case tCMPLTSF: case tCMPNESF: break; case tBRTS: case tBRFS: loadLabel(currInstr, 0); break; case tBR : loadLabel(currInstr, 0); break; case tBEQ : case tBGE : case tBGT : case tBLE : case tBLT : case tBNE : case tBEQF : case tBGEF : case tBGTF : case tBLEF : case tBLTF : case tBNEF : loadSource(currInstr, 0); loadSource(currInstr, 1); loadLabel (currInstr, 2); break; case tCALL: loadLabel(currInstr, 0); break; case tRET : break; case tEOF : break; case tPRTS: case tPRTR: break; default: perror(">>> INTERNAL ERROR IN LOAD_uCODE <<<"); exit(1); } loadEnd(); } /* Print an error message if an error occured */ if (codeFlag != valid) { loadError(loadText, loadLine, loadMessage); switch (codeFlag) { case invalidOpcode: break; case invalidForm: printCorrectForm(currInstr->Opcode); break; case invalidRegister: fprintf(stderr, "\n VALID RegisterS RANGE FROM 0 to "); fprintf(stderr, "%d", registerCount); fprintf(stderr, "\n"); break; case invalidLabel: fprintf(stderr, "\n VALID LABELS RANGE FROM 0 to "); fprintf(stderr, "%d", labelCount); fprintf(stderr, "\n"); break; case badLabel: case invalidString: break; default: perror(">>> INTERNAL ERROR IN uLOAD <<<"); exit(1); } } /* Process debugging information and update counters */ fillInformation(currInstr, loadText, loadLine); loadText[0] = '\0'; loadLine++; codeLine++; if (codeLine >= codeSize) { fprintf(stderr, ">>> CODE MEMORY OVERFLOW <<<\n"); exit(1); } } while (yytok != tEOF); /* Resolve labels if source was loaded successfully */ if (loadFlag == valid) { for (i = 0; i < labelCount; i++) { lab = label[i]; while ((lab < 0) && (lab != undefinedLabel)) { label[i] = label[-label[i]]; lab = label[i]; } } } return (loadFlag == valid) ?1 :0; }